jsql-neo 6.7.0-beta.2 → 6.7.0-beta.4
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 +120 -15
- 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.4** — 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));
|
|
@@ -201,20 +245,52 @@ function groupBy(statement, data) {
|
|
|
201
245
|
const at = (k) => (indices ? indices[k] : k);
|
|
202
246
|
|
|
203
247
|
// 分组的键多数是数字或短字符串,用 Map 即可;每组一个累加器数组。
|
|
204
|
-
const groups = new Map();
|
|
205
248
|
const nKeys = keyArrs.length;
|
|
249
|
+
|
|
250
|
+
// 分组容器:单列整数键且落在 [0, 65535] 时用数组桶 —— 10 万行 × 50 组的
|
|
251
|
+
// 基准里,Map.get/set 的哈希开销占了 GROUP BY 的大头。多列或非整数键
|
|
252
|
+
// 退回 Map,行为不变。
|
|
253
|
+
let bucket = null;
|
|
254
|
+
if (nKeys === 1) {
|
|
255
|
+
let maxKey = -1;
|
|
256
|
+
let usable = true;
|
|
257
|
+
for (let k = 0; k < scanLen; k++) {
|
|
258
|
+
const v = keyArrs[0][at(k)];
|
|
259
|
+
if (typeof v !== 'number' || !Number.isInteger(v) || v < 0 || v > 65535) { usable = false; break; }
|
|
260
|
+
if (v > maxKey) maxKey = v;
|
|
261
|
+
}
|
|
262
|
+
if (usable) bucket = new Array(maxKey + 1);
|
|
263
|
+
}
|
|
264
|
+
const groups = bucket ? null : new Map();
|
|
265
|
+
|
|
206
266
|
for (let k = 0; k < scanLen; k++) {
|
|
207
267
|
const i = at(k);
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
268
|
+
let entry;
|
|
269
|
+
if (bucket) {
|
|
270
|
+
const key = keyArrs[0][i];
|
|
271
|
+
entry = bucket[key];
|
|
272
|
+
if (!entry) {
|
|
273
|
+
entry = { keyVals: [key], acc: aggs.map(() => ({ n: 0, s: 0, best: undefined, seen: false })) };
|
|
274
|
+
bucket[key] = entry;
|
|
275
|
+
}
|
|
276
|
+
} else {
|
|
277
|
+
// 多列分组键:用 \u0000 拼接(SQL 字符串列不会包含 NUL),Map 里只存这个串。
|
|
278
|
+
let ck = '';
|
|
279
|
+
for (let q = 0; q < nKeys; q++) {
|
|
280
|
+
const kv = keyArrs[q][i];
|
|
281
|
+
// NULL 分组键交给通用路径:用 String(null) === 'null' 会把 NULL 组和
|
|
282
|
+
// 字符串 'null' 组错误地合并成一组。
|
|
283
|
+
if (kv === null || kv === undefined) return null;
|
|
284
|
+
ck += (q ? '\u0000' : '') + String(kv);
|
|
285
|
+
}
|
|
286
|
+
entry = groups.get(ck);
|
|
287
|
+
if (!entry) {
|
|
288
|
+
entry = {
|
|
289
|
+
keyVals: keyArrs.map(a => a[i]),
|
|
290
|
+
acc: aggs.map(() => ({ n: 0, s: 0, best: undefined, seen: false })),
|
|
291
|
+
};
|
|
292
|
+
groups.set(ck, entry);
|
|
293
|
+
}
|
|
218
294
|
}
|
|
219
295
|
const acc = entry.acc;
|
|
220
296
|
for (let j = 0; j < aggs.length; j++) {
|
|
@@ -234,8 +310,9 @@ function groupBy(statement, data) {
|
|
|
234
310
|
}
|
|
235
311
|
}
|
|
236
312
|
|
|
237
|
-
const
|
|
238
|
-
|
|
313
|
+
const entries = bucket ? bucket.filter(Boolean) : Array.from(groups.values());
|
|
314
|
+
let outRows = [];
|
|
315
|
+
for (const entry of entries) {
|
|
239
316
|
const row = [];
|
|
240
317
|
for (const c of cols) {
|
|
241
318
|
if (c.aggregate) {
|
|
@@ -256,6 +333,34 @@ function groupBy(statement, data) {
|
|
|
256
333
|
const outCols = cols.map(c => c.alias || (c.aggregate
|
|
257
334
|
? String(c.aggregate).toUpperCase() + '()'
|
|
258
335
|
: colNameOf(c.scalar || c.expr)));
|
|
336
|
+
|
|
337
|
+
// ORDER BY:在分组结果上排序 —— 组数通常远小于行数,代价很低。
|
|
338
|
+
// 只支持按输出列名 / 别名排序;找不到对应列就退回通用路径,
|
|
339
|
+
// 避免两边排序规则(尤其 NULL 位置)出现分叉。
|
|
340
|
+
if (statement.orderBy) {
|
|
341
|
+
const o = statement.orderBy[0];
|
|
342
|
+
const name = typeof o.column === 'string' ? o.column : colNameOf(o.column);
|
|
343
|
+
const idx = name ? outCols.indexOf(name) : -1;
|
|
344
|
+
if (idx < 0) return null;
|
|
345
|
+
const dir = o.dir === 'desc' ? -1 : 1;
|
|
346
|
+
outRows.sort((a, b) => {
|
|
347
|
+
const av = a[idx], bv = b[idx];
|
|
348
|
+
if (av === bv) return 0;
|
|
349
|
+
if (av === null || av === undefined) return -1 * dir;
|
|
350
|
+
if (bv === null || bv === undefined) return 1 * dir;
|
|
351
|
+
const d = (typeof av === 'number' && typeof bv === 'number')
|
|
352
|
+
? (av < bv ? -1 : 1)
|
|
353
|
+
: (String(av) < String(bv) ? -1 : 1);
|
|
354
|
+
return d * dir;
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// LIMIT / OFFSET:分组后切片(与通用路径语义一致)
|
|
359
|
+
if (statement.offset || statement.limit != null) {
|
|
360
|
+
const off = Number(statement.offset) || 0;
|
|
361
|
+
const end = statement.limit == null ? undefined : off + Number(statement.limit);
|
|
362
|
+
outRows = outRows.slice(off, end);
|
|
363
|
+
}
|
|
259
364
|
return { type: 'select', columns: outCols, rows: outRows };
|
|
260
365
|
}
|
|
261
366
|
|
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 = {};
|