jsql-neo 6.7.0-beta.2 → 6.7.0-beta.3
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/columnar_fast.js +82 -4
- package/package.json +1 -1
- package/test/columnar-vector.test.js +14 -2
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.0-beta.
|
|
6
|
+
> **v6.7.0-beta.3** — beta 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/columnar_fast.js
CHANGED
|
@@ -41,6 +41,32 @@ function whereIndices(statement, data) {
|
|
|
41
41
|
if (!arr) return null;
|
|
42
42
|
return { arr, op: node.op, val: node.right.value };
|
|
43
43
|
}
|
|
44
|
+
if (node.type === 'in') {
|
|
45
|
+
const col = colNameOf(node.operand);
|
|
46
|
+
if (!col || !Array.isArray(node.list)) return null;
|
|
47
|
+
// 列表里含 NULL / 非标量时语义变复杂(NULL IN (...) 是 UNKNOWN),交回通用路径。
|
|
48
|
+
if (node.list.some(v => v === null || v === undefined || typeof v === 'object')) return null;
|
|
49
|
+
const arr = data.cols.get(col);
|
|
50
|
+
if (!arr) return null;
|
|
51
|
+
return { arr, inList: node.list, not: !!node.not };
|
|
52
|
+
}
|
|
53
|
+
if (node.type === 'between') {
|
|
54
|
+
const col = colNameOf(node.operand);
|
|
55
|
+
if (!col) return null;
|
|
56
|
+
const lo = node.low && node.low.type === 'value' ? node.low.value : undefined;
|
|
57
|
+
const hi = node.high && node.high.type === 'value' ? node.high.value : undefined;
|
|
58
|
+
if (lo === undefined || hi === undefined) return null;
|
|
59
|
+
const arr = data.cols.get(col);
|
|
60
|
+
if (!arr) return null;
|
|
61
|
+
return { arr, between: [lo, hi], not: !!node.not };
|
|
62
|
+
}
|
|
63
|
+
if (node.type === 'isNull') {
|
|
64
|
+
const col = colNameOf(node.operand);
|
|
65
|
+
if (!col) return null;
|
|
66
|
+
const arr = data.cols.get(col);
|
|
67
|
+
if (!arr) return null;
|
|
68
|
+
return { arr, isNull: true, not: !!node.not };
|
|
69
|
+
}
|
|
44
70
|
return null;
|
|
45
71
|
};
|
|
46
72
|
const root = build(w);
|
|
@@ -63,7 +89,24 @@ function whereIndices(statement, data) {
|
|
|
63
89
|
const hit = (node, i) => {
|
|
64
90
|
if (node.and) return hit(node.and[0], i) && hit(node.and[1], i);
|
|
65
91
|
if (node.or) return hit(node.or[0], i) || hit(node.or[1], i);
|
|
66
|
-
|
|
92
|
+
const x = node.arr[i];
|
|
93
|
+
if (node.inList) {
|
|
94
|
+
// NULL IN (...) / NULL NOT IN (...) 都是 UNKNOWN → 不命中
|
|
95
|
+
if (x === null || x === undefined) return false;
|
|
96
|
+
const found = node.inList.indexOf(x) >= 0;
|
|
97
|
+
return node.not ? !found : found;
|
|
98
|
+
}
|
|
99
|
+
if (node.between) {
|
|
100
|
+
// NULL 在 BETWEEN 里是 UNKNOWN → 正向/反向都不命中
|
|
101
|
+
if (x === null || x === undefined) return false;
|
|
102
|
+
const okRange = x >= node.between[0] && x <= node.between[1];
|
|
103
|
+
return node.not ? !okRange : okRange;
|
|
104
|
+
}
|
|
105
|
+
if (node.isNull) {
|
|
106
|
+
const isn = x === null || x === undefined;
|
|
107
|
+
return node.not ? !isn : isn;
|
|
108
|
+
}
|
|
109
|
+
return test(x, node.op, node.val);
|
|
67
110
|
};
|
|
68
111
|
for (let i = 0; i < n; i++) if (hit(root, i)) idx[m++] = i;
|
|
69
112
|
return idx.subarray(0, m);
|
|
@@ -166,7 +209,8 @@ function groupBy(statement, data) {
|
|
|
166
209
|
const g = statement.groupBy;
|
|
167
210
|
if (!Array.isArray(g) || !g.length) return null;
|
|
168
211
|
if (statement.having || statement.distinct) return null;
|
|
169
|
-
|
|
212
|
+
// ORDER BY 只支持单个键(多键交给通用路径,避免排序规则漂移)
|
|
213
|
+
if (statement.orderBy && statement.orderBy.length !== 1) return null;
|
|
170
214
|
const gNames = g.map(colNameOf);
|
|
171
215
|
if (gNames.some(n => !n)) return null;
|
|
172
216
|
const keyArrs = gNames.map(n => data.cols.get(n));
|
|
@@ -207,7 +251,13 @@ function groupBy(statement, data) {
|
|
|
207
251
|
const i = at(k);
|
|
208
252
|
// 多列分组键:用 \u0000 拼接(SQL 字符串列不会包含 NUL),Map 里只存这个串。
|
|
209
253
|
let ck = '';
|
|
210
|
-
for (let q = 0; q < nKeys; q++)
|
|
254
|
+
for (let q = 0; q < nKeys; q++) {
|
|
255
|
+
const kv = keyArrs[q][i];
|
|
256
|
+
// NULL 分组键交给通用路径:用 String(null) === 'null' 会把 NULL 组和
|
|
257
|
+
// 字符串 'null' 组错误地合并成一组。
|
|
258
|
+
if (kv === null || kv === undefined) return null;
|
|
259
|
+
ck += (q ? '\u0000' : '') + String(kv);
|
|
260
|
+
}
|
|
211
261
|
let entry = groups.get(ck);
|
|
212
262
|
if (!entry) {
|
|
213
263
|
entry = {
|
|
@@ -234,7 +284,7 @@ function groupBy(statement, data) {
|
|
|
234
284
|
}
|
|
235
285
|
}
|
|
236
286
|
|
|
237
|
-
|
|
287
|
+
let outRows = [];
|
|
238
288
|
for (const [, entry] of groups) {
|
|
239
289
|
const row = [];
|
|
240
290
|
for (const c of cols) {
|
|
@@ -256,6 +306,34 @@ function groupBy(statement, data) {
|
|
|
256
306
|
const outCols = cols.map(c => c.alias || (c.aggregate
|
|
257
307
|
? String(c.aggregate).toUpperCase() + '()'
|
|
258
308
|
: colNameOf(c.scalar || c.expr)));
|
|
309
|
+
|
|
310
|
+
// ORDER BY:在分组结果上排序 —— 组数通常远小于行数,代价很低。
|
|
311
|
+
// 只支持按输出列名 / 别名排序;找不到对应列就退回通用路径,
|
|
312
|
+
// 避免两边排序规则(尤其 NULL 位置)出现分叉。
|
|
313
|
+
if (statement.orderBy) {
|
|
314
|
+
const o = statement.orderBy[0];
|
|
315
|
+
const name = typeof o.column === 'string' ? o.column : colNameOf(o.column);
|
|
316
|
+
const idx = name ? outCols.indexOf(name) : -1;
|
|
317
|
+
if (idx < 0) return null;
|
|
318
|
+
const dir = o.dir === 'desc' ? -1 : 1;
|
|
319
|
+
outRows.sort((a, b) => {
|
|
320
|
+
const av = a[idx], bv = b[idx];
|
|
321
|
+
if (av === bv) return 0;
|
|
322
|
+
if (av === null || av === undefined) return -1 * dir;
|
|
323
|
+
if (bv === null || bv === undefined) return 1 * dir;
|
|
324
|
+
const d = (typeof av === 'number' && typeof bv === 'number')
|
|
325
|
+
? (av < bv ? -1 : 1)
|
|
326
|
+
: (String(av) < String(bv) ? -1 : 1);
|
|
327
|
+
return d * dir;
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// LIMIT / OFFSET:分组后切片(与通用路径语义一致)
|
|
332
|
+
if (statement.offset || statement.limit != null) {
|
|
333
|
+
const off = Number(statement.offset) || 0;
|
|
334
|
+
const end = statement.limit == null ? undefined : off + Number(statement.limit);
|
|
335
|
+
outRows = outRows.slice(off, end);
|
|
336
|
+
}
|
|
259
337
|
return { type: 'select', columns: outCols, rows: outRows };
|
|
260
338
|
}
|
|
261
339
|
|
package/package.json
CHANGED
|
@@ -34,7 +34,8 @@ const norm = (rows) => JSON.stringify(
|
|
|
34
34
|
});
|
|
35
35
|
const rows = [];
|
|
36
36
|
for (let i = 0; i < 5000; i++) {
|
|
37
|
-
|
|
37
|
+
// 每 13 行留一个 NULL name:IS NULL / GROUP BY 遇到 NULL 分组键都要与行存一致
|
|
38
|
+
rows.push({ age: 20 + (i % 50), score: (i % 1000) / 10, name: i % 13 === 0 ? null : 'n' + (i % 7) });
|
|
38
39
|
}
|
|
39
40
|
for (let i = 0; i < rows.length; i += 500) await db.insert('t', rows.slice(i, i + 500));
|
|
40
41
|
|
|
@@ -64,11 +65,22 @@ const norm = (rows) => JSON.stringify(
|
|
|
64
65
|
'SELECT COUNT(*) AS c FROM t WHERE age > 30 OR score > 90',
|
|
65
66
|
'SELECT SUM(score) AS s FROM t WHERE age > 30 OR (score > 90 AND age > 20)',
|
|
66
67
|
'SELECT age, COUNT(*) AS c FROM t WHERE age > 30 OR score > 90 GROUP BY age',
|
|
68
|
+
// WHERE IN / NOT IN / BETWEEN / IS NULL
|
|
69
|
+
'SELECT COUNT(*) AS c FROM t WHERE age IN (30, 40)',
|
|
70
|
+
'SELECT COUNT(*) AS c FROM t WHERE age NOT IN (30, 40)',
|
|
71
|
+
'SELECT SUM(score) AS s FROM t WHERE age BETWEEN 20 AND 30',
|
|
72
|
+
'SELECT COUNT(*) AS c FROM t WHERE age NOT BETWEEN 20 AND 30',
|
|
73
|
+
'SELECT COUNT(*) AS c FROM t WHERE name IS NULL',
|
|
74
|
+
'SELECT COUNT(*) AS c FROM t WHERE name IS NOT NULL',
|
|
75
|
+
// GROUP BY + ORDER BY / LIMIT / OFFSET
|
|
76
|
+
'SELECT age, COUNT(*) AS c FROM t GROUP BY age ORDER BY age DESC',
|
|
77
|
+
'SELECT age, COUNT(*) AS c FROM t GROUP BY age ORDER BY c DESC LIMIT 3',
|
|
78
|
+
'SELECT age, COUNT(*) AS c FROM t GROUP BY age ORDER BY age LIMIT 4 OFFSET 2',
|
|
67
79
|
// 应退回通用路径(结果仍必须一致)
|
|
68
80
|
'SELECT COUNT(*) AS c FROM t LIMIT 0',
|
|
69
81
|
'SELECT COUNT(DISTINCT age) AS c FROM t',
|
|
70
82
|
'SELECT SUM(age + 1) AS s FROM t',
|
|
71
|
-
'SELECT age, COUNT(*) AS c FROM t GROUP BY age ORDER BY age',
|
|
83
|
+
'SELECT age, COUNT(*) AS c FROM t GROUP BY age ORDER BY age, c DESC',
|
|
72
84
|
];
|
|
73
85
|
|
|
74
86
|
const before = {};
|