Repository navigation
Anchor single-fact queries on the model that owns their metrics - #490
Conversation
A query whose metrics come from one model used to start from its first dimension's model. Metric rows with a NULL or unmatched key were dropped, so a breakdown by a joined dimension did not add up to the ungrouped total, and the same cell changed once a second model's metric was selected, because each source of a multi-fact query already kept its own rows. The metric owner is now the preserved side in both engines. Dimension models are LEFT JOINed onto it, unmatched metric rows form a NULL group, and dimension members without metric rows are no longer groups. An Explore base still names the anchor explicitly, directed relationships still pick the first model that can reach the others, and dimension-only queries are unchanged. The join-key rollup route keeps the rollup as the preserved side, declines any other explicit anchor, and orders its NULL group like raw planning. Tests that pinned the old rows are updated: zero-count member expectations move behind a dimension-anchored Explore, and golden join strings follow the new join order.
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7e6b4710b7
ℹ️ 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".
| FROM {preagg_table} AS {rollup_alias} | ||
| LEFT JOIN {remote_source} | ||
| ON {" AND ".join(join_conditions)}{group_by_clause}{order_by_clause}{limit_clause}""" |
There was a problem hiding this comment.
Evaluate remote dimensions before null-extending the join
When a remote dimension uses a non-null-preserving expression such as COALESCE(name, 'unknown'), preserving the rollup side now makes this route disagree with raw planning for unmatched fact keys. _generate_join_key_preaggregation evaluates the dimension expression after the LEFT JOIN, whereas _build_model_cte evaluates it before the join. Consequently, an unmatched order is grouped under 'unknown' by the rollup query but under NULL by the raw query, potentially merging its revenue with genuinely matched customers whose names are NULL. The equivalent Rust route has the same issue. Project remote dimension expressions in a subquery before joining, or otherwise preserve the raw route's null-extension semantics.
Useful? React with 👍 / 👎.
| nulls = "LAST" if direction == " DESC" else "FIRST" | ||
| order_clauses.append(f"{self._quote_alias(output_name)}{direction} NULLS {nulls}") |
There was a problem hiding this comment.
Render rollup null ordering through the target dialect
For MySQL or SQL Server connections, any join-key rollup query with order_by now emits unsupported NULLS FIRST or NULLS LAST syntax. Both dialects are supported by DRIVER_DIALECT_MAP, but this route returns its assembled SQL directly, so the new clause never passes through dialect-aware rendering. Unlike the ordinary ordering path, which uses SQLGlot expressions, this turns previously executable ordered rollup queries into syntax errors. Build and render the ordering with the target dialect so unsupported null-ordering clauses are omitted or emulated.
Useful? React with 👍 / 👎.
The rollup route evaluated a joined model's dimension expressions after its LEFT JOIN. An expression such as COALESCE(name, 'unknown') therefore renamed the NULL group of unmatched join keys and merged it with customers whose name really is NULL, while raw planning in Python computes dimensions in the model CTE and keeps the NULL group. Both engines now compute the remote keys and dimensions, including their time grain, in the joined subquery. The Rust raw planner evaluates dimensions after the join as well, so it guards a computed dimension of a joined model with that model's join key and returns the same rows as Python. The route's ORDER BY appended NULLS FIRST/LAST as text for every dialect. Python now builds the ordering as expressions rendered by the target dialect, like the ordinary path, and Rust omits the clause for MySQL and SQL Server, whose default NULL placement is the one wanted. The Python route also ignored a custom join predicate; it now declines such joins, as the Rust route already did.
This is a behaviour change.
A query whose metrics come from one model now keeps every row of that model: dimension models are LEFT JOINed onto it, rows with a NULL or unmatched key form a NULL dimension group, and a breakdown adds up to the ungrouped total. Previously the first dimension's model anchored the query, which dropped those rows and made a cell change when a second model's metric was added. For example, revenue by customer region returned 530 across regions against an ungrouped total of 540, and the missing 10 reappeared as soon as a ticket count was selected alongside it.
Dimension members without metric rows are no longer listed. To get them back, query through an Explore based on the dimension model, or also select a metric owned by it. Both engines and the join-key rollup route follow the same rule;
docs/semantic-input.md,docs/native-format.mdanddocs/compatibility/directed-relationships.mddescribe it.Notes for review:
IS NULL, so such a filter cannot select the unmatched bucket. The multi-fact path already behaves this way.FROM <model>does not choose the anchor; the metric owner does.COALESCE(name, 'unknown')stays NULL for unmatched facts on both routes. The Rust raw planner guards computed dimensions of joined models the same way. The Python rollup route now declines relationships with a custom join predicate, as the Rust route already did.NULLS FIRST/NULLS LAST.ORDER BYon the NULL group sorts NULLs first under the Python engine and last under the Rust engine. That difference predates this change.