-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Description
C.f. original comment here: #124335 (comment)
An INLINESTATS internally becomes an InlineJoin with the upstream plan as left child, an an Aggregate + StubRelation (in place of the upstream plan) as the right child. ReplaceAggregateNestedExpressionWithEval will take INLINESTATS ... BY x = some_expression(...) and turn x = some_expression(...) into an Eval placed ahead of the Aggregate in the right child.
But, this Eval needs to be placed in the left child, because it defines the join key x.
PropagateInlineEvals fixes this by moving the right hand side Eval to the left side. However, it does so in a complicated way, picking apart the Eval out of defensiveness, and performing name-based matching to find if an expression in said Eval is used as a grouping expression in the Aggregate.
This is complex and more interdependent than it needs to be. It already caused bugs that required a non-trivial PR to fix.
Let's fix the root cause: ReplaceAggregateNestedExpressionWithEval should be aware that it's currently working on an Aggregate that's sitting in the right hand side of an InlineJoin - and if so, it should place the Eval for the expression x = some_expression(...) on top of the left child of the join so this can be joined on.