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 Optional 

26 

27import sqlalchemy 

28 

29from ...core import ( 

30 DataCoordinate, 

31 Dimension, 

32 DimensionElement, 

33 DimensionRecord, 

34 makeDimensionElementTableSpec, 

35 Timespan, 

36) 

37from ...core.utils import NamedKeyDict 

38from ..interfaces import Database, DimensionRecordStorage, StaticTablesContext 

39from ..queries import QueryBuilder 

40 

41 

42class QueryDimensionRecordStorage(DimensionRecordStorage): 

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

44 

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

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

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

48 set of ``abstract_filter`` names referenced by any ``physical_filter``. 

49 

50 Parameters 

51 ---------- 

52 db : `Database` 

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

54 dimension records. 

55 element : `DimensionElement` 

56 The element whose records this storage will manage. 

57 """ 

58 def __init__(self, db: Database, element: DimensionElement): 

59 self._db = db 

60 self._element = element 

61 self._target = element.universe[element.viewOf] 

62 self._targetSpec = makeDimensionElementTableSpec(self._target) 

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

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

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

66 assert isinstance(element, Dimension), "An element cannot be a dependency unless it is a dimension." 

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

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

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

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

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

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

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

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

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

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

77 

78 @classmethod 

79 def initialize(cls, db: Database, element: DimensionElement, *, 

80 context: Optional[StaticTablesContext] = None) -> DimensionRecordStorage: 

81 # Docstring inherited from DimensionRecordStorage. 

82 return cls(db, element) 

83 

84 @property 

85 def element(self) -> DimensionElement: 

86 # Docstring inherited from DimensionRecordStorage.element. 

87 return self._element 

88 

89 def clearCaches(self): 

90 # Docstring inherited from DimensionRecordStorage.clearCaches. 

91 pass 

92 

93 def _ensureQuery(self): 

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

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

96 columns = [] 

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

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

99 # the ctor). 

100 for dimension in self.element.required: 

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

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

103 else: 

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

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

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

107 # as SelectableDimensionRecordStorage.fetch will do. 

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

109 self._query = sqlalchemy.sql.select( 

110 columns, distinct=True 

111 ).select_from( 

112 targetTable 

113 ).alias( 

114 self.element.name 

115 ) 

116 

117 def join( 

118 self, 

119 builder: QueryBuilder, *, 

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

121 timespans: Optional[NamedKeyDict[DimensionElement, Timespan[sqlalchemy.sql.ColumnElement]]] = None, 

122 ): 

123 # Docstring inherited from DimensionRecordStorage. 

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

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

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

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

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

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

130 # results. 

131 return 

132 self._ensureQuery() 

133 joinOn = builder.startJoin(self._query, list(self.element.required), 

134 self.element.RecordClass.__slots__) 

135 builder.finishJoin(self._query, joinOn) 

136 return self._query 

137 

138 def insert(self, *records: DimensionRecord): 

139 # Docstring inherited from DimensionRecordStorage.insert. 

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

141 

142 def sync(self, record: DimensionRecord): 

143 # Docstring inherited from DimensionRecordStorage.sync. 

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

145 

146 def fetch(self, dataId: DataCoordinate) -> Optional[DimensionRecord]: 

147 # Docstring inherited from DimensionRecordStorage.fetch. 

148 RecordClass = self.element.RecordClass 

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

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

151 return RecordClass.fromDict(dataId)