Path Query Patterns CIP2017-02-06 a.k.a. (Conjunctive) Regular Path - - PowerPoint PPT Presentation

path query patterns
SMART_READER_LITE
LIVE PREVIEW

Path Query Patterns CIP2017-02-06 a.k.a. (Conjunctive) Regular Path - - PowerPoint PPT Presentation

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 opencypher.org |


slide-1
SLIDE 1
  • pencypher.org | opencypher@googlegroups.com
  • pencypher.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

slide-2
SLIDE 2
  • pencypher.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]+]->()

slide-3
SLIDE 3
  • pencypher.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

slide-4
SLIDE 4
  • pencypher.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

slide-5
SLIDE 5
  • pencypher.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

slide-6
SLIDE 6
  • pencypher.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

slide-7
SLIDE 7
  • pencypher.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

slide-8
SLIDE 8
  • pencypher.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

slide-9
SLIDE 9
  • pencypher.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

slide-10
SLIDE 10
  • pencypher.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

slide-11
SLIDE 11
  • pencypher.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

slide-12
SLIDE 12
  • pencypher.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

slide-13
SLIDE 13
  • pencypher.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

slide-14
SLIDE 14
  • pencypher.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

slide-15
SLIDE 15
  • pencypher.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