Github user brkyvz commented on a diff in the pull request:
https://github.com/apache/spark/pull/7060#discussion_r33499886
--- Diff:
sql/core/src/test/scala/org/apache/spark/sql/DataFrameStatSuite.scala ---
@@ -65,22 +67,22 @@ class DataFrameStatSuite extends SparkFunSuite {
}
test("crosstab") {
- val df = Seq((0, 0), (2, 1), (1, 0), (2, 0), (0, 0), (2, 0)).toDF("a",
"b")
+ val rng = new Random()
+ val data = Seq.tabulate(25)(i => (rng.nextInt(5), rng.nextInt(10)))
+ val df = data.toDF("a", "b")
val crosstab = df.stat.crosstab("a", "b")
val columnNames = crosstab.schema.fieldNames
assert(columnNames(0) === "a_b")
- assert(columnNames(1) === "0")
- assert(columnNames(2) === "1")
- val rows: Array[Row] = crosstab.collect().sortBy(_.getString(0))
- assert(rows(0).get(0).toString === "0")
- assert(rows(0).getLong(1) === 2L)
- assert(rows(0).get(2) === 0L)
- assert(rows(1).get(0).toString === "1")
- assert(rows(1).getLong(1) === 1L)
- assert(rows(1).get(2) === 0L)
- assert(rows(2).get(0).toString === "2")
- assert(rows(2).getLong(1) === 2L)
- assert(rows(2).getLong(2) === 1L)
+ // reduce by key
+ val expected = data.map(t => (t, 1)).groupBy(_._1).mapValues(_.length)
+ val rows = crosstab.collect()
+ rows.foreach { row =>
+ val i = row.getString(0).toInt
+ for (col <- 1 to 9) {
+ val j = columnNames(col).toInt
+ assert(row.getLong(col) === expected.getOrElse((i, j), 0).toLong)
--- End diff --
That would need a lot of work, because we don't know the ordering of the
rows and the columns
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]