Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions python/lsst/daf/butler/formatters/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down Expand Up @@ -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.")

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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.


# 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."
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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."
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests of the functionality is missing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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)
Expand Down Expand Up @@ -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."""
Expand Down
Loading