Repository navigation
Fix multi-fact queries on PostgreSQL and ORDER BY position - #487
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 80ce20ddf0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| Expression::NullSafeEq(operands) => Expression::Eq(Box::new(BinaryOp::new( | ||
| array(operands.left), | ||
| array(operands.right), | ||
| ))), |
There was a problem hiding this comment.
Restrict array wrapping to scalar join keys
When an authored FULL JOIN compares array-valued columns, this unconditional rewrite can turn a valid query into a PostgreSQL runtime error. For example, ON a.id = b.id AND a.tags IS NOT DISTINCT FROM b.tags already has a hash-joinable key, but wrapping a NULL or empty int[] value in ARRAY[...] attempts to construct a multidimensional array and fails. This also affects ordinary SQL because rewrite_with_output_dialect sends every rewritten statement through this emitter, not just generated multi-fact joins. Restrict the lowering to generated scalar keys or preserve array-valued comparisons that already accompany a usable equality key.
Useful? React with 👍 / 👎.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Queries selecting metrics from several models aggregate each model in its own CTE and FULL OUTER JOIN them on the shared dimensions with IS NOT DISTINCT FROM. PostgreSQL only plans FULL JOIN on hash- or merge-joinable operators, so every multi-fact query with a dimension failed with "FULL JOIN is only supported with merge-joinable or hash-joinable join conditions". For PostgreSQL, compare single-element arrays instead: array equality is hash- and merge-joinable, uses the column type's own comparison, and treats NULL elements as equal, so NULL dimension buckets still meet in one row. Other dialects keep IS NOT DISTINCT FROM. The Rust engine gets the same condition in both combine paths, plus a PostgreSQL emission step for semantic SQL, whose leaves are generated as DuckDB before final emission. Its legacy combine path now also matches each source against every preceding source rather than only the first. ORDER BY positions in the same path were quoted as a column named "1". Pass a position through as a position in the Python engine and resolve it to the output column in the Rust aggregate plan.
The Rust engine lowered every FULL JOIN ... IS NOT DISTINCT FROM to an array comparison while emitting PostgreSQL, including joins written in the query. For an array-valued operand that changes the result: wrapping turns both NULL and the empty array into the empty array, so `a.tags IS NOT DISTINCT FROM b.tags` started matching NULL tags with empty tags. Remove the emitter rewrite. The semantic SQL rewriter now tells the generator which dialect the statement is emitted in, and the generator writes the PostgreSQL form of its own combine keys; authored joins pass through as written. The same wrapping made the generated combine merge the NULL and empty-array groups of an array-valued dimension. Both engines now also require the keys to agree on being NULL, which keeps the join hash-joinable and is a no-op for scalar keys.
80ce20d to
65f8de5
Compare
Queries combining metrics from two or more models with a shared dimension failed on PostgreSQL: the per-model aggregates were combined with
FULL OUTER JOIN ... ON a IS NOT DISTINCT FROM b, which PostgreSQL cannot plan ("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions").For PostgreSQL the combine now compares single-element arrays and requires both keys to agree on being NULL, which is hash-joinable, keeps the column type's own equality, lines up NULL dimension buckets and stays correct for array-valued dimension keys; other dialects are unchanged. The Rust engine applies the same condition where it generates the combine, in both combine paths; user-authored FULL JOINs are emitted as written.
ORDER BY <position>now works in multi-model queries on both engines instead of being quoted as a column named "1". Regression tests cover the generated PostgreSQL SQL and compare PostgreSQL results with DuckDB on identical data in the Postgres integration job.Notes for review: