@@ -216,28 +216,44 @@ func selectFieldsToQueryOptions(selectFields []string) queryOptions {
216
216
return qo
217
217
}
218
218
219
- // Whether to return all observations in the Observation response .
220
- func queryObs (qo * queryOptions ) bool {
219
+ // Whether the queryOptions are for a full observation request .
220
+ func isObservationRequest (qo * queryOptions ) bool {
221
221
return qo .date && qo .value
222
222
}
223
223
224
- func buildBaseObsStatement (variables []string , entities []string , date string ) spanner.Statement {
224
+ // Whether the queryOptions are for an existence request.
225
+ func isExistenceRequest (selectFields []string ) bool {
226
+ qo := selectFieldsToQueryOptions (selectFields )
227
+ return ! isObservationRequest (& qo ) && ! qo .facet
228
+ }
229
+
230
+ func buildBaseObsStatement (variables []string , entities []string , date string , filterObs bool ) spanner.Statement {
225
231
stmt := spanner.Statement {
226
232
Params : map [string ]interface {}{},
227
233
}
228
234
filters := []string {}
229
235
236
+ var baseStmt string
237
+ var obsStmt string
230
238
switch date {
231
239
case "" :
232
- stmt .SQL = statements .getAllObs
240
+ baseStmt = statements .getObs
241
+ obsStmt = statements .allObs
233
242
case shared .LATEST :
234
- stmt .SQL = statements .getLatestObs
243
+ baseStmt = statements .getObs
244
+ obsStmt = statements .latestObs
235
245
default :
236
- stmt . SQL = statements .getDateObs
246
+ baseStmt = statements .getDateObs
237
247
stmt .Params ["date" ] = fmt .Sprintf ("$.%s" , date )
248
+ obsStmt = statements .dateObs
238
249
filters = append (filters , statements .selectDate )
239
250
}
240
251
252
+ if filterObs {
253
+ obsStmt = statements .emptyObs
254
+ }
255
+ stmt .SQL = fmt .Sprintf (baseStmt , obsStmt )
256
+
241
257
if len (variables ) > 0 {
242
258
stmt .Params ["variables" ] = variables
243
259
filters = append (filters , statements .selectVariableDcids )
@@ -272,7 +288,7 @@ func observationsToObservationResponse(req *pbv2.ObservationRequest, observation
272
288
// For now, V3 will match the behavior of V2 to preserve backward compatibility and allow datasource merging.
273
289
// TODO: Unify these responses more.
274
290
qo := selectFieldsToQueryOptions (req .Select )
275
- if queryObs (& qo ) {
291
+ if isObservationRequest (& qo ) {
276
292
// Returns FacetObservations with PointStats.
277
293
return obsToObsResponse (req , observations )
278
294
} else if qo .facet {
0 commit comments