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"));
}
}
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);
}
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;
}
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);
}
}
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;
}
}
Aggregations