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 deregister methods to SessionContext#1473
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
03fde8a9169146b3bd04b79e3546File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -568,6 +568,15 @@ def register_object_store( | ||
| """ | ||
| self.ctx.register_object_store(schema, store, host) | ||
| def deregister_object_store(self, schema: str, host: str | None = None) -> None: | ||
| """Remove an object store from the session. | ||
| Args: | ||
| schema: The data source schema (e.g. ``"s3://"``). | ||
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. Would be nice to add this | ||
| host: URL for the host (e.g. bucket name). | ||
| """ | ||
| self.ctx.deregister_object_store(schema, host) | ||
timsaucer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def register_listing_table( | ||
| self, | ||
| name: str, | ||
| @@ -894,6 +903,14 @@ def register_udtf(self, func: TableFunction) -> None: | ||
| """Register a user defined table function.""" | ||
| self.ctx.register_udtf(func._udtf) | ||
| def deregister_udtf(self, name: str) -> None: | ||
| """Remove a user-defined table function from the session. | ||
| Args: | ||
| name: Name of the UDTF to deregister. | ||
| """ | ||
| self.ctx.deregister_udtf(name) | ||
| def register_record_batches( | ||
| self, name: str, partitions: list[list[pa.RecordBatch]] | ||
| ) -> None: | ||
| @@ -1105,14 +1122,38 @@ def register_udf(self, udf: ScalarUDF) -> None: | ||
| """Register a user-defined function (UDF) with the context.""" | ||
| self.ctx.register_udf(udf._udf) | ||
| def deregister_udf(self, name: str) -> None: | ||
| """Remove a user-defined scalar function from the session. | ||
| Args: | ||
| name: Name of the UDF to deregister. | ||
| """ | ||
| self.ctx.deregister_udf(name) | ||
| def register_udaf(self, udaf: AggregateUDF) -> None: | ||
| """Register a user-defined aggregation function (UDAF) with the context.""" | ||
| self.ctx.register_udaf(udaf._udaf) | ||
| def deregister_udaf(self, name: str) -> None: | ||
| """Remove a user-defined aggregate function from the session. | ||
| Args: | ||
| name: Name of the UDAF to deregister. | ||
| """ | ||
| self.ctx.deregister_udaf(name) | ||
| def register_udwf(self, udwf: WindowUDF) -> None: | ||
| """Register a user-defined window function (UDWF) with the context.""" | ||
| self.ctx.register_udwf(udwf._udwf) | ||
| def deregister_udwf(self, name: str) -> None: | ||
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. For all these deregister calls I assume they are for long running sessions and people aren't regularly registering and unregistering in quick succession. If they were then a context manager might be nice to manage scoped lifetimes. Probably out of scope for here or unless someone asks for it. | ||
| """Remove a user-defined window function from the session. | ||
| Args: | ||
| name: Name of the UDWF to deregister. | ||
| """ | ||
| self.ctx.deregister_udwf(name) | ||
| def catalog(self, name: str = "datafusion") -> Catalog: | ||
| """Retrieve a catalog by name.""" | ||
| return Catalog(self.ctx.catalog(name)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -31,6 +31,7 @@ | ||
| Table, | ||
| column, | ||
| literal, | ||
| udf, | ||
| ) | ||
| @@ -351,6 +352,125 @@ def test_deregister_table(ctx, database): | ||
| assert public.names() == {"csv1", "csv2"} | ||
| def test_deregister_udf(): | ||
| ctx = SessionContext() | ||
| is_null = udf( | ||
| lambda x: x.is_null(), | ||
| [pa.float64()], | ||
| pa.bool_(), | ||
| volatility="immutable", | ||
| name="my_is_null", | ||
| ) | ||
| ctx.register_udf(is_null) | ||
| # Verify it works | ||
| df = ctx.from_pydict({"a": [1.0, None]}) | ||
| ctx.register_table("t", df.into_view()) | ||
| result = ctx.sql("SELECT my_is_null(a) FROM t").collect() | ||
| assert result[0].column(0) == pa.array([False, True]) | ||
| # Deregister and verify it's gone | ||
| ctx.deregister_udf("my_is_null") | ||
| with pytest.raises(ValueError): | ||
| ctx.sql("SELECT my_is_null(a) FROM t").collect() | ||
timsaucer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def test_deregister_udaf(): | ||
| import pyarrow.compute as pc | ||
| ctx = SessionContext() | ||
| from datafusion import Accumulator, udaf | ||
| class MySum(Accumulator): | ||
| def __init__(self): | ||
| self._sum = 0.0 | ||
| def update(self, values: pa.Array) -> None: | ||
| self._sum += pc.sum(values).as_py() | ||
| def merge(self, states: list[pa.Array]) -> None: | ||
| self._sum += pc.sum(states[0]).as_py() | ||
| def state(self) -> list: | ||
| return [self._sum] | ||
| def evaluate(self) -> pa.Scalar: | ||
| return self._sum | ||
| my_sum = udaf( | ||
| MySum, | ||
| [pa.float64()], | ||
| pa.float64(), | ||
| [pa.float64()], | ||
| volatility="immutable", | ||
| name="my_sum", | ||
| ) | ||
| ctx.register_udaf(my_sum) | ||
| df = ctx.from_pydict({"a": [1.0, 2.0, 3.0]}) | ||
| ctx.register_table("t", df.into_view()) | ||
| result = ctx.sql("SELECT my_sum(a) FROM t").collect() | ||
| assert result[0].column(0) == pa.array([6.0]) | ||
| ctx.deregister_udaf("my_sum") | ||
| with pytest.raises(ValueError): | ||
| ctx.sql("SELECT my_sum(a) FROM t").collect() | ||
timsaucer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def test_deregister_udwf(): | ||
| ctx = SessionContext() | ||
| from datafusion import udwf | ||
| from datafusion.user_defined import WindowEvaluator | ||
| class MyRowNumber(WindowEvaluator): | ||
| def __init__(self): | ||
| self._row = 0 | ||
| def evaluate_all(self, values, num_rows): | ||
| return pa.array(list(range(1, num_rows + 1)), type=pa.uint64()) | ||
| my_row_number = udwf( | ||
| MyRowNumber, | ||
| [pa.float64()], | ||
| pa.uint64(), | ||
| volatility="immutable", | ||
| name="my_row_number", | ||
| ) | ||
| ctx.register_udwf(my_row_number) | ||
| df = ctx.from_pydict({"a": [1.0, 2.0, 3.0]}) | ||
| ctx.register_table("t", df.into_view()) | ||
| result = ctx.sql("SELECT my_row_number(a) OVER () FROM t").collect() | ||
| assert result[0].column(0) == pa.array([1, 2, 3], type=pa.uint64()) | ||
| ctx.deregister_udwf("my_row_number") | ||
| with pytest.raises(ValueError): | ||
| ctx.sql("SELECT my_row_number(a) OVER () FROM t").collect() | ||
timsaucer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def test_deregister_udtf(): | ||
| import pyarrow.dataset as ds | ||
| ctx = SessionContext() | ||
| from datafusion import Table, udtf | ||
| class MyTable: | ||
| def __call__(self): | ||
| batch = pa.RecordBatch.from_pydict({"x": [1, 2, 3]}) | ||
| return Table(ds.dataset([batch])) | ||
| my_table = udtf(MyTable(), "my_table") | ||
| ctx.register_udtf(my_table) | ||
| result = ctx.sql("SELECT * FROM my_table()").collect() | ||
| assert result[0].column(0) == pa.array([1, 2, 3]) | ||
| ctx.deregister_udtf("my_table") | ||
| with pytest.raises(ValueError): | ||
| ctx.sql("SELECT * FROM my_table()").collect() | ||
timsaucer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def test_register_table_from_dataframe(ctx): | ||
| df = ctx.from_pydict({"a": [1, 2]}) | ||
| ctx.register_table("df_tbl", df) | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.