Skip to content

Commit

Permalink
[CALCITE-5949] RexExecutable should return unchanged original express…
Browse files Browse the repository at this point in the history
…ions when it fails

Co-authored-by: arkanovicz <claude@renegat.net>
  • Loading branch information
rubenada and arkanovicz committed Oct 27, 2023
1 parent 0f824ed commit 417a1b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/src/main/java/org/apache/calcite/rex/RexExecutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -87,11 +88,14 @@ public void reduce(RexBuilder rexBuilder, List<RexNode> constExps,
values = new Object[constExps.size()];
} else {
assert values.length == constExps.size();
final List<RexNode> successfullyReduced = new ArrayList<>(constExps.size());
final List<@Nullable Object> valueList = Arrays.asList(values);
for (Pair<RexNode, @Nullable Object> value : Pair.zip(constExps, valueList)) {
reducedValues.add(
successfullyReduced.add(
rexBuilder.makeLiteral(value.right, value.left.getType(), true));
}
assert successfullyReduced.size() == constExps.size();
reducedValues.addAll(successfullyReduced);
}
} catch (RuntimeException e) {
// One or more of the expressions failed.
Expand Down
24 changes: 24 additions & 0 deletions core/src/test/java/org/apache/calcite/rex/RexExecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,28 @@ public void run() {
interface Action {
void check(RexBuilder rexBuilder, RexExecutorImpl executor);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-5949">[CALCITE-5949]
* RexExecutable should return unchanged original expressions when it fails</a>.
*/
@Test void testInvalidExpressionInList() {
check((rexBuilder, executor) -> {
final List<RexNode> reducedValues = new ArrayList<>();
final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
final RelDataType integer =
typeFactory.createSqlType(SqlTypeName.INTEGER);
final RexCall first =
(RexCall) rexBuilder.makeCall(SqlStdOperatorTable.LN,
rexBuilder.makeLiteral(3, integer, true));
final RexCall second =
(RexCall) rexBuilder.makeCall(SqlStdOperatorTable.LN,
rexBuilder.makeLiteral(-2, integer, true));
executor.reduce(rexBuilder, ImmutableList.of(first, second),
reducedValues);
assertThat(reducedValues, hasSize(2));
assertThat(reducedValues.get(0), instanceOf(RexCall.class));
assertThat(reducedValues.get(1), instanceOf(RexCall.class));
});
}
}

0 comments on commit 417a1b5

Please sign in to comment.