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 2016/06/15 16:39:30 UTC

[2/5] calcite git commit: [CALCITE-1283] leastRestrictiveSqlType() should account for nullability of each type and null types (Minji Kim)

[CALCITE-1283] leastRestrictiveSqlType() should account for nullability of each type and null types (Minji Kim)

Close apache/calcite#246


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

Branch: refs/heads/master
Commit: 0404349e84b6286aa16add5ce9ae6728240ffdbc
Parents: acd27fd
Author: Minji Kim <mi...@dremio.com>
Authored: Fri Jun 10 13:28:59 2016 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Tue Jun 14 18:02:59 2016 -0700

----------------------------------------------------------------------
 .../calcite/sql/type/SqlTypeFactoryImpl.java    |  6 +-
 .../calcite/sql/type/SqlTypeFactoryTest.java    | 58 ++++++++++++++------
 2 files changed, 44 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/0404349e/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 c7636cc..57378cc 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
@@ -226,11 +226,9 @@ public class SqlTypeFactoryImpl extends RelDataTypeFactoryImpl {
       if (typeName == null) {
         return null;
       }
-
       if (typeName == SqlTypeName.ANY) {
         anyCount++;
       }
-
       if (type.isNullable()) {
         ++nullableCount;
       }
@@ -242,10 +240,10 @@ 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);
+      return createTypeWithNullability(createSqlType(SqlTypeName.ANY),
+          nullCount > 0 || nullableCount > 0);
     }
 
     for (int i = 0; i < types.size(); ++i) {

http://git-wip-us.apache.org/repos/asf/calcite/blob/0404349e/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
index 5748b14..4ca75fa 100644
--- a/core/src/test/java/org/apache/calcite/sql/type/SqlTypeFactoryTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/type/SqlTypeFactoryTest.java
@@ -23,33 +23,59 @@ import com.google.common.collect.Lists;
 
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
 
 /**
- * Test for {@link SqlTypeFactoryImpl}
+ * 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);
+  @Test public void testLeastRestrictiveWithAny() {
+    Fixture f = new Fixture();
+    RelDataType leastRestrictive =
+        f.typeFactory.leastRestrictive(Lists.newArrayList(f.sqlBigInt, f.sqlAny));
+    assertThat(leastRestrictive.getSqlTypeName(), is(SqlTypeName.ANY));
+  }
 
+  @Test public void testLeastRestrictiveWithNumbers() {
+    Fixture f = new Fixture();
     RelDataType leastRestrictive =
-            typeFactory.leastRestrictive(Lists.newArrayList(sqlBigInt, sqlAny));
-    assertEquals(leastRestrictive.getSqlTypeName(), SqlTypeName.ANY);
+        f.typeFactory.leastRestrictive(Lists.newArrayList(f.sqlBigInt, f.sqlInt));
+    assertThat(leastRestrictive.getSqlTypeName(), is(SqlTypeName.BIGINT));
   }
 
-  @Test
-  public void testLeastRestrictiveWithNumbers() {
-    SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
-    final RelDataType sqlBigInt = typeFactory.createSqlType(SqlTypeName.BIGINT);
-    final RelDataType sqlInt = typeFactory.createSqlType(SqlTypeName.INTEGER);
+  @Test public void testLeastRestrictiveWithNullability() {
+    Fixture f = new Fixture();
+    RelDataType leastRestrictive =
+        f.typeFactory.leastRestrictive(Lists.newArrayList(f.sqlVarcharNullable, f.sqlAny));
+    assertThat(leastRestrictive.getSqlTypeName(), is(SqlTypeName.ANY));
+    assertThat(leastRestrictive.isNullable(), is(true));
+  }
 
+  @Test public void testLeastRestrictiveWithNull() {
+    Fixture f = new Fixture();
     RelDataType leastRestrictive =
-            typeFactory.leastRestrictive(Lists.newArrayList(sqlBigInt, sqlInt));
-    assertEquals(leastRestrictive.getSqlTypeName(), SqlTypeName.BIGINT);
+        f.typeFactory.leastRestrictive(Lists.newArrayList(f.sqlNull, f.sqlNull));
+    assertThat(leastRestrictive.getSqlTypeName(), is(SqlTypeName.NULL));
+    assertThat(leastRestrictive.isNullable(), is(true));
   }
+
+  /** Sets up data needed by a test. */
+  private static class Fixture {
+    SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+    final RelDataType sqlBigInt = typeFactory.createTypeWithNullability(
+        typeFactory.createSqlType(SqlTypeName.BIGINT), false);
+    final RelDataType sqlInt = typeFactory.createTypeWithNullability(
+        typeFactory.createSqlType(SqlTypeName.INTEGER), false);
+    final RelDataType sqlVarcharNullable = typeFactory.createTypeWithNullability(
+        typeFactory.createSqlType(SqlTypeName.VARCHAR), true);
+    final RelDataType sqlNull = typeFactory.createTypeWithNullability(
+        typeFactory.createSqlType(SqlTypeName.NULL), false);
+    final RelDataType sqlAny = typeFactory.createTypeWithNullability(
+        typeFactory.createSqlType(SqlTypeName.ANY), false);
+  }
+
 }
+
 // End SqlTypeFactoryTest.java