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

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