Search in sources :

Example 1 with SqlWindow

use of org.apache.calcite.sql.SqlWindow in project calcite by apache.

the class SqlValidatorImpl method validateWindow.

public void validateWindow(SqlNode windowOrId, SqlValidatorScope scope, SqlCall call) {
    // Enable nested aggregates with window aggregates (OVER operator)
    inWindow = true;
    final SqlWindow targetWindow;
    switch(windowOrId.getKind()) {
        case IDENTIFIER:
            // Just verify the window exists in this query.  It will validate
            // when the definition is processed
            targetWindow = getWindowByName((SqlIdentifier) windowOrId, scope);
            break;
        case WINDOW:
            targetWindow = (SqlWindow) windowOrId;
            break;
        default:
            throw Util.unexpected(windowOrId.getKind());
    }
    assert targetWindow.getWindowCall() == null;
    targetWindow.setWindowCall(call);
    targetWindow.validate(this, scope);
    targetWindow.setWindowCall(null);
    call.validate(this, scope);
    validateAggregateParams(call, null, scope);
    // Disable nested aggregates post validation
    inWindow = false;
}
Also used : SqlWindow(org.apache.calcite.sql.SqlWindow) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 2 with SqlWindow

use of org.apache.calcite.sql.SqlWindow in project calcite by apache.

the class SqlValidatorImpl method getWindowByName.

protected SqlWindow getWindowByName(SqlIdentifier id, SqlValidatorScope scope) {
    SqlWindow window = null;
    if (id.isSimple()) {
        final String name = id.getSimple();
        window = scope.lookupWindow(name);
    }
    if (window == null) {
        throw newValidationError(id, RESOURCE.windowNotFound(id.toString()));
    }
    return window;
}
Also used : SqlWindow(org.apache.calcite.sql.SqlWindow) BitString(org.apache.calcite.util.BitString)

Example 3 with SqlWindow

use of org.apache.calcite.sql.SqlWindow in project calcite by apache.

the class SqlValidatorImpl method validateWindowClause.

protected void validateWindowClause(SqlSelect select) {
    final SqlNodeList windowList = select.getWindowList();
    @SuppressWarnings("unchecked") final List<SqlWindow> windows = (List) windowList.getList();
    if (windows.isEmpty()) {
        return;
    }
    final SelectScope windowScope = (SelectScope) getFromScope(select);
    assert windowScope != null;
    // 2. ensure they are unique within this scope
    for (SqlWindow window : windows) {
        SqlIdentifier declName = window.getDeclName();
        if (!declName.isSimple()) {
            throw newValidationError(declName, RESOURCE.windowNameMustBeSimple());
        }
        if (windowScope.existingWindowName(declName.toString())) {
            throw newValidationError(declName, RESOURCE.duplicateWindowName());
        } else {
            windowScope.addWindowName(declName.toString());
        }
    }
    // Check for pairs of windows which are equivalent.
    for (int i = 0; i < windows.size(); i++) {
        SqlNode window1 = windows.get(i);
        for (int j = i + 1; j < windows.size(); j++) {
            SqlNode window2 = windows.get(j);
            if (window1.equalsDeep(window2, Litmus.IGNORE)) {
                throw newValidationError(window2, RESOURCE.dupWindowSpec());
            }
        }
    }
    for (SqlWindow window : windows) {
        final SqlNodeList expandedOrderList = (SqlNodeList) expand(window.getOrderList(), windowScope);
        window.setOrderList(expandedOrderList);
        expandedOrderList.validate(this, windowScope);
        final SqlNodeList expandedPartitionList = (SqlNodeList) expand(window.getPartitionList(), windowScope);
        window.setPartitionList(expandedPartitionList);
        expandedPartitionList.validate(this, windowScope);
    }
    // Hand off to validate window spec components
    windowList.validate(this, windowScope);
}
Also used : SqlWindow(org.apache.calcite.sql.SqlWindow) SqlNodeList(org.apache.calcite.sql.SqlNodeList) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) ImmutableNullableList(org.apache.calcite.util.ImmutableNullableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with SqlWindow

use of org.apache.calcite.sql.SqlWindow in project calcite by apache.

the class SelectScope method lookupWindow.

public SqlWindow lookupWindow(String name) {
    final SqlNodeList windowList = select.getWindowList();
    for (int i = 0; i < windowList.size(); i++) {
        SqlWindow window = (SqlWindow) windowList.get(i);
        final SqlIdentifier declId = window.getDeclName();
        assert declId.isSimple();
        if (declId.names.get(0).equals(name)) {
            return window;
        }
    }
    // if not in the select scope, then check window scope
    if (windowParent != null) {
        return windowParent.lookupWindow(name);
    } else {
        return null;
    }
}
Also used : SqlWindow(org.apache.calcite.sql.SqlWindow) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 5 with SqlWindow

use of org.apache.calcite.sql.SqlWindow in project flink by apache.

the class SqlValidatorImpl method getWindowByName.

protected SqlWindow getWindowByName(SqlIdentifier id, SqlValidatorScope scope) {
    SqlWindow window = null;
    if (id.isSimple()) {
        final String name = id.getSimple();
        window = scope.lookupWindow(name);
    }
    if (window == null) {
        throw newValidationError(id, RESOURCE.windowNotFound(id.toString()));
    }
    return window;
}
Also used : SqlWindow(org.apache.calcite.sql.SqlWindow) BitString(org.apache.calcite.util.BitString)

Aggregations

SqlWindow (org.apache.calcite.sql.SqlWindow)13 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)8 SqlNode (org.apache.calcite.sql.SqlNode)6 SqlNodeList (org.apache.calcite.sql.SqlNodeList)5 SqlCall (org.apache.calcite.sql.SqlCall)4 BitString (org.apache.calcite.util.BitString)4 ImmutableList (com.google.common.collect.ImmutableList)3 SqlSelect (org.apache.calcite.sql.SqlSelect)3 AbstractList (java.util.AbstractList)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SqlJoin (org.apache.calcite.sql.SqlJoin)2 SqlNumericLiteral (org.apache.calcite.sql.SqlNumericLiteral)2 SqlCountAggFunction (org.apache.calcite.sql.fun.SqlCountAggFunction)2 ImmutableNullableList (org.apache.calcite.util.ImmutableNullableList)2 RexFieldCollation (org.apache.calcite.rex.RexFieldCollation)1 RexNode (org.apache.calcite.rex.RexNode)1 RexShuttle (org.apache.calcite.rex.RexShuttle)1 SqlKind (org.apache.calcite.sql.SqlKind)1 SqlLiteral (org.apache.calcite.sql.SqlLiteral)1