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;
}
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;
}
}
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);
}
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);
}
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);
}
Aggregations