private RexNode castNullLiteralIfNeeded(RexNode node, RelDataType type) { if (!RexLiteral.isNullLiteral(node)) { return node; } return rexBuilder.makeCast(type, node); }
private RexNode castNullLiteralIfNeeded(RexNode node, RelDataType type) { if (!RexLiteral.isNullLiteral(node)) { return node; } return rexBuilder.makeCast(type, node); }
private RexNode castNullLiteralIfNeeded(RexNode node, RelDataType type) { if (!RexLiteral.isNullLiteral(node)) { return node; } return rexBuilder.makeCast(type, node); }
/** * Ensures expression is interpreted as a specified type. The returned * expression may be wrapped with a cast. * * @param type desired type * @param node expression * @param matchNullability whether to correct nullability of specified * type to match the expression; this usually should be true, except for * explicit casts which can override default nullability * * @return a casted expression or the original expression */ public RexNode ensureType( RelDataType type, RexNode node, boolean matchNullability) { RelDataType targetType = type; if (matchNullability) { targetType = matchNullability(type, node); } if (!node.getType().equals(targetType)) { return makeCast(targetType, node); } return node; }
/** * Ensures expression is interpreted as a specified type. The returned * expression may be wrapped with a cast. * * @param type desired type * @param node expression * @param matchNullability whether to correct nullability of specified * type to match the expression; this usually should * be true, except for explicit casts which can * override default nullability * @return a casted expression or the original expression */ public RexNode ensureType( RelDataType type, RexNode node, boolean matchNullability) { RelDataType targetType = type; if (matchNullability) { targetType = matchNullability(type, node); } if (!node.getType().equals(targetType)) { return makeCast(targetType, node); } return node; }
/** * Generates a cast for a row type. * * @param rexBuilder RexBuilder to use for constructing casts * @param lhsRowType target row type * @param rhsExps expressions to be cast * @return cast expressions */ public static List<RexNode> generateCastExpressions( RexBuilder rexBuilder, RelDataType lhsRowType, List<RexNode> rhsExps) { List<RelDataTypeField> lhsFields = lhsRowType.getFieldList(); List<RexNode> castExps = new ArrayList<RexNode>(); for (Pair<RelDataTypeField, RexNode> pair : Pair.zip(lhsFields, rhsExps, true)) { RelDataTypeField lhsField = pair.left; RelDataType lhsType = lhsField.getType(); final RexNode rhsExp = pair.right; RelDataType rhsType = rhsExp.getType(); if (lhsType.equals(rhsType)) { castExps.add(rhsExp); } else { castExps.add(rexBuilder.makeCast(lhsType, rhsExp)); } } return castExps; }
castExps.add(rhsExp); } else { castExps.add(rexBuilder.makeCast(lhsType, rhsExp));
@Override public RexNode visitCall(final RexCall call) { int i = reducibleExps.indexOf(call); if (i == -1) { return super.visitCall(call); } RexNode replacement = reducedValues.get(i); if (addCasts.get(i) && (replacement.getType() != call.getType())) { // Handle change from nullable to NOT NULL by claiming // that the result is still nullable, even though // we know it isn't. // // Also, we cannot reduce CAST('abc' AS VARCHAR(4)) to 'abc'. // If we make 'abc' of type VARCHAR(4), we may later encounter // the same expression in a ProjectRel's digest where it has // type VARCHAR(3), and that's wrong. replacement = rexBuilder.makeCast( call.getType(), replacement); } return replacement; } }
/** * Creates an expression corresponding to a null literal, cast to a specific * type and precision * * @param typeName name of the type that the null will be cast to * @param precision precision of the type * @return created expression */ public RexNode makeNullLiteral(SqlTypeName typeName, int precision) { RelDataType type = typeFactory.createTypeWithNullability( typeFactory.createSqlType(typeName, precision), true); return makeCast(type, constantNull()); }
/** * Creates a literal whose value is NULL, with a particular type. * * <p>The typing is necessary because RexNodes are strictly typed. For * example, in the Rex world the <code>NULL</code> parameter to <code> * SUBSTRING(NULL FROM 2 FOR 4)</code> must have a valid VARCHAR type so * that the result type can be determined. * * @param typeName Type to cast NULL to * @return NULL literal of given type */ public RexNode makeNullLiteral(SqlTypeName typeName) { RelDataType type = typeFactory.createTypeWithNullability( typeFactory.createSqlType(typeName), true); return makeCast(type, constantNull()); }
/** * Creates an expression corresponding to a null literal, cast to a specific * type and precision * * @param typeName name of the type that the null will be cast to * @param precision precision of the type * * @return created expression */ public RexNode makeNullLiteral(SqlTypeName typeName, int precision) { RelDataType type = typeFactory.createTypeWithNullability( typeFactory.createSqlType(typeName, precision), true); return makeCast( type, constantNull()); }
private void flattenNullLiteral( RelDataType type, List<RexNode> flattenedExps, List<String> flattenedFieldNames) { RelDataType flattenedType = SqlTypeUtil.flattenRecordType( rexBuilder.getTypeFactory(), type, null); for (RelDataTypeField field : flattenedType.getFieldList()) { flattenedExps.add( rexBuilder.makeCast( field.getType(), rexBuilder.constantNull())); flattenedFieldNames.add(field.getName()); } }
private void flattenNullLiteral( RelDataType type, List<RexNode> flattenedExps, List<String> flattenedFieldNames) { RelDataType flattenedType = SqlTypeUtil.flattenRecordType( rexBuilder.getTypeFactory(), type, null); for (RelDataTypeField field : flattenedType.getFieldList()) { flattenedExps.add( rexBuilder.makeCast( field.getType(), rexBuilder.constantNull())); flattenedFieldNames.add(field.getName()); } }
/** * Creates a literal whose value is NULL, with a particular type. * * <p>The typing is necessary because RexNodes are strictly typed. For * example, in the Rex world the <code>NULL</code> parameter to <code> * SUBSTRING(NULL FROM 2 FOR 4)</code> must have a valid VARCHAR type so * that the result type can be determined. * * @param typeName Type to cast NULL to * * @return NULL literal of given type */ public RexNode makeNullLiteral(SqlTypeName typeName) { RelDataType type = typeFactory.createTypeWithNullability( typeFactory.createSqlType(typeName), true); return makeCast( type, constantNull()); }
/** * Casts a RexNode value to the validated type of a SqlCall. If the value * was already of the validated type, then the value is returned without an * additional cast. */ public RexNode castToValidatedType( SqlRexContext cx, SqlCall call, RexNode value) { final RelDataType resType = cx.getValidator().getValidatedNodeType(call); if (value.getType() == resType) { return value; } return cx.getRexBuilder().makeCast(resType, value); }
/** * Casts a RexNode value to the validated type of a SqlCall. If the value * was already of the validated type, then the value is returned without an * additional cast. */ public RexNode castToValidatedType( SqlRexContext cx, SqlCall call, RexNode value) { final RelDataType resType = cx.getValidator().getValidatedNodeType(call); if (value.getType() == resType) { return value; } return cx.getRexBuilder().makeCast(resType, value); }
public RexNode visitCall(RexCall rexCall) { if (rexCall.isA(RexKind.Cast)) { RexNode input = rexCall.getOperands().get(0).accept(this); RelDataType targetType = removeDistinct(rexCall.getType()); return rexBuilder.makeCast( targetType, input); } if (!rexCall.isA(RexKind.Comparison)) { return super.visitCall(rexCall); } RexNode lhs = rexCall.getOperands().get(0); if (!lhs.getType().isStruct()) { // NOTE jvs 9-Mar-2005: Calls like IS NULL operate // on the representative null indicator. Since it comes // first, we don't have to do any special translation. return super.visitCall(rexCall); } // NOTE jvs 22-Mar-2005: Likewise, the null indicator takes // care of comparison null semantics without any special casing. return flattenComparison( rexBuilder, rexCall.getOperator(), rexCall.getOperands()); }
public RexNode visitCall(RexCall rexCall) { if (rexCall.isA(SqlKind.CAST)) { RexNode input = rexCall.getOperands().get(0).accept(this); RelDataType targetType = removeDistinct(rexCall.getType()); return rexBuilder.makeCast( targetType, input); } if (!rexCall.isA(SqlKind.COMPARISON)) { return super.visitCall(rexCall); } RexNode lhs = rexCall.getOperands().get(0); if (!lhs.getType().isStruct()) { // NOTE jvs 9-Mar-2005: Calls like IS NULL operate // on the representative null indicator. Since it comes // first, we don't have to do any special translation. return super.visitCall(rexCall); } // NOTE jvs 22-Mar-2005: Likewise, the null indicator takes // care of comparison null semantics without any special casing. return flattenComparison( rexBuilder, rexCall.getOperator(), rexCall.getOperands()); }
rexBuilder.makeCast( structuredType, rexBuilder.constantNull());