-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expose ExecutionContext.register_csv to the python bindings #524
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9498c8c
Expose register_csv
kszucs c10f6f9
Validate delimiter
kszucs 9b06c18
Fix tests
kszucs 9d8c63e
Pass schema
kszucs 539ca25
unused imports
kszucs bca0f54
add linting
kszucs 73eee6f
Update deps
kszucs ca3b663
Restore venv
kszucs 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
|---|---|---|
|
|
@@ -18,8 +18,8 @@ | |
| import numpy as np | ||
| import pyarrow as pa | ||
| import pytest | ||
| from datafusion import ExecutionContext | ||
|
|
||
| from datafusion import ExecutionContext | ||
| from . import generic as helpers | ||
|
|
||
|
|
||
|
|
@@ -33,12 +33,63 @@ def test_no_table(ctx): | |
| ctx.sql("SELECT a FROM b").collect() | ||
|
|
||
|
|
||
| def test_register(ctx, tmp_path): | ||
| def test_register_csv(ctx, tmp_path): | ||
| path = tmp_path / "test.csv" | ||
|
|
||
| table = pa.Table.from_arrays( | ||
| [ | ||
| [1, 2, 3, 4], | ||
| ["a", "b", "c", "d"], | ||
| [1.1, 2.2, 3.3, 4.4], | ||
| ], | ||
| names=["int", "str", "float"], | ||
| ) | ||
| pa.csv.write_csv(table, path) | ||
|
|
||
| ctx.register_csv("csv", path) | ||
| ctx.register_csv("csv1", str(path)) | ||
| ctx.register_csv( | ||
| "csv2", | ||
| path, | ||
| has_header=True, | ||
| delimiter=",", | ||
| schema_infer_max_records=10, | ||
| ) | ||
| alternative_schema = pa.schema( | ||
| [ | ||
| ("some_int", pa.int16()), | ||
| ("some_bytes", pa.string()), | ||
| ("some_floats", pa.float32()), | ||
| ] | ||
| ) | ||
| ctx.register_csv("csv3", path, schema=alternative_schema) | ||
|
|
||
| assert ctx.tables() == {"csv", "csv1", "csv2", "csv3"} | ||
|
|
||
| for table in ["csv", "csv1", "csv2"]: | ||
| result = ctx.sql(f"SELECT COUNT(int) FROM {table}").collect() | ||
| result = pa.Table.from_batches(result) | ||
| assert result.to_pydict() == {"COUNT(int)": [4]} | ||
|
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. 👍 |
||
|
|
||
| result = ctx.sql("SELECT * FROM csv3").collect() | ||
| result = pa.Table.from_batches(result) | ||
| assert result.schema == alternative_schema | ||
|
|
||
| with pytest.raises( | ||
| ValueError, match="Delimiter must be a single character" | ||
| ): | ||
| ctx.register_csv("csv4", path, delimiter="wrong") | ||
|
|
||
|
|
||
| def test_register_parquet(ctx, tmp_path): | ||
| path = helpers.write_parquet(tmp_path / "a.parquet", helpers.data()) | ||
| ctx.register_parquet("t", path) | ||
|
|
||
| assert ctx.tables() == {"t"} | ||
|
|
||
| result = ctx.sql("SELECT COUNT(a) FROM t").collect() | ||
| result = pa.Table.from_batches(result) | ||
| assert result.to_pydict() == {"COUNT(a)": [100]} | ||
|
|
||
|
|
||
| def test_execute(ctx, tmp_path): | ||
| data = [1, 1, 2, 2, 3, 11, 12] | ||
|
|
@@ -112,7 +163,9 @@ def test_cast(ctx, tmp_path): | |
| "float", | ||
| ] | ||
|
|
||
| select = ", ".join([f"CAST(9 AS {t}) AS A{i}" for i, t in enumerate(valid_types)]) | ||
| select = ", ".join( | ||
| [f"CAST(9 AS {t}) AS A{i}" for i, t in enumerate(valid_types)] | ||
| ) | ||
|
|
||
| # can execute, which implies that we can cast | ||
| ctx.sql(f"SELECT {select} FROM t").collect() | ||
|
|
@@ -141,7 +194,9 @@ def test_udf( | |
| ctx, tmp_path, fn, input_types, output_type, input_values, expected_values | ||
| ): | ||
| # write to disk | ||
| path = helpers.write_parquet(tmp_path / "a.parquet", pa.array(input_values)) | ||
| path = helpers.write_parquet( | ||
| tmp_path / "a.parquet", pa.array(input_values) | ||
| ) | ||
| ctx.register_parquet("t", path) | ||
| ctx.register_udf("udf", fn, input_types, output_type) | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied from https://github.com/apache/arrow-rs/blob/master/arrow-pyarrow-integration-testing/src/lib.rs#L136
Eventually we could add an optional module to arrow-rs where we implement the PyO3 conversion traits for
arrow-rs <-> pyarrowinteroperability for easier downstream integration.