Skip to content

Conversation

@shangyian
Copy link
Collaborator

@shangyian shangyian commented Jan 15, 2026

Summary

This PR fixes dimension replacement in derived metric expressions and outputs metric combiners in Druid dialect. It also refactors the build_v3 SQL generation to use a cleaner shared architecture.

Fix: Auto-detect dimensions from metric expressions

Metrics with expressions that referenced dimensions (e.g., LAG(...) OVER (ORDER BY week)) failed to generate correct SQL because the dimension in the ORDER BY clause wasn't being replaced with its alias.

This change adds add_dimensions_from_metric_expressions() to scan combiner ASTs for dimension references and add them to ctx.dimensions before SQL generation.

Fix: Output metric_combiners in Druid dialect

The metric_expression stored in materialization config was output in Spark dialect, but since it is being materialized to Druid, most tools consuming it will need it to be in the Druid dialect.

This change updates combiners.py to use render_for_dialect(...) when generating metric_combiners.

Refactor: Clean shared architecture for SQL generation

Before this change, build_measures_sql and build_metrics_sql had duplicated setup code. The cube path and non-cube path were parallel implementations.

With this change, here is the new setup:

      build_measures_sql()                         build_metrics_sql()
               │                                            │
               ▼                                            ▼
      setup_build_context()                        setup_build_context()
               │                                            │
               ▼                                            ▼
      ┌───────────────────┐                        ┌───────────────────┐
      │   BuildContext    │                        │   BuildContext    │
      │  .metric_groups   │                        │  .metric_groups   │
      │  .decomposed_metrics                       │  .decomposed_metrics
      └───────────────────┘                        └───────────────────┘
               │                                            │
               ▼                                   ┌────────┴────────┐
      build_grain_groups()                         │                 │
               │                                   ▼                 ▼
               ▼                              if cube:           else:
      return MeasuresSQL                      build_sql_     build_grain_groups()
                                              from_cube_impl()      │
                                                   │                │
                                                   └────────┬───────┘
                                                            │
                                                            ▼
                                                   compute_metric_layers()
                                                   generate_metrics_sql()
                                                            │
                                                            ▼
                                                     return GeneratedSQL

Test Plan

  • PR has an associated issue: #
  • make check passes
  • make test shows 100% unit test coverage

Deployment Plan

@netlify
Copy link

netlify bot commented Jan 15, 2026

Deploy Preview for thriving-cassata-78ae72 canceled.

Name Link
🔨 Latest commit a794455
🔍 Latest deploy log https://app.netlify.com/projects/thriving-cassata-78ae72/deploys/696a4bc08638e7000822f7eb

@shangyian shangyian force-pushed the dim-replace-druid-dialect branch from e821fc5 to c21458a Compare January 16, 2026 14:21
…sted dimensions, and automatically create mappings for them. This enables metrics with dimension references to work even if the dimension reference was not included in the query request
…ilding context (nodes, decomposition into metric groups etc) and branch off into own flows.
@shangyian shangyian force-pushed the dim-replace-druid-dialect branch from c21458a to a794455 Compare January 16, 2026 14:31
@shangyian shangyian changed the title Dim replace druid dialect Fix metric expr dimensions + Druid dialect; refactor build_v3 shared architecture Jan 16, 2026
@shangyian shangyian marked this pull request as ready for review January 16, 2026 14:57
@shangyian shangyian merged commit 595a8be into DataJunction:main Jan 16, 2026
17 checks passed
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