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 Any, Iterable, Mapping, Optional 

26 

27import sqlalchemy 

28 

29from ...core import ( 

30 DatabaseDimension, 

31 DatabaseDimensionElement, 

32 DataCoordinateIterable, 

33 DimensionElement, 

34 DimensionRecord, 

35 GovernorDimension, 

36 NamedKeyDict, 

37 NamedKeyMapping, 

38 TimespanDatabaseRepresentation, 

39) 

40from ..interfaces import ( 

41 Database, 

42 DatabaseDimensionRecordStorage, 

43 GovernorDimensionRecordStorage, 

44 StaticTablesContext, 

45) 

46from ..queries import QueryBuilder 

47 

48 

49class QueryDimensionRecordStorage(DatabaseDimensionRecordStorage): 

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

51 

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

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

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

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

56 

57 Parameters 

58 ---------- 

59 db : `Database` 

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

61 dimension records. 

62 element : `DatabaseDimensionElement` 

63 The element whose records this storage will manage. 

64 """ 

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

66 assert isinstance(element, DatabaseDimension), \ 

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

68 self._db = db 

69 self._element = element 

70 self._target = element.universe[viewOf] 

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

72 tsRepr=self._db.getTimespanRepresentation(), 

73 ) 

74 self._viewOf = viewOf 

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

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

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

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

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

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

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

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

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

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

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

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

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

88 

89 @classmethod 

90 def initialize( 

91 cls, 

92 db: Database, 

93 element: DatabaseDimensionElement, *, 

94 context: Optional[StaticTablesContext] = None, 

95 config: Mapping[str, Any], 

96 governors: NamedKeyMapping[GovernorDimension, GovernorDimensionRecordStorage], 

97 ) -> DatabaseDimensionRecordStorage: 

98 # Docstring inherited from DatabaseDimensionRecordStorage. 

99 viewOf = config["view_of"] 

100 return cls(db, element, viewOf) 

101 

102 @property 

103 def element(self) -> DatabaseDimension: 

104 # Docstring inherited from DimensionRecordStorage.element. 

105 return self._element 

106 

107 def clearCaches(self) -> None: 

108 # Docstring inherited from DimensionRecordStorage.clearCaches. 

109 pass 

110 

111 def _ensureQuery(self) -> None: 

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

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

114 assert targetTable is not None 

115 columns = [] 

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

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

118 # the ctor). 

119 for dimension in self.element.required: 

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

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

122 else: 

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

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

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

126 # as SelectableDimensionRecordStorage.fetch will do. 

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

128 self._query = sqlalchemy.sql.select( 

129 columns, distinct=True 

130 ).select_from( 

131 targetTable 

132 ).alias( 

133 self.element.name 

134 ) 

135 

136 def join( 

137 self, 

138 builder: QueryBuilder, *, 

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

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

141 ) -> None: 

142 # Docstring inherited from DimensionRecordStorage. 

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

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

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

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

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

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

149 # results. 

150 return 

151 self._ensureQuery() 

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

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

154 builder.finishJoin(self._query, joinOn) 

155 return self._query 

156 

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

158 # Docstring inherited from DimensionRecordStorage.insert. 

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

160 

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

162 # Docstring inherited from DimensionRecordStorage.sync. 

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

164 

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

166 # Docstring inherited from DimensionRecordStorage.fetch. 

167 RecordClass = self.element.RecordClass 

168 for dataId in dataIds: 

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

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

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

172 

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

174 # Docstring inherited from DimensionRecordStorage.digestTables. 

175 return []