Search in sources :

Example 31 with RexCall

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

the class RelOptUtil method collapseExpandedIsNotDistinctFromExpr.

/**
 * Helper method for
 * {@link #splitJoinCondition(RexBuilder, int, RexNode, List, List, List, List)} and
 * {@link #splitJoinCondition(List, List, RexNode, List, List, List, List)}.
 *
 * <p>If the given expr <code>call</code> is an expanded version of
 * IS NOT DISTINCT FROM function call, collapse it and return a
 * IS NOT DISTINCT FROM function call.
 *
 * <p>For example: {@code t1.key IS NOT DISTINCT FROM t2.key}
 * can rewritten in expanded form as
 * {@code t1.key = t2.key OR (t1.key IS NULL AND t2.key IS NULL)}.
 *
 * @param call       Function expression to try collapsing.
 * @param rexBuilder {@link RexBuilder} instance to create new {@link RexCall} instances.
 * @return If the given function is an expanded IS NOT DISTINCT FROM function call,
 *         return a IS NOT DISTINCT FROM function call. Otherwise return the input
 *         function call as it is.
 */
private static RexCall collapseExpandedIsNotDistinctFromExpr(final RexCall call, final RexBuilder rexBuilder) {
    if (call.getKind() != SqlKind.OR || call.getOperands().size() != 2) {
        return call;
    }
    final RexNode op0 = call.getOperands().get(0);
    final RexNode op1 = call.getOperands().get(1);
    if (!(op0 instanceof RexCall) || !(op1 instanceof RexCall)) {
        return call;
    }
    RexCall opEqCall = (RexCall) op0;
    RexCall opNullEqCall = (RexCall) op1;
    if (opEqCall.getKind() == SqlKind.AND && opNullEqCall.getKind() == SqlKind.EQUALS) {
        RexCall temp = opEqCall;
        opEqCall = opNullEqCall;
        opNullEqCall = temp;
    }
    if (opNullEqCall.getKind() != SqlKind.AND || opNullEqCall.getOperands().size() != 2 || opEqCall.getKind() != SqlKind.EQUALS) {
        return call;
    }
    final RexNode op10 = opNullEqCall.getOperands().get(0);
    final RexNode op11 = opNullEqCall.getOperands().get(1);
    if (op10.getKind() != SqlKind.IS_NULL || op11.getKind() != SqlKind.IS_NULL) {
        return call;
    }
    final RexNode isNullInput0 = ((RexCall) op10).getOperands().get(0);
    final RexNode isNullInput1 = ((RexCall) op11).getOperands().get(0);
    final String isNullInput0Digest = isNullInput0.toString();
    final String isNullInput1Digest = isNullInput1.toString();
    final String equalsInput0Digest = opEqCall.getOperands().get(0).toString();
    final String equalsInput1Digest = opEqCall.getOperands().get(1).toString();
    if ((isNullInput0Digest.equals(equalsInput0Digest) && isNullInput1Digest.equals(equalsInput1Digest)) || (isNullInput1Digest.equals(equalsInput0Digest) && isNullInput0Digest.equals(equalsInput1Digest))) {
        return (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_DISTINCT_FROM, ImmutableList.of(isNullInput0, isNullInput1));
    }
    return call;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexNode(org.apache.calcite.rex.RexNode)

Example 32 with RexCall

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

the class VisitorDataContext method removeCast.

private static RexNode removeCast(RexNode inputRef) {
    if (inputRef instanceof RexCall) {
        final RexCall castedRef = (RexCall) inputRef;
        final SqlOperator operator = castedRef.getOperator();
        if (operator instanceof SqlCastFunction) {
            inputRef = castedRef.getOperands().get(0);
        }
    }
    return inputRef;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) SqlCastFunction(org.apache.calcite.sql.fun.SqlCastFunction) SqlOperator(org.apache.calcite.sql.SqlOperator)

Example 33 with RexCall

use of org.apache.calcite.rex.RexCall 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()));
        }
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexLiteral(org.apache.calcite.rex.RexLiteral) TimestampString(org.apache.calcite.util.TimestampString) TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) RexUtil(org.apache.calcite.rex.RexUtil) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 34 with RexCall

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

the class RexProgramTest method testSimplifyNot.

@Test
public void testSimplifyNot() {
    final RelDataType booleanNullableType = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BOOLEAN), true);
    final RexNode booleanInput = rexBuilder.makeInputRef(booleanNullableType, 0);
    final RexNode isFalse = rexBuilder.makeCall(SqlStdOperatorTable.IS_FALSE, booleanInput);
    final RexCall result = (RexCall) simplify(isFalse);
    assertThat(result.getType().isNullable(), is(false));
    assertThat(result.getOperator(), is((SqlOperator) SqlStdOperatorTable.IS_FALSE));
    assertThat(result.getOperands().size(), is(1));
    assertThat(result.getOperands().get(0), is(booleanInput));
    // Make sure that IS_FALSE(IS_FALSE(nullable boolean)) != IS_TRUE(nullable boolean)
    // IS_FALSE(IS_FALSE(null)) = IS_FALSE(false) = true
    // IS_TRUE(null) = false
    final RexNode isFalseIsFalse = rexBuilder.makeCall(SqlStdOperatorTable.IS_FALSE, isFalse);
    final RexCall result2 = (RexCall) simplify(isFalseIsFalse);
    assertThat(result2.getType().isNullable(), is(false));
    assertThat(result2.getOperator(), is((SqlOperator) SqlStdOperatorTable.IS_NOT_FALSE));
    assertThat(result2.getOperands().size(), is(1));
    assertThat(result2.getOperands().get(0), is(booleanInput));
}
Also used : RexCall(org.apache.calcite.rex.RexCall) SqlOperator(org.apache.calcite.sql.SqlOperator) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 35 with RexCall

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

Aggregations

RexCall (org.apache.calcite.rex.RexCall)213 RexNode (org.apache.calcite.rex.RexNode)172 RexInputRef (org.apache.calcite.rex.RexInputRef)61 ArrayList (java.util.ArrayList)59 RexLiteral (org.apache.calcite.rex.RexLiteral)44 Nullable (javax.annotation.Nullable)35 RelNode (org.apache.calcite.rel.RelNode)26 RelDataType (org.apache.calcite.rel.type.RelDataType)24 SqlOperator (org.apache.calcite.sql.SqlOperator)23 List (java.util.List)22 RexBuilder (org.apache.calcite.rex.RexBuilder)22 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)19 SqlKind (org.apache.calcite.sql.SqlKind)14 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)14 RelOptUtil (org.apache.calcite.plan.RelOptUtil)11 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)11 RexCall (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall)10 RexTableInputRef (org.apache.calcite.rex.RexTableInputRef)10 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)9 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9