Skip to content

Commit e3edc88

Browse files
wakunGitHub Enterprise
authored andcommitted
[CARMEL-7483] Avoid generating too many constraints (apache#145)
### What changes were proposed in this pull request? [CARMEL-3941] Backport [SPARK-29606][SQL] Improve EliminateOuterJoin performance [CARMEL-6278] Avoid generating constraints if isnull and isnotnull refer to multiple expressions ### Why are the changes needed? <!-- Please clarify why the changes are needed. For instance, 1. If you propose a new API, clarify the use case for a new API. 2. If you fix a bug, you can clarify why it is a bug. --> ### Does this PR introduce _any_ user-facing change? <!-- Note that it means *any* user-facing change including all aspects such as the documentation fix. If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible. If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master. If no, write 'No'. --> ### How was this patch tested? <!-- If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible. If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future. If tests were not added, please describe why they were not added and/or why it was difficult to add. If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks. -->
1 parent d1bb109 commit e3edc88

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/LogicalPlan.scala

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,20 @@ trait UnaryNode extends LogicalPlan with UnaryLike[LogicalPlan] {
223223
allConstraints += EqualNullSafe(a.toAttribute, l)
224224
case a @ Alias(e, _) if e.deterministic =>
225225
// For every alias in `projectList`, replace the reference in constraints by its attribute.
226-
allConstraints ++= allConstraints.map(_ transform {
227-
case expr: Expression if expr.semanticEquals(e) =>
228-
a.toAttribute
229-
})
226+
allConstraints ++= allConstraints.iterator.map {
227+
// To Avoid generating too many constraints, for example,
228+
// SELECT a AS a1, b AS b1, ab AS ab1 FROM (SELECT a, b, a + b AS ab FROM tbl) t
229+
// Avoid generating ((a#4 + b1#2) <=> ab#0) for ((a#4 + b#5) <=> ab#0).
230+
case e @ EqualNullSafe(l, _: AttributeReference) if l.references.size > 1 => e
231+
// SELECT a AS a1 FROM (SELECT a, b, a + b AS ab FROM tbl WHERE a + b is null) t
232+
// Avoid generating isnull((a#3 + b#2)) for isnull((a#1 + b#2))
233+
case e @ IsNull(l) if l.references.size > 1 => e
234+
case e @ IsNotNull(l) if l.references.size > 1 => e
235+
case other => other transform {
236+
case expr: Expression if expr.semanticEquals(e) =>
237+
a.toAttribute
238+
}
239+
}
230240
allConstraints += EqualNullSafe(e, a.toAttribute)
231241
case _ => // Don't change.
232242
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/ConstraintPropagationSuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,12 @@ class ConstraintPropagationSuite extends SparkFunSuite with PlanTest {
428428
assert(aliasedRelation.analyze.constraints.isEmpty)
429429
}
430430
}
431+
432+
test("SPARK-29606 Avoid generating too many constraints") {
433+
val aliasedRelation = LocalRelation('a.int, 'b.int, 'c.int)
434+
.select('a, 'b, 'c, ('a + 'b + 'c).as("abc"))
435+
.select('a.as("a1"), 'b.as("b1"), 'c.as("c1"), 'abc.as("abc1"))
436+
assert(aliasedRelation.analyze.isInstanceOf[Project])
437+
assert(aliasedRelation.analyze.asInstanceOf[Project].validConstraints.size === 5)
438+
}
431439
}

0 commit comments

Comments
 (0)