Github user yangw1234 commented on a diff in the pull request:
https://github.com/apache/spark/pull/13585#discussion_r66742485
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala
---
@@ -92,6 +92,36 @@ object PhysicalOperation extends PredicateHelper {
.map(Alias(_, a.name)(a.exprId, a.qualifier, isGenerated =
a.isGenerated)).getOrElse(a)
}
}
+
+ /**
+ * Drop the non-partition key expression in the disjunctions, to
optimize the partition pruning.
+ * For instances: (We assume part1 & part2 are the partition keys)
+ * (part1 == 1 and a > 3) or (part2 == 2 and a < 5) ==> (part1 == 1 or
part1 == 2)
+ * (part1 == 1 and a > 3) or (a < 100) => None
+ * (a > 100 && b < 100) or (part1 = 10) => None
+ * (a > 100 && b < 100 and part1 = 10) or (part1 == 2) => (part1 = 10 or
part1 == 2)
+ * @param predicate disjunctions
+ * @param partitionKeyIds partition keys in attribute set
+ * @return
+ */
+ def partitionPrunningFromDisjunction(
+ predicate: Expression, partitionKeyIds: AttributeSet):
Option[Expression] = {
+ // ignore the pure non-partition key expression in conjunction of the
expression tree
+ val additionalPartPredicate = predicate transformUp {
+ case a @ And(left, right) if a.deterministic &&
+ left.references.intersect(partitionKeyIds).isEmpty => right
+ case a @ And(left, right) if a.deterministic &&
+ right.references.intersect(partitionKeyIds).isEmpty => left
--- End diff --
The problem is here. Imagine a record `a = 2` in `partition = 1`. Such a
record satisfies the above expression (`!(partition = 1 && a > 3)`), but if we
simply drop `a > 3` and push `!(partition = 1)` down to the table scan,
partition =1 will be discarded and the record won't appear in the result.
The test case passed because the `BooleanSimplification` optimizer rule
will transform `!(partition =1 && a > 3)` to `(!(partition=1) || (a <= 3))`,
such an expression will be dropped entirely by your
`partitionPrunningFromDisjunction`, in which case "partition = 1" will not be
discarded.
---
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]