SlideShare a Scribd company logo
opencypher.orgopencypher.org | opencypher@googlegroups.com
Schema and Constraints
Mats Rydberg
mats@neotechnology.com
opencypher.orgopencypher.org | opencypher@googlegroups.com
● Cypher is schema-optional
● Fits well with heterogenous data
● Makes typing and query planning harder
● Does not fit well with many existing table-based engines
● Constraints are the only tools to enforce structure in the data
Schema in Cypher is a point where we expect there to be major
developments as more actors get involved.
Schema in Cypher
2
opencypher.orgopencypher.org | opencypher@googlegroups.com
● Consistent syntax for all types of constraints (CIP)
● Re-use as much as possible from the rest of the language
● Allow for a large set of future constraints, some of which are
vendor-specific
● This allows vendors to use more strict schema when necessary
CREATE CONSTRAINT <name>
FOR <simple pattern>
REQUIRE <constraint expression>
New constraint syntax
3
opencypher.orgopencypher.org | opencypher@googlegroups.com
● Property uniqueness constraint
➢ CREATE CONSTRAINT unique_person_names
FOR (p:Person)
REQUIRE UNIQUE p.firstName, p.lastName
● Property existence constraint
➢ CREATE CONSTRAINT person_must_have_firstName
FOR (p:Person)
REQUIRE exists(p.firstName)
Constraints, examples
4
opencypher.orgopencypher.org | opencypher@googlegroups.com
● Property value constraint
➢ CREATE CONSTRAINT roads_must_have_positive_finite_length
FOR ()-[r:ROAD]-()
REQUIRE 0 < r.distance < infinity
● Property type constraint
➢ CREATE CONSTRAINT people_schema
FOR (p:Person)
REQUIRE p.email IS STRING
REQUIRE p.name IS STRING?
REQUIRE p.age IS INTEGER?
Constraints, examples
5
opencypher.orgopencypher.org | opencypher@googlegroups.com
● Cardinality constraints
➢ CREATE CONSTRAINT spread_the_love
FOR (p:Person)
REQUIRE size((p)-[:LOVES]->()) > 3
● Endpoint constraints
➢ CREATE CONSTRAINT can_only_own_things
FOR ()-[:OWNS]->(t)
REQUIRE (t:Vehicle) OR (t:Building) OR (t:Object)
● Label coexistence constraints
➢ CREATE CONSTRAINT programmers_are_people_too
FOR (p:Programmer)
REQUIRE p:Person
Constraints, examples
6
opencypher.orgopencypher.org | opencypher@googlegroups.com
That's it!
Questions ?
opencypher.orgopencypher.org | opencypher@googlegroups.com
Subqueries
Petra Selmer
petra.selmer@neotechnology.com
opencypher.orgopencypher.org | opencypher@googlegroups.com
MATCH (actor:Actor)
WHERE EXISTS {
(actor)-[:ACTED_IN]->(movie),
(other:Actor)-[:ACTED_IN]->(movie)
WHERE other.name = actor.name
AND actor <> other
}
RETURN actor
Existential subqueries
CIP
9
opencypher.orgopencypher.org | opencypher@googlegroups.com
MATCH (f:Farm {id: $farmId})-[:IS_IN]->(country:Country)
MATCH {
MATCH (u:User {id: $userId})-[:LIKES]->(b:Brand)
RETURN b AS item, b.designedDate AS dateOfInterest
UNION
MATCH (u:User {id: $userId})-[:BOUGHT]->(v:Vehicle)
WHERE v.leftHandDrive = country.leftHandDrive
RETURN v AS item, v.manufacturedDate AS dateOfInterest
}
RETURN DISTINCT v.code
ORDER BY dateOfInterest
Nested correlated subqueries
CIP
10
opencypher.orgopencypher.org | opencypher@googlegroups.com
MATCH (r:Root)
DO {
UNWIND range(1, 10) AS x
MERGE (c:Child {id: x})
MERGE (r)-[:PARENT]->(c)
}
● UNWIND manages the looping
● DO suppresses the increased cardinality from the inner query
● DO hides all new variable bindings (e.g. c)
Nested read/write subqueries
CIP
11
opencypher.orgopencypher.org | opencypher@googlegroups.com
MATCH (actor:Actor)
WHERE actor.age > {
MATCH (director:Director)-[:DIRECTED]->(:Movie)
RETURN max(director.age)
}
RETURN actor.name, actor.details
Scalar subqueries
12
CIR
opencypher.orgopencypher.org | opencypher@googlegroups.com
That's it!
Questions ?
opencypher.orgopencypher.org | opencypher@googlegroups.com
Homomorphic and Isomorphic
Pattern Matching
Stefan Plantikow
stefan.plantikow@neotechnology.com
opencypher.orgopencypher.org | opencypher@googlegroups.com
• MATCH(a)-[r]->(b)
≡ { (a, r, b) V E V | r (a,b) }
• MATCH (a)-[r1]->(b)-[r2]->(c)
≡ { (a, r1
, b, r2
, c) V E V E V | r 1
(a,b) r 2
(b,c) r1
≠ r2
}
• MATCH (a)-[rs*]->(b)
≡ { (a, rs, b) V E*
V | rs *
(a,b) rs=[...,r1
...,r2
,...] r1
≠ r2
}
Basic Pattern Matching Semantics
15
opencypher.orgopencypher.org | opencypher@googlegroups.com
• Why?
• No uniqueness Potentially infinitely many matching paths
• Node uniqueness Can't match self-loops
• When?
• All pattern variables in a MATCH
• And similar constructs (comprehensions etc.)
• Optional?
• Not for variable length
Default Uniqueness = Relationship Uniqueness
16
opencypher.orgopencypher.org | opencypher@googlegroups.com
• CIR 2017-174
• Defines uniqueness scope precisely
• Add configurable uniqueness
• No uniqueness => Graph homomorphism
• Relationship uniqueness => Graph (relationship) isomorphism
• Node uniqueness => Graph (node) isomorphism
• Default uniqueness
• Cardinality & path matching semantics
Let's fix this: Requirements
17
opencypher.orgopencypher.org | opencypher@googlegroups.com
• CIP 2017-01-18
• Uniqueness mode specifiers
• MATCH ALL (a)-[r*]->(b)
• MATCH UNIQUE RELATIONSHIPS
• MATCH UNIQUE NODES
• Subquery uniqueness mode specifiers
• MATCH [MODE] { ... }
• DO [MODE] { ... }
Let's fix this: Proposal
18
opencypher.orgopencypher.org | opencypher@googlegroups.com
• CIP 2017-01-18: Path predicates
• MATCH ALL p=()-[:T1*]->()<-[r:T2]->() WHERE ...
• node-isomorphic nodeUnique(p)
• relationship-isomorphic relUnique(p)
• has cycles | has no cycles open(p)|closed(p)
• no immediately repeated rels trek(p)
• no repeated cycles NOT redundant(p)
• ...
Let's fix this: Proposal
19
opencypher.orgopencypher.org | opencypher@googlegroups.com
That's it!
Questions ?
opencypher.orgopencypher.org | opencypher@googlegroups.com
(Conjunctive) Regular Path Queries
“Regular expressions over Graphs”
Tobias Lindaaker
tobias@neotechnology.com
opencypher.orgopencypher.org | opencypher@googlegroups.com
We’ve been wanting to do this for long
Been trying to find well balanced syntax
22
opencypher.orgopencypher.org | opencypher@googlegroups.com
Regular Path Patterns
• Disjunctions (unions) between patterns:
(a)-/alt1 | alt2 | alt3/-(b)
• Sequence of patterns:
(a)-/first second third/->(b)
• Repetition of pattern:
(a)<-/(many times)*/-(b)
• Definition of complex pattern predicates:
MATCH (a)-/complex/->(b)
PATH (x)-/complex/->(y) IS
(x)-[:LOVES]->(y), (y)-[:HATES]->(x)
• Shorthand for simple predicates:
(a)-/:REGULAR_RELATIONSHIP_TYPE/->(b)
• Combinations of the above
23
(CIP2017-02-06)
opencypher.orgopencypher.org | opencypher@googlegroups.com
Origins of the syntax in CIP2017-02-06
“Canterbury Style”
(from a meeting in Canterbury, summer 2015)
● In-line pattern expression
MATCH p=(a:Person{a})[
(x)-[:AUTHORED]->(:Book)<-[:AUTHORED]-(y)
WHERE x <> y
]*(b:Person{b})
RETURN p ORDER BY length(p) ASC LIMIT 1
Pros
● Pattern visible where used
Cons
● Large patterns, hard to overview
24
“PGQL Style”
(inspired by the path patterns in PGQL)
● Composition of simple pattern declarations
PATH CO_AUTHORED :=
(x)-[:AUTHORED]->(:Book)<-[:AUTHORED]-(y)
WHERE x <> y
MATCH p=(a:Person{a})-/CO_AUTHORED*/-(b:Person{b})
RETURN p ORDER BY length(p) ASC LIMIT 1
Pros
● No nested syntax, each pattern is clear
● Allows reuse of complex sub-pattern
● Pattern definitions align with “path-views”
Cons
● Definition removed from use
● Everything needs to be declared to be used
● Unclear which nodes are input, and which are
internal to a PATH pattern
opencypher.orgopencypher.org | opencypher@googlegroups.com
Syntax tradeoffs
Desirable
• Pattern visible where used
• No unnecessary ceremony
• Transparent complexity
• Familiar regular language
• Allow reuse of complex
sub-patterns
Undesirable
• Lots of details in patterns
- hard to view structure
• Definition of pattern
removed from use
• Very many ways to express
the same thing
25
opencypher.orgopencypher.org | opencypher@googlegroups.com
Evolving from Cypher today
• Limited support today:
• Disjunction on edge label: ()-[:A|B|C]->()
• Repetition of single edge: ()-[:X*]->()
• unhelpful when binding: ()-[x:X*]->() - binds as list!
• (these two forms can be combined)
• Regular Path Patterns
replace existing repetition syntax
• For CRPQs (Conjunctive Regular Path Queries)
Cypher need only add Regular Path Patterns,
it already has conjunctions
26
opencypher.orgopencypher.org | opencypher@googlegroups.com
Expressive powerComparing to GXPath (path expressions)
• ⟦ε⟧G
- {(v,v) | v ∈ V} - yes: PATH (a)-/nop/-(a) IS (a)
• ⟦_⟧G
- {(v,w)| (v,a,w) ∈ E for some a}
- yes: PATH (a)-/any/->(b) IS (a)-->(b)
• ⟦a⟧G
- {(v,w)| (v,a,w) ∈ E} - yes: (v)-/:a/->(w)
• ⟦a-
⟧G
= {(v,w) | (w,a,v) ∈ E} - yes: (v)-/<:a/->(w)
• ⟦ɑ*⟧G
= reflexive transitive closure of ɑ
- yes: ()-/ɑ*/->()
• ⟦ɑ · β⟧G
= ⟦ɑ⟧G
⟦β⟧G
- yes: ()-/ɑ β/->()
• ⟦ɑ ∪ β⟧G
= ⟦ɑ⟧G
∪ ⟦β⟧G
- yes: ()-/ɑ|β/->()
• ⟦¬ɑ⟧G
= V V - ⟦ɑ⟧G
- maybe:
PATH (v)-/not_alpha/->(w) IS (v), (w)
WHERE NOT EXISTS { (v)-/ɑ/->(w) }
Warning: the above is intractable, we might want to restrict to connected patterns
• ⟦[φ]⟧G
= {(v,v) | v ∈ ⟦φ⟧G
}
- yes: PATH (v)-/phi/-(v) IS (v) WHERE φ
• ⟦ɑn,m
⟧G
= ⋃k=n
m
(⟦ɑ⟧G
)k - yes: ()-/ɑ*n..m/->()
• ⟦ɑ=
⟧G
= {(v,w) ∈ ⟦ɑ=
⟧G
| ρ(v)=ρ(w)}
- yes: PATH (v)-/alpha_eq/->(w) IS (v)-/ɑ/->(w) WHERE v.ρ = w.ρ
• ⟦ɑ≠
⟧G
= {(v,w) ∈ ⟦ɑ=
⟧G
| ρ(v)≠ρ(w)}
- yes: PATH (v)-/alpha_not_eq/->(w) IS (v)-/ɑ/->(w) WHERE v.ρ <> w.ρ
• Conjunctions (for CRPQs): ⟦ɑ ∩ β⟧G
= ⟦ɑ⟧G
∪ ⟦β⟧G
- yes: PATH (v)-/alpha_and_beta/->(w) IS (v)-/ɑ/->(w), (v)-/β/->(w)
2
7
27Querying Graphs with Data - L.Libkin, W.Martens, D.Vrgoč
opencypher.orgopencypher.org | opencypher@googlegroups.com
Expressive power
Comparing to GXPath (node tests)
• ⟦⟨ɑ⟩⟧G
= {v | ∃w (v,w) ∈ ⟦ɑ⟧G
}
- yes: PATH (v)-/has_alpha/-(v) IS (v)-/ɑ/->()
• ⟦¬φ⟧G
= V - ⟦φ⟧G
- yes: PATH (v)-/not_phi/-(v) IS (v) WHERE NOT φ
• ⟦φ ∧ ψ⟧G
= ⟦φ⟧G
∩ ⟦ψ⟧G
- yes: PATH (v)-/phi_and_psi/-(v) IS (v) WHERE φ AND ψ
• ⟦φ ∨ ψ⟧G
= ⟦φ⟧G
∪ ⟦ψ⟧G
- yes: PATH (v)-/phi_or_psi/-(v) IS (v) WHERE φ OR ψ
• ⟦c=
⟧G
= {v ∈ V | ρ(v) = c}
- yes: PATH (v)-/rho_is_c/-(v) IS (v) WHERE v.ρ = c
• ⟦c≠
⟧G
= {v ∈ V | ρ(v) ≠ c}
- yes: PATH (v)-/rho_is_c/-(v) IS (v) WHERE v.ρ <> c
• ⟦⟨ɑ = β⟩⟧G
= {v ∈ V | ∃w,y (v,w)∈⟦ɑ⟧G
, (v,y)∈⟦β⟧G
, ρ(w)=ρ(y)}
- yes: PATH (v)-/alpha_eq_beta/-(v) IS
(v)-/ɑ/->(w), (v)-/β/->(y) WHERE w.ρ = y.ρ
• ⟦⟨ɑ ≠ β⟩⟧G
= {v ∈ V | ∃w,y (v,w)∈⟦ɑ⟧G
, (v,y)∈⟦β⟧G
, ρ(w)≠ρ(y)}
- yes: PATH (v)-/alpha_not_eq_beta/-(v) IS
(v)-/ɑ/->(w), (v)-/β/->(y) WHERE w.ρ <> y.ρ
2
8
28Querying Graphs with Data - L.Libkin, W.Martens, D.Vrgoč
opencypher.orgopencypher.org | opencypher@googlegroups.com
Can we do even better?
• Shorthand syntax for node labels
• Shorthand syntax for matching any edge
• Shorthand syntax for property predicates
• What is the scope of PATH declarations?
• ...and where in the query should they go?
• Beginning of query?
• Sub-clause of MATCH?
• Anywhere in the query?
• Can we parameterize PATH declarations?
• ...and use parameters for both input and output...
2
9
29
opencypher.orgopencypher.org | opencypher@googlegroups.com
<Your suggestion here>
What other features would
you like to see in Cypher?

More Related Content

PDF
my$talk=qr{((?:ir)?reg(?:ular )?exp(?:ressions?)?)}i;
PDF
Introduction To Lisp
PDF
Towards Seed-Free Music Playlist Generation: Enhancing Collaborative Filterin...
PPTX
[Book Reading] 機械翻訳 - Section 5 No.2
PDF
Gremlin's Graph Traversal Machinery
PDF
Dependent dynamic rules
PDF
The Gremlin Graph Traversal Language
PDF
Introduction to R for Data Science :: Session 5 [Data Structuring: Strings in R]
my$talk=qr{((?:ir)?reg(?:ular )?exp(?:ressions?)?)}i;
Introduction To Lisp
Towards Seed-Free Music Playlist Generation: Enhancing Collaborative Filterin...
[Book Reading] 機械翻訳 - Section 5 No.2
Gremlin's Graph Traversal Machinery
Dependent dynamic rules
The Gremlin Graph Traversal Language
Introduction to R for Data Science :: Session 5 [Data Structuring: Strings in R]

What's hot (20)

PPTX
R language
PDF
Cypher.PL: an executable specification of Cypher semantics
PDF
Introduction to R for Data Science :: Session 8 [Intro to Text Mining in R, M...
PDF
R Programming: Learn To Manipulate Strings In R
PDF
Introduction to R for Data Science :: Session 6 [Linear Regression in R]
PDF
Incremental View Maintenance for openCypher Queries
PDF
Introduction to R Programming
PDF
R Programming: Export/Output Data In R
PDF
Introduction to R for Data Science :: Session 7 [Multiple Linear Regression i...
PDF
Ejercicios de estilo en la programación
PPT
Rtutorial
PPTX
Data analysis with R
PDF
Beyond tf idf why, what & how
PPTX
2. R-basics, Vectors, Arrays, Matrices, Factors
PDF
Grokking regex
PDF
R Programming: Transform/Reshape Data In R
PDF
OPTEX Mathematical Modeling and Management System
PPT
Hash presentation
PDF
Grouping & Summarizing Data in R
PDF
SQL window functions for MySQL
R language
Cypher.PL: an executable specification of Cypher semantics
Introduction to R for Data Science :: Session 8 [Intro to Text Mining in R, M...
R Programming: Learn To Manipulate Strings In R
Introduction to R for Data Science :: Session 6 [Linear Regression in R]
Incremental View Maintenance for openCypher Queries
Introduction to R Programming
R Programming: Export/Output Data In R
Introduction to R for Data Science :: Session 7 [Multiple Linear Regression i...
Ejercicios de estilo en la programación
Rtutorial
Data analysis with R
Beyond tf idf why, what & how
2. R-basics, Vectors, Arrays, Matrices, Factors
Grokking regex
R Programming: Transform/Reshape Data In R
OPTEX Mathematical Modeling and Management System
Hash presentation
Grouping & Summarizing Data in R
SQL window functions for MySQL
Ad

Similar to Future features for openCypher: Schema, Constraints, Subqueries, Configurable Pattern Matching, Conjunctive regular Path Queries (20)

PDF
Path Pattern Queries: Introducing Regular Path Queries in openCypher
PDF
MLconf NYC Shan Shan Huang
PDF
PGQL: A Language for Graphs
PDF
Cypher.PL: Executable Specification of Cypher written in Prolog
PPT
Implementation of 'go-like' language constructions in scala [english version]
PDF
Regular expression for everyone
PDF
Scoped dynamic rewrite rules
PDF
Search and Replacement Techniques in Emacs: avy, swiper, multiple-cursor, ag,...
PDF
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
PDF
Introduction to Groovy (Serbian Developer Conference 2013)
PDF
Extending lifespan with Hadoop and R
PDF
Neal Gafter Java Evolution
PDF
Introductionto fp with groovy
PPTX
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
PDF
Greg Hogan – To Petascale and Beyond- Apache Flink in the Clouds
ODP
Introduction to R
PPT
Groovy presentation
PDF
Incremental View Maintenance for openCypher Queries
PDF
Configurable Pattern Matching Semantics in openCypher: Defining Levels of Nod...
PDF
Pune Clojure Course Outline
Path Pattern Queries: Introducing Regular Path Queries in openCypher
MLconf NYC Shan Shan Huang
PGQL: A Language for Graphs
Cypher.PL: Executable Specification of Cypher written in Prolog
Implementation of 'go-like' language constructions in scala [english version]
Regular expression for everyone
Scoped dynamic rewrite rules
Search and Replacement Techniques in Emacs: avy, swiper, multiple-cursor, ag,...
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Introduction to Groovy (Serbian Developer Conference 2013)
Extending lifespan with Hadoop and R
Neal Gafter Java Evolution
Introductionto fp with groovy
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Greg Hogan – To Petascale and Beyond- Apache Flink in the Clouds
Introduction to R
Groovy presentation
Incremental View Maintenance for openCypher Queries
Configurable Pattern Matching Semantics in openCypher: Defining Levels of Nod...
Pune Clojure Course Outline
Ad

More from openCypher (20)

PDF
Learning Timed Automata with Cypher
PDF
Formal semantics for Cypher queries and updates
PDF
Multiple Graphs: Updatable Views
PDF
Micro-Servicing Linked Data
PDF
Graph abstraction
PDF
From Cypher 9 to GQL: Conceptual overview of multiple named graphs and compos...
PDF
Cypher for Gremlin
PDF
Comparing PGQL, G-Core and Cypher
PDF
Multiple graphs in openCypher
PDF
Eighth openCypher Implementers Group Meeting: Status Update
PDF
Cypher for Gremlin
PDF
Supporting dates and times in Cypher
PDF
Seventh openCypher Implementers Group Meeting: Status Update
PDF
Academic research on graph processing: connecting recent findings to industri...
PDF
Property Graphs with Time
PDF
Use case: processing multiple graphs
PDF
openCypher Technology Compatibility Kit (TCK)
PDF
Cypher Editor in the Web
PDF
The inGraph project and incremental evaluation of Cypher queries
PDF
Formal Specification of Cypher
Learning Timed Automata with Cypher
Formal semantics for Cypher queries and updates
Multiple Graphs: Updatable Views
Micro-Servicing Linked Data
Graph abstraction
From Cypher 9 to GQL: Conceptual overview of multiple named graphs and compos...
Cypher for Gremlin
Comparing PGQL, G-Core and Cypher
Multiple graphs in openCypher
Eighth openCypher Implementers Group Meeting: Status Update
Cypher for Gremlin
Supporting dates and times in Cypher
Seventh openCypher Implementers Group Meeting: Status Update
Academic research on graph processing: connecting recent findings to industri...
Property Graphs with Time
Use case: processing multiple graphs
openCypher Technology Compatibility Kit (TCK)
Cypher Editor in the Web
The inGraph project and incremental evaluation of Cypher queries
Formal Specification of Cypher

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
Teaching material agriculture food technology
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Cloud computing and distributed systems.
PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
KodekX | Application Modernization Development
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Teaching material agriculture food technology
sap open course for s4hana steps from ECC to s4
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectral efficient network and resource selection model in 5G networks
Cloud computing and distributed systems.
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
Electronic commerce courselecture one. Pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KodekX | Application Modernization Development
Digital-Transformation-Roadmap-for-Companies.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
MYSQL Presentation for SQL database connectivity
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Future features for openCypher: Schema, Constraints, Subqueries, Configurable Pattern Matching, Conjunctive regular Path Queries

  • 1. opencypher.orgopencypher.org | opencypher@googlegroups.com Schema and Constraints Mats Rydberg mats@neotechnology.com
  • 2. opencypher.orgopencypher.org | opencypher@googlegroups.com ● Cypher is schema-optional ● Fits well with heterogenous data ● Makes typing and query planning harder ● Does not fit well with many existing table-based engines ● Constraints are the only tools to enforce structure in the data Schema in Cypher is a point where we expect there to be major developments as more actors get involved. Schema in Cypher 2
  • 3. opencypher.orgopencypher.org | opencypher@googlegroups.com ● Consistent syntax for all types of constraints (CIP) ● Re-use as much as possible from the rest of the language ● Allow for a large set of future constraints, some of which are vendor-specific ● This allows vendors to use more strict schema when necessary CREATE CONSTRAINT <name> FOR <simple pattern> REQUIRE <constraint expression> New constraint syntax 3
  • 4. opencypher.orgopencypher.org | opencypher@googlegroups.com ● Property uniqueness constraint ➢ CREATE CONSTRAINT unique_person_names FOR (p:Person) REQUIRE UNIQUE p.firstName, p.lastName ● Property existence constraint ➢ CREATE CONSTRAINT person_must_have_firstName FOR (p:Person) REQUIRE exists(p.firstName) Constraints, examples 4
  • 5. opencypher.orgopencypher.org | opencypher@googlegroups.com ● Property value constraint ➢ CREATE CONSTRAINT roads_must_have_positive_finite_length FOR ()-[r:ROAD]-() REQUIRE 0 < r.distance < infinity ● Property type constraint ➢ CREATE CONSTRAINT people_schema FOR (p:Person) REQUIRE p.email IS STRING REQUIRE p.name IS STRING? REQUIRE p.age IS INTEGER? Constraints, examples 5
  • 6. opencypher.orgopencypher.org | opencypher@googlegroups.com ● Cardinality constraints ➢ CREATE CONSTRAINT spread_the_love FOR (p:Person) REQUIRE size((p)-[:LOVES]->()) > 3 ● Endpoint constraints ➢ CREATE CONSTRAINT can_only_own_things FOR ()-[:OWNS]->(t) REQUIRE (t:Vehicle) OR (t:Building) OR (t:Object) ● Label coexistence constraints ➢ CREATE CONSTRAINT programmers_are_people_too FOR (p:Programmer) REQUIRE p:Person Constraints, examples 6
  • 9. opencypher.orgopencypher.org | opencypher@googlegroups.com MATCH (actor:Actor) WHERE EXISTS { (actor)-[:ACTED_IN]->(movie), (other:Actor)-[:ACTED_IN]->(movie) WHERE other.name = actor.name AND actor <> other } RETURN actor Existential subqueries CIP 9
  • 10. opencypher.orgopencypher.org | opencypher@googlegroups.com MATCH (f:Farm {id: $farmId})-[:IS_IN]->(country:Country) MATCH { MATCH (u:User {id: $userId})-[:LIKES]->(b:Brand) RETURN b AS item, b.designedDate AS dateOfInterest UNION MATCH (u:User {id: $userId})-[:BOUGHT]->(v:Vehicle) WHERE v.leftHandDrive = country.leftHandDrive RETURN v AS item, v.manufacturedDate AS dateOfInterest } RETURN DISTINCT v.code ORDER BY dateOfInterest Nested correlated subqueries CIP 10
  • 11. opencypher.orgopencypher.org | opencypher@googlegroups.com MATCH (r:Root) DO { UNWIND range(1, 10) AS x MERGE (c:Child {id: x}) MERGE (r)-[:PARENT]->(c) } ● UNWIND manages the looping ● DO suppresses the increased cardinality from the inner query ● DO hides all new variable bindings (e.g. c) Nested read/write subqueries CIP 11
  • 12. opencypher.orgopencypher.org | opencypher@googlegroups.com MATCH (actor:Actor) WHERE actor.age > { MATCH (director:Director)-[:DIRECTED]->(:Movie) RETURN max(director.age) } RETURN actor.name, actor.details Scalar subqueries 12 CIR
  • 14. opencypher.orgopencypher.org | opencypher@googlegroups.com Homomorphic and Isomorphic Pattern Matching Stefan Plantikow stefan.plantikow@neotechnology.com
  • 15. opencypher.orgopencypher.org | opencypher@googlegroups.com • MATCH(a)-[r]->(b) ≡ { (a, r, b) V E V | r (a,b) } • MATCH (a)-[r1]->(b)-[r2]->(c) ≡ { (a, r1 , b, r2 , c) V E V E V | r 1 (a,b) r 2 (b,c) r1 ≠ r2 } • MATCH (a)-[rs*]->(b) ≡ { (a, rs, b) V E* V | rs * (a,b) rs=[...,r1 ...,r2 ,...] r1 ≠ r2 } Basic Pattern Matching Semantics 15
  • 16. opencypher.orgopencypher.org | opencypher@googlegroups.com • Why? • No uniqueness Potentially infinitely many matching paths • Node uniqueness Can't match self-loops • When? • All pattern variables in a MATCH • And similar constructs (comprehensions etc.) • Optional? • Not for variable length Default Uniqueness = Relationship Uniqueness 16
  • 17. opencypher.orgopencypher.org | opencypher@googlegroups.com • CIR 2017-174 • Defines uniqueness scope precisely • Add configurable uniqueness • No uniqueness => Graph homomorphism • Relationship uniqueness => Graph (relationship) isomorphism • Node uniqueness => Graph (node) isomorphism • Default uniqueness • Cardinality & path matching semantics Let's fix this: Requirements 17
  • 18. opencypher.orgopencypher.org | opencypher@googlegroups.com • CIP 2017-01-18 • Uniqueness mode specifiers • MATCH ALL (a)-[r*]->(b) • MATCH UNIQUE RELATIONSHIPS • MATCH UNIQUE NODES • Subquery uniqueness mode specifiers • MATCH [MODE] { ... } • DO [MODE] { ... } Let's fix this: Proposal 18
  • 19. opencypher.orgopencypher.org | opencypher@googlegroups.com • CIP 2017-01-18: Path predicates • MATCH ALL p=()-[:T1*]->()<-[r:T2]->() WHERE ... • node-isomorphic nodeUnique(p) • relationship-isomorphic relUnique(p) • has cycles | has no cycles open(p)|closed(p) • no immediately repeated rels trek(p) • no repeated cycles NOT redundant(p) • ... Let's fix this: Proposal 19
  • 21. opencypher.orgopencypher.org | opencypher@googlegroups.com (Conjunctive) Regular Path Queries “Regular expressions over Graphs” Tobias Lindaaker tobias@neotechnology.com
  • 22. opencypher.orgopencypher.org | opencypher@googlegroups.com We’ve been wanting to do this for long Been trying to find well balanced syntax 22
  • 23. opencypher.orgopencypher.org | opencypher@googlegroups.com Regular Path Patterns • Disjunctions (unions) between patterns: (a)-/alt1 | alt2 | alt3/-(b) • Sequence of patterns: (a)-/first second third/->(b) • Repetition of pattern: (a)<-/(many times)*/-(b) • Definition of complex pattern predicates: MATCH (a)-/complex/->(b) PATH (x)-/complex/->(y) IS (x)-[:LOVES]->(y), (y)-[:HATES]->(x) • Shorthand for simple predicates: (a)-/:REGULAR_RELATIONSHIP_TYPE/->(b) • Combinations of the above 23 (CIP2017-02-06)
  • 24. opencypher.orgopencypher.org | opencypher@googlegroups.com Origins of the syntax in CIP2017-02-06 “Canterbury Style” (from a meeting in Canterbury, summer 2015) ● In-line pattern expression MATCH p=(a:Person{a})[ (x)-[:AUTHORED]->(:Book)<-[:AUTHORED]-(y) WHERE x <> y ]*(b:Person{b}) RETURN p ORDER BY length(p) ASC LIMIT 1 Pros ● Pattern visible where used Cons ● Large patterns, hard to overview 24 “PGQL Style” (inspired by the path patterns in PGQL) ● Composition of simple pattern declarations PATH CO_AUTHORED := (x)-[:AUTHORED]->(:Book)<-[:AUTHORED]-(y) WHERE x <> y MATCH p=(a:Person{a})-/CO_AUTHORED*/-(b:Person{b}) RETURN p ORDER BY length(p) ASC LIMIT 1 Pros ● No nested syntax, each pattern is clear ● Allows reuse of complex sub-pattern ● Pattern definitions align with “path-views” Cons ● Definition removed from use ● Everything needs to be declared to be used ● Unclear which nodes are input, and which are internal to a PATH pattern
  • 25. opencypher.orgopencypher.org | opencypher@googlegroups.com Syntax tradeoffs Desirable • Pattern visible where used • No unnecessary ceremony • Transparent complexity • Familiar regular language • Allow reuse of complex sub-patterns Undesirable • Lots of details in patterns - hard to view structure • Definition of pattern removed from use • Very many ways to express the same thing 25
  • 26. opencypher.orgopencypher.org | opencypher@googlegroups.com Evolving from Cypher today • Limited support today: • Disjunction on edge label: ()-[:A|B|C]->() • Repetition of single edge: ()-[:X*]->() • unhelpful when binding: ()-[x:X*]->() - binds as list! • (these two forms can be combined) • Regular Path Patterns replace existing repetition syntax • For CRPQs (Conjunctive Regular Path Queries) Cypher need only add Regular Path Patterns, it already has conjunctions 26
  • 27. opencypher.orgopencypher.org | opencypher@googlegroups.com Expressive powerComparing to GXPath (path expressions) • ⟦ε⟧G - {(v,v) | v ∈ V} - yes: PATH (a)-/nop/-(a) IS (a) • ⟦_⟧G - {(v,w)| (v,a,w) ∈ E for some a} - yes: PATH (a)-/any/->(b) IS (a)-->(b) • ⟦a⟧G - {(v,w)| (v,a,w) ∈ E} - yes: (v)-/:a/->(w) • ⟦a- ⟧G = {(v,w) | (w,a,v) ∈ E} - yes: (v)-/<:a/->(w) • ⟦ɑ*⟧G = reflexive transitive closure of ɑ - yes: ()-/ɑ*/->() • ⟦ɑ · β⟧G = ⟦ɑ⟧G ⟦β⟧G - yes: ()-/ɑ β/->() • ⟦ɑ ∪ β⟧G = ⟦ɑ⟧G ∪ ⟦β⟧G - yes: ()-/ɑ|β/->() • ⟦¬ɑ⟧G = V V - ⟦ɑ⟧G - maybe: PATH (v)-/not_alpha/->(w) IS (v), (w) WHERE NOT EXISTS { (v)-/ɑ/->(w) } Warning: the above is intractable, we might want to restrict to connected patterns • ⟦[φ]⟧G = {(v,v) | v ∈ ⟦φ⟧G } - yes: PATH (v)-/phi/-(v) IS (v) WHERE φ • ⟦ɑn,m ⟧G = ⋃k=n m (⟦ɑ⟧G )k - yes: ()-/ɑ*n..m/->() • ⟦ɑ= ⟧G = {(v,w) ∈ ⟦ɑ= ⟧G | ρ(v)=ρ(w)} - yes: PATH (v)-/alpha_eq/->(w) IS (v)-/ɑ/->(w) WHERE v.ρ = w.ρ • ⟦ɑ≠ ⟧G = {(v,w) ∈ ⟦ɑ= ⟧G | ρ(v)≠ρ(w)} - yes: PATH (v)-/alpha_not_eq/->(w) IS (v)-/ɑ/->(w) WHERE v.ρ <> w.ρ • Conjunctions (for CRPQs): ⟦ɑ ∩ β⟧G = ⟦ɑ⟧G ∪ ⟦β⟧G - yes: PATH (v)-/alpha_and_beta/->(w) IS (v)-/ɑ/->(w), (v)-/β/->(w) 2 7 27Querying Graphs with Data - L.Libkin, W.Martens, D.Vrgoč
  • 28. opencypher.orgopencypher.org | opencypher@googlegroups.com Expressive power Comparing to GXPath (node tests) • ⟦⟨ɑ⟩⟧G = {v | ∃w (v,w) ∈ ⟦ɑ⟧G } - yes: PATH (v)-/has_alpha/-(v) IS (v)-/ɑ/->() • ⟦¬φ⟧G = V - ⟦φ⟧G - yes: PATH (v)-/not_phi/-(v) IS (v) WHERE NOT φ • ⟦φ ∧ ψ⟧G = ⟦φ⟧G ∩ ⟦ψ⟧G - yes: PATH (v)-/phi_and_psi/-(v) IS (v) WHERE φ AND ψ • ⟦φ ∨ ψ⟧G = ⟦φ⟧G ∪ ⟦ψ⟧G - yes: PATH (v)-/phi_or_psi/-(v) IS (v) WHERE φ OR ψ • ⟦c= ⟧G = {v ∈ V | ρ(v) = c} - yes: PATH (v)-/rho_is_c/-(v) IS (v) WHERE v.ρ = c • ⟦c≠ ⟧G = {v ∈ V | ρ(v) ≠ c} - yes: PATH (v)-/rho_is_c/-(v) IS (v) WHERE v.ρ <> c • ⟦⟨ɑ = β⟩⟧G = {v ∈ V | ∃w,y (v,w)∈⟦ɑ⟧G , (v,y)∈⟦β⟧G , ρ(w)=ρ(y)} - yes: PATH (v)-/alpha_eq_beta/-(v) IS (v)-/ɑ/->(w), (v)-/β/->(y) WHERE w.ρ = y.ρ • ⟦⟨ɑ ≠ β⟩⟧G = {v ∈ V | ∃w,y (v,w)∈⟦ɑ⟧G , (v,y)∈⟦β⟧G , ρ(w)≠ρ(y)} - yes: PATH (v)-/alpha_not_eq_beta/-(v) IS (v)-/ɑ/->(w), (v)-/β/->(y) WHERE w.ρ <> y.ρ 2 8 28Querying Graphs with Data - L.Libkin, W.Martens, D.Vrgoč
  • 29. opencypher.orgopencypher.org | opencypher@googlegroups.com Can we do even better? • Shorthand syntax for node labels • Shorthand syntax for matching any edge • Shorthand syntax for property predicates • What is the scope of PATH declarations? • ...and where in the query should they go? • Beginning of query? • Sub-clause of MATCH? • Anywhere in the query? • Can we parameterize PATH declarations? • ...and use parameters for both input and output... 2 9 29
  • 30. opencypher.orgopencypher.org | opencypher@googlegroups.com <Your suggestion here> What other features would you like to see in Cypher?