8000 feat: expose `named_struct` in python (#700) · datapythonista/datafusion-python@d6c42b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit d6c42b4

Browse files
feat: expose named_struct in python (apache#700)
Ref apache#692
1 parent 344ebad commit d6c42b4

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

python/datafusion/tests/test_functions.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@ def df():
5050
return ctx.create_dataframe([[batch]])
5151

5252

53+
def test_named_struct(df):
54+
df = df.with_column(
55+
"d",
56+
f.named_struct(
57+
literal("a"),
58+
column("a"),
59+
literal("b"),
60+
column("b"),
61+
literal("c"),
62+
column("c"),
63+
),
64+
)
65+
66+
expected = """DataFrame()
67+
+-------+---+---------+------------------------------+
68+
| a | b | c | d |
69+
+-------+---+---------+------------------------------+
70+
| Hello | 4 | hello | {a: Hello, b: 4, c: hello } |
71+
| World | 5 | world | {a: World, b: 5, c: world } |
72+
| ! | 6 | ! | {a: !, b: 6, c: !} |
73+
+-------+---+---------+------------------------------+
74+
""".strip()
75+
76+
assert str(df) == expected
77+
78+
5379
def test_literal(df):
5480
df = df.select(
5581
literal(1),

src/functions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ expr_fn_vec!(trunc);
503503
expr_fn!(upper, arg1, "Converts the string to all upper case.");
504504
expr_fn!(uuid);
505505
expr_fn_vec!(r#struct); // Use raw identifier since struct is a keyword
506+
expr_fn_vec!(named_struct);
506507
expr_fn!(from_unixtime, unixtime);
507508
expr_fn!(arrow_typeof, arg_1);
508509
expr_fn!(random);
@@ -680,6 +681,7 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
680681
m.add_wrapped(wrap_pyfunction!(mean))?;
681682
m.add_wrapped(wrap_pyfunction!(median))?;
682683
m.add_wrapped(wrap_pyfunction!(min))?;
684+
m.add_wrapped(wrap_pyfunction!(named_struct))?;
683685
m.add_wrapped(wrap_pyfunction!(nanvl))?;
684686
m.add_wrapped(wrap_pyfunction!(now))?;
685687
m.add_wrapped(wrap_pyfunction!(nullif))?;

0 commit comments

Comments
 (0)
0