jsql-neo 6.7.7 → 6.8.0

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/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  > **One engine to rule them all** — a Rust-powered embedded database that speaks your language:
4
4
  > MySQL, PostgreSQL, MongoDB, Redis, SQL, TypeScript, **and the browser**.
5
5
 
6
- > **v6.7.7** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
6
+ > **v6.8.0** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
7
7
 
8
8
  ![Engines](https://img.shields.io/badge/engines-Native%20%7C%20WASM%20%7C%20Pure%20JS-7ee787)
9
9
  ![MySQL](https://img.shields.io/badge/protocol-MySQL%20compatible-1f6feb)
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * JSQL-NEO v6.7.7 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.8.0 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
package/lib/sql.js CHANGED
@@ -3459,16 +3459,27 @@ function isBinaryValue(v) { return v instanceof BinaryValue; }
3459
3459
  * Issue #111:按 Unicode code point 定位子串,返回 1-based 字符位置;
3460
3460
  * 未找到返回 0(与 MySQL LOCATE/INSTR 一致)。空子串返回 1。
3461
3461
  * 用 Array.from 把代理对拆成单个 code point,避免 emoji 被当成两个字符。
3462
+ *
3463
+ * Issue #116:新增 startPos(1-based,含起点),从该位置开始向右扫描,
3464
+ * 返回的仍是**绝对**字符位置(不是相对偏移),与 MySQL LOCATE(substr,str,pos) 一致:
3465
+ * LOCATE('bar','foobarbar',5) → 7
3466
+ * LOCATE('b','abcabc',3) → 5
3467
+ * LOCATE('b','abcabc',6) → 0
3468
+ * pos < 1 按 1 处理;pos 超出串长时只有空子串还可能命中(pos === len+1)。
3462
3469
  */
3463
- function codePointIndexOf(needle, haystack) {
3470
+ function codePointIndexOf(needle, haystack, startPos = 1) {
3464
3471
  const hay = Array.from(String(haystack));
3465
3472
  const pat = Array.from(String(needle));
3466
- if (pat.length === 0) return 1;
3467
- outer: for (let i = 0; i + pat.length <= hay.length; i++) {
3473
+ let pos = Math.trunc(Number(startPos));
3474
+ if (!Number.isFinite(pos) || pos < 1) pos = 1;
3475
+ if (pat.length === 0) return pos <= hay.length + 1 ? pos : 0;
3476
+ if (pos > hay.length) return 0;
3477
+ for (let i = pos - 1; i + pat.length <= hay.length; i++) {
3478
+ let hit = true;
3468
3479
  for (let j = 0; j < pat.length; j++) {
3469
- if (hay[i + j] !== pat[j]) continue outer;
3480
+ if (hay[i + j] !== pat[j]) { hit = false; break; }
3470
3481
  }
3471
- return i + 1;
3482
+ if (hit) return i + 1;
3472
3483
  }
3473
3484
  return 0;
3474
3485
  }
@@ -4732,6 +4743,34 @@ function applyScalarFunction(fnNode, row, ctx) {
4732
4743
  case 'MOD': return (args[0] == null || args[1] === 0) ? null : args[0] % args[1];
4733
4744
  case 'POWER': case 'POW': return (args[0] == null || args[1] == null) ? null : finiteOrNull(Math.pow(args[0], args[1]));
4734
4745
  case 'SQRT': return args[0] == null ? null : finiteOrNull(Math.sqrt(args[0]));
4746
+ // #117:这些函数在 MATH_SCALAR_FNS 里声明了,却没有 case 分支,落到
4747
+ // default 静默返回 NULL。定义域外(LOG(0)、ASIN(2)、COT(0)…)按 MySQL
4748
+ // 返回 NULL,统一走 finiteOrNull。
4749
+ case 'LOG': { // LOG(X) = ln(X);LOG(B, X) = 以 B 为底
4750
+ if (args[0] == null) return null;
4751
+ if (args.length < 2) return finiteOrNull(Math.log(args[0]));
4752
+ if (args[1] == null) return null;
4753
+ if (args[0] <= 0 || args[0] === 1 || args[1] <= 0) return null;
4754
+ return finiteOrNull(Math.log(args[1]) / Math.log(args[0]));
4755
+ }
4756
+ case 'LN': return args[0] == null ? null : finiteOrNull(Math.log(args[0]));
4757
+ case 'LOG10': return args[0] == null ? null : finiteOrNull(Math.log10(args[0]));
4758
+ case 'LOG2': return args[0] == null ? null : finiteOrNull(Math.log2(args[0]));
4759
+ case 'EXP': return args[0] == null ? null : finiteOrNull(Math.exp(args[0]));
4760
+ case 'SIN': return args[0] == null ? null : finiteOrNull(Math.sin(args[0]));
4761
+ case 'COS': return args[0] == null ? null : finiteOrNull(Math.cos(args[0]));
4762
+ case 'TAN': return args[0] == null ? null : finiteOrNull(Math.tan(args[0]));
4763
+ case 'COT': return args[0] == null ? null : finiteOrNull(1 / Math.tan(args[0]));
4764
+ case 'ASIN': return args[0] == null ? null : finiteOrNull(Math.asin(args[0]));
4765
+ case 'ACOS': return args[0] == null ? null : finiteOrNull(Math.acos(args[0]));
4766
+ case 'ATAN': { // ATAN(X);ATAN(Y, X) 等同 ATAN2(Y, X)
4767
+ if (args[0] == null) return null;
4768
+ if (args.length < 2) return finiteOrNull(Math.atan(args[0]));
4769
+ return args[1] == null ? null : finiteOrNull(Math.atan2(args[0], args[1]));
4770
+ }
4771
+ case 'ATAN2': return (args[0] == null || args[1] == null) ? null : finiteOrNull(Math.atan2(args[0], args[1]));
4772
+ case 'DEGREES': return args[0] == null ? null : finiteOrNull(args[0] * 180 / Math.PI);
4773
+ case 'RADIANS': return args[0] == null ? null : finiteOrNull(args[0] * Math.PI / 180);
4735
4774
  case 'IFNULL': case 'NVL': return args[0] != null ? args[0] : args[1];
4736
4775
  case 'COALESCE': {
4737
4776
  // 不能直接 return find(...):全都取不到值时 find 返回 undefined,
@@ -4785,8 +4824,13 @@ function applyScalarFunction(fnNode, row, ctx) {
4785
4824
  }
4786
4825
  case 'LOCATE': {
4787
4826
  if (args[0] == null || args[1] == null) return null;
4827
+ // Issue #116:三参形式 LOCATE(substr, str, pos) 必须从 pos 开始(含 pos)扫描,
4828
+ // 返回绝对 1-based 字符位置;此前 pos 被静默忽略,导致结果错误。
4829
+ // 任一参数为 NULL 时返回 NULL(与 MySQL 一致)。
4830
+ const startPos = args.length > 2 ? args[2] : 1;
4831
+ if (startPos == null) return null;
4788
4832
  // Issue #111:按 code point 定位,返回**字符**位置(不是 UTF-16 code unit 下标)
4789
- return codePointIndexOf(String(args[0]), String(args[1]));
4833
+ return codePointIndexOf(String(args[0]), String(args[1]), startPos);
4790
4834
  }
4791
4835
  case 'INSTR': {
4792
4836
  // INSTR(str, substr):参数顺序与 LOCATE(substr, str) 相反。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "6.7.7",
3
+ "version": "6.8.0",
4
4
  "description": "JSQL-NEO — Rust-powered embedded database with WASM, REST API, B-Tree indexes, WAL, crash recovery",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -52,12 +52,12 @@
52
52
  "build:native": "node scripts/build-native.js",
53
53
  "check:release": "node scripts/check-release-metadata.js",
54
54
  "release:prep": "node scripts/release.js",
55
- "test": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/native.test.js && node test/json-functions.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/query-opt.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/dml-constraints.test.js && node test/star-nag.test.js && node test/plugin-xql.test.js && node test/bug-regression-2.test.js && node test/issue-34-35-36.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js",
55
+ "test": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/native.test.js && node test/json-functions.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/query-opt.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/dml-constraints.test.js && node test/star-nag.test.js && node test/plugin-xql.test.js && node test/bug-regression-2.test.js && node test/issue-34-35-36.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/issue-117.test.js",
56
56
  "test:parser": "node test/sql-parser.test.js",
57
57
  "test:btree": "node test/btree.test.js",
58
58
  "test:native": "node test/native.test.js",
59
59
  "test:regress": "node test/regress-5.1.0.js",
60
- "test:all": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/native.test.js && node test/native-multi-instance.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/wasm.test.js && node test/coverage.js && node test/regress-5.1.0.js && node test/join.test.js && node test/btree.test.js && node test/dml-constraints.test.js && node test/savepoint.test.js && node test/alter-table.test.js && node test/null-semantics.test.js && node test/identifier-keywords.test.js && node test/issue-34-35-36.test.js && node test/sql-semantics.test.js && node test/extra-coverage.test.js && node test/filter-compile.test.js && node test/recursive-cte.test.js && node test/index-ddl.test.js && node test/mongo-group.test.js && node test/multiprotocol.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js",
60
+ "test:all": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/native.test.js && node test/native-multi-instance.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/wasm.test.js && node test/coverage.js && node test/regress-5.1.0.js && node test/join.test.js && node test/btree.test.js && node test/dml-constraints.test.js && node test/savepoint.test.js && node test/alter-table.test.js && node test/null-semantics.test.js && node test/identifier-keywords.test.js && node test/issue-34-35-36.test.js && node test/sql-semantics.test.js && node test/extra-coverage.test.js && node test/filter-compile.test.js && node test/recursive-cte.test.js && node test/index-ddl.test.js && node test/mongo-group.test.js && node test/multiprotocol.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/issue-117.test.js",
61
61
  "test:coverage": "c8 --all --include='lib/**' --exclude='lib/web_ui.js' --reporter=text --reporter=lcov node test/coverage.js",
62
62
  "test:orms": "node examples/orms/run-all.js",
63
63
  "test:dml": "node test/dml-constraints.test.js",
@@ -0,0 +1,82 @@
1
+ /* ---- Issue #116:LOCATE(substr, str, pos) 必须尊重第 3 个参数 pos ---- */
2
+ /*
3
+ * MySQL 8.0 语义(LOCATE(substr, str, pos)):
4
+ * - 从位置 pos 开始(**含** pos)向右找 substr;
5
+ * - 返回 substr 首次出现的**绝对** 1-based 字符位置(不是相对偏移);
6
+ * - 找不到返回 0;任一参数为 NULL 返回 NULL;
7
+ * - pos < 1 按 1 处理。
8
+ *
9
+ * 此前 `applyScalarFunction` 的 LOCATE 分支只把 args[0]/args[1] 交给
10
+ * codePointIndexOf(needle, haystack),args[2] 从未被读取 —— 三参形式
11
+ * 静默退化成两参形式,返回一个"看起来合理"但错误的位置。
12
+ *
13
+ * 参考值:
14
+ * LOCATE('b','abcabc',1) = 2 LOCATE('bar','foobarbar',5) = 7
15
+ * LOCATE('b','abcabc',3) = 5 LOCATE('b','abcabc',6) = 0
16
+ *
17
+ * node test/issue-116.test.js
18
+ */
19
+ const path = require('path');
20
+ const ROOT = path.join(__dirname, '..');
21
+ const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
22
+
23
+ let pass = 0, fail = 0;
24
+ function ok(name, cond, got) {
25
+ if (cond) { pass++; console.log('[OK] ' + name); }
26
+ else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
27
+ }
28
+
29
+ (async () => {
30
+ const db = new Database(':memory:', { autoSave: false });
31
+ const q = async (sql) => {
32
+ const r = await executeSQL(db, sql, { safety: false });
33
+ return r.rows[0][0];
34
+ };
35
+
36
+ // ---------- issue 的差分表:pos 必须生效 ----------
37
+ ok("LOCATE('b','abcabc') = 2", await q("SELECT LOCATE('b','abcabc')") === 2, await q("SELECT LOCATE('b','abcabc')"));
38
+ ok("LOCATE('b','abcabc',1) = 2", await q("SELECT LOCATE('b','abcabc',1)") === 2, await q("SELECT LOCATE('b','abcabc',1)"));
39
+ ok("LOCATE('b','abcabc',3) = 5", await q("SELECT LOCATE('b','abcabc',3)") === 5, await q("SELECT LOCATE('b','abcabc',3)"));
40
+ ok("LOCATE('b','abcabc',5) = 5", await q("SELECT LOCATE('b','abcabc',5)") === 5, await q("SELECT LOCATE('b','abcabc',5)"));
41
+ ok("LOCATE('b','abcabc',6) = 0", await q("SELECT LOCATE('b','abcabc',6)") === 0, await q("SELECT LOCATE('b','abcabc',6)"));
42
+ ok("LOCATE('bar','foobarbar',5) = 7", await q("SELECT LOCATE('bar','foobarbar',5)") === 7, await q("SELECT LOCATE('bar','foobarbar',5)"));
43
+
44
+ // 返回的是绝对位置,不是相对偏移(若实现成相对偏移这里会得到 2/10)
45
+ ok("LOCATE('bar','foobarbar',4) = 4", await q("SELECT LOCATE('bar','foobarbar',4)") === 4, await q("SELECT LOCATE('bar','foobarbar',4)"));
46
+ ok("LOCATE('bar','foobarbar',7) = 7", await q("SELECT LOCATE('bar','foobarbar',7)") === 7, await q("SELECT LOCATE('bar','foobarbar',7)"));
47
+ ok("LOCATE('bar','foobarbar',8) = 0(剩 'ar' 不够长)", await q("SELECT LOCATE('bar','foobarbar',8)") === 0, await q("SELECT LOCATE('bar','foobarbar',8)"));
48
+
49
+ // ---------- 边界 ----------
50
+ ok("LOCATE('b','abcabc',0) = 2(pos<1 按 1)", await q("SELECT LOCATE('b','abcabc',0)") === 2, await q("SELECT LOCATE('b','abcabc',0)"));
51
+ ok("LOCATE('b','abcabc',-5) = 2(pos<1 按 1)", await q("SELECT LOCATE('b','abcabc',-5)") === 2, await q("SELECT LOCATE('b','abcabc',-5)"));
52
+ ok("LOCATE('b','abcabc',7) = 0(pos>len)", await q("SELECT LOCATE('b','abcabc',7)") === 0, await q("SELECT LOCATE('b','abcabc',7)"));
53
+ ok("LOCATE('b','abcabc',99) = 0", await q("SELECT LOCATE('b','abcabc',99)") === 0, await q("SELECT LOCATE('b','abcabc',99)"));
54
+ ok("LOCATE('b','abcabc',3.9) = 5(pos 取整)", await q("SELECT LOCATE('b','abcabc',3.9)") === 5, await q("SELECT LOCATE('b','abcabc',3.9)"));
55
+
56
+ // 空子串:两参形式仍为 1(行为不变);三参形式为 pos(MySQL 同)
57
+ ok("LOCATE('','abc') = 1", await q("SELECT LOCATE('','abc')") === 1, await q("SELECT LOCATE('','abc')"));
58
+ ok("LOCATE('','abc',3) = 3", await q("SELECT LOCATE('','abc',3)") === 3, await q("SELECT LOCATE('','abc',3)"));
59
+
60
+ // ---------- NULL 传播 ----------
61
+ ok("LOCATE(NULL,'abc',2) IS NULL", await q("SELECT LOCATE(NULL,'abc',2)") === null, await q("SELECT LOCATE(NULL,'abc',2)"));
62
+ ok("LOCATE('b',NULL,2) IS NULL", await q("SELECT LOCATE('b',NULL,2)") === null, await q("SELECT LOCATE('b',NULL,2)"));
63
+ ok("LOCATE('b','abcabc',NULL) IS NULL", await q("SELECT LOCATE('b','abcabc',NULL)") === null, await q("SELECT LOCATE('b','abcabc',NULL)"));
64
+
65
+ // ---------- Unicode:pos 与返回值都按 code point 计(Issue #111 语义)----------
66
+ // 'a🚀b🚀' = a 🚀 b 🚀 字符位置:1 2 3 4
67
+ ok("LOCATE('b','a🚀b🚀',1) = 3", await q("SELECT LOCATE('b','a🚀b🚀',1)") === 3, await q("SELECT LOCATE('b','a🚀b🚀',1)"));
68
+ ok("LOCATE('🚀','a🚀b🚀',2) = 2", await q("SELECT LOCATE('🚀','a🚀b🚀',2)") === 2, await q("SELECT LOCATE('🚀','a🚀b🚀',2)"));
69
+ ok("LOCATE('🚀','a🚀b🚀',3) = 4", await q("SELECT LOCATE('🚀','a🚀b🚀',3)") === 4, await q("SELECT LOCATE('🚀','a🚀b🚀',3)"));
70
+ ok("LOCATE('🚀','a🚀b🚀',5) = 0", await q("SELECT LOCATE('🚀','a🚀b🚀',5)") === 0, await q("SELECT LOCATE('🚀','a🚀b🚀',5)"));
71
+
72
+ // ---------- 两参形式与 INSTR 不受影响 ----------
73
+ ok("LOCATE('bar','foobarbar') = 4", await q("SELECT LOCATE('bar','foobarbar')") === 4, await q("SELECT LOCATE('bar','foobarbar')"));
74
+ ok("INSTR('abcabc','b') = 2", await q("SELECT INSTR('abcabc','b')") === 2, await q("SELECT INSTR('abcabc','b')"));
75
+ ok("INSTR('foobarbar','bar') = 4", await q("SELECT INSTR('foobarbar','bar')") === 4, await q("SELECT INSTR('foobarbar','bar')"));
76
+
77
+ console.log(`\n${pass} passed, ${fail} failed`);
78
+ process.exit(fail === 0 ? 0 : 1);
79
+ })().catch(e => {
80
+ console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
81
+ process.exit(1);
82
+ });
@@ -0,0 +1,102 @@
1
+ /* ---- Issue #117:MATH_SCALAR_FNS 里声明的 15 个数学函数必须真正求值 ---- */
2
+ /*
3
+ * LOG / LOG10 / LOG2 / LN / EXP / SIN / COS / TAN / ASIN / ACOS / ATAN /
4
+ * ATAN2 / DEGREES / RADIANS / COT 此前在 applyScalarFunction 里没有 case
5
+ * 分支,落到 default 静默返回 NULL。
6
+ *
7
+ * MySQL 8.0 语义:
8
+ * - 任一参数为 NULL → NULL;
9
+ * - 定义域外(LOG(0)、LN(-1)、ASIN(2)、LOG(1, x)…)→ NULL;
10
+ * - LOG(X) 为自然对数,LOG(B, X) 为以 B 为底;ATAN(Y, X) 等同 ATAN2(Y, X)。
11
+ *
12
+ * node test/issue-117.test.js
13
+ */
14
+ const path = require('path');
15
+ const ROOT = path.join(__dirname, '..');
16
+ const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
17
+
18
+ let pass = 0, fail = 0;
19
+ function ok(name, cond, got) {
20
+ if (cond) { pass++; console.log('[OK] ' + name); }
21
+ else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
22
+ }
23
+ const near = (a, b) => typeof a === 'number' && Math.abs(a - b) < 1e-12;
24
+
25
+ (async () => {
26
+ const db = new Database(':memory:', { autoSave: false });
27
+ const q = async (sql) => {
28
+ const r = await executeSQL(db, sql, { safety: false });
29
+ return r.rows[0][0];
30
+ };
31
+ const eq = async (expr, want) => {
32
+ const got = await q('SELECT ' + expr);
33
+ ok(`${expr} = ${want}`, want === null ? got === null : near(got, want), got);
34
+ };
35
+
36
+ // ---------- issue 里的复现语句 ----------
37
+ {
38
+ const r = await executeSQL(db,
39
+ 'SELECT LOG2(8), LOG10(100), LN(1), EXP(0), SIN(0), COS(0), TAN(0), DEGREES(3.141592653589793), RADIANS(180)',
40
+ { safety: false });
41
+ const want = [3, 2, 0, 1, 0, 1, 0, 180, Math.PI];
42
+ ok('issue 复现语句', r.rows[0].length === want.length && r.rows[0].every((v, i) => near(v, want[i])), r.rows[0]);
43
+ }
44
+
45
+ // ---------- 每个函数的取值 ----------
46
+ await eq('LOG(EXP(1))', 1);
47
+ await eq('LOG(2, 8)', 3);
48
+ await eq('LOG(10, 1000)', 3);
49
+ await eq('LN(EXP(2))', 2);
50
+ await eq('LOG10(1000)', 3);
51
+ await eq('LOG2(1024)', 10);
52
+ await eq('EXP(1)', Math.E);
53
+ await eq('SIN(1.5707963267948966)', 1);
54
+ await eq('COS(3.141592653589793)', -1);
55
+ await eq('TAN(0.7853981633974483)', Math.tan(Math.PI / 4));
56
+ await eq('COT(1)', 1 / Math.tan(1));
57
+ await eq('ASIN(1)', Math.PI / 2);
58
+ await eq('ACOS(1)', 0);
59
+ await eq('ATAN(1)', Math.PI / 4);
60
+ await eq('ATAN(1, 1)', Math.PI / 4);
61
+ await eq('ATAN(-1, -1)', Math.atan2(-1, -1));
62
+ await eq('ATAN2(1, 1)', Math.PI / 4);
63
+ await eq('ATAN2(1, 0)', Math.PI / 2);
64
+ await eq('DEGREES(3.141592653589793)', 180);
65
+ await eq('RADIANS(90)', Math.PI / 2);
66
+ await eq("EXP('0')", 1);
67
+
68
+ // ---------- 定义域外 → NULL(不能漏出 NaN / Infinity)----------
69
+ await eq('LOG(0)', null);
70
+ await eq('LN(-1)', null);
71
+ await eq('LOG10(0)', null);
72
+ await eq('LOG2(-8)', null);
73
+ await eq('LOG(1, 8)', null);
74
+ await eq('LOG(-2, 8)', null);
75
+ await eq('LOG(2, 0)', null);
76
+ await eq('ASIN(2)', null);
77
+ await eq('ACOS(-1.5)', null);
78
+ await eq('COT(0)', null);
79
+ await eq('EXP(1000)', null);
80
+
81
+ // ---------- NULL 传播 ----------
82
+ for (const expr of ['LOG(NULL)', 'LOG(NULL, 8)', 'LOG(2, NULL)', 'LN(NULL)', 'LOG10(NULL)', 'LOG2(NULL)',
83
+ 'EXP(NULL)', 'SIN(NULL)', 'COS(NULL)', 'TAN(NULL)', 'COT(NULL)', 'ASIN(NULL)', 'ACOS(NULL)',
84
+ 'ATAN(NULL)', 'ATAN(1, NULL)', 'ATAN2(NULL, 1)', 'ATAN2(1, NULL)', 'DEGREES(NULL)', 'RADIANS(NULL)']) {
85
+ await eq(expr, null);
86
+ }
87
+
88
+ // ---------- BigInt 入参降级为 number(issue #38 的路径)----------
89
+ await eq('LOG2(9007199254740993)', 53);
90
+
91
+ // ---------- 用在表数据上 ----------
92
+ await executeSQL(db, 'CREATE TABLE m (id INT, x DOUBLE)', { safety: false });
93
+ await executeSQL(db, 'INSERT INTO m VALUES (1, 100), (2, 0), (3, NULL)', { safety: false });
94
+ const rows = (await executeSQL(db, 'SELECT id, LOG10(x) AS l FROM m ORDER BY id', { safety: false })).rows;
95
+ ok('LOG10 列计算:[2, NULL, NULL]', JSON.stringify(rows.map(r => r[1])) === '[2,null,null]', rows);
96
+
97
+ console.log(`\n${pass} passed, ${fail} failed`);
98
+ process.exit(fail === 0 ? 0 : 1);
99
+ })().catch(e => {
100
+ console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
101
+ process.exit(1);
102
+ });