Coverage for python / lsst / daf / butler / queries / _dataset_query_results.py: 54%
46 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-22 08:55 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-22 08:55 +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 ..dimensions import DimensionGroup
38from ._base import QueryResultsBase
39from .driver import QueryDriver
40from .result_specs import DataCoordinateResultSpec, DatasetRefResultSpec
41from .tree import QueryTree
43if TYPE_CHECKING:
44 from ._data_coordinate_query_results import DataCoordinateQueryResults
47@final
48class DatasetRefQueryResults(QueryResultsBase):
49 """A query for `DatasetRef` results with a single dataset type.
51 Parameters
52 ----------
53 driver : `QueryDriver`
54 Implementation object that knows how to actually execute queries.
55 tree : `QueryTree`
56 Description of the query as a tree of joins and column expressions. The
57 instance returned directly by the `Butler._query` entry point should be
58 constructed via `make_unit_query_tree`.
59 spec : `DatasetRefResultSpec`
60 Specification of the query result rows, including output columns,
61 ordering, and slicing.
63 Notes
64 -----
65 This class should never be constructed directly by users; use
66 `Query.datasets` instead.
67 """
69 def __init__(self, driver: QueryDriver, tree: QueryTree, spec: DatasetRefResultSpec):
70 spec.validate_tree(tree)
71 super().__init__(driver, tree)
72 self._spec = spec
74 def __iter__(self) -> Iterator[DatasetRef]:
75 for page in self._iter_pages():
76 yield from page
78 def _iter_pages(self) -> Iterator[list[DatasetRef]]:
79 """Return the results from the query in batches as they are returned
80 from the database.
81 """
82 # This method is used outside this class in other daf_butler-internal
83 # functions.
84 for page in self._driver.execute(self._spec, self._tree):
85 yield page.rows
87 @property
88 def dimensions(self) -> DimensionGroup:
89 # Docstring inherited
90 return self._spec.dimensions
92 @property
93 def dataset_type(self) -> DatasetType:
94 # Docstring inherited.
95 return DatasetType(self._spec.dataset_type_name, self._spec.dimensions, self._spec.storage_class_name)
97 @property
98 def data_ids(self) -> DataCoordinateQueryResults:
99 # Docstring inherited.
100 from ._data_coordinate_query_results import DataCoordinateQueryResults
102 return DataCoordinateQueryResults(
103 self._driver,
104 tree=self._tree,
105 spec=DataCoordinateResultSpec.model_construct(
106 dimensions=self.dataset_type.dimensions,
107 include_dimension_records=self._spec.include_dimension_records,
108 ),
109 )
111 @property
112 def has_dimension_records(self) -> bool:
113 """Whether all data IDs in this iterable contain dimension records."""
114 return self._spec.include_dimension_records
116 def with_dimension_records(self) -> DatasetRefQueryResults:
117 """Return a results object for which `has_dimension_records` is
118 `True`.
119 """
120 if self.has_dimension_records:
121 return self
122 return self._copy(tree=self._tree, include_dimension_records=True)
124 def count(self, *, exact: bool = True, discard: bool = False) -> int:
125 # Docstring inherited.
126 return self._driver.count(self._tree, self._spec, exact=exact, discard=discard)
128 def _copy(self, tree: QueryTree, **kwargs: Any) -> DatasetRefQueryResults:
129 # Docstring inherited.
130 return DatasetRefQueryResults(self._driver, tree, self._spec.model_copy(update=kwargs))
132 def _get_datasets(self) -> frozenset[str]:
133 # Docstring inherited.
134 return frozenset({self.dataset_type.name})