Debian Codegraph @

tests/sqlarraytest.py

raw
1 """
2 Tests for dealing with SQL arrays in ADQL.
3
4 (and perhaps elsewhere).
5 """
6
7 #c Copyright 2008-2025, the GAVO project <[email protected]>
8 #c
9 #c This program is free software, covered by the GNU GPL. See the
10 #c COPYING file in the source distribution.
11
12
13 import math
14
15 from gavo.helpers import testhelpers
16
17 from gavo import base
18 from gavo import utils
19
20 import adqltest
21 import tresc
22
23
24 class _ArrayTestbed(tresc.RDDataResource):
25 """A table that contains arrays.
26 """
27 rdName = "data/ufuncex.rd"
28 dataId = "import_arr"
29
30 _arrayTestbed = _ArrayTestbed()
31
32
33 class AdditionTest(testhelpers.VerboseTest):
34 resources = [
35 ("arrsampleable", _arrayTestbed),
36 ("querier", adqltest.adqlQuerier)]
37
38 def testBasic(self):
39 res = self.querier.queryADQL(
40 "SELECT flux,"
41 " flux+flux as fluxtwice,"
42 " plc+plc as plctwice"
43 " FROM test.arrsample")
44 self.assertEqual(
45 len(res.rows[0]["fluxtwice"]),
46 len(res.rows[0]["flux"]))
47 self.assertEqual(len(res.rows[0]["plctwice"]), 3)
48 self.assertAlmostEqual(
49 res.rows[0]["fluxtwice"][0]/2, res.rows[0]["flux"][0])
50 self.assertAlmostEqual(
51 res.rows[0]["plctwice"][2], -3.2491233)
52
53 def testUnequalLength(self):
54 res = self.querier.queryADQL(
55 "SELECT flux+CAST(plc AS real[]) as s FROM test.arrsample")
56 self.assertEqual(len(res.rows[0]["s"]), 6)
57 self.assertTrue(math.isnan(res.rows[0]["s"][-1]))
58 self.assertTrue(math.isnan(res.rows[0]["s"][-2]))
59 self.assertTrue(math.isnan(res.rows[0]["s"][-3]))
60 self.assertAlmostEqual(res.rows[0]["s"][-4], -0.86078703)
61 self.assertEqual(res.tableDef.getColumnByName("s").type, "real[]")
62
63 def testAddedMetadata(self):
64 res = self.querier.queryADQL(
65 "SELECT flux, flux+flux as ff, cast(flux as double precision[])+plc as fp"
66 " FROM test.arrsample")
67
68 col = res.tableDef.getColumnByName("flux")
69 self.assertEqual(col.ucd, "phot.mag")
70 self.assertEqual(col.unit, "Jy")
71 self.assertEqual(col.type, "real[]")
72 self.assertEqual(col.description, "Some flux values in an array")
73
74 col = res.tableDef.getColumnByName("ff")
75 self.assertEqual(col.ucd, "phot.mag")
76 self.assertEqual(col.unit, "Jy")
77 self.assertEqual(col.type, "real[]")
78 self.assertEqual(col.description, "This field has traces of: Some flux values in an array; Some flux values in an array -- *TAINTED*: the value was operated on in a way that unit and ucd may be severely wrong")
79
80 col = res.tableDef.getColumnByName("fp")
81 self.assertEqual(col.ucd, "")
82 self.assertEqual(col.unit, "")
83 self.assertEqual(col.type, "double precision[]")
84 self.assertEqual(col.description, "This field has traces of: Some flux values in an array; Possibly a cartesian position -- *TAINTED*: the value was operated on in a way that unit and ucd may be severely wrong")
85
86
87 class SubtractionTest(testhelpers.VerboseTest):
88 resources = [
89 ("arrsampleable", _arrayTestbed),
90 ("querier", adqltest.adqlQuerier)]
91
92 def testBasic(self):
93 res = self.querier.queryADQL(
94 "SELECT flux,"
95 " flux-flux as allnull,"
96 " flux-plc as indiff"
97 " FROM test.arrsample")
98 self.assertEqual(
99 len(res.rows[0]["allnull"]),
100 len(res.rows[0]["indiff"]))
101 self.assertEqual(res.rows[0]["allnull"][5], 0)
102 self.assertAlmostEqual(res.rows[0]["indiff"][2], 2.388336286310653)
103 self.assertTrue(math.isnan(res.rows[0]["indiff"][5]))
104
105
106 class MultiplicationTest(testhelpers.VerboseTest):
107 resources = [
108 ("arrsampleable", _arrayTestbed),
109 ("querier", adqltest.adqlQuerier)]
110
111 def testBasic(self):
112 res = self.querier.queryADQL(
113 "SELECT flux[6]*flux[6] as fstest,"
114 " flux*flux as fluxsquare,"
115 " flux[3]*plc[3] as mixtest,"
116 " flux*plc as mixed"
117 " FROM test.arrsample")
118 r = res.rows[0]
119
120 self.assertEqual(
121 len(r["fluxsquare"]),
122 len(r["mixed"]))
123 self.assertEqual(r["fluxsquare"][5], r["fstest"])
124 self.assertAlmostEqual(r["mixed"][2], r["mixtest"])
125 self.assertTrue(math.isnan(r["mixed"][5]))
126
127
128 class DivisionTest(testhelpers.VerboseTest):
129 resources = [
130 ("arrsampleable", _arrayTestbed),
131 ("querier", adqltest.adqlQuerier)]
132
133 def testBasic(self):
134 res = self.querier.queryADQL(
135 "SELECT flux[6]/flux[6] as fstest,"
136 " flux/flux as allones,"
137 " flux[3]/plc[3] as mixtest,"
138 " flux/plc as mixed"
139 " FROM test.arrsample")
140 r = res.rows[0]
141
142 self.assertEqual(
143 len(r["allones"]),
144 len(r["mixed"]))
145 self.assertEqual(r["allones"][5], r["fstest"])
146 self.assertAlmostEqual(r["mixed"][2], r["mixtest"])
147 self.assertTrue(math.isnan(r["mixed"][5]))
148
149 self.assertEqual(res.tableDef.getColumnByName("allones").type,
150 "real[]")
151 self.assertEqual(res.tableDef.getColumnByName("mixed").type,
152 "double precision[]")
153
154
155 class ScalarProdTest(testhelpers.VerboseTest):
156 resources = [
157 ("arrsampleable", _arrayTestbed),
158 ("querier", adqltest.adqlQuerier)]
159
160 def testBasic(self):
161 res = self.querier.queryADQL(
162 "SELECT"
163 " arr_dot(flux, flux) as fluxprod,"
164 " arr_dot(flux, plc) as mixedprod"
165 " FROM test.arrsample")
166 self.assertAlmostEqual(res.rows[0]["fluxprod"], 1.832107663154602)
167 self.assertTrue(math.isnan(res.rows[0]["mixedprod"]))
168
169 col = res.tableDef.getColumnByName("fluxprod")
170 self.assertEqual(col.type, "double precision")
171 self.assertEqual(col.ucd, "")
172 self.assertEqual(col.unit, "")
173
174 col = res.tableDef.getColumnByName("mixedprod")
175 self.assertEqual(col.type, "double precision")
176 self.assertEqual(col.ucd, "")
177 self.assertEqual(col.unit, "")
178
179
180 class ScalarMulTest(testhelpers.VerboseTest):
181 resources = [
182 ("arrsampleable", _arrayTestbed),
183 ("querier", adqltest.adqlQuerier)]
184
185 def testWithLiteral(self):
186 res = self.querier.queryADQL(
187 "SELECT"
188 " 2*flux-flux*2 as realzeros,"
189 " 5*plc as dub"
190 " FROM test.arrsample")
191
192 col = res.tableDef.getColumnByName("realzeros")
193 self.assertEqual(col.type, "real[]")
194 self.assertEqual(col.ucd, "phot.mag")
195 self.assertEqual(col.unit, "Jy")
196
197 self.assertEqual(res.rows[0]["realzeros"], [0., 0., 0., 0., 0., 0.])
198
199 col = res.tableDef.getColumnByName("dub")
200 self.assertEqual(col.type, "double precision[]")
201 self.assertEqual(col.ucd, "pos.cartesian")
202 self.assertEqual(col.unit, "m")
203 self.assertEqual(col.description, "Possibly a cartesian position -- *TAINTED*: the value was operated on in a way that unit and ucd may be severely wrong")
204
205 self.assertAlmostEqual(res.rows[0]["dub"][2], -8.122808264515303)
206
207 def testWithExpression(self):
208 res = self.querier.queryADQL(
209 "SELECT"
210 " plc[1]*flux as fromleft,"
211 " flux*plc[1] as fromright"
212 " FROM test.arrsample")
213
214 col = res.tableDef.getColumnByName("fromleft")
215 self.assertEqual(col.type, "double precision[]")
216 self.assertEqual(col.ucd, "")
217 self.assertEqual(col.unit, "m.Jy")
218
219 self.assertEqual([a-b for a, b in zip(
220 res.rows[0]["fromleft"],
221 res.rows[0]["fromright"])],
222 [0., 0., 0., 0., 0., 0.])
223
224
225 class ScalarDivTest(testhelpers.VerboseTest):
226 resources = [
227 ("arrsampleable", _arrayTestbed),
228 ("querier", adqltest.adqlQuerier)]
229
230 def testWithLiteral(self):
231 res = self.querier.queryADQL(
232 "SELECT flux/2 as half FROM test.arrsample")
233
234 col = res.tableDef.getColumnByName("half")
235 self.assertEqual(col.type, "real[]")
236 self.assertEqual(col.ucd, "phot.mag")
237 self.assertEqual(col.unit, "Jy")
238 self.assertEqual(res.rows[0]["half"][0], 0.06718212)
239
240 def testWithExpression(self):
241 res = self.querier.queryADQL(
242 "SELECT flux/plc[1] as k FROM test.arrsample")
243
244 col = res.tableDef.getColumnByName("k")
245 self.assertEqual(col.type, "double precision[]")
246 self.assertEqual(col.ucd, "")
247 self.assertEqual(col.unit, "Jy/(m)")
248 self.assertEqual(res.rows[0]["k"][-1], 0.7412795)
249
250
251 class DerivedAggFunctionTest(testhelpers.VerboseTest):
252 resources = [
253 ("arrsampleable", _arrayTestbed),
254 ("querier", adqltest.adqlQuerier)]
255
256 def testMessage(self):
257 self.assertRaisesWithMsg(base.ValidationError,
258 # TODO: this is a stupid message, but it's hard to do this
259 # much better as long as arr_min could actually be a column
260 # reference.
261 utils.EqualingRE(r"Field query: Could not parse your query: Expected FROM, found '\('"),
262 self.querier.queryADQL,
263 ("SELECT arr_min(plc, plc) FROM test.arrsample",))
264
265 def testMin(self):
266 res = self.querier.queryADQL(
267 "SELECT"
268 " arr_min(plc) as minplc,"
269 " arr_min(flux) as minflux"
270 " FROM test.arrsample")
271
272 self.assertAlmostEqual(res.rows[0]["minplc"], -1.6245616529030604)
273 self.assertAlmostEqual(res.rows[1]["minplc"], -0.7674541696434232)
274 self.assertAlmostEqual(res.rows[0]["minflux"], 0.13436425)
275
276 col = res.tableDef.getColumnByName("minplc")
277 self.assertEqual(col.type, "double precision")
278 self.assertEqual(col.ucd, "stat.min;pos.cartesian")
279 self.assertEqual(col.unit, "m")
280
281 col = res.tableDef.getColumnByName("minflux")
282 self.assertEqual(col.type, "double precision")
283 self.assertEqual(col.ucd, "stat.min;phot.mag")
284 self.assertEqual(col.unit, "Jy")
285
286 def testOnNonArray(self):
287 self.assertRaisesWithMsg(
288 base.ValidationError,
289 "Field query: Scalar array function called on non-array?",
290 self.querier.queryADQL,
291 ("SELECT arr_min(plc[0]) as minplc FROM test.arrsample",))
292
293 def testMax(self):
294 res = self.querier.queryADQL(
295 "SELECT"
296 " arr_max(plc) as maxplc,"
297 " arr_max(flux) as maxflux"
298 " FROM test.arrsample")
299
300 self.assertAlmostEqual(res.rows[1]["maxplc"], 0.6789216057608836)
301 self.assertAlmostEqual(res.rows[0]["maxflux"], 0.84743375)
302
303 col = res.tableDef.getColumnByName("maxplc")
304 self.assertEqual(col.ucd, "stat.max;pos.cartesian")
305 col = res.tableDef.getColumnByName("maxflux")
306 self.assertEqual(col.ucd, "stat.max;phot.mag")
307
308 def testSum(self):
309 res = self.querier.queryADQL(
310 "SELECT"
311 " ARR_sum(plc) as splc,"
312 " arr_SUM(flux) as sflux"
313 " FROM test.arrsample")
314
315 self.assertAlmostEqual(res.rows[1]["splc"], 0.3352440988313101)
316 self.assertAlmostEqual(res.rows[0]["sflux"], 2.9455676)
317
318 col = res.tableDef.getColumnByName("splc")
319 self.assertEqual(col.ucd, "pos.cartesian;arith.sum")
320 col = res.tableDef.getColumnByName("sflux")
321 self.assertEqual(col.ucd, "phot.mag;arith.sum")
322
323 def testAvg(self):
324 res = self.querier.queryADQL(
325 "SELECT"
326 " arr_avg(plc) as mplc,"
327 " arr_avg(flux) as mflux"
328 " FROM test.arrsample")
329
330 self.assertAlmostEqual(res.rows[1]["mplc"], 0.11174803294377005)
331 self.assertAlmostEqual(res.rows[0]["mflux"], 0.49092796)
332
333 col = res.tableDef.getColumnByName("mplc")
334 self.assertEqual(col.ucd, "pos.cartesian;stat.mean")
335 col = res.tableDef.getColumnByName("mflux")
336 self.assertEqual(col.ucd, "phot.mag;stat.mean")
337
338 def testStddev(self):
339 res = self.querier.queryADQL(
340 "SELECT"
341 " power(arr_stddev(plc), 2) as varce, plc,"
342 " arr_stddev(flux) as fluxerr, flux"
343 " FROM test.arrsample")
344
345 self.assertAlmostEqual(res.rows[0]["fluxerr"], 0.27786547)
346 self.assertAlmostEqual(res.rows[1]["varce"], 0.596022120)
347
348 col = res.tableDef.getColumnByName("varce")
349 self.assertEqual(col.ucd, "")
350 self.assertEqual(col.description, "Possibly a cartesian position -- *TAINTED*: the value was operated on in a way that unit and ucd may be severely wrong")
351 col = res.tableDef.getColumnByName("fluxerr")
352 self.assertEqual(col.ucd, "stat.stdev;phot.mag")
353
354 def testCount(self):
355 res = self.querier.queryADQL(
356 "SELECT"
357 " arr_count(plc) as plclen,"
358 " arr_count(flux) as fluxlen"
359 " FROM test.arrsample")
360
361 self.assertEqual(res.rows[0]["plclen"], 3)
362 self.assertEqual(res.rows[0]["fluxlen"], 6)
363
364 col = res.tableDef.getColumnByName("plclen")
365 self.assertEqual(col.ucd, "meta.number;pos.cartesian")
366 self.assertEqual(col.unit, "")
367 self.assertEqual(col.type, "integer")
368
369
370 class ArrayAggregationTest(testhelpers.VerboseTest):
371 resources = [
372 ("arrsampleable", _arrayTestbed),
373 ("querier", adqltest.adqlQuerier)]
374
375 def testBasicSum(self):
376 res = self.querier.queryADQL(
377 "SELECT"
378 " sum(flux) allflux,"
379 " sum(plc) as allplc"
380 " FROM test.arrsample")
381
382 self.assertEqual(len(res.rows[0]["allflux"]), 6)
383 self.assertEqual(len(res.rows[0]["allplc"]), 3)
384
385 self.assertAlmostEqual(res.rows[0]["allflux"][0], 1.0903986)
386 self.assertAlmostEqual(res.rows[0]["allplc"][-1], -1.2007849901)
387
388 self.assertEqual(res.tableDef.getColumnByName("allflux").ucd, "phot.mag")
389
390 def testBasicMin(self):
391 res = self.querier.queryADQL(
392 "SELECT"
393 " min(flux) minflux,"
394 " min(plc) as minplc"
395 " FROM test.arrsample")
396
397 self.assertEqual(len(res.rows[0]["minflux"]), 6)
398 self.assertEqual(len(res.rows[0]["minplc"]), 3)
399
400 self.assertAlmostEqual(res.rows[0]["minflux"][-1], 0.44949105)
401 self.assertAlmostEqual(res.rows[0]["minplc"][0], 0.606371890)
402
403 self.assertEqual(
404 res.tableDef.getColumnByName("minflux").ucd,
405 "stat.min;phot.mag")
406
407 def testMixedMin(self):
408 res = self.querier.queryADQL(
409 "SELECT min(arr) AS mixedmin FROM ("
410 " SELECT flux AS arr FROM test.arrsample"
411 " UNION"
412 " SELECT plc AS arr FROM test.arrsample) as q")
413
414 self.assertEqual(len(res.rows[0]["mixedmin"]), 6)
415
416 self.assertAlmostEqual(res.rows[0]["mixedmin"][-1], 0.44949105)
417 self.assertAlmostEqual(res.rows[0]["mixedmin"][0], 0.1343642473)
418
419 # lazyness alert: the UCD should probably be "", but I won't dig
420 # now why it's Not
421 self.assertEqual(
422 res.tableDef.getColumnByName("mixedmin").ucd,
423 None)
424 self.assertEqual(
425 res.tableDef.getColumnByName("mixedmin").unit,
426 "")
427
428 def testBasicMax(self):
429 res = self.querier.queryADQL(
430 "SELECT"
431 " max(flux) maxflux,"
432 " max(plc) as maxplc"
433 " FROM test.arrsample")
434
435 self.assertEqual(len(res.rows[0]["maxflux"]), 6)
436 self.assertEqual(len(res.rows[0]["maxplc"]), 3)
437
438 self.assertAlmostEqual(res.rows[0]["maxflux"][-1], 0.73596996)
439 self.assertAlmostEqual(res.rows[0]["maxplc"][0], 0.678921605)
440
441 self.assertEqual(
442 res.tableDef.getColumnByName("maxflux").ucd,
443 "stat.max;phot.mag")
444
445 def testBasicAvg(self):
446 res = self.querier.queryADQL(
447 "SELECT"
448 " avg(flux) avgflux,"
449 " avg(plc) as avgplc"
450 " FROM test.arrsample")
451
452 self.assertEqual(len(res.rows[0]["avgflux"]), 6)
453 self.assertEqual(len(res.rows[0]["avgplc"]), 3)
454
455 self.assertAlmostEqual(res.rows[0]["avgflux"][-1],
456 list(self.querier.connection.query(
457 "select avg(flux[6]) from test.arrsample"))[0][0])
458 self.assertAlmostEqual(res.rows[0]["avgplc"][0],
459 list(self.querier.connection.query(
460 "select avg(plc[1]) from test.arrsample"))[0][0])
461
462 self.assertEqual(
463 res.tableDef.getColumnByName("avgflux").ucd,
464 "phot.mag;stat.mean")
465
466 def testBasicStddev(self):
467 res = self.querier.queryADQL(
468 "SELECT"
469 " stddev(flux[4]) as fluxtest,"
470 " stddev(flux) fluxerr,"
471 " stddev(plc[1]) as plctest,"
472 " stddev(plc) as plcerr"
473 " FROM test.arrsample")
474
475 r = res.rows[0]
476 self.assertAlmostEqual(r["fluxerr"][3], r["fluxtest"])
477 self.assertAlmostEqual(r["plcerr"][0], r["plctest"])
478
479 self.assertEqual(
480 res.tableDef.getColumnByName("fluxtest").ucd,
481 "stat.stdev;phot.mag")
482
483
484 class ArrMapTest(testhelpers.VerboseTest):
485 resources = [
486 ("arrsampleable", _arrayTestbed),
487 ("querier", adqltest.adqlQuerier)]
488
489 def testBasic(self):
490 res = self.querier.queryADQL(
491 "SELECT arr_map(sin(x)+1, flux) as x, sin(flux[3])+1 as xt,"
492 " arr_map(round(x*100), plc) as p, round(plc[1]*100) as pt"
493 " FROM test.arrsample")
494
495 self.assertAlmostEqual(res.rows[0]["x"][2], res.rows[0]["xt"])
496 self.assertAlmostEqual(res.rows[0]["p"][0], res.rows[0]["pt"])
497
498 self.assertEqual(
499 res.tableDef.getColumnByName("x").ucd,
500 "")
501 self.assertEqual(
502 res.tableDef.getColumnByName("x").type,
503 "double precision[]")
504
505
506 if __name__=="__main__":
507 testhelpers.main(AdditionTest)