Skip to content

Commit

Permalink
Add the negative --limit facility to dimension records and data IDs c…
Browse files Browse the repository at this point in the history
…ommand line

This was missed before since we added negative support at the last minute.
We can't actually use that negative support handling because the
command line uses advanced query interface to do the dataset type
joining.
  • Loading branch information
timj committed Sep 9, 2024
1 parent 3533f90 commit 5f64de8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
9 changes: 7 additions & 2 deletions python/lsst/daf/butler/cli/opt/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,16 @@ def _config_split(*args: Any) -> dict[str | None, str]:
)


_default_limit = -20_000
limit_option = MWOptionDecorator(
"--limit",
help=unwrap("Limit the number of records, by default all records are shown."),
help=unwrap(
f"""Limit the number of results that are processed. 0 means no limit. A negative
value specifies a cap where a warning will be issued if the cap is hit.
Default value is {_default_limit}."""
),
type=int,
default=0,
default=_default_limit,
)

offset_option = MWOptionDecorator(
Expand Down
21 changes: 19 additions & 2 deletions python/lsst/daf/butler/script/queryDataIds.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ def __init__(self, dataIds: Iterable[DataCoordinate]):
# use dict to store dataIds as keys to preserve ordering
self.dataIds = dict.fromkeys(dataIds)

def __len__(self) -> int:
return len(self.dataIds)

Check warning on line 61 in python/lsst/daf/butler/script/queryDataIds.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/script/queryDataIds.py#L61

Added line #L61 was not covered by tests

def pop_last(self) -> None:
if self.dataIds:
final_key = list(self.dataIds.keys())[-1]
self.dataIds.pop(final_key)

Check warning on line 66 in python/lsst/daf/butler/script/queryDataIds.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/script/queryDataIds.py#L65-L66

Added lines #L65 - L66 were not covered by tests

def getAstropyTable(self, order: bool) -> AstropyTable:
"""Get the table as an astropy table.
Expand Down Expand Up @@ -195,12 +203,21 @@ def queryDataIds(
results = results.where(where)
if order_by:
results = results.order_by(*order_by)
if limit > 0:
results = results.limit(limit)
query_limit = abs(limit)
warn_limit = False
if limit != 0:
if limit < 0:
query_limit += 1
warn_limit = True

Check warning on line 211 in python/lsst/daf/butler/script/queryDataIds.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/script/queryDataIds.py#L210-L211

Added lines #L210 - L211 were not covered by tests

results = results.limit(query_limit)

Check warning on line 213 in python/lsst/daf/butler/script/queryDataIds.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/script/queryDataIds.py#L213

Added line #L213 was not covered by tests

if results.any(exact=False):
if results.dimensions:
table = _Table(results)
if warn_limit and len(table) == query_limit:
table.pop_last()
_LOG.warning("More data IDs are available than the request limit of %d", abs(limit))

Check warning on line 220 in python/lsst/daf/butler/script/queryDataIds.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/script/queryDataIds.py#L219-L220

Added lines #L219 - L220 were not covered by tests
if not table.dataIds:
return None, "Post-query region filtering removed all rows, since nothing overlapped."
return table.getAstropyTable(not order_by), None
Expand Down
16 changes: 14 additions & 2 deletions python/lsst/daf/butler/script/queryDimensionRecords.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from __future__ import annotations

import logging
from operator import attrgetter
from typing import Any

Expand All @@ -36,6 +37,8 @@
from .._butler import Butler
from .._timespan import Timespan

_LOG = logging.getLogger(__name__)


def queryDimensionRecords(
repo: str,
Expand Down Expand Up @@ -100,10 +103,19 @@ def queryDimensionRecords(
query_results = query_results.where(where)
if order_by:
query_results = query_results.order_by(*order_by)
if limit > 0:
query_results = query_results.limit(limit)
query_limit = abs(limit)
warn_limit = False
if limit != 0:
if limit < 0:
query_limit += 1
warn_limit = True

query_results = query_results.limit(query_limit)

records = list(query_results)
if warn_limit and len(records) == query_limit:
records.pop(-1)
_LOG.warning("More data IDs are available than the request limit of %d", abs(limit))

Check warning on line 118 in python/lsst/daf/butler/script/queryDimensionRecords.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/script/queryDimensionRecords.py#L117-L118

Added lines #L117 - L118 were not covered by tests

if not records:
return None
Expand Down

0 comments on commit 5f64de8

Please sign in to comment.