uros-db commented on code in PR #46040:
URL: https://github.com/apache/spark/pull/46040#discussion_r1565417498


##########
sql/core/src/test/scala/org/apache/spark/sql/CollationStringExpressionsSuite.scala:
##########
@@ -163,6 +163,155 @@ class CollationStringExpressionsSuite
     })
   }
 
+  test("Support Left/Right/Substr with implicit collation") {
+    case class SubstringTestCase(query: String, collation: String, result: Row)
+    val longString = "In the course of human events"
+    val checks = Seq("utf8_binary_lcase", "utf8_binary", "unicode", 
"unicode_ci").flatMap(
+      c => Seq(
+        SubstringTestCase(s"select left(left('$longString' collate " + c + ", 
5), 1)", c, Row("I")),
+        SubstringTestCase(
+          s"select right(right('$longString' collate " + c + ", 5), 1)", c, 
Row("s")),
+        SubstringTestCase(
+          s"select substr(substr('$longString' collate " + c + ", 4), 2)", c,
+          Row("he course of human events"))
+      )
+    )
+
+    checks.foreach { check =>
+      // Result & data type
+      checkAnswer(sql(check.query), check.result)
+      
assert(sql(check.query).schema.fields.head.dataType.sameType(StringType(check.collation)))
+    }
+  }
+
+  test("Support Left/Right/Substr with explicit proper collation") {
+    case class SubstringTestCase(query: String, collation: String, result: Row)
+    val checks = Seq("utf8_binary_lcase", "utf8_binary", "unicode", 
"unicode_ci").flatMap(
+      c => Seq(
+        SubstringTestCase("select left('abc' collate " + c + ", 1)", c, 
Row("a")),
+        SubstringTestCase("select right('def' collate " + c + ", 1)", c, 
Row("f")),
+        SubstringTestCase("select substr('abc' collate " + c + ", 2)", c, 
Row("bc")),
+        SubstringTestCase("select substr('example' collate " + c + ", 0, 2)", 
c, Row("ex")),
+        SubstringTestCase("select substr('example' collate " + c + ", 1, 2)", 
c, Row("ex")),
+        SubstringTestCase("select substr('example' collate " + c + ", 0, 7)", 
c, Row("example")),
+        SubstringTestCase("select substr('example' collate " + c + ", 1, 7)", 
c, Row("example")),
+        SubstringTestCase("select substr('example' collate " + c + ", 0, 
100)", c, Row("example")),
+        SubstringTestCase("select substr('example' collate " + c + ", 1, 
100)", c, Row("example")),
+        SubstringTestCase("select substr('example' collate " + c + ", 2, 2)", 
c, Row("xa")),
+        SubstringTestCase("select substr('example' collate " + c + ", 1, 6)", 
c, Row("exampl")),
+        SubstringTestCase("select substr('example' collate " + c + ", 2, 
100)", c, Row("xample")),
+        SubstringTestCase("select substr('example' collate " + c + ", 0, 0)", 
c, Row("")),
+        SubstringTestCase("select substr('example' collate " + c + ", 100, 
4)", c, Row("")),
+        SubstringTestCase("select substr('example' collate " + c + ", 0, 
100)", c, Row("example")),
+        SubstringTestCase("select substr('example' collate " + c + ", 1, 
100)", c, Row("example")),
+        SubstringTestCase("select substr('example' collate " + c + ", 2, 
100)", c, Row("xample")),
+        SubstringTestCase("select substr('example' collate " + c + ", -3, 2)", 
c, Row("pl")),
+        SubstringTestCase("select substr('example' collate " + c + ", -100, 
4)", c, Row("")),
+        SubstringTestCase("select substr('example' collate " + c + ", 
-2147483648, 6)", c, Row("")),
+        SubstringTestCase("select substr(' a世a ' collate " + c + ", 2, 3)", c, 
Row("a世a")), // scalastyle:ignore
+        SubstringTestCase("select left(' a世a ' collate " + c + ", 3)", c, 
Row(" a世")), // scalastyle:ignore
+        SubstringTestCase("select right(' a世a ' collate " + c + ", 3)", c, 
Row("世a ")), // scalastyle:ignore
+        SubstringTestCase("select substr('AaAaAaAa000000' collate " + c + ", 
2, 3)", c, Row("aAa")),
+        SubstringTestCase("select left('AaAaAaAa000000' collate " + c + ", 
3)", c, Row("AaA")),
+        SubstringTestCase("select right('AaAaAaAa000000' collate " + c + ", 
3)", c, Row("000")),
+        SubstringTestCase("select substr('' collate " + c + ", 1, 1)", c, 
Row("")),
+        SubstringTestCase("select left('' collate " + c + ", 1)", c, Row("")),
+        SubstringTestCase("select right('' collate " + c + ", 1)", c, Row("")),
+        SubstringTestCase("select left('ghi' collate " + c + ", 1)", c, 
Row("g"))

Review Comment:
   I don't think we need this many test cases here, if you didn't modify the 
way Substring/Left/Right expressions behave when given collated strings (i.e. 
you didn't introduce any collation awareness to nullSafeEval/doCodeGen), then 
there should be no need to go this deep - a couple of test cases should do the 
trick just fine
   
   also, I think these tests can be combined with the one above to make:
   `test("Support Left/Right/Substr with collation") {`
   
   so that we could have something like:
   ```
   checks.foreach { check =>
   // Result & data type (explicit collation)
   ...
   // Result & data type (implicit collation)
   ...
   ```



-- 
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]

Reply via email to