Repository navigation
test(compliance): add additional relational query (layer 2) query test cases - #628
Conversation
Add compliance/query/relational_semantics.sql, a DuckDB-runnable file of 29 test cases for core-spec/relational_semantics.md over the SCOTT schema. Each case pairs an Ossie query that uses AGG() (commented out) with an equivalent DuckDB query and its expected result. - Part A (12 cases): AGG() on one relation: grouping, filters, HAVING, NULL and empty-input values, ROLLUP, CTE propagation, subqueries. - Part B (17 cases): joins, including fan-out and many-to-many joins, and two queries run with each join type. The DuckDB queries use the reference evaluation strategy of Section 9. The model scott_model (commented out) uses dataset-scoped metrics (apache#591). The Ossie queries refer to its relations by two-part name, for example scott_model.emp. This is a light version of the .ost test format (apache#501) and view expander (apache#502). Co-authored-by: Isaac <no-reply@databricks.com>
1b4a613 to
a3dcba5
Compare
|
fyi: @julianhyde |
|
I checked the expected results against Section 8.1 by a second route. For B1-B17 I wrote each measure's input set out directly from 8.1, mostly as Two values depend on how "host dialect" in Section 8.1 is read:
Are the expected values in these cases meant for every engine, or for a host with standard SQL semantics? A1-A4, A6, A8, A10-A12, B1-B9, B13 and B14 also return the expected rows on ClickHouse 26.9 with default settings (translated by SQLGlot 30.24.0). That may help pick the first cases to merge. Queries for B1-B17-- B1: many-to-one, the join repeats no emp row
SELECT d.dname, SUM(e.sal), COUNT(*) FROM scott.emp e JOIN scott.dept d USING (deptno) GROUP BY 1 ORDER BY 1;
-- B2: many-to-one, the join repeats no emp row
SELECT e.job, SUM(e.sal), COUNT(*) FROM scott.emp e JOIN scott.dept d USING (deptno) WHERE d.loc = 'NEW YORK' GROUP BY 1 ORDER BY 1;
-- B3
SELECT e.job, SUM(e.sal) FROM scott.emp e WHERE e.deptno IN
(SELECT deptno FROM scott.emp GROUP BY deptno ORDER BY SUM(sal) DESC LIMIT 2) GROUP BY 1 ORDER BY 1;
-- B4
SELECT j.job,
(SELECT COUNT(*) FROM scott.dept d WHERE EXISTS (SELECT 1 FROM scott.emp e WHERE e.deptno = d.deptno AND e.job = j.job)),
(SELECT COUNT(*) FROM scott.emp e WHERE e.job = j.job AND EXISTS (SELECT 1 FROM scott.dept d WHERE d.deptno = e.deptno))
FROM (SELECT DISTINCT e.job FROM scott.emp e JOIN scott.dept d USING (deptno)) j ORDER BY 1;
-- B5
SELECT j.job,
(SELECT COUNT(*) FROM scott.emp e WHERE e.job = j.job AND EXISTS (SELECT 1 FROM scott.emp m WHERE e.mgr = m.empno)),
(SELECT COUNT(*) FROM scott.emp m WHERE EXISTS (SELECT 1 FROM scott.emp e WHERE e.mgr = m.empno AND e.job = j.job)),
(SELECT SUM(m.sal) FROM scott.emp m WHERE EXISTS (SELECT 1 FROM scott.emp e WHERE e.mgr = m.empno AND e.job = j.job))
FROM (SELECT DISTINCT e.job FROM scott.emp e JOIN scott.emp m ON e.mgr = m.empno) j ORDER BY 1;
-- B6
SELECT g.deptno,
(SELECT COUNT(*) FROM scott.emp e WHERE e.deptno = g.deptno AND EXISTS (SELECT 1 FROM scott.salgrade s WHERE e.sal BETWEEN s.losal AND s.hisal)),
(SELECT COUNT(*) FROM scott.salgrade s WHERE EXISTS (SELECT 1 FROM scott.emp e WHERE e.deptno = g.deptno AND e.sal BETWEEN s.losal AND s.hisal))
FROM (SELECT DISTINCT e.deptno FROM scott.emp e JOIN scott.salgrade s ON e.sal BETWEEN s.losal AND s.hisal) g ORDER BY 1;
-- B7
SELECT g.grade,
(SELECT COUNT(*) FROM scott.emp e WHERE e.sal BETWEEN g.losal AND g.hisal AND EXISTS (SELECT 1 FROM scott.dept d WHERE d.deptno = e.deptno)),
(SELECT SUM(e.sal) FROM scott.emp e WHERE e.sal BETWEEN g.losal AND g.hisal AND EXISTS (SELECT 1 FROM scott.dept d WHERE d.deptno = e.deptno)),
(SELECT COUNT(*) FROM scott.dept d WHERE EXISTS (SELECT 1 FROM scott.emp e WHERE e.deptno = d.deptno AND e.sal BETWEEN g.losal AND g.hisal))
FROM (SELECT DISTINCT s.grade, s.losal, s.hisal FROM scott.emp e JOIN scott.dept d USING (deptno) JOIN scott.salgrade s ON e.sal BETWEEN s.losal AND s.hisal) g ORDER BY 1;
-- B8: every emp row of the department appears on both sides
SELECT g.deptno,
(SELECT SUM(e.sal) FROM scott.emp e WHERE e.deptno = g.deptno),
(SELECT COUNT(*) FROM scott.emp c WHERE c.deptno = g.deptno)
FROM (SELECT DISTINCT e.deptno FROM scott.emp e JOIN scott.emp c ON e.deptno = c.deptno) g ORDER BY 1;
-- B9
SELECT (SELECT COUNT(*) FROM scott.emp e WHERE EXISTS (SELECT 1 FROM scott.emp m WHERE e.mgr = m.empno)),
(SELECT SUM(sal) FROM scott.emp e WHERE EXISTS (SELECT 1 FROM scott.emp m WHERE e.mgr = m.empno)),
(SELECT COUNT(*) FROM scott.emp m WHERE EXISTS (SELECT 1 FROM scott.emp e WHERE e.mgr = m.empno)),
(SELECT SUM(sal) FROM scott.emp m WHERE EXISTS (SELECT 1 FROM scott.emp e WHERE e.mgr = m.empno));
-- B10: every e row survives the LEFT JOIN
SELECT (SELECT COUNT(*) FROM scott.emp), (SELECT SUM(sal) FROM scott.emp),
(SELECT COUNT(*) FROM scott.emp m WHERE EXISTS (SELECT 1 FROM scott.emp e WHERE e.mgr = m.empno)),
(SELECT SUM(sal) FROM scott.emp m WHERE EXISTS (SELECT 1 FROM scott.emp e WHERE e.mgr = m.empno));
-- B11: every m row survives the RIGHT JOIN
SELECT (SELECT COUNT(*) FROM scott.emp e WHERE EXISTS (SELECT 1 FROM scott.emp m WHERE e.mgr = m.empno)),
(SELECT SUM(sal) FROM scott.emp e WHERE EXISTS (SELECT 1 FROM scott.emp m WHERE e.mgr = m.empno)),
(SELECT COUNT(*) FROM scott.emp), (SELECT SUM(sal) FROM scott.emp);
-- B12: every row of both sides survives the FULL JOIN
SELECT (SELECT COUNT(*) FROM scott.emp), (SELECT SUM(sal) FROM scott.emp), (SELECT COUNT(*) FROM scott.emp), (SELECT SUM(sal) FROM scott.emp);
-- B13: every row of both sides survives the CROSS JOIN
SELECT (SELECT COUNT(*) FROM scott.emp), (SELECT SUM(sal) FROM scott.emp), (SELECT COUNT(*) FROM scott.emp), (SELECT SUM(sal) FROM scott.emp);
-- B14
SELECT dc, ec, ss, ss / dc FROM (SELECT
(SELECT COUNT(*) FROM scott.dept d WHERE EXISTS (SELECT 1 FROM scott.emp e WHERE e.deptno = d.deptno)) dc,
(SELECT COUNT(*) FROM scott.emp e WHERE EXISTS (SELECT 1 FROM scott.dept d WHERE e.deptno = d.deptno)) ec,
(SELECT SUM(sal) FROM scott.emp e WHERE EXISTS (SELECT 1 FROM scott.dept d WHERE e.deptno = d.deptno)) ss);
-- B15: every dept row survives the LEFT JOIN
SELECT dc, ec, ss, ss / dc FROM (SELECT (SELECT COUNT(*) FROM scott.dept) dc,
(SELECT COUNT(*) FROM scott.emp e WHERE EXISTS (SELECT 1 FROM scott.dept d WHERE e.deptno = d.deptno)) ec,
(SELECT SUM(sal) FROM scott.emp e WHERE EXISTS (SELECT 1 FROM scott.dept d WHERE e.deptno = d.deptno)) ss);
-- B16: every dept row survives; only clerks match on the emp side
SELECT dc, ec, ss, ss / dc FROM (SELECT (SELECT COUNT(*) FROM scott.dept) dc,
(SELECT COUNT(*) FROM scott.emp e WHERE e.job = 'CLERK' AND EXISTS (SELECT 1 FROM scott.dept d WHERE e.deptno = d.deptno)) ec,
(SELECT SUM(sal) FROM scott.emp e WHERE e.job = 'CLERK' AND EXISTS (SELECT 1 FROM scott.dept d WHERE e.deptno = d.deptno)) ss);
-- B17: every dept row survives the LEFT JOIN
SELECT d.dname,
(SELECT COUNT(*) FROM scott.emp e WHERE e.deptno = d.deptno),
(SELECT SUM(sal) FROM scott.emp e WHERE e.deptno = d.deptno),
(SELECT AVG(sal) FROM scott.emp e WHERE e.deptno = d.deptno)
FROM scott.dept d ORDER BY 1; |
Hi @ekut, thanks for trying these out! I think we'd be aiming for standard SQL semantics as much as possible. So, for the empty result case, we'd be following ISO/IEC 9075 (SQL), which says empty sum / avg => null. The numeric point is an interesting one, esp. since a query optimization within the same engine can lead to slight differences due to floating point math. The most important thing for this suite is the query shapes. For numeric queries, we might need to work with simpler types (e.g. non-decimal) or accept some reasonable margin of error to account for precision |
|
I agree that we should be aiming for sql compliance when using the standard Ossie dialect. I can update the references to host language in #354 to make this clearer. We should update the expression language spec to clarify this as well. There will remain some gaps because the sql standard does not completely define function typing. For example, it allows |
| -- writes it (spec Section 11.3). | ||
| -- ============================================================================ | ||
|
|
||
| -- version: 0.2.0.dev0 |
There was a problem hiding this comment.
Is it the same as this file, in the final test suite, would this be a ost file that references the model yaml?
There was a problem hiding this comment.
yes - we'll be pulling the cases from this file into the that folder. once it's ready, we'll migrate these cases to the .ost infrastructure
| SELECT 'avg payroll per dept' AS metric, AVG(dept_sal) AS amount | ||
| FROM (SELECT deptno, SUM(sal) AS dept_sal |
There was a problem hiding this comment.
I believe further aggregations on top of AGG was one of the things discouraged in the proposal, remember seeing wording along the lines of "clients should avoid re aggregating...".
Isn't this query doing that? Should it be expressed differently in Ossie?
There was a problem hiding this comment.
There are a few distinct kinds of re-aggregation worth separating:
Re-aggregating results for efficiency. BI tools often compute aggregates in multiple places (e.g., on the server and locally) to minimize server queries. Pivot tables are a classic example: a tool might fetch cell-level aggregates and compute row/column subtotals locally by summing those results. This optimization is not generally safe for AGG(), since measures are not necessarily additive. This kind of re-aggregation is what the document warns against
Explicit two-pass aggregation. Sometimes the intended calculation genuinely requires multiple aggregation stages. For example, computing average regional sales requires first summing sales by region, then averaging those regional totals. SQL already supports this through subqueries or CTEs. We could eventually introduce first-class support for such calculations in Ossie, similar to Tableau's level-of-detail expressions.
| -- WITH staff AS ( | ||
| -- SELECT deptno, sum_sal AS payroll, emp_count AS headcount | ||
| -- FROM scott_model.emp | ||
| -- WHERE job IN ('CLERK', 'SALESMAN')) |
There was a problem hiding this comment.
- Is adding
jobin the group by another case that should be added? - Can we add complex expressions, like say
sum(case sal > 100 then sal else 0)ashigh_sal_total, and similar one for count distinct?
There was a problem hiding this comment.
- that could be another case - the more interesting grouping cases are when grouping by a non-join key when joining two tables with measures
- yes - those would be good. we'd probably use the FILTER (WHERE ...) formulation since that's a bit more explicit
|
Thank you for writing these tests. The format looks right. I like that you have included the Ossie query, expanded query, expected results, and a discussion of the algorithm (semantic rules) used to produce the expansion. The expansion algorithm -- based on a spine of distinct entities and correlated scalar subqueries -- also looks right, though I have not analyzed every single query. (I expect that we will collectively focus on a particular query only when we have a point of disagreement.) What do you think of adding an English description for each query? (I know it will make the corpus more verbose. And it will make some people think that this is a test for NL2SQL, which it is not. The Ossie query is the ground truth, not the English description.) I now have a PR for |
|
@julianhyde thanks for the feedback! 100% agreed on adding some English descriptions. I'll put up a PR with a few more cases that we can use to iterate on the naming scheme. Excited for the .ost PR #667 - I'll take a pass soon! |
|
I don't think I saw tests where the column being aggregated was on the "one" side and the filtering on the "many" side. An example would be SELECT e.job, d.avg_dept_name_length)
FROM emp AS e
WHERE sal > 2000
JOIN dept AS d USING (deptno)
GROUP BY e.jobwith the metric The data set does not make this easy. (There are no numeric attributes in the The "Measures in SQL" paper has an example WITH EnhancedCust o me rs AS
(SELECT *,
AVG(custAge) AS MEASURE avgAge
FROM Customers)
SELECT o. prodName ,
COUNT(*) AS orderCount ,
AVG(c.custAge) AS weightedAvgAge,
c.avgAge AS avgAge,
c.avgAge AT (VISIBLE) AS visibleAvgAge
FROM Orders AS o
JOIN EnhancedCust om er s AS c USING ( custName )
WHERE c.custAge >= 18
GROUP BY o . prodName ;with visible, non-visible, and weighted metrics. The "visible" keyword is not necessary in Ossie, because metrics are visible by default (i.e. rows are removed by the WHERE clause before calling the aggregate function), but I would like Ossie to have some gesture to say "non-visible" (In this case, what would mean giving the average age of all customers, not only those 18 or older, but only including in the query result products that have at least one customer aged 18 or older.) I think there's a matrix where we want to test all valid combinations. I'm not sure exactly what the dimensions of that matrix are, but a rough guess: filtering on the one vs many side; no filtering; metrics that are defined in the one side, many side, or after the join; filters that remove none, some or all of the values of the "many" side of a relationship. A fun and useful addition to the data set might be a And, of course, the employee named SCOTT would have a cat named TIGER. |
Good call on the matrix & the missing 1:M example. I like your pet table extension. @julianhyde would you want to take a first pass on updating the model / adding in the table? I'm happy to review (am setting aside some time today to look over the ost extension) |
|
@christianeu-db My next task is to add some basic tests to At first, I'll write a mock expander that simply parrots your SQL fragments, and that will get the test to "pass". But can you share the code / prompts that you used to create those fragments? Maybe I can easily convert it into Python that uses a SQLGlot AST. After that, we'll be off to the races and can add more tests and test data sets. I'll make |
Conflict: compliance/query/README.md. Take the version of main, which apache#632 merged. It replaces the README of this branch. Co-authored-by: Isaac <no-reply@databricks.com>
Move cases A2-A12 and B1-B17 into the case format of apache#632, one directory per case. A1 is already on main. - query.ossie.sql: the Ossie query and the case header, copied from relational_semantics.sql without change. B9-B16 also get the shared header of their group. - reference.duckdb.sql: the DuckDB query, copied without change. - expected.sql: the result of the DuckDB query. The values are the values of the ASCII result tables (doubles rounded to 2 places in the tables). - scott_model.ossie.yaml: the avg_sal and avg_comm metrics of the model in relational_semantics.sql. Co-authored-by: Isaac <no-reply@databricks.com>
The README of apache#632 does not allow an exact numeric result whose scale is implementation-defined, such as AVG over an exact numeric or the division of two exact numerics. - avg_sal and avg_comm are AVG(CAST(... AS DOUBLE PRECISION)). - A5 and B14-B16 cast before they divide. A12 casts the argument of its outer AVG. - A2 no longer returns avg_sal, as A1 does not. No other expected result changes. Co-authored-by: Isaac <no-reply@databricks.com>
@julianhyde suggested an English description of each query (apache#628). - Each query.ossie.sql header has a title that states what the case shows and a Question line that tells in English what the query asks. A1 gets a Question line too. - The case directories take names from the same rule, for example a09_rollup -> a09_rollup_not_average_of_averages. - The README describes the header and the naming rule. The question helps a reader; the Ossie query is the ground truth. The queries, reference queries, and expected results do not change. Co-authored-by: Isaac <no-reply@databricks.com>
… queries As @jbonofre suggested on apache#632, the README now states the two steps of Section 9 that the earlier wording left out: within each output group, discard rows whose primary_key is NULL, keep one row per primary_key, then aggregate. It also describes the shape of the join reference queries (the joined and spine CTEs and one subquery per measure), and each join reference query points to it. Co-authored-by: Isaac <no-reply@databricks.com>
…casts - Cast to DOUBLE, not DOUBLE PRECISION. The CAST of the Ossie expression language lists FLOAT and DOUBLE as its floating-point types. The reference queries do the same. expected.sql keeps DOUBLE PRECISION for its typed NULL, because it is standard SQL. - Write every expression of scott_model.ossie.yaml in the portable form, expression.sql, as the canonical examples do (apache#640). The model had only OSSIE_SQL_2026 entries in dialects. No expected result changes. Co-authored-by: Isaac <no-reply@databricks.com>
|
@julianhyde I split out these cases into their own folders - let me know if these all resonate and then we could merge in prep of the OST refactor |
B1-B3 report a measure of the "many" side over a many-to-one join, so a plain join-and-aggregate returns the same result and does not exercise the grain guarantee. These three cases put a measure of the "one" side where a fan-out inflates it, each through a different clause: - B18: a WHERE filter restricts a "one"-side measure that the join fans out. - B19: a join to a non-distinct filter subquery does not inflate a measure (the duplication comes from the filter relation, not a finer-grained table), covering the closure corollary of Section 8.4. - B20: a HAVING predicate over a fanned-out "one"-side measure, where a wrong grain changes the result set, not only the values. Each case reuses an existing metric, so the model is unchanged. For each, expected.sql equals reference.duckdb.sql on DuckDB, and a plain join-and-aggregate diverges (values noted in each header). Co-authored-by: Isaac <no-reply@databricks.com>
|
I am conflicted about the decision to break into separate files, and with the amount of boilerplate in general. If tests are concise, you can fit several on the same page, and therefore see the contrast between, say, a join being The choice of the scott-tiger schema, with small tables with few columns and short data values, was intentional. In the interests of conciseness, I would also remove the expanded SQL from a lot of tests, and just check in the results. The Speaking of conflicts, I have started working on #502 in my https://github.com/julianhyde/ossie/tree/502-expander branch. It builds on my PR #667 for case #501 but it also translates to |
@julianhyde Agreed, let's move full steam ahead with OST going forward. As a tactical step, would you be OK with landing these cases for now? They'd give us a starting point for compliance testing and concrete examples of query shapes in the repo. We can incrementally replace them with equivalent .ost cases as those become available. I'm also planning to update the website to mention compliance testing. While the current structure is far from ideal, these cases provide substantially more coverage than the single trivial case merged today. Otherwise, I agree with all your points. I particularly like the idea of grouping related cases in a single file to illustrate how different query variations behave. |
|
Sure, you can land these cases for now. But then please pause your development, because to complete #502 I will need to convert all of them to After 502 lands we can move tests around, remove boilerplate, etc. |
|
By the way, you'll notice that in #502 I added a create table scott.pet as
select cast(pname as varchar(10)) as pname,
cast(empno as smallint) as empno,
cast(species as varchar(7)) as species,
cast(age as tinyint) as age
from (values
('SNOOPY', 7499, 'DOG', 7),
('FELIX', 7566, 'CAT', 5),
('HAMMY', 7876, 'HAMSTER', 1),
('OREO', 7566, 'CAT', 2),
('RUBY', 7566, 'DOG', 7),
('STEALTH', 7902, 'CAT', 5),
('TIGER', 7788, 'CAT', 3),
('TRAMP', null, 'DOG', 4)) as t (pname, empno, species, age);It makes it easier to express fanout queries. (The current fanout queries self-join to |
|
@julianhyde sounds good - I'll merge this & pause. Then, my no. 1 goal on these compliance suites is to help you land #501 and #502 |
kayemkim
left a comment
There was a problem hiding this comment.
All 32 reference queries give their expected.sql rows under the README's comparison rules on DuckDB 2.0.0 as well, and the joined-row counts and plain-aggregate figures the headers quote, B4 through B20, come out as written. The model also passes today's validate.py, which has #677 since your merge commit. One small thing, not blocking: in A4 the HAVING leaves exactly three jobs, so LIMIT 3 never cuts a row and an implementation that ignores LIMIT still passes the case; LIMIT 2, with the header's "three" adjusted, would make it bite.
…che#502) Add a view expander, which translates a query over an Ossie model into an equivalent query over the model's tables, and functions that let a script use it: * `modelInline(name, yaml)` defines a model from Ossie YAML, parsed and validated by the apache-ossie package. Each dataset is a relation in schema `name`, such as `scott_model.emp`. * `createView(name, sql)` defines a view. * `expand(sql)` returns the expansion of a query. * `executeExpand(sql)` expands a query and executes the expansion. Its output has two parts: the expansion, then the result. A command may now have several outputs, each matched by its own rules. A query that reads a model or a view is expanded before it is executed. The expander parses with sqlglot. It replaces a relation of a model with its dataset's source, each `AGG(m)` (or `MEASURE(m)`) with the expression of metric `m`, and a view with its query. It does not support joins of a model's relations yet. Add a `doubleDigits` property that rounds doubles in tables to a given number of digits after the point, halves away from zero, as SQL's `ROUND` does. `set` now takes a literal of any kind, and checks it against the property's type. Add `compliance/query/relational_semantics.ost`, which translates cases A1 to A3 of the compliance queries in apache#628 into `.ost` form, and run it in CI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…12 (apache#502) Add cases A4 to A12 of the compliance queries in apache#628 to `compliance/query/relational_semantics.ost`, so that all of Part A runs. To support A10, the expander now expands each SELECT after the SELECTs within it, and records the measures of its output. A subquery or CTE that projects a measure without evaluating it passes the measure through: it projects, instead, the columns that the measure's expression needs, under a new name if the subquery uses the column's name for something else, and the enclosing query evaluates the measure. `*` passes every measure through. A metric that has no expression in a dialect that the expander can read is now an error only if a query evaluates it. The expansion keeps `NULLS FIRST` and `NULLS LAST` as the query wrote them. sqlglot omits them where they are DuckDB's default, but the expansion should not rely on the defaults of a particular database. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…1 to B5 (apache#502) A join can repeat a row of a measure's relation, and a plain aggregate would count each copy. Expand a query that joins relations and evaluates measures by the reference strategy of Section 9 of the spec. A CTE "joined" has the query's FROM, JOIN and WHERE clauses, and projects the grouping keys and, for each relation whose measures the query evaluates, its row-identity key (the dataset's primary key) and the columns that the measures need. A CTE "spine" has one row per group. Each AGG(m) becomes a correlated subquery that reduces the group's joined rows to one per key, ignoring rows whose key is NULL, then applies m's aggregate. The expander does not yet support, in a join, measures of a subquery, plain aggregates, GROUP BY ALL, ROLLUP, CUBE and GROUPING SETS, nor a query that passes measures through a join. The expansion now writes "x IS NOT NULL", not "NOT x IS NULL". Add cases B1 to B5 of the compliance queries in apache#628 to `compliance/query/relational_semantics.ost`. Their results, and those of A1 to A12, are the same as the expected results in apache#628. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add cases B6 to B17 of the compliance queries in apache#628 to `compliance/query/relational_semantics.ost`: a range join, fan-out on two sides of a three-way join, a many-to-many join, one self-join query with each join type, and one dept-emp query with inner and outer joins. All 29 cases of apache#628 now run, and give the same results as its expected results. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…#502) compliance/query now has the query compliance cases of apache#628, one directory per case, with a model, scott/scott_model.ossie.yaml, in which expressions have the form "sql", and AVG casts its argument to DOUBLE. Generate the cases of relational_semantics.ost from the cases' query.ossie.sql files, and its model from the upstream model, with the dataset pet added. This adds cases B18 to B20, and updates the queries of A1, A2, A5, A12, and B14 to B16, and the comments of every case. The results of all 32 cases equal those of the cases' expected.sql. Cases B5a, B8a and B9a to B13a, the note after B1, and the introductions of B9 to B13 and B14 to B17 remain. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Of the suite's 32 cases, 8 are asked as questions and return the expected result, 3 are asked and refused (a metric of the one side grouped or filtered by the many side), and 21 use a form of query no question has; each is named with that form. The weekly canary job also checks out core-spec, which two canaries read. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Of the suite's 32 cases, 8 are asked as questions and return the expected result, 3 are asked and refused (a metric of the one side grouped or filtered by the many side), and 21 use a form of query no question has; each is named with that form. The weekly canary job also checks out core-spec, which two canaries read. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…che#502) Add a view expander, which translates a query over an Ossie model into an equivalent query over the model's tables, and functions that let a script use it: * `modelInline(name, yaml)` defines a model from Ossie YAML, parsed and validated by the apache-ossie package. Each dataset is a relation in schema `name`, such as `scott_model.emp`. * `createView(name, sql)` defines a view. * `expand(sql)` returns the expansion of a query. * `executeExpand(sql)` expands a query and executes the expansion. Its output has two parts: the expansion, then the result. A command may now have several outputs, each matched by its own rules. A query that reads a model or a view is expanded before it is executed. The expander parses with sqlglot. It replaces a relation of a model with its dataset's source, each `AGG(m)` (or `MEASURE(m)`) with the expression of metric `m`, and a view with its query. It does not support joins of a model's relations yet. Add a `doubleDigits` property that rounds doubles in tables to a given number of digits after the point, halves away from zero, as SQL's `ROUND` does. `set` now takes a literal of any kind, and checks it against the property's type. Add `compliance/query/relational_semantics.ost`, which translates cases A1 to A3 of the compliance queries in apache#628 into `.ost` form, and run it in CI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…12 (apache#502) Add cases A4 to A12 of the compliance queries in apache#628 to `compliance/query/relational_semantics.ost`, so that all of Part A runs. To support A10, the expander now expands each SELECT after the SELECTs within it, and records the measures of its output. A subquery or CTE that projects a measure without evaluating it passes the measure through: it projects, instead, the columns that the measure's expression needs, under a new name if the subquery uses the column's name for something else, and the enclosing query evaluates the measure. `*` passes every measure through. A metric that has no expression in a dialect that the expander can read is now an error only if a query evaluates it. The expansion keeps `NULLS FIRST` and `NULLS LAST` as the query wrote them. sqlglot omits them where they are DuckDB's default, but the expansion should not rely on the defaults of a particular database. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…1 to B5 (apache#502) A join can repeat a row of a measure's relation, and a plain aggregate would count each copy. Expand a query that joins relations and evaluates measures by the reference strategy of Section 9 of the spec. A CTE "joined" has the query's FROM, JOIN and WHERE clauses, and projects the grouping keys and, for each relation whose measures the query evaluates, its row-identity key (the dataset's primary key) and the columns that the measures need. A CTE "spine" has one row per group. Each AGG(m) becomes a correlated subquery that reduces the group's joined rows to one per key, ignoring rows whose key is NULL, then applies m's aggregate. The expander does not yet support, in a join, measures of a subquery, plain aggregates, GROUP BY ALL, ROLLUP, CUBE and GROUPING SETS, nor a query that passes measures through a join. The expansion now writes "x IS NOT NULL", not "NOT x IS NULL". Add cases B1 to B5 of the compliance queries in apache#628 to `compliance/query/relational_semantics.ost`. Their results, and those of A1 to A12, are the same as the expected results in apache#628. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add cases B6 to B17 of the compliance queries in apache#628 to `compliance/query/relational_semantics.ost`: a range join, fan-out on two sides of a three-way join, a many-to-many join, one self-join query with each join type, and one dept-emp query with inner and outer joins. All 29 cases of apache#628 now run, and give the same results as its expected results. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…#502) compliance/query now has the query compliance cases of apache#628, one directory per case, with a model, scott/scott_model.ossie.yaml, in which expressions have the form "sql", and AVG casts its argument to DOUBLE. Generate the cases of relational_semantics.ost from the cases' query.ossie.sql files, and its model from the upstream model, with the dataset pet added. This adds cases B18 to B20, and updates the queries of A1, A2, A5, A12, and B14 to B16, and the comments of every case. The results of all 32 cases equal those of the cases' expected.sql. Cases B5a, B8a and B9a to B13a, the note after B1, and the introductions of B9 to B13 and B14 to B17 remain. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…che#502) Add a view expander, which translates a query over an Ossie model into an equivalent query over the model's tables, and functions that let a script use it: * `modelInline(name, yaml)` defines a model from Ossie YAML, parsed and validated by the apache-ossie package. Each dataset is a relation in schema `name`, such as `scott_model.emp`. * `createView(name, sql)` defines a view. * `expand(sql)` returns the expansion of a query. * `executeExpand(sql)` expands a query and executes the expansion. Its output has two parts: the expansion, then the result. A command may now have several outputs, each matched by its own rules. A query that reads a model or a view is expanded before it is executed. The expander parses with sqlglot. It replaces a relation of a model with its dataset's source, each `AGG(m)` (or `MEASURE(m)`) with the expression of metric `m`, and a view with its query. It does not support joins of a model's relations yet. Add a `doubleDigits` property that rounds doubles in tables to a given number of digits after the point, halves away from zero, as SQL's `ROUND` does. `set` now takes a literal of any kind, and checks it against the property's type. Add `compliance/query/relational_semantics.ost`, which translates cases A1 to A3 of the compliance queries in apache#628 into `.ost` form, and run it in CI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…12 (apache#502) Add cases A4 to A12 of the compliance queries in apache#628 to `compliance/query/relational_semantics.ost`, so that all of Part A runs. To support A10, the expander now expands each SELECT after the SELECTs within it, and records the measures of its output. A subquery or CTE that projects a measure without evaluating it passes the measure through: it projects, instead, the columns that the measure's expression needs, under a new name if the subquery uses the column's name for something else, and the enclosing query evaluates the measure. `*` passes every measure through. A metric that has no expression in a dialect that the expander can read is now an error only if a query evaluates it. The expansion keeps `NULLS FIRST` and `NULLS LAST` as the query wrote them. sqlglot omits them where they are DuckDB's default, but the expansion should not rely on the defaults of a particular database. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…1 to B5 (apache#502) A join can repeat a row of a measure's relation, and a plain aggregate would count each copy. Expand a query that joins relations and evaluates measures by the reference strategy of Section 9 of the spec. A CTE "joined" has the query's FROM, JOIN and WHERE clauses, and projects the grouping keys and, for each relation whose measures the query evaluates, its row-identity key (the dataset's primary key) and the columns that the measures need. A CTE "spine" has one row per group. Each AGG(m) becomes a correlated subquery that reduces the group's joined rows to one per key, ignoring rows whose key is NULL, then applies m's aggregate. The expander does not yet support, in a join, measures of a subquery, plain aggregates, GROUP BY ALL, ROLLUP, CUBE and GROUPING SETS, nor a query that passes measures through a join. The expansion now writes "x IS NOT NULL", not "NOT x IS NULL". Add cases B1 to B5 of the compliance queries in apache#628 to `compliance/query/relational_semantics.ost`. Their results, and those of A1 to A12, are the same as the expected results in apache#628. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add cases B6 to B17 of the compliance queries in apache#628 to `compliance/query/relational_semantics.ost`: a range join, fan-out on two sides of a three-way join, a many-to-many join, one self-join query with each join type, and one dept-emp query with inner and outer joins. All 29 cases of apache#628 now run, and give the same results as its expected results. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…#502) compliance/query now has the query compliance cases of apache#628, one directory per case, with a model, scott/scott_model.ossie.yaml, in which expressions have the form "sql", and AVG casts its argument to DOUBLE. Generate the cases of relational_semantics.ost from the cases' query.ossie.sql files, and its model from the upstream model, with the dataset pet added. This adds cases B18 to B20, and updates the queries of A1, A2, A5, A12, and B14 to B16, and the comments of every case. The results of all 32 cases equal those of the cases' expected.sql. Cases B5a, B8a and B9a to B13a, the note after B1, and the introductions of B9 to B13 and B14 to B17 remain. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
This PR adds cases A2-A12 and B1-B20 to the query compliance tests, in the case format that #632 merged. Each case is one directory under
compliance/query/cases/withquery.ossie.sql,expected.sql, andreference.duckdb.sql.The earlier version of this PR had all 29 cases in one file,
relational_semantics.sql. #632 took case A1 from it and settled the format. This version moves the other 28 cases into that format, and the single file is gone.WHERE,HAVING/ORDER BY/LIMIT, scalar expressions overAGG(), NULL inputs, an empty input set,GROUP BY ALL,ROLLUP, CTEs, scalar subqueries, and two-stage aggregation.ON, and a group with an empty input set.WHEREfilter, a non-distinct join-as-filter subquery, and aHAVINGpredicate, each over a measure of the "one" side, where a plain join-and-aggregate returns the wrong result.Commits
The cases move without change. Each later commit makes one kind of change:
compliance/query/README.mdtakes the version of test(compliance): add initial layer 2 query compliance test framework #632.relational_semantics.sqlinto case directories. Each Ossie query, reference query, and case header is copied without change. Eachexpected.sqlgives the values of the old ASCII result table.avg_salandavg_commcast their argument before they average. A5 and B14-B16 cast before they divide, and A12 casts the argument of its outerAVG. A2 no longer returnsavg_sal, as A1 does not. No other expected result changes.Question:lines. Only headers, directory names, and the README change. Theexpected.sqlandreference.duckdb.sqlfiles are pure renames.primary_keyis NULL, keep one row perprimary_key, and then aggregate.CAST(... AS DOUBLE), because theCASTofexpression_language.mdlistsFLOATandDOUBLEas its floating-point types.expected.sqlkeepsDOUBLE PRECISIONfor a typed NULL, because it is standard SQL. Every expression ofscott_model.ossie.yamlnow uses the portable form,expression: sql: ..., as the canonical examples do (docs: use portable sql fields in canonical examples #640). No expected result changes.Names and English descriptions
@julianhyde suggested an English description for each query. Each
query.ossie.sqlheader now has:A9. ROLLUP evaluates a measure again for each grouping set.;Question:line, for exampleWhat is the average salary of each department, and of the whole company?.The case directory names follow the same rule, for example
a09_rollup_not_average_of_averagesandb05_manager_counted_once. The README states that the question helps a reader and that the Ossie query is the ground truth.Case B5 as an example:
The 4 salesmen all report to BLAKE, so
managers_salfor SALESMAN is 2850. A plainSUM(m.sal)over the joined rows gives 11400.Out of scope
pettable. These need new data, and the README does not allow changes toscott.sqlin a case PR.FILTER (WHERE ...)andCOUNT(DISTINCT ...)metrics..ost(Add.ost, a file format for idempotent test scripts #501, feat(ost): add.ost, a file format for idempotent test scripts #667).Verification
relational_semantics.sql, and the rows of eachexpected.sqlequal the old result table. In the final version, 27 of the 28 results still equal the old tables; A2 has one column less.expected.sqlgives the same result asreference.duckdb.sqlon DuckDB 1.5.6 under the README comparison rules, and the column names of both are the aliases ofquery.ossie.sql.EXISTS) also give the rows ofexpected.sql.uv run validation/validate.py compliance/query/scott/scott_model.ossie.yamlpasses with the portablesqlform.Related Issues
Related: #354, #501, #502, #632, #640, #667.
Checklist
Specification
core-spec/and follow the existing structure (N/A: no spec change)Ontology
ontology/are consistent with spec changes (N/A)Converters
Validation
validation/are updated if the spec changed (N/A)validate.py)Documentation
docs/is updated to reflect any user-facing changes (N/A)compliance/query/README.md)CONTRIBUTING.mdis updated if the contribution process changed (N/A)Examples
examples/are added or updated for any new spec constructs (N/A)Tests
pytest/ CI green) (no code change; waiting for CI)Compliance
NOTICEand/orLICENSEhave been updated per ASF policy (N/A)AI disclosure
Per the ASF Generative Tooling Guidance, this contribution was prepared with AI assistance. I have reviewed and verified every change.
This pull request and its description were written by Isaac.