Coverage for python/lsst/daf/butler/registry/dimensions/query.py: 95%
40 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-21 09:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-21 09:54 +0000
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 collections.abc import Mapping
26from typing import TYPE_CHECKING, Any
28import sqlalchemy
29from lsst.daf.relation import Relation
31from ...core import (
32 DatabaseDimension,
33 DatabaseDimensionElement,
34 DataCoordinate,
35 DimensionKeyColumnTag,
36 DimensionRecord,
37 GovernorDimension,
38 NamedKeyMapping,
39)
40from ..interfaces import (
41 Database,
42 DatabaseDimensionRecordStorage,
43 GovernorDimensionRecordStorage,
44 StaticTablesContext,
45)
47if TYPE_CHECKING:
48 from .. import queries
51class QueryDimensionRecordStorage(DatabaseDimensionRecordStorage):
52 """A read-only record storage implementation backed by SELECT query.
54 At present, the only query this class supports is a SELECT DISTINCT over
55 the table for some other dimension that has this dimension as an implied
56 dependency. For example, we can use this class to provide access to the
57 set of ``band`` names referenced by any ``physical_filter``.
59 Parameters
60 ----------
61 db : `Database`
62 Interface to the database engine and namespace that will hold these
63 dimension records.
64 element : `DatabaseDimensionElement`
65 The element whose records this storage will manage.
66 view_target : `DatabaseDimensionRecordStorage`
67 Storage object for the element this target's storage is a view of.
68 """
70 def __init__(
71 self, db: Database, element: DatabaseDimensionElement, view_target: DatabaseDimensionRecordStorage
72 ):
73 assert isinstance(
74 element, DatabaseDimension
75 ), "An element cannot be a dependency unless it is a dimension."
76 self._db = db
77 self._element = element
78 self._target = view_target
79 if element not in self._target.element.graph.dimensions:
80 raise NotImplementedError("Query-backed dimension must be a dependency of its target.")
81 if element.metadata:
82 raise NotImplementedError("Cannot use query to back dimension with metadata.")
83 if element.implied:
84 raise NotImplementedError("Cannot use query to back dimension with implied dependencies.")
85 if element.alternateKeys:
86 raise NotImplementedError("Cannot use query to back dimension with alternate unique keys.")
87 if element.spatial is not None:
88 raise NotImplementedError("Cannot use query to back spatial dimension.")
89 if element.temporal is not None:
90 raise NotImplementedError("Cannot use query to back temporal dimension.")
92 @classmethod
93 def initialize(
94 cls,
95 db: Database,
96 element: DatabaseDimensionElement,
97 *,
98 context: StaticTablesContext | None = None,
99 config: Mapping[str, Any],
100 governors: NamedKeyMapping[GovernorDimension, GovernorDimensionRecordStorage],
101 view_target: DatabaseDimensionRecordStorage | None = None,
102 ) -> DatabaseDimensionRecordStorage:
103 # Docstring inherited from DatabaseDimensionRecordStorage.
104 assert view_target is not None, f"Storage for '{element}' is a view."
105 return cls(db, element, view_target)
107 @property
108 def element(self) -> DatabaseDimension:
109 # Docstring inherited from DimensionRecordStorage.element.
110 return self._element
112 def clearCaches(self) -> None:
113 # Docstring inherited from DimensionRecordStorage.clearCaches.
114 pass
116 def make_relation(self, context: queries.SqlQueryContext) -> Relation:
117 # Docstring inherited from DimensionRecordStorage.
118 columns = DimensionKeyColumnTag.generate([self.element.name])
119 return (
120 self._target.make_relation(context)
121 .with_only_columns(
122 frozenset(columns),
123 preferred_engine=context.preferred_engine,
124 require_preferred_engine=True,
125 )
126 .without_duplicates()
127 )
129 def insert(self, *records: DimensionRecord, replace: bool = False, skip_existing: bool = False) -> None:
130 # Docstring inherited from DimensionRecordStorage.insert.
131 raise TypeError(
132 f"Cannot insert {self.element.name} records, define as part of {self._target.element} instead."
133 )
135 def sync(self, record: DimensionRecord, update: bool = False) -> bool:
136 # Docstring inherited from DimensionRecordStorage.sync.
137 raise TypeError(
138 f"Cannot sync {self.element.name} records, define as part of {self._target.element} instead."
139 )
141 def fetch_one(self, data_id: DataCoordinate, context: queries.SqlQueryContext) -> DimensionRecord | None:
142 # Docstring inherited from DimensionRecordStorage.
143 # Given the restrictions imposed at construction, we know there's
144 # nothing to actually fetch: everything we need is in the data ID.
145 return self.element.RecordClass(**data_id.byName())
147 def digestTables(self) -> list[sqlalchemy.schema.Table]:
148 # Docstring inherited from DimensionRecordStorage.digestTables.
149 return []