diff --git a/compliance/query/README.md b/compliance/query/README.md index 1a6153ee8..d93b0a867 100644 --- a/compliance/query/README.md +++ b/compliance/query/README.md @@ -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: @@ -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 ---------------- @@ -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 ----------------- @@ -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. diff --git a/compliance/query/cases/a01_grand_total/query.ossie.sql b/compliance/query/cases/a01_grand_total/query.ossie.sql index d2a7fcf49..ad83b4682 100644 --- a/compliance/query/cases/a01_grand_total/query.ossie.sql +++ b/compliance/query/cases/a01_grand_total/query.ossie.sql @@ -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 -- ---------------------------------------------------------------------------- diff --git a/compliance/query/cases/a02_measure_per_group/expected.sql b/compliance/query/cases/a02_measure_per_group/expected.sql new file mode 100644 index 000000000..a6bf60bbf --- /dev/null +++ b/compliance/query/cases/a02_measure_per_group/expected.sql @@ -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); diff --git a/compliance/query/cases/a02_measure_per_group/query.ossie.sql b/compliance/query/cases/a02_measure_per_group/query.ossie.sql new file mode 100644 index 000000000..36501bcb0 --- /dev/null +++ b/compliance/query/cases/a02_measure_per_group/query.ossie.sql @@ -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; diff --git a/compliance/query/cases/a02_measure_per_group/reference.duckdb.sql b/compliance/query/cases/a02_measure_per_group/reference.duckdb.sql new file mode 100644 index 000000000..f27f8cc0c --- /dev/null +++ b/compliance/query/cases/a02_measure_per_group/reference.duckdb.sql @@ -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; diff --git a/compliance/query/cases/a03_where_before_measure/expected.sql b/compliance/query/cases/a03_where_before_measure/expected.sql new file mode 100644 index 000000000..e7884ba22 --- /dev/null +++ b/compliance/query/cases/a03_where_before_measure/expected.sql @@ -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); diff --git a/compliance/query/cases/a03_where_before_measure/query.ossie.sql b/compliance/query/cases/a03_where_before_measure/query.ossie.sql new file mode 100644 index 000000000..14a19a857 --- /dev/null +++ b/compliance/query/cases/a03_where_before_measure/query.ossie.sql @@ -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; diff --git a/compliance/query/cases/a03_where_before_measure/reference.duckdb.sql b/compliance/query/cases/a03_where_before_measure/reference.duckdb.sql new file mode 100644 index 000000000..59f689c16 --- /dev/null +++ b/compliance/query/cases/a03_where_before_measure/reference.duckdb.sql @@ -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; diff --git a/compliance/query/cases/a04_having_on_unselected_measure/expected.sql b/compliance/query/cases/a04_having_on_unselected_measure/expected.sql new file mode 100644 index 000000000..1227e790b --- /dev/null +++ b/compliance/query/cases/a04_having_on_unselected_measure/expected.sql @@ -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); diff --git a/compliance/query/cases/a04_having_on_unselected_measure/query.ossie.sql b/compliance/query/cases/a04_having_on_unselected_measure/query.ossie.sql new file mode 100644 index 000000000..5befd2f56 --- /dev/null +++ b/compliance/query/cases/a04_having_on_unselected_measure/query.ossie.sql @@ -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; diff --git a/compliance/query/cases/a04_having_on_unselected_measure/reference.duckdb.sql b/compliance/query/cases/a04_having_on_unselected_measure/reference.duckdb.sql new file mode 100644 index 000000000..11ec674f3 --- /dev/null +++ b/compliance/query/cases/a04_having_on_unselected_measure/reference.duckdb.sql @@ -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; diff --git a/compliance/query/cases/a05_scalar_ops_on_measure_values/expected.sql b/compliance/query/cases/a05_scalar_ops_on_measure_values/expected.sql new file mode 100644 index 000000000..aa4d3d07d --- /dev/null +++ b/compliance/query/cases/a05_scalar_ops_on_measure_values/expected.sql @@ -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); diff --git a/compliance/query/cases/a05_scalar_ops_on_measure_values/query.ossie.sql b/compliance/query/cases/a05_scalar_ops_on_measure_values/query.ossie.sql new file mode 100644 index 000000000..ec27cad44 --- /dev/null +++ b/compliance/query/cases/a05_scalar_ops_on_measure_values/query.ossie.sql @@ -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; diff --git a/compliance/query/cases/a05_scalar_ops_on_measure_values/reference.duckdb.sql b/compliance/query/cases/a05_scalar_ops_on_measure_values/reference.duckdb.sql new file mode 100644 index 000000000..080fcf2f9 --- /dev/null +++ b/compliance/query/cases/a05_scalar_ops_on_measure_values/reference.duckdb.sql @@ -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; diff --git a/compliance/query/cases/a06_nulls_follow_aggregate/expected.sql b/compliance/query/cases/a06_nulls_follow_aggregate/expected.sql new file mode 100644 index 000000000..21bff599d --- /dev/null +++ b/compliance/query/cases/a06_nulls_follow_aggregate/expected.sql @@ -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 A6 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (10, 3, 0, NULL, NULL), + (20, 5, 0, NULL, NULL), + (30, 6, 4, 2200.00, 5.5E2)) + AS expected (deptno, emp_count, comm_count, sum_comm, avg_comm); diff --git a/compliance/query/cases/a06_nulls_follow_aggregate/query.ossie.sql b/compliance/query/cases/a06_nulls_follow_aggregate/query.ossie.sql new file mode 100644 index 000000000..6becbba37 --- /dev/null +++ b/compliance/query/cases/a06_nulls_follow_aggregate/query.ossie.sql @@ -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. +-- ---------------------------------------------------------------------------- +-- A6. A measure follows the NULL rules of its aggregate. +-- Question: For each department, how many employees are there, how many of +-- them have a commission, and what are the total and the average +-- commission? +-- emp_count is COUNT(*) and counts every row. comm_count is COUNT(comm) and +-- ignores NULL values. Departments 10 and 20 have no commission values, so +-- sum_comm and avg_comm are NULL. In department 30, the commission of TURNER +-- is 0.00, which is a value, so avg_comm is 2200 / 4. +-- Spec: 8.1 +-- ---------------------------------------------------------------------------- +SELECT deptno, + AGG(emp_count) AS emp_count, + AGG(comm_count) AS comm_count, + AGG(sum_comm) AS sum_comm, + AGG(avg_comm) AS avg_comm +FROM scott_model.emp +GROUP BY deptno +ORDER BY deptno; diff --git a/compliance/query/cases/a06_nulls_follow_aggregate/reference.duckdb.sql b/compliance/query/cases/a06_nulls_follow_aggregate/reference.duckdb.sql new file mode 100644 index 000000000..25c897a3d --- /dev/null +++ b/compliance/query/cases/a06_nulls_follow_aggregate/reference.duckdb.sql @@ -0,0 +1,26 @@ +-- 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 A6 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +SELECT deptno, + COUNT(*) AS emp_count, + COUNT(comm) AS comm_count, + SUM(comm) AS sum_comm, + AVG(CAST(comm AS DOUBLE)) AS avg_comm +FROM scott.emp +GROUP BY deptno +ORDER BY deptno; diff --git a/compliance/query/cases/a07_empty_input_set/expected.sql b/compliance/query/cases/a07_empty_input_set/expected.sql new file mode 100644 index 000000000..fa2853d9c --- /dev/null +++ b/compliance/query/cases/a07_empty_input_set/expected.sql @@ -0,0 +1,20 @@ +-- 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 A7 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (0, CAST(NULL AS DECIMAL(38, 2)), CAST(NULL AS DOUBLE PRECISION))) + AS expected (emp_count, sum_sal, avg_sal); diff --git a/compliance/query/cases/a07_empty_input_set/query.ossie.sql b/compliance/query/cases/a07_empty_input_set/query.ossie.sql new file mode 100644 index 000000000..97b1bc1ad --- /dev/null +++ b/compliance/query/cases/a07_empty_input_set/query.ossie.sql @@ -0,0 +1,29 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- A7. A measure over no rows. +-- Question: What are the headcount, the payroll, and the average salary of +-- department 40? +-- No employee is in department 40. Each measure takes the empty-input value +-- of its aggregate: 0 for COUNT, NULL for SUM and AVG. +-- Spec: 8.1 +-- ---------------------------------------------------------------------------- +SELECT AGG(emp_count) AS emp_count, + AGG(sum_sal) AS sum_sal, + AGG(avg_sal) AS avg_sal +FROM scott_model.emp +WHERE deptno = 40; diff --git a/compliance/query/cases/a07_empty_input_set/reference.duckdb.sql b/compliance/query/cases/a07_empty_input_set/reference.duckdb.sql new file mode 100644 index 000000000..32228cf24 --- /dev/null +++ b/compliance/query/cases/a07_empty_input_set/reference.duckdb.sql @@ -0,0 +1,23 @@ +-- 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 A7 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +SELECT COUNT(*) AS emp_count, + SUM(sal) AS sum_sal, + AVG(CAST(sal AS DOUBLE)) AS avg_sal +FROM scott.emp +WHERE deptno = 40; diff --git a/compliance/query/cases/a08_group_by_all_skips_measures/expected.sql b/compliance/query/cases/a08_group_by_all_skips_measures/expected.sql new file mode 100644 index 000000000..20b7ef892 --- /dev/null +++ b/compliance/query/cases/a08_group_by_all_skips_measures/expected.sql @@ -0,0 +1,23 @@ +-- 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 A8 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (1980, 1, 800.00), + (1981, 10, 22825.00), + (1982, 1, 1300.00), + (1987, 2, 4100.00)) + AS expected (hire_year, emp_count, sum_sal); diff --git a/compliance/query/cases/a08_group_by_all_skips_measures/query.ossie.sql b/compliance/query/cases/a08_group_by_all_skips_measures/query.ossie.sql new file mode 100644 index 000000000..bc9f73bdf --- /dev/null +++ b/compliance/query/cases/a08_group_by_all_skips_measures/query.ossie.sql @@ -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. +-- ---------------------------------------------------------------------------- +-- A8. GROUP BY ALL does not group by measures. +-- Question: How many employees were hired in each year, and what is their +-- total salary? +-- A measure is not a grouping column. GROUP BY ALL groups by the other +-- expressions of the SELECT list: here, an expression on a field. +-- Spec: 7.4 +-- ---------------------------------------------------------------------------- +SELECT EXTRACT(YEAR FROM hiredate) AS hire_year, + AGG(emp_count) AS emp_count, + AGG(sum_sal) AS sum_sal +FROM scott_model.emp +GROUP BY ALL +ORDER BY hire_year; diff --git a/compliance/query/cases/a08_group_by_all_skips_measures/reference.duckdb.sql b/compliance/query/cases/a08_group_by_all_skips_measures/reference.duckdb.sql new file mode 100644 index 000000000..7fa795e35 --- /dev/null +++ b/compliance/query/cases/a08_group_by_all_skips_measures/reference.duckdb.sql @@ -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 A8 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +SELECT EXTRACT(YEAR FROM hiredate) AS hire_year, + COUNT(*) AS emp_count, + SUM(sal) AS sum_sal +FROM scott.emp +GROUP BY ALL +ORDER BY hire_year; diff --git a/compliance/query/cases/a09_rollup_not_average_of_averages/expected.sql b/compliance/query/cases/a09_rollup_not_average_of_averages/expected.sql new file mode 100644 index 000000000..abb5db6fe --- /dev/null +++ b/compliance/query/cases/a09_rollup_not_average_of_averages/expected.sql @@ -0,0 +1,23 @@ +-- 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 A9 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ( 10, 3, 2.9166666666666665E3), + ( 20, 5, 2.175E3), + ( 30, 6, 1.5666666666666667E3), + (NULL, 14, 2.073214285714286E3)) + AS expected (deptno, emp_count, avg_sal); diff --git a/compliance/query/cases/a09_rollup_not_average_of_averages/query.ossie.sql b/compliance/query/cases/a09_rollup_not_average_of_averages/query.ossie.sql new file mode 100644 index 000000000..4feb5905a --- /dev/null +++ b/compliance/query/cases/a09_rollup_not_average_of_averages/query.ossie.sql @@ -0,0 +1,31 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- A9. ROLLUP evaluates a measure again for each grouping set. +-- Question: What is the average salary of each department, and of the whole +-- company? +-- On the total row, AGG(avg_sal) is the average over all 14 employees +-- (2073.21). It is not the average of the three department averages +-- (2219.44). +-- Spec: 7.4, G1 (8.2) +-- ---------------------------------------------------------------------------- +SELECT deptno, + AGG(emp_count) AS emp_count, + AGG(avg_sal) AS avg_sal +FROM scott_model.emp +GROUP BY ROLLUP (deptno) +ORDER BY deptno NULLS LAST; diff --git a/compliance/query/cases/a09_rollup_not_average_of_averages/reference.duckdb.sql b/compliance/query/cases/a09_rollup_not_average_of_averages/reference.duckdb.sql new file mode 100644 index 000000000..31102cdb2 --- /dev/null +++ b/compliance/query/cases/a09_rollup_not_average_of_averages/reference.duckdb.sql @@ -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 A9 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +SELECT deptno, + COUNT(*) AS emp_count, + AVG(CAST(sal AS DOUBLE)) AS avg_sal +FROM scott.emp +GROUP BY ROLLUP (deptno) +ORDER BY deptno NULLS LAST; diff --git a/compliance/query/cases/a10_cte_passes_measures_through/expected.sql b/compliance/query/cases/a10_cte_passes_measures_through/expected.sql new file mode 100644 index 000000000..be08e0545 --- /dev/null +++ b/compliance/query/cases/a10_cte_passes_measures_through/expected.sql @@ -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 A10 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (10, 1300.00, 1), + (20, 1900.00, 2), + (30, 6550.00, 5)) AS expected (deptno, payroll, headcount); diff --git a/compliance/query/cases/a10_cte_passes_measures_through/query.ossie.sql b/compliance/query/cases/a10_cte_passes_measures_through/query.ossie.sql new file mode 100644 index 000000000..452488271 --- /dev/null +++ b/compliance/query/cases/a10_cte_passes_measures_through/query.ossie.sql @@ -0,0 +1,33 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- A10. A CTE passes measures through and restricts their rows. +-- Question: Counting only clerks and salesmen, what are the payroll and the +-- headcount of each department? +-- The CTE passes the measures through without evaluating them. A rename does +-- not change a measure. The WHERE clause of the CTE restricts the rows that +-- the outer AGG() calls see. +-- Spec: 7.1, 7.7, 8.1 +-- ---------------------------------------------------------------------------- +WITH staff AS ( + SELECT deptno, sum_sal AS payroll, emp_count AS headcount + FROM scott_model.emp + WHERE job IN ('CLERK', 'SALESMAN')) +SELECT deptno, AGG(payroll) AS payroll, AGG(headcount) AS headcount +FROM staff +GROUP BY deptno +ORDER BY deptno; diff --git a/compliance/query/cases/a10_cte_passes_measures_through/reference.duckdb.sql b/compliance/query/cases/a10_cte_passes_measures_through/reference.duckdb.sql new file mode 100644 index 000000000..29dce29b1 --- /dev/null +++ b/compliance/query/cases/a10_cte_passes_measures_through/reference.duckdb.sql @@ -0,0 +1,26 @@ +-- 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 A10 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +WITH staff AS ( + SELECT deptno, sal + FROM scott.emp + WHERE job IN ('CLERK', 'SALESMAN')) +SELECT deptno, SUM(sal) AS payroll, COUNT(*) AS headcount +FROM staff +GROUP BY deptno +ORDER BY deptno; diff --git a/compliance/query/cases/a11_agg_in_where_subquery/expected.sql b/compliance/query/cases/a11_agg_in_where_subquery/expected.sql new file mode 100644 index 000000000..74fa78f3b --- /dev/null +++ b/compliance/query/cases/a11_agg_in_where_subquery/expected.sql @@ -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 A11 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (10, 2), + (20, 3), + (30, 1)) AS expected (deptno, emp_count); diff --git a/compliance/query/cases/a11_agg_in_where_subquery/query.ossie.sql b/compliance/query/cases/a11_agg_in_where_subquery/query.ossie.sql new file mode 100644 index 000000000..1054ad52f --- /dev/null +++ b/compliance/query/cases/a11_agg_in_where_subquery/query.ossie.sql @@ -0,0 +1,29 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- A11. AGG() in a scalar subquery in WHERE. +-- Question: In each department, how many employees earn more than the company +-- average? +-- AGG() cannot appear directly in WHERE, but a scalar subquery in WHERE can +-- use it. The company average is 2073.21. +-- Spec: 7.3 +-- ---------------------------------------------------------------------------- +SELECT deptno, AGG(emp_count) AS emp_count +FROM scott_model.emp +WHERE sal > (SELECT AGG(avg_sal) FROM scott_model.emp) +GROUP BY deptno +ORDER BY deptno; diff --git a/compliance/query/cases/a11_agg_in_where_subquery/reference.duckdb.sql b/compliance/query/cases/a11_agg_in_where_subquery/reference.duckdb.sql new file mode 100644 index 000000000..7f2665a33 --- /dev/null +++ b/compliance/query/cases/a11_agg_in_where_subquery/reference.duckdb.sql @@ -0,0 +1,23 @@ +-- 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 A11 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +SELECT deptno, COUNT(*) AS emp_count +FROM scott.emp +WHERE sal > (SELECT AVG(CAST(sal AS DOUBLE)) FROM scott.emp) +GROUP BY deptno +ORDER BY deptno; diff --git a/compliance/query/cases/a12_aggregate_over_agg_values/expected.sql b/compliance/query/cases/a12_aggregate_over_agg_values/expected.sql new file mode 100644 index 000000000..17c0244fc --- /dev/null +++ b/compliance/query/cases/a12_aggregate_over_agg_values/expected.sql @@ -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 A12 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ('avg payroll per dept', 9.675E3), + ('avg salary per emp', 2.073214285714286E3)) + AS expected (metric, amount); diff --git a/compliance/query/cases/a12_aggregate_over_agg_values/query.ossie.sql b/compliance/query/cases/a12_aggregate_over_agg_values/query.ossie.sql new file mode 100644 index 000000000..5c2ef58ed --- /dev/null +++ b/compliance/query/cases/a12_aggregate_over_agg_values/query.ossie.sql @@ -0,0 +1,36 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- A12. An ordinary aggregate over AGG() values (two-stage aggregation). +-- Question: What is the average payroll of a department, and what is the +-- average salary of an employee? +-- The first branch averages the payroll of the three departments (29025 / 3). +-- The second branch is the average salary of an employee. After AGG() +-- evaluates the measures, the values are ordinary scalars, so the outer AVG +-- and UNION ALL are valid. The outer AVG casts its argument, so its result is +-- approximate (README, "Result semantics"). +-- Spec: A.3, 7.8 +-- ---------------------------------------------------------------------------- +SELECT 'avg payroll per dept' AS metric, + AVG(CAST(dept_sal AS DOUBLE)) AS amount +FROM (SELECT deptno, AGG(sum_sal) AS dept_sal + FROM scott_model.emp + GROUP BY deptno) AS d +UNION ALL +SELECT 'avg salary per emp', AGG(avg_sal) +FROM scott_model.emp +ORDER BY metric; diff --git a/compliance/query/cases/a12_aggregate_over_agg_values/reference.duckdb.sql b/compliance/query/cases/a12_aggregate_over_agg_values/reference.duckdb.sql new file mode 100644 index 000000000..eeeed914f --- /dev/null +++ b/compliance/query/cases/a12_aggregate_over_agg_values/reference.duckdb.sql @@ -0,0 +1,27 @@ +-- 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 A12 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +SELECT 'avg payroll per dept' AS metric, + AVG(CAST(dept_sal AS DOUBLE)) AS amount +FROM (SELECT deptno, SUM(sal) AS dept_sal + FROM scott.emp + GROUP BY deptno) AS d +UNION ALL +SELECT 'avg salary per emp', AVG(CAST(sal AS DOUBLE)) +FROM scott.emp +ORDER BY metric; diff --git a/compliance/query/cases/b01_many_to_one_no_fan_out/expected.sql b/compliance/query/cases/b01_many_to_one_no_fan_out/expected.sql new file mode 100644 index 000000000..33c7e1abb --- /dev/null +++ b/compliance/query/cases/b01_many_to_one_no_fan_out/expected.sql @@ -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 B1 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ('ACCOUNTING', 8750.00, 3), + ('RESEARCH', 10875.00, 5), + ('SALES', 9400.00, 6)) + AS expected (dname, sum_sal, emp_count); diff --git a/compliance/query/cases/b01_many_to_one_no_fan_out/query.ossie.sql b/compliance/query/cases/b01_many_to_one_no_fan_out/query.ossie.sql new file mode 100644 index 000000000..02f0c038e --- /dev/null +++ b/compliance/query/cases/b01_many_to_one_no_fan_out/query.ossie.sql @@ -0,0 +1,31 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B1. A many-to-one join repeats no row of the measure. +-- Question: What are the payroll and the headcount of each department, by +-- department name? +-- Each employee matches one department, so the join repeats no emp row. The +-- values are the per-department totals of A2. +-- Spec: 7.6 +-- ---------------------------------------------------------------------------- +SELECT d.dname, + AGG(e.sum_sal) AS sum_sal, + AGG(e.emp_count) AS emp_count +FROM scott_model.emp AS e +JOIN scott_model.dept AS d ON e.deptno = d.deptno +GROUP BY d.dname +ORDER BY d.dname; diff --git a/compliance/query/cases/b01_many_to_one_no_fan_out/reference.duckdb.sql b/compliance/query/cases/b01_many_to_one_no_fan_out/reference.duckdb.sql new file mode 100644 index 000000000..7535e94f1 --- /dev/null +++ b/compliance/query/cases/b01_many_to_one_no_fan_out/reference.duckdb.sql @@ -0,0 +1,39 @@ +-- 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 B1 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT d.dname, + e.empno AS e_k, e.sal AS e_sal + FROM scott.emp AS e + JOIN scott.dept AS d ON e.deptno = d.deptno), +spine AS (SELECT DISTINCT dname FROM joined) +SELECT g.dname, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.dname IS NOT DISTINCT FROM g.dname + GROUP BY j.e_k) AS x) AS sum_sal, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.dname IS NOT DISTINCT FROM g.dname + GROUP BY j.e_k) AS x) AS emp_count +FROM spine AS g +ORDER BY g.dname; diff --git a/compliance/query/cases/b02_filter_on_joined_relation/expected.sql b/compliance/query/cases/b02_filter_on_joined_relation/expected.sql new file mode 100644 index 000000000..07d33c62e --- /dev/null +++ b/compliance/query/cases/b02_filter_on_joined_relation/expected.sql @@ -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 B2 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ('CLERK', 1300.00, 1), + ('MANAGER', 2450.00, 1), + ('PRESIDENT', 5000.00, 1)) AS expected (job, sum_sal, emp_count); diff --git a/compliance/query/cases/b02_filter_on_joined_relation/query.ossie.sql b/compliance/query/cases/b02_filter_on_joined_relation/query.ossie.sql new file mode 100644 index 000000000..0495bcded --- /dev/null +++ b/compliance/query/cases/b02_filter_on_joined_relation/query.ossie.sql @@ -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. +-- ---------------------------------------------------------------------------- +-- B2. A filter on the joined relation restricts the measure. +-- Question: For the employees in New York, what are the payroll and the +-- headcount of each job? +-- A filter on dept removes emp rows from the input set, as a filter on emp +-- does. Only the employees in NEW YORK remain. +-- Spec: 7.6, G1 (8.2) +-- ---------------------------------------------------------------------------- +SELECT e.job, + AGG(e.sum_sal) AS sum_sal, + AGG(e.emp_count) AS emp_count +FROM scott_model.emp AS e +JOIN scott_model.dept AS d ON e.deptno = d.deptno +WHERE d.loc = 'NEW YORK' +GROUP BY e.job +ORDER BY e.job; diff --git a/compliance/query/cases/b02_filter_on_joined_relation/reference.duckdb.sql b/compliance/query/cases/b02_filter_on_joined_relation/reference.duckdb.sql new file mode 100644 index 000000000..ed3b85bd7 --- /dev/null +++ b/compliance/query/cases/b02_filter_on_joined_relation/reference.duckdb.sql @@ -0,0 +1,40 @@ +-- 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 B2 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.job, + e.empno AS e_k, e.sal AS e_sal + FROM scott.emp AS e + JOIN scott.dept AS d ON e.deptno = d.deptno + WHERE d.loc = 'NEW YORK'), +spine AS (SELECT DISTINCT job FROM joined) +SELECT g.job, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.e_k) AS x) AS sum_sal, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.e_k) AS x) AS emp_count +FROM spine AS g +ORDER BY g.job; diff --git a/compliance/query/cases/b03_top_n_join_as_filter/expected.sql b/compliance/query/cases/b03_top_n_join_as_filter/expected.sql new file mode 100644 index 000000000..3a46b362e --- /dev/null +++ b/compliance/query/cases/b03_top_n_join_as_filter/expected.sql @@ -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 B3 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ('ANALYST', 6000.00), + ('CLERK', 2850.00), + ('MANAGER', 5825.00), + ('SALESMAN', 5600.00)) AS expected (job, sum_sal); diff --git a/compliance/query/cases/b03_top_n_join_as_filter/query.ossie.sql b/compliance/query/cases/b03_top_n_join_as_filter/query.ossie.sql new file mode 100644 index 000000000..6945ac299 --- /dev/null +++ b/compliance/query/cases/b03_top_n_join_as_filter/query.ossie.sql @@ -0,0 +1,36 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B3. A join to a top-N subquery acts as a filter. +-- Question: In the two departments with the largest payroll, what is the +-- payroll of each job? +-- The subquery selects the two departments with the largest payroll (20 and +-- 30). The join to the subquery acts as a filter on emp, and the outer query +-- reports the payroll of each job in those departments. The departments that +-- the subquery ranks are the same values that the join matches (closure, +-- Section 8.4). +-- Spec: 7.6, 8.4, A.2 +-- ---------------------------------------------------------------------------- +SELECT e.job, AGG(e.sum_sal) AS sum_sal +FROM scott_model.emp AS e +JOIN (SELECT deptno + FROM scott_model.emp + GROUP BY deptno + ORDER BY AGG(sum_sal) DESC + LIMIT 2) AS t ON e.deptno = t.deptno +GROUP BY e.job +ORDER BY e.job; diff --git a/compliance/query/cases/b03_top_n_join_as_filter/reference.duckdb.sql b/compliance/query/cases/b03_top_n_join_as_filter/reference.duckdb.sql new file mode 100644 index 000000000..f26e0a2f6 --- /dev/null +++ b/compliance/query/cases/b03_top_n_join_as_filter/reference.duckdb.sql @@ -0,0 +1,38 @@ +-- 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 B3 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.job, + e.empno AS e_k, e.sal AS e_sal + FROM scott.emp AS e + JOIN (SELECT deptno + FROM scott.emp + GROUP BY deptno + ORDER BY SUM(sal) DESC + LIMIT 2) AS t ON e.deptno = t.deptno), +spine AS (SELECT DISTINCT job FROM joined) +SELECT g.job, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.e_k) AS x) AS sum_sal +FROM spine AS g +ORDER BY g.job; diff --git a/compliance/query/cases/b04_one_side_counted_once/expected.sql b/compliance/query/cases/b04_one_side_counted_once/expected.sql new file mode 100644 index 000000000..1f40195e1 --- /dev/null +++ b/compliance/query/cases/b04_one_side_counted_once/expected.sql @@ -0,0 +1,23 @@ +-- 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 B4 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ('ANALYST', 1, 2), + ('CLERK', 3, 4), + ('MANAGER', 3, 3), + ('PRESIDENT', 1, 1), + ('SALESMAN', 1, 4)) AS expected (job, dept_count, emp_count); diff --git a/compliance/query/cases/b04_one_side_counted_once/query.ossie.sql b/compliance/query/cases/b04_one_side_counted_once/query.ossie.sql new file mode 100644 index 000000000..3effcd82e --- /dev/null +++ b/compliance/query/cases/b04_one_side_counted_once/query.ossie.sql @@ -0,0 +1,34 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B4. Fan-out: a measure of the "one" side counts each of its rows once. +-- Question: For each job, how many departments have it, and how many +-- employees have it? +-- The join repeats each dept row once for each employee in the department. +-- AGG(d.dept_count) counts each department once in each group, so the result +-- is the number of departments that have the job. A plain COUNT(*) over the +-- joined rows gives 2, 4, 3, 1, 4. AGG(e.emp_count) gives the same values, +-- because the join repeats no emp row. +-- Spec: 7.6, G2 (8.3) +-- ---------------------------------------------------------------------------- +SELECT e.job, + AGG(d.dept_count) AS dept_count, + AGG(e.emp_count) AS emp_count +FROM scott_model.emp AS e +JOIN scott_model.dept AS d ON e.deptno = d.deptno +GROUP BY e.job +ORDER BY e.job; diff --git a/compliance/query/cases/b04_one_side_counted_once/reference.duckdb.sql b/compliance/query/cases/b04_one_side_counted_once/reference.duckdb.sql new file mode 100644 index 000000000..f13e4944b --- /dev/null +++ b/compliance/query/cases/b04_one_side_counted_once/reference.duckdb.sql @@ -0,0 +1,40 @@ +-- 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 B4 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.job, + e.empno AS e_k, + d.deptno AS d_k + FROM scott.emp AS e + JOIN scott.dept AS d ON e.deptno = d.deptno), +spine AS (SELECT DISTINCT job FROM joined) +SELECT g.job, + (SELECT COUNT(*) + FROM (SELECT j.d_k + FROM joined AS j + WHERE j.d_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.d_k) AS x) AS dept_count, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.e_k) AS x) AS emp_count +FROM spine AS g +ORDER BY g.job; diff --git a/compliance/query/cases/b05_manager_counted_once/expected.sql b/compliance/query/cases/b05_manager_counted_once/expected.sql new file mode 100644 index 000000000..b5f22fcbb --- /dev/null +++ b/compliance/query/cases/b05_manager_counted_once/expected.sql @@ -0,0 +1,23 @@ +-- 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 B5 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ('ANALYST', 2, 1, 2975.00), + ('CLERK', 4, 4, 11300.00), + ('MANAGER', 3, 1, 5000.00), + ('SALESMAN', 4, 1, 2850.00)) + AS expected (job, emp_count, managers, managers_sal); diff --git a/compliance/query/cases/b05_manager_counted_once/query.ossie.sql b/compliance/query/cases/b05_manager_counted_once/query.ossie.sql new file mode 100644 index 000000000..4fd91bd8e --- /dev/null +++ b/compliance/query/cases/b05_manager_counted_once/query.ossie.sql @@ -0,0 +1,37 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B5. Fan-out in a self-join: each manager is counted once. +-- Question: For each job, how many employees have a manager, how many +-- managers do they report to, and what is the total salary of those +-- managers? +-- e is the employee and m is the manager. The join repeats each manager row +-- once for each report. AGG(m.sum_sal) counts the salary of each manager once +-- in each group. The 4 salesmen all report to BLAKE, so the result is 2850, +-- and a plain SUM(m.sal) gives 11400. The 4 clerks report to 4 different +-- managers, so the two values are the same (11300). A plain SUM(m.sal) by job +-- gives 5950, 11300, 15000, 11400. +-- Spec: 7.6, 8.1, G2 (8.3) +-- ---------------------------------------------------------------------------- +SELECT e.job, + AGG(e.emp_count) AS emp_count, + AGG(m.emp_count) AS managers, + AGG(m.sum_sal) AS managers_sal +FROM scott_model.emp AS e +JOIN scott_model.emp AS m ON e.mgr = m.empno +GROUP BY e.job +ORDER BY e.job; diff --git a/compliance/query/cases/b05_manager_counted_once/reference.duckdb.sql b/compliance/query/cases/b05_manager_counted_once/reference.duckdb.sql new file mode 100644 index 000000000..5a7a2d94b --- /dev/null +++ b/compliance/query/cases/b05_manager_counted_once/reference.duckdb.sql @@ -0,0 +1,45 @@ +-- 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 B5 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.job, + e.empno AS e_k, + m.empno AS m_k, m.sal AS m_sal + FROM scott.emp AS e + JOIN scott.emp AS m ON e.mgr = m.empno), +spine AS (SELECT DISTINCT job FROM joined) +SELECT g.job, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.e_k) AS x) AS emp_count, + (SELECT COUNT(*) + FROM (SELECT j.m_k + FROM joined AS j + WHERE j.m_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.m_k) AS x) AS managers, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.m_sal) AS sal + FROM joined AS j + WHERE j.m_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.m_k) AS x) AS managers_sal +FROM spine AS g +ORDER BY g.job; diff --git a/compliance/query/cases/b06_range_join_fan_out/expected.sql b/compliance/query/cases/b06_range_join_fan_out/expected.sql new file mode 100644 index 000000000..950b8389d --- /dev/null +++ b/compliance/query/cases/b06_range_join_fan_out/expected.sql @@ -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 B6 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (10, 3, 3), + (20, 5, 2), + (30, 6, 4)) AS expected (deptno, emp_count, grade_count); diff --git a/compliance/query/cases/b06_range_join_fan_out/query.ossie.sql b/compliance/query/cases/b06_range_join_fan_out/query.ossie.sql new file mode 100644 index 000000000..54f8caa6a --- /dev/null +++ b/compliance/query/cases/b06_range_join_fan_out/query.ossie.sql @@ -0,0 +1,34 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B6. Fan-out through a range join. +-- Question: For each department, how many employees are there, and how many +-- salary grades do their salaries fall into? +-- The join condition can be any predicate over fields, not only an equality. +-- Each employee matches one salary grade, and one grade can match many +-- employees. AGG(s.grade_count) counts each grade once in each department, so +-- it gives the number of distinct grades in the department. A plain COUNT(*) +-- over the joined rows gives 3, 5, 6. +-- Spec: 7.6, G2 (8.3) +-- ---------------------------------------------------------------------------- +SELECT e.deptno, + AGG(e.emp_count) AS emp_count, + AGG(s.grade_count) AS grade_count +FROM scott_model.emp AS e +JOIN scott_model.salgrade AS s ON e.sal BETWEEN s.losal AND s.hisal +GROUP BY e.deptno +ORDER BY e.deptno; diff --git a/compliance/query/cases/b06_range_join_fan_out/reference.duckdb.sql b/compliance/query/cases/b06_range_join_fan_out/reference.duckdb.sql new file mode 100644 index 000000000..af20db571 --- /dev/null +++ b/compliance/query/cases/b06_range_join_fan_out/reference.duckdb.sql @@ -0,0 +1,40 @@ +-- 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 B6 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.deptno, + e.empno AS e_k, + s.grade AS s_k + FROM scott.emp AS e + JOIN scott.salgrade AS s ON e.sal BETWEEN s.losal AND s.hisal), +spine AS (SELECT DISTINCT deptno FROM joined) +SELECT g.deptno, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.deptno IS NOT DISTINCT FROM g.deptno + GROUP BY j.e_k) AS x) AS emp_count, + (SELECT COUNT(*) + FROM (SELECT j.s_k + FROM joined AS j + WHERE j.s_k IS NOT NULL AND j.deptno IS NOT DISTINCT FROM g.deptno + GROUP BY j.s_k) AS x) AS grade_count +FROM spine AS g +ORDER BY g.deptno; diff --git a/compliance/query/cases/b07_three_way_join_fan_out/expected.sql b/compliance/query/cases/b07_three_way_join_fan_out/expected.sql new file mode 100644 index 000000000..2fe074c7b --- /dev/null +++ b/compliance/query/cases/b07_three_way_join_fan_out/expected.sql @@ -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. +-- Expected result for case B7 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (1, 3, 2850.00, 2), + (2, 3, 3800.00, 2), + (3, 2, 3100.00, 1), + (4, 5, 14275.00, 3), + (5, 1, 5000.00, 1)) + AS expected (grade, emp_count, sum_sal, dept_count); diff --git a/compliance/query/cases/b07_three_way_join_fan_out/query.ossie.sql b/compliance/query/cases/b07_three_way_join_fan_out/query.ossie.sql new file mode 100644 index 000000000..0a0e5f55a --- /dev/null +++ b/compliance/query/cases/b07_three_way_join_fan_out/query.ossie.sql @@ -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. +-- ---------------------------------------------------------------------------- +-- B7. Fan-out in a three-way join. +-- Question: For each salary grade, how many employees are in it, what is +-- their total salary, and how many departments have one of them? +-- emp joins to dept and to salgrade, and the group is the salary grade. +-- AGG(d.dept_count) gives the number of departments that have an employee in +-- the grade. The join repeats each dept row once for each of its employees in +-- the grade. A plain COUNT(*) over the joined rows gives 3, 3, 2, 5, 1. +-- Spec: 7.6, G2 (8.3) +-- ---------------------------------------------------------------------------- +SELECT s.grade, + AGG(e.emp_count) AS emp_count, + AGG(e.sum_sal) AS sum_sal, + AGG(d.dept_count) AS dept_count +FROM scott_model.emp AS e +JOIN scott_model.dept AS d ON e.deptno = d.deptno +JOIN scott_model.salgrade AS s ON e.sal BETWEEN s.losal AND s.hisal +GROUP BY s.grade +ORDER BY s.grade; diff --git a/compliance/query/cases/b07_three_way_join_fan_out/reference.duckdb.sql b/compliance/query/cases/b07_three_way_join_fan_out/reference.duckdb.sql new file mode 100644 index 000000000..0c6158717 --- /dev/null +++ b/compliance/query/cases/b07_three_way_join_fan_out/reference.duckdb.sql @@ -0,0 +1,46 @@ +-- 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 B7 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT s.grade, + e.empno AS e_k, e.sal AS e_sal, + d.deptno AS d_k + FROM scott.emp AS e + JOIN scott.dept AS d ON e.deptno = d.deptno + JOIN scott.salgrade AS s ON e.sal BETWEEN s.losal AND s.hisal), +spine AS (SELECT DISTINCT grade FROM joined) +SELECT g.grade, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.grade IS NOT DISTINCT FROM g.grade + GROUP BY j.e_k) AS x) AS emp_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.grade IS NOT DISTINCT FROM g.grade + GROUP BY j.e_k) AS x) AS sum_sal, + (SELECT COUNT(*) + FROM (SELECT j.d_k + FROM joined AS j + WHERE j.d_k IS NOT NULL AND j.grade IS NOT DISTINCT FROM g.grade + GROUP BY j.d_k) AS x) AS dept_count +FROM spine AS g +ORDER BY g.grade; diff --git a/compliance/query/cases/b08_many_to_many_chasm_trap/expected.sql b/compliance/query/cases/b08_many_to_many_chasm_trap/expected.sql new file mode 100644 index 000000000..1f01f45ac --- /dev/null +++ b/compliance/query/cases/b08_many_to_many_chasm_trap/expected.sql @@ -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 B8 (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, colleagues); diff --git a/compliance/query/cases/b08_many_to_many_chasm_trap/query.ossie.sql b/compliance/query/cases/b08_many_to_many_chasm_trap/query.ossie.sql new file mode 100644 index 000000000..a4b482860 --- /dev/null +++ b/compliance/query/cases/b08_many_to_many_chasm_trap/query.ossie.sql @@ -0,0 +1,34 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B8. A many-to-many join repeats rows on both sides (chasm trap). +-- Question: For each department, what is the payroll, and how many colleagues +-- are there, when each employee is joined to every colleague in the +-- department? +-- Each employee is joined to every colleague in the same department, which +-- gives 3 x 3, 5 x 5, and 6 x 6 joined rows. Both sides repeat. Each measure +-- still counts each of its source rows once. A plain SUM(e.sal) gives 26250, +-- 54375, 56400, and a plain COUNT(*) gives 9, 25, 36. +-- Spec: 7.6, G2 (8.3) +-- ---------------------------------------------------------------------------- +SELECT e.deptno, + AGG(e.sum_sal) AS sum_sal, + AGG(c.emp_count) AS colleagues +FROM scott_model.emp AS e +JOIN scott_model.emp AS c ON e.deptno = c.deptno +GROUP BY e.deptno +ORDER BY e.deptno; diff --git a/compliance/query/cases/b08_many_to_many_chasm_trap/reference.duckdb.sql b/compliance/query/cases/b08_many_to_many_chasm_trap/reference.duckdb.sql new file mode 100644 index 000000000..137343825 --- /dev/null +++ b/compliance/query/cases/b08_many_to_many_chasm_trap/reference.duckdb.sql @@ -0,0 +1,40 @@ +-- 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 B8 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.deptno, + e.empno AS e_k, e.sal AS e_sal, + c.empno AS c_k + FROM scott.emp AS e + JOIN scott.emp AS c ON e.deptno = c.deptno), +spine AS (SELECT DISTINCT deptno FROM joined) +SELECT g.deptno, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.deptno IS NOT DISTINCT FROM g.deptno + GROUP BY j.e_k) AS x) AS sum_sal, + (SELECT COUNT(*) + FROM (SELECT j.c_k + FROM joined AS j + WHERE j.c_k IS NOT NULL AND j.deptno IS NOT DISTINCT FROM g.deptno + GROUP BY j.c_k) AS x) AS colleagues +FROM spine AS g +ORDER BY g.deptno; diff --git a/compliance/query/cases/b09_inner_join_matched_rows/expected.sql b/compliance/query/cases/b09_inner_join_matched_rows/expected.sql new file mode 100644 index 000000000..463e27509 --- /dev/null +++ b/compliance/query/cases/b09_inner_join_matched_rows/expected.sql @@ -0,0 +1,20 @@ +-- 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 B9 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (13, 24025.00, 6, 19275.00)) + AS expected (e_count, e_sal, m_count, m_sal); diff --git a/compliance/query/cases/b09_inner_join_matched_rows/query.ossie.sql b/compliance/query/cases/b09_inner_join_matched_rows/query.ossie.sql new file mode 100644 index 000000000..5820a0518 --- /dev/null +++ b/compliance/query/cases/b09_inner_join_matched_rows/query.ossie.sql @@ -0,0 +1,38 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B9. INNER JOIN: each side keeps only its matched rows. +-- Question: Over the employees that have a manager and the managers that have +-- a report, how many employees are on each side, and what do they earn? +-- B9 to B13 run one self-join query with each join type. e is the employee +-- and m is the manager, joined on e.mgr = m.empno. KING has no manager, and 8 +-- employees manage nobody. The join type decides which rows reach each +-- measure. No join type makes a measure count a row twice, and NULL-padded +-- rows add nothing. +-- +-- INNER gives 13 joined rows. KING is not on the e side, and the 8 employees +-- that manage nobody are not on the m side. A plain SUM(m.sal) gives 43650, +-- because it counts the salary of KING 3 times and the salary of BLAKE 5 +-- times. +-- Spec: 7.6, 8.1, G2 (8.3), A.6 +-- ---------------------------------------------------------------------------- +SELECT AGG(e.emp_count) AS e_count, + AGG(e.sum_sal) AS e_sal, + AGG(m.emp_count) AS m_count, + AGG(m.sum_sal) AS m_sal +FROM scott_model.emp AS e +JOIN scott_model.emp AS m ON e.mgr = m.empno; diff --git a/compliance/query/cases/b09_inner_join_matched_rows/reference.duckdb.sql b/compliance/query/cases/b09_inner_join_matched_rows/reference.duckdb.sql new file mode 100644 index 000000000..b5d9267f2 --- /dev/null +++ b/compliance/query/cases/b09_inner_join_matched_rows/reference.duckdb.sql @@ -0,0 +1,45 @@ +-- 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 B9 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.empno AS e_k, e.sal AS e_sal, + m.empno AS m_k, m.sal AS m_sal + FROM scott.emp AS e + JOIN scott.emp AS m ON e.mgr = m.empno) +SELECT (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS e_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS e_sal, + (SELECT COUNT(*) + FROM (SELECT j.m_k + FROM joined AS j + WHERE j.m_k IS NOT NULL + GROUP BY j.m_k) AS x) AS m_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.m_sal) AS sal + FROM joined AS j + WHERE j.m_k IS NOT NULL + GROUP BY j.m_k) AS x) AS m_sal; diff --git a/compliance/query/cases/b10_left_join_null_padding/expected.sql b/compliance/query/cases/b10_left_join_null_padding/expected.sql new file mode 100644 index 000000000..9b702968d --- /dev/null +++ b/compliance/query/cases/b10_left_join_null_padding/expected.sql @@ -0,0 +1,20 @@ +-- 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 B10 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (14, 29025.00, 6, 19275.00)) + AS expected (e_count, e_sal, m_count, m_sal); diff --git a/compliance/query/cases/b10_left_join_null_padding/query.ossie.sql b/compliance/query/cases/b10_left_join_null_padding/query.ossie.sql new file mode 100644 index 000000000..ccdd0de9b --- /dev/null +++ b/compliance/query/cases/b10_left_join_null_padding/query.ossie.sql @@ -0,0 +1,37 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B10. LEFT JOIN: a NULL-padded row adds nothing to a measure. +-- Question: Over all employees and their managers, how many employees are on +-- each side, and what do they earn? +-- B9 to B13 run one self-join query with each join type. e is the employee +-- and m is the manager, joined on e.mgr = m.empno. KING has no manager, and 8 +-- employees manage nobody. The join type decides which rows reach each +-- measure. No join type makes a measure count a row twice, and NULL-padded +-- rows add nothing. +-- +-- LEFT gives 14 joined rows. KING is kept on the e side. The row of KING is +-- NULL-padded on the m side, so the m measures are the same as for INNER +-- (B9). +-- Spec: 7.6, 8.1, G2 (8.3), A.6 +-- ---------------------------------------------------------------------------- +SELECT AGG(e.emp_count) AS e_count, + AGG(e.sum_sal) AS e_sal, + AGG(m.emp_count) AS m_count, + AGG(m.sum_sal) AS m_sal +FROM scott_model.emp AS e +LEFT JOIN scott_model.emp AS m ON e.mgr = m.empno; diff --git a/compliance/query/cases/b10_left_join_null_padding/reference.duckdb.sql b/compliance/query/cases/b10_left_join_null_padding/reference.duckdb.sql new file mode 100644 index 000000000..d95111887 --- /dev/null +++ b/compliance/query/cases/b10_left_join_null_padding/reference.duckdb.sql @@ -0,0 +1,45 @@ +-- 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 B10 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.empno AS e_k, e.sal AS e_sal, + m.empno AS m_k, m.sal AS m_sal + FROM scott.emp AS e + LEFT JOIN scott.emp AS m ON e.mgr = m.empno) +SELECT (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS e_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS e_sal, + (SELECT COUNT(*) + FROM (SELECT j.m_k + FROM joined AS j + WHERE j.m_k IS NOT NULL + GROUP BY j.m_k) AS x) AS m_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.m_sal) AS sal + FROM joined AS j + WHERE j.m_k IS NOT NULL + GROUP BY j.m_k) AS x) AS m_sal; diff --git a/compliance/query/cases/b11_right_join_keeps_non_managers/expected.sql b/compliance/query/cases/b11_right_join_keeps_non_managers/expected.sql new file mode 100644 index 000000000..819a95121 --- /dev/null +++ b/compliance/query/cases/b11_right_join_keeps_non_managers/expected.sql @@ -0,0 +1,20 @@ +-- 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 B11 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (13, 24025.00, 14, 29025.00)) + AS expected (e_count, e_sal, m_count, m_sal); diff --git a/compliance/query/cases/b11_right_join_keeps_non_managers/query.ossie.sql b/compliance/query/cases/b11_right_join_keeps_non_managers/query.ossie.sql new file mode 100644 index 000000000..387f239c8 --- /dev/null +++ b/compliance/query/cases/b11_right_join_keeps_non_managers/query.ossie.sql @@ -0,0 +1,37 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B11. RIGHT JOIN: the employees that manage nobody stay on the m side. +-- Question: Over all employees as managers and the employees that report to +-- them, how many employees are on each side, and what do they earn? +-- B9 to B13 run one self-join query with each join type. e is the employee +-- and m is the manager, joined on e.mgr = m.empno. KING has no manager, and 8 +-- employees manage nobody. The join type decides which rows reach each +-- measure. No join type makes a measure count a row twice, and NULL-padded +-- rows add nothing. +-- +-- RIGHT gives 21 joined rows. The 8 employees that manage nobody are kept on +-- the m side. Their rows are NULL-padded on the e side, so the e measures are +-- the same as for INNER (B9). +-- Spec: 7.6, 8.1, G2 (8.3), A.6 +-- ---------------------------------------------------------------------------- +SELECT AGG(e.emp_count) AS e_count, + AGG(e.sum_sal) AS e_sal, + AGG(m.emp_count) AS m_count, + AGG(m.sum_sal) AS m_sal +FROM scott_model.emp AS e +RIGHT JOIN scott_model.emp AS m ON e.mgr = m.empno; diff --git a/compliance/query/cases/b11_right_join_keeps_non_managers/reference.duckdb.sql b/compliance/query/cases/b11_right_join_keeps_non_managers/reference.duckdb.sql new file mode 100644 index 000000000..b9ecbb78a --- /dev/null +++ b/compliance/query/cases/b11_right_join_keeps_non_managers/reference.duckdb.sql @@ -0,0 +1,45 @@ +-- 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 B11 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.empno AS e_k, e.sal AS e_sal, + m.empno AS m_k, m.sal AS m_sal + FROM scott.emp AS e + RIGHT JOIN scott.emp AS m ON e.mgr = m.empno) +SELECT (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS e_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS e_sal, + (SELECT COUNT(*) + FROM (SELECT j.m_k + FROM joined AS j + WHERE j.m_k IS NOT NULL + GROUP BY j.m_k) AS x) AS m_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.m_sal) AS sal + FROM joined AS j + WHERE j.m_k IS NOT NULL + GROUP BY j.m_k) AS x) AS m_sal; diff --git a/compliance/query/cases/b12_full_join_each_row_once/expected.sql b/compliance/query/cases/b12_full_join_each_row_once/expected.sql new file mode 100644 index 000000000..3348fdc4c --- /dev/null +++ b/compliance/query/cases/b12_full_join_each_row_once/expected.sql @@ -0,0 +1,20 @@ +-- 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 B12 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (14, 29025.00, 14, 29025.00)) + AS expected (e_count, e_sal, m_count, m_sal); diff --git a/compliance/query/cases/b12_full_join_each_row_once/query.ossie.sql b/compliance/query/cases/b12_full_join_each_row_once/query.ossie.sql new file mode 100644 index 000000000..16057a41c --- /dev/null +++ b/compliance/query/cases/b12_full_join_each_row_once/query.ossie.sql @@ -0,0 +1,36 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B12. FULL OUTER JOIN: each side counts each of its rows once. +-- Question: Over all employees on both sides of the manager relationship, how +-- many employees are on each side, and what do they earn? +-- B9 to B13 run one self-join query with each join type. e is the employee +-- and m is the manager, joined on e.mgr = m.empno. KING has no manager, and 8 +-- employees manage nobody. The join type decides which rows reach each +-- measure. No join type makes a measure count a row twice, and NULL-padded +-- rows add nothing. +-- +-- FULL gives 22 joined rows. Both sides keep all 14 employees, and each side +-- counts each employee once. +-- Spec: 7.6, 8.1, G2 (8.3), A.6 +-- ---------------------------------------------------------------------------- +SELECT AGG(e.emp_count) AS e_count, + AGG(e.sum_sal) AS e_sal, + AGG(m.emp_count) AS m_count, + AGG(m.sum_sal) AS m_sal +FROM scott_model.emp AS e +FULL JOIN scott_model.emp AS m ON e.mgr = m.empno; diff --git a/compliance/query/cases/b12_full_join_each_row_once/reference.duckdb.sql b/compliance/query/cases/b12_full_join_each_row_once/reference.duckdb.sql new file mode 100644 index 000000000..f3e389b4d --- /dev/null +++ b/compliance/query/cases/b12_full_join_each_row_once/reference.duckdb.sql @@ -0,0 +1,45 @@ +-- 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 B12 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.empno AS e_k, e.sal AS e_sal, + m.empno AS m_k, m.sal AS m_sal + FROM scott.emp AS e + FULL JOIN scott.emp AS m ON e.mgr = m.empno) +SELECT (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS e_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS e_sal, + (SELECT COUNT(*) + FROM (SELECT j.m_k + FROM joined AS j + WHERE j.m_k IS NOT NULL + GROUP BY j.m_k) AS x) AS m_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.m_sal) AS sal + FROM joined AS j + WHERE j.m_k IS NOT NULL + GROUP BY j.m_k) AS x) AS m_sal; diff --git a/compliance/query/cases/b13_cross_join_no_inflation/expected.sql b/compliance/query/cases/b13_cross_join_no_inflation/expected.sql new file mode 100644 index 000000000..0486bf8fc --- /dev/null +++ b/compliance/query/cases/b13_cross_join_no_inflation/expected.sql @@ -0,0 +1,20 @@ +-- 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 B13 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (14, 29025.00, 14, 29025.00)) + AS expected (e_count, e_sal, m_count, m_sal); diff --git a/compliance/query/cases/b13_cross_join_no_inflation/query.ossie.sql b/compliance/query/cases/b13_cross_join_no_inflation/query.ossie.sql new file mode 100644 index 000000000..3fca60950 --- /dev/null +++ b/compliance/query/cases/b13_cross_join_no_inflation/query.ossie.sql @@ -0,0 +1,36 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B13. CROSS JOIN: each side still counts each of its rows once. +-- Question: Over all pairs of employees, how many employees are on each side, +-- and what do they earn? +-- B9 to B13 run one self-join query with each join type. e is the employee +-- and m is the manager, joined on e.mgr = m.empno. KING has no manager, and 8 +-- employees manage nobody. The join type decides which rows reach each +-- measure. No join type makes a measure count a row twice, and NULL-padded +-- rows add nothing. +-- +-- The cross join has no condition and gives 14 x 14 = 196 rows. Each side +-- still counts each employee once. A plain SUM(e.sal) gives 406350. +-- Spec: 7.6, 8.1, G2 (8.3), A.6 +-- ---------------------------------------------------------------------------- +SELECT AGG(e.emp_count) AS e_count, + AGG(e.sum_sal) AS e_sal, + AGG(m.emp_count) AS m_count, + AGG(m.sum_sal) AS m_sal +FROM scott_model.emp AS e +CROSS JOIN scott_model.emp AS m; diff --git a/compliance/query/cases/b13_cross_join_no_inflation/reference.duckdb.sql b/compliance/query/cases/b13_cross_join_no_inflation/reference.duckdb.sql new file mode 100644 index 000000000..df1102631 --- /dev/null +++ b/compliance/query/cases/b13_cross_join_no_inflation/reference.duckdb.sql @@ -0,0 +1,45 @@ +-- 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 B13 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.empno AS e_k, e.sal AS e_sal, + m.empno AS m_k, m.sal AS m_sal + FROM scott.emp AS e + CROSS JOIN scott.emp AS m) +SELECT (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS e_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS e_sal, + (SELECT COUNT(*) + FROM (SELECT j.m_k + FROM joined AS j + WHERE j.m_k IS NOT NULL + GROUP BY j.m_k) AS x) AS m_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.m_sal) AS sal + FROM joined AS j + WHERE j.m_k IS NOT NULL + GROUP BY j.m_k) AS x) AS m_sal; diff --git a/compliance/query/cases/b14_ratio_across_relations/expected.sql b/compliance/query/cases/b14_ratio_across_relations/expected.sql new file mode 100644 index 000000000..09ff7ccb6 --- /dev/null +++ b/compliance/query/cases/b14_ratio_across_relations/expected.sql @@ -0,0 +1,20 @@ +-- 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 B14 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (3, 14, 29025.00, 9.675E3)) + AS expected (dept_count, emp_count, sum_sal, sal_per_dept); diff --git a/compliance/query/cases/b14_ratio_across_relations/query.ossie.sql b/compliance/query/cases/b14_ratio_across_relations/query.ossie.sql new file mode 100644 index 000000000..9123ee291 --- /dev/null +++ b/compliance/query/cases/b14_ratio_across_relations/query.ossie.sql @@ -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. +-- ---------------------------------------------------------------------------- +-- B14. A ratio of measures from two relations. +-- Question: What is the payroll per department, over the departments that +-- have employees? +-- B14 to B16 run one dept-emp query with different joins. The query divides +-- the payroll by the number of departments: a ratio of two measures from +-- different relations. The join repeats each dept row once for each employee +-- in the department, but dept_count still counts each department once. The +-- CAST makes the division approximate (README, "Result semantics"). +-- +-- A plain COUNT(d.deptno) over the joined rows gives 14, not 3. +-- Spec: 7.2, 7.6, 8.1, A.6 +-- ---------------------------------------------------------------------------- +SELECT AGG(d.dept_count) AS dept_count, + AGG(e.emp_count) AS emp_count, + AGG(e.sum_sal) AS sum_sal, + CAST(AGG(e.sum_sal) AS DOUBLE) / AGG(d.dept_count) AS sal_per_dept +FROM scott_model.dept AS d +JOIN scott_model.emp AS e ON e.deptno = d.deptno; diff --git a/compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql b/compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql new file mode 100644 index 000000000..4a28501fb --- /dev/null +++ b/compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql @@ -0,0 +1,44 @@ +-- 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 B14 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT d.deptno AS d_k, + e.empno AS e_k, e.sal AS e_sal + FROM scott.dept AS d + JOIN scott.emp AS e ON e.deptno = d.deptno), +measures AS ( + SELECT (SELECT COUNT(*) + FROM (SELECT j.d_k + FROM joined AS j + WHERE j.d_k IS NOT NULL + GROUP BY j.d_k) AS x) AS dept_count, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS emp_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS sum_sal) +SELECT dept_count, emp_count, sum_sal, + CAST(sum_sal AS DOUBLE) / dept_count AS sal_per_dept +FROM measures; diff --git a/compliance/query/cases/b15_left_join_changes_ratio/expected.sql b/compliance/query/cases/b15_left_join_changes_ratio/expected.sql new file mode 100644 index 000000000..2f2b02f66 --- /dev/null +++ b/compliance/query/cases/b15_left_join_changes_ratio/expected.sql @@ -0,0 +1,20 @@ +-- 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 B15 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (4, 14, 29025.00, 7.25625E3)) + AS expected (dept_count, emp_count, sum_sal, sal_per_dept); diff --git a/compliance/query/cases/b15_left_join_changes_ratio/query.ossie.sql b/compliance/query/cases/b15_left_join_changes_ratio/query.ossie.sql new file mode 100644 index 000000000..48fbcfa71 --- /dev/null +++ b/compliance/query/cases/b15_left_join_changes_ratio/query.ossie.sql @@ -0,0 +1,39 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B15. A LEFT JOIN keeps an empty department, and the ratio changes. +-- Question: What is the payroll per department, over all departments? +-- B14 to B16 run one dept-emp query with different joins. The query divides +-- the payroll by the number of departments: a ratio of two measures from +-- different relations. The join repeats each dept row once for each employee +-- in the department, but dept_count still counts each department once. The +-- CAST makes the division approximate (README, "Result semantics"). +-- +-- OPERATIONS has no employees. The LEFT join keeps it, so the payroll is +-- divided by 4 departments and not by 3 (B14). The join type is the choice of +-- the tool, and it changes the answer (Sections 10.2 and A.6). A plain +-- COUNT(d.deptno) over the joined rows gives 15, not 4. A RIGHT join with the +-- sides swapped, or a FULL join, gives the same result, because every +-- employee has a department. +-- Spec: 7.2, 7.6, 8.1, 10.2, A.6 +-- ---------------------------------------------------------------------------- +SELECT AGG(d.dept_count) AS dept_count, + AGG(e.emp_count) AS emp_count, + AGG(e.sum_sal) AS sum_sal, + CAST(AGG(e.sum_sal) AS DOUBLE) / AGG(d.dept_count) AS sal_per_dept +FROM scott_model.dept AS d +LEFT JOIN scott_model.emp AS e ON e.deptno = d.deptno; diff --git a/compliance/query/cases/b15_left_join_changes_ratio/reference.duckdb.sql b/compliance/query/cases/b15_left_join_changes_ratio/reference.duckdb.sql new file mode 100644 index 000000000..22a0c3f23 --- /dev/null +++ b/compliance/query/cases/b15_left_join_changes_ratio/reference.duckdb.sql @@ -0,0 +1,44 @@ +-- 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 B15 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT d.deptno AS d_k, + e.empno AS e_k, e.sal AS e_sal + FROM scott.dept AS d + LEFT JOIN scott.emp AS e ON e.deptno = d.deptno), +measures AS ( + SELECT (SELECT COUNT(*) + FROM (SELECT j.d_k + FROM joined AS j + WHERE j.d_k IS NOT NULL + GROUP BY j.d_k) AS x) AS dept_count, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS emp_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS sum_sal) +SELECT dept_count, emp_count, sum_sal, + CAST(sum_sal AS DOUBLE) / dept_count AS sal_per_dept +FROM measures; diff --git a/compliance/query/cases/b16_on_filter_keeps_left_rows/expected.sql b/compliance/query/cases/b16_on_filter_keeps_left_rows/expected.sql new file mode 100644 index 000000000..51efc8189 --- /dev/null +++ b/compliance/query/cases/b16_on_filter_keeps_left_rows/expected.sql @@ -0,0 +1,20 @@ +-- 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 B16 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (4, 4, 4150.00, 1.0375E3)) + AS expected (dept_count, emp_count, sum_sal, sal_per_dept); diff --git a/compliance/query/cases/b16_on_filter_keeps_left_rows/query.ossie.sql b/compliance/query/cases/b16_on_filter_keeps_left_rows/query.ossie.sql new file mode 100644 index 000000000..988eddbac --- /dev/null +++ b/compliance/query/cases/b16_on_filter_keeps_left_rows/query.ossie.sql @@ -0,0 +1,37 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B16. A filter in the ON clause of a LEFT JOIN keeps every left row. +-- Question: What is the payroll of the clerks per department, over all +-- departments? +-- B14 to B16 run one dept-emp query with different joins. The query divides +-- the payroll by the number of departments: a ratio of two measures from +-- different relations. The join repeats each dept row once for each employee +-- in the department, but dept_count still counts each department once. The +-- CAST makes the division approximate (README, "Result semantics"). +-- +-- The condition e.job = 'CLERK' in ON keeps every department and matches only +-- clerks. If the condition were in WHERE, the query would remove the +-- NULL-padded OPERATIONS row, and dept_count would be 3. +-- Spec: 7.2, 7.6, 8.1, A.6 +-- ---------------------------------------------------------------------------- +SELECT AGG(d.dept_count) AS dept_count, + AGG(e.emp_count) AS emp_count, + AGG(e.sum_sal) AS sum_sal, + CAST(AGG(e.sum_sal) AS DOUBLE) / AGG(d.dept_count) AS sal_per_dept +FROM scott_model.dept AS d +LEFT JOIN scott_model.emp AS e ON e.deptno = d.deptno AND e.job = 'CLERK'; diff --git a/compliance/query/cases/b16_on_filter_keeps_left_rows/reference.duckdb.sql b/compliance/query/cases/b16_on_filter_keeps_left_rows/reference.duckdb.sql new file mode 100644 index 000000000..a902ad1f2 --- /dev/null +++ b/compliance/query/cases/b16_on_filter_keeps_left_rows/reference.duckdb.sql @@ -0,0 +1,44 @@ +-- 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 B16 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT d.deptno AS d_k, + e.empno AS e_k, e.sal AS e_sal + FROM scott.dept AS d + LEFT JOIN scott.emp AS e ON e.deptno = d.deptno AND e.job = 'CLERK'), +measures AS ( + SELECT (SELECT COUNT(*) + FROM (SELECT j.d_k + FROM joined AS j + WHERE j.d_k IS NOT NULL + GROUP BY j.d_k) AS x) AS dept_count, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS emp_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL + GROUP BY j.e_k) AS x) AS sum_sal) +SELECT dept_count, emp_count, sum_sal, + CAST(sum_sal AS DOUBLE) / dept_count AS sal_per_dept +FROM measures; diff --git a/compliance/query/cases/b17_left_join_empty_group/expected.sql b/compliance/query/cases/b17_left_join_empty_group/expected.sql new file mode 100644 index 000000000..147abe364 --- /dev/null +++ b/compliance/query/cases/b17_left_join_empty_group/expected.sql @@ -0,0 +1,23 @@ +-- 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 B17 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ('ACCOUNTING', 3, 8750.00, 2.9166666666666665E3), + ('OPERATIONS', 0, NULL, NULL), + ('RESEARCH', 5, 10875.00, 2.175E3), + ('SALES', 6, 9400.00, 1.5666666666666667E3)) + AS expected (dname, emp_count, sum_sal, avg_sal); diff --git a/compliance/query/cases/b17_left_join_empty_group/query.ossie.sql b/compliance/query/cases/b17_left_join_empty_group/query.ossie.sql new file mode 100644 index 000000000..57eec59bd --- /dev/null +++ b/compliance/query/cases/b17_left_join_empty_group/query.ossie.sql @@ -0,0 +1,34 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B17. LEFT JOIN, grouped: a group with an empty input set. +-- Question: For each department, including a department with no employees, +-- what are the headcount, the payroll, and the average salary? +-- The B15 query, grouped by department. The OPERATIONS group stays in the +-- result. Its joined row is NULL-padded on the emp side, so the emp measures +-- take their empty-input values: 0 for COUNT, NULL for SUM and AVG. The INNER +-- join of B1 omits OPERATIONS. +-- Spec: 7.6, 8.1, A.6 +-- ---------------------------------------------------------------------------- +SELECT d.dname, + AGG(e.emp_count) AS emp_count, + AGG(e.sum_sal) AS sum_sal, + AGG(e.avg_sal) AS avg_sal +FROM scott_model.dept AS d +LEFT JOIN scott_model.emp AS e ON e.deptno = d.deptno +GROUP BY d.dname +ORDER BY d.dname; diff --git a/compliance/query/cases/b17_left_join_empty_group/reference.duckdb.sql b/compliance/query/cases/b17_left_join_empty_group/reference.duckdb.sql new file mode 100644 index 000000000..e85fc21e7 --- /dev/null +++ b/compliance/query/cases/b17_left_join_empty_group/reference.duckdb.sql @@ -0,0 +1,44 @@ +-- 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 B17 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT d.dname, + e.empno AS e_k, e.sal AS e_sal + FROM scott.dept AS d + LEFT JOIN scott.emp AS e ON e.deptno = d.deptno), +spine AS (SELECT DISTINCT dname FROM joined) +SELECT g.dname, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.dname IS NOT DISTINCT FROM g.dname + GROUP BY j.e_k) AS x) AS emp_count, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.dname IS NOT DISTINCT FROM g.dname + GROUP BY j.e_k) AS x) AS sum_sal, + (SELECT AVG(CAST(x.sal AS DOUBLE)) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.dname IS NOT DISTINCT FROM g.dname + GROUP BY j.e_k) AS x) AS avg_sal +FROM spine AS g +ORDER BY g.dname; diff --git a/compliance/query/cases/b18_filter_restricts_one_side_measure/expected.sql b/compliance/query/cases/b18_filter_restricts_one_side_measure/expected.sql new file mode 100644 index 000000000..999d979d6 --- /dev/null +++ b/compliance/query/cases/b18_filter_restricts_one_side_measure/expected.sql @@ -0,0 +1,23 @@ +-- 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 B18 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ('ANALYST', 1, 2), + ('CLERK', 2, 2), + ('MANAGER', 3, 3), + ('PRESIDENT', 1, 1), + ('SALESMAN', 1, 4)) AS expected (job, dept_count, emp_count); diff --git a/compliance/query/cases/b18_filter_restricts_one_side_measure/query.ossie.sql b/compliance/query/cases/b18_filter_restricts_one_side_measure/query.ossie.sql new file mode 100644 index 000000000..c44517ee8 --- /dev/null +++ b/compliance/query/cases/b18_filter_restricts_one_side_measure/query.ossie.sql @@ -0,0 +1,36 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B18. WHERE restricts a "one"-side measure that the join fans out. +-- Question: Among the employees who earn more than 1000, how many +-- departments have each job, and how many such employees are there? +-- The join repeats each dept row once for each employee in the department, +-- and WHERE removes rows before the measures are evaluated. AGG(d.dept_count) +-- counts each surviving department once in each group. Removing the two +-- lowest-paid clerks (SMITH and JAMES) drops CLERK from three departments to +-- two. A plain COUNT(*) over the joined rows gives 2, 2, 3, 1, 4. +-- AGG(e.emp_count) gives the same values, because the join repeats no emp row. +-- Spec: 7.3, 7.6, G1 (8.2), G2 (8.3) +-- ---------------------------------------------------------------------------- +SELECT e.job, + AGG(d.dept_count) AS dept_count, + AGG(e.emp_count) AS emp_count +FROM scott_model.emp AS e +JOIN scott_model.dept AS d ON e.deptno = d.deptno +WHERE e.sal > 1000 +GROUP BY e.job +ORDER BY e.job; diff --git a/compliance/query/cases/b18_filter_restricts_one_side_measure/reference.duckdb.sql b/compliance/query/cases/b18_filter_restricts_one_side_measure/reference.duckdb.sql new file mode 100644 index 000000000..896aae7ed --- /dev/null +++ b/compliance/query/cases/b18_filter_restricts_one_side_measure/reference.duckdb.sql @@ -0,0 +1,41 @@ +-- 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 B18 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.job, + e.empno AS e_k, + d.deptno AS d_k + FROM scott.emp AS e + JOIN scott.dept AS d ON e.deptno = d.deptno + WHERE e.sal > 1000), +spine AS (SELECT DISTINCT job FROM joined) +SELECT g.job, + (SELECT COUNT(*) + FROM (SELECT j.d_k + FROM joined AS j + WHERE j.d_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.d_k) AS x) AS dept_count, + (SELECT COUNT(*) + FROM (SELECT j.e_k + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.e_k) AS x) AS emp_count +FROM spine AS g +ORDER BY g.job; diff --git a/compliance/query/cases/b19_nonunique_filter_no_fan_out/expected.sql b/compliance/query/cases/b19_nonunique_filter_no_fan_out/expected.sql new file mode 100644 index 000000000..cfbd6f71f --- /dev/null +++ b/compliance/query/cases/b19_nonunique_filter_no_fan_out/expected.sql @@ -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 B19 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ('CLERK', 950.00), + ('MANAGER', 2850.00), + ('SALESMAN', 5600.00)) AS expected (job, sum_sal); diff --git a/compliance/query/cases/b19_nonunique_filter_no_fan_out/query.ossie.sql b/compliance/query/cases/b19_nonunique_filter_no_fan_out/query.ossie.sql new file mode 100644 index 000000000..5e3e836fe --- /dev/null +++ b/compliance/query/cases/b19_nonunique_filter_no_fan_out/query.ossie.sql @@ -0,0 +1,36 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B19. A join to a non-distinct filter relation does not inflate a measure. +-- Question: In the departments that have a salesman, what is the payroll of +-- each job? +-- The subquery lists the department of each salesman. Only department 30 has +-- a salesman, and the subquery returns it four times, once per salesman. The +-- join repeats each employee of department 30 four times, but AGG(e.sum_sal) +-- counts each employee once. A plain SUM(e.sal) over the joined rows gives +-- 3800, 11400, 22400. Here the duplication comes from the filter relation, +-- not from a finer-grained table (B6), and G2 still counts each source row +-- once. +-- Spec: 7.6, 8.4, G2 (8.3), A.2 +-- ---------------------------------------------------------------------------- +SELECT e.job, AGG(e.sum_sal) AS sum_sal +FROM scott_model.emp AS e +JOIN (SELECT deptno + FROM scott_model.emp + WHERE job = 'SALESMAN') AS t ON e.deptno = t.deptno +GROUP BY e.job +ORDER BY e.job; diff --git a/compliance/query/cases/b19_nonunique_filter_no_fan_out/reference.duckdb.sql b/compliance/query/cases/b19_nonunique_filter_no_fan_out/reference.duckdb.sql new file mode 100644 index 000000000..682ce5858 --- /dev/null +++ b/compliance/query/cases/b19_nonunique_filter_no_fan_out/reference.duckdb.sql @@ -0,0 +1,36 @@ +-- 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 B19 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. +WITH joined AS ( + SELECT e.job, + e.empno AS e_k, e.sal AS e_sal + FROM scott.emp AS e + JOIN (SELECT deptno + FROM scott.emp + WHERE job = 'SALESMAN') AS t ON e.deptno = t.deptno), +spine AS (SELECT DISTINCT job FROM joined) +SELECT g.job, + (SELECT SUM(x.sal) + FROM (SELECT ANY_VALUE(j.e_sal) AS sal + FROM joined AS j + WHERE j.e_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.e_k) AS x) AS sum_sal +FROM spine AS g +ORDER BY g.job; diff --git a/compliance/query/cases/b20_having_on_one_side_measure/expected.sql b/compliance/query/cases/b20_having_on_one_side_measure/expected.sql new file mode 100644 index 000000000..7cd0581af --- /dev/null +++ b/compliance/query/cases/b20_having_on_one_side_measure/expected.sql @@ -0,0 +1,20 @@ +-- 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 B20 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES ('CLERK', 3), + ('MANAGER', 3)) AS expected (job, dept_count); diff --git a/compliance/query/cases/b20_having_on_one_side_measure/query.ossie.sql b/compliance/query/cases/b20_having_on_one_side_measure/query.ossie.sql new file mode 100644 index 000000000..15f2009b1 --- /dev/null +++ b/compliance/query/cases/b20_having_on_one_side_measure/query.ossie.sql @@ -0,0 +1,33 @@ +-- 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. +-- ---------------------------------------------------------------------------- +-- B20. HAVING on a "one"-side measure that the join fans out. +-- Question: Which jobs exist in more than one department, and in how many? +-- AGG(d.dept_count) may appear in HAVING, as any aggregate may (Section 7.5). +-- The join repeats each dept row once for each employee, and the measure +-- counts each department once, so HAVING keeps the jobs that span more than +-- one department: CLERK and MANAGER. A plain COUNT(*) > 1 in HAVING would use +-- the joined-row count and also keep ANALYST (2) and SALESMAN (4), whose +-- employees are all in one department. +-- Spec: 7.5, 7.6, G2 (8.3) +-- ---------------------------------------------------------------------------- +SELECT e.job, AGG(d.dept_count) AS dept_count +FROM scott_model.emp AS e +JOIN scott_model.dept AS d ON e.deptno = d.deptno +GROUP BY e.job +HAVING AGG(d.dept_count) > 1 +ORDER BY e.job; diff --git a/compliance/query/cases/b20_having_on_one_side_measure/reference.duckdb.sql b/compliance/query/cases/b20_having_on_one_side_measure/reference.duckdb.sql new file mode 100644 index 000000000..4a7b0dd1b --- /dev/null +++ b/compliance/query/cases/b20_having_on_one_side_measure/reference.duckdb.sql @@ -0,0 +1,40 @@ +-- 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 B20 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +-- For the joins, it applies the reference evaluation strategy of Section 9, +-- as ../../README.md describes. HAVING is applied as a filter on the +-- evaluated measure. +WITH joined AS ( + SELECT e.job, + e.empno AS e_k, + d.deptno AS d_k + FROM scott.emp AS e + JOIN scott.dept AS d ON e.deptno = d.deptno), +spine AS (SELECT DISTINCT job FROM joined), +per_job AS ( + SELECT g.job AS job, + (SELECT COUNT(*) + FROM (SELECT j.d_k + FROM joined AS j + WHERE j.d_k IS NOT NULL AND j.job IS NOT DISTINCT FROM g.job + GROUP BY j.d_k) AS x) AS dept_count + FROM spine AS g) +SELECT job, dept_count +FROM per_job +WHERE dept_count > 1 +ORDER BY job; diff --git a/compliance/query/scott/scott_model.ossie.yaml b/compliance/query/scott/scott_model.ossie.yaml index 442f0ab4d..5b25a3ebb 100644 --- a/compliance/query/scott/scott_model.ossie.yaml +++ b/compliance/query/scott/scott_model.ossie.yaml @@ -30,10 +30,9 @@ # The primary_key of each dataset is the row identity of its measures # (Section 3.2). # -# The model has no AVG metric yet. For AVG over an exact numeric, the SQL -# standard leaves the scale of the result implementation-defined, so a case -# that needs AVG adds a metric over an approximate argument, for example -# AVG(CAST(sal AS DOUBLE PRECISION)) (../README.md, "Result semantics"). +# The AVG metrics cast their argument to DOUBLE. For AVG over an exact +# numeric, the SQL standard leaves the scale of the result +# implementation-defined (../README.md, "Result semantics"). # # The emp-to-salgrade join (e.sal BETWEEN s.losal AND s.hisal) is a range join. # An Ossie relationship cannot express it, so a case that uses it writes the @@ -49,45 +48,67 @@ datasets: fields: - name: empno datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: empno}]} + expression: + sql: empno - name: ename datatype: String - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: ename}]} + expression: + sql: ename - name: job datatype: String - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: job}]} + expression: + sql: job - name: mgr datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: mgr}]} + expression: + sql: mgr - name: hiredate datatype: Date - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: hiredate}]} + expression: + sql: hiredate - name: sal datatype: Decimal - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: sal}]} + expression: + sql: sal - name: comm datatype: Decimal - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: comm}]} + expression: + sql: comm - name: deptno datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: deptno}]} + expression: + sql: deptno metrics: - name: sum_sal description: Total salary datatype: Decimal - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: SUM(sal)}]} + expression: + sql: SUM(sal) + - name: avg_sal + description: Average salary + datatype: Float + expression: + sql: AVG(CAST(sal AS DOUBLE)) - name: emp_count description: Number of employees datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(*)}]} + expression: + sql: COUNT(*) - name: comm_count description: Number of employees that have a commission datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(comm)}]} + expression: + sql: COUNT(comm) - name: sum_comm description: Total commission datatype: Decimal - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: SUM(comm)}]} + expression: + sql: SUM(comm) + - name: avg_comm + description: Average commission, over the employees that have a commission + datatype: Float + expression: + sql: AVG(CAST(comm AS DOUBLE)) - name: dept source: scott.dept @@ -95,18 +116,22 @@ datasets: fields: - name: deptno datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: deptno}]} + expression: + sql: deptno - name: dname datatype: String - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: dname}]} + expression: + sql: dname - name: loc datatype: String - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: loc}]} + expression: + sql: loc metrics: - name: dept_count description: Number of departments datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(*)}]} + expression: + sql: COUNT(*) - name: salgrade source: scott.salgrade @@ -114,18 +139,22 @@ datasets: fields: - name: grade datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: grade}]} + expression: + sql: grade - name: losal datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: losal}]} + expression: + sql: losal - name: hisal datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: hisal}]} + expression: + sql: hisal metrics: - name: grade_count description: Number of salary grades datatype: Integer - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(*)}]} + expression: + sql: COUNT(*) relationships: - name: emp_to_dept