Search in sources :

Example 1 with ControlFlowException

use of org.apache.calcite.util.ControlFlowException in project calcite by apache.

the class SqlWindow method validate.

@Override
public void validate(SqlValidator validator, SqlValidatorScope scope) {
    // REVIEW
    SqlValidatorScope operandScope = scope;
    SqlIdentifier declName = this.declName;
    SqlIdentifier refName = this.refName;
    SqlNodeList partitionList = this.partitionList;
    SqlNodeList orderList = this.orderList;
    SqlLiteral isRows = this.isRows;
    SqlNode lowerBound = this.lowerBound;
    SqlNode upperBound = this.upperBound;
    SqlLiteral allowPartial = this.allowPartial;
    if (refName != null) {
        SqlWindow win = validator.resolveWindow(this, operandScope, false);
        partitionList = win.partitionList;
        orderList = win.orderList;
        isRows = win.isRows;
        lowerBound = win.lowerBound;
        upperBound = win.upperBound;
        allowPartial = win.allowPartial;
    }
    for (SqlNode partitionItem : partitionList) {
        try {
            partitionItem.accept(Util.OverFinder.INSTANCE);
        } catch (ControlFlowException e) {
            throw validator.newValidationError(this, RESOURCE.partitionbyShouldNotContainOver());
        }
        partitionItem.validateExpr(validator, operandScope);
    }
    for (SqlNode orderItem : orderList) {
        boolean savedColumnReferenceExpansion = validator.getColumnReferenceExpansion();
        validator.setColumnReferenceExpansion(false);
        try {
            orderItem.accept(Util.OverFinder.INSTANCE);
        } catch (ControlFlowException e) {
            throw validator.newValidationError(this, RESOURCE.orderbyShouldNotContainOver());
        }
        try {
            orderItem.validateExpr(validator, scope);
        } finally {
            validator.setColumnReferenceExpansion(savedColumnReferenceExpansion);
        }
    }
    // 6.10 rule 6a Function RANK & DENSE_RANK require ORDER BY clause
    if (orderList.size() == 0 && !SqlValidatorUtil.containsMonotonic(scope) && windowCall != null && windowCall.getOperator().requiresOrder()) {
        throw validator.newValidationError(this, RESOURCE.funcNeedsOrderBy());
    }
    // Run framing checks if there are any
    if (upperBound != null || lowerBound != null) {
        // 6.10 Rule 6a RANK & DENSE_RANK do not allow ROWS or RANGE
        if (windowCall != null && !windowCall.getOperator().allowsFraming()) {
            throw validator.newValidationError(isRows, RESOURCE.rankWithFrame());
        }
        SqlTypeFamily orderTypeFam = null;
        // SQL03 7.10 Rule 11a
        if (orderList.size() > 0) {
            // if order by is a compound list then range not allowed
            if (orderList.size() > 1 && !isRows()) {
                throw validator.newValidationError(isRows, RESOURCE.compoundOrderByProhibitsRange());
            }
            // get the type family for the sort key for Frame Boundary Val.
            RelDataType orderType = validator.deriveType(operandScope, orderList.get(0));
            orderTypeFam = orderType.getSqlTypeName().getFamily();
        } else {
            // sorted already
            if (!isRows() && !SqlValidatorUtil.containsMonotonic(scope)) {
                throw validator.newValidationError(this, RESOURCE.overMissingOrderBy());
            }
        }
        // Let the bounds validate themselves
        validateFrameBoundary(lowerBound, isRows(), orderTypeFam, validator, operandScope);
        validateFrameBoundary(upperBound, isRows(), orderTypeFam, validator, operandScope);
        // Validate across boundaries. 7.10 Rule 8 a-d
        checkSpecialLiterals(this, validator);
    } else if (orderList.size() == 0 && !SqlValidatorUtil.containsMonotonic(scope) && windowCall != null && windowCall.getOperator().requiresOrder()) {
        throw validator.newValidationError(this, RESOURCE.overMissingOrderBy());
    }
    if (!isRows() && !isAllowPartial()) {
        throw validator.newValidationError(allowPartial, RESOURCE.cannotUseDisallowPartialWithRange());
    }
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) ControlFlowException(org.apache.calcite.util.ControlFlowException) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)1 SqlTypeFamily (org.apache.calcite.sql.type.SqlTypeFamily)1 SqlValidatorScope (org.apache.calcite.sql.validate.SqlValidatorScope)1 ControlFlowException (org.apache.calcite.util.ControlFlowException)1