Github user dongjoon-hyun commented on a diff in the pull request:
https://github.com/apache/spark/pull/20887#discussion_r194321737
--- Diff: sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
---
@@ -2792,4 +2793,40 @@ class SQLQuerySuite extends QueryTest with
SharedSQLContext {
}
}
}
+
+ test("`Cast` to CHAR/VARCHAR should truncate the values") {
+ withTable("t") {
+ val m = intercept[ParseException] {
+ sql("SELECT CAST('abc' AS CHAR(0))")
+ }.getMessage
+ assert(m.contains("Char length 0 is out of range [1, 255]"))
+
+ val m2 = intercept[ParseException] {
+ sql("SELECT CAST('abc' AS VARCHAR(0))")
+ }.getMessage
+ assert(m2.contains("VarChar length 0 is out of range [1, 65535]"))
+
+ checkAnswer(
+ sql("SELECT CAST('abc' AS CHAR(2)), CAST('abc' AS CHAR(4))"),
+ Row("ab", "abc"))
+
+ sql("CREATE TABLE t(a STRING) USING PARQUET")
+ sql("INSERT INTO t VALUES ('abc')")
+ sql("INSERT INTO t VALUES (null)")
+
+ checkAnswer(
+ sql("SELECT CAST(a AS CHAR(2)), CAST(a AS CHAR(3)), CAST(a AS
CHAR(4)) FROM t"),
+ Row("ab", "abc", "abc") :: Row(null, null, null) :: Nil)
+
+ sql(
+ """
+ |CREATE TABLE t_ctas
+ |USING ORC
+ |AS SELECT CAST(a AS CHAR(2)) c1, CAST(a AS CHAR(3)) c2, CAST(a
AS CHAR(4)) c3 FROM t
--- End diff --
What do you mean by `embedded`? This works in a subquery and in an
expression like function arguments. Could you give us some examples?
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]