Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-2506] Fix AssertionError in RexSimplify: coalesce(unaryPlus(… #836

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/calcite/rex/RexInterpreter.java
Expand Up @@ -185,6 +185,8 @@ public Comparable visitCall(RexCall call) {
return ceil(call, values);
case EXTRACT:
return extract(call, values);
case PLUS_PREFIX:
return containsNull(values) ? N : values.get(0);
default:
throw unbound(call);
}
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/org/apache/calcite/rex/RexSimplify.java
Expand Up @@ -207,6 +207,8 @@ private RexNode simplify_(RexNode e) {
case LESS_THAN_OR_EQUAL:
case NOT_EQUALS:
return simplifyComparison((RexCall) e);
case PLUS_PREFIX:
return simplify_(((RexCall) e).getOperands().get(0));
default:
return e;
}
Expand Down Expand Up @@ -562,7 +564,9 @@ private RexNode simplifyCoalesce(RexCall call) {
final List<RexNode> operands = new ArrayList<>();
for (RexNode operand : call.getOperands()) {
operand = simplify_(operand);
if (digests.add(operand.digest)) {
// Discard the first few NULL operands
if ((!operands.isEmpty() || !RexUtil.isNull(operand))
&& digests.add(operand.digest)) {
operands.add(operand);
}
if (!operand.getType().isNullable()) {
Expand Down
Expand Up @@ -468,7 +468,7 @@ protected RexNode vBoolNotNull(int arg) {
/**
* Creates {@code nullable int variable} with index of 0.
* If you need several distinct variables, use {@link #vInt(int)}.
* The resulting node would look like {@code ?0.notNullInt0}
* The resulting node would look like {@code ?0.int0}
*
* @return nullable int variable with index of 0
*/
Expand Down
39 changes: 32 additions & 7 deletions core/src/test/java/org/apache/calcite/test/RexProgramTest.java
Expand Up @@ -56,7 +56,6 @@
import com.google.common.collect.Multimap;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.math.BigDecimal;
Expand Down Expand Up @@ -2090,14 +2089,40 @@ private void assertTypeAndToString(
"IS DISTINCT FROM(?0.bool0, ?0.bool1)");
}

@Ignore("[CALCITE-2505] java.lang.AssertionError: result mismatch")
@Test public void coalescePlusNull() {
// when applied to {?0.int0=-1},
// COALESCE(+(null), +(?0.int0)) yielded -1,
// and +(null) yielded NULL
@Test public void coalesceUnaryPlus() {
checkSimplify2(
coalesce(unaryPlus(nullInt), unaryPlus(vInt())),
"...", "...");
"?0.int0", "?0.int0");

checkSimplify2(
coalesce(unaryPlus(nullInt), unaryPlus(nullInt)),
"null", "null");

checkSimplify2(
coalesce(unaryPlus(nullInt), unaryPlus(nullInt), unaryPlus(vInt())),
"?0.int0", "?0.int0");

checkSimplify2(
coalesce(unaryPlus(nullInt), unaryPlus(nullInt),
unaryPlus(vInt()), unaryPlus(vInt(1))),
"COALESCE(?0.int0, ?0.int1)", "COALESCE(?0.int0, ?0.int1)");

checkSimplify2(
coalesce(unaryPlus(nullInt), unaryPlus(nullInt),
unaryPlus(vIntNotNull()), unaryPlus(vInt(1))),
"?0.notNullInt0", "?0.notNullInt0");

checkSimplify2(
coalesce(unaryPlus(vInt(0)), unaryPlus(vInt(1))),
"COALESCE(?0.int0, ?0.int1)", "COALESCE(?0.int0, ?0.int1)");

checkSimplify2(
coalesce(unaryPlus(vInt(0)), unaryPlus(nullInt), unaryPlus(vInt(1))),
"COALESCE(?0.int0, null, ?0.int1)", "COALESCE(?0.int0, null, ?0.int1)");

checkSimplify2(
coalesce(unaryPlus(vIntNotNull()), unaryPlus(nullInt), unaryPlus(vInt(1))),
"?0.notNullInt0", "?0.notNullInt0");
}

/** Converts a map to a string, sorting on the string representation of its
Expand Down