`MoveTowardsLeafNodes` placement implies speculative evaluation, but the docs don't say so
維護者通常 1 天內回覆
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 75/100
- Issue 類型
- 文件
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- rust
研究方向
從 issue 連結的 ExpressionPlacement::MoveTowardsLeafNodes 和 ScalarUDFImpl::placement 文件開始。了解葉節點運算式擷取和 pushdown 如何在被 CASE 分支或篩選器排除的資料列上計算運算式,然後在兩個 API 中記錄安全性要求。完成條件是兩份文件都說明:採用此放置方式的運算式在任何輸入資料列上都不得失敗或產生副作用。
由索引模型根據 Issue 內容生成。
描述
Describe the bug
ExpressionPlacement::MoveTowardsLeafNodes is documented as a performance hint: the expression is cheap and can be pushed down towards the leaves of the plan. When the optimizer acts on that hint, it also runs the expression on rows that the original plan never evaluated it on:
ExtractLeafExpressionsextractsMoveTowardsLeafNodessub-expressions from anywhere inside an expression tree, including the branches ofCASE. In the extraction projection, they are evaluated for every input row, not just the rows the branch selects.PushDownLeafProjectionsthen pushes the extraction projections belowFilternodes, so the expressions also run on rows the filter removes.
So an expression with this placement must be safe to evaluate on rows that a filter or a conditional would exclude: it must not fail on them or have side effects. Neither ExpressionPlacement::MoveTowardsLeafNodes nor ScalarUDFImpl::placement states this. The built-in functions with this placement (get_field, input_file_name, file_row_index) meet the requirement as far as I can tell. A user-defined function that returns MoveTowardsLeafNodes because it is cheap, but fails on some inputs, can make a query fail even though SQL CASE and WHERE semantics say that input is never evaluated.
To Reproduce
The plan below shows how get_field is moved with the default configuration (datafusion.optimizer.enable_leaf_expression_pushdown = true):
CREATE TABLE t AS SELECT * FROM (VALUES
(1, named_struct('a', 1, 'b', 2)),
(-1, named_struct('a', 3, 'b', 4))
) AS v(x, s);
EXPLAIN SELECT CASE WHEN x > 0 THEN s['a'] WHEN x < -100 THEN 0 END FROM t WHERE x <> 5;
logical_plan
01)Projection: CASE WHEN t.x > Int64(0) THEN __datafusion_extracted_1 WHEN t.x < Int64(-100) THEN Int64(0) END AS ...
02)--Filter: t.x != Int64(5)
03)----Projection: get_field(t.s, Utf8("a")) AS __datafusion_extracted_1, t.x
04)------TableScan: t projection=[x, s]
get_field(t.s, 'a') came from the first THEN branch, and it is now evaluated below the filter for every row of t.
Replace s['a'] with a UDF that returns MoveTowardsLeafNodes and fails on some values, for example a checked conversion that fails when its argument is 0. The query then fails on rows where x <= 0 or x = 5, even though the result would never include the UDF's value for those rows.
Expected behavior
Either of these would resolve it:
- Document the requirement (preferred). State on
ExpressionPlacement::MoveTowardsLeafNodesandScalarUDFImpl::placementthat an expression with this placement may be evaluated on rows that enclosing filters or conditional expressions would exclude, so it must not fail or have side effects on any input row. This matches what the optimizer already relies on. - Stop speculative evaluation: don't extract leaf expressions from conditional sub-expressions (
CASEbranches, the right side of short-circuitingAND/OR), and don't push extraction projections below filters when the extracted expression could fail. That would give up much of the benefit of the leaf pushdown optimization, which is mainly about evaluatingget_fieldclose to the scan.
Additional context
The question came up while reviewing #26589, which precomputes MoveTowardsLeafNodes expressions over the whole batch before evaluating CASE branches (https://github.com/apache/datafusion/pull/26589#discussion_r4581490634). That PR relies on the same assumption at the physical level, so documenting the requirement would cover both.
- 主要語言
- Rust
- 星號
- 9.6k
- 分支
- 2.6k
- 平均合併
- 3 天 5 小時
- 30 天內合併 PR
- 352
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
apache/datafusion 的其他 Issue
-
`verify-release-candidate.sh` version check doesn't catch `-dev` versions and is skipped on macOS可能已有人在做 @vinycalves 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
apache/datafusion#27563 · 1 則留言 ·
維護者通常 1 天內回覆
-
`lpad` and `rpad` with a literal length above `i32::MAX` error on a NULL string or fill可能已有人在做 @TechWizard9999 於 2 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 77/100
apache/datafusion#27482 · 1 則留言 · 已指派 1 人 ·
維護者通常 1 天內回覆
-
Window constructors panic when the expression list is empty可能已有人在做 @abp002 於 4 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 72/100
apache/datafusion#27315 · 3 則留言 · 已指派 1 人 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 76/100
apache/datafusion#27094 · 1 則留言 ·
維護者通常 1 天內回覆
-
Substrait consumer doesn't recognise `shift_left` / `shift_right` from functions_arithmetic.yaml可能已有人在做 @jonasHanhan 於 15 天前認領。 未關閉bug
難度 2/5 1-3 小時 新手友好度 76/100
apache/datafusion#27059 ·
維護者通常 1 天內回覆
查看 apache/datafusion 的全部 Issue
相似的 Issue
-
marketing
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 74/100
kinqsradio/orbyn-engineering#729 ·
-
Plans page scenario races Shift+Tab against the plan list being drawn again in Firefox and WebKit未關閉bug
難度 2/5 1-3 小時 新手友好度 83/100
nledford/Ladderback#1091 ·
維護者通常 1 天內回覆
-
bug fix packaging ui user-sprint
難度 2/5 1-3 小時 新手友好度 78/100
Vondor/KassenSturz#1197 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 82/100
維護者通常 1 天內回覆