Search in sources :

Example 21 with SqlDynamicParam

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

the class HazelcastConcatWSOperator method checkOperandTypes.

@Override
public boolean checkOperandTypes(HazelcastCallBinding binding, boolean throwOnFailure) {
    HazelcastSqlValidator validator = binding.getValidator();
    if (binding.getOperandType(0).getSqlTypeName() != VARCHAR) {
        if (throwOnFailure) {
            throw binding.newValidationSignatureError();
        }
        return false;
    }
    for (int i = 1; i < binding.getOperandCount(); i++) {
        SqlNode operand = binding.operand(i);
        RelDataType operandType = binding.getOperandType(i);
        if (operandType.getSqlTypeName() != VARCHAR) {
            // Coerce everything to VARCHAR
            RelDataType newOperandType = HazelcastTypeUtils.createType(validator.getTypeFactory(), VARCHAR, operandType.isNullable());
            validator.getTypeCoercion().coerceOperandType(binding.getScope(), binding.getCall(), i, newOperandType);
        }
        // Set parameter converters
        if (operand.getKind() == SqlKind.DYNAMIC_PARAM) {
            int paramIndex = ((SqlDynamicParam) operand).getIndex();
            ParameterConverter paramConverter = new AnyToVarcharParameterConverter(paramIndex, operand.getParserPosition());
            validator.setParameterConverter(paramIndex, paramConverter);
        }
    }
    return true;
}
Also used : SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) ParameterConverter(com.hazelcast.sql.impl.ParameterConverter) AnyToVarcharParameterConverter(com.hazelcast.jet.sql.impl.validate.param.AnyToVarcharParameterConverter) AnyToVarcharParameterConverter(com.hazelcast.jet.sql.impl.validate.param.AnyToVarcharParameterConverter) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 22 with SqlDynamicParam

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

the class HazelcastComparisonPredicateUtils method setNumericParameterConverter.

private static void setNumericParameterConverter(HazelcastSqlValidator validator, SqlNode node, QueryDataType type) {
    if (node.getKind() == SqlKind.DYNAMIC_PARAM) {
        SqlDynamicParam node0 = (SqlDynamicParam) node;
        ParameterConverter converter = new NumericPrecedenceParameterConverter(node0.getIndex(), node.getParserPosition(), type);
        validator.setParameterConverter(node0.getIndex(), converter);
    }
}
Also used : SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) NumericPrecedenceParameterConverter(com.hazelcast.jet.sql.impl.validate.param.NumericPrecedenceParameterConverter) ParameterConverter(com.hazelcast.sql.impl.ParameterConverter) NumericPrecedenceParameterConverter(com.hazelcast.jet.sql.impl.validate.param.NumericPrecedenceParameterConverter)

Example 23 with SqlDynamicParam

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

the class HazelcastCastFunction method checkOperandTypes.

@Override
public boolean checkOperandTypes(HazelcastCallBinding binding, boolean throwOnFailure) {
    RelDataType sourceType = binding.getOperandType(0);
    RelDataType targetType = binding.getOperandType(1);
    SqlNode sourceOperand = binding.operand(0);
    if (sourceOperand.getKind() == SqlKind.DYNAMIC_PARAM) {
        int sourceParameterIndex = ((SqlDynamicParam) sourceOperand).getIndex();
        binding.getValidator().setParameterConverter(sourceParameterIndex, NoOpParameterConverter.INSTANCE);
    }
    if (canCast(sourceType, targetType)) {
        return true;
    }
    if (throwOnFailure) {
        SqlColumnType sourceType0 = toHazelcastType(sourceType).getTypeFamily().getPublicType();
        SqlColumnType targetType0 = toHazelcastType(targetType).getTypeFamily().getPublicType();
        throw binding.newError(HazelcastResources.RESOURCES.cannotCastValue(sourceType0.toString(), targetType0.toString()));
    } else {
        return false;
    }
}
Also used : SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlColumnType(com.hazelcast.sql.SqlColumnType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 24 with SqlDynamicParam

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

the class SqlValidatorImpl method inferUnknownTypes.

protected void inferUnknownTypes(@Nonnull RelDataType inferredType, @Nonnull SqlValidatorScope scope, @Nonnull SqlNode node) {
    Objects.requireNonNull(inferredType);
    Objects.requireNonNull(scope);
    Objects.requireNonNull(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, inferredType.getCharset(), inferredType.getCollation());
        }
        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().getList()) {
            inferUnknownTypes(whenType, scope, sqlNode);
        }
        RelDataType returnType = deriveType(scope, node);
        for (SqlNode sqlNode : caseCall.getThenOperands().getList()) {
            inferUnknownTypes(returnType, scope, sqlNode);
        }
        if (!SqlUtil.isNullLiteral(caseCall.getElseOperand(), false)) {
            inferUnknownTypes(returnType, scope, caseCall.getElseOperand());
        } else {
            setValidatedNodeType(caseCall.getElseOperand(), 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 25 with SqlDynamicParam

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

the class SqlValidatorImpl method getParameterRowType.

public RelDataType getParameterRowType(SqlNode sqlQuery) {
    // NOTE: We assume that bind variables occur in depth-first tree
    // traversal in the same order that they occurred in the SQL text.
    final List<RelDataType> types = new ArrayList<>();
    // NOTE: but parameters on fetch/offset would be counted twice
    // as they are counted in the SqlOrderBy call and the inner SqlSelect call
    final Set<SqlNode> alreadyVisited = new HashSet<>();
    sqlQuery.accept(new SqlShuttle() {

        @Override
        public SqlNode visit(SqlDynamicParam param) {
            if (alreadyVisited.add(param)) {
                RelDataType type = getValidatedNodeType(param);
                types.add(type);
            }
            return param;
        }
    });
    return typeFactory.createStructType(types, new AbstractList<String>() {

        @Override
        public String get(int index) {
            return "?" + index;
        }

        @Override
        public int size() {
            return types.size();
        }
    });
}
Also used : SqlShuttle(org.apache.calcite.sql.util.SqlShuttle) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlNode(org.apache.calcite.sql.SqlNode) HashSet(java.util.HashSet)

Aggregations

SqlDynamicParam (org.apache.calcite.sql.SqlDynamicParam)35 SqlNode (org.apache.calcite.sql.SqlNode)33 RelDataType (org.apache.calcite.rel.type.RelDataType)30 SqlNodeList (org.apache.calcite.sql.SqlNodeList)10 BitString (org.apache.calcite.util.BitString)10 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)9 SqlCall (org.apache.calcite.sql.SqlCall)7 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)5 SqlUpdate (org.apache.calcite.sql.SqlUpdate)5 SqlCase (org.apache.calcite.sql.fun.SqlCase)5 SqlOperandTypeInference (org.apache.calcite.sql.type.SqlOperandTypeInference)5 SqlShuttle (org.apache.calcite.sql.util.SqlShuttle)5 ParameterConverter (com.hazelcast.sql.impl.ParameterConverter)4 SqlValidatorImpl (org.apache.calcite.sql.validate.SqlValidatorImpl)4 SqlLiteral (org.apache.calcite.sql.SqlLiteral)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