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 registration methods#1474
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
30fc3d5e494bed246105d4a2d7ba03092ed0f96ea3d0b5d23b719b47af08ee6de89982a32292aFile 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 |
|---|---|---|
| @@ -903,6 +903,27 @@ def register_udtf(self, func: TableFunction) -> None: | ||
| """Register a user defined table function.""" | ||
| self.ctx.register_udtf(func._udtf) | ||
| def register_batch(self, name: str, batch: pa.RecordBatch) -> None: | ||
| """Register a single :py:class:`pa.RecordBatch` as a table. | ||
| Args: | ||
| name: Name of the resultant table. | ||
| batch: Record batch to register as a table. | ||
| Examples: | ||
| >>> ctx = dfn.SessionContext() | ||
| >>> batch = pa.RecordBatch.from_pydict({"a": [1, 2, 3]}) | ||
| >>> ctx.register_batch("batch_tbl", batch) | ||
| >>> ctx.sql("SELECT * FROM batch_tbl").collect()[0].column(0) | ||
| <pyarrow.lib.Int64Array object at ...> | ||
| [ | ||
| 1, | ||
| 2, | ||
| 3 | ||
| ] | ||
| """ | ||
| self.ctx.register_batch(name, batch) | ||
| def deregister_udtf(self, name: str) -> None: | ||
| """Remove a user-defined table function from the session. | ||
| @@ -1109,6 +1130,86 @@ def register_avro( | ||
| name, str(path), schema, file_extension, table_partition_cols | ||
| ) | ||
| def register_arrow( | ||
| self, | ||
| name: str, | ||
| path: str | pathlib.Path, | ||
| schema: pa.Schema | None = None, | ||
| file_extension: str = ".arrow", | ||
| table_partition_cols: list[tuple[str, str | pa.DataType]] | None = None, | ||
| ) -> None: | ||
| """Register an Arrow IPC file as a table. | ||
| The registered table can be referenced from SQL statements executed | ||
| against this context. | ||
| Args: | ||
| name: Name of the table to register. | ||
| path: Path to the Arrow IPC file. | ||
| schema: The data source schema. | ||
| file_extension: File extension to select. | ||
| table_partition_cols: Partition columns. | ||
| Examples: | ||
timsaucer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| >>> import tempfile, os | ||
| >>> ctx = dfn.SessionContext() | ||
| >>> table = pa.table({"x": [10, 20, 30]}) | ||
| >>> with tempfile.TemporaryDirectory() as tmpdir: | ||
| ... path = os.path.join(tmpdir, "data.arrow") | ||
| ... with pa.ipc.new_file(path, table.schema) as writer: | ||
| ... writer.write_table(table) | ||
| ... ctx.register_arrow("arrow_tbl", path) | ||
| ... ctx.sql("SELECT * FROM arrow_tbl").collect()[0].column(0) | ||
| <pyarrow.lib.Int64Array object at ...> | ||
| [ | ||
| 10, | ||
| 20, | ||
| 30 | ||
| ] | ||
| Provide an explicit ``schema`` to override schema inference: | ||
| >>> with tempfile.TemporaryDirectory() as tmpdir: | ||
| ... path = os.path.join(tmpdir, "data.arrow") | ||
| ... with pa.ipc.new_file(path, table.schema) as writer: | ||
| ... writer.write_table(table) | ||
| ... ctx.register_arrow( | ||
| ... "arrow_schema", | ||
| ... path, | ||
| ... schema=pa.schema([("x", pa.int64())]), | ||
| ... ) | ||
| ... ctx.sql("SELECT * FROM arrow_schema").collect()[0].column(0) | ||
| <pyarrow.lib.Int64Array object at ...> | ||
| [ | ||
| 10, | ||
| 20, | ||
| 30 | ||
| ] | ||
| Use ``file_extension`` to read files with a non-default extension: | ||
| >>> with tempfile.TemporaryDirectory() as tmpdir: | ||
| ... path = os.path.join(tmpdir, "data.ipc") | ||
| ... with pa.ipc.new_file(path, table.schema) as writer: | ||
| ... writer.write_table(table) | ||
| ... ctx.register_arrow( | ||
| ... "arrow_ipc", path, file_extension=".ipc" | ||
| ... ) | ||
| ... ctx.sql("SELECT * FROM arrow_ipc").collect()[0].column(0) | ||
| <pyarrow.lib.Int64Array object at ...> | ||
| [ | ||
| 10, | ||
| 20, | ||
| 30 | ||
| ] | ||
| """ | ||
| if table_partition_cols is None: | ||
| table_partition_cols = [] | ||
| table_partition_cols = _convert_table_partition_cols(table_partition_cols) | ||
| self.ctx.register_arrow( | ||
| name, str(path), schema, file_extension, table_partition_cols | ||
| ) | ||
| def register_dataset(self, name: str, dataset: pa.dataset.Dataset) -> None: | ||
| """Register a :py:class:`pa.dataset.Dataset` as a table. | ||
| @@ -1369,6 +1470,86 @@ def read_avro( | ||
| self.ctx.read_avro(str(path), schema, file_partition_cols, file_extension) | ||
| ) | ||
| def read_arrow( | ||
| self, | ||
| path: str | pathlib.Path, | ||
| schema: pa.Schema | None = None, | ||
| file_extension: str = ".arrow", | ||
| file_partition_cols: list[tuple[str, str | pa.DataType]] | None = None, | ||
| ) -> DataFrame: | ||
| """Create a :py:class:`DataFrame` for reading an Arrow IPC data source. | ||
| Args: | ||
| path: Path to the Arrow IPC file. | ||
| schema: The data source schema. | ||
| file_extension: File extension to select. | ||
| file_partition_cols: Partition columns. | ||
| Returns: | ||
| DataFrame representation of the read Arrow IPC file. | ||
| Examples: | ||
timsaucer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| >>> import tempfile, os | ||
| >>> ctx = dfn.SessionContext() | ||
| >>> table = pa.table({"a": [1, 2, 3]}) | ||
| >>> with tempfile.TemporaryDirectory() as tmpdir: | ||
| ... path = os.path.join(tmpdir, "data.arrow") | ||
| ... with pa.ipc.new_file(path, table.schema) as writer: | ||
| ... writer.write_table(table) | ||
| ... df = ctx.read_arrow(path) | ||
| ... df.collect()[0].column(0) | ||
| <pyarrow.lib.Int64Array object at ...> | ||
| [ | ||
| 1, | ||
| 2, | ||
| 3 | ||
| ] | ||
| Provide an explicit ``schema`` to override schema inference: | ||
| >>> with tempfile.TemporaryDirectory() as tmpdir: | ||
| ... path = os.path.join(tmpdir, "data.arrow") | ||
| ... with pa.ipc.new_file(path, table.schema) as writer: | ||
| ... writer.write_table(table) | ||
| ... df = ctx.read_arrow(path, schema=pa.schema([("a", pa.int64())])) | ||
| ... df.collect()[0].column(0) | ||
| <pyarrow.lib.Int64Array object at ...> | ||
| [ | ||
| 1, | ||
| 2, | ||
| 3 | ||
| ] | ||
| Use ``file_extension`` to read files with a non-default extension: | ||
| >>> with tempfile.TemporaryDirectory() as tmpdir: | ||
| ... path = os.path.join(tmpdir, "data.ipc") | ||
| ... with pa.ipc.new_file(path, table.schema) as writer: | ||
| ... writer.write_table(table) | ||
| ... df = ctx.read_arrow(path, file_extension=".ipc") | ||
| ... df.collect()[0].column(0) | ||
| <pyarrow.lib.Int64Array object at ...> | ||
| [ | ||
| 1, | ||
| 2, | ||
| 3 | ||
| ] | ||
| """ | ||
| if file_partition_cols is None: | ||
| file_partition_cols = [] | ||
| file_partition_cols = _convert_table_partition_cols(file_partition_cols) | ||
| return DataFrame( | ||
| self.ctx.read_arrow(str(path), schema, file_extension, file_partition_cols) | ||
| ) | ||
timsaucer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def read_empty(self) -> DataFrame: | ||
| """Create an empty :py:class:`DataFrame` with no columns or rows. | ||
| See Also: | ||
| This is an alias for :meth:`empty_table`. | ||
| """ | ||
| return self.empty_table() | ||
| def read_table( | ||
| self, table: Table | TableProviderExportable | DataFrame | pa.dataset.Dataset | ||
| ) -> DataFrame: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -788,6 +788,68 @@ def test_read_avro(ctx): | ||
| assert avro_df is not None | ||
| def test_read_arrow(ctx, tmp_path): | ||
| # Write an Arrow IPC file, then read it back | ||
| table = pa.table({"a": [1, 2, 3], "b": ["x", "y", "z"]}) | ||
| arrow_path = tmp_path / "test.arrow" | ||
| with pa.ipc.new_file(str(arrow_path), table.schema) as writer: | ||
| writer.write_table(table) | ||
| df = ctx.read_arrow(str(arrow_path)) | ||
| result = df.collect() | ||
| assert result[0].column(0) == pa.array([1, 2, 3]) | ||
| assert result[0].column(1) == pa.array(["x", "y", "z"]) | ||
| # Also verify pathlib.Path works | ||
| df = ctx.read_arrow(arrow_path) | ||
| result = df.collect() | ||
| assert result[0].column(0) == pa.array([1, 2, 3]) | ||
| def test_read_empty(ctx): | ||
| df = ctx.read_empty() | ||
| result = df.collect() | ||
| assert len(result) == 1 | ||
| assert result[0].num_columns == 0 | ||
| df = ctx.empty_table() | ||
| result = df.collect() | ||
| assert len(result) == 1 | ||
| assert result[0].num_columns == 0 | ||
timsaucer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def test_register_arrow(ctx, tmp_path): | ||
| # Write an Arrow IPC file, then register and query it | ||
| table = pa.table({"x": [10, 20, 30]}) | ||
| arrow_path = tmp_path / "test.arrow" | ||
| with pa.ipc.new_file(str(arrow_path), table.schema) as writer: | ||
| writer.write_table(table) | ||
| ctx.register_arrow("arrow_tbl", str(arrow_path)) | ||
| result = ctx.sql("SELECT * FROM arrow_tbl").collect() | ||
| assert result[0].column(0) == pa.array([10, 20, 30]) | ||
| # Also verify pathlib.Path works | ||
| ctx.register_arrow("arrow_tbl_path", arrow_path) | ||
| result = ctx.sql("SELECT * FROM arrow_tbl_path").collect() | ||
| assert result[0].column(0) == pa.array([10, 20, 30]) | ||
| def test_register_batch(ctx): | ||
| batch = pa.RecordBatch.from_pydict({"a": [1, 2, 3], "b": [4, 5, 6]}) | ||
| ctx.register_batch("batch_tbl", batch) | ||
| result = ctx.sql("SELECT * FROM batch_tbl").collect() | ||
| assert result[0].column(0) == pa.array([1, 2, 3]) | ||
| assert result[0].column(1) == pa.array([4, 5, 6]) | ||
| def test_register_batch_empty(ctx): | ||
| batch = pa.RecordBatch.from_pydict({"a": pa.array([], type=pa.int64())}) | ||
| ctx.register_batch("empty_batch_tbl", batch) | ||
| result = ctx.sql("SELECT * FROM empty_batch_tbl").collect() | ||
| assert result[0].num_rows == 0 | ||
| def test_create_sql_options(): | ||
| SQLOptions() | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.