Skip to content

feat(ost): lower Layer 3 queries, and run the wide table cases as scripts - #803

Open
ekut wants to merge 2 commits into
apache:mainfrom
ekut:wide-table-lowering
Open

ekut wants to merge 2 commits into
apache:mainfrom
ekut:wide-table-lowering

Conversation

@ekut

@ekut ekut commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #716: steps (3) and (4) of the next steps in the review, and the first of the two PRs planned in #707.

ost gets a reference lowering of Layer 3 queries to Layer 2 queries, and 16 of the wide table cases become .ost scripts. For example, compliance/wide_table/a05_groups_from_orders.ost runs its query.json and expects the lowering, then the rows of its expected.txt:

executeLower("retail_model", {
  "dimensions": ["customers.region"],
  "metrics": ["orders.total_revenue"]
});
> {_|
SELECT
  customers.region,
  AGG(orders.total_revenue) AS total_revenue
FROM retail_model.orders
LEFT JOIN retail_model.customers
  ON orders.customer_id = customers.customer_id
GROUP BY
  customers.region
|_} : string
+--------+---------------+
| region | total_revenue |
+--------+---------------+
| EAST   |           350 |
| WEST   |           300 |
+--------+---------------+

The lowering is in ost/src/ossie_ost/lowering.py and follows core-spec/wide_table_semantics.md:

  • It checks the query against the errors of Section 8, in the order of the table. An error message starts with the code, such as E_NO_PATH: metric ....
  • Each home dataset is a branch, with a LEFT JOIN for each step of each path (Sections 4 and 5.4).
  • Several branches meet on the keys of Section 5.5, in CTEs b_<home dataset> and keys. A component whose empty-input value is not NULL, such as COUNT(*), takes it where its branch has no row, through a present column. Without dimensions, the branches are cross joined.
  • A query without metrics reads its anchor (Section 5.6).

New in ost, documented in ost/docs/ost.md:

  • lower(model, query) returns the Layer 2 query, and executeLower(model, query) also expands and executes it. The query is the JSON of Section 3.7, written as the last argument.
  • connect("retail") and connect("stores") load the data of compliance/wide_table. A unit test checks that the copies match.
  • make compliance-wide-table and the ost CI workflow run the scripts.
  • The expander's _expression is now expression_text, because the lowering reads metric expressions too.

Points for reviewers:

Checks:

  • All 46 cases through executeLower: the 25 error cases give their code, and the 16 scripted cases give the rows of expected.txt.
  • For a12, a16, a19, a20 and a21, a script took the output of lower, replaced each measure, field and dataset with its expression or source table, and ran it on DuckDB. All five return the rows of expected.txt. Every join in them goes from the many side to a primary key, so no row repeats.
  • In ost, uv run pytest (175 tests) and ost tests/scripts pass. ost -d compliance compliance/query compliance/wide_table passes all 48 scripts, and dev/check-license passes.

cc @christianeu-db @jbonofre

…ipts

lower and executeLower take a query of the Wide Table Query Interface
in the JSON binding of Section 3.7 and lower it to the Layer 2 query
of Section 5: a branch for each home dataset, with a LEFT JOIN for
each step of each path, combined on the keys of Section 5.5.
executeLower expands the lowering with the view expander and executes
it. A query that raises an error of Section 8 gives an error that
starts with its code, checked in the order of that table.

Each component of a model-scoped metric that is not AGG(dataset.metric)
becomes a measure of its home dataset with a name of its own, such as
c1, so that the lowering evaluates it with AGG(), as Section 5.2 says.
A comment at the start of the lowering defines each such measure.

connect loads two new fixtures, retail and stores, with the data of
compliance/wide_table; a unit test checks that they match. 16 of the
21 wide table cases that return rows are now scripts,
compliance/wide_table/<case>.ost. The view expander does not support
yet the lowering of the other five: a join without measures (a12) and
a field whose expression is not its name (a16, a19 to a21).

The expander's _expression becomes expression_text, which the
lowering also uses.
@github-actions github-actions Bot added the infra label Nov 23, 2026
A filter with an unterminated string, such as region = 'EAST, made
sqlglot raise TokenError, which is not a ParseError, so the lowering
failed with the tokenizer's message instead of E_INVALID_QUERY. The
lowering now catches every sqlglot error when it parses a filter or a
metric's expression.

The lowering also rejects AGG with a FILTER clause, which the core
specification does not allow (Metric Scoping, rule 6), instead of
dropping the clause. And _parseexpression_text, a slip of the rename
of _expression, becomes _parse_expression.
@ekut
ekut marked this pull request as ready for review November 23, 2026 01:59

@christianeu-db christianeu-db left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ekut this looks great and very close to merge - thanks for putting it together so quickly!

I left a few inline comments, but the main things I'd like to see before merging are:

Remove the old .json/.text fixtures so the .ost cases are the single source of truth.

Generate test files for all .ost cases, allowing unsupported cases to be skipped with a link to a tracking issue. The issues can be as simple as "Unskip test case XX."

That way, we have the full test corpus in place, with clear follow-ups for the remaining gaps.

return Ref("field", dataset, f.name)
for m in dataset.metrics or []:
if normalize_name(m.name) == name:
return Ref("metric", dataset, m.name, m)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: kind == "metric" means a dataset-scoped metric only, so the checks in _metric that reject every other kind read as rejecting non-metrics, though they also reject model-scoped ones. Could the kinds be dataset_metric and model_metric, after the spec's two scopes?

Comment on lines +190 to +191
- The error cases as scripts. `ost` compares an error by its message, and the
cases check only the code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: this drops the caveat that ost compares cell text exactly, but "Comparing results" still says 350.00 matches 350, and a script with 350.00 in a05 fails. Could that section say the scripts compare text?

return None
text = self.query.filter_text
for node in f.walk():
if isinstance(node, exp.AggFunc) and not node.find_ancestor(exp.Window):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not a blocker: this skips every aggregate under a window, not only the window's own function, so an aggregate in its ORDER BY gives row 7 instead of E_AGGREGATE_IN_WHERE:

"filter": "ROW_NUMBER() OVER (ORDER BY SUM(orders.amount)) = 1"
> error: E_WINDOW_IN_WHERE: filter has a window function: ...

Could it skip only the call that the window applies to, as unwindowed_aggregates in validate.py does?

2. The expected result: the rows of `expected.txt`.

Cases `a12`, `a16`, `a19`, `a20`, and `a21` have no script yet, because the
view expander does not support their lowering: `a12` joins two datasets

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we open an issue for each of these (a join without a measure, and a field whose expression differs from its name) and link them in the PR? That would give the five pending cases something concrete to track against.

I've opened a few issues against the expander and have seen really fast community turnarounds so far. Perhaps we could follow a similar pattern here:

  1. Add a skip annotation to the .ost format and generate the test files for all five cases.
  2. Skipped cases remain in the test suite but aren't executed.
  3. Each skip includes an issue ID, so contributors can find and pick up the work.
  4. The exit criterion for closing an issue is a PR that removes the skip and gets the test passing.

That gives us a complete set of test cases, makes the remaining work easy to discover, and avoids maintaining a separate list of exceptions in the README.

cases check only the code.
- Scripts for `a12`, `a16`, `a19`, `a20`, and `a21`, when the view expander
supports their lowering.
- One file per case. The scripts repeat `query.json` and `expected.txt`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we remove the old json / expected as part of thsi PR (and this line in the readme). Then, we have a single source of truth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants