Implement Spark-compatible `zip_with` (higher-order function over two arrays)
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 40/100
Línea de trabajo
Start in crates/spark/src/function/lambda/mod.rs, an empty ScalarUDF placeholder, and read HigherOrderUDFImpl in datafusion/expr/src/higher_order_function.rs on branch-56. Implement zip_with with lambda_parameters returning two nullable element-type fields, then register it via register_higher_order_function/SessionStateBuilder::higher_order_functions. Add crates/sqllogictest/test_files/lambda/zip_with.slt covering the Spark examples plus padding, NULL and empty-array cases; done means those pass and the function has #[user_doc].
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Part of #176 and apache/datafusion-comet#7300.
What the function does
zip_with(left, right, func) merges two arrays element by element with a two-argument lambda (x, y) -> .... If
one array is shorter, it is padded with NULLs at the end to the length of the longer one before func is applied.
The result type is ARRAY<func.dataType> with containsNull = func.nullable. The result is NULL if either input
array is NULL, and nullable if either input is nullable. Both lambda parameters are always declared nullable,
because of the padding. Both arguments must be ARRAYs, and their element types can differ. There is no index
argument. Non-lambda func arguments are wrapped in a hidden lambda. Available since Spark 2.4.0. No aliases.
Examples
SELECT zip_with(array(1, 2, 3), array('a', 'b', 'c'), (x, y) -> (y, x));
-- [{"y":"a","x":1},{"y":"b","x":2},{"y":"c","x":3}]
SELECT zip_with(array(1, 2), array(3, 4), (x, y) -> x + y);
-- [4,6]
SELECT zip_with(array('a', 'b', 'c'), array('d', 'e', 'f'), (x, y) -> concat(x, y));
-- ["ad","be","cf"]
SELECT zip_with(array('a'), array('d', null, 'f'), (x, y) -> coalesce(x, y));
-- ["a",null,"f"]
SELECT zip_with(array(1, 2, 3), array(4, 5, 6), 100);
-- [100,100,100]
-- nested view (x, ys, zs) = (1, [32,97], [[12,99],[123,42],[1]]), (2, [77,-76], [[6,96,65],[-1,-2]]), (3, [12], [[17]])
SELECT zip_with(ys, zs, (a, b) -> a + size(b)) FROM nested;
-- [34,99,null] / [80,-74] / [13]
These come from the ExpressionDescription and higher-order-functions.sql.out. The last example is listed in view
order, and it shows padding: row 1 has 2 ys and 3 zs, so the third a is NULL.
Edge cases to test
- Arrays of different lengths, in both directions:
zip_with([1,2,3], [1,2,3,4], (x, y) -> x + y)gives
[2,4,6,null], andzip_with([1,null], [1,null,3], add)andzip_with([1,null,3], [1,null], add)both give
[2,null,null](HigherOrderFunctionsSuite). The padding must be NULL even when the input element type is
non-nullable. - The lambda still runs on padded positions, so a body that ignores NULLs produces values there:
coalesce(x, y)above gives"f". - Either array NULL gives NULL. Spark evaluates
leftfirst and does not evaluaterightifleftis NULL. - Both arrays empty gives
[]. One empty array gives a result as long as the other, computed with all-NULL values on
one side. - A NULL inner array within an array of arrays:
zip_with(aai1, aai1, (a1, a2) -> cast(transform(a1, x -> x + 1) as string))on[[1,2,3],null,[4,5]]gives
["[2, 3, 4]",null,"[5, 6]"]. - Element types that differ between
leftandright(int and string), and struct results built with(y, x).
Spark names the struct fields after the lambda variables ({"y":...,"x":...}). - Captured outer columns, nested lambdas, and nondeterministic bodies evaluated once per output position.
- Errors inside the lambda under ANSI mode, for example
x / ywith a zero divisor. They are raised only for
positions that are actually evaluated, which includes padded positions. - Lambda arity: exactly 2 parameters, otherwise
INVALID_LAMBDA_FUNCTION_CALL.NUM_ARGS_MISMATCH. A non-array
argument fails withDATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE.
Differences across Spark versions
- None found in
ZipWithbetween 3.4 and 4.2. ANSI mode is on by default from 4.0.
Implementation notes
- Spark:
ZipWithinsql/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_parametersmust return two parameter fields, both
nullable, with the two element types. - DataFusion core has no two-array higher-order function.
arrays_zipis not a higher-order function. To evaluate
vectorized, build per row themax(len_l, len_r)positions, thentakeboth value arrays with NULL indices for the
padding, which gives two aligned flattened columns. Evaluate the body once over them, and spread captures to the
same positions. The result offsets are the per-row maximum lengths. - 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. - Add
crates/sqllogictest/test_files/lambda/zip_with.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.
- Lenguaje dominante
- Rust
- Estrellas
- 7
- Forks
- 15
- Merge medio
- 16 h 43 min
- PR fusionados (30 d)
- 26
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de apache/datafusion-spark
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
apache/datafusion-spark#287 ·
Los mantenedores suelen responder en 1 día
-
enhancement good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 80/100
apache/datafusion-spark#266 ·
Los mantenedores suelen responder en 1 día
-
[Bug] make_dt_interval returns NULL for more than about 1.49 million days, Spark returns the intervalPosiblemente ocupada @Subodh-17 la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
apache/datafusion-spark#109 ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 5/5 Más de una semana Aptitud para principiantes 18/100
apache/datafusion-spark#299 ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
apache/datafusion-spark#298 ·
Los mantenedores suelen responder en 1 día
Todos los issues de apache/datafusion-spark
Issues similares
-
comp-rbac
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
ClickHouse/ClickHouse#131875 ·
Los mantenedores suelen responder en 1 día
-
bug(python): read_transaction raises TypeError on a Rewrite with rewritten indicesPosiblemente ocupada @Borg93 la tomó hoy. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
lance-format/lance#11117 ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
storytold/photocraft#15232 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
forge: fork preflight reads still send `requireCanonical: false`, which Rootstock rejects (-32603)Posiblemente ocupada @Ynyesto la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
foundry-rs/foundry#18485 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
bondybondbond/rushcut#238 · 1 comentario ·