@syncular/typegen 0.58.0 → 0.60.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 (78) hide show
  1. package/README.md +96 -43
  2. package/dist/cli.js +8 -3
  3. package/dist/emit-dart.d.ts +4 -3
  4. package/dist/emit-dart.js +40 -100
  5. package/dist/emit-kotlin.d.ts +4 -4
  6. package/dist/emit-kotlin.js +41 -99
  7. package/dist/emit-queries-dart.d.ts +1 -1
  8. package/dist/emit-queries-dart.js +17 -26
  9. package/dist/emit-queries-kotlin.d.ts +1 -1
  10. package/dist/emit-queries-kotlin.js +15 -24
  11. package/dist/emit-queries-rust.d.ts +1 -1
  12. package/dist/emit-queries-rust.js +18 -19
  13. package/dist/emit-queries-swift.d.ts +1 -1
  14. package/dist/emit-queries-swift.js +22 -26
  15. package/dist/emit-queries.d.ts +1 -1
  16. package/dist/emit-queries.js +18 -27
  17. package/dist/emit-swift.d.ts +4 -4
  18. package/dist/emit-swift.js +43 -102
  19. package/dist/emit.d.ts +3 -3
  20. package/dist/emit.js +37 -113
  21. package/dist/errors.d.ts +6 -2
  22. package/dist/errors.js +10 -2
  23. package/dist/fmt.js +11 -2
  24. package/dist/generate.d.ts +4 -3
  25. package/dist/generate.js +61 -73
  26. package/dist/init.js +5 -6
  27. package/dist/ir.d.ts +14 -2
  28. package/dist/ir.js +67 -49
  29. package/dist/lower.d.ts +0 -7
  30. package/dist/lower.js +0 -62
  31. package/dist/lsp.js +6 -1
  32. package/dist/manifest.d.ts +1 -2
  33. package/dist/manifest.js +13 -12
  34. package/dist/migration-lock.d.ts +32 -23
  35. package/dist/migration-lock.js +115 -92
  36. package/dist/naming.d.ts +25 -0
  37. package/dist/naming.js +92 -0
  38. package/dist/query.d.ts +94 -14
  39. package/dist/query.js +642 -461
  40. package/dist/sql.js +18 -3
  41. package/dist/syql-ast.d.ts +2 -0
  42. package/dist/syql-ast.js +7 -1
  43. package/dist/syql-browser.js +7 -6
  44. package/dist/syql-diagnostics.d.ts +1 -0
  45. package/dist/syql-diagnostics.js +1 -0
  46. package/dist/syql-lowering.js +11 -1
  47. package/dist/syql-parser.d.ts +4 -1
  48. package/dist/syql-parser.js +42 -5
  49. package/dist/syql-validator.js +13 -220
  50. package/package.json +3 -3
  51. package/src/cli.ts +9 -3
  52. package/src/emit-dart.ts +44 -116
  53. package/src/emit-kotlin.ts +41 -118
  54. package/src/emit-queries-dart.ts +23 -32
  55. package/src/emit-queries-kotlin.ts +20 -30
  56. package/src/emit-queries-rust.ts +29 -31
  57. package/src/emit-queries-swift.ts +26 -32
  58. package/src/emit-queries.ts +26 -47
  59. package/src/emit-swift.ts +49 -120
  60. package/src/emit.ts +45 -135
  61. package/src/errors.ts +13 -2
  62. package/src/fmt.ts +10 -2
  63. package/src/generate.ts +113 -107
  64. package/src/init.ts +8 -6
  65. package/src/ir.ts +91 -54
  66. package/src/lower.ts +0 -53
  67. package/src/lsp.ts +11 -4
  68. package/src/manifest.ts +22 -18
  69. package/src/migration-lock.ts +192 -137
  70. package/src/naming.ts +154 -0
  71. package/src/query.ts +864 -502
  72. package/src/sql.ts +30 -3
  73. package/src/syql-ast.ts +9 -0
  74. package/src/syql-browser.ts +8 -7
  75. package/src/syql-diagnostics.ts +2 -0
  76. package/src/syql-lowering.ts +16 -1
  77. package/src/syql-parser.ts +73 -8
  78. package/src/syql-validator.ts +21 -279
package/src/sql.ts CHANGED
@@ -242,7 +242,11 @@ function tokenizeStatements(sql: string, source: string): Token[][] {
242
242
  );
243
243
  }
244
244
  i = end;
245
- } else if (DIGIT.test(ch)) {
245
+ } else if (
246
+ DIGIT.test(ch) ||
247
+ ((ch === '-' || ch === '+') && DIGIT.test(sql[i + 1] ?? ''))
248
+ ) {
249
+ // A leading sign belongs to the number: `DEFAULT -1` is a literal.
246
250
  let end = i + 1;
247
251
  while (end < sql.length && /[0-9.]/.test(sql[end] as string)) end += 1;
248
252
  current.push({ kind: 'number', text: sql.slice(i, end) });
@@ -333,8 +337,12 @@ class Cursor {
333
337
  }
334
338
  }
335
339
 
336
- fail(message: string): never {
337
- throw new TypegenError(this.source, message);
340
+ fail(
341
+ message: string,
342
+ code?: string,
343
+ details?: Readonly<Record<string, unknown>>,
344
+ ): never {
345
+ throw new TypegenError(this.source, message, code, details);
338
346
  }
339
347
  }
340
348
 
@@ -401,6 +409,25 @@ function parseColumnDef(cursor: Cursor, allowPrimaryKey: boolean): ColumnDef {
401
409
  `column ${name}: DEFAULT expressions are unsupported (only literals)`,
402
410
  );
403
411
  }
412
+ // A word is a literal only as one of SQLite's keyword literals, so
413
+ // `DEFAULT NOT NULL` cannot swallow the NOT of a constraint.
414
+ if (
415
+ value.kind === 'word' &&
416
+ ![
417
+ 'NULL',
418
+ 'TRUE',
419
+ 'FALSE',
420
+ 'CURRENT_TIME',
421
+ 'CURRENT_DATE',
422
+ 'CURRENT_TIMESTAMP',
423
+ ].includes(value.text.toUpperCase())
424
+ ) {
425
+ cursor.fail(
426
+ 'DEFAULT needs a literal value: a number, a string, NULL, TRUE, FALSE, or CURRENT_TIME/DATE/TIMESTAMP',
427
+ 'typegen.invalid_default',
428
+ { column: name, found: value.text },
429
+ );
430
+ }
404
431
  // Literal defaults are accepted and ignored: typegen extracts the
405
432
  // schema shape; running migrations is the host's job.
406
433
  } else if (word === 'REFERENCES') {
package/src/syql-ast.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /** Stable, span-free semantic AST used by revision-1 conformance fixtures. */
2
+ import type { QueryAcknowledgement } from './query';
2
3
  import { isSyqlTrivia, type SyqlToken } from './syql-lexer';
3
4
  import type {
4
5
  SyqlDeclaration,
@@ -99,6 +100,7 @@ export type SyqlSemanticDeclaration =
99
100
  readonly defaultSize: number;
100
101
  readonly maxSize: number;
101
102
  };
103
+ readonly acknowledgements?: readonly QueryAcknowledgement[];
102
104
  };
103
105
 
104
106
  export interface SyqlSemanticFile {
@@ -261,6 +263,13 @@ function semanticDeclaration(
261
263
  maxSize: declaration.limit.maxSize,
262
264
  },
263
265
  }),
266
+ ...(declaration.acknowledgements.length === 0
267
+ ? {}
268
+ : {
269
+ acknowledgements: declaration.acknowledgements.map((ack) => ({
270
+ ...ack,
271
+ })),
272
+ }),
264
273
  };
265
274
  }
266
275
 
@@ -9,6 +9,7 @@
9
9
  import { formatSyql } from './fmt';
10
10
  import type { IrDocument } from './ir';
11
11
  import {
12
+ checkQueryPlan,
12
13
  type QueryBackend,
13
14
  type QueryDb,
14
15
  type QueryNamingOptions,
@@ -65,13 +66,13 @@ export function compileSyqlSource(
65
66
  const file = options.file ?? SYQL_BROWSER_DEFAULT_FILE;
66
67
  const semantic = analyzeSyqlSemantics(singleFileGraph(file, source));
67
68
  const validated = validateSyqlProgram(semantic, ir, db, options.naming);
68
- return {
69
- queries: validated.queries.map((query) =>
70
- lowerSyqlQuery(query, ir, db, options.naming, {
71
- ...(options.backend === undefined ? {} : { backend: options.backend }),
72
- }),
73
- ),
74
- };
69
+ const queries = validated.queries.map((query) =>
70
+ lowerSyqlQuery(query, ir, db, options.naming, {
71
+ ...(options.backend === undefined ? {} : { backend: options.backend }),
72
+ }),
73
+ );
74
+ for (const query of queries) checkQueryPlan(query.analysis, ir, db);
75
+ return { queries };
75
76
  }
76
77
 
77
78
  /** Format one virtual source file with the canonical SYQL formatter. */
@@ -58,6 +58,8 @@ export const SYQL_DIAGNOSTIC_REMEDIES = {
58
58
  'Rewrite the parameter using a supported value, optional, range, group, sort, or limit form.',
59
59
  SYQL2012_INVALID_QUERY_BODY:
60
60
  'Rewrite the query body using the clause order and forms defined by the SYQL grammar.',
61
+ SYQL2013_INVALID_ACKNOWLEDGEMENT:
62
+ "Write the acknowledgement as allow <finding> <table> '<reason>' directly above a query, once per finding.",
61
63
  SYQL3001_EXPECTED_EMBEDDED_TOKEN:
62
64
  'Insert the required token in the embedded SQL construct.',
63
65
  SYQL3002_INVALID_BIND:
@@ -1,7 +1,7 @@
1
1
  /** Revision-1 SYQL logical-to-physical lowering (§§15–17). */
2
2
  import { TypegenError } from './errors';
3
3
  import type { IrDocument } from './ir';
4
- import { buildNamingMap } from './naming';
4
+ import { buildNamingMap, validateQueryIdentifier } from './naming';
5
5
  import {
6
6
  type AnalyzedQuery,
7
7
  analyzeStatement,
@@ -364,6 +364,20 @@ function publicInputs(
364
364
  maxSize: validated.limit.maxSize,
365
365
  });
366
366
  }
367
+ for (const input of result) {
368
+ for (const langName of [
369
+ input.langName,
370
+ ...(input.kind === 'group' ? input.members : []).map((m) => m.langName),
371
+ ...(input.kind === 'sort' ? input.profiles : []).map((p) => p.langName),
372
+ ]) {
373
+ validateQueryIdentifier(
374
+ langName,
375
+ naming.targets,
376
+ query.module.file,
377
+ `query ${declaration.name} input`,
378
+ );
379
+ }
380
+ }
367
381
  return result;
368
382
  }
369
383
 
@@ -654,6 +668,7 @@ export function lowerSyqlQuery(
654
668
  file: logical.module.file,
655
669
  reactive: validated.reactive,
656
670
  syql: metadata,
671
+ acknowledgements: logical.declaration.acknowledgements,
657
672
  };
658
673
  return {
659
674
  validated,
@@ -6,6 +6,7 @@ import {
6
6
  type SyqlSourceSpan,
7
7
  type SyqlToken,
8
8
  } from './syql-lexer';
9
+ import { QUERY_PLAN_FINDINGS, type QueryAcknowledgement } from './query';
9
10
  import {
10
11
  parseSyqlEmbeddedTemplate,
11
12
  type SyqlEmbeddedTemplate,
@@ -151,6 +152,8 @@ export interface SyqlQueryDeclaration {
151
152
  readonly statement: SyqlTemplate;
152
153
  readonly sort?: SyqlSortSection;
153
154
  readonly limit?: SyqlLimitDeclaration;
155
+ /** Query plan findings acknowledged before the declaration (§14.5). */
156
+ readonly acknowledgements: readonly QueryAcknowledgement[];
154
157
  readonly span: SyqlSourceSpan;
155
158
  readonly nameSpan: SyqlSourceSpan;
156
159
  }
@@ -181,7 +184,8 @@ export type SyqlParseErrorCode =
181
184
  | 'SYQL2009_INVALID_INTEGER'
182
185
  | 'SYQL2010_INVALID_PAGE_RANGE'
183
186
  | 'SYQL2011_INVALID_PARAMETER'
184
- | 'SYQL2012_INVALID_QUERY_BODY';
187
+ | 'SYQL2012_INVALID_QUERY_BODY'
188
+ | 'SYQL2013_INVALID_ACKNOWLEDGEMENT';
185
189
 
186
190
  const CAMEL_IDENT_RE = /^[a-z][A-Za-z0-9]*$/;
187
191
  const INTEGER_LITERAL_RE = /^(?:0|[1-9][0-9]*)$/;
@@ -298,7 +302,9 @@ class Parser {
298
302
  }
299
303
 
300
304
  sawDeclaration = true;
301
- if (keyword.text === 'predicate') {
305
+ const acknowledgements = this.#parseAcknowledgements();
306
+ const next = this.#peek();
307
+ if (acknowledgements.length === 0 && next.text === 'predicate') {
302
308
  const declaration = this.#parsePredicate();
303
309
  this.#claimName(
304
310
  fileNames,
@@ -308,8 +314,11 @@ class Parser {
308
314
  );
309
315
  declarations.push(declaration);
310
316
  predicates.push(declaration);
311
- } else if (keyword.text === 'query' || keyword.text === 'sync') {
312
- const declaration = this.#parseQuery(keyword.text === 'sync');
317
+ } else if (next.text === 'query' || next.text === 'sync') {
318
+ const declaration = this.#parseQuery(
319
+ next.text === 'sync',
320
+ acknowledgements,
321
+ );
313
322
  this.#claimName(
314
323
  fileNames,
315
324
  declaration.name,
@@ -320,9 +329,13 @@ class Parser {
320
329
  queries.push(declaration);
321
330
  } else {
322
331
  this.#fail(
323
- 'SYQL2008_INVALID_MEMBER',
324
- keyword,
325
- `expected import, predicate, query, or sync query; found ${JSON.stringify(keyword.text)}`,
332
+ acknowledgements.length > 0
333
+ ? 'SYQL2013_INVALID_ACKNOWLEDGEMENT'
334
+ : 'SYQL2008_INVALID_MEMBER',
335
+ next,
336
+ acknowledgements.length > 0
337
+ ? `a query plan acknowledgement precedes a query declaration; found ${JSON.stringify(next.text || 'end of file')}`
338
+ : `expected import, predicate, query, or sync query; found ${JSON.stringify(next.text)}`,
326
339
  );
327
340
  }
328
341
  }
@@ -469,7 +482,58 @@ class Parser {
469
482
  };
470
483
  }
471
484
 
472
- #parseQuery(sync: boolean): SyqlQueryDeclaration {
485
+ #parseAcknowledgements(): QueryAcknowledgement[] {
486
+ const out: QueryAcknowledgement[] = [];
487
+ while (this.#peek().text === 'allow') {
488
+ const start = this.#take();
489
+ const kind = this.#take();
490
+ const finding = QUERY_PLAN_FINDINGS.find((name) => name === kind.text);
491
+ if (finding === undefined) {
492
+ this.#fail(
493
+ 'SYQL2013_INVALID_ACKNOWLEDGEMENT',
494
+ kind,
495
+ `expected a query plan finding (${QUERY_PLAN_FINDINGS.join(', ')}), found ${JSON.stringify(kind.text || 'end of file')}`,
496
+ );
497
+ }
498
+ const table = finding === 'many_tables' ? undefined : this.#take();
499
+ if (table !== undefined && table.kind !== 'identifier') {
500
+ this.#fail(
501
+ 'SYQL2013_INVALID_ACKNOWLEDGEMENT',
502
+ table,
503
+ `expected the table the finding names, found ${JSON.stringify(table.text || 'end of file')}`,
504
+ );
505
+ }
506
+ const reason = this.#take();
507
+ const text = reason.text.slice(1, -1).split("''").join("'");
508
+ if (reason.kind !== 'string' || text.trim() === '') {
509
+ this.#fail(
510
+ 'SYQL2013_INVALID_ACKNOWLEDGEMENT',
511
+ reason,
512
+ 'a query plan acknowledgement ends with a non-blank reason string',
513
+ );
514
+ }
515
+ if (
516
+ out.some((ack) => ack.finding === finding && ack.table === table?.text)
517
+ ) {
518
+ this.#fail(
519
+ 'SYQL2013_INVALID_ACKNOWLEDGEMENT',
520
+ start,
521
+ 'the query already acknowledges this finding',
522
+ );
523
+ }
524
+ out.push({
525
+ finding,
526
+ ...(table === undefined ? {} : { table: table.text }),
527
+ reason: text,
528
+ });
529
+ }
530
+ return out;
531
+ }
532
+
533
+ #parseQuery(
534
+ sync: boolean,
535
+ acknowledgements: readonly QueryAcknowledgement[],
536
+ ): SyqlQueryDeclaration {
473
537
  const start = sync ? this.#expectText('sync') : this.#peek();
474
538
  this.#expectText('query');
475
539
  const name = this.#parseCamelName('query name');
@@ -551,6 +615,7 @@ class Parser {
551
615
  statement: body.statement,
552
616
  ...(body.sort === undefined ? {} : { sort: body.sort }),
553
617
  ...(body.limit === undefined ? {} : { limit: body.limit }),
618
+ acknowledgements,
554
619
  span: spanBetween(start, body.close),
555
620
  nameSpan: name.span,
556
621
  };
@@ -1,6 +1,7 @@
1
1
  /** Schema-aware revision-1 SYQL validation (§§5, 8–13). */
2
2
  import type { IrColumnType, IrDocument } from './ir';
3
3
  import {
4
+ AGGREGATE_FUNCTIONS,
4
5
  type AnalyzedQuery,
5
6
  analyzeStatement,
6
7
  inferParamTypeEvidence,
@@ -12,8 +13,11 @@ import {
12
13
  type CteScope,
13
14
  PLAIN_REF_RE,
14
15
  type SelectItem,
16
+ type ScopeColumn,
15
17
  type SqlRelations,
16
18
  scanRelations,
19
+ scopeProofs,
20
+ sqlIdentifier,
17
21
  splitSelectList,
18
22
  scanTableRefs,
19
23
  stripCommentsAndStrings,
@@ -112,7 +116,6 @@ interface SqliteDiagnostic {
112
116
  readonly message: string;
113
117
  }
114
118
 
115
- const IDENT_SOURCE = '[A-Za-z_][A-Za-z0-9_]*';
116
119
  const IDENT_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
117
120
  const SQLITE_SUBJECT_RE = '[A-Za-z_][A-Za-z0-9_$]*';
118
121
  const NONDETERMINISTIC_FUNCTIONS = new Set([
@@ -186,24 +189,6 @@ function suggestion(authored: string, candidates: readonly string[]): string {
186
189
  return nearest === undefined ? '' : `; did you mean \`${nearest}\`?`;
187
190
  }
188
191
 
189
- function identifierText(token: SyqlToken | undefined): string | undefined {
190
- if (token?.kind === 'identifier') return token.text;
191
- if (token?.kind !== 'quoted-identifier') return undefined;
192
- if (token.text.startsWith('[') && token.text.endsWith(']')) {
193
- return token.text.slice(1, -1).replaceAll(']]', ']');
194
- }
195
- const quote = token.text[0];
196
- return token.text.slice(1, -1).replaceAll(`${quote}${quote}`, quote ?? '');
197
- }
198
- const AGGREGATE_FUNCTIONS = new Set([
199
- 'avg',
200
- 'count',
201
- 'group_concat',
202
- 'max',
203
- 'min',
204
- 'sum',
205
- 'total',
206
- ]);
207
192
  /**
208
193
  * The function surface of the standard SQLite 3.46.0 amalgamation.
209
194
  *
@@ -1158,7 +1143,7 @@ class Validator {
1158
1143
  const tokens = significant(logical.declaration.statement.tokens);
1159
1144
  const [qualifier, column] = subject.split('.', 2);
1160
1145
  const matches = (token: SyqlToken | undefined, expected: string): boolean =>
1161
- identifierText(token)?.toLowerCase() === expected.toLowerCase();
1146
+ sqlIdentifier(token)?.toLowerCase() === expected.toLowerCase();
1162
1147
 
1163
1148
  if (focus === 'table') {
1164
1149
  for (let index = 0; index < tokens.length - 1; index += 1) {
@@ -1295,154 +1280,11 @@ class Validator {
1295
1280
  readonly ref: TableRef;
1296
1281
  readonly scopes: readonly InferredScope[];
1297
1282
  };
1298
- const cleaned = stripCommentsAndStrings(markerSql);
1299
- const structure = this.#inspect(
1300
- cleaned,
1301
- logical.declaration.statement.span.file,
1302
- );
1303
- const markerScopes = scanRelations(markerSql, this.#ir);
1304
- const escapeRegex = (value: string): string =>
1305
- value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1306
- const requiredAt = (index: number): boolean => {
1307
- const scope = markerScopes.scopeAt(index);
1308
- if (scope === 'nested') return false;
1309
- const cte = markerScopes.cteScopes.find(
1310
- (candidate) => `cte:${candidate.name}` === scope,
1311
- );
1312
- const scopeItems =
1313
- cte === undefined
1314
- ? structure.outer
1315
- : structure.tokens.filter(
1316
- (item) =>
1317
- item.depth === 1 &&
1318
- item.token.span.start.offset >= cte.start &&
1319
- item.token.span.end.offset <= cte.end,
1320
- );
1321
- let whereStart: number | undefined;
1322
- for (const item of scopeItems) {
1323
- if (item.token.span.start.offset >= index) break;
1324
- const lower = tokenLower(item.token);
1325
- if (lower === 'where') whereStart = item.token.span.end.offset;
1326
- else if (OUTER_CLAUSE_ENDERS.has(lower ?? '')) whereStart = undefined;
1327
- }
1328
- if (whereStart === undefined) return false;
1329
-
1330
- const clauseEnd =
1331
- scopeItems.find(
1332
- (item) =>
1333
- item.token.span.start.offset > index &&
1334
- OUTER_CLAUSE_ENDERS.has(tokenLower(item.token) ?? ''),
1335
- )?.token.span.start.offset ??
1336
- cte?.end ??
1337
- cleaned.length;
1338
- const matchStack: number[] = [];
1339
- for (let cursor = whereStart; cursor < index; cursor += 1) {
1340
- if (cleaned[cursor] === '(') matchStack.push(cursor);
1341
- else if (cleaned[cursor] === ')') matchStack.pop();
1342
- }
1343
-
1344
- // Parenthesized boolean expressions are valid, but predicates inside a
1345
- // CTE or subquery are not outer proof obligations for sync coverage.
1346
- if (
1347
- matchStack.some((open) =>
1348
- /\b(?:SELECT|WITH)\b/i.test(cleaned.slice(open + 1, index)),
1349
- )
1350
- ) {
1351
- return false;
1352
- }
1353
-
1354
- // Negated equality does not constrain the result to that scope.
1355
- const prefix = cleaned.slice(whereStart, index);
1356
- const boundaries = [...prefix.matchAll(/\b(?:AND|OR)\b/gi)].map(
1357
- (match) => (match.index ?? -1) + match[0].length,
1358
- );
1359
- const lastBoundary = Math.max(prefix.lastIndexOf('('), ...boundaries);
1360
- if (/\bNOT\b/i.test(prefix.slice(lastBoundary + 1))) return false;
1361
- if (
1362
- matchStack.some((open) =>
1363
- /\bNOT\s*$/i.test(cleaned.slice(whereStart, open)),
1364
- )
1365
- ) {
1366
- return false;
1367
- }
1368
-
1369
- const stack: number[] = [];
1370
- for (let cursor = whereStart; cursor < clauseEnd; cursor += 1) {
1371
- const char = cleaned[cursor];
1372
- if (char === '(') stack.push(cursor);
1373
- else if (char === ')') stack.pop();
1374
- if (
1375
- (cursor === 0 || !/[A-Za-z0-9_:]/.test(cleaned[cursor - 1] ?? '')) &&
1376
- /^OR\b/i.test(cleaned.slice(cursor)) &&
1377
- stack.length <= matchStack.length &&
1378
- stack.every((open, stackIndex) => matchStack[stackIndex] === open)
1379
- ) {
1380
- return false;
1381
- }
1382
- }
1383
- return true;
1384
- };
1385
- type JoinEqualityContext =
1386
- | { readonly kind: 'inner' }
1387
- | {
1388
- readonly kind: 'outer';
1389
- /** Lowercased alias introduced by the JOIN that owns this ON. */
1390
- readonly introducedAlias: string;
1391
- /** The operand side this outer join null-extends. An outer join's
1392
- * ON constrains matched rows of the null-extended side only;
1393
- * preserved-side rows appear even when the ON predicate fails. */
1394
- readonly nullExtended: 'introduced' | 'prior';
1395
- };
1396
- const joinEqualityContextAt = (
1397
- index: number,
1398
- ): JoinEqualityContext | undefined => {
1399
- const prefix = cleaned.slice(0, index);
1400
- const clauses = [
1401
- ...prefix.matchAll(
1402
- /\b(ON|JOIN|WHERE|GROUP\s+BY|ORDER\s+BY|HAVING|LIMIT|UNION|EXCEPT|INTERSECT)\b/gi,
1403
- ),
1404
- ];
1405
- const last = clauses.at(-1);
1406
- if (last?.[1]?.toUpperCase() !== 'ON') return undefined;
1407
- const start = (last.index ?? 0) + last[0].length;
1408
- const next =
1409
- /\b(?:JOIN|WHERE|GROUP\s+BY|ORDER\s+BY|HAVING|LIMIT|UNION|EXCEPT|INTERSECT)\b/i.exec(
1410
- cleaned.slice(index),
1411
- );
1412
- const end = next === null ? cleaned.length : index + next.index;
1413
- const clause = cleaned.slice(start, end);
1414
- if (/\b(?:OR|NOT|SELECT|WITH)\b/i.test(clause)) return undefined;
1415
- const join = clauses.at(-2);
1416
- if (join?.[1]?.toUpperCase() !== 'JOIN') return undefined;
1417
- const joinIndex = join.index ?? 0;
1418
- const outerKind = /\b(LEFT|RIGHT|FULL)(?:\s+OUTER)?\s*$/i
1419
- .exec(prefix.slice(0, joinIndex))?.[1]
1420
- ?.toUpperCase();
1421
- if (outerKind === undefined) return { kind: 'inner' };
1422
- // A FULL join preserves both operands, so its ON constrains neither.
1423
- if (outerKind === 'FULL') return undefined;
1424
- const introducedAlias = (
1425
- prefix
1426
- .slice(joinIndex + join[0].length, last.index)
1427
- .match(new RegExp(IDENT_SOURCE, 'g')) ?? []
1428
- )
1429
- .filter((word) => word.toUpperCase() !== 'AS')
1430
- .at(-1)
1431
- ?.toLowerCase();
1432
- if (introducedAlias === undefined) return undefined;
1433
- return {
1434
- kind: 'outer',
1435
- introducedAlias,
1436
- nullExtended: outerKind === 'LEFT' ? 'introduced' : 'prior',
1437
- };
1438
- };
1439
- const refByAlias = new Map(
1440
- refs.map(
1441
- (ref) => [`${refScope(ref)}\0${ref.alias.toLowerCase()}`, ref] as const,
1442
- ),
1443
- );
1283
+ const proofs = scopeProofs(markerSql, this.#ir);
1444
1284
  const scopeNode = (scope: string, alias: string, column: string): string =>
1445
1285
  `${scope}\0${alias.toLowerCase()}\0${column.toLowerCase()}`;
1286
+ const nodeOf = (column: ScopeColumn): string =>
1287
+ scopeNode(column.scope, column.alias, column.column);
1446
1288
  const parents = new Map<string, string>();
1447
1289
  const find = (node: string): string => {
1448
1290
  const parent = parents.get(node);
@@ -1496,67 +1338,11 @@ class Validator {
1496
1338
  }
1497
1339
  return seen;
1498
1340
  };
1499
- const scopeColumn = (
1500
- scope: string,
1501
- alias: string,
1502
- column: string,
1503
- ): boolean => {
1504
- const ref = refByAlias.get(`${scope}\0${alias.toLowerCase()}`);
1505
- const table = this.#ir.tables.find((item) => item.name === ref?.table);
1506
- return (
1507
- table?.scopes.some(
1508
- (scope) => scope.column.toLowerCase() === column.toLowerCase(),
1509
- ) === true
1510
- );
1511
- };
1512
- // Scope inheritance across a join relies on a satisfied predicate proving
1513
- // both operands non-NULL. `IS` is null-safe (`NULL IS NULL` holds), so it
1514
- // would let a null-extended row on an outer join's preserved side carry a
1515
- // scope proof it never satisfies; only `=`/`==` qualify here.
1516
- const qualifiedEquality = new RegExp(
1517
- `(${IDENT_SOURCE})\\.(${IDENT_SOURCE})\\s*(?:=|==)\\s*(${IDENT_SOURCE})\\.(${IDENT_SOURCE})`,
1518
- 'gi',
1519
- );
1520
- for (const match of cleaned.matchAll(qualifiedEquality)) {
1521
- const leftAlias = match[1] as string;
1522
- const leftColumn = match[2] as string;
1523
- const rightAlias = match[3] as string;
1524
- const rightColumn = match[4] as string;
1525
- const index = match.index ?? 0;
1526
- const scope = markerScopes.scopeAt(index);
1527
- if (
1528
- !scopeColumn(scope, leftAlias, leftColumn) ||
1529
- !scopeColumn(scope, rightAlias, rightColumn)
1530
- ) {
1531
- continue;
1532
- }
1533
- const leftNode = scopeNode(scope, leftAlias, leftColumn);
1534
- const rightNode = scopeNode(scope, rightAlias, rightColumn);
1535
- if (requiredAt(index)) {
1536
- union(leftNode, rightNode);
1537
- continue;
1538
- }
1539
- const context = joinEqualityContextAt(index);
1540
- if (context === undefined) continue;
1541
- if (context.kind === 'inner') {
1542
- // An inner join's ON filters every result row, so the equality
1543
- // constrains both operands symmetrically.
1544
- union(leftNode, rightNode);
1545
- continue;
1546
- }
1547
- const leftIntroduced =
1548
- leftAlias.toLowerCase() === context.introducedAlias;
1549
- const rightIntroduced =
1550
- rightAlias.toLowerCase() === context.introducedAlias;
1551
- // An equality with both operands on one side of the outer join proves
1552
- // nothing across it; preserved-side rows appear regardless of the ON.
1553
- if (leftIntroduced === rightIntroduced) continue;
1554
- const introducedNode = leftIntroduced ? leftNode : rightNode;
1555
- const priorNode = leftIntroduced ? rightNode : leftNode;
1556
- if (context.nullExtended === 'introduced') {
1557
- addInheritEdge(introducedNode, priorNode);
1341
+ for (const equality of proofs.equalities) {
1342
+ if (equality.symmetric) {
1343
+ union(nodeOf(equality.target), nodeOf(equality.source));
1558
1344
  } else {
1559
- addInheritEdge(priorNode, introducedNode);
1345
+ addInheritEdge(nodeOf(equality.target), nodeOf(equality.source));
1560
1346
  }
1561
1347
  }
1562
1348
  const required = new Set(
@@ -1575,60 +1361,16 @@ class Validator {
1575
1361
  if (table === undefined) continue;
1576
1362
  const inferred: InferredScope[] = [];
1577
1363
  for (const scope of table.scopes) {
1578
- const alias = escapeRegex(ref.alias);
1579
- const column = escapeRegex(scope.column);
1580
- const unqualifiedMatches = refs.filter((candidate) => {
1581
- if (refScope(candidate) !== refScope(ref)) return false;
1582
- const other = this.#ir.tables.find(
1583
- (item) => item.name === candidate.table,
1584
- );
1585
- return other?.columns.some((item) => item.name === scope.column);
1586
- }).length;
1587
- const subject =
1588
- unqualifiedMatches === 1
1589
- ? `(?<![A-Za-z0-9_.:])(?:${alias}\\.)?${column}\\b`
1590
- : `(?<![A-Za-z0-9_.:])${alias}\\.${column}\\b`;
1591
- const found: Array<{ name: string; operator: 'equal' | 'in' }> = [];
1592
- const equality = new RegExp(
1593
- `${subject}\\s*(?:=|==|\\bIS\\b)\\s*:([A-Za-z_][A-Za-z0-9_]*)\\b|:([A-Za-z_][A-Za-z0-9_]*)\\b\\s*(?:=|==)\\s*${subject}`,
1594
- 'gi',
1595
- );
1596
- const inScope = (index: number): boolean =>
1597
- markerScopes.scopeAt(index) === refScope(ref) && requiredAt(index);
1598
- for (const match of cleaned.matchAll(equality)) {
1599
- if (!inScope(match.index)) continue;
1600
- const name = match[1] ?? match[2];
1601
- if (name !== undefined && required.has(name)) {
1602
- found.push({ name, operator: 'equal' });
1603
- }
1604
- }
1605
- const inList = new RegExp(`${subject}\\s+IN\\s*\\(([^)]*)\\)`, 'gi');
1606
- for (const match of cleaned.matchAll(inList)) {
1607
- if (!inScope(match.index)) continue;
1608
- const body = match[1] ?? '';
1609
- const bodyStart = match.index + match[0].length - body.length - 1;
1610
- const bodyTokens = significant(
1611
- lexSyqlSqlSource(
1612
- logical.declaration.statement.span.file,
1613
- markerSql.slice(bodyStart, bodyStart + body.length),
1614
- ),
1615
- );
1616
- const validBindList = bodyTokens.every((token, index) =>
1617
- index % 2 === 0
1618
- ? token.kind === 'bind'
1619
- : token.kind === 'punctuation' && token.text === ',',
1364
+ const node = scopeNode(refScope(ref), ref.alias, scope.column);
1365
+ const found = proofs.binds
1366
+ .filter(
1367
+ (proof) =>
1368
+ nodeOf(proof) === node &&
1369
+ proof.params.every((name) => required.has(name)),
1370
+ )
1371
+ .flatMap((proof) =>
1372
+ proof.params.map((name) => ({ name, operator: proof.operator })),
1620
1373
  );
1621
- const params = bodyTokens
1622
- .filter((_, index) => index % 2 === 0)
1623
- .map((token) => token.text.slice(1));
1624
- if (
1625
- validBindList &&
1626
- params.length > 0 &&
1627
- params.every((name) => required.has(name))
1628
- ) {
1629
- for (const name of params) found.push({ name, operator: 'in' });
1630
- }
1631
- }
1632
1374
  const params = [...new Set(found.map((item) => item.name))];
1633
1375
  if (params.length > 0) {
1634
1376
  const evidence: InferredScope = {