You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/01/11 20:52:06 UTC
[GitHub] [beam] ibzib commented on a change in pull request #16200: [BEAM-11808][BEAM-9879] Support aggregate functions with two arguments
ibzib commented on a change in pull request #16200:
URL: https://github.com/apache/beam/pull/16200#discussion_r782477782
##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/AggregateScanConverter.java
##########
@@ -240,18 +244,22 @@ private AggregateCall convertAggCall(
}
List<Integer> argList = new ArrayList<>();
- for (ResolvedExpr expr :
- ((ResolvedAggregateFunctionCall) computedColumn.getExpr()).getArgumentList()) {
+ ResolvedAggregateFunctionCall expr = ((ResolvedAggregateFunctionCall) computedColumn.getExpr());
+ List<ZetaSQLResolvedNodeKind.ResolvedNodeKind> resolvedNodeKinds =
+ Arrays.asList(RESOLVED_CAST, RESOLVED_COLUMN_REF, RESOLVED_GET_STRUCT_FIELD);
+ for (int i = 0; i < expr.getArgumentList().size(); i++) {
// Throw an error if aggregate function's input isn't either a ColumnRef or a cast(ColumnRef).
// TODO: is there a general way to handle aggregation calls conversion?
- if (expr.nodeKind() == RESOLVED_CAST
- || expr.nodeKind() == RESOLVED_COLUMN_REF
- || expr.nodeKind() == RESOLVED_GET_STRUCT_FIELD) {
+ ZetaSQLResolvedNodeKind.ResolvedNodeKind resolvedNodeKind =
+ expr.getArgumentList().get(i).nodeKind();
+ if (i == 0 && resolvedNodeKinds.contains(resolvedNodeKind)) {
Review comment:
What about the case of a literal as the first argument? If I'm understanding the logic right, it doesn't throw an error even though it should (instead it `continue`s).
##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/AggregateScanConverter.java
##########
@@ -148,24 +149,27 @@ private LogicalProject convertAggregateScanInputScanToLogicalProject(
// aggregation?
ResolvedAggregateFunctionCall aggregateFunctionCall =
((ResolvedAggregateFunctionCall) resolvedComputedColumn.getExpr());
- if (aggregateFunctionCall.getArgumentList() != null
- && aggregateFunctionCall.getArgumentList().size() == 1) {
- ResolvedExpr resolvedExpr = aggregateFunctionCall.getArgumentList().get(0);
-
- // TODO: assume aggregate function's input is either a ColumnRef or a cast(ColumnRef).
- // TODO: user might use multiple CAST so we need to handle this rare case.
- projects.add(
- getExpressionConverter()
- .convertRexNodeFromResolvedExpr(
- resolvedExpr,
- node.getInputScan().getColumnList(),
- input.getRowType().getFieldList(),
- ImmutableMap.of()));
- fieldNames.add(getTrait().resolveAlias(resolvedComputedColumn.getColumn()));
- } else if (aggregateFunctionCall.getArgumentList() != null
- && aggregateFunctionCall.getArgumentList().size() > 1) {
- throw new IllegalArgumentException(
- aggregateFunctionCall.getFunction().getName() + " has more than one argument.");
+ ImmutableList<ResolvedExpr> argumentList =
+ ImmutableList.copyOf(aggregateFunctionCall.getArgumentList());
+ if (argumentList != null && argumentList.size() >= 1) {
+ ResolvedExpr resolvedExpr = argumentList.get(0);
+ for (int i = 0; i < argumentList.size(); i++) {
+ if (i == 0) {
+ // TODO: assume aggregate function's input is either a ColumnRef or a cast(ColumnRef).
+ // TODO: user might use multiple CAST so we need to handle this rare case.
+ projects.add(
+ getExpressionConverter()
+ .convertRexNodeFromResolvedExpr(
+ resolvedExpr,
+ node.getInputScan().getColumnList(),
+ input.getRowType().getFieldList(),
+ ImmutableMap.of()));
+ } else {
+ projects.add(
+ getExpressionConverter().convertRexNodeFromResolvedExpr(argumentList.get(i)));
+ }
+ fieldNames.add(getTrait().resolveAlias(resolvedComputedColumn.getColumn()));
Review comment:
Ok, thanks for checking.
##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/AggregateScanConverter.java
##########
@@ -148,24 +149,27 @@ private LogicalProject convertAggregateScanInputScanToLogicalProject(
// aggregation?
ResolvedAggregateFunctionCall aggregateFunctionCall =
((ResolvedAggregateFunctionCall) resolvedComputedColumn.getExpr());
- if (aggregateFunctionCall.getArgumentList() != null
- && aggregateFunctionCall.getArgumentList().size() == 1) {
- ResolvedExpr resolvedExpr = aggregateFunctionCall.getArgumentList().get(0);
-
- // TODO: assume aggregate function's input is either a ColumnRef or a cast(ColumnRef).
- // TODO: user might use multiple CAST so we need to handle this rare case.
- projects.add(
- getExpressionConverter()
- .convertRexNodeFromResolvedExpr(
- resolvedExpr,
- node.getInputScan().getColumnList(),
- input.getRowType().getFieldList(),
- ImmutableMap.of()));
- fieldNames.add(getTrait().resolveAlias(resolvedComputedColumn.getColumn()));
- } else if (aggregateFunctionCall.getArgumentList() != null
- && aggregateFunctionCall.getArgumentList().size() > 1) {
- throw new IllegalArgumentException(
- aggregateFunctionCall.getFunction().getName() + " has more than one argument.");
+ ImmutableList<ResolvedExpr> argumentList =
+ ImmutableList.copyOf(aggregateFunctionCall.getArgumentList());
+ if (argumentList != null && argumentList.size() >= 1) {
Review comment:
Ah, ok.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org