dongjoon-hyun commented on code in PR #49340: URL: https://github.com/apache/spark/pull/49340#discussion_r1912532289
########## sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala: ########## @@ -695,6 +695,89 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils { } } } + + test(s"insert string literal into char/varchar column when " + + s"${SQLConf.PRESERVE_CHAR_VARCHAR_TYPE_INFO.key} is true") { + withSQLConf(SQLConf.PRESERVE_CHAR_VARCHAR_TYPE_INFO.key -> "true") { + withTable("t") { + sql(s"CREATE TABLE t(c1 CHAR(5), c2 VARCHAR(5)) USING $format") + sql("INSERT INTO t VALUES ('1234', '1234')") + checkAnswer(spark.table("t"), Row("1234 ", "1234")) + assertLengthCheckFailure("INSERT INTO t VALUES ('123456', '1')") + assertLengthCheckFailure("INSERT INTO t VALUES ('1', '123456')") + } + } + } + + test(s"insert from string column into char/varchar column when " + + s"${SQLConf.PRESERVE_CHAR_VARCHAR_TYPE_INFO.key} is true") { + withSQLConf(SQLConf.PRESERVE_CHAR_VARCHAR_TYPE_INFO.key -> "true") { + withTable("a", "b") { + sql(s"CREATE TABLE a AS SELECT '1234' as c1, '1234' as c2") + sql(s"CREATE TABLE b(c1 CHAR(5), c2 VARCHAR(5)) USING $format") + sql("INSERT INTO b SELECT * FROM a") + checkAnswer(spark.table("b"), Row("1234 ", "1234")) + spark.table("b").show() + } + } + } + + test(s"cast from char/varchar when ${SQLConf.PRESERVE_CHAR_VARCHAR_TYPE_INFO.key} is true") { + withSQLConf(SQLConf.PRESERVE_CHAR_VARCHAR_TYPE_INFO.key -> "true") { + Seq("char(5)", "varchar(5)").foreach { typ => + Seq( + "int" -> ("123", 123), + "long" -> ("123 ", 123L), + "boolean" -> ("true ", true), + "boolean" -> ("false", false), + "double" -> ("1.2", 1.2) + ).foreach { case (toType, (from, to)) => + assert(sql(s"select cast($from :: $typ as $toType)").collect() === Array(Row(to))) + } + } + } + } + + test(s"cast to char/varchar when ${SQLConf.PRESERVE_CHAR_VARCHAR_TYPE_INFO.key} is true") { + withSQLConf(SQLConf.PRESERVE_CHAR_VARCHAR_TYPE_INFO.key -> "true") { + Seq("char(10)", "varchar(10)").foreach { typ => + Seq( + 123 -> "123", + 123L-> "123", + true -> "true", + false -> "false", + 1.2 -> "1.2" + ).foreach { case (from, to) => + val paddedTo = if (typ == "char(10)") { + to.padTo(10, ' ') + } else { + to + } + sql(s"select cast($from as $typ)").collect() === Array(Row(paddedTo)) + } + } + } + } + + test("implicitly cast char/varchar into atomics") { Review Comment: This test case seems to be the root cause. -- 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