@quereus/quereus 0.6.12 → 0.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.
Files changed (49) hide show
  1. package/dist/src/parser/lexer.d.ts +6 -0
  2. package/dist/src/parser/lexer.d.ts.map +1 -1
  3. package/dist/src/parser/lexer.js +33 -1
  4. package/dist/src/parser/lexer.js.map +1 -1
  5. package/dist/src/parser/parser.d.ts.map +1 -1
  6. package/dist/src/parser/parser.js +28 -24
  7. package/dist/src/parser/parser.js.map +1 -1
  8. package/dist/src/planner/building/select-aggregates.d.ts +6 -1
  9. package/dist/src/planner/building/select-aggregates.d.ts.map +1 -1
  10. package/dist/src/planner/building/select-aggregates.js +23 -4
  11. package/dist/src/planner/building/select-aggregates.js.map +1 -1
  12. package/dist/src/planner/building/select-modifiers.js +7 -2
  13. package/dist/src/planner/building/select-modifiers.js.map +1 -1
  14. package/dist/src/planner/building/select.d.ts.map +1 -1
  15. package/dist/src/planner/building/select.js +2 -2
  16. package/dist/src/planner/building/select.js.map +1 -1
  17. package/dist/src/planner/nodes/join-node.d.ts.map +1 -1
  18. package/dist/src/planner/nodes/join-node.js +6 -1
  19. package/dist/src/planner/nodes/join-node.js.map +1 -1
  20. package/dist/src/planner/rules/access/rule-select-access-path.js +15 -2
  21. package/dist/src/planner/rules/access/rule-select-access-path.js.map +1 -1
  22. package/dist/src/schema/manager.d.ts +30 -0
  23. package/dist/src/schema/manager.d.ts.map +1 -1
  24. package/dist/src/schema/manager.js +205 -0
  25. package/dist/src/schema/manager.js.map +1 -1
  26. package/dist/src/vtab/best-access-plan.d.ts +2 -0
  27. package/dist/src/vtab/best-access-plan.d.ts.map +1 -1
  28. package/dist/src/vtab/best-access-plan.js.map +1 -1
  29. package/dist/src/vtab/memory/layer/scan-plan.js +2 -2
  30. package/dist/src/vtab/memory/layer/scan-plan.js.map +1 -1
  31. package/dist/src/vtab/memory/module.d.ts +1 -1
  32. package/dist/src/vtab/memory/module.d.ts.map +1 -1
  33. package/dist/src/vtab/memory/module.js +2 -1
  34. package/dist/src/vtab/memory/module.js.map +1 -1
  35. package/dist/src/vtab/module.d.ts +2 -1
  36. package/dist/src/vtab/module.d.ts.map +1 -1
  37. package/package.json +1 -1
  38. package/src/parser/lexer.ts +806 -771
  39. package/src/parser/parser.ts +3352 -3347
  40. package/src/planner/building/select-aggregates.ts +30 -5
  41. package/src/planner/building/select-modifiers.ts +8 -2
  42. package/src/planner/building/select.ts +567 -560
  43. package/src/planner/nodes/join-node.ts +6 -1
  44. package/src/planner/rules/access/rule-select-access-path.ts +399 -384
  45. package/src/schema/manager.ts +235 -1
  46. package/src/vtab/best-access-plan.ts +2 -0
  47. package/src/vtab/memory/layer/scan-plan.ts +2 -2
  48. package/src/vtab/memory/module.ts +2 -1
  49. package/src/vtab/module.ts +162 -160
@@ -1,384 +1,399 @@
1
- /**
2
- * Rule: Select Access Path
3
- *
4
- * Required Characteristics:
5
- * - Node must be a RetrieveNode representing a virtual table access boundary
6
- * - Module must support either supports() (query-based) or getBestAccessPlan() (index-based)
7
- *
8
- * Applied When:
9
- * - RetrieveNode needs to be converted to appropriate physical access method
10
- *
11
- * Benefits: Enables cost-based access path selection and module-specific execution
12
- */
13
-
14
- import { createLogger } from '../../../common/logger.js';
15
- import type { PlanNode, ScalarPlanNode } from '../../nodes/plan-node.js';
16
- import { isRelationalNode, type RelationalPlanNode } from '../../nodes/plan-node.js';
17
- import type { OptContext } from '../../framework/context.js';
18
- import { RetrieveNode } from '../../nodes/retrieve-node.js';
19
- import { RemoteQueryNode } from '../../nodes/remote-query-node.js';
20
- import { SeqScanNode, IndexScanNode, IndexSeekNode } from '../../nodes/table-access-nodes.js';
21
- import { seqScanCost } from '../../cost/index.js';
22
- import type { ColumnMeta, BestAccessPlanRequest, BestAccessPlanResult } from '../../../vtab/best-access-plan.js';
23
- import { FilterInfo } from '../../../vtab/filter-info.js';
24
- import type { IndexConstraintUsage } from '../../../vtab/index-info.js';
25
- import { TableReferenceNode } from '../../nodes/reference.js';
26
- import { FilterNode } from '../../nodes/filter.js';
27
- import { extractConstraintsForTable, type PredicateConstraint as PlannerPredicateConstraint, createTableInfoFromNode } from '../../analysis/constraint-extractor.js';
28
- import { LiteralNode } from '../../nodes/scalar.js';
29
- import type * as AST from '../../../parser/ast.js';
30
- import { IndexConstraintOp } from '../../../common/constants.js';
31
-
32
- const log = createLogger('optimizer:rule:select-access-path');
33
-
34
- export function ruleSelectAccessPath(node: PlanNode, context: OptContext): PlanNode | null {
35
- // Guard: node must be a RetrieveNode
36
- if (!(node instanceof RetrieveNode)) {
37
- return null;
38
- }
39
-
40
- const retrieveNode = node as RetrieveNode;
41
- const tableSchema = retrieveNode.tableRef.tableSchema;
42
- const vtabModule = retrieveNode.vtabModule;
43
-
44
- log('Selecting access path for retrieve over table %s', tableSchema.name);
45
-
46
- // Always allow fallback to sequential scan to guarantee physicalization
47
- // even when no specialized support is available.
48
-
49
- // If grow-retrieve established an index-style context, reuse it directly
50
- if (isIndexStyleContext(retrieveNode.moduleCtx)) {
51
- log('Using index-style context provided by grow-retrieve');
52
- const accessPlan = retrieveNode.moduleCtx.accessPlan;
53
- const originalConstraints = retrieveNode.moduleCtx.originalConstraints as unknown as PlannerPredicateConstraint[];
54
- const physicalLeaf: RelationalPlanNode = selectPhysicalNode(retrieveNode.tableRef, accessPlan, originalConstraints) as unknown as RelationalPlanNode;
55
- if (retrieveNode.moduleCtx.residualPredicate) {
56
- return new FilterNode(retrieveNode.scope, physicalLeaf, retrieveNode.moduleCtx.residualPredicate);
57
- }
58
- return physicalLeaf;
59
- }
60
-
61
- // Check if module supports query-based execution via supports() method
62
- if (vtabModule.supports && typeof vtabModule.supports === 'function') {
63
- log('Module has supports() method - checking support for current pipeline');
64
-
65
- // Check if module supports the current pipeline
66
- const assessment = vtabModule.supports(retrieveNode.source);
67
-
68
- if (assessment) {
69
- log('Pipeline supported - creating RemoteQueryNode (cost: %d)', assessment.cost);
70
- return new RemoteQueryNode(
71
- retrieveNode.scope,
72
- retrieveNode.source,
73
- retrieveNode.tableRef,
74
- assessment.ctx
75
- );
76
- } else {
77
- log('Pipeline not supported by module - falling back to sequential scan');
78
- return createSeqScan(retrieveNode.tableRef);
79
- }
80
- }
81
-
82
- // Check if module supports index-based execution via getBestAccessPlan() method
83
- if (vtabModule.getBestAccessPlan && typeof vtabModule.getBestAccessPlan === 'function') {
84
- log('Module has getBestAccessPlan() method - using index-based execution for %s', tableSchema.name);
85
-
86
- return createIndexBasedAccess(retrieveNode, context);
87
- }
88
-
89
- // Fall back to sequential scan if module has no access planning support
90
- log('No access planning support, using sequential scan for %s', tableSchema.name);
91
- return createSeqScan(retrieveNode.tableRef);
92
- }
93
-
94
- /**
95
- * Create index-based access for modules that support getBestAccessPlan()
96
- */
97
- function createIndexBasedAccess(retrieveNode: RetrieveNode, context: OptContext): PlanNode {
98
- const tableSchema = retrieveNode.tableRef.tableSchema;
99
- const vtabModule = retrieveNode.vtabModule;
100
-
101
- // Check if we have pre-computed access plan from ruleGrowRetrieve
102
- const indexCtx = retrieveNode.moduleCtx as any; // IndexStyleContext from grow rule
103
- let accessPlan: BestAccessPlanResult;
104
- let constraints: PlannerPredicateConstraint[];
105
- let residualPredicate: PlanNode | undefined;
106
-
107
- if (indexCtx?.accessPlan) {
108
- // Use pre-computed access plan from grow rule
109
- log('Using pre-computed access plan from grow rule');
110
- accessPlan = indexCtx.accessPlan;
111
- constraints = indexCtx.originalConstraints || [];
112
- residualPredicate = indexCtx.residualPredicate;
113
- } else {
114
- // Extract constraints from grown pipeline in source using table instance key
115
- const tInfo = createTableInfoFromNode(retrieveNode.tableRef, `${tableSchema.schemaName}.${tableSchema.name}`);
116
- constraints = extractConstraintsForTable(retrieveNode.source, tInfo.relationKey);
117
-
118
- // Build request for getBestAccessPlan
119
- const request: BestAccessPlanRequest = {
120
- columns: tableSchema.columns.map((col, index) => ({
121
- index,
122
- name: col.name,
123
- type: col.logicalType,
124
- isPrimaryKey: col.primaryKey || false,
125
- isUnique: col.primaryKey || false // For now, assume only PK columns are unique
126
- } as ColumnMeta)),
127
- filters: constraints,
128
- estimatedRows: retrieveNode.tableRef.estimatedRows
129
- };
130
-
131
- // Use the vtab module's getBestAccessPlan method to get an optimized access plan
132
- accessPlan = vtabModule.getBestAccessPlan!(context.db, tableSchema, request) as BestAccessPlanResult;
133
- }
134
-
135
- // Choose physical node based on access plan
136
- const physicalLeaf: RelationalPlanNode = selectPhysicalNode(retrieveNode.tableRef, accessPlan, constraints) as unknown as RelationalPlanNode;
137
-
138
- // If the Retrieve source contained a pipeline (e.g., Filter/Sort/Project), rebuild it above the physical leaf
139
- let rebuiltPipeline: RelationalPlanNode = physicalLeaf;
140
- if (retrieveNode.source !== retrieveNode.tableRef) {
141
- log('Rebuilding Retrieve pipeline above physical access node');
142
- rebuiltPipeline = rebuildPipelineWithNewLeaf(retrieveNode.source, retrieveNode.tableRef, physicalLeaf);
143
- }
144
-
145
- // Wrap with residual predicate if present (on top of rebuilt pipeline)
146
- let finalNode: PlanNode = rebuiltPipeline;
147
- if (residualPredicate) {
148
- log('Wrapping rebuilt pipeline with residual filter');
149
- finalNode = new FilterNode(rebuiltPipeline.scope, rebuiltPipeline as any, residualPredicate as any);
150
- }
151
-
152
- log('Selected access for table %s (cost: %f, rows: %s)', tableSchema.name, accessPlan.cost, accessPlan.rows);
153
- return finalNode;
154
- }
155
-
156
- /**
157
- * Rebuilds a relational pipeline by replacing the specified leaf with a new leaf.
158
- * Preserves all operators (e.g., Filter, Sort, Project) above the leaf.
159
- */
160
- function rebuildPipelineWithNewLeaf(
161
- pipelineRoot: RelationalPlanNode,
162
- oldLeaf: RelationalPlanNode,
163
- newLeaf: RelationalPlanNode
164
- ): RelationalPlanNode {
165
- if (pipelineRoot === oldLeaf) {
166
- return newLeaf;
167
- }
168
- const children = pipelineRoot.getChildren();
169
- const newChildren: PlanNode[] = children.map(child => {
170
- if (isRelationalNode(child)) {
171
- return rebuildPipelineWithNewLeaf(child, oldLeaf, newLeaf);
172
- }
173
- return child; // keep scalar children unchanged
174
- });
175
- return pipelineRoot.withChildren(newChildren) as RelationalPlanNode;
176
- }
177
-
178
- /**
179
- * Select the appropriate physical node based on access plan
180
- */
181
- function selectPhysicalNode(
182
- tableRef: TableReferenceNode,
183
- accessPlan: BestAccessPlanResult,
184
- constraints: PlannerPredicateConstraint[]
185
- ): SeqScanNode | IndexScanNode | IndexSeekNode {
186
-
187
- // Create a default FilterInfo for the physical nodes
188
- const filterInfo: FilterInfo = {
189
- idxNum: 0,
190
- idxStr: 'fullscan',
191
- constraints: [],
192
- args: [],
193
- indexInfoOutput: {
194
- nConstraint: 0,
195
- aConstraint: [],
196
- nOrderBy: 0,
197
- aOrderBy: [],
198
- aConstraintUsage: [] as IndexConstraintUsage[],
199
- idxNum: 0,
200
- idxStr: 'fullscan',
201
- orderByConsumed: false,
202
- estimatedCost: accessPlan.cost,
203
- estimatedRows: BigInt(accessPlan.rows || 1000),
204
- idxFlags: 0,
205
- colUsed: 0n,
206
- }
207
- };
208
-
209
- // Analyze the access plan to determine node type
210
- // Determine handled constraints by column index, not array position (robust to ordering)
211
- const handledByCol = new Set<number>();
212
- constraints.forEach((c, i) => {
213
- if (accessPlan.handledFilters[i] === true) handledByCol.add(c.columnIndex);
214
- });
215
- const eqHandled = constraints.filter(c => c.op === '=');
216
- const hasEqualityConstraints = eqHandled.length > 0;
217
- const hasRangeConstraints = constraints.some(c => ['>', '>=', '<', '<='].includes(c.op) && handledByCol.has(c.columnIndex));
218
-
219
- // Convert OrderingSpec[] to the format expected by physical nodes
220
- const providesOrdering = accessPlan.providesOrdering?.map(spec => ({
221
- column: spec.columnIndex,
222
- desc: spec.desc
223
- }));
224
-
225
- // Decision logic for access method
226
- const maybeRows = accessPlan.rows || 0;
227
- const pkCols = tableRef.tableSchema.primaryKeyDefinition ?? [];
228
- const eqByCol = new Map<number, PlannerPredicateConstraint>();
229
- for (const c of eqHandled) eqByCol.set(c.columnIndex, c);
230
- const coversPk = pkCols.length > 0 && pkCols.every(pk => eqByCol.has(pk.index));
231
-
232
- // If module didn't report handledFilters properly but we have full-PK equality,
233
- // treat it as handled for the purpose of selecting IndexSeek.
234
- const treatAsHandledPk = coversPk && pkCols.every(pk => handledByCol.has(pk.index) || eqByCol.has(pk.index));
235
-
236
- if ((hasEqualityConstraints && coversPk || treatAsHandledPk) && maybeRows <= 10) {
237
- // Build seek keys (as ScalarPlanNode) and constraint wiring for runtime args
238
- const seekKeys = pkCols.map(pk => {
239
- const c = eqByCol.get(pk.index)!;
240
- if (c.valueExpr) return c.valueExpr as unknown as ScalarPlanNode;
241
- const lit: AST.LiteralExpr = { type: 'literal', value: c.value } as unknown as AST.LiteralExpr;
242
- return new LiteralNode(tableRef.scope, lit);
243
- });
244
-
245
- // Build FilterInfo with EQ constraints carrying argvIndex placeholders
246
- const eqConstraints = pkCols.map((pk, i) => ({
247
- constraint: { iColumn: pk.index, op: IndexConstraintOp.EQ, usable: true },
248
- argvIndex: i + 1,
249
- }));
250
- const fi: FilterInfo = {
251
- ...filterInfo,
252
- constraints: eqConstraints,
253
- // idxStr plan=2 (equality); include primary idx tag for clarity
254
- idxStr: 'idx=_primary_(0);plan=2',
255
- };
256
-
257
- log('Using index seek on primary key');
258
- return new IndexSeekNode(
259
- tableRef.scope,
260
- tableRef,
261
- fi,
262
- 'primary',
263
- seekKeys,
264
- false,
265
- providesOrdering,
266
- accessPlan.cost
267
- );
268
- }
269
-
270
- if (hasRangeConstraints) {
271
- // Build dynamic range seek using IndexSeek with lower/upper bound expressions
272
- // Choose first handled range column (typically leading PK/index column)
273
- const rangeCols = constraints
274
- .filter(c => ['>', '>=', '<', '<='].includes(c.op) && handledByCol.has(c.columnIndex))
275
- .sort((a, b) => a.columnIndex - b.columnIndex);
276
-
277
- const primaryFirstCol = (tableRef.tableSchema.primaryKeyDefinition?.[0]?.index) ?? (rangeCols[0]?.columnIndex ?? 0);
278
- const lower = rangeCols.find(c => c.columnIndex === primaryFirstCol && (c.op === '>' || c.op === '>=')) as unknown as PlannerPredicateConstraint | undefined;
279
- const upper = rangeCols.find(c => c.columnIndex === primaryFirstCol && (c.op === '<' || c.op === '<=')) as unknown as PlannerPredicateConstraint | undefined;
280
-
281
- const seekKeys: ScalarPlanNode[] = [];
282
- const rangeConstraints: { constraint: { iColumn: number; op: number; usable: boolean }; argvIndex: number }[] = [] as any;
283
-
284
- let argv = 1;
285
- if (lower) {
286
- // argvIndex starts at 1
287
- rangeConstraints.push({ constraint: { iColumn: primaryFirstCol, op: opToIndexOp(lower.op as any), usable: true }, argvIndex: argv });
288
- seekKeys.push(lower.valueExpr ? lower.valueExpr as any : new LiteralNode(tableRef.scope, { type: 'literal', value: lower.value } as any));
289
- argv++;
290
- }
291
- if (upper) {
292
- rangeConstraints.push({ constraint: { iColumn: primaryFirstCol, op: opToIndexOp(upper.op as any), usable: true }, argvIndex: argv });
293
- seekKeys.push(upper.valueExpr ? upper.valueExpr as any : new LiteralNode(tableRef.scope, { type: 'literal', value: upper.value } as any));
294
- argv++;
295
- }
296
-
297
- const fi: FilterInfo = {
298
- ...filterInfo,
299
- constraints: rangeConstraints as any,
300
- idxStr: 'idx=_primary_(0);plan=3',
301
- };
302
-
303
- log('Using index seek (range) on primary key');
304
- return new IndexSeekNode(
305
- tableRef.scope,
306
- tableRef,
307
- fi,
308
- 'primary',
309
- seekKeys,
310
- true,
311
- providesOrdering,
312
- accessPlan.cost
313
- );
314
- } else if (providesOrdering) {
315
- // Ordering benefit without explicit range constraints: prefer index scan
316
- log('Using index scan (ordering provided)');
317
- return new IndexScanNode(
318
- tableRef.scope,
319
- tableRef,
320
- filterInfo,
321
- 'primary',
322
- providesOrdering,
323
- accessPlan.cost
324
- );
325
- } else {
326
- // Fall back to sequential scan
327
- log('Using sequential scan (no beneficial index access)');
328
- return createSeqScan(tableRef, filterInfo, accessPlan.cost);
329
- }
330
- }
331
-
332
- // Narrow module context originating from grow-retrieve index-style fallback
333
- function isIndexStyleContext(ctx: unknown): ctx is { kind: 'index-style'; accessPlan: BestAccessPlanResult; residualPredicate?: ScalarPlanNode; originalConstraints: unknown[] } {
334
- return !!ctx && typeof ctx === 'object' && (ctx as any).kind === 'index-style';
335
- }
336
-
337
- /**
338
- * Create a sequential scan node
339
- */
340
- function createSeqScan(tableRef: TableReferenceNode, filterInfo?: FilterInfo, cost?: number): SeqScanNode {
341
- const tableRows = tableRef.estimatedRows || 1000;
342
- const scanCost = cost ?? seqScanCost(tableRows);
343
-
344
- // Create default FilterInfo if not provided
345
- const effectiveFilterInfo = filterInfo || {
346
- idxNum: 0,
347
- idxStr: 'fullscan',
348
- constraints: [],
349
- args: [],
350
- indexInfoOutput: {
351
- nConstraint: 0,
352
- aConstraint: [],
353
- nOrderBy: 0,
354
- aOrderBy: [],
355
- aConstraintUsage: [] as IndexConstraintUsage[],
356
- idxNum: 0,
357
- idxStr: 'fullscan',
358
- orderByConsumed: false,
359
- estimatedCost: scanCost,
360
- estimatedRows: BigInt(tableRows),
361
- idxFlags: 0,
362
- colUsed: 0n,
363
- }
364
- };
365
-
366
- const seqScan = new SeqScanNode(
367
- tableRef.scope,
368
- tableRef,
369
- effectiveFilterInfo,
370
- scanCost
371
- );
372
-
373
- return seqScan;
374
- }
375
-
376
- function opToIndexOp(op: '>' | '>=' | '<' | '<='): number {
377
- switch (op) {
378
- case '>': return IndexConstraintOp.GT as unknown as number;
379
- case '>=': return IndexConstraintOp.GE as unknown as number;
380
- case '<': return IndexConstraintOp.LT as unknown as number;
381
- case '<=': return IndexConstraintOp.LE as unknown as number;
382
- default: return IndexConstraintOp.GE as unknown as number;
383
- }
384
- }
1
+ /**
2
+ * Rule: Select Access Path
3
+ *
4
+ * Required Characteristics:
5
+ * - Node must be a RetrieveNode representing a virtual table access boundary
6
+ * - Module must support either supports() (query-based) or getBestAccessPlan() (index-based)
7
+ *
8
+ * Applied When:
9
+ * - RetrieveNode needs to be converted to appropriate physical access method
10
+ *
11
+ * Benefits: Enables cost-based access path selection and module-specific execution
12
+ */
13
+
14
+ import { createLogger } from '../../../common/logger.js';
15
+ import type { PlanNode, ScalarPlanNode } from '../../nodes/plan-node.js';
16
+ import { isRelationalNode, type RelationalPlanNode } from '../../nodes/plan-node.js';
17
+ import type { OptContext } from '../../framework/context.js';
18
+ import { RetrieveNode } from '../../nodes/retrieve-node.js';
19
+ import { RemoteQueryNode } from '../../nodes/remote-query-node.js';
20
+ import { SeqScanNode, IndexScanNode, IndexSeekNode } from '../../nodes/table-access-nodes.js';
21
+ import { seqScanCost } from '../../cost/index.js';
22
+ import type { ColumnMeta, BestAccessPlanRequest, BestAccessPlanResult } from '../../../vtab/best-access-plan.js';
23
+ import { FilterInfo } from '../../../vtab/filter-info.js';
24
+ import type { IndexConstraintUsage } from '../../../vtab/index-info.js';
25
+ import { TableReferenceNode } from '../../nodes/reference.js';
26
+ import { FilterNode } from '../../nodes/filter.js';
27
+ import { extractConstraintsForTable, type PredicateConstraint as PlannerPredicateConstraint, createTableInfoFromNode } from '../../analysis/constraint-extractor.js';
28
+ import { LiteralNode } from '../../nodes/scalar.js';
29
+ import type * as AST from '../../../parser/ast.js';
30
+ import { IndexConstraintOp } from '../../../common/constants.js';
31
+
32
+ const log = createLogger('optimizer:rule:select-access-path');
33
+
34
+ export function ruleSelectAccessPath(node: PlanNode, context: OptContext): PlanNode | null {
35
+ // Guard: node must be a RetrieveNode
36
+ if (!(node instanceof RetrieveNode)) {
37
+ return null;
38
+ }
39
+
40
+ const retrieveNode = node as RetrieveNode;
41
+ const tableSchema = retrieveNode.tableRef.tableSchema;
42
+ const vtabModule = retrieveNode.vtabModule;
43
+
44
+ log('Selecting access path for retrieve over table %s', tableSchema.name);
45
+
46
+ // Always allow fallback to sequential scan to guarantee physicalization
47
+ // even when no specialized support is available.
48
+
49
+ // If grow-retrieve established an index-style context, reuse it directly
50
+ if (isIndexStyleContext(retrieveNode.moduleCtx)) {
51
+ log('Using index-style context provided by grow-retrieve');
52
+ const accessPlan = retrieveNode.moduleCtx.accessPlan;
53
+ const originalConstraints = retrieveNode.moduleCtx.originalConstraints as unknown as PlannerPredicateConstraint[];
54
+ const physicalLeaf: RelationalPlanNode = selectPhysicalNode(retrieveNode.tableRef, accessPlan, originalConstraints) as unknown as RelationalPlanNode;
55
+ if (retrieveNode.moduleCtx.residualPredicate) {
56
+ return new FilterNode(retrieveNode.scope, physicalLeaf, retrieveNode.moduleCtx.residualPredicate);
57
+ }
58
+ return physicalLeaf;
59
+ }
60
+
61
+ // Check if module supports query-based execution via supports() method
62
+ if (vtabModule.supports && typeof vtabModule.supports === 'function') {
63
+ log('Module has supports() method - checking support for current pipeline');
64
+
65
+ // Check if module supports the current pipeline
66
+ const assessment = vtabModule.supports(retrieveNode.source);
67
+
68
+ if (assessment) {
69
+ log('Pipeline supported - creating RemoteQueryNode (cost: %d)', assessment.cost);
70
+ return new RemoteQueryNode(
71
+ retrieveNode.scope,
72
+ retrieveNode.source,
73
+ retrieveNode.tableRef,
74
+ assessment.ctx
75
+ );
76
+ } else {
77
+ log('Pipeline not supported by module - falling back to sequential scan');
78
+ return createSeqScan(retrieveNode.tableRef);
79
+ }
80
+ }
81
+
82
+ // Check if module supports index-based execution via getBestAccessPlan() method
83
+ if (vtabModule.getBestAccessPlan && typeof vtabModule.getBestAccessPlan === 'function') {
84
+ log('Module has getBestAccessPlan() method - using index-based execution for %s', tableSchema.name);
85
+
86
+ return createIndexBasedAccess(retrieveNode, context);
87
+ }
88
+
89
+ // Fall back to sequential scan if module has no access planning support
90
+ log('No access planning support, using sequential scan for %s', tableSchema.name);
91
+ return createSeqScan(retrieveNode.tableRef);
92
+ }
93
+
94
+ /**
95
+ * Create index-based access for modules that support getBestAccessPlan()
96
+ */
97
+ function createIndexBasedAccess(retrieveNode: RetrieveNode, context: OptContext): PlanNode {
98
+ const tableSchema = retrieveNode.tableRef.tableSchema;
99
+ const vtabModule = retrieveNode.vtabModule;
100
+
101
+ // Check if we have pre-computed access plan from ruleGrowRetrieve
102
+ const indexCtx = retrieveNode.moduleCtx as any; // IndexStyleContext from grow rule
103
+ let accessPlan: BestAccessPlanResult;
104
+ let constraints: PlannerPredicateConstraint[];
105
+ let residualPredicate: PlanNode | undefined;
106
+
107
+ if (indexCtx?.accessPlan) {
108
+ // Use pre-computed access plan from grow rule
109
+ log('Using pre-computed access plan from grow rule');
110
+ accessPlan = indexCtx.accessPlan;
111
+ constraints = indexCtx.originalConstraints || [];
112
+ residualPredicate = indexCtx.residualPredicate;
113
+ } else {
114
+ // Extract constraints from grown pipeline in source using table instance key
115
+ const tInfo = createTableInfoFromNode(retrieveNode.tableRef, `${tableSchema.schemaName}.${tableSchema.name}`);
116
+ constraints = extractConstraintsForTable(retrieveNode.source, tInfo.relationKey);
117
+
118
+ // Build request for getBestAccessPlan
119
+ const request: BestAccessPlanRequest = {
120
+ columns: tableSchema.columns.map((col, index) => ({
121
+ index,
122
+ name: col.name,
123
+ type: col.logicalType,
124
+ isPrimaryKey: col.primaryKey || false,
125
+ isUnique: col.primaryKey || false // For now, assume only PK columns are unique
126
+ } as ColumnMeta)),
127
+ filters: constraints,
128
+ estimatedRows: retrieveNode.tableRef.estimatedRows
129
+ };
130
+
131
+ // Use the vtab module's getBestAccessPlan method to get an optimized access plan
132
+ accessPlan = vtabModule.getBestAccessPlan!(context.db, tableSchema, request) as BestAccessPlanResult;
133
+ }
134
+
135
+ // Choose physical node based on access plan
136
+ const physicalLeaf: RelationalPlanNode = selectPhysicalNode(retrieveNode.tableRef, accessPlan, constraints) as unknown as RelationalPlanNode;
137
+
138
+ // If the Retrieve source contained a pipeline (e.g., Filter/Sort/Project), rebuild it above the physical leaf
139
+ let rebuiltPipeline: RelationalPlanNode = physicalLeaf;
140
+ if (retrieveNode.source !== retrieveNode.tableRef) {
141
+ log('Rebuilding Retrieve pipeline above physical access node');
142
+ rebuiltPipeline = rebuildPipelineWithNewLeaf(retrieveNode.source, retrieveNode.tableRef, physicalLeaf);
143
+ }
144
+
145
+ // Wrap with residual predicate if present (on top of rebuilt pipeline)
146
+ let finalNode: PlanNode = rebuiltPipeline;
147
+ if (residualPredicate) {
148
+ log('Wrapping rebuilt pipeline with residual filter');
149
+ finalNode = new FilterNode(rebuiltPipeline.scope, rebuiltPipeline as any, residualPredicate as any);
150
+ }
151
+
152
+ log('Selected access for table %s (cost: %f, rows: %s)', tableSchema.name, accessPlan.cost, accessPlan.rows);
153
+ return finalNode;
154
+ }
155
+
156
+ /**
157
+ * Rebuilds a relational pipeline by replacing the specified leaf with a new leaf.
158
+ * Preserves all operators (e.g., Filter, Sort, Project) above the leaf.
159
+ */
160
+ function rebuildPipelineWithNewLeaf(
161
+ pipelineRoot: RelationalPlanNode,
162
+ oldLeaf: RelationalPlanNode,
163
+ newLeaf: RelationalPlanNode
164
+ ): RelationalPlanNode {
165
+ if (pipelineRoot === oldLeaf) {
166
+ return newLeaf;
167
+ }
168
+ const children = pipelineRoot.getChildren();
169
+ const newChildren: PlanNode[] = children.map(child => {
170
+ if (isRelationalNode(child)) {
171
+ return rebuildPipelineWithNewLeaf(child, oldLeaf, newLeaf);
172
+ }
173
+ return child; // keep scalar children unchanged
174
+ });
175
+ return pipelineRoot.withChildren(newChildren) as RelationalPlanNode;
176
+ }
177
+
178
+ /**
179
+ * Select the appropriate physical node based on access plan
180
+ */
181
+ function selectPhysicalNode(
182
+ tableRef: TableReferenceNode,
183
+ accessPlan: BestAccessPlanResult,
184
+ constraints: PlannerPredicateConstraint[]
185
+ ): SeqScanNode | IndexScanNode | IndexSeekNode {
186
+
187
+ // Create a default FilterInfo for the physical nodes
188
+ const filterInfo: FilterInfo = {
189
+ idxNum: 0,
190
+ idxStr: 'fullscan',
191
+ constraints: [],
192
+ args: [],
193
+ indexInfoOutput: {
194
+ nConstraint: 0,
195
+ aConstraint: [],
196
+ nOrderBy: 0,
197
+ aOrderBy: [],
198
+ aConstraintUsage: [] as IndexConstraintUsage[],
199
+ idxNum: 0,
200
+ idxStr: 'fullscan',
201
+ orderByConsumed: false,
202
+ estimatedCost: accessPlan.cost,
203
+ estimatedRows: BigInt(accessPlan.rows || 1000),
204
+ idxFlags: 0,
205
+ colUsed: 0n,
206
+ }
207
+ };
208
+
209
+ // Analyze the access plan to determine node type
210
+ // Determine handled constraints by column index, not array position (robust to ordering)
211
+ const handledByCol = new Set<number>();
212
+ constraints.forEach((c, i) => {
213
+ if (accessPlan.handledFilters[i] === true) handledByCol.add(c.columnIndex);
214
+ });
215
+ const eqHandled = constraints.filter(c => c.op === '=');
216
+ const hasEqualityConstraints = eqHandled.length > 0;
217
+ const hasRangeConstraints = constraints.some(c => ['>', '>=', '<', '<='].includes(c.op) && handledByCol.has(c.columnIndex));
218
+
219
+ // Convert OrderingSpec[] to the format expected by physical nodes
220
+ const providesOrdering = accessPlan.providesOrdering?.map(spec => ({
221
+ column: spec.columnIndex,
222
+ desc: spec.desc
223
+ }));
224
+
225
+ // Decision logic for access method
226
+ const maybeRows = accessPlan.rows || 0;
227
+ const pkCols = tableRef.tableSchema.primaryKeyDefinition ?? [];
228
+ const eqByCol = new Map<number, PlannerPredicateConstraint>();
229
+ for (const c of eqHandled) eqByCol.set(c.columnIndex, c);
230
+ const coversPk = pkCols.length > 0 && pkCols.every(pk => eqByCol.has(pk.index));
231
+
232
+ // If module didn't report handledFilters properly but we have full-PK equality,
233
+ // treat it as handled for the purpose of selecting IndexSeek.
234
+ const treatAsHandledPk = coversPk && pkCols.every(pk => handledByCol.has(pk.index) || eqByCol.has(pk.index));
235
+
236
+ if ((hasEqualityConstraints && coversPk || treatAsHandledPk) && maybeRows <= 10) {
237
+ // Build seek keys (as ScalarPlanNode) and constraint wiring for runtime args
238
+ const seekKeys = pkCols.map(pk => {
239
+ const c = eqByCol.get(pk.index)!;
240
+ if (c.valueExpr) return c.valueExpr as unknown as ScalarPlanNode;
241
+ const lit: AST.LiteralExpr = { type: 'literal', value: c.value } as unknown as AST.LiteralExpr;
242
+ return new LiteralNode(tableRef.scope, lit);
243
+ });
244
+
245
+ // Build FilterInfo with EQ constraints carrying argvIndex placeholders
246
+ const eqConstraints = pkCols.map((pk, i) => ({
247
+ constraint: { iColumn: pk.index, op: IndexConstraintOp.EQ, usable: true },
248
+ argvIndex: i + 1,
249
+ }));
250
+ const fi: FilterInfo = {
251
+ ...filterInfo,
252
+ constraints: eqConstraints,
253
+ // idxStr plan=2 (equality); include primary idx tag for clarity
254
+ idxStr: 'idx=_primary_(0);plan=2',
255
+ };
256
+
257
+ log('Using index seek on primary key');
258
+ return new IndexSeekNode(
259
+ tableRef.scope,
260
+ tableRef,
261
+ fi,
262
+ 'primary',
263
+ seekKeys,
264
+ false,
265
+ providesOrdering,
266
+ accessPlan.cost
267
+ );
268
+ }
269
+
270
+ if (hasRangeConstraints) {
271
+ // Build dynamic range seek using IndexSeek with lower/upper bound expressions
272
+ // Choose first handled range column (typically leading PK/index column)
273
+ const rangeCols = constraints
274
+ .filter(c => ['>', '>=', '<', '<='].includes(c.op) && handledByCol.has(c.columnIndex))
275
+ .sort((a, b) => a.columnIndex - b.columnIndex);
276
+
277
+ const primaryFirstCol = (tableRef.tableSchema.primaryKeyDefinition?.[0]?.index) ?? (rangeCols[0]?.columnIndex ?? 0);
278
+ const lower = rangeCols.find(c => c.columnIndex === primaryFirstCol && (c.op === '>' || c.op === '>=')) as unknown as PlannerPredicateConstraint | undefined;
279
+ const upper = rangeCols.find(c => c.columnIndex === primaryFirstCol && (c.op === '<' || c.op === '<=')) as unknown as PlannerPredicateConstraint | undefined;
280
+
281
+ const seekKeys: ScalarPlanNode[] = [];
282
+ const rangeConstraints: { constraint: { iColumn: number; op: number; usable: boolean }; argvIndex: number }[] = [] as any;
283
+
284
+ let argv = 1;
285
+ if (lower) {
286
+ // argvIndex starts at 1
287
+ rangeConstraints.push({ constraint: { iColumn: primaryFirstCol, op: opToIndexOp(lower.op as any), usable: true }, argvIndex: argv });
288
+ seekKeys.push(lower.valueExpr ? lower.valueExpr as any : new LiteralNode(tableRef.scope, { type: 'literal', value: lower.value } as any));
289
+ argv++;
290
+ }
291
+ if (upper) {
292
+ rangeConstraints.push({ constraint: { iColumn: primaryFirstCol, op: opToIndexOp(upper.op as any), usable: true }, argvIndex: argv });
293
+ seekKeys.push(upper.valueExpr ? upper.valueExpr as any : new LiteralNode(tableRef.scope, { type: 'literal', value: upper.value } as any));
294
+ argv++;
295
+ }
296
+
297
+ const fi: FilterInfo = {
298
+ ...filterInfo,
299
+ constraints: rangeConstraints as any,
300
+ idxStr: 'idx=_primary_(0);plan=3',
301
+ };
302
+
303
+ log('Using index seek (range) on primary key');
304
+ return new IndexSeekNode(
305
+ tableRef.scope,
306
+ tableRef,
307
+ fi,
308
+ 'primary',
309
+ seekKeys,
310
+ true,
311
+ providesOrdering,
312
+ accessPlan.cost
313
+ );
314
+ } else if (providesOrdering) {
315
+ // Ordering benefit without explicit range constraints: prefer index scan
316
+ // Use the index that provides the ordering, or fall back to primary
317
+ const indexName = accessPlan.orderingIndexName ?? 'primary';
318
+ log('Using index scan (ordering provided by %s)', indexName);
319
+
320
+ // Build FilterInfo with the correct index name for the runtime
321
+ const indexIdxStr = indexName === 'primary' ? '_primary_' : indexName;
322
+ const orderingFilterInfo: FilterInfo = {
323
+ ...filterInfo,
324
+ idxStr: `idx=${indexIdxStr}(0);plan=0`,
325
+ indexInfoOutput: {
326
+ ...filterInfo.indexInfoOutput,
327
+ idxStr: `idx=${indexIdxStr}(0);plan=0`,
328
+ orderByConsumed: true,
329
+ }
330
+ };
331
+
332
+ return new IndexScanNode(
333
+ tableRef.scope,
334
+ tableRef,
335
+ orderingFilterInfo,
336
+ indexName,
337
+ providesOrdering,
338
+ accessPlan.cost
339
+ );
340
+ } else {
341
+ // Fall back to sequential scan
342
+ log('Using sequential scan (no beneficial index access)');
343
+ return createSeqScan(tableRef, filterInfo, accessPlan.cost);
344
+ }
345
+ }
346
+
347
+ // Narrow module context originating from grow-retrieve index-style fallback
348
+ function isIndexStyleContext(ctx: unknown): ctx is { kind: 'index-style'; accessPlan: BestAccessPlanResult; residualPredicate?: ScalarPlanNode; originalConstraints: unknown[] } {
349
+ return !!ctx && typeof ctx === 'object' && (ctx as any).kind === 'index-style';
350
+ }
351
+
352
+ /**
353
+ * Create a sequential scan node
354
+ */
355
+ function createSeqScan(tableRef: TableReferenceNode, filterInfo?: FilterInfo, cost?: number): SeqScanNode {
356
+ const tableRows = tableRef.estimatedRows || 1000;
357
+ const scanCost = cost ?? seqScanCost(tableRows);
358
+
359
+ // Create default FilterInfo if not provided
360
+ const effectiveFilterInfo = filterInfo || {
361
+ idxNum: 0,
362
+ idxStr: 'fullscan',
363
+ constraints: [],
364
+ args: [],
365
+ indexInfoOutput: {
366
+ nConstraint: 0,
367
+ aConstraint: [],
368
+ nOrderBy: 0,
369
+ aOrderBy: [],
370
+ aConstraintUsage: [] as IndexConstraintUsage[],
371
+ idxNum: 0,
372
+ idxStr: 'fullscan',
373
+ orderByConsumed: false,
374
+ estimatedCost: scanCost,
375
+ estimatedRows: BigInt(tableRows),
376
+ idxFlags: 0,
377
+ colUsed: 0n,
378
+ }
379
+ };
380
+
381
+ const seqScan = new SeqScanNode(
382
+ tableRef.scope,
383
+ tableRef,
384
+ effectiveFilterInfo,
385
+ scanCost
386
+ );
387
+
388
+ return seqScan;
389
+ }
390
+
391
+ function opToIndexOp(op: '>' | '>=' | '<' | '<='): number {
392
+ switch (op) {
393
+ case '>': return IndexConstraintOp.GT as unknown as number;
394
+ case '>=': return IndexConstraintOp.GE as unknown as number;
395
+ case '<': return IndexConstraintOp.LT as unknown as number;
396
+ case '<=': return IndexConstraintOp.LE as unknown as number;
397
+ default: return IndexConstraintOp.GE as unknown as number;
398
+ }
399
+ }