Skip to content

Anchor single-fact queries on the model that owns their metrics - #490

Merged
nicosuave merged 2 commits into
mainfrom
fact-anchored-joins
Nov 10, 2026
Merged

nicosuave merged 2 commits into
mainfrom
fact-anchored-joins

Conversation

@nicosuave

@nicosuave nicosuave commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

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.md and docs/compatibility/directed-relationships.md describe it.

Notes for review:

  • Existing expectations changed to match the rule: 24 on the Python side and 6 Rust unit tests. Four cases that require zero counts for empty members now assert them through a dimension-anchored Explore rather than by default.
  • A filter or segment on a dimension model makes that join INNER, so the NULL group disappears; this includes IS NULL, so such a filter cannot select the unmatched bucket. The multi-fact path already behaves this way.
  • With directed relationships, the metric owner anchors only if it can reach every model through permitted paths. In one cyclic test graph that selected a different route, so that test now pins its base model explicitly.
  • SQL FROM <model> does not choose the anchor; the metric owner does.
  • The join-key rollup route computes remote dimension expressions before the join, as raw planning does, so a dimension such as 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.
  • The rollup route renders its null ordering through the target dialect, so MySQL and SQL Server no longer receive NULLS FIRST/NULLS LAST.
  • ORDER BY on the NULL group sorts NULLs first under the Python engine and last under the Rust engine. That difference predates this change.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

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:55:20.869215Z 7e6b471 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.

@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: 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".

Comment on lines +8182 to 8184
FROM {preagg_table} AS {rollup_alias}
LEFT JOIN {remote_source}
ON {" AND ".join(join_conditions)}{group_by_clause}{order_by_clause}{limit_clause}"""

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

Comment thread sidemantic/sql/generator.py Outdated
Comment on lines +8166 to +8167
nulls = "LAST" if direction == " DESC" else "FIRST"
order_clauses.append(f"{self._quote_alias(output_name)}{direction} NULLS {nulls}")

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 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.
@nicosuave
nicosuave merged commit 7bfb74a into main Nov 10, 2026
27 of 28 checks passed
@nicosuave
nicosuave deleted the fact-anchored-joins branch November 10, 2026 01:19
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