@atscript/db-mysql 0.1.182 → 0.1.183

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2901,9 +2901,10 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
2901
2901
  async vectorSearch(vector, query, indexName) {
2902
2902
  await this._detectVectorSupport();
2903
2903
  if (!this._supportsVector) throw new Error("Vector search requires MySQL 9.0+");
2904
- const { sql, params } = this._buildVectorSearchQuery(this._prepareVectorSearch(vector, query, indexName));
2904
+ const ctx = this._prepareVectorSearch(vector, query, indexName);
2905
+ const { sql, params } = this._buildVectorSearchQuery(ctx);
2905
2906
  this._log(sql, params);
2906
- return this._exec().all(sql, params);
2907
+ return (await this._exec().all(sql, params)).map((row) => (0, _atscript_db_sql_tools.addVectorScore)(row, ctx.metric));
2907
2908
  }
2908
2909
  async vectorSearchWithCount(vector, query, indexName) {
2909
2910
  await this._detectVectorSupport();
@@ -2915,7 +2916,7 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
2915
2916
  this._log(countSql, countParams);
2916
2917
  const [data, countRow] = await Promise.all([this._exec().all(sql, params), this._exec().get(countSql, countParams)]);
2917
2918
  return {
2918
- data,
2919
+ data: data.map((row) => (0, _atscript_db_sql_tools.addVectorScore)(row, ctx.metric)),
2919
2920
  count: countRow?.cnt ?? 0
2920
2921
  };
2921
2922
  }
@@ -2938,14 +2939,19 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
2938
2939
  field = first.value[0];
2939
2940
  vec = first.value[1];
2940
2941
  }
2941
- const distanceFn = similarityToMysqlFn(vec.similarity);
2942
+ if (vec.similarity === "dotProduct") throw new _atscript_db.DbError("INVALID_QUERY", [{
2943
+ path: vec.indexName,
2944
+ message: `Vector index "${vec.indexName}" uses "dotProduct", which the MySQL adapter cannot search — use "cosine" (the same order for normalized vectors) or "euclidean"`
2945
+ }]);
2946
+ const metric = vec.similarity === "euclidean" ? "euclidean" : "cosine";
2942
2947
  const where = buildWhere(query.filter, { qualifier: mysqlDialect.quoteTable(_atscript_db_sql_tools.SEARCH_SOURCE_ALIAS) });
2943
2948
  const controls = query.controls || {};
2944
2949
  const threshold = this._resolveVectorThreshold(controls, vec.indexName);
2945
2950
  return {
2946
2951
  field,
2947
2952
  vec,
2948
- distanceFn,
2953
+ distanceFn: metric === "euclidean" ? "VEC_DISTANCE_EUCLIDEAN" : "VEC_DISTANCE_COSINE",
2954
+ metric,
2949
2955
  where,
2950
2956
  controls,
2951
2957
  threshold,
@@ -2954,10 +2960,8 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
2954
2960
  };
2955
2961
  }
2956
2962
  /**
2957
- * The row source + distance cap of a vector search. The threshold is a
2958
- * normalized score matching MongoDB Atlas semantics: cosine score =
2959
- * (1 + cos_sim) / 2, VEC_DISTANCE_COSINE = 1 - cos_sim, so the distance cap
2960
- * is 2 * (1 - score).
2963
+ * The row source + distance cap of a vector search: the threshold is a
2964
+ * `$score` (MongoDB Atlas scale), converted to the distance function's scale.
2961
2965
  */
2962
2966
  _vectorSearchSource(ctx, withRows) {
2963
2967
  const distExpr = {
@@ -2966,7 +2970,7 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
2966
2970
  };
2967
2971
  return {
2968
2972
  source: (0, _atscript_db_sql_tools.vectorDistanceSource)(mysqlDialect, ctx.tableName, ctx.where, distExpr, withRows),
2969
- maxDistance: ctx.threshold === void 0 ? void 0 : 2 * (1 - ctx.threshold)
2973
+ maxDistance: ctx.threshold === void 0 ? void 0 : (0, _atscript_db_sql_tools.vectorMaxDistance)(ctx.metric, ctx.threshold)
2970
2974
  };
2971
2975
  }
2972
2976
  _buildVectorSearchQuery(ctx) {
@@ -3130,14 +3134,6 @@ const MYSQL_ESCAPES = {
3130
3134
  function unescapeMysqlString(body) {
3131
3135
  return body.replace(/\\([\s\S])|''/g, (match, ch) => ch === void 0 ? "'" : MYSQL_ESCAPES[ch] ?? ch);
3132
3136
  }
3133
- /** Maps generic similarity metric to MySQL 9+ distance function name. */
3134
- function similarityToMysqlFn(similarity) {
3135
- switch (similarity) {
3136
- case "euclidean": return "VEC_DISTANCE_EUCLIDEAN";
3137
- case "dotProduct": return "VEC_DISTANCE_DOT";
3138
- default: return "VEC_DISTANCE_COSINE";
3139
- }
3140
- }
3141
3137
  /** Formats a number[] vector as MySQL's STRING_TO_VECTOR input: '[1.0, 2.0, ...]'. */
3142
3138
  function vectorToString(vector) {
3143
3139
  return `[${vector.join(",")}]`;
package/dist/index.d.cts CHANGED
@@ -611,10 +611,8 @@ declare class MysqlAdapter extends BaseDbAdapter {
611
611
  /** Resolves vector field and computes shared context for vector search SQL builders. */
612
612
  private _prepareVectorSearch;
613
613
  /**
614
- * The row source + distance cap of a vector search. The threshold is a
615
- * normalized score matching MongoDB Atlas semantics: cosine score =
616
- * (1 + cos_sim) / 2, VEC_DISTANCE_COSINE = 1 - cos_sim, so the distance cap
617
- * is 2 * (1 - score).
614
+ * The row source + distance cap of a vector search: the threshold is a
615
+ * `$score` (MongoDB Atlas scale), converted to the distance function's scale.
618
616
  */
619
617
  private _vectorSearchSource;
620
618
  private _buildVectorSearchQuery;
package/dist/index.d.mts CHANGED
@@ -611,10 +611,8 @@ declare class MysqlAdapter extends BaseDbAdapter {
611
611
  /** Resolves vector field and computes shared context for vector search SQL builders. */
612
612
  private _prepareVectorSearch;
613
613
  /**
614
- * The row source + distance cap of a vector search. The threshold is a
615
- * normalized score matching MongoDB Atlas semantics: cosine score =
616
- * (1 + cos_sim) / 2, VEC_DISTANCE_COSINE = 1 - cos_sim, so the distance cap
617
- * is 2 * (1 - score).
614
+ * The row source + distance cap of a vector search: the threshold is a
615
+ * `$score` (MongoDB Atlas scale), converted to the distance function's scale.
618
616
  */
619
617
  private _vectorSearchSource;
620
618
  private _buildVectorSearchQuery;
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { i as mysqlOnUpdateExpr, r as mysqlName } from "./annotation-values-CsjIErsL.mjs";
2
2
  import { ALL_AGGREGATE_FNS, ALL_BUCKET_UNITS, ALL_ISOLATION_LEVELS, ALL_VIEW_CAPABILITIES, BaseDbAdapter, DbError, DbSpace, NoopLogger, bucketTimeZoneUnavailable, constraintViolation, containsRelationFilter, defaultFulltextIndex, describeFulltext, fkKey, forEachResolvedRelation, isConflict, resolveFulltextIndex, searchIndexNotFoundMessage, searchTermInteger, splitFulltextFields, uniqueKeyTuple, vectorIndexNotFoundMessage } from "@atscript/db";
3
3
  import { resolveAggregateSearch } from "@atscript/db/agg";
4
- import { EMPTY_OR, SEARCH_SOURCE_ALIAS, arithOverflowError, buildAggregateCount, buildAggregateSelect, buildCreateView, buildDelete, buildGeoSearchCount, buildGeoSearchSelect, buildInsert, buildInsertMany, buildJsonColumnCopy, buildJsonifyText, buildKeyViolationCount, buildPartitionedSelect, buildSelect, buildUpdate, buildVectorSearchCount, buildVectorSearchSelect, buildWhere as buildWhere$1, chunkInsertRows, defaultValueForType, defaultValueToSqlLiteral, derivedColumnExpr, fillReplacePayload, foreignKeySql, fromSourceHint, fromSourceSql, geoWindowFromControls, jsonDollarPath, mapQueryErrors, normalizeGeoPointValue, numericOutOfRangeError, orFragment, orderByList, parseRegexString, renameGeoDistance, replaceColumnsFor, searchRankOrder, sqlTimeZoneLiteral, stripPartitionRowNumber, toSqlValue, unquoteSqlStringLiteral, vectorDistanceSource, viewReadSource, writeLimitCount } from "@atscript/db-sql-tools";
4
+ import { EMPTY_OR, SEARCH_SOURCE_ALIAS, addVectorScore, arithOverflowError, buildAggregateCount, buildAggregateSelect, buildCreateView, buildDelete, buildGeoSearchCount, buildGeoSearchSelect, buildInsert, buildInsertMany, buildJsonColumnCopy, buildJsonifyText, buildKeyViolationCount, buildPartitionedSelect, buildSelect, buildUpdate, buildVectorSearchCount, buildVectorSearchSelect, buildWhere as buildWhere$1, chunkInsertRows, defaultValueForType, defaultValueToSqlLiteral, derivedColumnExpr, fillReplacePayload, foreignKeySql, fromSourceHint, fromSourceSql, geoWindowFromControls, jsonDollarPath, mapQueryErrors, normalizeGeoPointValue, numericOutOfRangeError, orFragment, orderByList, parseRegexString, renameGeoDistance, replaceColumnsFor, searchRankOrder, sqlTimeZoneLiteral, stripPartitionRowNumber, toSqlValue, unquoteSqlStringLiteral, vectorDistanceSource, vectorMaxDistance, viewReadSource, writeLimitCount } from "@atscript/db-sql-tools";
5
5
  import { BUCKET_MAX_INSTANT, BUCKET_MIN_INSTANT } from "@uniqu/core";
6
6
  //#region src/sql-builder.ts
7
7
  /**
@@ -2900,9 +2900,10 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
2900
2900
  async vectorSearch(vector, query, indexName) {
2901
2901
  await this._detectVectorSupport();
2902
2902
  if (!this._supportsVector) throw new Error("Vector search requires MySQL 9.0+");
2903
- const { sql, params } = this._buildVectorSearchQuery(this._prepareVectorSearch(vector, query, indexName));
2903
+ const ctx = this._prepareVectorSearch(vector, query, indexName);
2904
+ const { sql, params } = this._buildVectorSearchQuery(ctx);
2904
2905
  this._log(sql, params);
2905
- return this._exec().all(sql, params);
2906
+ return (await this._exec().all(sql, params)).map((row) => addVectorScore(row, ctx.metric));
2906
2907
  }
2907
2908
  async vectorSearchWithCount(vector, query, indexName) {
2908
2909
  await this._detectVectorSupport();
@@ -2914,7 +2915,7 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
2914
2915
  this._log(countSql, countParams);
2915
2916
  const [data, countRow] = await Promise.all([this._exec().all(sql, params), this._exec().get(countSql, countParams)]);
2916
2917
  return {
2917
- data,
2918
+ data: data.map((row) => addVectorScore(row, ctx.metric)),
2918
2919
  count: countRow?.cnt ?? 0
2919
2920
  };
2920
2921
  }
@@ -2937,14 +2938,19 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
2937
2938
  field = first.value[0];
2938
2939
  vec = first.value[1];
2939
2940
  }
2940
- const distanceFn = similarityToMysqlFn(vec.similarity);
2941
+ if (vec.similarity === "dotProduct") throw new DbError("INVALID_QUERY", [{
2942
+ path: vec.indexName,
2943
+ message: `Vector index "${vec.indexName}" uses "dotProduct", which the MySQL adapter cannot search — use "cosine" (the same order for normalized vectors) or "euclidean"`
2944
+ }]);
2945
+ const metric = vec.similarity === "euclidean" ? "euclidean" : "cosine";
2941
2946
  const where = buildWhere(query.filter, { qualifier: mysqlDialect.quoteTable(SEARCH_SOURCE_ALIAS) });
2942
2947
  const controls = query.controls || {};
2943
2948
  const threshold = this._resolveVectorThreshold(controls, vec.indexName);
2944
2949
  return {
2945
2950
  field,
2946
2951
  vec,
2947
- distanceFn,
2952
+ distanceFn: metric === "euclidean" ? "VEC_DISTANCE_EUCLIDEAN" : "VEC_DISTANCE_COSINE",
2953
+ metric,
2948
2954
  where,
2949
2955
  controls,
2950
2956
  threshold,
@@ -2953,10 +2959,8 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
2953
2959
  };
2954
2960
  }
2955
2961
  /**
2956
- * The row source + distance cap of a vector search. The threshold is a
2957
- * normalized score matching MongoDB Atlas semantics: cosine score =
2958
- * (1 + cos_sim) / 2, VEC_DISTANCE_COSINE = 1 - cos_sim, so the distance cap
2959
- * is 2 * (1 - score).
2962
+ * The row source + distance cap of a vector search: the threshold is a
2963
+ * `$score` (MongoDB Atlas scale), converted to the distance function's scale.
2960
2964
  */
2961
2965
  _vectorSearchSource(ctx, withRows) {
2962
2966
  const distExpr = {
@@ -2965,7 +2969,7 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
2965
2969
  };
2966
2970
  return {
2967
2971
  source: vectorDistanceSource(mysqlDialect, ctx.tableName, ctx.where, distExpr, withRows),
2968
- maxDistance: ctx.threshold === void 0 ? void 0 : 2 * (1 - ctx.threshold)
2972
+ maxDistance: ctx.threshold === void 0 ? void 0 : vectorMaxDistance(ctx.metric, ctx.threshold)
2969
2973
  };
2970
2974
  }
2971
2975
  _buildVectorSearchQuery(ctx) {
@@ -3129,14 +3133,6 @@ const MYSQL_ESCAPES = {
3129
3133
  function unescapeMysqlString(body) {
3130
3134
  return body.replace(/\\([\s\S])|''/g, (match, ch) => ch === void 0 ? "'" : MYSQL_ESCAPES[ch] ?? ch);
3131
3135
  }
3132
- /** Maps generic similarity metric to MySQL 9+ distance function name. */
3133
- function similarityToMysqlFn(similarity) {
3134
- switch (similarity) {
3135
- case "euclidean": return "VEC_DISTANCE_EUCLIDEAN";
3136
- case "dotProduct": return "VEC_DISTANCE_DOT";
3137
- default: return "VEC_DISTANCE_COSINE";
3138
- }
3139
- }
3140
3136
  /** Formats a number[] vector as MySQL's STRING_TO_VECTOR input: '[1.0, 2.0, ...]'. */
3141
3137
  function vectorToString(vector) {
3142
3138
  return `[${vector.join(",")}]`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db-mysql",
3
- "version": "0.1.182",
3
+ "version": "0.1.183",
4
4
  "description": "MySQL adapter for @atscript/db with mysql2 driver support.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -41,7 +41,7 @@
41
41
  "access": "public"
42
42
  },
43
43
  "dependencies": {
44
- "@atscript/db-sql-tools": "^0.1.182"
44
+ "@atscript/db-sql-tools": "^0.1.183"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@atscript/core": "^0.2.13",
@@ -55,7 +55,7 @@
55
55
  "@atscript/typescript": "^0.2.13",
56
56
  "@uniqu/core": "^0.2.2",
57
57
  "mysql2": ">=3.0.0",
58
- "@atscript/db": "^0.1.182"
58
+ "@atscript/db": "^0.1.183"
59
59
  },
60
60
  "peerDependenciesMeta": {
61
61
  "mysql2": {