GideonPotok commented on code in PR #46040:
URL: https://github.com/apache/spark/pull/46040#discussion_r1566782558


##########
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)))
+    })
+  }
+
+  test("Support Left/Right/Substr with session 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', 1)", c, Row("a")),
+          SubstringTestCase("select right('def', 1)", c, Row("f")),
+          SubstringTestCase("select substr('ghi', 1)", c, Row("ghi"))
+        )
+      }
+    checks.foreach(check => {
+      withSQLConf(SqlApiConf.DEFAULT_COLLATION -> check.collation) {
+        // 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 on struct fields with collation") {
+    Seq(None, Some("utf8_binary_lcase"), Some("utf8_binary"), Some("unicode"), 
Some("unicode_ci"))
+      .zipWithIndex
+      .foreach {
+        case (collationNameMaybe, i) =>
+          withTable(s"t1234$i") {
+            sql(s"CREATE TABLE t1234$i(i STRING, s" +
+              " struct<a: string" +
+              collationNameMaybe.map(cn => " collate " + cn).getOrElse("") +
+              ">) USING parquet")
+            (1 to 5).map(n => "a" + " " * n).foreach { v =>
+              sql(s"INSERT OVERWRITE t1234$i VALUES ('1', named_struct('a', 
'$v'))")
+            }
+            assert(sql(s"SELECT i, left(s.a, 1) FROM 
t1234$i").schema(1).dataType ==
+              collationNameMaybe.map(cn =>
+                StringType(cn)).getOrElse(StringType))
+            assert(sql(s"SELECT i, right(s.a, 1) FROM 
t1234$i").schema(1).dataType ==
+              collationNameMaybe.map(cn =>
+                StringType(cn)).getOrElse(StringType))
+            assert(sql(s"SELECT i, substr(s.a, 1, 0) FROM 
t1234$i").schema(1).dataType ==
+              collationNameMaybe.map(cn =>
+                StringType(cn)).getOrElse(StringType))
+          }
+      }
+  }

Review Comment:
   Above is the reason. I removed the test, but if you want me to reinstate it 
just let me know.



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