use of org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RexProgramTest method testSimplifyCaseNullableVarChar.
@Test
public void testSimplifyCaseNullableVarChar() {
RexNode condition = eq(rexBuilder.makeInputRef(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), false), 0), rexBuilder.makeLiteral("S"));
RexLiteral aLiteral = rexBuilder.makeLiteral("A");
RexLiteral bLiteral = rexBuilder.makeLiteral("B");
RexCall caseNode = (RexCall) case_(condition, aLiteral, bLiteral);
RexCall result = (RexCall) simplify.simplify(caseNode);
assertThat(result.getType().isNullable(), is(false));
assertThat(result.getType().getSqlTypeName(), is(SqlTypeName.CHAR));
assertThat(result, is(caseNode));
}
use of org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RexProgramTest method testCnf.
/**
* Unit test for {@link org.apache.calcite.rex.RexUtil#toCnf}.
*/
@Test
public void testCnf() {
final RelDataType booleanType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
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).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 fRef = rexBuilder.makeFieldAccess(range, 5);
final RexNode gRef = rexBuilder.makeFieldAccess(range, 6);
final RexNode hRef = rexBuilder.makeFieldAccess(range, 7);
final RexLiteral sevenLiteral = rexBuilder.makeExactLiteral(BigDecimal.valueOf(7));
final RexNode hEqSeven = eq(hRef, sevenLiteral);
checkCnf(aRef, "?0.a");
checkCnf(trueLiteral, "true");
checkCnf(falseLiteral, "false");
checkCnf(unknownLiteral, "null");
checkCnf(and(aRef, bRef), "AND(?0.a, ?0.b)");
checkCnf(and(aRef, bRef, cRef), "AND(?0.a, ?0.b, ?0.c)");
checkCnf(and(or(aRef, bRef), or(cRef, dRef)), "AND(OR(?0.a, ?0.b), OR(?0.c, ?0.d))");
checkCnf(or(and(aRef, bRef), and(cRef, dRef)), "AND(OR(?0.a, ?0.c), OR(?0.a, ?0.d), OR(?0.b, ?0.c), OR(?0.b, ?0.d))");
// Input has nested ORs, output ORs are flat
checkCnf(or(and(aRef, bRef), or(cRef, dRef)), "AND(OR(?0.a, ?0.c, ?0.d), OR(?0.b, ?0.c, ?0.d))");
checkCnf(or(aRef, not(and(bRef, not(hEqSeven)))), "OR(?0.a, NOT(?0.b), =(?0.h, 7))");
// apply de Morgan's theorem
checkCnf(not(or(aRef, not(bRef))), "AND(NOT(?0.a), ?0.b)");
// apply de Morgan's theorem,
// filter out 'OR ... FALSE' and 'AND ... TRUE'
checkCnf(not(or(and(aRef, trueLiteral), not(bRef), falseLiteral)), "AND(NOT(?0.a), ?0.b)");
checkCnf(and(aRef, or(bRef, and(cRef, dRef))), "AND(?0.a, OR(?0.b, ?0.c), OR(?0.b, ?0.d))");
checkCnf(and(aRef, or(bRef, and(cRef, or(dRef, and(eRef, or(fRef, gRef)))))), "AND(?0.a, OR(?0.b, ?0.c), OR(?0.b, ?0.d, ?0.e), OR(?0.b, ?0.d, ?0.f, ?0.g))");
checkCnf(and(aRef, or(bRef, and(cRef, or(dRef, and(eRef, or(fRef, and(gRef, or(trueLiteral, falseLiteral)))))))), "AND(?0.a, OR(?0.b, ?0.c), OR(?0.b, ?0.d, ?0.e), OR(?0.b, ?0.d, ?0.f, ?0.g))");
}
use of org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RexProgramTest method testThresholdCnf.
/**
* Unit test for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1290">[CALCITE-1290]
* When converting to CNF, fail if the expression exceeds a threshold</a>.
*/
@Test
public void testThresholdCnf() {
final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
final RelDataType rowType = typeFactory.builder().add("x", intType).add("y", intType).build();
final RexDynamicParam range = rexBuilder.makeDynamicParam(rowType, 0);
final RexNode xRef = rexBuilder.makeFieldAccess(range, 0);
final RexNode yRef = rexBuilder.makeFieldAccess(range, 1);
final RexLiteral literal1 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(1));
final RexLiteral literal2 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(2));
final RexLiteral literal3 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(3));
final RexLiteral literal4 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(4));
// Expression
// OR(=(?0.x, 1), AND(=(?0.x, 2), =(?0.y, 3)))
// transformation creates 7 nodes
// AND(OR(=(?0.x, 1), =(?0.x, 2)), OR(=(?0.x, 1), =(?0.y, 3)))
// Thus, it is triggered.
checkThresholdCnf(or(eq(xRef, literal1), and(eq(xRef, literal2), eq(yRef, literal3))), 8, "AND(OR(=(?0.x, 1), =(?0.x, 2)), OR(=(?0.x, 1), =(?0.y, 3)))");
// Expression
// OR(=(?0.x, 1), =(?0.x, 2), AND(=(?0.x, 3), =(?0.y, 4)))
// transformation creates 9 nodes
// AND(OR(=(?0.x, 1), =(?0.x, 2), =(?0.x, 3)),
// OR(=(?0.x, 1), =(?0.x, 2), =(?0.y, 8)))
// Thus, it is NOT triggered.
checkThresholdCnf(or(eq(xRef, literal1), eq(xRef, literal2), and(eq(xRef, literal3), eq(yRef, literal4))), 8, "OR(=(?0.x, 1), =(?0.x, 2), AND(=(?0.x, 3), =(?0.y, 4)))");
}
use of org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RelMetadataTest method testAllPredicatesAggregate2.
@Test
public void testAllPredicatesAggregate2() {
final String sql = "select * from (select a, max(b) from (\n" + " select empno as a, sal as b from emp)subq\n" + "group by a) \n" + "where a = 5";
final RelNode rel = convertSql(sql);
final RelMetadataQuery mq = RelMetadataQuery.instance();
RelOptPredicateList inputSet = mq.getAllPredicates(rel);
ImmutableList<RexNode> pulledUpPredicates = inputSet.pulledUpPredicates;
assertThat(pulledUpPredicates.size(), is(1));
RexCall call = (RexCall) pulledUpPredicates.get(0);
assertThat(call.getOperands().size(), is(2));
final RexTableInputRef inputRef1 = (RexTableInputRef) call.getOperands().get(0);
assertTrue(inputRef1.getQualifiedName().equals(EMP_QNAME));
assertThat(inputRef1.getIndex(), is(0));
final RexLiteral constant = (RexLiteral) call.getOperands().get(1);
assertThat(constant.toString(), is("5"));
}
use of org.apache.calcite.rex.RexLiteral in project calcite by apache.
the class RelMetadataTest method addRow.
private void addRow(ImmutableList.Builder<ImmutableList<RexLiteral>> builder, RexBuilder rexBuilder, Object... values) {
ImmutableList.Builder<RexLiteral> b = ImmutableList.builder();
final RelDataType varcharType = rexBuilder.getTypeFactory().createSqlType(SqlTypeName.VARCHAR);
for (Object value : values) {
final RexLiteral literal;
if (value == null) {
literal = rexBuilder.makeNullLiteral(varcharType);
} else if (value instanceof Integer) {
literal = rexBuilder.makeExactLiteral(BigDecimal.valueOf((Integer) value));
} else {
literal = rexBuilder.makeLiteral((String) value);
}
b.add(literal);
}
builder.add(b.build());
}
Aggregations