@askdb/core 1.0.0-beta.52 → 1.0.0-beta.53

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.
@@ -2,7 +2,7 @@ import { placeholderForTenantRoot, } from "../schema/v2/tenant-policy.js";
2
2
  import { SQLSERVER_DIALECT } from "./dialect-spec.js";
3
3
  import { decide, tenantFindings, throwIfDenied } from "./guardrail-decide.js";
4
4
  import { findPostgresOnlyForms } from "./lexer.js";
5
- import { mayBeSqlite, readSql, stringsSqliteReadsAsNames, tenantCodeReadings, tenantStatementReadings, unmatchedNonAsciiName, } from "./readings.js";
5
+ import { mayBeSqlite, readSql, QUERY_START_WORDS, stringsSqliteReadsAsNames, tenantCodeReadings, tenantStatementReadings, unmatchedNonAsciiName, } from "./readings.js";
6
6
  import { EXPRESSION_KEYWORDS, EXPRESSION_PUNCTUATION } from "./tsql-batch.js";
7
7
  import { denylistFor } from "./validate.js";
8
8
  /**
@@ -24,10 +24,15 @@ import { denylistFor } from "./validate.js";
24
24
  * needs the same predicate on its tenant ID column. Down a join path, the predicate
25
25
  * must be on a column that carries the IDs of the path's root or one of its ancestors
26
26
  * in the hierarchy. A polymorphic table also needs `typeColumn = '<key>'` ANDed into
27
- * the same query block, with a `mapping` key of the root whose placeholder filters
28
- * its id column. It does not tie a predicate
29
- * to one table reference, so an unfiltered reference beside a filtered one can
30
- * still pass; database-side row-level security is the sound boundary.
27
+ * the same `SELECT`, with a `mapping` key of the root whose placeholder filters
28
+ * its id column. Each read of a table needs its own predicate in its own `SELECT`
29
+ * (#399): a CTE body, a derived table and a subquery are each their own, and a
30
+ * qualified predicate counts only for the read its qualifier names. A read in a
31
+ * derived table or CTE also counts as filtered when every reference to that source
32
+ * is filtered on the column, which the source passes through unrenamed without
33
+ * computing across its rows. It doesn't
34
+ * check which columns a join or an `IN (…)` subquery equates (#400); database-side
35
+ * row-level security is the sound boundary.
31
36
  *
32
37
  * **This is not a security boundary.** Its purpose is to catch model mistakes
33
38
  * (a forgotten or widened tenant filter, an unclassified table) early and
@@ -217,7 +222,7 @@ function checkPolymorphicTable(sql, pt, policy, warnings) {
217
222
  return;
218
223
  }
219
224
  // The id predicate filters by one root's IDs, so the discriminator must pick that root's
220
- // rows: `owner_type = 'agency' AND owner_id = :tenant_agency_ids`, ANDed in the same block.
225
+ // rows: `owner_type = 'agency' AND owner_id = :tenant_agency_ids`, ANDed in the same `SELECT`.
221
226
  const paired = rootIds.map((rootId, i) => ({
222
227
  ...targets[i],
223
228
  discriminator: {
@@ -586,15 +591,30 @@ function mentionsTable(views, tableName) {
586
591
  * Whether `text[start, end)` in a code view is a word of `ORDER BY` or `GROUP BY`. `ORDER` and
587
592
  * `GROUP` are reserved on every engine, so a table named after one is written quoted (`"order"`,
588
593
  * whose closing quote mark breaks the phrase) or qualified (`billing.order`, which Postgres takes
589
- * unquoted), and the `BY` after them is the keyword.
594
+ * unquoted), and the `BY` after them is the keyword. A `.` before the word makes it a name even with
595
+ * whitespace or a comment (blanked in the view) between them, as every engine reads `billing . order`.
590
596
  */
591
597
  function inOrderOrGroupBy(text, start, end) {
592
- if (text[start - 1] === ".")
598
+ const afterDot = (at) => /\.\s*$/.test(text.slice(0, at));
599
+ if (afterDot(start))
593
600
  return false;
594
601
  const word = text.slice(start, end).toLowerCase();
595
602
  if (word === "order" || word === "group")
596
603
  return /^\s+by(?![\w$\u0080-\uffff])/i.test(text.slice(end));
597
- return word === "by" && /(?:^|[^\w$\u0080-\uffff.\u0001])(?:order|group)\s+$/i.test(text.slice(0, start));
604
+ if (word !== "by")
605
+ return false;
606
+ const before = /(?:order|group)\s+$/i.exec(text.slice(0, start));
607
+ if (before === null || /[\w$\u0080-\uffff\u0001]/.test(text[before.index - 1] ?? ""))
608
+ return false;
609
+ return !afterDot(before.index);
610
+ }
611
+ /**
612
+ * Whether a reading sees `tableName` as a name of its own, not part of a longer one (`orders$archive`):
613
+ * anywhere in code, or inside a quoted name, except as a word of `ORDER BY` or `GROUP BY`.
614
+ */
615
+ function mentionsAsName(view, tableName) {
616
+ const pattern = new RegExp(`(?<![\\w$\\u0080-\\uffff])${escapeRegex(tableName)}(?![\\w$\\u0080-\\uffff])`, "gi");
617
+ return [...view.text.matchAll(pattern)].some((m) => !inOrderOrGroupBy(view.text, m.index, m.index + m[0].length));
598
618
  }
599
619
  /** A tenant column counts only when every reading sees it in code. */
600
620
  function mentionsIdentifier(views, identifier) {
@@ -606,22 +626,45 @@ const TOKEN = /:[A-Za-z0-9_]+|::|\|\||<>|!=|<=|>=|[A-Za-z_\u0080-\uffff][\w$\u00
606
626
  const WORD_OR_NUMBER = /^[\w\u0080-\uffff]/;
607
627
  /**
608
628
  * Split a code view into tokens with their nesting depth. Strings and comments are already blanked.
609
- * A token is quoted when it lies inside a name the view reads as quoted, as the reading's lexer
610
- * spans it, so a {@link QUOTE_MARK} character written inside a quoted name changes nothing.
629
+ * A quoted identifier is one token, its whole name as the lexer reads it (escapes undone), so a
630
+ * space, dot or bracket inside it is never read as code, and a QUOTE_MARK character written inside
631
+ * it changes nothing. So is a string the reading takes as a name, as SQLite does (`FROM 'orders'`,
632
+ * `orders 'o'`): `'join'` there is a name, never the keyword.
611
633
  */
612
634
  function tokenize(view) {
613
635
  const tokens = [];
614
636
  const split = lexerWordEnds(view);
615
- const quoted = view.quoted;
637
+ const foldsCase = view.reading.profile.id === "postgres" || view.reading.profile.id === "generic";
638
+ const quotedAt = new Map();
639
+ for (const t of view.tokens) {
640
+ if (t.kind === "quoted_identifier")
641
+ quotedAt.set(t.start, { end: t.end, name: t.value });
642
+ else if (t.kind === "string" && view.quoted[t.start] === 1 && t.text.startsWith("'") && t.unterminated !== true) {
643
+ quotedAt.set(t.start, { end: t.end, name: t.text.slice(1, -1).replaceAll("''", "'") });
644
+ }
645
+ }
616
646
  let depth = 0;
647
+ let skipUntil = 0;
617
648
  for (const m of view.text.matchAll(TOKEN)) {
618
- if (m[0] === QUOTE_MARK)
649
+ if (m.index < skipUntil)
619
650
  continue;
651
+ if (m[0] === QUOTE_MARK) {
652
+ const q = quotedAt.get(m.index);
653
+ if (q === undefined)
654
+ continue;
655
+ const name = q.name;
656
+ // A name spelled like a mark (`"."`, `[,]`) keeps a prefix, so it never reads as that mark.
657
+ const text = /^[^\w\u0080-\uffff]{1,2}$/.test(name) ? QUOTE_MARK + name : name;
658
+ tokens.push({ text, lower: name.toLowerCase(), name, kw: "", depth, offset: m.index + 1, quoted: true });
659
+ skipUntil = q.end;
660
+ continue;
661
+ }
620
662
  for (const [t, offset] of WORD_OR_NUMBER.test(m[0]) ? split(m[0], m.index) : [[m[0], m.index]]) {
621
663
  if (t === ")")
622
664
  depth = Math.max(0, depth - 1);
623
665
  const lower = t.toLowerCase();
624
- tokens.push({ text: t, lower, kw: quoted[offset] === 1 ? "" : lower, depth, offset });
666
+ const name = foldsCase ? t.replace(/[A-Z]+/g, (s) => s.toLowerCase()) : t;
667
+ tokens.push({ text: t, lower, name, kw: lower, depth, offset });
625
668
  if (t === "(")
626
669
  depth++;
627
670
  }
@@ -692,7 +735,7 @@ const CLAUSE_END = new Set([
692
735
  ]);
693
736
  /** Words that put a predicate somewhere other than a filter conjunct (select list, `CASE`, assignment, …). */
694
737
  const NOT_A_FILTER = new Set(["select", "from", "by", "when", "then", "else", "case", "end", "as", "set", "values", "distinct", "is"]);
695
- /** Keywords that combine query blocks; each block is checked on its own. */
738
+ /** Keywords that combine `SELECT`s; each branch is checked on its own. */
696
739
  const SET_OPERATORS = new Set(["union", "intersect", "except", "minus"]);
697
740
  /** Words other than identifiers that the predicate forms use. */
698
741
  const PREDICATE_WORDS = new Set(["in", "any", "and", "or", "not", "xor"]);
@@ -700,53 +743,320 @@ const PREDICATE_WORDS = new Set(["in", "any", "and", "or", "not", "xor"]);
700
743
  * Whether the unquoted word at `i` is part of `ORDER BY` or `GROUP BY`. `ORDER` and `GROUP` are
701
744
  * reserved on every engine, so a table named after one is written quoted. Any other keyword may
702
745
  * name a table, unquoted, on some engine (`user` on MySQL, `lock` on Postgres), so its spelling
703
- * always counts as the table.
746
+ * always counts as the table. After a `.` (`billing.order by`), `order` is a table's name and
747
+ * `by` its alias.
704
748
  */
705
749
  function isOrderOrGroupBy(tokens, i) {
706
750
  const kw = tokens[i].kw;
707
751
  if (kw === "order" || kw === "group")
708
- return tokens[i + 1]?.kw === "by";
709
- return kw === "by" && (tokens[i - 1]?.kw === "order" || tokens[i - 1]?.kw === "group");
752
+ return tokens[i + 1]?.kw === "by" && tokens[i - 1]?.text !== ".";
753
+ return kw === "by" && (tokens[i - 1]?.kw === "order" || tokens[i - 1]?.kw === "group") && tokens[i - 2]?.text !== ".";
754
+ }
755
+ /** Words that start a query right after its `(`. */
756
+ const QUERY_START = QUERY_START_WORDS;
757
+ /** Words that end a row source with no alias under every reading: no engine takes one as an alias. */
758
+ const ENDS_A_SOURCE = new Set([
759
+ "where", "group", "order", "having", "union", "intersect", "except", "join", "inner", "left", "right", "cross", "outer", "on",
760
+ ]);
761
+ /** The words of a join, which put a second source in a `FROM`. */
762
+ const JOIN_WORDS = new Set(["join", "inner", "left", "right", "full", "cross", "outer", "natural", "straight_join", "apply", "using"]);
763
+ const analyses = new WeakMap();
764
+ /** The view's tokens and `SELECT`s, read once per view. */
765
+ function analyze(view) {
766
+ let analysis = analyses.get(view);
767
+ if (analysis === undefined) {
768
+ const tokens = tokenize(view);
769
+ analysis = { view, tokens, scopes: readScopes(tokens) };
770
+ analyses.set(view, analysis);
771
+ }
772
+ return analysis;
773
+ }
774
+ /** Whether the `(` at `open` opens a query: `SELECT`, `WITH`, `VALUES` or `TABLE` right after it, past any further `(`. */
775
+ function opensQuery(tokens, open) {
776
+ let j = open + 1;
777
+ while (tokens[j]?.text === "(")
778
+ j++;
779
+ return QUERY_START.has(tokens[j]?.kw ?? "");
780
+ }
781
+ function readScopes(tokens) {
782
+ const of = [];
783
+ const close = new Map();
784
+ const branched = new Set();
785
+ const frames = [{ open: -1, branch: 0, depth: 0 }];
786
+ tokens.forEach((t, i) => {
787
+ let top = frames[frames.length - 1];
788
+ if (t.text === ")" && top.open >= 0 && t.depth === tokens[top.open].depth) {
789
+ close.set(top.open, i);
790
+ frames.pop();
791
+ top = frames[frames.length - 1];
792
+ }
793
+ of.push(scopeKey(top.open, top.branch));
794
+ if (SET_OPERATORS.has(t.kw) && t.depth === top.depth) {
795
+ top.branch++;
796
+ branched.add(top.open);
797
+ }
798
+ if (t.text === "(" && opensQuery(tokens, i))
799
+ frames.push({ open: i, branch: 0, depth: t.depth + 1 });
800
+ });
801
+ return { of, close, branched };
802
+ }
803
+ /** A `SELECT`'s key: the `(` that opens its query (-1 for the statement) and its set-operation branch. */
804
+ function scopeKey(open, branch) {
805
+ return `${open}:${branch}`;
806
+ }
807
+ /** The `(` that opens a `SELECT`'s query, or -1 for the statement's (see {@link scopeKey}). */
808
+ function scopeOpen(scope) {
809
+ return Number.parseInt(scope, 10);
710
810
  }
711
811
  /**
712
- * Whether the statement has a tenant predicate for `table` on every reading: a column
713
- * and placeholder from `targets`, compared as a conjunct of a filter clause. Every query
714
- * block that mentions the table needs its own: the statement, and each branch of every
715
- * `UNION` / `INTERSECT` / `EXCEPT`, at the top level or inside a derived table or CTE.
716
- * See {@link validateTenantGuardrails} for the rule.
812
+ * A token's name for matching a table or CTE: lower-cased, and for a quoted name without its
813
+ * trailing whitespace, which SQL Server ignores (`[orders ]` reads `orders`). Matching more
814
+ * spellings finds more reads, so the check only gets stricter on engines that keep it.
815
+ */
816
+ function tableNameOf(t) {
817
+ return t.quoted === true ? t.lower.trimEnd() : t.lower;
818
+ }
819
+ /** Whether `token` may be a name: a quoted name, whatever it holds, or a word that starts with a letter or `_`. */
820
+ function isWord(token) {
821
+ return token !== undefined && (token.quoted === true || /^[A-Za-z_\u0080-\uffff]/.test(token.text));
822
+ }
823
+ /**
824
+ * Whether the token at `k`, right after a row source, ends that source with no alias in `view`'s
825
+ * reading: the end of the statement, `,`, `)`, `;`, a word in {@link ENDS_A_SOURCE}, a join word
826
+ * the reading's engine reserves (`FULL` but on MySQL, `NATURAL` and `USING` but on SQL Server,
827
+ * MySQL's `STRAIGHT_JOIN`), a SQL Server `WITH (` hint, or a MySQL `FORCE`/`USE`/`IGNORE INDEX`.
828
+ * Any other word may be an alias on some engine (`force`, `lock`, `qualify` are on Postgres).
829
+ */
830
+ function endsSource(a, k) {
831
+ const t = a.tokens[k];
832
+ if (t === undefined || t.text === "," || t.text === ")" || t.text === ";")
833
+ return true;
834
+ const engine = a.view.reading.profile.id;
835
+ const next = a.tokens[k + 1];
836
+ switch (t.kw) {
837
+ case "full": return engine !== "mysql";
838
+ case "natural":
839
+ case "using": return engine !== "sqlserver";
840
+ case "straight_join": return engine === "mysql";
841
+ case "with": return next?.text === "(";
842
+ case "force":
843
+ case "use":
844
+ case "ignore": return next?.kw === "index" || next?.kw === "key";
845
+ default: return ENDS_A_SOURCE.has(t.kw);
846
+ }
847
+ }
848
+ /**
849
+ * The alias of the row source ending at `i`, in the only forms the check reads one: `orders o`,
850
+ * `orders AS o`, `) t`, where the alias is followed by what ends the source ({@link endsSource})
851
+ * or by its column list. Anything else (`orders TABLESAMPLE …`, `orders NOT INDEXED`) is no alias
852
+ * the check recognises, so it returns undefined and the caller fails closed.
853
+ */
854
+ function aliasAt(a, i) {
855
+ const as = a.tokens[i + 1]?.kw === "as";
856
+ if (!as && endsSource(a, i + 1))
857
+ return undefined;
858
+ const alias = as ? i + 2 : i + 1;
859
+ if (!isWord(a.tokens[alias]))
860
+ return undefined;
861
+ return endsSource(a, alias + 1) || a.tokens[alias + 1]?.text === "(" ? alias : undefined;
862
+ }
863
+ /**
864
+ * The names the table reference at `i` goes by: its alias, or its own name when what follows ends
865
+ * it with no alias. Anything else after it is a form the check doesn't read, so the read gets no
866
+ * name and only an unqualified predicate filters it.
867
+ */
868
+ function readNames(a, i) {
869
+ const end = sourceEnd(a, i);
870
+ if (hiddenByGroupAlias(a, end))
871
+ return new Set();
872
+ const alias = aliasAt(a, end);
873
+ if (alias !== undefined)
874
+ return a.tokens[alias + 1]?.text === "(" ? new Set() : new Set([a.tokens[alias].name]);
875
+ return endsSource(a, end + 1) ? new Set([a.tokens[i].name]) : new Set();
876
+ }
877
+ /**
878
+ * Whether the row source ending at `end` sits inside parentheses, in a source position, that
879
+ * something other than the source's end follows: an alias (`("order" x) AS y`,
880
+ * `(orders x CROSS JOIN s) AS j`) hides the names inside, so a predicate on `x` names another
881
+ * read. Any other word there is a form the check doesn't read, so it fails closed the same way.
882
+ */
883
+ function hiddenByGroupAlias(a, end) {
884
+ const { tokens } = a;
885
+ for (let open = enclosing(tokens, end, "("); open >= 0; open = enclosing(tokens, open, "(")) {
886
+ if (opensQuery(tokens, open) || !inSourcePosition(tokens, open))
887
+ return false;
888
+ const close = enclosing(tokens, open + 1, ")");
889
+ if (close < 0 || !endsSource(a, close + 1))
890
+ return true;
891
+ }
892
+ return false;
893
+ }
894
+ /**
895
+ * Where the table reference at `i` ends: past the `)` of any parentheses that hold it alone
896
+ * (`ONLY (orders) a`, SQLite's `(orders) a`), since its alias follows them.
897
+ */
898
+ function sourceEnd(a, i) {
899
+ let start = qualifiedStart(a.tokens, i);
900
+ let end = i;
901
+ // Only parentheses in a source position (after `FROM`, `JOIN`, `ONLY`, `,` or another `(`), not a call's.
902
+ const opensSource = (open) => {
903
+ const before = a.tokens[open - 1];
904
+ return before !== undefined && ((before.kw === "from" && opensFromClause(a.tokens, open - 1)) || ["join", "only", "lateral", "straight_join"].includes(before.kw) || before.text === "," || before.text === "(");
905
+ };
906
+ while (a.tokens[start - 1]?.text === "(" && a.tokens[end + 1]?.text === ")" && opensSource(start - 1)) {
907
+ start--;
908
+ end++;
909
+ }
910
+ return end;
911
+ }
912
+ /**
913
+ * Whether the `(` at `open` sits where a row source starts: right after `FROM`, a join word,
914
+ * `LATERAL`, `APPLY`, a comma of a `FROM` list, or another `(` that does.
915
+ */
916
+ function inSourcePosition(tokens, open) {
917
+ const before = tokens[open - 1];
918
+ if (before === undefined)
919
+ return false;
920
+ if (before.kw === "from")
921
+ return opensFromClause(tokens, open - 1);
922
+ if (["join", "straight_join", "lateral", "apply"].includes(before.kw))
923
+ return true;
924
+ if (before.text === "(")
925
+ return !opensQuery(tokens, open - 1) && inSourcePosition(tokens, open - 1);
926
+ if (before.text !== ",")
927
+ return false;
928
+ for (let k = open - 2; k >= 0; k--) {
929
+ const t = tokens[k];
930
+ if (t.depth < before.depth)
931
+ return false;
932
+ if (t.depth > before.depth)
933
+ continue;
934
+ if (t.kw === "from")
935
+ return opensFromClause(tokens, k);
936
+ if (t.kw === "select" || t.kw === "where" || t.kw === "having" || (CLAUSE_END.has(t.kw) && !JOIN_WORDS.has(t.kw)))
937
+ return false;
938
+ }
939
+ return false;
940
+ }
941
+ /**
942
+ * Whether a table read can start at `start`: right after a `SELECT`'s own `FROM`, a join word,
943
+ * `ONLY`, `LATERAL`, a comma of a `FROM` list, or a `(` in one of those places that opens no query.
944
+ * A whole-row reference in an expression (`json_agg(orders)`, `(orders).id`) starts none.
945
+ */
946
+ function startsSource(tokens, start) {
947
+ const before = tokens[start - 1];
948
+ if (before === undefined)
949
+ return false;
950
+ if (before.kw === "from")
951
+ return opensFromClause(tokens, start - 1);
952
+ if (["join", "straight_join", "only", "lateral"].includes(before.kw))
953
+ return true;
954
+ if (before.text === "(")
955
+ return !opensQuery(tokens, start - 1) && startsSource(tokens, start - 1);
956
+ return before.text === "," && inSourcePosition(tokens, start);
957
+ }
958
+ /**
959
+ * Whether the `FROM` at `k` opens its `SELECT`'s own `FROM` clause: not `IS [NOT] DISTINCT FROM`,
960
+ * and not a `FROM` inside a call's parentheses (`EXTRACT(YEAR FROM d)`, `SUBSTRING(s FROM 2)`).
961
+ */
962
+ function opensFromClause(tokens, k) {
963
+ if (tokens[k]?.kw !== "from" || tokens[k - 1]?.kw === "distinct")
964
+ return false;
965
+ const open = enclosing(tokens, k, "(");
966
+ return open < 0 || opensQuery(tokens, open);
967
+ }
968
+ /** Whether the alias of the row source ending at `i` has a column list (`x t (a, b)`), which renames its output. */
969
+ function hasColumnList(a, i) {
970
+ const alias = aliasAt(a, i);
971
+ return alias !== undefined && a.tokens[alias + 1]?.text === "(";
972
+ }
973
+ /** Whether the `(` at `open` follows `AS` (or `AS [NOT] MATERIALIZED`), as a CTE body does. */
974
+ function opensAfterAs(tokens, open) {
975
+ let k = open - 1;
976
+ if (tokens[k]?.kw === "materialized")
977
+ k -= tokens[k - 1]?.kw === "not" ? 2 : 1;
978
+ return tokens[k]?.kw === "as";
979
+ }
980
+ /**
981
+ * The CTE named before the `(` at `open` (`WITH x AS (`, `, x AS MATERIALIZED (`): its name and
982
+ * token. Undefined when it isn't one, or when a column list (`x (a, b) AS (`) renames its output.
983
+ */
984
+ function cteAt(tokens, open) {
985
+ let k = open - 1;
986
+ if (tokens[k]?.kw === "materialized")
987
+ k -= tokens[k - 1]?.kw === "not" ? 2 : 1;
988
+ if (tokens[k]?.kw !== "as" || !isWord(tokens[k - 1]))
989
+ return undefined;
990
+ const before = tokens[k - 2];
991
+ if (!(before?.kw === "with" || before?.kw === "recursive" || before?.text === ","))
992
+ return undefined;
993
+ return { name: tableNameOf(tokens[k - 1]), at: k - 1 };
994
+ }
995
+ /**
996
+ * The reads of `name`: every spelling of it, quoted or not, except a word of `ORDER BY` or
997
+ * `GROUP BY` and a qualifier (`orders.id`), which refers to a read rather than being one. A read
998
+ * goes by its names ({@link readNames}): an alias only, once it has one, since the table's name
999
+ * then refers to another read, or to none.
1000
+ */
1001
+ function readsOf(a, name) {
1002
+ const reads = [];
1003
+ a.tokens.forEach((t, i) => {
1004
+ if (tableNameOf(t) !== name || isOrderOrGroupBy(a.tokens, i) || a.tokens[i + 1]?.text === ".")
1005
+ return;
1006
+ reads.push({ at: i, scope: a.scopes.of[i], names: readNames(a, i) });
1007
+ });
1008
+ return reads;
1009
+ }
1010
+ /** The qualifier of the column at `column` (`o` in `o.agency_id`), if any, as the reading compares names. */
1011
+ function qualifierOf(tokens, column) {
1012
+ return tokens[column - 1]?.text === "." ? tokens[column - 2]?.name : undefined;
1013
+ }
1014
+ /** Every tenant predicate in the view for one of `targets`, with the `SELECT`s it filters. */
1015
+ function occurrences(a, targets) {
1016
+ const found = [];
1017
+ a.tokens.forEach((t, k) => {
1018
+ for (const target of targets) {
1019
+ // A quoted name spelled like the placeholder is a name, which nothing binds.
1020
+ if (t.text !== target.placeholder || t.quoted === true)
1021
+ continue;
1022
+ const predicate = predicateAt(a.tokens, k, target.column, a.view.reading.profile.id);
1023
+ if (!predicate)
1024
+ continue;
1025
+ const filters = filteredScopes(a, predicate.start, predicate.end, qualifierOf(a.tokens, predicate.column), predicate.column);
1026
+ if (filters.length > 0)
1027
+ found.push({ target, filters });
1028
+ }
1029
+ });
1030
+ return found;
1031
+ }
1032
+ /**
1033
+ * Whether every read of `table` is filtered, on every reading, by a predicate from `targets`
1034
+ * (#399): a column and placeholder compared as a conjunct of a filter clause in the read's own
1035
+ * `SELECT`, and tied to that read by its qualifier. Each branch of a `UNION` / `INTERSECT` /
1036
+ * `EXCEPT` is its own `SELECT`, and so is every CTE body, derived table and subquery. A read in a
1037
+ * derived table or CTE is also filtered from outside, when that source passes the column through
1038
+ * unrenamed and every reference to it is filtered on it. See {@link validateTenantGuardrails}.
717
1039
  */
718
1040
  function hasTenantPredicate(views, table, targets) {
719
1041
  const name = table.toLowerCase();
720
1042
  return views.every((view) => {
721
- const tokens = tokenize(view);
722
- // Read the Postgres way, a column list after the table's alias renames its columns, and a quoted
723
- // name is compared exactly.
724
- const postgres = view.reading.profile.id === "postgres";
725
- if (postgres && renamesColumns(tokens, name))
726
- return false;
727
- // Every spelling of the name counts as the table, quoted or not, except the words of `ORDER BY`.
728
- const mentions = (from, to) => {
729
- for (let i = from; i < to; i++)
730
- if (tokens[i].lower === name && !isOrderOrGroupBy(tokens, i))
731
- return true;
732
- return false;
733
- };
734
- return queryBlocks(tokens)
735
- .filter(([from, to]) => mentions(from, to))
736
- .every(([from, to]) => {
737
- for (let k = from; k < to; k++) {
738
- for (const target of targets) {
739
- // A quoted name spelled like the placeholder is a name, which nothing binds.
740
- if (tokens[k].text !== target.placeholder || tokens[k].kw === "")
741
- continue;
742
- const span = predicateSpan(tokens, k, target, postgres);
743
- if (!span || !isFilterConjunct(tokens, span[0], span[1], view.reading))
744
- continue;
745
- if (!target.discriminator || hasDiscriminator(tokens, from, to, view, target))
746
- return true;
747
- }
748
- }
1043
+ const a = analyze(view);
1044
+ // Read the Postgres way, a column list after a read's alias, or after a parenthesized source it
1045
+ // sits in, renames its columns, so the read can't be checked.
1046
+ if (view.reading.profile.id === "postgres" && renamesColumns(a.tokens, name))
749
1047
  return false;
1048
+ const reads = readsOf(a, name);
1049
+ // A reading that sees the table's name but no read of it (the name only inside a longer quoted
1050
+ // name, or only as a qualifier) can't tell what filters it, so it fails closed.
1051
+ if (reads.length === 0)
1052
+ return !mentionsAsName(view, name);
1053
+ const found = occurrences(a, targets);
1054
+ const tableReads = reads.filter((r) => startsSource(a.tokens, qualifiedStart(a.tokens, r.at)));
1055
+ return reads.every((read) => {
1056
+ const sameSelect = tableReads.filter((r) => r.scope === read.scope);
1057
+ return tableReads.includes(read) && sameSelect.length > 1
1058
+ ? selfJoinReadFiltered(a, found, targets, read, name, sameSelect)
1059
+ : readFiltered(a, found, targets, read, name);
750
1060
  });
751
1061
  });
752
1062
  }
@@ -802,22 +1112,267 @@ function aliasWithColumnList(tokens, j) {
802
1112
  return as || alias.kw === "" || !NOT_A_TABLE_ALIAS.has(alias.kw);
803
1113
  }
804
1114
  /**
805
- * Whether the block `tokens[from, to)` ANDs in `col = '<value>'` for `target.discriminator`:
806
- * its column (optionally qualified with the target's table or alias), `=`, and a plain
807
- * `'…'` literal, read from `view.source` at the offsets where the view blanked it, whose
808
- * value is one of the discriminator's values and which nothing but whitespace follows up
809
- * to the next token (no `''`-escape, no adjacent literal, no comment).
1115
+ * A read of a table its `SELECT` reads more than once (a self-join): it counts as filtered only
1116
+ * when every read of the table there is canonical (`table alias` or `table AS alias`, outside
1117
+ * parentheses, with a plain unquoted alias no other read there shares, in any case) and a
1118
+ * predicate qualified with its own alias filters it. Anything less can't tell the reads apart, so
1119
+ * it fails closed.
1120
+ */
1121
+ function selfJoinReadFiltered(a, found, targets, read, table, sameSelect) {
1122
+ const aliases = sameSelect.map((r) => canonicalAlias(a, r));
1123
+ if (aliases.some((alias) => alias === undefined))
1124
+ return false;
1125
+ if (new Set(aliases.map((alias) => alias.toLowerCase())).size !== aliases.length)
1126
+ return false;
1127
+ const mine = canonicalAlias(a, read);
1128
+ return found.some((o) => o.filters.some((f) => f.scope === read.scope && f.qualifier === mine && targets.includes(o.target) &&
1129
+ (!o.target.discriminator || hasDiscriminator(a, read, o.target))));
1130
+ }
1131
+ /** The plain alias of a read written `table alias` or `table AS alias`, at its `SELECT`'s own level; else undefined. */
1132
+ function canonicalAlias(a, read) {
1133
+ const { tokens } = a;
1134
+ const open = scopeOpen(read.scope);
1135
+ const depth = open < 0 ? 0 : tokens[open].depth + 1;
1136
+ if (tokens[read.at].depth !== depth || sourceEnd(a, read.at) !== read.at)
1137
+ return undefined;
1138
+ const at = aliasAt(a, read.at);
1139
+ if (at === undefined || tokens[at].quoted === true || tokens[at + 1]?.text === "(")
1140
+ return undefined;
1141
+ return tokens[at].name;
1142
+ }
1143
+ function readFiltered(a, found, targets, read, table) {
1144
+ for (const o of found) {
1145
+ for (const f of o.filters) {
1146
+ if (f.scope !== read.scope || !filtersRead(a, f, o.target, read, table))
1147
+ continue;
1148
+ if (o.target.discriminator && !hasDiscriminator(a, read, o.target))
1149
+ continue;
1150
+ return true;
1151
+ }
1152
+ }
1153
+ return targets.some((target) => target.table.toLowerCase() === table && !target.discriminator && filteredFromOutside(a, found, read, target, 0));
1154
+ }
1155
+ /**
1156
+ * Whether a predicate that filters `read`'s `SELECT` through `filter` filters `read` itself.
1157
+ * Unqualified, it does: the read's table has the column, so the name resolves to it or the SQL is
1158
+ * ambiguous. On the read's own column, its qualifier must name that read ({@link readNames});
1159
+ * on another table's column (a root filtered through a column
1160
+ * that carries its IDs), a read of that table. A qualifier that names no read the check sees,
1161
+ * such as a table function's alias, counts for nothing. Through `col IN (…)`, which read of
1162
+ * another table `col` filters is left to the join.
1163
+ */
1164
+ function filtersRead(a, filter, target, read, table) {
1165
+ const q = filter.qualifier;
1166
+ if (q === undefined)
1167
+ return filter.on === undefined || seenByOn(a.tokens, filter.on, read.at);
1168
+ const owner = target.table.toLowerCase();
1169
+ if (owner === table)
1170
+ return read.names.has(q);
1171
+ return !filter.ownSelect || readsOf(a, owner).some((r) => r.names.has(q));
1172
+ }
1173
+ /**
1174
+ * Whether `read`, inside a derived table or CTE, is filtered from outside on `target`'s column:
1175
+ * its `SELECT` is the source's whole body, the body passes the column through unrenamed, and
1176
+ * every reference to the source is filtered on that column in its own `SELECT`, directly or,
1177
+ * through a source around it, the same way. A set operation in the body, a column list on the
1178
+ * source, a recursive CTE or a CTE nothing reads fails closed.
1179
+ */
1180
+ function filteredFromOutside(a, found, read, target, depth) {
1181
+ // Each source's answer is worked out once per call: re-exploring it once per path is exponential
1182
+ // in a chain of sources that each read the one before several times.
1183
+ let memo = outsideMemo.get(found);
1184
+ if (memo === undefined)
1185
+ outsideMemo.set(found, (memo = new Map()));
1186
+ const key = `${read.scope}|${depth}|${[...read.names].join(" ")}|${target.table}|${target.column}|${target.placeholder}`;
1187
+ const known = memo.get(key);
1188
+ if (known !== undefined)
1189
+ return known;
1190
+ memo.set(key, false); // a cycle fails closed
1191
+ const open = scopeOpen(read.scope);
1192
+ let filtered = false;
1193
+ if (depth <= 8 && open >= 0 && !a.scopes.branched.has(open)) {
1194
+ const refs = sourceRefs(a, open);
1195
+ filtered = refs !== undefined && refs.length > 0 && passesThrough(a, open, read.names, target.column) &&
1196
+ refs.every((ref) => found.some((o) => o.target === target &&
1197
+ o.filters.some((f) => f.ownSelect && f.scope === ref.scope && (f.qualifier === undefined ? f.on === undefined || seenByOn(a.tokens, f.on, ref.at) : ref.names.has(f.qualifier)))) ||
1198
+ filteredFromOutside(a, found, ref, target, depth + 1));
1199
+ }
1200
+ memo.set(key, filtered);
1201
+ return filtered;
1202
+ }
1203
+ const outsideMemo = new WeakMap();
1204
+ const sourceRefsMemo = new WeakMap();
1205
+ /**
1206
+ * The references to the row source whose query opens at `open`, or undefined when it isn't a source
1207
+ * this check follows: a column list on the source or on a reference to it renames its output.
1208
+ */
1209
+ function sourceRefs(a, open) {
1210
+ let memo = sourceRefsMemo.get(a);
1211
+ if (memo === undefined)
1212
+ sourceRefsMemo.set(a, (memo = new Map()));
1213
+ if (!memo.has(open))
1214
+ memo.set(open, readSourceRefs(a, open));
1215
+ return memo.get(open);
1216
+ }
1217
+ function readSourceRefs(a, open) {
1218
+ const { tokens, scopes } = a;
1219
+ const close = scopes.close.get(open);
1220
+ if (close === undefined || subqueryUse(tokens, open) !== "table")
1221
+ return undefined;
1222
+ // A derived table only in a source position: a subquery after `ON` or in an expression is no row source.
1223
+ if (!opensAfterAs(tokens, open) && !inSourcePosition(tokens, open))
1224
+ return undefined;
1225
+ if (opensAfterAs(tokens, open)) {
1226
+ const cte = cteAt(tokens, open);
1227
+ if (cte === undefined)
1228
+ return undefined; // a column list renames the output, or it's no CTE this check reads
1229
+ const refs = [];
1230
+ for (let j = 0; j < tokens.length; j++) {
1231
+ if (j === cte.at || tableNameOf(tokens[j]) !== cte.name || tokens[j + 1]?.text === "." || isOrderOrGroupBy(tokens, j))
1232
+ continue;
1233
+ if (j > open && j < close)
1234
+ return undefined; // recursive
1235
+ if (tokens[j + 1]?.text === "(" || hasColumnList(a, sourceEnd(a, j)))
1236
+ return undefined;
1237
+ refs.push({ at: j, scope: scopes.of[j], names: readNames(a, j) });
1238
+ }
1239
+ return refs;
1240
+ }
1241
+ if (hasColumnList(a, close))
1242
+ return undefined;
1243
+ const alias = aliasAt(a, close);
1244
+ const hidden = hiddenByGroupAlias(a, alias ?? close);
1245
+ return [{ at: open, scope: scopes.of[open], names: new Set(alias === undefined || hidden ? [] : [tokens[alias].name]) }];
1246
+ }
1247
+ /** Clauses that compute over a source's rows, or pick some of them, before an outside filter runs. */
1248
+ const COMPUTES_OVER_ROWS = new Set(["group", "having", "window", "qualify", "limit", "offset", "fetch", "top", "over"]);
1249
+ /**
1250
+ * Whether the body of the query at `open` passes `column` of the read named `names` through to
1251
+ * its output unrenamed, and computes nothing across rows: every select-list item is a plain column
1252
+ * (`col` or `q.col`, optionally aliased), `q.*` or `*`; exactly one yields that name, and it is the
1253
+ * column itself, the read's `q.*`, or `*` over a single source; and the body has no `GROUP BY`,
1254
+ * `HAVING`, window, `QUALIFY`, `LIMIT`, `OFFSET`, `FETCH` or `TOP`, which would run over every tenant's rows
1255
+ * before the outside filter. An item with a string or comment in it, `DISTINCT ON` or a nested
1256
+ * `WITH` fails closed. The column and the name it yields are compared as {@link namesColumn} reads
1257
+ * them, and an item that yields the name in another case fails closed too.
1258
+ */
1259
+ function passesThrough(a, open, names, column) {
1260
+ const { tokens, scopes, view } = a;
1261
+ const reading = view.reading.profile.id;
1262
+ const scope = scopeKey(open, 0);
1263
+ const depth = tokens[open].depth + 1;
1264
+ const level = (i) => scopes.of[i] === scope && tokens[i].depth === depth;
1265
+ const select = open + 1;
1266
+ if (tokens[select]?.kw !== "select")
1267
+ return false;
1268
+ let from = select + 1;
1269
+ while (from < tokens.length && !(level(from) && tokens[from].kw === "from"))
1270
+ from++;
1271
+ if (from >= tokens.length)
1272
+ return false;
1273
+ let first = select + 1;
1274
+ if (tokens[first]?.kw === "all" || tokens[first]?.kw === "distinct")
1275
+ first++;
1276
+ if (tokens[first]?.kw === "on")
1277
+ return false;
1278
+ const end = scopes.close.get(open) ?? tokens.length;
1279
+ for (let i = select; i < end; i++)
1280
+ if (scopes.of[i] === scope && COMPUTES_OVER_ROWS.has(tokens[i].kw))
1281
+ return false;
1282
+ const items = [[]];
1283
+ for (let i = first; i < from; i++) {
1284
+ if (level(i) && tokens[i].text === ",")
1285
+ items.push([]);
1286
+ else
1287
+ items[items.length - 1].push(i);
1288
+ }
1289
+ let yields = 0;
1290
+ for (const item of items) {
1291
+ if (item.length === 0)
1292
+ return false;
1293
+ // Text the view blanked (a string, a comment) could be part of the item, so don't guess.
1294
+ const last = tokens[item[item.length - 1]];
1295
+ for (let p = tokens[item[0]].offset; p < last.offset + last.text.length; p++) {
1296
+ if (view.text[p] !== view.source[p] && view.text[p] !== QUOTE_MARK)
1297
+ return false;
1298
+ }
1299
+ const ts = item.map((i) => tokens[i]);
1300
+ const n = ts.length;
1301
+ if (n === 1 && ts[0].text === "*") {
1302
+ if (!singleSource(a, open, scope, depth))
1303
+ return false;
1304
+ yields++;
1305
+ continue;
1306
+ }
1307
+ if (n === 3 && ts[1].text === "." && ts[2].text === "*") {
1308
+ if (!names.has(ts[0].name))
1309
+ return false;
1310
+ yields++;
1311
+ continue;
1312
+ }
1313
+ let alias;
1314
+ let expr = ts;
1315
+ if (n >= 3 && ts[n - 2].kw === "as" && isWord(ts[n - 1])) {
1316
+ alias = ts[n - 1];
1317
+ expr = ts.slice(0, n - 2);
1318
+ }
1319
+ else if (n >= 2 && isWord(ts[n - 1]) && (isWord(ts[n - 2]) || ts[n - 2].text === ")" || /^\d/.test(ts[n - 2].text))) {
1320
+ alias = ts[n - 1];
1321
+ expr = ts.slice(0, n - 1);
1322
+ }
1323
+ const isColumn = expr.length === 1 ? isWord(expr[0]) : expr.length === 3 && isWord(expr[0]) && expr[1].text === "." && isWord(expr[2]);
1324
+ if (!isColumn)
1325
+ return false; // an expression, a call or an aggregate
1326
+ const name = expr[expr.length - 1];
1327
+ // An item that yields the name in any case is a candidate; it must name the column the way the engine reads it.
1328
+ if ((alias ?? name).lower !== column.toLowerCase())
1329
+ continue;
1330
+ if (!namesColumn(alias ?? name, column, reading) || !namesColumn(name, column, reading))
1331
+ return false;
1332
+ if (expr.length === 3 && !names.has(expr[0].name))
1333
+ return false; // another expression named like the column
1334
+ yields++;
1335
+ }
1336
+ return yields === 1;
1337
+ }
1338
+ /** Whether the `FROM` of the query at `open` has one source: no join and no comma before its next clause. */
1339
+ function singleSource(a, open, scope, depth) {
1340
+ const { tokens, scopes } = a;
1341
+ const end = scopes.close.get(open) ?? tokens.length;
1342
+ let i = open + 1;
1343
+ while (i < end && !(scopes.of[i] === scope && tokens[i].depth === depth && tokens[i].kw === "from"))
1344
+ i++;
1345
+ for (i++; i < end; i++) {
1346
+ const t = tokens[i];
1347
+ if (scopes.of[i] !== scope || t.depth !== depth)
1348
+ continue;
1349
+ if (t.text === "," || JOIN_WORDS.has(t.kw) || (t.text === "(" && !opensQuery(tokens, i)))
1350
+ return false;
1351
+ // Only a clause no engine takes as an alias ends the FROM: past `lock` or `qualify`, which
1352
+ // may be the first source's alias, a second source can still follow.
1353
+ if (ENDS_A_SOURCE.has(t.kw) && !JOIN_WORDS.has(t.kw))
1354
+ return true;
1355
+ }
1356
+ return true;
1357
+ }
1358
+ /**
1359
+ * Whether `read`'s `SELECT` ANDs in `col = '<value>'` for `target.discriminator`: its column, as
1360
+ * {@link namesColumn} compares it (unqualified, or qualified with the read's table or alias), `=`, and a plain `'…'` literal,
1361
+ * read from the statement at the offsets where the view blanked it, whose value is one of the
1362
+ * discriminator's values and which nothing but whitespace follows up to the next token (no
1363
+ * `''`-escape, no adjacent literal, no comment).
810
1364
  */
811
- function hasDiscriminator(tokens, from, to, view, target) {
1365
+ function hasDiscriminator(a, read, target) {
812
1366
  const { column, values } = target.discriminator;
1367
+ const { tokens, view } = a;
813
1368
  const source = view.source;
814
- for (let i = from; i < to; i++) {
1369
+ for (let i = 0; i < tokens.length; i++) {
815
1370
  const t = tokens[i];
816
1371
  const eq = tokens[i + 1];
817
- if (!isIdentifier(t) || t.lower !== column.toLowerCase() || eq?.text !== "=")
1372
+ if (a.scopes.of[i] !== read.scope || !isIdentifier(t) || !namesColumn(t, column, view.reading.profile.id) || eq?.text !== "=")
818
1373
  continue;
819
- const qualifier = tokens[i - 1]?.text === "." ? tokens[i - 2] : undefined;
820
- if (qualifier !== undefined && namesOtherTable(tokens, qualifier.lower, target.table.toLowerCase()))
1374
+ const qualifier = qualifierOf(tokens, i);
1375
+ if (qualifier !== undefined && !read.names.has(qualifier))
821
1376
  continue;
822
1377
  let open = eq.offset + 1;
823
1378
  while (/\s/.test(source[open] ?? ""))
@@ -834,38 +1389,15 @@ function hasDiscriminator(tokens, from, to, view, target) {
834
1389
  const next = tokens[i + 2]?.offset ?? source.length;
835
1390
  if (view.text.slice(open, close + 1).trim() !== "" || source.slice(close + 1, next).trim() !== "")
836
1391
  continue;
837
- if (isFilterConjunct(tokens, qualifiedStart(tokens, i), i + 1, view.reading))
1392
+ const filters = filteredScopes(a, qualifiedStart(tokens, i), i + 1, qualifier, i);
1393
+ if (filters.some((f) => f.ownSelect && f.scope === read.scope && (qualifier !== undefined || f.on === undefined || seenByOn(tokens, f.on, read.at))))
838
1394
  return true;
839
1395
  }
840
1396
  return false;
841
1397
  }
842
- /**
843
- * Token ranges `[from, to)` of the statement's query blocks: the whole statement, and
844
- * each branch of every set operation at any depth (bounded by the parentheses around it).
845
- */
846
- function queryBlocks(tokens) {
847
- const blocks = [[0, tokens.length]];
848
- const splits = new Map(); // enclosing "(" (or -1) → set-operator positions
849
- tokens.forEach((t, i) => {
850
- if (!SET_OPERATORS.has(t.kw))
851
- return;
852
- const open = enclosing(tokens, i, "(");
853
- splits.set(open, [...(splits.get(open) ?? []), i]);
854
- });
855
- for (const [open, positions] of splits) {
856
- const end = open < 0 ? tokens.length : enclosing(tokens, open + 1, ")");
857
- let from = open + 1;
858
- for (const at of positions) {
859
- blocks.push([from, at]);
860
- from = at + 1;
861
- }
862
- blocks.push([from, end < 0 ? tokens.length : end]);
863
- }
864
- return blocks;
865
- }
866
1398
  /** An identifier token: a word that isn't one of the predicate forms' keywords. */
867
1399
  function isIdentifier(token) {
868
- return /^[A-Za-z_\u0080-\uffff]/.test(token.text) && !PREDICATE_WORDS.has(token.kw);
1400
+ return token.quoted === true || (/^[A-Za-z_\u0080-\uffff]/.test(token.text) && !PREDICATE_WORDS.has(token.kw));
869
1401
  }
870
1402
  /**
871
1403
  * A clause keyword used as a function name (`LEFT(status, 1)`, `RIGHT(…)`) is an identifier.
@@ -882,37 +1414,28 @@ function qualifiedStart(tokens, end) {
882
1414
  return start;
883
1415
  }
884
1416
  /**
885
- * The token span `[start, end]` of a tenant predicate around the placeholder at `k`:
886
- * `col = P`, `P = col`, `col IN (P)` or `col = ANY (P)`, where `col` (the last part of a
887
- * possibly qualified name) is the target's column. A qualifier that names, or is the
888
- * alias of, a table other than the target's makes it no predicate for that table. With
889
- * `exactQuoted` (read the Postgres way, where a quoted name is compared exactly), a quoted
890
- * `col` counts only when spelled as the schema spells the column: `"AGENCY_ID"` is another
891
- * column there, which another source in the query may hold.
1417
+ * The span `[start, end]` and column index of a tenant predicate around the placeholder at `k`:
1418
+ * `col = P`, `P = col`, `col IN (P)` or `col = ANY (P)`, where `col` (the last part of a possibly
1419
+ * qualified name) is `column`, as {@link namesColumn} compares them under `reading`. Which read
1420
+ * the column belongs to is its qualifier's to say.
892
1421
  */
893
- function predicateSpan(tokens, k, target, exactQuoted) {
1422
+ function predicateAt(tokens, k, column, reading) {
894
1423
  const at = (i) => tokens[i];
895
- const column = target.column.toLowerCase();
896
1424
  const matches = (i) => {
897
1425
  const t = at(i);
898
- if (t === undefined || !isIdentifier(t) || t.lower !== column)
899
- return false;
900
- if (exactQuoted && t.kw === "" && t.text !== target.column)
901
- return false;
902
- const qualifier = at(i - 1)?.text === "." ? at(i - 2) : undefined;
903
- return qualifier === undefined || !namesOtherTable(tokens, qualifier.lower, target.table.toLowerCase());
1426
+ return t !== undefined && isIdentifier(t) && namesColumn(t, column, reading);
904
1427
  };
905
1428
  // col = P
906
1429
  if (at(k - 1)?.text === "=" && matches(k - 2))
907
- return [qualifiedStart(tokens, k - 2), k];
1430
+ return { start: qualifiedStart(tokens, k - 2), end: k, column: k - 2 };
908
1431
  const closed = at(k + 1)?.text === ")";
909
1432
  // col IN ( P )
910
1433
  if (closed && at(k - 1)?.text === "(" && at(k - 2)?.kw === "in" && matches(k - 3)) {
911
- return [qualifiedStart(tokens, k - 3), k + 1];
1434
+ return { start: qualifiedStart(tokens, k - 3), end: k + 1, column: k - 3 };
912
1435
  }
913
1436
  // col = ANY ( P )
914
1437
  if (closed && at(k - 1)?.text === "(" && at(k - 2)?.kw === "any" && at(k - 3)?.text === "=" && matches(k - 4)) {
915
- return [qualifiedStart(tokens, k - 4), k + 1];
1438
+ return { start: qualifiedStart(tokens, k - 4), end: k + 1, column: k - 4 };
916
1439
  }
917
1440
  // P = col
918
1441
  if (at(k + 1)?.text === "=") {
@@ -920,32 +1443,34 @@ function predicateSpan(tokens, k, target, exactQuoted) {
920
1443
  while (at(end + 1)?.text === "." && at(end + 2) !== undefined && isIdentifier(at(end + 2)))
921
1444
  end += 2;
922
1445
  if (matches(end))
923
- return [k, end];
1446
+ return { start: k, end, column: end };
924
1447
  }
925
1448
  return undefined;
926
1449
  }
927
1450
  /**
928
- * Whether the qualifier `name` names a table other than `table`: `name` is that other
929
- * table, or is declared as its alias (`FROM appointments a`, `JOIN appointments AS a`).
930
- * A qualifier the statement doesn't declare (a derived table's or CTE's name) names no
931
- * known table, so it doesn't rule the predicate out.
1451
+ * Whether the written column `t` is the policy's `column` under the `reading`'s rule, compared with
1452
+ * the column's own spelling, which the policy writes as the schema does. A name that compares as
1453
+ * another column may be another source's, so it ties nothing to the policy's.
1454
+ * - Read the Postgres way (`postgres`, `cockroachdb`): a quoted name exactly, an unquoted one with
1455
+ * its ASCII letters lower-cased. So `"AGENCY_ID"` is another column than `agency_id`, and an
1456
+ * unquoted `agencyid` another than `"agencyId"`.
1457
+ * - Read the SQL Server way, the name as written: the database's collation may compare names
1458
+ * case-sensitively, and the check can't see it, so `agencyid` is another column than `agencyId`.
1459
+ * - Read the MySQL way (MySQL and MariaDB), lower-cased.
1460
+ * - Otherwise (SQLite, and the generic reading), with ASCII letters lower-cased, quoted or not, so
1461
+ * `égency_id` is another column than `Égency_id`.
1462
+ * A custom id or none is read every way, so the column has to match under each.
932
1463
  */
933
- function namesOtherTable(tokens, name, table) {
934
- if (name === table)
935
- return false;
936
- for (let i = 1; i < tokens.length; i++) {
937
- if (tokens[i].lower !== name || !isIdentifier(tokens[i]))
938
- continue;
939
- const source = tokens[i - 1]?.kw === "as" ? i - 2 : i - 1;
940
- const src = tokens[source];
941
- if (src === undefined || !isIdentifier(src) || tokens[source + 1]?.text === ".")
942
- continue;
943
- const before = tokens[qualifiedStart(tokens, source) - 1];
944
- if (before !== undefined && (before.kw === "from" || before.kw === "join" || before.text === ",")) {
945
- return src.lower !== table;
946
- }
947
- }
948
- return false;
1464
+ function namesColumn(t, column, reading) {
1465
+ if (reading === "postgres" || reading === "sqlserver")
1466
+ return t.name === column;
1467
+ if (reading === "mysql")
1468
+ return t.lower === column.toLowerCase();
1469
+ return asciiLower(t.name) === asciiLower(column);
1470
+ }
1471
+ /** `name` with its ASCII letters lower-cased, as SQLite folds a name. */
1472
+ function asciiLower(name) {
1473
+ return name.replace(/[A-Z]+/g, (s) => s.toLowerCase());
949
1474
  }
950
1475
  /**
951
1476
  * Whether the subquery opened by the `(` at `open` is a row source (a derived table in
@@ -972,6 +1497,9 @@ function subqueryUse(tokens, open) {
972
1497
  return tokens[open - 2] !== undefined && isIdentifier(tokens[open - 2]) ? "in" : "other";
973
1498
  if (opener.kw === "as" || opener.kw === "lateral")
974
1499
  return "table";
1500
+ // A CTE body after `AS MATERIALIZED (` or `AS NOT MATERIALIZED (`.
1501
+ if (opener.kw === "materialized" && (tokens[open - 2]?.kw === "as" || (tokens[open - 2]?.kw === "not" && tokens[open - 3]?.kw === "as")))
1502
+ return "table";
975
1503
  const depth = tokens[open].depth;
976
1504
  for (let i = open - 1; i >= 0; i--) {
977
1505
  const t = tokens[i];
@@ -1029,29 +1557,94 @@ function isOuterJoinOn(tokens, on) {
1029
1557
  }
1030
1558
  return false;
1031
1559
  }
1560
+ /** Join words that may take an `ON`: MySQL's `STRAIGHT_JOIN` may, so it counts as one. */
1561
+ const TAKES_ON = new Set(["join", "straight_join"]);
1032
1562
  /**
1033
- * Whether the term `tokens[start..end]` is ANDed into a `WHERE`, `ON` (of an inner join)
1034
- * or `HAVING` clause of the statement: at its own level and at every enclosing
1035
- * parenthesized level up to that clause, it sits between `AND`s (or the clause's start
1036
- * and end), with no `OR`, `XOR` (or MySQL's `||`) at that level and no `NOT` in front of
1037
- * it. A clause inside a subquery counts only when the subquery is a row source (a
1038
- * derived table or CTE), or when the whole `col IN (…)` around it is itself such a
1039
- * conjunct; not inside `EXISTS (…)`, a scalar subquery, or a comparison. A level opened by a function call or a comparison doesn't count.
1563
+ * Whether the read at `at` is among the operands of the join the `ON` at `on` closes: the only reads
1564
+ * an unqualified column there can name. They run from the join's left operand up to the `ON`. Going
1565
+ * left from the join's own `JOIN`, the left operand stops at `FROM`, at the `(` around the join, at
1566
+ * a `FROM`-list comma (which binds looser than a join, though not on SQLite, where this only
1567
+ * narrows the check), or at a `JOIN` whose `ON` or `USING` hasn't come yet (`a LEFT JOIN b JOIN c
1568
+ * ON … ON …`). `CROSS JOIN`, `NATURAL JOIN` and `APPLY` take no `ON`, so the operand runs past them.
1040
1569
  */
1041
- function isFilterConjunct(tokens, start, end, reading) {
1042
- const disjunction = (t) => t.kw === "or" || t.kw === "xor" || (t.text === "||" && reading.pipesAreOr);
1570
+ function seenByOn(tokens, on, at) {
1571
+ if (at >= on)
1572
+ return false;
1573
+ const depth = tokens[on].depth;
1574
+ let join = -1;
1575
+ for (let i = on - 1; i >= 0 && join < 0; i--) {
1576
+ const t = tokens[i];
1577
+ if (t.depth < depth)
1578
+ return false;
1579
+ if (t.depth > depth)
1580
+ continue;
1581
+ if (t.kw === "from" || t.text === ",")
1582
+ return false;
1583
+ if (TAKES_ON.has(t.kw))
1584
+ join = i;
1585
+ }
1586
+ if (join < 0)
1587
+ return false;
1588
+ let pending = 0; // ON or USING seen going left, each closing a JOIN further left
1589
+ for (let i = join - 1; i >= 0; i--) {
1590
+ const t = tokens[i];
1591
+ if (t.depth < depth)
1592
+ return at > i;
1593
+ if (t.depth > depth)
1594
+ continue;
1595
+ if (t.kw === "from" || t.text === ",")
1596
+ return at > i;
1597
+ if (t.kw === "on" || t.kw === "using")
1598
+ pending++;
1599
+ else if (TAKES_ON.has(t.kw) && !takesNoOn(tokens, i)) {
1600
+ if (pending === 0)
1601
+ return at > i;
1602
+ pending--;
1603
+ }
1604
+ }
1605
+ return true;
1606
+ }
1607
+ /** Whether the `JOIN` at `join` is a `CROSS JOIN` or a `NATURAL … JOIN`, which take no `ON`. */
1608
+ function takesNoOn(tokens, join) {
1609
+ const depth = tokens[join].depth;
1610
+ for (let k = join - 1; k >= 0; k--) {
1611
+ const w = tokens[k];
1612
+ if (w.depth !== depth || BEFORE_A_JOIN.has(w.kw) || w.text === ",")
1613
+ return false;
1614
+ if ((w.kw === "cross" || w.kw === "natural") && tokens[k - 1]?.text !== "." && tokens[k + 1]?.text !== ".")
1615
+ return true;
1616
+ }
1617
+ return false;
1618
+ }
1619
+ /**
1620
+ * The `SELECT`s whose reads the term `tokens[start..end]` filters (#399), each with the qualifier
1621
+ * that ties it to a read there. The term must be ANDed into a `WHERE`, `ON` (of an inner join) or
1622
+ * `HAVING` clause: at its own level and at every enclosing parenthesized level up to that clause,
1623
+ * it sits between `AND`s (or the clause's start and end), with no `OR`, `XOR` (or MySQL's `||`)
1624
+ * at that level and no `NOT` in front of it. It then filters that clause's `SELECT`, through
1625
+ * `qualifier`, its column's. When that `SELECT` is the whole right side of `col IN (…)` and the
1626
+ * `col IN (…)` is itself such a conjunct, it filters the query around it too, through `col`'s
1627
+ * qualifier. A level opened by a function call or a comparison doesn't count.
1628
+ */
1629
+ function filteredScopes(a, start, end, qualifier, termColumn) {
1630
+ const { tokens, scopes } = a;
1631
+ const disjunction = (t) => t.kw === "or" || t.kw === "xor" || (t.text === "||" && a.view.reading.pipesAreOr);
1043
1632
  const endsClause = (i) => CLAUSE_END.has(tokens[i].kw) && !isFunctionCall(tokens, i);
1633
+ const filters = [];
1044
1634
  let lo = start;
1045
1635
  let hi = end;
1046
1636
  let depth = tokens[start].depth;
1637
+ let q = qualifier;
1638
+ let ownSelect = true;
1639
+ let column = termColumn;
1047
1640
  for (;;) {
1048
1641
  // What is directly around the term, at its level.
1049
1642
  const before = tokens[lo - 1];
1050
1643
  const after = tokens[hi + 1];
1051
- const leftOk = before !== undefined && (before.kw === "and" || before.text === "(" || FILTER_CLAUSE.has(before.kw));
1644
+ const leftOk = before !== undefined && (conjunctAnd(tokens, lo - 1) || before.text === "(" || FILTER_CLAUSE.has(before.kw));
1052
1645
  const rightOk = after === undefined || after.kw === "and" || after.text === ")" || after.text === ";" || endsClause(hi + 1);
1053
1646
  if (!leftOk || !rightOk)
1054
- return false;
1647
+ return filters;
1055
1648
  // Scan the rest of this level on both sides for a disjunction, a NOT, or the clause keyword.
1056
1649
  let clause = -1;
1057
1650
  let open = -1;
@@ -1064,13 +1657,13 @@ function isFilterConjunct(tokens, start, end, reading) {
1064
1657
  if (t.depth > depth)
1065
1658
  continue;
1066
1659
  if (disjunction(t) || t.kw === "not")
1067
- return false;
1660
+ return filters;
1068
1661
  if (FILTER_CLAUSE.has(t.kw)) {
1069
1662
  clause = i;
1070
1663
  break;
1071
1664
  }
1072
1665
  if ((endsClause(i) || NOT_A_FILTER.has(t.kw)) && !isFunctionCall(tokens, i))
1073
- return false;
1666
+ return filters;
1074
1667
  }
1075
1668
  let close = tokens.length;
1076
1669
  for (let j = hi + 1; j < tokens.length; j++) {
@@ -1081,44 +1674,160 @@ function isFilterConjunct(tokens, start, end, reading) {
1081
1674
  }
1082
1675
  if (t.depth > depth)
1083
1676
  continue;
1084
- if (t.text === ";" || endsClause(j))
1677
+ if (t.text === ";" || endsFilterScan(tokens, j, a.view.reading.profile.id))
1085
1678
  break;
1086
1679
  if (disjunction(t))
1087
- return false;
1680
+ return filters;
1088
1681
  }
1089
1682
  if (clause >= 0) {
1090
1683
  if (tokens[clause].kw === "on" && isOuterJoinOn(tokens, clause))
1091
- return false;
1092
- if (depth === 0)
1093
- return true;
1094
- // The clause is inside a subquery: how does the query around it use that subquery?
1095
- const blockOpen = enclosing(tokens, clause, "(");
1096
- const blockClose = enclosing(tokens, clause, ")");
1097
- if (blockOpen < 0 || blockClose < 0)
1098
- return false;
1099
- const use = subqueryUse(tokens, blockOpen);
1100
- if (use === "table")
1101
- return true;
1102
- if (use === "other")
1103
- return false;
1104
- lo = qualifiedStart(tokens, blockOpen - 2); // `col IN (…)`
1105
- hi = blockClose;
1106
- depth = tokens[blockOpen].depth;
1684
+ return filters;
1685
+ if (tokens[clause].kw === "having" && !onGroupKey(a, clause, column))
1686
+ return filters;
1687
+ const scope = scopes.of[clause];
1688
+ filters.push({ scope, qualifier: q, ownSelect, ...(tokens[clause].kw === "on" ? { on: clause } : {}) });
1689
+ // `col IN (<this SELECT>)` limits the query around it to the rows this SELECT returns.
1690
+ const query = scopeOpen(scope);
1691
+ const queryClose = scopes.close.get(query);
1692
+ if (query < 0 || queryClose === undefined || scopes.branched.has(query) || subqueryUse(tokens, query) !== "in")
1693
+ return filters;
1694
+ lo = qualifiedStart(tokens, query - 2);
1695
+ hi = queryClose;
1696
+ depth = tokens[query].depth;
1697
+ q = qualifierOf(tokens, query - 2);
1698
+ column = query - 2;
1699
+ ownSelect = false;
1107
1700
  continue;
1108
1701
  }
1109
1702
  if (open < 0 || tokens[open].text !== "(" || close >= tokens.length || tokens[close].text !== ")")
1110
- return false;
1703
+ return filters;
1111
1704
  // A parenthesized group counts only as a plain boolean conjunct: opened right after
1112
1705
  // WHERE/ON/HAVING, AND, or another "(". Anything else (`f(`, `IN (`, `EXISTS (`,
1113
1706
  // `NOT (`, `= (`) makes it something other than a filter conjunct.
1114
1707
  const opener = tokens[open - 1];
1115
- if (opener === undefined || !(FILTER_CLAUSE.has(opener.kw) || opener.kw === "and" || opener.text === "("))
1116
- return false;
1708
+ if (opener === undefined || !(FILTER_CLAUSE.has(opener.kw) || conjunctAnd(tokens, open - 1) || opener.text === "("))
1709
+ return filters;
1117
1710
  lo = open;
1118
1711
  hi = close;
1119
1712
  depth = tokens[open].depth;
1120
1713
  }
1121
1714
  }
1715
+ /** Whether the token at `i` is an `AND` that joins two conjuncts, not the `AND` of a `BETWEEN`. */
1716
+ function conjunctAnd(tokens, i) {
1717
+ return tokens[i]?.kw === "and" && !andOfBetween(tokens, i);
1718
+ }
1719
+ /**
1720
+ * Whether the `AND` at `and` closes a `BETWEEN` (`x BETWEEN 0 AND y`), so that what follows is the
1721
+ * upper bound, not a conjunct: MySQL reads `0 BETWEEN 0 AND col IN (…)` with the `IN` inside the
1722
+ * bound. Each `BETWEEN` at the `AND`'s level, from the start of its clause or parentheses, takes the
1723
+ * first `AND` after it there, whatever its lower bound holds (`LEFT(…)`, a cast); an `AND` inside a
1724
+ * `CASE … END` belongs to the `CASE`.
1725
+ */
1726
+ function andOfBetween(tokens, and) {
1727
+ const depth = tokens[and].depth;
1728
+ // A word beside a `.` is a name (`o.and`, `o.end`), never the keyword.
1729
+ const dotted = (i) => tokens[i - 1]?.text === "." || tokens[i + 1]?.text === ".";
1730
+ let from = and - 1;
1731
+ while (from >= 0) {
1732
+ const t = tokens[from];
1733
+ if (t.depth < depth || (t.depth === depth && !dotted(from) && (FILTER_CLAUSE.has(t.kw) || t.kw === "select")))
1734
+ break;
1735
+ from--;
1736
+ }
1737
+ let pending = 0;
1738
+ let inCase = 0;
1739
+ for (let i = from + 1; i < and; i++) {
1740
+ const t = tokens[i];
1741
+ if (t.depth !== depth || dotted(i))
1742
+ continue;
1743
+ if (t.kw === "case")
1744
+ inCase++;
1745
+ else if (t.kw === "end" && inCase > 0)
1746
+ inCase--;
1747
+ else if (inCase === 0 && t.kw === "between")
1748
+ pending++;
1749
+ else if (inCase === 0 && t.kw === "and" && pending > 0)
1750
+ pending--;
1751
+ }
1752
+ return pending > 0;
1753
+ }
1754
+ /**
1755
+ * Whether the word at `j` ends the scan for an `OR` beside a predicate: a clause start that every
1756
+ * engine reserves (`WHERE`, `HAVING`, `GROUP BY`, `ORDER BY`, a set operation, `JOIN`), not beside a
1757
+ * `.`, and MySQL's `STRAIGHT_JOIN` where the SQL is read as MySQL. Other clause words (`LIMIT`, `LOCK`,
1758
+ * `QUALIFY`, `NATURAL`, `USING`, `WINDOW`) are names on some engine, so the scan goes on past them, and
1759
+ * an `OR` after one still counts as beside the predicate.
1760
+ */
1761
+ function endsFilterScan(tokens, j, engine) {
1762
+ const t = tokens[j];
1763
+ if (tokens[j - 1]?.text === "." || tokens[j + 1]?.text === "." || isFunctionCall(tokens, j))
1764
+ return false;
1765
+ if (t.kw === "group" || t.kw === "order")
1766
+ return tokens[j + 1]?.kw === "by";
1767
+ if (t.kw === "straight_join")
1768
+ return engine === "mysql";
1769
+ return ["where", "having", "union", "intersect", "except", "join"].includes(t.kw);
1770
+ }
1771
+ /**
1772
+ * Whether a predicate on the column at `column`, ANDed into the `HAVING` at `having`, filters rows:
1773
+ * only when that column is a plain key of its `SELECT`'s `GROUP BY` (`agency_id`, or `o.agency_id`
1774
+ * written the same way). On any other column, a group holds every row it covers, and SQLite and
1775
+ * MariaDB take the column's value from one of them. Read the MySQL way, an unqualified column that a
1776
+ * select-list item is aliased to never counts: MySQL and MariaDB resolve such a `HAVING` name to the
1777
+ * alias in some forms and to the `GROUP BY` column in others, so the check refuses the name either way.
1778
+ */
1779
+ function onGroupKey(a, having, column) {
1780
+ const { tokens } = a;
1781
+ const depth = tokens[having].depth;
1782
+ let select = -1;
1783
+ let groupBy = -1;
1784
+ for (let i = having - 1; i >= 0 && select < 0; i--) {
1785
+ const t = tokens[i];
1786
+ if (t.depth < depth)
1787
+ break;
1788
+ if (t.depth > depth)
1789
+ continue;
1790
+ if (t.kw === "by" && tokens[i - 1]?.kw === "group" && groupBy < 0)
1791
+ groupBy = i;
1792
+ if (t.kw === "select")
1793
+ select = i;
1794
+ }
1795
+ if (select < 0 || groupBy < 0)
1796
+ return false;
1797
+ const name = tokens[column].lower;
1798
+ const qualifier = qualifierOf(tokens, column);
1799
+ const itemRanges = (from, to) => {
1800
+ const out = [];
1801
+ let itemStart = from;
1802
+ for (let i = from; i <= to; i++) {
1803
+ const t = tokens[i];
1804
+ if (i === to || (t?.depth === depth && t.text === ",")) {
1805
+ if (i > itemStart)
1806
+ out.push([itemStart, i - 1]);
1807
+ itemStart = i + 1;
1808
+ }
1809
+ }
1810
+ return out;
1811
+ };
1812
+ if (a.view.reading.profile.id === "mysql" && qualifier === undefined) {
1813
+ let from = select + 1;
1814
+ while (tokens[from]?.kw === "distinct" || tokens[from]?.kw === "all")
1815
+ from++;
1816
+ let fromKw = from;
1817
+ while (fromKw < having && !(tokens[fromKw].depth === depth && tokens[fromKw].kw === "from"))
1818
+ fromKw++;
1819
+ const aliased = itemRanges(from, fromKw).some(([s, e]) => e > s && tokens[e - 1]?.text !== "." && isIdentifier(tokens[e]) && tokens[e].lower === name);
1820
+ if (aliased)
1821
+ return false;
1822
+ }
1823
+ return itemRanges(groupBy + 1, having).some(([s, e]) => {
1824
+ const last = tokens[e];
1825
+ if (!isIdentifier(last) || last.lower !== name)
1826
+ return false;
1827
+ const plain = e === s || (e === s + 2 && tokens[s + 1]?.text === ".");
1828
+ return plain && qualifierOf(tokens, e) === qualifier;
1829
+ });
1830
+ }
1122
1831
  /** Index of the `(` or `)` that encloses the token at `i`, or -1 at the top level. */
1123
1832
  function enclosing(tokens, i, paren) {
1124
1833
  const depth = tokens[i].depth;