Skip to content

Commit be530d5

Browse files
timsaucerclaude
andcommitted
docs(spark): fix map_from_entries doctest to use the right function
The example called map_from_arrays, so it never exercised map_from_entries. Build an array-of-struct input and call the documented function. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent c921001 commit be530d5

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

python/datafusion/functions/spark.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -999,17 +999,22 @@ def map_from_arrays(col1: Expr, col2: Expr) -> Expr:
999999
def map_from_entries(col: Expr) -> Expr:
10001000
"""Spark ``map_from_entries``: build a map from an array of key/value structs.
10011001
1002+
``col`` must be an array whose elements are two-field structs; the first
1003+
field becomes the map key and the second the value.
1004+
10021005
Examples:
1006+
>>> import pyarrow as pa
10031007
>>> ctx = dfn.SessionContext()
1004-
>>> df = ctx.from_pydict({"x": [1]})
1008+
>>> entry_type = pa.list_(
1009+
... pa.struct([("key", pa.string()), ("value", pa.int64())]))
1010+
>>> entries = pa.array(
1011+
... [[{"key": "a", "value": 1}, {"key": "b", "value": 2}]],
1012+
... type=entry_type)
1013+
>>> df = ctx.from_arrow(pa.record_batch([entries], names=["e"]))
10051014
>>> r = df.select(
1006-
... dfn.functions.spark.map_from_arrays(
1007-
... dfn.functions.spark.array(dfn.lit("a")),
1008-
... dfn.functions.spark.array(dfn.lit(1)),
1009-
... ).alias("v")
1010-
... )
1015+
... dfn.functions.spark.map_from_entries(dfn.col("e")).alias("v"))
10111016
>>> r.collect_column("v")[0].as_py()
1012-
[('a', 1)]
1017+
[('a', 1), ('b', 2)]
10131018
"""
10141019
return Expr(_f.map_from_entries(col.expr))
10151020

0 commit comments

Comments
 (0)