Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/compatibility/directed-relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ An automatic query anchor must reach the selected models through permitted
paths; an explicitly chosen invalid anchor is rejected. The same direction
restriction applies to junction paths and nested relationship roles.

The automatic anchor is the model that owns the selected metrics. When that model
cannot reach every selected model through permitted paths, the first selected
model that can do so anchors the query instead. A metric on `customers` grouped
by an `orders` dimension therefore anchors on `orders` here, because nothing may
traverse from `customers` to `orders`. If another permitted route exists, such as
`customers -> accounts -> orders`, the metric owner anchors and that route is the
one joined; name the anchor with an Explore to choose the population yourself.

Aggregate deduplication still accounts for repeated target rows. Queries with
metrics at different grains use preaggregation when either a many-to-one or
one-to-many path can repeat a metric's rows, including forward-only paths.
Expand Down
66 changes: 66 additions & 0 deletions docs/native-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,72 @@ Relationship types:
- `one_to_many`
- `many_to_many`

### Which rows a joined query keeps

The model that owns the selected metrics is the preserved side of the join. Models
that only supply dimensions are `LEFT JOIN`ed onto it, across every hop:

- A metric row whose key is `NULL` or matches no dimension row is counted in a
`NULL` dimension group, so a breakdown adds up to the ungrouped total.
- A dimension row that no metric row references is not a group. `orders.revenue`
by `customers.region` lists no region without orders.
- A filter or segment on a dimension model's column keeps only metric rows that
have a matching dimension row, so it removes the `NULL` group. That includes
`IS NULL` predicates; filter on the metric model's own key column to inspect
unmatched rows.
- The same holds when the metric sits on the "one" side: `customers.customer_count`
by `orders.status` keeps every customer once, with customers without orders in a
`NULL` status group.

Metrics from several models keep each model's own rows, so their groups are the
union of those populations. A query with dimensions and no metric starts from its
first dimension's model.

To list every dimension member, including those without metric rows, either query
through an Explore whose base `model` is the dimension model, or also select a
metric owned by that model:

```yaml
models:
- name: customers
table: customers
primary_key: id
dimensions:
- name: region
type: categorical
metrics:
- name: customer_count
agg: count
- name: orders
table: orders
primary_key: id
relationships:
- name: customers
type: many_to_one
foreign_key: customer_id
metrics:
- name: revenue
agg: sum
sql: amount
- name: order_count
agg: count

explores:
- name: by_customer
model: customers
```

```bash
# Every customer region; regions without orders have NULL revenue and a zero count.
sidemantic query --explore by_customer --metric orders.revenue --metric orders.order_count --dimension region
# Both populations: regions without orders and orders without a region.
sidemantic query --metric orders.revenue --metric customers.customer_count --dimension customers.region
```

Releases through 0.13.1 anchored a single-model metric query on its first
dimension's model instead: it listed dimension members without metric rows and
dropped metric rows without a matching dimension row.

Composite relationship:

```yaml
Expand Down
16 changes: 13 additions & 3 deletions docs/semantic-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,20 @@ Derived and ratio metrics reconstruct their source-local aggregate dependencies;
metric predicates become HAVING over those reconstructed states. Empty count
populations return zero, while nullable sums and averages remain null.

The model that owns the selected metrics anchors an ordinary query in both
engines: dimension models are LEFT JOINed onto it, metric rows without a matching
dimension row form a NULL group, and dimension members without metric rows are
not groups. An Explore's base model replaces that anchor; see
[Which rows a joined query keeps](native-format.md#which-rows-a-joined-query-keeps).

One-hop many-to-one dimension queries can route through a rollup carrying every
join key, including composite keys. The remote dimension model remains the query
domain, so members with no facts retain zero counts. Explicit anchors requiring a
different domain, explicit join kinds, colliding output aliases, uncovered join keys, multi-hop or fanout joins and row filters on
join key, including composite keys. The rollup stands in for the metric model and
is the preserved side, so stored groups whose join key is NULL or unmatched stay
in a NULL dimension group, exactly as in raw planning. Dimension expressions of a
joined model apply only to rows that have a matching dimension row: an expression
such as `COALESCE(name, 'unknown')` leaves unmatched metric rows in the NULL group
on both routes and in both engines. An explicit anchor on
another model, explicit join kinds, colliding output aliases, uncovered join keys, multi-hop or fanout joins and row filters on
this join route use ordinary source planning. Compatible stored dimensions and
time grains retain their existing routing checks: bucketed timestamps cannot serve
finer grains or untruncated timestamps, and week buckets cannot serve months.
Expand Down
4 changes: 2 additions & 2 deletions scripts/benchmark_semantic_sql_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,8 @@ def _cases(layer: SemanticLayer) -> list[BenchmarkCase]:
expected_plan="join_key_preaggregation",
expected_rules=("join_key_preaggregation_route_selection",),
expected_sql_contains=(
"FROM customers AS customers",
"LEFT JOIN orders_preagg_by_customer AS orders_rollup",
"FROM orders_preagg_by_customer AS orders_rollup",
"FROM customers AS customers) AS customers",
),
forbidden_sql_contains="FROM orders\n",
min_speedup=1.2,
Expand Down
Loading
Loading