asugranyes commented on code in PR #56180:
URL: https://github.com/apache/spark/pull/56180#discussion_r3329366271
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -973,8 +973,19 @@ object LimitPushDown extends Rule[LogicalPlan] {
val newAgg = EliminateSorts(a.copy(child = LocalLimit(le,
a.child))).asInstanceOf[Aggregate]
Limit(le, p.copy(child = Project(newAgg.aggregateExpressions,
newAgg.child)))
// Merge offset value and limit value into LocalLimit and pushes down
LocalLimit through Offset.
+ // Fold the sum eagerly when both operands are integer literals so this
rule produces a
+ // planable logical plan in a single application. Otherwise physical
planning
+ // (BasicOperators in SparkStrategies) would fail with `AssertionError: No
plan for
+ // LocalLimit (a + b)` when ConstantFolding is excluded from the optimizer
pipeline, since
+ // BasicOperators only matches LocalLimit(IntegerLiteral, _). In practice
both operands
+ // come from `LIMIT N OFFSET M` clauses (already folded to literals) so
this single-case
+ // fold covers all realistic inputs.
case LocalLimit(le, Offset(oe, grandChild)) =>
- Offset(oe, LocalLimit(Add(le, oe), grandChild))
+ val mergedLimit = (le, oe) match {
+ case (IntegerLiteral(l), IntegerLiteral(o)) => Literal(l + o,
IntegerType)
Review Comment:
Using `Add(le, oe).eval(EmptyRow) ` reuses the same path
`ConstantFolding.tryFold ` takes — folds correctly for normal values and
surfaces ANSI overflow errors when applicable, without reimplementing the
checks here.
```suggestion
case (IntegerLiteral(l), IntegerLiteral(o)) => Literal(Add(le,
oe).eval(EmptyRow), IntegerType)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]