Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions core/src/main/java/org/apache/calcite/rel/rules/PushProjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,22 @@
|| (childRel instanceof SetOp)) {
// if nothing is projected from the children, arbitrarily project
// the first columns; this is necessary since Fennel doesn't
// handle 0-column projections
if (nProject == 0 && childPreserveExprs.isEmpty()) {
// handle 0-column projections.
//
// An input may legitimately have a zero-column row type: for
// example, a Values with an empty row type and a single empty
// tuple, which returns one row with zero columns and is the
// identity for cross join. There is no first column to fall back
// on in that case, so skip the workaround rather than set a bit
// that points past the input's fields; createProjectRefsAndExprs
// would use it to index into an empty field list.
if (nProject == 0 && childPreserveExprs.isEmpty() && nFields > 0) {
projRefs.set(0);
nProject = 1;
}
if (childRel instanceof Join) {
if (nRightProject == 0 && rightPreserveExprs.isEmpty()) {
if (nRightProject == 0 && rightPreserveExprs.isEmpty()

Check warning on line 474 in core/src/main/java/org/apache/calcite/rel/rules/PushProjector.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Merge this if statement with the enclosing one.

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AZ-izjC4lUCwE_icmhOx&open=AZ-izjC4lUCwE_icmhOx&pullRequest=5125
&& nFieldsRight > 0) {
projRefs.set(nFields);
nRightProject = 1;
}
Expand Down
50 changes: 50 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 @@ -1559,6 +1559,56 @@ private void checkSemiOrAntiJoinProjectTranspose(JoinRelType type) {
checkJoinProjectTransposeDoesNotMatch(JoinRelType.LEFT_MARK);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7487">[CALCITE-7487]
* ProjectJoinTransposeRule throws ArrayIndexOutOfBoundsException in
* PushProjector when a Join input has a zero-column row type</a>. */
@Test void testProjectJoinTransposeWithZeroColumnRightInput() {
relFn(b -> zeroColumnJoinInputRelFn(b, false))
.withRule(CoreRules.PROJECT_JOIN_TRANSPOSE).check();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7487">[CALCITE-7487]
* ProjectJoinTransposeRule throws ArrayIndexOutOfBoundsException in
* PushProjector when a Join input has a zero-column row type</a>. */
@Test void testProjectJoinTransposeWithZeroColumnLeftInput() {
relFn(b -> zeroColumnJoinInputRelFn(b, true))
.withRule(CoreRules.PROJECT_JOIN_TRANSPOSE).check();
}

/** Builds {@code Project(CAST(col1))} over a cross join in which one input is
* DEE -- a {@link org.apache.calcite.rel.core.Values} with an empty row type,
* the identity for cross join. The project must be non-identity, otherwise
* {@link RelBuilder} collapses it away and the rule never fires. */
private static RelNode zeroColumnJoinInputRelFn(RelBuilder b,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be reproduced with a quidem test?
Not all plans that the builder can build can surface from a real program.

boolean deeOnLeft) {
final RelDataTypeFactory typeFactory = b.getTypeFactory();
final RelDataType bigintType =
typeFactory.createSqlType(SqlTypeName.BIGINT);
final RelNode nonEmpty = b
.values(
ImmutableList.of(
ImmutableList.of(
(RexLiteral) b.getRexBuilder().makeZeroLiteral(bigintType))),
typeFactory.builder().add("col1", bigintType).build())
.build();
final RelNode dee = b
.values(ImmutableList.of(ImmutableList.of()),
typeFactory.builder().build())
.build();
final RelDataType varcharType =
typeFactory.createSqlType(SqlTypeName.VARCHAR);
return b
.push(deeOnLeft ? dee : nonEmpty)
.push(deeOnLeft ? nonEmpty : dee)
.join(JoinRelType.INNER, b.literal(true))
// DEE contributes no fields, so the sole column is at index 0
// whichever side it is on.
.project(b.getRexBuilder().makeCast(varcharType, b.field(0)))
.build();
}

/** A SEMI, ANTI or LEFT_MARK join does not project its right input, so
* {@link JoinProjectTransposeRule} must not pull projects above it. */
private void checkJoinProjectTransposeDoesNotMatch(JoinRelType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12359,6 +12359,44 @@ LogicalProject(DEPTNO=[$0])
LogicalAggregate(group=[{}], DUMMY=[COUNT()])
LogicalProject(EMPNO=[$0])
LogicalTableScan(table=[[scott, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testProjectJoinTransposeWithZeroColumnLeftInput">
<Resource name="planBefore">
<![CDATA[
LogicalProject(col1=[CAST($0):VARCHAR NOT NULL])
LogicalJoin(condition=[true], joinType=[inner])
LogicalValues(tuples=[[{ }]])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalJoin(condition=[true], joinType=[inner])
LogicalProject
LogicalValues(tuples=[[{ }]])
LogicalProject(col1=[CAST($0):VARCHAR NOT NULL])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
</TestCase>
<TestCase name="testProjectJoinTransposeWithZeroColumnRightInput">
<Resource name="planBefore">
<![CDATA[
LogicalProject(col1=[CAST($0):VARCHAR NOT NULL])
LogicalJoin(condition=[true], joinType=[inner])
LogicalValues(tuples=[[{ 0 }]])
LogicalValues(tuples=[[{ }]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalJoin(condition=[true], joinType=[inner])
LogicalProject(col1=[CAST($0):VARCHAR NOT NULL])
LogicalValues(tuples=[[{ 0 }]])
LogicalProject
LogicalValues(tuples=[[{ }]])
]]>
</Resource>
</TestCase>
Expand Down
Loading