Coverage for python/lsst/daf/butler/core/_column_type_info.py: 37%

61 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-25 15:14 +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/>. 

21 

22from __future__ import annotations 

23 

24__all__ = ("ColumnTypeInfo", "LogicalColumn") 

25 

26import dataclasses 

27import datetime 

28from collections.abc import Iterable 

29from typing import cast 

30 

31import astropy.time 

32import sqlalchemy 

33from lsst.daf.relation import ColumnTag, sql 

34 

35from . import ddl 

36from ._column_tags import DatasetColumnTag, DimensionKeyColumnTag, DimensionRecordColumnTag 

37from .dimensions import Dimension, DimensionUniverse 

38from .timespan import TimespanDatabaseRepresentation 

39 

40LogicalColumn = sqlalchemy.sql.ColumnElement | TimespanDatabaseRepresentation 

41"""A type alias for the types used to represent columns in SQL relations. 

42 

43This is the butler specialization of the `lsst.daf.relation.sql.LogicalColumn` 

44concept. 

45""" 

46 

47 

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 """ 

53 

54 timespan_cls: type[TimespanDatabaseRepresentation] 

55 """An abstraction around the column type or types used for timespans by 

56 this database engine. 

57 """ 

58 

59 universe: DimensionUniverse 

60 """Object that manages the definitions of all dimension and dimension 

61 elements. 

62 """ 

63 

64 dataset_id_spec: ddl.FieldSpec 

65 """Field specification for the dataset primary key column. 

66 """ 

67 

68 run_key_spec: ddl.FieldSpec 

69 """Field specification for the `~CollectionType.RUN` primary key column. 

70 """ 

71 

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 """ 

76 

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}") 

86 

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. 

93 

94 This is used primarily to create temporary tables for query results. 

95 

96 Parameters 

97 ---------- 

98 columns : `~collections.abc.Iterable` [ `ColumnTag` ] 

99 Iterable of column identifiers. 

100 unique_keys : `~collections.abc.Iterable` \ 

101 [ `~collections.abc.Iterable` [ `ColumnTag` ] ] 

102 Unique constraints to add the table, as a nested iterable of 

103 (first) constraint and (second) the columns within that constraint. 

104 

105 Returns 

106 ------- 

107 spec : `ddl.TableSpec` 

108 Specification for a table. 

109 """ 

110 result = ddl.TableSpec(fields=()) 

111 columns = list(columns) 

112 if not columns: 

113 result.fields.add( 

114 ddl.FieldSpec( 

115 sql.Engine.EMPTY_COLUMNS_NAME, 

116 dtype=sql.Engine.EMPTY_COLUMNS_TYPE, 

117 nullable=True, 

118 default=True, 

119 ) 

120 ) 

121 for tag in columns: 

122 match tag: 

123 case DimensionKeyColumnTag(dimension=dimension_name): 

124 result.fields.add( 

125 dataclasses.replace( 

126 cast(Dimension, self.universe[dimension_name]).primaryKey, 

127 name=tag.qualified_name, 

128 primaryKey=False, 

129 nullable=False, 

130 ) 

131 ) 

132 case DimensionRecordColumnTag(column="region"): 

133 result.fields.add(ddl.FieldSpec.for_region(tag.qualified_name)) 

134 case DimensionRecordColumnTag(column="timespan") | DatasetColumnTag(column="timespan"): 

135 result.fields.update( 

136 self.timespan_cls.makeFieldSpecs(nullable=True, name=tag.qualified_name) 

137 ) 

138 case DimensionRecordColumnTag(element=element_name, column=column): 

139 element = self.universe[element_name] 

140 result.fields.add( 

141 dataclasses.replace( 

142 element.RecordClass.fields.facts[column], 

143 name=tag.qualified_name, 

144 nullable=True, 

145 primaryKey=False, 

146 ) 

147 ) 

148 case DatasetColumnTag(column="dataset_id"): 

149 result.fields.add( 

150 dataclasses.replace( 

151 self.dataset_id_spec, name=tag.qualified_name, primaryKey=False, nullable=False 

152 ) 

153 ) 

154 case DatasetColumnTag(column="run"): 

155 result.fields.add( 

156 dataclasses.replace( 

157 self.run_key_spec, name=tag.qualified_name, primaryKey=False, nullable=False 

158 ) 

159 ) 

160 case DatasetColumnTag(column="ingest_date"): 

161 result.fields.add( 

162 ddl.FieldSpec(tag.qualified_name, dtype=self.ingest_date_dtype, nullable=False) 

163 ) 

164 case _: 

165 raise TypeError(f"Unexpected column tag {tag}.") 

166 for unique_key in unique_keys: 

167 result.unique.add(tuple(tag.qualified_name for tag in unique_key)) 

168 return result