Skip to content

Commit

Permalink
[SPARK-50781][SQL] Cache QueryPlan.expressions
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?
Cache `QueryPlan.expressions`

### Why are the changes needed?
We observed that we were spending a significant amount of time recomputing.

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
Existing Tests

### Was this patch authored or co-authored using generative AI tooling?
No

Closes apache#49435 from zhengruifeng/sql_cache_expressions.

Authored-by: Ruifeng Zheng <[email protected]>
Signed-off-by: Ruifeng Zheng <[email protected]>
  • Loading branch information
zhengruifeng committed Jan 10, 2025
1 parent 868549e commit 7845867
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]]
}

/** Returns all of the expressions present in this query plan operator. */
final def expressions: Seq[Expression] = {
final def expressions: Seq[Expression] = _expressions()

private val _expressions = new BestEffortLazyVal[Seq[Expression]](() => {
// Recursively find all expressions from a traversable.
def seqToExpressions(seq: Iterable[Any]): Iterable[Expression] = seq.flatMap {
case e: Expression => e :: Nil
Expand All @@ -297,7 +299,7 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]]
case seq: Iterable[_] => seqToExpressions(seq)
case other => Nil
}.toSeq
}
})

/**
* A variant of `transformUp`, which takes care of the case that the rule replaces a plan node
Expand Down

0 comments on commit 7845867

Please sign in to comment.