Yang Jie created SPARK-62350:
--------------------------------
Summary: Nested NULLIF or BETWEEN over foldable operands takes
exponential time to optimize
Key: SPARK-62350
URL: https://issues.apache.org/jira/browse/SPARK-62350
Project: Spark
Issue Type: Improvement
Components: SQL
Affects Versions: 5.0.0
Reporter: Yang Jie
{{NullIf}} and {{Between}} read their first operand twice, so they build a
{{With}} that evaluates it once (SPARK-45760). {{RewriteWithExpression}}
substitutes a deterministic definition at each of its references when
{{CollapseProject.isCheap}} holds, and {{isCheap}} is true for any foldable
expression. In a command, every deterministic definition is substituted
(SPARK-59962). So when NULLIF is nested over a foldable operand, each level's
operand ends up in both references of the next level, and rules that walk the
tree visit O(2^depth) nodes. Results are correct; the cost is planning time.
SPARK-59496 removed a similar blow-up at analysis time, and this one happens
during optimization.
Measured on master at 082cf160890, local mode, one run each, with {{nullif(...,
k)}} nested over the literal 1:
||depth||SELECT (optimizer only)||CREATE TABLE ... DEFAULT (V2 catalog)||DELETE
... WHERE (V2 catalog)||
|18|270 ms|437 ms|394 ms|
|20|1167 ms|1905 ms|2008 ms|
|22|4860 ms|7398 ms|7521 ms|
The same nest over a column ({{FROM range(1)}}) optimizes in 30 to 40 ms at
every depth, because that definition is not cheap and is evaluated once in a
{{Project}}. At depth 20, {{CollapseProject}}, {{ConstantFolding}} and
{{FoldablePropagation}} take most of the optimizer time in the {{RuleExecutor}}
metrics. Spark 4.2.0 shows the same growth (1.8 s at depth 20 for the SELECT).
Column defaults of V1 DDL and of writes are handled in SPARK-60029
([https://github.com/apache/spark/pull/60816]), which folds such a {{With}} by
evaluating each definition once. A similar fold in the optimizer, for a
{{With}} whose definitions are all foldable, would avoid the copies here. The
{{CommonExpressionDef}} doc explains why a definition stays unevaluable while a
{{With}} is left in a conditional branch, so that case needs care.
A generated-IV {{aes_encrypt}} copied this way is evaluated once per copy.
SPARK-60934 makes it nondeterministic, which stops the copying.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]