Coverage for python/lsst/daf/butler/queries/_data_coordinate_query_results.py: 57%
36 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-08 02:51 -0700
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-08 02:51 -0700
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28from __future__ import annotations
30__all__ = ("DataCoordinateQueryResults",)
32from collections.abc import Iterator
33from typing import TYPE_CHECKING, Any, final
35from ..dimensions import DataCoordinate, DimensionGroup
36from ._base import QueryResultsBase
37from .driver import QueryDriver
38from .tree import QueryTree
40if TYPE_CHECKING:
41 from .result_specs import DataCoordinateResultSpec
44@final
45class DataCoordinateQueryResults(QueryResultsBase):
46 """A query for `DataCoordinate` results.
48 Parameters
49 ----------
50 driver : `QueryDriver`
51 Implementation object that knows how to actually execute queries.
52 tree : `QueryTree`
53 Description of the query as a tree of joins and column expressions. The
54 instance returned directly by the `Butler._query` entry point should be
55 constructed via `make_unit_query_tree`.
56 spec : `DataCoordinateResultSpec`
57 Specification of the query result rows, including output columns,
58 ordering, and slicing.
60 Notes
61 -----
62 This class should never be constructed directly by users; use
63 `Query.data_ids` instead.
64 """
66 def __init__(self, driver: QueryDriver, tree: QueryTree, spec: DataCoordinateResultSpec):
67 spec.validate_tree(tree)
68 super().__init__(driver, tree)
69 self._spec = spec
71 def __iter__(self) -> Iterator[DataCoordinate]:
72 page = self._driver.execute(self._spec, self._tree)
73 yield from page.rows
74 while page.next_key is not None:
75 page = self._driver.fetch_next_page(self._spec, page.next_key)
76 yield from page.rows
78 @property
79 def dimensions(self) -> DimensionGroup:
80 # Docstring inherited
81 return self._spec.dimensions
83 @property
84 def has_dimension_records(self) -> bool:
85 """Whether all data IDs in this iterable contain dimension records."""
86 return self._spec.include_dimension_records
88 def with_dimension_records(self) -> DataCoordinateQueryResults:
89 """Return a results object for which `has_dimension_records` is
90 `True`.
91 """
92 if self.has_dimension_records:
93 return self
94 return self._copy(tree=self._tree, include_dimension_records=True)
96 def count(self, *, exact: bool = True, discard: bool = False) -> int:
97 # Docstring inherited.
98 return self._driver.count(self._tree, self._spec, exact=exact, discard=discard)
100 def _copy(self, tree: QueryTree, **kwargs: Any) -> DataCoordinateQueryResults:
101 # Docstring inherited.
102 return DataCoordinateQueryResults(self._driver, tree, spec=self._spec.model_copy(update=kwargs))
104 def _get_datasets(self) -> frozenset[str]:
105 # Docstring inherited.
106 return frozenset()