Repository navigation
Description
Problem, by example
The two SQL queries below are each lowered over a join. The example comes from reading the code; it has not been run.
-- (1) the qualifier names the wrong side, but the query still lowers
SELECT a.k, arg_max(a.payload, a.ts)
FROM t a JOIN u b ON a.k = b.k
GROUP BY a.k; -- suppose only `u` has a `ts` column
-- (2) the qualifier names a side, but the query is refused
SELECT a.k, arg_max(a.payload, a.v)
FROM t a JOIN u b ON a.k = b.k
GROUP BY a.k; -- both `t` and `u` have `v`- What happens today. In (1),
a.tsbinds tob.ts, becausetsis the only column with that name. In (2),a.vfails withcolumn v matches several columns. - What should happen. (1) should report that
ahas no columnts. (2) should binda.vto the left side'sv.
The same applies to the timestamp, value and group arguments of SQL temporal aggregates.
Why it happens
A schema carries no table qualifier any more: column lineage replaced Field.table. The front end resolves every other qualified name with its own Scope (crates/frontend-common/src/resolve.rs), using the qualifiers it tracks for each column.
Two paths do not go through that scope. They keep a ColumnRef::Qualified and resolve it later with resolve_column_ref (crates/types/src/ir/scalar/column_resolution.rs), which drops the qualifier and matches the name only:
- the payload columns of an extension intent (
arg_max/arg_min), resolved atcrates/types/src/ir/operator/agg_intent.rs(resolve("arg_col")/resolve("val_col")); - the arguments of a SQL temporal aggregate, resolved in
crates/frontend-sql/src/sql/mod.rs(timestamp_ref,value_ref,group_ref).
Making the Qualified arm strict (a debug_assert or an error) breaks 18 existing tests, so for now it stays name-only, and its comment says so.
Proposal
The front ends bind these references through their Scope while lowering, as they do for every other column, and pass a positional ColumnRef::Id or Named into the IR. After that, resolve_column_ref rejects Qualified as an error ("a qualified name reaches the IR unbound").
Scope and acceptance
- In scope: the SQL front end's extension intents and temporal aggregates, plus the
Qualifiedarm ofresolve_column_ref. - Out of scope: PromQL, which has no qualifiers.
- Acceptance:
- Query (1) returns an error.
- Query (2) binds
a.v. - The 18 tests that a strict arm breaks today pass with the strict arm.
Related
- This is a follow-up to the column lineage refactor (refactor(types, frontends): column lineage replaces table qualifiers (#1207 PR 2b) #1233), part of Tracking: implement the optimization formulation (memo candidates, properties, edges y_{u,c,i}, MILP) as named code per stage #1207.
Activity