Skip to content

Commit b3c5ab8

Browse files
authored
Merge pull request #52 from zinic/upstream-main
fix(cysql): ensure that optional matches are only rendered with valid previous frame - BED-7054
2 parents 83d62ab + f104890 commit b3c5ab8

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

cypher/models/pgsql/test/translation_cases/multipart.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,6 @@ with s0 as (with s1 as (with recursive s2(root_id, next_id, depth, satisfied, is
8585

8686
-- case: match (a:NodeKind2)-[:EdgeKind1]->(g:NodeKind1)-[:EdgeKind2]->(s:NodeKind2) with count(a) as uc where uc > 5 match p = (a)-[:EdgeKind1]->(g)-[:EdgeKind2]->(s) return p
8787
with s0 as (with s1 as (select (e0.id, e0.start_id, e0.end_id, e0.kind_id, e0.properties)::edgecomposite as e0, (n0.id, n0.kind_ids, n0.properties)::nodecomposite as n0, (n1.id, n1.kind_ids, n1.properties)::nodecomposite as n1 from edge e0 join node n0 on n0.kind_ids operator (pg_catalog.@>) array [2]::int2[] and n0.id = e0.start_id join node n1 on n1.kind_ids operator (pg_catalog.@>) array [1]::int2[] and n1.id = e0.end_id where e0.kind_id = any (array [3]::int2[])), s2 as (select s1.e0 as e0, (e1.id, e1.start_id, e1.end_id, e1.kind_id, e1.properties)::edgecomposite as e1, s1.n0 as n0, s1.n1 as n1, (n2.id, n2.kind_ids, n2.properties)::nodecomposite as n2 from s1 join edge e1 on (s1.n1).id = e1.start_id join node n2 on n2.kind_ids operator (pg_catalog.@>) array [2]::int2[] and n2.id = e1.end_id where e1.kind_id = any (array [4]::int2[])) select count(s2.n0)::int8 as i0 from s2), s3 as (select (e2.id, e2.start_id, e2.end_id, e2.kind_id, e2.properties)::edgecomposite as e2, s0.i0 as i0, (n3.id, n3.kind_ids, n3.properties)::nodecomposite as n3, (n4.id, n4.kind_ids, n4.properties)::nodecomposite as n4 from s0, edge e2 join node n3 on n3.id = e2.start_id join node n4 on n4.id = e2.end_id where e2.kind_id = any (array [3]::int2[]) and (s0.i0 > 5)), s4 as (select s3.e2 as e2, (e3.id, e3.start_id, e3.end_id, e3.kind_id, e3.properties)::edgecomposite as e3, s3.i0 as i0, s3.n3 as n3, s3.n4 as n4, (n5.id, n5.kind_ids, n5.properties)::nodecomposite as n5 from s3 join edge e3 on (s3.n4).id = e3.start_id join node n5 on n5.id = e3.end_id where e3.kind_id = any (array [4]::int2[])) select edges_to_path(variadic array [(s4.e2).id, (s4.e3).id]::int8[])::pathcomposite as p from s4;
88+
89+
-- case: optional match (g:NodeKind1)<-[r:EdgeKind1]-(m:NodeKind2) with g, count(r) as memberCount where memberCount = 0 return g
90+
with s0 as (with s1 as (select (e0.id, e0.start_id, e0.end_id, e0.kind_id, e0.properties)::edgecomposite as e0, (n0.id, n0.kind_ids, n0.properties)::nodecomposite as n0, (n1.id, n1.kind_ids, n1.properties)::nodecomposite as n1 from edge e0 join node n0 on n0.kind_ids operator (pg_catalog.@>) array [1]::int2[] and n0.id = e0.end_id join node n1 on n1.kind_ids operator (pg_catalog.@>) array [2]::int2[] and n1.id = e0.start_id where e0.kind_id = any (array [3]::int2[])) select s1.n0 as n0, count(s1.e0)::int8 as i0 from s1 group by n0) select s0.n0 as g from s0 where (s0.i0 = 0);

cypher/models/pgsql/translate/match.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ func (s *Translator) translateMatch(match *cypher.Match) error {
3636
return err
3737
}
3838

39-
// If there is no previous frame, then skip translating an `OPTIONAL MATCH`/treat as plain `MATCH`
40-
if match.Optional && s.scope.CurrentFrame().Previous != nil {
41-
return s.translateOptionalMatch()
39+
// If there is no valid previous frame, skip translating an `OPTIONAL MATCH`/treat as plain `MATCH`
40+
if match.Optional {
41+
if _, hasValidPrevious := s.previousValidFrame(s.scope.CurrentFrame()); hasValidPrevious {
42+
return s.translateOptionalMatch()
43+
}
4244
}
4345

4446
return nil

0 commit comments

Comments
 (0)