-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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-3662] Generate wrong SQL when plan contains Project(Sort(Aggregate)) and aggregate field has no alias #1715
base: main
Are you sure you want to change the base?
Changes from 1 commit
2d0335a
e3fddf2
48cb514
4c05dd9
e6943c9
ba08a0c
c6c1e72
bfc7246
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,6 +177,17 @@ public class JdbcAdapterTest { | |
+ "ON \"t\".\"DEPTNO\" = \"t0\".\"DEPTNO\""); | ||
} | ||
|
||
@Test public void testGroupByWithLimitPlan() { | ||
CalciteAssert.model(JdbcTest.SCOTT_MODEL) | ||
.query("select sum(sal)\n" | ||
+ "from scott.emp \n" | ||
+ "group by ename limit 10") | ||
.planHasSql("SELECT SUM(\"SAL\")\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add the tests in |
||
+ "FROM \"SCOTT\".\"EMP\"\n" | ||
+ "GROUP BY \"ENAME\"\n" | ||
+ "LIMIT 10"); | ||
} | ||
|
||
@Test public void testPushDownSort() { | ||
CalciteAssert.model(JdbcTest.SCOTT_MODEL) | ||
.query("select ename \n" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,14 +468,14 @@ private Fluent pig(String script) { | |
+ "HIREDATE, SAL, COMM, DEPTNO)) AS A\n" | ||
+ " FROM scott.EMP\n" | ||
+ " GROUP BY DEPTNO) AS $cor4,\n" | ||
+ " LATERAL (SELECT COLLECT(ROW(ENAME, JOB, DEPTNO, SAL)) AS X\n" | ||
+ " FROM (SELECT ENAME, JOB, DEPTNO, SAL\n" | ||
+ " LATERAL (SELECT X\n" | ||
+ " FROM (SELECT 'all' AS $f0, COLLECT(ROW(ENAME, JOB, DEPTNO, SAL)) AS X\n" | ||
+ " FROM UNNEST (SELECT $cor4.A AS $f0\n" | ||
+ " FROM (VALUES (0)) AS t (ZERO)) " | ||
+ "AS t2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)\n" | ||
+ " WHERE JOB <> 'CLERK'\n" | ||
+ " ORDER BY SAL) AS t5\n" | ||
+ " GROUP BY 'all') AS t8) AS $cor5,\n" | ||
+ " GROUP BY 'all'\n" | ||
+ " ORDER BY SAL) AS t7) AS t8) AS $cor5,\n" | ||
+ " LATERAL UNNEST (SELECT $cor5.X AS $f0\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @khaitranq @ihuzenko @danny0405 @vlsi There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently select list is given as '*' when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Background info: Hi Jin Xing, I referenced method [1] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for explanation @helloppx
RelToSqlConverter will convert the plan to below sql (with the fix bring by this PR):
Obviously it's not correct (the outmost There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we rectify the alias for subquery, above case I mentioned should work well. |
||
+ " FROM (VALUES (0)) AS t (ZERO)) " | ||
+ "AS t11 (ENAME, JOB, DEPTNO, SAL) AS t110\n" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
builder.clauses.add(Clause.SELECT)
is useless.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change, i believe that code snippet in
visit(Sort)
can be removed:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think below fix snippet here is not correct.
Above change apply the projects on the select list from
Sort
. IfSort
uses ordinal inORDER BY
clause, the semantics can be wrong.Check below case
It will be converted as
The order of the result is obviously inconsistent with the original Sql.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we fix the alias, I think we don't need the change here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may need to replace the
order by ordinal
withorder by alias
to solve this problem completely. Not all the DBs supports order by ordinal, they all support order by alias.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much @jinxing64 , @danny0405
I did some test, it will cause another issue[1] to replace
order by ordinal
withorder by alias
,so I deleted the code snippet[2]
[1]
[2]