-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathstatements.go
More file actions
303 lines (301 loc) · 7.57 KB
/
statements.go
File metadata and controls
303 lines (301 loc) · 7.57 KB
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
294
295
296
297
298
299
300
301
302
303
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Query statements used by the SpannerClient.
package spanner
// SQL / GQL statements executed by the SpannerClient
var statements = struct {
// Fetch latest CompletionTimestamp from IngestionHistory table.
getCompletionTimestamp string
// Filter by single id.
getId string
// Filter by multiple ids.
getIds string
// Fetch Properties for out arcs.
getPropsBySubjectID string
// Fetch Properties for in arcs.
getPropsByObjectID string
// Prefix for a graph query.
graphPrefix string
// Prefix for a graph query that matches any path.
graphPrefixAny string
// Fetch Edges for out arcs with a single hop.
getEdgesBySubjectID string
// Fetch Edges for out arcs with chaining.
getChainedEdgesBySubjectID string
// Fetch Edges for in arcs with a single hop.
getEdgesByObjectID string
// Fetch Edges for in arcs with chaining.
getChainedEdgesByObjectID string
// Subquery to filter edges by predicate.
filterPredicate string
// Subquery to filter edges by multiple predicates.
filterPredicates string
// Subquery to filter edges by object properties.
filterProperty string
// Subquery to filter edges by an object value.
filterValue string
// Subquery to filter edges by multiple object values.
filterValues string
// Default subquery to return Edges.
returnEdges string
// Default subquery to return Edges for arcs with chaining.
returnChainedEdges string
// Subquery to return Edges with filters.
returnFilterEdges string
// Subquery to apply page offset.
applyOffset string
// Subquery to apply page limit.
applyLimit string
// Fetch Observations.
getObs string
// Filter by variable dcids.
selectVariableDcids string
// Filter by entity dcids.
selectEntityDcids string
// Fetch observations for variable + contained in place.
getObsByVariableAndContainedInPlace string
// Search nodes by name only.
searchNodesByQuery string
// Subquery to filter search results by types.
filterTypes string
// Resolve dcid to dcid (ie dcid search).
resolveDcidToDcid string
// Resolve dcid to other property.
resolveDcidToProp string
// Resolve other property to dcid.
resolvePropToDcid string
// Resolve one property to another.
resolvePropToProp string
// Generic node pattern.
node string
// Generic subquery for filtering a Node.
nodeFilter string
// Generic triple pattern.
triple string
// Get variable metadata.
getVariableMetadata string
}{
getCompletionTimestamp: ` SELECT
CompletionTimestamp
FROM
IngestionHistory
WHERE
IngestionFailure = FALSE
ORDER BY
CompletionTimestamp DESC
LIMIT 1`,
getId: `= @id`,
getIds: `IN UNNEST(@id)`,
getPropsBySubjectID: ` GRAPH DCGraph MATCH -[e:Edge
WHERE
e.subject_id %s]->
RETURN DISTINCT
e.subject_id,
e.predicate
ORDER BY
e.subject_id,
e.predicate`,
getPropsByObjectID: ` GRAPH DCGraph MATCH -[e:Edge
WHERE
e.object_id %s]->
RETURN DISTINCT
e.object_id AS subject_id,
e.predicate
ORDER BY
subject_id,
predicate`,
graphPrefix: ` GRAPH DCGraph MATCH `,
graphPrefixAny: ` GRAPH DCGraph MATCH ANY `,
getEdgesBySubjectID: `(m:Node
WHERE
m.subject_id %[1]s)-[e:Edge%[2]s]->(n:Node)`,
getChainedEdgesBySubjectID: `(m:Node
WHERE
m.subject_id %s)-[e:Edge
WHERE
e.predicate = @predicate]->{1,%d}(n:Node)`,
getEdgesByObjectID: `(m:Node
WHERE
m.subject_id %[1]s)<-[e:Edge%[2]s]-(n:Node)`,
getChainedEdgesByObjectID: `(m:Node
WHERE
m.subject_id %s)<-[e:Edge
WHERE
e.predicate = @predicate]-{1,%d}(n:Node)`,
filterPredicate: `
WHERE
e.predicate = @predicate`,
filterPredicates: `
WHERE
e.predicate IN UNNEST(@predicate)`,
filterProperty: `(n)-[filter%[1]d:Edge
WHERE
filter%[1]d.predicate = @prop%[1]d%s]->`,
filterValue: `
AND filter%[1]d.object_id = @val%[1]d`,
filterValues: `
AND filter%[1]d.object_id IN UNNEST(@val%[1]d)`,
returnEdges: `
RETURN
m.subject_id,
e.predicate,
e.provenance,
n.value,
n.bytes,
n.name,
n.types
ORDER BY
subject_id,
predicate,
n.subject_id,
provenance`,
returnChainedEdges: `
RETURN DISTINCT
m.subject_id,
n.subject_id AS object_id
NEXT MATCH (n)
WHERE
n.subject_id = object_id
RETURN
subject_id,
@result_predicate AS predicate,
'' AS provenance,
n.value,
n.bytes,
n.name,
n.types
ORDER BY
subject_id,
object_id`,
returnFilterEdges: `
RETURN
m.subject_id,
n.subject_id AS object_id,
e.predicate,
e.provenance
NEXT MATCH (n)
WHERE
n.subject_id = object_id
RETURN
subject_id,
predicate,
provenance,
n.value,
n.bytes,
n.name,
n.types
ORDER BY
subject_id,
predicate,
object_id,
provenance`,
applyOffset: `
OFFSET %d`,
applyLimit: `
LIMIT %d`,
getObs: ` SELECT
variable_measured,
observation_about,
observations,
import_name,
observation_period,
measurement_method,
unit,
scaling_factor,
provenance_url,
is_dc_aggregate,
facet_id
FROM
Observation`,
selectVariableDcids: `variable_measured IN UNNEST(@variables)`,
selectEntityDcids: `observation_about IN UNNEST(@entities)`,
getObsByVariableAndContainedInPlace: ` SELECT
obs.variable_measured,
obs.observation_about,
obs.observations,
obs.import_name,
obs.observation_period,
obs.measurement_method,
obs.unit,
obs.scaling_factor,
obs.provenance_url,
obs.is_dc_aggregate,
obs.facet_id
FROM
GRAPH_TABLE (
DCGraph MATCH <-[e:Edge
WHERE
e.object_id = @ancestor
AND e.predicate = 'linkedContainedInPlace']-()-[{predicate: 'typeOf', object_id: @childPlaceType}]->
RETURN
e.subject_id as object_id
)result
INNER JOIN (%s)obs
ON
result.object_id = obs.observation_about`,
searchNodesByQuery: ` GRAPH DCGraph MATCH (n:Node)
WHERE
SEARCH(n.name_tokenlist, @query)%s
RETURN
n.subject_id,
n.name,
n.types,
SCORE(n.name_tokenlist, @query, enhance_query => TRUE) AS score
ORDER BY
score + IF(n.name = @query, 1, 0) DESC,
n.name ASC
LIMIT %d`,
filterTypes: `
AND ARRAY_INCLUDES_ANY(n.types, @types)`,
resolveDcidToDcid: ` GRAPH DCGraph MATCH (n:Node
WHERE
n.subject_id IN UNNEST(@nodes))
RETURN
n.subject_id AS node,
n.subject_id AS candidate`,
resolveDcidToProp: ` GRAPH DCGraph MATCH -[o:Edge
WHERE
o.subject_id IN UNNEST(@nodes)
AND o.predicate = @outProp]->(n:Node)
RETURN
o.subject_id AS node,
n.value AS candidate`,
resolvePropToDcid: ` GRAPH DCGraph MATCH <-[i:Edge
WHERE
i.object_id IN UNNEST(@nodes)
AND i.predicate = @inProp]-
RETURN
i.object_id AS node,
i.subject_id AS candidate`,
resolvePropToProp: ` GRAPH DCGraph MATCH <-[i:Edge
WHERE
i.object_id IN UNNEST(@nodes)
AND i.predicate = @inProp]-()-[o:Edge
WHERE
o.predicate = @outProp]->(n:Node)
RETURN
i.object_id AS node,
n.value AS candidate`,
node: `(%[1]s:Node%[2]s)`,
nodeFilter: `
WHERE
%[1]s.subject_id IN UNNEST(@%[1]s)`,
triple: `(%[1]s:Node%[2]s)-[:Edge {predicate: @predicate%[3]d}]->(%[4]s:Node%[5]s)`,
getVariableMetadata: ` SELECT
*
FROM
VariableMetadata
WHERE
variable_measured %s`,
}