You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2017/05/21 17:42:57 UTC

[3/3] calcite git commit: [CALCITE-1792] In RelToSqlConverter, handle TRUE join condition (Sergii Simonov)

[CALCITE-1792] In RelToSqlConverter, handle TRUE join condition (Sergii Simonov)

Close apache/calcite#451


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

Branch: refs/heads/master
Commit: 7d8e0528bec568e2abe7f8287510c1e79b178b1d
Parents: ac364e8
Author: Sergii Simonov <si...@teamaol.com>
Authored: Tue May 16 14:14:57 2017 +0100
Committer: Julian Hyde <jh...@apache.org>
Committed: Sat May 20 22:40:21 2017 -0700

----------------------------------------------------------------------
 .../calcite/rel/rel2sql/SqlImplementor.java     |  9 ++++-
 .../rel/rel2sql/RelToSqlConverterTest.java      | 35 ++++++++++++++++----
 2 files changed, 36 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/7d8e0528/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 181bfc7..22eac7b 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
@@ -262,6 +262,12 @@ public abstract class SqlImplementor {
   public static SqlNode convertConditionToSqlNode(RexNode node,
       Context leftContext,
       Context rightContext, int leftFieldCount) {
+    if (node.isAlwaysTrue()) {
+      return SqlLiteral.createBoolean(true, POS);
+    }
+    if (node.isAlwaysFalse()) {
+      return SqlLiteral.createBoolean(false, POS);
+    }
     if (!(node instanceof RexCall)) {
       throw new AssertionError(node);
     }
@@ -336,8 +342,9 @@ public abstract class SqlImplementor {
       joinContext =
           leftContext.implementor().joinContext(leftContext, rightContext);
       return joinContext.toSql(null, node);
+    default:
+      throw new AssertionError(node);
     }
-    throw new AssertionError(node);
   }
 
   /** Removes cast from string.

http://git-wip-us.apache.org/repos/asf/calcite/blob/7d8e0528/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 af122d5..295e443 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
@@ -378,7 +378,7 @@ public class RelToSqlConverterTest {
     sql(query).dialect(DatabaseProduct.DB2.getDialect()).ok(expected);
   }
 
-  @Test public void testCartesianProduct() {
+  @Test public void testCartesianProductWithCommaSyntax() {
     String query = "select * from \"department\" , \"employee\"";
     String expected = "SELECT *\n"
         + "FROM \"foodmart\".\"department\",\n"
@@ -386,6 +386,24 @@ public class RelToSqlConverterTest {
     sql(query).ok(expected);
   }
 
+  @Test public void testCartesianProductWithInnerJoinSyntax() {
+    String query = "select * from \"department\"\n"
+        + "INNER JOIN \"employee\" ON TRUE";
+    String expected = "SELECT *\n"
+        + "FROM \"foodmart\".\"department\",\n"
+        + "\"foodmart\".\"employee\"";
+    sql(query).ok(expected);
+  }
+
+  @Test public void testFullJoinOnTrueCondition() {
+    String query = "select * from \"department\"\n"
+        + "FULL JOIN \"employee\" ON TRUE";
+    String expected = "SELECT *\n"
+        + "FROM \"foodmart\".\"department\"\n"
+        + "FULL JOIN \"foodmart\".\"employee\" ON TRUE";
+    sql(query).ok(expected);
+  }
+
   @Test public void testSimpleIn() {
     String query = "select * from \"department\" where \"department_id\" in (\n"
         + "  select \"department_id\" from \"employee\"\n"
@@ -571,20 +589,23 @@ public class RelToSqlConverterTest {
         + "inner join \"foodmart\".\"customer\" as \"t2\"\n"
         + "on \"t1\".\"customer_id\" = \"t2\".\"customer_id\" or "
         + "(\"t1\".\"customer_id\" is null "
-        + "and \"t2\".\"customer_id\" is null)\n"
+        + "and \"t2\".\"customer_id\" is null) or\n"
+        + "\"t2\".\"occupation\" is null\n"
         + "inner join \"foodmart\".\"product\" as \"t3\"\n"
         + "on \"t1\".\"product_id\" = \"t3\".\"product_id\" or "
         + "(\"t1\".\"product_id\" is not null or "
         + "\"t3\".\"product_id\" is not null)";
+    // Some of the "IS NULL" and "IS NOT NULL" are reduced to TRUE or FALSE,
+    // but not all.
     String expected = "SELECT *\nFROM \"foodmart\".\"sales_fact_1997\"\n"
         + "INNER JOIN \"foodmart\".\"customer\" "
         + "ON \"sales_fact_1997\".\"customer_id\" = \"customer\".\"customer_id\""
-        + " OR \"sales_fact_1997\".\"customer_id\" IS NULL "
-        + "AND \"customer\".\"customer_id\" IS NULL\n"
+        + " OR FALSE AND FALSE"
+        + " OR \"customer\".\"occupation\" IS NULL\n"
         + "INNER JOIN \"foodmart\".\"product\" "
-        + "ON \"sales_fact_1997\".\"product_id\" = \"product\".\"product_id\" OR "
-        + "\"sales_fact_1997\".\"product_id\" IS NOT NULL "
-        + "OR \"product\".\"product_id\" IS NOT NULL";
+        + "ON \"sales_fact_1997\".\"product_id\" = \"product\".\"product_id\""
+        + " OR TRUE"
+        + " OR TRUE";
     sql(query).ok(expected);
   }