MaxGekk commented on code in PR #47620:
URL: https://github.com/apache/spark/pull/47620#discussion_r1712935837
##########
sql/core/src/test/scala/org/apache/spark/sql/CollationStringExpressionsSuite.scala:
##########
@@ -17,135 +17,130 @@
package org.apache.spark.sql
-import org.apache.spark.{SparkConf, SparkIllegalArgumentException}
-import org.apache.spark.sql.catalyst.expressions.{ExpressionEvalHelper,
Literal, StringTrim, StringTrimLeft, StringTrimRight}
-import org.apache.spark.sql.catalyst.util.CollationFactory
+import org.apache.spark.SparkConf
+import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSparkSession
-import org.apache.spark.sql.types.{ArrayType, BinaryType, BooleanType,
DataType, IntegerType, StringType}
+import org.apache.spark.sql.types._
// scalastyle:off nonascii
class CollationStringExpressionsSuite
extends QueryTest
with SharedSparkSession
with ExpressionEvalHelper {
- test("Support ConcatWs string expression with collation") {
- // Supported collations
- case class ConcatWsTestCase[R](s: String, a: Array[String], c: String,
result: R)
+ test("Support `ConcatWs` string expression with collation") {
+ case class ConcatWsTestCase[R](
+ sep: String,
+ arrayStr: Array[String],
+ collation: String,
+ result: R)
val testCases = Seq(
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UTF8_BINARY", "Spark SQL"),
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UTF8_LCASE", "Spark SQL"),
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UNICODE", "Spark SQL"),
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UNICODE_CI", "Spark SQL")
)
testCases.foreach(t => {
- val arrCollated = t.a.map(s => s"collate('$s', '${t.c}')").mkString(", ")
- var query = s"SELECT concat_ws(collate('${t.s}', '${t.c}'),
$arrCollated)"
- // Result & data type
- checkAnswer(sql(query), Row(t.result))
- assert(sql(query).schema.fields.head.dataType.sameType(StringType(t.c)))
- // Implicit casting
- val arr = t.a.map(s => s"'$s'").mkString(", ")
- query = s"SELECT concat_ws(collate('${t.s}', '${t.c}'), $arr)"
- checkAnswer(sql(query), Row(t.result))
- assert(sql(query).schema.fields.head.dataType.sameType(StringType(t.c)))
- query = s"SELECT concat_ws('${t.s}', $arrCollated)"
- checkAnswer(sql(query), Row(t.result))
- assert(sql(query).schema.fields.head.dataType.sameType(StringType(t.c)))
+ // Unit test.
+ val inputExprs = t.arrayStr.map {
+ case null => Literal.create(null, StringType(t.collation))
+ case s: String => Literal.create(s, StringType(t.collation))
+ }
+ val sepExpr = Literal.create(t.sep, StringType(t.collation))
+ checkEvaluation(ConcatWs(sepExpr +: inputExprs.toIndexedSeq), t.result)
+ // E2E SQL test.
+ withSQLConf(SQLConf.DEFAULT_COLLATION.key -> t.collation) {
+ val array = t.arrayStr.map(s => s"'$s'").mkString(", ")
+ val query = s"select concat_ws('${t.sep}', $array)"
+ checkAnswer(sql(query), Row(t.result))
+
assert(sql(query).schema.fields.head.dataType.sameType(StringType(t.collation)))
+ }
})
- // Collation mismatch
- checkError(
- exception = intercept[AnalysisException] {
- sql("SELECT concat_ws(' ', collate('Spark', 'UTF8_LCASE'),
collate('SQL', 'UNICODE'))")
- },
- errorClass = "COLLATION_MISMATCH.EXPLICIT",
- parameters = Map("explicitTypes" -> "`string collate UTF8_LCASE`,
`string collate UNICODE`")
Review Comment:
Did you move this test somewhere or it is duplicate of another test?
##########
sql/core/src/test/scala/org/apache/spark/sql/CollationStringExpressionsSuite.scala:
##########
@@ -17,135 +17,130 @@
package org.apache.spark.sql
-import org.apache.spark.{SparkConf, SparkIllegalArgumentException}
-import org.apache.spark.sql.catalyst.expressions.{ExpressionEvalHelper,
Literal, StringTrim, StringTrimLeft, StringTrimRight}
-import org.apache.spark.sql.catalyst.util.CollationFactory
+import org.apache.spark.SparkConf
+import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSparkSession
-import org.apache.spark.sql.types.{ArrayType, BinaryType, BooleanType,
DataType, IntegerType, StringType}
+import org.apache.spark.sql.types._
// scalastyle:off nonascii
class CollationStringExpressionsSuite
extends QueryTest
with SharedSparkSession
with ExpressionEvalHelper {
- test("Support ConcatWs string expression with collation") {
- // Supported collations
- case class ConcatWsTestCase[R](s: String, a: Array[String], c: String,
result: R)
+ test("Support `ConcatWs` string expression with collation") {
+ case class ConcatWsTestCase[R](
+ sep: String,
+ arrayStr: Array[String],
+ collation: String,
+ result: R)
val testCases = Seq(
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UTF8_BINARY", "Spark SQL"),
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UTF8_LCASE", "Spark SQL"),
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UNICODE", "Spark SQL"),
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UNICODE_CI", "Spark SQL")
)
testCases.foreach(t => {
- val arrCollated = t.a.map(s => s"collate('$s', '${t.c}')").mkString(", ")
- var query = s"SELECT concat_ws(collate('${t.s}', '${t.c}'),
$arrCollated)"
- // Result & data type
- checkAnswer(sql(query), Row(t.result))
- assert(sql(query).schema.fields.head.dataType.sameType(StringType(t.c)))
- // Implicit casting
- val arr = t.a.map(s => s"'$s'").mkString(", ")
- query = s"SELECT concat_ws(collate('${t.s}', '${t.c}'), $arr)"
- checkAnswer(sql(query), Row(t.result))
- assert(sql(query).schema.fields.head.dataType.sameType(StringType(t.c)))
- query = s"SELECT concat_ws('${t.s}', $arrCollated)"
- checkAnswer(sql(query), Row(t.result))
- assert(sql(query).schema.fields.head.dataType.sameType(StringType(t.c)))
+ // Unit test.
+ val inputExprs = t.arrayStr.map {
+ case null => Literal.create(null, StringType(t.collation))
+ case s: String => Literal.create(s, StringType(t.collation))
+ }
+ val sepExpr = Literal.create(t.sep, StringType(t.collation))
+ checkEvaluation(ConcatWs(sepExpr +: inputExprs.toIndexedSeq), t.result)
+ // E2E SQL test.
+ withSQLConf(SQLConf.DEFAULT_COLLATION.key -> t.collation) {
+ val array = t.arrayStr.map(s => s"'$s'").mkString(", ")
+ val query = s"select concat_ws('${t.sep}', $array)"
+ checkAnswer(sql(query), Row(t.result))
+
assert(sql(query).schema.fields.head.dataType.sameType(StringType(t.collation)))
Review Comment:
Actually you repeats this 2 lines many times, how about to create a private
helper method.
##########
sql/core/src/test/scala/org/apache/spark/sql/CollationStringExpressionsSuite.scala:
##########
@@ -17,135 +17,130 @@
package org.apache.spark.sql
-import org.apache.spark.{SparkConf, SparkIllegalArgumentException}
-import org.apache.spark.sql.catalyst.expressions.{ExpressionEvalHelper,
Literal, StringTrim, StringTrimLeft, StringTrimRight}
-import org.apache.spark.sql.catalyst.util.CollationFactory
+import org.apache.spark.SparkConf
+import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSparkSession
-import org.apache.spark.sql.types.{ArrayType, BinaryType, BooleanType,
DataType, IntegerType, StringType}
+import org.apache.spark.sql.types._
// scalastyle:off nonascii
class CollationStringExpressionsSuite
extends QueryTest
with SharedSparkSession
with ExpressionEvalHelper {
- test("Support ConcatWs string expression with collation") {
- // Supported collations
- case class ConcatWsTestCase[R](s: String, a: Array[String], c: String,
result: R)
+ test("Support `ConcatWs` string expression with collation") {
+ case class ConcatWsTestCase[R](
+ sep: String,
+ arrayStr: Array[String],
+ collation: String,
+ result: R)
val testCases = Seq(
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UTF8_BINARY", "Spark SQL"),
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UTF8_LCASE", "Spark SQL"),
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UNICODE", "Spark SQL"),
ConcatWsTestCase(" ", Array("Spark", "SQL"), "UNICODE_CI", "Spark SQL")
)
testCases.foreach(t => {
- val arrCollated = t.a.map(s => s"collate('$s', '${t.c}')").mkString(", ")
- var query = s"SELECT concat_ws(collate('${t.s}', '${t.c}'),
$arrCollated)"
- // Result & data type
- checkAnswer(sql(query), Row(t.result))
- assert(sql(query).schema.fields.head.dataType.sameType(StringType(t.c)))
- // Implicit casting
Review Comment:
Did you remove the implicit casting tests? If so, please, explain why.
--
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]