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