Search in sources :

Example 1 with RexDynamicParam

use of org.apache.calcite.rex.RexDynamicParam in project calcite by apache.

the class RelFieldTrimmer method trimFields.

/**
 * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
 * {@link org.apache.calcite.rel.core.Sort}.
 */
public TrimResult trimFields(Sort sort, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    final RelDataType rowType = sort.getRowType();
    final int fieldCount = rowType.getFieldCount();
    final RelCollation collation = sort.getCollation();
    final RelNode input = sort.getInput();
    // We use the fields used by the consumer, plus any fields used as sort
    // keys.
    final ImmutableBitSet.Builder inputFieldsUsed = fieldsUsed.rebuild();
    for (RelFieldCollation field : collation.getFieldCollations()) {
        inputFieldsUsed.set(field.getFieldIndex());
    }
    // Create input with trimmed columns.
    final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
    TrimResult trimResult = trimChild(sort, input, inputFieldsUsed.build(), inputExtraFields);
    RelNode newInput = trimResult.left;
    final Mapping inputMapping = trimResult.right;
    // there's nothing we can do.
    if (newInput == input && inputMapping.isIdentity() && fieldsUsed.cardinality() == fieldCount) {
        return result(sort, Mappings.createIdentity(fieldCount));
    }
    // leave the Sort unchanged in case we have dynamic limits
    if (sort.offset instanceof RexDynamicParam || sort.fetch instanceof RexDynamicParam) {
        return result(sort, inputMapping);
    }
    relBuilder.push(newInput);
    final int offset = sort.offset == null ? 0 : RexLiteral.intValue(sort.offset);
    final int fetch = sort.fetch == null ? -1 : RexLiteral.intValue(sort.fetch);
    final ImmutableList<RexNode> fields = relBuilder.fields(RexUtil.apply(inputMapping, collation));
    relBuilder.sortLimit(offset, fetch, fields);
    // needs them for its condition.
    return result(relBuilder.build(), inputMapping);
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelDataType(org.apache.calcite.rel.type.RelDataType) Mapping(org.apache.calcite.util.mapping.Mapping) RelCollation(org.apache.calcite.rel.RelCollation) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with RexDynamicParam

use of org.apache.calcite.rex.RexDynamicParam 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)");
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataType(org.apache.calcite.rel.type.RelDataType) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 3 with RexDynamicParam

use of org.apache.calcite.rex.RexDynamicParam 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}"));
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataType(org.apache.calcite.rel.type.RelDataType) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 4 with RexDynamicParam

use of org.apache.calcite.rex.RexDynamicParam in project calcite by apache.

the class RexProgramTest method testCnf2.

/**
 * Unit test for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-394">[CALCITE-394]
 * Add RexUtil.toCnf, to convert expressions to conjunctive normal form
 * (CNF)</a>.
 */
@Test
public void testCnf2() {
    final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
    final RelDataType rowType = typeFactory.builder().add("x", intType).add("y", intType).add("z", intType).add("a", intType).add("b", intType).build();
    final RexDynamicParam range = rexBuilder.makeDynamicParam(rowType, 0);
    final RexNode xRef = rexBuilder.makeFieldAccess(range, 0);
    final RexNode yRef = rexBuilder.makeFieldAccess(range, 1);
    final RexNode zRef = rexBuilder.makeFieldAccess(range, 2);
    final RexNode aRef = rexBuilder.makeFieldAccess(range, 3);
    final RexNode bRef = rexBuilder.makeFieldAccess(range, 4);
    final RexLiteral literal1 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(1));
    final RexLiteral literal2 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(2));
    final RexLiteral literal3 = rexBuilder.makeExactLiteral(BigDecimal.valueOf(3));
    checkCnf(or(and(eq(xRef, literal1), eq(yRef, literal1), eq(zRef, literal1)), and(eq(xRef, literal2), eq(yRef, literal2), eq(aRef, literal2)), and(eq(xRef, literal3), eq(aRef, literal3), eq(bRef, literal3))), "AND(" + "OR(=(?0.x, 1), =(?0.x, 2), =(?0.x, 3)), " + "OR(=(?0.x, 1), =(?0.x, 2), =(?0.a, 3)), " + "OR(=(?0.x, 1), =(?0.x, 2), =(?0.b, 3)), " + "OR(=(?0.x, 1), =(?0.y, 2), =(?0.x, 3)), " + "OR(=(?0.x, 1), =(?0.y, 2), =(?0.a, 3)), " + "OR(=(?0.x, 1), =(?0.y, 2), =(?0.b, 3)), " + "OR(=(?0.x, 1), =(?0.a, 2), =(?0.x, 3)), " + "OR(=(?0.x, 1), =(?0.a, 2), =(?0.a, 3)), " + "OR(=(?0.x, 1), =(?0.a, 2), =(?0.b, 3)), " + "OR(=(?0.y, 1), =(?0.x, 2), =(?0.x, 3)), " + "OR(=(?0.y, 1), =(?0.x, 2), =(?0.a, 3)), " + "OR(=(?0.y, 1), =(?0.x, 2), =(?0.b, 3)), " + "OR(=(?0.y, 1), =(?0.y, 2), =(?0.x, 3)), " + "OR(=(?0.y, 1), =(?0.y, 2), =(?0.a, 3)), " + "OR(=(?0.y, 1), =(?0.y, 2), =(?0.b, 3)), " + "OR(=(?0.y, 1), =(?0.a, 2), =(?0.x, 3)), " + "OR(=(?0.y, 1), =(?0.a, 2), =(?0.a, 3)), " + "OR(=(?0.y, 1), =(?0.a, 2), =(?0.b, 3)), " + "OR(=(?0.z, 1), =(?0.x, 2), =(?0.x, 3)), " + "OR(=(?0.z, 1), =(?0.x, 2), =(?0.a, 3)), " + "OR(=(?0.z, 1), =(?0.x, 2), =(?0.b, 3)), " + "OR(=(?0.z, 1), =(?0.y, 2), =(?0.x, 3)), " + "OR(=(?0.z, 1), =(?0.y, 2), =(?0.a, 3)), " + "OR(=(?0.z, 1), =(?0.y, 2), =(?0.b, 3)), " + "OR(=(?0.z, 1), =(?0.a, 2), =(?0.x, 3)), " + "OR(=(?0.z, 1), =(?0.a, 2), =(?0.a, 3)), " + "OR(=(?0.z, 1), =(?0.a, 2), =(?0.b, 3)))");
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataType(org.apache.calcite.rel.type.RelDataType) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 5 with RexDynamicParam

use of org.apache.calcite.rex.RexDynamicParam in project calcite by apache.

the class RexProgramTest method testPullFactors.

/**
 * Unit test for {@link org.apache.calcite.rex.RexUtil#pullFactors}.
 */
@Test
public void testPullFactors() {
    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);
    // Most of the expressions in testCnf are unaffected by pullFactors.
    checkPullFactors(or(and(aRef, bRef), and(cRef, aRef, dRef, aRef)), "AND(?0.a, OR(?0.b, AND(?0.c, ?0.d)))");
    checkPullFactors(aRef, "?0.a");
    checkPullFactors(trueLiteral, "true");
    checkPullFactors(falseLiteral, "false");
    checkPullFactors(unknownLiteral, "null");
    checkPullFactors(and(aRef, bRef), "AND(?0.a, ?0.b)");
    checkPullFactors(and(aRef, bRef, cRef), "AND(?0.a, ?0.b, ?0.c)");
    checkPullFactorsUnchanged(and(or(aRef, bRef), or(cRef, dRef)));
    checkPullFactorsUnchanged(or(and(aRef, bRef), and(cRef, dRef)));
    // Input has nested ORs, output ORs are flat; different from CNF
    checkPullFactors(or(and(aRef, bRef), or(cRef, dRef)), "OR(AND(?0.a, ?0.b), ?0.c, ?0.d)");
    checkPullFactorsUnchanged(or(aRef, not(and(bRef, not(hEqSeven)))));
    checkPullFactorsUnchanged(not(or(aRef, not(bRef))));
    checkPullFactorsUnchanged(not(or(and(aRef, trueLiteral), not(bRef), falseLiteral)));
    checkPullFactorsUnchanged(and(aRef, or(bRef, and(cRef, dRef))));
    checkPullFactorsUnchanged(and(aRef, or(bRef, and(cRef, or(dRef, and(eRef, or(fRef, gRef)))))));
    checkPullFactorsUnchanged(and(aRef, or(bRef, and(cRef, or(dRef, and(eRef, or(fRef, and(gRef, or(trueLiteral, falseLiteral)))))))));
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataType(org.apache.calcite.rel.type.RelDataType) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Aggregations

RexDynamicParam (org.apache.calcite.rex.RexDynamicParam)13 RexNode (org.apache.calcite.rex.RexNode)11 RelDataType (org.apache.calcite.rel.type.RelDataType)10 RexLiteral (org.apache.calcite.rex.RexLiteral)8 Test (org.junit.Test)7 RelNode (org.apache.calcite.rel.RelNode)3 RexDynamicParam (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam)2 RelCollation (org.apache.calcite.rel.RelCollation)2 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)2 Mapping (org.apache.calcite.util.mapping.Mapping)2 StatementExecutionException (herddb.model.StatementExecutionException)1 RawString (herddb.utils.RawString)1 BigDecimal (java.math.BigDecimal)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ByteString (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString)1