Search in sources :

Example 1 with OrderByScope

use of org.apache.calcite.sql.validate.OrderByScope in project calcite by apache.

the class SqlAbstractGroupFunction method validateCall.

@Override
public void validateCall(SqlCall call, SqlValidator validator, SqlValidatorScope scope, SqlValidatorScope operandScope) {
    super.validateCall(call, validator, scope, operandScope);
    final SelectScope selectScope = SqlValidatorUtil.getEnclosingSelectScope(scope);
    assert selectScope != null;
    final SqlSelect select = selectScope.getNode();
    if (!validator.isAggregate(select)) {
        throw validator.newValidationError(call, Static.RESOURCE.groupingInAggregate(getName()));
    }
    final AggregatingSelectScope aggregatingSelectScope = SqlValidatorUtil.getEnclosingAggregateSelectScope(scope);
    if (aggregatingSelectScope == null) {
        // We're probably in the GROUP BY clause
        throw validator.newValidationError(call, Static.RESOURCE.groupingInWrongClause(getName()));
    }
    for (SqlNode operand : call.getOperandList()) {
        if (scope instanceof OrderByScope) {
            operand = validator.expandOrderExpr(select, operand);
        } else {
            operand = validator.expand(operand, scope);
        }
        if (!aggregatingSelectScope.resolved.get().isGroupingExpr(operand)) {
            throw validator.newValidationError(operand, Static.RESOURCE.groupingArgument(getName()));
        }
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SelectScope(org.apache.calcite.sql.validate.SelectScope) AggregatingSelectScope(org.apache.calcite.sql.validate.AggregatingSelectScope) OrderByScope(org.apache.calcite.sql.validate.OrderByScope) AggregatingSelectScope(org.apache.calcite.sql.validate.AggregatingSelectScope) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNode (org.apache.calcite.sql.SqlNode)1 SqlSelect (org.apache.calcite.sql.SqlSelect)1 AggregatingSelectScope (org.apache.calcite.sql.validate.AggregatingSelectScope)1 OrderByScope (org.apache.calcite.sql.validate.OrderByScope)1 SelectScope (org.apache.calcite.sql.validate.SelectScope)1