Skip to content

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

Merged
christianeu-db merged 7 commits into
apache:mainfrom
ekut:wide-table-lowering
Nov 24, 2026
Merged

christianeu-db merged 7 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 each of the 46 wide table cases becomes an .ost script, which replaces the case's folder. For example, compliance/wide_table/a05_groups_from_orders.ost runs the query of the case and expects the lowering, then the rows of the result:

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.
  • skip(reason) does not run the next command, and keeps its expected output as written, for a case that ost cannot run yet. The reason is the URL of the issue that tracks the gap.
  • 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:

  • ost -d compliance compliance/query compliance/wide_table passes all 78 scripts, with the five skips. The 25 error cases give the codes of the former expected.txt.
  • For a12, the lowering run as plain SQL on the retail data returns the rows of its script. For a16, a19, a20 and a21, a script renamed the column d_year to year and set the field's expression to year, and executeLower then returned the rows of each script. Every join in them goes from the many side to a primary key, so no row repeats.
  • In ost, uv run pytest (179 tests) and ost tests/scripts pass, and so do make compliance-validate and dev/check-license.

cc @christianeu-db @jbonofre

@github-actions github-actions Bot added the infra label Nov 23, 2026
@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.

Comment thread ost/src/ossie_ost/lowering.py Outdated
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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in b5c831e: the kinds are now dataset_metric and model_metric.

Comment thread compliance/wide_table/README.md Outdated
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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. "Comparing results" now says that types are not compared, so 350.00 from an implementation matches 350, and that ost compares the text of each cell, so a script that expects 350.00 in a05 fails.

Comment thread ost/src/ossie_ost/lowering.py Outdated
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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in b5c831e. The check skips only the function of the window, with _is_windowed as in validate.py, and your filter now gives E_AGGREGATE_IN_WHERE. lowering.ost also covers an aggregate in PARTITION BY and in the argument of the windowed function, and FILTER before OVER.

Comment thread compliance/wide_table/README.md Outdated
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. ost has skip(reason) now, documented in ost/docs/ost.md: it does not run the next command and keeps its expected output as written. The five cases are scripts, each after a skip with its issue: #813 for a12, and #814 for a16, a19, a20 and a21. Their expected output is the lowering and the rows of the case, so a fix only has to remove the skip. A unit test checks that each skip in compliance/ names an issue by its URL.

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.

Thanks, this works well. One small thing: the README still names the five skipped cases and their gaps, and that list goes stale as #813 and #814 land. Each skip already names its issue, so could that paragraph end at "the script passes."?

That can be a followup PR

Comment thread compliance/wide_table/README.md Outdated
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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done: cases/ is gone, and each script is the only copy of its case. That includes query.ossie.sql and cases/README.md, since each script carries the lowering and the description of the case.

ekut added 6 commits November 24, 2026 10:43
…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.
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.
skip(reason) makes ost not run the next command, and keep its expected
output as written, so a script can hold a test whose feature is missing,
with the output that it should produce. The reason, typically the URL
of the issue that tracks the gap, must not be empty; the fix removes the
skip.
…es under a window

A reference to a dataset-scoped metric has kind dataset_metric instead
of metric, after the two scopes of the core specification, so the
checks that reject other kinds no longer read as rejecting non-metrics.

The check for an aggregate in a filter (Section 8, row 7) skipped every
aggregate under a window. It now skips only the window's own function,
as unwindowed_aggregates in validate.py does, so an aggregate in the
window's ORDER BY or PARTITION BY, or in an argument of the windowed
function, raises E_AGGREGATE_IN_WHERE instead of E_WINDOW_IN_WHERE.
Each of the 46 cases is now a script, and the scripts are the only
copy of the cases: the case folders, with query.json, expected.txt,
query.ossie.sql, and cases/README.md, are removed. Each script carries
the query, the lowering, the expected result, and the description of
the case.

The 25 error cases expect the error of the ost lowering; README.md says
that a case requires only its code. Cases a12, a16, a19, a20, and a21
are skipped, each with the issue of the view expander that it waits on;
their expected output is the lowering and the rows of the case.
…me an issue

skip now fails, and skips nothing, if no command follows it, and its
reason must have a character other than a space; ost.md says so, and
that the next command is the next one after blank lines and comments,
even another skip. A unit test checks that each skip in a compliance
script names an issue of apache/ossie by its URL.

The comment on the aggregate-under-a-window tests names rows 6 and 7
of Section 8, not 7 and 8.
a12 waits on apache#813, and a16, a19, a20, and a21 on apache#814. The check of the
skips reads the scripts, not the regenerated copies under target/.

@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 LGTM, thanks! I replied on the README thread about the skip list; that's fine as a follow-up

@christianeu-db
christianeu-db merged commit 3876f8e into apache:main Nov 24, 2026
17 of 19 checks passed
christianeu-db added a commit to christianeu-db/ossie that referenced this pull request Nov 25, 2026
A step of the Layer 3 lowering now crosses a relationship from `from` to
`to` if its cardinality is ManyToOne, the default, or OneToOne, and from
`to` to `from` if it is OneToMany or OneToOne (Section 4). The join
condition starts from the dataset where the step starts.

retail_model declares customers_to_visits from customers, as OneToMany,
so a03 crosses it from visits to customers and lowers as before. a22
groups visits by customer region, and e26 checks that customers does not
reach visits. With the lowering of apache#803, a22 raises E_NO_PATH and e26
lowers to one row per visit.

Co-authored-by: Isaac <no-reply@databricks.com>
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