Variable Length Relationship Pattern Extensions Teon Banek March , - - PowerPoint PPT Presentation

variable length relationship pattern extensions
SMART_READER_LITE
LIVE PREVIEW

Variable Length Relationship Pattern Extensions Teon Banek March , - - PowerPoint PPT Presentation

Variable Length Relationship Pattern Extensions Teon Banek March , cbnd About Me Teon Banek Graduated from University of Zagreb, Faculty of Electrical Engineering and Computing Lead query engine developer at Memgraph


slide-1
SLIDE 1

Variable Length Relationship Pattern Extensions

Teon Banek March , cbnd

slide-2
SLIDE 2

About Me

Teon Banek

  • Graduated from University of Zagreb, Faculty of Electrical Engineering and

Computing

  • Lead query engine developer at Memgraph
  • Loves fencing, lasagne and black tea
  • teon.banek@memgraph.com

Variable Length Relationship Pattern Extensions — About

  • f
slide-3
SLIDE 3

About Us

Memgraph Ltd.

  • Startup, founded in 6
  • Building a graph database
  • In-memory
  • High-performance
  • Distributed
  • https://memgraph.com

Variable Length Relationship Pattern Extensions — About

  • f
slide-4
SLIDE 4

Contents

  • About
  • Filtering
  • Traversal Strategies
  • Conclusion

Variable Length Relationship Pattern Extensions — About

  • f
slide-5
SLIDE 5

Relationship Pattern Syntax

  • MATCH ()-[var? types? variableLength? properties?]-()
  • types = ':' name ( '|' types )?
  • variableLength = '*' min_bound? ( '..' max_bound )?
  • properties = '{' ( key_name ':' value )* '}'

Variable Length Relationship Pattern Extensions — About

  • f
slide-6
SLIDE 6

MATCH ()-[rs:FriendOf *2..3 {years: 3}]->()

Variable Length Relationship Pattern Extensions — About 6 of

slide-7
SLIDE 7

MATCH ()-[rs:FriendOf *2..3 {years: 3}]->()

Variable Length Relationship Pattern Extensions — About 6 of

slide-8
SLIDE 8

Standard Filtering

  • We can filter on relationship type & property equality

What if want arbitrary expression predicate?

We can traverse the obtained list. For example: ALL(r IN rs WHERE r.years > 2)

What if we want to filter on traversed nodes?

Still possible, we have access to whole path. But, the query can get complicated.

Variable Length Relationship Pattern Extensions — Filtering

  • f
slide-9
SLIDE 9

Standard Filtering

  • We can filter on relationship type & property equality
  • What if want arbitrary expression predicate?

We can traverse the obtained list. For example: ALL(r IN rs WHERE r.years > 2)

What if we want to filter on traversed nodes?

Still possible, we have access to whole path. But, the query can get complicated.

Variable Length Relationship Pattern Extensions — Filtering

  • f
slide-10
SLIDE 10

Standard Filtering

  • We can filter on relationship type & property equality
  • What if want arbitrary expression predicate?
  • We can traverse the obtained list.
  • For example: ALL(r IN rs WHERE r.years > 2)

What if we want to filter on traversed nodes?

Still possible, we have access to whole path. But, the query can get complicated.

Variable Length Relationship Pattern Extensions — Filtering

  • f
slide-11
SLIDE 11

Standard Filtering

  • We can filter on relationship type & property equality
  • What if want arbitrary expression predicate?
  • We can traverse the obtained list.
  • For example: ALL(r IN rs WHERE r.years > 2)
  • What if we want to filter on traversed nodes?
  • Still possible, we have access to whole path.
  • But, the query can get complicated.

Variable Length Relationship Pattern Extensions — Filtering

  • f
slide-12
SLIDE 12

Filter Lambda

  • For example

MATCH ()-[rs * (next_r, next_n | next_r.years > 2)]-()

  • Essentially ALL embedded in pattern syntax

MATCH ()-[rs *]-() WHERE ALL(next_r IN rs WHERE next_r.years > 2) Lambda syntax '(' rel_var ',' node_var '|' expr ')' rel_var — next relationship that we are about to traverse node_var — next node that we are about to reach expr — arbitrary expression which when evaluated produces true if we want to continue traversing

Variable Length Relationship Pattern Extensions — Filtering 8 of

slide-13
SLIDE 13

Filter Lambda

  • For example

MATCH ()-[rs * (next_r, next_n | next_r.years > 2)]-()

  • Essentially ALL embedded in pattern syntax

MATCH ()-[rs *]-() WHERE ALL(next_r IN rs WHERE next_r.years > 2)

  • Lambda syntax '(' rel_var ',' node_var '|' expr ')'
  • rel_var — next relationship that we are about to traverse
  • node_var — next node that we are about to reach
  • expr — arbitrary expression which when evaluated produces true if we want to

continue traversing

Variable Length Relationship Pattern Extensions — Filtering 8 of

slide-14
SLIDE 14

MATCH (:Alice)-[rs:FriendOf * (r, n | r.years + n.age > 34)]->()

Variable Length Relationship Pattern Extensions — Filtering

  • f
slide-15
SLIDE 15

MATCH (:Alice)-[rs:FriendOf * (r, n | r.years + n.age > 34)]->()

Variable Length Relationship Pattern Extensions — Filtering

  • f
slide-16
SLIDE 16

MATCH (:Alice)-[rs:FriendOf * (r, n | r.years + n.age > 34)]->()

Variable Length Relationship Pattern Extensions — Filtering

  • f
slide-17
SLIDE 17

Filter Lambda Conclusions

  • We can match using arbitrary expression on each upcoming node and

relationship.

  • Covers the most common use case of regular expression like patterns.
  • There’s still the issue of filtering based on remote parts of the traversed paths.

Variable Length Relationship Pattern Extensions — Filtering

  • f
slide-18
SLIDE 18

Depth-First Search

  • Variable length expansion essentially performs a depth-first search.
  • Each result is a list of relationships forming the current step of the algorithm.

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-19
SLIDE 19

MATCH (:Alice)-[*]->(:Daniel)

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-20
SLIDE 20

MATCH (:Alice)-[*]->(:Daniel)

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-21
SLIDE 21

MATCH (:Alice)-[*]->(:Daniel)

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-22
SLIDE 22

MATCH (:Alice)-[*]->(:Daniel)

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-23
SLIDE 23

Breadth-First Search

  • One of the more common graph use cases is finding shortest paths.
  • Breadth-first search seems like a logical addition.
  • Syntax remains the same, but to enable the algorithm just append bfs to *.
  • MATCH ()-[var? types? '*bfs' properties? filterLambda?]-()

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-24
SLIDE 24

MATCH (:Alice)-[*bfs]->(:Daniel)

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-25
SLIDE 25

MATCH (:Alice)-[*bfs]->(:Daniel)

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-26
SLIDE 26

BFS Conclusion

  • BFS is nice and simple.
  • What about path cost determined by something other than a relationship

traversal?

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-27
SLIDE 27

Weighted Shortest Path Search

  • Natural extension is assigning weights to relationships.
  • Algorithm is enabled by appending wShortest to *.
  • We also need syntax for tracking the weight.

MATCH ()-[var? types? '*wShortest' properties? weightLambda weightVar filterLambda?]-() weightLambda — just like filterLambda but needs to produce a positive number as the current weight. weightVar — stores the final weight of the path.

Variable Length Relationship Pattern Extensions — Traversal Strategies 6 of

slide-28
SLIDE 28

Weighted Shortest Path Search

  • Natural extension is assigning weights to relationships.
  • Algorithm is enabled by appending wShortest to *.
  • We also need syntax for tracking the weight.
  • MATCH ()-[var? types? '*wShortest' properties?

weightLambda weightVar filterLambda?]-()

  • weightLambda — just like filterLambda but needs to produce a positive

number as the current weight.

  • weightVar — stores the final weight of the path.

Variable Length Relationship Pattern Extensions — Traversal Strategies 6 of

slide-29
SLIDE 29

MATCH (:Alice)-[*wShortest (r, n | r.years) total_weight]-(:Daniel)

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-30
SLIDE 30

MATCH (:Alice)-[*wShortest (r, n | r.years) total_weight]-(:Daniel)

Variable Length Relationship Pattern Extensions — Traversal Strategies

  • f
slide-31
SLIDE 31

Conclusion

  • Syntactic additions are tied with pattern matching.
  • No special functions doing pattern matching outside of patterns.
  • This avoids surprising users.

Implementation maps naturally to how we do pattern matching.

Variable Length Relationship Pattern Extensions — Conclusion 8 of

slide-32
SLIDE 32

Conclusion

  • Syntactic additions are tied with pattern matching.
  • No special functions doing pattern matching outside of patterns.
  • This avoids surprising users.
  • Implementation maps naturally to how we do pattern matching.

Variable Length Relationship Pattern Extensions — Conclusion 8 of

slide-33
SLIDE 33

Thank You

  • Thank you for you attention!
  • Any questions?

Variable Length Relationship Pattern Extensions — Conclusion

  • f