Implement Spark-compatible `transform_values` (higher-order map function)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 42/100
Hướng nghiên cứu
Start by reading datafusion/functions-nested/src/array_transform.rs as the model for evaluating a lambda over flattened entries, then look at crates/spark/src/function/lambda/mod.rs, which is still an empty ScalarUDF placeholder needing a HigherOrderUDF list and registration. Reuse the MapArray helpers in crates/spark/src/function/map/utils.rs to rebuild the output map with the original offsets, nulls and key array. Done means crates/sqllogictest/test_files/lambda/transform_values.slt passes against the examples and edge cases above, including arity mismatch errors and NULL/empty maps.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Part of #176 and apache/datafusion-comet#7300.
What the function does
transform_values(expr, func) returns a map with the same keys, in the same order, whose values are func(k, v)
for each entry of the map expr. The result type is MAP<keyType, func.dataType> with
valueContainsNull = func.nullable. A NULL map gives NULL, and the result is nullable if the input is. The lambda
takes exactly 2 parameters: k (non-null) and v (nullable when the input's valueContainsNull is true). expr
must be a MAP (DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE otherwise). A non-lambda func is wrapped in a hidden
lambda. Available since Spark 3.0.0. No aliases.
Keys are not modified, so there is no dedup policy and no NULL_MAP_KEY check. Spark reuses the input key array
directly (new ArrayBasedMapData(map.keyArray(), resultValues)).
Examples
SELECT transform_values(map_from_arrays(array(1, 2, 3), array(1, 2, 3)), (k, v) -> v + 1);
-- {1:2,2:3,3:4}
SELECT transform_values(map_from_arrays(array(1, 2, 3), array(1, 2, 3)), (k, v) -> k + v);
-- {1:2,2:4,3:6}
-- nested view: (1, map(1,1,2,2,3,3)), (2, map(4,4,5,5,6,6)) as t(x, ys)
SELECT transform_values(ys, (k, v) -> k + v) FROM nested;
-- {1:2,2:4,3:6} / {4:8,5:10,6:12}
SELECT transform_values(map('a', 1, 'b', 2), 999);
-- {"a":999,"b":999}
SELECT transform_values(map('a', 1, 'b', 2), struct('val', 999));
-- {"a":{"col1":"val","col2":999},"b":{"col1":"val","col2":999}}
These come from the ExpressionDescription and higher-order-functions.sql.out. HigherOrderFunctionsSuite adds:
| map | lambda | result |
|---|---|---|
{1:1, 2:null, 3:3} |
v + 1 |
{1:2, 2:null, 3:4} |
{1:1, 2:null, 3:3} |
k * k |
{1:1, 2:4, 3:9} |
{"a":"xy", "bb":"yz", "ccc":"zx"} |
length(v) + 1 |
{"a":3, "bb":3, "ccc":3} |
{"a":"xy", "bb":null, "ccc":"zx"} |
concat(k, v) |
{"a":"axy", "bb":null, "ccc":"ccczx"} |
{} |
any | {} |
| NULL | any | NULL |
Edge cases to test
- A NULL map gives NULL. An empty map gives
{}, also when the value type changes (MAP<STRING, INT>from
length(v) + 1). - NULL values passed to the lambda:
v + 1gives NULL there, whilek * kignoresvand gives a value. The result
valueContainsNullfollows the lambda's nullability. - Value-type changes (string to int, int to struct, int to map). Chained calls:
transform_values(transform_values(m, (k, v) -> concat(k, v)), (k, v) -> concat(k, v))on
{"a":"xy","bb":null,"ccc":"zx"}gives{"a":"aaxy","bb":null,"ccc":"cccccczx"}. - Key order and key values are preserved exactly, including float keys such as
-0.0andNaN, which are not
re-normalized. Use the input key array as it is. - Lambda arity:
v -> v + 1and(k, v, x) -> ...must fail withINVALID_LAMBDA_FUNCTION_CALL.NUM_ARGS_MISMATCH.
DataFusion's planner allows a lambda to declare fewer parameters than offered, so this check has to be explicit. - Captured outer columns, nondeterministic value expressions (once per entry), and ANSI errors inside the lambda
(v / 0, overflow). - Sliced
MapArrayinput and values hidden under NULL map rows. The lambda must not be evaluated on them, because
they could raise errors Spark never raises.
Differences across Spark versions
- None found in
TransformValuesbetween 3.4 and 4.2. The literal-argument cases in the golden file were added in
4.1, which only adds tests. ANSI mode is on by default from 4.0.
Implementation notes
- Spark:
TransformValuesinsql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/higherOrderFunctions.scala. - 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. This is the simplest of the map functions to build:
- Evaluate the lambda once over the flattened entry keys and values, spreading captures with the per-entry row
index, asarray_transformdoes indatafusion/functions-nested/src/array_transform.rs. - Rebuild the
MapArraywith the original offsets, nulls and keys, and the new values.
- Evaluate the lambda once over the flattened entry keys and values, spreading captures with the per-entry row
- 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.rshasMapArrayhelpers. - Add
crates/sqllogictest/test_files/lambda/transform_values.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.
- Ngôn ngữ chính
- Rust
- Star
- 7
- Fork
- 15
- Merge trung bình
- 16 giờ 43 phút
- Pull request đã merge (30 ngày)
- 26
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của apache/datafusion-spark
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
apache/datafusion-spark#287 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
apache/datafusion-spark#266 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug] make_dt_interval returns NULL for more than about 1.49 million days, Spark returns the intervalCó thể đã có người làm @Subodh-17 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
apache/datafusion-spark#109 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 18/100
apache/datafusion-spark#299 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
apache/datafusion-spark#298 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của apache/datafusion-spark
Issue tương tự
-
comp-rbac
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
ClickHouse/ClickHouse#131875 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug(python): read_transaction raises TypeError on a Rewrite with rewritten indicesCó thể đã có người làm @Borg93 đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
lance-format/lance#11117 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
storytold/photocraft#15232 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
forge: fork preflight reads still send `requireCanonical: false`, which Rootstock rejects (-32603)Có thể đã có người làm @Ynyesto đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
foundry-rs/foundry#18485 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
bondybondbond/rushcut#238 · 1 bình luận ·