Github user aokolnychyi commented on a diff in the pull request: https://github.com/apache/spark/pull/18692#discussion_r130662925 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala --- @@ -152,3 +152,72 @@ object EliminateOuterJoin extends Rule[LogicalPlan] with PredicateHelper { if (j.joinType == newJoinType) f else Filter(condition, j.copy(joinType = newJoinType)) } } + +/** + * A rule that uses propagated constraints to infer join conditions. The optimization is applicable + * only to inner joins. + * + * For instance, if there is a join, where the left relation has 'a = 1' and the right relation + * has 'b = 1', then the rule infers 'a = b' as a join predicate. Only semantically new predicates + * are appended to the existing join condition. + */ +object InferJoinConditionsFromConstraints extends Rule[LogicalPlan] with PredicateHelper { + + def apply(plan: LogicalPlan): LogicalPlan = { + if (SQLConf.get.constraintPropagationEnabled) { + inferJoinConditions(plan) + } else { + plan + } + } + + private def inferJoinConditions(plan: LogicalPlan): LogicalPlan = plan transform { + case join @ Join(left, right, Inner, conditionOpt) => --- End diff -- I also thought about this but decided to start with a smaller scope. The motivation was that `"SELECT * FROM t1, t2"` is resolved into an Inner Join and one has to explicitly use the Cross Join syntax to allow cartesian products. I was not sure if it was OK to replace an explicit Cross Join with a join of a different type. Semantically, we can have `InnerLike` here.
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org