calcite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jh...@apache.org
Subject [2/2] calcite git commit: [CALCITE-2143] SqlImplementor wrongly generates SQL that refers to aliases in SELECT clause (Sam Waggoner)
Date Tue, 23 Jan 2018 01:33:17 GMT
[CALCITE-2143] SqlImplementor wrongly generates SQL that refers to aliases in SELECT clause
(Sam Waggoner)

Close apache/calcite#613


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/26b18910
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/26b18910
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/26b18910

Branch: refs/heads/master
Commit: 26b18910f267233f812c3774723516742b5b3f42
Parents: 2f06426
Author: Sam Waggoner <samwagg0583@gmail.com>
Authored: Mon Jan 22 06:54:07 2018 -0800
Committer: Julian Hyde <jhyde@apache.org>
Committed: Mon Jan 22 16:08:12 2018 -0800

----------------------------------------------------------------------
 .../calcite/rel/rel2sql/SqlImplementor.java     |  6 ++---
 .../rel/rel2sql/RelToSqlConverterTest.java      | 23 ++++++++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/26b18910/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
index b8a51b0..594cd3c 100644
--- a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
+++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
@@ -945,9 +945,9 @@ public abstract class SqlImplementor {
         if (needNew
                 && neededAlias != null
                 && (aliases.size() != 1 || !aliases.containsKey(neededAlias))) {
-          newContext =
-              aliasContext(ImmutableMap.of(neededAlias, rel.getRowType()),
-                  qualified);
+          final Map<String, RelDataType> newAliases =
+              ImmutableMap.of(neededAlias, rel.getInput(0).getRowType());
+          newContext = aliasContext(newAliases, qualified);
         } else {
           newContext = aliasContext(aliases, qualified);
         }

http://git-wip-us.apache.org/repos/asf/calcite/blob/26b18910/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 54cb4cd..d6bb976 100644
--- a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -2288,6 +2288,29 @@ public class RelToSqlConverterTest {
     sql(sql).ok(expected);
   }
 
+  @Test public void testFieldNamesWithAggregateSubQuery() {
+    final String query = "select mytable.\"city\",\n"
+        + "  sum(mytable.\"store_sales\") as \"my-alias\"\n"
+        + "from (select c.\"city\", s.\"store_sales\"\n"
+        + "  from \"sales_fact_1997\" as s\n"
+        + "    join \"customer\" as c using (\"customer_id\")\n"
+        + "  group by c.\"city\", s.\"store_sales\") AS mytable\n"
+        + "group by mytable.\"city\"";
+
+    final String expected = "SELECT \"t0\".\"city\","
+        + " SUM(\"t0\".\"store_sales\") AS \"my-alias\"\n"
+        + "FROM (SELECT \"customer\".\"city\","
+        + " \"sales_fact_1997\".\"store_sales\"\n"
+        + "FROM \"foodmart\".\"sales_fact_1997\"\n"
+        + "INNER JOIN \"foodmart\".\"customer\""
+        + " ON \"sales_fact_1997\".\"customer_id\""
+        + " = \"customer\".\"customer_id\"\n"
+        + "GROUP BY \"customer\".\"city\","
+        + " \"sales_fact_1997\".\"store_sales\") AS \"t0\"\n"
+        + "GROUP BY \"t0\".\"city\"";
+    sql(query).ok(expected);
+  }
+
   /** Fluid interface to run tests. */
   private static class Sql {
     private CalciteAssert.SchemaSpec schemaSpec;


Mime
View raw message