Coverage for python/lsst/daf/butler/direct_query.py: 56%

23 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-13 10:57 +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__ = ["DirectQuery"] 

31 

32from collections.abc import Iterable, Mapping 

33from typing import TYPE_CHECKING, Any 

34 

35from ._query import Query 

36from .direct_query_results import ( 

37 DirectDataCoordinateQueryResults, 

38 DirectDatasetQueryResults, 

39 DirectDimensionRecordQueryResults, 

40 DirectSingleTypeDatasetQueryResults, 

41) 

42from .registry import queries as registry_queries 

43from .registry.sql_registry import SqlRegistry 

44 

45if TYPE_CHECKING: 

46 from ._query_results import DataCoordinateQueryResults, DatasetQueryResults, DimensionRecordQueryResults 

47 from .dimensions import DataId, DimensionGroup 

48 from .registry._registry import CollectionArgType 

49 

50 

51class DirectQuery(Query): 

52 """Implementation of `Query` interface used by `DirectButler`. 

53 

54 Parameters 

55 ---------- 

56 registry : `SqlRegistry` 

57 The object that manages dataset metadata and relationships. 

58 """ 

59 

60 _registry: SqlRegistry 

61 

62 def __init__(self, registry: SqlRegistry): 

63 self._registry = registry 

64 

65 def data_ids( 

66 self, 

67 dimensions: DimensionGroup | Iterable[str] | str, 

68 *, 

69 data_id: DataId | None = None, 

70 where: str = "", 

71 bind: Mapping[str, Any] | None = None, 

72 **kwargs: Any, 

73 ) -> DataCoordinateQueryResults: 

74 # Docstring inherited. 

75 registry_query_result = self._registry.queryDataIds( 

76 dimensions, 

77 dataId=data_id, 

78 where=where, 

79 bind=bind, 

80 **kwargs, 

81 ) 

82 return DirectDataCoordinateQueryResults(registry_query_result) 

83 

84 def datasets( 

85 self, 

86 dataset_type: Any, 

87 collections: CollectionArgType | None = None, 

88 *, 

89 find_first: bool = True, 

90 data_id: DataId | None = None, 

91 where: str = "", 

92 bind: Mapping[str, Any] | None = None, 

93 **kwargs: Any, 

94 ) -> DatasetQueryResults: 

95 # Docstring inherited. 

96 registry_query_result = self._registry.queryDatasets( 

97 dataset_type, 

98 collections=collections, 

99 dataId=data_id, 

100 where=where, 

101 findFirst=find_first, 

102 bind=bind, 

103 **kwargs, 

104 ) 

105 if isinstance(registry_query_result, registry_queries.ParentDatasetQueryResults): 

106 return DirectSingleTypeDatasetQueryResults(registry_query_result) 

107 else: 

108 return DirectDatasetQueryResults(registry_query_result) 

109 

110 def dimension_records( 

111 self, 

112 element: str, 

113 *, 

114 data_id: DataId | None = None, 

115 where: str = "", 

116 bind: Mapping[str, Any] | None = None, 

117 **kwargs: Any, 

118 ) -> DimensionRecordQueryResults: 

119 # Docstring inherited. 

120 registry_query_result = self._registry.queryDimensionRecords( 

121 element, 

122 dataId=data_id, 

123 where=where, 

124 bind=bind, 

125 **kwargs, 

126 ) 

127 return DirectDimensionRecordQueryResults(registry_query_result)