jsql-neo 6.5.29 → 6.5.31
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/date-types.js +14 -3
- package/lib/redis_server.js +16 -5
- package/lib/sql.js +188 -64
- package/lib/xql/src/compiler.js +20 -20
- package/native/jsql-neo-native.node +0 -0
- package/package.json +3 -3
- package/test/extra-coverage.test.js +5 -2
- package/test/issue-50.test.js +29 -0
- package/test/issue-51.test.js +20 -0
- package/test/issue-52.test.js +22 -0
- package/test/issue-53.test.js +33 -0
- package/test/issue-59.test.js +76 -0
- package/test/issue-60.test.js +92 -0
- package/test/issue-63-68.test.js +91 -0
- package/native/.installed.json +0 -8
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.5.
|
|
6
|
+
> **v6.5.31** — 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/date-types.js
CHANGED
|
@@ -74,8 +74,14 @@ function validateDate(value, fieldName) {
|
|
|
74
74
|
return toLocalDateString(value);
|
|
75
75
|
}
|
|
76
76
|
if (typeof value === 'number') {
|
|
77
|
-
// Unix timestamp → date
|
|
78
|
-
|
|
77
|
+
// Unix timestamp → date。NaN / ±Infinity / 超出 Date 范围的数会生成
|
|
78
|
+
// Invalid Date,此前被格式化成字面量 "NaN-NaN-NaN" 静默落库(#64);
|
|
79
|
+
// 与字符串分支保持一致,直接判为非法日期值。
|
|
80
|
+
const d = new Date(value * 1000);
|
|
81
|
+
if (!Number.isFinite(value) || Number.isNaN(d.getTime())) {
|
|
82
|
+
throw createError('ER_INVALID_DATE', fieldName, String(value));
|
|
83
|
+
}
|
|
84
|
+
return toLocalDateString(d);
|
|
79
85
|
}
|
|
80
86
|
if (typeof value === 'string') {
|
|
81
87
|
const s = value.trim();
|
|
@@ -131,7 +137,12 @@ function validateDateTime(value, fieldName) {
|
|
|
131
137
|
return toLocalDateTimeString(value);
|
|
132
138
|
}
|
|
133
139
|
if (typeof value === 'number') {
|
|
134
|
-
|
|
140
|
+
// 同 validateDate:拒绝 NaN / Infinity / 越界时间戳(#64)
|
|
141
|
+
const d = new Date(value * 1000);
|
|
142
|
+
if (!Number.isFinite(value) || Number.isNaN(d.getTime())) {
|
|
143
|
+
throw createError('ER_INVALID_DATE', fieldName, String(value));
|
|
144
|
+
}
|
|
145
|
+
return toLocalDateTimeString(d);
|
|
135
146
|
}
|
|
136
147
|
if (typeof value === 'string') {
|
|
137
148
|
// ISO format: 2024-01-15T10:30:00.000Z / +08:00(可能带时区标记)
|
package/lib/redis_server.js
CHANGED
|
@@ -504,7 +504,8 @@ class RedisServer {
|
|
|
504
504
|
case 'ZSCORE': {
|
|
505
505
|
const v = this._get(args[0]);
|
|
506
506
|
if (!v || v.type !== 'zset' || !(args[1] in v.val)) return null;
|
|
507
|
-
|
|
507
|
+
// ZSCORE 的分数恒为 bulk string(含整数分数),不能落进 integer reply(#67)
|
|
508
|
+
return String(v.val[args[1]]);
|
|
508
509
|
}
|
|
509
510
|
case 'ZCARD': {
|
|
510
511
|
const v = this._get(args[0]);
|
|
@@ -535,7 +536,8 @@ class RedisServer {
|
|
|
535
536
|
const member = args[2];
|
|
536
537
|
zset[member] = (zset[member] || 0) + inc;
|
|
537
538
|
this._scheduleSnapshot();
|
|
538
|
-
|
|
539
|
+
// 同 ZSCORE:分数用 bulk string 回复(#67)
|
|
540
|
+
return String(zset[member]);
|
|
539
541
|
}
|
|
540
542
|
case 'MSET': {
|
|
541
543
|
const rest = args;
|
|
@@ -741,10 +743,19 @@ class RedisServer {
|
|
|
741
743
|
} else if (val === null) {
|
|
742
744
|
out = '$-1\r\n';
|
|
743
745
|
} else if (typeof val === 'number') {
|
|
744
|
-
|
|
746
|
+
// RESP integer reply 只能是十进制整数;小数必须走 bulk string(#67)。
|
|
747
|
+
// ZSCORE / ZINCRBY 的分数已在 handler 里转成字符串,这里是非整数数字的最后兜底。
|
|
748
|
+
out = Number.isInteger(val)
|
|
749
|
+
? ':' + val + '\r\n'
|
|
750
|
+
: '$' + Buffer.byteLength(String(val), 'utf8') + '\r\n' + val + '\r\n';
|
|
745
751
|
} else if (Array.isArray(val)) {
|
|
746
|
-
// 长度用字节数,不能用 String.length ——
|
|
747
|
-
|
|
752
|
+
// 长度用字节数,不能用 String.length —— 否则中文等多字节值会让客户端解帧错位。
|
|
753
|
+
// 批量回复里缺失的键必须是 nil bulk ($-1),不能是 "null" 字符串(#66)。
|
|
754
|
+
out = '*' + val.length + '\r\n' + val.map(v =>
|
|
755
|
+
(v === null || v === undefined)
|
|
756
|
+
? '$-1\r\n'
|
|
757
|
+
: '$' + Buffer.byteLength(String(v), 'utf8') + '\r\n' + v + '\r\n'
|
|
758
|
+
).join('');
|
|
748
759
|
} else if (typeof val === 'string') {
|
|
749
760
|
if (val === 'OK' || val === 'PONG') {
|
|
750
761
|
out = '+' + val + '\r\n';
|
package/lib/sql.js
CHANGED
|
@@ -534,6 +534,24 @@ class Parser {
|
|
|
534
534
|
parseStatement() {
|
|
535
535
|
const t = this.peek();
|
|
536
536
|
if (t.type === 'eof') return null;
|
|
537
|
+
// 括号化的查询表达式(#60):(SELECT ...) UNION (SELECT ...)。
|
|
538
|
+
// 解析完括号内的 SELECT 后,括号外仍可能有集合运算符/ORDER BY/LIMIT。
|
|
539
|
+
if (t.type === 'op' && t.value === '(' && this.isKeyword('SELECT', 1)) {
|
|
540
|
+
this.next();
|
|
541
|
+
const stmt = this.parseSelect();
|
|
542
|
+
this.expect('op', ')');
|
|
543
|
+
// 括号内 SELECT 自身的 ORDER BY/LIMIT/OFFSET 属于该操作数,
|
|
544
|
+
// 不能作为整个组合查询的尾部子句(否则会作用到 union 后的结果)。
|
|
545
|
+
if (this.isKeyword('UNION') || this.isKeyword('INTERSECT') || this.isKeyword('EXCEPT')) {
|
|
546
|
+
stmt._operandTail = { orderBy: stmt.orderBy, limit: stmt.limit, offset: stmt.offset };
|
|
547
|
+
stmt.orderBy = null;
|
|
548
|
+
stmt.limit = null;
|
|
549
|
+
stmt.offset = 0;
|
|
550
|
+
}
|
|
551
|
+
this.parseSetOps(stmt);
|
|
552
|
+
this.optionalTailSemicolon();
|
|
553
|
+
return stmt;
|
|
554
|
+
}
|
|
537
555
|
if (t.type !== 'keyword') {
|
|
538
556
|
// 少数语句起始词刻意不进关键字表(它们同时是常见标识符),按上下文识别
|
|
539
557
|
const w = String(t.value).toUpperCase();
|
|
@@ -1564,53 +1582,70 @@ class Parser {
|
|
|
1564
1582
|
}
|
|
1565
1583
|
let having = null;
|
|
1566
1584
|
if (this.isKeyword('HAVING')) { this.next(); having = this.parseBooleanExpr(); }
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1585
|
+
const orderBy = this.parseOrderByClause();
|
|
1586
|
+
const lim = this.parseLimitOffset();
|
|
1587
|
+
const limit = lim.limit;
|
|
1588
|
+
const offset = lim.offset;
|
|
1589
|
+
const node = { type: 'select', columns, aggregate, distinct, from, where, groupBy, having, orderBy, limit, offset, union: null, intersect: null, except: null, rollup };
|
|
1590
|
+
this.parseSetOps(node);
|
|
1591
|
+
this.optionalTailSemicolon();
|
|
1592
|
+
this.depth--;
|
|
1593
|
+
|
|
1594
|
+
return node;
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
/** ORDER BY 子句(可选)。返回 null 或 [{column, dir}]。 */
|
|
1598
|
+
parseOrderByClause() {
|
|
1599
|
+
if (!this.isKeyword('ORDER')) return null;
|
|
1600
|
+
this.expectKeyword('ORDER'); this.expectKeyword('BY');
|
|
1601
|
+
const orderBy = [];
|
|
1602
|
+
while (true) {
|
|
1603
|
+
// 支持表达式排序:ORDER BY a + b DESC、ORDER BY UPPER(name)
|
|
1604
|
+
// 纯列名仍走 parseColumnRef,保持既有字符串形态不破坏下游
|
|
1605
|
+
let col;
|
|
1606
|
+
const save = this.pos;
|
|
1607
|
+
// ORDER BY 项的合法结束符;不是这些就说明是表达式(如 a+b、UPPER(name))
|
|
1608
|
+
const ORDER_END = ['ASC', 'DESC', 'LIMIT', 'OFFSET', 'FETCH', 'UNION', 'INTERSECT',
|
|
1609
|
+
'EXCEPT', 'RETURNING', 'FOR', 'WITH'];
|
|
1610
|
+
const endsClause = (tk) => {
|
|
1611
|
+
if (tk.type === 'eof') return true;
|
|
1612
|
+
if (tk.type === 'op' && (tk.value === ',' || tk.value === ';' || tk.value === ')')) return true;
|
|
1613
|
+
return ORDER_END.some(w => this.isWord(w));
|
|
1614
|
+
};
|
|
1615
|
+
try {
|
|
1616
|
+
col = this.parseColumnRef();
|
|
1617
|
+
if (!endsClause(this.peek())) {
|
|
1591
1618
|
this.pos = save;
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1619
|
+
col = this.parseScalar();
|
|
1620
|
+
}
|
|
1621
|
+
} catch (e) {
|
|
1622
|
+
this.pos = save;
|
|
1623
|
+
// ORDER BY 可以引用输出列别名,而别名可能与聚合函数同名:
|
|
1624
|
+
// SELECT AVG(v) AS avg FROM t GROUP BY g ORDER BY avg DESC
|
|
1625
|
+
// 这种 token 已被词法器标成 keyword(AVG/SUM/COUNT/MAX/MIN 都在关键字表里),
|
|
1626
|
+
// parseColumnRef 只认 ident 会失败,parseScalar 又会把它当函数调用 → "got 'AVG'"。
|
|
1627
|
+
// 此处把裸 keyword 当作列名,与 SQLite / MySQL 的行为一致。
|
|
1628
|
+
const tk = this.peek();
|
|
1629
|
+
if (tk.type === 'keyword' && !this.isWord('ASC') && !this.isWord('DESC')
|
|
1630
|
+
&& !ORDER_END.some(w => this.isWord(w))) {
|
|
1631
|
+
this.next();
|
|
1632
|
+
col = tk.value;
|
|
1633
|
+
} else {
|
|
1634
|
+
col = this.parseScalar();
|
|
1605
1635
|
}
|
|
1606
|
-
let dir = 'asc';
|
|
1607
|
-
if (this.isKeyword('ASC')) { this.next(); }
|
|
1608
|
-
else if (this.isKeyword('DESC')) { this.next(); dir = 'desc'; }
|
|
1609
|
-
orderBy.push({ column: col, dir });
|
|
1610
|
-
if (this.peek().type === 'op' && this.peek().value === ',') { this.next(); continue; }
|
|
1611
|
-
break;
|
|
1612
1636
|
}
|
|
1637
|
+
let dir = 'asc';
|
|
1638
|
+
if (this.isKeyword('ASC')) { this.next(); }
|
|
1639
|
+
else if (this.isKeyword('DESC')) { this.next(); dir = 'desc'; }
|
|
1640
|
+
orderBy.push({ column: col, dir });
|
|
1641
|
+
if (this.peek().type === 'op' && this.peek().value === ',') { this.next(); continue; }
|
|
1642
|
+
break;
|
|
1613
1643
|
}
|
|
1644
|
+
return orderBy;
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
/** LIMIT / OFFSET 子句(MySQL 语法,含 LIMIT a,b 与独立 OFFSET)。 */
|
|
1648
|
+
parseLimitOffset() {
|
|
1614
1649
|
let limit = null, offset = 0;
|
|
1615
1650
|
if (this.isKeyword('LIMIT')) {
|
|
1616
1651
|
this.next();
|
|
@@ -1628,38 +1663,67 @@ class Parser {
|
|
|
1628
1663
|
if (typeof offset === 'number' && offset < 0) throw new Error(`OFFSET must be a non-negative integer, got ${offset}`);
|
|
1629
1664
|
limit = Number.MAX_SAFE_INTEGER;
|
|
1630
1665
|
}
|
|
1631
|
-
|
|
1666
|
+
return { limit, offset };
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
/**
|
|
1670
|
+
* 集合运算后缀(#60):UNION [ALL|DISTINCT] / INTERSECT / EXCEPT。
|
|
1671
|
+
* 右侧操作数允许括号化的 SELECT((SELECT ...))。
|
|
1672
|
+
* 标准 SQL 里组合查询尾部的 ORDER BY / LIMIT / OFFSET 作用于整个结果,
|
|
1673
|
+
* 语法上它们先被解析进最右侧 SELECT(#57),这里逐层提升到外层,
|
|
1674
|
+
* 否则执行层在 concat 后直接返回,尾部子句被静默丢弃。
|
|
1675
|
+
*
|
|
1676
|
+
* 单独成方法,供普通 SELECT 与括号化根节点 (SELECT ...) UNION ... 共用。
|
|
1677
|
+
*/
|
|
1678
|
+
parseSetOps(statement) {
|
|
1632
1679
|
let union = null, intersect = null, except = null;
|
|
1680
|
+
let rhsParenthesized = false;
|
|
1681
|
+
const rhsOperand = () => {
|
|
1682
|
+
const operand = this.parseSetOperand();
|
|
1683
|
+
rhsParenthesized = operand.parenthesized;
|
|
1684
|
+
return operand.select;
|
|
1685
|
+
};
|
|
1633
1686
|
if (this.isKeyword('UNION')) {
|
|
1634
1687
|
this.next();
|
|
1635
1688
|
const all = this.isKeyword('ALL');
|
|
1636
1689
|
if (all) this.next();
|
|
1637
1690
|
else if (this.isKeyword('DISTINCT')) this.next();
|
|
1638
|
-
union = { all, select:
|
|
1691
|
+
union = { all, select: rhsOperand() };
|
|
1639
1692
|
} else if (this.isKeyword('INTERSECT')) {
|
|
1640
1693
|
this.next();
|
|
1641
1694
|
const all = this.isKeyword('ALL');
|
|
1642
1695
|
if (all) this.next();
|
|
1643
1696
|
else if (this.isKeyword('DISTINCT')) this.next();
|
|
1644
|
-
intersect = { all, select:
|
|
1697
|
+
intersect = { all, select: rhsOperand() };
|
|
1645
1698
|
} else if (this.isKeyword('EXCEPT')) {
|
|
1646
1699
|
this.next();
|
|
1647
1700
|
const all = this.isKeyword('ALL');
|
|
1648
1701
|
if (all) this.next();
|
|
1649
1702
|
else if (this.isKeyword('DISTINCT')) this.next();
|
|
1650
|
-
except = { all, select:
|
|
1651
|
-
}
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1703
|
+
except = { all, select: rhsOperand() };
|
|
1704
|
+
}
|
|
1705
|
+
statement.union = union;
|
|
1706
|
+
statement.intersect = intersect;
|
|
1707
|
+
statement.except = except;
|
|
1708
|
+
// 括号化操作数(#60)内部的 ORDER BY/LIMIT/OFFSET 属于它自己,不能提升;
|
|
1709
|
+
// 普通无括号的最右侧 SELECT 上挂的尾部子句才属于整个组合查询(#57)。
|
|
1710
|
+
if (!rhsParenthesized) {
|
|
1656
1711
|
const rhsSel = (union || intersect || except || {}).select;
|
|
1657
1712
|
if (rhsSel) {
|
|
1658
|
-
if (rhsSel.orderBy) { orderBy = rhsSel.orderBy; rhsSel.orderBy = null; }
|
|
1659
|
-
if (rhsSel.limit !== null && rhsSel.limit !== undefined) { limit = rhsSel.limit; rhsSel.limit = null; }
|
|
1660
|
-
if (rhsSel.offset) { offset = rhsSel.offset; rhsSel.offset = 0; }
|
|
1713
|
+
if (rhsSel.orderBy) { statement.orderBy = rhsSel.orderBy; rhsSel.orderBy = null; }
|
|
1714
|
+
if (rhsSel.limit !== null && rhsSel.limit !== undefined) { statement.limit = rhsSel.limit; rhsSel.limit = null; }
|
|
1715
|
+
if (rhsSel.offset) { statement.offset = rhsSel.offset; rhsSel.offset = 0; }
|
|
1661
1716
|
}
|
|
1662
1717
|
}
|
|
1718
|
+
// 括号化操作数会把尾部 ORDER BY/LIMIT/OFFSET 留在括号外(#60):
|
|
1719
|
+
// 此时它们属于整个组合查询,在此直接解析。普通路径下这些子句已被
|
|
1720
|
+
// 最右侧 SELECT 消费并提升,不会重复解析。
|
|
1721
|
+
if (this.isKeyword('ORDER')) statement.orderBy = this.parseOrderByClause();
|
|
1722
|
+
if (this.isKeyword('LIMIT') || this.isKeyword('OFFSET')) {
|
|
1723
|
+
const tail = this.parseLimitOffset();
|
|
1724
|
+
statement.limit = tail.limit;
|
|
1725
|
+
statement.offset = tail.offset;
|
|
1726
|
+
}
|
|
1663
1727
|
// FETCH FIRST n ROWS ONLY(SQL 标准的 LIMIT 写法)
|
|
1664
1728
|
if (this.isWord('FETCH')) {
|
|
1665
1729
|
this.next();
|
|
@@ -1669,14 +1733,26 @@ class Parser {
|
|
|
1669
1733
|
let n = nTok.type === 'number' ? nTok.value : Number(nTok.value);
|
|
1670
1734
|
if (typeof n === 'bigint') n = Number(n <= BigInt(Number.MAX_SAFE_INTEGER) ? n : BigInt(Number.MAX_SAFE_INTEGER));
|
|
1671
1735
|
if (!Number.isFinite(n)) throw new Error(`Expected row count after FETCH FIRST, got '${nTok.value}'`);
|
|
1672
|
-
limit = n;
|
|
1736
|
+
statement.limit = n;
|
|
1673
1737
|
if (this.isWord('ROW') || this.isWord('ROWS')) this.next();
|
|
1674
1738
|
if (this.isWord('ONLY')) this.next();
|
|
1675
1739
|
}
|
|
1676
|
-
|
|
1677
|
-
|
|
1740
|
+
return statement;
|
|
1741
|
+
}
|
|
1678
1742
|
|
|
1679
|
-
|
|
1743
|
+
/** 集合运算的操作数:普通 SELECT,或括号化的 SELECT(#60)。 */
|
|
1744
|
+
parseSetOperand() {
|
|
1745
|
+
if (this.peek().type === 'op' && this.peek().value === '(' && this.isKeyword('SELECT', 1)) {
|
|
1746
|
+
this.next();
|
|
1747
|
+
const inner = this.parseSelect();
|
|
1748
|
+
this.expect('op', ')');
|
|
1749
|
+
// A UNION (B) UNION C —— 括号后若仍有集合运算符,继续挂到该操作数上。
|
|
1750
|
+
if (this.isKeyword('UNION') || this.isKeyword('INTERSECT') || this.isKeyword('EXCEPT')) {
|
|
1751
|
+
this.parseSetOps(inner);
|
|
1752
|
+
}
|
|
1753
|
+
return { select: inner, parenthesized: true };
|
|
1754
|
+
}
|
|
1755
|
+
return { select: this.parseSelect(), parenthesized: false };
|
|
1680
1756
|
}
|
|
1681
1757
|
|
|
1682
1758
|
parseFrom() {
|
|
@@ -2533,6 +2609,20 @@ class Parser {
|
|
|
2533
2609
|
if (this.peek().type === 'op' && this.peek().value === '(') { this.next(); this.expect('op', ')'); }
|
|
2534
2610
|
return { type: 'func', name, args: [] };
|
|
2535
2611
|
}
|
|
2612
|
+
// POSITION(substr IN str) —— 标准 SQL / MySQL 写法,等价于 LOCATE(substr, str)。
|
|
2613
|
+
// IN 是关键字,通用函数参数循环会把它当成 IN 表达式的一部分并报
|
|
2614
|
+
// "Expected ( but got 'abc'",所以在这里单独归一(#63)。
|
|
2615
|
+
if (String(t.value).toUpperCase() === 'POSITION' && this.peek().type === 'op' && this.peek().value === '(') {
|
|
2616
|
+
this.next();
|
|
2617
|
+
const substr = this.parseOperand();
|
|
2618
|
+
if (!(this.isWord('IN') || this.isKeyword('IN'))) {
|
|
2619
|
+
throw new Error(`Expected IN in POSITION, got '${this.peek().value}'`);
|
|
2620
|
+
}
|
|
2621
|
+
this.next();
|
|
2622
|
+
const str = this.parseOperand();
|
|
2623
|
+
this.expect('op', ')');
|
|
2624
|
+
return { type: 'func', name: 'LOCATE', args: [substr, str] };
|
|
2625
|
+
}
|
|
2536
2626
|
// EXTRACT(YEAR FROM expr) —— 标准 SQL 的 FROM 形式
|
|
2537
2627
|
if (String(t.value).toUpperCase() === 'EXTRACT' && this.peek().type === 'op' && this.peek().value === '(') {
|
|
2538
2628
|
this.next();
|
|
@@ -3666,6 +3756,15 @@ function pgAge(from, to) {
|
|
|
3666
3756
|
return `${years} years ${months} mons ${days} days`;
|
|
3667
3757
|
}
|
|
3668
3758
|
|
|
3759
|
+
/**
|
|
3760
|
+
* 数学定义域外的输入(SQRT(-1) / POWER(-2,0.5) 等)在 JS 里会得到
|
|
3761
|
+
* NaN / Infinity。MySQL 语义应返回 NULL;NaN 还会通过后续算术与聚合
|
|
3762
|
+
* 继续传播,且 JSON.stringify(NaN) === 'null' 会在序列化时把问题藏起来。
|
|
3763
|
+
* 统一在这里拦掉(#65)。
|
|
3764
|
+
*/
|
|
3765
|
+
function finiteOrNull(x) {
|
|
3766
|
+
return (typeof x === 'number' && !Number.isFinite(x)) ? null : x;
|
|
3767
|
+
}
|
|
3669
3768
|
function applyScalarFunction(fnNode, row, ctx) {
|
|
3670
3769
|
const name = (fnNode.name || '').toUpperCase();
|
|
3671
3770
|
const rawArgs = fnNode.args || [];
|
|
@@ -4008,8 +4107,8 @@ function applyScalarFunction(fnNode, row, ctx) {
|
|
|
4008
4107
|
case 'FLOOR': return args[0] == null ? null : Math.floor(args[0]);
|
|
4009
4108
|
case 'CEIL': case 'CEILING': return args[0] == null ? null : Math.ceil(args[0]);
|
|
4010
4109
|
case 'MOD': return (args[0] == null || args[1] === 0) ? null : args[0] % args[1];
|
|
4011
|
-
case 'POWER': case 'POW': return (args[0] == null || args[1] == null) ? null : Math.pow(args[0], args[1]);
|
|
4012
|
-
case 'SQRT': return args[0] == null ? null : Math.sqrt(args[0]);
|
|
4110
|
+
case 'POWER': case 'POW': return (args[0] == null || args[1] == null) ? null : finiteOrNull(Math.pow(args[0], args[1]));
|
|
4111
|
+
case 'SQRT': return args[0] == null ? null : finiteOrNull(Math.sqrt(args[0]));
|
|
4013
4112
|
case 'IFNULL': case 'NVL': return args[0] != null ? args[0] : args[1];
|
|
4014
4113
|
case 'COALESCE': {
|
|
4015
4114
|
// 不能直接 return find(...):全都取不到值时 find 返回 undefined,
|
|
@@ -4702,6 +4801,22 @@ function dedupeRows(rows) {
|
|
|
4702
4801
|
});
|
|
4703
4802
|
}
|
|
4704
4803
|
|
|
4804
|
+
/**
|
|
4805
|
+
* 集合运算两侧列数必须一致(#60)。
|
|
4806
|
+
* MySQL/PostgreSQL 遇到列数不同的组合查询会直接报错;此前 jsql-neo 静默按左分支
|
|
4807
|
+
* 截断,第二个 SELECT 多出的列被丢弃 → 静默数据丢失。
|
|
4808
|
+
*/
|
|
4809
|
+
function assertSetOpColumnCount(left, right) {
|
|
4810
|
+
const lc = (left && left.columns ? left.columns.length : 0);
|
|
4811
|
+
const rc = (right && right.columns ? right.columns.length : 0);
|
|
4812
|
+
if (lc !== rc) {
|
|
4813
|
+
const err = new Error('The used SELECT statements have a different number of columns');
|
|
4814
|
+
err.code = 'ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT';
|
|
4815
|
+
err.errno = 1222;
|
|
4816
|
+
throw err;
|
|
4817
|
+
}
|
|
4818
|
+
}
|
|
4819
|
+
|
|
4705
4820
|
// 比较前把布尔归一化为 1/0,但只在“另一侧是数值/布尔”时做,
|
|
4706
4821
|
// 避免把 true 与字符串 'true' 的比较悄悄改成 1 与 'true' 的比较。
|
|
4707
4822
|
function normalizeCmpValue(a, other) {
|
|
@@ -7487,8 +7602,11 @@ class SQLExecutor {
|
|
|
7487
7602
|
|
|
7488
7603
|
// UNION 处理
|
|
7489
7604
|
if (statement.union) {
|
|
7490
|
-
const
|
|
7605
|
+
const leftStatement = { ...statement, ...setOpBase, union: null };
|
|
7606
|
+
if (statement._operandTail) Object.assign(leftStatement, statement._operandTail);
|
|
7607
|
+
const left = await this.executeSelect(leftStatement);
|
|
7491
7608
|
const right = await this.executeSelect(statement.union.select);
|
|
7609
|
+
assertSetOpColumnCount(left, right);
|
|
7492
7610
|
let rows = left.rows.concat(right.rows);
|
|
7493
7611
|
if (!statement.union.all) rows = dedupeRows(rows);
|
|
7494
7612
|
rows = this._finalizeSetOp(rows, left.columns, statement);
|
|
@@ -7497,8 +7615,11 @@ class SQLExecutor {
|
|
|
7497
7615
|
|
|
7498
7616
|
// INTERSECT:保留同时出现在右侧结果中的行(默认 DISTINCT,与 UNION 的处理保持一致)
|
|
7499
7617
|
if (statement.intersect) {
|
|
7500
|
-
const
|
|
7618
|
+
const leftStatement = { ...statement, ...setOpBase, intersect: null, except: null };
|
|
7619
|
+
if (statement._operandTail) Object.assign(leftStatement, statement._operandTail);
|
|
7620
|
+
const left = await this.executeSelect(leftStatement);
|
|
7501
7621
|
const right = await this.executeSelect(statement.intersect.select);
|
|
7622
|
+
assertSetOpColumnCount(left, right);
|
|
7502
7623
|
const seen = new Set(right.rows.map(r => JSON.stringify(r)));
|
|
7503
7624
|
let out = left.rows.filter(r => seen.has(JSON.stringify(r)));
|
|
7504
7625
|
if (!statement.intersect.all) out = dedupeRows(out);
|
|
@@ -7508,8 +7629,11 @@ class SQLExecutor {
|
|
|
7508
7629
|
|
|
7509
7630
|
// EXCEPT:去掉出现在右侧结果中的行(默认 DISTINCT)
|
|
7510
7631
|
if (statement.except) {
|
|
7511
|
-
const
|
|
7632
|
+
const leftStatement = { ...statement, ...setOpBase, intersect: null, except: null };
|
|
7633
|
+
if (statement._operandTail) Object.assign(leftStatement, statement._operandTail);
|
|
7634
|
+
const left = await this.executeSelect(leftStatement);
|
|
7512
7635
|
const right = await this.executeSelect(statement.except.select);
|
|
7636
|
+
assertSetOpColumnCount(left, right);
|
|
7513
7637
|
const seen = new Set(right.rows.map(r => JSON.stringify(r)));
|
|
7514
7638
|
let out = left.rows.filter(r => !seen.has(JSON.stringify(r)));
|
|
7515
7639
|
if (!statement.except.all) out = dedupeRows(out);
|
package/lib/xql/src/compiler.js
CHANGED
|
@@ -492,6 +492,26 @@ function compileQuery(q, opts = {}) {
|
|
|
492
492
|
B.push(' for (let _i = 0; _i < rows.length; _i++) _out.push(rows[_i]);');
|
|
493
493
|
}
|
|
494
494
|
|
|
495
|
+
// 3) ORDER BY
|
|
496
|
+
if (orderBy) {
|
|
497
|
+
// 每个排序键一个块。注意不能用 continue —— 块级作用域里没有循环可继续,
|
|
498
|
+
// 改成 if 包裹。
|
|
499
|
+
//
|
|
500
|
+
// 字符串用 < / > 而不是 localeCompare:后者是 locale 感知的排序,
|
|
501
|
+
// 6.8 万行排序要调 ~110 万次,实测比朴素比较多一个量级
|
|
502
|
+
// (这一项就是从 2.93 倍降到持平的关键)。
|
|
503
|
+
const cmp = orderBy.map((o, i) => ` { const av = _ok${i}(a), bv = _ok${i}(b);
|
|
504
|
+
if (av !== bv) {
|
|
505
|
+
if (av === null || av === undefined) return -1 * ${o.dir};
|
|
506
|
+
if (bv === null || bv === undefined) return 1 * ${o.dir};
|
|
507
|
+
const d = (typeof av === 'number' && typeof bv === 'number') || (typeof av === 'boolean' && typeof bv === 'boolean')
|
|
508
|
+
? (av < bv ? -1 : 1)
|
|
509
|
+
: (String(av) < String(bv) ? -1 : 1);
|
|
510
|
+
if (d !== 0) return d * ${o.dir};
|
|
511
|
+
} }`).join('\n');
|
|
512
|
+
B.push(' _out.sort((a, b) => {\n' + cmp + '\n return 0;\n });');
|
|
513
|
+
}
|
|
514
|
+
|
|
495
515
|
// 2) SELECT —— 与 WHERE 同理,投影语句也内联进循环。
|
|
496
516
|
// 每列调一次函数的话,6.8 万行 × 2 列就是 13.6 万次调用,
|
|
497
517
|
// 实测占掉整个管道的大半时间(2.81 倍差距主要来自这里)。
|
|
@@ -542,26 +562,6 @@ function compileQuery(q, opts = {}) {
|
|
|
542
562
|
const limit = q.limit;
|
|
543
563
|
const offset = q.offset || 0;
|
|
544
564
|
|
|
545
|
-
// 3) ORDER BY
|
|
546
|
-
if (orderBy) {
|
|
547
|
-
// 每个排序键一个块。注意不能用 continue —— 块级作用域里没有循环可继续,
|
|
548
|
-
// 改成 if 包裹。
|
|
549
|
-
//
|
|
550
|
-
// 字符串用 < / > 而不是 localeCompare:后者是 locale 感知的排序,
|
|
551
|
-
// 6.8 万行排序要调 ~110 万次,实测比朴素比较多一个量级
|
|
552
|
-
// (这一项就是从 2.93 倍降到持平的关键)。
|
|
553
|
-
const cmp = orderBy.map((o, i) => ` { const av = _ok${i}(a), bv = _ok${i}(b);
|
|
554
|
-
if (av !== bv) {
|
|
555
|
-
if (av === null || av === undefined) return -1 * ${o.dir};
|
|
556
|
-
if (bv === null || bv === undefined) return 1 * ${o.dir};
|
|
557
|
-
const d = (typeof av === 'number' && typeof bv === 'number') || (typeof av === 'boolean' && typeof bv === 'boolean')
|
|
558
|
-
? (av < bv ? -1 : 1)
|
|
559
|
-
: (String(av) < String(bv) ? -1 : 1);
|
|
560
|
-
if (d !== 0) return d * ${o.dir};
|
|
561
|
-
} }`).join('\n');
|
|
562
|
-
B.push(' _out.sort((a, b) => {\n' + cmp + '\n return 0;\n });');
|
|
563
|
-
}
|
|
564
|
-
|
|
565
565
|
// 4) OFFSET / LIMIT
|
|
566
566
|
if (offset || (limit !== null && limit !== undefined)) {
|
|
567
567
|
const end = (limit === null || limit === undefined) ? 'undefined' : String(offset + limit);
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsql-neo",
|
|
3
|
-
"version": "6.5.
|
|
3
|
+
"version": "6.5.31",
|
|
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",
|
|
@@ -50,12 +50,12 @@
|
|
|
50
50
|
"scripts": {
|
|
51
51
|
"postinstall": "node postinstall.js",
|
|
52
52
|
"build:native": "node scripts/build-native.js",
|
|
53
|
-
"test": "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",
|
|
53
|
+
"test": "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",
|
|
54
54
|
"test:parser": "node test/sql-parser.test.js",
|
|
55
55
|
"test:btree": "node test/btree.test.js",
|
|
56
56
|
"test:native": "node test/native.test.js",
|
|
57
57
|
"test:regress": "node test/regress-5.1.0.js",
|
|
58
|
-
"test:all": "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",
|
|
58
|
+
"test:all": "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",
|
|
59
59
|
"test:coverage": "c8 --all --include='lib/**' --exclude='lib/web_ui.js' --reporter=text --reporter=lcov node test/coverage.js",
|
|
60
60
|
"test:orms": "node examples/orms/run-all.js",
|
|
61
61
|
"test:dml": "node test/dml-constraints.test.js",
|
|
@@ -561,10 +561,13 @@ test('数学函数:ABS / CEIL / FLOOR / ROUND / POWER / SQRT', async () => {
|
|
|
561
561
|
assert.strictEqual(await one(q, 'SELECT SQRT(16)'), 4);
|
|
562
562
|
});
|
|
563
563
|
|
|
564
|
-
|
|
564
|
+
// #65:此前断言 SQRT(-1) 返回 NaN,等于把 bug 钉成了预期。
|
|
565
|
+
// MySQL 语义下数学定义域外的输入应返回 NULL;NaN 会污染后续算术/聚合,
|
|
566
|
+
// 且 JSON.stringify(NaN) === 'null' 会在序列化时把问题藏起来。
|
|
567
|
+
test('SQRT 负数返回 NULL', async () => {
|
|
565
568
|
const { q } = makeDb();
|
|
566
569
|
const v = await one(q, 'SELECT SQRT(-1)');
|
|
567
|
-
assert.
|
|
570
|
+
assert.strictEqual(v, null);
|
|
568
571
|
});
|
|
569
572
|
|
|
570
573
|
test('REPLACE 函数', async () => {
|
package/test/issue-50.test.js
CHANGED
|
@@ -75,6 +75,35 @@ function localStr(utcMs) {
|
|
|
75
75
|
validateDateType('2024-01-15T10:30:00Z', 'datetime', 'dt'));
|
|
76
76
|
ok('#50 validateDateType 非法日期仍报错',
|
|
77
77
|
(() => { try { validateDateType('2024-13-45T10:30:00Z', 'datetime', 'dt'); return false; } catch (e) { return true; } })());
|
|
78
|
+
|
|
79
|
+
// 8) 更多等价写法:小写 z、紧凑偏移 ±HHMM、非整点时区、跨年、带空格的偏移
|
|
80
|
+
const isoCases = [
|
|
81
|
+
'2024-01-15T10:30:00z',
|
|
82
|
+
'2024-01-15T10:30:00-0500',
|
|
83
|
+
'2024-01-15T10:30:00+0530',
|
|
84
|
+
'2024-12-31T23:59:59Z',
|
|
85
|
+
'2024-02-29T00:00:00+09:00',
|
|
86
|
+
'2024-01-15 10:30:00+08:00',
|
|
87
|
+
];
|
|
88
|
+
await q('CREATE TABLE tz (id INTEGER PRIMARY KEY, dt DATETIME)');
|
|
89
|
+
for (let i = 0; i < isoCases.length; i++) {
|
|
90
|
+
const s = isoCases[i];
|
|
91
|
+
await q(`INSERT INTO tz (id, dt) VALUES (${i + 1}, '${s}')`);
|
|
92
|
+
const got = (await q(`SELECT dt FROM tz WHERE id = ${i + 1}`)).rows[0][0];
|
|
93
|
+
const want = localStr(Date.parse(s));
|
|
94
|
+
ok(`#50 等价写法 ${s} 换算正确`, got === want, { got, want });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 9) DATETIME 存储的是「瞬间」:跨日/跨年的 UTC 换算会改变日期部分
|
|
98
|
+
await q('CREATE TABLE crossyear (id INTEGER PRIMARY KEY, dt DATETIME)');
|
|
99
|
+
await q("INSERT INTO crossyear VALUES (1, '2024-01-01T00:30:00+09:00')");
|
|
100
|
+
r = await q('SELECT dt FROM crossyear WHERE id = 1');
|
|
101
|
+
// UTC+8 下应为 2023-12-31 23:30:00(跨回上一年)
|
|
102
|
+
const expectCross = localStr(Date.parse('2024-01-01T00:30:00+09:00'));
|
|
103
|
+
ok('#50 时区换算可跨年', r.rows[0][0] === expectCross, { got: r.rows[0][0], want: expectCross });
|
|
104
|
+
if (new Date().getTimezoneOffset() === -480) {
|
|
105
|
+
ok('#50 UTC+8 跨年具体值', r.rows[0][0] === '2023-12-31 23:30:00', r.rows[0][0]);
|
|
106
|
+
}
|
|
78
107
|
})();
|
|
79
108
|
|
|
80
109
|
setTimeout(() => {
|
package/test/issue-51.test.js
CHANGED
|
@@ -51,6 +51,26 @@ const norm = (rows) => rows.map(r => r.map(v => (typeof v === 'bigint' ? Number(
|
|
|
51
51
|
r = await q('SELECT a, b, COUNT(*) FROM g GROUP BY a, b');
|
|
52
52
|
ok('#51 多列分组未被破坏', r.rows.length === 4, r.rows);
|
|
53
53
|
|
|
54
|
+
// 表达式与裸列混排(#51 扩展):每一项都能独立走表达式解析
|
|
55
|
+
r = await q('SELECT a%2 AS p, b, COUNT(*) FROM g GROUP BY a%2, b');
|
|
56
|
+
ok('#51 表达式 + 裸列混合分组', JSON.stringify(norm(r.rows)) === JSON.stringify([[0, 20, 1], [0, 40, 1], [1, 10, 1], [1, 30, 1]]), r.rows);
|
|
57
|
+
|
|
58
|
+
r = await q('SELECT b, a%2 AS p FROM g GROUP BY b, a%2');
|
|
59
|
+
ok('#51 裸列 + 表达式混合分组(表达式在后)', JSON.stringify(norm(r.rows)) === JSON.stringify([[10, 1], [20, 0], [30, 1], [40, 0]]), r.rows);
|
|
60
|
+
|
|
61
|
+
r = await q('SELECT a*2+1 AS x, COUNT(*) FROM g GROUP BY a*2+1');
|
|
62
|
+
ok('#51 多运算符表达式分组', JSON.stringify(norm(r.rows)) === JSON.stringify([[3, 1], [5, 1], [7, 1], [9, 1]]), r.rows);
|
|
63
|
+
|
|
64
|
+
// 字符串函数分组:结果真正的分组边界
|
|
65
|
+
await q('CREATE TABLE s (id INTEGER PRIMARY KEY, name TEXT)');
|
|
66
|
+
await q("INSERT INTO s VALUES (1,'ab'),(2,'cd'),(3,'ab'),(4,'ef'),(5,'cd')");
|
|
67
|
+
r = await q('SELECT LOWER(name) AS n, COUNT(*) FROM s GROUP BY LOWER(name)');
|
|
68
|
+
ok('#51 字符串函数表达式分组', JSON.stringify(norm(r.rows)) === JSON.stringify([['ab', 2], ['cd', 2], ['ef', 1]]), r.rows);
|
|
69
|
+
|
|
70
|
+
// ORDER BY 表达式仍然可用(回归保护)
|
|
71
|
+
r = await q('SELECT a FROM g ORDER BY a%2');
|
|
72
|
+
ok('#51 ORDER BY 表达式未被破坏', r.rows.length === 4, r.rows);
|
|
73
|
+
|
|
54
74
|
console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILURES'} — ${pass} passed, ${fail} failed\n`);
|
|
55
75
|
if (fail > 0) process.exit(1);
|
|
56
76
|
})();
|
package/test/issue-52.test.js
CHANGED
|
@@ -57,6 +57,28 @@ const j = (rows) => JSON.stringify(rows, (k, v) => (typeof v === 'bigint' ? v.to
|
|
|
57
57
|
r = await q('SELECT CASE WHEN COUNT(*)>2 THEN 1 ELSE 0 END AS f FROM t HAVING CASE WHEN COUNT(*)>2 THEN 1 ELSE 0 END = 1');
|
|
58
58
|
ok('#52 HAVING 里的 CASE 聚合', j(r.rows) === j([[1]]), r.rows);
|
|
59
59
|
|
|
60
|
+
// 聚合藏在函数参数里再进 CASE(#52 扩展)
|
|
61
|
+
r = await q('SELECT CASE WHEN ROUND(AVG(v),0) > 2 THEN COUNT(*) ELSE 0 END FROM t');
|
|
62
|
+
ok('#52 函数包裹的聚合在 CASE 条件里', j(r.rows) === j([[4]]), r.rows);
|
|
63
|
+
|
|
64
|
+
r = await q('SELECT CASE WHEN SUM(v)/COUNT(*) > 2 THEN 1 ELSE 0 END FROM t');
|
|
65
|
+
ok('#52 CASE 条件里的聚合算术表达式', j(r.rows) === j([[1]]), r.rows);
|
|
66
|
+
|
|
67
|
+
r = await q('SELECT CASE WHEN SUM(v)/COUNT(*) > 3 THEN 1 ELSE 0 END FROM t');
|
|
68
|
+
ok('#52 聚合算术表达式条件为假', j(r.rows) === j([[0]]), r.rows);
|
|
69
|
+
|
|
70
|
+
// GROUP BY 路径:表达式 + SUM 条件
|
|
71
|
+
r = await q('SELECT g, CASE WHEN SUM(v)>3 THEN 1 ELSE 0 END AS f FROM t GROUP BY g ORDER BY g');
|
|
72
|
+
ok('#52 分组路径 CASE+SUM(排序后)', j(r.rows) === j([['a', 0], ['b', 1]]), r.rows);
|
|
73
|
+
|
|
74
|
+
// 多个分支同时含聚合
|
|
75
|
+
r = await q('SELECT CASE WHEN COUNT(*)>10 THEN COUNT(*) WHEN COUNT(*)>=4 THEN MIN(v) ELSE 0 END FROM t');
|
|
76
|
+
ok('#52 多分支聚合 CASE', j(r.rows) === j([[1]]), r.rows);
|
|
77
|
+
|
|
78
|
+
// 不含聚合的 CASE 仍然逐行求值,不能被误当成聚合列
|
|
79
|
+
r = await q('SELECT CASE WHEN v>2 THEN 1 ELSE 0 END AS f FROM t ORDER BY id');
|
|
80
|
+
ok('#52 无聚合 CASE 仍逐行', j(r.rows) === j([[0], [0], [1], [1]]), r.rows);
|
|
81
|
+
|
|
60
82
|
console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILURES'} — ${pass} passed, ${fail} failed\n`);
|
|
61
83
|
if (fail > 0) process.exit(1);
|
|
62
84
|
})();
|
package/test/issue-53.test.js
CHANGED
|
@@ -41,6 +41,39 @@ try {
|
|
|
41
41
|
const left = big.search(5);
|
|
42
42
|
ok('#53 批量删除后剩余行完整', JSON.stringify(left) === JSON.stringify(Array.from({ length: 20 }, (_, i) => i + 20)), left);
|
|
43
43
|
ok('#53 批量删除后 size 按 distinct keys 计(1)', big.size === 1, big.size);
|
|
44
|
+
|
|
45
|
+
// 多叶子场景:40 个 key × 5 行,order=4 会分裂成多层;逐行删除全部应都返回 true
|
|
46
|
+
const multi = new BTree(4);
|
|
47
|
+
for (let k = 0; k < 40; k++) for (let r = 0; r < 5; r++) multi.insert(k, k * 100 + r);
|
|
48
|
+
ok('#53 多叶子:size 按 distinct keys 计', multi.size === 40, multi.size);
|
|
49
|
+
let allTrue = true;
|
|
50
|
+
let rowsRemoved = 0;
|
|
51
|
+
for (let k = 0; k < 40; k++) {
|
|
52
|
+
for (let r = 0; r < 5; r++) {
|
|
53
|
+
if (multi.remove(k, k * 100 + r) !== true) allTrue = false;
|
|
54
|
+
rowsRemoved++;
|
|
55
|
+
const expect = 4 - r; // 删到第 r 行后剩下的行数
|
|
56
|
+
const leftN = multi.search(k).length;
|
|
57
|
+
if (leftN !== expect) { allTrue = false; console.log('[FAIL] 残留行数不对', k, r, leftN, expect); }
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
ok('#53 多叶子:删除全部行都返回 true 且无残留', allTrue && rowsRemoved === 200, { allTrue, rowsRemoved });
|
|
61
|
+
ok('#53 多叶子:全部删完后 size 归零', multi.size === 0, multi.size);
|
|
62
|
+
|
|
63
|
+
// 删除不存在的行不能改动已有行;重复删除同一行第二次应返回 false
|
|
64
|
+
const noop = new BTree(4);
|
|
65
|
+
noop.insert(3, 30);
|
|
66
|
+
noop.insert(3, 31);
|
|
67
|
+
ok('#53 删不存在的 rowIndex 返回 false 且 no-op', noop.remove(3, 99) === false && noop.size === 1);
|
|
68
|
+
ok('#53 同一行重复删除第二次返回 false', noop.remove(3, 30) === true && noop.remove(3, 30) === false);
|
|
69
|
+
ok('#53 重复删除后剩余行不变', JSON.stringify(noop.search(3)) === JSON.stringify([31]), noop.search(3));
|
|
70
|
+
|
|
71
|
+
// unique 索引:重复插入被忽略,删除仍应返回 true
|
|
72
|
+
const uni = new BTree(64, true);
|
|
73
|
+
uni.insert(5, 1);
|
|
74
|
+
uni.insert(5, 2); // unique:忽略
|
|
75
|
+
ok('#53 unique:重复插入不增加 size', uni.size === 1, uni.size);
|
|
76
|
+
ok('#53 unique:删除返回 true 且 size 归零', uni.remove(5, 1) === true && uni.size === 0, uni.size);
|
|
44
77
|
} catch (e) {
|
|
45
78
|
fail++;
|
|
46
79
|
console.log('[FAIL] unexpected exception:', (e && e.stack) || e);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Issue #59 — WHERE 子句拒绝裸布尔谓词(#21/#42 回归)。
|
|
3
|
+
*
|
|
4
|
+
* WHERE 1 / WHERE col / WHERE name 这类没有显式比较运算符的谓词,
|
|
5
|
+
* 在布尔上下文里应按 MySQL 三值逻辑的 truthiness 求值:
|
|
6
|
+
* 数字/布尔 → <> 0;字符串 → 非空;NULL → 未知(不匹配)。
|
|
7
|
+
*/
|
|
8
|
+
const path = require('path');
|
|
9
|
+
const ROOT = path.join(__dirname, '..');
|
|
10
|
+
const { Database } = require(path.join(ROOT, 'index.js'));
|
|
11
|
+
const { executeSQL } = require(path.join(ROOT, 'lib', 'sql.js'));
|
|
12
|
+
|
|
13
|
+
let pass = 0, fail = 0;
|
|
14
|
+
function ok(name, cond, extra) {
|
|
15
|
+
if (cond) { pass++; console.log('[OK] ', name); }
|
|
16
|
+
else { fail++; console.log('[FAIL]', name, extra !== undefined ? '-> ' + JSON.stringify(extra).slice(0, 400) : ''); }
|
|
17
|
+
}
|
|
18
|
+
const j = (rows) => JSON.stringify(rows);
|
|
19
|
+
|
|
20
|
+
(async () => {
|
|
21
|
+
const db = new Database(':memory:');
|
|
22
|
+
const q = (sql) => executeSQL(db, sql, { safety: false });
|
|
23
|
+
|
|
24
|
+
await q('CREATE TABLE t (id INT PRIMARY KEY, active INT, name TEXT, flag INT)');
|
|
25
|
+
await q("INSERT INTO t VALUES (1, 1, 'a', 1), (2, 0, NULL, 0), (3, 5, 'c', 1)");
|
|
26
|
+
|
|
27
|
+
// 裸列谓词
|
|
28
|
+
let r = await q('SELECT id FROM t WHERE active');
|
|
29
|
+
ok('#59 WHERE active(整数列 truthiness)=> [1,3]', j(r.rows) === j([[1], [3]]), r.rows);
|
|
30
|
+
|
|
31
|
+
r = await q('SELECT id FROM t WHERE name');
|
|
32
|
+
ok('#59 WHERE name(非空字符串)=> [1,3]', j(r.rows) === j([[1], [3]]), r.rows);
|
|
33
|
+
|
|
34
|
+
r = await q('SELECT id FROM t WHERE flag');
|
|
35
|
+
ok('#59 WHERE flag => [1,3]', j(r.rows) === j([[1], [3]]), r.rows);
|
|
36
|
+
|
|
37
|
+
// 裸常量谓词
|
|
38
|
+
r = await q('SELECT id FROM t WHERE 1');
|
|
39
|
+
ok('#59 WHERE 1 恒真 => 全部行', j(r.rows) === j([[1], [2], [3]]), r.rows);
|
|
40
|
+
|
|
41
|
+
r = await q('SELECT id FROM t WHERE 0');
|
|
42
|
+
ok('#59 WHERE 0 恒假 => 空', j(r.rows) === j([]), r.rows);
|
|
43
|
+
|
|
44
|
+
r = await q("SELECT id FROM t WHERE ''");
|
|
45
|
+
ok('#59 WHERE 空字符串 => 空', j(r.rows) === j([]), r.rows);
|
|
46
|
+
|
|
47
|
+
// 与 ORDER BY / 逻辑运算组合
|
|
48
|
+
r = await q('SELECT id FROM t WHERE active ORDER BY id DESC');
|
|
49
|
+
ok('#59 WHERE 裸谓词 + ORDER BY', j(r.rows) === j([[3], [1]]), r.rows);
|
|
50
|
+
|
|
51
|
+
r = await q('SELECT id FROM t WHERE 1 AND id > 1 ORDER BY id');
|
|
52
|
+
ok('#59 WHERE 裸谓词 AND 比较式', j(r.rows) === j([[2], [3]]), r.rows);
|
|
53
|
+
|
|
54
|
+
r = await q('SELECT id FROM t WHERE 0 OR active');
|
|
55
|
+
ok('#59 WHERE 裸谓词 OR 裸谓词', j(r.rows) === j([[1], [3]]), r.rows);
|
|
56
|
+
|
|
57
|
+
// HAVING 裸谓词
|
|
58
|
+
r = await q('SELECT active, COUNT(*) AS c FROM t GROUP BY active HAVING flag ORDER BY active');
|
|
59
|
+
ok('#59 HAVING 裸谓词', j(r.rows) === j([[1, 1], [5, 1]]), r.rows);
|
|
60
|
+
|
|
61
|
+
// 不破坏 #21/#42 既有比较语义
|
|
62
|
+
r = await q('SELECT id FROM t WHERE active = 1');
|
|
63
|
+
ok('#59 普通比较未回归(= 1)', j(r.rows) === j([[1]]), r.rows);
|
|
64
|
+
|
|
65
|
+
r = await q('SELECT id FROM t WHERE active IN (0, 5) ORDER BY id');
|
|
66
|
+
ok('#59 普通比较未回归(IN)', j(r.rows) === j([[2], [3]]), r.rows);
|
|
67
|
+
|
|
68
|
+
r = await q('SELECT id FROM t WHERE name IS NULL');
|
|
69
|
+
ok('#59 IS NULL 未回归', j(r.rows) === j([[2]]), r.rows);
|
|
70
|
+
|
|
71
|
+
r = await q('SELECT id FROM t WHERE active BETWEEN 1 AND 3');
|
|
72
|
+
ok('#59 BETWEEN 未回归', j(r.rows) === j([[1]]), r.rows);
|
|
73
|
+
|
|
74
|
+
console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILURES'} — ${pass} passed, ${fail} failed\n`);
|
|
75
|
+
if (fail > 0) process.exit(1);
|
|
76
|
+
})();
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Issue #60 — 集合运算不校验等列数(静默截断)+ 括号化集合查询被拒。
|
|
3
|
+
*
|
|
4
|
+
* 1) UNION / INTERSECT / EXCEPT 两侧列数不一致时必须显式报错(MySQL 1222),
|
|
5
|
+
* 而不是按左分支静默丢弃右侧多出的列。
|
|
6
|
+
* 2) 集合运算的操作数允许括号化:(SELECT ...) UNION (SELECT ...)。
|
|
7
|
+
* 括号内自带的 ORDER BY / LIMIT / OFFSET 只作用于该操作数;
|
|
8
|
+
* 括号外的尾部子句作用于整个组合查询。
|
|
9
|
+
*/
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const ROOT = path.join(__dirname, '..');
|
|
12
|
+
const { Database } = require(path.join(ROOT, 'index.js'));
|
|
13
|
+
const { executeSQL } = require(path.join(ROOT, 'lib', 'sql.js'));
|
|
14
|
+
|
|
15
|
+
let pass = 0, fail = 0;
|
|
16
|
+
function ok(name, cond, extra) {
|
|
17
|
+
if (cond) { pass++; console.log('[OK] ', name); }
|
|
18
|
+
else { fail++; console.log('[FAIL]', name, extra !== undefined ? '-> ' + JSON.stringify(extra).slice(0, 400) : ''); }
|
|
19
|
+
}
|
|
20
|
+
const j = (rows) => JSON.stringify(rows);
|
|
21
|
+
|
|
22
|
+
async function expectErr(q, sql, needle) {
|
|
23
|
+
try {
|
|
24
|
+
await q(sql);
|
|
25
|
+
return { ok: false, reason: 'no error thrown' };
|
|
26
|
+
} catch (e) {
|
|
27
|
+
const msg = String(e && e.message);
|
|
28
|
+
return { ok: msg.includes(needle), reason: msg, code: e && e.code };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
(async () => {
|
|
33
|
+
const db = new Database(':memory:');
|
|
34
|
+
const q = (sql) => executeSQL(db, sql, { safety: false });
|
|
35
|
+
|
|
36
|
+
await q('CREATE TABLE t (id INT PRIMARY KEY, v INT, s TEXT)');
|
|
37
|
+
await q("INSERT INTO t VALUES (1, 10, 'a'), (2, 20, 'b')");
|
|
38
|
+
|
|
39
|
+
// ---- 列数校验 ----
|
|
40
|
+
const wsp = 'The used SELECT statements have a different number of columns';
|
|
41
|
+
let e = await expectErr(q, 'SELECT id FROM t UNION SELECT id, s FROM t', wsp);
|
|
42
|
+
ok('#60 UNION 列数不同报错', e.ok, e.reason);
|
|
43
|
+
|
|
44
|
+
e = await expectErr(q, 'SELECT id FROM t UNION ALL SELECT id, s FROM t', wsp);
|
|
45
|
+
ok('#60 UNION ALL 列数不同报错', e.ok, e.reason);
|
|
46
|
+
|
|
47
|
+
e = await expectErr(q, 'SELECT id FROM t INTERSECT SELECT id, s FROM t', wsp);
|
|
48
|
+
ok('#60 INTERSECT 列数不同报错', e.ok, e.reason);
|
|
49
|
+
|
|
50
|
+
e = await expectErr(q, 'SELECT id FROM t EXCEPT SELECT id, s FROM t', wsp);
|
|
51
|
+
ok('#60 EXCEPT 列数不同报错', e.ok, e.reason);
|
|
52
|
+
|
|
53
|
+
let r = await q('SELECT id FROM t UNION SELECT v FROM t');
|
|
54
|
+
ok('#60 等列数 UNION 正常', j(r.rows) === j([[1], [2], [10], [20]]), r.rows);
|
|
55
|
+
|
|
56
|
+
r = await q('SELECT id FROM t UNION SELECT id FROM t');
|
|
57
|
+
ok('#60 同列数单列 UNION 正常', j(r.rows) === j([[1], [2]]), r.rows);
|
|
58
|
+
|
|
59
|
+
// ---- 括号化操作数 ----
|
|
60
|
+
r = await q('(SELECT id FROM t) UNION (SELECT id FROM t)');
|
|
61
|
+
ok('#60 (SELECT) UNION (SELECT) 可解析', j(r.rows) === j([[1], [2]]), r.rows);
|
|
62
|
+
|
|
63
|
+
r = await q('(SELECT id FROM t WHERE id = 1) UNION ALL (SELECT id FROM t WHERE id = 2)');
|
|
64
|
+
ok('#60 括号化 UNION ALL', j(r.rows) === j([[1], [2]]), r.rows);
|
|
65
|
+
|
|
66
|
+
r = await q('SELECT id FROM t UNION (SELECT id FROM t)');
|
|
67
|
+
ok('#60 右侧括号化操作数', j(r.rows) === j([[1], [2]]), r.rows);
|
|
68
|
+
|
|
69
|
+
r = await q('(SELECT id FROM t) INTERSECT (SELECT id FROM t WHERE id = 2)');
|
|
70
|
+
ok('#60 括号化 INTERSECT', j(r.rows) === j([[2]]), r.rows);
|
|
71
|
+
|
|
72
|
+
r = await q('(SELECT id FROM t) EXCEPT (SELECT id FROM t WHERE id = 1)');
|
|
73
|
+
ok('#60 括号化 EXCEPT', j(r.rows) === j([[2]]), r.rows);
|
|
74
|
+
|
|
75
|
+
// 括号外尾部 ORDER BY / LIMIT 作用于整体
|
|
76
|
+
r = await q('(SELECT id FROM t) UNION (SELECT id FROM t) ORDER BY id DESC LIMIT 1');
|
|
77
|
+
ok('#60 括号化组合 + 尾部 ORDER BY/LIMIT', j(r.rows) === j([[2]]), r.rows);
|
|
78
|
+
|
|
79
|
+
// 括号内 ORDER BY / LIMIT 只作用于该操作数
|
|
80
|
+
r = await q('(SELECT id FROM t ORDER BY id DESC LIMIT 1) UNION (SELECT id FROM t ORDER BY id ASC LIMIT 1)');
|
|
81
|
+
ok('#60 括号内 LIMIT 局部生效', j(r.rows) === j([[2], [1]]), r.rows);
|
|
82
|
+
|
|
83
|
+
r = await q('(SELECT id FROM t ORDER BY id DESC LIMIT 1) UNION ALL (SELECT id FROM t LIMIT 1)');
|
|
84
|
+
ok('#60 UNION ALL 保留括号内局部 LIMIT', j(r.rows) === j([[2], [1]]), r.rows);
|
|
85
|
+
|
|
86
|
+
// 括号化操作数也参与列数校验
|
|
87
|
+
e = await expectErr(q, '(SELECT id FROM t) UNION (SELECT id, s FROM t)', wsp);
|
|
88
|
+
ok('#60 括号化操作数列数不同报错', e.ok, e.reason);
|
|
89
|
+
|
|
90
|
+
console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILURES'} — ${pass} passed, ${fail} failed\n`);
|
|
91
|
+
if (fail > 0) process.exit(1);
|
|
92
|
+
})();
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/* ---- issue #63~#68 回归 ---- */
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const ROOT = path.join(__dirname, '..');
|
|
4
|
+
const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
|
|
5
|
+
|
|
6
|
+
let pass = 0, fail = 0;
|
|
7
|
+
function ok(name, cond, got) {
|
|
8
|
+
if (cond) { pass++; console.log('[OK] ' + name); }
|
|
9
|
+
else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
|
|
10
|
+
}
|
|
11
|
+
const isNull = (v) => v === null || v === undefined;
|
|
12
|
+
|
|
13
|
+
(async () => {
|
|
14
|
+
const db = new Database(':memory:', { autoSave: false });
|
|
15
|
+
const q = async (sql) => (await executeSQL(db, sql, { safety: false })).rows[0][0];
|
|
16
|
+
|
|
17
|
+
// ---------- #63 POSITION(substr IN str) ----------
|
|
18
|
+
ok('#63 POSITION 命中', await q("SELECT POSITION('b' IN 'abc')") === 2, await q("SELECT POSITION('b' IN 'abc')"));
|
|
19
|
+
ok('#63 POSITION 未命中返回 0', await q("SELECT POSITION('z' IN 'abc')") === 0, await q("SELECT POSITION('z' IN 'abc')"));
|
|
20
|
+
ok('#63 与 LOCATE 等价', await q("SELECT LOCATE('b','abc')") === await q("SELECT POSITION('b' IN 'abc')"), null);
|
|
21
|
+
ok('#63 支持列引用', await q("SELECT POSITION('b' IN CONCAT('a','bc'))") === 2, null);
|
|
22
|
+
|
|
23
|
+
// ---------- #64 日期类型拒绝非法数字 ----------
|
|
24
|
+
const dt = require(path.join(ROOT, 'lib', 'date-types.js'));
|
|
25
|
+
for (const v of [NaN, Infinity, -Infinity, 1e20]) {
|
|
26
|
+
let threw = false;
|
|
27
|
+
try { dt.validateDateType(v, 'date', 'd'); } catch (e) { threw = /Incorrect date value/.test(e.message); }
|
|
28
|
+
ok('#64 date 拒绝 ' + String(v), threw, null);
|
|
29
|
+
let threw2 = false;
|
|
30
|
+
try { dt.validateDateType(v, 'datetime', 'd'); } catch (e) { threw2 = /Incorrect date value/.test(e.message); }
|
|
31
|
+
ok('#64 datetime 拒绝 ' + String(v), threw2, null);
|
|
32
|
+
}
|
|
33
|
+
ok('#64 合法时间戳仍可用', dt.validateDateType(0, 'date', 'd') === '1970-01-01', dt.validateDateType(0, 'date', 'd'));
|
|
34
|
+
|
|
35
|
+
// ---------- #65 数值域错误返回 NULL ----------
|
|
36
|
+
for (const sql of ['SELECT SQRT(-1)', 'SELECT POWER(-2,0.5)', 'SELECT SQRT(-1) + 5', 'SELECT LOG(-5)', 'SELECT ACOS(2)']) {
|
|
37
|
+
const v = await q(sql);
|
|
38
|
+
ok('#65 ' + sql + ' → NULL', isNull(v), v);
|
|
39
|
+
}
|
|
40
|
+
ok('#65 SQRT(4) 仍为 2', await q('SELECT SQRT(4)') === 2, null);
|
|
41
|
+
ok('#65 POWER(2,3) 仍为 8', await q('SELECT POWER(2,3)') === 8, null);
|
|
42
|
+
|
|
43
|
+
// ---------- #66 / #67 Redis RESP 序列化 ----------
|
|
44
|
+
const { createRedisServer } = require(path.join(ROOT, 'lib', 'redis_server.js'));
|
|
45
|
+
const srv = createRedisServer({ port: 0 });
|
|
46
|
+
function send(cmd, args) {
|
|
47
|
+
const out = [];
|
|
48
|
+
srv._handle({ write: (s) => out.push(s) }, cmd, args);
|
|
49
|
+
return out.join('');
|
|
50
|
+
}
|
|
51
|
+
send('SET', ['a', '1']);
|
|
52
|
+
const mget = send('MGET', ['a', 'missing']);
|
|
53
|
+
ok('#66 MGET 缺失键为 nil bulk', mget.includes('$-1\r\n') && !mget.includes('$4\r\nnull'), mget);
|
|
54
|
+
ok('#66 MGET 存在的键仍正常', mget.includes('$1\r\n1\r\n'), mget);
|
|
55
|
+
|
|
56
|
+
send('ZADD', ['z', '10.5', 'm']);
|
|
57
|
+
const zscore = send('ZSCORE', ['z', 'm']);
|
|
58
|
+
ok('#67 ZSCORE 小数分数为 bulk string', zscore === '$4\r\n10.5\r\n', zscore);
|
|
59
|
+
const zincr = send('ZINCRBY', ['z', '0.5', 'm']);
|
|
60
|
+
// 10.5 + 0.5 = 11,"11" 是 2 字节,所以是 $2 而不是 $3
|
|
61
|
+
ok('#67 ZINCRBY 分数为 bulk string', /^\$2\r\n11\r\n$/.test(zincr) || /^\$\d+\r\n11\.0+\r\n$/.test(zincr), zincr);
|
|
62
|
+
send('ZADD', ['zi', '10', 'm']);
|
|
63
|
+
const zint = send('ZSCORE', ['zi', 'm']);
|
|
64
|
+
ok('#67 整数分数也是 bulk(不是 :10)', zint === '$2\r\n10\r\n', zint);
|
|
65
|
+
ok('#67 _send 非整数 number 不产出非法 integer reply',
|
|
66
|
+
(() => { const o = []; srv._send({ write: (s) => o.push(s) }, 10.5); return o[0] === '$4\r\n10.5\r\n'; })(), null);
|
|
67
|
+
ok('#67 _send 整数仍是 integer reply',
|
|
68
|
+
(() => { const o = []; srv._send({ write: (s) => o.push(s) }, 3); return o[0] === ':3\r\n'; })(), null);
|
|
69
|
+
|
|
70
|
+
// ---------- #68 XQL ORDER BY 非投影列 ----------
|
|
71
|
+
const { xql } = require(path.join(ROOT, 'lib', 'plugins'));
|
|
72
|
+
const db2 = new Database(':memory:', { autoSave: false });
|
|
73
|
+
await db2.createTable('users', {
|
|
74
|
+
id: { type: 'integer', primaryKey: true, autoIncrement: true },
|
|
75
|
+
name: { type: 'string' }, age: { type: 'integer' },
|
|
76
|
+
});
|
|
77
|
+
await db2.insert('users', [{ name: 'alice', age: 30 }, { name: 'bob', age: 25 }, { name: 'carol', age: 30 }]);
|
|
78
|
+
db2.use(xql());
|
|
79
|
+
const both = await db2.xql('FROM users |> ORDER BY age DESC |> SELECT name, age');
|
|
80
|
+
const nameOnly = await db2.xql('FROM users |> ORDER BY age DESC |> SELECT name');
|
|
81
|
+
ok('#68 投影含排序列时结果正确', both.map(r => r.name).join(',') === 'alice,carol,bob', both.map(r => r.name));
|
|
82
|
+
ok('#68 排序列不在投影里也生效', nameOnly.map(r => r.name).join(',') === 'alice,carol,bob', nameOnly.map(r => r.name));
|
|
83
|
+
const asc = await db2.xql('FROM users |> ORDER BY age ASC |> SELECT name');
|
|
84
|
+
ok('#68 升序同样生效', asc[0].name === 'bob', asc.map(r => r.name));
|
|
85
|
+
|
|
86
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
87
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
88
|
+
})().catch(e => {
|
|
89
|
+
console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
|
|
90
|
+
process.exit(1);
|
|
91
|
+
});
|