@quereus/quereus 0.4.11 → 0.5.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 +40 -16
- package/dist/src/core/database.d.ts +18 -0
- package/dist/src/core/database.d.ts.map +1 -1
- package/dist/src/core/database.js +22 -0
- package/dist/src/core/database.js.map +1 -1
- package/dist/src/func/builtins/conversion.d.ts +51 -0
- package/dist/src/func/builtins/conversion.d.ts.map +1 -0
- package/dist/src/func/builtins/conversion.js +152 -0
- package/dist/src/func/builtins/conversion.js.map +1 -0
- package/dist/src/func/builtins/index.d.ts.map +1 -1
- package/dist/src/func/builtins/index.js +20 -1
- package/dist/src/func/builtins/index.js.map +1 -1
- package/dist/src/func/builtins/json.d.ts +1 -0
- package/dist/src/func/builtins/json.d.ts.map +1 -1
- package/dist/src/func/builtins/json.js +100 -0
- package/dist/src/func/builtins/json.js.map +1 -1
- package/dist/src/func/builtins/schema.js +1 -1
- package/dist/src/func/builtins/schema.js.map +1 -1
- package/dist/src/index.d.ts +8 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +6 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/runtime/emit/binary.d.ts.map +1 -1
- package/dist/src/runtime/emit/binary.js +6 -2
- package/dist/src/runtime/emit/binary.js.map +1 -1
- package/dist/src/runtime/emit/constraint-check.d.ts.map +1 -1
- package/dist/src/runtime/emit/constraint-check.js +8 -1
- package/dist/src/runtime/emit/constraint-check.js.map +1 -1
- package/dist/src/runtime/emit/insert.d.ts.map +1 -1
- package/dist/src/runtime/emit/insert.js +4 -24
- package/dist/src/runtime/emit/insert.js.map +1 -1
- package/dist/src/runtime/emit/scalar-function.d.ts +9 -0
- package/dist/src/runtime/emit/scalar-function.d.ts.map +1 -1
- package/dist/src/runtime/emit/scalar-function.js +23 -1
- package/dist/src/runtime/emit/scalar-function.js.map +1 -1
- package/dist/src/runtime/emit/unary.d.ts.map +1 -1
- package/dist/src/runtime/emit/unary.js +3 -1
- package/dist/src/runtime/emit/unary.js.map +1 -1
- package/dist/src/schema/column.d.ts +4 -1
- package/dist/src/schema/column.d.ts.map +1 -1
- package/dist/src/schema/column.js +3 -0
- package/dist/src/schema/column.js.map +1 -1
- package/dist/src/schema/function.d.ts +15 -0
- package/dist/src/schema/function.d.ts.map +1 -1
- package/dist/src/schema/function.js.map +1 -1
- package/dist/src/schema/table.d.ts.map +1 -1
- package/dist/src/schema/table.js +10 -1
- package/dist/src/schema/table.js.map +1 -1
- package/dist/src/types/builtin-types.d.ts +37 -0
- package/dist/src/types/builtin-types.d.ts.map +1 -0
- package/dist/src/types/builtin-types.js +359 -0
- package/dist/src/types/builtin-types.js.map +1 -0
- package/dist/src/types/index.d.ts +7 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +13 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/json-type.d.ts +8 -0
- package/dist/src/types/json-type.d.ts.map +1 -0
- package/dist/src/types/json-type.js +143 -0
- package/dist/src/types/json-type.js.map +1 -0
- package/dist/src/types/logical-type.d.ts +52 -0
- package/dist/src/types/logical-type.d.ts.map +1 -0
- package/dist/src/types/logical-type.js +34 -0
- package/dist/src/types/logical-type.js.map +1 -0
- package/dist/src/types/plugin-interface.d.ts +9 -0
- package/dist/src/types/plugin-interface.d.ts.map +1 -0
- package/dist/src/types/plugin-interface.js +2 -0
- package/dist/src/types/plugin-interface.js.map +1 -0
- package/dist/src/types/registry.d.ts +72 -0
- package/dist/src/types/registry.d.ts.map +1 -0
- package/dist/src/types/registry.js +168 -0
- package/dist/src/types/registry.js.map +1 -0
- package/dist/src/types/temporal-types.d.ts +17 -0
- package/dist/src/types/temporal-types.d.ts.map +1 -0
- package/dist/src/types/temporal-types.js +178 -0
- package/dist/src/types/temporal-types.js.map +1 -0
- package/dist/src/types/validation.d.ts +52 -0
- package/dist/src/types/validation.d.ts.map +1 -0
- package/dist/src/types/validation.js +96 -0
- package/dist/src/types/validation.js.map +1 -0
- package/dist/src/util/comparison.d.ts +24 -5
- package/dist/src/util/comparison.d.ts.map +1 -1
- package/dist/src/util/comparison.js +71 -9
- package/dist/src/util/comparison.js.map +1 -1
- package/dist/src/util/plugin-loader.d.ts.map +1 -1
- package/dist/src/util/plugin-loader.js +17 -0
- package/dist/src/util/plugin-loader.js.map +1 -1
- package/dist/src/vtab/manifest.d.ts +4 -0
- package/dist/src/vtab/manifest.d.ts.map +1 -1
- package/dist/src/vtab/memory/index.d.ts +1 -0
- package/dist/src/vtab/memory/index.d.ts.map +1 -1
- package/dist/src/vtab/memory/index.js +15 -4
- package/dist/src/vtab/memory/index.js.map +1 -1
- package/dist/src/vtab/memory/layer/manager.d.ts.map +1 -1
- package/dist/src/vtab/memory/layer/manager.js +20 -2
- package/dist/src/vtab/memory/layer/manager.js.map +1 -1
- package/dist/src/vtab/memory/utils/primary-key.d.ts.map +1 -1
- package/dist/src/vtab/memory/utils/primary-key.js +17 -12
- package/dist/src/vtab/memory/utils/primary-key.js.map +1 -1
- package/package.json +5 -4
- package/src/core/database.ts +24 -0
- package/src/func/builtins/conversion.ts +201 -0
- package/src/func/builtins/index.ts +20 -1
- package/src/func/builtins/json.ts +121 -0
- package/src/func/builtins/schema.ts +1 -1
- package/src/index.ts +35 -0
- package/src/runtime/emit/binary.ts +8 -2
- package/src/runtime/emit/constraint-check.ts +9 -1
- package/src/runtime/emit/insert.ts +4 -16
- package/src/runtime/emit/scalar-function.ts +27 -1
- package/src/runtime/emit/unary.ts +4 -1
- package/src/schema/column.ts +8 -1
- package/src/schema/function.ts +18 -0
- package/src/schema/table.ts +411 -398
- package/src/types/builtin-types.ts +350 -0
- package/src/types/index.ts +17 -0
- package/src/types/json-type.ts +152 -0
- package/src/types/logical-type.ts +75 -0
- package/src/types/plugin-interface.ts +10 -0
- package/src/types/registry.ts +200 -0
- package/src/types/temporal-types.ts +167 -0
- package/src/types/validation.ts +120 -0
- package/src/util/comparison.ts +87 -14
- package/src/util/plugin-loader.ts +19 -0
- package/src/vtab/manifest.ts +7 -1
- package/src/vtab/memory/index.ts +191 -178
- package/src/vtab/memory/layer/manager.ts +28 -2
- package/src/vtab/memory/utils/primary-key.ts +19 -14
package/src/vtab/memory/index.ts
CHANGED
|
@@ -1,178 +1,191 @@
|
|
|
1
|
-
import { BTree } from 'inheritree';
|
|
2
|
-
import { StatusCode, type Row, type SqlValue } from '../../common/types.js';
|
|
3
|
-
import {
|
|
4
|
-
import type { BTreeKeyForPrimary, BTreeKeyForIndex, MemoryIndexEntry } from './types.js';
|
|
5
|
-
import type { IndexColumnSchema as IndexColumnSpec } from '../../schema/table.js'; // Renamed for clarity
|
|
6
|
-
import type { ColumnSchema } from '../../schema/column.js';
|
|
7
|
-
import { quereusError } from '../../common/errors.js';
|
|
8
|
-
|
|
9
|
-
/** Definition for creating a memory index (matches IndexSchema columns usually) */
|
|
10
|
-
export interface IndexSpec {
|
|
11
|
-
name?: string;
|
|
12
|
-
columns: ReadonlyArray<IndexColumnSpec>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** Functions for extracting and comparing index keys */
|
|
16
|
-
interface IndexKeyFunctions {
|
|
17
|
-
keyFromRow: (row: Row) => BTreeKeyForIndex;
|
|
18
|
-
compareKeys: (a: BTreeKeyForIndex, b: BTreeKeyForIndex) => number;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** Represents a secondary index within a MemoryTable */
|
|
22
|
-
export class MemoryIndex {
|
|
23
|
-
public readonly name: string | undefined;
|
|
24
|
-
public readonly specColumns: ReadonlyArray<IndexColumnSpec>;
|
|
25
|
-
public readonly keyFromRow: (row: Row) => BTreeKeyForIndex;
|
|
26
|
-
public readonly compareKeys: (a: BTreeKeyForIndex, b: BTreeKeyForIndex) => number;
|
|
27
|
-
public data: BTree<BTreeKeyForIndex, MemoryIndexEntry>;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
this.
|
|
32
|
-
|
|
33
|
-
this.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
this.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return this.
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
|
|
1
|
+
import { BTree } from 'inheritree';
|
|
2
|
+
import { StatusCode, type Row, type SqlValue } from '../../common/types.js';
|
|
3
|
+
import { createTypedComparator, resolveCollation } from '../../util/comparison.js';
|
|
4
|
+
import type { BTreeKeyForPrimary, BTreeKeyForIndex, MemoryIndexEntry } from './types.js';
|
|
5
|
+
import type { IndexColumnSchema as IndexColumnSpec } from '../../schema/table.js'; // Renamed for clarity
|
|
6
|
+
import type { ColumnSchema } from '../../schema/column.js';
|
|
7
|
+
import { quereusError } from '../../common/errors.js';
|
|
8
|
+
|
|
9
|
+
/** Definition for creating a memory index (matches IndexSchema columns usually) */
|
|
10
|
+
export interface IndexSpec {
|
|
11
|
+
name?: string;
|
|
12
|
+
columns: ReadonlyArray<IndexColumnSpec>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Functions for extracting and comparing index keys */
|
|
16
|
+
interface IndexKeyFunctions {
|
|
17
|
+
keyFromRow: (row: Row) => BTreeKeyForIndex;
|
|
18
|
+
compareKeys: (a: BTreeKeyForIndex, b: BTreeKeyForIndex) => number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Represents a secondary index within a MemoryTable */
|
|
22
|
+
export class MemoryIndex {
|
|
23
|
+
public readonly name: string | undefined;
|
|
24
|
+
public readonly specColumns: ReadonlyArray<IndexColumnSpec>;
|
|
25
|
+
public readonly keyFromRow: (row: Row) => BTreeKeyForIndex;
|
|
26
|
+
public readonly compareKeys: (a: BTreeKeyForIndex, b: BTreeKeyForIndex) => number;
|
|
27
|
+
public data: BTree<BTreeKeyForIndex, MemoryIndexEntry>;
|
|
28
|
+
private readonly allTableColumnsSchema: ReadonlyArray<ColumnSchema>;
|
|
29
|
+
|
|
30
|
+
constructor(spec: IndexSpec, allTableColumnsSchema: ReadonlyArray<ColumnSchema>, baseInheritreeTable?: BTree<BTreeKeyForIndex, MemoryIndexEntry>) {
|
|
31
|
+
this.name = spec.name;
|
|
32
|
+
this.specColumns = Object.freeze(spec.columns.map(c => ({ ...c })));
|
|
33
|
+
this.allTableColumnsSchema = allTableColumnsSchema;
|
|
34
|
+
|
|
35
|
+
this.validateColumnIndexes(allTableColumnsSchema);
|
|
36
|
+
|
|
37
|
+
const keyFunctions = this.createIndexKeyFunctions();
|
|
38
|
+
this.keyFromRow = keyFunctions.keyFromRow;
|
|
39
|
+
this.compareKeys = keyFunctions.compareKeys;
|
|
40
|
+
|
|
41
|
+
this.data = this.createBTree(baseInheritreeTable);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private validateColumnIndexes(allTableColumnsSchema: ReadonlyArray<ColumnSchema>): void {
|
|
45
|
+
const hasInvalidIndex = this.specColumns.some(sc =>
|
|
46
|
+
sc.index < 0 || sc.index >= allTableColumnsSchema.length
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
if (hasInvalidIndex) {
|
|
50
|
+
quereusError(`Invalid column index in index '${this.name ?? '(unnamed)'}'.`, StatusCode.INTERNAL);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private createIndexKeyFunctions(): IndexKeyFunctions {
|
|
55
|
+
if (this.specColumns.length === 1) {
|
|
56
|
+
return this.createSingleColumnKeyFunctions();
|
|
57
|
+
} else {
|
|
58
|
+
return this.createCompositeColumnKeyFunctions();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private createSingleColumnKeyFunctions(): IndexKeyFunctions {
|
|
63
|
+
const specCol = this.specColumns[0];
|
|
64
|
+
const colSchemaIndex = specCol.index;
|
|
65
|
+
const columnSchema = this.allTableColumnsSchema[colSchemaIndex];
|
|
66
|
+
const descMultiplier = specCol.desc ? -1 : 1;
|
|
67
|
+
|
|
68
|
+
// Create type-aware comparator
|
|
69
|
+
const collationFunc = specCol.collation ? resolveCollation(specCol.collation) : undefined;
|
|
70
|
+
const typedComparator = createTypedComparator(columnSchema.logicalType, collationFunc);
|
|
71
|
+
|
|
72
|
+
const keyFromRow = (row: Row): BTreeKeyForIndex => {
|
|
73
|
+
this.validateRowLength(row, colSchemaIndex);
|
|
74
|
+
return row[colSchemaIndex];
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const compareKeys = (a: BTreeKeyForIndex, b: BTreeKeyForIndex): number => {
|
|
78
|
+
return typedComparator(a as SqlValue, b as SqlValue) * descMultiplier;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return { keyFromRow, compareKeys };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private createCompositeColumnKeyFunctions(): IndexKeyFunctions {
|
|
85
|
+
const localSpecColumns = this.specColumns;
|
|
86
|
+
|
|
87
|
+
// Pre-create type-aware comparators for each column
|
|
88
|
+
const comparators = localSpecColumns.map(sc => {
|
|
89
|
+
const columnSchema = this.allTableColumnsSchema[sc.index];
|
|
90
|
+
const collationFunc = sc.collation ? resolveCollation(sc.collation) : undefined;
|
|
91
|
+
return createTypedComparator(columnSchema.logicalType, collationFunc);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const keyFromRow = (row: Row): BTreeKeyForIndex => {
|
|
95
|
+
return localSpecColumns.map(sc => {
|
|
96
|
+
this.validateRowLength(row, sc.index);
|
|
97
|
+
return row[sc.index];
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const compareKeys = (a: BTreeKeyForIndex, b: BTreeKeyForIndex): number => {
|
|
102
|
+
const arrA = a as SqlValue[];
|
|
103
|
+
const arrB = b as SqlValue[];
|
|
104
|
+
|
|
105
|
+
for (let i = 0; i < localSpecColumns.length; i++) {
|
|
106
|
+
if (i >= arrA.length || i >= arrB.length) {
|
|
107
|
+
return arrA.length - arrB.length;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const specCol = localSpecColumns[i];
|
|
111
|
+
const comparison = comparators[i](arrA[i], arrB[i]);
|
|
112
|
+
|
|
113
|
+
if (comparison !== 0) {
|
|
114
|
+
return specCol.desc ? -comparison : comparison;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return 0;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
return { keyFromRow, compareKeys };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private validateRowLength(row: Row, columnIndex: number): void {
|
|
124
|
+
if (columnIndex < 0 || columnIndex >= row.length) {
|
|
125
|
+
quereusError(`Index key col index ${columnIndex} OOB for row len ${row.length}`, StatusCode.INTERNAL);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private createBTree(baseInheritreeTable?: BTree<BTreeKeyForIndex, MemoryIndexEntry>): BTree<BTreeKeyForIndex, MemoryIndexEntry> {
|
|
130
|
+
return new BTree<BTreeKeyForIndex, MemoryIndexEntry>(
|
|
131
|
+
(entry: MemoryIndexEntry) => entry.indexKey,
|
|
132
|
+
this.compareKeys,
|
|
133
|
+
baseInheritreeTable
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Adds a mapping from index key to primary key */
|
|
138
|
+
addEntry(indexKey: BTreeKeyForIndex, primaryKey: BTreeKeyForPrimary): void {
|
|
139
|
+
const path = this.data.find(indexKey);
|
|
140
|
+
if (path.on) {
|
|
141
|
+
// Entry exists, add to the existing set of primary keys
|
|
142
|
+
const existingEntry = this.data.at(path)!;
|
|
143
|
+
existingEntry.primaryKeys.add(primaryKey);
|
|
144
|
+
} else {
|
|
145
|
+
// Create new entry with a Set containing the primary key
|
|
146
|
+
const newEntry: MemoryIndexEntry = {
|
|
147
|
+
indexKey,
|
|
148
|
+
primaryKeys: new Set([primaryKey])
|
|
149
|
+
};
|
|
150
|
+
this.data.insert(newEntry);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Removes a mapping from index key to primary key */
|
|
155
|
+
removeEntry(indexKey: BTreeKeyForIndex, primaryKey: BTreeKeyForPrimary): void {
|
|
156
|
+
const path = this.data.find(indexKey);
|
|
157
|
+
if (path.on) {
|
|
158
|
+
const entry = this.data.at(path)!;
|
|
159
|
+
entry.primaryKeys.delete(primaryKey);
|
|
160
|
+
|
|
161
|
+
// If no primary keys remain, remove the entire entry
|
|
162
|
+
if (entry.primaryKeys.size === 0) {
|
|
163
|
+
this.data.deleteAt(path);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Returns the primary keys for a given index key */
|
|
169
|
+
getPrimaryKeys(indexKey: BTreeKeyForIndex): BTreeKeyForPrimary[] {
|
|
170
|
+
const entry = this.data.get(indexKey);
|
|
171
|
+
return entry ? Array.from(entry.primaryKeys) : [];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Gets the count of unique index values */
|
|
175
|
+
get size(): number {
|
|
176
|
+
return this.data.getCount();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Clears all entries from the index */
|
|
180
|
+
clear(): void {
|
|
181
|
+
const base = (this.data as any).baseTable as BTree<BTreeKeyForIndex, MemoryIndexEntry> | undefined;
|
|
182
|
+
this.data = this.createBTree(base);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Clears the base inheritance for this index (used during layer collapse) */
|
|
186
|
+
clearBase(): void {
|
|
187
|
+
if (typeof (this.data as any).clearBase === 'function') {
|
|
188
|
+
(this.data as any).clearBase();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -19,6 +19,7 @@ import { createPrimaryKeyFunctions, buildPrimaryKeyFromValues, type PrimaryKeyFu
|
|
|
19
19
|
import { createMemoryTableLoggers } from '../utils/logging.js';
|
|
20
20
|
import { getSyncLiteral } from '../../../parser/utils.js';
|
|
21
21
|
import { createLogger } from '../../../common/logger.js';
|
|
22
|
+
import { validateAndParse } from '../../../types/validation.js';
|
|
22
23
|
|
|
23
24
|
let tableManagerCounter = 0;
|
|
24
25
|
const logger = createMemoryTableLoggers('layer:manager');
|
|
@@ -420,7 +421,20 @@ export class MemoryTableManager {
|
|
|
420
421
|
throw new QuereusError("INSERT requires values.", StatusCode.MISUSE);
|
|
421
422
|
}
|
|
422
423
|
|
|
423
|
-
|
|
424
|
+
// Validate and parse values according to column types
|
|
425
|
+
const schema = targetLayer.getSchema();
|
|
426
|
+
const validatedRow: Row = values.map((value, index) => {
|
|
427
|
+
if (index >= schema.columns.length) {
|
|
428
|
+
throw new QuereusError(
|
|
429
|
+
`Too many values for INSERT into ${this._tableName}: expected ${schema.columns.length}, got ${values.length}`,
|
|
430
|
+
StatusCode.ERROR
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
const column = schema.columns[index];
|
|
434
|
+
return validateAndParse(value, column.logicalType, column.name);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
const newRowData: Row = validatedRow;
|
|
424
438
|
const primaryKey = this.primaryKeyFromRow(newRowData);
|
|
425
439
|
const skipPkCheck = this.shouldSkipPkCheck(values);
|
|
426
440
|
|
|
@@ -448,8 +462,20 @@ export class MemoryTableManager {
|
|
|
448
462
|
throw new QuereusError("UPDATE requires new values and old key values.", StatusCode.MISUSE);
|
|
449
463
|
}
|
|
450
464
|
|
|
451
|
-
|
|
465
|
+
// Validate and parse values according to column types
|
|
452
466
|
const schema = targetLayer.getSchema();
|
|
467
|
+
const validatedRow: Row = values.map((value, index) => {
|
|
468
|
+
if (index >= schema.columns.length) {
|
|
469
|
+
throw new QuereusError(
|
|
470
|
+
`Too many values for UPDATE on ${this._tableName}: expected ${schema.columns.length}, got ${values.length}`,
|
|
471
|
+
StatusCode.ERROR
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
const column = schema.columns[index];
|
|
475
|
+
return validateAndParse(value, column.logicalType, column.name);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
const newRowData: Row = validatedRow;
|
|
453
479
|
const targetPrimaryKey = buildPrimaryKeyFromValues(oldKeyValues, schema.primaryKeyDefinition);
|
|
454
480
|
const oldRowData = this.lookupEffectiveRow(targetPrimaryKey, targetLayer);
|
|
455
481
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { TableSchema, PrimaryKeyColumnDefinition } from '../../../schema/table.js';
|
|
2
2
|
import type { Row, SqlValue } from '../../../common/types.js';
|
|
3
3
|
import type { BTreeKeyForPrimary } from '../types.js';
|
|
4
|
-
import { compareSqlValuesFast, resolveCollation, type CollationFunction } from '../../../util/comparison.js';
|
|
4
|
+
import { compareSqlValuesFast, resolveCollation, type CollationFunction, createTypedComparator } from '../../../util/comparison.js';
|
|
5
5
|
import { QuereusError } from '../../../common/errors.js';
|
|
6
6
|
import { StatusCode } from '../../../common/types.js';
|
|
7
7
|
|
|
@@ -26,9 +26,9 @@ export function createPrimaryKeyFunctions(schema: TableSchema): PrimaryKeyFuncti
|
|
|
26
26
|
if (pkDefinition.length === 0) {
|
|
27
27
|
return createSingletonPrimaryKeyFunctions();
|
|
28
28
|
} else if (pkDefinition.length === 1) {
|
|
29
|
-
return createSingleColumnPrimaryKeyFunctions(pkDefinition[0]);
|
|
29
|
+
return createSingleColumnPrimaryKeyFunctions(pkDefinition[0], schema);
|
|
30
30
|
} else {
|
|
31
|
-
return createCompositeColumnPrimaryKeyFunctions(pkDefinition);
|
|
31
|
+
return createCompositeColumnPrimaryKeyFunctions(pkDefinition, schema);
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -50,14 +50,16 @@ function createSingletonPrimaryKeyFunctions(): PrimaryKeyFunctions {
|
|
|
50
50
|
* Creates functions for single-column primary keys (optimized path)
|
|
51
51
|
*/
|
|
52
52
|
function createSingleColumnPrimaryKeyFunctions(
|
|
53
|
-
columnDef: PrimaryKeyColumnDefinition
|
|
53
|
+
columnDef: PrimaryKeyColumnDefinition,
|
|
54
|
+
schema: TableSchema
|
|
54
55
|
): PrimaryKeyFunctions {
|
|
55
56
|
const pkColIndex = columnDef.index;
|
|
56
|
-
const collation = columnDef.collation || 'BINARY';
|
|
57
57
|
const descMultiplier = columnDef.desc ? -1 : 1;
|
|
58
58
|
|
|
59
|
-
//
|
|
60
|
-
const
|
|
59
|
+
// Get the column's logical type and create type-aware comparator
|
|
60
|
+
const columnSchema = schema.columns[pkColIndex];
|
|
61
|
+
const collationFunc = columnDef.collation ? resolveCollation(columnDef.collation) : undefined;
|
|
62
|
+
const typedComparator = createTypedComparator(columnSchema.logicalType, collationFunc);
|
|
61
63
|
|
|
62
64
|
const extractFromRow = (row: Row): BTreeKeyForPrimary => {
|
|
63
65
|
if (!row || !Array.isArray(row)) {
|
|
@@ -76,7 +78,7 @@ function createSingleColumnPrimaryKeyFunctions(
|
|
|
76
78
|
};
|
|
77
79
|
|
|
78
80
|
const compare = (a: BTreeKeyForPrimary, b: BTreeKeyForPrimary): number => {
|
|
79
|
-
return
|
|
81
|
+
return typedComparator(a as SqlValue, b as SqlValue) * descMultiplier;
|
|
80
82
|
};
|
|
81
83
|
|
|
82
84
|
return { extractFromRow, compare };
|
|
@@ -86,12 +88,15 @@ function createSingleColumnPrimaryKeyFunctions(
|
|
|
86
88
|
* Creates functions for composite (multi-column) primary keys
|
|
87
89
|
*/
|
|
88
90
|
function createCompositeColumnPrimaryKeyFunctions(
|
|
89
|
-
pkDefinition: ReadonlyArray<PrimaryKeyColumnDefinition
|
|
91
|
+
pkDefinition: ReadonlyArray<PrimaryKeyColumnDefinition>,
|
|
92
|
+
schema: TableSchema
|
|
90
93
|
): PrimaryKeyFunctions {
|
|
91
|
-
// Pre-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
94
|
+
// Pre-create type-aware comparators for each primary key column
|
|
95
|
+
const comparators = pkDefinition.map(def => {
|
|
96
|
+
const columnSchema = schema.columns[def.index];
|
|
97
|
+
const collationFunc = def.collation ? resolveCollation(def.collation) : undefined;
|
|
98
|
+
return createTypedComparator(columnSchema.logicalType, collationFunc);
|
|
99
|
+
});
|
|
95
100
|
|
|
96
101
|
const extractFromRow = (row: Row): BTreeKeyForPrimary => {
|
|
97
102
|
if (!row || !Array.isArray(row)) {
|
|
@@ -121,7 +126,7 @@ function createCompositeColumnPrimaryKeyFunctions(
|
|
|
121
126
|
}
|
|
122
127
|
|
|
123
128
|
const def = pkDefinition[i];
|
|
124
|
-
const comparison =
|
|
129
|
+
const comparison = comparators[i](arrA[i], arrB[i]);
|
|
125
130
|
|
|
126
131
|
if (comparison !== 0) {
|
|
127
132
|
return def.desc ? -comparison : comparison;
|