Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose named_struct in python #700

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: expose named_struct in python
Ref #692
  • Loading branch information
Michael-J-Ward committed May 15, 2024
commit a51720247c1e96a6222892ce81f5a912db9258de
26 changes: 26 additions & 0 deletions python/datafusion/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,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);
Expand Down Expand Up @@ -678,6 +679,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))?;
Expand Down
Loading