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

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

# 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/>. 

 

__all__ = ("ResultColumnsManager",) 

 

import logging 

import itertools 

from collections import defaultdict 

 

from sqlalchemy.sql import select 

 

from lsst.sphgeom import Region, DISJOINT 

from .. import DatasetRef, DataId 

 

 

_LOG = logging.getLogger(__name__) 

 

 

class ResultColumnsManager: 

"""Helper class that manages the columns in the selectDimensions query. 

 

Parameters 

---------- 

registry : `SqlRegistry` 

Registry instance the query is being run against. 

""" 

 

def __init__(self, registry): 

self.registry = registry 

self._columns = [] 

self._indicesForDimensionLinks = {} 

self._indicesForPerDatasetTypeDimensionLinks = defaultdict(dict) 

self._indicesForRegions = {} 

self._indicesForDatasetIds = {} 

self._needSkyPixRegion = False 

 

def logState(self): 

"""Log the state of ``self`` at debug level. 

""" 

_LOG.debug("ResultColumnsManager columns: %s", self._columns) 

if self._indicesForDimensionLinks: 

_LOG.debug("ResultColumnsManager dimensions: %s", self._indicesForDimensionLinks) 

if self._indicesForRegions: 

_LOG.debug("ResultColumnsManager regions: %s", self._indicesForRegions) 

if self._indicesForDatasetIds: 

_LOG.debug("ResultColumnsManager datasets: %s", self._indicesForDatasetIds) 

 

def addDimensionLink(self, selectable, link, datasetType=None): 

"""Add a column containing a dimension link value. 

 

Parameters 

---------- 

selectable : `sqlalchemy.FromClause` 

SQLAlchemy object representing a part of the query from which 

a column can be selected. 

link : `str` 

String name of a dimension link column. If this link is 

already in the selected columns, this method does nothing. 

datasetType : `DatasetType`, optional 

If not `None`, this link corresponds to a "per-DatasetType" 

dimension in a query for this `DatasetType`. 

""" 

column = selectable.columns[link] 

if datasetType is None: 

if link in self._indicesForDimensionLinks: 

return 

self._indicesForDimensionLinks[link] = len(self._columns) 

else: 

if link in self._indicesForPerDatasetTypeDimensionLinks[datasetType]: 

return 

column = column.label(f"{datasetType.name}_{link}") 

self._indicesForPerDatasetTypeDimensionLinks[datasetType][link] = len(self._columns) 

self._columns.append(column) 

 

def addRegion(self, selectable, holder): 

"""Add a column containing a region. 

 

Parameters 

---------- 

selectable : `sqlalchemy.FromClause` 

SQLAlchemy object representing a part of the query from which 

a column can be selected. 

holder : `DimensionElement` 

`Dimension` or `DimensionElement` the region is associated with. 

If this holder is already in the selected columns, this method does 

nothing. 

""" 

if holder in self._indicesForRegions: 

return 

if holder.name == "skypix": 

# We obtain these regions directly from the registry, but require 

# that the "skypix" dimension link be present to do so. We can't 

# add that ourselves (we don't know what selectable to obtain it 

# from), and we can't assume it's *already* been added. 

# Instead we'll remember that we do need it, and raise when trying 

# to create the full query (in `selectFrom`) if it's not present. 

self._needskypixRegion = True 

return 

else: 

column = selectable.columns.region 

self._indicesForRegions[holder] = len(self._columns) 

self._columns.append(column) 

 

def addDatasetId(self, selectable, datasetType): 

"""Add a column containing a dataset ID. 

 

Parameters 

---------- 

selectable : `sqlalchemy.FromClause` 

SQLAlchemy object representing a part of the query from which 

a column can be selected. 

datasetType : `DatasetType` 

`DatasetType` this ID column is for. If this `DatasetType` is 

already in the selected columns, this method does nothing. 

""" 

if datasetType in self._indicesForDatasetIds: 

return 

column = selectable.columns["dataset_id"] 

self._indicesForDatasetIds[datasetType] = len(self._columns) 

self._columns.append(column) 

 

def selectFrom(self, fromClause): 

"""Return a select query that extracts the managed columns from the 

given from clause. 

 

Parameters 

---------- 

fromClause : `sqlalchemy.FromClause` 

SQLAlchemy object representing the full FROM clause for the query. 

 

Returns 

------- 

select : `sqlalchemy.Select` 

SQLAlchemy object representing the SELECT and FROM clauses of the 

query. 

""" 

if self._needSkyPixRegion and "skypix" not in self._indicesForDimensionLinks: 

raise RuntimeError("skypix region added to query without associated link.") 

return select(self._columns).select_from(fromClause) 

 

def manageRow(self, row): 

"""Return an object that manages raw query result row. 

 

Parameters 

---------- 

row : `sqlalchemy.sql.RowProxy`, optional 

Direct SQLAlchemy row result object. 

 

Returns 

------- 

managed : `ManagedRow` or `None` 

Proxy for the row that understands the columns managed by ``self``. 

Will be `None` if and only if ``row`` is `None`. 

""" 

if row is None: 

return None 

else: 

return self.ManagedRow(self, row) 

 

class ManagedRow: 

"""An intermediate query result row class that understands the columns 

managed by a `ResultColumnsManager`. 

 

Parameters 

---------- 

manager : `ResultColumnsManager` 

Object that manages the columns in the query's SELECT clause. 

row : `sqlalchemy.sql.RowProxy` 

Direct SQLAlchemy row result object. 

""" 

 

__slots__ = ("registry", "_dimensionLinks", "_perDatasetTypeDimensionLinks", "_regions", 

"_datasetIds") 

 

def __init__(self, manager, row): 

self.registry = manager.registry 

self._dimensionLinks = { 

link: row[index] for link, index in manager._indicesForDimensionLinks.items() 

} 

self._perDatasetTypeDimensionLinks = { 

datasetType: {link: row[index] for link, index in indices.items()} 

for datasetType, indices in manager._indicesForPerDatasetTypeDimensionLinks.items() 

} 

self._regions = { 

holder: Region.decode(row[index]) for holder, index in manager._indicesForRegions.items() 

} 

self._datasetIds = { 

datasetType: row[index] for datasetType, index in manager._indicesForDatasetIds.items() 

} 

skypix = self._dimensionLinks.get("skypix", None) 

if skypix is not None: 

self._regions[self.registry.dimensions["skypix"]] = self.registry.pixelization.pixel(skypix) 

 

def areRegionsDisjoint(self): 

"""Test whether the regions in this result row are disjoint. 

 

Returns 

------- 

disjoint : `bool` 

`True` if any region in the result row is disjoint with any 

other region in the result row. 

""" 

for reg1, reg2 in itertools.combinations(self._regions.values(), 2): 

if reg1.relate(reg2) == DISJOINT: 

return True 

return False 

 

def makeDataId(self, *, datasetType=None, expandDataId=True, **kwds): 

"""Construct a `DataId` from the result row. 

 

Parameters 

---------- 

datasetType : `DatasetType`, optional 

If provided, the `DatasetType` this data ID will describe. 

This will enable per-DatasetType dimension link values to be 

used and set the `~DataId.dimensions` to be those of the 

`DatasetType`. If not provided, the returned data ID will 

cover all common dimensions in the graph. 

expandDataId : `bool` 

If `True` (default), query the `Registry` to further expand 

the data ID to include additional information. 

kwds 

Additional keyword arguments passed to the `DataId` 

constructor. 

 

Returns 

------- 

dataId : `DataId` 

A new `DataId` instance. 

""" 

if datasetType is None: 

result = DataId(self._dimensionLinks, universe=self.registry.dimensions, **kwds) 

else: 

d = self._dimensionLinks.copy() 

d.update(self._perDatasetTypeDimensionLinks.get(datasetType, {})) 

result = DataId(d, dimensions=datasetType.dimensions, **kwds) 

if result.region is None: 

holder = result.dimensions().getRegionHolder() 

if holder is not None: 

result.region = self._regions.get(holder) 

if expandDataId: 

self.registry.expandDataId(result) 

return result 

 

def makeDatasetRef(self, datasetType, *, expandDataId=True, **kwds): 

"""Construct a `DatasetRef` from the result row. 

 

Parameters 

---------- 

datasetType : `DatasetType` 

The `DatasetType` the returned `DatasetRef` will identify. 

expandDataId : `bool` 

If `True` (default), query the `Registry` to further expand 

the data ID to include additional information. 

kwds 

Additional keyword arguments passed to the `DataId` 

constructor. 

 

Returns 

------- 

ref : `DatasetRef` 

New `DatasetRef` instance. `DatasetRef.id` will be `None` if 

the ``dataset_id`` for this dataset was either not included in 

the result columns or NULL in the result row. If 

`DatasetRef.id` is not `None`, any component dataset references 

will also be present. 

""" 

dataId = self.makeDataId(datasetType=datasetType, expandDataId=expandDataId, **kwds) 

datasetId = self._datasetIds.get(datasetType) 

if datasetId is None: 

return DatasetRef(datasetType, dataId) 

else: 

# Need to ask registry to expand to include components if they 

# exist. 

return self.registry.getDataset(id=datasetId, dataId=dataId, datasetType=datasetType)