Coverage for python/lsst/daf/butler/queries/_dimension_record_query_results.py: 49%
45 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__ = ("DimensionRecordQueryResults",)
32from collections.abc import Iterator
33from typing import Any, final
35from ..dimensions import (
36 DimensionElement,
37 DimensionGroup,
38 DimensionRecord,
39 DimensionRecordSet,
40 DimensionRecordTable,
41)
42from ._base import QueryResultsBase
43from .driver import QueryDriver
44from .result_specs import DimensionRecordResultSpec
45from .tree import QueryTree
48@final
49class DimensionRecordQueryResults(QueryResultsBase):
50 """A query for `DimensionRecord` results.
52 Parameters
53 ----------
54 driver : `QueryDriver`
55 Implementation object that knows how to actually execute queries.
56 tree : `QueryTree`
57 Description of the query as a tree of joins and column expressions.
58 The instance returned directly by the `Butler._query` entry point
59 should be constructed via `make_unit_query_tree`.
60 spec : `DimensionRecordResultSpec`
61 Specification of the query result rows, including output columns,
62 ordering, and slicing.
64 Notes
65 -----
66 This class should never be constructed directly by users; use
67 `Query.dimension_records` instead.
68 """
70 def __init__(self, driver: QueryDriver, tree: QueryTree, spec: DimensionRecordResultSpec):
71 spec.validate_tree(tree)
72 super().__init__(driver, tree)
73 self._spec = spec
75 def __iter__(self) -> Iterator[DimensionRecord]:
76 page = self._driver.execute(self._spec, self._tree)
77 yield from page.rows
78 while page.next_key is not None:
79 page = self._driver.fetch_next_page(self._spec, page.next_key)
80 yield from page.rows
82 def iter_table_pages(self) -> Iterator[DimensionRecordTable]:
83 """Return an iterator over individual pages of results as table-backed
84 collections.
86 Yields
87 ------
88 table : `DimensionRecordTable`
89 A table-backed collection of dimension records.
90 """
91 page = self._driver.execute(self._spec, self._tree)
92 yield page.as_table()
93 while page.next_key is not None:
94 page = self._driver.fetch_next_page(self._spec, page.next_key)
95 yield page.as_table()
97 def iter_set_pages(self) -> Iterator[DimensionRecordSet]:
98 """Return an iterator over individual pages of results as set-backed
99 collections.
101 Yields
102 ------
103 table : `DimensionRecordSet`
104 A set-backed collection of dimension records.
105 """
106 page = self._driver.execute(self._spec, self._tree)
107 yield page.as_set()
108 while page.next_key is not None:
109 page = self._driver.fetch_next_page(self._spec, page.next_key)
110 yield page.as_set()
112 @property
113 def element(self) -> DimensionElement:
114 # Docstring inherited.
115 return self._spec.element
117 @property
118 def dimensions(self) -> DimensionGroup:
119 # Docstring inherited
120 return self._spec.dimensions
122 def count(self, *, exact: bool = True, discard: bool = False) -> int:
123 # Docstring inherited.
124 return self._driver.count(self._tree, self._spec, exact=exact, discard=discard)
126 def _copy(self, tree: QueryTree, **kwargs: Any) -> DimensionRecordQueryResults:
127 # Docstring inherited.
128 return DimensionRecordQueryResults(self._driver, tree, self._spec.model_copy(update=kwargs))
130 def _get_datasets(self) -> frozenset[str]:
131 # Docstring inherited.
132 return frozenset()