Skip to content

Commit

Permalink
Always check for aliases when deriving data types (apache#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjbanghart authored and olivrlee committed Aug 18, 2023
1 parent 5344035 commit e03b950
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/java/org/apache/calcite/test/UdtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
* <a href="https://issues.apache.org/jira/browse/CALCITE-3045">[CALCITE-3045]
* NullPointerException when casting null literal to composite user defined type</a>. */
Expand Down

0 comments on commit e03b950

Please sign in to comment.