jsql-neo 6.7.0 → 6.7.2
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 +1 -1
- package/index.js +1 -1
- package/lib/sql.js +33 -2
- package/package.json +3 -3
- package/test/cast-rounding.test.js +56 -0
- package/test/convert-subquery.test.js +88 -0
- package/test/sql-parser.test.js +5 -3
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.
|
|
6
|
+
> **v6.7.2** — 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
|

|
|
9
9
|

|
package/index.js
CHANGED
package/lib/sql.js
CHANGED
|
@@ -2705,6 +2705,32 @@ class Parser {
|
|
|
2705
2705
|
this.expect('op', ')');
|
|
2706
2706
|
return { type: 'cast', expr, dataType };
|
|
2707
2707
|
}
|
|
2708
|
+
// CONVERT(expr, type) —— MySQL 的另一种 CAST 写法,语义与 CAST 完全一致,
|
|
2709
|
+
// 直接复用 cast 节点。此前完全没解析,SIGNED / CHAR 被当成列名,
|
|
2710
|
+
// 报 "no such column: SIGNED"。
|
|
2711
|
+
// `CONVERT(expr USING charset)` 是另一套语法,这里不支持(会报语法错,
|
|
2712
|
+
// 比静默当成列引用好)。
|
|
2713
|
+
if (String(t.value).toUpperCase() === 'CONVERT' && this.peek().type === 'op' && this.peek().value === '(') {
|
|
2714
|
+
this.next();
|
|
2715
|
+
const expr = this.parseScalar();
|
|
2716
|
+
this.expect('op', ',');
|
|
2717
|
+
const typeTok = this.next();
|
|
2718
|
+
let dataType = String(typeTok.value).toUpperCase();
|
|
2719
|
+
if (this.peek().type === 'op' && this.peek().value === '(') {
|
|
2720
|
+
this.next();
|
|
2721
|
+
let depth = 1;
|
|
2722
|
+
let spec = '';
|
|
2723
|
+
while (depth > 0 && this.peek().type !== 'eof') {
|
|
2724
|
+
const tk = this.next();
|
|
2725
|
+
if (tk.type === 'op' && tk.value === '(') depth++;
|
|
2726
|
+
else if (tk.type === 'op' && tk.value === ')') { depth--; if (depth === 0) break; }
|
|
2727
|
+
spec += tk.value;
|
|
2728
|
+
}
|
|
2729
|
+
dataType += '(' + spec + ')';
|
|
2730
|
+
}
|
|
2731
|
+
this.expect('op', ')');
|
|
2732
|
+
return { type: 'cast', expr, dataType };
|
|
2733
|
+
}
|
|
2708
2734
|
// 零参时间函数:CURRENT_DATE / CURRENT_TIME / CURRENT_TIMESTAMP(可省略括号)。
|
|
2709
2735
|
// 此前不在 KEYWORDS 里,被当成列名解析 → "no such column: CURRENT_DATE"
|
|
2710
2736
|
if (['CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP'].includes(String(t.value).toUpperCase())) {
|
|
@@ -3252,12 +3278,17 @@ function applyCast(value, dataType) {
|
|
|
3252
3278
|
if (t === 'INTEGER' || t === 'INT' || t === 'BIGINT' || t === 'SMALLINT' ||
|
|
3253
3279
|
t === 'TINYINT' || t === 'SIGNED' || t === 'MEDIUMINT' || t === 'UNSIGNED') {
|
|
3254
3280
|
// MySQL CAST(... AS SIGNED/INT):取数字前缀('12abc'→12),无法解析时返回 0(而非 null)。
|
|
3281
|
+
// MySQL 的 CAST(x AS SIGNED/INT) 对小数是**四舍五入**(半远离零),
|
|
3282
|
+
// 不是截断:CAST(1.9 AS SIGNED) = 2、CAST(-1.5 AS SIGNED) = -2。
|
|
3283
|
+
// 此前用 Math.trunc,1.9 会得到 1。Math.round 是「半向 +∞」,
|
|
3284
|
+
// 负数要先取绝对值再舍入,否则 -1.5 会变成 -1。
|
|
3285
|
+
const roundHalfAway = (x) => (x < 0 ? -Math.round(-x) : Math.round(x));
|
|
3255
3286
|
let n;
|
|
3256
3287
|
if (typeof value === 'boolean') n = value ? 1 : 0;
|
|
3257
|
-
else if (typeof value === 'number') n = Number.isFinite(value) ?
|
|
3288
|
+
else if (typeof value === 'number') n = Number.isFinite(value) ? roundHalfAway(value) : 0;
|
|
3258
3289
|
else {
|
|
3259
3290
|
const m = String(value).trim().match(/^[+-]?\d+(\.\d+)?/);
|
|
3260
|
-
n = m ?
|
|
3291
|
+
n = m ? roundHalfAway(Number(m[0])) : 0;
|
|
3261
3292
|
}
|
|
3262
3293
|
// CAST(... AS UNSIGNED) 是 BIGINT UNSIGNED:负数按 2^64 回绕,
|
|
3263
3294
|
// 而不是原样保留负号。CAST(-5 AS UNSIGNED) 应为 18446744073709551611。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsql-neo",
|
|
3
|
-
"version": "6.7.
|
|
3
|
+
"version": "6.7.2",
|
|
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",
|
|
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",
|
|
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",
|
|
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",
|
|
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,56 @@
|
|
|
1
|
+
/* ---- CAST 到整数:MySQL 是四舍五入,不是截断 ---- */
|
|
2
|
+
/*
|
|
3
|
+
* 差分探针发现 `CAST(1.9 AS SIGNED)` 返回 1,MySQL 8.0 返回 2 —— 小数转
|
|
4
|
+
* 整数是 **四舍五入(半远离零)**,不是截断。此前用 Math.trunc。
|
|
5
|
+
* 注意 Math.round 是「半向 +∞」,负数必须取绝对值再舍入,否则 -1.5 → -1。
|
|
6
|
+
*/
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const ROOT = path.join(__dirname, '..');
|
|
9
|
+
const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
|
|
10
|
+
|
|
11
|
+
let pass = 0, fail = 0;
|
|
12
|
+
function ok(name, cond, got) {
|
|
13
|
+
if (cond) { pass++; console.log('[OK] ' + name); }
|
|
14
|
+
else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
(async () => {
|
|
18
|
+
const db = new Database(':memory:', { autoSave: false });
|
|
19
|
+
const q = async (sql) => (await executeSQL(db, sql, { safety: false })).rows[0][0];
|
|
20
|
+
|
|
21
|
+
// ---------- 正数:四舍五入 ----------
|
|
22
|
+
ok('CAST(1.4 AS SIGNED) = 1', await q('SELECT CAST(1.4 AS SIGNED)') === 1, null);
|
|
23
|
+
ok('CAST(1.5 AS SIGNED) = 2(半远离零)', await q('SELECT CAST(1.5 AS SIGNED)') === 2, null);
|
|
24
|
+
ok('CAST(1.9 AS SIGNED) = 2(此前错为 1)', await q('SELECT CAST(1.9 AS SIGNED)') === 2, null);
|
|
25
|
+
ok('CAST(2.5 AS SIGNED) = 3', await q('SELECT CAST(2.5 AS SIGNED)') === 3, null);
|
|
26
|
+
ok('CAST(2.4 AS SIGNED) = 2', await q('SELECT CAST(2.4 AS SIGNED)') === 2, null);
|
|
27
|
+
|
|
28
|
+
// ---------- 负数:不能变成「半向 +∞」 ----------
|
|
29
|
+
ok('CAST(-1.4 AS SIGNED) = -1', await q('SELECT CAST(-1.4 AS SIGNED)') === -1, null);
|
|
30
|
+
ok('CAST(-1.5 AS SIGNED) = -2(半远离零)', await q('SELECT CAST(-1.5 AS SIGNED)') === -2, null);
|
|
31
|
+
ok('CAST(-1.9 AS SIGNED) = -2', await q('SELECT CAST(-1.9 AS SIGNED)') === -2, null);
|
|
32
|
+
|
|
33
|
+
// ---------- 字符串 ----------
|
|
34
|
+
ok("CAST('1.9' AS SIGNED) = 2", await q("SELECT CAST('1.9' AS SIGNED)") === 2, null);
|
|
35
|
+
ok("CAST('12abc' AS SIGNED) = 12(取数字前缀)", await q("SELECT CAST('12abc' AS SIGNED)") === 12, null);
|
|
36
|
+
ok("CAST('abc' AS SIGNED) = 0", await q("SELECT CAST('abc' AS SIGNED)") === 0, null);
|
|
37
|
+
ok("CAST('-1.5' AS SIGNED) = -2", await q("SELECT CAST('-1.5' AS SIGNED)") === -2, null);
|
|
38
|
+
|
|
39
|
+
// ---------- UNSIGNED / NULL ----------
|
|
40
|
+
ok('CAST(2.5 AS UNSIGNED) = 3', await q('SELECT CAST(2.5 AS UNSIGNED)') === 3, null);
|
|
41
|
+
ok('CAST(-5 AS UNSIGNED) 按 2^64 回绕',
|
|
42
|
+
String(await q('SELECT CAST(-5 AS UNSIGNED)')) === '18446744073709551611', null);
|
|
43
|
+
ok('CAST(NULL AS SIGNED) = NULL', await q('SELECT CAST(NULL AS SIGNED)') === null, null);
|
|
44
|
+
|
|
45
|
+
// ---------- 不回归:DECIMAL 标度(#96)与其它类型 ----------
|
|
46
|
+
ok('CAST(123.456 AS DECIMAL(10,2)) = 123.46',
|
|
47
|
+
await q('SELECT CAST(123.456 AS DECIMAL(10,2))') === 123.46, null);
|
|
48
|
+
ok('CAST(1.9 AS CHAR) 仍是字符串', await q('SELECT CAST(1.9 AS CHAR)') === '1.9', null);
|
|
49
|
+
ok('CAST(42 AS CHAR) 仍是字符串', await q('SELECT CAST(42 AS CHAR)') === '42', null);
|
|
50
|
+
|
|
51
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
52
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
53
|
+
})().catch(e => {
|
|
54
|
+
console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
|
|
55
|
+
process.exit(1);
|
|
56
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/* ---- CONVERT / BINARY / GROUP_CONCAT / HAVING / IN 子查询的 NULL 语义 ---- */
|
|
2
|
+
/*
|
|
3
|
+
* 这一轮差分探针(16 条)只错 2 条,都是 `CONVERT(expr, type)` 没被解析
|
|
4
|
+
* (报 no such column: SIGNED)—— 功能缺失,不是静默错值。补上后连同
|
|
5
|
+
* 同批验证过、但此前没有回归覆盖的边界一起钉住。
|
|
6
|
+
*/
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const ROOT = path.join(__dirname, '..');
|
|
9
|
+
const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
|
|
10
|
+
|
|
11
|
+
let pass = 0, fail = 0;
|
|
12
|
+
function ok(name, cond, got) {
|
|
13
|
+
if (cond) { pass++; console.log('[OK] ' + name); }
|
|
14
|
+
else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
(async () => {
|
|
18
|
+
const db = new Database(':memory:', { autoSave: false });
|
|
19
|
+
await db.createTable('t', {
|
|
20
|
+
id: { type: 'integer', primaryKey: true, autoIncrement: true },
|
|
21
|
+
g: { type: 'string' }, v: { type: 'integer' },
|
|
22
|
+
});
|
|
23
|
+
await db.insert('t', [{ g: 'a', v: 1 }, { g: 'a', v: 2 }, { g: 'b', v: 3 }]);
|
|
24
|
+
await db.createTable('s', {
|
|
25
|
+
id: { type: 'integer', primaryKey: true, autoIncrement: true }, v: { type: 'integer' },
|
|
26
|
+
});
|
|
27
|
+
await db.insert('s', [{ v: 1 }, { v: null }]);
|
|
28
|
+
await db.createTable('empty', {
|
|
29
|
+
id: { type: 'integer', primaryKey: true, autoIncrement: true }, v: { type: 'integer' },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const q = async (sql) => (await executeSQL(db, sql, { safety: false })).rows[0][0];
|
|
33
|
+
|
|
34
|
+
// ---------- CONVERT(expr, type):与 CAST 语义一致 ----------
|
|
35
|
+
ok("CONVERT('12abc', SIGNED) = 12", await q("SELECT CONVERT('12abc', SIGNED)") === 12, null);
|
|
36
|
+
ok('CONVERT(42, CHAR) = "42"', await q('SELECT CONVERT(42, CHAR)') === '42', null);
|
|
37
|
+
ok('CONVERT(1.9, SIGNED) = 2(四舍五入)', await q('SELECT CONVERT(1.9, SIGNED)') === 2, null);
|
|
38
|
+
ok('CONVERT(12.9, INTEGER) = 13', await q('SELECT CONVERT(12.9, INTEGER)') === 13, null);
|
|
39
|
+
ok("CONVERT('3.14159', DECIMAL(10,2)) = 3.14",
|
|
40
|
+
await q("SELECT CONVERT('3.14159', DECIMAL(10,2))") === 3.14, null);
|
|
41
|
+
ok('CONVERT(NULL, SIGNED) = NULL', await q('SELECT CONVERT(NULL, SIGNED)') === null, null);
|
|
42
|
+
ok('CONVERT(-5, UNSIGNED) 按 2^64 回绕',
|
|
43
|
+
String(await q('SELECT CONVERT(-5, UNSIGNED)')) === '18446744073709551611', null);
|
|
44
|
+
ok('CONVERT 与 CAST 结果一致',
|
|
45
|
+
await q('SELECT CONVERT(12.9, SIGNED)') === await q('SELECT CAST(12.9 AS SIGNED)'), null);
|
|
46
|
+
|
|
47
|
+
// ---------- BINARY:大小写敏感 ----------
|
|
48
|
+
ok("BINARY g = 'A' 不命中小写 'a'",
|
|
49
|
+
await q("SELECT COUNT(*) AS c FROM t WHERE BINARY g = 'A'") === 0, null);
|
|
50
|
+
ok("默认 collation 下 g = 'A' 命中 'a'(CI)",
|
|
51
|
+
await q("SELECT COUNT(*) AS c FROM t WHERE g = 'A'") === 2, null);
|
|
52
|
+
|
|
53
|
+
// ---------- GROUP_CONCAT ----------
|
|
54
|
+
ok('GROUP_CONCAT 默认逗号', await q('SELECT GROUP_CONCAT(v) AS x FROM t') === '1,2,3', null);
|
|
55
|
+
ok("GROUP_CONCAT SEPARATOR '|'",
|
|
56
|
+
await q("SELECT GROUP_CONCAT(v SEPARATOR '|') AS x FROM t") === '1|2|3', null);
|
|
57
|
+
ok('GROUP_CONCAT ORDER BY DESC',
|
|
58
|
+
await q('SELECT GROUP_CONCAT(v ORDER BY v DESC) AS x FROM t') === '3,2,1', null);
|
|
59
|
+
ok('GROUP_CONCAT DISTINCT', await q('SELECT GROUP_CONCAT(DISTINCT g) AS x FROM t') === 'a,b', null);
|
|
60
|
+
|
|
61
|
+
// ---------- HAVING:表达式与别名等价 ----------
|
|
62
|
+
const h1 = JSON.stringify((await executeSQL(db, 'SELECT g, SUM(v) AS s FROM t GROUP BY g HAVING SUM(v) > 2', { safety: false })).rows);
|
|
63
|
+
const h2 = JSON.stringify((await executeSQL(db, 'SELECT g, SUM(v) AS s FROM t GROUP BY g HAVING s > 2', { safety: false })).rows);
|
|
64
|
+
ok('HAVING 聚合表达式', h1 === '[["a",3],["b",3]]', h1);
|
|
65
|
+
ok('HAVING 用别名与表达式结果一致', h1 === h2, { h1, h2 });
|
|
66
|
+
|
|
67
|
+
// ---------- IN / NOT IN 子查询的三值逻辑 ----------
|
|
68
|
+
ok('IN 子查询命中', await q('SELECT COUNT(*) AS c FROM t WHERE v IN (SELECT v FROM s)') === 1, null);
|
|
69
|
+
// 关键:子查询含 NULL 时,NOT IN 对任何行都不成立(结果集为空)
|
|
70
|
+
ok('NOT IN 含 NULL → 0 行(三值逻辑)',
|
|
71
|
+
await q('SELECT COUNT(*) AS c FROM t WHERE v NOT IN (SELECT v FROM s)') === 0, null);
|
|
72
|
+
ok('IN 空子查询 → 0 行',
|
|
73
|
+
await q('SELECT COUNT(*) AS c FROM t WHERE v IN (SELECT v FROM empty)') === 0, null);
|
|
74
|
+
ok('NOT IN 空子查询 → 全部行',
|
|
75
|
+
await q('SELECT COUNT(*) AS c FROM t WHERE v NOT IN (SELECT v FROM empty)') === 3, null);
|
|
76
|
+
|
|
77
|
+
// ---------- EXISTS / NOT EXISTS ----------
|
|
78
|
+
ok('EXISTS 相关子查询',
|
|
79
|
+
await q('SELECT COUNT(*) AS c FROM t WHERE EXISTS (SELECT 1 FROM s WHERE s.v = t.v)') === 1, null);
|
|
80
|
+
ok('NOT EXISTS 相关子查询',
|
|
81
|
+
await q('SELECT COUNT(*) AS c FROM t WHERE NOT EXISTS (SELECT 1 FROM s WHERE s.v = t.v)') === 2, null);
|
|
82
|
+
|
|
83
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
84
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
85
|
+
})().catch(e => {
|
|
86
|
+
console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
|
|
87
|
+
process.exit(1);
|
|
88
|
+
});
|
package/test/sql-parser.test.js
CHANGED
|
@@ -521,7 +521,8 @@ const rowsOf = (r) => (Array.isArray(r) ? r[0] : r).rows;
|
|
|
521
521
|
// CAST(x AS INT):数字前缀解析、非法为 0(此前返回 null)
|
|
522
522
|
ok("CAST('abc' AS INT) → 0", await scalar("SELECT CAST('abc' AS INT) AS x") === 0);
|
|
523
523
|
ok("CAST('12abc' AS INT) → 12", await scalar("SELECT CAST('12abc' AS INT) AS x") === 12);
|
|
524
|
-
|
|
524
|
+
// MySQL 8.0:小数转整数是四舍五入(半远离零),不是截断 → 3.9 应为 4
|
|
525
|
+
ok("CAST('3.9' AS INT) → 4", await scalar("SELECT CAST('3.9' AS INT) AS x") === 4);
|
|
525
526
|
// ROUND 中点浮点偏差
|
|
526
527
|
ok('ROUND(1.005, 2) → 1.01', await scalar('SELECT ROUND(1.005, 2) AS x') === 1.01);
|
|
527
528
|
ok('ROUND(2.5, 0) → 3(半远离零)', await scalar('SELECT ROUND(2.5, 0) AS x') === 3);
|
|
@@ -735,8 +736,9 @@ const rowsOf = (r) => (Array.isArray(r) ? r[0] : r).rows;
|
|
|
735
736
|
const cast = [
|
|
736
737
|
[`SELECT CAST('12abc' AS SIGNED) AS v`, 12, 'SIGNED 取数字前缀'],
|
|
737
738
|
[`SELECT CAST('abc' AS SIGNED) AS v`, 0, 'SIGNED 非数字返 0'],
|
|
738
|
-
|
|
739
|
-
[`SELECT CAST(12.9 AS
|
|
739
|
+
// 小数按 MySQL 语义四舍五入(此前实现与这两条断言都写成了截断,已修正)
|
|
740
|
+
[`SELECT CAST(12.9 AS SIGNED) AS v`, 13, 'SIGNED 四舍五入小数'],
|
|
741
|
+
[`SELECT CAST(12.9 AS INTEGER) AS v`, 13, 'INTEGER 与 SIGNED 一致'],
|
|
740
742
|
];
|
|
741
743
|
for (const [sql, expect, desc] of cast) {
|
|
742
744
|
let got = null, err = null;
|