-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcypher_optional.go
More file actions
351 lines (300 loc) · 7.77 KB
/
cypher_optional.go
File metadata and controls
351 lines (300 loc) · 7.77 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package graphdb
import (
"context"
"strings"
)
// ---------------------------------------------------------------------------
// OPTIONAL MATCH — left-outer-join semantics.
//
// MATCH (n:Person) OPTIONAL MATCH (n)-[r:WROTE]->(b) RETURN n, b
//
// For each row from the main MATCH, the optional pattern is attempted.
// If the optional pattern matches, one row per match is emitted.
// If not, a single row is emitted with nil for unmatched bindings.
// ---------------------------------------------------------------------------
// execWithOptionalMatch handles queries that have an OPTIONAL MATCH clause.
// It executes the main MATCH first, then for each result row attempts the
// optional pattern expansion.
func (db *DB) execWithOptionalMatch(_ context.Context, q *CypherQuery) (*CypherResult, error) {
// Step 1: Execute the main MATCH to get primary bindings.
mainQ := &CypherQuery{
Match: q.Match,
Where: q.Where,
}
mainBindings, err := db.collectMainBindings(mainQ)
if err != nil {
return nil, err
}
optPat := q.OptionalMatch.Pattern
// Step 2: For each main binding, attempt the optional match.
var allBindings []map[string]any
for _, binding := range mainBindings {
optRows, err := db.attemptOptionalMatch(binding, optPat, q.OptionalWhere)
if err != nil {
return nil, err
}
if len(optRows) == 0 {
// No optional matches — emit the row with nil for optional bindings.
nulled := db.nullifyOptionalBindings(binding, optPat)
allBindings = append(allBindings, nulled)
} else {
allBindings = append(allBindings, optRows...)
}
}
// Step 3: Project the combined bindings into the result.
return db.projectBindings(q, allBindings)
}
// collectMainBindings executes the main MATCH and returns intermediate
// binding maps (variable name → value) before RETURN projection.
func (db *DB) collectMainBindings(q *CypherQuery) ([]map[string]any, error) {
pat := q.Match.Pattern
var bindings []map[string]any
switch {
case len(pat.Nodes) == 1 && len(pat.Rels) == 0:
np := pat.Nodes[0]
varName := np.Variable
if varName == "" {
varName = "_n"
}
candidates, err := db.findCandidates(np)
if err != nil {
return nil, err
}
for _, n := range candidates {
if !matchProps(n.Props, np.Props) {
continue
}
if q.Where != nil {
b := map[string]any{varName: n}
ok, err := evalBool(q.Where, b)
if err != nil {
return nil, err
}
if !ok {
continue
}
}
bindings = append(bindings, map[string]any{varName: n})
}
case len(pat.Nodes) == 2 && len(pat.Rels) == 1:
aPat := pat.Nodes[0]
rel := pat.Rels[0]
bPat := pat.Nodes[1]
aVar := aPat.Variable
if aVar == "" {
aVar = "_a"
}
rVar := rel.Variable
bVar := bPat.Variable
if bVar == "" {
bVar = "_b"
}
aCandidates, err := db.findCandidates(aPat)
if err != nil {
return nil, err
}
for _, a := range aCandidates {
dir := rel.Dir
edges, err := db.getEdgesForNode(a.ID, dir)
if err != nil {
continue
}
for _, e := range edges {
if rel.Label != "" && !strings.EqualFold(e.Label, rel.Label) {
continue
}
targetID := e.To
if dir == Incoming {
targetID = e.From
}
bNode, err := db.getNode(targetID)
if err != nil {
continue
}
if len(bPat.Labels) > 0 && !matchLabels(bNode.Labels, bPat.Labels) {
continue
}
if !matchProps(bNode.Props, bPat.Props) {
continue
}
row := map[string]any{aVar: a, bVar: bNode}
if rVar != "" {
row[rVar] = e
}
if q.Where != nil {
ok, err := evalBool(q.Where, row)
if err != nil {
return nil, err
}
if !ok {
continue
}
}
bindings = append(bindings, row)
}
}
}
return bindings, nil
}
// attemptOptionalMatch tries to match the optional pattern using the already-bound
// variables from the main MATCH. Returns expanded binding rows.
func (db *DB) attemptOptionalMatch(binding map[string]any, optPat Pattern, optWhere *Expression) ([]map[string]any, error) {
// The most common case: OPTIONAL MATCH (n)-[r:TYPE]->(b)
// where n is already bound from the main MATCH.
if len(optPat.Nodes) == 2 && len(optPat.Rels) == 1 {
aPat := optPat.Nodes[0]
rel := optPat.Rels[0]
bPat := optPat.Nodes[1]
aVar := aPat.Variable
if aVar == "" {
aVar = "_a"
}
rVar := rel.Variable
bVar := bPat.Variable
if bVar == "" {
bVar = "_b"
}
// Look up the source node from existing bindings.
boundVal, ok := binding[aVar]
if !ok {
return nil, nil // source variable not bound
}
sourceNode, ok := boundVal.(*Node)
if !ok {
return nil, nil
}
dir := rel.Dir
edges, err := db.getEdgesForNode(sourceNode.ID, dir)
if err != nil {
return nil, nil // treat errors as no match for optional
}
var rows []map[string]any
for _, e := range edges {
if rel.Label != "" && !strings.EqualFold(e.Label, rel.Label) {
continue
}
targetID := e.To
if dir == Incoming {
targetID = e.From
}
bNode, err := db.getNode(targetID)
if err != nil {
continue
}
if len(bPat.Labels) > 0 && !matchLabels(bNode.Labels, bPat.Labels) {
continue
}
if !matchProps(bNode.Props, bPat.Props) {
continue
}
// Build the combined row.
row := make(map[string]any, len(binding)+2)
for k, v := range binding {
row[k] = v
}
row[bVar] = bNode
if rVar != "" {
row[rVar] = e
}
// Apply OPTIONAL WHERE.
if optWhere != nil {
ok, err := evalBool(optWhere, row)
if err != nil {
continue
}
if !ok {
continue
}
}
rows = append(rows, row)
}
return rows, nil
}
// Single-node optional match: OPTIONAL MATCH (b:Label)
if len(optPat.Nodes) == 1 && len(optPat.Rels) == 0 {
np := optPat.Nodes[0]
varName := np.Variable
if varName == "" {
varName = "_opt"
}
candidates, err := db.findCandidates(np)
if err != nil {
return nil, nil
}
var rows []map[string]any
for _, n := range candidates {
row := make(map[string]any, len(binding)+1)
for k, v := range binding {
row[k] = v
}
row[varName] = n
if optWhere != nil {
ok, err := evalBool(optWhere, row)
if err != nil {
continue
}
if !ok {
continue
}
}
rows = append(rows, row)
}
return rows, nil
}
return nil, nil
}
// nullifyOptionalBindings creates a copy of the binding with nil for any
// variables introduced by the optional pattern.
func (db *DB) nullifyOptionalBindings(binding map[string]any, optPat Pattern) map[string]any {
row := make(map[string]any, len(binding)+3)
for k, v := range binding {
row[k] = v
}
// Add nil for variables from the optional pattern.
for _, np := range optPat.Nodes {
if np.Variable != "" {
if _, exists := row[np.Variable]; !exists {
row[np.Variable] = nil
}
}
}
for _, rp := range optPat.Rels {
if rp.Variable != "" {
if _, exists := row[rp.Variable]; !exists {
row[rp.Variable] = nil
}
}
}
return row
}
// projectBindings projects a list of binding maps into a CypherResult
// using the RETURN clause, ORDER BY, and LIMIT.
func (db *DB) projectBindings(q *CypherQuery, bindings []map[string]any) (*CypherResult, error) {
result := &CypherResult{}
// Build columns.
for _, item := range q.Return.Items {
result.Columns = append(result.Columns, returnItemName(item))
}
// Build rows.
for _, binding := range bindings {
row := make(map[string]any, len(q.Return.Items))
for _, item := range q.Return.Items {
colName := item.Alias
if colName == "" {
colName = returnItemName(item)
}
val, _ := evalExpr(&item.Expr, binding)
row[colName] = val
}
result.Rows = append(result.Rows, row)
}
// ORDER BY.
if len(q.OrderBy) > 0 {
sortRows(result.Rows, q.OrderBy)
}
// LIMIT.
if q.Limit > 0 && len(result.Rows) > q.Limit {
result.Rows = result.Rows[:q.Limit]
}
return result, nil
}