use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class PigFilter method getSingleFilterCondition.
private String getSingleFilterCondition(Implementor implementor, String op, RexCall call) {
final String fieldName;
final String literal;
final RexNode left = call.operands.get(0);
final RexNode right = call.operands.get(1);
if (left.getKind() == LITERAL) {
if (right.getKind() != INPUT_REF) {
throw new IllegalArgumentException("Expected a RexCall with a single field and single literal");
} else {
fieldName = implementor.getFieldName(this, ((RexInputRef) right).getIndex());
literal = getLiteralAsString((RexLiteral) left);
}
} else if (right.getKind() == LITERAL) {
if (left.getKind() != INPUT_REF) {
throw new IllegalArgumentException("Expected a RexCall with a single field and single literal");
} else {
fieldName = implementor.getFieldName(this, ((RexInputRef) left).getIndex());
literal = getLiteralAsString((RexLiteral) right);
}
} else {
throw new IllegalArgumentException("Expected a RexCall with a single field and single literal");
}
return '(' + fieldName + ' ' + op + ' ' + literal + ')';
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RexImplicationCheckerTest method testSimplifyFloor.
/**
* Test case for simplifier of ceil and floor.
*/
@Test
public void testSimplifyFloor() {
final ImmutableList<TimeUnitRange> timeUnitRanges = ImmutableList.of(TimeUnitRange.WEEK, TimeUnitRange.YEAR, TimeUnitRange.QUARTER, TimeUnitRange.MONTH, TimeUnitRange.DAY, TimeUnitRange.HOUR, TimeUnitRange.MINUTE, TimeUnitRange.SECOND, TimeUnitRange.MILLISECOND, TimeUnitRange.MICROSECOND);
final Fixture f = new Fixture();
final RexUtil.ExprSimplifier defaultSimplifier = new RexUtil.ExprSimplifier(f.simplify, true);
final RexNode literalTs = f.timestampLiteral(new TimestampString("2010-10-10 00:00:00"));
// Exclude WEEK as it is only used for the negative tests
for (int i = 1; i < timeUnitRanges.size(); i++) {
final RexNode innerFloorCall = f.rexBuilder.makeCall(SqlStdOperatorTable.CEIL, literalTs, f.rexBuilder.makeFlag(timeUnitRanges.get(i)));
for (int j = 1; j <= i; j++) {
final RexNode outerFloorCall = f.rexBuilder.makeCall(SqlStdOperatorTable.FLOOR, innerFloorCall, f.rexBuilder.makeFlag(timeUnitRanges.get(j)));
final RexCall simplifiedExpr = (RexCall) defaultSimplifier.apply(outerFloorCall);
assertThat(simplifiedExpr.getKind(), is(SqlKind.FLOOR));
assertThat(((RexLiteral) simplifiedExpr.getOperands().get(1)).getValue().toString(), is(timeUnitRanges.get(j).toString()));
assertThat(simplifiedExpr.getOperands().get(0).toString(), is(literalTs.toString()));
}
}
// Negative test
for (int i = timeUnitRanges.size() - 1; i >= 0; i--) {
final RexNode innerFloorCall = f.rexBuilder.makeCall(SqlStdOperatorTable.FLOOR, literalTs, f.rexBuilder.makeFlag(timeUnitRanges.get(i)));
for (int j = timeUnitRanges.size() - 1; j > i; j--) {
final RexNode outerFloorCall = f.rexBuilder.makeCall(SqlStdOperatorTable.CEIL, innerFloorCall, f.rexBuilder.makeFlag(timeUnitRanges.get(j)));
final RexCall simplifiedExpr = (RexCall) defaultSimplifier.apply(outerFloorCall);
assertThat(simplifiedExpr.toString(), is(outerFloorCall.toString()));
}
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RexProgramTest method testSimplifyLiterals.
@Test
public void testSimplifyLiterals() {
final RexLiteral literalAbc = rexBuilder.makeLiteral("abc");
final RexLiteral literalDef = rexBuilder.makeLiteral("def");
final RexLiteral literalZero = rexBuilder.makeExactLiteral(BigDecimal.ZERO);
final RexLiteral literalOne = rexBuilder.makeExactLiteral(BigDecimal.ONE);
final RexLiteral literalOneDotZero = rexBuilder.makeExactLiteral(new BigDecimal(1.0));
// Check string comparison
checkSimplify(eq(literalAbc, literalAbc), "true");
checkSimplify(eq(literalAbc, literalDef), "false");
checkSimplify(ne(literalAbc, literalAbc), "false");
checkSimplify(ne(literalAbc, literalDef), "true");
checkSimplify(gt(literalAbc, literalDef), "false");
checkSimplify(gt(literalDef, literalAbc), "true");
checkSimplify(gt(literalDef, literalDef), "false");
checkSimplify(ge(literalAbc, literalDef), "false");
checkSimplify(ge(literalDef, literalAbc), "true");
checkSimplify(ge(literalDef, literalDef), "true");
checkSimplify(lt(literalAbc, literalDef), "true");
checkSimplify(lt(literalAbc, literalDef), "true");
checkSimplify(lt(literalDef, literalDef), "false");
checkSimplify(le(literalAbc, literalDef), "true");
checkSimplify(le(literalDef, literalAbc), "false");
checkSimplify(le(literalDef, literalDef), "true");
// Check whole number comparison
checkSimplify(eq(literalZero, literalOne), "false");
checkSimplify(eq(literalOne, literalZero), "false");
checkSimplify(ne(literalZero, literalOne), "true");
checkSimplify(ne(literalOne, literalZero), "true");
checkSimplify(gt(literalZero, literalOne), "false");
checkSimplify(gt(literalOne, literalZero), "true");
checkSimplify(gt(literalOne, literalOne), "false");
checkSimplify(ge(literalZero, literalOne), "false");
checkSimplify(ge(literalOne, literalZero), "true");
checkSimplify(ge(literalOne, literalOne), "true");
checkSimplify(lt(literalZero, literalOne), "true");
checkSimplify(lt(literalOne, literalZero), "false");
checkSimplify(lt(literalOne, literalOne), "false");
checkSimplify(le(literalZero, literalOne), "true");
checkSimplify(le(literalOne, literalZero), "false");
checkSimplify(le(literalOne, literalOne), "true");
// Check decimal equality comparison
checkSimplify(eq(literalOne, literalOneDotZero), "true");
checkSimplify(eq(literalOneDotZero, literalOne), "true");
checkSimplify(ne(literalOne, literalOneDotZero), "false");
checkSimplify(ne(literalOneDotZero, literalOne), "false");
// Check different types shouldn't change simplification
checkSimplifyUnchanged(eq(literalZero, literalAbc));
checkSimplifyUnchanged(eq(literalAbc, literalZero));
checkSimplifyUnchanged(ne(literalZero, literalAbc));
checkSimplifyUnchanged(ne(literalAbc, literalZero));
checkSimplifyUnchanged(gt(literalZero, literalAbc));
checkSimplifyUnchanged(gt(literalAbc, literalZero));
checkSimplifyUnchanged(ge(literalZero, literalAbc));
checkSimplifyUnchanged(ge(literalAbc, literalZero));
checkSimplifyUnchanged(lt(literalZero, literalAbc));
checkSimplifyUnchanged(lt(literalAbc, literalZero));
checkSimplifyUnchanged(le(literalZero, literalAbc));
checkSimplifyUnchanged(le(literalAbc, literalZero));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RexProgramTest method testSimplify.
@Test
public void testSimplify() {
final RelDataType booleanType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
final RelDataType intNullableType = typeFactory.createTypeWithNullability(intType, true);
final RelDataType rowType = typeFactory.builder().add("a", booleanType).add("b", booleanType).add("c", booleanType).add("d", booleanType).add("e", booleanType).add("f", booleanType).add("g", booleanType).add("h", intType).add("i", intNullableType).build();
final RexDynamicParam range = rexBuilder.makeDynamicParam(rowType, 0);
final RexNode aRef = rexBuilder.makeFieldAccess(range, 0);
final RexNode bRef = rexBuilder.makeFieldAccess(range, 1);
final RexNode cRef = rexBuilder.makeFieldAccess(range, 2);
final RexNode dRef = rexBuilder.makeFieldAccess(range, 3);
final RexNode eRef = rexBuilder.makeFieldAccess(range, 4);
final RexNode hRef = rexBuilder.makeFieldAccess(range, 7);
final RexNode iRef = rexBuilder.makeFieldAccess(range, 8);
final RexLiteral literal1 = rexBuilder.makeExactLiteral(BigDecimal.ONE);
// and: remove duplicates
checkSimplify(and(aRef, bRef, aRef), "AND(?0.a, ?0.b)");
// and: remove true
checkSimplify(and(aRef, bRef, trueLiteral), "AND(?0.a, ?0.b)");
// and: false falsifies
checkSimplify(and(aRef, bRef, falseLiteral), "false");
// and: remove duplicate "not"s
checkSimplify(and(not(aRef), bRef, not(cRef), not(aRef)), "AND(?0.b, NOT(?0.a), NOT(?0.c))");
// and: "not true" falsifies
checkSimplify(and(not(aRef), bRef, not(trueLiteral)), "false");
// and: flatten and remove duplicates
checkSimplify(and(aRef, and(and(bRef, not(cRef), dRef, not(eRef)), not(eRef))), "AND(?0.a, ?0.b, ?0.d, NOT(?0.c), NOT(?0.e))");
// and: expand "... and not(or(x, y))" to "... and not(x) and not(y)"
checkSimplify(and(aRef, bRef, not(or(cRef, or(dRef, eRef)))), "AND(?0.a, ?0.b, NOT(?0.c), NOT(?0.d), NOT(?0.e))");
checkSimplify(and(aRef, bRef, not(or(not(cRef), dRef, not(eRef)))), "AND(?0.a, ?0.b, ?0.c, ?0.e, NOT(?0.d))");
// or: remove duplicates
checkSimplify(or(aRef, bRef, aRef), "OR(?0.a, ?0.b)");
// or: remove false
checkSimplify(or(aRef, bRef, falseLiteral), "OR(?0.a, ?0.b)");
// or: true makes everything true
checkSimplify(or(aRef, bRef, trueLiteral), "true");
// case: remove false branches
checkSimplify(case_(eq(bRef, cRef), dRef, falseLiteral, aRef, eRef), "CASE(=(?0.b, ?0.c), ?0.d, ?0.e)");
// case: true branches become the last branch
checkSimplify(case_(eq(bRef, cRef), dRef, trueLiteral, aRef, eq(cRef, dRef), eRef, cRef), "CASE(=(?0.b, ?0.c), ?0.d, ?0.a)");
// case: singleton
checkSimplify(case_(trueLiteral, aRef, eq(cRef, dRef), eRef, cRef), "?0.a");
// case: always same value
checkSimplify(case_(aRef, literal1, bRef, literal1, cRef, literal1, dRef, literal1, literal1), "1");
// case: trailing false and null, no simplification
checkSimplify2(case_(aRef, trueLiteral, bRef, trueLiteral, cRef, falseLiteral, unknownLiteral), "CASE(?0.a, true, ?0.b, true, ?0.c, false, null)", "CAST(OR(?0.a, ?0.b)):BOOLEAN");
// case: form an AND of branches that return true
checkSimplify(case_(aRef, trueLiteral, bRef, falseLiteral, cRef, falseLiteral, dRef, trueLiteral, falseLiteral), "OR(?0.a, AND(?0.d, NOT(?0.b), NOT(?0.c)))");
checkSimplify(case_(aRef, trueLiteral, bRef, falseLiteral, cRef, falseLiteral, dRef, trueLiteral, eRef, falseLiteral, trueLiteral), "OR(?0.a, AND(?0.d, NOT(?0.b), NOT(?0.c)), AND(NOT(?0.b), NOT(?0.c), NOT(?0.e)))");
// is null, applied to not-null value
checkSimplify(rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, aRef), "false");
// is not null, applied to not-null value
checkSimplify(rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, aRef), "true");
// condition, and the inverse - nothing to do due to null values
checkSimplify2(and(le(aRef, literal1), gt(aRef, literal1)), "AND(<=(?0.a, 1), >(?0.a, 1))", "false");
checkSimplify(and(le(aRef, literal1), ge(aRef, literal1)), "AND(<=(?0.a, 1), >=(?0.a, 1))");
checkSimplify2(and(lt(aRef, literal1), eq(aRef, literal1), ge(aRef, literal1)), "AND(<(?0.a, 1), =(?0.a, 1), >=(?0.a, 1))", "false");
checkSimplify(and(lt(aRef, literal1), or(falseLiteral, falseLiteral)), "false");
checkSimplify(and(lt(aRef, literal1), or(falseLiteral, gt(bRef, cRef))), "AND(<(?0.a, 1), >(?0.b, ?0.c))");
checkSimplify(or(lt(aRef, literal1), and(trueLiteral, trueLiteral)), "true");
checkSimplify(or(lt(aRef, literal1), and(trueLiteral, or(trueLiteral, falseLiteral))), "true");
checkSimplify(or(lt(aRef, literal1), and(trueLiteral, and(trueLiteral, falseLiteral))), "<(?0.a, 1)");
checkSimplify(or(lt(aRef, literal1), and(trueLiteral, or(falseLiteral, falseLiteral))), "<(?0.a, 1)");
// "x = x" simplifies to "x is not null"
checkSimplify(eq(literal1, literal1), "true");
checkSimplify(eq(hRef, hRef), "true");
checkSimplify2(eq(iRef, iRef), "=(?0.i, ?0.i)", "IS NOT NULL(?0.i)");
checkSimplify(eq(iRef, hRef), "=(?0.i, ?0.h)");
// "x <= x" simplifies to "x is not null"
checkSimplify(le(literal1, literal1), "true");
checkSimplify(le(hRef, hRef), "true");
checkSimplify2(le(iRef, iRef), "<=(?0.i, ?0.i)", "IS NOT NULL(?0.i)");
checkSimplify(le(iRef, hRef), "<=(?0.i, ?0.h)");
// "x >= x" simplifies to "x is not null"
checkSimplify(ge(literal1, literal1), "true");
checkSimplify(ge(hRef, hRef), "true");
checkSimplify2(ge(iRef, iRef), ">=(?0.i, ?0.i)", "IS NOT NULL(?0.i)");
checkSimplify(ge(iRef, hRef), ">=(?0.i, ?0.h)");
// "x != x" simplifies to "false"
checkSimplify(ne(literal1, literal1), "false");
checkSimplify(ne(hRef, hRef), "false");
checkSimplify2(ne(iRef, iRef), "<>(?0.i, ?0.i)", "false");
checkSimplify(ne(iRef, hRef), "<>(?0.i, ?0.h)");
// "x < x" simplifies to "false"
checkSimplify(lt(literal1, literal1), "false");
checkSimplify(lt(hRef, hRef), "false");
checkSimplify2(lt(iRef, iRef), "<(?0.i, ?0.i)", "false");
checkSimplify(lt(iRef, hRef), "<(?0.i, ?0.h)");
// "x > x" simplifies to "false"
checkSimplify(gt(literal1, literal1), "false");
checkSimplify(gt(hRef, hRef), "false");
checkSimplify2(gt(iRef, iRef), ">(?0.i, ?0.i)", "false");
checkSimplify(gt(iRef, hRef), ">(?0.i, ?0.h)");
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RexProgramTest method testConstantMap.
@Test
public void testConstantMap() {
final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
final RelDataType rowType = typeFactory.builder().add("a", intType).add("b", intType).add("c", intType).add("d", intType).add("e", intType).build();
final RexDynamicParam range = rexBuilder.makeDynamicParam(rowType, 0);
final RexNode aRef = rexBuilder.makeFieldAccess(range, 0);
final RexNode bRef = rexBuilder.makeFieldAccess(range, 1);
final RexNode cRef = rexBuilder.makeFieldAccess(range, 2);
final RexNode dRef = rexBuilder.makeFieldAccess(range, 3);
final RexNode eRef = rexBuilder.makeFieldAccess(range, 4);
final RexLiteral literal1 = rexBuilder.makeExactLiteral(BigDecimal.ONE);
final RexLiteral literal2 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(2));
final ImmutableMap<RexNode, RexNode> map = RexUtil.predicateConstants(RexNode.class, rexBuilder, ImmutableList.of(eq(aRef, bRef), eq(cRef, literal1), eq(cRef, aRef), eq(dRef, eRef)));
assertThat(getString(map), is("{1=?0.c, ?0.a=?0.b, ?0.b=?0.a, ?0.c=1, ?0.d=?0.e, ?0.e=?0.d}"));
// Contradictory constraints yield no constants
final RexNode ref0 = rexBuilder.makeInputRef(rowType, 0);
final ImmutableMap<RexNode, RexNode> map2 = RexUtil.predicateConstants(RexNode.class, rexBuilder, ImmutableList.of(eq(ref0, literal1), eq(ref0, literal2)));
assertThat(getString(map2), is("{}"));
// Contradictory constraints on field accesses SHOULD yield no constants
// but currently there's a bug
final ImmutableMap<RexNode, RexNode> map3 = RexUtil.predicateConstants(RexNode.class, rexBuilder, ImmutableList.of(eq(aRef, literal1), eq(aRef, literal2)));
assertThat(getString(map3), is("{1=?0.a, 2=?0.a}"));
}
Aggregations