A Deductive Database with Datalog and SQL Query Languages
Total Page:16
File Type:pdf, Size:1020Kb
A Deductive Database with Datalog and SQL Query Languages FernandoFernando SSááenzenz PPéérez,rez, RafaelRafael CaballeroCaballero andand YolandaYolanda GarcGarcííaa--RuizRuiz GrupoGrupo dede ProgramaciProgramacióónn DeclarativaDeclarativa (GPD)(GPD) UniversidadUniversidad ComplutenseComplutense dede MadridMadrid (Spain)(Spain) 12/5/2011 APLAS 2011 1 ~ ContentsContents 1.1. IntroductionIntroduction 2.2. QueryQuery LanguagesLanguages 3.3. IntegrityIntegrity ConstraintsConstraints 4.4. DuplicatesDuplicates 5.5. OuterOuter JoinsJoins 6.6. AggregatesAggregates 7.7. DebuggersDebuggers andand TracersTracers 8.8. SQLSQL TestTest CaseCase GeneratorGenerator 9.9. ConclusionsConclusions 12/5/2011 APLAS 2011 2 ~ 1.1. IntroductionIntroduction SomeSome concepts:concepts: DatabaseDatabase (DB)(DB) DatabaseDatabase ManagementManagement SystemSystem (DBMS)(DBMS) DataData modelmodel (Abstract) data structures Operations Constraints 12/5/2011 APLAS 2011 3 ~ IntroductionIntroduction DeDe--factofacto standardstandard technologiestechnologies inin databases:databases: “Relational” model SQL But,But, aa currentcurrent trendtrend towardstowards deductivedeductive databases:databases: Datalog 2.0 Conference The resurgence of Datalog inin academiaacademia andand industryindustry Ontologies Semantic Web Social networks Policy languages 12/5/2011 APLAS 2011 4 ~ Introduction.Introduction. SystemsSystems Classic academic deductive systems: LDL++ (UCLA) CORAL (Univ. of Wisconsin) NAIL! (Stanford University) Ongoing developments Recent commercial deductive systems: DLV (Italy, University of Calabria) LogicBlox (USA) Intellidimension (USA) Semmle (UK) Recent academic deductive systems: 4QL (Warsaw University) bddbddb (Stanford University) ConceptBase (Passau, Aachen, Tilburg Universities, since 1987) XSB (Stony Brook University, Universidade Nova de Lisboa, XSB, Inc., Katholieke Universiteit LeuvLeuven,en, and Uppsala Universitet) DES (Complutense University) 12/5/2011 APLAS 2011 5 ~ DatalogDatalog EducationalEducational SystemSystem (DES)(DES) YetYet anotheranother system,system, Why?Why? WeWe neededneeded anan interactiveinteractive systemsystem targetedtargeted atat teachingteaching DatalogDatalog inin classroomsclassrooms So,So, whatwhat aa wholewhole setset ofof featuresfeatures wewe werewere askingasking forfor suchsuch aa system?system? A system oriented at teaching User-friendly: Installation Usability Multiplatform (Windows, Linux, Mac, …) Interactive Query languages … 12/5/2011 APLAS 2011 6 ~ DESDES ConcreteConcrete FeaturesFeatures (1/4)(1/4) Free,Free, OpenOpen--source,source, Multiplatform,Multiplatform, PortablePortable QueryQuery languages:languages: (Extended)(Extended) DatalogDatalog (Recursive)(Recursive) SQLSQL followingfollowing ANSI/ISOANSI/ISO standardstandard StratifiedStratified NegationNegation IntegrityIntegrity constraintsconstraints DuplicatesDuplicates NullNull valuevalue supportsupport àà lala SQLSQL OuterOuter joinsjoins forfor bothboth SQLSQL andand DatalogDatalog AggregatesAggregates 12/5/2011 APLAS 2011 7 ~ DESDES ConcreteConcrete FeaturesFeatures (2/4)(2/4) DeclarativeDeclarative debuggersdebuggers andand tracerstracers TestTest casecase generatorgenerator forfor SQLSQL viewsviews FullFull--fledgedfledged arithmeticarithmetic DatabaseDatabase updatesupdates TemporaryTemporary DatalogDatalog viewsviews TypeType systemsystem BatchBatch processingprocessing TextualTextual APIAPI 12/5/2011 APLAS 2011 8 ~ DESDES ConcreteConcrete FeaturesFeatures (3/4)(3/4) ProgramProgram analysis:analysis: Safe rules (classical safety for range restriction) Negation Head variables Safe metapredicates Aggregates Duplicate elimination SourceSource--toto--sourcesource programprogram transformations:transformations: Safety (command /safe on) Aggregates (solving) Performance (/simplification on) ... 12/5/2011 APLAS 2011 9 ~ DESDES ConcreteConcrete FeaturesFeatures (4/4)(4/4) But,But, quitequite relevantrelevant featuresfeatures are:are: InteractivenessInteractiveness Database updates A wide set of commands (>70) EasyEasy toto installinstall andand useuse des.sourceforge.net RobustRobust (up(up toto bugs)bugs) 12/5/2011 APLAS 2011 10 ~ DESDES allowsallows TeachTeach (Declarative)(Declarative) QueryQuery Languages:Languages: FromFrom SQLSQL toto DatalogDatalog ButBut alsoalso forfor rapidrapid prototyping:prototyping: NovelNovel features:features: SQL hypothetical queries Outer joins in Datalog Datalog and SQL declarative (algorithmic) debuggers and tracers Test case generation for SQL views ...... andand ExperimentExperiment withwith DatalogDatalog forfor researchresearch Theses,Theses, Papers,Papers, …… SeeSee DESDES FactsFacts atat itsits webweb pagepage 12/5/2011 APLAS 2011 11 ~ 2.2. QueryQuery Languages.Languages. DatalogDatalog AA databasedatabase queryquery languagelanguage stemmingstemming fromfrom PrologProlog Prolog Datalog Predicate Relation Goal Query Goals are solved one answer at a time (backtracking) Queries are solved by computing its meaning once DeductiveDeductive database:database: Extensional database: Facts Intensional database: Rules. 12/5/2011 APLAS 2011 12 ~ DatalogDatalog DatalogDatalog differsdiffers fromfrom Prolog:Prolog: Datalog does not allow function symbols in arguments Facts are ground (safety) Datalog is truly declarative: Clause order is irrelevant Order of literals in a body is irrelevant No extra-logical constructors as the feared cut 12/5/2011 APLAS 2011 13 ~ DatalogDatalog SyntaxSyntax Program: Set of rules. Rule: head :- body. ground_head. Head: Positive atom. Body: Conjunctions (,) and disjunctions (;) of literals Literal: Atom, Built-in (>, <, …). Query: Literal with variables or constants in arguments Body (Conjunctive queries, …) 12/5/2011 APLAS 2011 14 ~ QueryQuery Languages.Languages. SQLSQL Follows ISO Standard DQL: SELECT Expressions FROM Relations WHERE Condition WITH RECURSIVE LocalViewDefs Statement ASSUME LocalViewDefs IN Statement (ongoing work) DML: INSERT … UPDATE … DELETE … DDL: CREATE [OR REPLACE] TABLE … CREATE [OR REPLACE] VIEW … DROP … 12/5/2011 APLAS 2011 15 ~ DatalogDatalog andand SQLSQL inin DESDES DES Datalog SQL Deductive engine (DE): Deductive Engine Tabling implementation Cache Datalog programs are solved by DE ODBC In-memory Compilation of SQL views and Prolog (MySQL, Access, SQL Server, queries to Datalog programs DB Oracle, DB2,...) SQL queries are also solved by DE Interoperability is allowed: SQL and Datalog do share the deductive database! Datalog queries ↔ SQL queries Datalog typed relations ↔ SQL tables and views 12/5/2011 APLAS 2011 16 ~ DatalogDatalog tom grace ExampleExample jack amy tony carolI Facts: fred carolII father(tom,amy). father(jack,fred). carolIII father(tony,carolII). father(fred,carolIII). mother(graceI,amy). mother(amy,fred). mother(carolI,carolII). Query: mother(carolII,carolIII). parent(X,Y) Rules: Minimal model for parent: { parent(X,Y) :- father(X,Y). (tom,amy), (grace,amy), (jack,fred), parent(X,Y) :- mother(X,Y). (amy,fred), … } 12/5/2011 APLAS 2011 17 ~ tom grace RecursionRecursion jack amy tony carolI father(tom,amy). fred carolII father(jack,fred). father(tony,carolII). father(fred,carolIII). carolIII mother(graceI,amy). mother(amy,fred). DES implements a fixpoint semantics for recursive mother(carolI,carolII). Datalog, finding the least fixpoint as answer mother(carolII,carolIII). parent(X,Y) :- father(X,Y). ancestor(tom,X) parent(X,Y) :- mother(X,Y). { ancestor(X,Y) :- ancestor(tom,amy), parent(X,Y). ancestor(tom,carolIII), ancestor(X,Y) :- ancestor(tom,fred) parent(X,Z), } ancestor(Z,Y). 12/5/2011 APLAS 2011 18 ~ SQLSQL RecursionRecursion asas ofof CurrentCurrent DBMS'sDBMS's CREATE VIEW parent(parent,child) AS SELECT * FROM father UNION SELECT * FROM mother; CREATE OR REPLACE VIEW ancestor(ancestor,descendant) AS WITH RECURSIVE rec_ancestor(ancestor,descendant) AS SELECT * FROM parent UNION SELECT parent,descendant FROM parent,rec_ancestor WHERE parent.child=rec_ancestor.ancestor SELECT * FROM rec_ancestor; DES-SQL> SELECT * FROM ancestor WHERE ancestor='tom'; 12/5/2011 APLAS 2011 19 ~ SQLSQL SimplifiedSimplified SyntaxSyntax inin DESDES Simply:Simply: CREATE OR REPLACE VIEW ancestor(ancestor, descendant) AS SELECT * FROM parent UNION SELECT parent, descendant FROM parent, ancestor WHERE parent.child=ancestor.ancestor; InsteadInstead ofof resortingresorting toto WITH:WITH: CREATE OR REPLACE VIEW ancestor(ancestor,descendant) AS WITH RECURSIVE rec_ancestor(ancestor,descendant) AS SELECT * FROM parent UNION SELECT parent, descendant FROM parent, rec_ancestor WHERE parent.child=rec_ancestor.ancestor SELECT * FROM rec_ancestor; 12/5/2011 APLAS 2011 20 ~ SQLSQL HypotheticalHypothetical QueriesQueries CREATE TABLE flight(origin string, destination string, time real) INSERT INTO flight VALUES('lon','ny',9.0); INSERT INTO flight VALUES('mad','par',1.5); INSERT INTO flight VALUES('par','ny',10.0); CREATE VIEW travel(origin,destination,time) AS WITH connected(origin,destination,time) AS SELECT * FROM flight UNION SELECT flight.origin, connected.destination, flight.time+connected.time FROM flight, connected WHERE flight.destination = connected.origin SELECT * FROM connected; 12/5/2011 APLAS 2011 21 ~ SQLSQL HypotheticalHypothetical QueriesQueries DES-Datalog> SELECT * FROM travel { answer(lon,ny,9.0), answer(mad,ny,11.5), answer(mad,par,1.5), answer(par,ny,10.0) } Info: 4 tuples computed. DES-Datalog> ASSUME SELECT 'mad','lon',2.0 IN flight(origin,destination,time)