-
Notifications
You must be signed in to change notification settings - Fork 20
Added parquet support for filters parameter #1308
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -131,6 +131,8 @@ def _read_parquet( | |
| component: str | None = None, | ||
| expected_size: int = -1, | ||
| ) -> Any: | ||
| import pyarrow.dataset as ds | ||
|
|
||
| with generic_open(path, fs) as handle: | ||
| schema = pq.read_schema(handle) | ||
|
|
||
|
|
@@ -156,6 +158,7 @@ def _read_parquet( | |
| return len(temp_table[schema.names[0]]) | ||
|
|
||
| par_columns = None | ||
| par_filters = None | ||
| if self.file_descriptor.parameters: | ||
| par_columns = self.file_descriptor.parameters.pop("columns", None) | ||
| if par_columns: | ||
|
|
@@ -190,6 +193,27 @@ def _read_parquet( | |
| par_columns, | ||
| ) | ||
|
|
||
| par_filters = self.file_descriptor.parameters.pop("filters", None) | ||
| if par_filters: | ||
| # Validate basic filter structure | ||
| if not isinstance(par_filters, ds.Expression): | ||
| if not isinstance(par_filters, list): | ||
| raise TypeError("Filters must be a list or a pyarrow.dataset.Expression.") | ||
|
|
||
| # Lists must contain tuples or lists of tuples | ||
| if all(isinstance(item, tuple) for item in par_filters): | ||
| par_filters = [par_filters] # convert to DNF for column checking | ||
| elif not all(isinstance(item, list) for item in par_filters): | ||
| raise TypeError("List filters must contain tuples or lists of tuples.") | ||
|
|
||
| # Ensure requested columns are in schema | ||
| for predicate in par_filters: | ||
| for col, _, _ in predicate: | ||
| if col not in schema.names: | ||
| raise ValueError( | ||
| f"Column {col} specified in filters not available in parquet file." | ||
| ) | ||
|
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. Is the indentation correct above? This won't do the check if it is an Expression; is that intended?
Contributor
Author
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. It is not, I should fix it to perform the check if it is an Expression as well. |
||
|
|
||
| if len(self.file_descriptor.parameters): | ||
| raise ValueError( | ||
| f"Unsupported parameters {self.file_descriptor.parameters} in ArrowTable read." | ||
|
|
@@ -202,6 +226,7 @@ def _read_parquet( | |
| columns=par_columns, | ||
| use_threads=False, | ||
| use_pandas_metadata=(b"pandas" in metadata), | ||
| filters=par_filters, | ||
| ) | ||
|
|
||
| return arrow_table | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -408,6 +408,12 @@ def testSingleIndexDataFrame(self): | |
| # Passing an unrecognized column should be a ValueError. | ||
| with self.assertRaises(ValueError): | ||
| self.butler.get(self.datasetType, dataId={}, parameters={"columns": ["e"]}) | ||
| # Filter predicates with unrecognized column should be a ValueError. | ||
| # FIXME: ValueErrorWeirdness1. This raises an AssertionError, | ||
| # but Ctrl+f "ValueErrorWeirdness2" below. | ||
| # with self.assertRaises(ValueError): | ||
| # self.butler.get(self.datasetType, dataId={}, | ||
| # parameters={"filters": [("e", ">", 1)]}) | ||
|
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. Tests of the functionality is missing.
Contributor
Author
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. Yes, this is addressed in the third bullet points of the notes above. I couldn't figure out why it raises a ValueError outside of the context manager, but calling the context manager to check for a ValueError raises an AssertionError. |
||
|
|
||
| def testSingleIndexDataFrameWithLists(self): | ||
| df1, allColumns = _makeSingleIndexDataFrame(include_lists=True) | ||
|
|
@@ -455,6 +461,11 @@ def testMultiIndexDataFrame(self): | |
| # Passing an unrecognized column should be a ValueError. | ||
| with self.assertRaises(ValueError): | ||
| self.butler.get(self.datasetType, dataId={}, parameters={"columns": ["d"]}) | ||
| # Filter predicates with unrecognized column should be a ValueError. | ||
| # FIXME: ValueErrorWeirdness2. This raises a ValueError, but Ctrl+f | ||
| # "ValueErrorWeirdness1" above. | ||
| # df7 = self.butler.get(self.datasetType, dataId={}, | ||
| # parameters={"filters": [("d", ">", 1)]}) | ||
|
|
||
| def testSingleIndexDataFrameEmptyString(self): | ||
| """Test persisting a single index dataframe with empty strings.""" | ||
|
|
||
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.
I don't understand this section at all. If it's a list of tuples you convert a list to a list of lists? And then if it's a list of lists it raises?
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.
I converted the list of tuples to a list of list of tuples for streamlined column checking later on (par_filters in DNF). It should only raise the TypeError if it is not a list of tuples or list of lists.