cloud-fan commented on code in PR #60661:
URL: https://github.com/apache/spark/pull/60661#discussion_r4626469816
##########
sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala:
##########
@@ -619,6 +619,137 @@ trait CharVarcharTestSuite extends QueryTest {
}
}
+ test("char type comparison: IN list mixing CHAR columns with other
elements") {
+ // `c IN (e1, e2)` must return what `c = e1 OR c = e2` returns, where each
equality pads its
+ // own sides: c is padded against a CHAR column of another length and a
string literal, but
+ // not against a STRING or VARCHAR column.
+ val lists = Seq(
+ Seq("c5", "s"), Seq("s", "c5"), Seq("c5", "v"), Seq("c5", "'x'"),
Seq("s", "'a'"),
+ Seq("c5", "s", "v", "'x'", "null"), Seq("c5b", "s"), Seq("c5b", "v"),
Seq("c5b", "s", "'b'"),
+ Seq("s", "v"))
+
+ def checkInAsOr(table: String, value: String): Unit = {
+ val df = spark.table(table)
+ val ins = lists.flatMap { list =>
+ val elems = list.mkString(", ")
+ Seq(s"$value IN ($elems)", s"$value NOT IN ($elems)")
+ }
+ val ors = lists.flatMap { list =>
+ val or = list.map(e => s"$value = $e").mkString(" OR ")
+ Seq(s"$or", s"NOT ($or)")
+ }
+ checkAnswer(df.selectExpr(ins: _*), df.selectExpr(ors: _*))
+ }
+
+ withTable("t") {
+ sql(s"CREATE TABLE t(c CHAR(2), c5 CHAR(5), c5b CHAR(5), s STRING, v
VARCHAR(5)) " +
+ s"USING $format")
+ sql("INSERT INTO t VALUES ('a', 'a', 'b', 'a', 'a')")
+ testConditions(spark.table("t"), Seq(
+ ("c = c5", true),
+ ("c = s", false),
+ ("c = v", false),
+ ("c IN (c5, s)", true),
+ ("c IN (s, c5)", true),
+ ("c NOT IN (c5, s)", false),
+ ("c IN (c5, v)", true),
+ ("c IN (c5, 'x')", true),
+ ("c IN (s, 'a')", true),
+ ("c IN (c5, s, v, 'x', null)", true),
+ // s and v hold 'a' without the padding of c, so they must not be
padded to match it.
+ ("c IN (c5b, s)", false),
+ ("c IN (c5b, v)", false),
+ ("c IN (c5b, s, 'b')", false),
+ ("c IN (s, v)", false)))
+ checkInAsOr("t", "c")
+
+ // A list of only columns and literals keeps all its literals in one IN.
+ val ins = spark.table("t").selectExpr("c IN (c5, 'x', s,
'y')").queryExecution.analyzed
+ .flatMap(_.expressions.flatMap(_.collect { case i: In => i }))
+ assert(ins.length == 3 && ins.exists(i => i.list.length == 2 &&
i.list.forall(_.foldable)))
Review Comment:
Added in acda41b8b0a: `c IN (c2, s)` with `c` and `c2` both `CHAR(2)` keeps
a single IN with both elements in the analyzed plan.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala:
##########
@@ -98,6 +100,25 @@ object ApplyCharTypePaddingHelper {
}
.getOrElse(b)
+ // `value IN (e1, e2, ...)` returns what `value = e1 OR value = e2 OR
...` returns, and each
+ // of these comparisons pads its sides on its own. The two `In` cases
below pad the value
+ // and all the elements to a common length, which is only right if all
the elements are
+ // padded the same way. If the list mixes elements that are padded in
different ways, e.g. a
+ // CHAR column and a STRING column that is not padded at all, split the
IN into an OR of
+ // INs whose elements are each padded the same way, so that the value is
padded for the
+ // elements of each IN on its own. `InWithMixedPadding` describes how
the list is split, and
+ // which elements are not padded yet.
Review Comment:
Fixed in acda41b8b0a with your wording.
--
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]