Skip to content
Open
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
79 changes: 75 additions & 4 deletions core/src/main/java/org/apache/calcite/rex/RexSimplify.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -86,6 +87,8 @@ public class RexSimplify {
private final RexExecutor executor;

private static final Strong STRONG = new Strong();

private Object[] recordedArray;

/**
* Creates a RexSimplify.
Expand Down Expand Up @@ -672,9 +675,28 @@ private void simplifyAndTerms(List<RexNode> terms, RexUnknownAs unknownAs) {
RexSimplify simplify = this;
for (int i = 0; i < terms.size(); i++) {
RexNode t = terms.get(i);
if (Predicate.of(t) == null) {
Predicate predicate = Predicate.of(t);
if (predicate == null) {
continue;
}

//add for array-type
if (predicate instanceof Comparison) {
Comparison cmp = (Comparison) predicate;
if (cmp.rexCall != null && cmp.rexCall.getKind().equals(SqlKind.ARRAY_VALUE_CONSTRUCTOR)) {
RexCall arrayCall = cmp.rexCall;
if (i == 0) {
recordedArray = arrayCall.operands.toArray();
continue;
} else {
if (!Arrays.equals(arrayCall.operands.toArray(), recordedArray)) {
terms.set(i, rexBuilder.makeLiteral(false));
break;
}
}
}
}

terms.set(i, simplify.simplify(t, unknownAs));
RelOptPredicateList newPredicates =
simplify.predicates.union(rexBuilder,
Expand Down Expand Up @@ -2622,21 +2644,34 @@ private static class VariableCollector extends RexVisitorImpl<Void> {

/** Represents a simple Comparison.
*
* <p>Left hand side is a {@link RexNode}, right hand side is a literal.
* <p>Left hand side is a {@link RexNode}, right hand side is a literal/array-call.
*/
private static class Comparison implements Predicate {
final RexNode ref;
final SqlKind kind;
//RexCall or RexLiteral
final RexLiteral literal;
final RexCall rexCall;

private Comparison(RexNode ref, SqlKind kind, RexLiteral literal) {
this.ref = requireNonNull(ref, "ref");
this.kind = requireNonNull(kind, "kind");
this.literal = requireNonNull(literal, "literal");
this.rexCall = null;
}

/**
* Add for RexCall, such as ARRAY[...,...].
*/
private Comparison(RexNode ref, SqlKind kind, RexCall arrayCall) {
this.ref = requireNonNull(ref, "ref");
this.kind = requireNonNull(kind, "kind");
this.rexCall = requireNonNull(arrayCall, "arrayCall");
this.literal = null;
}

/** Creates a comparison, between a {@link RexInputRef} or {@link RexFieldAccess} or
* deterministic {@link RexCall} and a literal. */
* deterministic {@link RexCall} and a literal/array-call. */
static @Nullable Comparison of(RexNode e) {
return of(e, node -> RexUtil.isReferenceOrAccess(node, true)
|| RexUtil.isDeterministic(node));
Expand All @@ -2660,6 +2695,24 @@ private Comparison(RexNode ref, SqlKind kind, RexLiteral literal) {
return new Comparison(left, e.getKind(), (RexLiteral) right);
}
break;
//add for RexCall like "CAST(ARRAY(..., ...))..."
case CAST:
final RexCall castCall = (RexCall) right;
final RexNode castLeft = castCall.getOperands().get(0);
switch (castLeft.getKind()) {
case ARRAY_VALUE_CONSTRUCTOR:
//array-type RexCall --> create a comparison of RexCall
if (nodePredicate.test(left)) {
return new Comparison(left, e.getKind(), (RexCall) castLeft);
}
}
break;
case ARRAY_VALUE_CONSTRUCTOR:
//array-type RexCall --> create a comparison of RexCall
if (nodePredicate.test(left)) {
return new Comparison(left, e.getKind(), (RexCall) right);
}
break;
default:
break;
}
Expand All @@ -2669,10 +2722,28 @@ private Comparison(RexNode ref, SqlKind kind, RexLiteral literal) {
return new Comparison(right, e.getKind().reverse(), (RexLiteral) left);
}
break;
//add for RexCall like "CAST(ARRAY(..., ...))..."
case CAST:
final RexCall castCall = (RexCall) left;
final RexNode castLeft = castCall.getOperands().get(0);
switch (castLeft.getKind()) {
case ARRAY_VALUE_CONSTRUCTOR:
//array-type RexCall --> create a comparison of RexCall
if (nodePredicate.test(right)) {
return new Comparison(right, e.getKind().reverse(), (RexCall) castLeft);
}
}
break;
case ARRAY_VALUE_CONSTRUCTOR:
//array-type RexCall --> create a comparison of RexCall
if (nodePredicate.test(right)) {
return new Comparison(right, e.getKind().reverse(), (RexCall) left);
}
break;
default:
break;
}
break;
break;
default:
break;
}
Expand Down