@@ -49,3 +49,247 @@ The meta age range for each collection object should appear in the age column of
49
49
50
50
The "Any" age query bug is being push to a later issue. For now, do a Age Range query from "13800" to "0" to get all COs with age data (query all of time, the age of the universe 😀
51
51
52
+ ## Example Queries Generated by Extended Age QB
53
+
54
+ Here is a compiled SQLAlchemy query that get generated for a simple CollectionObject Age query:
55
+
56
+ ``` sql
57
+ SELECT
58
+ collectionobject.` CollectionObjectID` ,
59
+ concat_ws(' - ' ,
60
+ ifnull(regexp_replace(CAST(agg_subq .max_start_period AS CHAR ), ' \\ .(0+)$' , ' ' ), ' ' ),
61
+ ifnull(regexp_replace(CAST(agg_subq .min_end_period AS CHAR ), ' \\ .(0+)$' , ' ' ), ' ' )) AS age
62
+ FROM
63
+ collectionobject
64
+ INNER JOIN (
65
+ SELECT
66
+ unioned .coid AS coid,
67
+ min (unioned .endperiod ) AS min_end_period,
68
+ max (unioned .startperiod ) AS max_start_period
69
+ FROM
70
+ (
71
+ SELECT
72
+ absoluteage.` CollectionObjectID` AS coid,
73
+ CAST(absoluteage.` AbsoluteAge` AS DECIMAL (10 , 6 )) - coalesce(absoluteage.` AgeUncertainty` , 0 ) AS startperiod,
74
+ CAST(absoluteage.` AbsoluteAge` AS DECIMAL (10 , 6 )) + coalesce(absoluteage.` AgeUncertainty` , 0 ) AS endperiod
75
+ FROM
76
+ absoluteage
77
+ WHERE
78
+ CAST(absoluteage.` AbsoluteAge` AS DECIMAL (10 , 6 )) - coalesce(absoluteage.` AgeUncertainty` , 0 ) <= 2000 .0
79
+ AND CAST(absoluteage.` AbsoluteAge` AS DECIMAL (10 , 6 )) + coalesce(absoluteage.` AgeUncertainty` , 0 ) >= 2 .0
80
+ UNION ALL
81
+ SELECT
82
+ r.` CollectionObjectID` AS coid,
83
+ CASE
84
+ WHEN (r.` AgeNameEndID` IS NOT NULL ) THEN greatest(CAST(a.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(a.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 ), CAST(aend.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(aend.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 ))
85
+ ELSE CAST(a.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(a.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 )
86
+ END AS startperiod,
87
+ CASE
88
+ WHEN (r.` AgeNameEndID` IS NOT NULL ) THEN least((CAST(a.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(a.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 ), (CAST(aend.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(aend.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 ))
89
+ ELSE (CAST(a.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(a.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 )
90
+ END AS endperiod
91
+ FROM
92
+ relativeage AS r
93
+ INNER JOIN geologictimeperiod AS a ON
94
+ r.` AgeNameID` = a.` GeologicTimePeriodID`
95
+ LEFT OUTER JOIN geologictimeperiod AS aend ON
96
+ r.` AgeNameEndID` = aend.` GeologicTimePeriodID`
97
+ WHERE
98
+ a.` StartPeriod` IS NOT NULL
99
+ AND a.` EndPeriod` IS NOT NULL
100
+ AND a.` StartPeriod` >= a.` EndPeriod`
101
+ AND (r.` AgeNameEndID` IS NULL
102
+ OR aend.` StartPeriod` IS NOT NULL
103
+ AND aend.` EndPeriod` IS NOT NULL
104
+ AND aend.` StartPeriod` >= aend.` EndPeriod` )
105
+ AND CASE
106
+ WHEN (r.` AgeNameEndID` IS NOT NULL ) THEN greatest(CAST(a.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(a.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 ), CAST(aend.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(aend.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 ))
107
+ ELSE CAST(a.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(a.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 )
108
+ END <= 2000 .0
109
+ AND CASE
110
+ WHEN (r.` AgeNameEndID` IS NOT NULL ) THEN least((CAST(a.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(a.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 ), (CAST(aend.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(aend.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 ))
111
+ ELSE (CAST(a.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(a.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 )
112
+ END >= 2 .0
113
+ UNION ALL
114
+ SELECT
115
+ DISTINCT c.` CollectionObjectID` AS coid,
116
+ CASE
117
+ WHEN (p.` ChronosStratEndID` IS NOT NULL ) THEN least(CAST(cs.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(cs.` StartUncertainty` , 0 ), CAST(csend.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(csend.` StartUncertainty` , 0 ))
118
+ ELSE CAST(cs.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(cs.` StartUncertainty` , 0 )
119
+ END AS startperiod,
120
+ CASE
121
+ WHEN (p.` ChronosStratEndID` IS NOT NULL ) THEN greatest(CAST(cs.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(cs.` EndUncertainty` , 0 ), CAST(csend.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(csend.` EndUncertainty` , 0 ))
122
+ ELSE CAST(cs.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(cs.` EndUncertainty` , 0 )
123
+ END AS endperiod
124
+ FROM
125
+ collectionobject AS c
126
+ LEFT OUTER JOIN collectingevent AS ce ON
127
+ c.` CollectingEventID` = ce.` CollectingEventID`
128
+ LEFT OUTER JOIN locality AS l ON
129
+ ce.` LocalityID` = l.` LocalityID`
130
+ LEFT OUTER JOIN paleocontext AS p ON
131
+ c.` PaleoContextID` = p.` PaleoContextID`
132
+ OR ce.` PaleoContextID` = p.` PaleoContextID`
133
+ OR l.` PaleoContextID` = p.` PaleoContextID`
134
+ LEFT OUTER JOIN geologictimeperiod AS cs ON
135
+ p.` ChronosStratID` = cs.` GeologicTimePeriodID`
136
+ LEFT OUTER JOIN geologictimeperiod AS csend ON
137
+ p.` ChronosStratEndID` = csend.` GeologicTimePeriodID`
138
+ WHERE
139
+ p.` PaleoContextID` IS NOT NULL
140
+ AND cs.` StartPeriod` IS NOT NULL
141
+ AND cs.` EndPeriod` IS NOT NULL
142
+ AND cs.` StartPeriod` >= cs.` EndPeriod`
143
+ AND (p.` ChronosStratEndID` IS NULL
144
+ OR csend.` StartPeriod` IS NOT NULL
145
+ AND csend.` EndPeriod` IS NOT NULL
146
+ AND csend.` StartPeriod` >= csend.` EndPeriod` )
147
+ AND CASE
148
+ WHEN (p.` ChronosStratEndID` IS NOT NULL ) THEN least(CAST(cs.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(cs.` StartUncertainty` , 0 ), CAST(csend.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(csend.` StartUncertainty` , 0 ))
149
+ ELSE CAST(cs.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(cs.` StartUncertainty` , 0 )
150
+ END <= 2000 .0
151
+ AND CASE
152
+ WHEN (p.` ChronosStratEndID` IS NOT NULL ) THEN greatest(CAST(cs.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(cs.` EndUncertainty` , 0 ), CAST(csend.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(csend.` EndUncertainty` , 0 ))
153
+ ELSE CAST(cs.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(cs.` EndUncertainty` , 0 )
154
+ END >= 2 .0 ) AS unioned
155
+ GROUP BY
156
+ unioned .coid ) AS agg_subq ON
157
+ collectionobject.` CollectionObjectID` = agg_subq .coid
158
+ WHERE
159
+ collectionobject.` CollectionID` = 98304 ;
160
+ ```
161
+
162
+ Here is another example of a compiled SQLAlchemy query that gets generated:
163
+
164
+ ``` sql
165
+ SELECT
166
+ collectionobject.` CollectionObjectID` ,
167
+ concat_ws(' - ' , ifnull(regexp_replace(CAST(agg_subq .max_start_period AS CHAR ), ' \\ .(0+)$' , ' ' ), ' ' ), ifnull(regexp_replace(CAST(agg_subq .min_end_period AS CHAR ), ' \\ .(0+)$' , ' ' ), ' ' )) AS age,
168
+ absoluteage_1.` AbsoluteAge` ,
169
+ geologictimeperiod_1.` Name` ,
170
+ geologictimeperiod_2.` Name` ,
171
+ geologictimeperiod_3.` Name` ,
172
+ geologictimeperiod_4.` Name` ,
173
+ geologictimeperiod_5.` Name` ,
174
+ geologictimeperiod_6.` Name`
175
+ FROM
176
+ collectionobject
177
+ INNER JOIN (
178
+ SELECT
179
+ unioned .coid AS coid,
180
+ min (unioned .endperiod ) AS min_end_period,
181
+ max (unioned .startperiod ) AS max_start_period
182
+ FROM
183
+ (
184
+ SELECT
185
+ absoluteage.` CollectionObjectID` AS coid,
186
+ CAST(absoluteage.` AbsoluteAge` AS DECIMAL (10 , 6 )) - coalesce(absoluteage.` AgeUncertainty` , 0 ) AS startperiod,
187
+ CAST(absoluteage.` AbsoluteAge` AS DECIMAL (10 , 6 )) + coalesce(absoluteage.` AgeUncertainty` , 0 ) AS endperiod
188
+ FROM
189
+ absoluteage
190
+ WHERE
191
+ CAST(absoluteage.` AbsoluteAge` AS DECIMAL (10 , 6 )) - coalesce(absoluteage.` AgeUncertainty` , 0 ) <= 2000 .0
192
+ AND CAST(absoluteage.` AbsoluteAge` AS DECIMAL (10 , 6 )) + coalesce(absoluteage.` AgeUncertainty` , 0 ) >= 2 .0
193
+ UNION ALL
194
+ SELECT
195
+ r.` CollectionObjectID` AS coid,
196
+ CASE
197
+ WHEN (r.` AgeNameEndID` IS NOT NULL ) THEN greatest(CAST(a.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(a.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 ), CAST(aend.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(aend.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 ))
198
+ ELSE CAST(a.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(a.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 )
199
+ END AS startperiod,
200
+ CASE
201
+ WHEN (r.` AgeNameEndID` IS NOT NULL ) THEN least((CAST(a.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(a.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 ), (CAST(aend.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(aend.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 ))
202
+ ELSE (CAST(a.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(a.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 )
203
+ END AS endperiod
204
+ FROM
205
+ relativeage AS r
206
+ INNER JOIN geologictimeperiod AS a ON
207
+ r.` AgeNameID` = a.` GeologicTimePeriodID`
208
+ LEFT OUTER JOIN geologictimeperiod AS aend ON
209
+ r.` AgeNameEndID` = aend.` GeologicTimePeriodID`
210
+ WHERE
211
+ a.` StartPeriod` IS NOT NULL
212
+ AND a.` EndPeriod` IS NOT NULL
213
+ AND a.` StartPeriod` >= a.` EndPeriod`
214
+ AND (r.` AgeNameEndID` IS NULL
215
+ OR aend.` StartPeriod` IS NOT NULL
216
+ AND aend.` EndPeriod` IS NOT NULL
217
+ AND aend.` StartPeriod` >= aend.` EndPeriod` )
218
+ AND CASE
219
+ WHEN (r.` AgeNameEndID` IS NOT NULL ) THEN greatest(CAST(a.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(a.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 ), CAST(aend.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(aend.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 ))
220
+ ELSE CAST(a.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(a.` StartUncertainty` , 0 ) + coalesce(r.` AgeUncertainty` , 0 )
221
+ END <= 2000 .0
222
+ AND CASE
223
+ WHEN (r.` AgeNameEndID` IS NOT NULL ) THEN least((CAST(a.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(a.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 ), (CAST(aend.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(aend.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 ))
224
+ ELSE (CAST(a.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(a.` EndUncertainty` , 0 )) - coalesce(r.` AgeUncertainty` , 0 )
225
+ END >= 2 .0
226
+ UNION ALL
227
+ SELECT
228
+ DISTINCT c.` CollectionObjectID` AS coid,
229
+ CASE
230
+ WHEN (p.` ChronosStratEndID` IS NOT NULL ) THEN least(CAST(cs.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(cs.` StartUncertainty` , 0 ), CAST(csend.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(csend.` StartUncertainty` , 0 ))
231
+ ELSE CAST(cs.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(cs.` StartUncertainty` , 0 )
232
+ END AS startperiod,
233
+ CASE
234
+ WHEN (p.` ChronosStratEndID` IS NOT NULL ) THEN greatest(CAST(cs.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(cs.` EndUncertainty` , 0 ), CAST(csend.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(csend.` EndUncertainty` , 0 ))
235
+ ELSE CAST(cs.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(cs.` EndUncertainty` , 0 )
236
+ END AS endperiod
237
+ FROM
238
+ collectionobject AS c
239
+ LEFT OUTER JOIN collectingevent AS ce ON
240
+ c.` CollectingEventID` = ce.` CollectingEventID`
241
+ LEFT OUTER JOIN locality AS l ON
242
+ ce.` LocalityID` = l.` LocalityID`
243
+ LEFT OUTER JOIN paleocontext AS p ON
244
+ c.` PaleoContextID` = p.` PaleoContextID`
245
+ OR ce.` PaleoContextID` = p.` PaleoContextID`
246
+ OR l.` PaleoContextID` = p.` PaleoContextID`
247
+ LEFT OUTER JOIN geologictimeperiod AS cs ON
248
+ p.` ChronosStratID` = cs.` GeologicTimePeriodID`
249
+ LEFT OUTER JOIN geologictimeperiod AS csend ON
250
+ p.` ChronosStratEndID` = csend.` GeologicTimePeriodID`
251
+ WHERE
252
+ p.` PaleoContextID` IS NOT NULL
253
+ AND cs.` StartPeriod` IS NOT NULL
254
+ AND cs.` EndPeriod` IS NOT NULL
255
+ AND cs.` StartPeriod` >= cs.` EndPeriod`
256
+ AND (p.` ChronosStratEndID` IS NULL
257
+ OR csend.` StartPeriod` IS NOT NULL
258
+ AND csend.` EndPeriod` IS NOT NULL
259
+ AND csend.` StartPeriod` >= csend.` EndPeriod` )
260
+ AND CASE
261
+ WHEN (p.` ChronosStratEndID` IS NOT NULL ) THEN least(CAST(cs.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(cs.` StartUncertainty` , 0 ), CAST(csend.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(csend.` StartUncertainty` , 0 ))
262
+ ELSE CAST(cs.` StartPeriod` AS DECIMAL (10 , 6 )) + coalesce(cs.` StartUncertainty` , 0 )
263
+ END <= 2000 .0
264
+ AND CASE
265
+ WHEN (p.` ChronosStratEndID` IS NOT NULL ) THEN greatest(CAST(cs.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(cs.` EndUncertainty` , 0 ), CAST(csend.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(csend.` EndUncertainty` , 0 ))
266
+ ELSE CAST(cs.` EndPeriod` AS DECIMAL (10 , 6 )) - coalesce(cs.` EndUncertainty` , 0 )
267
+ END >= 2 .0 ) AS unioned
268
+ GROUP BY
269
+ unioned .coid ) AS agg_subq ON
270
+ collectionobject.` CollectionObjectID` = agg_subq .coid
271
+ LEFT OUTER JOIN absoluteage AS absoluteage_1 ON
272
+ collectionobject.` CollectionObjectID` = absoluteage_1.` CollectionObjectID`
273
+ LEFT OUTER JOIN relativeage AS relativeage_1 ON
274
+ collectionobject.` CollectionObjectID` = relativeage_1.` CollectionObjectID`
275
+ LEFT OUTER JOIN geologictimeperiod AS geologictimeperiod_1 ON
276
+ geologictimeperiod_1.` GeologicTimePeriodID` = relativeage_1.` AgeNameID`
277
+ LEFT OUTER JOIN geologictimeperiod AS geologictimeperiod_2 ON
278
+ geologictimeperiod_2.` GeologicTimePeriodID` = relativeage_1.` AgeNameEndID`
279
+ LEFT OUTER JOIN paleocontext AS paleocontext_1 ON
280
+ paleocontext_1.` PaleoContextID` = collectionobject.` PaleoContextID`
281
+ LEFT OUTER JOIN geologictimeperiod AS geologictimeperiod_3 ON
282
+ geologictimeperiod_3.` GeologicTimePeriodID` = paleocontext_1.` ChronosStratID`
283
+ LEFT OUTER JOIN geologictimeperiod AS geologictimeperiod_4 ON
284
+ geologictimeperiod_4.` GeologicTimePeriodID` = paleocontext_1.` ChronosStratEndID`
285
+ LEFT OUTER JOIN collectingevent AS collectingevent_1 ON
286
+ paleocontext_1.` PaleoContextID` = collectingevent_1.` PaleoContextID`
287
+ LEFT OUTER JOIN paleocontext AS paleocontext_2 ON
288
+ paleocontext_2.` PaleoContextID` = collectingevent_1.` PaleoContextID`
289
+ LEFT OUTER JOIN geologictimeperiod AS geologictimeperiod_5 ON
290
+ geologictimeperiod_5.` GeologicTimePeriodID` = paleocontext_2.` ChronosStratID`
291
+ LEFT OUTER JOIN geologictimeperiod AS geologictimeperiod_6 ON
292
+ geologictimeperiod_6.` GeologicTimePeriodID` = paleocontext_2.` ChronosStratEndID`
293
+ WHERE
294
+ collectionobject.` CollectionID` = 98304 ;
295
+ ```
0 commit comments