Coverage for python/lsst/daf/butler/queries/_dimension_record_query_results.py: 46%

42 statements  

« 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/>. 

27 

28from __future__ import annotations 

29 

30__all__ = ("DimensionRecordQueryResults",) 

31 

32from collections.abc import Iterator 

33from typing import Any, final 

34 

35from ..dimensions import DimensionElement, DimensionRecord, DimensionRecordSet, DimensionRecordTable 

36from ._base import QueryResultsBase 

37from .driver import QueryDriver 

38from .result_specs import DimensionRecordResultSpec 

39from .tree import QueryTree 

40 

41 

42@final 

43class DimensionRecordQueryResults(QueryResultsBase): 

44 """A query for `DimensionRecord` results. 

45 

46 Parameters 

47 ---------- 

48 driver : `QueryDriver` 

49 Implementation object that knows how to actually execute queries. 

50 tree : `QueryTree` 

51 Description of the query as a tree of joins and column expressions. 

52 The instance returned directly by the `Butler._query` entry point 

53 should be constructed via `make_unit_query_tree`. 

54 spec : `DimensionRecordResultSpec` 

55 Specification of the query result rows, including output columns, 

56 ordering, and slicing. 

57 

58 Notes 

59 ----- 

60 This class should never be constructed directly by users; use 

61 `Query.dimension_records` instead. 

62 """ 

63 

64 def __init__(self, driver: QueryDriver, tree: QueryTree, spec: DimensionRecordResultSpec): 

65 spec.validate_tree(tree) 

66 super().__init__(driver, tree) 

67 self._spec = spec 

68 

69 def __iter__(self) -> Iterator[DimensionRecord]: 

70 page = self._driver.execute(self._spec, self._tree) 

71 yield from page.rows 

72 while page.next_key is not None: 

73 page = self._driver.fetch_next_page(self._spec, page.next_key) 

74 yield from page.rows 

75 

76 def iter_table_pages(self) -> Iterator[DimensionRecordTable]: 

77 """Return an iterator over individual pages of results as table-backed 

78 collections. 

79 

80 Yields 

81 ------ 

82 table : `DimensionRecordTable` 

83 A table-backed collection of dimension records. 

84 """ 

85 page = self._driver.execute(self._spec, self._tree) 

86 yield page.as_table() 

87 while page.next_key is not None: 

88 page = self._driver.fetch_next_page(self._spec, page.next_key) 

89 yield page.as_table() 

90 

91 def iter_set_pages(self) -> Iterator[DimensionRecordSet]: 

92 """Return an iterator over individual pages of results as set-backed 

93 collections. 

94 

95 Yields 

96 ------ 

97 table : `DimensionRecordSet` 

98 A set-backed collection of dimension records. 

99 """ 

100 page = self._driver.execute(self._spec, self._tree) 

101 yield page.as_set() 

102 while page.next_key is not None: 

103 page = self._driver.fetch_next_page(self._spec, page.next_key) 

104 yield page.as_set() 

105 

106 @property 

107 def element(self) -> DimensionElement: 

108 # Docstring inherited. 

109 return self._spec.element 

110 

111 def count(self, *, exact: bool = True, discard: bool = False) -> int: 

112 # Docstring inherited. 

113 return self._driver.count(self._tree, self._spec, exact=exact, discard=discard) 

114 

115 def _copy(self, tree: QueryTree, **kwargs: Any) -> DimensionRecordQueryResults: 

116 # Docstring inherited. 

117 return DimensionRecordQueryResults(self._driver, tree, self._spec.model_copy(update=kwargs)) 

118 

119 def _get_datasets(self) -> frozenset[str]: 

120 # Docstring inherited. 

121 return frozenset()