Skip to content

Keep same-name tables physical and compose semantic relations - #412

Merged
nicosuave merged 4 commits into
mainfrom
duckdb-ext-routing
Oct 7, 2026
Merged

nicosuave merged 4 commits into
mainfrom
duckdb-ext-routing

Conversation

@nicosuave

Copy link
Copy Markdown
Member

Fixes DuckDB extension routing found while reviewing the extension, adds semantic.<model> composition, makes day-and-coarser time grains DATE in both engines, and tightens extension ergonomics.

Routing (same-name tables)

Loading a model named like its table (the README's name: orders, table: orders) previously changed ordinary SQL: SELECT * FROM orders WHERE ... returned semantic rows, SELECT orders.status FROM orders returned distinct statuses, and raw-column queries failed.

  • A query keeps its native meaning when every reference binds to the table, including SELECT * and raw columns. A field only the model provides (metric, time grain, model-only dimension) makes it semantic. Mixing table-only columns and model-only fields through one name is an error that points at semantic.orders / main.orders.
  • In the DuckDB host, schema-qualified tables (main.orders) are physical. This is an opt-in on the Rust QueryRewriter; policy-enforcing hosts keep the default where qualifiers never bypass semantic compilation.
  • Physical subqueries inside a semantic statement stay physical.

semantic.<model> composition

Each reference becomes a derived table grouped by the model dimensions the surrounding query uses (SELECT, GROUP BY, ORDER BY, join conditions, USING). DuckDB keeps ownership of the outer query, so joins with physical tables, outer aggregates, DESCRIBE and SUMMARIZE work.

  • Dimension-only WHERE predicates on a non-NULL-extended relation filter before grouping.
  • Re-aggregating a metric is allowed when groups hold one relation row or the aggregation composes (sum of sum/count, min of min, max of max); otherwise it is rejected. count(*) over a relation with no fields is rejected.

Time grains

Day, week, month, quarter and year buckets are DATE on DuckDB, PostgreSQL and Redshift in both the Python and Rust engines, including same-grain pre-aggregation reads. Hour and finer stay timestamps; other dialects are unchanged. The Python planner keeps the TIMESTAMP type of a user-written DATE_TRUNC it folds into a semantic grain, so optimized results still match the written SQL. Test expectations that encoded the old type were updated.

Extension ergonomics

  • Unknown fields list close matches and the model's fields; unknown time grains list valid grains; unknown semantic.<model> names suggest models.
  • Error text no longer stacks Error: ... Validation error: labels.
  • SHOW definitions omit null metadata, and data_type falls back to the type the field produces (as information_schema reports).
  • Definition statements and file loads report what they created, replaced or dropped.
  • Compact model blocks reject several declarations on one line instead of creating an empty model.
  • README updated for routing, composition and preview builds.

DuckDB 2.0 alpha

New CMake probes (BOUND_TABLE_FUNCTION, GRAMMAR_EXTENSION_LIST) let the extension build against v2.0.0-alpha41489 as well as the pinned CI commit. A signed preview for that runtime (macOS arm64) is published to https://extensions.sidequery.dev.

Routing: a query whose references all bind to a same-name table keeps its
native SQL meaning. A model-only field makes it semantic, mixing table-only
columns and model-only fields through one name is an error, and in the DuckDB
host schema-qualified tables such as main.orders are never models.

semantic.<model> references compile to derived tables grouped by the
dimensions the surrounding query uses, so they join, filter, aggregate and
describe like views. Dimension-only predicates filter before grouping;
non-composable re-aggregation and grainless relations are rejected.

Day and coarser time buckets are DATE on DuckDB, PostgreSQL and Redshift in
both engines, including same-grain rollup reads. The planner keeps the
TIMESTAMP type of a user-written DATE_TRUNC it folds into a semantic grain.

Extension ergonomics: unknown fields and grains suggest close matches,
error labels no longer stack, SHOW reports bound data types and omits null
metadata, and definitions report what they changed. Compact model blocks
reject several declarations on one line instead of creating an empty model.
The extension also builds against DuckDB v2.0.0-alpha41489.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-07T01:23:28.729687Z 51e63ef 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: 51e63ef9a3

ℹ️ 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 thread sidemantic/sql/query_rewriter.py Outdated
Comment on lines +3252 to +3256
expression.alias
for expression in query.expressions
if isinstance(expression, exp.Alias)
and isinstance(expression.this, exp.TimestampTrunc)
and self._timestamp_trunc_unit(expression.this) in DATE_GRANULARITIES

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 Preserve types for unaliased DATE_TRUNC projections

When a DuckDB/Postgres/Redshift query projects DATE_TRUNC without an explicit alias, such as SELECT DATE_TRUNC('month', order_date) ..., this set remains empty because the projection is an exp.TimestampTrunc, not an exp.Alias. The optimized semantic grain therefore retains the new DATE type instead of being cast back to the TIMESTAMP type returned by the written SQL, changing result metadata and potentially downstream behavior. Include direct TimestampTrunc projections using their generated output name.

Useful? React with 👍 / 👎.

Comment on lines +944 to +947
for (auto &column : join.using_columns) {
for (auto &relation : found) {
auto field = SidemanticName(column);
if (CanonicalField(relation.model, field)) AddField(relation, field);

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 Restrict USING fields to the joined subtrees

For a nested join, each USING clause currently adds its column to every semantic relation in the entire SELECT, including relations outside that join's operands. For example, in semantic.orders o CROSS JOIN (semantic.returns r JOIN targets t USING (status)), status is also added to o when both models expose that dimension, so orders is compiled at an unintended finer grain and can emit extra rows or multiply results. Track the semantic relations under the current join's left and right subtrees rather than iterating over the global found list.

Useful? React with 👍 / 👎.

The planner now records the dimensions it folds from a written DATE_TRUNC,
so their TIMESTAMP output type is kept whether or not the projection has an
alias.

In the DuckDB extension, a JOIN ... USING column adds to the grain of only
the semantic relations under that join, not every relation in the SELECT.
An unaliased outer expression is named by the database after its SQL text,
so folding it into a semantic grain or metric renamed the column (for
example sum(revenue) became revenue). The planner now keeps the written
query for such projections.

Remove the committed clangd compile_commands.json indexes and ignore them.
A PyPI connection timeout during uv run failed the DuckDB extension host
acceptance step after uv's default retries. Raise the retry count and
per-request timeout for every job that sets up uv.
@nicosuave
nicosuave merged commit 1528257 into main Oct 7, 2026
48 checks passed
@nicosuave
nicosuave deleted the duckdb-ext-routing branch October 7, 2026 13:34
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