Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 166
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all 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
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -98,6 +98,7 @@ | ||
| "arrays_overlap", | ||
| "arrays_zip", | ||
| "arrow_cast", | ||
| "arrow_metadata", | ||
| "arrow_typeof", | ||
| "ascii", | ||
| "asin", | ||
| @@ -163,6 +164,7 @@ | ||
| "gcd", | ||
| "gen_series", | ||
| "generate_series", | ||
| "get_field", | ||
| "greatest", | ||
| "ifnull", | ||
| "in_list", | ||
| @@ -280,6 +282,7 @@ | ||
| "reverse", | ||
| "right", | ||
| "round", | ||
| "row", | ||
| "row_number", | ||
| "rpad", | ||
| "rtrim", | ||
| @@ -322,12 +325,15 @@ | ||
| "translate", | ||
| "trim", | ||
| "trunc", | ||
| "union_extract", | ||
| "union_tag", | ||
| "upper", | ||
| "uuid", | ||
| "var", | ||
| "var_pop", | ||
| "var_samp", | ||
| "var_sample", | ||
| "version", | ||
| "when", | ||
| # Window Functions | ||
| "window", | ||
| @@ -2628,22 +2634,184 @@ def arrow_typeof(arg: Expr) -> Expr: | ||
| return Expr(f.arrow_typeof(arg.expr)) | ||
| def arrow_cast(expr: Expr, data_type: Expr) -> Expr: | ||
| def arrow_cast(expr: Expr, data_type: Expr | str | pa.DataType) -> Expr: | ||
| """Casts an expression to a specified data type. | ||
| The ``data_type`` can be a string, a ``pyarrow.DataType``, or an | ||
| ``Expr``. For simple types, :py:meth:`Expr.cast() | ||
| <datafusion.expr.Expr.cast>` is more concise | ||
| (e.g., ``col("a").cast(pa.float64())``). Use ``arrow_cast`` when | ||
| you want to specify the target type as a string using DataFusion's | ||
| type syntax, which can be more readable for complex types like | ||
| ``"Timestamp(Nanosecond, None)"``. | ||
| Examples: | ||
| >>> ctx = dfn.SessionContext() | ||
| >>> df = ctx.from_pydict({"a": [1]}) | ||
| >>> data_type = dfn.string_literal("Float64") | ||
| >>> result = df.select( | ||
| ... dfn.functions.arrow_cast(dfn.col("a"), data_type).alias("c") | ||
| ... dfn.functions.arrow_cast(dfn.col("a"), "Float64").alias("c") | ||
| ... ) | ||
| >>> result.collect_column("c")[0].as_py() | ||
| 1.0 | ||
| >>> import pyarrow as pa | ||
| >>> result = df.select( | ||
| ... dfn.functions.arrow_cast( | ||
| ... dfn.col("a"), data_type=pa.float64() | ||
| ... ).alias("c") | ||
| ... ) | ||
| >>> result.collect_column("c")[0].as_py() | ||
| 1.0 | ||
| """ | ||
| if isinstance(data_type, pa.DataType): | ||
| return expr.cast(data_type) | ||
| if isinstance(data_type, str): | ||
timsaucer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| data_type = Expr.string_literal(data_type) | ||
| return Expr(f.arrow_cast(expr.expr, data_type.expr)) | ||
| def arrow_metadata(expr: Expr, key: Expr | str | None = None) -> Expr: | ||
| """Returns the metadata of the input expression. | ||
| If called with one argument, returns a Map of all metadata key-value pairs. | ||
| If called with two arguments, returns the value for the specified metadata key. | ||
| Examples: | ||
| >>> import pyarrow as pa | ||
| >>> field = pa.field("val", pa.int64(), metadata={"k": "v"}) | ||
| >>> schema = pa.schema([field]) | ||
| >>> batch = pa.RecordBatch.from_arrays([pa.array([1])], schema=schema) | ||
| >>> ctx = dfn.SessionContext() | ||
| >>> df = ctx.create_dataframe([[batch]]) | ||
| >>> result = df.select( | ||
| ... dfn.functions.arrow_metadata(dfn.col("val")).alias("meta") | ||
| ... ) | ||
| >>> ("k", "v") in result.collect_column("meta")[0].as_py() | ||
| True | ||
| >>> result = df.select( | ||
| ... dfn.functions.arrow_metadata( | ||
| ... dfn.col("val"), key="k" | ||
| ... ).alias("meta_val") | ||
| ... ) | ||
| >>> result.collect_column("meta_val")[0].as_py() | ||
| 'v' | ||
| """ | ||
| if key is None: | ||
| return Expr(f.arrow_metadata(expr.expr)) | ||
| if isinstance(key, str): | ||
| key = Expr.string_literal(key) | ||
| return Expr(f.arrow_metadata(expr.expr, key.expr)) | ||
| def get_field(expr: Expr, name: Expr | str) -> Expr: | ||
| """Extracts a field from a struct or map by name. | ||
| When the field name is a static string, the bracket operator | ||
| ``expr["field"]`` is a convenient shorthand. Use ``get_field`` | ||
| when the field name is a dynamic expression. | ||
| Examples: | ||
| >>> ctx = dfn.SessionContext() | ||
| >>> df = ctx.from_pydict({"a": [1], "b": [2]}) | ||
| >>> df = df.with_column( | ||
| ... "s", | ||
| ... dfn.functions.named_struct( | ||
| ... [("x", dfn.col("a")), ("y", dfn.col("b"))] | ||
| ... ), | ||
| ... ) | ||
| >>> result = df.select( | ||
| ... dfn.functions.get_field(dfn.col("s"), "x").alias("x_val") | ||
| ... ) | ||
| >>> result.collect_column("x_val")[0].as_py() | ||
| 1 | ||
| Equivalent using bracket syntax: | ||
| >>> result = df.select( | ||
| ... dfn.col("s")["x"].alias("x_val") | ||
| ... ) | ||
| >>> result.collect_column("x_val")[0].as_py() | ||
| 1 | ||
| """ | ||
| if isinstance(name, str): | ||
| name = Expr.string_literal(name) | ||
| return Expr(f.get_field(expr.expr, name.expr)) | ||
| def union_extract(union_expr: Expr, field_name: Expr | str) -> Expr: | ||
| """Extracts a value from a union type by field name. | ||
| Returns the value of the named field if it is the currently selected | ||
| variant, otherwise returns NULL. | ||
| Examples: | ||
| >>> import pyarrow as pa | ||
| >>> ctx = dfn.SessionContext() | ||
| >>> types = pa.array([0, 1, 0], type=pa.int8()) | ||
| >>> offsets = pa.array([0, 0, 1], type=pa.int32()) | ||
| >>> arr = pa.UnionArray.from_dense( | ||
| ... types, offsets, [pa.array([1, 2]), pa.array(["hi"])], | ||
| ... ["int", "str"], [0, 1], | ||
| ... ) | ||
| >>> batch = pa.RecordBatch.from_arrays([arr], names=["u"]) | ||
| >>> df = ctx.create_dataframe([[batch]]) | ||
| >>> result = df.select( | ||
| ... dfn.functions.union_extract(dfn.col("u"), "int").alias("val") | ||
| ... ) | ||
| >>> result.collect_column("val").to_pylist() | ||
| [1, None, 2] | ||
| """ | ||
| if isinstance(field_name, str): | ||
| field_name = Expr.string_literal(field_name) | ||
| return Expr(f.union_extract(union_expr.expr, field_name.expr)) | ||
| def union_tag(union_expr: Expr) -> Expr: | ||
| """Returns the tag (active field name) of a union type. | ||
| Examples: | ||
| >>> import pyarrow as pa | ||
| >>> ctx = dfn.SessionContext() | ||
| >>> types = pa.array([0, 1, 0], type=pa.int8()) | ||
| >>> offsets = pa.array([0, 0, 1], type=pa.int32()) | ||
| >>> arr = pa.UnionArray.from_dense( | ||
| ... types, offsets, [pa.array([1, 2]), pa.array(["hi"])], | ||
| ... ["int", "str"], [0, 1], | ||
| ... ) | ||
| >>> batch = pa.RecordBatch.from_arrays([arr], names=["u"]) | ||
| >>> df = ctx.create_dataframe([[batch]]) | ||
| >>> result = df.select( | ||
| ... dfn.functions.union_tag(dfn.col("u")).alias("tag") | ||
| ... ) | ||
| >>> result.collect_column("tag").to_pylist() | ||
| ['int', 'str', 'int'] | ||
| """ | ||
| return Expr(f.union_tag(union_expr.expr)) | ||
| def version() -> Expr: | ||
| """Returns the DataFusion version string. | ||
| Examples: | ||
| >>> ctx = dfn.SessionContext() | ||
| >>> df = ctx.empty_table() | ||
| >>> result = df.select(dfn.functions.version().alias("v")) | ||
| >>> "Apache DataFusion" in result.collect_column("v")[0].as_py() | ||
| True | ||
| """ | ||
| return Expr(f.version()) | ||
| def row(*args: Expr) -> Expr: | ||
| """Returns a struct with the given arguments. | ||
| See Also: | ||
| This is an alias for :py:func:`struct`. | ||
| """ | ||
| return struct(*args) | ||
| def random() -> Expr: | ||
| """Returns a random value in the range ``0.0 <= x < 1.0``. | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.