From e03b95002d64176fff52cd2bc59da414fe7f1255 Mon Sep 17 00:00:00 2001 From: TJ Banghart <66929211+tjbanghart@users.noreply.github.com> Date: Sun, 5 Feb 2023 22:36:53 -0800 Subject: [PATCH] Always check for aliases when deriving data types (#13) --- .../java/org/apache/calcite/sql/SqlDataTypeSpec.java | 6 +++++- .../test/java/org/apache/calcite/test/UdtTest.java | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/calcite/sql/SqlDataTypeSpec.java b/core/src/main/java/org/apache/calcite/sql/SqlDataTypeSpec.java index 3433113119fe..50a8f82cd4fb 100644 --- a/core/src/main/java/org/apache/calcite/sql/SqlDataTypeSpec.java +++ b/core/src/main/java/org/apache/calcite/sql/SqlDataTypeSpec.java @@ -230,7 +230,11 @@ public RelDataType deriveType(SqlValidator validator) { */ public RelDataType deriveType(SqlValidator validator, boolean nullable) { RelDataType type; - type = typeNameSpec.deriveType(validator); + // check if the type name is being used as an alias before calling `deriveType` + type = validator.getCatalogReader().getNamedType(typeNameSpec.getTypeName()); + if (type == null) { + type = typeNameSpec.deriveType(validator); + } // Fix-up the nullability, default is false. final RelDataTypeFactory typeFactory = validator.getTypeFactory(); diff --git a/core/src/test/java/org/apache/calcite/test/UdtTest.java b/core/src/test/java/org/apache/calcite/test/UdtTest.java index 970a40a0b785..e9930a3c68fb 100644 --- a/core/src/test/java/org/apache/calcite/test/UdtTest.java +++ b/core/src/test/java/org/apache/calcite/test/UdtTest.java @@ -29,6 +29,10 @@ private CalciteAssert.AssertThat withUdt() { + " {\n" + " name: 'foo',\n" + " type: 'BIGINT'\n" + + " },\n" + + " {\n" + + " name: 'TIMESTAMP',\n" + + " type: 'TIMESTAMP_WITH_LOCAL_TIME_ZONE'\n" + " }" + " ],\n" + " schemas: [\n" @@ -71,6 +75,13 @@ private CalciteAssert.AssertThat withUdt() { withUdt().query(sql).returns("LD=1\n"); } + @Test void testAliasOnBasicType() { + final String sql = "select CAST(\"date\" AS TIMESTAMP) as ts " + + "from (VALUES ROW(DATE '2020-12-25', 'SameName')) AS \"t\" (\"date\", \"desc\")"; + withUdt().query(sql).typeIs("[TS TIMESTAMP_WITH_LOCAL_TIME_ZONE NOT NULL]"); + } + + /** Test case for * [CALCITE-3045] * NullPointerException when casting null literal to composite user defined type. */