-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-42468][CONNECT] Implement agg by (String, String)* #40057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
152 changes: 152 additions & 0 deletions
152
...tor/connect/client/jvm/src/main/scala/org/apache/spark/sql/RelationalGroupedDataset.scala
This file contains hidden or 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,152 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql | ||
|
|
||
| import java.util.Locale | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
|
|
||
| import org.apache.spark.connect.proto | ||
|
|
||
| /** | ||
| * A set of methods for aggregations on a `DataFrame`, created by [[Dataset#groupBy groupBy]], | ||
| * [[Dataset#cube cube]] or [[Dataset#rollup rollup]] (and also `pivot`). | ||
| * | ||
| * The main method is the `agg` function, which has multiple variants. This class also contains | ||
| * some first-order statistics such as `mean`, `sum` for convenience. | ||
| * | ||
| * @note | ||
| * This class was named `GroupedData` in Spark 1.x. | ||
| * | ||
| * @since 3.4.0 | ||
| */ | ||
| class RelationalGroupedDataset protected[sql] ( | ||
| private[sql] val df: DataFrame, | ||
| private[sql] val groupingExprs: Seq[proto.Expression]) { | ||
|
|
||
| private[this] def toDF(aggExprs: Seq[proto.Expression]): DataFrame = { | ||
| // TODO: support other GroupByType such as Rollup, Cube, Pivot. | ||
| df.session.newDataset { builder => | ||
| builder.getAggregateBuilder | ||
| .setGroupType(proto.Aggregate.GroupType.GROUP_TYPE_GROUPBY) | ||
| .setInput(df.plan.getRoot) | ||
| .addAllGroupingExpressions(groupingExprs.asJava) | ||
| .addAllAggregateExpressions(aggExprs.asJava) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * (Scala-specific) Compute aggregates by specifying the column names and aggregate methods. The | ||
| * resulting `DataFrame` will also contain the grouping columns. | ||
| * | ||
| * The available aggregate methods are `avg`, `max`, `min`, `sum`, `count`. | ||
| * {{{ | ||
| * // Selects the age of the oldest employee and the aggregate expense for each department | ||
| * df.groupBy("department").agg( | ||
| * "age" -> "max", | ||
| * "expense" -> "sum" | ||
| * ) | ||
| * }}} | ||
| * | ||
| * @since 3.4.0 | ||
| */ | ||
| def agg(aggExpr: (String, String), aggExprs: (String, String)*): DataFrame = { | ||
| toDF((aggExpr +: aggExprs).map { case (colName, expr) => | ||
| strToExpr(expr, df(colName).expr) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * (Scala-specific) Compute aggregates by specifying a map from column name to aggregate | ||
| * methods. The resulting `DataFrame` will also contain the grouping columns. | ||
| * | ||
| * The available aggregate methods are `avg`, `max`, `min`, `sum`, `count`. | ||
| * {{{ | ||
| * // Selects the age of the oldest employee and the aggregate expense for each department | ||
| * df.groupBy("department").agg(Map( | ||
| * "age" -> "max", | ||
| * "expense" -> "sum" | ||
| * )) | ||
| * }}} | ||
| * | ||
| * @since 3.4.0 | ||
| */ | ||
| def agg(exprs: Map[String, String]): DataFrame = { | ||
| toDF(exprs.map { case (colName, expr) => | ||
| strToExpr(expr, df(colName).expr) | ||
| }.toSeq) | ||
| } | ||
|
|
||
| /** | ||
| * (Java-specific) Compute aggregates by specifying a map from column name to aggregate methods. | ||
| * The resulting `DataFrame` will also contain the grouping columns. | ||
| * | ||
| * The available aggregate methods are `avg`, `max`, `min`, `sum`, `count`. | ||
| * {{{ | ||
| * // Selects the age of the oldest employee and the aggregate expense for each department | ||
| * import com.google.common.collect.ImmutableMap; | ||
| * df.groupBy("department").agg(ImmutableMap.of("age", "max", "expense", "sum")); | ||
| * }}} | ||
| * | ||
| * @since 3.4.0 | ||
| */ | ||
| def agg(exprs: java.util.Map[String, String]): DataFrame = { | ||
| agg(exprs.asScala.toMap) | ||
| } | ||
|
|
||
| private[this] def strToExpr(expr: String, inputExpr: proto.Expression): proto.Expression = { | ||
| val builder = proto.Expression.newBuilder() | ||
|
|
||
| expr.toLowerCase(Locale.ROOT) match { | ||
| // We special handle a few cases that have alias that are not in function registry. | ||
| case "avg" | "average" | "mean" => | ||
| builder.getUnresolvedFunctionBuilder | ||
| .setFunctionName("avg") | ||
| .addArguments(inputExpr) | ||
| .setIsDistinct(false) | ||
| case "stddev" | "std" => | ||
| builder.getUnresolvedFunctionBuilder | ||
| .setFunctionName("stddev") | ||
| .addArguments(inputExpr) | ||
| .setIsDistinct(false) | ||
| // Also special handle count because we need to take care count(*). | ||
| case "count" | "size" => | ||
| // Turn count(*) into count(1) | ||
| inputExpr match { | ||
| case s if s.hasUnresolvedStar => | ||
| val exprBuilder = proto.Expression.newBuilder | ||
| exprBuilder.getLiteralBuilder.setInteger(1) | ||
| builder.getUnresolvedFunctionBuilder | ||
| .setFunctionName("count") | ||
| .addArguments(exprBuilder) | ||
| .setIsDistinct(false) | ||
| case _ => | ||
| builder.getUnresolvedFunctionBuilder | ||
| .setFunctionName("count") | ||
| .addArguments(inputExpr) | ||
| .setIsDistinct(false) | ||
| } | ||
| case name => | ||
| builder.getUnresolvedFunctionBuilder | ||
| .setFunctionName(name) | ||
| .addArguments(inputExpr) | ||
| .setIsDistinct(false) | ||
| } | ||
| builder.build() | ||
| } | ||
| } | ||
This file contains hidden or 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
2 changes: 2 additions & 0 deletions
2
connector/connect/common/src/test/resources/query-tests/explain-results/groupby_agg.explain
This file contains hidden or 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,2 @@ | ||
| Aggregate [id#0L], [id#0L, max(a#0) AS max(a)#0, stddev(b#0) AS stddev(b)#0, stddev(b#0) AS stddev(b)#0, avg(b#0) AS avg(b)#0, avg(b#0) AS avg(b)#0, avg(b#0) AS avg(b)#0, count(1) AS count(1)#0L, count(a#0) AS count(a)#0L] | ||
| +- LocalRelation <empty>, [id#0L, a#0, b#0] |
88 changes: 88 additions & 0 deletions
88
connector/connect/common/src/test/resources/query-tests/queries/groupby_agg.json
This file contains hidden or 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,88 @@ | ||
| { | ||
| "aggregate": { | ||
| "input": { | ||
| "localRelation": { | ||
| "schema": "struct\u003cid:bigint,a:int,b:double\u003e" | ||
| } | ||
| }, | ||
| "groupType": "GROUP_TYPE_GROUPBY", | ||
| "groupingExpressions": [{ | ||
| "unresolvedAttribute": { | ||
| "unparsedIdentifier": "id" | ||
| } | ||
| }], | ||
| "aggregateExpressions": [{ | ||
| "unresolvedFunction": { | ||
| "functionName": "max", | ||
| "arguments": [{ | ||
| "unresolvedAttribute": { | ||
| "unparsedIdentifier": "a" | ||
| } | ||
| }] | ||
| } | ||
| }, { | ||
| "unresolvedFunction": { | ||
| "functionName": "stddev", | ||
| "arguments": [{ | ||
| "unresolvedAttribute": { | ||
| "unparsedIdentifier": "b" | ||
| } | ||
| }] | ||
| } | ||
| }, { | ||
| "unresolvedFunction": { | ||
| "functionName": "stddev", | ||
| "arguments": [{ | ||
| "unresolvedAttribute": { | ||
| "unparsedIdentifier": "b" | ||
| } | ||
| }] | ||
| } | ||
| }, { | ||
| "unresolvedFunction": { | ||
| "functionName": "avg", | ||
| "arguments": [{ | ||
| "unresolvedAttribute": { | ||
| "unparsedIdentifier": "b" | ||
| } | ||
| }] | ||
| } | ||
| }, { | ||
| "unresolvedFunction": { | ||
| "functionName": "avg", | ||
| "arguments": [{ | ||
| "unresolvedAttribute": { | ||
| "unparsedIdentifier": "b" | ||
| } | ||
| }] | ||
| } | ||
| }, { | ||
| "unresolvedFunction": { | ||
| "functionName": "avg", | ||
| "arguments": [{ | ||
| "unresolvedAttribute": { | ||
| "unparsedIdentifier": "b" | ||
| } | ||
| }] | ||
| } | ||
| }, { | ||
| "unresolvedFunction": { | ||
| "functionName": "count", | ||
| "arguments": [{ | ||
| "literal": { | ||
| "integer": 1 | ||
| } | ||
| }] | ||
| } | ||
| }, { | ||
| "unresolvedFunction": { | ||
| "functionName": "count", | ||
| "arguments": [{ | ||
| "unresolvedAttribute": { | ||
| "unparsedIdentifier": "a" | ||
| } | ||
| }] | ||
| } | ||
| }] | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
connector/connect/common/src/test/resources/query-tests/queries/groupby_agg.proto.bin
This file contains hidden or 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,19 @@ | ||
| J� | ||
| $Z" struct<id:bigint,a:int,b:double> | ||
| id" | ||
| max | ||
| a" | ||
| stddev | ||
| b" | ||
| stddev | ||
| b" | ||
| avg | ||
| b" | ||
| avg | ||
| b" | ||
| avg | ||
| b" | ||
| count | ||
| 0" | ||
| count | ||
| a |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need to take care of
count(*)? we don't need it in python client #39622 (comment)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to match existing scala side Dataframe impl. @cloud-fan do you know if we need count(*) to count(1) conversion? If not we can both change here and existing DF.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, we can update them later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we don't need it. We can address when we replace this stuff by the functions API.