Search in sources :

Example 6 with SqlValidatorImpl

use of org.apache.calcite.sql.validate.SqlValidatorImpl in project flink by apache.

the class SqlCastFunction method inferReturnType.

// ~ Methods ----------------------------------------------------------------
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    assert opBinding.getOperandCount() == 2;
    RelDataType ret = opBinding.getOperandType(1);
    RelDataType firstType = opBinding.getOperandType(0);
    ret = opBinding.getTypeFactory().createTypeWithNullability(ret, firstType.isNullable());
    if (opBinding instanceof SqlCallBinding) {
        SqlCallBinding callBinding = (SqlCallBinding) opBinding;
        SqlNode operand0 = callBinding.operand(0);
        // to them using the type they are casted to.
        if (((operand0 instanceof SqlLiteral) && (((SqlLiteral) operand0).getValue() == null)) || (operand0 instanceof SqlDynamicParam)) {
            final SqlValidatorImpl validator = (SqlValidatorImpl) callBinding.getValidator();
            validator.setValidatedNodeType(operand0, ret);
        }
    }
    return ret;
}
Also used : SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 7 with SqlValidatorImpl

use of org.apache.calcite.sql.validate.SqlValidatorImpl in project flink by apache.

the class FlinkConvertletTable method convertTryCast.

// Slightly modified version of StandardConvertletTable::convertCast
private RexNode convertTryCast(SqlRexContext cx, final SqlCall call) {
    RelDataTypeFactory typeFactory = cx.getTypeFactory();
    final SqlNode leftNode = call.operand(0);
    final SqlNode rightNode = call.operand(1);
    final RexNode valueRex = cx.convertExpression(leftNode);
    RelDataType type;
    if (rightNode instanceof SqlIntervalQualifier) {
        type = typeFactory.createSqlIntervalType((SqlIntervalQualifier) rightNode);
    } else if (rightNode instanceof SqlDataTypeSpec) {
        SqlDataTypeSpec dataType = ((SqlDataTypeSpec) rightNode);
        type = dataType.deriveType(cx.getValidator());
        if (type == null) {
            type = cx.getValidator().getValidatedNodeType(dataType.getTypeName());
        }
    } else {
        throw new IllegalStateException("Invalid right argument type for TRY_CAST: " + rightNode);
    }
    type = typeFactory.createTypeWithNullability(type, true);
    if (SqlUtil.isNullLiteral(leftNode, false)) {
        final SqlValidatorImpl validator = (SqlValidatorImpl) cx.getValidator();
        validator.setValidatedNodeType(leftNode, type);
        return cx.convertExpression(leftNode);
    }
    return cx.getRexBuilder().makeCall(type, FlinkSqlOperatorTable.TRY_CAST, Collections.singletonList(valueRex));
}
Also used : SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 8 with SqlValidatorImpl

use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.

the class SqlOperator method constructArgTypeList.

protected List<RelDataType> constructArgTypeList(SqlValidator validator, SqlValidatorScope scope, SqlCall call, List<SqlNode> args, boolean convertRowArgToColumnList) {
    // Scope for operands. Usually the same as 'scope'.
    final SqlValidatorScope operandScope = scope.getOperandScope(call);
    final ImmutableList.Builder<RelDataType> argTypeBuilder = ImmutableList.builder();
    for (SqlNode operand : args) {
        RelDataType nodeType;
        // for sure that the row argument maps to a ColumnList type
        if (operand.getKind() == SqlKind.ROW && convertRowArgToColumnList) {
            RelDataTypeFactory typeFactory = validator.getTypeFactory();
            nodeType = typeFactory.createSqlType(SqlTypeName.COLUMN_LIST);
            ((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
        } else {
            nodeType = validator.deriveType(operandScope, operand);
        }
        argTypeBuilder.add(nodeType);
    }
    return argTypeBuilder.build();
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) ImmutableList(com.google.common.collect.ImmutableList) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 9 with SqlValidatorImpl

use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.

the class SqlOperator method validateOperands.

/**
 * Validates the operands of a call, inferring the return type in the
 * process.
 *
 * @param validator active validator
 * @param scope     validation scope
 * @param call      call to be validated
 * @return inferred type
 */
public final RelDataType validateOperands(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
    // Let subclasses know what's up.
    preValidateCall(validator, scope, call);
    // Check the number of operands
    checkOperandCount(validator, operandTypeChecker, call);
    SqlCallBinding opBinding = new SqlCallBinding(validator, scope, call);
    checkOperandTypes(opBinding, true);
    // Now infer the result type.
    RelDataType ret = inferReturnType(opBinding);
    ((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
    return ret;
}
Also used : SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 10 with SqlValidatorImpl

use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.

the class SqlOverOperator method deriveType.

public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
    // Validate type of the inner aggregate call
    validateOperands(validator, scope, call);
    // Assume the first operand is an aggregate call and derive its type.
    // When we are sure the window is not empty, pass that information to the
    // aggregate's operator return type inference as groupCount=1
    // Otherwise pass groupCount=0 so the agg operator understands the window
    // can be empty
    SqlNode agg = call.operand(0);
    if (!(agg instanceof SqlCall)) {
        throw new IllegalStateException("Argument to SqlOverOperator" + " should be SqlCall, got " + agg.getClass() + ": " + agg);
    }
    SqlNode window = call.operand(1);
    SqlWindow w = validator.resolveWindow(window, scope, false);
    final int groupCount = w.isAlwaysNonEmpty() ? 1 : 0;
    final SqlCall aggCall = (SqlCall) agg;
    SqlCallBinding opBinding = new SqlCallBinding(validator, scope, aggCall) {

        @Override
        public int getGroupCount() {
            return groupCount;
        }
    };
    RelDataType ret = aggCall.getOperator().inferReturnType(opBinding);
    // Copied from validateOperands
    ((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
    ((SqlValidatorImpl) validator).setValidatedNodeType(agg, ret);
    return ret;
}
Also used : SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

SqlValidatorImpl (org.apache.calcite.sql.validate.SqlValidatorImpl)13 RelDataType (org.apache.calcite.rel.type.RelDataType)11 SqlNode (org.apache.calcite.sql.SqlNode)6 ArrayList (java.util.ArrayList)4 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)4 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)4 SqlNodeList (org.apache.calcite.sql.SqlNodeList)4 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 RexNode (org.apache.calcite.rex.RexNode)2 SqlCall (org.apache.calcite.sql.SqlCall)2 SqlDynamicParam (org.apache.calcite.sql.SqlDynamicParam)2 SqlLiteral (org.apache.calcite.sql.SqlLiteral)2 SqlValidatorScope (org.apache.calcite.sql.validate.SqlValidatorScope)2 SQLException (java.sql.SQLException)1 AbstractList (java.util.AbstractList)1 Strong (org.apache.calcite.plan.Strong)1 RelNode (org.apache.calcite.rel.RelNode)1 RelRoot (org.apache.calcite.rel.RelRoot)1 Collect (org.apache.calcite.rel.core.Collect)1