`MoveTowardsLeafNodes` placement implies speculative evaluation, but the docs don't say so

オープン 初心者向け
#27,487 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@Inf1mum がすでに取り組んでいます。

2026年11月17日 から。

  • #27519 @Inf1mum による — オープン

評価

難易度
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:

  • ExtractLeafExpressions extracts MoveTowardsLeafNodes sub-expressions from anywhere inside an expression tree, including the branches of CASE. In the extraction projection, they are evaluated for every input row, not just the rows the branch selects.
  • PushDownLeafProjections then pushes the extraction projections below Filter nodes, 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:

  1. Document the requirement (preferred). State on ExpressionPlacement::MoveTowardsLeafNodes and ScalarUDFImpl::placement that 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.
  2. Stop speculative evaluation: don't extract leaf expressions from conditional sub-expressions (CASE branches, the right side of short-circuiting AND/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 evaluating get_field close 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時間
マージ済み PR(30日)
352

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/datafusion のほかの issue

apache/datafusion の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。