Skip to content

Commit

Permalink
[CALCITE-6353] Optimization CoreRules.PROJECT_REDUCE_EXPRESSIONS cras…
Browse files Browse the repository at this point in the history
…hes while optimizing ARRAY_CONCAT expression

Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
mihaibudiu committed Apr 5, 2024
1 parent 4c69588 commit 2f5d54e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3349,7 +3349,7 @@ private static class SortArrayImplementor extends AbstractRexCallImplementor {
}
}

/** Implementor for a array concat. */
/** Implementor for array concat. */
private static class ArrayConcatImplementor extends AbstractRexCallImplementor {
ArrayConcatImplementor() {
super("array_concat", NullPolicy.STRICT, false);
Expand All @@ -3364,10 +3364,11 @@ private static class ArrayConcatImplementor extends AbstractRexCallImplementor {
for (Expression expression : argValueList) {
blockBuilder.add(
Expressions.ifThenElse(
Expressions.or(
Expressions.orElse(
Expressions.equal(nullValue, list),
Expressions.equal(nullValue, expression)),
Expressions.assign(list, nullValue),
Expressions.statement(
Expressions.assign(list, nullValue)),
Expressions.statement(
Expressions.call(list,
BuiltInMethod.COLLECTION_ADDALL.method, expression))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,13 @@ private void addResult(RexNode exp) {
if (operand instanceof RexLiteral) {
return;
}
if (operand instanceof RexCall) {
RexCall opCall = (RexCall) operand;
if (opCall.getKind() == SqlKind.ARRAY_VALUE_CONSTRUCTOR) {
// We can't simplify casts of arrays even if arrays are constant.
return;
}
}
}
constExprs.add(exp);

Expand Down
14 changes: 14 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,20 @@ private void checkSemiOrAntiJoinProjectTranspose(JoinRelType type) {
.check();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6353">[CALCITE-6353]
* Optimization CoreRules.PROJECT_REDUCE_EXPRESSIONS crashes
* while optimizing ARRAY_CONCAT expression</a>. */
@Test void testArrayConcat() {
final String sql = "select array_concat(ARRAY [1, 2], ARRAY [3, 4])";
sql(sql).withFactory(
t -> t.withOperatorTable(
opTab -> SqlLibraryOperatorTableFactory.INSTANCE.getOperatorTable(
SqlLibrary.STANDARD, SqlLibrary.BIG_QUERY)))
.withRule(CoreRules.PROJECT_REDUCE_EXPRESSIONS)
.check();
}

@Test void testDistinctCountGroupingSets2() {
final String sql = "select deptno, job, count(distinct ename), sum(sal)\n"
+ "from sales.emp group by rollup(deptno,job)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,23 @@ LogicalProject(DEPTNO=[$0], EXPR$1=[OR(AND(IS NOT NULL($5), <>($2, 0)), AND(<($3
LogicalAggregate(group=[{0}], i=[LITERAL_AGG(true)])
LogicalProject(MGR=[$3])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testArrayConcat">
<Resource name="sql">
<![CDATA[select array_concat(ARRAY [1, 2], ARRAY [3, 4])]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalProject(EXPR$0=[ARRAY_CONCAT(ARRAY(1, 2), ARRAY(3, 4))])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalProject(EXPR$0=[CAST(ARRAY(1, 2, 3, 4)):INTEGER NOT NULL ARRAY NOT NULL])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit 2f5d54e

Please sign in to comment.