Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21from __future__ import annotations 

22 

23__all__ = ["QueryDimensionRecordStorage"] 

24 

25from typing import Iterable, Optional 

26 

27import sqlalchemy 

28 

29from ...core import ( 

30 Config, 

31 DatabaseDimension, 

32 DatabaseDimensionElement, 

33 DataCoordinateIterable, 

34 DimensionElement, 

35 DimensionRecord, 

36 GovernorDimension, 

37 NamedKeyDict, 

38 NamedKeyMapping, 

39 TimespanDatabaseRepresentation, 

40) 

41from ..interfaces import ( 

42 Database, 

43 DatabaseDimensionRecordStorage, 

44 GovernorDimensionRecordStorage, 

45 StaticTablesContext, 

46) 

47from ..queries import QueryBuilder 

48 

49 

50class QueryDimensionRecordStorage(DatabaseDimensionRecordStorage): 

51 """A read-only record storage implementation backed by SELECT query. 

52 

53 At present, the only query this class supports is a SELECT DISTNCT over the 

54 table for some other dimension that has this dimension as an implied 

55 dependency. For example, we can use this class to provide access to the 

56 set of ``band`` names referenced by any ``physical_filter``. 

57 

58 Parameters 

59 ---------- 

60 db : `Database` 

61 Interface to the database engine and namespace that will hold these 

62 dimension records. 

63 element : `DatabaseDimensionElement` 

64 The element whose records this storage will manage. 

65 """ 

66 def __init__(self, db: Database, element: DatabaseDimensionElement, viewOf: str): 

67 assert isinstance(element, DatabaseDimension), \ 

68 "An element cannot be a dependency unless it is a dimension." 

69 self._db = db 

70 self._element = element 

71 self._target = element.universe[viewOf] 

72 self._targetSpec = self._target.RecordClass.fields.makeTableSpec( 

73 tsRepr=self._db.getTimespanRepresentation(), 

74 ) 

75 self._viewOf = viewOf 

76 self._query = None # Constructed on first use. 

77 if element not in self._target.graph.dimensions: 77 ↛ 78line 77 didn't jump to line 78, because the condition on line 77 was never true

78 raise NotImplementedError("Query-backed dimension must be a dependency of its target.") 

79 if element.metadata: 79 ↛ 80line 79 didn't jump to line 80, because the condition on line 79 was never true

80 raise NotImplementedError("Cannot use query to back dimension with metadata.") 

81 if element.implied: 81 ↛ 82line 81 didn't jump to line 82, because the condition on line 81 was never true

82 raise NotImplementedError("Cannot use query to back dimension with implied dependencies.") 

83 if element.alternateKeys: 83 ↛ 84line 83 didn't jump to line 84, because the condition on line 83 was never true

84 raise NotImplementedError("Cannot use query to back dimension with alternate unique keys.") 

85 if element.spatial is not None: 85 ↛ 86line 85 didn't jump to line 86, because the condition on line 85 was never true

86 raise NotImplementedError("Cannot use query to back spatial dimension.") 

87 if element.temporal is not None: 87 ↛ 88line 87 didn't jump to line 88, because the condition on line 87 was never true

88 raise NotImplementedError("Cannot use query to back temporal dimension.") 

89 

90 @classmethod 

91 def initialize( 

92 cls, 

93 db: Database, 

94 element: DatabaseDimensionElement, *, 

95 context: Optional[StaticTablesContext] = None, 

96 config: Config, 

97 governors: NamedKeyMapping[GovernorDimension, GovernorDimensionRecordStorage], 

98 ) -> DatabaseDimensionRecordStorage: 

99 # Docstring inherited from DatabaseDimensionRecordStorage. 

100 viewOf = config["view_of"] 

101 return cls(db, element, viewOf) 

102 

103 @property 

104 def element(self) -> DatabaseDimension: 

105 # Docstring inherited from DimensionRecordStorage.element. 

106 return self._element 

107 

108 def clearCaches(self) -> None: 

109 # Docstring inherited from DimensionRecordStorage.clearCaches. 

110 pass 

111 

112 def _ensureQuery(self) -> None: 

113 if self._query is None: 113 ↛ exitline 113 didn't return from function '_ensureQuery', because the condition on line 113 was never false

114 targetTable = self._db.getExistingTable(self._target.name, self._targetSpec) 

115 assert targetTable is not None 

116 columns = [] 

117 # The only columns for this dimension are ones for its required 

118 # dependencies and its own primary key (guaranteed by the checks in 

119 # the ctor). 

120 for dimension in self.element.required: 

121 if dimension == self.element: 121 ↛ 124line 121 didn't jump to line 124, because the condition on line 121 was never false

122 columns.append(targetTable.columns[dimension.name].label(dimension.primaryKey.name)) 

123 else: 

124 columns.append(targetTable.columns[dimension.name].label(dimension.name)) 

125 # This query doesn't do a SELECT DISTINCT, because that's confusing 

126 # and potentially wasteful if we apply a restrictive WHERE clause, 

127 # as SelectableDimensionRecordStorage.fetch will do. 

128 # Instead, we add DISTINCT in join() only. 

129 self._query = sqlalchemy.sql.select( 

130 columns, distinct=True 

131 ).select_from( 

132 targetTable 

133 ).alias( 

134 self.element.name 

135 ) 

136 

137 def join( 

138 self, 

139 builder: QueryBuilder, *, 

140 regions: Optional[NamedKeyDict[DimensionElement, sqlalchemy.sql.ColumnElement]] = None, 

141 timespans: Optional[NamedKeyDict[DimensionElement, TimespanDatabaseRepresentation]] = None, 

142 ) -> None: 

143 # Docstring inherited from DimensionRecordStorage. 

144 assert regions is None, "Should be guaranteed by constructor checks." 

145 assert timespans is None, "Should be guaranteed by constructor checks." 

146 if self._target in builder.summary.mustHaveKeysJoined: 146 ↛ 151line 146 didn't jump to line 151, because the condition on line 146 was never true

147 # Do nothing; the target dimension is already being included, so 

148 # joining against a subquery referencing it would just produce a 

149 # more complicated query that's guaranteed to return the same 

150 # results. 

151 return 

152 self._ensureQuery() 

153 joinOn = builder.startJoin(self._query, self.element.required, 

154 self.element.RecordClass.fields.required.names) 

155 builder.finishJoin(self._query, joinOn) 

156 return self._query 

157 

158 def insert(self, *records: DimensionRecord) -> None: 

159 # Docstring inherited from DimensionRecordStorage.insert. 

160 raise TypeError(f"Cannot insert {self.element.name} records.") 

161 

162 def sync(self, record: DimensionRecord) -> bool: 

163 # Docstring inherited from DimensionRecordStorage.sync. 

164 raise TypeError(f"Cannot sync {self.element.name} records.") 

165 

166 def fetch(self, dataIds: DataCoordinateIterable) -> Iterable[DimensionRecord]: 

167 # Docstring inherited from DimensionRecordStorage.fetch. 

168 RecordClass = self.element.RecordClass 

169 for dataId in dataIds: 

170 # Given the restrictions imposed at construction, we know there's 

171 # nothing to actually fetch: everything we need is in the data ID. 

172 yield RecordClass(**dataId.byName()) 

173 

174 def digestTables(self) -> Iterable[sqlalchemy.schema.Table]: 

175 # Docstring inherited from DimensionRecordStorage.digestTables. 

176 return []