|
| 1 | +# ADR 0006: Internal query binding model |
| 2 | + |
| 3 | +## Status |
| 4 | + |
| 5 | +Accepted for the vNext implementation. The model remains package-internal until a |
| 6 | +column-completion vertical slice has validated it across the supported dialect |
| 7 | +corpora. |
| 8 | + |
| 9 | +## Context |
| 10 | + |
| 11 | +Column completion needs more than a parser's flat table list. It must distinguish |
| 12 | +query blocks, relation declarations, aliases, correlation, and clause-specific |
| 13 | +visibility. The parser adapter also has strict trust boundaries: raw parser ASTs |
| 14 | +are private, parser results can be partial, and cached analysis must belong to |
| 15 | +the exact statement and parser authority that produced it. |
| 16 | + |
| 17 | +The model must remain cheap enough for interactive use and must not make the |
| 18 | +existing parser-independent relation completion path wait for parsing. |
| 19 | + |
| 20 | +## Decision |
| 21 | + |
| 22 | +Add an internal, backend-neutral query binding model with: |
| 23 | + |
| 24 | +- statement-relative UTF-16 ranges; |
| 25 | +- query blocks and relation bindings identified by model-local array indexes; |
| 26 | +- a persistent scope chain in which each node adds at most one binding; |
| 27 | +- ordered, globally non-overlapping visibility regions that point to a scope; |
| 28 | +- independent query-block, relation-binding, and visibility coverage; |
| 29 | +- closed issue and unknown-source reason sets; |
| 30 | +- private authentication and exact statement/parser-authority ownership. |
| 31 | + |
| 32 | +Globally non-overlapping regions deliberately exclude nested query text from |
| 33 | +their parent region. A decoder splits a parent region around nested queries. |
| 34 | +This permits binary-search lookup without duplicating the visible binding set. |
| 35 | +Persistent scopes then reconstruct bindings in declaration order using linear |
| 36 | +space. In particular: |
| 37 | + |
| 38 | +- a `SELECT` list can point to the completed `FROM` scope even though it appears |
| 39 | + earlier in the source; |
| 40 | +- a join condition points to the scope containing prior relations and its |
| 41 | + current right-hand relation; |
| 42 | +- a correlated child block can enter through a parent scope; |
| 43 | +- an ordinary derived table enters through a scope that excludes same-level |
| 44 | + siblings; a future proven `LATERAL` decoder may opt into that scope. |
| 45 | + |
| 46 | +Aliases are the visible qualifier when present and hide a named relation's base |
| 47 | +name. Identifier equality is supplied by the dialect runtime; the model does |
| 48 | +not lowercase or otherwise reinterpret identifiers. |
| 49 | + |
| 50 | +Construction accepts untrusted plain data and validates it before issuing an |
| 51 | +immutable authenticated model. It rejects accessors, sparse or oversized |
| 52 | +arrays, malformed UTF-16 identifiers, invalid or unrelated ranges, forward |
| 53 | +parent edges, repeated bindings in a scope chain, overlapping regions, and |
| 54 | +unknown bindings paired with complete relation coverage. |
| 55 | + |
| 56 | +Resolution returns: |
| 57 | + |
| 58 | +- a visible binding list with complete or partial coverage; |
| 59 | +- one resolved binding, known ambiguity, or authoritative no-match; |
| 60 | +- unavailable instead of no-match when coverage is partial. |
| 61 | + |
| 62 | +## Ownership and invalidation |
| 63 | + |
| 64 | +The future parser service owns bounded parse/model caching and in-flight |
| 65 | +deduplication. A document session owns the mapping from its current statement |
| 66 | +slots to immutable models. The cache key includes exact statement text, |
| 67 | +dialect, parser authority/conformance, and implementation version. |
| 68 | +Statement-relative coordinates allow an unchanged statement to be reused after |
| 69 | +an edit shifts its absolute document position. Source, dialect, or parser |
| 70 | +authority changes invalidate the model; catalog epoch and completion context |
| 71 | +changes do not. |
| 72 | + |
| 73 | +The interactive statement limit is initially 16 KiB. The model also caps query |
| 74 | +blocks at 256, relation bindings at 1,024, scopes and regions at 2,048 each, |
| 75 | +nesting by the block limit, issues at 256, path depth at 8, and identifier |
| 76 | +components at 256 UTF-16 code units. |
| 77 | + |
| 78 | +## Degradation |
| 79 | + |
| 80 | +A decoder may claim complete evidence only for directly conforming parser output |
| 81 | +and constructs it understands. Compatibility parses can supply useful partial |
| 82 | +evidence but cannot prove absence. Unknown AST subtrees become an unknown source |
| 83 | +or a closed issue and downgrade the relevant coverage; they are never silently |
| 84 | +dropped under complete coverage. |
| 85 | + |
| 86 | +Invalid, failed, opaque, incomplete, or resource-limited analysis becomes |
| 87 | +partial or unavailable. Parser-independent relation completion remains |
| 88 | +available. Dremio remains partial or unavailable until it has an owned semantic |
| 89 | +corpus. |
| 90 | + |
| 91 | +## Consequences |
| 92 | + |
| 93 | +The model can support lazy batched column loading without exposing a parser AST |
| 94 | +or CodeMirror's eager `SQLNamespace`. Its indexes are not stable identities |
| 95 | +across revisions. Projection inference, output columns, star expansion, types, |
| 96 | +expression binding, semantic diagnostics, navigation, rename, formatting, and |
| 97 | +vendor constructs such as `PIVOT`, `UNNEST`, table functions, and unproven |
| 98 | +`LATERAL` semantics are explicitly outside this slice. |
| 99 | + |
| 100 | +The node-sql-parser integration now supplies a strict AST-to-plain-data decoder, |
| 101 | +normalizes inside the worker, and checks direct-versus-worker parity. Public |
| 102 | +semantic APIs remain deferred until a column-completion vertical slice proves |
| 103 | +the model against consumer and dialect corpora. |
0 commit comments