Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6561] Add GROUP BY test cases for AS MEASURE aggregates #3946

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions core/src/test/resources/sql/measure.iq
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,136 @@ from emp;

!update

create view empm_comm as
Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't create a view for just one query. (Unless a view is necessary to demonstrate the behavior, and a CTE wouldn't do it.)

If it's not too disruptive of later tests, you could add avg_comm to empm.

select *, avg(sal) as measure avg_sal, avg(comm) as measure avg_comm
from emp;
(0 rows modified)

!update

# AGGREGATE with GROUP BY of a column that includes NULLs
select mgr, avg_comm from empm_comm
group by mgr;
+------+----------+
| MGR | AVG_COMM |
+------+----------+
| 7566 | |
| 7698 | 550.00 |
| 7782 | |
| 7788 | |
| 7839 | |
| 7902 | |
| | |
+------+----------+
(7 rows)

!ok

# AGGREGATE with GROUP BY of an expression
SELECT (job || '_yo') as job_yo, avg_sal FROM empm
GROUP BY (job || '_yo');
+--------------+---------+
| JOB_YO | AVG_SAL |
+--------------+---------+
| ANALYST_yo | 3000.00 |
| CLERK_yo | 1037.50 |
| MANAGER_yo | 2758.33 |
| PRESIDENT_yo | 5000.00 |
| SALESMAN_yo | 1400.00 |
+--------------+---------+
(5 rows)

!ok

# single GROUPING SETS group with AS MEASURE reference
SELECT job, year(hiredate) as hire_year, avg_sal FROM empm
Copy link
Contributor

Choose a reason for hiding this comment

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

if you're going to take 4 lines for GROUP BY, at least put FROM on its own line. (I find queries much more readable if clauses are on separate lines.)

Copy link
Contributor

Choose a reason for hiding this comment

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

This file is ordered by complexity - simple queries first - and groups related concepts. GROUPING SETS is relatively advanced functionality, so I would move these queries further down the measure.iq file.

GROUP BY GROUPING SETS
(
(YEAR(hiredate), job)
)
ORDER BY job, YEAR(hiredate);
+-----------+-----------+---------+
| JOB | HIRE_YEAR | AVG_SAL |
+-----------+-----------+---------+
| ANALYST | 1981 | 3000.00 |
| ANALYST | 1987 | 3000.00 |
| CLERK | 1980 | 800.00 |
| CLERK | 1981 | 950.00 |
| CLERK | 1982 | 1300.00 |
| CLERK | 1987 | 1100.00 |
| MANAGER | 1981 | 2758.33 |
| PRESIDENT | 1981 | 5000.00 |
| SALESMAN | 1981 | 1400.00 |
+-----------+-----------+---------+
(9 rows)

!ok

# GROUP BY empty group for totals
SELECT avg_sal FROM empm
GROUP BY ();
+---------+
| AVG_SAL |
+---------+
| 2073.21 |
+---------+
(1 row)

!ok

!if (fixed.fixed.calcite6561) {
# GROUP BY a dimension with NULLs
SELECT mgr, avg_sal FROM empm
GROUP BY mgr;
+------+---------+
| MGR | AVG_SAL |
+------+---------+
| 7566 | 3000.00 |
| 7698 | 1310.00 |
| 7782 | 1300.00 |
| 7788 | 1100.00 |
| 7839 | 2758.33 |
| 7902 | 800.00 |
| | 5000.00 |
+------+---------+
(7 rows)

!ok
!}

!if (fixed.fixed.calcite6562) {
# GROUP BY with multiple GROUPING SETS including total
SELECT job, year(hiredate) as hire_year, avg_sal FROM empm
GROUP BY GROUPING SETS
(
(YEAR(hiredate), job),
(YEAR(hiredate)),
()
)
ORDER BY job, YEAR(hiredate);
+-----------+-----------+---------+
| JOB | HIRE_YEAR | AVG_SAL |
+-----------+-----------+---------+
| ANALYST | 1981 | 3000.00 |
| ANALYST | 1987 | 3000.00 |
| CLERK | 1980 | 800.00 |
| CLERK | 1981 | 950.00 |
| CLERK | 1982 | 1300.00 |
| CLERK | 1987 | 1100.00 |
| MANAGER | 1981 | 2758.33 |
| PRESIDENT | 1981 | 5000.00 |
| SALESMAN | 1981 | 1400.00 |
| | 1980 | 800.00 |
| | 1981 | 2282.50 |
| | 1982 | 1300.00 |
| | 1987 | 2050.00 |
| | | 2073.21 |
+-----------+-----------+---------+
(14 rows)

!ok
!}

# Aggregate query with naked measure
select job, avg_sal as a
from empm
Expand Down
Loading