Search in sources :

Example 6 with SqlValidatorScope

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);
}
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 7 with SqlValidatorScope

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());
    }
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlUpdate(org.apache.calcite.sql.SqlUpdate) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SqlInsert(org.apache.calcite.sql.SqlInsert) TypeCoercionImpl(org.apache.calcite.sql.validate.implicit.TypeCoercionImpl) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) RESOURCE(org.apache.calcite.util.Static.RESOURCE) SqlCall(org.apache.calcite.sql.SqlCall) SqlNode(org.apache.calcite.sql.SqlNode) SqlUtil(org.apache.calcite.sql.SqlUtil) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlSelect(org.apache.calcite.sql.SqlSelect) OBJECT(com.hazelcast.sql.impl.type.QueryDataType.OBJECT) ANY(org.apache.calcite.sql.type.SqlTypeName.ANY) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlKind(org.apache.calcite.sql.SqlKind) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlTypeUtil(org.apache.calcite.sql.type.SqlTypeUtil) SqlUserDefinedTypeNameSpec(org.apache.calcite.sql.SqlUserDefinedTypeNameSpec) Consumer(java.util.function.Consumer) List(java.util.List) SqlFunction(org.apache.calcite.sql.SqlFunction) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) HazelcastSqlOperatorTable(com.hazelcast.jet.sql.impl.validate.HazelcastSqlOperatorTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) NULL(org.apache.calcite.sql.type.SqlTypeName.NULL) SqlSelect(org.apache.calcite.sql.SqlSelect) SqlCall(org.apache.calcite.sql.SqlCall) SqlNode(org.apache.calcite.sql.SqlNode)

Example 8 with SqlValidatorScope

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());
            }
        }
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlInsert(org.apache.calcite.sql.SqlInsert) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlWriter(org.apache.calcite.sql.SqlWriter) SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) RESOURCE(com.hazelcast.jet.sql.impl.parse.ParserResource.RESOURCE) Collectors(java.util.stream.Collectors) QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) ArrayList(java.util.ArrayList) TableField(com.hazelcast.sql.impl.schema.TableField) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) List(java.util.List) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode) ImmutableList(com.google.common.collect.ImmutableList) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Map(java.util.Map) QueryPath(com.hazelcast.sql.impl.extract.QueryPath) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) TableField(com.hazelcast.sql.impl.schema.TableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) QueryPath(com.hazelcast.sql.impl.extract.QueryPath) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) SqlNode(org.apache.calcite.sql.SqlNode)

Example 9 with SqlValidatorScope

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;
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) ArrayList(java.util.ArrayList) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 10 with SqlValidatorScope

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);
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlSelect(org.apache.calcite.sql.SqlSelect) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlValidatorScope (org.apache.calcite.sql.validate.SqlValidatorScope)20 RelDataType (org.apache.calcite.rel.type.RelDataType)12 SqlNode (org.apache.calcite.sql.SqlNode)10 ArrayList (java.util.ArrayList)7 SqlNodeList (org.apache.calcite.sql.SqlNodeList)6 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)6 RelNode (org.apache.calcite.rel.RelNode)5 SqlCall (org.apache.calcite.sql.SqlCall)5 List (java.util.List)4 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)4 NlsString (org.apache.calcite.util.NlsString)4 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 RexNode (org.apache.calcite.rex.RexNode)3 SqlOperator (org.apache.calcite.sql.SqlOperator)3 SqlSelect (org.apache.calcite.sql.SqlSelect)3 ImmutableList (com.google.common.collect.ImmutableList)2 HazelcastSqlValidator (com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator)2 QueryDataTypeFamily (com.hazelcast.sql.impl.type.QueryDataTypeFamily)2 Consumer (java.util.function.Consumer)2