forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make aggregation statement compilation robust
Signed-off-by: Lantao Jin <[email protected]>
- Loading branch information
Showing
15 changed files
with
434 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
core/src/main/java/org/opensearch/sql/analysis/QueryCompilationError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.analysis; | ||
|
||
import static org.opensearch.sql.common.utils.StringUtils.format; | ||
|
||
import org.opensearch.sql.ast.expression.UnresolvedExpression; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.exception.SemanticCheckException; | ||
import org.opensearch.sql.expression.NamedExpression; | ||
|
||
/** Grouping error messages from {@link SemanticCheckException} thrown during query compilation. */ | ||
public class QueryCompilationError { | ||
|
||
public static SemanticCheckException fieldNotInGroupByClauseError(NamedExpression expr) { | ||
return new SemanticCheckException( | ||
format( | ||
"Field [%s] must appear in the GROUP BY clause or be used in an aggregate function", | ||
expr.getName())); | ||
} | ||
|
||
public static SemanticCheckException groupByClauseIsMissingError(UnresolvedExpression expr) { | ||
return new SemanticCheckException( | ||
format( | ||
"Explicit GROUP BY clause is required because expression [%s] contains non-aggregated" | ||
+ " column", | ||
expr)); | ||
} | ||
|
||
public static SemanticCheckException aggregateFunctionNotAllowedInGroupByError( | ||
String functionName) { | ||
return new SemanticCheckException( | ||
format( | ||
"Aggregate function is not allowed in a GROUP BY clause, but found [%s]", | ||
functionName)); | ||
} | ||
|
||
public static SemanticCheckException nonBooleanExpressionInFilterOrHavingError(ExprType type) { | ||
return new SemanticCheckException( | ||
format( | ||
"FILTER or HAVING expression must be type boolean, but found [%s]", type.typeName())); | ||
} | ||
|
||
public static SemanticCheckException groupByOrdinalRefersToAggregateFunctionError(int ordinal) { | ||
return new SemanticCheckException( | ||
format( | ||
"GROUP BY %s refers to an expression that contains an aggregate function. Aggregate" | ||
+ " functions are not allowed in GROUP BY", | ||
ordinal)); | ||
} | ||
|
||
public static SemanticCheckException ordinalRefersOutOfBounds(int ordinal) { | ||
return new SemanticCheckException( | ||
format("Ordinal [%d] is out of bound of select item list", ordinal)); | ||
} | ||
|
||
public static SemanticCheckException aggregateFunctionNotAllowedInFilterError( | ||
String functionName) { | ||
return new SemanticCheckException( | ||
format("Aggregate function is not allowed in a FILTER, but found [%s]", functionName)); | ||
} | ||
|
||
public static SemanticCheckException windowFunctionNotAllowedError() { | ||
return new SemanticCheckException("window functions are not allowed in WHERE or HAVING"); | ||
} | ||
|
||
public static SemanticCheckException unsupportedAggregateFunctionError(String functionName) { | ||
return new SemanticCheckException(format("Unsupported aggregation function %s", functionName)); | ||
} | ||
|
||
public static SemanticCheckException rankingWindowFunctionMissesOrderClauseError( | ||
String functionName) { | ||
return new SemanticCheckException( | ||
format( | ||
"Window function [%s] requires window to be ordered, please add ORDER BY clause.", | ||
functionName)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
core/src/main/java/org/opensearch/sql/ast/tree/Having.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.ast.tree; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.List; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
import org.opensearch.sql.ast.expression.UnresolvedExpression; | ||
|
||
/** | ||
* Represents unresolved HAVING clause, its child can be Aggregation. Having without aggregation | ||
* equals to {@link Filter} | ||
*/ | ||
@Getter | ||
@Setter | ||
@ToString | ||
@EqualsAndHashCode(callSuper = false) | ||
public class Having extends UnresolvedPlan { | ||
private List<UnresolvedExpression> aggregators; | ||
private UnresolvedExpression condition; | ||
private UnresolvedPlan child; | ||
|
||
public Having(List<UnresolvedExpression> aggregators, UnresolvedExpression condition) { | ||
this.aggregators = aggregators; | ||
this.condition = condition; | ||
} | ||
|
||
@Override | ||
public UnresolvedPlan attach(UnresolvedPlan child) { | ||
this.child = child; | ||
return this; | ||
} | ||
|
||
@Override | ||
public List<UnresolvedPlan> getChild() { | ||
return ImmutableList.of(child); | ||
} | ||
|
||
@Override | ||
public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) { | ||
return nodeVisitor.visitHaving(this, context); | ||
} | ||
} |
Oops, something went wrong.