Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.apache.spark.sql.catalyst.plans.logical
import scala.annotation.tailrec

import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.util.UnsafeRowUtils.isBinaryStable


trait QueryPlanConstraints extends ConstraintHelper { self: LogicalPlan =>
Expand Down Expand Up @@ -65,15 +66,18 @@ trait ConstraintHelper {
// IsNotNull should be constructed by `constructIsNotNullConstraints`.
val predicates = constraints.filterNot(_.isInstanceOf[IsNotNull])
predicates.foreach {
case eq @ EqualTo(l: Attribute, r: Attribute) =>
case eq @ EqualTo(l: Attribute, r: Attribute)
if isBinaryStable(l.dataType) && isBinaryStable(r.dataType) =>
// Also remove EqualNullSafe with the same l and r to avoid Once strategy's idempotence
// is broken. l = r and l <=> r can infer l <=> l and r <=> r which is useless.
val candidateConstraints = predicates - eq - EqualNullSafe(l, r)
inferredConstraints ++= replaceConstraints(candidateConstraints, l, r)
inferredConstraints ++= replaceConstraints(candidateConstraints, r, l)
case eq @ EqualTo(l @ Cast(_: Attribute, _, _, _), r: Attribute) =>
case eq @ EqualTo(l @ Cast(lc: Attribute, _, _, _), r: Attribute)
if isBinaryStable(lc.dataType) && isBinaryStable(r.dataType) =>
inferredConstraints ++= replaceConstraints(predicates - eq - EqualNullSafe(l, r), r, l)
case eq @ EqualTo(l: Attribute, r @ Cast(_: Attribute, _, _, _)) =>
case eq @ EqualTo(l: Attribute, r @ Cast(rc: Attribute, _, _, _))
if isBinaryStable(l.dataType) && isBinaryStable(rc.dataType) =>
inferredConstraints ++= replaceConstraints(predicates - eq - EqualNullSafe(l, r), l, r)
case _ => // No inference
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,18 @@ class CollationSuite extends DatasourceV2SQLBase with AdaptiveSparkPlanHelper {
}
}

test("SPARK-57727: constraint inference does not substitute non-binary-stable attributes") {
withTable("t1") {
sql("CREATE TABLE t1 (a STRING COLLATE UTF8_LCASE, b STRING COLLATE UTF8_LCASE)")
sql("INSERT INTO t1 VALUES ('hello', 'HELLO')")

checkAnswer(
sql("SELECT a, b FROM t1 WHERE a = b AND a = 'hello' COLLATE UTF8_BINARY"),
Row("hello", "HELLO")
)
}
}

test("ConstantPropagation: replaces binary-stable attributes with contradicting predicates") {
withTable("t1") {
sql("CREATE TABLE t1 (c STRING)")
Expand Down