jsql-neo 6.7.0-beta.4 → 6.7.0

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.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)
6
+ > **v6.7.0** — 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
  ![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)
@@ -222,8 +222,9 @@ A **derived in-memory view**, not an on-disk format: any write invalidates it
222
222
  single-column `GROUP BY` scan the column arrays directly (no row objects).
223
223
 
224
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).
225
+ **2–6x slower** (it was 36–70x before the vectorized column path). Still behind —
226
+ not claimed otherwise. `JOIN`-then-aggregate is **not** optimized yet and falls
227
+ back to the row path. Honest numbers and limits: [doc/columnar.md](doc/columnar.md).
227
228
 
228
229
  ## ✨ XQL 集成
229
230
 
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * JSQL-NEO v6.7.0-beta.4 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.7.0 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
package/lib/sql.js CHANGED
@@ -7071,7 +7071,10 @@ class SQLExecutor {
7071
7071
  const tblObj = this.engine && this.engine._tables ? this.engine._tables[table] : null;
7072
7072
  if (tblObj && typeof tblObj.isColumnar === 'function' && tblObj.isColumnar()
7073
7073
  && statement && !statement.where && !statement.groupBy
7074
- && !statement.having && !statement.orderBy && !statement.distinct) {
7074
+ && !statement.having && !statement.distinct
7075
+ // ORDER BY 只放行「单键 + 带 LIMIT」:这时只需排索引数组取前 K 行,
7076
+ // 不必把 10 万行都物化成对象。无 LIMIT 的全量排序仍走通用路径。
7077
+ && (!statement.orderBy || (statement.orderBy.length === 1 && statement.limit != null))) {
7075
7078
  const hooks = this.engine._hooks;
7076
7079
  const hasFindHooks = !!(hooks && (
7077
7080
  (Array.isArray(hooks.beforeFind) && hooks.beforeFind.length) ||
@@ -7079,9 +7082,57 @@ class SQLExecutor {
7079
7082
  const wanted = hasFindHooks ? null : this._columnarColumns(statement);
7080
7083
  if (wanted) {
7081
7084
  const data = tblObj._columnar;
7082
- const names = wanted === '*' ? data.names : wanted;
7085
+ let names = wanted === '*' ? data.names : wanted;
7086
+ // ORDER BY 的列可能不在投影里(SELECT id FROM t ORDER BY age)。
7087
+ // 行里必须带上它,否则后续 _validateColumnRefs 报 no such column,
7088
+ // 排序也无从取值。多带的这一列在最终投影时会被忽略。
7089
+ const ordStmt = statement.orderBy && statement.orderBy[0];
7090
+ if (ordStmt) {
7091
+ const kn = typeof ordStmt.column === 'string'
7092
+ ? ordStmt.column
7093
+ : (ordStmt.column && ordStmt.column.type === 'column' ? ordStmt.column.name : null);
7094
+ if (kn && !names.includes(kn)) names = names.concat(kn);
7095
+ }
7083
7096
  const arrays = names.map(c => data.cols.get(c));
7084
7097
  if (arrays.every(Array.isArray)) {
7098
+ // ORDER BY + LIMIT:只排索引数组、物化前 K 行,避免把 10 万行都建成
7099
+ // 对象再排序。同值用原始下标决胜,等价于 Array.prototype.sort 的稳定
7100
+ // 语义,所以结果与「全排序后切前 K 行」完全一致(后续 executeSelect
7101
+ // 仍会再排一次并切片,得到的就是正确的那几行)。
7102
+ const ord = statement.orderBy && statement.orderBy[0];
7103
+ if (ord) {
7104
+ const keyName = typeof ord.column === 'string'
7105
+ ? ord.column
7106
+ : (ord.column && ord.column.type === 'column' ? ord.column.name : null);
7107
+ const keyArr = keyName ? data.cols.get(keyName) : null;
7108
+ if (keyArr) {
7109
+ const total = data.rowCount;
7110
+ const idx = new Array(total);
7111
+ for (let i = 0; i < total; i++) idx[i] = i;
7112
+ const dir = ord.dir === 'desc' ? -1 : 1;
7113
+ idx.sort((a, b) => {
7114
+ const av = keyArr[a], bv = keyArr[b];
7115
+ if (av === bv) return a - b;
7116
+ if (av === null || av === undefined) return -1 * dir;
7117
+ if (bv === null || bv === undefined) return 1 * dir;
7118
+ const d = (typeof av === 'number' && typeof bv === 'number')
7119
+ ? (av < bv ? -1 : 1)
7120
+ : (String(av) < String(bv) ? -1 : 1);
7121
+ return d * dir;
7122
+ });
7123
+ const off = Number(statement.offset) || 0;
7124
+ const k = Math.min(total, off + Number(statement.limit));
7125
+ const out = new Array(k);
7126
+ for (let j = 0; j < k; j++) {
7127
+ const i = idx[j];
7128
+ const o = {};
7129
+ for (let t = 0; t < names.length; t++) o[names[t]] = arrays[t][i];
7130
+ out[j] = o;
7131
+ }
7132
+ if (typeof this.engine._touchTable === 'function') this.engine._touchTable(table);
7133
+ return { schema, rows: out };
7134
+ }
7135
+ }
7085
7136
  const capC = this._pushdownLimitCap(statement);
7086
7137
  const n = capC == null ? data.rowCount : Math.min(capC, data.rowCount);
7087
7138
  const out = new Array(n);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "6.7.0-beta.4",
3
+ "version": "6.7.0",
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",
@@ -83,14 +83,33 @@ const norm = (rows) => JSON.stringify(
83
83
  'SELECT age, COUNT(*) AS c FROM t GROUP BY age ORDER BY age, c DESC',
84
84
  ];
85
85
 
86
+ // ORDER BY + LIMIT:不仅结果集相同,**顺序**也必须一致(含同值行的相对
87
+ // 顺序)。所以这一组基线用保序的 JSON.stringify,不走 norm 的排序。
88
+ const ORDER_QUERIES = [
89
+ 'SELECT id FROM t ORDER BY age LIMIT 10',
90
+ 'SELECT id FROM t ORDER BY age DESC LIMIT 10',
91
+ 'SELECT id, age FROM t ORDER BY age LIMIT 10 OFFSET 5',
92
+ 'SELECT name FROM t ORDER BY name LIMIT 5',
93
+ 'SELECT * FROM t ORDER BY score DESC LIMIT 3',
94
+ 'SELECT id FROM t ORDER BY age LIMIT 10 OFFSET 4995',
95
+ ];
96
+
86
97
  const before = {};
87
98
  for (const q of QUERIES) {
88
99
  try { before[q] = norm((await db.query(q)).rows); }
89
100
  catch (e) { before[q] = 'ERR ' + e.message; }
90
101
  }
102
+ const beforeOrder = {};
103
+ for (const q of ORDER_QUERIES) beforeOrder[q] = JSON.stringify((await db.query(q)).rows);
91
104
 
92
105
  db.convertToColumnar('t');
93
106
 
107
+ for (const q of ORDER_QUERIES) {
108
+ const got = JSON.stringify((await db.query(q)).rows);
109
+ ok('顺序一致:' + q.slice(0, 56), got === beforeOrder[q],
110
+ { got: got.slice(0, 48), exp: String(beforeOrder[q]).slice(0, 48) });
111
+ }
112
+
94
113
  for (const q of QUERIES) {
95
114
  let got;
96
115
  try { got = norm((await db.query(q)).rows); }