36
Cypher Query Language Andrés Taylor and Alistair Jones FOSDEM 2012 Graph Processing Room 5 Feb 2012 Wednesday, February 8, 2012

Cypher Query Language

Embed Size (px)

DESCRIPTION

Cypher is a declarative query language recently added to the Neo4j graph database. In this talk we explain the motivations for creating this language, describe its current features and design, and dive a little into its implementation. Finally, we discuss the future direction of how we expect the language to evolve.

Citation preview

Page 1: Cypher Query Language

Cypher Query LanguageAndrés Taylor and Alistair Jones

FOSDEM 2012Graph Processing Room5 Feb 2012

Wednesday, February 8, 2012

Page 2: Cypher Query Language

What is Cypher?

• Graph Query Language for Neo4j

• Aims to make querying simple

Wednesday, February 8, 2012

Page 3: Cypher Query Language

Motivation

Something new?

• Existing Neo4j query mechanisms were not simple enough

• Too verbose (Java API)

• Too prescriptive (Gremlin)

Wednesday, February 8, 2012

Page 4: Cypher Query Language

Motivation

SQL?

• Unable to express paths

• these are crucial for graph-based reasoning

• neo4j is schema/table free

Wednesday, February 8, 2012

Page 5: Cypher Query Language

Motivation

SPARQL?

• SPARQL designed for a different data model

• namespaces

• properties as nodes

Wednesday, February 8, 2012

Page 6: Cypher Query Language

Design

Wednesday, February 8, 2012

Page 7: Cypher Query Language

Design Decisions

DeclarativeImperative Declarative

follow relationshipbreadth-first vs depth-first

explicit algorithm

specify starting pointspecify desired outcome

algorithm adaptablebased on query

Wednesday, February 8, 2012

Page 8: Cypher Query Language

Design Decisions

Pattern matching

Wednesday, February 8, 2012

Page 9: Cypher Query Language

Design Decisions

Pattern matching

A

B C

Wednesday, February 8, 2012

Page 10: Cypher Query Language

Design Decisions

Pattern matchingA

B C

Wednesday, February 8, 2012

Page 11: Cypher Query Language

Design Decisions

Pattern matchingA

B C

Wednesday, February 8, 2012

Page 12: Cypher Query Language

Design Decisions

Pattern matchingA

B C

Wednesday, February 8, 2012

Page 13: Cypher Query Language

Design Decisions

Pattern matchingA

B C

Wednesday, February 8, 2012

Page 14: Cypher Query Language

Design Decisions

ASCII-art patterns

Wednesday, February 8, 2012

Page 15: Cypher Query Language

Design Decisions

ASCII-art patterns

() --> ()

Wednesday, February 8, 2012

Page 16: Cypher Query Language

Design Decisions

ASCII-art patterns

A B

Wednesday, February 8, 2012

Page 17: Cypher Query Language

Design Decisions

ASCII-art patterns

(A) --> (B)A B

Wednesday, February 8, 2012

Page 18: Cypher Query Language

Design Decisions

ASCII-art patterns

A BLOVES

Wednesday, February 8, 2012

Page 19: Cypher Query Language

Design Decisions

ASCII-art patterns

A -[:LOVES]-> B

A BLOVES

Wednesday, February 8, 2012

Page 20: Cypher Query Language

Design Decisions

ASCII-art patterns

A B C

Wednesday, February 8, 2012

Page 21: Cypher Query Language

Design Decisions

ASCII-art patterns

A --> B --> CA B C

Wednesday, February 8, 2012

Page 22: Cypher Query Language

Design Decisions

ASCII-art patterns

A

B C

Wednesday, February 8, 2012

Page 23: Cypher Query Language

Design Decisions

ASCII-art patterns

A --> B --> C, A --> C

A

B C

Wednesday, February 8, 2012

Page 24: Cypher Query Language

Design Decisions

ASCII-art patterns

A --> B --> C, A --> C

A

B C

A --> B --> C <-- AWednesday, February 8, 2012

Page 25: Cypher Query Language

Design Decisions

Variable length paths

A B

A B

A B

...

Wednesday, February 8, 2012

Page 26: Cypher Query Language

Design Decisions

Variable length paths

A -[*]-> B

A B

A B

A B

...

Wednesday, February 8, 2012

Page 27: Cypher Query Language

Design Decisions

Optional relationships

A B

Wednesday, February 8, 2012

Page 28: Cypher Query Language

Design Decisions

Optional relationships

A -[?]-> BA B

Wednesday, February 8, 2012

Page 29: Cypher Query Language

Design Decisions

Closures

start london = node(1), moscow = node(2)match path = london -[*]-> moscowwhere all(city in nodes(path) where city.capital = true)

Wednesday, February 8, 2012

Page 30: Cypher Query Language

Design Decisions

Parsed, not an internal DSL

Execution Semantics

Type System

Serialisation

Portability

Wednesday, February 8, 2012

Page 31: Cypher Query Language

Design Decisions

Familiar for SQL users

selectfrom

wheregroup byorder by

startmatchwherereturn

Wednesday, February 8, 2012

Page 32: Cypher Query Language

Implementation

Wednesday, February 8, 2012

Page 33: Cypher Query Language

Implementation

Execution Plan

start n=node(0)return n

Parameters()Nodes(n)Extract([n])ColumnFilter([n])

Wednesday, February 8, 2012

Page 34: Cypher Query Language

Implementation

Execution Plan

start n=node(0)match n-[*]-> b return n.name, n, count(*) order by n.age

Parameters()Nodes(n)PatternMatch(n-[*]->b)Extract([n.name, n])EagerAggregation( keys: [n.name, n], aggregates: [count(*)])Extract([n.age])Sort(n.age ASC)ColumnFilter([n.name,n,count(*)])

Wednesday, February 8, 2012

Page 35: Cypher Query Language

Implementation

Execution Plan

start n=node(0) match n-[*]-> b return n.name, n, count(*) order by n.name

Parameters()Nodes(n)PatternMatch(n-[*]->b)Extract([n.name, n])Sort(n.name ASC,n ASC)EagerAggregation( keys: [n.name, n], aggregates: [count(*)])ColumnFilter([n.name,n,count(*)])

Wednesday, February 8, 2012

Page 36: Cypher Query Language

Thanks for Listening!

Questions?Andrés Taylor [email protected] @andres_taylor

Alistair Jones [email protected] @apcj

Wednesday, February 8, 2012