This is an automated email from the ASF dual-hosted git repository.

agrove pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-python.git


The following commit(s) were added to refs/heads/main by this push:
     new d6c42b4  feat: expose `named_struct` in python (#700)
d6c42b4 is described below

commit d6c42b4dec452f75305f37f937ed1669dbf5809e
Author: Michael J Ward <[email protected]>
AuthorDate: Wed May 15 09:18:47 2024 -0500

    feat: expose `named_struct` in python (#700)
    
    Ref #692
---
 python/datafusion/tests/test_functions.py | 26 ++++++++++++++++++++++++++
 src/functions.rs                          |  2 ++
 2 files changed, 28 insertions(+)

diff --git a/python/datafusion/tests/test_functions.py 
b/python/datafusion/tests/test_functions.py
index d2dafc1..d34e46b 100644
--- a/python/datafusion/tests/test_functions.py
+++ b/python/datafusion/tests/test_functions.py
@@ -50,6 +50,32 @@ def df():
     return ctx.create_dataframe([[batch]])
 
 
+def test_named_struct(df):
+    df = df.with_column(
+        "d",
+        f.named_struct(
+            literal("a"),
+            column("a"),
+            literal("b"),
+            column("b"),
+            literal("c"),
+            column("c"),
+        ),
+    )
+
+    expected = """DataFrame()
++-------+---+---------+------------------------------+
+| a     | b | c       | d                            |
++-------+---+---------+------------------------------+
+| Hello | 4 | hello   | {a: Hello, b: 4, c: hello }  |
+| World | 5 |  world  | {a: World, b: 5, c:  world } |
+| !     | 6 |  !      | {a: !, b: 6, c:  !}          |
++-------+---+---------+------------------------------+
+""".strip()
+
+    assert str(df) == expected
+
+
 def test_literal(df):
     df = df.select(
         literal(1),
diff --git a/src/functions.rs b/src/functions.rs
index 8a7d738..975025b 100644
--- a/src/functions.rs
+++ b/src/functions.rs
@@ -503,6 +503,7 @@ expr_fn_vec!(trunc);
 expr_fn!(upper, arg1, "Converts the string to all upper case.");
 expr_fn!(uuid);
 expr_fn_vec!(r#struct); // Use raw identifier since struct is a keyword
+expr_fn_vec!(named_struct);
 expr_fn!(from_unixtime, unixtime);
 expr_fn!(arrow_typeof, arg_1);
 expr_fn!(random);
@@ -680,6 +681,7 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
     m.add_wrapped(wrap_pyfunction!(mean))?;
     m.add_wrapped(wrap_pyfunction!(median))?;
     m.add_wrapped(wrap_pyfunction!(min))?;
+    m.add_wrapped(wrap_pyfunction!(named_struct))?;
     m.add_wrapped(wrap_pyfunction!(nanvl))?;
     m.add_wrapped(wrap_pyfunction!(now))?;
     m.add_wrapped(wrap_pyfunction!(nullif))?;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to