Skip to content

fix: let measures read derived dimensions outside the group-by (#310) - #311

Merged
hachej merged 7 commits into
mainfrom
fix/issue-310-measure-derived-dims
Nov 11, 2026
Merged

hachej merged 7 commits into
mainfrom
fix/issue-310-measure-derived-dims

Conversation

@hachej

@hachej hachej commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #310 for single models. Derived-dimension measures on joined models are tracked separately (see the scope note below).

Problem

On a single model, a measure over a derived dimension crashed unless the query also grouped by that dimension:

sm = (to_semantic_table(tbl)
      .with_dimensions(revenue=lambda t: t.quantity * t.price, cat=lambda t: t.category)
      .with_measures(total_revenue=lambda t: t.revenue.sum()))
sm.group_by("cat").aggregate("total_revenue").execute()
# AttributeError: 'Table' object has no attribute 'revenue'

with_measures() also checked measure lambdas against the raw table, so a derived dimension named close to a column (url_s vs url) could be reported as a typo.

Fix

  • SemanticAggregateOp.to_untagged(), single-model path only: after the aggregation plan is built, _mutate_measure_dimension_deps probes each requested base measure and calc measure. When one reads a derived dimension the table lacks, that dimension and its dependencies are materialized. This also covers inline reductions inside calc measures (t.revenue.sum() / t.all(t.revenue.sum())).
  • SemanticModel.with_measures(), single models only: measures are classified against the table with derived dimensions added (best-effort, never overwriting existing columns).
  • Safety rules found in review:
    • A column named like a measure is never added, directly or through a dimension's dependencies. Calc scopes resolve columns before measures, so the column would hide the measure.
    • Retrying stops when materializing makes no progress (dotted dimension names used to loop forever).

Scope note: joins are unchanged

Four rounds of independent review found that bare-name dimension resolution on joined models has its own problems: the owning model vs a same-named dimension or raw column on another model, wrapper measures, and dependency chains. The early versions of this PR got some of those wrong, including silently wrong numbers in one case. So join paths (_compile_preagg, _compile_deferred, and the join branch of to_untagged) behave exactly as on main. Separately, #314 tracks a calc measure mixing an inline reduction with a measure reference, which fails on main even with raw columns.

Two more notes on joins:

  • A model whose relation contains a join (including join.as_table() and models built on a raw ibis join) is also treated as joined.
  • One known difference from main: four join_many inline-calc cases that already fail on main now raise ValueError instead of AttributeError, because leaf-model classification changed. No successful result changes.

Independent review: 6 rounds with gpt-6-astra; round 6 approved. Join compiler files are byte-identical to main, and 77 paired join cases were compared against main.

Tests

tests/test_measure_derived_dimensions.py has 12 single-model cases: grouped by another dimension, ungrouped, chained derived dimensions, calc measure on top, inline-reduction calc, filter, query() API, the url_s near-name case, a measure and a dimension with the same name (direct and through a dependency, at definition and at query time), and termination on dotted names.

  • Full suite: 1801 passed. The only 2 failures are test_chart_png_format_plotly, which needs headless Chrome (missing on the dev VM; passes in CI).
  • All 18 example scripts pass against the current source.

🤖 Generated with Claude Code

boringdata and others added 3 commits October 25, 2026 19:51
Query compilation only materialized the group-by keys, so a measure over a
derived dimension (total_revenue = t.revenue.sum()) raised AttributeError
whenever the query grouped by something else or not at all. Materialize
the derived dimensions each requested measure reads, on the single-table,
joined and join_many pre-aggregation paths.

with_measures() also classified lambdas against the raw table, so a
derived dimension named close to a column (url_s vs url) was reported as
a typo. Classify against the table enriched with derived dimensions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- with_measures: don't add a dimension named like a measure to the
  classification table; calc scopes resolve columns first, so it hid the
  measure and turned calc measures into base ones.
- Resolve the bare dimension name a measure reads against the measure's
  own model first, so a same-named dimension on another joined model no
  longer blocks it.
- Apply the same materialization on the deferred-join path.
- Probe calc measures too, so inline reductions over derived dimensions
  (t.revenue.sum() / t.all(t.revenue.sum())) resolve.
- Stop retrying when materializing makes no progress (dotted dimension
  names used to loop forever).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- with_measures: also skip dimensions whose dependency chain would add a
  column named like a measure.
- On joins, alias the owning model's dimensions under their bare names
  (owner taken from the measure's prefix, or from its unique qualified
  name when requested by short name), so the model wins over a
  same-named dimension elsewhere, through dependency chains too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
boringdata and others added 4 commits November 11, 2026 06:29
- When a measure reads its own model's dimension through a name that is
  a raw column of another joined model, raise a clear error instead of
  silently reading the other model's column.
- Never materialize a measure-named column at query time, including
  through a dimension's dependencies.
- Materialize derived dimensions for measures defined on the join
  itself (pre-aggregation wrapper path).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Four review rounds showed that bare-name dimension resolution on joined
models has edge cases beyond #310 (ownership, foreign columns, wrapper
measures). Restore main's behaviour for joins and keep the fix to the
single-model query and with_measures paths that the issue reports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
as_table() flattens a SemanticJoin into a plain model without
_source_join, so the single-model guard let derived-dimension
materialization run on a joined table and read another model's raw
column. Treat any model whose relation contains a join as joined.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hachej
hachej merged commit 367c1ff into main Nov 11, 2026
9 checks passed
@hachej
hachej deleted the fix/issue-310-measure-derived-dims branch November 11, 2026 07:14
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.

Measures referencing derived dimensions fail at query time (and at definition time on close column names)

2 participants