Search in sources :

Example 1 with HazelcastTypeUtils

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

the class HazelcastCaseOperator method checkOperandTypes.

@Override
@SuppressWarnings("checkstyle:MethodLength")
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
    HazelcastSqlValidator validator = (HazelcastSqlValidator) callBinding.getValidator();
    HazelcastSqlCase sqlCall = (HazelcastSqlCase) callBinding.getCall();
    // at this point `CASE x WHEN y ...` is already converted to `CASE WHEN x=y ...`
    assert sqlCall.getValueOperand() == null;
    SqlNodeList whenList = sqlCall.getWhenOperands();
    SqlNodeList thenList = sqlCall.getThenOperands();
    SqlNode elseOperand = sqlCall.getElseOperand();
    assert whenList.size() > 0 : "no WHEN clause";
    assert whenList.size() == thenList.size();
    SqlValidatorScope scope = callBinding.getScope();
    if (!typeCheckWhen(scope, validator, whenList)) {
        if (throwOnFailure) {
            throw callBinding.newError(RESOURCE.expectedBoolean());
        }
        return false;
    }
    List<RelDataType> argTypes = new ArrayList<>(thenList.size() + 1);
    for (SqlNode node : thenList) {
        argTypes.add(validator.deriveType(scope, node));
    }
    argTypes.add(validator.deriveType(scope, elseOperand));
    // noinspection OptionalGetWithoutIsPresent
    RelDataType caseReturnType = argTypes.stream().reduce(HazelcastTypeUtils::withHigherPrecedence).get();
    Supplier<CalciteContextException> exceptionSupplier = () -> validator.newValidationError(sqlCall, HazelcastResources.RESOURCES.cannotInferCaseResult(argTypes.toString(), "CASE"));
    for (int i = 0; i < thenList.size(); i++) {
        int finalI = i;
        if (!coerceItem(validator, scope, thenList.get(i), caseReturnType, sqlNode -> thenList.getList().set(finalI, sqlNode), throwOnFailure, exceptionSupplier)) {
            return false;
        }
    }
    return coerceItem(validator, scope, elseOperand, caseReturnType, sqlNode -> sqlCall.setOperand(ELSE_BRANCH_OPERAND_INDEX, sqlNode), throwOnFailure, exceptionSupplier);
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlSyntax(org.apache.calcite.sql.SqlSyntax) SqlCaseOperator(org.apache.calcite.sql.fun.SqlCaseOperator) CalciteContextException(org.apache.calcite.runtime.CalciteContextException) HazelcastReturnTypeInference.wrap(com.hazelcast.jet.sql.impl.validate.operators.typeinference.HazelcastReturnTypeInference.wrap) SqlOperandCountRanges(org.apache.calcite.sql.type.SqlOperandCountRanges) SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) HazelcastResources(com.hazelcast.jet.sql.impl.validate.HazelcastResources) Supplier(java.util.function.Supplier) RESOURCE(org.apache.calcite.util.Static.RESOURCE) ArrayList(java.util.ArrayList) SqlCall(org.apache.calcite.sql.SqlCall) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode) Pair(org.apache.calcite.util.Pair) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlOperatorBinding(org.apache.calcite.sql.SqlOperatorBinding) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) SqlKind(org.apache.calcite.sql.SqlKind) SqlWriter(org.apache.calcite.sql.SqlWriter) HazelcastTypeUtils(com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlTypeUtil(org.apache.calcite.sql.type.SqlTypeUtil) HazelcastSqlCase(com.hazelcast.jet.sql.impl.validate.operators.special.HazelcastSqlCase) Consumer(java.util.function.Consumer) SqlReturnTypeInference(org.apache.calcite.sql.type.SqlReturnTypeInference) List(java.util.List) SqlOperandCountRange(org.apache.calcite.sql.SqlOperandCountRange) SqlNodeList(org.apache.calcite.sql.SqlNodeList) CalciteContextException(org.apache.calcite.runtime.CalciteContextException) HazelcastSqlCase(com.hazelcast.jet.sql.impl.validate.operators.special.HazelcastSqlCase) ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

HazelcastResources (com.hazelcast.jet.sql.impl.validate.HazelcastResources)1 HazelcastSqlValidator (com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator)1 HazelcastSqlCase (com.hazelcast.jet.sql.impl.validate.operators.special.HazelcastSqlCase)1 HazelcastReturnTypeInference.wrap (com.hazelcast.jet.sql.impl.validate.operators.typeinference.HazelcastReturnTypeInference.wrap)1 HazelcastTypeUtils (com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 CalciteContextException (org.apache.calcite.runtime.CalciteContextException)1 SqlCall (org.apache.calcite.sql.SqlCall)1 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)1 SqlKind (org.apache.calcite.sql.SqlKind)1 SqlLiteral (org.apache.calcite.sql.SqlLiteral)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1 SqlOperandCountRange (org.apache.calcite.sql.SqlOperandCountRange)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 SqlOperatorBinding (org.apache.calcite.sql.SqlOperatorBinding)1