GideonPotok commented on code in PR #46597:
URL: https://github.com/apache/spark/pull/46597#discussion_r1604955280
##########
sql/core/src/test/scala/org/apache/spark/sql/CollationSQLExpressionsSuite.scala:
##########
@@ -1404,6 +1408,81 @@ class CollationSQLExpressionsSuite
})
}
+ test("Support mode for string expression with collation - Basic Test") {
+ Seq("utf8_binary", "utf8_binary_lcase", "unicode_ci", "unicode").foreach {
collationId =>
+ val query = s"SELECT mode(collate('abc', '${collationId}'))"
+ checkAnswer(sql(query), Row("abc"))
+
assert(sql(query).schema.fields.head.dataType.sameType(StringType(collationId)))
+ }
+ }
+
+ test("Support mode for string expression with collation - Advanced Test") {
+ case class ModeTestCase[R](collationId: String, bufferValues: Map[String,
Long], result: R)
+ val testCases = Seq(
+ ModeTestCase("utf8_binary", Map("a" -> 3L, "b" -> 2L, "B" -> 2L), "a"),
+ ModeTestCase("utf8_binary_lcase", Map("a" -> 3L, "b" -> 2L, "B" -> 2L),
"b"),
+ ModeTestCase("unicode_ci", Map("a" -> 3L, "b" -> 2L, "B" -> 2L), "b"),
+ ModeTestCase("unicode", Map("a" -> 3L, "b" -> 2L, "B" -> 2L), "a")
+ )
+ testCases.foreach(t => {
+ val valuesToAdd = t.bufferValues.map { case (elt, numRepeats) =>
+ (0L to numRepeats).map(_ => s"('$elt')").mkString(",")
+ }.mkString(",")
+
+ withTable("t") {
+ sql("CREATE TABLE t(i STRING) USING parquet")
+ sql("INSERT INTO t VALUES " + valuesToAdd)
+ val query = s"SELECT mode(collate(i, '${t.collationId}')) FROM t"
+ checkAnswer(sql(query), Row(t.result))
+
assert(sql(query).schema.fields.head.dataType.sameType(StringType(t.collationId)))
+
+ }
+ })
+ }
+
+ test("Support Mode.eval(buffer)") {
+ case class ModeTestCase[R](
+ collationId: String,
+ bufferValues: Map[String, Long],
+ result: R)
+ case class UTF8StringModeTestCase[R](
+ collationId: String,
+ bufferValues: Map[UTF8String, Long],
+ result: R)
+
+ val bufferValues = Map("a" -> 5L, "b" -> 4L, "B" -> 3L, "d" -> 2L, "e" ->
1L)
+ val testCasesStrings = Seq(ModeTestCase("utf8_binary", bufferValues, "a"),
+ ModeTestCase("utf8_binary_lcase", bufferValues, "b"),
+ ModeTestCase("unicode_ci", bufferValues, "b"),
+ ModeTestCase("unicode", bufferValues, "a"))
+
+ val bufferValuesUTF8String = Map(
+ UTF8String.fromString("a") -> 5L,
+ UTF8String.fromString("b") -> 4L,
+ UTF8String.fromString("B") -> 3L,
+ UTF8String.fromString("d") -> 2L,
+ UTF8String.fromString("e") -> 1L)
+
+ val testCasesUTF8String = Seq(
+ UTF8StringModeTestCase("utf8_binary", bufferValuesUTF8String, "a"),
+ UTF8StringModeTestCase("utf8_binary_lcase", bufferValuesUTF8String, "b"),
+ UTF8StringModeTestCase("unicode_ci", bufferValuesUTF8String, "b"),
+ UTF8StringModeTestCase("unicode", bufferValuesUTF8String, "a"))
+
+ testCasesStrings.foreach(t => {
+ val buffer = new OpenHashMap[AnyRef, Long](5)
+ val myMode = Mode(child = Literal.create("some_column_name",
StringType(t.collationId)))
+ t.bufferValues.foreach { case (k, v) => buffer.update(k, v) }
+ assert(myMode.eval(buffer).toString.toLowerCase() ==
t.result.toLowerCase())
+ })
+
+ testCasesUTF8String.foreach(t => {
+ val buffer = new OpenHashMap[AnyRef, Long](5)
+ val myMode = Mode(child = Literal.create("some_column_name",
StringType(t.collationId)))
+ t.bufferValues.foreach { case (k, v) => buffer.update(k, v) }
+ assert(myMode.eval(buffer).toString.toLowerCase() ==
t.result.toLowerCase())
+ })
+ }
Review Comment:
I would love to add some E2E Benchmark tests. But it is okay if not.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]