`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
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
tinystream-dev/tinystream#132 ·
-
contract good first issue testing
难度 2/5 1-3 小时 新手友好度 72/100
Emmyt24/nova-launch#2436 ·
维护者通常 1 天内回复