opencypher.org | opencypher@googlegroups.comopencypher.org | opencypher@googlegroups.com
Path Query Patterns
CIP2017-02-06
a.k.a. (Conjunctive) Regular Path Queries
“Regular expressions over Graphs”
Tobias Lindaaker
tobias@neotechnology.com
opencypher.org | opencypher@googlegroups.com
Find complex connections
MATCH (a)-[~cousin]-(b)
MATCH (me)-[:ALLY | [:ENEMY :ENEMY]]-(friend)
MATCH (x)-[[:A :B]+ :C+ [:B :A]*]->(y)
MATCH p = (a)-[[:KNOWS | :LOVES]+]->()
opencypher.org | opencypher@googlegroups.com
● Reuse and extend existing syntax
● Direct notation for common simple patterns
● Allow for complex patterns
● Avoid repetition and ceremony
Syntax design philosophy
opencypher.org | opencypher@googlegroups.com
● Shorthand syntax for node labels!
● Shorthand syntax for matching any edge!
● Shorthand syntax for property predicates!
● Declared Path Patterns are scoped per query
And declared at the head of the query
● Non-repetitive syntax for Declared Path Patterns
Improved since last time
opencypher.org | opencypher@googlegroups.com
Regular Expressions over Paths
● Sequence / Concatenation:
()-[ ]-()
● Alternative / Disjunction:
()-[ | ]-()
● Transitive closure:
()-[ *]-() // zero or more times
()-[ +]-() // one or more times
()-[ *n..]-() // n or more times
()-[ *n..m]-() // at least n, at most m
● Overriding direction for sub-pattern:
()-[< ]-() ()-[ >]-() ()-[< >]-()
Composition of Patterns
opencypher.org | opencypher@googlegroups.com
● Edge Label
()-[:KNOWS]-()
● Node Label
()-[(:Person)]-()
● Edge Properties
()-[:ROAD{length:12}]-()
● Node Properties
()-[({year:2017})]-()
● Edges with any label, and matching empty path
()-[-]-() ()-[()]-()
● Grouping of expressions
()-[[:KNOWS :LOVES]+]-()
Predicates for Path Patterns
opencypher.org | opencypher@googlegroups.com
● Referenced using ~
MATCH (a)-[~my_pattern]-(b)
● Declaration:
PATH PATTERN my_pattern =
()-[:FOO :BAR* :BAZ]-()
● Makes the pattern language Context Free
Declared Path Patterns
opencypher.org | opencypher@googlegroups.com
● PATH PATTERN same_color_nodes = (a)-[-]-(b)
WHERE a.color = b.color
MATCH (x)-[~same_color_nodes+]-(y)
● PATH PATTERN older_friends = (a)-[:KNOWS]-(b)
WHERE b.birthday < a.birthday
Advanced property tests
opencypher.org | opencypher@googlegroups.com
● PATH PATTERN friends_in_same_city = (a)-[:KNOWS]-(b)
WHERE EXISTS {
(a)-[:LIVES_IN]->()<-[:LIVES_IN]-(b)
}
● One pattern must be designated the main pattern
● Other patterns are put in WHERE
● The main pattern is presented when binding a matched path:
MATCH friendships = (a)-[~friends_in_same_city+]-()
Conjunctive Path Queries
opencypher.org | opencypher@googlegroups.com
Classic Context Free use case
// treats siblings as “zeroth cousin”
PATH PATTERN cousin =
()-[:PARENT> [() | ~cousin] <:PARENT]-()
MATCH rel=(me)-[~cousin]-(cus)
WHERE length(rel) > 2 // filter away siblings
Balanced Path Matching
opencypher.org | opencypher@googlegroups.com
● Covers all of GXPath - except negation
● Weaker scoping reach than
Regular Expressions with Memory
○ Variables only reach within single
Declared Path Pattern
● Defines a Context Free Language over paths
rather than just a Regular Language
● More sophisticated data tests than GXPath
Expressive power
compared to academic languages
opencypher.org | opencypher@googlegroups.com
PATH PATTERN same_pet_name = (a)-[:KNOWS]-(b)
WHERE EXISTS {
MATCH (a)-[:OWNS]-(petA:Animal),
(b)-[:OWNS]-(petB:Animal)
WHERE petA.name = petB.name
}
MATCH peculiar_friends = (x)-[~same_pet_name*]-()
Sophisticated data test
opencypher.org | opencypher@googlegroups.com
● Differentiate between single edge and path
MATCH (a)-[single:EDGE]-(and_a)-/~path*/-()
● Single edge matching allows variable binding
● Binding a path results in a whole path value bound
Alternative syntax ideas
opencypher.org | opencypher@googlegroups.com
“Battle testing”
● Prototype implementation
● Try to express interesting queries
○ Needs to find / define interesting queries
● Measure up against “yardstick queries”
● Academic evaluation of
○ expressive power
○ computational complexity
Next steps
opencypher.org | opencypher@googlegroups.com
that I have not yet been able to express
● Paths where the property value differs in all
nodes
○ quite possibly not tractable
● Paths where the property value in all
subsequent nodes differs from the first node
Yardstick queries

Path Pattern Queries: Introducing Regular Path Queries in openCypher

  • 1.
    opencypher.org | opencypher@googlegroups.comopencypher.org| opencypher@googlegroups.com Path Query Patterns CIP2017-02-06 a.k.a. (Conjunctive) Regular Path Queries “Regular expressions over Graphs” Tobias Lindaaker tobias@neotechnology.com
  • 2.
    opencypher.org | opencypher@googlegroups.com Findcomplex connections MATCH (a)-[~cousin]-(b) MATCH (me)-[:ALLY | [:ENEMY :ENEMY]]-(friend) MATCH (x)-[[:A :B]+ :C+ [:B :A]*]->(y) MATCH p = (a)-[[:KNOWS | :LOVES]+]->()
  • 3.
    opencypher.org | opencypher@googlegroups.com ●Reuse and extend existing syntax ● Direct notation for common simple patterns ● Allow for complex patterns ● Avoid repetition and ceremony Syntax design philosophy
  • 4.
    opencypher.org | opencypher@googlegroups.com ●Shorthand syntax for node labels! ● Shorthand syntax for matching any edge! ● Shorthand syntax for property predicates! ● Declared Path Patterns are scoped per query And declared at the head of the query ● Non-repetitive syntax for Declared Path Patterns Improved since last time
  • 5.
    opencypher.org | opencypher@googlegroups.com RegularExpressions over Paths ● Sequence / Concatenation: ()-[ ]-() ● Alternative / Disjunction: ()-[ | ]-() ● Transitive closure: ()-[ *]-() // zero or more times ()-[ +]-() // one or more times ()-[ *n..]-() // n or more times ()-[ *n..m]-() // at least n, at most m ● Overriding direction for sub-pattern: ()-[< ]-() ()-[ >]-() ()-[< >]-() Composition of Patterns
  • 6.
    opencypher.org | opencypher@googlegroups.com ●Edge Label ()-[:KNOWS]-() ● Node Label ()-[(:Person)]-() ● Edge Properties ()-[:ROAD{length:12}]-() ● Node Properties ()-[({year:2017})]-() ● Edges with any label, and matching empty path ()-[-]-() ()-[()]-() ● Grouping of expressions ()-[[:KNOWS :LOVES]+]-() Predicates for Path Patterns
  • 7.
    opencypher.org | opencypher@googlegroups.com ●Referenced using ~ MATCH (a)-[~my_pattern]-(b) ● Declaration: PATH PATTERN my_pattern = ()-[:FOO :BAR* :BAZ]-() ● Makes the pattern language Context Free Declared Path Patterns
  • 8.
    opencypher.org | opencypher@googlegroups.com ●PATH PATTERN same_color_nodes = (a)-[-]-(b) WHERE a.color = b.color MATCH (x)-[~same_color_nodes+]-(y) ● PATH PATTERN older_friends = (a)-[:KNOWS]-(b) WHERE b.birthday < a.birthday Advanced property tests
  • 9.
    opencypher.org | opencypher@googlegroups.com ●PATH PATTERN friends_in_same_city = (a)-[:KNOWS]-(b) WHERE EXISTS { (a)-[:LIVES_IN]->()<-[:LIVES_IN]-(b) } ● One pattern must be designated the main pattern ● Other patterns are put in WHERE ● The main pattern is presented when binding a matched path: MATCH friendships = (a)-[~friends_in_same_city+]-() Conjunctive Path Queries
  • 10.
    opencypher.org | opencypher@googlegroups.com ClassicContext Free use case // treats siblings as “zeroth cousin” PATH PATTERN cousin = ()-[:PARENT> [() | ~cousin] <:PARENT]-() MATCH rel=(me)-[~cousin]-(cus) WHERE length(rel) > 2 // filter away siblings Balanced Path Matching
  • 11.
    opencypher.org | opencypher@googlegroups.com ●Covers all of GXPath - except negation ● Weaker scoping reach than Regular Expressions with Memory ○ Variables only reach within single Declared Path Pattern ● Defines a Context Free Language over paths rather than just a Regular Language ● More sophisticated data tests than GXPath Expressive power compared to academic languages
  • 12.
    opencypher.org | opencypher@googlegroups.com PATHPATTERN same_pet_name = (a)-[:KNOWS]-(b) WHERE EXISTS { MATCH (a)-[:OWNS]-(petA:Animal), (b)-[:OWNS]-(petB:Animal) WHERE petA.name = petB.name } MATCH peculiar_friends = (x)-[~same_pet_name*]-() Sophisticated data test
  • 13.
    opencypher.org | opencypher@googlegroups.com ●Differentiate between single edge and path MATCH (a)-[single:EDGE]-(and_a)-/~path*/-() ● Single edge matching allows variable binding ● Binding a path results in a whole path value bound Alternative syntax ideas
  • 14.
    opencypher.org | opencypher@googlegroups.com “Battletesting” ● Prototype implementation ● Try to express interesting queries ○ Needs to find / define interesting queries ● Measure up against “yardstick queries” ● Academic evaluation of ○ expressive power ○ computational complexity Next steps
  • 15.
    opencypher.org | opencypher@googlegroups.com thatI have not yet been able to express ● Paths where the property value differs in all nodes ○ quite possibly not tractable ● Paths where the property value in all subsequent nodes differs from the first node Yardstick queries