Datalog Recursive SQL LogicBlox

Similar documents
Plan of the lecture. G53RDB: Theory of Relational Databases Lecture 14. Example. Datalog syntax: rules. Datalog query. Meaning of Datalog rules

Lecture 1: Conjunctive Queries

Logic As a Query Language. Datalog. A Logical Rule. Anatomy of a Rule. sub-goals Are Atoms. Anatomy of a Rule

Datalog. Susan B. Davidson. CIS 700: Advanced Topics in Databases MW 1:30-3 Towne 309

Lecture 9: Datalog with Negation

Datalog. Susan B. Davidson. CIS 700: Advanced Topics in Databases MW 1:30-3 Towne 309

Recap and Schedule. Till Now: Today: Ø Query Languages: Relational Algebra; SQL. Ø Datalog A logical query language. Ø SQL Recursion.

Ontology and Database Systems: Foundations of Database Systems

CMPS 277 Principles of Database Systems. Lecture #11

Database Theory: Beyond FO

Announcements. Database Systems CSE 414. Datalog. What is Datalog? Why Do We Learn Datalog? HW2 is due today 11pm. WQ2 is due tomorrow 11pm

DATABASE THEORY. Lecture 12: Evaluation of Datalog (2) TU Dresden, 30 June Markus Krötzsch

DATABASE THEORY. Lecture 15: Datalog Evaluation (2) TU Dresden, 26th June Markus Krötzsch Knowledge-Based Systems

Foundations of Databases

Announcements. What is Datalog? Why Do We Learn Datalog? Database Systems CSE 414. Midterm. Datalog. Lecture 13: Datalog (Ch

Logical Query Languages. Motivation: 1. Logical rules extend more naturally to. recursive queries than does relational algebra. Used in SQL recursion.

Data Integration: Logic Query Languages

Midterm. Database Systems CSE 414. What is Datalog? Datalog. Why Do We Learn Datalog? Why Do We Learn Datalog?

Introduction to Data Management CSE 344. Lecture 14: Datalog (guest lecturer Dan Suciu)

Database Systems CSE 414. Lecture 9-10: Datalog (Ch )

Datalog. Rules Programs Negation

Range Restriction for General Formulas

Chapter 5: Other Relational Languages.! Query-by-Example (QBE)! Datalog

DATABASE THEORY. Lecture 11: Introduction to Datalog. TU Dresden, 12th June Markus Krötzsch Knowledge-Based Systems

Midterm. Introduction to Data Management CSE 344. Datalog. What is Datalog? Why Do We Learn Datalog? Why Do We Learn Datalog? Lecture 13: Datalog

Program Analysis in Datalog

CSE 344 JANUARY 26 TH DATALOG

Chapter 5: Other Relational Languages

I Relational Database Modeling how to define

Annoucements. Where are we now? Today. A motivating example. Recursion! Lecture 21 Recursive Query Evaluation and Datalog Instructor: Sudeepa Roy

Conjunctive queries. Many computational problems are much easier for conjunctive queries than for general first-order queries.

Database Theory VU , SS Codd s Theorem. Reinhard Pichler

10.3 Recursive Programming in Datalog. While relational algebra can express many useful operations on relations, there

A Retrospective on Datalog 1.0

Datalog Evaluation. Linh Anh Nguyen. Institute of Informatics University of Warsaw

}Optimization Formalisms for recursive queries. Module 11: Optimization of Recursive Queries. Module Outline Datalog

}Optimization. Module 11: Optimization of Recursive Queries. Module Outline

Advances in Data Management Beyond records and objects A.Poulovassilis

CSE 344 JANUARY 29 TH DATALOG

Introduction to Data Management CSE 344. Lecture 14: Datalog

Datalog Evaluation. Serge Abiteboul. 5 mai 2009 INRIA. Serge Abiteboul (INRIA) Datalog Evaluation 5 mai / 1

FOUNDATIONS OF DATABASES AND QUERY LANGUAGES

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Foundations of Databases

Data Integration: Datalog

Relational Databases

I Relational Database Modeling how to define

CSE 544: Principles of Database Systems

Relational Model, Relational Algebra, and SQL

Midterm Review. Winter Lecture 13

Deductive Databases. Motivation. Datalog. Chapter 25

FOUNDATIONS OF SEMANTIC WEB TECHNOLOGIES

Logical reasoning systems

Chapter 6: Bottom-Up Evaluation

Database Theory: Datalog, Views

Three Query Language Formalisms

Introduction to Data Management CSE 344

Three Query Language Formalisms

Logical Aspects of Massively Parallel and Distributed Systems

CSE 344 MAY 7 TH EXAM REVIEW

A Simple SQL Injection Pattern

Towards a Logical Reconstruction of Relational Database Theory

Database Theory VU , SS Introduction to Datalog. Reinhard Pichler. Institute of Logic and Computation DBAI Group TU Wien

Cooperating Languages - Core Language

CMPS 277 Principles of Database Systems. Lecture #4

CMP-3440 Database Systems

Logical Query Languages. Motivation: 1. Logical rules extend more naturally to. recursive queries than does relational algebra.

SQL Recursion, Window Queries

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

Encyclopedia of Database Systems, Editors-in-chief: Özsu, M. Tamer; Liu, Ling, Springer, MAINTENANCE OF RECURSIVE VIEWS. Suzanne W.

INTRODUCTION TO PROLOG

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

Fundamentals of Database Systems

Introduction to Finite Model Theory. Jan Van den Bussche Universiteit Hasselt

Chap 6: Spatial Networks

1. Introduction; Selection, Projection. 2. Cartesian Product, Join. 5. Formal Definitions, A Bit of Theory

Induction and Semantics in Dafny

Automatic Reasoning (Section 8.3)

Schema Mappings and Data Exchange

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

3. Relational Data Model 3.5 The Tuple Relational Calculus

Can We Trust SQL as a Data Analytics Tool?

Datalog. S. Costantini

Denotational Semantics. Domain Theory

Optimization of logical query plans Eliminating redundant joins

Incomplete Information: Null Values

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 6 Outline. Unary Relational Operations: SELECT and

Informationslogistik Unit 4: The Relational Algebra

Chapter 4: SQL. Basic Structure

A SQL-Middleware Unifying Why and Why-Not Provenance for First-Order Queries

Other Relational Languages

Negation wrapped inside a recursion makes no. separated, there can be ambiguity about what. the rules mean, and some one meaning must

Knowledge-Based Systems and Deductive Databases

CSE20: Discrete Mathematics

Optimization of Nested Queries in a Complex Object Model

Principles of Database Systems CSE 544. Lecture #2 SQL, Relational Algebra, Relational Calculus

Logic and Databases. Phokion G. Kolaitis. UC Santa Cruz & IBM Research - Almaden

Database Systems SQL SL03

Other Relational Languages

Lecture 5 Design Theory and Normalization

Transcription:

CS 500: Fundamentals of Databases Datalog Recursive SQL LogicBlox supplementary material: Foundations of Databases by Abiteboul, Hull, Vianu, Ch. 12 class notes slides are based on teaching materials by Serge Abiteboul and Jeff Ullman (stoyanovich@drexel.edu)

Transitive closure of a graph Graph G, represented by an edge relation G(x, y) 0" 1" 4" 5" 8" 9" 2" 3" 6" 7" 10" 11" Q: Is there a path from 0 to 11? Can we write this in SQL? 2

Datalog basics P(x, y) : G(x, y) P(x, y) : G(x,z),P(z, y) Q() : P(0,11) G stands for edges P stands for paths Q stands for query Datalog stands for database logic, syntax is similar to Prolog, but there are important differences Programs are sets or rules, with 1 head relation predicate and 0 or more body relation predicates Read a rule as if body then head, e.g., if there is an edge from x to y, then there is a path from x to y Predicates in the body are also called subgoals, rule body is a conjunction (logical and) of its subgoals A program has a distinguished rule called query - this is what the program computes, other rules are in support of that computation 3

Datalog basics P(x, y) : G(x, y) P(x, y) : G(x,z),P(z, y) Q() : P(0,11) I :{G(0,1), G(1,2), G(2,3)} tuples are called facts A relation is extensional (edb) if it does not appear in the head of any rule A relation is intensional (idb) if it appears in the head of at least 1 rule Distinguishing feature of datalog - support for recursion. Most common case of recursion is when a relation appears the head and in the body of the same rule Not all datalog programs are recursive (will see many non-recursive programs today) We will look at set semantics of datalog, bag semantics is also possible 4

A(x, y) : B(x, y) Datalog by example A(x, y) : C(x, y,_,_) T () : A(10,z) Variables are local If a variable is not reused, we don t have to name it (use _ ) Arithmetic predicates are allowed in the body in addition to relational predicates Unlike in Prolog: function symbols are not supported Q(x, y) : R(x,z), S(z, y), x < y P(x, x) : E(x,_) x, y, z are variables 10 is a constant data is separate from the program, a program executes on different edb instances (its input), maps finite edb instances to finite idb instances (its output) 5

Safety Every variable that appears anywhere in the rule must appear in a positive relational subgoal of the body (We focus on positive datalog (no negation) for most of this class, but will illustrate some issues related to negation) V(x, y) : P(x) W (x, y) : P(x), y > x Q(x, y) : R(x,z), S(z, y), T (x, y) U(x, y) : R(x,z),S(z, y),t (x,w) Why this condition? safe? explain! 6

Understanding datalog rules P(x, y) Q(x,z), R(z, y), Q(x, y) I :{Q(1,2), Q(1,3), R(2,3), R(3,1)} Q(x,z) R(z, y) Consistent? Q(x, y) P(x, y) (1,2) (2,3) TRUE FALSE null (1,2) (3,1) FALSE N/A null (1,3) (2,3) FALSE N/A null (1,3) (3,1) TRUE TRUE (1,1) 7

Fixpoint semantics A fact A is an immediate consequence for instance I and program P if 1. A is an extensional fact in I or, 2. for some instantiation of a rule A :- A 1,, A n in P, and each A 1,, A n is in I. T P (K), the immediate consequence operator, is monotone That is, the output of each subsequent round is a subset of the output of the previous round. When it is no longer a proper subset (no new facts are derived), the program terminates. 8

Example 12.3.3 graph G: Recall the program P TC for computing the transitive closure of a T (x, y) G(x, y) T (x, y) G(x, z), T (z, y). Consider the input instance I ={G(1, 2), G(2, 3), G(3, 4), G(4, 5)}. Then we have T PTC (I) = I {T(1, 2), T (2, 3), T (3, 4), T (4, 5)} TP 2 TC (I) = T PTC (I) {T(1, 3), T (2, 4), T (3, 5)} TP 3 TC (I) = TP 2 TC (I) {T(1, 4), T (2, 5)} TP 4 TC (I) = TP 3 TC (I) {T(1, 5)} TP 5 TC (I) = TP 4 TC (I). Thus TP ω TC (I) = TP 4 TC (I). 9

P(x, y) : G(x, y) Naive evaluation P(x, y) : G(x,z),P(z, y) Algorithm Naive: Given a set of rules and an edb I (1)Initialize all idb relations as empty I :{G(0,1), G(1,2), G(2,3)} (2)Instantiate (with constants) variables of all rules in all possible ways. If all subgoals become true - we are inferring new fact(s) for the head relation (3)Repeat (2) in rounds, as long as new idb facts are being inferred Step (2) makes sense for finite edb and safe rules - each variable appears in some non-negated relational subgoal, this limits the active domain Steps (1) - (3) converges to the least fixed point of the program and the edb, we ll elaborate on this 10

P(x, y) : G(x, y) Naive evaluation P(x, y) : G(x,z),P(z, y) Algorithm Naive: Given a set of rules and an edb I (1)Initialize all idb relations as empty I :{G(0,1), G(1,2), G(2,3)} (2)Instantiate (with constants) variables of all rules in all possible ways. If all subgoals become true - we are inferring new fact(s) for the head relation (3)Repeat (2) in rounds, as long as new idb facts are being inferred 3 rounds to convergence init : P(x, y) = round 1: P(0,1) P(1, 2) P(2, 3) round 2 : P(0,1) P(1, 2) P(2, 3)P(0, 2) P(1, 3) round 3: P(0,1) P(1, 2) P(2, 3)P(0, 2) P(1, 3) P(0, 3) 11

Example: Paris subway Concorde Tuileries Palais-Royal Louvre Chatelet 1 St.-Germain Odeon St.-Michel 4 Pont de Sevres Billancourt Michel-Ange 9 Voltaire Republique FDR Iena Links(line, station,next _ station) 12

Additional examples Links(line, station,next _ station) What are the stations reachable from Odeon? - directed graph version St _ reachable(x, y) : Links(_, x, y) St _ reachable(x, y) : Links(_, x,z),st _ reachable(z, y) Q(y) : St _ reachable('odeon', y) What are the stations reachable from Odeon? - undirected graph version St _ reachable(x, y) : Links(_, x, y) St _ reachable(x, y) : Links(_, y, x) St _ reachable(x, y) : St _ reachable(x,z),st _ reachable(z, y), x y Q(y) : St _ reachable('odeon', y) 13

Additional examples Links(line, station,next _ station) What are the lines reachable from Odeon? - directed graph version St _ reachable(x, y) : Links(_, x, y) St _ reachable(x, y) : Links(_, x,z),st _ reachable(z, y) Li _ reachable(x,u) : Links(u, x,_) Li _ reachable(x,u) : Links(u,_, x) Li _ reachable(x,u) : St _ reachable(x, y), Li _ reachable(y,u) Q(u) : Li _ reachable('odeon',u) 14

Additional examples Links(line, station,next _ station) What are the lines reachable from Odeon? - undirected graph version St _ reachable(x, y) : Links(_, x, y) St _ reachable(x, y) : Links(_, y, x) St _ reachable(x, y) : St _ reachable(x,z),st _ reachable(z, y), x y Li _ reachable(x,u) : Links(u, x,_) Li _ reachable(x,u) : Links(u,_, x) Li _ reachable(x,u) : St _ reachable(x, y), Li _ reachable(y,u) Q(u) : Li _ reachable('odeon',u) 15

Additional examples Can we go from Odeon to Chatelet? Q() : St _ reachable('odeon','chatelet ') Are all pairs of stations connected? All _ pairs(x, y) : Links(_, x,_), Links(_,_, y), x y Q() : All _ pairs(x, y),st _ reachable(x, y) Both programs can run on either the directed or the undirected version of St_reachable. Is there a cycle in the graph? - directed graph version only Q() : St _ reachable(x, x) 16

Relational algebra vs. datalog Any relational algebra expression can be expressed in datalog - we ll see how to do this now Any (one) datalog rule can be expressed in relational algebra A multi-rule datalog program is strictly more powerful than relational algebra, since rules are allowed to interact - recursion A syntactic difference: in datalog we do not refer to attributes by name, only by position (this is logic programming syntax, since a relation with attributes is viewed as a predicate with arguments) Both relational algebra and datalog work with sets, although both can be extended to bags We are working with positive datalog here, and positive relational algebra (no set difference) 17

Relational algebra vs. Datalog R(A, B,C) σ A>3 (R) π A,B (R) S(A, B,C) Q(x, y,z) : R(x, y,z), x > 3 Q(x, y) : R(x, y,_) R I S R U S R S R >< S Q(x, y,z) : R(x, y,z),s(x, y,z) Q(x, y,z) : R(x, y,z) Q(x, y,z) : S(x, y,z) Q(u,v,w, x, y,z) : R(u,v,w),S(x, y,z) Q(x, y,z) : R(x, y,z),s(x, y,z) 18

Monotonicity Recall fixpoint semantics: convergence depends on monotonicity of T P (K), the immediate consequence operator That is, the output of each subsequent round is a superset of the output of the previous round. When it is no longer a proper superset (no new facts are derived), the program terminates Monotonicity of positive datalog can be shown based on monotonicity of union, select, project, product, join operators in relational algebra. With this, and using induction on the number of rounds, one can show that naive datalog evaluation produces the least fixed point, i.e., the minimal set of facts provable from the edb by the program 19

Negation Why is negation problematic: negative relational subgoals in datalog? When combined with recursion, negation introduces nonmonotone behavior, i.e., the program may not converge to a unique fixed point. Example: a fact from R is either in P or in Q, but not in both P(x) : R(x),Q(x) Q(x) : R(x),P(x) I = {R(0)} round P Q {(0)} {(0)} {(0)} {(0)} aggregation + recursion can also lead to non-monotone behavior 1 2 3 4 20

Recursive SQL The WITH statement in SQL allows to define temporary relations (recursive or not) To write recursive WITH statements, use the RECURSIVE keyword and use the temporary relation within the statement that defines it Example: Flights relation below, what cities are reachable from San Francisco (SFO) create table Flights( airline char(2), src char(3), dest char(3), departs time, arrives time, primary key (airline, src, dest, departs) ); R(x, y) : F(_, x, y,_,_) R(x, y) : R(x,z), R(z, y) Q(x, y) : R('SFO', y) with recursive Reaches(src, dest) as ( select src, dest from Flights union select R1.src, R2.dest from Reaches R1, Reaches R2 where R1.dest = R2.src) select * from Reaches; ERROR: recursive reference to query "reaches" must not appear more than once LINE 5: from Reaches R1, Reaches R2 21

Recursive SQL The WITH statement in SQL allows to define temporary relations (recursive or not) To write recursive WITH statements, use the RECURSIVE keyword and use the temporary relation within the statement that defines it Example: Flights relation below, what cities are reachable from San Francisco (SFO) create table Flights( airline char(2), src char(3), dest char(3), departs time, arrives time, primary key (airline, src, dest, departs) ); src dest -----+------ SFO DEN SFO DAL SFO NYC SFO CHI (4 rows) 22 R(x, y) : F(_, x, y,_,_) R(x, y) : R(x,z),F(z, y) Q(x, y) : R('SFO', y) with recursive Reaches(src, dest) as ( select src, dest from Flights union select R.src, F.dest from Reaches R, Flights F where R.dest = F.src) select * from Reaches where src = 'SFO';

Recursion S(x, y) : P(x,z),P(y,z), x y C(x, y) : P(x, xp),p(y, yp),s(xp, yp) C(x, y) := P(x, xp), P(y, yp),c(xp, yp) R(x, y) : S(x, y) R(x, y) : R(x,z),P(y,z) R(x, y) : R(z, y),p(x,z) Draw a dependency graph to identify cycles 2 cycles: R and C; these predicates are recursive, S and P are not P must be non-recursive, why? R S P C 23

Dependency graphs P(x, y) : G(x, y) P(x, y) : G(x,z),P(z, y) P G P(x) : R(x),Q(x) Q(x) : R(x),P(x) P Q R P and Q are mutually recursive SQL only supports linear recursion: no rule has more than 1 term subgoal that is mutually recursive with the head. (That includes self-loops.) The reason is efficiency, not semantics, i.e., monotonicity is not a problem. 24

Dependency graphs SQL only supports linear recursion: no rule has more than 1 term subgoal that is mutually recursive with the head. (That includes self-loops.) R(x, y) : F(_, x, y,_,_) R(x, y) : R(x,z), R(z, y) Q(x, y) : R('SFO', y) Q R F R(x, y) : F(_, x, y,_,_) R is recursive R(x, y) : R(x,z),F(z, y) Q(x, y) : R('SFO', y) 25

Recall: Naive evaluation P(x, y) : G(x, y) P(x, y) : G(x,z),P(z, y) I :{G(0,1), G(1,2), G(2,3)} Algorithm Naive: Given a set of rules and an edb I (1)Initialize all idb relations as empty (2)Instantiate (with constants) variables of all rules in all possible ways. If all subgoals become true - we are inferring new fact(s) for the head relation (3)Repeat (2) in rounds, as long as new idb facts are being inferred 3 rounds to convergence init : P(x, y) = round 1: P(0,1) P(1, 2) P(2, 3) round 2 : P(0,1) P(1, 2) P(2, 3)P(0, 2) P(1, 3) round 3: P(0,1) P(1, 2) P(2, 3)P(0, 2) P(1, 3) P(0, 3) Very inefficient: all idb facts are re-derived on each round! 26

Datalog evaluation Naive: bottom-up, derives new facts from facts that hold during the previous rounds Semi-naive: bottom-up, more efficient than naive, attempts to avoid re-computation from round to round Query-subquery (QSQ): top-down, related to the proof-theoretic semantics of datalog, which we did not discuss Magic set: bottom-up 27

Semi-naive evaluation Intuition: if a fact was derived for the first time at round i, it must depend on some fact or facts derived at round i-1 For each idb predicate over relation R, keep both R and delta-r Deltas represent new facts inferred on the most recent round Algorithm Semi-naive: Given a set of rules and an edb I (1)Initialize (a) idb relations using only rules without idb subgoals; and (b) delta-idb relations to be equal to the corresponding idb relations (2)For each idb relation R, and for each rule with R in the head, compute delta-r with one subgoal treated as a delta relation, others as regular relations (do for all possible choices of the delta-subgoal) (3)Remove from new delta-r all facts that are already in R (4) Compute R :- R U delta-r (5)Repeat (2-4) in rounds, as long as new idb facts are being inferred 28

3 0 Semi-naive: example P(x, y) : G(x, y) P(x, y) : G(x,z),P(z, y) P = G 1 ΔP = G Iterate until ΔP = ΔP = π 1,3 (G >< ΔP) ΔP = ΔP P P = ΔP U P 2 round G P delta-p init 1 2 3 (0,1) (1,2) (2,3) (0,1) (1,2) (2,3) (0,1) (1,2) (2,3) (0,1) (1,2) (2,3) (0,1) (1,2) (2,3) (0,1) (1,2) (2,3) (0,2) (1,3) (0,1) (1,2) (2,3) (0,2) (1,3) (0,3) (0,1) (1,2) (2,3) (0,2) (1,3) (0,3) (0,1) (1,2) (2,3) (0,2) (1,3) (0,3) 29

Semi-naive: another example R(x, y) : S(x, y) R(x, y) : T (x, y,_) R(x, y) : R(x,z), R(z, y) round S T R delta-r init (0,1) (1,2) (0,1,3) (2,5,3) (0,1) (1,2) (2,5) (0,1) (1,2) (2,5) Init : R = S Uπ 1,2 T ΔR = S Uπ 1,2 T 1 (0,1) (1,2) (0,1,3) (2,5,3) (0,1) (0,2) (1,2) (1,5) (2,5) (0,2) (1,5) Iterate until ΔR = ΔR = π 1,3 ((ΔR >< R) U (R >< ΔR)) ΔR = ΔR R R = ΔR U R 2 3 (0,1) (1,2) (0,1) (1,2) (0,1,3) (2,5,3) (0,1,3) (2,5,3) (0,1) (0,2) (0,5) (1,2) (1,5) (2,5) (0,1) (0,2) (0,5) (1,2) (1,5) (2,5) (0,5) 30

Datalog: why do we care? A beautiful set of problems / solutions Support for (a limited form) of recursion in SQL, triggers - convergence questions More recently: a true revival Declara've networking [e.g. Lou et al] Data integra'on and exchange [e.g. Clio, Orchestra] Program verifica'on [e.g. Semmle] Data extrac'on from the Web [e.g. GoElob, Lixto] Knowledge representa'on [e.g. GoElob] Ar'fact and workflows [e.g. Ac'veXML] Web data management [e.g. Webdamlog] LogicBlox - will see next! 31

LogicBlox www.logicblox.com https://developer.logicblox.com/playground/ https://developer.logicblox.com/content/docs4/core-reference/webhelp 32

LogiQL A dialect of datalog Relations are in 6NF- at most one non-key attribute per relation Predicates of the form R(x 1,.., x n ) or R[x 1,, x n-1 ] = x n both can be base predicates (extensional, EDB) or derived predicates (intensional, IDB) more on the distinction between R(x 1,.., x n ) and R[x 1,, x n-1 ] = x n next Derivation rules specify how facts are calculated support for arithmetic operations and aggregation Integrity constraints specify types of attributes, types are declared (better!) or inferred functional dependencies inclusion constraints 33

LogiQL basic predicate declaration: key attributes only person(x) -> string(x). functional predicate declaration: one non-key attribute (cal) food[name, cat] = cal -> string(name), string(cat), int(cal). functional dependencies friend_of(x,y) -> string(x), string(y). friend_of(x,y) -> person(x), person(y). person_age[x] = n -> string(x), int(n). person_age(x,_) -> person(x). 34

LogiQL derived predicates friend_of_friend(x,y) -> string(x), string(y). friend_of_friend(x,y) -> person(x), person(y). friend_of_friend(x,y) <- friend_of(x,p), friend_of(p,y), x!= y. <- in LogiQL means the same as :- in datalog arithmetic operations older_friend[x,y] = d -> string(x), string(y), int(d). older_friend(x,y,_) -> person(x), person(y). older_friend(x,y,d) <- friend_of(x, y), person_age(x,n), person_age(y,m), n > m, d = n - m. aggregation number_of_friends(x,n) -> string(x), int(n). number_of_friends(x,_) -> person(x). number_of_friends[x] = n <- agg << n = count() >> friend_of(x,_). 35

LogiQL adding tuples to an EDB person(x) -> string(x). +person("ann"). +person("bob"). +person("cory"). +person("dave"). +person("emma"). friend_of(x,y) -> string(x), string(y). friend_of(x,y) -> person(x), person(y). +friend_of("ann","bob"). +friend_of( Ann","Cory"). +friend_of("cory","dave"). +friend_of("dave","emma"). person_age[x] = n -> string(x), int(n). person_age(x,_) -> person(x). +person_age("ann",30). +person_age("bob",25). +person_age("cory",28). +person_age("dave",22). +person_age("emma",18). 36

LogiQL executing commands https://developer.logicblox.com/playground/ addblock 'person(x) -> string(x).' addblock ' friend_of(x,y) -> string(x), string(y). friend_of(x,y) -> person(x), person(y). ' exec ' +person("ann"). +person("bob"). +person("cory"). +person("dave"). +person("emma"). ' print friend_of print person_age print older_friend 37