From 36b8a99d89600866c395b05144cc6fce560f8665 Mon Sep 17 00:00:00 2001 From: Barry Kelly Date: Tue, 26 Mar 2024 20:25:22 +0000 Subject: [PATCH] [CALCITE-6343] Ensure that AS operator doesn't change return type of measures Previously, adding a column alias (AS) would make a measure into a non-measure, because of the type inference rule used by the AS operator. Since the PR was originally created, this issue has already been fixed in the rework of [CALCITE-5689], but this commit adds some tests. Close apache/calcite#3741 --- .../sql/type/RelDataTypeSystemTest.java | 11 ++++++++ .../calcite/test/SqlToRelConverterTest.java | 28 +++++++++++++------ .../calcite/test/SqlToRelConverterTest.xml | 12 ++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/core/src/test/java/org/apache/calcite/sql/type/RelDataTypeSystemTest.java b/core/src/test/java/org/apache/calcite/sql/type/RelDataTypeSystemTest.java index 6332f69dcfb8..5d19ae0f6ac8 100644 --- a/core/src/test/java/org/apache/calcite/sql/type/RelDataTypeSystemTest.java +++ b/core/src/test/java/org/apache/calcite/sql/type/RelDataTypeSystemTest.java @@ -239,6 +239,17 @@ final class CustomTypeSystem extends RelDataTypeSystemImpl { assertThat(dataType, is(innerType)); } + /** [CALCITE-6343] + * Ensure that AS operator doesn't change return type of measures. */ + @Test void testAsOperatorReturnTypeInferenceDoesNotRemoveMeasure() { + final SqlTypeFactoryImpl f = new Fixture().typeFactory; + RelDataType innerType = f.createSqlType(SqlTypeName.DOUBLE); + RelDataType measureType = f.createMeasureType(innerType); + RelDataType dataType = + SqlStdOperatorTable.AS.inferReturnType(f, Lists.newArrayList(measureType)); + assertThat(dataType, is(measureType)); + } + @Test void testCustomDecimalPlusReturnTypeInference() { final SqlTypeFactoryImpl f = new Fixture().customTypeFactory; RelDataType operand1 = f.createSqlType(SqlTypeName.DECIMAL, 38, 10); diff --git a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java index 47efce21c62d..7e4819aee4c2 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java +++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java @@ -4863,10 +4863,24 @@ void checkUserDefinedOrderByOver(NullCollation nullCollation) { sql(sql).ok(); } - /** Test case for: + /** Test case for + * [CALCITE-6343] + * Ensure that AS operator doesn't change return type of measures. */ + @Test void testMeasureRefWithAlias() { + final String sql = "select count_plus_100 as c\n" + + "from empm"; + fixture() + .withFactory(c -> + c.withOperatorTable(t -> + SqlValidatorTest.operatorTableFor(SqlLibrary.CALCITE))) + .withCatalogReader(MockCatalogReaderExtended::create) + .withSql(sql) + .ok(); + } + + /** Test case for * [CALCITE-6013] - * Unnecessary measures added as projects during rel construction. - */ + * Unnecessary measures added as projects during rel construction. */ @Test void testAvoidUnnecessaryMeasureProject() { final String sql = "select deptno\n" + "from empm\n" @@ -4880,11 +4894,10 @@ void checkUserDefinedOrderByOver(NullCollation nullCollation) { .ok(); } - /** Test case for: + /** Test case for * [CALCITE-3310] * Approximate and exact aggregate calls are recognized as the same - * during sql-to-rel conversion. - */ + * during sql-to-rel conversion. */ @Test void testProjectApproximateAndExactAggregates() { final String sql = "SELECT empno, count(distinct ename),\n" + "approx_count_distinct(ename)\n" @@ -4908,8 +4921,7 @@ void checkUserDefinedOrderByOver(NullCollation nullCollation) { /** Test case for * [CALCITE-3456] * AssertionError throws when aggregation same digest in sub-query in same - * scope. - */ + * scope. */ @Test void testAggregateWithSameDigestInSubQueries() { final String sql = "select\n" + " CASE WHEN job IN ('810000', '820000') THEN job\n" diff --git a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml index 5d6f6f2f0ff6..4946f20873b1 100644 --- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml +++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml @@ -4686,6 +4686,18 @@ group by deptno]]> LogicalAggregate(group=[{0}], C=[AGG_M2V($1)]) LogicalProject(DEPTNO=[$7], COUNT_PLUS_100=[$9]) LogicalTableScan(table=[[CATALOG, SALES, EMPM]]) +]]> + + + + + + + +