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 DataCoordinateIterable,
31 Dimension,
32 DimensionElement,
33 DimensionRecord,
34 makeDimensionElementTableSpec,
35 NamedKeyDict,
36 Timespan,
37)
38from ..interfaces import Database, DimensionRecordStorage, StaticTablesContext
39from ..queries import QueryBuilder
42class QueryDimensionRecordStorage(DimensionRecordStorage):
43 """A read-only record storage implementation backed by SELECT query.
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``.
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 assert element.viewOf is not None
60 self._db = db
61 self._element = element
62 self._target = element.universe[element.viewOf]
63 self._targetSpec = makeDimensionElementTableSpec(self._target)
64 self._query = None # Constructed on first use.
65 if element not in self._target.graph.dimensions: 65 ↛ 66line 65 didn't jump to line 66, because the condition on line 65 was never true
66 raise NotImplementedError("Query-backed dimension must be a dependency of its target.")
67 assert isinstance(element, Dimension), "An element cannot be a dependency unless it is a dimension."
68 if element.metadata: 68 ↛ 69line 68 didn't jump to line 69, because the condition on line 68 was never true
69 raise NotImplementedError("Cannot use query to back dimension with metadata.")
70 if element.implied: 70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never true
71 raise NotImplementedError("Cannot use query to back dimension with implied dependencies.")
72 if element.alternateKeys: 72 ↛ 73line 72 didn't jump to line 73, because the condition on line 72 was never true
73 raise NotImplementedError("Cannot use query to back dimension with alternate unique keys.")
74 if element.spatial is not None: 74 ↛ 75line 74 didn't jump to line 75, because the condition on line 74 was never true
75 raise NotImplementedError("Cannot use query to back spatial dimension.")
76 if element.temporal is not None: 76 ↛ 77line 76 didn't jump to line 77, because the condition on line 76 was never true
77 raise NotImplementedError("Cannot use query to back temporal dimension.")
79 @classmethod
80 def initialize(cls, db: Database, element: DimensionElement, *,
81 context: Optional[StaticTablesContext] = None) -> DimensionRecordStorage:
82 # Docstring inherited from DimensionRecordStorage.
83 return cls(db, element)
85 @property
86 def element(self) -> DimensionElement:
87 # Docstring inherited from DimensionRecordStorage.element.
88 return self._element
90 def clearCaches(self) -> None:
91 # Docstring inherited from DimensionRecordStorage.clearCaches.
92 pass
94 def _ensureQuery(self) -> None:
95 if self._query is None: 95 ↛ exitline 95 didn't return from function '_ensureQuery', because the condition on line 95 was never false
96 targetTable = self._db.getExistingTable(self._target.name, self._targetSpec)
97 assert targetTable is not None
98 columns = []
99 # The only columns for this dimension are ones for its required
100 # dependencies and its own primary key (guaranteed by the checks in
101 # the ctor).
102 for dimension in self.element.required:
103 if dimension == self.element: 103 ↛ 106line 103 didn't jump to line 106, because the condition on line 103 was never false
104 columns.append(targetTable.columns[dimension.name].label(dimension.primaryKey.name))
105 else:
106 columns.append(targetTable.columns[dimension.name].label(dimension.name))
107 # This query doesn't do a SELECT DISTINCT, because that's confusing
108 # and potentially wasteful if we apply a restrictive WHERE clause,
109 # as SelectableDimensionRecordStorage.fetch will do.
110 # Instead, we add DISTINCT in join() only.
111 self._query = sqlalchemy.sql.select(
112 columns, distinct=True
113 ).select_from(
114 targetTable
115 ).alias(
116 self.element.name
117 )
119 def join(
120 self,
121 builder: QueryBuilder, *,
122 regions: Optional[NamedKeyDict[DimensionElement, sqlalchemy.sql.ColumnElement]] = None,
123 timespans: Optional[NamedKeyDict[DimensionElement, Timespan[sqlalchemy.sql.ColumnElement]]] = None,
124 ) -> None:
125 # Docstring inherited from DimensionRecordStorage.
126 assert regions is None, "Should be guaranteed by constructor checks."
127 assert timespans is None, "Should be guaranteed by constructor checks."
128 if self._target in builder.summary.mustHaveKeysJoined: 128 ↛ 133line 128 didn't jump to line 133, because the condition on line 128 was never true
129 # Do nothing; the target dimension is already being included, so
130 # joining against a subquery referencing it would just produce a
131 # more complicated query that's guaranteed to return the same
132 # results.
133 return
134 self._ensureQuery()
135 joinOn = builder.startJoin(self._query, list(self.element.required),
136 self.element.RecordClass.__slots__)
137 builder.finishJoin(self._query, joinOn)
138 return self._query
140 def insert(self, *records: DimensionRecord) -> None:
141 # Docstring inherited from DimensionRecordStorage.insert.
142 raise TypeError(f"Cannot insert {self.element.name} records.")
144 def sync(self, record: DimensionRecord) -> bool:
145 # Docstring inherited from DimensionRecordStorage.sync.
146 raise TypeError(f"Cannot sync {self.element.name} records.")
148 def fetch(self, dataIds: DataCoordinateIterable) -> Iterable[DimensionRecord]:
149 # Docstring inherited from DimensionRecordStorage.fetch.
150 RecordClass = self.element.RecordClass
151 for dataId in dataIds:
152 # Given the restrictions imposed at construction, we know there's
153 # nothing to actually fetch: everything we need is in the data ID.
154 yield RecordClass.fromDict(dataId.byName())
156 def digestTables(self) -> Iterable[sqlalchemy.schema.Table]:
157 # Docstring inherited from DimensionRecordStorage.digestTables.
158 return []