Skip to content

[SQL] Minor: Introduce SchemaRDD#aggregate() for simple aggregations #874

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions sql/core/src/main/scala/org/apache/spark/sql/SchemaRDD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import java.util.{Map => JMap}
* // Importing the SQL context gives access to all the SQL functions and implicit conversions.
* import sqlContext._
*
* val rdd = sc.parallelize((1 to 100).map(i => Record(i, s"val_\$i")))
* val rdd = sc.parallelize((1 to 100).map(i => Record(i, s"val_$i")))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example doesn't compile with the \ in there.

* // Any RDD containing case classes can be registered as a table. The schema of the table is
* // automatically inferred using scala reflection.
* rdd.registerAsTable("records")
Expand Down Expand Up @@ -204,6 +204,20 @@ class SchemaRDD(
new SchemaRDD(sqlContext, Aggregate(groupingExprs, aliasedExprs, logicalPlan))
}

/**
* Performs an aggregation over all Rows in this RDD.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you say in the scaladoc that this is equivalent to groupBy()(...) ?

* This is equivalent to a groupBy with no grouping expressions.
*
* {{{
* schemaRDD.aggregate(Sum('sales) as 'totalSales)
* }}}
*
* @group Query
*/
def aggregate(aggregateExprs: Expression*): SchemaRDD = {
groupBy()(aggregateExprs: _*)
}

/**
* Applies a qualifier to the attributes of this relation. Can be used to disambiguate attributes
* with the same name, for example, when performing self-joins.
Expand Down Expand Up @@ -281,7 +295,7 @@ class SchemaRDD(
* supports features such as filter pushdown.
*/
@Experimental
override def count(): Long = groupBy()(Count(Literal(1))).collect().head.getLong(0)
override def count(): Long = aggregate(Count(Literal(1))).collect().head.getLong(0)

/**
* :: Experimental ::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class DslQuerySuite extends QueryTest {
testData2.groupBy('a)('a, Sum('b)),
Seq((1,3),(2,3),(3,3))
)
checkAnswer(
testData2.groupBy('a)('a, Sum('b) as 'totB).aggregate(Sum('totB)),
9
)
checkAnswer(
testData2.aggregate(Sum('b)),
9
)
}

test("select *") {
Expand Down