Coverage for python/lsst/daf/butler/queries/_dataset_query_results.py: 59%
42 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-05 10:00 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-05 10:00 +0000
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__ = ("DatasetRefQueryResults",)
32from collections.abc import Iterator
33from typing import TYPE_CHECKING, Any, final
35from .._dataset_ref import DatasetRef
36from .._dataset_type import DatasetType
37from ._base import QueryResultsBase
38from .driver import QueryDriver
39from .result_specs import DataCoordinateResultSpec, DatasetRefResultSpec
40from .tree import QueryTree
42if TYPE_CHECKING:
43 from ._data_coordinate_query_results import DataCoordinateQueryResults
46@final
47class DatasetRefQueryResults(QueryResultsBase):
48 """A query for `DatasetRef` results with a single dataset type.
50 Parameters
51 ----------
52 driver : `QueryDriver`
53 Implementation object that knows how to actually execute queries.
54 tree : `QueryTree`
55 Description of the query as a tree of joins and column expressions. The
56 instance returned directly by the `Butler._query` entry point should be
57 constructed via `make_unit_query_tree`.
58 spec : `DatasetRefResultSpec`
59 Specification of the query result rows, including output columns,
60 ordering, and slicing.
62 Notes
63 -----
64 This class should never be constructed directly by users; use
65 `Query.datasets` instead.
66 """
68 def __init__(self, driver: QueryDriver, tree: QueryTree, spec: DatasetRefResultSpec):
69 spec.validate_tree(tree)
70 super().__init__(driver, tree)
71 self._spec = spec
73 def __iter__(self) -> Iterator[DatasetRef]:
74 page = self._driver.execute(self._spec, self._tree)
75 yield from page.rows
76 while page.next_key is not None:
77 page = self._driver.fetch_next_page(self._spec, page.next_key)
78 yield from page.rows
80 @property
81 def dataset_type(self) -> DatasetType:
82 # Docstring inherited.
83 return DatasetType(self._spec.dataset_type_name, self._spec.dimensions, self._spec.storage_class_name)
85 @property
86 def data_ids(self) -> DataCoordinateQueryResults:
87 # Docstring inherited.
88 from ._data_coordinate_query_results import DataCoordinateQueryResults
90 return DataCoordinateQueryResults(
91 self._driver,
92 tree=self._tree,
93 spec=DataCoordinateResultSpec.model_construct(
94 dimensions=self.dataset_type.dimensions.as_group(),
95 include_dimension_records=self._spec.include_dimension_records,
96 ),
97 )
99 @property
100 def has_dimension_records(self) -> bool:
101 """Whether all data IDs in this iterable contain dimension records."""
102 return self._spec.include_dimension_records
104 def with_dimension_records(self) -> DatasetRefQueryResults:
105 """Return a results object for which `has_dimension_records` is
106 `True`.
107 """
108 if self.has_dimension_records:
109 return self
110 return self._copy(tree=self._tree, include_dimension_records=True)
112 def count(self, *, exact: bool = True, discard: bool = False) -> int:
113 # Docstring inherited.
114 return self._driver.count(self._tree, self._spec, exact=exact, discard=discard)
116 def _copy(self, tree: QueryTree, **kwargs: Any) -> DatasetRefQueryResults:
117 # Docstring inherited.
118 return DatasetRefQueryResults(self._driver, tree, self._spec.model_copy(update=kwargs))
120 def _get_datasets(self) -> frozenset[str]:
121 # Docstring inherited.
122 return frozenset({self.dataset_type.name})