Github user liancheng commented on a diff in the pull request:
https://github.com/apache/spark/pull/2501#discussion_r18081221
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/LogicalPlan.scala
---
@@ -73,6 +74,52 @@ abstract class LogicalPlan extends
QueryPlan[LogicalPlan] with Logging {
def childrenResolved: Boolean = !children.exists(!_.resolved)
/**
+ * Returns true when the given logical plan will return the same results
as this logical plan.
+ *
+ * Since its likely undecideable to generally determine if two given
plans will produce the same
+ * results, it is okay for this function to return false, even if the
results are actually
+ * the same. Such behavior will not affect correctness, only the
application of performance
+ * enhancements like caching. However, it is not acceptable to return
true if the results could
+ * possibly be different.
+ *
+ * By default this function performs a modified version of equality that
is tolerant of cosmetic
+ * differences like attribute naming and or expression id differences.
Logical operators that
+ * can do better should override this function.
+ */
+ def sameResult(plan: LogicalPlan): Boolean = {
+ if (plan.children.size != children.size) {
+ false
+ } else if (plan.children.zip(children).exists { case (l, r) =>
!l.sameResult(r) }) {
+ false
+ } else if (plan.getClass != this.getClass) {
+ false
+ } else {
+ logDebug(
+ s"[${cleanArgs.mkString(", ")}] == [${plan.cleanArgs.mkString(",
")}]")
+ cleanArgs == plan.cleanArgs
+ }
+ }
--- End diff --
Moving class and size comparison first would be more efficient:
```scala
plan.getClass == this.getClass &&
plan.children.size == children.size && {
logDebug(s"[${cleanArgs.mkString(", ")}] ==
[${plan.cleanArgs.mkString(", ")}]")
cleanArgs == plan.cleanArgs
} &&
(plan.children, children).zipped.forall(_ sameResult _)
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]