Skip to content
Merged
40 changes: 30 additions & 10 deletions compliance/query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ example `scott_model.emp`.
Case files
----------

`query.ossie.sql` starts with a comment header: the case ID and title, the
behavior under test, and the spec sections (for example, `Spec: 6, 7.2`). The
file can be submitted to an implementation as it is.
`query.ossie.sql` starts with a comment header:

- the case ID and a title that states what the case shows;
- a `Question:` line that tells in English what the query asks;
- the behavior under test, and why the result has its values;
- the spec sections (for example, `Spec: 6, 7.2`).

The question helps a reader. It is not an input of the test: the Ossie query
is the ground truth. The file can be submitted to an implementation as it is.

`expected.sql` is one query:

Expand All @@ -79,9 +85,22 @@ FROM (VALUES (29025.00, 14)) AS expected (sum_sal, emp_count);
`reference.duckdb.sql` is non-normative. If it disagrees with the spec, the
spec is correct, and the case MUST be fixed. It uses the column names of
`query.ossie.sql`. For one relation, it replaces each `AGG(m)` with the
expression of `m`, and each model relation with its source table. For joins,
it follows the reference evaluation strategy of Section 9: it removes join
duplicates by `primary_key`, and then it aggregates.
expression of `m`, and each model relation with its source table.

For joins, it follows the reference evaluation strategy of Section 9: within
each output group, it discards rows whose `primary_key` is NULL, keeps one row
per `primary_key`, and then aggregates. A group with no remaining rows takes
the empty-input value (`NULL` for `SUM`, `0` for `COUNT`). The query has this
shape:

1. The CTE `joined` is the `FROM`, `JOIN`, and `WHERE` of `query.ossie.sql`.
For each measure, it also selects the `primary_key` of the measure's
relation (`e_k` for alias `e`) and the arguments of the aggregate.
2. The CTE `spine` has one row for each group: `SELECT DISTINCT` over the
grouping columns. A query without `GROUP BY` has one group and no `spine`.
3. For each measure, a correlated subquery selects the rows of `joined` in the
group, discards the rows whose key is NULL, keeps one row per key
(`GROUP BY` the key), and then applies the aggregate.

Result semantics
----------------
Expand All @@ -96,8 +115,8 @@ implementation-defined, such as `AVG` over an exact numeric or the division of
two exact numerics. The tolerance does not cover these results: for `AVG` over
`DECIMAL(7, 2)`, an implementation can return a scale of 2, which differs from
the true value by more than 1e-9. To use `AVG`, cast its argument to an
approximate type, for example `AVG(CAST(sal AS DOUBLE PRECISION))`. A case
MUST NOT return NaN or an infinity.
approximate type of the Ossie expression language, for example
`AVG(CAST(sal AS DOUBLE))`. A case MUST NOT return NaN or an infinity.

Comparing results
-----------------
Expand Down Expand Up @@ -150,8 +169,9 @@ Adding a case
-------------

- Add a directory under `cases/` with the three files. Name it with the
lowercase case ID, with a two-digit number, and a short name, for example
`a02_group_by`. Use the ID prefix `A` for one relation and `B` for joins.
lowercase case ID, with a two-digit number, and a short name that says what
the case shows, for example `a09_rollup_not_average_of_averages`. Use the ID
prefix `A` for one relation and `B` for joins.
- Do not change `scott/scott.sql`. Add any new metric to
`scott/scott_model.ossie.yaml` in the same pull request, and validate the
model.
Expand Down
1 change: 1 addition & 0 deletions compliance/query/cases/a01_grand_total/query.ossie.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
-- under the License.
-- ----------------------------------------------------------------------------
-- A1. Grand total.
-- Question: What is the total salary, and how many employees are there?
-- AGG() is an aggregate. Without GROUP BY, the query returns one row.
-- Spec: 6, 7.2
-- ----------------------------------------------------------------------------
Expand Down
21 changes: 21 additions & 0 deletions compliance/query/cases/a02_measure_per_group/expected.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- Expected result for case A2 (query.ossie.sql), in standard SQL.
SELECT *
FROM (VALUES (10, 8750.00, 3),
(20, 10875.00, 5),
(30, 9400.00, 6)) AS expected (deptno, sum_sal, emp_count);
28 changes: 28 additions & 0 deletions compliance/query/cases/a02_measure_per_group/query.ossie.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- ----------------------------------------------------------------------------
-- A2. A measure is evaluated for each group.
-- Question: What are the payroll and the headcount of each department?
-- The grouping sets the level at which each measure is reported.
-- Spec: 7.4, G1 (8.2)
-- ----------------------------------------------------------------------------
SELECT deptno,
AGG(sum_sal) AS sum_sal,
AGG(emp_count) AS emp_count
FROM scott_model.emp
GROUP BY deptno
ORDER BY deptno;
24 changes: 24 additions & 0 deletions compliance/query/cases/a02_measure_per_group/reference.duckdb.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- Reference query for case A2 (query.ossie.sql), over the base tables in
-- ../../scott/scott.sql. Its result is equal to the result of expected.sql.
SELECT deptno,
SUM(sal) AS sum_sal,
COUNT(*) AS emp_count
FROM scott.emp
GROUP BY deptno
ORDER BY deptno;
21 changes: 21 additions & 0 deletions compliance/query/cases/a03_where_before_measure/expected.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- Expected result for case A3 (query.ossie.sql), in standard SQL.
SELECT *
FROM (VALUES (10, 7450.00, 2),
(20, 8975.00, 3),
(30, 8450.00, 5)) AS expected (deptno, sum_sal, emp_count);
30 changes: 30 additions & 0 deletions compliance/query/cases/a03_where_before_measure/query.ossie.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- ----------------------------------------------------------------------------
-- A3. WHERE removes rows before a measure is evaluated.
-- Question: Without the clerks, what are the payroll and the headcount of
-- each department?
-- Each measure sees only the rows that remain after WHERE.
-- Spec: 7.3, G1 (8.2)
-- ----------------------------------------------------------------------------
SELECT deptno,
AGG(sum_sal) AS sum_sal,
AGG(emp_count) AS emp_count
FROM scott_model.emp
WHERE job <> 'CLERK'
GROUP BY deptno
ORDER BY deptno;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- Reference query for case A3 (query.ossie.sql), over the base tables in
-- ../../scott/scott.sql. Its result is equal to the result of expected.sql.
SELECT deptno,
SUM(sal) AS sum_sal,
COUNT(*) AS emp_count
FROM scott.emp
WHERE job <> 'CLERK'
GROUP BY deptno
ORDER BY deptno;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- Expected result for case A4 (query.ossie.sql), in standard SQL.
SELECT *
FROM (VALUES ('MANAGER', 8275.00),
('SALESMAN', 5600.00),
('CLERK', 4150.00)) AS expected (job, sum_sal);
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- ----------------------------------------------------------------------------
-- A4. HAVING and ORDER BY can use a measure that is not selected.
-- Question: Of the jobs that more than two employees have, which three have
-- the largest payroll?
-- AGG() can appear in HAVING and ORDER BY, as any aggregate can. HAVING can
-- use a measure that the SELECT list does not contain. HAVING removes ANALYST
-- (6000, 2 employees) and PRESIDENT (5000, 1 employee), so CLERK (4150) is in
-- the top three.
-- Spec: 7.5
-- ----------------------------------------------------------------------------
SELECT job, AGG(sum_sal) AS sum_sal
FROM scott_model.emp
GROUP BY job
HAVING AGG(emp_count) > 2
ORDER BY AGG(sum_sal) DESC
LIMIT 3;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- Reference query for case A4 (query.ossie.sql), over the base tables in
-- ../../scott/scott.sql. Its result is equal to the result of expected.sql.
SELECT job, SUM(sal) AS sum_sal
FROM scott.emp
GROUP BY job
HAVING COUNT(*) > 2
ORDER BY SUM(sal) DESC
LIMIT 3;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- Expected result for case A5 (query.ossie.sql), in standard SQL.
SELECT *
FROM (VALUES (10, 2.9166666666666665E3, 2.9166666666666665E3, 8750.00),
(20, 2.175E3, 2.175E3, 10875.00),
(30, 1.5666666666666667E3, 1.5666666666666667E3, 11600.00))
AS expected (deptno, sal_per_emp, avg_sal, total_pay);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- ----------------------------------------------------------------------------
-- A5. Scalar operators apply to the values of AGG().
-- Question: For each department, what is the salary per employee, computed in
-- two ways, and what is the total of salaries and commissions?
-- Each AGG() gives one scalar value for each group. Scalar operators and
-- functions then apply to these values. The ratio of two measures, sum_sal /
-- emp_count, is equal to avg_sal. The CAST makes the division approximate:
-- the scale of a division of two exact numerics is implementation-defined
-- (README, "Result semantics"). COALESCE replaces the NULL commission total
-- of departments 10 and 20.
-- Spec: 7.2, 8.1
-- ----------------------------------------------------------------------------
SELECT deptno,
CAST(AGG(sum_sal) AS DOUBLE) / AGG(emp_count) AS sal_per_emp,
AGG(avg_sal) AS avg_sal,
AGG(sum_sal) + COALESCE(AGG(sum_comm), 0) AS total_pay
FROM scott_model.emp
GROUP BY deptno
ORDER BY deptno;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
-- Reference query for case A5 (query.ossie.sql), over the base tables in
-- ../../scott/scott.sql. Its result is equal to the result of expected.sql.
SELECT deptno,
CAST(SUM(sal) AS DOUBLE) / COUNT(*) AS sal_per_emp,
AVG(CAST(sal AS DOUBLE)) AS avg_sal,
SUM(sal) + COALESCE(SUM(comm), 0) AS total_pay
FROM scott.emp
GROUP BY deptno
ORDER BY deptno;
Loading
Loading