Search in sources :

Example 6 with HazelcastCallBinding

use of com.hazelcast.jet.sql.impl.validate.HazelcastCallBinding in project hazelcast by hazelcast.

the class CoalesceOperandTypeInference method inferOperandTypes.

@Override
public void inferOperandTypes(SqlCallBinding binding, RelDataType returnType, RelDataType[] operandTypes) {
    if (operandTypes.length == 0) {
        // will be handled later
        return;
    }
    assert binding.getOperandCount() == operandTypes.length;
    boolean hasParameters = HazelcastTypeUtils.hasParameters(binding);
    List<Integer> unknownOperandIndexes = new ArrayList<>();
    RelDataType knownType = null;
    int knownTypePrecedence = Integer.MIN_VALUE;
    for (int i = 0; i < binding.getOperandCount(); i++) {
        operandTypes[i] = binding.getOperandType(i);
        if (!operandTypes[i].equals(binding.getValidator().getUnknownType())) {
            if (hasParameters && toHazelcastType(operandTypes[i]).getTypeFamily().isNumericInteger()) {
                // If there are parameters in the operands, widen all the integers to BIGINT so that an expression
                // `COALESCE(1, ?)` is resolved to `COALESCE((BIGINT)1, (BIGINT)?)` rather than
                // `COALESCE((TINYINT)1, (TINYINT)?)`
                operandTypes[i] = createType(binding.getTypeFactory(), SqlTypeName.BIGINT, operandTypes[i].isNullable());
            }
            int precedence = precedenceOf(operandTypes[i]);
            if (knownTypePrecedence < precedence) {
                knownType = operandTypes[i];
                knownTypePrecedence = precedence;
            }
        } else {
            unknownOperandIndexes.add(i);
        }
    }
    // since we cannot deduce the return type
    if (knownType == null || knownType.getSqlTypeName() == SqlTypeName.NULL && hasParameters) {
        throw new HazelcastCallBinding(binding).newValidationSignatureError();
    }
    for (Integer index : unknownOperandIndexes) {
        operandTypes[index] = knownType;
    }
}
Also used : HazelcastCallBinding(com.hazelcast.jet.sql.impl.validate.HazelcastCallBinding) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

HazelcastCallBinding (com.hazelcast.jet.sql.impl.validate.HazelcastCallBinding)6 RelDataType (org.apache.calcite.rel.type.RelDataType)5 ArrayList (java.util.ArrayList)2 HazelcastSqlValidator (com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator)1 NamedOperandCheckerProgram (com.hazelcast.jet.sql.impl.validate.operand.NamedOperandCheckerProgram)1 OperandChecker (com.hazelcast.jet.sql.impl.validate.operand.OperandChecker)1 OperandCheckerProgram (com.hazelcast.jet.sql.impl.validate.operand.OperandCheckerProgram)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 ExplicitOperatorBinding (org.apache.calcite.sql.ExplicitOperatorBinding)1 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)1 SqlLiteral (org.apache.calcite.sql.SqlLiteral)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1 ComparableOperandTypeChecker (org.apache.calcite.sql.type.ComparableOperandTypeChecker)1