Skip to content

Commit

Permalink
[CALCITE-6343] Ensure that AS operator doesn't change return type of …
Browse files Browse the repository at this point in the history
…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 #3741
  • Loading branch information
barrkel authored and julianhyde committed Sep 17, 2024
1 parent 682c285 commit 36b8a99
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ final class CustomTypeSystem extends RelDataTypeSystemImpl {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4863,10 +4863,24 @@ void checkUserDefinedOrderByOver(NullCollation nullCollation) {
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"
Expand All @@ -4880,11 +4894,10 @@ void checkUserDefinedOrderByOver(NullCollation nullCollation) {
.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"
Expand All @@ -4908,8 +4921,7 @@ void checkUserDefinedOrderByOver(NullCollation nullCollation) {
/** 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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 36b8a99

Please sign in to comment.