Implement Spark-compatible `map_filter` (higher-order map function)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
調査の方向性
Start at crates/spark/src/function/lambda/mod.rs, an empty ScalarUDF placeholder that needs a HigherOrderUDF plus registration via register_higher_order_function; crates/spark/src/function/map/utils.rs has the MapArray helpers. Model the body on DataFusion's array_filter (datafusion/functions-nested/src/array_filter.rs) and HigherOrderUDFImpl/spread_captures in datafusion/expr/src/higher_order_function.rs. Done means the new crates/sqllogictest/test_files/lambda/map_filter.slt passes the NULL-map, NULL-predicate, order and arity cases, with #[user_doc] added.
索引モデルが issue の本文から書いたものです。
説明
Part of #176 and apache/datafusion-comet#7300.
What the function does
map_filter(expr, func) keeps the entries of the map expr for which the lambda (k, v) -> BOOLEAN returns
true. It drops entries where the lambda returns false or NULL. Entries keep their original order. The result
type is the input map type, unchanged. A NULL map gives NULL, and the result is nullable if the input is nullable.
The key variable is non-nullable, and the value variable has the map's valueContainsNull nullability. The lambda
must take exactly 2 parameters and return BOOLEAN. A non-boolean body, or a non-lambda literal such as
map_filter(m, 1), fails with DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE. expr must be a MAP. Available since
Spark 3.0.0. No aliases.
Examples
SELECT map_filter(map(1, 0, 2, 2, 3, -1), (k, v) -> k > v);
-- {1:0,3:-1}
From the ExpressionDescription. HigherOrderFunctionsSuite adds the following cases:
| map | lambda | result |
|---|---|---|
{1:0, 2:10, 3:-1} |
k > v |
{1:0, 3:-1} |
{1:null, 2:10, 3:null} |
k > v |
{} (NULL predicate drops the entry) |
{1:0, 2:10, 3:-1} |
v IS NULL |
{} |
{1:null, 2:10, 3:null} |
v IS NULL |
{1:null, 3:null} |
{"abcdf":5, "abc":10, "":0} |
length(k) = v |
{"abcdf":5, "":0} |
{"abcdf":5, "abc":10, "":null} |
length(k) = v |
{"abcdf":5} |
{1:[0,1,2], 2:null, -3:[-1,0,-2,3]} |
size(v) + k > 3 |
{1:[0,1,2]} |
| NULL map | any | NULL |
Edge cases to test
- A NULL map gives NULL. An empty map gives
{}. A map whose entries are all filtered out gives{}, not NULL. - A NULL predicate result is treated as
false. Spark checksf.eval(...).asInstanceOf[Boolean], and a NULL unboxes
tofalse. - NULL values in the map.
- In the
size(v) + k > 3case,size(NULL)is-1when ANSI is off (spark.sql.legacy.sizeOfNull, default true)
and NULL when ANSI is on. The2:nullentry is dropped either way, because1 > 3isfalseand a NULL predicate
also drops the entry. Test both settings. - Entry order is preserved, and duplicate keys cannot appear (the input map is already deduplicated).
- Captured outer columns:
map_filter(m, (k, v) -> v > threshold_col). - Nondeterministic predicates, evaluated once per entry.
- Errors inside the predicate under ANSI mode (
v / 0 > 1), raised for every evaluated entry. - Lambda arity:
k -> ...and(k, v, x) -> ...both fail withINVALID_LAMBDA_FUNCTION_CALL.NUM_ARGS_MISMATCH.
DataFusion's planner accepts lambdas with fewer parameters than offered, so the Spark arity check has to be
enforced explicitly. - Key types: integers, strings, binary, structs, and floating point (including
NaNand-0.0keys). - Sliced
MapArrayinput, where offsets do not start at 0.
Differences across Spark versions
- None found in
MapFilterbetween 3.4 and 4.2. The literal-argument error tests were added to the golden file in
4.1, which only adds tests. ANSI mode is on by default from 4.0.
Implementation notes
- Spark:
MapFilterinsql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/higherOrderFunctions.scala.
It builds the result withArrayBasedMapDatadirectly, with no dedup and no key normalization. - Infrastructure. DataFusion's higher-order function framework (
HigherOrderUDFImplin
datafusion/expr/src/higher_order_function.rs, andLambdaArgument::evaluatewithspread_capturesfor captured
columns) is onbranch-56, which this crate uses.lambda_parametersshould offer[key (non-null), value]. - DataFusion core has no higher-order map functions.
array_filter(datafusion/functions-nested/src/array_filter.rs)
is the closest model: evaluate the predicate over the flattened keys and values of theMapArrayentries, then
filterthe entries struct and rebuild the offsets. Its handling of NULL predicates (treated as not matching) and
of values under NULL rows matches what is needed here. - This crate's
crates/spark/src/function/lambda/mod.rsis an emptyScalarUDFplaceholder. It needs a
HigherOrderUDFlist and registration throughregister_higher_order_function/
SessionStateBuilder::higher_order_functions.crates/spark/src/function/map/utils.rshasMapArray
construction helpers. - Add
crates/sqllogictest/test_files/lambda/map_filter.sltand document the function with#[user_doc].
This issue was generated by an LLM (Claude) from the Apache Spark source code and tests. Examples come from Spark's ExpressionDescription examples, test suites and .sql.out golden files, but please verify them against Spark before relying on them.
- 主要言語
- Rust
- スター
- 7
- フォーク
- 15
- 平均マージ
- 16時間 43分
- マージ済み PR(30日)
- 26
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/datafusion-spark のほかの issue
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
apache/datafusion-spark#287 ·
メンテナーはふだん 1 日以内に返信
-
enhancement good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
apache/datafusion-spark#266 ·
メンテナーはふだん 1 日以内に返信
-
[Bug] make_dt_interval returns NULL for more than about 1.49 million days, Spark returns the interval対応中かも @Subodh-17 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/datafusion-spark#109 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 18/100
apache/datafusion-spark#299 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
apache/datafusion-spark#298 ·
メンテナーはふだん 1 日以内に返信
apache/datafusion-spark の issue をすべて見る
似ている issue
-
comp-rbac
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
ClickHouse/ClickHouse#131875 ·
メンテナーはふだん 1 日以内に返信
-
bug(python): read_transaction raises TypeError on a Rewrite with rewritten indices対応中かも @Borg93 が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
lance-format/lance#11117 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
storytold/photocraft#15232 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
forge: fork preflight reads still send `requireCanonical: false`, which Rootstock rejects (-32603)対応中かも @Ynyesto が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
foundry-rs/foundry#18485 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
bondybondbond/rushcut#238 · コメント 1 件 ·