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


##########
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"))
+      )
+    )
+
+    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 improper 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(null collate " + c + ", 1)", c, 
Row(null)),
+        SubstringTestCase("select right(null collate " + c + ", 1)", c, 
Row(null)),
+        SubstringTestCase("select substr(null collate " + c + ", 1)", c, 
Row(null)),
+        SubstringTestCase("select substr(null collate " + c + ", 1, 1)", c, 
Row(null)),
+        SubstringTestCase("select left('' collate " + c + ", null)", c, 
Row(null)),
+        SubstringTestCase("select right('' collate " + c + ", null)", c, 
Row(null)),
+        SubstringTestCase("select substr('' collate " + c + ", null)", c, 
Row(null)),
+        SubstringTestCase("select substr('' collate " + c + ", null, null)", 
c, Row(null))
+      )
+    )
+    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 improper length & position") {
+    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(' 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 right('' collate " + c + ", null)", c, 
Row(null)),
+        SubstringTestCase("select substr('' collate " + c + ", null)", c, 
Row(null)),
+        SubstringTestCase("select substr('' collate " + c + ", null, null)", 
c, Row(null)),
+        SubstringTestCase("select left('' collate " + c + ", null)", c, 
Row(null))
+      )
+    )
+    checks.foreach(check => {
+      // Result & data type
+      checkAnswer(sql(check.query), check.result)
+      
assert(sql(check.query).schema.fields.head.dataType.sameType(StringType(check.collation)))
+    })
+  }

Review Comment:
   improper length & position in context of collations should only matter with 
variable byte length characters, so make sure to use such examples in 
`test("Support Left/Right/Substr with collation")` to keep this test particular 
suite to a minimum



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to