Skip to content

Fix multi-fact queries on PostgreSQL and ORDER BY position - #487

Merged
nicosuave merged 2 commits into
default-time-dim-groupingfrom
postgres-multifact-join
Nov 10, 2026
Merged

nicosuave merged 2 commits into
default-time-dim-groupingfrom
postgres-multifact-join

Conversation

@nicosuave

@nicosuave nicosuave commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

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:

  • BigQuery, Snowflake, ClickHouse, Databricks and Spark keep the previous join condition and could not be run here.

@nicosuave
nicosuave added this pull request to stack #489 November 10, 2026 00:12
@nicosuave nicosuave changed the title Combine multi-fact sources with join keys PostgreSQL can plan Fix multi-fact queries on PostgreSQL and ORDER BY position Nov 10, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +252 to +255
Expression::NullSafeEq(operands) => Expression::Eq(Box::new(BinaryOp::new(
array(operands.left),
array(operands.right),
))),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T00:14:13.024555Z 80ce20d PR opened
ℹ️ 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" or "@codex security review".

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.
@nicosuave
nicosuave force-pushed the postgres-multifact-join branch from 80ce20d to 65f8de5 Compare November 10, 2026 00:31
@nicosuave
nicosuave merged commit 498f2ff into main Nov 10, 2026
30 of 57 checks passed
@nicosuave
nicosuave deleted the postgres-multifact-join branch November 10, 2026 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant