This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git

commit 36b8a99d89600866c395b05144cc6fce560f8665
Author: Barry Kelly <[email protected]>
AuthorDate: Tue Mar 26 20:25:22 2024 +0000

    [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
---
 .../calcite/sql/type/RelDataTypeSystemTest.java    | 11 +++++++++
 .../apache/calcite/test/SqlToRelConverterTest.java | 28 +++++++++++++++-------
 .../apache/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 6332f69dcf..5d19ae0f6a 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 @@ class RelDataTypeSystemTest {
     assertThat(dataType, is(innerType));
   }
 
+  /** <a 
href="https://issues.apache.org/jira/browse/CALCITE-6343";>[CALCITE-6343]</a>
+   * 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 47efce21c6..7e4819aee4 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 @@ class SqlToRelConverterTest extends SqlToRelTestBase {
     sql(sql).ok();
   }
 
-  /** Test case for:
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6343";>[CALCITE-6343]
+   * Ensure that AS operator doesn't change return type of measures</a>. */
+  @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
    * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6013";>[CALCITE-6013]
-   * Unnecessary measures added as projects during rel construction</a>.
-   */
+   * Unnecessary measures added as projects during rel construction</a>. */
   @Test void testAvoidUnnecessaryMeasureProject() {
     final String sql = "select deptno\n"
         + "from empm\n"
@@ -4880,11 +4894,10 @@ class SqlToRelConverterTest extends SqlToRelTestBase {
         .ok();
   }
 
-  /** Test case for:
+  /** Test case for
    * <a 
href="https://issues.apache.org/jira/browse/CALCITE-3310";>[CALCITE-3310]
    * Approximate and exact aggregate calls are recognized as the same
-   * during sql-to-rel conversion</a>.
-   */
+   * during sql-to-rel conversion</a>. */
   @Test void testProjectApproximateAndExactAggregates() {
     final String sql = "SELECT empno, count(distinct ename),\n"
             + "approx_count_distinct(ename)\n"
@@ -4908,8 +4921,7 @@ class SqlToRelConverterTest extends SqlToRelTestBase {
   /** Test case for
    * <a 
href="https://issues.apache.org/jira/browse/CALCITE-3456";>[CALCITE-3456]
    * AssertionError throws when aggregation same digest in sub-query in same
-   * scope</a>.
-   */
+   * scope</a>. */
   @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 5d6f6f2f0f..4946f20873 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]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testMeasureRefWithAlias">
+    <Resource name="sql">
+      <![CDATA[select count_plus_100 as c
+from empm]]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[
+LogicalProject(C=[M2V($9)])
+  LogicalTableScan(table=[[CATALOG, SALES, EMPM]])
 ]]>
     </Resource>
   </TestCase>

Reply via email to