Search in sources :

Example 66 with RexNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.

the class RelBuilderTest method testTypeInferenceValidation.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1595">[CALCITE-1595]
 * RelBuilder.call throws NullPointerException if argument types are
 * invalid</a>.
 */
@Test
public void testTypeInferenceValidation() {
    final RelBuilder builder = RelBuilder.create(config().build());
    // test for a) call(operator, Iterable<RexNode>)
    final RexNode arg0 = builder.literal(0);
    final RexNode arg1 = builder.literal("xyz");
    try {
        builder.call(SqlStdOperatorTable.PLUS, Lists.newArrayList(arg0, arg1));
        fail("Invalid combination of parameter types");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("cannot derive type"));
    }
    // test for b) call(operator, RexNode...)
    try {
        builder.call(SqlStdOperatorTable.PLUS, arg0, arg1);
        fail("Invalid combination of parameter types");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("cannot derive type"));
    }
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 67 with RexNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.

the class RexImpTable method implementCall.

private static Expression implementCall(final RexToLixTranslator translator, RexCall call, NotNullImplementor implementor, final NullAs nullAs) {
    List<Expression> translatedOperands = translator.translateList(call.getOperands());
    // handled for nulls before being passed to the NotNullImplementor.
    if (nullAs == NullAs.NOT_POSSIBLE) {
        List<Expression> nullHandled = translatedOperands;
        for (int i = 0; i < translatedOperands.size(); i++) {
            RexNode arg = call.getOperands().get(i);
            Expression e = translatedOperands.get(i);
            if (!translator.isNullable(arg)) {
                if (nullHandled == translatedOperands) {
                    nullHandled = new ArrayList<>(translatedOperands.subList(0, i));
                }
                nullHandled.add(translator.handleNull(e, nullAs));
            } else if (nullHandled != translatedOperands) {
                nullHandled.add(e);
            }
        }
        translatedOperands = nullHandled;
    }
    Expression result = implementor.implement(translator, call, translatedOperands);
    return translator.handleNull(result, nullAs);
}
Also used : UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Expression(org.apache.calcite.linq4j.tree.Expression) MemberExpression(org.apache.calcite.linq4j.tree.MemberExpression) RexNode(org.apache.calcite.rex.RexNode)

Example 68 with RexNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.

the class RexImpTable method harmonize.

/**
 * Ensures that operands have identical type.
 */
private static List<RexNode> harmonize(final RexToLixTranslator translator, final List<RexNode> operands) {
    int nullCount = 0;
    final List<RelDataType> types = new ArrayList<>();
    final RelDataTypeFactory typeFactory = translator.builder.getTypeFactory();
    for (RexNode operand : operands) {
        RelDataType type = operand.getType();
        type = toSql(typeFactory, type);
        if (translator.isNullable(operand)) {
            ++nullCount;
        } else {
            type = typeFactory.createTypeWithNullability(type, false);
        }
        types.add(type);
    }
    if (allSame(types)) {
        // unchanged.
        return operands;
    }
    final RelDataType type = typeFactory.leastRestrictive(types);
    if (type == null) {
        // to be harmonized.
        return operands;
    }
    assert (nullCount > 0) == type.isNullable();
    final List<RexNode> list = new ArrayList<>();
    for (RexNode operand : operands) {
        list.add(translator.builder.ensureType(type, operand, false));
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 69 with RexNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.

the class RexToLixTranslator method translate0.

/**
 * Translates an expression that is not in the cache.
 *
 * @param expr Expression
 * @param nullAs If false, if expression is definitely not null at
 *   runtime. Therefore we can optimize. For example, we can cast to int
 *   using x.intValue().
 * @return Translated expression
 */
private Expression translate0(RexNode expr, RexImpTable.NullAs nullAs, Type storageType) {
    if (nullAs == RexImpTable.NullAs.NULL && !expr.getType().isNullable()) {
        nullAs = RexImpTable.NullAs.NOT_POSSIBLE;
    }
    switch(expr.getKind()) {
        case INPUT_REF:
            final int index = ((RexInputRef) expr).getIndex();
            Expression x = inputGetter.field(list, index, storageType);
            // safe to share
            Expression input = list.append("inp" + index + "_", x);
            if (nullAs == RexImpTable.NullAs.NOT_POSSIBLE && input.type.equals(storageType)) {
                // unboxing via nullAs.handle below.
                return input;
            }
            return handleNull(input, nullAs);
        case LOCAL_REF:
            return translate(deref(expr), nullAs, storageType);
        case LITERAL:
            return translateLiteral((RexLiteral) expr, nullifyType(expr.getType(), isNullable(expr) && nullAs != RexImpTable.NullAs.NOT_POSSIBLE), typeFactory, nullAs);
        case DYNAMIC_PARAM:
            return translateParameter((RexDynamicParam) expr, nullAs, storageType);
        case CORREL_VARIABLE:
            throw new RuntimeException("Cannot translate " + expr + ". Correlated" + " variables should always be referenced by field access");
        case FIELD_ACCESS:
            RexFieldAccess fieldAccess = (RexFieldAccess) expr;
            RexNode target = deref(fieldAccess.getReferenceExpr());
            // only $cor.field access is supported
            if (!(target instanceof RexCorrelVariable)) {
                throw new RuntimeException("cannot translate expression " + expr);
            }
            if (correlates == null) {
                throw new RuntimeException("Cannot translate " + expr + " since " + "correlate variables resolver is not defined");
            }
            InputGetter getter = correlates.apply(((RexCorrelVariable) target).getName());
            return getter.field(list, fieldAccess.getField().getIndex(), storageType);
        default:
            if (expr instanceof RexCall) {
                return translateCall((RexCall) expr, nullAs);
            }
            throw new RuntimeException("cannot translate expression " + expr);
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) Expression(org.apache.calcite.linq4j.tree.Expression) UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RexInputRef(org.apache.calcite.rex.RexInputRef) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) RexNode(org.apache.calcite.rex.RexNode)

Example 70 with RexNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.

the class RexToLixTranslator method deref.

/**
 * Dereferences an expression if it is a
 * {@link org.apache.calcite.rex.RexLocalRef}.
 */
public RexNode deref(RexNode expr) {
    if (expr instanceof RexLocalRef) {
        RexLocalRef ref = (RexLocalRef) expr;
        final RexNode e2 = program.getExprList().get(ref.getIndex());
        assert ref.getType().equals(e2.getType());
        return e2;
    } else {
        return expr;
    }
}
Also used : RexLocalRef(org.apache.calcite.rex.RexLocalRef) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexNode (org.apache.calcite.rex.RexNode)1167 ArrayList (java.util.ArrayList)423 RelNode (org.apache.calcite.rel.RelNode)363 RelDataType (org.apache.calcite.rel.type.RelDataType)289 RexBuilder (org.apache.calcite.rex.RexBuilder)262 RexInputRef (org.apache.calcite.rex.RexInputRef)207 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)198 RexCall (org.apache.calcite.rex.RexCall)185 Test (org.junit.Test)138 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)136 RexLiteral (org.apache.calcite.rex.RexLiteral)104 HashMap (java.util.HashMap)102 List (java.util.List)97 AggregateCall (org.apache.calcite.rel.core.AggregateCall)83 Pair (org.apache.calcite.util.Pair)79 Project (org.apache.calcite.rel.core.Project)77 RelBuilder (org.apache.calcite.tools.RelBuilder)77 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)66 ImmutableList (com.google.common.collect.ImmutableList)64 Map (java.util.Map)63