Repository navigation
Keep same-name tables physical and compose semantic relations - #412
Conversation
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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| 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 |
There was a problem hiding this comment.
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 👍 / 👎.
| for (auto &column : join.using_columns) { | ||
| for (auto &relation : found) { | ||
| auto field = SidemanticName(column); | ||
| if (CanonicalField(relation.model, field)) AddField(relation, field); |
There was a problem hiding this comment.
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.
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 ordersreturned distinct statuses, and raw-column queries failed.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 atsemantic.orders/main.orders.main.orders) are physical. This is an opt-in on the RustQueryRewriter; policy-enforcing hosts keep the default where qualifiers never bypass semantic compilation.semantic.<model>compositionEach 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,
DESCRIBEandSUMMARIZEwork.WHEREpredicates on a non-NULL-extended relation filter before grouping.count(*)over a relation with no fields is rejected.Time grains
Day, week, month, quarter and year buckets are
DATEon 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 theTIMESTAMPtype of a user-writtenDATE_TRUNCit folds into a semantic grain, so optimized results still match the written SQL. Test expectations that encoded the old type were updated.Extension ergonomics
semantic.<model>names suggest models.Error: ... Validation error:labels.SHOWdefinitions omit null metadata, anddata_typefalls back to the type the field produces (asinformation_schemareports).DuckDB 2.0 alpha
New CMake probes (
BOUND_TABLE_FUNCTION,GRAMMAR_EXTENSION_LIST) let the extension build againstv2.0.0-alpha41489as well as the pinned CI commit. A signed preview for that runtime (macOS arm64) is published tohttps://extensions.sidequery.dev.