relational algebra
play

Relational Algebra Database Systems: The Complete Book Ch 2.4 (plus - PowerPoint PPT Presentation

Relational Algebra Database Systems: The Complete Book Ch 2.4 (plus preview of 15.1, 16.1) The running theme Replace [thing] with better, but equivalent [thing] . The running theme Replace [thing] with better, but equivalent [thing] . How


  1. Relational Algebra Database Systems: The Complete Book Ch 2.4 (plus preview of 15.1, 16.1)

  2. The running theme… Replace [thing] with better, but equivalent [thing] .

  3. The running theme… Replace [thing] with better, but equivalent [thing] . How can we tell if How can we tell if [thing] is better? [thing] is equivalent?

  4. First, a few definitions…

  5. Relational Data • Relation (Table): A collection of Tuples of Values • All tuples have the same set of attributes, or schema • What constraints are present on the collection? Uniqueness Order Matters <Redshirt, Ensign> <Kirk, Capt.> <Spock, Lt.> <Spock, Lt.> <Spock, Lt.> <Kirk, Capt.> <Kirk, Capt.> <McCoy, Lt. Cmdr> <Redshirt, Ensign> <Redshirt, Ensign> <Redshirt, Ensign> <McCoy, Lt. Cmdr> <Redshirt, Ensign> <Redshirt, Ensign> <McCoy, Lt. Cmdr> <Redshirt, Ensign> Set Bag List

  6. Declarative Languages Declarative Imperative Say how you want Say what you want to get it “Look at every T report, For each week, “Get me the TPS reports” Sum up the sprocket count Find that week’s S report etc….” C, Scala, Java, SQL, RA, R, … Ruby, Python, …

  7. Declarative languages make it easier to explore equivalent computations to find the best one.

  8. How do you build a query processor?

  9. Project Outline Parser & SQL Query Relational Algebra Translator .sql JSqlParser ??? Optimizer Statistics Trained Monkeys? Query Evaluation Execution Plan Result Engine

  10. .sql JSqlParser .sql CCJSqlParser parser = new CCJSqlParser( ) Statement stmt; while((stmt = parser.Statement() != null) { if(stmt instanceof Select) { … } else if(stmt instanceof CreateTable) { … } }

  11. .sql JSqlParser .sql CCJSqlParser parser = new CCJSqlParser( ) Statement stmt; while((stmt = parser.Statement() != null) { if(stmt instanceof Select) { … } else if(stmt instanceof CreateTable) { … } } Now what?

  12. The Evaluation Pipeline Parsed Query Results Data

  13. The Evaluation Pipeline Parsed Query Results Data Done?

  14. The Evaluation Pipeline Parsed Query Results Data Done? No! Evaluating SQL is HARD.

  15. The Evaluation Pipeline ??? Parsed Query Results Data First, transform the query into something simpler. (simpler, but equivalent)

  16. What’s in the box?

  17. Formal Query Languages • Two mathematical query languages form the basis for user-facing languages (e.g., SQL): • Relational Algebra: Operational, useful for representing how queries are evaluated. • Relational Calculus: Declarative, useful for representing what a user wants rather than how to compute it. 13

  18. Formal Query Languages • Two mathematical query languages form the For basis for user-facing languages (e.g., SQL): Now • Relational Algebra: Operational, useful for representing how queries are evaluated. • Relational Calculus: Declarative, useful for representing what a user wants rather than how to compute it. 13

  19. Preliminaries Queries are applied to Relations Q(Officers, Ships, …) A Query works on fixed relation schemas. … but runs on any relation instance 14

  20. Preliminaries Important : The result of a query is also a relation ! Q 2 (Officers, Q 1 (Ships)) Allows simple, composable query operators 15

  21. Example Instances Captains FirstName, LastName, Rank, Ship [James, Kirk, 4.0, 1701A] [Jean Luc, Picard, 4.0, 1701D] [Benjamin, Sisko, 3.0, DS9 ] Locations [Kathryn, Janeway, 4.0, 74656] Ship, Location [Nerys, Kira, 2.5, 75633] [1701A, Earth ] FirstOfficers [1701D, Risa ] [ 75633 , Bajor ] FirstName, LastName, Rank, Ship [DS9, Bajor ] [Spock, NULL, 2.5, 1701A] [William, Riker, 2.5, 1701D] [Nerys, Kira, 2.5, DS9 ] [Chakotay, NULL, 3.0, 74656] 16

  22. Relational Algebra Operation Sym Meaning Selection Select a subset of the input rows 𝝉 Projection Delete unwanted columns π Cross-product x Combine two relations Set-difference - Tuples in Rel 1, but not Rel 2 Union U Tuples either in Rel 1 or in Rel 2 Also: Intersection, Join , Division, Renaming (Not essential, but can be useful) 17

  23. Relational Algebra Each operation returns a relation! Operations can be composed (Relational Algebra operators are closed ) 18

  24. Relational Algebra Data Relational Algebra Data

  25. Relational Algebra A Set of Tuples Data [Set] Relational Relational Algebra Algebra A Set of Tuples Data

  26. Relational Algebra A Set of Tuples A Bag of Tuples Data [Set] Relational Bag Relational Relational Algebra Algebra Algebra A Set of Tuples A Bag of Tuples Data

  27. Relational Algebra A Set of Tuples A Bag of Tuples A List of Tuples Data Extended [Set] Relational Bag Relational Relational Relational Algebra Algebra Algebra Algebra A Set of Tuples A Bag of Tuples A List of Tuples Data

  28. Relational Algebra A Set of Tuples A Bag of Tuples A List of Tuples Data Extended [Set] Relational Bag Relational Relational Relational Algebra Algebra Algebra Algebra A Set of Tuples A Bag of Tuples A List of Tuples Data Today

  29. Projection ( π ) Delete attributes not in the projection list . π lastname, ship (Captains) FirstName, LastName, Rank, Ship [Spock, NULL, 2.5, 1701A] [William, Riker, 2.5, 1701D] [Nerys, Kira, 2.5, DS9 ] [Chakotay, NULL, 3.0, 74656] 20

  30. Projection ( π ) Delete attributes not in the projection list . π lastname, ship (Captains) FirstName, LastName, Rank, Ship LastName, Ship [Spock, NULL, 2.5, 1701A] [Kirk, 1701A] [William, Riker, 2.5, 1701D] [Picard, 1701D] [Nerys, Kira, 2.5, DS9 ] [Sisko, DS9 ] [Chakotay, NULL, 3.0, 74656] [Janeway, 74656] [Kira, 75633] π rank (FirstOfficers) 20

  31. Projection ( π ) Delete attributes not in the projection list . π lastname, ship (Captains) FirstName, LastName, Rank, Ship LastName, Ship [Spock, NULL, 2.5, 1701A] [Kirk, 1701A] [William, Riker, 2.5, 1701D] [Picard, 1701D] [Nerys, Kira, 2.5, DS9 ] [Sisko, DS9 ] [Chakotay, NULL, 3.0, 74656] [Janeway, 74656] Why is this strange? [Kira, 75633] π rank (FirstOfficers) Rank [2.5 ] [3.0 ] 20

  32. Projection ( π ) Delete attributes not in the projection list . π lastname, ship (Captains) FirstName, LastName, Rank, Ship LastName, Ship [Spock, NULL, 2.5, 1701A] [Kirk, 1701A] [William, Riker, 2.5, 1701D] [Picard, 1701D] [Nerys, Kira, 2.5, DS9 ] [Sisko, DS9 ] [Chakotay, NULL, 3.0, 74656] [Janeway, 74656] Why is this strange? [Kira, 75633] Relational Algebra on Bags: π rank (FirstOfficers) Bag Relational Algebra Rank [2.5 ] [3.0 ] Why? 20

  33. Projection ( π ) Queries are relations What is this (query) relation’s schema? π lastname, ship (Captains) 21

  34. Selection ( ) 𝝉 Selects rows that satisfy the selection condition . 𝝉 rank < 3.5 (Captains) When does selection need FirstName, LastName, Rank, Ship to eliminate duplicates? [Benjamin, Sisko, 3.0, DS9 ] [Nerys, Kira, 2.5, 75633] π lastname ( 𝝉 rank > 3.5 (Captains)) What is the schema of these queries? LastName [Kirk ] [Picard ] [Janeway ] 22

  35. Union, Intersection, Set Difference Each takes two relations that are union-compatible (Both relations have the same number of fields with the same types) Union : Return all tuples in either relation π firstname,lastname (Captains) U π firstname,lastname (FirstOfficers) FirstName, Lastname [James, Kirk ] [Jean Luc, Picard ] [Benjamin, Sisko ] [Kathryn, Janeway ] [Spock, NULL ] [William, Riker ] [Nerys, Kira ] [Chakotay, NULL ] 23

  36. Union, Intersection, Set Difference Each takes two relations that are union-compatible (Both relations have the same number of fields with the same types) Intersection: Return all tuples in both relations π firstname,lastname (Captains) ∩ π firstname,lastname (FirstOfficers) FirstName, Lastname [Nerys, Kira ] 24

  37. Union, Intersection, Set Difference Each takes two relations that are union-compatible (Both relations have the same number of fields with the same types) Set Difference: Return all tuples in the first but not the second relation π firstname,lastname (Captains) - π firstname,lastname (FirstOfficers) FirstName, LastName [James, Kirk ] [Jean Luc, Picard ] [Benjamin, Sisko ] [Kathryn, Janeway ] 25

  38. Union, Intersection, Set Difference Each takes two relations that are union-compatible (Both relations have the same number of fields with the same types) What is the schema of the result of any of these operators? 26

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend