predicates, joins, and algebra, oh my!
DESCRIPTION
Predicates, Joins, and Algebra, Oh my!. Matt Dube Doctoral Student, Spatial Information Science and Engineering. Wednesday’s Lecture. Projection (unary) Horizontal reduction Selection (unary) Vertical reduction Renaming (unary) Making attributes kosher Cartesian Product (binary or more) - PowerPoint PPT PresentationTRANSCRIPT
Predicates, Joins, and Algebra, Oh my!
Matt DubeDoctoral Student, Spatial Information Science and EngineeringPredicates, Joins, and Algebra, Oh my!Wednesdays LectureProjection (unary)Horizontal reductionSelection (unary)Vertical reductionRenaming (unary)Making attributes kosherCartesian Product (binary or more)Most expensive operator, consisting of all possible combinationsUnion (binary or more)LinkerDifference (binary or more)Whats mine is only mineTodayRelations as first order language predicates
Efficiencies in combination
Algebra
JoinProjectionProjection takes a tuple and reduces its number of attributes
dog(fido,dalmatian,bob).
Suppose we would like a table that stored only the owners name and the dogs name.
petname(A,B):-dog(B,C,A).
This is a projection.SelectionSelections act upon particular criteria
dog(fido,dalmatian,bob).
Suppose we want to return anyone who owns a dalmatian and what the name of the dog is
dalmatian(A,B):-dog(A,dalmatian,B).
This is a selection. Note the constant for the variable.RenamingRenaming changes the name of an attribute or a relation.
dog(fido,dalmatian,bob).
Suppose I now want to call all dogs hippos.
hippo(A,B,C):-dog(A,B,C).
This is a renaming. It makes more sense with an attribute if you consider attributes spilled out in binary listings.Cartesian ProductCartesian products bind two different relations together, maintaining all attributes
dog(fido,dalmatian,bob).coffee(starbucks,brazil).
Suppose we want something to give us data about all coffee and dogs
javadog(A,B,C,D,E):-coffee(A,B),dog(C,D,E).
This is a Cartesian product. Note that it is an AND statement and needs all variables.UnionUnions link different relations together in similar strucutres
dog(fido,dalmatian,bob).cat(mittens,coon,matt).
Suppose we want to know all pets. Dogs and cats are pets.
pet(A,B,C):-dog(A,B,C) cat(A,B,C).
This is a union of the dog relation and the cat relation.DifferenceDifferences show what is in one set, but not the other
dog(fido,dalmatian,bob).cat(fido,dalmatian,bob).dog(rover,pitbull,joe).
Suppose I want to find dogs who dont share qualities with a cat.
onlydog(A,B,C):-dog(A,B,C),cat(A,B,C),!. (fail it)onlydog(A,B,C):-dog(A,B,C).
Only unique dogs are produced here.Combination of OperationsAll of our operators produce relationsRemember, some of the operators need to refine the output to have a relation output (think projection of a non-key)Since all operators produce relations, this system of operators is a closed system
Closed systems take in inputs of a particular form (in our case relation) and output that form in return.
Since the system is closed, we can string together operators.Equivalent Combinationsa1,,an (A (R)) = A ( a1,,an (R))A (R P) = A (R) A (P)AB (R) = A (B (R)) = B (A (R)) AB (R) = A (R) B (R)A (R x P) = BCD (R x P) = D (B(R) x C(P)) Optimizationa1,,an (A (R)) = A ( a1,,an (R))A (R P) = A (R) A (P)AB (R) = A (B (R)) = B (A (R)) AB (R) = A (R) B (R)A (R x P) = BCD (R x P) = D (B(R) x C(P)) Project, then select.Difference, then select.Union, then select.Break apart complex selectionsAlgebraFrom the arabic Al-jabr, meaning reunion
Algebras are mathematical structures formed off of operators
You are familiar with an algebra from your studies in mathematics (+, -, *, /), which can be boiled down to simply + and *
Algebras are closed systems under their operators and also have identities for every operator (+ 0, - 0, * 1, / 1)Relational AlgebraWhat is the identity form for each operator?ProjectionProject all of the attributesSelectionSelect the entire key structureRenamingRename the attribute to the same nameCartesian ProductCross with an empty relationUnionUnion with a subset of the original relationDifferenceDifference with a mutually exclusive setWhy is an Algebra important?Algebras (reunions) are what allows for operations to be strung together
An example of an important consequence: operational efficiency
You have seen the relational algebra equivalencies, but there is an easier example that you already know.Heres the test:What are the key properties of an algebra?Closed under operationsEvery operation has an identity state
Why is an algebra important for a system?Stringing together operatorsEfficient processes (think the distributive property)Operational equivalenceJoinsJoins are what we would call a higher level operation
Higher level operation?Think of an exponent (successive multiplications of terms)
Why can we use higher level operations? (You already know the answer)
Most important operator for some key reasons
Done by imposing a condition on a pair of attributesTypes of JoinsInner Joins
Equi-Joins
Natural Joins
Outer JoinsInner JoinSelection on a Cartesian Product
Selection involves some sort of criteria between a member of one relation and a member of the other (similar to the keys for the Cartesian product)Inner Joina1a2abc45cde52b1b245zyx23ijkA.a2>B.b1 (A x B)ABInner Joina1a2b1b2abc4545zyxabc4523ijkcde5245zyxcde5223ijkA.a2>B.b1 (A x B)A x BInner Joina1a2b1b2abc4545zyxabc4523ijkcde5245zyxcde5223ijkA.a2>B.b1 (A x B)A x BInner Joina1a2b1b2abc4545zyxabc4523ijkcde5245zyxcde5223ijkA.a2>B.b1 (A x B)A x BInner Joina1a2b1b2abc4523ijkcde5245zyxcde5223ijkA.a2>B.b1 (A x B)Equi-Join Another selection on a Cartesian product
As the name implies, this has to do with two of the attributes being equalEqui-Joina1a2abc45cde52b1b245zyx23ijkA.a2=B.b1 (A x B)ABEqui-Joina1a2b1b2abc4545zyxabc4523ijkcde5245zyxcde5223ijkA.a2=B.b1 (A x B)A x BEqui-Joina1a2b1b2abc4545zyxA.a2=B.b1 (A x B)Natural Join < A.attribute_1 = B.attribute_2 > (A x B)attribute_1 and attribute_2 must have same nameonly one column with this attribute shows up in the result (i.e., a projection follows)
Selection on a Cartesian product (Equi-Join) followed by a projectionNatural Joinxxyyabc45cde52yyzz45zyx23ijkA.yy=B.yy (A x B)ABNatural Joinxxyyyyzzabc4545zyxabc4523ijkcde5245zyxcde5223ijkA.a2=B.b1 (A x B)A x BNatural Joinxxyyyyzzabc4545zyx (A.a2=B.b1 (A x B)) Natural Joinxxyyzzabc45zyx (A.a2=B.b1 (A x B)) Outer JoinsAn outer join is a join which allows for NULL values to be involved
Three typesLeft Outer JoinEverything in the left relation will be present, regardless if found in B or notRight Outer JoinEverything in the right relation will be present, regardless if found in A or notFull Outer JoinBoth left and right outer joinsSummaryConverted operators into a first order language parallel
Showed how to combine operators for efficiency
Defined the concept of algebra
Showed the different types of joins