Hide keyboard shortcuts

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

from __future__ import annotations 

 

__all__ = ( 

"makeForeignKeySpec", 

"addDimensionForeignKey", 

"makeElementTableSpec", 

"REGION_FIELD_SPEC", 

) 

 

import copy 

 

from typing import TYPE_CHECKING 

 

from .. import ddl 

from ..utils import NamedValueSet 

from ..timespan import TIMESPAN_FIELD_SPECS 

 

38 ↛ 39line 38 didn't jump to line 39, because the condition on line 38 was never trueif TYPE_CHECKING: # Imports needed only for type annotations; may be circular. 

from .elements import DimensionElement, Dimension 

 

 

# Most regions are small (they're quadrilaterals), but visit ones can be quite 

# large because they have a complicated boundary. For HSC, about ~1400 bytes. 

REGION_FIELD_SPEC = ddl.FieldSpec(name="region", nbytes=2048, dtype=ddl.Base64Region) 

 

 

def makeForeignKeySpec(dimension: Dimension) -> ddl.ForeignKeySpec: 

"""Make a `ddl.ForeignKeySpec` that references the table for the given 

`Dimension` table. 

 

Most callers should use the higher-level `addDimensionForeignKey` function 

instead. 

 

Parameters 

---------- 

dimension : `Dimension` 

The dimension to be referenced. Caller guarantees that it is actually 

associated with a table. 

 

Returns 

------- 

spec : `ddl.ForeignKeySpec` 

A database-agnostic foreign key specification. 

""" 

source = [] 

target = [] 

for other in dimension.graph.required: 

if other == dimension: 

target.append(dimension.primaryKey.name) 

else: 

target.append(other.name) 

source.append(other.name) 

return ddl.ForeignKeySpec(table=dimension.name, source=tuple(source), target=tuple(target)) 

 

 

def addDimensionForeignKey(tableSpec: ddl.TableSpec, dimension: Dimension, *, 

primaryKey: bool, nullable: bool = False): 

"""Add a field and possibly a foreign key to a table specification that 

reference the table for the given `Dimension`. 

 

Parameters 

---------- 

tableSpec : `ddl.TableSpec` 

Specification the field and foreign key are to be added to. 

dimension : `Dimension` 

Dimension to be referenced. If this dimension has required 

dependencies, those must have already been added to the table. A field 

will be added that correspond to this dimension's primary key, and a 

foreign key constraint will be added only if the dimension is 

associated with a table of its own. 

primaryKey : `bool` 

If `True`, the new field will be added as part of a compound primary 

key for the table. 

nullable : `bool`, optional 

If `False` (default) the new field will be added with a NOT NULL 

constraint. 

""" 

# Add the dependency's primary key field, but use the dimension name for 

# the field name to make it unique and more meaningful in this table. 

fieldSpec = copy.copy(dimension.primaryKey) 

fieldSpec.name = dimension.name 

fieldSpec.primaryKey = primaryKey 

fieldSpec.nullable = nullable 

tableSpec.fields.add(fieldSpec) 

# Also add a foreign key constraint on the dependency table, but only if 

# there actually is one. 

if dimension.hasTable() and dimension.viewOf is None: 

tableSpec.foreignKeys.append(makeForeignKeySpec(dimension)) 

 

 

def makeElementTableSpec(element: DimensionElement) -> ddl.TableSpec: 

"""Create a complete table specification for a `DimensionElement`. 

 

This combines the foreign key fields from dependencies, unique keys 

for true `Dimension` instances, metadata fields, and region/timestamp 

fields for spatial/temporal elements. 

 

Most callers should use `DimensionElement.makeTableSpec` or 

`DimensionUniverse.makeSchemaSpec` instead, which account for elements 

that have no table or reference another table. 

 

Parameters 

---------- 

element : `DimensionElement` 

Element for which to make a table specification. 

 

Returns 

------- 

spec : `ddl.TableSpec` 

Database-agnostic specification for a table. 

""" 

tableSpec = ddl.TableSpec( 

fields=NamedValueSet(), 

unique=set(), 

foreignKeys=[] 

) 

# Add the primary key fields of required dimensions. These continue to be 

# primary keys in the table for this dimension. 

dependencies = [] 

for dimension in element.graph.required: 

if dimension != element: 

addDimensionForeignKey(tableSpec, dimension, primaryKey=True) 

dependencies.append(dimension.name) 

else: 

# A Dimension instance is in its own required dependency graph 

# (always at the end, because of topological ordering). In this 

# case we don't want to rename the field. 

tableSpec.fields.add(element.primaryKey) 

# Add fields and foreign keys for implied dimensions. These are primary 

# keys in their own table, but should not be here. As with required 

# dependencies, we rename the fields with the dimension name. 

# We use element.implied instead of element.graph.implied because we don't 

# want *recursive* implied dependencies. 

for dimension in element.implied: 

addDimensionForeignKey(tableSpec, dimension, primaryKey=False, nullable=True) 

# Add non-primary unique keys and unique constraints for them. 

for fieldSpec in getattr(element, "alternateKeys", ()): 

tableSpec.fields.add(fieldSpec) 

tableSpec.unique.add(tuple(dependencies) + (fieldSpec.name,)) 

# Add metadata fields, temporal timespans, and spatial regions. 

for fieldSpec in element.metadata: 

tableSpec.fields.add(fieldSpec) 

if element.spatial: 

tableSpec.fields.add(REGION_FIELD_SPEC) 

if element.temporal: 

for fieldSpec in TIMESPAN_FIELD_SPECS: 

tableSpec.fields.add(fieldSpec) 

return tableSpec