use of org.apache.calcite.sql.validate.SqlValidatorScope 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 org.apache.calcite.sql.validate.SqlValidatorScope in project hazelcast by hazelcast.
the class HazelcastTypeCoercion method rowTypeCoercion.
/**
* {@inheritDoc}
* <p>
* We change the contract of the superclass' return type. According to the
* superclass contract we're supposed to return true iff we successfully
* added a CAST. This method returns true if the expression can now be
* assigned to {@code targetType}, either because a CAST was added, or
* because it already was assignable (e.g. the type was same). This is
* needed for {@link #querySourceCoercion} method, which calls this method.
*
* @return True, if the source column can now be assigned to {@code
* targetType}
*/
@Override
public boolean rowTypeCoercion(SqlValidatorScope scope, SqlNode query, int columnIndex, RelDataType targetType) {
switch(query.getKind()) {
case SELECT:
SqlSelect selectNode = (SqlSelect) query;
SqlValidatorScope selectScope = validator.getSelectScope(selectNode);
if (!rowTypeElementCoercion(selectScope, selectNode.getSelectList().get(columnIndex), targetType, newNode -> selectNode.getSelectList().set(columnIndex, newNode))) {
return false;
}
updateInferredColumnType(selectScope, query, columnIndex, targetType);
return true;
case VALUES:
for (SqlNode rowConstructor : ((SqlCall) query).getOperandList()) {
if (!rowTypeElementCoercion(scope, ((SqlCall) rowConstructor).operand(columnIndex), targetType, newNode -> ((SqlCall) rowConstructor).setOperand(columnIndex, newNode))) {
return false;
}
}
updateInferredColumnType(scope, query, columnIndex, targetType);
return true;
default:
throw new UnsupportedOperationException("unexpected: " + query.getKind());
}
}
use of org.apache.calcite.sql.validate.SqlValidatorScope in project hazelcast by hazelcast.
the class SqlExtendedInsert method validate.
@Override
public void validate(SqlValidator validator, SqlValidatorScope scope) {
SqlValidatorTable table0 = validator.getCatalogReader().getTable(tableNames());
if (table0 == null) {
super.validate(validator, scope);
// should have failed with "Object not found"
assert false;
}
HazelcastTable table = table0.unwrap(HazelcastTable.class);
if (getTargetColumnList() == null) {
RelDataType rowType = table.getRowType(validator.getTypeFactory());
List<SqlNode> columnListWithoutHidden = new ArrayList<>();
for (RelDataTypeField f : rowType.getFieldList()) {
if (!table.isHidden(f.getName())) {
columnListWithoutHidden.add(new SqlIdentifier(f.getName(), SqlParserPos.ZERO));
}
}
overrideColumnList = new SqlNodeList(columnListWithoutHidden, SqlParserPos.ZERO);
}
super.validate(validator, scope);
Map<String, TableField> fieldsMap = table.getTarget().getFields().stream().collect(Collectors.toMap(TableField::getName, f -> f));
for (SqlNode fieldNode : getTargetColumnList()) {
TableField field = fieldsMap.get(((SqlIdentifier) fieldNode).getSimple());
if (field instanceof MapTableField) {
QueryPath path = ((MapTableField) field).getPath();
if (path.getPath() == null && field.getType().getTypeFamily() == QueryDataTypeFamily.OBJECT) {
throw validator.newValidationError(fieldNode, RESOURCE.insertToTopLevelObject());
}
}
}
}
use of org.apache.calcite.sql.validate.SqlValidatorScope in project hazelcast by hazelcast.
the class HazelcastCoalesceFunction method checkOperandTypes.
@Override
protected boolean checkOperandTypes(HazelcastCallBinding callBinding, boolean throwOnFailure) {
HazelcastSqlValidator validator = callBinding.getValidator();
SqlValidatorScope scope = callBinding.getScope();
SqlBasicCall sqlCall = (SqlBasicCall) callBinding.getCall();
List<SqlNode> operandList = sqlCall.getOperandList();
List<RelDataType> argTypes = new ArrayList<>(operandList.size());
for (SqlNode node : operandList) {
argTypes.add(validator.deriveType(scope, node));
}
assert !argTypes.isEmpty();
RelDataType returnType = argTypes.stream().reduce(HazelcastTypeUtils::withHigherPrecedence).get();
for (int i = 0; i < operandList.size(); i++) {
int finalI = i;
boolean elementTypeCoerced = validator.getTypeCoercion().rowTypeElementCoercion(scope, operandList.get(i), returnType, sqlNode -> sqlCall.getOperands()[finalI] = sqlNode);
if (!elementTypeCoerced) {
if (throwOnFailure) {
throw validator.newValidationError(sqlCall, RESOURCES.cannotInferCaseResult(argTypes.toString(), getName()));
} else {
return false;
}
}
}
return true;
}
use of org.apache.calcite.sql.validate.SqlValidatorScope in project calcite by apache.
the class SqlTesterImpl method getMonotonicity.
public SqlMonotonicity getMonotonicity(String sql) {
final SqlValidator validator = getValidator();
final SqlNode node = parseAndValidate(validator, sql);
final SqlSelect select = (SqlSelect) node;
final SqlNode selectItem0 = select.getSelectList().get(0);
final SqlValidatorScope scope = validator.getSelectScope(select);
return selectItem0.getMonotonicity(scope);
}
Aggregations