L8-S1 Datalog Queries 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San.

Slides:



Advertisements
Similar presentations
Relational Calculus and Datalog
Advertisements

Lecture 11: Datalog Tuesday, February 6, Outline Datalog syntax Examples Semantics: –Minimal model –Least fixpoint –They are equivalent Naive evaluation.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
1 Conjunctions of Queries. 2 Conjunctive Queries A conjunctive query is a single Datalog rule with only non-negated atoms in the body. (Note: No negated.
L3-S1 Practical Solutions 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
1 4. Datalog Queries Datalog query – a finite set of rules of the form: R 0 (x 1,…,x k ) :– R 1 (x 1,1,…, x 1,k 1 ),..., R n (x n,1,…, x n,k n ) where.
1 3. Relational Algebra and SQL Example: Let the following relations describe point sets: A(x, y), B(x, y), J(x, y) 2D points in the plane H(x, y, z),
L3-1-S1 OO Concepts © M.E. Fayad SJSU -- CMPE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room.
L3-5c-S1 Object Diagrams © M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department,
© M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José.
1 9. Evaluation of Queries Query evaluation – Quantifier Elimination and Satisfiability Example: Logical Level: r   y 1,…y n  r’ Constraint.
L16-S1 Object Diagrams 2003 SJSU -- CmpE Software Patterns Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
L28-S1 Interaction Diagrams 2003 SJSU -- CmpE Advanced Object-Oriented Analysis & Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room.
© M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José.
L19-S1 More on Class Diagrams 2003 SJSU -- CmpE Object-Oriented Analysis & Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I.
L21-S1 Model-Based Arch SJSU -- CmpE Advanced Object-Oriented Analysis & Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I.
© M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José.
L3-5d-S1 Class Diagrams © M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room.
L06-2-S1 Activity Diagrams 2003 SJSU -- CmpE Software Engineering II Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
© M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
P2-S1 MySQL 2003 SJSU -- CmpE-- M.E. Fayad Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
© M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
© M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José.
SJSU -- CmpE © Dr. M. E. Fayad Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San.
L5-S1 Class Diagrams 2003 SJSU -- CmpE Software Patterns Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San.
Chapter 5 Other Relational Languages By Cui, Can B.
L8-S1 CRC Cards 2003 SJSU -- CmpE Advanced Object-Oriented Analysis & Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College.
© M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José.
1999 – 2006 M.E. Fayad SJSU -- CmpE Software Engineering Management Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
Embedded SQL Direct SQL is rarely used: usually, SQL is embedded in some application code. We need some method to reference SQL statements. But: there.
2 Graphs and Functions Sections 2.2–2.3 © 2008 Pearson Addison-Wesley.
© M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
© M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
L3-6b-S1 Sequence Diagrams © M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room.
Introduction to Functions
4.4 Linear Inequalities in Two Variables
DEDUCTIVE DATABASE.
TH EDITION LIAL HORNSBY SCHNEIDER COLLEGE ALGEBRA.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 2 Graphs and Functions Copyright © 2013, 2009, 2005 Pearson Education, Inc.
Chapter 4.8: Determine if the Relation is a Function.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Datalog Inspired by the impedance mismatch in relational databases. Main expressive advantage: recursive queries. More convenient for analysis: papers.
Datalog –Another query language –cleaner – closer to a “logic” notation, prolog – more convenient for analysis – can express queries that are not expressible.
Advanced Relational Algebra & SQL (Part1 )
L8-2-S1 Misc Topics © M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of.
Mathematical Preliminaries
Functions and Their Representations
Relations. Important Definitions We covered all of these definitions on the board on Monday, November 7 th. Definition 1 Definition 2 Definition 3 Definition.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Row Types in SQL-3 Row types define types for tuples, and they can be nested. CREATE ROW TYPE AddressType{ street CHAR(50), city CHAR(25), zipcode CHAR(10)
L6-S1 MLPQ 2003 SJSU -- CmpE-- M.E. Fayad Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
Goal: Identify and graph functions..  Relation: mapping or pairing, of input values with output values.  Domain: Set of input values.  Range: set of.
Copyright © Cengage Learning. All rights reserved. Graphs; Equations of Lines; Functions; Variation 3.
L1-S1 Infinite R-DB 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San.
L12-S1 Spatiotmporal DB 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
Section 7.6 Functions Math in Our World. Learning Objectives  Identify functions.  Write functions in function notation.  Evaluate functions.  Find.
Advanced Object-Oriented Analysis & Design
Database Design Dr. M.E. Fayad, Professor
Component-Based & Software Reuse
Software System Engineering
Object-Oriented Analysis & Design
Schedule Today: Next And then Relational Algebra.
Relations and functions
Datalog Inspired by the impedance mismatch in relational databases.
Database Design Dr. M.E. Fayad, Professor
Database Design Dr. M.E. Fayad, Professor
Database Design Dr. M.E. Fayad, Professor
Software System Engineering
Object-Oriented Analysis & Design
Presentation transcript:

L8-S1 Datalog Queries 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José State University One Washington Square San José, CA

L8-S2 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad 2 Lesson 08: Datalog Queries

L8-S3 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Lesson Objectives 3 Understand datalog langauge Learn about: î Syntax of Datalog Rules î Datalog with Sets î Datalog with Abstract Data Types î Semantics î Recursive Datalog Queries

L8-S4 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Datalog is a rule-based language that is related to Prolog Each rule is a statement has some points that belong to some relations and other points must belong to a defined relation. Each Datalog query contains a Datalog program and an input database. 4 Datalog

L8-S5 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Datalog query – a finite set of rules of the form: R 0 (x 1,…,x k ) :– R 1 (x 1,1,…, x 1,k 1 ),..., R n (x n,1,…, x n,k n ) where each R i is either an input or a defined relation name. including built-in relations such as +(x,y,z) which means x + y = z. (We normally use the latter syntax.) The preceding rule is read “R 0 is true if R 1 and.. and R n are all true. head of the rule – R 0 body of the rule – R 1,…,R n 5 Datalog Queries: Syntax

L8-S6 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad [4.1.1] See Taxrecord and Taxtable (Page 8) Find the SSN and the tax. Tax_Due(s, t) :– Taxrecord(s, w, i, c), Taxtable(inc, t), w+i+c = inc. Requires one rule where Taxrecord and Taxtable are input database relations and Tax_Due is the only defined relation. 6 Datalog Queries: Syntax – Example (1)

L8-S7 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad [4.1.2] Suppose that relation Street (n, x, y) contains all combinations of street name n and locations (x, y) such that a location belongs to the street. Find the streets that can be reached from (x 0,y 0 ). Reach(n) :– Street(n, x 0, y 0 ). -- Rule 1 This rule says that street n is reachable if it contains the initial point. Reach(n) :– Reach(m), Street(m, x, y), Street(n, x, y). – Rule 2 This rule says that if m is reachable and m and n intersect on some point, than n is also reachable. 7 Datalog Queries: Syntax – Example (2)

L8-S8 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad [4.1.3] Find the time to travel from x to y. Travel(x, y, t) :– Go(x, 0, y, t). Travel(x, y, t) :– Travel(x, z, t 2 ), Go(z, t 2, y, t). The head defines the travel(x, y, t) relation, which is true if it is possible to travel from city x to city y in time t. 8 Datalog Queries: Syntax – Example (3)

L8-S9 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad [4.1.8] Find town points covered by a radio station or find every place in town that can e reached by at least one broadcast station. A kind of “map overlay problem” In this kind of problems, the scales and the points of the maps to be overlayed are not the same. Suppose that a relation Parameters(ID, Scale, X 0, Y 0 ) records for each map it scale and point of origin. 9 Datalog Queries: Syntax – Example (4)

L8-S10 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Suppose a town map where each point corresponds to a half kilometer, and in the broadcast map each point corresponds to a kilometer and the points of origin are the same for both maps. Then (1, 1, 0, 0), (2, 1, 0, 0), (3, 1, 0, 0), and (San Jose, 0.5, 0, 0) would be parameters relation. 10 Datalog Queries: Syntax – Example (4)

L8-S11 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad We can find the points in town that are covered using the following query Covered(x 2, y 2 ) :– Broadcast(n, x, y), Town(t, x 2, y 2 ), Parameters(n, s, blat, blong), Parameters(t, s 2, tlat, tlong), x 2 = s/ s 2 x + (tlat – blat), y 2 = s/ s 2 y + (tlong – blong). This query at first scales up and shift every point (x, y) in the broadcast map n to match the scale and point of the town map. If it corresponds to a point (x 2, y 2 ) in the town map, then it is added to relation Covered. 11 Datalog Queries: Syntax – Example (4)

L8-S12 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad 12 Datalog Queries: Syntax – Example (4)

L8-S13 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad We think of the domain of each attribute is a set. We can represent an undirected graph in two relations: Vertices and Edge (X 1, X 2 ) that contains a pair of singleton sets of city names iff there is an edge between them. For example: Edge ({San Jose}, {San Francisco}) would be one tuple in the Edge relation. 13 Datalog with Sets (1)

L8-S14 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Hamiltonian Cycle in An Undirected graph is a path that starts and ends with the same vertex and goes through each vertex exactly once. Assume Start (X) is an input relation where X is a singleton set containing the name of starting vertex of the cycle. Find a Hamiltonian Cycle where a set of vertices A not yet visited. 14 Datalog with Sets (2)

L8-S15 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Example: Hamiltonian Cycle Input: Vertices(S) where S is a set of vertices Edge ({c 1 }, {c 2 }) if there is an edge from c 1 to c 2 Start({c}) where c is start city name Output: Path ({c}, B) if there is a path from c that uses all vertices except those in B. Hamiltonian ({c}) if there is a Hamiltonian path. 15 Datalog with Sets (3)

L8-S16 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Base case – Path is a single vertex. All vertices except the start vertex is unvisited. Path(X 1, B) :– Vertices(A), Start(X 1 ), B = A \ X 1. Recursion – a path to X 1 with B unvisited exists if there is Path(X 1, B) : – Path(X 2, A), a path to X 2 with A unvisited Edge(X 2, X 1 ), and an edge from X 2 to X 1, X 1  A, which is unvisited, and B = A \ X 1. B is A minus X 1 16 Datalog with Sets (4)

L8-S17 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad If there is a path from start to X 2 that visits all vertices and an edge from X 2 to start, then there is a Hamiltonian cycle. Hamiltonian(X 1 ) :– Path(X 2,  ), Edge(X 2, X 1 ), Start(X 1 ). 17 Datalog with Sets (5)

L8-S18 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad The domain of each abstract data type ranges over some set S. For example, in the Street example, the Extend attribute was an abstract data type that ranged over sets of points in the plane over R 2 where R is the set of real numbers. With each abstract data type only certain operations are allowed and can be used with the query. 18 Datalog with Abstract Data Types (1)

L8-S19 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad For an abstract data type that ranges over points in the plane, all the basic set operations can be allowed: –equality (=) –containment  –intersection  –union U 19 Datalog with Abstract Data Types (2)

L8-S20 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Example: Streets(Name, Extent) where extent is a set of 2D points. Let (x 0, y 0 ) be a start location. Express the reach relation: Reach(n) :– Street(n, Extent), {( x 0,y 0 )}  Extent. Reach(n) :– Reach(m), Street(m, S 1 ), Street(n, S 2 ), S 1  S 2   20 Datalog with Abstract Data Types (3)

L8-S21 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Rule instantiation – substitution of variables by constants ⊢ Q,I R(a 1,…..a k ) – R(a 1,….a k ) has a proof using query Q and input database I, iff R represents input relation r and (a 1,….a k )  r, or There is some rule and instantiation R(a 1,…,a k ):–R 1 (a 1,1,…,a 1,k 1 ),…, R n (a n,1,…, a n, k n ). where ⊢ Q,I R i (a i,1, …,a i,k i ) for each 1  i  n. 21 Semantics (1)

L8-S22 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Reach(Vine) Reach(Vine) :– Street(Vine, 5, 2). -- from point (5, 2) Reach(Bear) Reach(Bear) :– Reach(Vine), Street(Vine, 5, 12), Street(Bear, 5, 12). -- (5, 12) is the intersection point Reach(Hare) Reach(Hare) :– Reach(Bear), Street(Bear, 8, 13), Street(Hare, 8, 13). 22 Semantics: Example (1)

L8-S23 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Example: By the input database (Figure 1.2): Go(Omaha, 0, Lincoln, 60) Go(Lincoln, 60, Kansas_City, 210) Go(Kansas_City, 210, Des_Moines, 390) Go(Des_Moines, 390, Chicago, 990) We also have: Travel(Omaha, Lincoln, 60) Travel(Omaha, Lincoln, 60):- Go(Omaha, 0, Lincoln, 60) Travel(Omaha, Kansas_City, 210) Travel(Omaha,Kansas_City,210):- Travel(Omaha,Lincoln,60), Go(Lincoln,60,Kansas_City,210). 23 Semantics: Example (2)

L8-S24 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad 24 Proof Trees (1)

L8-S25 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad Proof-based semantics – derived relations are the set of tuples that can be proven. Fixed point semantics – an interpretation of the derived relations such that nothing new can be proven. Least fixed point semantics – smallest possible FP semantics. Proof-based semantics = Least fixed point semantics 25 Proof Trees (2)

L8-S26 Datalog Queries 2003 SJSU – CmpE --- M.E. Fayad T/F 1.Datalog is an extension of SQL. 2.Datalog is a rule-based language and it is related to Prolog. 3.A fact is the same thing as a tuple in a relation or a row in an SQL table. 4.A rule is a way to derive new facts, that is, a rule is (part of) a query. 5.Datalog is more powerful than SQL3 since it permits a more general form of recursion. Discuss: Since we can do selection, projection, and join in Datalog just like in SQL and relational algebra, is there any difference among the three? 26 Discussion Questions