Coverage for python/lsst/daf/butler/core/_column_type_info.py: 36%
61 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-18 09:13 +0000
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-18 09:13 +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/>.
22from __future__ import annotations
24__all__ = ("ColumnTypeInfo", "LogicalColumn")
26import dataclasses
27import datetime
28from collections.abc import Iterable
29from typing import Union, cast
31import astropy.time
32import sqlalchemy
33from lsst.daf.relation import ColumnTag, sql
35from . import ddl
36from ._column_tags import DatasetColumnTag, DimensionKeyColumnTag, DimensionRecordColumnTag
37from .dimensions import Dimension, DimensionUniverse
38from .timespan import TimespanDatabaseRepresentation
40LogicalColumn = Union[sqlalchemy.sql.ColumnElement, TimespanDatabaseRepresentation]
41"""A type alias for the types used to represent columns in SQL relations.
43This is the butler specialization of the `lsst.daf.relation.sql.LogicalColumn`
44concept.
45"""
48@dataclasses.dataclass(frozen=True, eq=False)
49class ColumnTypeInfo:
50 """A struct that aggregates information about column types that can differ
51 across data repositories due to `Registry` and dimension configuration.
52 """
54 timespan_cls: type[TimespanDatabaseRepresentation]
55 """An abstraction around the column type or types used for timespans by
56 this database engine.
57 """
59 universe: DimensionUniverse
60 """Object that manages the definitions of all dimension and dimension
61 elements.
62 """
64 dataset_id_spec: ddl.FieldSpec
65 """Field specification for the dataset primary key column.
66 """
68 run_key_spec: ddl.FieldSpec
69 """Field specification for the `~CollectionType.RUN` primary key column.
70 """
72 ingest_date_dtype: type[ddl.AstropyTimeNsecTai] | type[sqlalchemy.TIMESTAMP]
73 """Type of the ``ingest_date`` column, can be either
74 `~lsst.daf.butler.core.ddl.AstropyTimeNsecTai` or `sqlalchemy.TIMESTAMP`.
75 """
77 @property
78 def ingest_date_pytype(self) -> type:
79 """Python type corresponding to ``ingest_date`` column type."""
80 if self.ingest_date_dtype is ddl.AstropyTimeNsecTai:
81 return astropy.time.Time
82 elif self.ingest_date_dtype is sqlalchemy.TIMESTAMP:
83 return datetime.datetime
84 else:
85 raise TypeError(f"Unexpected type of ingest_date_dtype: {self.ingest_date_dtype}")
87 def make_relation_table_spec(
88 self,
89 columns: Iterable[ColumnTag],
90 unique_keys: Iterable[Iterable[ColumnTag]] = (),
91 ) -> ddl.TableSpec:
92 """Create a specification for a table with the given relation columns.
94 This is used primarily to create temporary tables for query results.
96 Parameters
97 ----------
98 columns : `Iterable` [ `ColumnTag` ]
99 Iterable of column identifiers.
100 unique_keys : `Iterable` [ `Iterable` [ `ColumnTag` ] ]
101 Unique constraints to add the table, as a nested iterable of
102 (first) constraint and (second) the columns within that constraint.
104 Returns
105 -------
106 spec : `ddl.TableSpec`
107 Specification for a table.
108 """
109 result = ddl.TableSpec(fields=())
110 columns = list(columns)
111 if not columns:
112 result.fields.add(
113 ddl.FieldSpec(
114 sql.Engine.EMPTY_COLUMNS_NAME,
115 dtype=sql.Engine.EMPTY_COLUMNS_TYPE,
116 nullable=True,
117 default=True,
118 )
119 )
120 for tag in columns:
121 match tag:
122 case DimensionKeyColumnTag(dimension=dimension_name):
123 result.fields.add(
124 dataclasses.replace(
125 cast(Dimension, self.universe[dimension_name]).primaryKey,
126 name=tag.qualified_name,
127 primaryKey=False,
128 nullable=False,
129 )
130 )
131 case DimensionRecordColumnTag(column="region"):
132 result.fields.add(ddl.FieldSpec.for_region(tag.qualified_name))
133 case DimensionRecordColumnTag(column="timespan") | DatasetColumnTag(column="timespan"):
134 result.fields.update(
135 self.timespan_cls.makeFieldSpecs(nullable=True, name=tag.qualified_name)
136 )
137 case DimensionRecordColumnTag(element=element_name, column=column):
138 element = self.universe[element_name]
139 result.fields.add(
140 dataclasses.replace(
141 element.RecordClass.fields.facts[column],
142 name=tag.qualified_name,
143 nullable=True,
144 primaryKey=False,
145 )
146 )
147 case DatasetColumnTag(column="dataset_id"):
148 result.fields.add(
149 dataclasses.replace(
150 self.dataset_id_spec, name=tag.qualified_name, primaryKey=False, nullable=False
151 )
152 )
153 case DatasetColumnTag(column="run"):
154 result.fields.add(
155 dataclasses.replace(
156 self.run_key_spec, name=tag.qualified_name, primaryKey=False, nullable=False
157 )
158 )
159 case DatasetColumnTag(column="ingest_date"):
160 result.fields.add(
161 ddl.FieldSpec(tag.qualified_name, dtype=self.ingest_date_dtype, nullable=False)
162 )
163 case _:
164 raise TypeError(f"Unexpected column tag {tag}.")
165 for unique_key in unique_keys:
166 result.unique.add(tuple(tag.qualified_name for tag in unique_key))
167 return result