Repository: calcite Updated Branches: refs/heads/master ca48431ee -> 7f46cd5f0
Clean up following [CALCITE-1684] Remove CAST calls that were erroneously introduced. Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/7f46cd5f Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/7f46cd5f Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/7f46cd5f Branch: refs/heads/master Commit: 7f46cd5f070b9f4f4855f7f90cf23f23ab03da8e Parents: ca48431 Author: Julian Hyde <[email protected]> Authored: Thu Mar 16 16:24:13 2017 -0700 Committer: Julian Hyde <[email protected]> Committed: Thu Mar 16 16:24:13 2017 -0700 ---------------------------------------------------------------------- .../adapter/enumerable/RexToLixTranslator.java | 12 +++++----- .../java/org/apache/calcite/rex/RexBuilder.java | 8 +++---- .../calcite/sql/type/SqlTypeFactoryImpl.java | 11 +++------- .../apache/calcite/sql/type/SqlTypeUtil.java | 23 ++++++++++++++++++++ .../calcite/sql/type/SqlTypeFactoryTest.java | 23 ++++++++++++++++++++ .../org/apache/calcite/test/DruidAdapterIT.java | 4 ++-- .../apache/calcite/adapter/pig/PigFilter.java | 15 ++----------- .../org/apache/calcite/test/PigAdapterTest.java | 2 +- 8 files changed, 63 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/7f46cd5f/core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java index ef7f120..7d5ef75 100644 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java +++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java @@ -45,6 +45,7 @@ import org.apache.calcite.runtime.SqlFunctions; import org.apache.calcite.sql.SqlIntervalQualifier; import org.apache.calcite.sql.SqlKind; import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.type.SqlTypeUtil; import org.apache.calcite.util.BuiltInMethod; import org.apache.calcite.util.ControlFlowException; import org.apache.calcite.util.NlsString; @@ -385,16 +386,13 @@ public class RexToLixTranslator { case VARBINARY: // If this is a widening cast, no need to truncate. final int sourcePrecision = sourceType.getPrecision(); - if (sourcePrecision >= 0 - && (sourcePrecision <= targetPrecision - || targetPrecision - == RelDataType.PRECISION_NOT_SPECIFIED)) { + if (SqlTypeUtil.comparePrecision(sourcePrecision, targetPrecision) + <= 0) { truncate = false; } // If this is a widening cast, no need to pad. - if (sourcePrecision == RelDataType.PRECISION_NOT_SPECIFIED - || sourcePrecision >= 0 - && sourcePrecision >= targetPrecision + if (SqlTypeUtil.comparePrecision(sourcePrecision, targetPrecision) + >= 0 && targetPrecision != RelDataType.PRECISION_NOT_SPECIFIED) { pad = false; } http://git-wip-us.apache.org/repos/asf/calcite/blob/7f46cd5f/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 2a4667c..eefb2d8 100644 --- a/core/src/main/java/org/apache/calcite/rex/RexBuilder.java +++ b/core/src/main/java/org/apache/calcite/rex/RexBuilder.java @@ -595,9 +595,9 @@ public class RexBuilder { final int length = ((NlsString) value).getValue().length(); switch (toType.getSqlTypeName()) { case CHAR: - return toType.getPrecision() == length; + return SqlTypeUtil.comparePrecision(toType.getPrecision(), length) == 0; case VARCHAR: - return toType.getPrecision() >= length; + return SqlTypeUtil.comparePrecision(toType.getPrecision(), length) >= 0; default: throw new AssertionError(toType); } @@ -606,9 +606,9 @@ public class RexBuilder { final int length = ((ByteString) value).length(); switch (toType.getSqlTypeName()) { case BINARY: - return toType.getPrecision() == length; + return SqlTypeUtil.comparePrecision(toType.getPrecision(), length) == 0; case VARBINARY: - return toType.getPrecision() >= length; + return SqlTypeUtil.comparePrecision(toType.getPrecision(), length) >= 0; default: throw new AssertionError(toType); } http://git-wip-us.apache.org/repos/asf/calcite/blob/7f46cd5f/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 d2532f0..bbe0801 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 @@ -290,8 +290,9 @@ public class SqlTypeFactoryImpl extends RelDataTypeFactoryImpl { SqlCollation collation2 = resultType.getCollation(); // TODO: refine collation combination rules - final int precision = maxPrecision(resultType.getPrecision(), - type.getPrecision()); + final int precision = + SqlTypeUtil.maxPrecision(resultType.getPrecision(), + type.getPrecision()); // If either type is LOB, then result is LOB with no precision. // Otherwise, if either is variable width, result is variable @@ -484,12 +485,6 @@ public class SqlTypeFactoryImpl extends RelDataTypeFactoryImpl { return resultType; } - /** Returns the larger of two precisions, treating - * {@link RelDataType#PRECISION_NOT_SPECIFIED} as infinity. */ - private int maxPrecision(int p0, int p1) { - return (p0 == RelDataType.PRECISION_NOT_SPECIFIED || p0 >= p1) ? p0 : p1; - } - /** * Controls behavior discussed <a * href="http://sf.net/mailarchive/message.php?msg_id=13337379">here</a>. http://git-wip-us.apache.org/repos/asf/calcite/blob/7f46cd5f/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java index e2162e1..093a8e2 100644 --- a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java +++ b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java @@ -1285,6 +1285,29 @@ public abstract class SqlTypeUtil { } return charset.name().startsWith("UTF"); } + + /** Returns the larger of two precisions, treating + * {@link RelDataType#PRECISION_NOT_SPECIFIED} as infinity. */ + public static int maxPrecision(int p0, int p1) { + return (p0 == RelDataType.PRECISION_NOT_SPECIFIED + || p0 >= p1 + && p1 != RelDataType.PRECISION_NOT_SPECIFIED) ? p0 : p1; + } + + /** Returns whether a precision is greater or equal than another, + * treating {@link RelDataType#PRECISION_NOT_SPECIFIED} as infinity. */ + public static int comparePrecision(int p0, int p1) { + if (p0 == p1) { + return 0; + } + if (p0 == RelDataType.PRECISION_NOT_SPECIFIED) { + return 1; + } + if (p1 == RelDataType.PRECISION_NOT_SPECIFIED) { + return -1; + } + return Integer.compare(p0, p1); + } } // End SqlTypeUtil.java http://git-wip-us.apache.org/repos/asf/calcite/blob/7f46cd5f/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 4ca75fa..2b1a164 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 @@ -61,6 +61,29 @@ public class SqlTypeFactoryTest { assertThat(leastRestrictive.isNullable(), is(true)); } + /** Unit test for {@link SqlTypeUtil#comparePrecision(int, int)} + * and {@link SqlTypeUtil#maxPrecision(int, int)}. */ + @Test public void testMaxPrecision() { + final int un = RelDataType.PRECISION_NOT_SPECIFIED; + checkPrecision(1, 1, 1, 0); + checkPrecision(2, 1, 2, 1); + checkPrecision(2, 100, 100, -1); + checkPrecision(2, un, un, -1); + checkPrecision(un, 2, un, 1); + checkPrecision(un, un, un, 0); + } + + private void checkPrecision(int p0, int p1, int expectedMax, + int expectedComparison) { + assertThat(SqlTypeUtil.maxPrecision(p0, p1), is(expectedMax)); + assertThat(SqlTypeUtil.maxPrecision(p1, p0), is(expectedMax)); + assertThat(SqlTypeUtil.maxPrecision(p0, p0), is(p0)); + assertThat(SqlTypeUtil.maxPrecision(p1, p1), is(p1)); + assertThat(SqlTypeUtil.comparePrecision(p0, p1), is(expectedComparison)); + assertThat(SqlTypeUtil.comparePrecision(p0, p0), is(0)); + assertThat(SqlTypeUtil.comparePrecision(p1, p1), is(0)); + } + /** Sets up data needed by a test. */ private static class Fixture { SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT); http://git-wip-us.apache.org/repos/asf/calcite/blob/7f46cd5f/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java ---------------------------------------------------------------------- diff --git a/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java b/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java index 099d0c9..bcce57e 100644 --- a/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java +++ b/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java @@ -1259,10 +1259,10 @@ public class DruidAdapterIT { + "'intervals':['1900-01-09T00:00:00.000/2992-01-10T00:00:00.000']}"; final String explain = "PLAN=EnumerableInterpreter\n" + " DruidQuery(table=[[foodmart, foodmart]], intervals=[[1900-01-09T00:00:00.000/2992-01-10T00:00:00.000]]," - + " filter=[AND(=(CAST($3):VARCHAR(24) CHARACTER SET \"ISO-8859-1\" COLLATE \"ISO-8859-1$en_US$primary\", 'High Top Dried Mushrooms')," + + " filter=[AND(=($3, 'High Top Dried Mushrooms')," + " OR(=($87, 'Q2')," + " =($87, 'Q3'))," - + " =(CAST($30):VARCHAR(2) CHARACTER SET \"ISO-8859-1\" COLLATE \"ISO-8859-1$en_US$primary\", 'WA'))]," + + " =($30, 'WA'))]," + " projects=[[$30, $29, $3]], groups=[{0, 1, 2}], aggs=[[]])\n"; sql(sql) .queryContains(druidChecker(druidQuery)) http://git-wip-us.apache.org/repos/asf/calcite/blob/7f46cd5f/pig/src/main/java/org/apache/calcite/adapter/pig/PigFilter.java ---------------------------------------------------------------------- diff --git a/pig/src/main/java/org/apache/calcite/adapter/pig/PigFilter.java b/pig/src/main/java/org/apache/calcite/adapter/pig/PigFilter.java index 339f761..63cc1ea 100644 --- a/pig/src/main/java/org/apache/calcite/adapter/pig/PigFilter.java +++ b/pig/src/main/java/org/apache/calcite/adapter/pig/PigFilter.java @@ -101,8 +101,8 @@ public class PigFilter extends Filter implements PigRel { private String getSingleFilterCondition(Implementor implementor, String op, RexCall call) { final String fieldName; final String literal; - final RexNode left = strip(call.operands.get(0)); - final RexNode right = strip(call.operands.get(1)); + final RexNode left = call.operands.get(0); + final RexNode right = call.operands.get(1); if (left.getKind() == LITERAL) { if (right.getKind() != INPUT_REF) { throw new IllegalArgumentException( @@ -127,17 +127,6 @@ public class PigFilter extends Filter implements PigRel { return '(' + fieldName + ' ' + op + ' ' + literal + ')'; } - private RexNode strip(RexNode e) { - switch (e.getKind()) { - case CAST: - final RexNode e2 = ((RexCall) e).operands.get(0); - if (e2.getKind() == LITERAL) { - return e2; - } - } - return e; - } - private boolean containsOnlyConjunctions(RexNode condition) { return RelOptUtil.disjunctions(condition).size() == 1; } http://git-wip-us.apache.org/repos/asf/calcite/blob/7f46cd5f/pig/src/test/java/org/apache/calcite/test/PigAdapterTest.java ---------------------------------------------------------------------- diff --git a/pig/src/test/java/org/apache/calcite/test/PigAdapterTest.java b/pig/src/test/java/org/apache/calcite/test/PigAdapterTest.java index 084f1cb..b1b6b31 100644 --- a/pig/src/test/java/org/apache/calcite/test/PigAdapterTest.java +++ b/pig/src/test/java/org/apache/calcite/test/PigAdapterTest.java @@ -58,7 +58,7 @@ public class PigAdapterTest extends AbstractPigTest { .query("select * from \"t\" where \"tc0\" > 'abc' and \"tc1\" = '3'") .explainContains( "PigToEnumerableConverter\n" - + " PigFilter(condition=[AND(>($0, 'abc'), =($1, CAST('3'):VARCHAR CHARACTER SET \"ISO-8859-1\" COLLATE \"ISO-8859-1$en_US$primary\" NOT NULL))])\n" + + " PigFilter(condition=[AND(>($0, 'abc'), =($1, '3'))])\n" + " PigTableScan(table=[[PIG, t]])") .runs() .queryContains(
