Search in sources :

Example 1 with HazelcastSqlValidator

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

the class WindowOperandMetadata method checkOperandTypes.

private boolean checkOperandTypes(HazelcastCallBinding binding, boolean throwOnFailure, int columnIndex) {
    SqlNode lag = binding.operand(columnIndex);
    HazelcastSqlValidator validator = binding.getValidator();
    SqlTypeName orderingColumnType = getOrderingColumnType(binding, 1).getSqlTypeName();
    boolean result;
    SqlTypeName lagType = ((SqlValidator) validator).getValidatedNodeType(lag).getSqlTypeName();
    if (SqlTypeName.INT_TYPES.contains(orderingColumnType)) {
        result = SqlTypeName.INT_TYPES.contains(lagType);
    } else if (SqlTypeName.DATETIME_TYPES.contains(orderingColumnType)) {
        result = lagType.getFamily() == SqlTypeFamily.INTERVAL_DAY_TIME;
    } else {
        result = false;
    }
    if (!result && throwOnFailure) {
        QueryDataTypeFamily hzOrderingColumnType = toHazelcastTypeFromSqlTypeName(orderingColumnType).getTypeFamily();
        QueryDataTypeFamily hzLagType = toHazelcastTypeFromSqlTypeName(lagType).getTypeFamily();
        throw validator.newValidationError(binding.getCall(), RESOURCES.windowFunctionTypeMismatch(hzOrderingColumnType.toString(), hzLagType.toString()));
    }
    return result;
}
Also used : QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) HazelcastTypeUtils.toHazelcastTypeFromSqlTypeName(com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils.toHazelcastTypeFromSqlTypeName) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with HazelcastSqlValidator

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

the class AbstractOperandChecker method check.

@Override
public boolean check(HazelcastCallBinding callBinding, boolean throwOnFailure, int operandIndex) {
    HazelcastSqlValidator validator = callBinding.getValidator();
    SqlNode operand = callBinding.getCall().operand(operandIndex);
    if (operand.getKind() == SqlKind.DYNAMIC_PARAM) {
        validateDynamicParam((SqlDynamicParam) operand, validator);
        return true;
    }
    if (operand.getKind() == SqlKind.ARGUMENT_ASSIGNMENT) {
        SqlNode value = ((SqlCall) operand).operand(0);
        if (value.getKind() == SqlKind.DYNAMIC_PARAM) {
            validateDynamicParam((SqlDynamicParam) value, validator);
            return true;
        }
    }
    RelDataType operandType = validator.deriveType(callBinding.getScope(), operand);
    assert operandType.getSqlTypeName() != SqlTypeName.NULL : "Operand type is not resolved";
    // Handle type match
    if (matchesTargetType(operandType)) {
        return true;
    }
    // Handle coercion if possible
    if (coerce(validator, callBinding, operand, operandType, operandIndex)) {
        return true;
    }
    // Failed
    if (throwOnFailure) {
        throw callBinding.newValidationSignatureError();
    } else {
        return false;
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with HazelcastSqlValidator

use of com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator 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)

Example 4 with HazelcastSqlValidator

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

the class HazelcastOperandTypeCheckerAware method prepareBinding.

default HazelcastCallBinding prepareBinding(SqlCallBinding binding, SqlOperandTypeInference operandTypeInference) {
    HazelcastSqlValidator validator = (HazelcastSqlValidator) binding.getValidator();
    boolean resolveOperands = false;
    for (int i = 0; i < binding.getOperandCount(); i++) {
        RelDataType operandType = binding.getOperandType(i);
        if (operandType.getSqlTypeName() == SqlTypeName.NULL) {
            resolveOperands = true;
            break;
        }
    }
    if (resolveOperands) {
        RelDataType unknownType = binding.getValidator().getUnknownType();
        RelDataType[] operandTypes = new RelDataType[binding.getOperandCount()];
        Arrays.fill(operandTypes, unknownType);
        operandTypeInference.inferOperandTypes(binding, binding.getValidator().getUnknownType(), operandTypes);
        for (int i = 0; i < binding.getOperandCount(); i++) {
            validator.setValidatedNodeType(binding.getCall().operand(i), operandTypes[i]);
        }
    }
    // Provide custom binding
    return new HazelcastCallBinding(binding);
}
Also used : HazelcastCallBinding(com.hazelcast.jet.sql.impl.validate.HazelcastCallBinding) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 5 with HazelcastSqlValidator

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

the class OptimizerContext method create.

public static OptimizerContext create(HazelcastSchema rootSchema, List<List<String>> schemaPaths, List<Object> arguments, int memberCount, IMapResolver iMapResolver) {
    DistributionTraitDef distributionTraitDef = new DistributionTraitDef(memberCount);
    Prepare.CatalogReader catalogReader = createCatalogReader(rootSchema, schemaPaths);
    HazelcastSqlValidator validator = new HazelcastSqlValidator(catalogReader, arguments, iMapResolver);
    VolcanoPlanner volcanoPlanner = createPlanner(distributionTraitDef);
    HazelcastRelOptCluster cluster = createCluster(volcanoPlanner, distributionTraitDef);
    QueryParser parser = new QueryParser(validator);
    QueryConverter converter = new QueryConverter(validator, catalogReader, cluster);
    QueryPlanner planner = new QueryPlanner(volcanoPlanner);
    return new OptimizerContext(cluster, parser, converter, planner);
}
Also used : HazelcastRelOptCluster(org.apache.calcite.plan.HazelcastRelOptCluster) QueryParser(com.hazelcast.jet.sql.impl.parse.QueryParser) QueryConverter(com.hazelcast.jet.sql.impl.parse.QueryConverter) Prepare(org.apache.calcite.prepare.Prepare) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) VolcanoPlanner(org.apache.calcite.plan.volcano.VolcanoPlanner) DistributionTraitDef(com.hazelcast.jet.sql.impl.opt.distribution.DistributionTraitDef)

Aggregations

HazelcastSqlValidator (com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator)11 RelDataType (org.apache.calcite.rel.type.RelDataType)8 SqlNode (org.apache.calcite.sql.SqlNode)7 SqlCall (org.apache.calcite.sql.SqlCall)3 AnyToVarcharParameterConverter (com.hazelcast.jet.sql.impl.validate.param.AnyToVarcharParameterConverter)2 ParameterConverter (com.hazelcast.sql.impl.ParameterConverter)2 ArrayList (java.util.ArrayList)2 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)2 SqlDynamicParam (org.apache.calcite.sql.SqlDynamicParam)2 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)2 SqlValidatorScope (org.apache.calcite.sql.validate.SqlValidatorScope)2 DistributionTraitDef (com.hazelcast.jet.sql.impl.opt.distribution.DistributionTraitDef)1 QueryConverter (com.hazelcast.jet.sql.impl.parse.QueryConverter)1 QueryParser (com.hazelcast.jet.sql.impl.parse.QueryParser)1 HazelcastCallBinding (com.hazelcast.jet.sql.impl.validate.HazelcastCallBinding)1 HazelcastResources (com.hazelcast.jet.sql.impl.validate.HazelcastResources)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 HazelcastTypeUtils.toHazelcastTypeFromSqlTypeName (com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils.toHazelcastTypeFromSqlTypeName)1