February 6, 2019
If X and Y are equivalent and Y is better,
then replace all Xs with Ys
Today's focus: Provable Equivalence for RA Expressions
We say that Q1≡Q2 if and only if
we can guarantee that the bag of tuples produced by Q1(R,S,T,…)
is the same as the bag of tuples produced by Q2(R,S,T,…)
for any combination of valid inputs R,S,T,….
... that satisfy any necessary properties.
Selection | |
---|---|
σc1∧c2(R)≡σc1(σc2(R)) | (Decomposability) |
Projection | |
πA(R)≡πA(πA∪B(R)) | (Idempotence) |
Cross Product | |
R×(S×T)≡(R×S)×T | (Associativity) |
R×S≡S×R | (Commutativity) |
Union | |
R∪(S∪T)≡(R∪S)∪T | (Associativity) |
R∪S≡S∪R | (Commutativity) |
Show that R×(S×T)≡T×(S×R)
Show that σc1(σc2(R))≡σc2(σc1(R))
Show that R⋈cS≡S⋈cR
Show that σR.B=S.B∧R.A>3(R×S)≡σR.A>3(R⋈BS)
Selection + Projection | |
---|---|
πA(σc(R))≡σc(πA(R)) | (Commutativity) |
... but only if A and c are compatible
A must include all columns referenced by c (cols(c))
Show that πA(σc(R))≡πA(σc(π(A∪cols(c))(R)))
Selection + Cross Product | |
---|---|
σc(R×S)≡(σc(R))×S | (Commutativity) |
... but only if c references only columns of R
cols(c)⊆cols(R)
Show that σR.B=S.B∧R.A>3(R×S)≡(σR.A>3(R))⋈BS
Projection + Cross Product | |
---|---|
πA(R×S)≡(πAR(R))×(πAS(S)) | (Commutativity) |
... where AR and AS are the columns of A from R and S respectively.
AR=A∩cols(R) AS=A∩cols(S)
Show that πA(R⋈cS)≡(πAR(R))⋈c(πAS(S))
Intersection | |
---|---|
R∩(S∩T)≡(R∩S)∩T | (Associativity) |
R∩S≡S∩R | (Commutativity) |
Selection + | |
σc(R∪S)≡(σc(R))∪(σc(R)) | (Commutativity) |
σc(R∩S)≡(σc(R))∩(σc(R)) | (Commutativity) |
Projection + | |
πA(R∪S)≡(πA(R))∪(πA(R)) | (Commutativity) |
πA(R∩S)≡(πA(R))∩(πA(R)) | (Commutativity) |
Cross Product + Union | |
R×(S∪T)≡(R×S)∪(R×T) | (Distributivity) |
SELECT R.A, T.E
FROM R, S, T
WHERE R.B = S.B
AND S.C < 5
AND S.D = T.D
➔
Input: Dumb translation of SQL to RA
⬇︎
Apply rewrites
⬇︎
Output: Better, but equivalent query
Which rewrite rules should we apply?
Some rewrites are situational... we need more information to decide when to apply them.