jsql-neo 6.6.5 → 6.7.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  > **One engine to rule them all** — a Rust-powered embedded database that speaks your language:
4
4
  > MySQL, PostgreSQL, MongoDB, Redis, SQL, TypeScript, **and the browser**.
5
5
 
6
- > **v6.6.5** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
6
+ > **v6.7.0-beta.1** — 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
  ![Engines](https://img.shields.io/badge/engines-Native%20%7C%20WASM%20%7C%20Pure%20JS-7ee787)
9
9
  ![MySQL](https://img.shields.io/badge/protocol-MySQL%20compatible-1f6feb)
@@ -196,6 +196,35 @@ To check which native addon this installation is actually using:
196
196
  `UNVERIFIED` / `UNREADABLE`.
197
197
  - `jsql native refresh` — re-fetches a checksum-verified addon
198
198
 
199
+ ## SQL functions
200
+
201
+ Full reference: [doc/sql-reference.md](doc/sql-reference.md).
202
+
203
+ | Group | Functions |
204
+ | --- | --- |
205
+ | JSON | `JSON_EXTRACT`, `JSON_CONTAINS` (path + type-sensitive), `JSON_SEARCH` (case-sensitive, LIKE wildcards), `JSON_SET`, `JSON_REMOVE`, `JSON_MERGE_PATCH`, … |
206
+ | Date / time | UTC wall-clock semantics; `TO_DAYS`, `FROM_DAYS`, `TO_SECONDS`, `TIME_TO_SEC`, `SEC_TO_TIME`, `DATE_ADD`, `DATE_FORMAT`, `TIMESTAMPDIFF`, … |
207
+ | Base / utility | `BIN`, `OCT`, `HEX`, `UNHEX`, `CONV`, `BIT_LENGTH`, `CRC32`, `QUOTE`, `FIND_IN_SET`, `INSERT`, `FORMAT`, `INET_ATON` / `INET_NTOA`, `MAKE_SET`, `EXPORT_SET`, `SOUNDEX` |
208
+ | Exact integers | Values above `2^53` stay exact via BigInt; integer-math functions use exact paths, floating-point ones return DOUBLE |
209
+
210
+ `JSON_TYPE` / `JSON_DEPTH` follow **this project's** documented semantics
211
+ (lowercase type names, flat object depth 1), not MySQL's. See the SQL reference.
212
+
213
+ ## Columnar storage (one-shot convert)
214
+
215
+ ```js
216
+ db.convertToColumnar('events'); // split each column into its own array
217
+ db.convertToRow('events'); // back to row storage
218
+ ```
219
+
220
+ A **derived in-memory view**, not an on-disk format: any write invalidates it
221
+ (reads fall back to row storage), so the two can never disagree. Aggregates and
222
+ single-column `GROUP BY` scan the column arrays directly (no row objects).
223
+
224
+ Point queries measure **2.7–6.5x faster than DuckDB**; aggregation is now
225
+ **2–8x slower** (it was 36–70x before the vectorized column path). Still behind —
226
+ not claimed otherwise. Honest numbers: [doc/columnar.md](doc/columnar.md).
227
+
199
228
  ## ✨ XQL 集成
200
229
 
201
230
  用 [XQL](https://github.com/vexify-org/XQL) 查询 JSQL 表:把 SQL 直接接到文档上,
@@ -243,6 +272,10 @@ report the reason instead of failing silently. Loopback traffic (the
243
272
  `127.0.0.1` links between `jsql serve` and the client / Web UI) is not
244
273
  external access and is unaffected.
245
274
 
275
+ `jsql native status|refresh|verify` is **entirely local** — it reads
276
+ `native/.installed.json` and hashes the on-disk addon. `verify --json` is
277
+ suitable for CI gates. See [doc/cli.md](doc/cli.md).
278
+
246
279
  ## 📖 Documentation
247
280
 
248
281
  Detailed docs live in `doc/` — click to read.
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * JSQL-NEO v6.6.5 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.7.0-beta.1 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
@@ -0,0 +1,245 @@
1
+ 'use strict';
2
+ /**
3
+ * 列存的向量化快路径 —— 纯函数,不依赖 SQLExecutor,便于单测。
4
+ *
5
+ * 输入是 `Table._columnar`({ names, cols: Map<col, array[]>, rowCount }),
6
+ * 输出是 SELECT 结果骨架;返回 null 表示「这条查询不适用快路径」,
7
+ * 由调用方走通用行路径。
8
+ */
9
+
10
+ /** AST 节点 → 列名;非简单列引用返回 null。 */
11
+ function colNameOf(node) {
12
+ if (!node) return null;
13
+ if (typeof node === 'string') return node;
14
+ return node.type === 'column' ? node.name : null;
15
+ }
16
+
17
+ /**
18
+ * 把 WHERE 编译成「命中行下标数组」。
19
+ * 只支持 `列 OP 字面量` 以及它们的 AND 组合;其它(OR / LIKE / 函数 / 子查询 /
20
+ * 列与列比较)一律返回 null,交给通用路径 —— 宁可慢,不可错。
21
+ */
22
+ function whereIndices(statement, data) {
23
+ const w = statement.where;
24
+ if (!w) return null;
25
+ const build = (node) => {
26
+ if (!node) return null;
27
+ if (node.type === 'and') {
28
+ const l = build(node.left), r = build(node.right);
29
+ if (!l || !r) return null;
30
+ return { and: [l, r] };
31
+ }
32
+ if (node.type === 'compare') {
33
+ const col = colNameOf(node.left);
34
+ if (!col || !node.right || node.right.type !== 'value') return null;
35
+ const arr = data.cols.get(col);
36
+ if (!arr) return null;
37
+ return { arr, op: node.op, val: node.right.value };
38
+ }
39
+ return null;
40
+ };
41
+ const root = build(w);
42
+ if (!root) return null;
43
+
44
+ const n = data.rowCount;
45
+ const idx = new Uint32Array(n);
46
+ let m = 0;
47
+ const test = (x, op, v) => {
48
+ switch (op) {
49
+ case '=': return x === v;
50
+ case '!=': case '<>': return x !== v;
51
+ case '>': return x > v;
52
+ case '>=': return x >= v;
53
+ case '<': return x < v;
54
+ case '<=': return x <= v;
55
+ default: return false;
56
+ }
57
+ };
58
+ const hit = (node, i) => (node.and
59
+ ? hit(node.and[0], i) && hit(node.and[1], i)
60
+ : test(node.arr[i], node.op, node.val));
61
+ for (let i = 0; i < n; i++) if (hit(root, i)) idx[m++] = i;
62
+ return idx.subarray(0, m);
63
+ }
64
+
65
+ /**
66
+ * 纯聚合(无 GROUP BY / HAVING / DISTINCT / LIMIT)。
67
+ * 直接扫列数组,不构造任何行对象。
68
+ */
69
+ function aggregate(statement, data) {
70
+ const cols = statement.columns || [];
71
+ if (!cols.length) return null;
72
+ if (statement.groupBy || statement.having || statement.distinct) return null;
73
+ if (statement.limit != null && statement.limit !== Infinity) return null;
74
+
75
+ const indices = statement.where ? whereIndices(statement, data) : null;
76
+ if (statement.where && !indices) return null;
77
+ const scanLen = indices ? indices.length : data.rowCount;
78
+ const at = (k) => (indices ? indices[k] : k);
79
+
80
+ const outRow = [];
81
+ const outCols = [];
82
+ for (const c of cols) {
83
+ if (!c || !c.aggregate) return null;
84
+ // COUNT(DISTINCT x) / SUM(DISTINCT x) 需要去重,快路径不实现 → 退回通用路径。
85
+ // 漏这条会让 COUNT(DISTINCT age) 直接返回行数(静默错值)。
86
+ if (c.distinct) return null;
87
+ const fn = String(c.aggregate).toUpperCase();
88
+ const argNode = c.column;
89
+ const argName = colNameOf(argNode);
90
+ if (argNode != null && !argName) return null;
91
+ const values = argName ? data.cols.get(argName) : null;
92
+ if (argName && !values) return null;
93
+
94
+ let v;
95
+ switch (fn) {
96
+ case 'COUNT': {
97
+ if (!argName) { v = scanLen; break; }
98
+ let n = 0;
99
+ for (let k = 0; k < scanLen; k++) {
100
+ const x = values[at(k)];
101
+ if (x !== null && x !== undefined) n++;
102
+ }
103
+ v = n;
104
+ break;
105
+ }
106
+ case 'SUM': {
107
+ let s = 0, seen = false;
108
+ for (let k = 0; k < scanLen; k++) {
109
+ const x = values[at(k)];
110
+ if (x === null || x === undefined) continue;
111
+ if (typeof x !== 'number') return null; // BigInt 等 → 通用路径
112
+ s += x; seen = true;
113
+ }
114
+ v = seen ? s : null;
115
+ break;
116
+ }
117
+ case 'AVG': {
118
+ let s = 0, n = 0;
119
+ for (let k = 0; k < scanLen; k++) {
120
+ const x = values[at(k)];
121
+ if (x === null || x === undefined) continue;
122
+ if (typeof x !== 'number') return null;
123
+ s += x; n++;
124
+ }
125
+ v = n ? s / n : null;
126
+ break;
127
+ }
128
+ case 'MIN': case 'MAX': {
129
+ let best, seen = false;
130
+ for (let k = 0; k < scanLen; k++) {
131
+ const x = values[at(k)];
132
+ if (x === null || x === undefined) continue;
133
+ if (typeof x !== 'number' && typeof x !== 'string') return null;
134
+ if (!seen) { best = x; seen = true; }
135
+ else if (fn === 'MIN' ? x < best : x > best) best = x;
136
+ }
137
+ v = seen ? best : null;
138
+ break;
139
+ }
140
+ default: return null;
141
+ }
142
+ outRow.push(v);
143
+ outCols.push(c.alias || (fn + '()'));
144
+ }
145
+ return { type: 'select', columns: outCols, rows: [outRow] };
146
+ }
147
+
148
+ /**
149
+ * 单列 GROUP BY + 聚合。
150
+ *
151
+ * 条件:GROUP BY 恰好一列、投影 = 该分组列 + COUNT/SUM/AVG/MIN/MAX、
152
+ * 无 HAVING / DISTINCT / LIMIT。命中时扫一遍分组列建桶,再输出每组的聚合值,
153
+ * 全程不构造行对象。
154
+ *
155
+ * 注意输出顺序是「分组值首次出现的顺序」,不保证与通用路径一致 ——
156
+ * 需要确定性顺序请加 ORDER BY(那样会退回通用路径)。
157
+ */
158
+ function groupBy(statement, data) {
159
+ const g = statement.groupBy;
160
+ if (!Array.isArray(g) || g.length !== 1) return null;
161
+ if (statement.having || statement.distinct) return null;
162
+ if (statement.limit != null) return null;
163
+ const gName = colNameOf(g[0]);
164
+ if (!gName) return null;
165
+ const keyArr = data.cols.get(gName);
166
+ if (!keyArr) return null;
167
+
168
+ const cols = statement.columns || [];
169
+ if (!cols.length) return null;
170
+ const aggs = [];
171
+ let sawGroupCol = false;
172
+ for (const c of cols) {
173
+ if (c && c.aggregate) {
174
+ // 同 aggregate:DISTINCT 聚合不适用快路径
175
+ if (c.distinct) return null;
176
+ const fn = String(c.aggregate).toUpperCase();
177
+ if (!['COUNT', 'SUM', 'AVG', 'MIN', 'MAX'].includes(fn)) return null;
178
+ const argName = colNameOf(c.column);
179
+ if (c.column != null && !argName) return null;
180
+ const arr = argName ? data.cols.get(argName) : null;
181
+ if (argName && !arr) return null;
182
+ aggs.push({ c, fn, argName, arr });
183
+ continue;
184
+ }
185
+ const n = colNameOf(c && (c.scalar || c.expr));
186
+ if (n !== gName) return null;
187
+ sawGroupCol = true;
188
+ }
189
+ if (!sawGroupCol) return null;
190
+
191
+ const indices = statement.where ? whereIndices(statement, data) : null;
192
+ if (statement.where && !indices) return null;
193
+ const scanLen = indices ? indices.length : data.rowCount;
194
+ const at = (k) => (indices ? indices[k] : k);
195
+
196
+ // 分组的键多数是数字或短字符串,用 Map 即可;每组一个累加器数组。
197
+ const groups = new Map();
198
+ for (let k = 0; k < scanLen; k++) {
199
+ const i = at(k);
200
+ const key = keyArr[i];
201
+ let acc = groups.get(key);
202
+ if (!acc) {
203
+ acc = aggs.map(() => ({ n: 0, s: 0, best: undefined, seen: false }));
204
+ groups.set(key, acc);
205
+ }
206
+ for (let j = 0; j < aggs.length; j++) {
207
+ const a = aggs[j], st = acc[j];
208
+ if (a.fn === 'COUNT') {
209
+ if (!a.argName) { st.n++; continue; }
210
+ const x = a.arr[i];
211
+ if (x !== null && x !== undefined) st.n++;
212
+ continue;
213
+ }
214
+ const x = a.arr[i];
215
+ if (x === null || x === undefined) continue;
216
+ if (typeof x !== 'number') return null; // BigInt 等 → 通用路径
217
+ st.n++; st.s += x;
218
+ if (!st.seen) { st.best = x; st.seen = true; }
219
+ else if (a.fn === 'MIN' ? x < st.best : x > st.best) st.best = x;
220
+ }
221
+ }
222
+
223
+ const outRows = [];
224
+ for (const [key, acc] of groups) {
225
+ const row = [];
226
+ for (const c of cols) {
227
+ if (c.aggregate) {
228
+ const j = aggs.findIndex(a => a.c === c);
229
+ const st = acc[j];
230
+ const fn = String(c.aggregate).toUpperCase();
231
+ if (fn === 'COUNT') row.push(st.n);
232
+ else if (fn === 'SUM') row.push(st.seen ? st.s : null);
233
+ else if (fn === 'AVG') row.push(st.n ? st.s / st.n : null);
234
+ else row.push(st.seen ? st.best : null);
235
+ } else {
236
+ row.push(key);
237
+ }
238
+ }
239
+ outRows.push(row);
240
+ }
241
+ const outCols = cols.map(c => c.alias || (c.aggregate ? String(c.aggregate).toUpperCase() + '()' : gName));
242
+ return { type: 'select', columns: outCols, rows: outRows };
243
+ }
244
+
245
+ module.exports = { colNameOf, whereIndices, aggregate, groupBy };
package/lib/database.js CHANGED
@@ -1869,6 +1869,27 @@ class Database {
1869
1869
  return rows;
1870
1870
  }
1871
1871
 
1872
+ /**
1873
+ * 一键把表转成列存(按字段拆成「每列一个数组」)。
1874
+ *
1875
+ * 适合「批量导入 → 转换 → 反复扫描/聚合」的读多写少场景。
1876
+ * `_rows` 始终是权威数据;列存是查询用的派生缓存,任何写入都会让它
1877
+ * 失效(下次读取自动回到行存路径),因此不会出现两份数据不一致。
1878
+ * 需要重新转换后可再次调用。
1879
+ */
1880
+ convertToColumnar(tableName) {
1881
+ const table = this._ensureTable(tableName);
1882
+ if (!table) throw createError('ER_NO_SUCH_TABLE', tableName);
1883
+ return table.toColumnar();
1884
+ }
1885
+
1886
+ /** 把表转回行存(丢弃列存缓存)。 */
1887
+ convertToRow(tableName) {
1888
+ const table = this._ensureTable(tableName);
1889
+ if (!table) throw createError('ER_NO_SUCH_TABLE', tableName);
1890
+ return table.toRow();
1891
+ }
1892
+
1872
1893
  exists(tableName, filter = {}) {
1873
1894
  const table = this._ensureTable(tableName);
1874
1895
  if (!table) throw createError('ER_NO_SUCH_TABLE', tableName);
@@ -4,6 +4,8 @@
4
4
  * Listens on ONE port and sniffs the first bytes of each connection to route
5
5
  * to the correct wire-protocol handler:
6
6
  *
7
+ * - MongoDB : OP_MSG / OP_QUERY opcode at offset 12 (checked first — its
8
+ * little-endian length can start with 0x00 and look like PG)
7
9
  * - PostgreSQL : first byte 0x00 (big-endian Int32 length, length < 2^24)
8
10
  * - MySQL : first byte 0x0a/0x0d (protocol handshake version / cap flag)
9
11
  * - Redis : anything else (ASCII command or RESP prefix)