This is an automated email from the ASF dual-hosted git repository. danny0405 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/master by this push: new dfb842e [CALCITE-3924] Fix flakey test to handle TIMESTAMP and TIMESTAMP(0) correctly (neoReMinD) dfb842e is described below commit dfb842e55e1fa7037c8a731341010ed1c0cfb6f7 Author: xu <xu...@alibaba-inc.com> AuthorDate: Wed Apr 15 09:27:45 2020 +0800 [CALCITE-3924] Fix flakey test to handle TIMESTAMP and TIMESTAMP(0) correctly (neoReMinD) The whole if block should be removed. Not only the DATETIME_TYPES has this problem, all the types that allows precision could have such a problem, the root cause is that BasicSqlType#toString and digest has different handling of printing non specified precision. One way to solve is to not fire caching of types without precision and with default precision. close apache/calcite#1916 --- .../org/apache/calcite/sql/type/BasicSqlType.java | 13 ------- .../calcite/sql/type/SqlTypeFactoryTest.java | 42 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/sql/type/BasicSqlType.java b/core/src/main/java/org/apache/calcite/sql/type/BasicSqlType.java index f8323c5..bea494e 100644 --- a/core/src/main/java/org/apache/calcite/sql/type/BasicSqlType.java +++ b/core/src/main/java/org/apache/calcite/sql/type/BasicSqlType.java @@ -179,19 +179,6 @@ public class BasicSqlType extends AbstractSqlType { boolean printPrecision = precision != PRECISION_NOT_SPECIFIED; boolean printScale = scale != SCALE_NOT_SPECIFIED; - // for the digest, print the precision when defaulted, - // since (for instance) TIME is equivalent to TIME(0). - if (withDetail) { - // -1 means there is no default value for precision - if (typeName.allowsPrec() - && typeSystem.getDefaultPrecision(typeName) > -1) { - printPrecision = true; - } - if (typeName.getDefaultScale() > -1) { - printScale = true; - } - } - if (printPrecision) { sb.append('('); sb.append(getPrecision()); 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 e573413..f1c63fd 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 @@ -170,4 +170,46 @@ class SqlTypeFactoryTest { } } + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-3924">[CALCITE-3924] + * Fix flakey test to handle TIMESTAMP and TIMESTAMP(0) correctly</a>. */ + @Test void testCreateSqlTypeWithPrecision() { + SqlTypeFixture f = new SqlTypeFixture(); + checkCreateSqlTypeWithPrecision(f.typeFactory, SqlTypeName.TIME); + checkCreateSqlTypeWithPrecision(f.typeFactory, SqlTypeName.TIMESTAMP); + checkCreateSqlTypeWithPrecision(f.typeFactory, SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE); + checkCreateSqlTypeWithPrecision(f.typeFactory, SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE); + } + + private void checkCreateSqlTypeWithPrecision( + RelDataTypeFactory typeFactory, SqlTypeName sqlTypeName) { + RelDataType ts = typeFactory.createSqlType(sqlTypeName); + RelDataType tsWithoutPrecision = typeFactory.createSqlType(sqlTypeName, -1); + RelDataType tsWithPrecision0 = typeFactory.createSqlType(sqlTypeName, 0); + RelDataType tsWithPrecision1 = typeFactory.createSqlType(sqlTypeName, 1); + RelDataType tsWithPrecision2 = typeFactory.createSqlType(sqlTypeName, 2); + RelDataType tsWithPrecision3 = typeFactory.createSqlType(sqlTypeName, 3); + // for instance, 8 exceeds max precision for timestamp which is 3 + RelDataType tsWithPrecision8 = typeFactory.createSqlType(sqlTypeName, 8); + + assertThat(ts.toString(), is(sqlTypeName.getName() + "(0)")); + assertThat(ts.getFullTypeString(), is(sqlTypeName.getName() + "(0) NOT NULL")); + assertThat(tsWithoutPrecision.toString(), is(sqlTypeName.getName())); + assertThat(tsWithoutPrecision.getFullTypeString(), is(sqlTypeName.getName() + " NOT NULL")); + assertThat(tsWithPrecision0.toString(), is(sqlTypeName.getName() + "(0)")); + assertThat(tsWithPrecision0.getFullTypeString(), is(sqlTypeName.getName() + "(0) NOT NULL")); + assertThat(tsWithPrecision1.toString(), is(sqlTypeName.getName() + "(1)")); + assertThat(tsWithPrecision1.getFullTypeString(), is(sqlTypeName.getName() + "(1) NOT NULL")); + assertThat(tsWithPrecision2.toString(), is(sqlTypeName.getName() + "(2)")); + assertThat(tsWithPrecision2.getFullTypeString(), is(sqlTypeName.getName() + "(2) NOT NULL")); + assertThat(tsWithPrecision3.toString(), is(sqlTypeName.getName() + "(3)")); + assertThat(tsWithPrecision3.getFullTypeString(), is(sqlTypeName.getName() + "(3) NOT NULL")); + assertThat(tsWithPrecision8.toString(), is(sqlTypeName.getName() + "(3)")); + assertThat(tsWithPrecision8.getFullTypeString(), is(sqlTypeName.getName() + "(3) NOT NULL")); + + assertThat(ts != tsWithoutPrecision, is(true)); + assertThat(ts == tsWithPrecision0, is(true)); + assertThat(tsWithPrecision3 == tsWithPrecision8, is(true)); + } + }