calcite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jh...@apache.org
Subject [2/4] calcite git commit: [CALCITE-1252] Handle ANY type in RexBuilder.ensureType and TypeFactory.leastRestrictive (Mehand Baid, Minji Kim)
Date Mon, 06 Jun 2016 18:32:07 GMT
[CALCITE-1252] Handle ANY type in RexBuilder.ensureType and TypeFactory.leastRestrictive (Mehand
Baid, Minji Kim)

Close apache/calcite#243


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

Branch: refs/heads/branch-1.8
Commit: a4ccb80a91b4592f902e3f34355aa391d6cb4066
Parents: e77b866
Author: Minji Kim <minji@dremio.com>
Authored: Fri Jun 3 14:05:31 2016 -0700
Committer: Julian Hyde <jhyde@apache.org>
Committed: Mon Jun 6 11:14:18 2016 -0700

----------------------------------------------------------------------
 .../java/org/apache/calcite/rex/RexBuilder.java |  7 ++
 .../calcite/sql/type/SqlTypeFactoryImpl.java    | 12 +++
 .../org/apache/calcite/rex/RexBuilderTest.java  | 84 ++++++++++++++++++++
 .../calcite/sql/type/SqlTypeFactoryTest.java    | 55 +++++++++++++
 .../org/apache/calcite/test/CalciteSuite.java   |  4 +
 .../calcite/test/SqlToRelConverterTest.xml      |  4 +-
 6 files changed, 164 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/a4ccb80a/core/src/main/java/org/apache/calcite/rex/RexBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rex/RexBuilder.java b/core/src/main/java/org/apache/calcite/rex/RexBuilder.java
index 1796784..2d8b459 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexBuilder.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexBuilder.java
@@ -999,6 +999,13 @@ public class RexBuilder {
     if (matchNullability) {
       targetType = matchNullability(type, node);
     }
+
+    if (targetType.getSqlTypeName() == SqlTypeName.ANY
+        && (!matchNullability
+            || targetType.isNullable() == node.getType().isNullable())) {
+      return node;
+    }
+
     if (!node.getType().equals(targetType)) {
       return makeCast(targetType, node);
     }

http://git-wip-us.apache.org/repos/asf/calcite/blob/a4ccb80a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeFactoryImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeFactoryImpl.java b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeFactoryImpl.java
index 28a5d2d..c7636cc 100644
--- a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeFactoryImpl.java
+++ b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeFactoryImpl.java
@@ -219,12 +219,18 @@ public class SqlTypeFactoryImpl extends RelDataTypeFactoryImpl {
     int nullCount = 0;
     int nullableCount = 0;
     int javaCount = 0;
+    int anyCount = 0;
 
     for (RelDataType type : types) {
       final SqlTypeName typeName = type.getSqlTypeName();
       if (typeName == null) {
         return null;
       }
+
+      if (typeName == SqlTypeName.ANY) {
+        anyCount++;
+      }
+
       if (type.isNullable()) {
         ++nullableCount;
       }
@@ -236,6 +242,12 @@ public class SqlTypeFactoryImpl extends RelDataTypeFactoryImpl {
       }
     }
 
+
+    //  if any of the inputs are ANY, the output is ANY
+    if (anyCount > 0) {
+      return createTypeWithNullability(createSqlType(SqlTypeName.ANY), nullCount > 0);
+    }
+
     for (int i = 0; i < types.size(); ++i) {
       RelDataType type = types.get(i);
       RelDataTypeFamily family = type.getFamily();

http://git-wip-us.apache.org/repos/asf/calcite/blob/a4ccb80a/core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java b/core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java
new file mode 100644
index 0000000..28671fc
--- /dev/null
+++ b/core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rex;
+
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+/**
+ * Test for {@link RexBuilder}
+ */
+public class RexBuilderTest {
+
+  /**
+   * Test RexBuilder.ensureType()
+   */
+  @Test
+  public void testEnsureTypeWithAny() {
+    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+    RexBuilder builder = new RexBuilder(typeFactory);
+
+    RexNode node =  new RexLiteral(
+            Boolean.TRUE, typeFactory.createSqlType(SqlTypeName.BOOLEAN), SqlTypeName.BOOLEAN);
+    RexNode ensuredNode = builder.ensureType(
+            typeFactory.createSqlType(SqlTypeName.ANY), node, true);
+
+    assertEquals(node, ensuredNode);
+  }
+
+  /**
+   * Test RexBuilder.ensureType()
+   */
+  @Test
+  public void testEnsureTypeWithItself() {
+    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+    RexBuilder builder = new RexBuilder(typeFactory);
+
+    RexNode node =  new RexLiteral(
+            Boolean.TRUE, typeFactory.createSqlType(SqlTypeName.BOOLEAN), SqlTypeName.BOOLEAN);
+    RexNode ensuredNode = builder.ensureType(
+            typeFactory.createSqlType(SqlTypeName.BOOLEAN), node, true);
+
+    assertEquals(node, ensuredNode);
+  }
+
+  /**
+   * Test RexBuilder.ensureType()
+   */
+  @Test
+  public void testEnsureTypeWithDifference() {
+    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+    RexBuilder builder = new RexBuilder(typeFactory);
+
+    RexNode node =  new RexLiteral(
+            Boolean.TRUE, typeFactory.createSqlType(SqlTypeName.BOOLEAN), SqlTypeName.BOOLEAN);
+    RexNode ensuredNode = builder.ensureType(
+            typeFactory.createSqlType(SqlTypeName.INTEGER), node, true);
+
+    assertNotEquals(node, ensuredNode);
+    assertEquals(ensuredNode.getType(), typeFactory.createSqlType(SqlTypeName.INTEGER));
+  }
+
+}
+// End RexBuilderTest.java

http://git-wip-us.apache.org/repos/asf/calcite/blob/a4ccb80a/core/src/test/java/org/apache/calcite/sql/type/SqlTypeFactoryTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/sql/type/SqlTypeFactoryTest.java b/core/src/test/java/org/apache/calcite/sql/type/SqlTypeFactoryTest.java
new file mode 100644
index 0000000..5748b14
--- /dev/null
+++ b/core/src/test/java/org/apache/calcite/sql/type/SqlTypeFactoryTest.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.sql.type;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+
+import com.google.common.collect.Lists;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Test for {@link SqlTypeFactoryImpl}
+ */
+public class SqlTypeFactoryTest {
+
+  @Test
+  public void testLeastRestrictiveWithAny() {
+    SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+    final RelDataType sqlBigInt = typeFactory.createSqlType(SqlTypeName.BIGINT);
+    final RelDataType sqlAny = typeFactory.createSqlType(SqlTypeName.ANY);
+
+    RelDataType leastRestrictive =
+            typeFactory.leastRestrictive(Lists.newArrayList(sqlBigInt, sqlAny));
+    assertEquals(leastRestrictive.getSqlTypeName(), SqlTypeName.ANY);
+  }
+
+  @Test
+  public void testLeastRestrictiveWithNumbers() {
+    SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+    final RelDataType sqlBigInt = typeFactory.createSqlType(SqlTypeName.BIGINT);
+    final RelDataType sqlInt = typeFactory.createSqlType(SqlTypeName.INTEGER);
+
+    RelDataType leastRestrictive =
+            typeFactory.leastRestrictive(Lists.newArrayList(sqlBigInt, sqlInt));
+    assertEquals(leastRestrictive.getSqlTypeName(), SqlTypeName.BIGINT);
+  }
+}
+// End SqlTypeFactoryTest.java

http://git-wip-us.apache.org/repos/asf/calcite/blob/a4ccb80a/core/src/test/java/org/apache/calcite/test/CalciteSuite.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/CalciteSuite.java b/core/src/test/java/org/apache/calcite/test/CalciteSuite.java
index c43a76e..0132016 100644
--- a/core/src/test/java/org/apache/calcite/test/CalciteSuite.java
+++ b/core/src/test/java/org/apache/calcite/test/CalciteSuite.java
@@ -26,6 +26,7 @@ import org.apache.calcite.plan.volcano.VolcanoPlannerTest;
 import org.apache.calcite.plan.volcano.VolcanoPlannerTraitTest;
 import org.apache.calcite.rel.RelCollationTest;
 import org.apache.calcite.rel.rel2sql.RelToSqlConverterTest;
+import org.apache.calcite.rex.RexBuilderTest;
 import org.apache.calcite.rex.RexExecutorTest;
 import org.apache.calcite.runtime.BinarySearchTest;
 import org.apache.calcite.runtime.EnumerablesTest;
@@ -36,6 +37,7 @@ import org.apache.calcite.sql.test.SqlAdvisorTest;
 import org.apache.calcite.sql.test.SqlOperatorTest;
 import org.apache.calcite.sql.test.SqlPrettyWriterTest;
 import org.apache.calcite.sql.test.SqlTypeNameTest;
+import org.apache.calcite.sql.type.SqlTypeFactoryTest;
 import org.apache.calcite.test.enumerable.EnumerableCorrelateTest;
 import org.apache.calcite.tools.FrameworksTest;
 import org.apache.calcite.tools.PlannerTest;
@@ -94,6 +96,8 @@ import org.junit.runners.Suite;
     ExceptionMessageTest.class,
     InduceGroupingTypeTest.class,
     RelOptPlanReaderTest.class,
+    RexBuilderTest.class,
+    SqlTypeFactoryTest.class,
 
     // medium tests (above 0.1s)
     SqlParserTest.class,

http://git-wip-us.apache.org/repos/asf/calcite/blob/a4ccb80a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
----------------------------------------------------------------------
diff --git a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
index 1ac074f..d16b3fd 100644
--- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
@@ -3363,7 +3363,7 @@ LogicalProject(**=[$0], **0=[$1])
             <![CDATA[
 LogicalAggregate(group=[{0}], CNT=[COUNT()])
   LogicalProject(N_REGIONKEY=[ITEM($0, 'N_REGIONKEY')])
-    LogicalFilter(condition=[>(CAST(ITEM($0, 'N_NATIONKEY')):INTEGER, 5)])
+    LogicalFilter(condition=[>(ITEM($0, 'N_NATIONKEY'), 5)])
       LogicalProject(**=[$0])
         LogicalTableScan(table=[[CATALOG, SALES, NATION]])
 ]]>
@@ -3391,7 +3391,7 @@ LogicalProject(**=[$0], **0=[$1])
         <Resource name="plan">
             <![CDATA[
 LogicalProject(**=[$1], R_REGIONKEY=[$2], R_NAME=[$3], R_COMMENT=[$4])
-  LogicalFilter(condition=[=(CAST($0):INTEGER, $2)])
+  LogicalFilter(condition=[=($0, $2)])
     LogicalJoin(condition=[true], joinType=[inner])
       LogicalTableScan(table=[[CATALOG, SALES, NATION]])
       LogicalTableScan(table=[[CATALOG, SALES, REGION]])


Mime
View raw message