recursion How expressive is SQL? Full programming languages can - - PowerPoint PPT Presentation

recursion how expressive is sql
SMART_READER_LITE
LIVE PREVIEW

recursion How expressive is SQL? Full programming languages can - - PowerPoint PPT Presentation

recursion How expressive is SQL? Full programming languages can express all computable functions (C, Java, etc) Can SQL express all computable queries? A: YES B: NO 2 How expressive is SQL? flight from to SD LA SD SF LA


slide-1
SLIDE 1

recursion ¡

slide-2
SLIDE 2
  • Full programming languages can express all

computable functions (C, Java, etc)

Can SQL express all computable queries? A: YES B: NO

How expressive is SQL?

2 ¡

slide-3
SLIDE 3

Can SQL express the following query: “Is there a way to get from City1 to City2?” A: YES B: NO

How expressive is SQL?

3 ¡

from to SD LA SD SF LA NY … …

flight

slide-4
SLIDE 4

“Is there a way to get from City1 to City2 by a direct flight?”

select * from flight where from = ‘City1’ and to =‘City2’ City1 City2

Easier

slide-5
SLIDE 5

“Is there a way to get from City1 to City2 with at most one stopovers?”

select * from flight where from = ‘City1’ and to =‘City2’ City1 City2 OR City1 City2 select x.from, y.to from flight x, flight y where x.from = ‘City1’ and x.to = y.from and y.to = ‘City2’ x y

Easier

slide-6
SLIDE 6

“Is there a way to get from City1 to City2 with at most two stopovers?”

select * from flight where from = ‘City1’ and to =‘City2’ City1 City2 OR City1 City2 select x.from, y.to from flight x, flight y where x.from = ‘City1’ and x.to = y.from and y.to = ‘City2’ OR City1 City2 select x.from, z.to from flight x, flight y, flight z where x.from = ‘City1’ and x.to = y.from and y.to = z.from and z.to = ‘City2’ x y x y z

Easier

slide-7
SLIDE 7

“Is there a way to get from City1 to City2 with at most k stopovers?”

City1 City2 …

Easier

Need k+1 tuple variables!

slide-8
SLIDE 8

“Is there a way to get from City1 to City2 with any number of stopovers?”

Now

Cannot do in basic SQL!

slide-9
SLIDE 9
  • Parts-components relation:

“Find all subparts of some given part A”

  • Parent/child relation:

“Find all of John’s descendants”

Similar Examples

9 ¡

slide-10
SLIDE 10

Transitive closure of graph

10 ¡

More general: a b c d e

A B a b c a b c … …

G

Find the pairs of nodes <x, y> that are connected by some directed path

A B a b a d a e b d b e … …

slide-11
SLIDE 11

Computing transitive closure T of G

11 ¡

“Find the pairs of nodes <a,b> that are connected in G” Same as: “find pairs of nodes <a,b> at distance 1” UNION “find pairs of nodes <a,b> at distance at most 2” UNION ……….. “find pairs of nodes <a,b> at distance at most k” UNION ……….. When to stop? At some point, no new nodes are added. Distance cannot be larger than total number

  • f nodes in G.
slide-12
SLIDE 12

a b c d e

Example

12 ¡

slide-13
SLIDE 13

a b c d e

Distance 1

Example

13 ¡

slide-14
SLIDE 14

a b c d e

Distance ≤ 2

Example

14 ¡

slide-15
SLIDE 15

a b c d e

Example

Distance ≤ 3

15 ¡

slide-16
SLIDE 16

T1 : “find pairs of nodes <a,b> at distance 1” Denote by Tk the pairs of nodes at distance at most k select * from G Tk : “find the pairs of nodes <a,b> at distance at most k” a b G Tk-1 a b Tk-1 OR (select * from Tk-1) union (select x.A, y.B from G x, Tk-1 y where x.B = y.A)

Algorithm

slide-17
SLIDE 17

a b c d e

T1

Example

17 ¡

slide-18
SLIDE 18

a b c d e

T2

Example

18 ¡

slide-19
SLIDE 19

a b c d e

Example

T3

19 ¡

slide-20
SLIDE 20

Add recursion to SQL

20 ¡

One Solution

create recursive view T as (select * from G) union (select x.A, y.B from G x, T y where x.B = y.A)

Semantics: 1. Start with empty T 2. While T changes {evaluate view with current T; union result with T } Note: This must terminate, since there are finitely many tuples

  • ne can add to T (if no new values

are created) (Not part of the standard)

slide-21
SLIDE 21

Add recursion to SQL

21 ¡

One Solution

with recursive T as (select * from G) union (select x.A, y.B from G x, T y where x.B = y.A) select * from T ;

Alternative formulation:

slide-22
SLIDE 22

22 ¡

frequents drinker bar Friends: Drinkers who frequent the same bar Find transitive closure of Friends create recursive view T as (select f1.drinker as drinker1, f2.drinker as drinker2 from frequents f1, frequents f2 where f1.bar = f2.bar) union (select t1.drinker1, f2.drinker as drinker2 from T t1, frequents f1, frequents f2 where t1.drinker2 = f1.drinker and f1.bar = f2.bar)

Another Example

slide-23
SLIDE 23

Problematic example

create ¡recursive ¡view ¡T ¡as ¡ (select ¡ ¡A, ¡ ¡0 ¡as ¡N ¡from ¡R) ¡ union ¡ (select ¡A, ¡N+1 ¡as ¡N ¡ from ¡T) ¡

R A

  • Never terminates
  • Arithmetic in selects, aggregate functions are

forbidden in recursive definitions 23 ¡

slide-24
SLIDE 24

Client:

full programming language (Java, C+, etc)

DB Server

24 ¡

Embedded SQL

Powerful way to overcome SQL limitations SQL Requests Answers

Another Solution

slide-25
SLIDE 25

T := G Δ := G while Δ ≠ ∅ do { Told = T T := (select * from T) union (select x.A, y.B from G x, T y where x.B = y.A) Δ := T – Told } Output T

Transitive Closure in embedded SQL

25 ¡ <pseudo-code>

slide-26
SLIDE 26

a b c d e

T1 and Δ1

Example

26 ¡

slide-27
SLIDE 27

a b c d e

T2 and Δ2

Example

27 ¡

slide-28
SLIDE 28

a b c d e

T3 and Δ3

Example

28 ¡

slide-29
SLIDE 29

a b c d e

T4 = T3 and Δ4 = ∅: Stop!

Example

29 ¡

slide-30
SLIDE 30

T := G Δ := G while Δ ≠ ∅ do { Told = T T := (select * from T) union (select x.A, y.B from G x, T y where x.B = y.A) Δ := T – Told } Output T

Algorithm revisited

30 ¡

Converges in diameter(G) iterations (maximum distance between two nodes in G)

<pseudo-code>

slide-31
SLIDE 31

T := G Δ := G while Δ ≠ Φ do { Told = T T := (select * from T) union (select x.A, y.B from G x, Δ y where x.B = y.A) Δ := T – Told } Output T

Optimization: “semi-naïve” evaluation

31 ¡

Use at least one new tuple (from Δ) every time!

<pseudo-code>

slide-32
SLIDE 32

a b c d e

Example

32 ¡

T1 and Δ1

slide-33
SLIDE 33

a b c d e

T1 and Δ2

Example

33 ¡

No longer recompute <c,b> but recompute <c,d>

slide-34
SLIDE 34

a b c d e

Example

34 ¡

T1 and Δ3

No longer recompute <a,d> but recompute <c,e>

slide-35
SLIDE 35

a b c d e

Example

35 ¡

T4 = T3 and Δ4 = ∅: Stop!

slide-36
SLIDE 36

T := G Δ := G while Δ ≠ ∅ do { Told = T T := (select * from T) union (select x.A, y.B from T x, T y where x.B = y.A) Δ := T – Told } Output T

Faster Convergence (double recursion)

36 ¡

Converges in log(diameter(G)) iterations

<pseudo-code>

slide-37
SLIDE 37

a0 a1 a2 a3 a4 a5 a6 a7 a8

Focus on computing <a0,a8>

Example

37 ¡

slide-38
SLIDE 38

a0 a1 a2 a3 a4 a5 a6 a7 a8

Focus on computing <a0,a8>

Example

38 ¡

slide-39
SLIDE 39

a0 a1 a2 a3 a4 a5 a6 a7 a8

Focus on computing <a0,a8>

Example

39 ¡

slide-40
SLIDE 40

a0 a1 a2 a3 a4 a5 a6 a7 a8

Focus on computing <a0,a8>

Example

40 ¡

slide-41
SLIDE 41

a0 a1 a2 a3 a4 a5 a6 a7 a8

Compare to linear recursion (first program)

Example

41 ¡

slide-42
SLIDE 42

a0 a1 a2 a3 a4 a5 a6 a7 a8

Compare to linear recursion (first program)

Example

42 ¡

slide-43
SLIDE 43

a0 a1 a2 a3 a4 a5 a6 a7 a8

Compare to linear recursion (first program)

Example

43 ¡

slide-44
SLIDE 44

a0 a1 a2 a3 a4 a5 a6 a7 a8

Compare to linear recursion (first program)

Example

44 ¡

slide-45
SLIDE 45

a0 a1 a2 a3 a4 a5 a6 a7 a8

Compare to linear recursion (first program)

Example

45 ¡

slide-46
SLIDE 46

a0 a1 a2 a3 a4 a5 a6 a7 a8

Compare to linear recursion (first program)

Example

46 ¡

slide-47
SLIDE 47

a0 a1 a2 a3 a4 a5 a6 a7 a8

Compare to linear recursion (first program)

Example

47 ¡

slide-48
SLIDE 48

a0 a1 a2 a3 a4 a5 a6 a7 a8

Compare to linear recursion (first program)

Example

48 ¡

slide-49
SLIDE 49

a0 a1 a2 a3 a4 a5 a6 a7 a8

Compare to linear recursion (first program)

Example

49 ¡

slide-50
SLIDE 50

T := G Δ := G while Δ ≠ ∅ do { Told = T T := (select * from T) union (select x.A, y.B from Δ x, T y where x.B = y.A) union (select x.A, y.B from T x, Δ y where x.B = y.A) Δ := T – Told } Output T

<pseudo-code>

Optimization: “semi-naïve” evaluation

50 ¡

Again, use at least one new tuple (from Δ) every time!

slide-51
SLIDE 51

JDBC

  • Java Database Connectivity
  • Allows SQL to be executed from within Java

programs

  • Similar to embedded SQL with the following

difference:

  • Embedded SQL: SQL processed at compile time
  • JDBC: SQL interpreted at run-time

51 ¡