From a3dcba5eeadb764c8003bd67ff4b7ec1cc78ed81 Mon Sep 17 00:00:00 2001 From: Chris Eubank <108756251+christianeu-db@users.noreply.github.com> Date: Wed, 21 Oct 2026 14:53:32 +0000 Subject: [PATCH 1/7] test(compliance): add relational-semantics query compliance tests Add compliance/query/relational_semantics.sql, a DuckDB-runnable file of 29 test cases for core-spec/relational_semantics.md over the SCOTT schema. Each case pairs an Ossie query that uses AGG() (commented out) with an equivalent DuckDB query and its expected result. - Part A (12 cases): AGG() on one relation: grouping, filters, HAVING, NULL and empty-input values, ROLLUP, CTE propagation, subqueries. - Part B (17 cases): joins, including fan-out and many-to-many joins, and two queries run with each join type. The DuckDB queries use the reference evaluation strategy of Section 9. The model scott_model (commented out) uses dataset-scoped metrics (#591). The Ossie queries refer to its relations by two-part name, for example scott_model.emp. This is a light version of the .ost test format (#501) and view expander (#502). Co-authored-by: Isaac --- compliance/query/README.md | 44 + compliance/query/relational_semantics.sql | 1452 +++++++++++++++++++++ 2 files changed, 1496 insertions(+) create mode 100644 compliance/query/README.md create mode 100644 compliance/query/relational_semantics.sql diff --git a/compliance/query/README.md b/compliance/query/README.md new file mode 100644 index 000000000..3ce7f14b9 --- /dev/null +++ b/compliance/query/README.md @@ -0,0 +1,44 @@ + + +Query compliance tests +====================== + +Test cases for the +[Relational Query Interface](../../core-spec/relational_semantics.md). + +[`relational_semantics.sql`](relational_semantics.sql) loads the SCOTT schema +and defines an Ossie model over it with dataset-scoped metrics (in comments). +It then gives 29 test cases: 12 on one relation and 17 on joins. The join +cases include fan-out and many-to-many joins, and two queries that are run +with each join type. Each case is an Ossie query that uses `AGG()` (in +comments), an equivalent DuckDB query, and the expected result (in comments). +The DuckDB queries for joins follow the reference evaluation strategy of +Section 9 of the spec. + +Run the file with DuckDB. It needs no extensions: + +```sh +duckdb < compliance/query/relational_semantics.sql +``` + +These tests are a light version of the `.ost` test format +([#501](https://github.com/apache/ossie/issues/501)) and the view expander +([#502](https://github.com/apache/ossie/issues/502)). When those are +available, each case can become an `.ost` test. diff --git a/compliance/query/relational_semantics.sql b/compliance/query/relational_semantics.sql new file mode 100644 index 000000000..6f4b4e2e5 --- /dev/null +++ b/compliance/query/relational_semantics.sql @@ -0,0 +1,1452 @@ +-- 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. + +-- ============================================================================ +-- Compliance queries: Relational Query Interface (Layer 2) +-- +-- Test cases for core-spec/relational_semantics.md over the classic SCOTT +-- schema. Each case has three parts: +-- 1. An Ossie query that evaluates measures with AGG() (commented out). +-- 2. An equivalent DuckDB query over the base tables (runnable). +-- 3. The expected result (commented out). +-- +-- Every Ossie query evaluates its measures with AGG(). +-- +-- Part A covers the fundamentals on one relation. Part B covers joins: joins +-- that repeat no measure row, joins that repeat rows (fan-out and +-- many-to-many), and two queries that are run with each join type. For +-- joins, the DuckDB query applies the reference evaluation strategy of +-- Section 9 of the spec: it removes join duplicates by row-identity key (the +-- dataset's primary_key), then it aggregates. +-- +-- Run: +-- duckdb < compliance/query/relational_semantics.sql +-- +-- The file needs no extensions or imports. Everything that DuckDB cannot run +-- is commented out. +-- +-- Expected-result format: +-- * Column headers are in uppercase. +-- * Numbers are right-aligned. Strings and dates are left-aligned. +-- * NULL is shown as an empty cell. +-- * DOUBLE values are rounded to 2 decimal places. +-- +-- References: +-- * Spec: core-spec/relational_semantics.md (apache/ossie#354) +-- * Dataset-scoped metrics: apache/ossie#591 +-- * Test format: apache/ossie#501. View expander: apache/ossie#502. +-- ============================================================================ + + +-- ============================================================================ +-- Data: the SCOTT schema +-- +-- Values are the same as the SCOTT tables in Apache Calcite +-- (core/src/test/resources/sql/within-distinct.iq). +-- ============================================================================ + +DROP SCHEMA IF EXISTS scott CASCADE; +CREATE SCHEMA scott; + +CREATE TABLE scott.dept ( + deptno INTEGER NOT NULL PRIMARY KEY, + dname VARCHAR NOT NULL, + loc VARCHAR NOT NULL +); + +INSERT INTO scott.dept VALUES + (10, 'ACCOUNTING', 'NEW YORK'), + (20, 'RESEARCH', 'DALLAS'), + (30, 'SALES', 'CHICAGO'), + (40, 'OPERATIONS', 'BOSTON'); + +CREATE TABLE scott.emp ( + empno INTEGER NOT NULL PRIMARY KEY, + ename VARCHAR NOT NULL, + job VARCHAR NOT NULL, + mgr INTEGER, + hiredate DATE NOT NULL, + sal DECIMAL(7, 2) NOT NULL, + comm DECIMAL(7, 2), + deptno INTEGER NOT NULL +); + +INSERT INTO scott.emp VALUES + (7369, 'SMITH', 'CLERK', 7902, DATE '1980-12-17', 800.00, NULL, 20), + (7499, 'ALLEN', 'SALESMAN', 7698, DATE '1981-02-20', 1600.00, 300.00, 30), + (7521, 'WARD', 'SALESMAN', 7698, DATE '1981-02-22', 1250.00, 500.00, 30), + (7566, 'JONES', 'MANAGER', 7839, DATE '1981-02-04', 2975.00, NULL, 20), + (7654, 'MARTIN', 'SALESMAN', 7698, DATE '1981-09-28', 1250.00, 1400.00, 30), + (7698, 'BLAKE', 'MANAGER', 7839, DATE '1981-01-05', 2850.00, NULL, 30), + (7782, 'CLARK', 'MANAGER', 7839, DATE '1981-06-09', 2450.00, NULL, 10), + (7788, 'SCOTT', 'ANALYST', 7566, DATE '1987-04-19', 3000.00, NULL, 20), + (7839, 'KING', 'PRESIDENT', NULL, DATE '1981-11-17', 5000.00, NULL, 10), + (7844, 'TURNER', 'SALESMAN', 7698, DATE '1981-09-08', 1500.00, 0.00, 30), + (7876, 'ADAMS', 'CLERK', 7788, DATE '1987-05-23', 1100.00, NULL, 20), + (7900, 'JAMES', 'CLERK', 7698, DATE '1981-12-03', 950.00, NULL, 30), + (7902, 'FORD', 'ANALYST', 7566, DATE '1981-12-03', 3000.00, NULL, 20), + (7934, 'MILLER', 'CLERK', 7782, DATE '1982-01-23', 1300.00, NULL, 10); + +CREATE TABLE scott.salgrade ( + grade INTEGER NOT NULL PRIMARY KEY, + losal INTEGER NOT NULL, + hisal INTEGER NOT NULL +); + +INSERT INTO scott.salgrade VALUES + (1, 700, 1200), + (2, 1201, 1400), + (3, 1401, 2000), + (4, 2001, 3000), + (5, 3001, 9999); + +-- BONUS has no rows in SCOTT. It is here for completeness; no test uses it. +CREATE TABLE scott.bonus ( + ename VARCHAR, + job VARCHAR, + sal DECIMAL(7, 2), + comm DECIMAL(7, 2) +); + + +-- ============================================================================ +-- Model: Ossie semantic model over SCOTT (commented out) +-- +-- The metrics are dataset-scoped (datasets[].metrics, apache/ossie#591). Each +-- dataset is exposed as a relation with the same name, and each metric is a +-- measure column of that relation (spec Section 3.4). All relations of the +-- model are in one schema with the name of the model (spec Section 11.2). The +-- Ossie queries below refer to these relations by two-part name: +-- scott_model.emp, scott_model.dept, and scott_model.salgrade. +-- +-- The primary_key of each dataset is the row identity of its measures (spec +-- Section 3.2). +-- +-- The emp-to-salgrade join (e.sal BETWEEN s.losal AND s.hisal) is a range +-- join. An Ossie relationship cannot express it, so the consuming query +-- writes it (spec Section 11.3). +-- ============================================================================ + +-- version: 0.2.0.dev0 +-- name: scott_model +-- description: The SCOTT schema with dataset-scoped metrics +-- datasets: +-- - name: emp +-- source: scott.emp +-- primary_key: [empno] +-- fields: +-- - name: empno +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: empno}]} +-- - name: ename +-- datatype: String +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: ename}]} +-- - name: job +-- datatype: String +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: job}]} +-- - name: mgr +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: mgr}]} +-- - name: hiredate +-- datatype: Date +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: hiredate}]} +-- - name: sal +-- datatype: Decimal +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: sal}]} +-- - name: comm +-- datatype: Decimal +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: comm}]} +-- - name: deptno +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: deptno}]} +-- metrics: +-- - name: sum_sal +-- description: Total salary +-- datatype: Decimal +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: SUM(sal)}]} +-- - name: avg_sal +-- description: Average salary +-- datatype: Float +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: AVG(sal)}]} +-- - name: emp_count +-- description: Number of employees +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(*)}]} +-- - name: comm_count +-- description: Number of employees that have a commission +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(comm)}]} +-- - name: sum_comm +-- description: Total commission +-- datatype: Decimal +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: SUM(comm)}]} +-- - name: avg_comm +-- description: Average commission, over employees that have a commission +-- datatype: Float +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: AVG(comm)}]} +-- +-- - name: dept +-- source: scott.dept +-- primary_key: [deptno] +-- fields: +-- - name: deptno +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: deptno}]} +-- - name: dname +-- datatype: String +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: dname}]} +-- - name: loc +-- datatype: String +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: loc}]} +-- metrics: +-- - name: dept_count +-- description: Number of departments +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(*)}]} +-- +-- - name: salgrade +-- source: scott.salgrade +-- primary_key: [grade] +-- fields: +-- - name: grade +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: grade}]} +-- - name: losal +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: losal}]} +-- - name: hisal +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: hisal}]} +-- metrics: +-- - name: grade_count +-- description: Number of salary grades +-- datatype: Integer +-- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(*)}]} +-- +-- relationships: +-- - name: emp_to_dept +-- from: emp +-- to: dept +-- from_columns: [deptno] +-- to_columns: [deptno] +-- - name: emp_to_manager +-- from: emp +-- to: emp +-- from_columns: [mgr] +-- to_columns: [empno] + + +-- ============================================================================ +-- Part A: fundamentals (one relation) +-- +-- On one relation, no join repeats rows. So AGG(m) gives the same result as +-- the metric's aggregate over the same rows. Each DuckDB query is the Ossie +-- query with each AGG(m) replaced by the expression of m, and with each model +-- relation (scott_model.emp) replaced by its source table (scott.emp). +-- ============================================================================ + +-- ---------------------------------------------------------------------------- +-- A1. Grand total. +-- AGG() is an aggregate. Without GROUP BY, the query returns one row. +-- Spec: 6, 7.2 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- SELECT AGG(sum_sal) AS sum_sal, +-- AGG(emp_count) AS emp_count, +-- AGG(avg_sal) AS avg_sal +-- FROM scott_model.emp; +SELECT SUM(sal) AS sum_sal, + COUNT(*) AS emp_count, + AVG(sal) AS avg_sal +FROM scott.emp; +-- +----------+-----------+---------+ +-- | SUM_SAL | EMP_COUNT | AVG_SAL | +-- +----------+-----------+---------+ +-- | 29025.00 | 14 | 2073.21 | +-- +----------+-----------+---------+ +-- (1 row) + +-- ---------------------------------------------------------------------------- +-- A2. GROUP BY a field. +-- The grouping sets the level at which each measure is reported. +-- Spec: 7.4, G1 (8.2) +-- ---------------------------------------------------------------------------- +-- Ossie: +-- SELECT deptno, +-- AGG(sum_sal) AS sum_sal, +-- AGG(emp_count) AS emp_count, +-- AGG(avg_sal) AS avg_sal +-- FROM scott_model.emp +-- GROUP BY deptno +-- ORDER BY deptno; +SELECT deptno, + SUM(sal) AS sum_sal, + COUNT(*) AS emp_count, + AVG(sal) AS avg_sal +FROM scott.emp +GROUP BY deptno +ORDER BY deptno; +-- +--------+----------+-----------+---------+ +-- | DEPTNO | SUM_SAL | EMP_COUNT | AVG_SAL | +-- +--------+----------+-----------+---------+ +-- | 10 | 8750.00 | 3 | 2916.67 | +-- | 20 | 10875.00 | 5 | 2175.00 | +-- | 30 | 9400.00 | 6 | 1566.67 | +-- +--------+----------+-----------+---------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- A3. WHERE on a field. +-- WHERE removes rows before evaluation, so each measure sees only the rows +-- that remain. +-- Spec: 7.3, G1 (8.2) +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +SELECT deptno, + SUM(sal) AS sum_sal, + COUNT(*) AS emp_count +FROM scott.emp +WHERE job <> 'CLERK' +GROUP BY deptno +ORDER BY deptno; +-- +--------+---------+-----------+ +-- | DEPTNO | SUM_SAL | EMP_COUNT | +-- +--------+---------+-----------+ +-- | 10 | 7450.00 | 2 | +-- | 20 | 8975.00 | 3 | +-- | 30 | 8450.00 | 5 | +-- +--------+---------+-----------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- A4. HAVING, ORDER BY, and LIMIT on measures. +-- 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 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +SELECT job, SUM(sal) AS sum_sal +FROM scott.emp +GROUP BY job +HAVING COUNT(*) > 2 +ORDER BY SUM(sal) DESC +LIMIT 3; +-- +----------+---------+ +-- | JOB | SUM_SAL | +-- +----------+---------+ +-- | MANAGER | 8275.00 | +-- | SALESMAN | 5600.00 | +-- | CLERK | 4150.00 | +-- +----------+---------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- A5. Scalar expressions over AGG() results. +-- Each AGG() gives a scalar value for each group. Scalar operators and +-- functions then apply to these values. AGG(sum_sal) / AGG(emp_count) is equal +-- to AGG(avg_sal). COALESCE replaces the NULL commission of departments 10 +-- and 20. +-- Spec: 7.2, 8.1 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- SELECT deptno, +-- AGG(sum_sal) / 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; +SELECT deptno, + SUM(sal) / COUNT(*) AS sal_per_emp, + AVG(sal) AS avg_sal, + SUM(sal) + COALESCE(SUM(comm), 0) AS total_pay +FROM scott.emp +GROUP BY deptno +ORDER BY deptno; +-- +--------+-------------+---------+-----------+ +-- | DEPTNO | SAL_PER_EMP | AVG_SAL | TOTAL_PAY | +-- +--------+-------------+---------+-----------+ +-- | 10 | 2916.67 | 2916.67 | 8750.00 | +-- | 20 | 2175.00 | 2175.00 | 10875.00 | +-- | 30 | 1566.67 | 1566.67 | 11600.00 | +-- +--------+-------------+---------+-----------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- A6. NULL inputs. +-- A measure follows the NULL rules of its aggregate. emp_count is COUNT(*) +-- and counts every row. comm_count is COUNT(comm) and ignores NULL. Departments +-- 10 and 20 have no commission values, so SUM and AVG give NULL. In department +-- 30, TURNER's 0.00 is a value, so AVG(comm) is 2200 / 4. +-- Spec: 8.1 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +SELECT deptno, + COUNT(*) AS emp_count, + COUNT(comm) AS comm_count, + SUM(comm) AS sum_comm, + AVG(comm) AS avg_comm +FROM scott.emp +GROUP BY deptno +ORDER BY deptno; +-- +--------+-----------+------------+----------+----------+ +-- | DEPTNO | EMP_COUNT | COMM_COUNT | SUM_COMM | AVG_COMM | +-- +--------+-----------+------------+----------+----------+ +-- | 10 | 3 | 0 | | | +-- | 20 | 5 | 0 | | | +-- | 30 | 6 | 4 | 2200.00 | 550.00 | +-- +--------+-----------+------------+----------+----------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- A7. Empty input set. +-- 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 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +SELECT COUNT(*) AS emp_count, + SUM(sal) AS sum_sal, + AVG(sal) AS avg_sal +FROM scott.emp +WHERE deptno = 40; +-- +-----------+---------+---------+ +-- | EMP_COUNT | SUM_SAL | AVG_SAL | +-- +-----------+---------+---------+ +-- | 0 | | | +-- +-----------+---------+---------+ +-- (1 row) + +-- ---------------------------------------------------------------------------- +-- A8. GROUP BY ALL on an expression. +-- 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 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +-----------+-----------+----------+ +-- | HIRE_YEAR | EMP_COUNT | SUM_SAL | +-- +-----------+-----------+----------+ +-- | 1980 | 1 | 800.00 | +-- | 1981 | 10 | 22825.00 | +-- | 1982 | 1 | 1300.00 | +-- | 1987 | 2 | 4100.00 | +-- +-----------+-----------+----------+ +-- (4 rows) + +-- ---------------------------------------------------------------------------- +-- A9. ROLLUP. +-- A measure is evaluated again for each grouping set. 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) +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +SELECT deptno, + COUNT(*) AS emp_count, + AVG(sal) AS avg_sal +FROM scott.emp +GROUP BY ROLLUP (deptno) +ORDER BY deptno NULLS LAST; +-- +--------+-----------+---------+ +-- | DEPTNO | EMP_COUNT | AVG_SAL | +-- +--------+-----------+---------+ +-- | 10 | 3 | 2916.67 | +-- | 20 | 5 | 2175.00 | +-- | 30 | 6 | 1566.67 | +-- | | 14 | 2073.21 | +-- +--------+-----------+---------+ +-- (4 rows) + +-- ---------------------------------------------------------------------------- +-- A10. Measure through a CTE that filters and renames. +-- 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 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +--------+---------+-----------+ +-- | DEPTNO | PAYROLL | HEADCOUNT | +-- +--------+---------+-----------+ +-- | 10 | 1300.00 | 1 | +-- | 20 | 1900.00 | 2 | +-- | 30 | 6550.00 | 5 | +-- +--------+---------+-----------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- A11. AGG() in a scalar subquery. +-- AGG() cannot appear directly in WHERE, but a scalar subquery in WHERE can +-- use it. The query counts, by department, the employees whose salary is +-- greater than the company average (2073.21). +-- Spec: 7.3 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +SELECT deptno, COUNT(*) AS emp_count +FROM scott.emp +WHERE sal > (SELECT AVG(sal) FROM scott.emp) +GROUP BY deptno +ORDER BY deptno; +-- +--------+-----------+ +-- | DEPTNO | EMP_COUNT | +-- +--------+-----------+ +-- | 10 | 2 | +-- | 20 | 3 | +-- | 30 | 1 | +-- +--------+-----------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- A12. Two-stage aggregation and a set operator. +-- The first branch averages the per-department payroll (29025 / 3). The +-- second branch is the average salary per employee. After AGG() evaluates the +-- measures, the values are ordinary scalars, so UNION ALL is valid. +-- Spec: A.3, 7.8 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- SELECT 'avg payroll per dept' AS metric, AVG(dept_sal) 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; +SELECT 'avg payroll per dept' AS metric, AVG(dept_sal) 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(sal) +FROM scott.emp +ORDER BY metric; +-- +----------------------+---------+ +-- | METRIC | AMOUNT | +-- +----------------------+---------+ +-- | avg payroll per dept | 9675.00 | +-- | avg salary per emp | 2073.21 | +-- +----------------------+---------+ +-- (2 rows) + + +-- ============================================================================ +-- Part B: joins +-- +-- A join can repeat a row of a measure's source (fan-out). An outer join can +-- also add NULL-padded rows that come from no source row. AGG(m) aggregates +-- each source row of m once and ignores NULL-padded rows (spec Section 8.1). +-- The comments give the result of a plain aggregate over the joined rows +-- ("naive") where it differs. +-- +-- Each DuckDB query applies the reference strategy of spec Section 9: +-- 1. The CTE "joined" is the Ossie query's FROM, JOIN, and WHERE. For each +-- measure, it also carries the row-identity key (_k, which is the +-- primary key of the dataset) and the arguments of the aggregate. +-- 2. The CTE "spine" has one row for each group: SELECT DISTINCT over the +-- grouping columns. A grand total has one group and needs no spine. +-- 3. For each measure, a correlated subquery selects the joined rows of the +-- group, removes the rows where the key is NULL, reduces the rows to one +-- for each key (GROUP BY key), and then applies the aggregate. +-- 4. Scalar expressions over AGG() results apply to the per-group values. +-- +-- B1-B3: joins that repeat no row of the measure's source. +-- B4-B8: joins that repeat rows: fan-out and many-to-many. +-- B9-B13: one self-join query with each join type. +-- B14-B17: one dept-emp query with inner and outer joins. +-- ============================================================================ + +-- ---------------------------------------------------------------------------- +-- B1. Many-to-one join, group by a field of the joined relation. +-- Each employee matches one department, so the join repeats no emp row. The +-- measures are the same as the per-department totals of A2. +-- Spec: 7.6 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +------------+----------+-----------+ +-- | DNAME | SUM_SAL | EMP_COUNT | +-- +------------+----------+-----------+ +-- | ACCOUNTING | 8750.00 | 3 | +-- | RESEARCH | 10875.00 | 5 | +-- | SALES | 9400.00 | 6 | +-- +------------+----------+-----------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- B2. WHERE on a field of the joined relation. +-- 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) +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +-----------+---------+-----------+ +-- | JOB | SUM_SAL | EMP_COUNT | +-- +-----------+---------+-----------+ +-- | CLERK | 1300.00 | 1 | +-- | MANAGER | 2450.00 | 1 | +-- | PRESIDENT | 5000.00 | 1 | +-- +-----------+---------+-----------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- B3. Top-N filter through a join. +-- The subquery selects the two departments with the largest payroll (20 and +-- 30). The join to it acts as a filter on emp, and the outer query reports +-- payroll by job for those departments. The departments that the subquery +-- ranks are the same values that the join matches (closure, spec 8.4). +-- Spec: 7.6, 8.4, A.2 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +----------+---------+ +-- | JOB | SUM_SAL | +-- +----------+---------+ +-- | ANALYST | 6000.00 | +-- | CLERK | 2850.00 | +-- | MANAGER | 5825.00 | +-- | SALESMAN | 5600.00 | +-- +----------+---------+ +-- (4 rows) + +-- ---------------------------------------------------------------------------- +-- B4. Fan-out: a measure of the "one" side, grouped by the "many" side. +-- The join repeats each dept row once for each employee in it. AGG(dept_count) +-- counts each department once in each group, so the result is the number of +-- departments that have the job. Naive COUNT(*) gives 2, 4, 3, 1, 4, which +-- AGG(e.emp_count) also gives, because the join repeats no emp row. +-- Spec: 7.6, G2 (8.3) +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +-----------+------------+-----------+ +-- | JOB | DEPT_COUNT | EMP_COUNT | +-- +-----------+------------+-----------+ +-- | ANALYST | 1 | 2 | +-- | CLERK | 3 | 4 | +-- | MANAGER | 3 | 3 | +-- | PRESIDENT | 1 | 1 | +-- | SALESMAN | 1 | 4 | +-- +-----------+------------+-----------+ +-- (5 rows) + +-- ---------------------------------------------------------------------------- +-- B5. Fan-out in a self-join: the manager's salary. +-- e is the employee and m is the manager. The join repeats each manager row +-- once for each report. AGG(m.sum_sal) counts each manager's salary once in +-- each group. The 4 salesmen all report to BLAKE, so the result is 2850; the +-- naive SUM(m.sal) is 11400. The 4 clerks report to 4 different managers, so +-- the two values are the same (11300). Naive SUM(m.sal) by job is 5950, +-- 11300, 15000, 11400. +-- Spec: 7.6, 8.1, G2 (8.3) +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +----------+-----------+----------+--------------+ +-- | JOB | EMP_COUNT | MANAGERS | MANAGERS_SAL | +-- +----------+-----------+----------+--------------+ +-- | ANALYST | 2 | 1 | 2975.00 | +-- | CLERK | 4 | 4 | 11300.00 | +-- | MANAGER | 3 | 1 | 5000.00 | +-- | SALESMAN | 4 | 1 | 2850.00 | +-- +----------+-----------+----------+--------------+ +-- (4 rows) + +-- ---------------------------------------------------------------------------- +-- B6. Fan-out through a range join. +-- The join condition can be any predicate over fields, not only an equality. +-- Each employee matches one salary grade, and a 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. Naive COUNT(*) +-- gives 3, 5, 6. +-- Spec: 7.6, G2 (8.3) +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +--------+-----------+-------------+ +-- | DEPTNO | EMP_COUNT | GRADE_COUNT | +-- +--------+-----------+-------------+ +-- | 10 | 3 | 3 | +-- | 20 | 5 | 2 | +-- | 30 | 6 | 4 | +-- +--------+-----------+-------------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- B7. Fan-out on two sides of a three-way join. +-- emp joins to dept and to salgrade. The group is the salary grade, and +-- 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. Naive COUNT(*) gives 3, 3, 2, 5, 1. +-- Spec: 7.6, G2 (8.3) +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +-------+-----------+----------+------------+ +-- | GRADE | EMP_COUNT | SUM_SAL | DEPT_COUNT | +-- +-------+-----------+----------+------------+ +-- | 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 | +-- +-------+-----------+----------+------------+ +-- (5 rows) + +-- ---------------------------------------------------------------------------- +-- B8. Many-to-many join (chasm trap). +-- 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. Naive SUM(e.sal) gives 26250, +-- 54375, 56400, and naive COUNT(*) gives 9, 25, 36. +-- Spec: 7.6, G2 (8.3) +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +--------+----------+------------+ +-- | DEPTNO | SUM_SAL | COLLEAGUES | +-- +--------+----------+------------+ +-- | 10 | 8750.00 | 3 | +-- | 20 | 10875.00 | 5 | +-- | 30 | 9400.00 | 6 | +-- +--------+----------+------------+ +-- (3 rows) + +-- ---------------------------------------------------------------------------- +-- B9-B13. One self-join query with each join type. +-- +-- e is the employee and m is the manager, joined on e.mgr = m.empno. Both +-- sides have rows that do not match: +-- * KING has no manager, so KING has no match on the e side. +-- * 8 employees manage nobody, so they have no match on the m side. +-- The join type decides which source rows reach each measure. The join never +-- inflates a measure, and NULL-padded rows add nothing: +-- +-- join type joined rows e_count e_sal m_count m_sal +-- INNER 13 13 24025.00 6 19275.00 +-- LEFT 14 14 29025.00 6 19275.00 +-- RIGHT 21 13 24025.00 14 29025.00 +-- FULL 22 14 29025.00 14 29025.00 +-- CROSS 196 14 29025.00 14 29025.00 +-- +-- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 +-- times and BLAKE's 5 times. +-- Spec: 7.6, 8.1, G2 (8.3), A.6 +-- ---------------------------------------------------------------------------- + +-- ---------------------------------------------------------------------------- +-- B9. INNER JOIN. +-- Only employees that have a manager, and only employees that are managers. +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +---------+----------+---------+----------+ +-- | E_COUNT | E_SAL | M_COUNT | M_SAL | +-- +---------+----------+---------+----------+ +-- | 13 | 24025.00 | 6 | 19275.00 | +-- +---------+----------+---------+----------+ +-- (1 row) + +-- ---------------------------------------------------------------------------- +-- B10. LEFT JOIN. +-- KING is kept on the e side. KING's row is NULL-padded on the m side, so the +-- m measures do not change. +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +---------+----------+---------+----------+ +-- | E_COUNT | E_SAL | M_COUNT | M_SAL | +-- +---------+----------+---------+----------+ +-- | 14 | 29025.00 | 6 | 19275.00 | +-- +---------+----------+---------+----------+ +-- (1 row) + +-- ---------------------------------------------------------------------------- +-- B11. RIGHT JOIN. +-- 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. +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +---------+----------+---------+----------+ +-- | E_COUNT | E_SAL | M_COUNT | M_SAL | +-- +---------+----------+---------+----------+ +-- | 13 | 24025.00 | 14 | 29025.00 | +-- +---------+----------+---------+----------+ +-- (1 row) + +-- ---------------------------------------------------------------------------- +-- B12. FULL OUTER JOIN. +-- Both sides keep all 14 employees. Each side counts each employee once, +-- although the join has 22 rows. +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +---------+----------+---------+----------+ +-- | E_COUNT | E_SAL | M_COUNT | M_SAL | +-- +---------+----------+---------+----------+ +-- | 14 | 29025.00 | 14 | 29025.00 | +-- +---------+----------+---------+----------+ +-- (1 row) + +-- ---------------------------------------------------------------------------- +-- B13. CROSS JOIN. +-- The cross join has no condition and gives 14 x 14 = 196 rows. Each side +-- still counts each employee once. Naive SUM(e.sal) is 406350. +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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; +-- +---------+----------+---------+----------+ +-- | E_COUNT | E_SAL | M_COUNT | M_SAL | +-- +---------+----------+---------+----------+ +-- | 14 | 29025.00 | 14 | 29025.00 | +-- +---------+----------+---------+----------+ +-- (1 row) + +-- ---------------------------------------------------------------------------- +-- B14-B17. One dept-emp query with inner and outer joins. +-- +-- The query computes the payroll per department as a ratio of two measures +-- from different relations: AGG(e.sum_sal) / AGG(d.dept_count). The join +-- repeats each dept row once for each employee in it (fan-out), but +-- dept_count still counts each department once. Naive COUNT(d.deptno) is 14 +-- for INNER and 15 for LEFT. +-- +-- The join type is the tool's choice, and it changes the answer (spec 10.2, +-- A.6). The LEFT join keeps OPERATIONS, which has no employees, so the +-- payroll is divided by 4 departments and not by 3. A filter in the ON clause +-- of the LEFT join keeps all 4 departments but matches only the clerks. +-- A RIGHT join with the sides swapped, or a FULL join, gives the LEFT +-- result, because every employee has a department. +-- Spec: 7.2, 7.6, 8.1, A.6 +-- ---------------------------------------------------------------------------- + +-- ---------------------------------------------------------------------------- +-- B14. INNER JOIN. +-- ---------------------------------------------------------------------------- +-- Ossie: +-- SELECT AGG(d.dept_count) AS dept_count, +-- AGG(e.emp_count) AS emp_count, +-- AGG(e.sum_sal) AS sum_sal, +-- AGG(e.sum_sal) / 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; +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, sum_sal / dept_count AS sal_per_dept +FROM measures; +-- +------------+-----------+----------+--------------+ +-- | DEPT_COUNT | EMP_COUNT | SUM_SAL | SAL_PER_DEPT | +-- +------------+-----------+----------+--------------+ +-- | 3 | 14 | 29025.00 | 9675.00 | +-- +------------+-----------+----------+--------------+ +-- (1 row) + +-- ---------------------------------------------------------------------------- +-- B15. LEFT JOIN. +-- ---------------------------------------------------------------------------- +-- Ossie: +-- SELECT AGG(d.dept_count) AS dept_count, +-- AGG(e.emp_count) AS emp_count, +-- AGG(e.sum_sal) AS sum_sal, +-- AGG(e.sum_sal) / 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; +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, sum_sal / dept_count AS sal_per_dept +FROM measures; +-- +------------+-----------+----------+--------------+ +-- | DEPT_COUNT | EMP_COUNT | SUM_SAL | SAL_PER_DEPT | +-- +------------+-----------+----------+--------------+ +-- | 4 | 14 | 29025.00 | 7256.25 | +-- +------------+-----------+----------+--------------+ +-- (1 row) + +-- ---------------------------------------------------------------------------- +-- B16. LEFT JOIN with a filter in the ON clause. +-- 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. +-- ---------------------------------------------------------------------------- +-- Ossie: +-- SELECT AGG(d.dept_count) AS dept_count, +-- AGG(e.emp_count) AS emp_count, +-- AGG(e.sum_sal) AS sum_sal, +-- AGG(e.sum_sal) / 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'; +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, sum_sal / dept_count AS sal_per_dept +FROM measures; +-- +------------+-----------+---------+--------------+ +-- | DEPT_COUNT | EMP_COUNT | SUM_SAL | SAL_PER_DEPT | +-- +------------+-----------+---------+--------------+ +-- | 4 | 4 | 4150.00 | 1037.50 | +-- +------------+-----------+---------+--------------+ +-- (1 row) + +-- ---------------------------------------------------------------------------- +-- B17. LEFT JOIN, grouped: a group with an empty input set. +-- 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 +-- ---------------------------------------------------------------------------- +-- Ossie: +-- 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; +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(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 avg_sal +FROM spine AS g +ORDER BY g.dname; +-- +------------+-----------+----------+---------+ +-- | DNAME | EMP_COUNT | SUM_SAL | AVG_SAL | +-- +------------+-----------+----------+---------+ +-- | ACCOUNTING | 3 | 8750.00 | 2916.67 | +-- | OPERATIONS | 0 | | | +-- | RESEARCH | 5 | 10875.00 | 2175.00 | +-- | SALES | 6 | 9400.00 | 1566.67 | +-- +------------+-----------+----------+---------+ +-- (4 rows) + +-- End relational_semantics.sql From 2b246517b3f6a001b17623ed586f6185e98ff7ab Mon Sep 17 00:00:00 2001 From: Chris Eubank <108756251+christianeu-db@users.noreply.github.com> Date: Thu, 5 Nov 2026 06:56:47 +0000 Subject: [PATCH 2/7] test(compliance): split relational_semantics.sql into case directories Move cases A2-A12 and B1-B17 into the case format of #632, one directory per case. A1 is already on main. - query.ossie.sql: the Ossie query and the case header, copied from relational_semantics.sql without change. B9-B16 also get the shared header of their group. - reference.duckdb.sql: the DuckDB query, copied without change. - expected.sql: the result of the DuckDB query. The values are the values of the ASCII result tables (doubles rounded to 2 places in the tables). - scott_model.ossie.yaml: the avg_sal and avg_comm metrics of the model in relational_semantics.sql. Co-authored-by: Isaac --- .../query/cases/a02_group_by/expected.sql | 22 + .../query/cases/a02_group_by/query.ossie.sql | 28 + .../cases/a02_group_by/reference.duckdb.sql | 25 + compliance/query/cases/a03_where/expected.sql | 21 + .../query/cases/a03_where/query.ossie.sql | 29 + .../cases/a03_where/reference.duckdb.sql | 25 + .../a04_having_order_by_limit/expected.sql | 21 + .../a04_having_order_by_limit/query.ossie.sql | 30 + .../reference.duckdb.sql | 24 + .../cases/a05_scalar_expressions/expected.sql | 22 + .../a05_scalar_expressions/query.ossie.sql | 31 + .../reference.duckdb.sql | 25 + .../query/cases/a06_null_inputs/expected.sql | 22 + .../cases/a06_null_inputs/query.ossie.sql | 32 + .../a06_null_inputs/reference.duckdb.sql | 26 + .../cases/a07_empty_input_set/expected.sql | 20 + .../cases/a07_empty_input_set/query.ossie.sql | 27 + .../a07_empty_input_set/reference.duckdb.sql | 23 + .../query/cases/a08_group_by_all/expected.sql | 23 + .../cases/a08_group_by_all/query.ossie.sql | 28 + .../a08_group_by_all/reference.duckdb.sql | 24 + .../query/cases/a09_rollup/expected.sql | 23 + .../query/cases/a09_rollup/query.ossie.sql | 29 + .../cases/a09_rollup/reference.duckdb.sql | 24 + .../cases/a10_cte_filter_rename/expected.sql | 21 + .../a10_cte_filter_rename/query.ossie.sql | 31 + .../reference.duckdb.sql | 26 + .../cases/a11_scalar_subquery/expected.sql | 21 + .../cases/a11_scalar_subquery/query.ossie.sql | 28 + .../a11_scalar_subquery/reference.duckdb.sql | 23 + .../a12_two_stage_aggregation/expected.sql | 21 + .../a12_two_stage_aggregation/query.ossie.sql | 31 + .../reference.duckdb.sql | 26 + .../cases/b01_many_to_one_join/expected.sql | 22 + .../b01_many_to_one_join/query.ossie.sql | 29 + .../b01_many_to_one_join/reference.duckdb.sql | 37 + .../b02_where_on_joined_relation/expected.sql | 21 + .../query.ossie.sql | 30 + .../reference.duckdb.sql | 38 + .../query/cases/b03_top_n_filter/expected.sql | 22 + .../cases/b03_top_n_filter/query.ossie.sql | 33 + .../b03_top_n_filter/reference.duckdb.sql | 36 + .../query/cases/b04_fan_out/expected.sql | 23 + .../query/cases/b04_fan_out/query.ossie.sql | 31 + .../cases/b04_fan_out/reference.duckdb.sql | 38 + .../cases/b05_fan_out_self_join/expected.sql | 23 + .../b05_fan_out_self_join/query.ossie.sql | 34 + .../reference.duckdb.sql | 43 + .../cases/b06_fan_out_range_join/expected.sql | 21 + .../b06_fan_out_range_join/query.ossie.sql | 32 + .../reference.duckdb.sql | 38 + .../b07_fan_out_three_way_join/expected.sql | 24 + .../query.ossie.sql | 33 + .../reference.duckdb.sql | 44 + .../cases/b08_many_to_many_join/expected.sql | 21 + .../b08_many_to_many_join/query.ossie.sql | 31 + .../reference.duckdb.sql | 38 + .../cases/b09_self_join_inner/expected.sql | 20 + .../cases/b09_self_join_inner/query.ossie.sql | 46 + .../b09_self_join_inner/reference.duckdb.sql | 43 + .../cases/b10_self_join_left/expected.sql | 20 + .../cases/b10_self_join_left/query.ossie.sql | 47 + .../b10_self_join_left/reference.duckdb.sql | 43 + .../cases/b11_self_join_right/expected.sql | 20 + .../cases/b11_self_join_right/query.ossie.sql | 47 + .../b11_self_join_right/reference.duckdb.sql | 43 + .../cases/b12_self_join_full/expected.sql | 20 + .../cases/b12_self_join_full/query.ossie.sql | 47 + .../b12_self_join_full/reference.duckdb.sql | 43 + .../cases/b13_self_join_cross/expected.sql | 20 + .../cases/b13_self_join_cross/query.ossie.sql | 47 + .../b13_self_join_cross/reference.duckdb.sql | 43 + .../cases/b14_dept_emp_inner/expected.sql | 20 + .../cases/b14_dept_emp_inner/query.ossie.sql | 41 + .../b14_dept_emp_inner/reference.duckdb.sql | 41 + .../cases/b15_dept_emp_left/expected.sql | 20 + .../cases/b15_dept_emp_left/query.ossie.sql | 41 + .../b15_dept_emp_left/reference.duckdb.sql | 41 + .../b16_dept_emp_left_on_filter/expected.sql | 20 + .../query.ossie.sql | 44 + .../reference.duckdb.sql | 41 + .../b17_dept_emp_left_grouped/expected.sql | 23 + .../b17_dept_emp_left_grouped/query.ossie.sql | 32 + .../reference.duckdb.sql | 42 + compliance/query/relational_semantics.sql | 1452 ----------------- compliance/query/scott/scott_model.ossie.yaml | 8 + 86 files changed, 2537 insertions(+), 1452 deletions(-) create mode 100644 compliance/query/cases/a02_group_by/expected.sql create mode 100644 compliance/query/cases/a02_group_by/query.ossie.sql create mode 100644 compliance/query/cases/a02_group_by/reference.duckdb.sql create mode 100644 compliance/query/cases/a03_where/expected.sql create mode 100644 compliance/query/cases/a03_where/query.ossie.sql create mode 100644 compliance/query/cases/a03_where/reference.duckdb.sql create mode 100644 compliance/query/cases/a04_having_order_by_limit/expected.sql create mode 100644 compliance/query/cases/a04_having_order_by_limit/query.ossie.sql create mode 100644 compliance/query/cases/a04_having_order_by_limit/reference.duckdb.sql create mode 100644 compliance/query/cases/a05_scalar_expressions/expected.sql create mode 100644 compliance/query/cases/a05_scalar_expressions/query.ossie.sql create mode 100644 compliance/query/cases/a05_scalar_expressions/reference.duckdb.sql create mode 100644 compliance/query/cases/a06_null_inputs/expected.sql create mode 100644 compliance/query/cases/a06_null_inputs/query.ossie.sql create mode 100644 compliance/query/cases/a06_null_inputs/reference.duckdb.sql create mode 100644 compliance/query/cases/a07_empty_input_set/expected.sql create mode 100644 compliance/query/cases/a07_empty_input_set/query.ossie.sql create mode 100644 compliance/query/cases/a07_empty_input_set/reference.duckdb.sql create mode 100644 compliance/query/cases/a08_group_by_all/expected.sql create mode 100644 compliance/query/cases/a08_group_by_all/query.ossie.sql create mode 100644 compliance/query/cases/a08_group_by_all/reference.duckdb.sql create mode 100644 compliance/query/cases/a09_rollup/expected.sql create mode 100644 compliance/query/cases/a09_rollup/query.ossie.sql create mode 100644 compliance/query/cases/a09_rollup/reference.duckdb.sql create mode 100644 compliance/query/cases/a10_cte_filter_rename/expected.sql create mode 100644 compliance/query/cases/a10_cte_filter_rename/query.ossie.sql create mode 100644 compliance/query/cases/a10_cte_filter_rename/reference.duckdb.sql create mode 100644 compliance/query/cases/a11_scalar_subquery/expected.sql create mode 100644 compliance/query/cases/a11_scalar_subquery/query.ossie.sql create mode 100644 compliance/query/cases/a11_scalar_subquery/reference.duckdb.sql create mode 100644 compliance/query/cases/a12_two_stage_aggregation/expected.sql create mode 100644 compliance/query/cases/a12_two_stage_aggregation/query.ossie.sql create mode 100644 compliance/query/cases/a12_two_stage_aggregation/reference.duckdb.sql create mode 100644 compliance/query/cases/b01_many_to_one_join/expected.sql create mode 100644 compliance/query/cases/b01_many_to_one_join/query.ossie.sql create mode 100644 compliance/query/cases/b01_many_to_one_join/reference.duckdb.sql create mode 100644 compliance/query/cases/b02_where_on_joined_relation/expected.sql create mode 100644 compliance/query/cases/b02_where_on_joined_relation/query.ossie.sql create mode 100644 compliance/query/cases/b02_where_on_joined_relation/reference.duckdb.sql create mode 100644 compliance/query/cases/b03_top_n_filter/expected.sql create mode 100644 compliance/query/cases/b03_top_n_filter/query.ossie.sql create mode 100644 compliance/query/cases/b03_top_n_filter/reference.duckdb.sql create mode 100644 compliance/query/cases/b04_fan_out/expected.sql create mode 100644 compliance/query/cases/b04_fan_out/query.ossie.sql create mode 100644 compliance/query/cases/b04_fan_out/reference.duckdb.sql create mode 100644 compliance/query/cases/b05_fan_out_self_join/expected.sql create mode 100644 compliance/query/cases/b05_fan_out_self_join/query.ossie.sql create mode 100644 compliance/query/cases/b05_fan_out_self_join/reference.duckdb.sql create mode 100644 compliance/query/cases/b06_fan_out_range_join/expected.sql create mode 100644 compliance/query/cases/b06_fan_out_range_join/query.ossie.sql create mode 100644 compliance/query/cases/b06_fan_out_range_join/reference.duckdb.sql create mode 100644 compliance/query/cases/b07_fan_out_three_way_join/expected.sql create mode 100644 compliance/query/cases/b07_fan_out_three_way_join/query.ossie.sql create mode 100644 compliance/query/cases/b07_fan_out_three_way_join/reference.duckdb.sql create mode 100644 compliance/query/cases/b08_many_to_many_join/expected.sql create mode 100644 compliance/query/cases/b08_many_to_many_join/query.ossie.sql create mode 100644 compliance/query/cases/b08_many_to_many_join/reference.duckdb.sql create mode 100644 compliance/query/cases/b09_self_join_inner/expected.sql create mode 100644 compliance/query/cases/b09_self_join_inner/query.ossie.sql create mode 100644 compliance/query/cases/b09_self_join_inner/reference.duckdb.sql create mode 100644 compliance/query/cases/b10_self_join_left/expected.sql create mode 100644 compliance/query/cases/b10_self_join_left/query.ossie.sql create mode 100644 compliance/query/cases/b10_self_join_left/reference.duckdb.sql create mode 100644 compliance/query/cases/b11_self_join_right/expected.sql create mode 100644 compliance/query/cases/b11_self_join_right/query.ossie.sql create mode 100644 compliance/query/cases/b11_self_join_right/reference.duckdb.sql create mode 100644 compliance/query/cases/b12_self_join_full/expected.sql create mode 100644 compliance/query/cases/b12_self_join_full/query.ossie.sql create mode 100644 compliance/query/cases/b12_self_join_full/reference.duckdb.sql create mode 100644 compliance/query/cases/b13_self_join_cross/expected.sql create mode 100644 compliance/query/cases/b13_self_join_cross/query.ossie.sql create mode 100644 compliance/query/cases/b13_self_join_cross/reference.duckdb.sql create mode 100644 compliance/query/cases/b14_dept_emp_inner/expected.sql create mode 100644 compliance/query/cases/b14_dept_emp_inner/query.ossie.sql create mode 100644 compliance/query/cases/b14_dept_emp_inner/reference.duckdb.sql create mode 100644 compliance/query/cases/b15_dept_emp_left/expected.sql create mode 100644 compliance/query/cases/b15_dept_emp_left/query.ossie.sql create mode 100644 compliance/query/cases/b15_dept_emp_left/reference.duckdb.sql create mode 100644 compliance/query/cases/b16_dept_emp_left_on_filter/expected.sql create mode 100644 compliance/query/cases/b16_dept_emp_left_on_filter/query.ossie.sql create mode 100644 compliance/query/cases/b16_dept_emp_left_on_filter/reference.duckdb.sql create mode 100644 compliance/query/cases/b17_dept_emp_left_grouped/expected.sql create mode 100644 compliance/query/cases/b17_dept_emp_left_grouped/query.ossie.sql create mode 100644 compliance/query/cases/b17_dept_emp_left_grouped/reference.duckdb.sql delete mode 100644 compliance/query/relational_semantics.sql diff --git a/compliance/query/cases/a02_group_by/expected.sql b/compliance/query/cases/a02_group_by/expected.sql new file mode 100644 index 000000000..bba5497f7 --- /dev/null +++ b/compliance/query/cases/a02_group_by/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 A2 (query.ossie.sql), in standard SQL. +SELECT * +FROM (VALUES (10, 8750.00, 3, 2.9166666666666665E3), + (20, 10875.00, 5, 2.175E3), + (30, 9400.00, 6, 1.5666666666666667E3)) + AS expected (deptno, sum_sal, emp_count, avg_sal); diff --git a/compliance/query/cases/a02_group_by/query.ossie.sql b/compliance/query/cases/a02_group_by/query.ossie.sql new file mode 100644 index 000000000..f709f97e3 --- /dev/null +++ b/compliance/query/cases/a02_group_by/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. GROUP BY a field. +-- 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, + AGG(avg_sal) AS avg_sal +FROM scott_model.emp +GROUP BY deptno +ORDER BY deptno; diff --git a/compliance/query/cases/a02_group_by/reference.duckdb.sql b/compliance/query/cases/a02_group_by/reference.duckdb.sql new file mode 100644 index 000000000..176cb0dd6 --- /dev/null +++ b/compliance/query/cases/a02_group_by/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 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, + AVG(sal) AS avg_sal +FROM scott.emp +GROUP BY deptno +ORDER BY deptno; diff --git a/compliance/query/cases/a03_where/expected.sql b/compliance/query/cases/a03_where/expected.sql new file mode 100644 index 000000000..e7884ba22 --- /dev/null +++ b/compliance/query/cases/a03_where/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/query.ossie.sql b/compliance/query/cases/a03_where/query.ossie.sql new file mode 100644 index 000000000..95d488fcf --- /dev/null +++ b/compliance/query/cases/a03_where/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. +-- ---------------------------------------------------------------------------- +-- A3. WHERE on a field. +-- WHERE removes rows before evaluation, so each measure sees only the rows +-- that remain. +-- 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/reference.duckdb.sql b/compliance/query/cases/a03_where/reference.duckdb.sql new file mode 100644 index 000000000..59f689c16 --- /dev/null +++ b/compliance/query/cases/a03_where/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_order_by_limit/expected.sql b/compliance/query/cases/a04_having_order_by_limit/expected.sql new file mode 100644 index 000000000..1227e790b --- /dev/null +++ b/compliance/query/cases/a04_having_order_by_limit/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_order_by_limit/query.ossie.sql b/compliance/query/cases/a04_having_order_by_limit/query.ossie.sql new file mode 100644 index 000000000..f8e694c6d --- /dev/null +++ b/compliance/query/cases/a04_having_order_by_limit/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. +-- ---------------------------------------------------------------------------- +-- A4. HAVING, ORDER BY, and LIMIT on measures. +-- 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_order_by_limit/reference.duckdb.sql b/compliance/query/cases/a04_having_order_by_limit/reference.duckdb.sql new file mode 100644 index 000000000..11ec674f3 --- /dev/null +++ b/compliance/query/cases/a04_having_order_by_limit/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_expressions/expected.sql b/compliance/query/cases/a05_scalar_expressions/expected.sql new file mode 100644 index 000000000..aa4d3d07d --- /dev/null +++ b/compliance/query/cases/a05_scalar_expressions/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_expressions/query.ossie.sql b/compliance/query/cases/a05_scalar_expressions/query.ossie.sql new file mode 100644 index 000000000..b68faff3d --- /dev/null +++ b/compliance/query/cases/a05_scalar_expressions/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. +-- ---------------------------------------------------------------------------- +-- A5. Scalar expressions over AGG() results. +-- Each AGG() gives a scalar value for each group. Scalar operators and +-- functions then apply to these values. AGG(sum_sal) / AGG(emp_count) is equal +-- to AGG(avg_sal). COALESCE replaces the NULL commission of departments 10 +-- and 20. +-- Spec: 7.2, 8.1 +-- ---------------------------------------------------------------------------- +SELECT deptno, + AGG(sum_sal) / 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_expressions/reference.duckdb.sql b/compliance/query/cases/a05_scalar_expressions/reference.duckdb.sql new file mode 100644 index 000000000..2c8cfcb20 --- /dev/null +++ b/compliance/query/cases/a05_scalar_expressions/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, + SUM(sal) / COUNT(*) AS sal_per_emp, + AVG(sal) 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_null_inputs/expected.sql b/compliance/query/cases/a06_null_inputs/expected.sql new file mode 100644 index 000000000..21bff599d --- /dev/null +++ b/compliance/query/cases/a06_null_inputs/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_null_inputs/query.ossie.sql b/compliance/query/cases/a06_null_inputs/query.ossie.sql new file mode 100644 index 000000000..b47c1ce78 --- /dev/null +++ b/compliance/query/cases/a06_null_inputs/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. +-- ---------------------------------------------------------------------------- +-- A6. NULL inputs. +-- A measure follows the NULL rules of its aggregate. emp_count is COUNT(*) +-- and counts every row. comm_count is COUNT(comm) and ignores NULL. Departments +-- 10 and 20 have no commission values, so SUM and AVG give NULL. In department +-- 30, TURNER's 0.00 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_null_inputs/reference.duckdb.sql b/compliance/query/cases/a06_null_inputs/reference.duckdb.sql new file mode 100644 index 000000000..862702355 --- /dev/null +++ b/compliance/query/cases/a06_null_inputs/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(comm) 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..31238cb25 --- /dev/null +++ b/compliance/query/cases/a07_empty_input_set/query.ossie.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. +-- ---------------------------------------------------------------------------- +-- A7. Empty input set. +-- 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..6384e8bf2 --- /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(sal) AS avg_sal +FROM scott.emp +WHERE deptno = 40; diff --git a/compliance/query/cases/a08_group_by_all/expected.sql b/compliance/query/cases/a08_group_by_all/expected.sql new file mode 100644 index 000000000..20b7ef892 --- /dev/null +++ b/compliance/query/cases/a08_group_by_all/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/query.ossie.sql b/compliance/query/cases/a08_group_by_all/query.ossie.sql new file mode 100644 index 000000000..4d73bd539 --- /dev/null +++ b/compliance/query/cases/a08_group_by_all/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. +-- ---------------------------------------------------------------------------- +-- A8. GROUP BY ALL on an expression. +-- 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/reference.duckdb.sql b/compliance/query/cases/a08_group_by_all/reference.duckdb.sql new file mode 100644 index 000000000..7fa795e35 --- /dev/null +++ b/compliance/query/cases/a08_group_by_all/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/expected.sql b/compliance/query/cases/a09_rollup/expected.sql new file mode 100644 index 000000000..abb5db6fe --- /dev/null +++ b/compliance/query/cases/a09_rollup/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/query.ossie.sql b/compliance/query/cases/a09_rollup/query.ossie.sql new file mode 100644 index 000000000..98aff10d3 --- /dev/null +++ b/compliance/query/cases/a09_rollup/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. +-- ---------------------------------------------------------------------------- +-- A9. ROLLUP. +-- A measure is evaluated again for each grouping set. 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/reference.duckdb.sql b/compliance/query/cases/a09_rollup/reference.duckdb.sql new file mode 100644 index 000000000..b2163e130 --- /dev/null +++ b/compliance/query/cases/a09_rollup/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(sal) AS avg_sal +FROM scott.emp +GROUP BY ROLLUP (deptno) +ORDER BY deptno NULLS LAST; diff --git a/compliance/query/cases/a10_cte_filter_rename/expected.sql b/compliance/query/cases/a10_cte_filter_rename/expected.sql new file mode 100644 index 000000000..be08e0545 --- /dev/null +++ b/compliance/query/cases/a10_cte_filter_rename/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_filter_rename/query.ossie.sql b/compliance/query/cases/a10_cte_filter_rename/query.ossie.sql new file mode 100644 index 000000000..dc00b1859 --- /dev/null +++ b/compliance/query/cases/a10_cte_filter_rename/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. +-- ---------------------------------------------------------------------------- +-- A10. Measure through a CTE that filters and renames. +-- 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_filter_rename/reference.duckdb.sql b/compliance/query/cases/a10_cte_filter_rename/reference.duckdb.sql new file mode 100644 index 000000000..29dce29b1 --- /dev/null +++ b/compliance/query/cases/a10_cte_filter_rename/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_scalar_subquery/expected.sql b/compliance/query/cases/a11_scalar_subquery/expected.sql new file mode 100644 index 000000000..74fa78f3b --- /dev/null +++ b/compliance/query/cases/a11_scalar_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_scalar_subquery/query.ossie.sql b/compliance/query/cases/a11_scalar_subquery/query.ossie.sql new file mode 100644 index 000000000..de8f87a04 --- /dev/null +++ b/compliance/query/cases/a11_scalar_subquery/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. +-- ---------------------------------------------------------------------------- +-- A11. AGG() in a scalar subquery. +-- AGG() cannot appear directly in WHERE, but a scalar subquery in WHERE can +-- use it. The query counts, by department, the employees whose salary is +-- greater than the company average (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_scalar_subquery/reference.duckdb.sql b/compliance/query/cases/a11_scalar_subquery/reference.duckdb.sql new file mode 100644 index 000000000..c9e99775b --- /dev/null +++ b/compliance/query/cases/a11_scalar_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(sal) FROM scott.emp) +GROUP BY deptno +ORDER BY deptno; diff --git a/compliance/query/cases/a12_two_stage_aggregation/expected.sql b/compliance/query/cases/a12_two_stage_aggregation/expected.sql new file mode 100644 index 000000000..17c0244fc --- /dev/null +++ b/compliance/query/cases/a12_two_stage_aggregation/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_two_stage_aggregation/query.ossie.sql b/compliance/query/cases/a12_two_stage_aggregation/query.ossie.sql new file mode 100644 index 000000000..8c9fb7eba --- /dev/null +++ b/compliance/query/cases/a12_two_stage_aggregation/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. +-- ---------------------------------------------------------------------------- +-- A12. Two-stage aggregation and a set operator. +-- The first branch averages the per-department payroll (29025 / 3). The +-- second branch is the average salary per employee. After AGG() evaluates the +-- measures, the values are ordinary scalars, so UNION ALL is valid. +-- Spec: A.3, 7.8 +-- ---------------------------------------------------------------------------- +SELECT 'avg payroll per dept' AS metric, AVG(dept_sal) 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_two_stage_aggregation/reference.duckdb.sql b/compliance/query/cases/a12_two_stage_aggregation/reference.duckdb.sql new file mode 100644 index 000000000..38aaa2bef --- /dev/null +++ b/compliance/query/cases/a12_two_stage_aggregation/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 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(dept_sal) 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(sal) +FROM scott.emp +ORDER BY metric; diff --git a/compliance/query/cases/b01_many_to_one_join/expected.sql b/compliance/query/cases/b01_many_to_one_join/expected.sql new file mode 100644 index 000000000..33c7e1abb --- /dev/null +++ b/compliance/query/cases/b01_many_to_one_join/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_join/query.ossie.sql b/compliance/query/cases/b01_many_to_one_join/query.ossie.sql new file mode 100644 index 000000000..d3df9e722 --- /dev/null +++ b/compliance/query/cases/b01_many_to_one_join/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. +-- ---------------------------------------------------------------------------- +-- B1. Many-to-one join, group by a field of the joined relation. +-- Each employee matches one department, so the join repeats no emp row. The +-- measures are the same as 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_join/reference.duckdb.sql b/compliance/query/cases/b01_many_to_one_join/reference.duckdb.sql new file mode 100644 index 000000000..fec7cc2d7 --- /dev/null +++ b/compliance/query/cases/b01_many_to_one_join/reference.duckdb.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. +-- 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. +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_where_on_joined_relation/expected.sql b/compliance/query/cases/b02_where_on_joined_relation/expected.sql new file mode 100644 index 000000000..07d33c62e --- /dev/null +++ b/compliance/query/cases/b02_where_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_where_on_joined_relation/query.ossie.sql b/compliance/query/cases/b02_where_on_joined_relation/query.ossie.sql new file mode 100644 index 000000000..851c06a79 --- /dev/null +++ b/compliance/query/cases/b02_where_on_joined_relation/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. +-- ---------------------------------------------------------------------------- +-- B2. WHERE on a field of the joined relation. +-- 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_where_on_joined_relation/reference.duckdb.sql b/compliance/query/cases/b02_where_on_joined_relation/reference.duckdb.sql new file mode 100644 index 000000000..7d7c91e4e --- /dev/null +++ b/compliance/query/cases/b02_where_on_joined_relation/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 B2 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +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_filter/expected.sql b/compliance/query/cases/b03_top_n_filter/expected.sql new file mode 100644 index 000000000..3a46b362e --- /dev/null +++ b/compliance/query/cases/b03_top_n_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_filter/query.ossie.sql b/compliance/query/cases/b03_top_n_filter/query.ossie.sql new file mode 100644 index 000000000..251af4d20 --- /dev/null +++ b/compliance/query/cases/b03_top_n_filter/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. +-- ---------------------------------------------------------------------------- +-- B3. Top-N filter through a join. +-- The subquery selects the two departments with the largest payroll (20 and +-- 30). The join to it acts as a filter on emp, and the outer query reports +-- payroll by job for those departments. The departments that the subquery +-- ranks are the same values that the join matches (closure, spec 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_filter/reference.duckdb.sql b/compliance/query/cases/b03_top_n_filter/reference.duckdb.sql new file mode 100644 index 000000000..f6aac3344 --- /dev/null +++ b/compliance/query/cases/b03_top_n_filter/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 B3 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +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_fan_out/expected.sql b/compliance/query/cases/b04_fan_out/expected.sql new file mode 100644 index 000000000..1f40195e1 --- /dev/null +++ b/compliance/query/cases/b04_fan_out/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_fan_out/query.ossie.sql b/compliance/query/cases/b04_fan_out/query.ossie.sql new file mode 100644 index 000000000..7dde682ff --- /dev/null +++ b/compliance/query/cases/b04_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. +-- ---------------------------------------------------------------------------- +-- B4. Fan-out: a measure of the "one" side, grouped by the "many" side. +-- The join repeats each dept row once for each employee in it. AGG(dept_count) +-- counts each department once in each group, so the result is the number of +-- departments that have the job. Naive COUNT(*) gives 2, 4, 3, 1, 4, which +-- AGG(e.emp_count) also gives, 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_fan_out/reference.duckdb.sql b/compliance/query/cases/b04_fan_out/reference.duckdb.sql new file mode 100644 index 000000000..ffd0836f4 --- /dev/null +++ b/compliance/query/cases/b04_fan_out/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 B4 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +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_fan_out_self_join/expected.sql b/compliance/query/cases/b05_fan_out_self_join/expected.sql new file mode 100644 index 000000000..b5f22fcbb --- /dev/null +++ b/compliance/query/cases/b05_fan_out_self_join/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_fan_out_self_join/query.ossie.sql b/compliance/query/cases/b05_fan_out_self_join/query.ossie.sql new file mode 100644 index 000000000..6fbc05b0d --- /dev/null +++ b/compliance/query/cases/b05_fan_out_self_join/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. +-- ---------------------------------------------------------------------------- +-- B5. Fan-out in a self-join: the manager's salary. +-- e is the employee and m is the manager. The join repeats each manager row +-- once for each report. AGG(m.sum_sal) counts each manager's salary once in +-- each group. The 4 salesmen all report to BLAKE, so the result is 2850; the +-- naive SUM(m.sal) is 11400. The 4 clerks report to 4 different managers, so +-- the two values are the same (11300). Naive SUM(m.sal) by job is 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_fan_out_self_join/reference.duckdb.sql b/compliance/query/cases/b05_fan_out_self_join/reference.duckdb.sql new file mode 100644 index 000000000..b8f809586 --- /dev/null +++ b/compliance/query/cases/b05_fan_out_self_join/reference.duckdb.sql @@ -0,0 +1,43 @@ +-- 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. +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_fan_out_range_join/expected.sql b/compliance/query/cases/b06_fan_out_range_join/expected.sql new file mode 100644 index 000000000..950b8389d --- /dev/null +++ b/compliance/query/cases/b06_fan_out_range_join/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_fan_out_range_join/query.ossie.sql b/compliance/query/cases/b06_fan_out_range_join/query.ossie.sql new file mode 100644 index 000000000..7477c412c --- /dev/null +++ b/compliance/query/cases/b06_fan_out_range_join/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. +-- ---------------------------------------------------------------------------- +-- B6. Fan-out through a range join. +-- The join condition can be any predicate over fields, not only an equality. +-- Each employee matches one salary grade, and a 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. Naive COUNT(*) +-- 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_fan_out_range_join/reference.duckdb.sql b/compliance/query/cases/b06_fan_out_range_join/reference.duckdb.sql new file mode 100644 index 000000000..8eb330dd9 --- /dev/null +++ b/compliance/query/cases/b06_fan_out_range_join/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 B6 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +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_fan_out_three_way_join/expected.sql b/compliance/query/cases/b07_fan_out_three_way_join/expected.sql new file mode 100644 index 000000000..2fe074c7b --- /dev/null +++ b/compliance/query/cases/b07_fan_out_three_way_join/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_fan_out_three_way_join/query.ossie.sql b/compliance/query/cases/b07_fan_out_three_way_join/query.ossie.sql new file mode 100644 index 000000000..6cb9a4627 --- /dev/null +++ b/compliance/query/cases/b07_fan_out_three_way_join/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. +-- ---------------------------------------------------------------------------- +-- B7. Fan-out on two sides of a three-way join. +-- emp joins to dept and to salgrade. The group is the salary grade, and +-- 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. Naive COUNT(*) 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_fan_out_three_way_join/reference.duckdb.sql b/compliance/query/cases/b07_fan_out_three_way_join/reference.duckdb.sql new file mode 100644 index 000000000..10a6f5715 --- /dev/null +++ b/compliance/query/cases/b07_fan_out_three_way_join/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 B7 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +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_join/expected.sql b/compliance/query/cases/b08_many_to_many_join/expected.sql new file mode 100644 index 000000000..1f01f45ac --- /dev/null +++ b/compliance/query/cases/b08_many_to_many_join/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_join/query.ossie.sql b/compliance/query/cases/b08_many_to_many_join/query.ossie.sql new file mode 100644 index 000000000..114b8904a --- /dev/null +++ b/compliance/query/cases/b08_many_to_many_join/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. +-- ---------------------------------------------------------------------------- +-- B8. Many-to-many join (chasm trap). +-- 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. Naive SUM(e.sal) gives 26250, +-- 54375, 56400, and naive 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_join/reference.duckdb.sql b/compliance/query/cases/b08_many_to_many_join/reference.duckdb.sql new file mode 100644 index 000000000..d58e9cff2 --- /dev/null +++ b/compliance/query/cases/b08_many_to_many_join/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 B8 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +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_self_join_inner/expected.sql b/compliance/query/cases/b09_self_join_inner/expected.sql new file mode 100644 index 000000000..463e27509 --- /dev/null +++ b/compliance/query/cases/b09_self_join_inner/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_self_join_inner/query.ossie.sql b/compliance/query/cases/b09_self_join_inner/query.ossie.sql new file mode 100644 index 000000000..ebb567947 --- /dev/null +++ b/compliance/query/cases/b09_self_join_inner/query.ossie.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. +-- ---------------------------------------------------------------------------- +-- B9. INNER JOIN. +-- Only employees that have a manager, and only employees that are managers. +-- +-- B9-B13. One self-join query with each join type. +-- +-- e is the employee and m is the manager, joined on e.mgr = m.empno. Both +-- sides have rows that do not match: +-- * KING has no manager, so KING has no match on the e side. +-- * 8 employees manage nobody, so they have no match on the m side. +-- The join type decides which source rows reach each measure. The join never +-- inflates a measure, and NULL-padded rows add nothing: +-- +-- join type joined rows e_count e_sal m_count m_sal +-- INNER 13 13 24025.00 6 19275.00 +-- LEFT 14 14 29025.00 6 19275.00 +-- RIGHT 21 13 24025.00 14 29025.00 +-- FULL 22 14 29025.00 14 29025.00 +-- CROSS 196 14 29025.00 14 29025.00 +-- +-- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 +-- times and BLAKE's 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_self_join_inner/reference.duckdb.sql b/compliance/query/cases/b09_self_join_inner/reference.duckdb.sql new file mode 100644 index 000000000..80c9a72c2 --- /dev/null +++ b/compliance/query/cases/b09_self_join_inner/reference.duckdb.sql @@ -0,0 +1,43 @@ +-- 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. +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_self_join_left/expected.sql b/compliance/query/cases/b10_self_join_left/expected.sql new file mode 100644 index 000000000..9b702968d --- /dev/null +++ b/compliance/query/cases/b10_self_join_left/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_self_join_left/query.ossie.sql b/compliance/query/cases/b10_self_join_left/query.ossie.sql new file mode 100644 index 000000000..e93baf344 --- /dev/null +++ b/compliance/query/cases/b10_self_join_left/query.ossie.sql @@ -0,0 +1,47 @@ +-- 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. +-- KING is kept on the e side. KING's row is NULL-padded on the m side, so the +-- m measures do not change. +-- +-- B9-B13. One self-join query with each join type. +-- +-- e is the employee and m is the manager, joined on e.mgr = m.empno. Both +-- sides have rows that do not match: +-- * KING has no manager, so KING has no match on the e side. +-- * 8 employees manage nobody, so they have no match on the m side. +-- The join type decides which source rows reach each measure. The join never +-- inflates a measure, and NULL-padded rows add nothing: +-- +-- join type joined rows e_count e_sal m_count m_sal +-- INNER 13 13 24025.00 6 19275.00 +-- LEFT 14 14 29025.00 6 19275.00 +-- RIGHT 21 13 24025.00 14 29025.00 +-- FULL 22 14 29025.00 14 29025.00 +-- CROSS 196 14 29025.00 14 29025.00 +-- +-- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 +-- times and BLAKE's 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 +LEFT JOIN scott_model.emp AS m ON e.mgr = m.empno; diff --git a/compliance/query/cases/b10_self_join_left/reference.duckdb.sql b/compliance/query/cases/b10_self_join_left/reference.duckdb.sql new file mode 100644 index 000000000..8b7158e8a --- /dev/null +++ b/compliance/query/cases/b10_self_join_left/reference.duckdb.sql @@ -0,0 +1,43 @@ +-- 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. +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_self_join_right/expected.sql b/compliance/query/cases/b11_self_join_right/expected.sql new file mode 100644 index 000000000..819a95121 --- /dev/null +++ b/compliance/query/cases/b11_self_join_right/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_self_join_right/query.ossie.sql b/compliance/query/cases/b11_self_join_right/query.ossie.sql new file mode 100644 index 000000000..64da5384e --- /dev/null +++ b/compliance/query/cases/b11_self_join_right/query.ossie.sql @@ -0,0 +1,47 @@ +-- 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 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-B13. One self-join query with each join type. +-- +-- e is the employee and m is the manager, joined on e.mgr = m.empno. Both +-- sides have rows that do not match: +-- * KING has no manager, so KING has no match on the e side. +-- * 8 employees manage nobody, so they have no match on the m side. +-- The join type decides which source rows reach each measure. The join never +-- inflates a measure, and NULL-padded rows add nothing: +-- +-- join type joined rows e_count e_sal m_count m_sal +-- INNER 13 13 24025.00 6 19275.00 +-- LEFT 14 14 29025.00 6 19275.00 +-- RIGHT 21 13 24025.00 14 29025.00 +-- FULL 22 14 29025.00 14 29025.00 +-- CROSS 196 14 29025.00 14 29025.00 +-- +-- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 +-- times and BLAKE's 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 +RIGHT JOIN scott_model.emp AS m ON e.mgr = m.empno; diff --git a/compliance/query/cases/b11_self_join_right/reference.duckdb.sql b/compliance/query/cases/b11_self_join_right/reference.duckdb.sql new file mode 100644 index 000000000..a1dfd1753 --- /dev/null +++ b/compliance/query/cases/b11_self_join_right/reference.duckdb.sql @@ -0,0 +1,43 @@ +-- 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. +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_self_join_full/expected.sql b/compliance/query/cases/b12_self_join_full/expected.sql new file mode 100644 index 000000000..3348fdc4c --- /dev/null +++ b/compliance/query/cases/b12_self_join_full/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_self_join_full/query.ossie.sql b/compliance/query/cases/b12_self_join_full/query.ossie.sql new file mode 100644 index 000000000..36591ef4f --- /dev/null +++ b/compliance/query/cases/b12_self_join_full/query.ossie.sql @@ -0,0 +1,47 @@ +-- 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. +-- Both sides keep all 14 employees. Each side counts each employee once, +-- although the join has 22 rows. +-- +-- B9-B13. One self-join query with each join type. +-- +-- e is the employee and m is the manager, joined on e.mgr = m.empno. Both +-- sides have rows that do not match: +-- * KING has no manager, so KING has no match on the e side. +-- * 8 employees manage nobody, so they have no match on the m side. +-- The join type decides which source rows reach each measure. The join never +-- inflates a measure, and NULL-padded rows add nothing: +-- +-- join type joined rows e_count e_sal m_count m_sal +-- INNER 13 13 24025.00 6 19275.00 +-- LEFT 14 14 29025.00 6 19275.00 +-- RIGHT 21 13 24025.00 14 29025.00 +-- FULL 22 14 29025.00 14 29025.00 +-- CROSS 196 14 29025.00 14 29025.00 +-- +-- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 +-- times and BLAKE's 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 +FULL JOIN scott_model.emp AS m ON e.mgr = m.empno; diff --git a/compliance/query/cases/b12_self_join_full/reference.duckdb.sql b/compliance/query/cases/b12_self_join_full/reference.duckdb.sql new file mode 100644 index 000000000..724bc3b66 --- /dev/null +++ b/compliance/query/cases/b12_self_join_full/reference.duckdb.sql @@ -0,0 +1,43 @@ +-- 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. +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_self_join_cross/expected.sql b/compliance/query/cases/b13_self_join_cross/expected.sql new file mode 100644 index 000000000..0486bf8fc --- /dev/null +++ b/compliance/query/cases/b13_self_join_cross/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_self_join_cross/query.ossie.sql b/compliance/query/cases/b13_self_join_cross/query.ossie.sql new file mode 100644 index 000000000..fe049d758 --- /dev/null +++ b/compliance/query/cases/b13_self_join_cross/query.ossie.sql @@ -0,0 +1,47 @@ +-- 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. +-- The cross join has no condition and gives 14 x 14 = 196 rows. Each side +-- still counts each employee once. Naive SUM(e.sal) is 406350. +-- +-- B9-B13. One self-join query with each join type. +-- +-- e is the employee and m is the manager, joined on e.mgr = m.empno. Both +-- sides have rows that do not match: +-- * KING has no manager, so KING has no match on the e side. +-- * 8 employees manage nobody, so they have no match on the m side. +-- The join type decides which source rows reach each measure. The join never +-- inflates a measure, and NULL-padded rows add nothing: +-- +-- join type joined rows e_count e_sal m_count m_sal +-- INNER 13 13 24025.00 6 19275.00 +-- LEFT 14 14 29025.00 6 19275.00 +-- RIGHT 21 13 24025.00 14 29025.00 +-- FULL 22 14 29025.00 14 29025.00 +-- CROSS 196 14 29025.00 14 29025.00 +-- +-- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 +-- times and BLAKE's 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 +CROSS JOIN scott_model.emp AS m; diff --git a/compliance/query/cases/b13_self_join_cross/reference.duckdb.sql b/compliance/query/cases/b13_self_join_cross/reference.duckdb.sql new file mode 100644 index 000000000..4b05a33e7 --- /dev/null +++ b/compliance/query/cases/b13_self_join_cross/reference.duckdb.sql @@ -0,0 +1,43 @@ +-- 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. +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_dept_emp_inner/expected.sql b/compliance/query/cases/b14_dept_emp_inner/expected.sql new file mode 100644 index 000000000..09ff7ccb6 --- /dev/null +++ b/compliance/query/cases/b14_dept_emp_inner/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_dept_emp_inner/query.ossie.sql b/compliance/query/cases/b14_dept_emp_inner/query.ossie.sql new file mode 100644 index 000000000..673dd05d1 --- /dev/null +++ b/compliance/query/cases/b14_dept_emp_inner/query.ossie.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. +-- ---------------------------------------------------------------------------- +-- B14. INNER JOIN. +-- +-- B14-B17. One dept-emp query with inner and outer joins. +-- +-- The query computes the payroll per department as a ratio of two measures +-- from different relations: AGG(e.sum_sal) / AGG(d.dept_count). The join +-- repeats each dept row once for each employee in it (fan-out), but +-- dept_count still counts each department once. Naive COUNT(d.deptno) is 14 +-- for INNER and 15 for LEFT. +-- +-- The join type is the tool's choice, and it changes the answer (spec 10.2, +-- A.6). The LEFT join keeps OPERATIONS, which has no employees, so the +-- payroll is divided by 4 departments and not by 3. A filter in the ON clause +-- of the LEFT join keeps all 4 departments but matches only the clerks. +-- A RIGHT join with the sides swapped, or a FULL join, gives the LEFT +-- result, because every employee has a department. +-- 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, + AGG(e.sum_sal) / 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_dept_emp_inner/reference.duckdb.sql b/compliance/query/cases/b14_dept_emp_inner/reference.duckdb.sql new file mode 100644 index 000000000..82566970b --- /dev/null +++ b/compliance/query/cases/b14_dept_emp_inner/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 B14 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +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, sum_sal / dept_count AS sal_per_dept +FROM measures; diff --git a/compliance/query/cases/b15_dept_emp_left/expected.sql b/compliance/query/cases/b15_dept_emp_left/expected.sql new file mode 100644 index 000000000..2f2b02f66 --- /dev/null +++ b/compliance/query/cases/b15_dept_emp_left/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_dept_emp_left/query.ossie.sql b/compliance/query/cases/b15_dept_emp_left/query.ossie.sql new file mode 100644 index 000000000..85d42777a --- /dev/null +++ b/compliance/query/cases/b15_dept_emp_left/query.ossie.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. +-- ---------------------------------------------------------------------------- +-- B15. LEFT JOIN. +-- +-- B14-B17. One dept-emp query with inner and outer joins. +-- +-- The query computes the payroll per department as a ratio of two measures +-- from different relations: AGG(e.sum_sal) / AGG(d.dept_count). The join +-- repeats each dept row once for each employee in it (fan-out), but +-- dept_count still counts each department once. Naive COUNT(d.deptno) is 14 +-- for INNER and 15 for LEFT. +-- +-- The join type is the tool's choice, and it changes the answer (spec 10.2, +-- A.6). The LEFT join keeps OPERATIONS, which has no employees, so the +-- payroll is divided by 4 departments and not by 3. A filter in the ON clause +-- of the LEFT join keeps all 4 departments but matches only the clerks. +-- A RIGHT join with the sides swapped, or a FULL join, gives the LEFT +-- result, because every employee has a department. +-- 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, + AGG(e.sum_sal) / 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_dept_emp_left/reference.duckdb.sql b/compliance/query/cases/b15_dept_emp_left/reference.duckdb.sql new file mode 100644 index 000000000..8f045355b --- /dev/null +++ b/compliance/query/cases/b15_dept_emp_left/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 B15 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +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, sum_sal / dept_count AS sal_per_dept +FROM measures; diff --git a/compliance/query/cases/b16_dept_emp_left_on_filter/expected.sql b/compliance/query/cases/b16_dept_emp_left_on_filter/expected.sql new file mode 100644 index 000000000..51efc8189 --- /dev/null +++ b/compliance/query/cases/b16_dept_emp_left_on_filter/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_dept_emp_left_on_filter/query.ossie.sql b/compliance/query/cases/b16_dept_emp_left_on_filter/query.ossie.sql new file mode 100644 index 000000000..01eacabc7 --- /dev/null +++ b/compliance/query/cases/b16_dept_emp_left_on_filter/query.ossie.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. +-- ---------------------------------------------------------------------------- +-- B16. LEFT JOIN with a filter in the ON clause. +-- 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. +-- +-- B14-B17. One dept-emp query with inner and outer joins. +-- +-- The query computes the payroll per department as a ratio of two measures +-- from different relations: AGG(e.sum_sal) / AGG(d.dept_count). The join +-- repeats each dept row once for each employee in it (fan-out), but +-- dept_count still counts each department once. Naive COUNT(d.deptno) is 14 +-- for INNER and 15 for LEFT. +-- +-- The join type is the tool's choice, and it changes the answer (spec 10.2, +-- A.6). The LEFT join keeps OPERATIONS, which has no employees, so the +-- payroll is divided by 4 departments and not by 3. A filter in the ON clause +-- of the LEFT join keeps all 4 departments but matches only the clerks. +-- A RIGHT join with the sides swapped, or a FULL join, gives the LEFT +-- result, because every employee has a department. +-- 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, + AGG(e.sum_sal) / 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_dept_emp_left_on_filter/reference.duckdb.sql b/compliance/query/cases/b16_dept_emp_left_on_filter/reference.duckdb.sql new file mode 100644 index 000000000..74f07b727 --- /dev/null +++ b/compliance/query/cases/b16_dept_emp_left_on_filter/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 B16 (query.ossie.sql), over the base tables in +-- ../../scott/scott.sql. Its result is equal to the result of expected.sql. +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, sum_sal / dept_count AS sal_per_dept +FROM measures; diff --git a/compliance/query/cases/b17_dept_emp_left_grouped/expected.sql b/compliance/query/cases/b17_dept_emp_left_grouped/expected.sql new file mode 100644 index 000000000..147abe364 --- /dev/null +++ b/compliance/query/cases/b17_dept_emp_left_grouped/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_dept_emp_left_grouped/query.ossie.sql b/compliance/query/cases/b17_dept_emp_left_grouped/query.ossie.sql new file mode 100644 index 000000000..ba79342f1 --- /dev/null +++ b/compliance/query/cases/b17_dept_emp_left_grouped/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. +-- ---------------------------------------------------------------------------- +-- B17. LEFT JOIN, grouped: a group with an empty input set. +-- 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_dept_emp_left_grouped/reference.duckdb.sql b/compliance/query/cases/b17_dept_emp_left_grouped/reference.duckdb.sql new file mode 100644 index 000000000..f177baace --- /dev/null +++ b/compliance/query/cases/b17_dept_emp_left_grouped/reference.duckdb.sql @@ -0,0 +1,42 @@ +-- 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. +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(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 avg_sal +FROM spine AS g +ORDER BY g.dname; diff --git a/compliance/query/relational_semantics.sql b/compliance/query/relational_semantics.sql deleted file mode 100644 index 6f4b4e2e5..000000000 --- a/compliance/query/relational_semantics.sql +++ /dev/null @@ -1,1452 +0,0 @@ --- 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. - --- ============================================================================ --- Compliance queries: Relational Query Interface (Layer 2) --- --- Test cases for core-spec/relational_semantics.md over the classic SCOTT --- schema. Each case has three parts: --- 1. An Ossie query that evaluates measures with AGG() (commented out). --- 2. An equivalent DuckDB query over the base tables (runnable). --- 3. The expected result (commented out). --- --- Every Ossie query evaluates its measures with AGG(). --- --- Part A covers the fundamentals on one relation. Part B covers joins: joins --- that repeat no measure row, joins that repeat rows (fan-out and --- many-to-many), and two queries that are run with each join type. For --- joins, the DuckDB query applies the reference evaluation strategy of --- Section 9 of the spec: it removes join duplicates by row-identity key (the --- dataset's primary_key), then it aggregates. --- --- Run: --- duckdb < compliance/query/relational_semantics.sql --- --- The file needs no extensions or imports. Everything that DuckDB cannot run --- is commented out. --- --- Expected-result format: --- * Column headers are in uppercase. --- * Numbers are right-aligned. Strings and dates are left-aligned. --- * NULL is shown as an empty cell. --- * DOUBLE values are rounded to 2 decimal places. --- --- References: --- * Spec: core-spec/relational_semantics.md (apache/ossie#354) --- * Dataset-scoped metrics: apache/ossie#591 --- * Test format: apache/ossie#501. View expander: apache/ossie#502. --- ============================================================================ - - --- ============================================================================ --- Data: the SCOTT schema --- --- Values are the same as the SCOTT tables in Apache Calcite --- (core/src/test/resources/sql/within-distinct.iq). --- ============================================================================ - -DROP SCHEMA IF EXISTS scott CASCADE; -CREATE SCHEMA scott; - -CREATE TABLE scott.dept ( - deptno INTEGER NOT NULL PRIMARY KEY, - dname VARCHAR NOT NULL, - loc VARCHAR NOT NULL -); - -INSERT INTO scott.dept VALUES - (10, 'ACCOUNTING', 'NEW YORK'), - (20, 'RESEARCH', 'DALLAS'), - (30, 'SALES', 'CHICAGO'), - (40, 'OPERATIONS', 'BOSTON'); - -CREATE TABLE scott.emp ( - empno INTEGER NOT NULL PRIMARY KEY, - ename VARCHAR NOT NULL, - job VARCHAR NOT NULL, - mgr INTEGER, - hiredate DATE NOT NULL, - sal DECIMAL(7, 2) NOT NULL, - comm DECIMAL(7, 2), - deptno INTEGER NOT NULL -); - -INSERT INTO scott.emp VALUES - (7369, 'SMITH', 'CLERK', 7902, DATE '1980-12-17', 800.00, NULL, 20), - (7499, 'ALLEN', 'SALESMAN', 7698, DATE '1981-02-20', 1600.00, 300.00, 30), - (7521, 'WARD', 'SALESMAN', 7698, DATE '1981-02-22', 1250.00, 500.00, 30), - (7566, 'JONES', 'MANAGER', 7839, DATE '1981-02-04', 2975.00, NULL, 20), - (7654, 'MARTIN', 'SALESMAN', 7698, DATE '1981-09-28', 1250.00, 1400.00, 30), - (7698, 'BLAKE', 'MANAGER', 7839, DATE '1981-01-05', 2850.00, NULL, 30), - (7782, 'CLARK', 'MANAGER', 7839, DATE '1981-06-09', 2450.00, NULL, 10), - (7788, 'SCOTT', 'ANALYST', 7566, DATE '1987-04-19', 3000.00, NULL, 20), - (7839, 'KING', 'PRESIDENT', NULL, DATE '1981-11-17', 5000.00, NULL, 10), - (7844, 'TURNER', 'SALESMAN', 7698, DATE '1981-09-08', 1500.00, 0.00, 30), - (7876, 'ADAMS', 'CLERK', 7788, DATE '1987-05-23', 1100.00, NULL, 20), - (7900, 'JAMES', 'CLERK', 7698, DATE '1981-12-03', 950.00, NULL, 30), - (7902, 'FORD', 'ANALYST', 7566, DATE '1981-12-03', 3000.00, NULL, 20), - (7934, 'MILLER', 'CLERK', 7782, DATE '1982-01-23', 1300.00, NULL, 10); - -CREATE TABLE scott.salgrade ( - grade INTEGER NOT NULL PRIMARY KEY, - losal INTEGER NOT NULL, - hisal INTEGER NOT NULL -); - -INSERT INTO scott.salgrade VALUES - (1, 700, 1200), - (2, 1201, 1400), - (3, 1401, 2000), - (4, 2001, 3000), - (5, 3001, 9999); - --- BONUS has no rows in SCOTT. It is here for completeness; no test uses it. -CREATE TABLE scott.bonus ( - ename VARCHAR, - job VARCHAR, - sal DECIMAL(7, 2), - comm DECIMAL(7, 2) -); - - --- ============================================================================ --- Model: Ossie semantic model over SCOTT (commented out) --- --- The metrics are dataset-scoped (datasets[].metrics, apache/ossie#591). Each --- dataset is exposed as a relation with the same name, and each metric is a --- measure column of that relation (spec Section 3.4). All relations of the --- model are in one schema with the name of the model (spec Section 11.2). The --- Ossie queries below refer to these relations by two-part name: --- scott_model.emp, scott_model.dept, and scott_model.salgrade. --- --- The primary_key of each dataset is the row identity of its measures (spec --- Section 3.2). --- --- The emp-to-salgrade join (e.sal BETWEEN s.losal AND s.hisal) is a range --- join. An Ossie relationship cannot express it, so the consuming query --- writes it (spec Section 11.3). --- ============================================================================ - --- version: 0.2.0.dev0 --- name: scott_model --- description: The SCOTT schema with dataset-scoped metrics --- datasets: --- - name: emp --- source: scott.emp --- primary_key: [empno] --- fields: --- - name: empno --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: empno}]} --- - name: ename --- datatype: String --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: ename}]} --- - name: job --- datatype: String --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: job}]} --- - name: mgr --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: mgr}]} --- - name: hiredate --- datatype: Date --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: hiredate}]} --- - name: sal --- datatype: Decimal --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: sal}]} --- - name: comm --- datatype: Decimal --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: comm}]} --- - name: deptno --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: deptno}]} --- metrics: --- - name: sum_sal --- description: Total salary --- datatype: Decimal --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: SUM(sal)}]} --- - name: avg_sal --- description: Average salary --- datatype: Float --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: AVG(sal)}]} --- - name: emp_count --- description: Number of employees --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(*)}]} --- - name: comm_count --- description: Number of employees that have a commission --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(comm)}]} --- - name: sum_comm --- description: Total commission --- datatype: Decimal --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: SUM(comm)}]} --- - name: avg_comm --- description: Average commission, over employees that have a commission --- datatype: Float --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: AVG(comm)}]} --- --- - name: dept --- source: scott.dept --- primary_key: [deptno] --- fields: --- - name: deptno --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: deptno}]} --- - name: dname --- datatype: String --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: dname}]} --- - name: loc --- datatype: String --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: loc}]} --- metrics: --- - name: dept_count --- description: Number of departments --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(*)}]} --- --- - name: salgrade --- source: scott.salgrade --- primary_key: [grade] --- fields: --- - name: grade --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: grade}]} --- - name: losal --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: losal}]} --- - name: hisal --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: hisal}]} --- metrics: --- - name: grade_count --- description: Number of salary grades --- datatype: Integer --- expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: COUNT(*)}]} --- --- relationships: --- - name: emp_to_dept --- from: emp --- to: dept --- from_columns: [deptno] --- to_columns: [deptno] --- - name: emp_to_manager --- from: emp --- to: emp --- from_columns: [mgr] --- to_columns: [empno] - - --- ============================================================================ --- Part A: fundamentals (one relation) --- --- On one relation, no join repeats rows. So AGG(m) gives the same result as --- the metric's aggregate over the same rows. Each DuckDB query is the Ossie --- query with each AGG(m) replaced by the expression of m, and with each model --- relation (scott_model.emp) replaced by its source table (scott.emp). --- ============================================================================ - --- ---------------------------------------------------------------------------- --- A1. Grand total. --- AGG() is an aggregate. Without GROUP BY, the query returns one row. --- Spec: 6, 7.2 --- ---------------------------------------------------------------------------- --- Ossie: --- SELECT AGG(sum_sal) AS sum_sal, --- AGG(emp_count) AS emp_count, --- AGG(avg_sal) AS avg_sal --- FROM scott_model.emp; -SELECT SUM(sal) AS sum_sal, - COUNT(*) AS emp_count, - AVG(sal) AS avg_sal -FROM scott.emp; --- +----------+-----------+---------+ --- | SUM_SAL | EMP_COUNT | AVG_SAL | --- +----------+-----------+---------+ --- | 29025.00 | 14 | 2073.21 | --- +----------+-----------+---------+ --- (1 row) - --- ---------------------------------------------------------------------------- --- A2. GROUP BY a field. --- The grouping sets the level at which each measure is reported. --- Spec: 7.4, G1 (8.2) --- ---------------------------------------------------------------------------- --- Ossie: --- SELECT deptno, --- AGG(sum_sal) AS sum_sal, --- AGG(emp_count) AS emp_count, --- AGG(avg_sal) AS avg_sal --- FROM scott_model.emp --- GROUP BY deptno --- ORDER BY deptno; -SELECT deptno, - SUM(sal) AS sum_sal, - COUNT(*) AS emp_count, - AVG(sal) AS avg_sal -FROM scott.emp -GROUP BY deptno -ORDER BY deptno; --- +--------+----------+-----------+---------+ --- | DEPTNO | SUM_SAL | EMP_COUNT | AVG_SAL | --- +--------+----------+-----------+---------+ --- | 10 | 8750.00 | 3 | 2916.67 | --- | 20 | 10875.00 | 5 | 2175.00 | --- | 30 | 9400.00 | 6 | 1566.67 | --- +--------+----------+-----------+---------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- A3. WHERE on a field. --- WHERE removes rows before evaluation, so each measure sees only the rows --- that remain. --- Spec: 7.3, G1 (8.2) --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -SELECT deptno, - SUM(sal) AS sum_sal, - COUNT(*) AS emp_count -FROM scott.emp -WHERE job <> 'CLERK' -GROUP BY deptno -ORDER BY deptno; --- +--------+---------+-----------+ --- | DEPTNO | SUM_SAL | EMP_COUNT | --- +--------+---------+-----------+ --- | 10 | 7450.00 | 2 | --- | 20 | 8975.00 | 3 | --- | 30 | 8450.00 | 5 | --- +--------+---------+-----------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- A4. HAVING, ORDER BY, and LIMIT on measures. --- 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 --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -SELECT job, SUM(sal) AS sum_sal -FROM scott.emp -GROUP BY job -HAVING COUNT(*) > 2 -ORDER BY SUM(sal) DESC -LIMIT 3; --- +----------+---------+ --- | JOB | SUM_SAL | --- +----------+---------+ --- | MANAGER | 8275.00 | --- | SALESMAN | 5600.00 | --- | CLERK | 4150.00 | --- +----------+---------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- A5. Scalar expressions over AGG() results. --- Each AGG() gives a scalar value for each group. Scalar operators and --- functions then apply to these values. AGG(sum_sal) / AGG(emp_count) is equal --- to AGG(avg_sal). COALESCE replaces the NULL commission of departments 10 --- and 20. --- Spec: 7.2, 8.1 --- ---------------------------------------------------------------------------- --- Ossie: --- SELECT deptno, --- AGG(sum_sal) / 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; -SELECT deptno, - SUM(sal) / COUNT(*) AS sal_per_emp, - AVG(sal) AS avg_sal, - SUM(sal) + COALESCE(SUM(comm), 0) AS total_pay -FROM scott.emp -GROUP BY deptno -ORDER BY deptno; --- +--------+-------------+---------+-----------+ --- | DEPTNO | SAL_PER_EMP | AVG_SAL | TOTAL_PAY | --- +--------+-------------+---------+-----------+ --- | 10 | 2916.67 | 2916.67 | 8750.00 | --- | 20 | 2175.00 | 2175.00 | 10875.00 | --- | 30 | 1566.67 | 1566.67 | 11600.00 | --- +--------+-------------+---------+-----------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- A6. NULL inputs. --- A measure follows the NULL rules of its aggregate. emp_count is COUNT(*) --- and counts every row. comm_count is COUNT(comm) and ignores NULL. Departments --- 10 and 20 have no commission values, so SUM and AVG give NULL. In department --- 30, TURNER's 0.00 is a value, so AVG(comm) is 2200 / 4. --- Spec: 8.1 --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -SELECT deptno, - COUNT(*) AS emp_count, - COUNT(comm) AS comm_count, - SUM(comm) AS sum_comm, - AVG(comm) AS avg_comm -FROM scott.emp -GROUP BY deptno -ORDER BY deptno; --- +--------+-----------+------------+----------+----------+ --- | DEPTNO | EMP_COUNT | COMM_COUNT | SUM_COMM | AVG_COMM | --- +--------+-----------+------------+----------+----------+ --- | 10 | 3 | 0 | | | --- | 20 | 5 | 0 | | | --- | 30 | 6 | 4 | 2200.00 | 550.00 | --- +--------+-----------+------------+----------+----------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- A7. Empty input set. --- 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 --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -SELECT COUNT(*) AS emp_count, - SUM(sal) AS sum_sal, - AVG(sal) AS avg_sal -FROM scott.emp -WHERE deptno = 40; --- +-----------+---------+---------+ --- | EMP_COUNT | SUM_SAL | AVG_SAL | --- +-----------+---------+---------+ --- | 0 | | | --- +-----------+---------+---------+ --- (1 row) - --- ---------------------------------------------------------------------------- --- A8. GROUP BY ALL on an expression. --- 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 --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +-----------+-----------+----------+ --- | HIRE_YEAR | EMP_COUNT | SUM_SAL | --- +-----------+-----------+----------+ --- | 1980 | 1 | 800.00 | --- | 1981 | 10 | 22825.00 | --- | 1982 | 1 | 1300.00 | --- | 1987 | 2 | 4100.00 | --- +-----------+-----------+----------+ --- (4 rows) - --- ---------------------------------------------------------------------------- --- A9. ROLLUP. --- A measure is evaluated again for each grouping set. 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) --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -SELECT deptno, - COUNT(*) AS emp_count, - AVG(sal) AS avg_sal -FROM scott.emp -GROUP BY ROLLUP (deptno) -ORDER BY deptno NULLS LAST; --- +--------+-----------+---------+ --- | DEPTNO | EMP_COUNT | AVG_SAL | --- +--------+-----------+---------+ --- | 10 | 3 | 2916.67 | --- | 20 | 5 | 2175.00 | --- | 30 | 6 | 1566.67 | --- | | 14 | 2073.21 | --- +--------+-----------+---------+ --- (4 rows) - --- ---------------------------------------------------------------------------- --- A10. Measure through a CTE that filters and renames. --- 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 --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +--------+---------+-----------+ --- | DEPTNO | PAYROLL | HEADCOUNT | --- +--------+---------+-----------+ --- | 10 | 1300.00 | 1 | --- | 20 | 1900.00 | 2 | --- | 30 | 6550.00 | 5 | --- +--------+---------+-----------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- A11. AGG() in a scalar subquery. --- AGG() cannot appear directly in WHERE, but a scalar subquery in WHERE can --- use it. The query counts, by department, the employees whose salary is --- greater than the company average (2073.21). --- Spec: 7.3 --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -SELECT deptno, COUNT(*) AS emp_count -FROM scott.emp -WHERE sal > (SELECT AVG(sal) FROM scott.emp) -GROUP BY deptno -ORDER BY deptno; --- +--------+-----------+ --- | DEPTNO | EMP_COUNT | --- +--------+-----------+ --- | 10 | 2 | --- | 20 | 3 | --- | 30 | 1 | --- +--------+-----------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- A12. Two-stage aggregation and a set operator. --- The first branch averages the per-department payroll (29025 / 3). The --- second branch is the average salary per employee. After AGG() evaluates the --- measures, the values are ordinary scalars, so UNION ALL is valid. --- Spec: A.3, 7.8 --- ---------------------------------------------------------------------------- --- Ossie: --- SELECT 'avg payroll per dept' AS metric, AVG(dept_sal) 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; -SELECT 'avg payroll per dept' AS metric, AVG(dept_sal) 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(sal) -FROM scott.emp -ORDER BY metric; --- +----------------------+---------+ --- | METRIC | AMOUNT | --- +----------------------+---------+ --- | avg payroll per dept | 9675.00 | --- | avg salary per emp | 2073.21 | --- +----------------------+---------+ --- (2 rows) - - --- ============================================================================ --- Part B: joins --- --- A join can repeat a row of a measure's source (fan-out). An outer join can --- also add NULL-padded rows that come from no source row. AGG(m) aggregates --- each source row of m once and ignores NULL-padded rows (spec Section 8.1). --- The comments give the result of a plain aggregate over the joined rows --- ("naive") where it differs. --- --- Each DuckDB query applies the reference strategy of spec Section 9: --- 1. The CTE "joined" is the Ossie query's FROM, JOIN, and WHERE. For each --- measure, it also carries the row-identity key (_k, which is the --- primary key of the dataset) and the arguments of the aggregate. --- 2. The CTE "spine" has one row for each group: SELECT DISTINCT over the --- grouping columns. A grand total has one group and needs no spine. --- 3. For each measure, a correlated subquery selects the joined rows of the --- group, removes the rows where the key is NULL, reduces the rows to one --- for each key (GROUP BY key), and then applies the aggregate. --- 4. Scalar expressions over AGG() results apply to the per-group values. --- --- B1-B3: joins that repeat no row of the measure's source. --- B4-B8: joins that repeat rows: fan-out and many-to-many. --- B9-B13: one self-join query with each join type. --- B14-B17: one dept-emp query with inner and outer joins. --- ============================================================================ - --- ---------------------------------------------------------------------------- --- B1. Many-to-one join, group by a field of the joined relation. --- Each employee matches one department, so the join repeats no emp row. The --- measures are the same as the per-department totals of A2. --- Spec: 7.6 --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +------------+----------+-----------+ --- | DNAME | SUM_SAL | EMP_COUNT | --- +------------+----------+-----------+ --- | ACCOUNTING | 8750.00 | 3 | --- | RESEARCH | 10875.00 | 5 | --- | SALES | 9400.00 | 6 | --- +------------+----------+-----------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- B2. WHERE on a field of the joined relation. --- 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) --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +-----------+---------+-----------+ --- | JOB | SUM_SAL | EMP_COUNT | --- +-----------+---------+-----------+ --- | CLERK | 1300.00 | 1 | --- | MANAGER | 2450.00 | 1 | --- | PRESIDENT | 5000.00 | 1 | --- +-----------+---------+-----------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- B3. Top-N filter through a join. --- The subquery selects the two departments with the largest payroll (20 and --- 30). The join to it acts as a filter on emp, and the outer query reports --- payroll by job for those departments. The departments that the subquery --- ranks are the same values that the join matches (closure, spec 8.4). --- Spec: 7.6, 8.4, A.2 --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +----------+---------+ --- | JOB | SUM_SAL | --- +----------+---------+ --- | ANALYST | 6000.00 | --- | CLERK | 2850.00 | --- | MANAGER | 5825.00 | --- | SALESMAN | 5600.00 | --- +----------+---------+ --- (4 rows) - --- ---------------------------------------------------------------------------- --- B4. Fan-out: a measure of the "one" side, grouped by the "many" side. --- The join repeats each dept row once for each employee in it. AGG(dept_count) --- counts each department once in each group, so the result is the number of --- departments that have the job. Naive COUNT(*) gives 2, 4, 3, 1, 4, which --- AGG(e.emp_count) also gives, because the join repeats no emp row. --- Spec: 7.6, G2 (8.3) --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +-----------+------------+-----------+ --- | JOB | DEPT_COUNT | EMP_COUNT | --- +-----------+------------+-----------+ --- | ANALYST | 1 | 2 | --- | CLERK | 3 | 4 | --- | MANAGER | 3 | 3 | --- | PRESIDENT | 1 | 1 | --- | SALESMAN | 1 | 4 | --- +-----------+------------+-----------+ --- (5 rows) - --- ---------------------------------------------------------------------------- --- B5. Fan-out in a self-join: the manager's salary. --- e is the employee and m is the manager. The join repeats each manager row --- once for each report. AGG(m.sum_sal) counts each manager's salary once in --- each group. The 4 salesmen all report to BLAKE, so the result is 2850; the --- naive SUM(m.sal) is 11400. The 4 clerks report to 4 different managers, so --- the two values are the same (11300). Naive SUM(m.sal) by job is 5950, --- 11300, 15000, 11400. --- Spec: 7.6, 8.1, G2 (8.3) --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +----------+-----------+----------+--------------+ --- | JOB | EMP_COUNT | MANAGERS | MANAGERS_SAL | --- +----------+-----------+----------+--------------+ --- | ANALYST | 2 | 1 | 2975.00 | --- | CLERK | 4 | 4 | 11300.00 | --- | MANAGER | 3 | 1 | 5000.00 | --- | SALESMAN | 4 | 1 | 2850.00 | --- +----------+-----------+----------+--------------+ --- (4 rows) - --- ---------------------------------------------------------------------------- --- B6. Fan-out through a range join. --- The join condition can be any predicate over fields, not only an equality. --- Each employee matches one salary grade, and a 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. Naive COUNT(*) --- gives 3, 5, 6. --- Spec: 7.6, G2 (8.3) --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +--------+-----------+-------------+ --- | DEPTNO | EMP_COUNT | GRADE_COUNT | --- +--------+-----------+-------------+ --- | 10 | 3 | 3 | --- | 20 | 5 | 2 | --- | 30 | 6 | 4 | --- +--------+-----------+-------------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- B7. Fan-out on two sides of a three-way join. --- emp joins to dept and to salgrade. The group is the salary grade, and --- 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. Naive COUNT(*) gives 3, 3, 2, 5, 1. --- Spec: 7.6, G2 (8.3) --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +-------+-----------+----------+------------+ --- | GRADE | EMP_COUNT | SUM_SAL | DEPT_COUNT | --- +-------+-----------+----------+------------+ --- | 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 | --- +-------+-----------+----------+------------+ --- (5 rows) - --- ---------------------------------------------------------------------------- --- B8. Many-to-many join (chasm trap). --- 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. Naive SUM(e.sal) gives 26250, --- 54375, 56400, and naive COUNT(*) gives 9, 25, 36. --- Spec: 7.6, G2 (8.3) --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +--------+----------+------------+ --- | DEPTNO | SUM_SAL | COLLEAGUES | --- +--------+----------+------------+ --- | 10 | 8750.00 | 3 | --- | 20 | 10875.00 | 5 | --- | 30 | 9400.00 | 6 | --- +--------+----------+------------+ --- (3 rows) - --- ---------------------------------------------------------------------------- --- B9-B13. One self-join query with each join type. --- --- e is the employee and m is the manager, joined on e.mgr = m.empno. Both --- sides have rows that do not match: --- * KING has no manager, so KING has no match on the e side. --- * 8 employees manage nobody, so they have no match on the m side. --- The join type decides which source rows reach each measure. The join never --- inflates a measure, and NULL-padded rows add nothing: --- --- join type joined rows e_count e_sal m_count m_sal --- INNER 13 13 24025.00 6 19275.00 --- LEFT 14 14 29025.00 6 19275.00 --- RIGHT 21 13 24025.00 14 29025.00 --- FULL 22 14 29025.00 14 29025.00 --- CROSS 196 14 29025.00 14 29025.00 --- --- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 --- times and BLAKE's 5 times. --- Spec: 7.6, 8.1, G2 (8.3), A.6 --- ---------------------------------------------------------------------------- - --- ---------------------------------------------------------------------------- --- B9. INNER JOIN. --- Only employees that have a manager, and only employees that are managers. --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +---------+----------+---------+----------+ --- | E_COUNT | E_SAL | M_COUNT | M_SAL | --- +---------+----------+---------+----------+ --- | 13 | 24025.00 | 6 | 19275.00 | --- +---------+----------+---------+----------+ --- (1 row) - --- ---------------------------------------------------------------------------- --- B10. LEFT JOIN. --- KING is kept on the e side. KING's row is NULL-padded on the m side, so the --- m measures do not change. --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +---------+----------+---------+----------+ --- | E_COUNT | E_SAL | M_COUNT | M_SAL | --- +---------+----------+---------+----------+ --- | 14 | 29025.00 | 6 | 19275.00 | --- +---------+----------+---------+----------+ --- (1 row) - --- ---------------------------------------------------------------------------- --- B11. RIGHT JOIN. --- 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. --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +---------+----------+---------+----------+ --- | E_COUNT | E_SAL | M_COUNT | M_SAL | --- +---------+----------+---------+----------+ --- | 13 | 24025.00 | 14 | 29025.00 | --- +---------+----------+---------+----------+ --- (1 row) - --- ---------------------------------------------------------------------------- --- B12. FULL OUTER JOIN. --- Both sides keep all 14 employees. Each side counts each employee once, --- although the join has 22 rows. --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +---------+----------+---------+----------+ --- | E_COUNT | E_SAL | M_COUNT | M_SAL | --- +---------+----------+---------+----------+ --- | 14 | 29025.00 | 14 | 29025.00 | --- +---------+----------+---------+----------+ --- (1 row) - --- ---------------------------------------------------------------------------- --- B13. CROSS JOIN. --- The cross join has no condition and gives 14 x 14 = 196 rows. Each side --- still counts each employee once. Naive SUM(e.sal) is 406350. --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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; --- +---------+----------+---------+----------+ --- | E_COUNT | E_SAL | M_COUNT | M_SAL | --- +---------+----------+---------+----------+ --- | 14 | 29025.00 | 14 | 29025.00 | --- +---------+----------+---------+----------+ --- (1 row) - --- ---------------------------------------------------------------------------- --- B14-B17. One dept-emp query with inner and outer joins. --- --- The query computes the payroll per department as a ratio of two measures --- from different relations: AGG(e.sum_sal) / AGG(d.dept_count). The join --- repeats each dept row once for each employee in it (fan-out), but --- dept_count still counts each department once. Naive COUNT(d.deptno) is 14 --- for INNER and 15 for LEFT. --- --- The join type is the tool's choice, and it changes the answer (spec 10.2, --- A.6). The LEFT join keeps OPERATIONS, which has no employees, so the --- payroll is divided by 4 departments and not by 3. A filter in the ON clause --- of the LEFT join keeps all 4 departments but matches only the clerks. --- A RIGHT join with the sides swapped, or a FULL join, gives the LEFT --- result, because every employee has a department. --- Spec: 7.2, 7.6, 8.1, A.6 --- ---------------------------------------------------------------------------- - --- ---------------------------------------------------------------------------- --- B14. INNER JOIN. --- ---------------------------------------------------------------------------- --- Ossie: --- SELECT AGG(d.dept_count) AS dept_count, --- AGG(e.emp_count) AS emp_count, --- AGG(e.sum_sal) AS sum_sal, --- AGG(e.sum_sal) / 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; -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, sum_sal / dept_count AS sal_per_dept -FROM measures; --- +------------+-----------+----------+--------------+ --- | DEPT_COUNT | EMP_COUNT | SUM_SAL | SAL_PER_DEPT | --- +------------+-----------+----------+--------------+ --- | 3 | 14 | 29025.00 | 9675.00 | --- +------------+-----------+----------+--------------+ --- (1 row) - --- ---------------------------------------------------------------------------- --- B15. LEFT JOIN. --- ---------------------------------------------------------------------------- --- Ossie: --- SELECT AGG(d.dept_count) AS dept_count, --- AGG(e.emp_count) AS emp_count, --- AGG(e.sum_sal) AS sum_sal, --- AGG(e.sum_sal) / 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; -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, sum_sal / dept_count AS sal_per_dept -FROM measures; --- +------------+-----------+----------+--------------+ --- | DEPT_COUNT | EMP_COUNT | SUM_SAL | SAL_PER_DEPT | --- +------------+-----------+----------+--------------+ --- | 4 | 14 | 29025.00 | 7256.25 | --- +------------+-----------+----------+--------------+ --- (1 row) - --- ---------------------------------------------------------------------------- --- B16. LEFT JOIN with a filter in the ON clause. --- 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. --- ---------------------------------------------------------------------------- --- Ossie: --- SELECT AGG(d.dept_count) AS dept_count, --- AGG(e.emp_count) AS emp_count, --- AGG(e.sum_sal) AS sum_sal, --- AGG(e.sum_sal) / 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'; -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, sum_sal / dept_count AS sal_per_dept -FROM measures; --- +------------+-----------+---------+--------------+ --- | DEPT_COUNT | EMP_COUNT | SUM_SAL | SAL_PER_DEPT | --- +------------+-----------+---------+--------------+ --- | 4 | 4 | 4150.00 | 1037.50 | --- +------------+-----------+---------+--------------+ --- (1 row) - --- ---------------------------------------------------------------------------- --- B17. LEFT JOIN, grouped: a group with an empty input set. --- 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 --- ---------------------------------------------------------------------------- --- Ossie: --- 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; -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(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 avg_sal -FROM spine AS g -ORDER BY g.dname; --- +------------+-----------+----------+---------+ --- | DNAME | EMP_COUNT | SUM_SAL | AVG_SAL | --- +------------+-----------+----------+---------+ --- | ACCOUNTING | 3 | 8750.00 | 2916.67 | --- | OPERATIONS | 0 | | | --- | RESEARCH | 5 | 10875.00 | 2175.00 | --- | SALES | 6 | 9400.00 | 1566.67 | --- +------------+-----------+----------+---------+ --- (4 rows) - --- End relational_semantics.sql diff --git a/compliance/query/scott/scott_model.ossie.yaml b/compliance/query/scott/scott_model.ossie.yaml index 442f0ab4d..1eb37cbcc 100644 --- a/compliance/query/scott/scott_model.ossie.yaml +++ b/compliance/query/scott/scott_model.ossie.yaml @@ -76,6 +76,10 @@ datasets: description: Total salary datatype: Decimal expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: SUM(sal)}]} + - name: avg_sal + description: Average salary + datatype: Float + expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: AVG(sal)}]} - name: emp_count description: Number of employees datatype: Integer @@ -88,6 +92,10 @@ datasets: description: Total commission datatype: Decimal expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: SUM(comm)}]} + - name: avg_comm + description: Average commission, over employees that have a commission + datatype: Float + expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: AVG(comm)}]} - name: dept source: scott.dept From f39718cf9bb21ec43482bfc7a3427175de11caf8 Mon Sep 17 00:00:00 2001 From: Chris Eubank <108756251+christianeu-db@users.noreply.github.com> Date: Thu, 5 Nov 2026 06:57:12 +0000 Subject: [PATCH 3/7] test(compliance): cast AVG and division results to DOUBLE PRECISION The README of #632 does not allow an exact numeric result whose scale is implementation-defined, such as AVG over an exact numeric or the division of two exact numerics. - avg_sal and avg_comm are AVG(CAST(... AS DOUBLE PRECISION)). - A5 and B14-B16 cast before they divide. A12 casts the argument of its outer AVG. - A2 no longer returns avg_sal, as A1 does not. No other expected result changes. Co-authored-by: Isaac --- .../query/cases/a02_group_by/expected.sql | 7 +++---- .../query/cases/a02_group_by/query.ossie.sql | 3 +-- .../cases/a02_group_by/reference.duckdb.sql | 3 +-- .../a05_scalar_expressions/query.ossie.sql | 2 +- .../reference.duckdb.sql | 4 ++-- .../a06_null_inputs/reference.duckdb.sql | 2 +- .../a07_empty_input_set/reference.duckdb.sql | 2 +- .../cases/a09_rollup/reference.duckdb.sql | 2 +- .../a11_scalar_subquery/reference.duckdb.sql | 2 +- .../a12_two_stage_aggregation/query.ossie.sql | 3 ++- .../reference.duckdb.sql | 5 +++-- .../cases/b14_dept_emp_inner/query.ossie.sql | 2 +- .../b14_dept_emp_inner/reference.duckdb.sql | 3 ++- .../cases/b15_dept_emp_left/query.ossie.sql | 2 +- .../b15_dept_emp_left/reference.duckdb.sql | 3 ++- .../query.ossie.sql | 2 +- .../reference.duckdb.sql | 3 ++- .../reference.duckdb.sql | 2 +- compliance/query/scott/scott_model.ossie.yaml | 19 ++++++++++++------- 19 files changed, 39 insertions(+), 32 deletions(-) diff --git a/compliance/query/cases/a02_group_by/expected.sql b/compliance/query/cases/a02_group_by/expected.sql index bba5497f7..a6bf60bbf 100644 --- a/compliance/query/cases/a02_group_by/expected.sql +++ b/compliance/query/cases/a02_group_by/expected.sql @@ -16,7 +16,6 @@ -- under the License. -- Expected result for case A2 (query.ossie.sql), in standard SQL. SELECT * -FROM (VALUES (10, 8750.00, 3, 2.9166666666666665E3), - (20, 10875.00, 5, 2.175E3), - (30, 9400.00, 6, 1.5666666666666667E3)) - AS expected (deptno, sum_sal, emp_count, avg_sal); +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_group_by/query.ossie.sql b/compliance/query/cases/a02_group_by/query.ossie.sql index f709f97e3..3c55d89d8 100644 --- a/compliance/query/cases/a02_group_by/query.ossie.sql +++ b/compliance/query/cases/a02_group_by/query.ossie.sql @@ -21,8 +21,7 @@ -- ---------------------------------------------------------------------------- SELECT deptno, AGG(sum_sal) AS sum_sal, - AGG(emp_count) AS emp_count, - AGG(avg_sal) AS avg_sal + AGG(emp_count) AS emp_count FROM scott_model.emp GROUP BY deptno ORDER BY deptno; diff --git a/compliance/query/cases/a02_group_by/reference.duckdb.sql b/compliance/query/cases/a02_group_by/reference.duckdb.sql index 176cb0dd6..f27f8cc0c 100644 --- a/compliance/query/cases/a02_group_by/reference.duckdb.sql +++ b/compliance/query/cases/a02_group_by/reference.duckdb.sql @@ -18,8 +18,7 @@ -- ../../scott/scott.sql. Its result is equal to the result of expected.sql. SELECT deptno, SUM(sal) AS sum_sal, - COUNT(*) AS emp_count, - AVG(sal) AS avg_sal + COUNT(*) AS emp_count FROM scott.emp GROUP BY deptno ORDER BY deptno; diff --git a/compliance/query/cases/a05_scalar_expressions/query.ossie.sql b/compliance/query/cases/a05_scalar_expressions/query.ossie.sql index b68faff3d..895621b0c 100644 --- a/compliance/query/cases/a05_scalar_expressions/query.ossie.sql +++ b/compliance/query/cases/a05_scalar_expressions/query.ossie.sql @@ -23,7 +23,7 @@ -- Spec: 7.2, 8.1 -- ---------------------------------------------------------------------------- SELECT deptno, - AGG(sum_sal) / AGG(emp_count) AS sal_per_emp, + CAST(AGG(sum_sal) AS DOUBLE PRECISION) / 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 diff --git a/compliance/query/cases/a05_scalar_expressions/reference.duckdb.sql b/compliance/query/cases/a05_scalar_expressions/reference.duckdb.sql index 2c8cfcb20..50bb7207f 100644 --- a/compliance/query/cases/a05_scalar_expressions/reference.duckdb.sql +++ b/compliance/query/cases/a05_scalar_expressions/reference.duckdb.sql @@ -17,8 +17,8 @@ -- 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, - SUM(sal) / COUNT(*) AS sal_per_emp, - AVG(sal) AS avg_sal, + CAST(SUM(sal) AS DOUBLE PRECISION) / COUNT(*) AS sal_per_emp, + AVG(CAST(sal AS DOUBLE PRECISION)) AS avg_sal, SUM(sal) + COALESCE(SUM(comm), 0) AS total_pay FROM scott.emp GROUP BY deptno diff --git a/compliance/query/cases/a06_null_inputs/reference.duckdb.sql b/compliance/query/cases/a06_null_inputs/reference.duckdb.sql index 862702355..cdebaced4 100644 --- a/compliance/query/cases/a06_null_inputs/reference.duckdb.sql +++ b/compliance/query/cases/a06_null_inputs/reference.duckdb.sql @@ -20,7 +20,7 @@ SELECT deptno, COUNT(*) AS emp_count, COUNT(comm) AS comm_count, SUM(comm) AS sum_comm, - AVG(comm) AS avg_comm + AVG(CAST(comm AS DOUBLE PRECISION)) AS avg_comm FROM scott.emp GROUP BY deptno ORDER BY deptno; diff --git a/compliance/query/cases/a07_empty_input_set/reference.duckdb.sql b/compliance/query/cases/a07_empty_input_set/reference.duckdb.sql index 6384e8bf2..3f25b4cad 100644 --- a/compliance/query/cases/a07_empty_input_set/reference.duckdb.sql +++ b/compliance/query/cases/a07_empty_input_set/reference.duckdb.sql @@ -18,6 +18,6 @@ -- ../../scott/scott.sql. Its result is equal to the result of expected.sql. SELECT COUNT(*) AS emp_count, SUM(sal) AS sum_sal, - AVG(sal) AS avg_sal + AVG(CAST(sal AS DOUBLE PRECISION)) AS avg_sal FROM scott.emp WHERE deptno = 40; diff --git a/compliance/query/cases/a09_rollup/reference.duckdb.sql b/compliance/query/cases/a09_rollup/reference.duckdb.sql index b2163e130..7aaf0c223 100644 --- a/compliance/query/cases/a09_rollup/reference.duckdb.sql +++ b/compliance/query/cases/a09_rollup/reference.duckdb.sql @@ -18,7 +18,7 @@ -- ../../scott/scott.sql. Its result is equal to the result of expected.sql. SELECT deptno, COUNT(*) AS emp_count, - AVG(sal) AS avg_sal + AVG(CAST(sal AS DOUBLE PRECISION)) AS avg_sal FROM scott.emp GROUP BY ROLLUP (deptno) ORDER BY deptno NULLS LAST; diff --git a/compliance/query/cases/a11_scalar_subquery/reference.duckdb.sql b/compliance/query/cases/a11_scalar_subquery/reference.duckdb.sql index c9e99775b..77cf176c3 100644 --- a/compliance/query/cases/a11_scalar_subquery/reference.duckdb.sql +++ b/compliance/query/cases/a11_scalar_subquery/reference.duckdb.sql @@ -18,6 +18,6 @@ -- ../../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(sal) FROM scott.emp) +WHERE sal > (SELECT AVG(CAST(sal AS DOUBLE PRECISION)) FROM scott.emp) GROUP BY deptno ORDER BY deptno; diff --git a/compliance/query/cases/a12_two_stage_aggregation/query.ossie.sql b/compliance/query/cases/a12_two_stage_aggregation/query.ossie.sql index 8c9fb7eba..6c49fca1e 100644 --- a/compliance/query/cases/a12_two_stage_aggregation/query.ossie.sql +++ b/compliance/query/cases/a12_two_stage_aggregation/query.ossie.sql @@ -21,7 +21,8 @@ -- measures, the values are ordinary scalars, so UNION ALL is valid. -- Spec: A.3, 7.8 -- ---------------------------------------------------------------------------- -SELECT 'avg payroll per dept' AS metric, AVG(dept_sal) AS amount +SELECT 'avg payroll per dept' AS metric, + AVG(CAST(dept_sal AS DOUBLE PRECISION)) AS amount FROM (SELECT deptno, AGG(sum_sal) AS dept_sal FROM scott_model.emp GROUP BY deptno) AS d diff --git a/compliance/query/cases/a12_two_stage_aggregation/reference.duckdb.sql b/compliance/query/cases/a12_two_stage_aggregation/reference.duckdb.sql index 38aaa2bef..fbb691407 100644 --- a/compliance/query/cases/a12_two_stage_aggregation/reference.duckdb.sql +++ b/compliance/query/cases/a12_two_stage_aggregation/reference.duckdb.sql @@ -16,11 +16,12 @@ -- 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(dept_sal) AS amount +SELECT 'avg payroll per dept' AS metric, + AVG(CAST(dept_sal AS DOUBLE PRECISION)) 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(sal) +SELECT 'avg salary per emp', AVG(CAST(sal AS DOUBLE PRECISION)) FROM scott.emp ORDER BY metric; diff --git a/compliance/query/cases/b14_dept_emp_inner/query.ossie.sql b/compliance/query/cases/b14_dept_emp_inner/query.ossie.sql index 673dd05d1..177de9d68 100644 --- a/compliance/query/cases/b14_dept_emp_inner/query.ossie.sql +++ b/compliance/query/cases/b14_dept_emp_inner/query.ossie.sql @@ -36,6 +36,6 @@ SELECT AGG(d.dept_count) AS dept_count, AGG(e.emp_count) AS emp_count, AGG(e.sum_sal) AS sum_sal, - AGG(e.sum_sal) / AGG(d.dept_count) AS sal_per_dept + CAST(AGG(e.sum_sal) AS DOUBLE PRECISION) / 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_dept_emp_inner/reference.duckdb.sql b/compliance/query/cases/b14_dept_emp_inner/reference.duckdb.sql index 82566970b..fd0b5cadc 100644 --- a/compliance/query/cases/b14_dept_emp_inner/reference.duckdb.sql +++ b/compliance/query/cases/b14_dept_emp_inner/reference.duckdb.sql @@ -37,5 +37,6 @@ measures AS ( 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, sum_sal / dept_count AS sal_per_dept +SELECT dept_count, emp_count, sum_sal, + CAST(sum_sal AS DOUBLE PRECISION) / dept_count AS sal_per_dept FROM measures; diff --git a/compliance/query/cases/b15_dept_emp_left/query.ossie.sql b/compliance/query/cases/b15_dept_emp_left/query.ossie.sql index 85d42777a..6b25ec111 100644 --- a/compliance/query/cases/b15_dept_emp_left/query.ossie.sql +++ b/compliance/query/cases/b15_dept_emp_left/query.ossie.sql @@ -36,6 +36,6 @@ SELECT AGG(d.dept_count) AS dept_count, AGG(e.emp_count) AS emp_count, AGG(e.sum_sal) AS sum_sal, - AGG(e.sum_sal) / AGG(d.dept_count) AS sal_per_dept + CAST(AGG(e.sum_sal) AS DOUBLE PRECISION) / 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_dept_emp_left/reference.duckdb.sql b/compliance/query/cases/b15_dept_emp_left/reference.duckdb.sql index 8f045355b..13b0b9bf2 100644 --- a/compliance/query/cases/b15_dept_emp_left/reference.duckdb.sql +++ b/compliance/query/cases/b15_dept_emp_left/reference.duckdb.sql @@ -37,5 +37,6 @@ measures AS ( 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, sum_sal / dept_count AS sal_per_dept +SELECT dept_count, emp_count, sum_sal, + CAST(sum_sal AS DOUBLE PRECISION) / dept_count AS sal_per_dept FROM measures; diff --git a/compliance/query/cases/b16_dept_emp_left_on_filter/query.ossie.sql b/compliance/query/cases/b16_dept_emp_left_on_filter/query.ossie.sql index 01eacabc7..0614e3b2a 100644 --- a/compliance/query/cases/b16_dept_emp_left_on_filter/query.ossie.sql +++ b/compliance/query/cases/b16_dept_emp_left_on_filter/query.ossie.sql @@ -39,6 +39,6 @@ SELECT AGG(d.dept_count) AS dept_count, AGG(e.emp_count) AS emp_count, AGG(e.sum_sal) AS sum_sal, - AGG(e.sum_sal) / AGG(d.dept_count) AS sal_per_dept + CAST(AGG(e.sum_sal) AS DOUBLE PRECISION) / 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_dept_emp_left_on_filter/reference.duckdb.sql b/compliance/query/cases/b16_dept_emp_left_on_filter/reference.duckdb.sql index 74f07b727..4ae3e3674 100644 --- a/compliance/query/cases/b16_dept_emp_left_on_filter/reference.duckdb.sql +++ b/compliance/query/cases/b16_dept_emp_left_on_filter/reference.duckdb.sql @@ -37,5 +37,6 @@ measures AS ( 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, sum_sal / dept_count AS sal_per_dept +SELECT dept_count, emp_count, sum_sal, + CAST(sum_sal AS DOUBLE PRECISION) / dept_count AS sal_per_dept FROM measures; diff --git a/compliance/query/cases/b17_dept_emp_left_grouped/reference.duckdb.sql b/compliance/query/cases/b17_dept_emp_left_grouped/reference.duckdb.sql index f177baace..98b14db22 100644 --- a/compliance/query/cases/b17_dept_emp_left_grouped/reference.duckdb.sql +++ b/compliance/query/cases/b17_dept_emp_left_grouped/reference.duckdb.sql @@ -33,7 +33,7 @@ SELECT g.dname, 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(x.sal) + (SELECT AVG(CAST(x.sal AS DOUBLE PRECISION)) 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 diff --git a/compliance/query/scott/scott_model.ossie.yaml b/compliance/query/scott/scott_model.ossie.yaml index 1eb37cbcc..1a2ff7022 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 PRECISION. 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 @@ -79,7 +78,10 @@ datasets: - name: avg_sal description: Average salary datatype: Float - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: AVG(sal)}]} + expression: + dialects: + - dialect: OSSIE_SQL_2026 + expression: AVG(CAST(sal AS DOUBLE PRECISION)) - name: emp_count description: Number of employees datatype: Integer @@ -93,9 +95,12 @@ datasets: datatype: Decimal expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: SUM(comm)}]} - name: avg_comm - description: Average commission, over employees that have a commission + description: Average commission, over the employees that have a commission datatype: Float - expression: {dialects: [{dialect: OSSIE_SQL_2026, expression: AVG(comm)}]} + expression: + dialects: + - dialect: OSSIE_SQL_2026 + expression: AVG(CAST(comm AS DOUBLE PRECISION)) - name: dept source: scott.dept From e8bc1ececbea51ea43cee976e31b34a614cfa10f Mon Sep 17 00:00:00 2001 From: Chris Eubank <108756251+christianeu-db@users.noreply.github.com> Date: Thu, 5 Nov 2026 06:57:50 +0000 Subject: [PATCH 4/7] test(compliance): name cases for what they show and add Question lines @julianhyde suggested an English description of each query (#628). - Each query.ossie.sql header has a title that states what the case shows and a Question line that tells in English what the query asks. A1 gets a Question line too. - The case directories take names from the same rule, for example a09_rollup -> a09_rollup_not_average_of_averages. - The README describes the header and the naming rule. The question helps a reader; the Ossie query is the ground truth. The queries, reference queries, and expected results do not change. Co-authored-by: Isaac --- compliance/query/README.md | 17 +++++-- .../cases/a01_grand_total/query.ossie.sql | 1 + .../expected.sql | 0 .../query.ossie.sql | 3 +- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 7 +-- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 4 +- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 14 ++++-- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 13 +++-- .../reference.duckdb.sql | 0 .../cases/a07_empty_input_set/query.ossie.sql | 4 +- .../expected.sql | 0 .../query.ossie.sql | 4 +- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 10 ++-- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 4 +- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 7 +-- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 12 +++-- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 6 ++- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 4 +- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 11 +++-- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 13 +++-- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 15 +++--- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 8 ++-- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 8 ++-- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 9 ++-- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 32 +++++-------- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 32 +++++-------- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 32 +++++-------- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 36 ++++++++++++++ .../reference.duckdb.sql | 0 .../cases/b12_self_join_full/query.ossie.sql | 47 ------------------- .../expected.sql | 0 .../query.ossie.sql | 31 ++++-------- .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 24 ++++------ .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 30 ++++++------ .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 25 ++++------ .../reference.duckdb.sql | 0 .../expected.sql | 0 .../query.ossie.sql | 6 ++- .../reference.duckdb.sql | 0 85 files changed, 229 insertions(+), 240 deletions(-) rename compliance/query/cases/{a02_group_by => a02_measure_per_group}/expected.sql (100%) rename compliance/query/cases/{a02_group_by => a02_measure_per_group}/query.ossie.sql (90%) rename compliance/query/cases/{a02_group_by => a02_measure_per_group}/reference.duckdb.sql (100%) rename compliance/query/cases/{a03_where => a03_where_before_measure}/expected.sql (100%) rename compliance/query/cases/{a03_where => a03_where_before_measure}/query.ossie.sql (84%) rename compliance/query/cases/{a03_where => a03_where_before_measure}/reference.duckdb.sql (100%) rename compliance/query/cases/{a04_having_order_by_limit => a04_having_on_unselected_measure}/expected.sql (100%) rename compliance/query/cases/{a04_having_order_by_limit => a04_having_on_unselected_measure}/query.ossie.sql (88%) rename compliance/query/cases/{a04_having_order_by_limit => a04_having_on_unselected_measure}/reference.duckdb.sql (100%) rename compliance/query/cases/{a05_scalar_expressions => a05_scalar_ops_on_measure_values}/expected.sql (100%) rename compliance/query/cases/{a05_scalar_expressions => a05_scalar_ops_on_measure_values}/query.ossie.sql (66%) rename compliance/query/cases/{a05_scalar_expressions => a05_scalar_ops_on_measure_values}/reference.duckdb.sql (100%) rename compliance/query/cases/{a06_null_inputs => a06_nulls_follow_aggregate}/expected.sql (100%) rename compliance/query/cases/{a06_null_inputs => a06_nulls_follow_aggregate}/query.ossie.sql (70%) rename compliance/query/cases/{a06_null_inputs => a06_nulls_follow_aggregate}/reference.duckdb.sql (100%) rename compliance/query/cases/{a08_group_by_all => a08_group_by_all_skips_measures}/expected.sql (100%) rename compliance/query/cases/{a08_group_by_all => a08_group_by_all_skips_measures}/query.ossie.sql (90%) rename compliance/query/cases/{a08_group_by_all => a08_group_by_all_skips_measures}/reference.duckdb.sql (100%) rename compliance/query/cases/{a09_rollup => a09_rollup_not_average_of_averages}/expected.sql (100%) rename compliance/query/cases/{a09_rollup => a09_rollup_not_average_of_averages}/query.ossie.sql (78%) rename compliance/query/cases/{a09_rollup => a09_rollup_not_average_of_averages}/reference.duckdb.sql (100%) rename compliance/query/cases/{a10_cte_filter_rename => a10_cte_passes_measures_through}/expected.sql (100%) rename compliance/query/cases/{a10_cte_filter_rename => a10_cte_passes_measures_through}/query.ossie.sql (88%) rename compliance/query/cases/{a10_cte_filter_rename => a10_cte_passes_measures_through}/reference.duckdb.sql (100%) rename compliance/query/cases/{a11_scalar_subquery => a11_agg_in_where_subquery}/expected.sql (100%) rename compliance/query/cases/{a11_scalar_subquery => a11_agg_in_where_subquery}/query.ossie.sql (86%) rename compliance/query/cases/{a11_scalar_subquery => a11_agg_in_where_subquery}/reference.duckdb.sql (100%) rename compliance/query/cases/{a12_two_stage_aggregation => a12_aggregate_over_agg_values}/expected.sql (100%) rename compliance/query/cases/{a12_two_stage_aggregation => a12_aggregate_over_agg_values}/query.ossie.sql (70%) rename compliance/query/cases/{a12_two_stage_aggregation => a12_aggregate_over_agg_values}/reference.duckdb.sql (100%) rename compliance/query/cases/{b01_many_to_one_join => b01_many_to_one_no_fan_out}/expected.sql (100%) rename compliance/query/cases/{b01_many_to_one_join => b01_many_to_one_no_fan_out}/query.ossie.sql (86%) rename compliance/query/cases/{b01_many_to_one_join => b01_many_to_one_no_fan_out}/reference.duckdb.sql (100%) rename compliance/query/cases/{b02_where_on_joined_relation => b02_filter_on_joined_relation}/expected.sql (100%) rename compliance/query/cases/{b02_where_on_joined_relation => b02_filter_on_joined_relation}/query.ossie.sql (89%) rename compliance/query/cases/{b02_where_on_joined_relation => b02_filter_on_joined_relation}/reference.duckdb.sql (100%) rename compliance/query/cases/{b03_top_n_filter => b03_top_n_join_as_filter}/expected.sql (100%) rename compliance/query/cases/{b03_top_n_filter => b03_top_n_join_as_filter}/query.ossie.sql (76%) rename compliance/query/cases/{b03_top_n_filter => b03_top_n_join_as_filter}/reference.duckdb.sql (100%) rename compliance/query/cases/{b04_fan_out => b04_one_side_counted_once}/expected.sql (100%) rename compliance/query/cases/{b04_fan_out => b04_one_side_counted_once}/query.ossie.sql (69%) rename compliance/query/cases/{b04_fan_out => b04_one_side_counted_once}/reference.duckdb.sql (100%) rename compliance/query/cases/{b05_fan_out_self_join => b05_manager_counted_once}/expected.sql (100%) rename compliance/query/cases/{b05_fan_out_self_join => b05_manager_counted_once}/query.ossie.sql (69%) rename compliance/query/cases/{b05_fan_out_self_join => b05_manager_counted_once}/reference.duckdb.sql (100%) rename compliance/query/cases/{b06_fan_out_range_join => b06_range_join_fan_out}/expected.sql (100%) rename compliance/query/cases/{b06_fan_out_range_join => b06_range_join_fan_out}/query.ossie.sql (81%) rename compliance/query/cases/{b06_fan_out_range_join => b06_range_join_fan_out}/reference.duckdb.sql (100%) rename compliance/query/cases/{b07_fan_out_three_way_join => b07_three_way_join_fan_out}/expected.sql (100%) rename compliance/query/cases/{b07_fan_out_three_way_join => b07_three_way_join_fan_out}/query.ossie.sql (81%) rename compliance/query/cases/{b07_fan_out_three_way_join => b07_three_way_join_fan_out}/reference.duckdb.sql (100%) rename compliance/query/cases/{b08_many_to_many_join => b08_many_to_many_chasm_trap}/expected.sql (100%) rename compliance/query/cases/{b08_many_to_many_join => b08_many_to_many_chasm_trap}/query.ossie.sql (78%) rename compliance/query/cases/{b08_many_to_many_join => b08_many_to_many_chasm_trap}/reference.duckdb.sql (100%) rename compliance/query/cases/{b09_self_join_inner => b09_inner_join_matched_rows}/expected.sql (100%) rename compliance/query/cases/{b09_self_join_inner => b09_inner_join_matched_rows}/query.ossie.sql (53%) rename compliance/query/cases/{b09_self_join_inner => b09_inner_join_matched_rows}/reference.duckdb.sql (100%) rename compliance/query/cases/{b10_self_join_left => b10_left_join_null_padding}/expected.sql (100%) rename compliance/query/cases/{b10_self_join_left => b10_left_join_null_padding}/query.ossie.sql (52%) rename compliance/query/cases/{b10_self_join_left => b10_left_join_null_padding}/reference.duckdb.sql (100%) rename compliance/query/cases/{b11_self_join_right => b11_right_join_keeps_non_managers}/expected.sql (100%) rename compliance/query/cases/{b11_self_join_right => b11_right_join_keeps_non_managers}/query.ossie.sql (51%) rename compliance/query/cases/{b11_self_join_right => b11_right_join_keeps_non_managers}/reference.duckdb.sql (100%) rename compliance/query/cases/{b12_self_join_full => b12_full_join_each_row_once}/expected.sql (100%) create mode 100644 compliance/query/cases/b12_full_join_each_row_once/query.ossie.sql rename compliance/query/cases/{b12_self_join_full => b12_full_join_each_row_once}/reference.duckdb.sql (100%) delete mode 100644 compliance/query/cases/b12_self_join_full/query.ossie.sql rename compliance/query/cases/{b13_self_join_cross => b13_cross_join_no_inflation}/expected.sql (100%) rename compliance/query/cases/{b13_self_join_cross => b13_cross_join_no_inflation}/query.ossie.sql (54%) rename compliance/query/cases/{b13_self_join_cross => b13_cross_join_no_inflation}/reference.duckdb.sql (100%) rename compliance/query/cases/{b14_dept_emp_inner => b14_ratio_across_relations}/expected.sql (100%) rename compliance/query/cases/{b14_dept_emp_inner => b14_ratio_across_relations}/query.ossie.sql (60%) rename compliance/query/cases/{b14_dept_emp_inner => b14_ratio_across_relations}/reference.duckdb.sql (100%) rename compliance/query/cases/{b15_dept_emp_left => b15_left_join_changes_ratio}/expected.sql (100%) rename compliance/query/cases/{b15_dept_emp_left => b15_left_join_changes_ratio}/query.ossie.sql (56%) rename compliance/query/cases/{b15_dept_emp_left => b15_left_join_changes_ratio}/reference.duckdb.sql (100%) rename compliance/query/cases/{b16_dept_emp_left_on_filter => b16_on_filter_keeps_left_rows}/expected.sql (100%) rename compliance/query/cases/{b16_dept_emp_left_on_filter => b16_on_filter_keeps_left_rows}/query.ossie.sql (63%) rename compliance/query/cases/{b16_dept_emp_left_on_filter => b16_on_filter_keeps_left_rows}/reference.duckdb.sql (100%) rename compliance/query/cases/{b17_dept_emp_left_grouped => b17_left_join_empty_group}/expected.sql (100%) rename compliance/query/cases/{b17_dept_emp_left_grouped => b17_left_join_empty_group}/query.ossie.sql (88%) rename compliance/query/cases/{b17_dept_emp_left_grouped => b17_left_join_empty_group}/reference.duckdb.sql (100%) diff --git a/compliance/query/README.md b/compliance/query/README.md index 1a6153ee8..12c006711 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: @@ -150,8 +156,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_group_by/expected.sql b/compliance/query/cases/a02_measure_per_group/expected.sql similarity index 100% rename from compliance/query/cases/a02_group_by/expected.sql rename to compliance/query/cases/a02_measure_per_group/expected.sql diff --git a/compliance/query/cases/a02_group_by/query.ossie.sql b/compliance/query/cases/a02_measure_per_group/query.ossie.sql similarity index 90% rename from compliance/query/cases/a02_group_by/query.ossie.sql rename to compliance/query/cases/a02_measure_per_group/query.ossie.sql index 3c55d89d8..36501bcb0 100644 --- a/compliance/query/cases/a02_group_by/query.ossie.sql +++ b/compliance/query/cases/a02_measure_per_group/query.ossie.sql @@ -15,7 +15,8 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A2. GROUP BY a field. +-- 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) -- ---------------------------------------------------------------------------- diff --git a/compliance/query/cases/a02_group_by/reference.duckdb.sql b/compliance/query/cases/a02_measure_per_group/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/a02_group_by/reference.duckdb.sql rename to compliance/query/cases/a02_measure_per_group/reference.duckdb.sql diff --git a/compliance/query/cases/a03_where/expected.sql b/compliance/query/cases/a03_where_before_measure/expected.sql similarity index 100% rename from compliance/query/cases/a03_where/expected.sql rename to compliance/query/cases/a03_where_before_measure/expected.sql diff --git a/compliance/query/cases/a03_where/query.ossie.sql b/compliance/query/cases/a03_where_before_measure/query.ossie.sql similarity index 84% rename from compliance/query/cases/a03_where/query.ossie.sql rename to compliance/query/cases/a03_where_before_measure/query.ossie.sql index 95d488fcf..14a19a857 100644 --- a/compliance/query/cases/a03_where/query.ossie.sql +++ b/compliance/query/cases/a03_where_before_measure/query.ossie.sql @@ -15,9 +15,10 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A3. WHERE on a field. --- WHERE removes rows before evaluation, so each measure sees only the rows --- that remain. +-- 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, diff --git a/compliance/query/cases/a03_where/reference.duckdb.sql b/compliance/query/cases/a03_where_before_measure/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/a03_where/reference.duckdb.sql rename to compliance/query/cases/a03_where_before_measure/reference.duckdb.sql diff --git a/compliance/query/cases/a04_having_order_by_limit/expected.sql b/compliance/query/cases/a04_having_on_unselected_measure/expected.sql similarity index 100% rename from compliance/query/cases/a04_having_order_by_limit/expected.sql rename to compliance/query/cases/a04_having_on_unselected_measure/expected.sql diff --git a/compliance/query/cases/a04_having_order_by_limit/query.ossie.sql b/compliance/query/cases/a04_having_on_unselected_measure/query.ossie.sql similarity index 88% rename from compliance/query/cases/a04_having_order_by_limit/query.ossie.sql rename to compliance/query/cases/a04_having_on_unselected_measure/query.ossie.sql index f8e694c6d..5befd2f56 100644 --- a/compliance/query/cases/a04_having_order_by_limit/query.ossie.sql +++ b/compliance/query/cases/a04_having_on_unselected_measure/query.ossie.sql @@ -15,7 +15,9 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A4. HAVING, ORDER BY, and LIMIT on measures. +-- 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 diff --git a/compliance/query/cases/a04_having_order_by_limit/reference.duckdb.sql b/compliance/query/cases/a04_having_on_unselected_measure/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/a04_having_order_by_limit/reference.duckdb.sql rename to compliance/query/cases/a04_having_on_unselected_measure/reference.duckdb.sql diff --git a/compliance/query/cases/a05_scalar_expressions/expected.sql b/compliance/query/cases/a05_scalar_ops_on_measure_values/expected.sql similarity index 100% rename from compliance/query/cases/a05_scalar_expressions/expected.sql rename to compliance/query/cases/a05_scalar_ops_on_measure_values/expected.sql diff --git a/compliance/query/cases/a05_scalar_expressions/query.ossie.sql b/compliance/query/cases/a05_scalar_ops_on_measure_values/query.ossie.sql similarity index 66% rename from compliance/query/cases/a05_scalar_expressions/query.ossie.sql rename to compliance/query/cases/a05_scalar_ops_on_measure_values/query.ossie.sql index 895621b0c..5d28ae15d 100644 --- a/compliance/query/cases/a05_scalar_expressions/query.ossie.sql +++ b/compliance/query/cases/a05_scalar_ops_on_measure_values/query.ossie.sql @@ -15,11 +15,15 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A5. Scalar expressions over AGG() results. --- Each AGG() gives a scalar value for each group. Scalar operators and --- functions then apply to these values. AGG(sum_sal) / AGG(emp_count) is equal --- to AGG(avg_sal). COALESCE replaces the NULL commission of departments 10 --- and 20. +-- 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, diff --git a/compliance/query/cases/a05_scalar_expressions/reference.duckdb.sql b/compliance/query/cases/a05_scalar_ops_on_measure_values/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/a05_scalar_expressions/reference.duckdb.sql rename to compliance/query/cases/a05_scalar_ops_on_measure_values/reference.duckdb.sql diff --git a/compliance/query/cases/a06_null_inputs/expected.sql b/compliance/query/cases/a06_nulls_follow_aggregate/expected.sql similarity index 100% rename from compliance/query/cases/a06_null_inputs/expected.sql rename to compliance/query/cases/a06_nulls_follow_aggregate/expected.sql diff --git a/compliance/query/cases/a06_null_inputs/query.ossie.sql b/compliance/query/cases/a06_nulls_follow_aggregate/query.ossie.sql similarity index 70% rename from compliance/query/cases/a06_null_inputs/query.ossie.sql rename to compliance/query/cases/a06_nulls_follow_aggregate/query.ossie.sql index b47c1ce78..6becbba37 100644 --- a/compliance/query/cases/a06_null_inputs/query.ossie.sql +++ b/compliance/query/cases/a06_nulls_follow_aggregate/query.ossie.sql @@ -15,11 +15,14 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A6. NULL inputs. --- A measure follows the NULL rules of its aggregate. emp_count is COUNT(*) --- and counts every row. comm_count is COUNT(comm) and ignores NULL. Departments --- 10 and 20 have no commission values, so SUM and AVG give NULL. In department --- 30, TURNER's 0.00 is a value, so AVG(comm) is 2200 / 4. +-- 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, diff --git a/compliance/query/cases/a06_null_inputs/reference.duckdb.sql b/compliance/query/cases/a06_nulls_follow_aggregate/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/a06_null_inputs/reference.duckdb.sql rename to compliance/query/cases/a06_nulls_follow_aggregate/reference.duckdb.sql diff --git a/compliance/query/cases/a07_empty_input_set/query.ossie.sql b/compliance/query/cases/a07_empty_input_set/query.ossie.sql index 31238cb25..97b1bc1ad 100644 --- a/compliance/query/cases/a07_empty_input_set/query.ossie.sql +++ b/compliance/query/cases/a07_empty_input_set/query.ossie.sql @@ -15,7 +15,9 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A7. Empty input set. +-- 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 diff --git a/compliance/query/cases/a08_group_by_all/expected.sql b/compliance/query/cases/a08_group_by_all_skips_measures/expected.sql similarity index 100% rename from compliance/query/cases/a08_group_by_all/expected.sql rename to compliance/query/cases/a08_group_by_all_skips_measures/expected.sql diff --git a/compliance/query/cases/a08_group_by_all/query.ossie.sql b/compliance/query/cases/a08_group_by_all_skips_measures/query.ossie.sql similarity index 90% rename from compliance/query/cases/a08_group_by_all/query.ossie.sql rename to compliance/query/cases/a08_group_by_all_skips_measures/query.ossie.sql index 4d73bd539..bc9f73bdf 100644 --- a/compliance/query/cases/a08_group_by_all/query.ossie.sql +++ b/compliance/query/cases/a08_group_by_all_skips_measures/query.ossie.sql @@ -15,7 +15,9 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A8. GROUP BY ALL on an expression. +-- 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 diff --git a/compliance/query/cases/a08_group_by_all/reference.duckdb.sql b/compliance/query/cases/a08_group_by_all_skips_measures/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/a08_group_by_all/reference.duckdb.sql rename to compliance/query/cases/a08_group_by_all_skips_measures/reference.duckdb.sql diff --git a/compliance/query/cases/a09_rollup/expected.sql b/compliance/query/cases/a09_rollup_not_average_of_averages/expected.sql similarity index 100% rename from compliance/query/cases/a09_rollup/expected.sql rename to compliance/query/cases/a09_rollup_not_average_of_averages/expected.sql diff --git a/compliance/query/cases/a09_rollup/query.ossie.sql b/compliance/query/cases/a09_rollup_not_average_of_averages/query.ossie.sql similarity index 78% rename from compliance/query/cases/a09_rollup/query.ossie.sql rename to compliance/query/cases/a09_rollup_not_average_of_averages/query.ossie.sql index 98aff10d3..4feb5905a 100644 --- a/compliance/query/cases/a09_rollup/query.ossie.sql +++ b/compliance/query/cases/a09_rollup_not_average_of_averages/query.ossie.sql @@ -15,10 +15,12 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A9. ROLLUP. --- A measure is evaluated again for each grouping set. 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). +-- 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, diff --git a/compliance/query/cases/a09_rollup/reference.duckdb.sql b/compliance/query/cases/a09_rollup_not_average_of_averages/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/a09_rollup/reference.duckdb.sql rename to compliance/query/cases/a09_rollup_not_average_of_averages/reference.duckdb.sql diff --git a/compliance/query/cases/a10_cte_filter_rename/expected.sql b/compliance/query/cases/a10_cte_passes_measures_through/expected.sql similarity index 100% rename from compliance/query/cases/a10_cte_filter_rename/expected.sql rename to compliance/query/cases/a10_cte_passes_measures_through/expected.sql diff --git a/compliance/query/cases/a10_cte_filter_rename/query.ossie.sql b/compliance/query/cases/a10_cte_passes_measures_through/query.ossie.sql similarity index 88% rename from compliance/query/cases/a10_cte_filter_rename/query.ossie.sql rename to compliance/query/cases/a10_cte_passes_measures_through/query.ossie.sql index dc00b1859..452488271 100644 --- a/compliance/query/cases/a10_cte_filter_rename/query.ossie.sql +++ b/compliance/query/cases/a10_cte_passes_measures_through/query.ossie.sql @@ -15,7 +15,9 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A10. Measure through a CTE that filters and renames. +-- 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. diff --git a/compliance/query/cases/a10_cte_filter_rename/reference.duckdb.sql b/compliance/query/cases/a10_cte_passes_measures_through/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/a10_cte_filter_rename/reference.duckdb.sql rename to compliance/query/cases/a10_cte_passes_measures_through/reference.duckdb.sql diff --git a/compliance/query/cases/a11_scalar_subquery/expected.sql b/compliance/query/cases/a11_agg_in_where_subquery/expected.sql similarity index 100% rename from compliance/query/cases/a11_scalar_subquery/expected.sql rename to compliance/query/cases/a11_agg_in_where_subquery/expected.sql diff --git a/compliance/query/cases/a11_scalar_subquery/query.ossie.sql b/compliance/query/cases/a11_agg_in_where_subquery/query.ossie.sql similarity index 86% rename from compliance/query/cases/a11_scalar_subquery/query.ossie.sql rename to compliance/query/cases/a11_agg_in_where_subquery/query.ossie.sql index de8f87a04..1054ad52f 100644 --- a/compliance/query/cases/a11_scalar_subquery/query.ossie.sql +++ b/compliance/query/cases/a11_agg_in_where_subquery/query.ossie.sql @@ -15,10 +15,11 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A11. AGG() in a scalar subquery. +-- 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 query counts, by department, the employees whose salary is --- greater than the company average (2073.21). +-- use it. The company average is 2073.21. -- Spec: 7.3 -- ---------------------------------------------------------------------------- SELECT deptno, AGG(emp_count) AS emp_count diff --git a/compliance/query/cases/a11_scalar_subquery/reference.duckdb.sql b/compliance/query/cases/a11_agg_in_where_subquery/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/a11_scalar_subquery/reference.duckdb.sql rename to compliance/query/cases/a11_agg_in_where_subquery/reference.duckdb.sql diff --git a/compliance/query/cases/a12_two_stage_aggregation/expected.sql b/compliance/query/cases/a12_aggregate_over_agg_values/expected.sql similarity index 100% rename from compliance/query/cases/a12_two_stage_aggregation/expected.sql rename to compliance/query/cases/a12_aggregate_over_agg_values/expected.sql diff --git a/compliance/query/cases/a12_two_stage_aggregation/query.ossie.sql b/compliance/query/cases/a12_aggregate_over_agg_values/query.ossie.sql similarity index 70% rename from compliance/query/cases/a12_two_stage_aggregation/query.ossie.sql rename to compliance/query/cases/a12_aggregate_over_agg_values/query.ossie.sql index 6c49fca1e..94db0248a 100644 --- a/compliance/query/cases/a12_two_stage_aggregation/query.ossie.sql +++ b/compliance/query/cases/a12_aggregate_over_agg_values/query.ossie.sql @@ -15,10 +15,14 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- A12. Two-stage aggregation and a set operator. --- The first branch averages the per-department payroll (29025 / 3). The --- second branch is the average salary per employee. After AGG() evaluates the --- measures, the values are ordinary scalars, so UNION ALL is valid. +-- 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, diff --git a/compliance/query/cases/a12_two_stage_aggregation/reference.duckdb.sql b/compliance/query/cases/a12_aggregate_over_agg_values/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/a12_two_stage_aggregation/reference.duckdb.sql rename to compliance/query/cases/a12_aggregate_over_agg_values/reference.duckdb.sql diff --git a/compliance/query/cases/b01_many_to_one_join/expected.sql b/compliance/query/cases/b01_many_to_one_no_fan_out/expected.sql similarity index 100% rename from compliance/query/cases/b01_many_to_one_join/expected.sql rename to compliance/query/cases/b01_many_to_one_no_fan_out/expected.sql diff --git a/compliance/query/cases/b01_many_to_one_join/query.ossie.sql b/compliance/query/cases/b01_many_to_one_no_fan_out/query.ossie.sql similarity index 86% rename from compliance/query/cases/b01_many_to_one_join/query.ossie.sql rename to compliance/query/cases/b01_many_to_one_no_fan_out/query.ossie.sql index d3df9e722..02f0c038e 100644 --- a/compliance/query/cases/b01_many_to_one_join/query.ossie.sql +++ b/compliance/query/cases/b01_many_to_one_no_fan_out/query.ossie.sql @@ -15,9 +15,11 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B1. Many-to-one join, group by a field of the joined relation. +-- 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 --- measures are the same as the per-department totals of A2. +-- values are the per-department totals of A2. -- Spec: 7.6 -- ---------------------------------------------------------------------------- SELECT d.dname, diff --git a/compliance/query/cases/b01_many_to_one_join/reference.duckdb.sql b/compliance/query/cases/b01_many_to_one_no_fan_out/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b01_many_to_one_join/reference.duckdb.sql rename to compliance/query/cases/b01_many_to_one_no_fan_out/reference.duckdb.sql diff --git a/compliance/query/cases/b02_where_on_joined_relation/expected.sql b/compliance/query/cases/b02_filter_on_joined_relation/expected.sql similarity index 100% rename from compliance/query/cases/b02_where_on_joined_relation/expected.sql rename to compliance/query/cases/b02_filter_on_joined_relation/expected.sql diff --git a/compliance/query/cases/b02_where_on_joined_relation/query.ossie.sql b/compliance/query/cases/b02_filter_on_joined_relation/query.ossie.sql similarity index 89% rename from compliance/query/cases/b02_where_on_joined_relation/query.ossie.sql rename to compliance/query/cases/b02_filter_on_joined_relation/query.ossie.sql index 851c06a79..0495bcded 100644 --- a/compliance/query/cases/b02_where_on_joined_relation/query.ossie.sql +++ b/compliance/query/cases/b02_filter_on_joined_relation/query.ossie.sql @@ -15,7 +15,9 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B2. WHERE on a field of the joined relation. +-- 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) diff --git a/compliance/query/cases/b02_where_on_joined_relation/reference.duckdb.sql b/compliance/query/cases/b02_filter_on_joined_relation/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b02_where_on_joined_relation/reference.duckdb.sql rename to compliance/query/cases/b02_filter_on_joined_relation/reference.duckdb.sql diff --git a/compliance/query/cases/b03_top_n_filter/expected.sql b/compliance/query/cases/b03_top_n_join_as_filter/expected.sql similarity index 100% rename from compliance/query/cases/b03_top_n_filter/expected.sql rename to compliance/query/cases/b03_top_n_join_as_filter/expected.sql diff --git a/compliance/query/cases/b03_top_n_filter/query.ossie.sql b/compliance/query/cases/b03_top_n_join_as_filter/query.ossie.sql similarity index 76% rename from compliance/query/cases/b03_top_n_filter/query.ossie.sql rename to compliance/query/cases/b03_top_n_join_as_filter/query.ossie.sql index 251af4d20..6945ac299 100644 --- a/compliance/query/cases/b03_top_n_filter/query.ossie.sql +++ b/compliance/query/cases/b03_top_n_join_as_filter/query.ossie.sql @@ -15,11 +15,14 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B3. Top-N filter through a join. +-- 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 it acts as a filter on emp, and the outer query reports --- payroll by job for those departments. The departments that the subquery --- ranks are the same values that the join matches (closure, spec 8.4). +-- 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 diff --git a/compliance/query/cases/b03_top_n_filter/reference.duckdb.sql b/compliance/query/cases/b03_top_n_join_as_filter/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b03_top_n_filter/reference.duckdb.sql rename to compliance/query/cases/b03_top_n_join_as_filter/reference.duckdb.sql diff --git a/compliance/query/cases/b04_fan_out/expected.sql b/compliance/query/cases/b04_one_side_counted_once/expected.sql similarity index 100% rename from compliance/query/cases/b04_fan_out/expected.sql rename to compliance/query/cases/b04_one_side_counted_once/expected.sql diff --git a/compliance/query/cases/b04_fan_out/query.ossie.sql b/compliance/query/cases/b04_one_side_counted_once/query.ossie.sql similarity index 69% rename from compliance/query/cases/b04_fan_out/query.ossie.sql rename to compliance/query/cases/b04_one_side_counted_once/query.ossie.sql index 7dde682ff..3effcd82e 100644 --- a/compliance/query/cases/b04_fan_out/query.ossie.sql +++ b/compliance/query/cases/b04_one_side_counted_once/query.ossie.sql @@ -15,11 +15,14 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B4. Fan-out: a measure of the "one" side, grouped by the "many" side. --- The join repeats each dept row once for each employee in it. AGG(dept_count) --- counts each department once in each group, so the result is the number of --- departments that have the job. Naive COUNT(*) gives 2, 4, 3, 1, 4, which --- AGG(e.emp_count) also gives, because the join repeats no emp row. +-- 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, diff --git a/compliance/query/cases/b04_fan_out/reference.duckdb.sql b/compliance/query/cases/b04_one_side_counted_once/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b04_fan_out/reference.duckdb.sql rename to compliance/query/cases/b04_one_side_counted_once/reference.duckdb.sql diff --git a/compliance/query/cases/b05_fan_out_self_join/expected.sql b/compliance/query/cases/b05_manager_counted_once/expected.sql similarity index 100% rename from compliance/query/cases/b05_fan_out_self_join/expected.sql rename to compliance/query/cases/b05_manager_counted_once/expected.sql diff --git a/compliance/query/cases/b05_fan_out_self_join/query.ossie.sql b/compliance/query/cases/b05_manager_counted_once/query.ossie.sql similarity index 69% rename from compliance/query/cases/b05_fan_out_self_join/query.ossie.sql rename to compliance/query/cases/b05_manager_counted_once/query.ossie.sql index 6fbc05b0d..4fd91bd8e 100644 --- a/compliance/query/cases/b05_fan_out_self_join/query.ossie.sql +++ b/compliance/query/cases/b05_manager_counted_once/query.ossie.sql @@ -15,13 +15,16 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B5. Fan-out in a self-join: the manager's salary. +-- 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 each manager's salary once in --- each group. The 4 salesmen all report to BLAKE, so the result is 2850; the --- naive SUM(m.sal) is 11400. The 4 clerks report to 4 different managers, so --- the two values are the same (11300). Naive SUM(m.sal) by job is 5950, --- 11300, 15000, 11400. +-- 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, diff --git a/compliance/query/cases/b05_fan_out_self_join/reference.duckdb.sql b/compliance/query/cases/b05_manager_counted_once/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b05_fan_out_self_join/reference.duckdb.sql rename to compliance/query/cases/b05_manager_counted_once/reference.duckdb.sql diff --git a/compliance/query/cases/b06_fan_out_range_join/expected.sql b/compliance/query/cases/b06_range_join_fan_out/expected.sql similarity index 100% rename from compliance/query/cases/b06_fan_out_range_join/expected.sql rename to compliance/query/cases/b06_range_join_fan_out/expected.sql diff --git a/compliance/query/cases/b06_fan_out_range_join/query.ossie.sql b/compliance/query/cases/b06_range_join_fan_out/query.ossie.sql similarity index 81% rename from compliance/query/cases/b06_fan_out_range_join/query.ossie.sql rename to compliance/query/cases/b06_range_join_fan_out/query.ossie.sql index 7477c412c..54f8caa6a 100644 --- a/compliance/query/cases/b06_fan_out_range_join/query.ossie.sql +++ b/compliance/query/cases/b06_range_join_fan_out/query.ossie.sql @@ -16,11 +16,13 @@ -- 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 a grade can match many +-- 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. Naive COUNT(*) --- gives 3, 5, 6. +-- 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, diff --git a/compliance/query/cases/b06_fan_out_range_join/reference.duckdb.sql b/compliance/query/cases/b06_range_join_fan_out/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b06_fan_out_range_join/reference.duckdb.sql rename to compliance/query/cases/b06_range_join_fan_out/reference.duckdb.sql diff --git a/compliance/query/cases/b07_fan_out_three_way_join/expected.sql b/compliance/query/cases/b07_three_way_join_fan_out/expected.sql similarity index 100% rename from compliance/query/cases/b07_fan_out_three_way_join/expected.sql rename to compliance/query/cases/b07_three_way_join_fan_out/expected.sql diff --git a/compliance/query/cases/b07_fan_out_three_way_join/query.ossie.sql b/compliance/query/cases/b07_three_way_join_fan_out/query.ossie.sql similarity index 81% rename from compliance/query/cases/b07_fan_out_three_way_join/query.ossie.sql rename to compliance/query/cases/b07_three_way_join_fan_out/query.ossie.sql index 6cb9a4627..0a0e5f55a 100644 --- a/compliance/query/cases/b07_fan_out_three_way_join/query.ossie.sql +++ b/compliance/query/cases/b07_three_way_join_fan_out/query.ossie.sql @@ -15,11 +15,13 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B7. Fan-out on two sides of a three-way join. --- emp joins to dept and to salgrade. The group is the salary grade, and +-- 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. Naive COUNT(*) gives 3, 3, 2, 5, 1. +-- the grade. A plain COUNT(*) over the joined rows gives 3, 3, 2, 5, 1. -- Spec: 7.6, G2 (8.3) -- ---------------------------------------------------------------------------- SELECT s.grade, diff --git a/compliance/query/cases/b07_fan_out_three_way_join/reference.duckdb.sql b/compliance/query/cases/b07_three_way_join_fan_out/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b07_fan_out_three_way_join/reference.duckdb.sql rename to compliance/query/cases/b07_three_way_join_fan_out/reference.duckdb.sql diff --git a/compliance/query/cases/b08_many_to_many_join/expected.sql b/compliance/query/cases/b08_many_to_many_chasm_trap/expected.sql similarity index 100% rename from compliance/query/cases/b08_many_to_many_join/expected.sql rename to compliance/query/cases/b08_many_to_many_chasm_trap/expected.sql diff --git a/compliance/query/cases/b08_many_to_many_join/query.ossie.sql b/compliance/query/cases/b08_many_to_many_chasm_trap/query.ossie.sql similarity index 78% rename from compliance/query/cases/b08_many_to_many_join/query.ossie.sql rename to compliance/query/cases/b08_many_to_many_chasm_trap/query.ossie.sql index 114b8904a..a4b482860 100644 --- a/compliance/query/cases/b08_many_to_many_join/query.ossie.sql +++ b/compliance/query/cases/b08_many_to_many_chasm_trap/query.ossie.sql @@ -15,11 +15,14 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B8. Many-to-many join (chasm trap). +-- 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. Naive SUM(e.sal) gives 26250, --- 54375, 56400, and naive COUNT(*) gives 9, 25, 36. +-- 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, diff --git a/compliance/query/cases/b08_many_to_many_join/reference.duckdb.sql b/compliance/query/cases/b08_many_to_many_chasm_trap/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b08_many_to_many_join/reference.duckdb.sql rename to compliance/query/cases/b08_many_to_many_chasm_trap/reference.duckdb.sql diff --git a/compliance/query/cases/b09_self_join_inner/expected.sql b/compliance/query/cases/b09_inner_join_matched_rows/expected.sql similarity index 100% rename from compliance/query/cases/b09_self_join_inner/expected.sql rename to compliance/query/cases/b09_inner_join_matched_rows/expected.sql diff --git a/compliance/query/cases/b09_self_join_inner/query.ossie.sql b/compliance/query/cases/b09_inner_join_matched_rows/query.ossie.sql similarity index 53% rename from compliance/query/cases/b09_self_join_inner/query.ossie.sql rename to compliance/query/cases/b09_inner_join_matched_rows/query.ossie.sql index ebb567947..5820a0518 100644 --- a/compliance/query/cases/b09_self_join_inner/query.ossie.sql +++ b/compliance/query/cases/b09_inner_join_matched_rows/query.ossie.sql @@ -15,27 +15,19 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B9. INNER JOIN. --- Only employees that have a manager, and only employees that are managers. +-- 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. -- --- B9-B13. One self-join query with each join type. --- --- e is the employee and m is the manager, joined on e.mgr = m.empno. Both --- sides have rows that do not match: --- * KING has no manager, so KING has no match on the e side. --- * 8 employees manage nobody, so they have no match on the m side. --- The join type decides which source rows reach each measure. The join never --- inflates a measure, and NULL-padded rows add nothing: --- --- join type joined rows e_count e_sal m_count m_sal --- INNER 13 13 24025.00 6 19275.00 --- LEFT 14 14 29025.00 6 19275.00 --- RIGHT 21 13 24025.00 14 29025.00 --- FULL 22 14 29025.00 14 29025.00 --- CROSS 196 14 29025.00 14 29025.00 --- --- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 --- times and BLAKE's 5 times. +-- 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, diff --git a/compliance/query/cases/b09_self_join_inner/reference.duckdb.sql b/compliance/query/cases/b09_inner_join_matched_rows/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b09_self_join_inner/reference.duckdb.sql rename to compliance/query/cases/b09_inner_join_matched_rows/reference.duckdb.sql diff --git a/compliance/query/cases/b10_self_join_left/expected.sql b/compliance/query/cases/b10_left_join_null_padding/expected.sql similarity index 100% rename from compliance/query/cases/b10_self_join_left/expected.sql rename to compliance/query/cases/b10_left_join_null_padding/expected.sql diff --git a/compliance/query/cases/b10_self_join_left/query.ossie.sql b/compliance/query/cases/b10_left_join_null_padding/query.ossie.sql similarity index 52% rename from compliance/query/cases/b10_self_join_left/query.ossie.sql rename to compliance/query/cases/b10_left_join_null_padding/query.ossie.sql index e93baf344..ccdd0de9b 100644 --- a/compliance/query/cases/b10_self_join_left/query.ossie.sql +++ b/compliance/query/cases/b10_left_join_null_padding/query.ossie.sql @@ -15,28 +15,18 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B10. LEFT JOIN. --- KING is kept on the e side. KING's row is NULL-padded on the m side, so the --- m measures do not change. +-- 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. -- --- B9-B13. One self-join query with each join type. --- --- e is the employee and m is the manager, joined on e.mgr = m.empno. Both --- sides have rows that do not match: --- * KING has no manager, so KING has no match on the e side. --- * 8 employees manage nobody, so they have no match on the m side. --- The join type decides which source rows reach each measure. The join never --- inflates a measure, and NULL-padded rows add nothing: --- --- join type joined rows e_count e_sal m_count m_sal --- INNER 13 13 24025.00 6 19275.00 --- LEFT 14 14 29025.00 6 19275.00 --- RIGHT 21 13 24025.00 14 29025.00 --- FULL 22 14 29025.00 14 29025.00 --- CROSS 196 14 29025.00 14 29025.00 --- --- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 --- times and BLAKE's 5 times. +-- 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, diff --git a/compliance/query/cases/b10_self_join_left/reference.duckdb.sql b/compliance/query/cases/b10_left_join_null_padding/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b10_self_join_left/reference.duckdb.sql rename to compliance/query/cases/b10_left_join_null_padding/reference.duckdb.sql diff --git a/compliance/query/cases/b11_self_join_right/expected.sql b/compliance/query/cases/b11_right_join_keeps_non_managers/expected.sql similarity index 100% rename from compliance/query/cases/b11_self_join_right/expected.sql rename to compliance/query/cases/b11_right_join_keeps_non_managers/expected.sql diff --git a/compliance/query/cases/b11_self_join_right/query.ossie.sql b/compliance/query/cases/b11_right_join_keeps_non_managers/query.ossie.sql similarity index 51% rename from compliance/query/cases/b11_self_join_right/query.ossie.sql rename to compliance/query/cases/b11_right_join_keeps_non_managers/query.ossie.sql index 64da5384e..387f239c8 100644 --- a/compliance/query/cases/b11_self_join_right/query.ossie.sql +++ b/compliance/query/cases/b11_right_join_keeps_non_managers/query.ossie.sql @@ -15,28 +15,18 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B11. RIGHT JOIN. --- 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. +-- 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. -- --- B9-B13. One self-join query with each join type. --- --- e is the employee and m is the manager, joined on e.mgr = m.empno. Both --- sides have rows that do not match: --- * KING has no manager, so KING has no match on the e side. --- * 8 employees manage nobody, so they have no match on the m side. --- The join type decides which source rows reach each measure. The join never --- inflates a measure, and NULL-padded rows add nothing: --- --- join type joined rows e_count e_sal m_count m_sal --- INNER 13 13 24025.00 6 19275.00 --- LEFT 14 14 29025.00 6 19275.00 --- RIGHT 21 13 24025.00 14 29025.00 --- FULL 22 14 29025.00 14 29025.00 --- CROSS 196 14 29025.00 14 29025.00 --- --- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 --- times and BLAKE's 5 times. +-- 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, diff --git a/compliance/query/cases/b11_self_join_right/reference.duckdb.sql b/compliance/query/cases/b11_right_join_keeps_non_managers/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b11_self_join_right/reference.duckdb.sql rename to compliance/query/cases/b11_right_join_keeps_non_managers/reference.duckdb.sql diff --git a/compliance/query/cases/b12_self_join_full/expected.sql b/compliance/query/cases/b12_full_join_each_row_once/expected.sql similarity index 100% rename from compliance/query/cases/b12_self_join_full/expected.sql rename to compliance/query/cases/b12_full_join_each_row_once/expected.sql 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_self_join_full/reference.duckdb.sql b/compliance/query/cases/b12_full_join_each_row_once/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b12_self_join_full/reference.duckdb.sql rename to compliance/query/cases/b12_full_join_each_row_once/reference.duckdb.sql diff --git a/compliance/query/cases/b12_self_join_full/query.ossie.sql b/compliance/query/cases/b12_self_join_full/query.ossie.sql deleted file mode 100644 index 36591ef4f..000000000 --- a/compliance/query/cases/b12_self_join_full/query.ossie.sql +++ /dev/null @@ -1,47 +0,0 @@ --- 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. --- Both sides keep all 14 employees. Each side counts each employee once, --- although the join has 22 rows. --- --- B9-B13. One self-join query with each join type. --- --- e is the employee and m is the manager, joined on e.mgr = m.empno. Both --- sides have rows that do not match: --- * KING has no manager, so KING has no match on the e side. --- * 8 employees manage nobody, so they have no match on the m side. --- The join type decides which source rows reach each measure. The join never --- inflates a measure, and NULL-padded rows add nothing: --- --- join type joined rows e_count e_sal m_count m_sal --- INNER 13 13 24025.00 6 19275.00 --- LEFT 14 14 29025.00 6 19275.00 --- RIGHT 21 13 24025.00 14 29025.00 --- FULL 22 14 29025.00 14 29025.00 --- CROSS 196 14 29025.00 14 29025.00 --- --- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 --- times and BLAKE's 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 -FULL JOIN scott_model.emp AS m ON e.mgr = m.empno; diff --git a/compliance/query/cases/b13_self_join_cross/expected.sql b/compliance/query/cases/b13_cross_join_no_inflation/expected.sql similarity index 100% rename from compliance/query/cases/b13_self_join_cross/expected.sql rename to compliance/query/cases/b13_cross_join_no_inflation/expected.sql diff --git a/compliance/query/cases/b13_self_join_cross/query.ossie.sql b/compliance/query/cases/b13_cross_join_no_inflation/query.ossie.sql similarity index 54% rename from compliance/query/cases/b13_self_join_cross/query.ossie.sql rename to compliance/query/cases/b13_cross_join_no_inflation/query.ossie.sql index fe049d758..3fca60950 100644 --- a/compliance/query/cases/b13_self_join_cross/query.ossie.sql +++ b/compliance/query/cases/b13_cross_join_no_inflation/query.ossie.sql @@ -15,28 +15,17 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B13. CROSS JOIN. --- The cross join has no condition and gives 14 x 14 = 196 rows. Each side --- still counts each employee once. Naive SUM(e.sal) is 406350. --- --- B9-B13. One self-join query with each join type. --- --- e is the employee and m is the manager, joined on e.mgr = m.empno. Both --- sides have rows that do not match: --- * KING has no manager, so KING has no match on the e side. --- * 8 employees manage nobody, so they have no match on the m side. --- The join type decides which source rows reach each measure. The join never --- inflates a measure, and NULL-padded rows add nothing: +-- 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. -- --- join type joined rows e_count e_sal m_count m_sal --- INNER 13 13 24025.00 6 19275.00 --- LEFT 14 14 29025.00 6 19275.00 --- RIGHT 21 13 24025.00 14 29025.00 --- FULL 22 14 29025.00 14 29025.00 --- CROSS 196 14 29025.00 14 29025.00 --- --- Naive SUM(m.sal) for the INNER join is 43650: KING's salary is counted 3 --- times and BLAKE's 5 times. +-- 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, diff --git a/compliance/query/cases/b13_self_join_cross/reference.duckdb.sql b/compliance/query/cases/b13_cross_join_no_inflation/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b13_self_join_cross/reference.duckdb.sql rename to compliance/query/cases/b13_cross_join_no_inflation/reference.duckdb.sql diff --git a/compliance/query/cases/b14_dept_emp_inner/expected.sql b/compliance/query/cases/b14_ratio_across_relations/expected.sql similarity index 100% rename from compliance/query/cases/b14_dept_emp_inner/expected.sql rename to compliance/query/cases/b14_ratio_across_relations/expected.sql diff --git a/compliance/query/cases/b14_dept_emp_inner/query.ossie.sql b/compliance/query/cases/b14_ratio_across_relations/query.ossie.sql similarity index 60% rename from compliance/query/cases/b14_dept_emp_inner/query.ossie.sql rename to compliance/query/cases/b14_ratio_across_relations/query.ossie.sql index 177de9d68..0526e5db5 100644 --- a/compliance/query/cases/b14_dept_emp_inner/query.ossie.sql +++ b/compliance/query/cases/b14_ratio_across_relations/query.ossie.sql @@ -15,22 +15,16 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B14. INNER JOIN. +-- 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"). -- --- B14-B17. One dept-emp query with inner and outer joins. --- --- The query computes the payroll per department as a ratio of two measures --- from different relations: AGG(e.sum_sal) / AGG(d.dept_count). The join --- repeats each dept row once for each employee in it (fan-out), but --- dept_count still counts each department once. Naive COUNT(d.deptno) is 14 --- for INNER and 15 for LEFT. --- --- The join type is the tool's choice, and it changes the answer (spec 10.2, --- A.6). The LEFT join keeps OPERATIONS, which has no employees, so the --- payroll is divided by 4 departments and not by 3. A filter in the ON clause --- of the LEFT join keeps all 4 departments but matches only the clerks. --- A RIGHT join with the sides swapped, or a FULL join, gives the LEFT --- result, because every employee has a department. +-- 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, diff --git a/compliance/query/cases/b14_dept_emp_inner/reference.duckdb.sql b/compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b14_dept_emp_inner/reference.duckdb.sql rename to compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql diff --git a/compliance/query/cases/b15_dept_emp_left/expected.sql b/compliance/query/cases/b15_left_join_changes_ratio/expected.sql similarity index 100% rename from compliance/query/cases/b15_dept_emp_left/expected.sql rename to compliance/query/cases/b15_left_join_changes_ratio/expected.sql diff --git a/compliance/query/cases/b15_dept_emp_left/query.ossie.sql b/compliance/query/cases/b15_left_join_changes_ratio/query.ossie.sql similarity index 56% rename from compliance/query/cases/b15_dept_emp_left/query.ossie.sql rename to compliance/query/cases/b15_left_join_changes_ratio/query.ossie.sql index 6b25ec111..bd8cac18f 100644 --- a/compliance/query/cases/b15_dept_emp_left/query.ossie.sql +++ b/compliance/query/cases/b15_left_join_changes_ratio/query.ossie.sql @@ -15,23 +15,21 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B15. LEFT JOIN. +-- 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"). -- --- B14-B17. One dept-emp query with inner and outer joins. --- --- The query computes the payroll per department as a ratio of two measures --- from different relations: AGG(e.sum_sal) / AGG(d.dept_count). The join --- repeats each dept row once for each employee in it (fan-out), but --- dept_count still counts each department once. Naive COUNT(d.deptno) is 14 --- for INNER and 15 for LEFT. --- --- The join type is the tool's choice, and it changes the answer (spec 10.2, --- A.6). The LEFT join keeps OPERATIONS, which has no employees, so the --- payroll is divided by 4 departments and not by 3. A filter in the ON clause --- of the LEFT join keeps all 4 departments but matches only the clerks. --- A RIGHT join with the sides swapped, or a FULL join, gives the LEFT --- result, because every employee has a department. --- Spec: 7.2, 7.6, 8.1, A.6 +-- 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, diff --git a/compliance/query/cases/b15_dept_emp_left/reference.duckdb.sql b/compliance/query/cases/b15_left_join_changes_ratio/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b15_dept_emp_left/reference.duckdb.sql rename to compliance/query/cases/b15_left_join_changes_ratio/reference.duckdb.sql diff --git a/compliance/query/cases/b16_dept_emp_left_on_filter/expected.sql b/compliance/query/cases/b16_on_filter_keeps_left_rows/expected.sql similarity index 100% rename from compliance/query/cases/b16_dept_emp_left_on_filter/expected.sql rename to compliance/query/cases/b16_on_filter_keeps_left_rows/expected.sql diff --git a/compliance/query/cases/b16_dept_emp_left_on_filter/query.ossie.sql b/compliance/query/cases/b16_on_filter_keeps_left_rows/query.ossie.sql similarity index 63% rename from compliance/query/cases/b16_dept_emp_left_on_filter/query.ossie.sql rename to compliance/query/cases/b16_on_filter_keeps_left_rows/query.ossie.sql index 0614e3b2a..18d6e7919 100644 --- a/compliance/query/cases/b16_dept_emp_left_on_filter/query.ossie.sql +++ b/compliance/query/cases/b16_on_filter_keeps_left_rows/query.ossie.sql @@ -15,25 +15,18 @@ -- specific language governing permissions and limitations -- under the License. -- ---------------------------------------------------------------------------- --- B16. LEFT JOIN with a filter in the ON clause. +-- 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. --- --- B14-B17. One dept-emp query with inner and outer joins. --- --- The query computes the payroll per department as a ratio of two measures --- from different relations: AGG(e.sum_sal) / AGG(d.dept_count). The join --- repeats each dept row once for each employee in it (fan-out), but --- dept_count still counts each department once. Naive COUNT(d.deptno) is 14 --- for INNER and 15 for LEFT. --- --- The join type is the tool's choice, and it changes the answer (spec 10.2, --- A.6). The LEFT join keeps OPERATIONS, which has no employees, so the --- payroll is divided by 4 departments and not by 3. A filter in the ON clause --- of the LEFT join keeps all 4 departments but matches only the clerks. --- A RIGHT join with the sides swapped, or a FULL join, gives the LEFT --- result, because every employee has a department. -- Spec: 7.2, 7.6, 8.1, A.6 -- ---------------------------------------------------------------------------- SELECT AGG(d.dept_count) AS dept_count, diff --git a/compliance/query/cases/b16_dept_emp_left_on_filter/reference.duckdb.sql b/compliance/query/cases/b16_on_filter_keeps_left_rows/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b16_dept_emp_left_on_filter/reference.duckdb.sql rename to compliance/query/cases/b16_on_filter_keeps_left_rows/reference.duckdb.sql diff --git a/compliance/query/cases/b17_dept_emp_left_grouped/expected.sql b/compliance/query/cases/b17_left_join_empty_group/expected.sql similarity index 100% rename from compliance/query/cases/b17_dept_emp_left_grouped/expected.sql rename to compliance/query/cases/b17_left_join_empty_group/expected.sql diff --git a/compliance/query/cases/b17_dept_emp_left_grouped/query.ossie.sql b/compliance/query/cases/b17_left_join_empty_group/query.ossie.sql similarity index 88% rename from compliance/query/cases/b17_dept_emp_left_grouped/query.ossie.sql rename to compliance/query/cases/b17_left_join_empty_group/query.ossie.sql index ba79342f1..57eec59bd 100644 --- a/compliance/query/cases/b17_dept_emp_left_grouped/query.ossie.sql +++ b/compliance/query/cases/b17_left_join_empty_group/query.ossie.sql @@ -16,10 +16,12 @@ -- 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. +-- 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, diff --git a/compliance/query/cases/b17_dept_emp_left_grouped/reference.duckdb.sql b/compliance/query/cases/b17_left_join_empty_group/reference.duckdb.sql similarity index 100% rename from compliance/query/cases/b17_dept_emp_left_grouped/reference.duckdb.sql rename to compliance/query/cases/b17_left_join_empty_group/reference.duckdb.sql From d466fac1b4e0001032f4b4fecb77352adec3cfb1 Mon Sep 17 00:00:00 2001 From: Chris Eubank <108756251+christianeu-db@users.noreply.github.com> Date: Thu, 5 Nov 2026 06:58:12 +0000 Subject: [PATCH 5/7] docs(compliance): describe the Section 9 recipe of the join reference queries As @jbonofre suggested on #632, the README now states the two steps of Section 9 that the earlier wording left out: within each output group, discard rows whose primary_key is NULL, keep one row per primary_key, then aggregate. It also describes the shape of the join reference queries (the joined and spine CTEs and one subquery per measure), and each join reference query points to it. Co-authored-by: Isaac --- compliance/query/README.md | 19 ++++++++++++++++--- .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ .../reference.duckdb.sql | 2 ++ 18 files changed, 50 insertions(+), 3 deletions(-) diff --git a/compliance/query/README.md b/compliance/query/README.md index 12c006711..84d0e1eb1 100644 --- a/compliance/query/README.md +++ b/compliance/query/README.md @@ -85,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 ---------------- 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 index fec7cc2d7..7535e94f1 100644 --- 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 @@ -16,6 +16,8 @@ -- 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 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 index 7d7c91e4e..ed3b85bd7 100644 --- a/compliance/query/cases/b02_filter_on_joined_relation/reference.duckdb.sql +++ b/compliance/query/cases/b02_filter_on_joined_relation/reference.duckdb.sql @@ -16,6 +16,8 @@ -- 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 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 index f6aac3344..f26e0a2f6 100644 --- 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 @@ -16,6 +16,8 @@ -- 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 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 index ffd0836f4..f13e4944b 100644 --- a/compliance/query/cases/b04_one_side_counted_once/reference.duckdb.sql +++ b/compliance/query/cases/b04_one_side_counted_once/reference.duckdb.sql @@ -16,6 +16,8 @@ -- 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, diff --git a/compliance/query/cases/b05_manager_counted_once/reference.duckdb.sql b/compliance/query/cases/b05_manager_counted_once/reference.duckdb.sql index b8f809586..5a7a2d94b 100644 --- a/compliance/query/cases/b05_manager_counted_once/reference.duckdb.sql +++ b/compliance/query/cases/b05_manager_counted_once/reference.duckdb.sql @@ -16,6 +16,8 @@ -- 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, 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 index 8eb330dd9..af20db571 100644 --- a/compliance/query/cases/b06_range_join_fan_out/reference.duckdb.sql +++ b/compliance/query/cases/b06_range_join_fan_out/reference.duckdb.sql @@ -16,6 +16,8 @@ -- 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, 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 index 10a6f5715..0c6158717 100644 --- 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 @@ -16,6 +16,8 @@ -- 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, 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 index d58e9cff2..137343825 100644 --- 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 @@ -16,6 +16,8 @@ -- 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, 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 index 80c9a72c2..b5d9267f2 100644 --- a/compliance/query/cases/b09_inner_join_matched_rows/reference.duckdb.sql +++ b/compliance/query/cases/b09_inner_join_matched_rows/reference.duckdb.sql @@ -16,6 +16,8 @@ -- 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 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 index 8b7158e8a..d95111887 100644 --- a/compliance/query/cases/b10_left_join_null_padding/reference.duckdb.sql +++ b/compliance/query/cases/b10_left_join_null_padding/reference.duckdb.sql @@ -16,6 +16,8 @@ -- 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 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 index a1dfd1753..b9ecbb78a 100644 --- 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 @@ -16,6 +16,8 @@ -- 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 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 index 724bc3b66..f3e389b4d 100644 --- 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 @@ -16,6 +16,8 @@ -- 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 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 index 4b05a33e7..df1102631 100644 --- a/compliance/query/cases/b13_cross_join_no_inflation/reference.duckdb.sql +++ b/compliance/query/cases/b13_cross_join_no_inflation/reference.duckdb.sql @@ -16,6 +16,8 @@ -- 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 diff --git a/compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql b/compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql index fd0b5cadc..d707e0474 100644 --- a/compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql +++ b/compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql @@ -16,6 +16,8 @@ -- 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 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 index 13b0b9bf2..e9793c023 100644 --- a/compliance/query/cases/b15_left_join_changes_ratio/reference.duckdb.sql +++ b/compliance/query/cases/b15_left_join_changes_ratio/reference.duckdb.sql @@ -16,6 +16,8 @@ -- 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 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 index 4ae3e3674..626930f64 100644 --- 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 @@ -16,6 +16,8 @@ -- 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 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 index 98b14db22..683f1b898 100644 --- a/compliance/query/cases/b17_left_join_empty_group/reference.duckdb.sql +++ b/compliance/query/cases/b17_left_join_empty_group/reference.duckdb.sql @@ -16,6 +16,8 @@ -- 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 cf330529c8615438289567311f3894a644829769 Mon Sep 17 00:00:00 2001 From: Chris Eubank <108756251+christianeu-db@users.noreply.github.com> Date: Thu, 5 Nov 2026 07:08:06 +0000 Subject: [PATCH 6/7] test(compliance): use the Ossie expression language in the model and casts - Cast to DOUBLE, not DOUBLE PRECISION. The CAST of the Ossie expression language lists FLOAT and DOUBLE as its floating-point types. The reference queries do the same. expected.sql keeps DOUBLE PRECISION for its typed NULL, because it is standard SQL. - Write every expression of scott_model.ossie.yaml in the portable form, expression.sql, as the canonical examples do (#640). The model had only OSSIE_SQL_2026 entries in dialects. No expected result changes. Co-authored-by: Isaac --- compliance/query/README.md | 4 +- .../query.ossie.sql | 2 +- .../reference.duckdb.sql | 4 +- .../reference.duckdb.sql | 2 +- .../a07_empty_input_set/reference.duckdb.sql | 2 +- .../reference.duckdb.sql | 2 +- .../reference.duckdb.sql | 2 +- .../query.ossie.sql | 2 +- .../reference.duckdb.sql | 4 +- .../query.ossie.sql | 2 +- .../reference.duckdb.sql | 2 +- .../query.ossie.sql | 2 +- .../reference.duckdb.sql | 2 +- .../query.ossie.sql | 2 +- .../reference.duckdb.sql | 2 +- .../reference.duckdb.sql | 2 +- compliance/query/scott/scott_model.ossie.yaml | 72 +++++++++++-------- 17 files changed, 63 insertions(+), 47 deletions(-) diff --git a/compliance/query/README.md b/compliance/query/README.md index 84d0e1eb1..d93b0a867 100644 --- a/compliance/query/README.md +++ b/compliance/query/README.md @@ -115,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 ----------------- 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 index 5d28ae15d..ec27cad44 100644 --- 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 @@ -27,7 +27,7 @@ -- Spec: 7.2, 8.1 -- ---------------------------------------------------------------------------- SELECT deptno, - CAST(AGG(sum_sal) AS DOUBLE PRECISION) / AGG(emp_count) AS sal_per_emp, + 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 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 index 50bb7207f..080fcf2f9 100644 --- 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 @@ -17,8 +17,8 @@ -- 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 PRECISION) / COUNT(*) AS sal_per_emp, - AVG(CAST(sal AS DOUBLE PRECISION)) AS avg_sal, + 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 diff --git a/compliance/query/cases/a06_nulls_follow_aggregate/reference.duckdb.sql b/compliance/query/cases/a06_nulls_follow_aggregate/reference.duckdb.sql index cdebaced4..25c897a3d 100644 --- a/compliance/query/cases/a06_nulls_follow_aggregate/reference.duckdb.sql +++ b/compliance/query/cases/a06_nulls_follow_aggregate/reference.duckdb.sql @@ -20,7 +20,7 @@ SELECT deptno, COUNT(*) AS emp_count, COUNT(comm) AS comm_count, SUM(comm) AS sum_comm, - AVG(CAST(comm AS DOUBLE PRECISION)) AS avg_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/reference.duckdb.sql b/compliance/query/cases/a07_empty_input_set/reference.duckdb.sql index 3f25b4cad..32228cf24 100644 --- a/compliance/query/cases/a07_empty_input_set/reference.duckdb.sql +++ b/compliance/query/cases/a07_empty_input_set/reference.duckdb.sql @@ -18,6 +18,6 @@ -- ../../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 PRECISION)) AS avg_sal + AVG(CAST(sal AS DOUBLE)) AS avg_sal FROM scott.emp WHERE deptno = 40; 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 index 7aaf0c223..31102cdb2 100644 --- 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 @@ -18,7 +18,7 @@ -- ../../scott/scott.sql. Its result is equal to the result of expected.sql. SELECT deptno, COUNT(*) AS emp_count, - AVG(CAST(sal AS DOUBLE PRECISION)) AS avg_sal + 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/a11_agg_in_where_subquery/reference.duckdb.sql b/compliance/query/cases/a11_agg_in_where_subquery/reference.duckdb.sql index 77cf176c3..7f2665a33 100644 --- a/compliance/query/cases/a11_agg_in_where_subquery/reference.duckdb.sql +++ b/compliance/query/cases/a11_agg_in_where_subquery/reference.duckdb.sql @@ -18,6 +18,6 @@ -- ../../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 PRECISION)) 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/query.ossie.sql b/compliance/query/cases/a12_aggregate_over_agg_values/query.ossie.sql index 94db0248a..5c2ef58ed 100644 --- a/compliance/query/cases/a12_aggregate_over_agg_values/query.ossie.sql +++ b/compliance/query/cases/a12_aggregate_over_agg_values/query.ossie.sql @@ -26,7 +26,7 @@ -- Spec: A.3, 7.8 -- ---------------------------------------------------------------------------- SELECT 'avg payroll per dept' AS metric, - AVG(CAST(dept_sal AS DOUBLE PRECISION)) AS amount + 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 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 index fbb691407..eeeed914f 100644 --- a/compliance/query/cases/a12_aggregate_over_agg_values/reference.duckdb.sql +++ b/compliance/query/cases/a12_aggregate_over_agg_values/reference.duckdb.sql @@ -17,11 +17,11 @@ -- 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 PRECISION)) AS amount + 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 PRECISION)) +SELECT 'avg salary per emp', AVG(CAST(sal AS DOUBLE)) FROM scott.emp ORDER BY metric; diff --git a/compliance/query/cases/b14_ratio_across_relations/query.ossie.sql b/compliance/query/cases/b14_ratio_across_relations/query.ossie.sql index 0526e5db5..9123ee291 100644 --- a/compliance/query/cases/b14_ratio_across_relations/query.ossie.sql +++ b/compliance/query/cases/b14_ratio_across_relations/query.ossie.sql @@ -30,6 +30,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 PRECISION) / AGG(d.dept_count) AS sal_per_dept + 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 index d707e0474..4a28501fb 100644 --- a/compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql +++ b/compliance/query/cases/b14_ratio_across_relations/reference.duckdb.sql @@ -40,5 +40,5 @@ measures AS ( 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 PRECISION) / dept_count AS sal_per_dept + CAST(sum_sal AS DOUBLE) / dept_count AS sal_per_dept FROM measures; 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 index bd8cac18f..48fbcfa71 100644 --- a/compliance/query/cases/b15_left_join_changes_ratio/query.ossie.sql +++ b/compliance/query/cases/b15_left_join_changes_ratio/query.ossie.sql @@ -34,6 +34,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 PRECISION) / AGG(d.dept_count) AS sal_per_dept + 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 index e9793c023..22a0c3f23 100644 --- a/compliance/query/cases/b15_left_join_changes_ratio/reference.duckdb.sql +++ b/compliance/query/cases/b15_left_join_changes_ratio/reference.duckdb.sql @@ -40,5 +40,5 @@ measures AS ( 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 PRECISION) / dept_count AS sal_per_dept + 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/query.ossie.sql b/compliance/query/cases/b16_on_filter_keeps_left_rows/query.ossie.sql index 18d6e7919..988eddbac 100644 --- 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 @@ -32,6 +32,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 PRECISION) / AGG(d.dept_count) AS sal_per_dept + 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 index 626930f64..a902ad1f2 100644 --- 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 @@ -40,5 +40,5 @@ measures AS ( 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 PRECISION) / dept_count AS sal_per_dept + CAST(sum_sal AS DOUBLE) / dept_count AS sal_per_dept FROM measures; 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 index 683f1b898..e85fc21e7 100644 --- a/compliance/query/cases/b17_left_join_empty_group/reference.duckdb.sql +++ b/compliance/query/cases/b17_left_join_empty_group/reference.duckdb.sql @@ -35,7 +35,7 @@ SELECT g.dname, 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 PRECISION)) + (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 diff --git a/compliance/query/scott/scott_model.ossie.yaml b/compliance/query/scott/scott_model.ossie.yaml index 1a2ff7022..5b25a3ebb 100644 --- a/compliance/query/scott/scott_model.ossie.yaml +++ b/compliance/query/scott/scott_model.ossie.yaml @@ -30,8 +30,8 @@ # The primary_key of each dataset is the row identity of its measures # (Section 3.2). # -# The AVG metrics cast their argument to DOUBLE PRECISION. For AVG over an -# exact numeric, the SQL standard leaves the scale of the result +# 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. @@ -48,59 +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: - dialects: - - dialect: OSSIE_SQL_2026 - expression: AVG(CAST(sal AS DOUBLE PRECISION)) + 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: - dialects: - - dialect: OSSIE_SQL_2026 - expression: AVG(CAST(comm AS DOUBLE PRECISION)) + sql: AVG(CAST(comm AS DOUBLE)) - name: dept source: scott.dept @@ -108,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 @@ -127,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 From 9d28a9f15d478cad0e083d332931cda2a8b23458 Mon Sep 17 00:00:00 2001 From: Chris Eubank <108756251+christianeu-db@users.noreply.github.com> Date: Thu, 5 Nov 2026 17:42:33 +0000 Subject: [PATCH 7/7] test(compliance): add fan-out risk cases B18-B20 with one-side measures B1-B3 report a measure of the "many" side over a many-to-one join, so a plain join-and-aggregate returns the same result and does not exercise the grain guarantee. These three cases put a measure of the "one" side where a fan-out inflates it, each through a different clause: - B18: a WHERE filter restricts a "one"-side measure that the join fans out. - B19: a join to a non-distinct filter subquery does not inflate a measure (the duplication comes from the filter relation, not a finer-grained table), covering the closure corollary of Section 8.4. - B20: a HAVING predicate over a fanned-out "one"-side measure, where a wrong grain changes the result set, not only the values. Each case reuses an existing metric, so the model is unchanged. For each, expected.sql equals reference.duckdb.sql on DuckDB, and a plain join-and-aggregate diverges (values noted in each header). Co-authored-by: Isaac --- .../expected.sql | 23 +++++++++++ .../query.ossie.sql | 36 ++++++++++++++++ .../reference.duckdb.sql | 41 +++++++++++++++++++ .../expected.sql | 21 ++++++++++ .../query.ossie.sql | 36 ++++++++++++++++ .../reference.duckdb.sql | 36 ++++++++++++++++ .../expected.sql | 20 +++++++++ .../query.ossie.sql | 33 +++++++++++++++ .../reference.duckdb.sql | 40 ++++++++++++++++++ 9 files changed, 286 insertions(+) create mode 100644 compliance/query/cases/b18_filter_restricts_one_side_measure/expected.sql create mode 100644 compliance/query/cases/b18_filter_restricts_one_side_measure/query.ossie.sql create mode 100644 compliance/query/cases/b18_filter_restricts_one_side_measure/reference.duckdb.sql create mode 100644 compliance/query/cases/b19_nonunique_filter_no_fan_out/expected.sql create mode 100644 compliance/query/cases/b19_nonunique_filter_no_fan_out/query.ossie.sql create mode 100644 compliance/query/cases/b19_nonunique_filter_no_fan_out/reference.duckdb.sql create mode 100644 compliance/query/cases/b20_having_on_one_side_measure/expected.sql create mode 100644 compliance/query/cases/b20_having_on_one_side_measure/query.ossie.sql create mode 100644 compliance/query/cases/b20_having_on_one_side_measure/reference.duckdb.sql 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;