Skip to content
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
2 changes: 1 addition & 1 deletion sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Dataset[T] private[sql](
* implicit so that we can use it when constructing new [[Dataset]] objects that have the same
* object type (that will be possibly resolved to a different schema).
*/
private implicit val unresolvedTEncoder: ExpressionEncoder[T] = encoderFor(tEncoder)
private[sql] implicit val unresolvedTEncoder: ExpressionEncoder[T] = encoderFor(tEncoder)

/** The encoder for this [[Dataset]] that has been resolved to its output schema. */
private[sql] val resolvedTEncoder: ExpressionEncoder[T] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,11 @@ class GroupedDataset[K, V] private[sql](
val namedColumns =
columns.map(
_.withInputType(resolvedVEncoder, dataAttributes).named)
val keyColumn = if (groupingAttributes.length > 1) {
Alias(CreateStruct(groupingAttributes), "key")()
} else {
val keyColumn = if (resolvedKEncoder.flat) {
assert(groupingAttributes.length == 1)
groupingAttributes.head
} else {
Alias(CreateStruct(groupingAttributes), "key")()
}
val aggregate = Aggregate(groupingAttributes, keyColumn +: namedColumns, logicalPlan)
val execution = new QueryExecution(sqlContext, aggregate)
Expand Down
19 changes: 19 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/DatasetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ class DatasetSuite extends QueryTest with SharedSQLContext {
3 -> "abcxyz", 5 -> "hello")
}

test("groupBy single field class, count") {
val ds = Seq("abc", "xyz", "hello").toDS()
val count = ds.groupBy(s => Tuple1(s.length)).count()

checkAnswer(
count,
(Tuple1(3), 2L), (Tuple1(5), 1L)
)
}

test("groupBy columns, map") {
val ds = Seq(("a", 10), ("a", 20), ("b", 1), ("b", 2), ("c", 1)).toDS()
val grouped = ds.groupBy($"_1")
Expand All @@ -282,6 +292,15 @@ class DatasetSuite extends QueryTest with SharedSQLContext {
("a", 30), ("b", 3), ("c", 1))
}

test("groupBy columns, count") {
val ds = Seq("a" -> 1, "b" -> 1, "a" -> 2).toDS()
val count = ds.groupBy($"_1").count()

checkAnswer(
count,
(Row("a"), 2L), (Row("b"), 1L))
}

test("groupBy columns asKey, map") {
val ds = Seq(("a", 10), ("a", 20), ("b", 1), ("b", 2), ("c", 1)).toDS()
val grouped = ds.groupBy($"_1").keyAs[String]
Expand Down
6 changes: 3 additions & 3 deletions sql/core/src/test/scala/org/apache/spark/sql/QueryTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ abstract class QueryTest extends PlanTest {
* for cases where reordering is done on fields. For such tests, user `checkDecoding` instead
* which performs a subset of the checks done by this function.
*/
protected def checkAnswer[T : Encoder](
ds: => Dataset[T],
protected def checkAnswer[T](
ds: Dataset[T],
expectedAnswer: T*): Unit = {
checkAnswer(
ds.toDF(),
sqlContext.createDataset(expectedAnswer).toDF().collect().toSeq)
sqlContext.createDataset(expectedAnswer)(ds.unresolvedTEncoder).toDF().collect().toSeq)

checkDecoding(ds, expectedAnswer: _*)
}
Expand Down