-
Notifications
You must be signed in to change notification settings - Fork 151
Add missing scalar functions #1470
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
Merged
+296
−9
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
148f62e
Add missing scalar functions: get_field, union_extract, union_tag, ar…
timsaucer ea2370a
Add tests for new scalar functions
timsaucer 02eb255
Accept str for field name and type parameters in scalar functions
timsaucer df1ead1
Accept str for key parameter in arrow_metadata for consistency
timsaucer 0f50a31
Merge branch 'main' into feat/add-missing-scalar-fns
timsaucer a662e18
Add doctest examples and fix docstring style for new scalar functions
timsaucer b627d30
Support pyarrow DataType in arrow_cast
timsaucer f760e70
Document bracket syntax shorthand in get_field docstring
timsaucer d12f721
Fix arrow_cast with pyarrow DataType by delegating to Expr.cast
timsaucer 056f712
Clarify when to use arrow_cast vs Expr.cast in docstring
timsaucer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ | |
| import numpy as np | ||
| import pyarrow as pa | ||
| import pytest | ||
| from datafusion import SessionContext, column, literal, string_literal | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love that this is no longer needed |
||
| from datafusion import SessionContext, column, literal | ||
| from datafusion import functions as f | ||
|
|
||
| np.seterr(invalid="ignore") | ||
|
|
@@ -1143,11 +1143,8 @@ def test_make_time(df): | |
|
|
||
| def test_arrow_cast(df): | ||
| df = df.select( | ||
| # we use `string_literal` to return utf8 instead of `literal` which returns | ||
| # utf8view because datafusion.arrow_cast expects a utf8 instead of utf8view | ||
| # https://github.com/apache/datafusion/blob/86740bfd3d9831d6b7c1d0e1bf4a21d91598a0ac/datafusion/functions/src/core/arrow_cast.rs#L179 | ||
| f.arrow_cast(column("b"), string_literal("Float64")).alias("b_as_float"), | ||
| f.arrow_cast(column("b"), string_literal("Int32")).alias("b_as_int"), | ||
| f.arrow_cast(column("b"), "Float64").alias("b_as_float"), | ||
| f.arrow_cast(column("b"), "Int32").alias("b_as_int"), | ||
| ) | ||
| result = df.collect() | ||
| assert len(result) == 1 | ||
|
|
@@ -1660,3 +1657,86 @@ def df_with_nulls(): | |
| def test_conditional_functions(df_with_nulls, expr, expected): | ||
| result = df_with_nulls.select(expr.alias("result")).collect()[0] | ||
| assert result.column(0) == expected | ||
|
|
||
|
|
||
| def test_get_field(df): | ||
| df = df.with_column( | ||
| "s", | ||
| f.named_struct( | ||
| [ | ||
| ("x", column("a")), | ||
| ("y", column("b")), | ||
| ] | ||
| ), | ||
| ) | ||
| result = df.select( | ||
| f.get_field(column("s"), "x").alias("x_val"), | ||
| f.get_field(column("s"), "y").alias("y_val"), | ||
| ).collect()[0] | ||
|
|
||
| assert result.column(0) == pa.array(["Hello", "World", "!"], type=pa.string_view()) | ||
| assert result.column(1) == pa.array([4, 5, 6]) | ||
|
|
||
|
|
||
| def test_arrow_metadata(): | ||
| ctx = SessionContext() | ||
| field = pa.field("val", pa.int64(), metadata={"key1": "value1", "key2": "value2"}) | ||
| schema = pa.schema([field]) | ||
| batch = pa.RecordBatch.from_arrays([pa.array([1, 2, 3])], schema=schema) | ||
| df = ctx.create_dataframe([[batch]]) | ||
|
|
||
| # One-argument form: returns a Map of all metadata key-value pairs | ||
| result = df.select( | ||
| f.arrow_metadata(column("val")).alias("meta"), | ||
| ).collect()[0] | ||
| assert result.column(0).type == pa.map_(pa.utf8(), pa.utf8()) | ||
timsaucer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| meta = result.column(0)[0].as_py() | ||
| assert ("key1", "value1") in meta | ||
| assert ("key2", "value2") in meta | ||
|
|
||
| # Two-argument form: returns the value for a specific metadata key | ||
| result = df.select( | ||
| f.arrow_metadata(column("val"), "key1").alias("meta_val"), | ||
| ).collect()[0] | ||
| assert result.column(0)[0].as_py() == "value1" | ||
|
|
||
|
|
||
| def test_version(): | ||
| ctx = SessionContext() | ||
| df = ctx.from_pydict({"a": [1]}) | ||
| result = df.select(f.version().alias("v")).collect()[0] | ||
| version_str = result.column(0)[0].as_py() | ||
| assert "Apache DataFusion" in version_str | ||
|
|
||
|
|
||
| def test_row(df): | ||
| result = df.select( | ||
| f.row(column("a"), column("b")).alias("r"), | ||
| f.struct(column("a"), column("b")).alias("s"), | ||
| ).collect()[0] | ||
| # row is an alias for struct, so they should produce the same output | ||
| assert result.column(0) == result.column(1) | ||
|
|
||
|
|
||
| def test_union_tag(): | ||
| ctx = SessionContext() | ||
| types = pa.array([0, 1, 0], type=pa.int8()) | ||
| offsets = pa.array([0, 0, 1], type=pa.int32()) | ||
| children = [pa.array([1, 2]), pa.array(["hello"])] | ||
| arr = pa.UnionArray.from_dense(types, offsets, children, ["int", "str"], [0, 1]) | ||
| df = ctx.create_dataframe([[pa.RecordBatch.from_arrays([arr], names=["u"])]]) | ||
|
|
||
| result = df.select(f.union_tag(column("u")).alias("tag")).collect()[0] | ||
| assert result.column(0).to_pylist() == ["int", "str", "int"] | ||
|
|
||
|
|
||
| def test_union_extract(): | ||
| ctx = SessionContext() | ||
| types = pa.array([0, 1, 0], type=pa.int8()) | ||
| offsets = pa.array([0, 0, 1], type=pa.int32()) | ||
| children = [pa.array([1, 2]), pa.array(["hello"])] | ||
| arr = pa.UnionArray.from_dense(types, offsets, children, ["int", "str"], [0, 1]) | ||
| df = ctx.create_dataframe([[pa.RecordBatch.from_arrays([arr], names=["u"])]]) | ||
|
|
||
| result = df.select(f.union_extract(column("u"), "int").alias("val")).collect()[0] | ||
| assert result.column(0).to_pylist() == [1, None, 2] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.