Skip to content

Commit 9142bfc

Browse files
committed
review
1 parent acdff36 commit 9142bfc

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,22 +4000,22 @@ object ApplyCharTypePadding extends Rule[LogicalPlan] {
40004000
// String literal is treated as char type when it's compared to a char type column.
40014001
// We should pad the shorter one to the longer length.
40024002
case b @ BinaryComparison(attr: Attribute, lit) if lit.foldable =>
4003-
padAttrLitCmp(attr, lit).map { newChildren =>
4003+
padAttrLitCmp(attr, attr.metadata, lit).map { newChildren =>
40044004
b.withNewChildren(newChildren)
40054005
}.getOrElse(b)
40064006

40074007
case b @ BinaryComparison(lit, attr: Attribute) if lit.foldable =>
4008-
padAttrLitCmp(attr, lit).map { newChildren =>
4008+
padAttrLitCmp(attr, attr.metadata, lit).map { newChildren =>
40094009
b.withNewChildren(newChildren.reverse)
40104010
}.getOrElse(b)
40114011

4012-
case b @ BinaryComparison(OuterReference(attr: Attribute), lit) if lit.foldable =>
4013-
padOuterAttrLitComp(attr, lit).map { newChildren =>
4012+
case b @ BinaryComparison(or @ OuterReference(attr: Attribute), lit) if lit.foldable =>
4013+
padAttrLitCmp(or, attr.metadata, lit).map { newChildren =>
40144014
b.withNewChildren(newChildren)
40154015
}.getOrElse(b)
40164016

4017-
case b @ BinaryComparison(lit, OuterReference(attr: Attribute)) if lit.foldable =>
4018-
padOuterAttrLitComp(attr, lit).map { newChildren =>
4017+
case b @ BinaryComparison(lit, or @ OuterReference(attr: Attribute)) if lit.foldable =>
4018+
padAttrLitCmp(or, attr.metadata, lit).map { newChildren =>
40194019
b.withNewChildren(newChildren.reverse)
40204020
}.getOrElse(b)
40214021

@@ -4053,19 +4053,22 @@ object ApplyCharTypePadding extends Rule[LogicalPlan] {
40534053
}
40544054
}
40554055

4056-
private def padAttrLitCmp(attr: Attribute, lit: Expression): Option[Seq[Expression]] = {
4057-
if (attr.dataType == StringType) {
4058-
CharVarcharUtils.getRawType(attr.metadata).flatMap {
4056+
private def padAttrLitCmp(
4057+
expr: Expression,
4058+
metadata: Metadata,
4059+
lit: Expression): Option[Seq[Expression]] = {
4060+
if (expr.dataType == StringType) {
4061+
CharVarcharUtils.getRawType(metadata).flatMap {
40594062
case CharType(length) =>
40604063
val str = lit.eval().asInstanceOf[UTF8String]
40614064
if (str == null) {
40624065
None
40634066
} else {
40644067
val stringLitLen = str.numChars()
40654068
if (length < stringLitLen) {
4066-
Some(Seq(StringRPad(attr, Literal(stringLitLen)), lit))
4069+
Some(Seq(StringRPad(expr, Literal(stringLitLen)), lit))
40674070
} else if (length > stringLitLen) {
4068-
Some(Seq(attr, StringRPad(lit, Literal(length))))
4071+
Some(Seq(expr, StringRPad(lit, Literal(length))))
40694072
} else {
40704073
None
40714074
}
@@ -4077,17 +4080,6 @@ object ApplyCharTypePadding extends Rule[LogicalPlan] {
40774080
}
40784081
}
40794082

4080-
private def padOuterAttrLitComp(
4081-
outerAttr: Attribute,
4082-
lit: Expression): Option[Seq[Expression]] = {
4083-
padAttrLitCmp(outerAttr, lit).map { case Seq(newAttr, newLit) =>
4084-
val newOuterRef = newAttr.transform {
4085-
case ar: Attribute if ar.semanticEquals(outerAttr) => OuterReference(ar)
4086-
}
4087-
Seq(newOuterRef, newLit)
4088-
}
4089-
}
4090-
40914083
private def padOuterRefAttrCmp(outerAttr: Attribute, attr: Attribute): Seq[Expression] = {
40924084
val Seq(r, newAttr) = CharVarcharUtils.addPaddingInStringComparison(Seq(outerAttr, attr))
40934085
val newOuterRef = r.transform {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,6 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
653653
| FROM t
654654
| WHERE c0 = t1.c0 AND $predicate
655655
|) > 0
656-
|LIMIT 3
657656
""".stripMargin),
658657
Row(1))
659658
}

0 commit comments

Comments
 (0)