Coverage for python/lsst/daf/butler/queries/_dataset_query_results.py: 62%
46 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-03 02:48 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-03 02:48 -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__ = ("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 page = self._driver.execute(self._spec, self._tree)
76 yield from page.rows
77 while page.next_key is not None:
78 page = self._driver.fetch_next_page(self._spec, page.next_key)
79 yield from page.rows
81 @property
82 def dimensions(self) -> DimensionGroup:
83 # Docstring inherited
84 return self._spec.dimensions
86 @property
87 def dataset_type(self) -> DatasetType:
88 # Docstring inherited.
89 return DatasetType(self._spec.dataset_type_name, self._spec.dimensions, self._spec.storage_class_name)
91 @property
92 def data_ids(self) -> DataCoordinateQueryResults:
93 # Docstring inherited.
94 from ._data_coordinate_query_results import DataCoordinateQueryResults
96 return DataCoordinateQueryResults(
97 self._driver,
98 tree=self._tree,
99 spec=DataCoordinateResultSpec.model_construct(
100 dimensions=self.dataset_type.dimensions.as_group(),
101 include_dimension_records=self._spec.include_dimension_records,
102 ),
103 )
105 @property
106 def has_dimension_records(self) -> bool:
107 """Whether all data IDs in this iterable contain dimension records."""
108 return self._spec.include_dimension_records
110 def with_dimension_records(self) -> DatasetRefQueryResults:
111 """Return a results object for which `has_dimension_records` is
112 `True`.
113 """
114 if self.has_dimension_records:
115 return self
116 return self._copy(tree=self._tree, include_dimension_records=True)
118 def count(self, *, exact: bool = True, discard: bool = False) -> int:
119 # Docstring inherited.
120 return self._driver.count(self._tree, self._spec, exact=exact, discard=discard)
122 def _copy(self, tree: QueryTree, **kwargs: Any) -> DatasetRefQueryResults:
123 # Docstring inherited.
124 return DatasetRefQueryResults(self._driver, tree, self._spec.model_copy(update=kwargs))
126 def _get_datasets(self) -> frozenset[str]:
127 # Docstring inherited.
128 return frozenset({self.dataset_type.name})