fix: v0.7.3 数据正确性与边界窗口收尾 — INSERT 语句级原子(三引擎+Aria PK 批内重复)/ 索引列 IS NULL 恒空 / delete RESTRICT 破坏索引 / queryStream 子查询静默空结果 / ALTER DROP 索引残留 / UNIQUE INDEX 存量校验 / SELECT * 别名投影 / WAL BEGIN/ROLLBACK 事务边界 / aria $in 与级联重复扫描性能 / $and 等值下推 / ANALYZE 索引统计 / React-Vue hooks 生命周期 / 迁移主键兜底 + 58 回归
CI / test (18.x) (push) Successful in 17m43s
CI / test (22.x) (push) Successful in 13m45s
CI / test (20.x) (push) Successful in 15m33s
CI / test (24.x) (push) Successful in 24m46s
CI / e2e (push) Successful in 52s

This commit is contained in:
thzxx
2026-08-14 22:53:46 +08:00
parent f8f8d1b2ff
commit 50468b9b0e
29 changed files with 2374 additions and 650 deletions
+360 -156
View File
@@ -36,7 +36,7 @@
// ---------------------------------------------------------------------------
// 版本
// ---------------------------------------------------------------------------
const VERSION = '0.7.2';
const VERSION = '0.7.3';
/**
* metona-sqlark Shared WHERE Matcher 统一的条件匹配逻辑
@@ -441,6 +441,11 @@
if (!schema.columns[column.name]) {
throw new DatabaseError(`Column "${column.name}" does not exist in table "${tableName}"`, 'COLUMN_NOT_FOUND');
}
// v0.7.3: 被删列是索引列 → 同步清理索引 Map —— 此前残留旧索引:
// 查询已删列仍走旧索引(不含新行)→ 结果不完整(对齐 AriaEngine cleanupTableIndexes
if (schema.columns[column.name].index || schema.columns[column.name].unique) {
this.indexes.get(tableName)?.delete(column.name);
}
delete schema.columns[column.name];
// 清理已有行中该列的值(find 返回行引用,直接删除生效)
const table = this.tables.get(tableName);
@@ -456,18 +461,42 @@
const table = this.tables.get(tableName);
const pkColumn = this.getPrimaryKey(schema);
const pks = [];
// v0.7.3: 语句级原子性 —— 两阶段(先全量预检,后执行)。
// 此前逐行"校验+写入":第 N 行主键重复/唯一冲突抛错时,前 N-1 行已提交
// (无事务下语句级部分提交,与 v0.7.2 修复的 UPDATE 同类问题)。
const validated = [];
const pkSet = new Set();
const batchUnique = new Map();
// 阶段 1:全量预检(任何一行失败 → 整条语句不执行)
for (const row of rows) {
const validatedRow = this.validateRow(schema, row);
const pkValue = String(validatedRow[pkColumn]);
if (table.has(pkValue))
// 批内主键互查(内存表尚未反映本批写入)
if (table.has(pkValue) || pkSet.has(pkValue)) {
throw new DatabaseError(`Duplicate primary key "${pkValue}" in table "${tableName}"`, 'DUPLICATE_KEY');
this.checkUniqueness(schema, validatedRow);
}
pkSet.add(pkValue);
// v0.7.3: 批内唯一互查 + 索引查(此前两行同批写入同一唯一值时,
// 第一行已写入索引 → 第二行 checkUniqueness 抛错 → 第一行残留)
this.checkInsertUniqueness(schema, tableName, validatedRow, batchUnique);
validated.push(validatedRow);
}
// 阶段 2:执行(预检已通过,此阶段不再抛校验类错误)
for (const validatedRow of validated) {
const pkValue = String(validatedRow[pkColumn]);
table.set(pkValue, validatedRow);
this.updateIndexes(tableName, validatedRow, pkValue);
pks.push(pkValue);
}
return pks;
}
/** v0.7.3: 按主键取已验证行(KVStoreEngine 持久化 validated 行用,含 default/类型归一) */
getRow(tableName, pkValue) {
const table = this.tables.get(tableName);
if (!table)
return null;
return table.get(pkValue) ?? null;
}
async find(tableName, query) {
this.ensureTable(tableName);
const table = this.tables.get(tableName);
@@ -560,7 +589,37 @@
return count;
}
/**
* v0.7.2: 更新唯一性预检 批内互查多条行更新到同一唯一值+ 索引查
* v0.7.3: 插入唯一性预检 批内互查本批前几行写入同一唯一值
* + 索引查表中已有行 update checkUpdateUniqueness 对称
* 两阶段 insert 预检阶段调用索引尚未反映本批写入
*/
checkInsertUniqueness(schema, tableName, row, batchUnique) {
const tableIndexes = this.indexes.get(tableName);
for (const [colName, colDef] of Object.entries(schema.columns)) {
if (!colDef.unique)
continue;
const value = row[colName];
if (value === undefined || value === null)
continue;
let seen = batchUnique.get(colName);
if (!seen) {
seen = new Set();
batchUnique.set(colName, seen);
}
if (seen.has(value)) {
throw new DatabaseError(`Unique constraint violation on column "${colName}" in table "${schema.name}"`, 'UNIQUE_VIOLATION');
}
seen.add(value);
if (!tableIndexes)
continue;
const colIndex = tableIndexes.get(colName);
if (colIndex && colIndex.has(value)) {
throw new DatabaseError(`Unique constraint violation on column "${colName}" in table "${schema.name}"`, 'UNIQUE_VIOLATION');
}
}
}
/**
* v0.7.3: 更新唯一性预检 批内互查多条行更新到同一唯一值+ 索引查
* 排除自身旧条目阶段 1 中索引尚未更新批内互查避免"两行同时改到
* 同一新值"绕过唯一约束
*/
@@ -630,30 +689,11 @@
/**
* v0.4.2-fix: ON UPDATE 外键级联 被引用表主键变更时处理引用表
* RESTRICT 抛错 / CASCADE 更新 FK / SET NULL 置空
* 分两阶段先全量 RESTRICT 检查任何修改前再执行级联防部分修改
* v0.7.3-perf: 删除冗余的阶段 1 RESTRICT 扫描 checkUpdateRestrict 已在
* 两阶段 update 预检阶段 1b覆盖 RESTRICT SET NULL+required
* 此处任何修改前重复全表扫描纯属浪费直接执行 CASCADE / SET NULL
*/
async applyUpdateCascade(tableName, oldPk, newPk) {
// 阶段 1: RESTRICT 检查(引用旧主键的行存在即拒绝)
for (const [refTableName, refSchema] of this.schemas) {
if (refTableName === tableName)
continue;
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
if (!colDef.references || !colDef.onUpdate)
continue;
const [refTable] = colDef.references.split('.');
if (refTable !== tableName)
continue;
const refTableData = this.tables.get(refTableName);
if (!refTableData)
continue;
for (const [, refRow] of refTableData) {
if (String(refRow[colName]) === oldPk && colDef.onUpdate === 'RESTRICT') {
throw new DatabaseError(`Cannot update "${tableName}" key "${oldPk}": foreign key "${colName}" in "${refTableName}" has dependent rows`, 'FOREIGN_KEY_VIOLATION');
}
}
}
}
// 阶段 2: CASCADE / SET NULL
for (const [refTableName, refSchema] of this.schemas) {
if (refTableName === tableName)
continue;
@@ -684,28 +724,27 @@
const toDelete = [];
for (const [pk, row] of table) {
if (!query.where || Object.keys(query.where).length === 0 || matchWhere(row, query.where)) {
// v0.3.3: 删除行前清理其索引条目(修复删除后索引残留)
this.removeIndexEntries(tableName, row, pk);
toDelete.push(pk);
toDelete.push({ pk, row });
}
}
// v0.6.3-fix: 级联两阶段 —— 先对全部待删行做 RESTRICT 预检(沿 CASCADE 链递归),
// 任何一行违规则整体拒绝。此前逐行执行:第 N 行 RESTRICT 抛错时,前 N-1 行的
// 级联子行已被删除、父行未删 → 无事务下部分级联(数据不一致)
//
// v0.7.3-fix: 索引清理移到预检之后 —— 此前 removeIndexEntries 在收集阶段执行,
// RESTRICT 预检抛错时行未删但索引条目已删 → 唯一约束失效、索引查询丢行
const restrictVisited = new Set();
for (const pk of toDelete) {
const row = table.get(pk);
if (row)
this.checkCascadeRestrict(tableName, pk, restrictVisited);
for (const { pk } of toDelete) {
this.checkCascadeRestrict(tableName, pk, restrictVisited);
}
// 级联删除:检查引用此表的其他表(RESTRICT 已预检通过,此阶段不再抛
// 预检通过:清理索引 + 级联删除(此阶段不再抛校验类错误
let cascadeCount = 0;
for (const pk of toDelete) {
const row = table.get(pk);
if (row)
cascadeCount += await this.cascadeDelete(tableName, pk, row);
for (const { pk, row } of toDelete) {
// v0.3.3: 删除行前清理其索引条目(修复删除后索引残留)
this.removeIndexEntries(tableName, row, pk);
cascadeCount += await this.cascadeDelete(tableName, pk, row);
}
for (const pk of toDelete)
for (const { pk } of toDelete)
table.delete(pk);
return toDelete.length + cascadeCount;
}
@@ -784,22 +823,34 @@
throw new DatabaseError(`Column "${column}" does not exist in table "${tableName}"`, 'COLUMN_NOT_FOUND');
if (colDef.index || colDef.unique)
return; // 已存在
colDef.index = true;
if (unique)
colDef.unique = true;
const tableIndexes = this.indexes.get(tableName);
if (!tableIndexes.has(column))
tableIndexes.set(column, new Map());
const colIndex = tableIndexes.get(column);
const table = this.tables.get(tableName);
for (const [pk, row] of table) {
const value = row[column];
if (value !== undefined && value !== null) {
if (!colIndex.has(value))
colIndex.set(value, new Set());
colIndex.get(value).add(pk);
try {
for (const [pk, row] of table) {
const value = row[column];
if (value !== undefined && value !== null) {
// v0.7.3: UNIQUE 索引回填校验存量唯一性 —— 此前重复数据静默建索引
// (SQLite 语义应报错),且此后该列唯一约束永远无法满足
if (unique && colIndex.has(value)) {
throw new DatabaseError(`Unique index on column "${column}" in table "${tableName}" cannot be created: duplicate value "${String(value)}"`, 'UNIQUE_VIOLATION');
}
if (!colIndex.has(value))
colIndex.set(value, new Set());
colIndex.get(value).add(pk);
}
}
}
catch (error) {
// 回填失败(唯一冲突):清理半初始化索引,标志未落,保持原子语义
tableIndexes.delete(column);
throw error;
}
colDef.index = true;
if (unique)
colDef.unique = true;
}
async dropIndex(tableName, column, _indexName) {
// v0.7.2: 同 createIndex —— 列级标志修改无法通过事务快照回滚,显式拒绝
@@ -922,26 +973,30 @@
break;
}
}
/** O(1) 唯一性检查:利用哈希索引 */
checkUniqueness(schema, row) {
const tableIndexes = this.indexes.get(schema.name);
if (!tableIndexes)
return;
for (const [colName, colDef] of Object.entries(schema.columns)) {
if (!colDef.unique || row[colName] === undefined || row[colName] === null)
continue;
const colIndex = tableIndexes.get(colName);
if (colIndex && colIndex.has(row[colName])) {
throw new DatabaseError(`Unique constraint violation on column "${colName}" in table "${schema.name}"`, 'UNIQUE_VIOLATION');
}
}
}
/** 索引查找 */
tryIndexLookup(tableName, table, query) {
const tableIndexes = this.indexes.get(tableName);
if (!tableIndexes || !query.where)
return Array.from(table.values());
for (const [col, condition] of Object.entries(query.where)) {
// v0.7.3: 递归展开 $and 中的等值条件 —— 此前仅顶层键,
// `WHERE a AND b`(解析为顶层 $and)永远全表扫描,索引形同虚设。
// $or/$not 语义不适用单索引下推,保守跳过。命中索引后 find 仍以
// 全条件 matchWhere 过滤(子集语义安全)。
const flat = [];
const collect = (w) => {
for (const [k, v] of Object.entries(w)) {
if (k === '$and') {
for (const sub of v)
collect(sub);
continue;
}
if (k === '$or' || k === '$not')
continue;
flat.push([k, v]);
}
};
collect(query.where);
for (const [col, condition] of flat) {
// v0.4.1: 支持 { $eq: value } 形式(SQL 解析器生成的等值条件)走索引
let targetValue;
if (typeof condition !== 'object' || condition === null) {
@@ -953,6 +1008,11 @@
else {
continue;
}
// v0.7.3: null/undefined 条件不走索引 —— 索引不含 null 条目,
// colIndex.get(null) 恒 undefined → return [] 短路全表扫描 → 索引列
// IS NULL 恒空(对齐 AriaEngine v0.6.2 修复)
if (targetValue === null || targetValue === undefined)
continue;
const colIndex = tableIndexes.get(col);
if (colIndex) {
const pks = colIndex.get(targetValue);
@@ -2299,11 +2359,14 @@
const schema = await this.memory.getTableSchema(tableName);
if (!schema)
throw new DatabaseError(`Table "${tableName}" does not exist`, 'TABLE_NOT_FOUND');
const pkCol = this.getPK(schema);
// v0.7.3: 持久化内存中的 validated 行(含 default 值/类型归一/列投影)——
// 此前写原始入参 rowdefault 不落盘、schema 外列被持久化,重启后行不一致
const puts = {};
rows.forEach((row, i) => {
puts[this.rowKey(tableName, String(pks[i] ?? row[pkCol]))] = enc(JSON.stringify(row));
});
for (const pk of pks) {
const row = this.memory.getRow(tableName, pk);
if (row)
puts[this.rowKey(tableName, pk)] = enc(JSON.stringify(row));
}
await this.kv.putMany(puts);
return pks;
}
@@ -4912,7 +4975,8 @@
const computedNew = crc32(recordBytes);
const computedLegacy = this.legacyChecksum(recordBytes);
if ((computedNew >>> 0) !== storedCrc && (computedLegacy >>> 0) !== storedCrc) {
// CRC 不匹配,跳过此损坏记录
// CRC 不匹配,跳过此损坏记录(长度字段链完整时后续好记录仍可恢复,
// 行为由 aria-wal-crc 测试锁定)
// eslint-disable-next-line no-console
console.warn(`[AriaEngine WAL] CRC mismatch at record LSN=${lsn}, skipping`);
continue;
@@ -6877,6 +6941,22 @@
validatedRows.push({ row: validated, pkValue, key: `${tableName}:${pkValue}` });
}
await this.lsm.prefetchKeys(validatedRows.map((v) => v.key));
// v0.7.3: 主键批内互查 + 预检 —— 此前 PK 重复检查在写入循环内:
// 第 N 行重复抛错时,前 N-1 行已 put LSM 且其 WAL 记录随 appendBatch 一起
// 丢失 → 语句级部分提交 + 内存/WAL 不一致(与 v0.6.2 的 unique 预检同一阶段)。
const pkSet = new Set();
for (const { pkValue, key } of validatedRows) {
if (pkSet.has(pkValue)) {
throw new DatabaseError(`Duplicate primary key "${pkValue}" in table "${tableName}"`, 'DUPLICATE_KEY');
}
pkSet.add(pkValue);
const existing = this.currentTxnId
? (this.txnSnapshot?.get(key) ?? this.lsm.get(key))
: this.lsm.get(key);
if (existing && !existing.__txn_deleted) {
throw new DatabaseError(`Duplicate primary key "${pkValue}" in table "${tableName}"`, 'DUPLICATE_KEY');
}
}
// v0.6.2: 唯一约束 — 批量预加载本批唯一列涉及的索引范围(一次 drainChain
for (const colName of uniqueCols) {
const idxLsm = this.secondaryIndexes.get(`${tableName}:idx:${colName}`);
@@ -6911,13 +6991,7 @@
}
}
for (const { row: validated, pkValue, key } of validatedRows) {
// Check duplicate in LSM + transaction snapshot
const existing = this.currentTxnId
? (this.txnSnapshot?.get(key) ?? this.lsm.get(key))
: this.lsm.get(key);
if (existing && !existing.__txn_deleted) {
throw new DatabaseError(`Duplicate primary key "${pkValue}" in table "${tableName}"`, 'DUPLICATE_KEY');
}
// PK 重复已在批预检阶段检查(v0.7.3),此处不再重复查询
if (this.currentTxnId && this.txnSnapshot) {
// Within transaction: buffer to snapshot + MVCC version chain
this.txnSnapshot.set(key, validated);
@@ -7155,25 +7229,9 @@
if (visited.has(visitKey))
return;
visited.add(visitKey);
// 阶段 1: RESTRICT 检查
for (const [refTableName, refSchema] of this.schemas) {
if (refTableName === tableName)
continue;
for (const [colName, colDef] of Object.entries(refSchema.columns)) {
if (!colDef.references || !colDef.onUpdate)
continue;
const [refTable] = colDef.references.split('.');
if (refTable !== tableName)
continue;
if (colDef.onUpdate !== 'RESTRICT')
continue;
const refRows = await this.getAllRows(refTableName);
if (refRows.some((r) => String(r[colName]) === oldPk)) {
throw new DatabaseError(`Cannot update "${tableName}" key "${oldPk}": foreign key "${colName}" in "${refTableName}" has dependent rows`, 'FOREIGN_KEY_VIOLATION');
}
}
}
// 阶段 2: CASCADE / SET NULL
// v0.7.3-perf: 删除冗余的阶段 1 RESTRICT 扫描 —— checkForeignKeyUpdateRestrict
// 已在两阶段 update 预检(阶段 1b)覆盖 RESTRICT 与 SET NULL+required
// 此处任何修改前重复全表扫描纯属浪费。直接执行 CASCADE / SET NULL。
for (const [refTableName, refSchema] of this.schemas) {
if (refTableName === tableName)
continue;
@@ -7556,9 +7614,6 @@
// 但索引 LSM 未恢复 → 此前静默 return 导致索引永久缺失)
if (this.secondaryIndexes.has(idxKey))
return;
colDef.index = true;
if (unique)
colDef.unique = true;
const idxLsm = new LSM({
memtableSizeThreshold: this.config.memtableSizeThreshold,
levelSizeMultiplier: this.config.levelSizeMultiplier,
@@ -7569,16 +7624,38 @@
});
await idxLsm.init();
this.secondaryIndexes.set(idxKey, idxLsm);
// 从主 LSM 重建索引数据
const pkCol = this.tablePKs.get(tableName);
const rows = await this.getAllRows(tableName);
for (const row of rows) {
const value = row[column];
if (value !== undefined && value !== null) {
idxLsm.put(`${String(value)}:${row[pkCol]}`, { pk: row[pkCol] });
try {
// 从主 LSM 重建索引数据
const pkCol = this.tablePKs.get(tableName);
const rows = await this.getAllRows(tableName);
const seen = new Set();
for (const row of rows) {
const value = row[column];
if (value !== undefined && value !== null) {
const v = String(value);
// v0.7.3: UNIQUE 索引回填校验存量唯一性 —— 此前重复数据静默建索引
// SQLite 语义应报错),与 MemoryEngine 对齐
if (unique && seen.has(v)) {
throw new DatabaseError(`Unique index on column "${column}" in table "${tableName}" cannot be created: duplicate value "${v}"`, 'UNIQUE_VIOLATION');
}
seen.add(v);
idxLsm.put(`${v}:${row[pkCol]}`, { pk: row[pkCol] });
}
}
await idxLsm.flush();
}
await idxLsm.flush();
catch (error) {
// 回填失败(唯一冲突):清理半初始化索引(内存 + 存储),标志未落,保持原子语义
this.secondaryIndexes.delete(idxKey);
try {
await idxLsm.clear();
}
catch { /* 清理失败不阻塞 */ }
throw error;
}
colDef.index = true;
if (unique)
colDef.unique = true;
await this.persistSchemas();
}
async dropIndex(tableName, column, _indexName) {
@@ -7615,12 +7692,23 @@
throw new DatabaseError('Transaction already in progress', 'TX_ACTIVE');
this.currentTxnId = this.mvcc.beginTransaction();
this.txnSnapshot = new Map();
await this.wal.append({
type: WALRecordType.BEGIN,
txnId: this.currentTxnId,
tableName: '',
key: '',
});
// v0.7.3-fix: WAL BEGIN 写失败回滚内存事务状态 —— 此前 append 抛错(full 模式)
// 时 currentTxnId 已设置 → TX_ACTIVE 永久泄漏(后续无法开始新事务)。
// 回滚 mvcc 登记 + 快照后重抛,调用方可重试。
try {
await this.wal.append({
type: WALRecordType.BEGIN,
txnId: this.currentTxnId,
tableName: '',
key: '',
});
}
catch (error) {
this.mvcc.rollbackTransaction(this.currentTxnId);
this.currentTxnId = null;
this.txnSnapshot = null;
throw error;
}
}
async commitTransaction() {
if (!this.currentTxnId)
@@ -7652,6 +7740,18 @@
async rollbackTransaction() {
if (!this.currentTxnId)
throw new DatabaseError('No active transaction', 'TX_NONE');
const txnId = this.currentTxnId;
// v0.7.3-fix: 先持久化 WAL ROLLBACK,再回滚内存 —— 与 commitTransaction 的
// "WAL 领先内存"v0.4.3-fix)对齐。此前内存先回滚、ROLLBACK 记录后写:
// full 模式写失败时崩溃重放无 ROLLBACK 记录 → 已回滚事务的数据复活。
// 现在写失败 → 内存未回滚、事务仍活跃(调用方可重试),崩溃后重放
// 看到 ROLLBACK 记录同样不会复活数据。
await this.wal.append({
type: WALRecordType.ROLLBACK,
txnId,
tableName: '',
key: '',
});
// v0.3.3: 记录事务涉及的表(用于回滚后重建索引,消除索引残留)
const affectedTables = new Set();
if (this.txnSnapshot) {
@@ -7661,14 +7761,8 @@
affectedTables.add(key.slice(0, idx));
}
}
this.mvcc.rollbackTransaction(this.currentTxnId);
this.mvcc.rollbackTransaction(txnId);
this.txnSnapshot = null;
await this.wal.append({
type: WALRecordType.ROLLBACK,
txnId: this.currentTxnId,
tableName: '',
key: '',
});
this.currentTxnId = null;
// v0.3.3: 事务内直接写入了二级索引 LSM,回滚后全量重建受影响表的索引
for (const tableName of affectedTables) {
@@ -8086,10 +8180,25 @@
if (!schema)
return null;
const pkCol = this.tablePKs.get(tableName);
for (const [col, condition] of Object.entries(query.where)) {
// 跳过 $and/$or/$not 逻辑组合
if (col === '$and' || col === '$or' || col === '$not')
continue;
// v0.7.3: 递归展开 $and 中的等值条件 —— 此前仅顶层键,
// `WHERE a AND b`(解析为顶层 $and)永远全表扫描,索引形同虚设。
// $or/$not 语义不适用单索引下推,保守跳过。命中索引后 find 仍以
// 全条件 matchWhere 过滤(子集语义安全)。
const flat = [];
const collect = (w) => {
for (const [k, v] of Object.entries(w)) {
if (k === '$and') {
for (const sub of v)
collect(sub);
continue;
}
if (k === '$or' || k === '$not' || k === '$exists')
continue;
flat.push([k, v]);
}
};
collect(query.where);
for (const [col, condition] of flat) {
const colDef = schema.columns[col];
const hasIndex = colDef && (colDef.index || colDef.unique || colDef.primaryKey);
if (!hasIndex && col !== pkCol)
@@ -8167,18 +8276,31 @@
// v0.6.2-fix(P1): IN 列表含 null 不走索引(索引不含 null 条目,会漏匹配 null 行)
if (c.$in.some((v) => v === null))
continue;
// v0.7.3-perf: 批级预加载全部值的索引范围 + 主表行(各一次 drainChain)——
// 此前逐值 indexScanToRows:每个值一次 prefetchRange + prefetchKeys
// 后台 compaction 长耗时时 N 倍放大(与 v0.6.1 修的 insert 批量预加载
// 性能悬崖同类)。批级预加载后循环内同步 rangeScan/get。
const values = c.$in.map((v) => String(v));
await idxLsm.prefetchPrefixRanges(values.map((v) => [v, `${v}\uffff`]));
const results = [];
const seenPks = new Set(); // v0.4.1: IN 值可能重复,按 pk 去重
for (const val of c.$in) {
const rows = await this.indexScanToRows(tableName, pkCol, idxLsm, String(val), String(val));
for (const row of rows) {
const pk = String(row[pkCol]);
if (!seenPks.has(pk)) {
const pks = [];
for (const val of values) {
const entries = idxLsm.rangeScan(val, `${val}\uffff`);
for (const [, idxEntry] of entries) {
const pk = idxEntry.pk;
if (pk && !seenPks.has(pk)) {
seenPks.add(pk);
results.push(row);
pks.push(pk);
}
}
}
await this.lsm.prefetchKeys(pks.map((pk) => `${tableName}:${pk}`));
for (const pk of pks) {
const row = this.lsm.get(`${tableName}:${pk}`);
if (row)
results.push({ ...row, [pkCol]: pk });
}
return results;
}
// $gt / $gte / $lt / $lte → 范围扫描
@@ -8242,10 +8364,6 @@
this.mvcc.gc(50);
}
}
/** 估算 WAL 大小(字节) */
getWALEstimatedSize() {
return this.wal.getBufferedCount() * 200; // 粗略估算每条 ~200B
}
/**
* ANALYZE: 收集表统计信息
* 返回行数平均行大小索引深度等
@@ -8254,15 +8372,28 @@
this.ensureOpen();
this.ensureTable(tableName);
const rows = await this.getAllRows(tableName);
// v0.7.3: 统计汇总主 LSM + 该表全部二级索引 LSM —— 此前只统计主 LSM,
// 表带多个索引时索引深度/SSTable 数量严重低估
let sstableCount = this.lsm.getStats().sstableCount;
let memtableSize = this.lsm.getStats().memtableSize;
let indexDepth = this.lsm.getStats().levelCounts.filter((c) => c > 0).length;
for (const [idxKey, idxLsm] of this.secondaryIndexes) {
if (!idxKey.startsWith(`${tableName}:idx:`))
continue;
const s = idxLsm.getStats();
sstableCount += s.sstableCount;
memtableSize += s.memtableSize;
indexDepth = Math.max(indexDepth, s.levelCounts.filter((c) => c > 0).length);
}
const stats = {
table: tableName,
rowCount: rows.length,
avgRowSize: rows.length > 0
? Math.round(rows.reduce((s, r) => s + JSON.stringify(r).length, 0) / rows.length)
: 0,
indexDepth: this.lsm.getStats().levelCounts.filter((c) => c > 0).length,
sstableCount: this.lsm.getStats().sstableCount,
memtableSize: this.lsm.getStats().memtableSize,
indexDepth,
sstableCount,
memtableSize,
estimatedMemory: this.lsm.getEstimatedMemory(),
};
// 列基数统计
@@ -9604,6 +9735,12 @@
if (this.curTokenIs(TokenType.STAR)) {
columns.push('*');
this.nextToken();
// v0.7.3: `SELECT *, col [AS alias], ...` —— '*' 后可继续列列表
// (此前 '*' 独占分支,逗号后直接 PARSE_ERRORexecutor 侧投影已支持混合)
while (this.curTokenIs(TokenType.COMMA)) {
this.nextToken();
columns.push(this.parseColumnWithAlias());
}
}
else {
columns.push(...this.parseColumnList());
@@ -10729,26 +10866,36 @@
catch { /* 非查询语句无 QueryPlan */ }
// v0.7.0: 真实索引命中信息(此前 usingIndex 恒为 'auto' 占位)。
// 引擎无关启发式:WHERE 中存在主键/索引/唯一列条件 → 对应引擎索引路径。
// v0.7.3: 递归识别 $and 嵌套等值条件(与 Memory/Aria 的 $and 下推行为对齐;
// $or/$not 不下推,保持 none)。
let usingIndex = plan?.table ? 'none' : 'none';
if (plan && plan.table && plan.where && Object.keys(plan.where).length > 0) {
try {
const schema = await this.engine.getTableSchema(plan.table);
if (schema) {
for (const col of Object.keys(plan.where)) {
if (col.startsWith('$'))
continue;
const colDef = schema.columns[col];
if (!colDef)
continue;
if (colDef.primaryKey) {
usingIndex = 'pk';
break;
const findIndex = (w) => {
for (const [k, v] of Object.entries(w)) {
if (k === '$and') {
for (const sub of v) {
const hit = findIndex(sub);
if (hit)
return hit;
}
continue;
}
if (k === '$or' || k === '$not')
continue;
const colDef = schema.columns[k];
if (!colDef)
continue;
if (colDef.primaryKey)
return 'pk';
if (colDef.index || colDef.unique)
return `index:${k}`;
}
if (colDef.index || colDef.unique) {
usingIndex = `index:${col}`;
break;
}
}
return null;
};
usingIndex = findIndex(plan.where) ?? 'none';
}
}
catch { /* schema 读取失败保持 none */ }
@@ -10879,7 +11026,11 @@
}
if (stmt.orderBy && stmt.orderBy.length > 0)
rows = applyOrderBy(rows, stmt.orderBy);
if (!hasGroupBy && !hasAggregate && stmt.columns.length > 0 && stmt.columns[0] !== '*') {
// v0.7.3: `SELECT *, col AS alias` —— 此前 columns[0]==='*' 直接不投影,
// 别名列/常量列丢失。仅当 '*' 是唯一列时跳过投影(projectRow 对裸 '*'
// 合并原行全部列,其余表达式覆盖/追加)
if (!hasGroupBy && !hasAggregate && stmt.columns.length > 0
&& !(stmt.columns.length === 1 && stmt.columns[0] === '*')) {
rows = rows.map((row) => this.projectRow(row, stmt.columns));
}
// v0.3.3: ORDER BY 别名 → 投影后才存在,需在投影后重新排序
@@ -11449,9 +11600,13 @@
const aliasCols = [];
const caseCols = [];
const constCols = [];
// v0.7.3: 裸 '*' 与列表达式混合(SELECT *, name AS nick)→ 原行全部列为基
let hasStar = false;
for (const col of columns) {
if (col === '*')
if (col === '*') {
hasStar = true;
continue;
}
const expr = parseCaseExpression(col);
if (expr) {
caseCols.push({ alias: expr.alias ?? col, expr });
@@ -11465,20 +11620,26 @@
// v0.4.0: 字符串常量列 SELECT 'lit' → 常量输出
const lit = col.match(/^'(.*)'$/s);
if (lit) {
const value = lit[1].replace(/\\'/g, "'");
// v0.7.3: SQL 标准 '' 转义还原(readString 已把 '' 合并为单个 '
// 打包回列的文本中相邻两个 ' 即一个引号字面量)
const value = lit[1].replace(/''/g, "'");
constCols.push({ key: col, value });
continue;
}
plain.push(col);
}
const projected = plain.length > 0 ? projectColumns(row, plain) : {};
// v0.7.3: hasStar 时以原行全部列为基(projectColumns 仅投影 plain 列,不含 * 的其余列)
const projected = hasStar
? { ...row }
: (plain.length > 0 ? projectColumns(row, plain) : {});
for (const { alias, source } of aliasCols) {
if (source === '*') {
Object.assign(projected, row);
}
else {
const lit = source.match(/^'(.*)'$/s);
projected[alias] = lit ? lit[1].replace(/\\'/g, "'") : row[source];
// v0.7.3: 同 constCols —— SQL 标准 '' 转义还原
projected[alias] = lit ? lit[1].replace(/''/g, "'") : row[source];
}
}
for (const { key, value } of constCols) {
@@ -12350,9 +12511,41 @@
const select = stmt;
// 不可流式场景:JOIN / GROUP BY / HAVING / DISTINCT / 聚合 / UNION / 关联子查询 / ORDER BY
const aggregate = select.columns.some((c) => /^(COUNT|SUM|AVG|MIN|MAX)\(/i.test(c));
// v0.7.3: WHERE 含子查询($subquery / $exists / 嵌套 $col 列引用)不可流式 ——
// 引擎层 matchWhere 的 $in/$nin 遇未解析的 $subquery 对象返回 false → 所有行
// 被静默过滤(空结果);$col 操作符无对应匹配分支会抛 QUERY_ERROR。
// 递归检测后回退物化路径(resolveSubqueries 正确解析)。
const hasSubquery = (where) => {
if (!where)
return false;
for (const [k, v] of Object.entries(where)) {
if (k === '$and' || k === '$or') {
if (v.some((sub) => hasSubquery(sub)))
return true;
continue;
}
if (k === '$not') {
if (hasSubquery(v))
return true;
continue;
}
if (k === '$exists')
return true;
if (typeof v === 'object' && v !== null) {
for (const [, operand] of Object.entries(v)) {
if (typeof operand === 'object' && operand !== null) {
const ops = operand;
if ('$subquery' in ops || '$col' in ops)
return true;
}
}
}
}
return false;
};
const streamable = !select.joins && !select.groupBy && !select.having && !select.distinct
&& !aggregate && !(select.orderBy && select.orderBy.length > 0)
&& !(select.where && select.where['$exists'] !== undefined);
&& !hasSubquery(select.where);
if (streamable && typeof this.engine.findStream === 'function') {
// 用户回调为 async(返回 Promise)时引擎同步扫描无法 await → 回退物化
const isAsync = onRow.constructor?.name === 'AsyncFunction';
@@ -12509,9 +12702,20 @@
async triggerStatementHooks(stmt, phase, result) {
switch (stmt.type) {
case 'INSERT': {
// v0.7.3: 列映射对齐 executor —— 省略列名时按 schema 列顺序映射
// (此前用数字键 String(i),与 executor 写入的真实行键不一致)
let cols = stmt.columns ?? [];
if (cols.length === 0) {
try {
const schema = await this.engine.getTableSchema(stmt.into);
cols = schema ? Object.keys(schema.columns) : [];
}
catch {
cols = [];
}
}
const rows = (stmt.values ?? []).map((vals) => {
const row = {};
const cols = stmt.columns ?? [];
for (let i = 0; i < vals.length; i++) {
row[cols[i] ?? String(i)] = vals[i];
}