Database Reference
In-Depth Information
europe
country
(
Scotland
)
country
(
England
)
ruler
(
James V
)
ruler
(
Mary I
)
ruler
(
James VI&I
)
ruler
(
Charles I
)
ruler
(
Elizabeth I
)
ruler
(
James VI&I
)
ruler
(
Charles I
)
(a) Tree
T
countries
r
monarch
(
James V
)
monarch
(
Mary I
)
monarch
(
Elisabeth I
)
monarch
(
James VI&I
)
monarch
(
Charles I
)
kingdom
(
Scotland
)
kingdom
(
Scotland
)
kingdom
(
England
)
kingdom
(
England
)
kingdom
(
Scotland
)
kingdom
(
England
)
kingdom
(
Scotland
)
(b) Tree
T
monarchs
Figure 14.1 A
TQL
example:
T
monarchs
=
Q
2
(
T
countries
) for
Q
2
in
Example 14.1
Suppose that we want to list countries ruled by Charles I. This can be done with the
following query
Q
1
:
r
[
for europe
/
country
(
x
)
/
ruler
(
“Charles I”
)
return
country
(
x
)]
.
The next query will involve nesting. Suppose we want to output the tree
T
monarchs
given in
Figure 14.1
: it restructures the input by combining, for each monarch, all his/her kingdoms.
The
TQL
query for this transformation is
Q
2
=
r
[
q
],where
q
is the following forest query:
for europe
//
ruler
(
x
)
return
monarch
(
x
)
for r
/
country
(
y
)
/
ruler
(
x
)
return
kingdom
(
y
)
.
As these examples indicate, patterns in queries must use constants in addition to vari-
ables. In general, we shall only use patterns with child and descendant axes, wildcard, and
constants. We denote the class of such patterns by
Π
p
(C
ONST
,
⇓
,
=) (i.e., the set of pure
patterns from
,
=) that can reuse variables and use constants). Formally, these are given
by the grammar below:
Π
(
⇓
π
:=
(
x
)[
λ
]
|
(
x
)
(patterns)
(14.1)
λ
:=
π
|
//
π
|
λ
,
λ
(sets)
where tuples
x
can mix both variables and constants from C
ONST
. We sometimes write
π
b
,etc.,
(
a
,
x
) to be explicit about constants and variables used in patterns; of course
a
,
will stand for tuples of constants, and
x
,
y
, etc., for tuples of variables.
Now we are ready to see the formal definition of
TQL
.