Coverage for python/lsst/daf/butler/registry/dimensions/query.py : 79%

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
23__all__ = ["QueryDimensionRecordStorage"]
25from typing import Iterable, Optional
27import sqlalchemy
29from ...core import (
30 DatabaseTimespanRepresentation,
31 DataCoordinateIterable,
32 Dimension,
33 DimensionElement,
34 DimensionRecord,
35 NamedKeyDict,
36)
37from ..interfaces import Database, DimensionRecordStorage, StaticTablesContext
38from ..queries import QueryBuilder
41class QueryDimensionRecordStorage(DimensionRecordStorage):
42 """A read-only record storage implementation backed by SELECT query.
44 At present, the only query this class supports is a SELECT DISTNCT over the
45 table for some other dimension that has this dimension as an implied
46 dependency. For example, we can use this class to provide access to the
47 set of ``band`` names referenced by any ``physical_filter``.
49 Parameters
50 ----------
51 db : `Database`
52 Interface to the database engine and namespace that will hold these
53 dimension records.
54 element : `DimensionElement`
55 The element whose records this storage will manage.
56 """
57 def __init__(self, db: Database, element: DimensionElement):
58 assert element.viewOf is not None
59 self._db = db
60 self._element = element
61 self._target = element.universe[element.viewOf]
62 self._targetSpec = self._target.RecordClass.fields.makeTableSpec(
63 tsRepr=self._db.getTimespanRepresentation(),
64 )
65 self._query = None # Constructed on first use.
66 if element not in self._target.graph.dimensions: 66 ↛ 67line 66 didn't jump to line 67, because the condition on line 66 was never true
67 raise NotImplementedError("Query-backed dimension must be a dependency of its target.")
68 assert isinstance(element, Dimension), "An element cannot be a dependency unless it is a dimension."
69 if element.metadata: 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 metadata.")
71 if element.implied: 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 implied dependencies.")
73 if element.alternateKeys: 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 dimension with alternate unique keys.")
75 if element.spatial 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 spatial dimension.")
77 if element.temporal is not None: 77 ↛ 78line 77 didn't jump to line 78, because the condition on line 77 was never true
78 raise NotImplementedError("Cannot use query to back temporal dimension.")
80 @classmethod
81 def initialize(cls, db: Database, element: DimensionElement, *,
82 context: Optional[StaticTablesContext] = None) -> DimensionRecordStorage:
83 # Docstring inherited from DimensionRecordStorage.
84 return cls(db, element)
86 @property
87 def element(self) -> DimensionElement:
88 # Docstring inherited from DimensionRecordStorage.element.
89 return self._element
91 def clearCaches(self) -> None:
92 # Docstring inherited from DimensionRecordStorage.clearCaches.
93 pass
95 def _ensureQuery(self) -> None:
96 if self._query is None: 96 ↛ exitline 96 didn't return from function '_ensureQuery', because the condition on line 96 was never false
97 targetTable = self._db.getExistingTable(self._target.name, self._targetSpec)
98 assert targetTable is not None
99 columns = []
100 # The only columns for this dimension are ones for its required
101 # dependencies and its own primary key (guaranteed by the checks in
102 # the ctor).
103 for dimension in self.element.required:
104 if dimension == self.element: 104 ↛ 107line 104 didn't jump to line 107, because the condition on line 104 was never false
105 columns.append(targetTable.columns[dimension.name].label(dimension.primaryKey.name))
106 else:
107 columns.append(targetTable.columns[dimension.name].label(dimension.name))
108 # This query doesn't do a SELECT DISTINCT, because that's confusing
109 # and potentially wasteful if we apply a restrictive WHERE clause,
110 # as SelectableDimensionRecordStorage.fetch will do.
111 # Instead, we add DISTINCT in join() only.
112 self._query = sqlalchemy.sql.select(
113 columns, distinct=True
114 ).select_from(
115 targetTable
116 ).alias(
117 self.element.name
118 )
120 def join(
121 self,
122 builder: QueryBuilder, *,
123 regions: Optional[NamedKeyDict[DimensionElement, sqlalchemy.sql.ColumnElement]] = None,
124 timespans: Optional[NamedKeyDict[DimensionElement, DatabaseTimespanRepresentation]] = None,
125 ) -> None:
126 # Docstring inherited from DimensionRecordStorage.
127 assert regions is None, "Should be guaranteed by constructor checks."
128 assert timespans is None, "Should be guaranteed by constructor checks."
129 if self._target in builder.summary.mustHaveKeysJoined: 129 ↛ 134line 129 didn't jump to line 134, because the condition on line 129 was never true
130 # Do nothing; the target dimension is already being included, so
131 # joining against a subquery referencing it would just produce a
132 # more complicated query that's guaranteed to return the same
133 # results.
134 return
135 self._ensureQuery()
136 joinOn = builder.startJoin(self._query, self.element.required,
137 self.element.RecordClass.fields.required.names)
138 builder.finishJoin(self._query, joinOn)
139 return self._query
141 def insert(self, *records: DimensionRecord) -> None:
142 # Docstring inherited from DimensionRecordStorage.insert.
143 raise TypeError(f"Cannot insert {self.element.name} records.")
145 def sync(self, record: DimensionRecord) -> bool:
146 # Docstring inherited from DimensionRecordStorage.sync.
147 raise TypeError(f"Cannot sync {self.element.name} records.")
149 def fetch(self, dataIds: DataCoordinateIterable) -> Iterable[DimensionRecord]:
150 # Docstring inherited from DimensionRecordStorage.fetch.
151 RecordClass = self.element.RecordClass
152 for dataId in dataIds:
153 # Given the restrictions imposed at construction, we know there's
154 # nothing to actually fetch: everything we need is in the data ID.
155 yield RecordClass(**dataId.byName())
157 def digestTables(self) -> Iterable[sqlalchemy.schema.Table]:
158 # Docstring inherited from DimensionRecordStorage.digestTables.
159 return []