You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2019/12/26 03:05:26 UTC

[GitHub] [calcite] helloppx commented on a change in pull request #1680: [CALCITE-3621] Push down sort to DB, SQL of Sort rel contains explicit field name instead of * when sort is not top rel

helloppx commented on a change in pull request #1680: [CALCITE-3621] Push down sort to DB, SQL of Sort rel contains explicit field name instead of * when sort is not top rel
URL: https://github.com/apache/calcite/pull/1680#discussion_r360756860
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
 ##########
 @@ -634,6 +634,13 @@ public Result visit(Sort e) {
     }
     Result x = visitChild(0, e.getInput());
     Builder builder = x.builder(e, Clause.ORDER_BY);
+    if (builder.select.getSelectList() == null) {
 
 Review comment:
   When JdbcSort is used, a regression issue will be trigged: JdbcTest#testSelfJoinDifferentColumns()
   
   Following is the regression issue info:
   
   Input SQL: A
   select e1."full_name"
     from "foodmart"."employee" as e1
     join "foodmart"."employee" as e2 on e1."first_name" = e2."last_name"
   order by e1."last_name" limit 3
   
   Best REL:
   JdbcToEnumerableConverter
   --JdbcProject(full_name=[$1], last_name=[$3])
   ----JdbcSort(sort0=[$3], dir0=[ASC], fetch=[3])
   ------JdbcJoin(condition=[=($2, $0)], joinType=[inner])
   --------JdbcProject(last_name=[$3])
   ----------JdbcTableScan(table=[[foodmart, employee]])
   --------JdbcProject(full_name=[$1], first_name=[$2], last_name=[$3])
   ----------JdbcTableScan(table=[[foodmart, employee]])
   
   Output SQL: B
   SELECT "t2"."full_name", "t2"."last_name0" AS "last_name"
   FROM (SELECT *
   FROM (SELECT "last_name"
   FROM "foodmart"."employee") AS "t"
   INNER JOIN (SELECT "full_name", "first_name", "last_name"
   FROM "foodmart"."employee") AS "t0" ON "t"."last_name" = "t0"."first_name"
   ORDER BY "t0"."last_name" NULLS LAST
   LIMIT 3) AS "t2"
   
   After fix, output SQL: C
   SELECT "t2"."full_name", "t2"."last_name0" AS "last_name"
   FROM (SELECT "t"."last_name", "t0"."full_name", "t0"."first_name", "t0"."last_name" AS "last_name0"
   FROM (SELECT "last_name"
   FROM "foodmart"."employee") AS "t"
   INNER JOIN (SELECT "full_name", "first_name", "last_name"
   FROM "foodmart"."employee") AS "t0" ON "t"."last_name" = "t0"."first_name"
   ORDER BY "t0"."last_name" NULLS LAST
   LIMIT 3) AS "t2"
   
   SQL B is wrong, since "t2" is a subquery but it's select list is ambiguous. Database complains "last_name0" cannot be found in schema. 
   that is, the SQL of RelToSqlConverter#visit(Sort e) generated should has explicit select list when the Sort is not outter rel.
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services