April 29, 2021
How does the input data relate to a query output.
|
|
Why is $\left<1\right>$ in $\pi_A (R \bowtie S)$?
|
|
... but that's not the only reason
|
|
Why is $\left<1\right>$ in $\pi_A (R \bowtie S)$?
$\left\{ R_1, S_1 \right\}$, $\left\{ R_1, S_2 \right\}$, $\left\{ R_2, S_3 \right\}$
Witness: Any subset of the original database that still produces the same result.
(Generally we only want 'minimal' witnesses)
|
|
How is $\left<1\right>$ derived in $\pi_A (R \bowtie S)$?
$(R_1 \bowtie S_1) \oplus (R_1 \bowtie S_2) \oplus (R_2 \bowtie S_3)$
[head] :- [body]
$$Q(A) :-~~ R(A, B), S(B, C)$$
like SELECT A FROM R NATURAL JOIN S
Stop thinking about relations as collections of records, and instead think of them as collections of facts
R | A | B |
---|---|---|
1 | 1 | 2 |
2 | 1 | 3 |
3 | 2 | 3 |
4 | 2 | 4 |
The fact $R(1, 2)$ is true.
The fact $R(2, 1)$ is false (or unknown).
A table contains all facts that are provably true.
$$Q(A) :-~~ R(A, B), S(B, C)$$
For any $A$, the fact $Q(A)$ is true if...$\forall A : \big( \exists B, C : R(A, B) \wedge S(B, C) \big) \rightarrow Q(A)$
$$Q(A) :-~~ R(A, B), S(B, C)$$ $$Q(A) :-~~ R(A, B), R(B, C)$$
Treat multiple rules as a disjunction.
($Q(A)$ is true if any rule is satisfied)
$[[ A > B ]]$ | A | B |
---|---|---|
1 | 0 | |
2 | 0 | |
3 | 0 | |
... | ||
2 | 1 | |
... |
Relations are Sets of Facts. We can have a relation consisting of all pairs $A, B$ where $A$ is bigger.
Safety Property: Every variable must appear in at least one finite relation in a rule body.
Recursive datalog: The body can reference the head atom
$$Q(A, B) :-~~ R(A, B)$$ $$Q(A, C) :-~~ Q(A, B), R(B, C)$$(~Dijkstra's algorithm)
... is like a very large number of queries with no head variables
...
The fact $Q(1, 1)$ is true if $\exists B : R(1, B) \wedge S(B, 1)$
Think of the relation as a function from potential facts to their truthiness.
R | A | B | |
---|---|---|---|
1 | 1 | 2 | → T |
2 | 1 | 3 | → T |
3 | 2 | 3 | → T |
4 | 2 | 4 | → T |
5 | 1 | 1 | → F |
6 | ... | → F |
Every row not explicitly listed is mapped to False
$Q(A) :-~~ R(A, B), S(B, C)$
$Q(1) :-~~ R(1, B), S(B, C)$
$Q(1) \equiv R(1, 1) \wedge S(1, 1)$
$~~~~~~~~ \vee ~~ R(1, 2) \wedge S(2, 1)$
$~~~~~~~~ \vee ~~ R(1, 3) \wedge S(3, 1)$
...
$~~~~~~~~ \vee ~~ R(1, 1) \wedge S(1, 2)$
$~~~~~~~~ \vee ~~ R(1, 2) \wedge S(2, 2)$
$~~~~~~~~ \vee ~~ R(1, 3) \wedge S(3, 2)$
...
R | A | B |
---|---|---|
1 | 1 | 2 |
2 | 1 | 3 |
3 | 2 | 3 |
4 | 2 | 4 |
S | B | C |
---|---|---|
1 | 2 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
$Q(1) \equiv R(1, 1) \wedge S(1, 1)$
$~~~~~~~~ \vee ~~ R(1, 2) \wedge S(2, 1)$
$~~~~~~~~ \vee ~~ R(1, 3) \wedge S(3, 1)$
...
$~~~~~~~~ \vee ~~ R(1, 1) \wedge S(1, 2)$
$~~~~~~~~ \vee ~~ R(1, 2) \wedge S(2, 2)$
$~~~~~~~~ \vee ~~ R(1, 3) \wedge S(3, 2)$
...
R | A | B |
---|---|---|
1 | 1 | 2 |
2 | 1 | 3 |
3 | 2 | 3 |
4 | 2 | 4 |
S | B | C |
---|---|---|
1 | 2 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
$Q(1) \equiv R(1, 1) \wedge S(1, 1)$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge S(2, 1)$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge S(3, 1)$
...
$~~~~~~~~ \vee ~~ R(1, 1) \wedge S(1, 2)$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge S(2, 2)$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge S(3, 2)$
...
R | A | B |
---|---|---|
1 | 1 | 2 |
2 | 1 | 3 |
3 | 2 | 3 |
4 | 2 | 4 |
S | B | C |
---|---|---|
1 | 2 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
$Q(1) \equiv ~~~~~F~~~~ \wedge S(1, 1)$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge S(2, 1)$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge S(3, 1)$
...
$~~~~~~~~ \vee ~~ ~~~~~F~~~~ \wedge S(1, 2)$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge S(2, 2)$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge S(3, 2)$
...
R | A | B |
---|---|---|
1 | 1 | 2 |
2 | 1 | 3 |
3 | 2 | 3 |
4 | 2 | 4 |
S | B | C |
---|---|---|
1 | 2 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
$Q(1) \equiv ~~~~~F~~~~ \wedge ~~~~F~~~~~$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge ~~~~T~~~~~$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge ~~~~F~~~~~$
...
$~~~~~~~~ \vee ~~ ~~~~~F~~~~ \wedge ~~~~F~~~~~$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge ~~~~T~~~~~$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge ~~~~F~~~~~$
...
R | A | B |
---|---|---|
1 | 1 | 2 |
2 | 1 | 3 |
3 | 2 | 3 |
4 | 2 | 4 |
S | B | C |
---|---|---|
1 | 2 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
$Q(1) \equiv$$~~~~~~F~~~~ \wedge ~~~~F~~~~~$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge ~~~~T~~~~~$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge ~~~~F~~~~~$
...
$~~~~~~~~ \vee ~~ ~~~~~F~~~~ \wedge ~~~~F~~~~~$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge ~~~~T~~~~~$
$~~~~~~~~ \vee ~~ ~~~~~T~~~~ \wedge ~~~~F~~~~~$
...
R | A | B |
---|---|---|
1 | 1 | 2 |
2 | 1 | 3 |
3 | 2 | 3 |
4 | 2 | 4 |
S | B | C |
---|---|---|
1 | 2 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
$Q(1) \equiv$$~R(1, 1) \wedge S(1, 1)$
$~~~~~~~~ \vee ~~ R(1, 2) \wedge S(2, 1)$
$~~~~~~~~ \vee ~~ R(1, 3) \wedge S(3, 1)$
...
$~~~~~~~~ \vee ~~ R(1, 1) \wedge S(1, 2)$
$~~~~~~~~ \vee ~~ R(1, 2) \wedge S(2, 2)$
$~~~~~~~~ \vee ~~ R(1, 3) \wedge S(3, 2)$
...
R | A | B |
---|---|---|
1 | 1 | 2 |
2 | 1 | 2 |
3 | 1 | 2 |
4 | 1 | 3 |
5 | 2 | 3 |
6 | 2 | 3 |
7 | 2 | 4 |
R | A | B | |
---|---|---|---|
1 | 1 | 2 | → 3 |
2 | 1 | 3 | → 1 |
3 | 2 | 3 | → 2 |
4 | 2 | 4 | → 1 |
|
|
$Q(1) =~?$
|
|
$Q(1) = 3\times 1 + 3 \times 2 + 1 \times 3 = 12$
$Q(1) \equiv R(1, 1) \wedge S(1, 1)$
$~~~~~~~~ \vee ~~ R(1, 2) \wedge S(2, 1)$
$~~~~~~~~ \vee ~~ R(1, 3) \wedge S(3, 1)$
...
$~~~~~~~~ \vee ~~ R(1, 1) \wedge S(1, 2)$
$~~~~~~~~ \vee ~~ R(1, 2) \wedge S(2, 2)$
$~~~~~~~~ \vee ~~ R(1, 3) \wedge S(3, 2)$
...
R | A | B | |
---|---|---|---|
1 | 1 | 2 | → 3 |
2 | 1 | 3 | → 1 |
3 | 2 | 3 | → 2 |
4 | 2 | 4 | → 1 |
S | B | C | |
---|---|---|---|
1 | 2 | 1 | → 1 |
2 | 2 | 2 | → 2 |
3 | 3 | 3 | → 3 |
$Q(1) \equiv R(1, 1) \times S(1, 1)$
$~~~~~~~~ + ~~ R(1, 2) \times S(2, 1)$
$~~~~~~~~ + ~~ R(1, 3) \times S(3, 1)$
...
$~~~~~~~~ + ~~ R(1, 1) \times S(1, 2)$
$~~~~~~~~ + ~~ R(1, 2) \times S(2, 2)$
$~~~~~~~~ + ~~ R(1, 3) \times S(3, 2)$
...
R | A | B | |
---|---|---|---|
1 | 1 | 2 | → 3 |
2 | 1 | 3 | → 1 |
3 | 2 | 3 | → 2 |
4 | 2 | 4 | → 1 |
S | B | C | |
---|---|---|---|
1 | 2 | 1 | → 1 |
2 | 2 | 2 | → 2 |
3 | 3 | 3 | → 3 |
$Q(1) \equiv 0 \times 0$
$~~~~~~~~ + ~~ 3 \times 1$
$~~~~~~~~ + ~~ 1 \times 0$
...
$~~~~~~~~ + ~~ 0 \times 0$
$~~~~~~~~ + ~~ 3 \times 2$
$~~~~~~~~ + ~~ 1 \times 0$
...
R | A | B | |
---|---|---|---|
1 | 1 | 2 | → 3 |
2 | 1 | 3 | → 1 |
3 | 2 | 3 | → 2 |
4 | 2 | 4 | → 1 |
S | B | C | |
---|---|---|---|
1 | 2 | 1 | → 1 |
2 | 2 | 2 | → 2 |
3 | 3 | 3 | → 3 |
$Q(1) \equiv $$~0 \times 0$
$~~~~~~~~ + ~~ 3 \times 1$
$~~~~~~~~ + ~~ 1 \times 0$
...
$~~~~~~~~ + ~~ 3 \times 0$
$~~~~~~~~ + ~~ 3 \times 2$
$~~~~~~~~ + ~~ 1 \times 0$
...
|
|
|
|
R | A | B | |
---|---|---|---|
1 | 1 | 2 | → a |
2 | 1 | 3 | → b |
3 | 2 | 3 | → c |
4 | 2 | 4 | → d |
5 | ... | → $\mathbf{0}$ |
S | B | C | |
---|---|---|---|
1 | 2 | 1 | → e |
2 | 2 | 2 | → f |
3 | 3 | 3 | → g |
4 | ... | → $\mathbf{0}$ |
$Q(1) \equiv R(1, 1) \otimes S(1, 1)$
$~~~~~~~~ \oplus ~~ R(1, 2) \otimes S(2, 1)$
$~~~~~~~~ \oplus ~~ R(1, 3) \otimes S(3, 1)$
...
$~~~~~~~~ \oplus ~~ R(1, 1) \otimes S(1, 2)$
$~~~~~~~~ \oplus ~~ R(1, 2) \otimes S(2, 2)$
$~~~~~~~~ \oplus ~~ R(1, 3) \otimes S(3, 2)$
...
R | A | B | |
---|---|---|---|
1 | 1 | 2 | → a |
2 | 1 | 3 | → b |
3 | 2 | 3 | → c |
4 | 2 | 4 | → d |
5 | ... | → $\mathbf{0}$ |
S | B | C | |
---|---|---|---|
1 | 2 | 1 | → e |
2 | 2 | 2 | → f |
3 | 3 | 3 | → g |
4 | ... | → $\mathbf{0}$ |
$Q(1) \equiv \mathbf{0} \otimes \mathbf{0}$
$~~~~~~~~ \oplus ~~ a \otimes e$
$~~~~~~~~ \oplus ~~ b \otimes \mathbf{0}$
...
$~~~~~~~~ \oplus ~~ \mathbf{0} \otimes \mathbf{0}$
$~~~~~~~~ \oplus ~~ a \otimes f$
$~~~~~~~~ \oplus ~~ b \otimes \mathbf{0}$
...
$(a\otimes e) \oplus (a \otimes f) \oplus (b \otimes g) \oplus \mathbf{0} \oplus \ldots$
$(T\wedge T) \vee (T \wedge T) \vee (T \wedge T) \vee F \vee \ldots$
$(3\times 1) + (3 \times 1) + (1 \times 3) + 0 + \ldots$
... and more
Any pair of operators (along with their domain) that follows these rules is called a commutative semiring
If a Table is a function, so is a query result!
Sum over all projected-away variables
Truncate filtered rows to 0.
Sum annotations through union.
Multiply annotations through cross product.
Domain of Tuple IDs: $\mathbb T$
A Set of Tuple IDs: $2^{\mathbb T}$
A Set of Sets of Tuple IDs: $2^{2^{\mathbb T}}$
e.g., $\{ \{t_1, t_5\}, \{t_1, t_6\}, \{t_2, t_7\} \}$
|
|
Polynomials are also a semiring
|
|
$ae + af + bg$
Plug in boolean annotations: $T$
Plug in multiplicities: $12$
Plug in tuple IDs:
$\{\{t_1, t_5\}, \{t_1, t_6\}, \{t_2, t_7\}\}$
$ae + af + bg$