Skip to content

Commit

Permalink
[CALCITE-6343] Don't strip MEASUREness from measures when typing AS a…
Browse files Browse the repository at this point in the history
…lias operator

When inferring the return type of an operator application, don't unwrap
MEASURE types if the operator is an AS alias.
  • Loading branch information
barrkel committed Mar 29, 2024
1 parent d5b6b5c commit b41526d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/src/main/java/org/apache/calcite/sql/SqlOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,10 @@ public RelDataType inferReturnType(
}

// MEASURE wrapper should be removed, e.g. MEASURE<DOUBLE> should just be DOUBLE
if (isMeasure(returnType) && returnType.getMeasureElementType() != null) {
returnType = Objects.requireNonNull(returnType.getMeasureElementType());
if (opBinding.getOperator().kind != SqlKind.AS
&& isMeasure(returnType)
&& returnType.getMeasureElementType() != null) {
returnType = returnType.getMeasureElementType();
}

if (operandTypeInference != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ static class Fixture extends SqlTypeFixture {
assertThat(dataType, is(innerType));
}

/** 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 @@ -4756,6 +4756,22 @@ void checkUserDefinedOrderByOver(NullCollation nullCollation) {
.ok();
}

/** Regression test for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6343">[CALCITE-6343]
* AS alias operator stripping MEASUREness from 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>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4572,6 +4572,18 @@ group by deptno]]>
LogicalAggregate(group=[{0}], C=[AGGREGATE($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=[$9])
LogicalTableScan(table=[[CATALOG, SALES, EMPM]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit b41526d

Please sign in to comment.