Search in sources :

Example 6 with SqlDynamicParam

use of org.apache.calcite.sql.SqlDynamicParam in project hazelcast by hazelcast.

the class HazelcastIsTrueFalseNullPredicate method checkOperandTypes.

@Override
public boolean checkOperandTypes(HazelcastCallBinding binding, boolean throwOnFailure) {
    if (objectOperand) {
        // IS (NOT) NULL accept any operand types
        SqlNode node = binding.operand(0);
        if (node.getKind() == SqlKind.DYNAMIC_PARAM) {
            int parameterIndex = ((SqlDynamicParam) node).getIndex();
            binding.getValidator().setParameterConverter(parameterIndex, NoOpParameterConverter.INSTANCE);
        }
        return true;
    } else {
        // IS (NOT) TRUE|FALSE accept boolean operands only
        return TypedOperandChecker.BOOLEAN.check(binding, throwOnFailure, 0);
    }
}
Also used : SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlNode(org.apache.calcite.sql.SqlNode)

Example 7 with SqlDynamicParam

use of org.apache.calcite.sql.SqlDynamicParam in project calcite by apache.

the class SqlCastFunction method inferReturnType.

// ~ Methods ----------------------------------------------------------------
@Override
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 (SqlUtil.isNullLiteral(operand0, false) || (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) SqlNode(org.apache.calcite.sql.SqlNode)

Example 8 with SqlDynamicParam

use of org.apache.calcite.sql.SqlDynamicParam in project calcite by apache.

the class SqlCastFunction method checkOperandTypes.

/**
 * Makes sure that the number and types of arguments are allowable.
 * Operators (such as "ROW" and "AS") which do not check their arguments can
 * override this method.
 */
@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
    final SqlNode left = callBinding.operand(0);
    final SqlNode right = callBinding.operand(1);
    if (SqlUtil.isNullLiteral(left, false) || left instanceof SqlDynamicParam) {
        return true;
    }
    RelDataType validatedNodeType = callBinding.getValidator().getValidatedNodeType(left);
    RelDataType returnType = SqlTypeUtil.deriveType(callBinding, right);
    if (!SqlTypeUtil.canCastFrom(returnType, validatedNodeType, true)) {
        if (throwOnFailure) {
            throw callBinding.newError(RESOURCE.cannotCastValue(validatedNodeType.toString(), returnType.toString()));
        }
        return false;
    }
    if (SqlTypeUtil.areCharacterSetsMismatched(validatedNodeType, returnType)) {
        if (throwOnFailure) {
            // set mismatch.
            throw callBinding.newError(RESOURCE.cannotCastValue(validatedNodeType.getFullTypeString(), returnType.getFullTypeString()));
        }
        return false;
    }
    return true;
}
Also used : SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 9 with SqlDynamicParam

use of org.apache.calcite.sql.SqlDynamicParam in project calcite by apache.

the class SqlValidatorImpl method inferUnknownTypes.

protected void inferUnknownTypes(RelDataType inferredType, SqlValidatorScope scope, SqlNode node) {
    requireNonNull(inferredType, "inferredType");
    requireNonNull(scope, "scope");
    requireNonNull(node, "node");
    final SqlValidatorScope newScope = scopes.get(node);
    if (newScope != null) {
        scope = newScope;
    }
    boolean isNullLiteral = SqlUtil.isNullLiteral(node, false);
    if ((node instanceof SqlDynamicParam) || isNullLiteral) {
        if (inferredType.equals(unknownType)) {
            if (isNullLiteral) {
                if (config.typeCoercionEnabled()) {
                    // derive type of null literal
                    deriveType(scope, node);
                    return;
                } else {
                    throw newValidationError(node, RESOURCE.nullIllegal());
                }
            } else {
                throw newValidationError(node, RESOURCE.dynamicParamIllegal());
            }
        }
        // REVIEW:  should dynamic parameter types always be nullable?
        RelDataType newInferredType = typeFactory.createTypeWithNullability(inferredType, true);
        if (SqlTypeUtil.inCharFamily(inferredType)) {
            newInferredType = typeFactory.createTypeWithCharsetAndCollation(newInferredType, getCharset(inferredType), getCollation(inferredType));
        }
        setValidatedNodeType(node, newInferredType);
    } else if (node instanceof SqlNodeList) {
        SqlNodeList nodeList = (SqlNodeList) node;
        if (inferredType.isStruct()) {
            if (inferredType.getFieldCount() != nodeList.size()) {
                // bust out, and the error will be detected higher up
                return;
            }
        }
        int i = 0;
        for (SqlNode child : nodeList) {
            RelDataType type;
            if (inferredType.isStruct()) {
                type = inferredType.getFieldList().get(i).getType();
                ++i;
            } else {
                type = inferredType;
            }
            inferUnknownTypes(type, scope, child);
        }
    } else if (node instanceof SqlCase) {
        final SqlCase caseCall = (SqlCase) node;
        final RelDataType whenType = caseCall.getValueOperand() == null ? booleanType : unknownType;
        for (SqlNode sqlNode : caseCall.getWhenOperands()) {
            inferUnknownTypes(whenType, scope, sqlNode);
        }
        RelDataType returnType = deriveType(scope, node);
        for (SqlNode sqlNode : caseCall.getThenOperands()) {
            inferUnknownTypes(returnType, scope, sqlNode);
        }
        SqlNode elseOperand = requireNonNull(caseCall.getElseOperand(), () -> "elseOperand for " + caseCall);
        if (!SqlUtil.isNullLiteral(elseOperand, false)) {
            inferUnknownTypes(returnType, scope, elseOperand);
        } else {
            setValidatedNodeType(elseOperand, returnType);
        }
    } else if (node.getKind() == SqlKind.AS) {
        // For AS operator, only infer the operand not the alias
        inferUnknownTypes(inferredType, scope, ((SqlCall) node).operand(0));
    } else if (node instanceof SqlCall) {
        final SqlCall call = (SqlCall) node;
        final SqlOperandTypeInference operandTypeInference = call.getOperator().getOperandTypeInference();
        final SqlCallBinding callBinding = new SqlCallBinding(this, scope, call);
        final List<SqlNode> operands = callBinding.operands();
        final RelDataType[] operandTypes = new RelDataType[operands.size()];
        Arrays.fill(operandTypes, unknownType);
        // instead; for now just eat it
        if (operandTypeInference != null) {
            operandTypeInference.inferOperandTypes(callBinding, inferredType, operandTypes);
        }
        for (int i = 0; i < operands.size(); ++i) {
            final SqlNode operand = operands.get(i);
            if (operand != null) {
                inferUnknownTypes(operandTypes[i], scope, operand);
            }
        }
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlOperandTypeInference(org.apache.calcite.sql.type.SqlOperandTypeInference) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlCase(org.apache.calcite.sql.fun.SqlCase) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 10 with SqlDynamicParam

use of org.apache.calcite.sql.SqlDynamicParam in project calcite by apache.

the class SqlValidatorImpl method checkTypeAssignment.

/**
 * Checks the type assignment of an INSERT or UPDATE query.
 *
 * <p>Skip the virtual columns(can not insert into) type assignment
 * check if the source fields count equals with
 * the real target table fields count, see how #checkFieldCount was used.
 *
 * @param sourceScope   Scope of query source which is used to infer node type
 * @param table         Target table
 * @param sourceRowType Source row type
 * @param targetRowType Target row type, it should either contain all the virtual columns
 *                      (can not insert into) or exclude all the virtual columns
 * @param query The query
 */
protected void checkTypeAssignment(@Nullable SqlValidatorScope sourceScope, SqlValidatorTable table, RelDataType sourceRowType, RelDataType targetRowType, final SqlNode query) {
    // NOTE jvs 23-Feb-2006: subclasses may allow for extra targets
    // representing system-maintained columns, so stop after all sources
    // matched
    boolean isUpdateModifiableViewTable = false;
    if (query instanceof SqlUpdate) {
        final SqlNodeList targetColumnList = ((SqlUpdate) query).getTargetColumnList();
        if (targetColumnList != null) {
            final int targetColumnCnt = targetColumnList.size();
            targetRowType = SqlTypeUtil.extractLastNFields(typeFactory, targetRowType, targetColumnCnt);
            sourceRowType = SqlTypeUtil.extractLastNFields(typeFactory, sourceRowType, targetColumnCnt);
        }
        isUpdateModifiableViewTable = table.unwrap(ModifiableViewTable.class) != null;
    }
    if (SqlTypeUtil.equalAsStructSansNullability(typeFactory, sourceRowType, targetRowType, null)) {
        // Returns early if source and target row type equals sans nullability.
        return;
    }
    if (config.typeCoercionEnabled() && !isUpdateModifiableViewTable) {
        // Try type coercion first if implicit type coercion is allowed.
        boolean coerced = typeCoercion.querySourceCoercion(sourceScope, sourceRowType, targetRowType, query);
        if (coerced) {
            return;
        }
    }
    // Fall back to default behavior: compare the type families.
    List<RelDataTypeField> sourceFields = sourceRowType.getFieldList();
    List<RelDataTypeField> targetFields = targetRowType.getFieldList();
    final int sourceCount = sourceFields.size();
    for (int i = 0; i < sourceCount; ++i) {
        RelDataType sourceType = sourceFields.get(i).getType();
        RelDataType targetType = targetFields.get(i).getType();
        if (!SqlTypeUtil.canAssignFrom(targetType, sourceType)) {
            SqlNode node = getNthExpr(query, i, sourceCount);
            if (node instanceof SqlDynamicParam) {
                continue;
            }
            String targetTypeString;
            String sourceTypeString;
            if (SqlTypeUtil.areCharacterSetsMismatched(sourceType, targetType)) {
                sourceTypeString = sourceType.getFullTypeString();
                targetTypeString = targetType.getFullTypeString();
            } else {
                sourceTypeString = sourceType.toString();
                targetTypeString = targetType.toString();
            }
            throw newValidationError(node, RESOURCE.typeNotAssignable(targetFields.get(i).getName(), targetTypeString, sourceFields.get(i).getName(), sourceTypeString));
        }
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlUpdate(org.apache.calcite.sql.SqlUpdate) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlDynamicParam (org.apache.calcite.sql.SqlDynamicParam)30 SqlNode (org.apache.calcite.sql.SqlNode)28 RelDataType (org.apache.calcite.rel.type.RelDataType)25 SqlNodeList (org.apache.calcite.sql.SqlNodeList)8 BitString (org.apache.calcite.util.BitString)8 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)7 SqlCall (org.apache.calcite.sql.SqlCall)6 ParameterConverter (com.hazelcast.sql.impl.ParameterConverter)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 SqlUpdate (org.apache.calcite.sql.SqlUpdate)4 SqlCase (org.apache.calcite.sql.fun.SqlCase)4 SqlOperandTypeInference (org.apache.calcite.sql.type.SqlOperandTypeInference)4 SqlShuttle (org.apache.calcite.sql.util.SqlShuttle)4 SqlValidatorImpl (org.apache.calcite.sql.validate.SqlValidatorImpl)3 HazelcastSqlValidator (com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator)2 AnyToVarcharParameterConverter (com.hazelcast.jet.sql.impl.validate.param.AnyToVarcharParameterConverter)2 NumericPrecedenceParameterConverter (com.hazelcast.jet.sql.impl.validate.param.NumericPrecedenceParameterConverter)2 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)2