Skip to content

Commit 2653671

Browse files
committed
Fix nested char padding
1 parent ba92e8e commit 2653671

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,19 @@ object CharVarcharUtils extends Logging with SparkCharVarcharUtils {
303303
val fieldExpr = GetStructField(expr, i, Some(field.name))
304304
val padded = padCharToTargetLength(
305305
fieldExpr, field.dataType, targets(i).dataType, alwaysPad)
306-
needPadding = padded.isDefined
306+
needPadding |= padded.isDefined
307307
createStructExprs += Literal(field.name)
308308
createStructExprs += padded.getOrElse(fieldExpr)
309309
i += 1
310310
}
311-
if (needPadding) Some(CreateNamedStruct(createStructExprs.toSeq)) else None
311+
val struct = if (needPadding) Some(CreateNamedStruct(createStructExprs.toSeq)) else None
312+
struct.map { padded =>
313+
if (expr.nullable) {
314+
If(IsNull(expr), Literal(null, padded.dataType), padded)
315+
} else {
316+
padded
317+
}
318+
}
312319

313320
case (ArrayType(et, containsNull), ArrayType(target, _)) =>
314321
val param = NamedLambdaVariable("x", replaceCharVarcharWithString(et), containsNull)

sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,18 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
541541
}
542542
}
543543

544+
test("char type comparison: nested in struct with multiple fields") {
545+
withTable("t") {
546+
sql(s"CREATE TABLE t(c1 STRUCT<c: CHAR(2), d: CHAR(3)>, c2 STRUCT<c: CHAR(5), d: CHAR(3)>) " +
547+
s"USING $format")
548+
sql("INSERT INTO t VALUES (struct('a', 'b'), struct('a', 'b'))")
549+
testConditions(spark.table("t"), Seq(
550+
("c1 = c2", true),
551+
("c1 < c2", false),
552+
("c1 IN (c2)", true)))
553+
}
554+
}
555+
544556
test("char type comparison: nested in array") {
545557
withTable("t") {
546558
sql(s"CREATE TABLE t(c1 ARRAY<CHAR(2)>, c2 ARRAY<CHAR(5)>) USING $format")
@@ -576,6 +588,15 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
576588
}
577589
}
578590

591+
test("char type comparison: nested in array of struct with nulls") {
592+
withTable("t") {
593+
sql("CREATE TABLE t(c1 ARRAY<STRUCT<c: CHAR(2)>>, c2 ARRAY<STRUCT<c: CHAR(5)>>) " +
594+
s"USING $format")
595+
sql("INSERT INTO t VALUES (array(NULL), array(struct(NULL)))")
596+
testConditions(spark.table("t"), Seq(("c1 = c2", false)))
597+
}
598+
}
599+
579600
test("char type comparison: nested in array of array") {
580601
withTable("t") {
581602
sql("CREATE TABLE t(c1 ARRAY<ARRAY<CHAR(2)>>, c2 ARRAY<ARRAY<CHAR(5)>>) " +

0 commit comments

Comments
 (0)