Skip to content

Implement semantic SQL rewrite planner#174

Merged
nicosuave merged 4 commits into
mainfrom
semantic-sql-planner
May 23, 2026
Merged

Implement semantic SQL rewrite planner#174
nicosuave merged 4 commits into
mainfrom
semantic-sql-planner

Conversation

@nicosuave

@nicosuave nicosuave commented May 23, 2026

Copy link
Copy Markdown
Member

Implements the semantic SQL rewrite planner and optimizer coverage in this PR: explainable rewrite plans, semantic island optimization, slicer/dropdown rewrites, additive totals/subtotals, Top-N variants, Superset-style wrapper pushdown, conditional aggregate pivots, safe time-grain expression recovery, set-operation branch optimization, and preaggregation route selection support.

Adds BI corpus accepted/rejected coverage, benchmark proof cases, and expanded explain/SQL-shape/execution-equivalence assertions. Also fixes related projection-order and cross-model subquery behavior found during validation.

Validated locally with ruff check, ruff format --check, and pytest -v.

@nicosuave nicosuave changed the title Implement semantic SQL rewrite shapes Implement semantic SQL rewrite planner May 23, 2026
@nicosuave nicosuave force-pushed the semantic-sql-planner branch from 6a0e12b to 89dfba8 Compare May 23, 2026 18:02
@nicosuave nicosuave marked this pull request as ready for review May 23, 2026 18:04

@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: 89dfba82c1

ℹ️ 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 +4631 to +4633
if dim_model_name == metric_model_name:
column_expr = f"{rollup_alias}.{self._quote_identifier(dim_name)}"
else:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Read local time dims from pre-agg grain column

When join-key preaggregation is selected and the query includes a local time dimension (for example orders.order_date__month), this branch always reads orders_rollup.order_date before applying DATE_TRUNC. Pre-aggregation tables store time dimensions as grain-specific columns (e.g. order_date_day), so the generated SQL references a non-existent column and fails at execution for these queries. This affects any join-key preaggregation route with time-grained local dimensions while use_preaggregations is enabled.

Useful? React with 👍 / 👎.

@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: cf3cc4fe74

ℹ️ 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 +4639 to +4642
column_name = dim_name
if granularity and preagg.time_dimension == dim_name and preagg.granularity:
column_name = f"{dim_name}_{preagg.granularity}"
column_expr = f"{rollup_alias}.{self._quote_identifier(column_name)}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Read ungrained local time dims from rollup grain column

When join-key preaggregation is selected and a query projects a local time dimension without an explicit __granularity suffix (for example orders.order_date), this code keeps column_name = dim_name and emits orders_rollup.order_date. Pre-aggregation tables only materialize the grain column (for example order_date_day), so these valid queries fail at execution with an unknown-column error whenever a time-grained local dimension is requested alongside remote dimensions.

Useful? React with 👍 / 👎.

Comment thread sidemantic/sql/generator.py Outdated
Comment on lines +4695 to +4696
LEFT JOIN {remote_model.table} AS {remote_alias}
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.

P1 Badge Join remote model through semantic SQL source

This join always targets remote_model.table, which skips remote_model.sql when the remote model is defined by a SQL subquery/view. In that configuration, join-key preaggregation rewrites no longer apply the remote model’s semantic source logic (such as row filters or derived source columns), so optimized results can diverge from normal semantic queries and may include rows that should be excluded.

Useful? React with 👍 / 👎.

@nicosuave nicosuave merged commit 93a65df into main May 23, 2026
16 checks passed
@nicosuave nicosuave deleted the semantic-sql-planner branch May 23, 2026 19: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