Constraint

Query

Languages

Paris

C.

Gabriel

M.

Peter

Technical

Report

No

October

University

Box

Providence

RI

T.J.

Watson

Research

Center

Yorktown

Heights

NY

University

Box

Providence

RI

CONSTRAINT

QUERY

LANGUAGES

Paris

C.

Kanellakisy

Gabriel

M.

Kuperz

Peter

Z.

Reveszx

July

Abstract

We

investigate

the

relationship

between

programming

with

constraints

and

database

query

languages

We

show

that

efficient

declarative

database

programming

can

be

combined

with

efficient

constraint

solving

The

key

intuition

is

that

the

generalization

of

a

ground

fact

or

tuple

is

a

conjunction

of

constraints

over

a

small

number

of

variables

We

describe

the

basic

Constraint

Query

Language

design

principles

and

illustrate

them

with

four

classes

of

constraints

real

polynomial

inequalities

dense

linear

order

inequalities

equalities

over

an

infinite

domain

and

boolean

equalities

For

the

analysis

we

use

quantifier

elimination

techniques

from

logic

and

the

concept

of

data

complexity

from

database

theory

This

framework

is

applicable

to

managing

spatial

data

and

can

be

combined

with

existing

multidimensional

searching

algorithms

and

data

structures

Introduction

Motivation

and

Framework

Q

What

's

in

a

tuple

A

Constraints

Constraint

programming

paradigms

are

inherently

declarative

since

they

describe

computations

by

specifying

how

these

computations

are

constrained

-LSB-

-RSB-

A

major

recent

development

in

logic

programming

systems

is

the

integration

of

logic

and

constraint

paradigms

A

preliminary

version

of

the

results

in

this

paper

appeared

in

-LSB-

-RSB-

This

is

also

a

revision

with

additional

material

of

Brown

University

technical

report

yBrown

University

Providence

RI

Research

was

supported

by

IBM

by

an

Alfred

P.

Sloan

Fellowship

and

by

ONR

grants

ARPA

Order

No.

and

ARPA

Order

No.

zIBM

T.J.

Watson

Research

Center

Yorktown

Heights

NY

xBrown

University

Providence

RI

Research

was

supported

by

NSF

grant

and

by

NSF-INRIA

grant

e.g.

in

CLP

-LSB-

-RSB-

in

Prolog

III

-LSB-

-RSB-

and

in

CHIP

-LSB-

-RSB-

for

a

recent

survey

see

-LSB-

-RSB-

One

intuitive

reason

for

this

successful

integration

is

as

follows

A

strength

of

Prolog

is

its

top-down

depth-first

search

strategy

The

operation

of

first-order

term

unification

at

the

forefront

of

this

search

is

a

special

form

of

efficient

constraint

solving

Additional

constraint

solving

increases

the

depth

of

the

search

and

thus

the

effectiveness

of

the

approach

The

declarative

style

of

database

query

languages

is

an

important

aspect

of

database

systems

Indeed

having

such

a

language

for

ad-hoc

database

querying

is

a

requirement

today

It

is

rather

surprising

that

constraint

programming

has

not

really

influenced

database

query

language

design

There

has

been

some

previous

research

on

the

power

of

constraints

for

the

implicit

specification

of

temporal

data

-LSB-

-RSB-

for

extending

relational

algebra

-LSB-

-RSB-

and

for

magic

set

evaluation

-LSB-

-RSB-

but

no

overall

design

principles

The

bottom-up

and

set-at-a-time

style

of

evaluation

emphasized

in

databases

and

more

recently

in

knowledge

bases

seems

to

contradict

the

top-down

depth-first

intuition

behind

Constraint

Logic

Programming

The

main

contribution

of

this

paper

is

to

show

that

it

is

possible

to

bridge

the

gap

between

bottom-up

efficient

declarative

database

programming

and

efficient

constraint

solving

A

key

intuition

comes

from

Constraint

Logic

Programming

a

conjunction

of

constraints

is

the

correct

generalization

of

the

ground

fact

The

technical

tools

for

this

integration

are

data

complexity

-LSB-

-RSB-

from

database

theory

and

quantifier

elimination

methods

from

mathematical

logic

Let

us

provide

some

motivation

for

the

integration

of

database

and

constraint

solving

methods

Manipulation

of

spatial

data

is

an

important

application

area

-LRB-

e.g.

spatial

or

geographic

databases

-RRB-

that

requires

both

relational

query

language

techniques

and

arithmetic

calculations

Indexes

for

range

searching

and

modeling

of

complex

structures

have

been

used

to

bridge

the

gap

between

declarative

accessing

of

large

volumes

of

spatial

data

and

performing

common

computational

geometry

tasks

However

even

with

these

extensions

arithmetic

calculations

have

not

been

given

first-class

citizen

status

in

the

various

query

languages

used

and

the

integration

of

language

and

application

has

been

loose

For

an

example

of

tight

integration

of

application

language

paradigm

and

implementation

let

us

review

the

relational

data

model

In

the

relational

data

model

-LSB-

-RSB-

an

important

application

area

-LRB-

data

processing

-RRB-

is

described

in

a

declarative

style

-LRB-

relational

calculus

-RRB-

so

that

it

can

be

automatically

and

efficiently

translated

into

procedural

style

-LRB-

relational

algebra

-RRB-

Program

evaluation

is

bottom-up

and

setat-a-time

as

opposed

to

top-down

and

tuple-at-a-time

because

the

applications

involve

massive

amounts

of

structured

data

This

evaluation

may

be

optimized

e.g.

via

algebraic

transformations

selection

propagation

etc.

It

may

be

performed

in-core

in

PTIME

because

of

the

low

complexity

of

the

calculations

expressed

Most

importantly

it

may

be

implemented

efficiently

with

large

amounts

of

data

in

secondary

storage

via

indexing

and

hashing

Our

claim

in

this

paper

is

that

by

generalizing

relational

formalisms

to

constraint

formalisms

it

is

in

principle

possible

to

generalize

all

the

key

features

of

the

relational

data

model

-LRB-

-RRB-

The

language

framework

that

we

propose

preserves

the

declarative

style

and

the

efficiency

of

relational

database

languages

-LRB-

-RRB-

The

possible

applications

of

constraint

databases

include

both

data

processing

and

numerical

processing

of

spatial

data

-LRB-

-RRB-

The

implementation

technology

of

spatial

access

methods

-LRB-

see

-LSB-

-RSB-

-RRB-

naturally

matches

the

new

formalism

OE

-LRB-

db

constraints

-RRB-

query

program

database

outputdatabase

input

tuple

V

constraints

closed

form

evaluated

bottom-up

low

data

complexity

Figure

The

CQL

database

framework

We

will

now

explain

our

new

framework

and

give

arguments

in

support

of

the

above

-LRB-

-RRB-

-LRB-

-RRB-

What

could

be

sound

criteria

for

achieving

language

integration

of

data

processing

and

other

computations

such

as

arithmetic

calculations

Here

are

some

examples

-LRB-

a

-RRB-

Preserving

the

declarative

language

style

is

desirable

-LRB-

b

-RRB-

Additional

expressive

power

is

desirable

but

must

come

without

a

serious

loss

of

efficiency

-LRB-

c

-RRB-

Bottom-up

processing

is

desirable

since

it

is

a

good

candidate

for

many

optimizations

These

criteria

are

satisfied

by

the

Constraint

Query

Language

-LRB-

CQL

-RRB-

design

principles

outlined

below

-LRB-

and

illustrated

in

Figure

-RRB-

The

formal

definitions

are

in

Section

ffl

A

generalized

k-tuple

is

a

quantifier-free

conjunction

of

constraints

on

k

variables

which

range

over

a

domain

D

In

the

relational

database

model

R

-LRB-

-RRB-

is

a

tuple

of

arity

It

can

be

thought

of

as

a

single

point

in

space

and

also

as

R

-LRB-

x

y

-RRB-

with

x

and

y

where

x

y

range

over

some

finite

domain

In

our

framework

R

-LRB-

x

y

-RRB-

with

-LRB-

x

y

x

-RRB-

is

a

generalized

tuple

of

arity

and

so

is

R

-LRB-

x

y

-RRB-

with

x

y

where

x

y

range

over

the

rational

or

the

real

numbers

Hence

a

generalized

tuple

of

arity

k

is

a

finite

representation

of

a

possibly

infinite

set

of

tuples

of

arity

k.

ffl

A

generalized

relation

of

arity

k

is

a

finite

set

of

generalized

k-tuples

with

each

k-tuple

over

the

same

variables

It

is

a

disjunction

of

conjunctions

-LRB-

i.e.

in

disjunctive

normal

form

DNF

-RRB-

of

constraints

which

uses

at

most

k

variables

ranging

over

domain

D

A

generalized

database

is

a

finite

set

of

generalized

relations

Each

generalized

relation

of

arity

k

is

a

quantifier-free

DNF

formula

of

the

logical

theory

of

con

straints

used

It

contains

at

most

k

distinct

variables

and

describes

a

possibly

infinite

set

of

arity

k

tuples

-LRB-

or

points

in

k-dimensional

space

Dk

-RRB-

ffl

The

syntax

of

a

CQL

is

the

union

of

an

existing

database

query

language

and

a

decidable

logical

theory

For

example

Relational

calculus

-LSB-

-RSB-

the

theory

of

real

closed

fields

-LSB-

-RSB-

-LRB-

Section

-RRB-

Inflationary

Datalog

-LSB-

-RSB-

the

theory

of

dense

linear

order

with

constants

-LRB-

Section

-RRB-

Inflationary

Datalog

the

theory

of

equality

on

an

infinite

domain

with

constants

-LRB-

Section

-RRB-

and

Datalog

boolean

equations

-LRB-

Section

-RRB-

In

each

of

these

cases

we

combine

in

the

obvious

way

the

syntax

of

the

database

language

and

the

logical

theory

ffl

The

semantics

of

a

CQL

is

based

on

that

of

the

decidable

logical

theory

by

interpreting

database

atoms

as

shorthands

for

formulas

of

the

theory

Let

OE

OE

-LRB-

xm

-RRB-

be

a

query

program

using

free

variables

xm

Let

predicate

symbols

Rn

in

OE

name

the

input

generalized

relations

and

let

rn

be

corresponding

input

generalized

relations

We

interpret

the

program

in

the

context

of

such

an

input

Let

OE

-LSB-

rn

Rn

-RSB-

be

the

formula

of

the

theory

that

is

obtained

by

replacing

in

OE

each

database

atom

Ri

-LRB-

zk

-RRB-

by

the

DNF

formula

for

input

generalized

relation

ri

with

its

variables

appropriately

renamed

to

zk

-LRB-

Note

that

without

loss

of

generality

an

occurrence

of

a

database

atom

in

OE

is

of

the

form

Ri

-LRB-

zk

-RRB-

i

n

where

Ri

is

a

predicate

symbol

of

arity

k

and

zk

are

distinct

variables

this

is

because

in

our

framework

we

can

always

use

equality

constraints

among

variables

in

OE

-RRB-

Let

D

be

the

constraint

domain

Query

program

OE

OE

-LRB-

xm

-RRB-

applied

to

input

database

rn

is

a

formula

of

the

logical

theory

of

constraints

used

i.e.

OE

-LSB-

rn

Rn

-RSB-

The

output

is

the

possibly

infinite

set

of

points

in

m-dimensional

space

Dm

such

that

instantiating

the

free

variables

xm

of

this

formula

to

any

one

of

these

points

makes

the

formula

true

ffl

For

each

input

the

queries

must

be

evaluable

in

closed

form

and

bottom-up

By

closed

form

we

mean

that

the

output

of

any

query

program

applied

to

any

input

generalized

relations

must

be

a

generalized

relation

The

analogue

for

the

relational

model

is

that

relations

are

finite

structures

and

queries

are

supposed

to

preserve

this

finiteness

This

is

a

requirement

that

creates

various

safety

problems

in

relational

databases

-LSB-

-RSB-

The

precise

analogue

in

relational

databases

is

the

notion

of

weak

safety

of

-LSB-

-RSB-

In

our

framework

it

is

finiteness

of

representation

of

constraints

that

must

be

preserved

Evaluation

of

a

query

corresponds

to

an

instance

of

a

decision

problem

Interestingly

many

quantifier

elimination

procedures

realize

the

goal

of

closed

form

Also

they

use

induction

on

the

structure

of

formulas

which

leads

to

bottom-up

evaluation

ffl

For

each

input

the

queries

must

be

evaluable

efficiently

in

the

input

size

i.e.

with

low

data

complexity

Database

atomic

formulas

indicate

in

the

declarative

query

language

itself

the

parts

that

can

grow

asymptotically

versus

the

parts

that

are

constant-size

By

fixing

the

program

size

and

letting

the

database

grow

we

can

prove

that

the

evaluation

can

be

performed

in

PTIME

or

in

NC

or

in

LOGSPACE

depending

on

the

constraints

that

we

consider

-LRB-

for

the

various

complexity

classes

see

-LSB-

-RSB-

-RRB-

-LRB-

-RRB-

Let

us

motivate

these

design

principles

by

a

very

common

task

from

computational

geometry

and

spatial

databases

the

problem

of

computing

all

rectangle

intersections

-LSB-

-RSB-

Note

that

the

theory

of

constraints

used

in

this

simple

but

very

common

example

is

the

theory

of

dense

linear

order

with

constants

-LRB-

see

Section

-RRB-

s

-LRB-

-RRB-

s

-LRB-

-RRB-

s

-LRB-

-RRB-

s

-LRB-

-RRB-

s

-LRB-

-RRB-

s

-LRB-

-RRB-

s

-LRB-

-RRB-

s

-LRB-

-RRB-

Figure

Rectangle

intersection

Example

The

database

consists

of

a

set

of

rectangles

in

the

plane

and

we

want

to

compute

all

pairs

of

distinct

intersecting

rectangles

This

query

is

expressible

in

a

relational

data

model

that

has

a

interpreted

predicate

One

possibility

is

to

store

the

data

in

a

relation

named

R

This

relation

will

contain

tuples

of

the

form

-LRB-

n

a

b

c

d

-RRB-

and

such

a

tuple

will

mean

that

n

is

the

name

of

the

rectangle

with

corners

at

-LRB-

a

b

-RRB-

-LRB-

a

d

-RRB-

-LRB-

c

b

-RRB-

and

-LRB-

c

d

-RRB-

We

can

express

the

intersection

query

as

f

-LRB-

-RRB-

-LRB-

-RRB-

-LRB-

R

-LRB-

-RRB-

R

-LRB-

-RRB-

-LRB-

y

-RRB-

-LRB-

x

y

x

y

-RRB-

-RRB-

g

To

see

that

this

query

expresses

rectangle

intersection

note

the

following

the

two

rectangles

and

share

a

point

if

and

only

if

they

share

a

point

whose

coordinates

belong

to

the

set

This

can

be

shown

by

exhaustively

examining

all

possible

intersecting

configurations

Thus

one

could

eliminate

the

-LRB-

y

-RRB-

quantification

altogether

and

replace

it

by

a

boolean

combination

of

atomic

formulas

involving

the

various

cases

of

intersecting

rectangles

The

above

query

program

is

particular

to

rectangles

and

does

not

work

for

triangles

or

for

interiors

of

rectangles

Recall

that

in

the

relational

data

model

quantification

is

over

constants

that

appear

in

the

database

By

contrast

if

we

use

generalized

relations

the

query

can

be

expressed

very

simply

-LRB-

without

case

analysis

-RRB-

and

applies

to

more

general

shapes

Let

R

-LRB-

z

x

y

-RRB-

be

a

ternary

relation

We

interpret

R

-LRB-

z

x

y

-RRB-

to

mean

that

-LRB-

x

y

-RRB-

is

a

point

in

the

rectangle

with

name

z

The

rectangle

that

was

stored

above

by

-LRB-

n

a

b

c

d

-RRB-

would

now

be

stored

as

the

generalized

tuple

-LRB-

z

n

-RRB-

-LRB-

a

x

c

-RRB-

-LRB-

b

y

d

-RRB-

The

set

of

all

intersecting

rectangles

can

now

be

expressed

as

f

-LRB-

-RRB-

-LRB-

y

-RRB-

-LRB-

R

-LRB-

x

y

-RRB-

R

-LRB-

x

y

-RRB-

g

The

simplicity

of

this

program

is

due

to

the

ability

in

CQL

to

describe

and

name

point-sets

using

constraints

The

same

program

can

be

used

for

intersecting

triangles

In

this

paper

we

shall

argue

that

this

simplicity

of

expression

can

be

combined

with

efficient

evaluation

techniques

even

if

quantification

is

over

an

infinite

domain

We

refer

to

Section

for

more

concrete

examples

from

computational

geometry

Other

examples

are

presented

in

Section

-LRB-

the

balanced

checkbook

example

-RRB-

and

Section

-LRB-

the

adder

circuit

example

-RRB-

Remark

A

The

constraint

theories

that

we

investigate

here

are

applicable

to

spatial

databases

Temporal

databases

require

the

development

of

analogous

frameworks

for

the

theory

of

discrete

linear

order

with

constants

e.g.

see

-LSB-

-RSB-

For

recent

developments

of

constraint-based

approaches

to

temporal

databases

we

refer

to

-LSB-

-RSB-

Our

results

on

linear

order

only

apply

to

dense

linear

order

The

case

of

discrete

or

integer

linear

order

is

analyzed

in

-LSB-

-RSB-

Remark

B

The

key

concept

in

CQL

illustrated

by

Example

is

that

constraints

describe

point-sets

such

that

all

their

points

are

in

the

database

With

the

appropriate

constraint

theory

these

point-sets

are

accurate

-LRB-

and

perhaps

the

most

intuitive

-RRB-

representations

of

spatial

objects

Our

framework

is

thus

one

of

complete

information

Constraint

logic

programming

paradigms

are

currently

attracting

a

great

deal

of

attention

in

languages

for

operations

research

applications

-LSB-

-RSB-

and

have

also

impacted

the

field

of

concurrent

programming

language

design

-LSB-

-RSB-

The

use

of

constraints

for

operations

research

and

for

concurrency

is

sometimes

semantically

different

from

their

use

in

our

framework

For

example

Constraints

can

be

used

to

represent

the

many

possible

states

-LRB-

of

which

one

is

true

-RRB-

of

a

set

of

concurrent

processes

Each

individual

concurrent

process

maintains

and

manipulates

constraints

that

describe

the

partial

information

it

has

about

the

state

of

all

processes

-LRB-

-RRB-

The

language

framework

of

the

relational

data

model

does

have

low

data

complexity

but

does

not

account

for

searches

that

are

logarithmic

or

faster

in

the

sizes

of

input

relations

Without

the

ability

to

perform

such

searches

relational

databases

would

have

been

impractical

Very

efficient

use

of

secondary

storage

is

an

additional

requirement

beyond

low

data

complexity

whose

satisfaction

greatly

contributes

to

relational

technology

B-trees

and

their

variants

B

trees

-LSB-

-RSB-

are

examples

of

important

data

structures

for

implementing

relational

databases

In

particular

let

each

secondary

memory

access

transmit

B

units

of

data

let

r

be

a

relation

with

N

tuples

and

let

us

have

a

B

tree

on

the

attribute

x

of

r

The

space

used

in

this

case

is

O

-LRB-

N

-RRB-

The

following

operations

define

the

problem

of

searching

on

relational

database

attribute

x

with

the

corresponding

performance

bounds

using

a

B

tree

on

x

-LRB-

i

-RRB-

Find

all

tuples

such

that

for

their

x

attribute

-LRB-

x

-RRB-

If

the

output

size

is

K

tuples

then

this

range

searching

is

in

worst-case

O

-LRB-

logB

N

K

B

-RRB-

secondary

memory

accesses

If

and

x

is

a

key

then

this

is

key-based

searching

-LRB-

ii

-RRB-

Insert

or

delete

a

given

tuple

These

are

in

worst-case

O

-LRB-

logB

N

-RRB-

secondary

memory

accesses

The

problem

of

k-dimensional

searching

on

relational

database

attributes

xk

generalizes

searching

to

k

attributes

with

range

searching

on

k-dimensional

intervals

It

is

a

central

problem

in

spatial

databases

for

which

there

are

many

solutions

with

good

secondary

memory

access

performance

e.g.

grid-files

quad-trees

R-trees

-LRB-

see

the

surveys

-LSB-

-RSB-

-RRB-

For

generalized

databases

we

can

define

an

analogous

problem

of

searching

on

generalized

database

attribute

x

using

the

operations

-LRB-

i

-RRB-

Find

a

generalized

database

that

represents

all

tuples

of

the

input

generalized

database

such

that

their

x

attribute

satisfies

-LRB-

x

-RRB-

-LRB-

ii

-RRB-

Insert

or

delete

a

given

generalized

tuple

If

-LRB-

x

-RRB-

is

a

constraint

of

our

CQL

then

there

is

a

trivial

but

inefficient

solution

to

the

problem

of

searching

on

generalized

database

attribute

x

One

can

add

constraint

-LRB-

x

-RRB-

to

every

generalized

tuple

-LRB-

i.e.

conjunction

of

constraints

-RRB-

and

naively

insert

or

delete

generalized

tuples

in

a

table

This

would

involve

a

linear

scan

of

the

generalized

relation

and

introduces

a

lot

of

redundancy

in

the

representation

In

many

cases

the

projection

of

any

generalized

tuple

on

x

is

one

interval

-LRB-

a

x

-RRB-

This

is

true

for

Example

for

our

CQL

's

with

dense

linear

order

for

relational

calculus

with

linear

inequalities

over

the

reals

and

in

general

when

a

generalized

tuple

represents

a

convex

set

Under

such

natural

assumptions

there

is

a

better

solution

for

searching

on

generalized

database

attribute

x.

ffl

A

generalized

index

is

a

set

of

intervals

where

each

interval

is

associated

with

a

generalized

tuple

Each

interval

-LRB-

a

x

-RRB-

in

the

index

is

the

projection

on

x

of

its

associated

generalized

tuple

The

two

endpoint

a

representation

of

an

interval

is

a

fixed

length

generalized

key

ffl

Finding

a

generalized

database

that

represents

all

tuples

of

the

input

generalized

database

such

that

their

x

attribute

satisfies

-LRB-

x

-RRB-

can

be

performed

by

adding

constraint

-LRB-

x

-RRB-

to

only

those

generalized

tuples

whose

generalized

keys

have

a

non-empty

intersection

with

it

ffl

Inserting

or

deleting

a

given

generalized

tuple

are

performed

by

computing

its

projection

and

inserting

or

deleting

intervals

from

a

set

of

intervals

The

use

of

generalized

indexes

reduces

redundancy

of

representation

and

transforms

searching

on

generalized

database

attribute

x

into

the

problem

of

online

intersections

in

a

dynamic

set

of

intervals

This

is

a

well-known

problem

with

many

elegant

solutions

from

computational

geometry

-LSB-

-RSB-

It

is

a

special

case

of

searching

in

relational

databases

called

searching

in

-LSB-

-RSB-

For

example

the

priority

search

trees

of

-LSB-

-RSB-

are

a

linear

space

data

structure

with

logarithmic-time

update

and

search

algorithms

for

in-core

processing

Grid-files

R-trees

and

quad-trees

have

all

been

used

for

solving

this

problem

with

good

secondary

memory

access

performance

In

summary

current

spatial

database

access

methods

are

applicable

to

indexing

in

our

CQL

framework

because

If

-LRB-

x

-RRB-

is

a

constraint

of

our

CQL

and

the

projection

of

any

generalized

tuple

on

x

is

an

interval

-LRB-

a

x

-RRB-

then

the

problem

of

searching

on

generalized

database

attribute

x

is

a

special

case

of

searching

in

relational

databases

We

will

now

concentrate

on

the

technical

development

of

the

CQL

framework

and

on

the

existence

of

natural

constraint

query

languages

with

closed

form

bottom-up

evaluation

of

low

data

complexity

Basic

Definitions

The

framework

of

generalized

relations

with

corresponding

query

languages

can

be

applied

to

many

different

classes

of

constraints

Definition

The

classes

we

consider

in

Sections

are

as

follows

Real

polynomial

inequality

constraints

are

all

formulas

-LRB-

and

their

negations

-RRB-

of

the

form

p

-LRB-

xj

-RRB-

where

p

is

a

polynomial

with

real

coefficients

variables

xj

and

is

one

of

-LRB-

or

its

negation

>=-RRB-

The

domain

D

is

the

set

of

real

numbers

and

function

symbols

predicate

symbols

and

constants

are

intepreted

in

the

standard

way

over

D.

Dense

linear

order

inequality

constraints

are

all

formulas

-LRB-

and

their

negations

-RRB-

of

the

form

x?y

and

x?c

where

x

y

are

variables

c

is

a

constant

and

is

one

of

-LRB-

or

its

negation

>=-RRB-

We

assume

D

is

a

countably

infinite

set

-LRB-

e.g.

the

rational

numbers

-RRB-

with

a

binary

relation

which

is

a

dense

linear

order

Constants

and

are

interpreted

as

elements

equality

the

dense

linear

order

and

the

irreflexive

dense

linear

order

of

D.

Equality

constraints

over

an

infinite

domain

are

all

formulas

-LRB-

and

their

negations

-RRB-

of

the

form

x?y

and

x?c

where

x

y

are

variables

c

is

a

constant

and

is

-LRB-

or

=-RRB-

We

assume

D

is

a

countably

infinite

set

-LRB-

e.g.

the

integer

numbers

-RRB-

but

without

order

Constants

and

are

interpreted

as

elements

and

equality

of

D

In

Section

we

present

the

definitions

and

analysis

for

boolean

equality

constraints

Remark

C

There

are

of

course

other

classes

of

constraints

that

could

illustrate

the

CQL

framework

e.g.

linear

inequalities

over

the

reals

or

discrete

linear

order

constraints

However

the

examples

we

have

chosen

illustrate

all

of

our

analytical

techniques

-LRB-

a

-RRB-

Real

polynomial

inequality

constraints

are

quite

general

They

show

the

possible

applicability

of

the

framework

to

problems

of

computational

geometry

and

the

limits

of

data

complexity

analysis

It

is

possible

to

combine

them

with

relational

calculus

but

not

with

recursive

formalisms

-LRB-

b

-RRB-

Dense

linear

order

constraints

are

also

very

general

since

one

may

use

them

to

simulate

any

PTIME

computation

-LRB-

as

in

-LSB-

-RSB-

and

-LSB-

-RSB-

-RRB-

We

devote

a

large

part

of

our

analysis

to

this

case

because

it

best

illustrates

the

desired

integration

with

relational

calculus

and

various

recursive

formalisms

Discrete

linear

order

is

much

harder

to

combine

with

recursion

-LSB-

-RSB-

-LRB-

c

-RRB-

Equality

constraints

over

an

infinite

domain

were

chosen

as

the

simplest

generalization

of

the

relational

data

model

The

analysis

here

is

very

close

to

that

of

dense

linear

order

constraints

-LRB-

d

-RRB-

Finally

boolean

equality

constraints

capture

important

operations

research

applications

although

their

CQL

is

not

as

efficient

as

in

the

other

cases

Definition

Let

be

a

class

of

constraints

A

generalized

k-tuple

-LRB-

over

variables

xk

-RRB-

is

a

finite

conjunction

'N

where

each

i

i

N

is

a

constraint

in

Furthermore

the

variables

in

each

i

are

all

free

and

among

xk

A

generalized

relation

of

arity

k

is

a

finite

set

r

f

Mg

where

each

i

i

M

is

a

generalized

k-tuple

over

the

same

variables

xk

The

formula

corresponding

to

a

generalized

relation

r

is

the

disjunction

M

We

use

OEr

to

denote

the

quantifier-free

formula

corresponding

to

relation

r.

A

generalized

database

is

a

finite

set

of

generalized

relations

In

database

theory

a

k-ary

relation

r

is

a

finite

set

of

k-tuples

-LRB-

or

points

in

a

k-dimensional

space

-RRB-

and

a

database

is

a

finite

set

of

relations

However

the

relational

calculus

and

algebra

can

be

developed

without

the

finiteness

assumption

for

relations

We

will

use

the

term

unrestricted

relation

for

finite

or

infinite

sets

of

points

in

a

k-dimensional

space

It

is

possible

to

develop

query

languages

using

such

unrestricted

relations

-LRB-

e.g.

see

-LSB-

-RSB-

-RRB-

In

order

to

be

able

to

do

something

useful

with

such

unrestricted

relations

we

need

a

finite

representation

that

we

can

manipulate

This

is

exactly

what

the

generalized

tuples

provide

Definition

Let

be

a

class

of

constraints

interpreted

over

domain

D

r

a

generalized

relation

of

arity

k

with

constraints

in

and

OEr

OEr

-LRB-

xk

-RRB-

the

formula

corresponding

to

r

with

free

variables

xk

The

generalized

relation

r

represents

the

unrestricted

k-ary

relation

which

consists

of

all

-LRB-

ak

-RRB-

in

Dk

such

that

OEr

-LRB-

ak

-RRB-

is

true

A

generalized

database

represents

the

finite

set

of

unrestricted

relations

that

are

represented

by

its

generalized

relations

Example

This

is

a

generalization

of

the

relational

data

model

Let

relation

r

consist

of

the

tuples

-LRB-

-RRB-

and

-LRB-

-RRB-

These

tuples

are

equivalent

to

the

generalized

x

y

and

x

y

Therefore

the

r

corresponds

to

the

set

fx

y

x

y

and

the

formula

OEr

-LRB-

x

y

-RRB-

-LRB-

x

y

-RRB-

It

should

be

clear

that

a

point

-LRB-

x

y

-RRB-

is

in

the

generalized

relation

iff

it

satisfies

the

corresponding

formula

Let

us

illustrate

our

framework

using

real

polynomial

inequality

constraints

Let

generalized

relation

r

consist

of

two

generalized

tuples

-LRB-

y

x

x

y

-RRB-

and

-LRB-

x

y

-RRB-

Corresponding

to

this

r

is

the

DNF

formula

OEr

-LRB-

y

x

x

y

-RRB-

-LRB-

x

y

-RRB-

OEr

describes

an

infinite

set

of

points

in

space

namely

the

half

plane

x

y

and

the

line

y

x

without

the

point

x

y

Note

that

the

representation

of

an

unrestricted

relation

by

a

finite

set

of

generalized

tuples

need

not

be

uniquely

defined

Relational

calculus

constraints

We

present

a

short

but

self-contained

description

of

the

relational

calculus

with

a

given

a

class

of

constraints

For

more

details

on

the

relational

calculus

in

database

theory

see

-LSB-

-RSB-

Definition

Let

be

a

class

of

constraints

Let

Ri

be

predicate

symbols

each

with

a

fixed

arity

A

relational

calculus

query

program

is

a

formula

of

the

first-order

predicate

calculus

with

equality

such

that

its

atomic

formulas

are

-LRB-

-RRB-

of

the

form

Ri

-LRB-

xj

-RRB-

where

j

is

the

arity

of

predicate

symbol

Ri

or

-LRB-

-RRB-

formulas

from

the

class

of

constraints

Example

Let

be

the

class

of

dense

linear

order

constraints

If

is

a

predicate

symbol

of

arity

then

the

following

is

a

query

OE

-LRB-

-RRB-

-LRB-

-RRB-

-LRB-

-LRB-

y

-RRB-

-LRB-

y

-RRB-

-LRB-

-RRB-

-LRB-

y

-RRB-

-RRB-

In

order

to

formally

define

its

meaning

we

need

interpretations

for

the

predicate

symbols

These

will

come

from

input

generalized

relations

We

also

need

interpretations

of

the

symbols

in

the

constraints

These

will

come

from

the

particular

theory

of

constraints

used

Definition

Let

D

be

the

domain

of

constraint

class

and

ffi

the

interpretation

of

the

symbols

in

these

constraints

Let

OE

be

a

relational

calculus

query

program

with

predicate

symbols

Rn

and

with

free

variables

xm

Let

rn

be

generalized

relations

of

the

same

arities

as

Rn

These

generalized

relations

represent

unrestricted

relations

aen

-LRB-

where

aei

is

the

set

of

points

that

satisfy

OEri

-RRB-

Using

the

standard

first

order

meaning

of

j

we

define

ae

OE

-LSB-

aen

Rn

-RSB-

am

Dm

j

hD

ffi

aeni

j

OE

-LRB-

am

-RRB-

g

The

query

expressed

by

program

OE

is

defined

as

a

mapping

from

unrestricted

relations

aen

-LRB-

represented

by

the

input

generalized

relations

rn

-RRB-

to

an

arity

m

unrestricted

relation

ae

We

also

require

that

ae

be

representable

by

some

generalized

relation

r

of

arity

m.

Although

unrestricted

relation

ae

OE

-LSB-

aen

Rn

-RSB-

is

always

well

defined

the

reader

should

note

that

our

definition

requires

an

additional

closure

condition

Both

input

and

output

should

be

representable

by

generalized

relations

Remark

D

It

is

easy

to

verify

that

this

definition

is

equivalent

to

interpreting

database

atoms

as

shorthands

for

formulas

of

the

theory

of

constraints

as

we

required

in

our

CQL

design

principles

In

other

words

if

we

let

OE

-LSB-

rn

Rn

-RSB-

be

the

result

of

replacing

each

occurrence

of

Ri

in

OE

by

the

formula

OEri

then

OE

-LSB-

aen

Rn

-RSB-

is

precisely

the

set

of

points

that

satisfy

This

formula

however

might

contain

quantifiers

and

even

not

correspond

to

any

generalized

database

So

closure

is

a

non-trivial

condition

Quantifier-elimination

in

the

theory

of

constraints

will

allow

us

to

satisfy

this

condition

Example

For

a

simple

example

where

closure

does

not

hold

consider

real

polynomial

equalities

These

are

constraints

of

the

form

p

-LRB-

xn

-RRB-

where

is

or

Let

R

-LRB-

x

y

-RRB-

be

a

binary

predicate

symbol

for

the

input

generalized

relation

fy

The

result

-LRB-

interpreting

the

generalized

relation

as

an

infinite

set

of

points

-RRB-

of

R

-LRB-

x

y

-RRB-

is

the

set

fyjy

g

which

can

not

be

represented

by

polynomial

equality

constraints

Datalog

constraints

We

now

consider

Datalog

with

constraints

The

syntax

is

that

of

Datalog

-LRB-

e.g.

see

-LSB-

-RSB-

-RRB-

but

we

allow

the

bodies

of

rules

to

contain

constraints

Definition

Let

be

a

class

of

constraints

Let

Ri

be

predicate

symbols

each

with

a

fixed

arity

A

Datalog

query

program

ss

is

a

finite

set

of

rules

of

the

form

tl

the

rule

head

must

be

an

atomic

formula

of

the

form

R

-LRB-

xk

-RRB-

where

R

is

some

predicate

symbol

of

arity

k

The

expressions

tl

the

rule

body

are

either

of

the

form

-LRB-

-RRB-

where

is

some

predicate

symbol

of

arity

or

are

constraints

from

The

predicate

symbols

that

appear

in

heads

of

rules

are

called

intentional

database

predicates

-LRB-

IDBs

-RRB-

and

the

rest

are

called

extensional

database

predicates

-LRB-

EDBs

-RRB-

The

meaning

of

a

Datalog

query

program

ss

on

generalized

relations

rn

that

represent

the

unrestricted

relations

aen

is

the

least

fixpoint

of

the

monotone

mapping

defined

by

a

first-order

formula

OEss

and

aen

The

definition

is

the

same

as

in

the

case

without

constraints

the

only

difference

being

the

use

of

unrestricted

relational

databases

-LSB-

-RSB-

We

present

this

definition

by

example

Example

Consider

the

Datalog

query

program

ss

with

dense

linear

order

constraints

R

-LRB-

x

y

-RRB-

R

-LRB-

x

z

-RRB-

-LRB-

z

y

-RRB-

x

y

y

z

R

-LRB-

x

y

-RRB-

-LRB-

x

y

-RRB-

Apply

this

query

program

to

the

generalized

database

that

represents

the

unrestricted

relation

Then

OEss

is

the

following

first-order

formula

OEss

-LRB-

x

y

-RRB-

-LRB-

x

y

R

-RRB-

-LRB-

x

y

-RRB-

-LRB-

R

-LRB-

x

z

-RRB-

-LRB-

z

y

-RRB-

x

y

y

z

-RRB-

OEss

defines

a

mapping

from

arity

unrestricted

relations

ae

to

arity

unrestricted

relations

Note

that

in

this

formula

is

always

interpreted

as

Predicate

symbol

R

is

singled

out

because

its

interpretation

as

any

value

ae

defines

the

mapping

ae

fa

b

D

ffi

ae

j

OEss

-LRB-

a

b

-RRB-

g

This

mapping

is

monotone

with

respect

to

set

inclusion

for

ae

By

the

Tarski

fixpoint

theorem

it

has

a

least

fixpoint

which

is

the

output

of

the

query

program

applied

to

input

The

mere

existence

of

the

fixpoint

as

guaranteed

by

the

Tarski

fixpoint

theorem

is

not

enough

for

our

purposes

As

in

the

case

of

the

relational

calculus

we

require

that

the

result

of

a

Datalog

query

be

finitely

representable

as

a

generalized

database

We

shall

show

that

this

closure

condition

is

satisfied

by

Datalog

when

we

consider

constraints

from

the

language

of

dense

linear

order

or

equality

over

an

infinite

domain

Unfortunately

as

the

next

example

shows

this

rules

out

the

use

of

Datalog

with

real

polynomial

inequalities

Example

Let

ss

be

the

query

program

that

consists

of

the

rules

S

-LRB-

x

y

-RRB-

R

-LRB-

x

y

-RRB-

and

S

-LRB-

x

y

-RRB-

R

-LRB-

x

z

-RRB-

S

-LRB-

z

y

-RRB-

-LRB-

i.e.

S

is

the

transitive

closure

of

R

-RRB-

If

the

input

r

for

R

consists

of

the

generalized

relation

y

x

then

the

result

of

the

query

is

the

set

of

all

points

-LRB-

x

y

-RRB-

that

satisfy

y

x

for

some

i

This

set

is

not

finitely

representable

in

the

language

of

polynomial

inequality

constraints

Inflationary

Datalog

constraints

The

syntax

is

that

of

Datalog

with

constraints

with

one

addition

We

allow

in

a

rule

body

expressions

of

the

form

-LRB-

-RRB-

where

is

some

predicate

symbol

of

arity

We

give

the

language

inflationary

semantics

-LSB-

-RSB-

In

the

inflationary

semantics

after

each

iteration

the

set

of

facts

derived

is

added

to

the

set

of

facts

that

were

derived

in

the

previous

iterations

We

shall

show

that

the

closure

results

mentioned

above

for

Datalog

with

dense

order

or

with

equality

constraints

hold

with

inflationary

negation

as

well

Remark

E

We

have

given

the

semantics

of

a

Datalog

-LRB-

Datalog

-RRB-

query

program

on

a

generalized

database

as

the

least

fixpoint

of

a

monotone

-LRB-

inflationary

-RRB-

mapping

from

unrestricted

relations

to

unrestricted

relations

It

is

easy

to

verify

that

our

definition

is

equivalent

to

interpreting

EDB

atoms

as

shorthands

for

formulas

of

the

theory

of

constraints

as

we

required

in

our

CQL

design

principles

Various

fragments

of

relational

calculus

and

Datalog

have

been

found

to

be

particularly

useful

in

databases

and

have

been

examined

in

depth

Tableaux

query

programs

form

such

a

fragment

We

provide

definitions

and

examples

for

them

in

Section

and

refer

to

-LSB-

-RSB-

for

a

more

detailed

treatment

Complexity

We

assume

familiarity

with

the

definitions

of

basic

complexity

classes

such

as

LOGSPACE

PTIME

NC

and

-LRB-

see

-LSB-

-RSB-

-RRB-

The

prototypical

logspace-complete

problem

in

is

the

AE-quantified

boolean

formula

problem

Input

a

formula

-LRB-

x

y

-RRB-

where

x

y

are

sets

of

boolean

variables

and

-LRB-

x

y

-RRB-

a

propositional

formula

over

these

variables

Question

is

the

input

formula

true

We

now

define

data

complexity

Our

definition

involves

the

complexity

of

evaluating

some

representation

for

the

output

of

a

fixed

query

Q

given

a

variable

input

generalized

database

This

is

more

general

than

the

definition

of

data-complexity

for

yes/no

decision

problems

Definition

Our

sequential

machine

model

is

a

Turing

Machine

-LRB-

TM

-RRB-

with

a

read-only

input

tape

a

write-only

output

tape

and

a

fixed

number

of

work

tapes

Our

parallel

machine

model

is

a

Parallel

Random

Access

Machine

-LRB-

PRAM

-RRB-

Our

input

generalized

relations

are

encoded

using

some

fixed

binary

encoding

A

query

Q

has

data

complexity

in

PTIME

-LRB-

resp

LOGSPACE

NC

-RRB-

if

there

is

a

TM

-LRB-

resp

TM

PRAM

-RRB-

which

given

input

generalized

relations

d

produces

some

generalized

relation

representing

the

output

of

Q

-LRB-

d

-RRB-

and

uses

polynomial

time

-LRB-

resp

logarithmic

space

on

the

work

tape

polynomial

number

of

processors

running

in

polylogarithmic

parallel

time

-RRB-

Overview

of

Contributions

From

Codd

's

original

work

-LSB-

-RSB-

it

follows

that

safe

relational

calculus

can

be

evaluated

bottomup

in

closed

form

and

LOGSPACE

data

complexity

Codd

defines

safe

formulas

via

syntactic

restrictions

on

relational

calculus

The

LOGSPACE

data

complexity

analysis

is

from

-LSB-

-RSB-

We

provide

as

evidence

of

the

soundness

of

our

design

principles

many

variations

of

this

observation

in

the

context

of

constraints

The

following

table

summarizes

the

main

data

complexity

results

Polynomial

Dense

Order

Equality

Relational

Calculus

NC

LOGSPACE

LOGSPACE

Datalog

Not

closed

PTIME

PTIME

In

more

detail

Relational

calculus

with

real

polynomial

inequality

constraints

can

be

evaluated

bottomup

in

closed

form

and

NC

data

complexity

This

is

a

direct

consequence

of

-LSB-

-RSB-

and

illustrates

the

potential

applicability

of

the

framework

to

spatial

databases

-LRB-

Section

-RRB-

As

part

of

our

analysis

of

the

relational

calculus

and

real

polynomial

inequality

constraints

we

provide

a

new

interpretation

of

the

homomorphism

theorem

for

tableau

query

containment

from

-LSB-

-RSB-

Our

interpretation

is

based

on

the

simple

geometric

fact

that

an

affine

space

is

contained

in

a

finite

union

of

affine

spaces

iff

it

is

contained

in

one

member

of

this

union

-LSB-

-RSB-

p.

We

show

that

deciding

containment

between

tableaux

queries

with

linear

equalities

is

NP-complete

but

that

with

quadratic

equalities

it

is

-LRB-

Section

-RRB-

Relational

calculus

-LRB-

Inflationary

Datalog

:-RRB-

with

dense

linear

order

constraints

can

be

evaluated

bottom-up

in

closed

form

and

LOGSPACE

-LRB-

PTIME

-RRB-

data

complexity

This

is

shown

by

adapting

the

proof

of

-LSB-

-RSB-

Also

by

a

slight

modification

of

-LSB-

-RSB-

Inflationary

Datalog

with

dense

linear

order

expresses

exactly

PTIME

-LRB-

Section

-RRB-

For

Datalog

with

dense

linear

order

constraints

we

develop

a

bottom-up

evaluation

method

that

is

closer

to

the

classical

foundations

of

logic

programming

-LSB-

-RSB-

and

knowledge

bases

-LSB-

-RSB-

-LRB-

Section

-RRB-

This

allows

us

to

show

that

piecewise

linear

Datalog

with

dense

linear

order

constraints

can

be

evaluated

bottom-up

in

closed

form

and

NC

data

complexity

-LRB-

Section

-RRB-

Relational

calculus

-LRB-

Inflationary

Datalog

:-RRB-

with

equality

constraints

over

an

infinite

domain

can

be

evaluated

bottom-up

in

closed

form

and

LOGSPACE

-LRB-

PTIME

-RRB-

data

complexity

This

extends

the

approach

to

safe

queries

of

-LSB-

-RSB-

-LRB-

Section

-RRB-

Finally

Datalog

with

boolean

equality

constraints

can

be

evaluated

bottom-up

and

in

closed

form

For

the

definitions

we

refer

to

Section

and

-LSB-

-RSB-

The

data

complexity

here

is

higher

than

in

the

previous

cases

and

it

depends

on

the

use

of

free

boolean

algebras

with

m

generators

We

partly

analyze

this

data

complexity

and

show

it

to

be

-LRB-

Section

-RRB-

Real

Polynomial

Inequality

Constraints

Throughout

Section

we

assume

that

the

constraint

domain

D

is

the

set

of

real

numbers

but

our

analysis

applies

to

any

real

closed

field

In

Section

we

give

our

first

example

of

a

CQL

by

combining

relational

calculus

with

real

polynomial

inequalities

In

Section

we

investigate

tableaux

queries

with

constraints

We

present

several

results

on

the

optimization

of

such

queries

in

the

presence

of

linear

equations

quadratic

equations

and

simple

inequalities

without

arithmetic

operations

Relational

Calculus

with

Constraints

and

Computational

Geometry

Consider

a

query

language

consisting

of

all

first-order

formulas

over

the

database

predicates

together

with

real

polynomial

inequality

constraints

The

syntax

is

the

union

of

relational

calculus

with

that

of

the

theory

of

real

closed

fields

-LSB-

-RSB-

For

the

semantics

the

database

atomic

formulas

will

be

used

as

shorthands

for

large

formulas

of

the

theory

of

real

closed

fields

as

described

in

Section

The

critical

observation

is

that

database

atomic

formulas

express

and

highlight

in

the

declarative

query

language

itself

the

parts

that

can

grow

asymptotically

versus

the

parts

that

are

constant-size

calculations

That

the

database

size

N

dominates

the

query

size

by

many

orders

of

magnitude

is

the

rationale

of

data

complexity

In

the

following

examples

N

is

the

only

parameter

that

grows

asymptotically

In

Example

we

already

illustrated

this

language

using

the

problem

of

object

intersection

It

is

interesting

to

note

that

most

other

basic

operations

of

computational

geometry

-LRB-

e.g.

Convex

Hull

and

Voronoi

diagram

-LCB-

see

-LSB-

-RSB-

-RRB-

can

be

described

in

this

declarative

query

language

which

also

happens

to

be

efficiently

bottom-up

evaluable

Example

Convex

hull

The

database

consists

of

an

arity

relation

r

that

describes

N

points

of

the

plane

We

want

to

select

those

points

from

r

that

form

the

convex

hull

To

do

this

observe

that

a

point

-LRB-

x

y

-RRB-

is

not

a

convex

hull

point

iff

there

are

other

points

in

r

such

that

-LRB-

x

y

-RRB-

is

inside

the

triangle

that

they

generate

Using

constraints

we

can

define

a

predicate

Intriangle

-LRB-

x

y

-RRB-

that

holds

when

-LRB-

x

y

-RRB-

is

in

the

triangle

generated

by

-LRB-

-RRB-

-LRB-

-RRB-

and

-LRB-

-RRB-

Point

-LRB-

x

y

-RRB-

in

r

will

be

in

the

convex

hull

iff

there

do

not

exist

points

in

r

such

that

Intriangle

-LRB-

x

y

-RRB-

The

naive

algorithm

based

on

this

observation

known

as

Floyd

's

method

takes

O

-LRB-

-RRB-

time

because

it

involves

four

database

atomic

formulas

Although

it

can

not

compete

with

various

known

O

-LRB-

N

log

N

-RRB-

algorithms

it

is

still

useful

in

combination

with

other

convex

hull

techniques

Example

Voronoi

diagram

We

can

show

how

to

find

the

graph

called

the

dual

of

the

Voronoi

diagram

-LSB-

-RSB-

To

do

this

note

that

two

points

u

and

v

are

adjacent

in

the

Voronoi

dual

iff

all

the

points

on

the

line

from

u

to

v

are

closer

to

u

or

to

v

than

to

any

other

point

in

the

database

This

condition

can

easily

be

expressed

in

our

language

Queries

in

the

language

of

relational

calculus

and

real

polynomial

inequality

constraints

can

be

evaluated

bottom-up

in

closed

form

i.e.

the

result

of

a

query

on

a

generalized

relation

is

also

a

generalized

relation

This

closure

property

follows

immediately

from

the

decision

procedure

of

Tarski

for

the

theory

of

real

closed

fields

-LSB-

-RSB-

and

is

one

of

its

basic

properties

One

can

think

of

Tarski

's

procedure

as

a

generalized

relational

algebra

where

all

the

operations

are

simple

variants

of

the

familiar

database

ones

except

for

projection

Projection

corresponds

to

quantifier

elimination

and

is

the

nontrivial

operation

Unfortunately

Tarski

's

decision

procedure

has

extremely

high

complexity

even

in

our

setting

In

general

the

decision

problem

for

the

theory

of

real

closed

fields

requires

nondeterministic

exponential

time

Fortunately

our

setting

has

much

more

structure

than

the

general

problem

of

geometric

theorem

proving

The

reason

for

this

is

that

if

we

focus

our

attention

on

data

complexity

then

the

problem

is

tractable

If

we

have

a

fixed

query

on

a

generalized

database

we

have

a

fixed

bound

on

the

number

of

variables

and

on

the

quantifier

depth

We

can

then

use

the

results

of

-LSB-

-RSB-

to

show

that

the

data

complexity

is

in

NC

Theorem

Relational

calculus

with

real

polynomial

inequality

constraints

can

be

evaluated

bottom-up

in

closed

form

and

NC

data

complexity

Proof

This

is

a

direct

application

of

-LSB-

-RSB-

To

see

this

use

the

fixed

dimension

case

of

the

theorem

p.

in

-LSB-

-RSB-

The

cell

decomposition

method

in

sections

of

-LSB-

-RSB-

can

be

used

to

output

a

formula

in

DNF

-LRB-

of

size

polynomial

in

the

input

-RRB-

that

represents

the

output

generalized

database

It

is

true

that

the

general-purpose

bottom-up

evaluation

based

on

geometric

theorem

proving

is

not

as

efficient

as

the

various

specialized

computational

geometry

algorithms

But

it

can

be

thought

of

as

a

statement

that

the

potential

for

optimization

is

present

Of

course

given

the

NC

data

complexity

bounds

there

are

computations

that

are

not

expressible

in

relational

calculus

with

real

polynomial

inequality

constraints

It

would

be

interesting

to

determine

which

natural

computational

problems

are

or

are

not

expressible

For

example

we

conjecture

that

computing

Euclidean

Spanning

Trees

is

not

expressible

because

it

involves

reachability

computations

As

we

pointed

out

in

Section

if

we

consider

Datalog

with

polynomial

constraints

the

resulting

language

is

not

closed

Furthermore

such

a

language

combining

arithmetic

with

recursion

has

full

Turing

computability

power

It

would

be

interesting

to

design

a

CQL

with

low

data

complexity

which

allows

limited

use

of

recursion

and

real

polynomial

inequalities

Tableaux

Query

Programs

and

their

Containment

Problem

Data

complexity

is

based

on

the

assumption

that

there

are

sufficient

resources

for

unlimited

processing

of

a

query

program

This

is

only

a

theoretical

approximation

and

many

sophisticated

x

Balanced

x

f

r

m

Expenses

x

s

Savings

x

w

i

Income

f

r

m

s

w

i

Figure

The

tableau

with

constraints

balanced

checkbook

query

program

techniques

have

been

developed

for

query

optimization

A

key

problem

for

optimization

is

testing

containment

of

query

programs

Each

query

program

OE

computes

for

any

input

generalized

database

d

an

output

generalized

relation

OE

-LSB-

d

-RSB-

Recall

that

generalized

relations

represent

possibly

infinite

sets

of

points

We

say

that

a

query

program

is

contained

in

query

program

denoted

iff

for

each

input

generalized

database

d

all

the

points

in

-LSB-

d

-RSB-

are

also

in

-LSB-

d

-RSB-

The

containment

problem

is

Given

two

query

programs

decide

if

We

now

examine

-LRB-

tagged

untyped

-RRB-

tableaux

query

programs

These

query

programs

were

the

subject

of

many

investigations

in

relational

database

theory

and

can

be

presented

as

nonrecursive

Datalog

rules

The

terminology

-LRB-

tagged

untyped

-RRB-

tableau

is

used

because

each

program

can

be

described

as

a

table

with

variables

or

constants

appearing

in

each

entry

with

the

predicate

symbols

as

row-tags

and

possibly

with

some

untyped

variable

appearing

in

many

columns

We

augment

these

queries

using

special

real

polynomial

inequalities

such

as

linear

equations

quadratic

equations

and

inequalities

without

For

instance

Example

In

nonrecursive

Datalog

notation

and

using

a

single

linear

equation

constraint

we

express

the

following

balanced

checkbook

query

Balanced

-LRB-

x

-RRB-

Expenses

-LRB-

x

f

r

m

-RRB-

Savings

-LRB-

x

s

-RRB-

Income

-LRB-

x

w

i

-RRB-

f

r

m

s

w

i

This

is

a

query

program

with

Expenses

Savings

and

Income

input

relations

Balanced

output

relation

and

a

single

linear

equation

constraint

x

is

user-id

f

is

amount

spent

for

food

r

for

rent

m

for

miscellaneous

s

for

transfer

to

savings

w

for

wages

and

i

for

interest

The

intended

output

of

this

query

is

the

list

of

user-ids

whose

checkbooks

balance

In

tableau

notation

the

checkbook

query

can

be

represented

by

a

four

row

tableau

with

Balanced

Expenses

Savings

and

Income

row-tags

The

first

row

corresponds

to

the

head

of

the

rule

and

is

called

the

summary

row

The

other

three

rows

correspond

to

predicate

symbol

occurrences

in

the

body

of

the

rule

Each

of

these

rows

has

width

four

because

we

add

dummy

arguments

up

to

the

maximum

arity

i.e.

new

distinct

variables

denoted

-LRB-

see

Figure

-RRB-

The

linear

equation

constraint

is

extra

For

the

detailed

terminology

see

-LSB-

-RSB-

Let

us

now

explain

normal

forms

symbol

mappings

and

homomorphisms

We

break

up

each

OE

tableau

query

program

with

constraints

into

a

tableau

part

T

that

consists

exclusively

of

distinct

occurrences

of

variables

and

a

conjunction

of

constraints

C

This

normal

form

-LRB-

T

C

-RRB-

is

without

loss

of

generality

since

the

constraints

in

C

can

force

any

equalities

of

the

distinct

symbols

in

T

Let

-LRB-

-RRB-

and

-LRB-

-RRB-

be

two

normal

form

tableaux

query

programs

with

real

polynomial

inequality

constraints

A

function

h

is

a

symbol

mapping

from

the

symbols

of

to

those

of

iff

it

maps

the

summary

row

of

into

the

summary

row

of

every

constant

to

itself

and

the

tagged

rows

of

into

similarly

tagged

rows

of

A

symbol

mapping

h

extends

naturally

to

rows

and

to

constraints

We

shall

call

such

a

symbol

mapping

h

a

homomorphism

from

to

if

it

also

has

the

property

that

whenever

constraints

are

satisfied

so

are

h

-LRB-

-RRB-

i.e.

when

constraints

imply

constraints

h

-LRB-

-RRB-

Lemma

Let

-LRB-

-RRB-

and

-LRB-

-RRB-

be

two

normal

form

tableaux

query

programs

with

real

polynomial

inequality

constraints

Let

hm

be

all

the

possible

symbol

mappings

from

to

-LRB-

-LSB-

d

-RSB-

-LSB-

d

-RSB-

-RRB-

iff

-LRB-

implies

-LRB-

-RRB-

hm

-LRB-

-RRB-

-RRB-

Proof

-LRB-

If

-RRB-

If

is

any

constraint

satisfying

valuation

for

-LRB-

i.e.

is

an

assignment

of

values

to

variables

of

satisfying

-RRB-

then

-LRB-

-RRB-

is

true

and

by

the

hypothesis

there

is

a

symbol

mapping

hk

such

that

-LRB-

hk

-LRB-

-RRB-

-RRB-

is

true

Then

we

can

take

as

a

satisfying

valuation

for

This

implies

that

for

any

generalized

database

d

-LSB-

d

-RSB-

-LSB-

d

-RSB-

-LRB-

Only

if

-RRB-

Let

d

be

any

generalized

database

and

be

a

valuation

for

that

satisfies

yielding

some

summary

row

output

Then

there

must

be

another

valuation

for

that

satisfies

yielding

the

same

summary

row

output

Moreover

we

can

restrict

to

map

the

rows

of

only

to

the

image

of

i.e.

to

the

database

tuples

accessed

to

make

a

valid

valuation

This

restriction

is

without

loss

of

generality

because

the

database

could

indeed

be

no

larger

and

if

the

query

containment

holds

in

this

restriction

then

it

also

holds

for

any

larger

database

that

contains

the

image

Now

take

any

row

t

in

maps

t

into

a

tuple

in

the

database

also

maps

at

least

one

row

into

-LRB-

choose

an

arbitrary

-RRB-

Then

we

can

construct

a

mapping

h

from

t

to

by

following

the

arrows

in

the

mapping

of

and

reversing

the

arrows

in

the

mapping

of

For

example

if

t

-LRB-

a

b

c

-RRB-

-LRB-

-RRB-

and

-LRB-

x

y

z

-RRB-

then

we

can

take

h

-LRB-

a

x

b

y

c

z

-RRB-

Moreover

continuing

this

way

h

can

be

expanded

into

a

complete

symbol

mapping

from

to

because

the

variables

are

distinct

in

so

there

are

no

clashes

in

the

symbol

mapping

This

shows

that

if

-LRB-

-RRB-

is

true

then

there

is

a

valuation

and

a

symbol

mapping

h

such

that

-LRB-

-RRB-

is

true

and

and

thus

-LRB-

h

-LRB-

-RRB-

-RRB-

is

true

Therefore

we

see

that

for

any

valid

valuation

of

there

is

some

symbol

mapping

h

depending

on

such

that

-LRB-

h

-LRB-

-RRB-

-RRB-

is

true

Moreover

the

above

argument

did

not

use

any

assumption

about

Hence

this

shows

that

for

all

's

implies

-LRB-

-RRB-

hm

-LRB-

-RRB-

where

hm

are

all

the

possible

symbol

mappings

from

to

Let

-LRB-

-RRB-

and

-LRB-

-RRB-

be

two

tableau

query

programs

with

constraints

in

normal

form

We

say

that

they

have

the

homomorphism

property

whenever

there

is

a

symbol

mapping

h

from

to

such

that

-LRB-

for

all

generalized

databases

d

-LSB-

d

-RSB-

-LSB-

d

-RSB-

-RRB-

iff

-LRB-

implies

h

-LRB-

-RRB-

-RRB-

We

will

now

show

that

the

homomorphism

property

holds

when

we

have

linear

equations

and

is

the

key

to

proving

containment

in

NP

This

extends

the

basic

technique

of

-LSB-

-RSB-

Theorem

Given

two

query

programs

each

a

tableau

with

a

conjunction

of

linear

equation

constraints

deciding

containment

is

NP-complete

Proof

NP-hardness

is

immediate

since

it

is

NP-complete

to

determine

containment

for

such

queries

just

with

equations

of

the

form

x

y

-LSB-

-RSB-

The

new

part

is

showing

membership

in

NP

given

more

general

linear

equations

We

show

that

for

two

queries

and

in

normal

form

is

contained

in

iff

there

is

a

homomorphism

mapping

into

We

use

the

previous

lemma

and

from

-LSB-

-RSB-

p.

the

simple

geometric

fact

an

affine

space

is

contained

in

a

finite

union

of

affine

spaces

iff

it

is

contained

in

one

member

of

this

union

For

linear

equation

constraints

each

of

the

conjunction

of

constraints

and

hi

-LRB-

-RRB-

describes

an

affine

space

Moreover

implies

-LRB-

-RRB-

hm

-LRB-

-RRB-

iff

the

affine

space

is

contained

in

the

union

of

other

affine

spaces

But

this

can

happen

only

if

one

of

the

affine

spaces

hi

-LRB-

-RRB-

contains

the

affine

space

Therefore

one

of

the

symbol

mappings

must

be

a

homomorphism

from

to

Such

a

homomorphism

can

be

guessed

in

NP

and

containment

of

an

affine

space

in

another

can

be

checked

in

polynomial

time

In

contrast

with

quadratic

equations

we

can

show

Theorem

Given

two

query

programs

each

a

tableau

with

a

conjunction

of

quadratic

equation

constraints

deciding

containment

is

Proof

We

can

give

a

simple

reduction

from

the

-LRB-

x

y

-RRB-

quantified

boolean

formula

problem

which

is

known

to

be

Without

loss

of

generality

assume

that

in

negation

is

only

used

on

the

boolean

variables

i.e.

negation

has

been

pushed

to

the

leaves

of

the

parse

tree

of

Let

be

R

-LRB-

x

-RRB-

-LRB-

-RRB-

xn

-LRB-

xn

-RRB-

-LRB-

-RRB-

ym

-LRB-

ym

-RRB-

O

-LRB-

x

y

s

-RRB-

In

all

the

constraints

except

the

last

one

are

used

to

restrict

the

x

-LRB-

xn

-RRB-

and

the

y

-LRB-

ym

-RRB-

vectors

of

variables

to

be

either

's

or

's

The

formula

O

-LRB-

x

y

s

-RRB-

denotes

the

conjunction

of

quadratic

constraints

that

is

constructed

as

follows

Let

Fl

be

the

subformulas

of

with

Fl

Let

sl

be

distinct

fresh

variables

Then

add

the

conjunct

sk

si

sj

whenever

Fk

Fi

Fj

add

sk

sisj

whenever

Fk

Fi

Fj

add

sk

-LRB-

si

-RRB-

whenever

Fk

Fi

If

Fk

Fi

and

Fi

is

a

boolean

variable

xi

or

yi

in

-LRB-

x

y

-RRB-

add

sk

-LRB-

xi

-RRB-

or

sk

-LRB-

yi

-RRB-

Finally

add

the

conjunct

sl

By

induction

it

can

be

proven

that

for

any

truth

assignment

-LRB-

x

y

-RRB-

is

true

iff

-LRB-

x

y

s

-RRB-

is

true

assigning

-LRB-

-RRB-

to

xi

yi

if

the

respective

boolean

variables

get

assigned

true

-LRB-

false

-RRB-

The

basic

intuition

is

that

subformula

Fi

is

made

true

by

the

assignment

iff

constraint

si

is

satisfied

Hence

will

have

as

output

all

x

truth

assignments

for

which

there

is

some

y

truth

assignment

such

that

-LRB-

x

y

-RRB-

holds

Then

let

be

R

-LRB-

x

-RRB-

-LRB-

-RRB-

xn

-LRB-

xn

-RRB-

Note

that

will

have

as

output

all

possible

x

vectors

hence

if

and

only

if

the

quantified

boolean

formula

holds

Containment

in

the

presence

of

inequality

constraints

without

and

is

the

problem

examined

in

-LSB-

-RSB-

For

containment

of

tableaux

with

inequalities

and

no

we

can

show

that

the

homomorphism

property

fails

even

for

semiinterval

query

programs

Semiinterval

query

programs

are

those

in

which

each

variable

is

bounded

by

a

constant

from

only

one

side

i.e.

left

or

right

and

there

are

no

other

constraints

In

-LSB-

-RSB-

it

is

shown

that

the

homomorphism

property

holds

for

left-semiinterval

queries

or

right-semiinterval

queries

alone

and

does

not

work

for

interval

queries

i.e.

variables

are

bounded

from

both

sides

Theorem

The

homomorphism

property

fails

for

semiinterval

query

programs

Proof

We

give

two

example

queries

and

such

that

but

there

is

no

homomorphism

from

to

The

query

is

-LRB-

u

-RRB-

-LRB-

u

-RRB-

R

-LRB-

v

w

-RRB-

v

w

While

the

query

is

-LRB-

u

-RRB-

-LRB-

u

-RRB-

R

-LRB-

x

y

-RRB-

R

-LRB-

y

z

-RRB-

x

z

There

are

two

possible

symbol

mappings

from

the

symbols

of

to

the

symbols

of

The

first

is

-LRB-

u

u

v

x

w

y

-RRB-

and

the

second

is

-LRB-

u

u

v

y

w

z

-RRB-

is

easy

to

see

because

either

y

and

R

-LRB-

x

y

-RRB-

R

-LRB-

y

z

-RRB-

x

z

implies

R

-LRB-

x

y

-RRB-

x

y

which

is

the

same

as

in

after

renaming

or

y

and

R

-LRB-

x

y

-RRB-

R

-LRB-

y

z

-RRB-

x

z

implies

R

-LRB-

y

z

-RRB-

y

z

which

is

again

the

same

as

in

after

a

different

renaming

Therefore

for

each

database

if

gives

some

output

then

also

gives

a

superset

of

that

output

However

one

symbol

mapping

is

not

enough

to

show

containment

Consider

now

the

database

R

-LRB-

-RRB-

R

-LRB-

-RRB-

-LRB-

-RRB-

Then

for

the

valuation

-LRB-

x

y

z

u

-RRB-

yields

-LRB-

-RRB-

However

-LRB-

-RRB-

is

not

a

valid

valuation

for

because

-LRB-

R

-LRB-

v

w

-RRB-

v

w

-RRB-

R

-LRB-

-RRB-

i.e.

it

is

unsatisfiable

and

can

not

produce

any

output

tuple

Similarly

considering

the

database

R

-LRB-

-RRB-

R

-LRB-

-RRB-

-LRB-

-RRB-

and

the

valuation

-LRB-

x

y

z

u

-RRB-

again

yields

-LRB-

-RRB-

However

-LRB-

-RRB-

is

not

a

valid

valuation

because

-LRB-

R

-LRB-

v

w

-RRB-

v

w

-RRB-

R

-LRB-

-RRB-

i.e.

it

is

also

unsatisfiable

and

can

not

produce

any

output

tuple

Dense

Linear

Order

Inequality

Constraints

Throughout

Section

we

assume

that

the

constraint

domain

D

is

the

set

of

rational

numbers

but

our

analysis

applies

to

any

set

with

a

dense

linear

order

In

Section

we

show

that

the

relational

calculus

-LRB-

Datalog

:-RRB-

with

dense

linear

order

inequality

constraints

can

be

evaluated

bottom-up

in

closed

form

and

LOGSPACE

-LRB-

PTIME

-RRB-

data

complexity

These

are

tighter

bounds

than

the

NC

bounds

that

we

get

from

the

analysis

of

relational

calculus

with

real

polynomial

inequality

constraints

In

Section

we

provide

an

alternative

bottom-up

evaluation

for

Datalog

which

emphasizes

logic

programming

tools

as

opposed

to

decision

procedures

for

logical

theories

The

motivation

for

this

is

to

gain

more

intuition

about

Herbrand

atoms

minimal

models

derivation

trees

and

the

other

machinery

of

constraint

logic

programming

-LSB-

-RSB-

In

Section

we

examine

the

parallel

bottom-up

evaluation

of

Datalog

programs

with

dense

linear

order

constraints

using

the

logic

programming

tools

developed

in

Data

Complexity

of

Relational

Calculus

and

Datalog

with

Dense

Order

We

will

first

show

that

the

relational

calculus

with

dense

linear

order

inequality

constraints

has

LOGSPACE

data

complexity

The

proof

of

this

result

is

based

on

the

proof

of

-LSB-

-RSB-

which

we

extend

to

languages

with

constants

and

adapt

for

data

complexity

analysis

Our

basic

technical

contribution

is

the

appropriate

definition

of

r-configuration

-LRB-

for

rational-configuration

-RRB-

In

order

to

use

the

decision

procedure

techniques

of

-LSB-

-RSB-

we

transform

the

query

program

applied

to

the

input

set

of

constraints

into

one

semantically

equivalent

formula

OE

of

the

theory

of

dense

linear

order

with

constants

-LRB-

see

Section

-RRB-

For

example

let

R

-LRB-

x

y

-RRB-

be

a

binary

generalized

relation

containing

the

three

generalized

tuples

x

y

x

and

y

Consider

the

query

program

-LRB-

-RRB-

-LRB-

R

-LRB-

x

z

-RRB-

R

-LRB-

z

y

-RRB-

-RRB-

applied

to

this

generalized

database

Then

the

equivalent

formula

is

OE

-LRB-

x

y

-RRB-

-LRB-

-RRB-

-LRB-

-LRB-

x

z

x

z

-RRB-

-LRB-

z

y

z

y

-RRB-

-RRB-

Furthermore

we

can

within

the

required

resource

bounds

eliminate

all

occurrences

of

and

just

use

atoms

of

the

form

xi

xj

xi

c

c

xi

For

this

replace

x

y

by

-LRB-

x

y

-RRB-

-LRB-

x

y

-RRB-

and

x

y

by

:-LRB-

-LRB-

x

y

-RRB-

-LRB-

y

x

-RRB-

-RRB-

We

similarly

eliminate

all

logical

connectives

but

This

is

for

minimizing

the

case

analysis

in

the

proof

In

what

follows

we

fix

the

query

program

and

the

input

generalized

database

and

consider

the

equivalent

formula

OE

-LRB-

as

in

the

above

example

-RRB-

The

following

definition

of

r-configuration

is

the

key

one

Note

that

it

is

with

respect

to

the

formula

OE

which

we

consider

fixed

We

shall

refer

throughout

to

r-configurations

without

mentioning

the

formula

OE

Throughout

this

section

we

use

DOE

be

the

set

of

constants

that

appear

in

OE

Definition

An

r-configuration

-LRB-

f

l

u

-RRB-

of

size

n

consists

of

a

sequence

f

-LRB-

fn

-RRB-

where

fng

jg

for

some

j

n

and

two

sequences

l

-LRB-

ln

-RRB-

and

u

-LRB-

un

-RRB-

where

the

li

's

are

in

DOE

-LSB-

f

and

ui

's

are

in

DOE

-LSB-

f

g

such

that

For

all

i

li

ui

There

is

no

constant

c

in

DOE

with

the

property

that

li

c

ui

Whenever

fi

fj

then

li

uj

Whenever

fi

fj

then

li

lj

and

ui

uj

The

idea

behind

r-configurations

is

as

follows

Consider

two

points

x

-LRB-

xn

-RRB-

and

y

-LRB-

yn

-RRB-

in

Dn

We

want

to

know

whether

they

can

be

distinguished

using

the

order

constraints

and

the

available

constants

We

say

that

these

points

can

be

distinguished

if

The

relative

order

of

the

xi

's

is

different

from

the

relative

order

of

the

yi

's

or

Some

xi

is

in

a

different

relation

to

some

constant

in

DOE

than

some

yi

Each

r-configuration

characterizes

a

set

of

non-distinguishable

points

The

fi

's

describe

the

relative

order

of

the

xi

's

i.e.

xi

xj

iff

fi

fj

li

and

ui

on

the

other

hand

bound

xi

from

below

and

above

by

constants

from

DOE

-LSB-

f

in

the

tightest

fashion

possible

Example

Assume

that

the

constants

in

DOE

are

The

sequence

of

numbers

-LRB-

-RRB-

can

then

be

represented

by

the

r-configuration

consisting

of

f

-LRB-

-RRB-

describing

the

order

between

the

elements

of

the

sequence

l

-LRB-

-RRB-

u

-LRB-

-RRB-

We

also

need

some

technical

definitions

-LRB-

-RRB-

Express

an

r-configuration

as

a

conjunction

of

constraints

-LRB-

-RRB-

The

points

satisfying

this

conjunction

are

the

indistinguishable

points

denoted

by

the

r-configuration

-LRB-

-RRB-

An

r-configuration

can

be

extended

by

adding

other

variables

Definition

The

formula

F

-LRB-

-RRB-

with

n

free

variables

xng

corresponding

to

an

rconfiguration

-LRB-

f

l

u

-RRB-

of

size

n

is

the

conjunction

of

-LRB-

-RRB-

xi

xj

whenever

fi

fj

-LRB-

-RRB-

xi

xj

whenever

fi

fj

-LRB-

-RRB-

li

xi

ui

whenever

li

ui

-LRB-

-RRB-

xi

li

whenever

li

ui

Definition

j

F

-LRB-

-RRB-

-LRB-

an

-RRB-

will

mean

that

F

-LRB-

-RRB-

is

satisfied

by

the

assignment

of

each

ai

to

the

corresponding

variable

xi

Definition

Let

-LRB-

f

l

u

-RRB-

be

an

r-configuration

of

size

n

An

r-configuration

-LRB-

f

-RRB-

of

size

n

is

an

extension

of

if

f

is

an

extension

of

f

This

means

that

for

all

i

j

i

j

n

f

i

f

j

iff

fi

fj

-LRB-

ln

ln

-RRB-

and

-LRB-

un

un

-RRB-

The

idea

behind

the

proof

is

as

follows

We

show

that

the

r-configurations

partition

multidimensional

space

in

such

a

way

that

to

test

whether

a

subformula

of

the

query

holds

throughout

an

r-configuration

it

suffices

to

test

whether

it

holds

at

an

arbitrary

point

in

this

configuration

This

partitioning

is

made

formal

using

the

five

Lemmas

-LCB-

We

use

this

partitioning

to

construct

an

algorithm

EVALOE

which

evaluates

the

query

in

closed

form

The

output

of

EVALOE

consists

of

a

set

of

r-configurations

Its

correctness

is

described

in

the

two

Lemmas

-LCB-

We

then

argue

that

EVALOE

can

be

implemented

in

LOGSPACE

Query

Evaluation

Algorithm

EVALOE

Input

of

EVALOE

A

generalized

database

We

will

assume

from

now

on

that

OE

is

the

result

of

substituting

the

definition

of

the

generalized

database

for

each

occurrence

of

a

predicate

symbol

in

the

query

-LRB-

we

comment

later

on

why

this

does

not

affect

the

LOGSPACE

complexity

-RRB-

For

each

r-configuration

of

size

n

with

constants

from

DOE

test

whether

F

-LRB-

-RRB-

OE

is

valid

-LRB-

i.e.

true

for

all

assignments

to

its

free

variables

-RRB-

This

test

is

performed

using

recursive

procedure

Boolean-EVALOE

Procedure

Boolean-EVAL

takes

as

input

an

r-configuration

It

is

only

called

on

subformulas

of

OE

thus

guaranteeing

that

all

the

constants

in

are

in

DOE

Boolean-EVAL

returns

iff

F

-LRB-

-RRB-

is

valid

Its

various

cases

are

is

an

atomic

formula

xi

xj

If

fi

fj

-LRB-

where

fi

fj

are

from

-RRB-

then

return

else

return

is

an

atomic

formula

xi

c

or

c

xi

For

xi

c

if

li

ui

c

or

li

ui

c

-LRB-

where

li

ui

are

from

-RRB-

then

return

else

return

For

c

xi

if

c

li

ui

or

c

li

ui

-LRB-

where

li

ui

are

from

-RRB-

then

return

else

return

is

If

the

result

of

Boolean-EVAL

-LRB-

-RRB-

is

then

return

else

return

result

of

Boolean-EVAL

-LRB-

-RRB-

is

If

result

of

Boolean-EVAL

-LRB-

-RRB-

is

then

return

else

return

is

-LRB-

-RRB-

For

every

extension

of

do

Boolean-EVAL

-LRB-

-RRB-

If

the

result

on

one

of

these

r-configurations

is

then

return

else

return

Output

of

EVALOE

The

disjunction

of

the

F

-LRB-

-RRB-

's

for

which

F

-LRB-

-RRB-

OE

is

valid

Lemma

Let

be

an

r-configuration

of

size

n

and

let

be

an

extension

of

size

n

Then

we

have

that

j

F

-LRB-

-RRB-

-LRB-

an

-RRB-

iff

for

some

a

j

F

-LRB-

-RRB-

-LRB-

an

a

-RRB-

Proof

Since

F

-LRB-

-RRB-

is

a

conjunction

of

some

of

the

conjuncts

in

F

-LRB-

-RRB-

F

-LRB-

-RRB-

-LRB-

an

a

-RRB-

implies

F

-LRB-

-RRB-

-LRB-

an

-RRB-

For

the

converse

If

for

some

i

fn

fi

let

a

ai

Otherwise

let

i

and

j

be

such

that

fi

and

fj

are

maximal

and

minimal

respectively

satisfying

fi

fn

fj

-LRB-

the

construction

can

easily

be

modified

to

handle

the

boundary

cases

with

i

or

j

nonexistent

-RRB-

If

ln

un

let

a

ln

It

then

follows

that

ai

a

aj

Otherwise

pick

a

arbitrarily

satisfying

the

conditions

ai

a

aj

and

ln

a

un

The

reason

such

an

a

exists

is

shown

as

follows

Given

the

density

of

order

such

an

a

would

not

exist

only

if

aj

ln

or

un

ai

Assume

aj

ln

fn

fj

together

with

part

of

Definition

implies

that

ln

uj

However

by

the

definition

of

F

-LRB-

-RRB-

lj

aj

Since

lj

aj

ln

uj

it

follows

that

lj

uj

and

by

the

definition

of

F

-LRB-

-RRB-

once

more

lj

aj

We

therefore

have

lj

aj

ln

uj

which

contradicts

part

of

Definition

The

second

case

is

similar

In

both

cases

it

follows

that

-LRB-

an

a

-RRB-

satisfies

F

-LRB-

-RRB-

Lemma

Let

be

an

r-configuration

of

size

n

There

exist

elements

an

of

D

such

that

j

F

-LRB-

-RRB-

-LRB-

an

-RRB-

Proof

Induction

on

the

size

of

using

Lemma

Lemma

Let

an

be

elements

of

D

There

exists

a

unique

r-configuration

of

size

n

such

that

j

F

-LRB-

-RRB-

-LRB-

an

-RRB-

Proof

Uniqueness

follows

from

the

fact

that

if

then

F

-LRB-

-RRB-

F

-LRB-

-RRB-

is

unsatisfiable

To

show

this

suppose

that

-LRB-

an

-RRB-

satisfies

F

-LRB-

-RRB-

F

-LRB-

-RRB-

Let

-LRB-

-RRB-

and

-LRB-

-RRB-

Let

w.l.o.g

Since

is

a

sequence

that

consists

of

all

the

integers

from

to

some

k

-LRB-

possibly

with

repetitions

-RRB-

it

follows

that

for

some

j

But

then

by

Definition

aj

ai

This

implies

using

Definition

again

that

Repeating

this

we

get

an

infinite

descending

sequence

of

positive

integers

a

contradiction

Let

w.l.o.g.

Suppose

first

that

Then

by

Definition

ai

ai

a

contradiction

On

the

other

hand

if

then

part

of

Definition

implies

that

Once

more

by

Definition

ai

ai

a

contradiction

The

case

when

is

similar

Existence

is

shown

by

induction

on

n

The

case

n

is

trivial

Assuming

that

the

result

holds

for

n

let

an

an

be

given

and

let

be

such

that

-LRB-

an

-RRB-

satisfies

F

-LRB-

-RRB-

We

show

how

to

extend

to

such

that

-LRB-

an

-RRB-

satisfies

F

-LRB-

-RRB-

There

are

two

cases

to

consider

for

a

an

For

some

i

a

ai

is

defined

by

f

j

fj

for

i

n

f

n

fi

li

and

ui

Let

i

and

j

be

such

that

ai

and

aj

are

maximal

and

minimal

respectively

satisfying

ai

a

aj

-LRB-

the

construction

can

easily

be

modified

to

handle

the

boundary

cases

-RRB-

ln

will

be

the

largest

constant

in

DOE

such

that

ln

a

un

the

smallest

such

that

a

un

f

is

defined

in

such

a

way

that

it

is

compatible

with

the

ordering

of

the

ai

's

i.e.

-LRB-

a

-RRB-

If

fk

fi

then

f

k

fk

-LRB-

b

-RRB-

If

fk

fj

then

f

k

fk

-LRB-

c

-RRB-

f

n

fj

In

both

cases

j

F

-LRB-

-RRB-

-LRB-

an

-RRB-

Lemma

Let

be

a

formula

with

at

most

k

free

variables

using

only

constants

in

DOE

Let

be

an

r-configuration

of

size

k

and

j

F

-LRB-

-RRB-

-LRB-

ak

-RRB-

and

j

F

-LRB-

-RRB-

-LRB-

-RRB-

then

j

-LRB-

ak

-RRB-

j

-LRB-

-RRB-

Proof

We

show

by

induction

on

the

size

of

that

the

result

holds

for

all

r-configurations

and

all

ai

's

and

's

Atomic

formulas

There

are

two

types

of

atomic

formulas

xi

xj

and

xi

c

-LRB-

c

xi

is

treated

similarly

-RRB-

In

the

first

case

ai

aj

iff

fi

fj

and

likewise

iff

fi

fj

Therefore

ai

aj

iff

In

the

second

case

ai

c

iff

ui

c

or

li

ui

c

and

similarly

for

If

is

of

the

form

or

the

proof

is

straightforward

If

is

-LRB-

-RRB-

then

-LRB-

ak

-RRB-

satisfies

iff

for

some

a

-LRB-

ak

a

-RRB-

satisfies

By

Lemma

there

is

an

r-configuration

such

that

-LRB-

ak

a

-RRB-

satisfies

F

-LRB-

-RRB-

It

is

easy

to

see

-LRB-

by

the

existence

argument

in

Lemma

-RRB-

that

must

be

an

extension

of

Since

-LRB-

-RRB-

satisfies

F

-LRB-

-RRB-

by

Lemma

there

is

an

such

that

-LRB-

-RRB-

satisfies

F

-LRB-

-RRB-

But

then

by

the

induction

hypothesis

-LRB-

-RRB-

satisfies

and

therefore

-LRB-

-RRB-

satisfies

Lemma

Let

be

an

r-configuration

and

a

formula

using

only

the

constants

in

DOE

Then

F

-LRB-

-RRB-

is

valid

-LRB-

i.e.

true

for

all

assignments

to

its

free

variables

-RRB-

iff

F

-LRB-

-RRB-

is

satisfiable

-LRB-

i.e.

true

for

some

assignment

to

its

free

variables

-RRB-

Proof

If

F

-LRB-

-RRB-

is

valid

then

Lemma

implies

that

F

-LRB-

-RRB-

is

satisfiable

On

the

other

hand

if

F

-LRB-

-RRB-

is

satisfiable

say

by

-LRB-

an

-RRB-

and

F

-LRB-

-RRB-

is

satisfied

by

-LRB-

-RRB-

then

Lemma

implies

that

is

satisfied

by

-LRB-

-RRB-

We

now

prove

correctness

of

Boolean-EVAL

and

EVALOE

Lemma

The

algorithm

Boolean-EVAL

-LRB-

-RRB-

described

above

returns

iff

F

-LRB-

-RRB-

is

valid

Proof

The

proof

is

by

induction

on

the

structure

of

is

an

atomic

formula

xi

xj

The

proof

of

correctness

is

trivial

is

an

atomic

formula

xi

c.

Correctness

when

li

ui

c

is

trivial

When

li

ui

c

correctness

follows

from

the

fact

that

c

DOE

and

thus

c

ui

implies

c

li

is

an

atomic

formula

c

xi

The

proof

is

similar

with

is

Correctness

follows

from

Lemma

together

with

the

fact

that

F

-LRB-

-RRB-

-LRB-

-RRB-

is

satisfiable

iff

-LRB-

F

-LRB-

-RRB-

-RRB-

-LRB-

F

-LRB-

-RRB-

-RRB-

is

satisfiable

is

To

show

correctness

we

must

show

that

F

-LRB-

-RRB-

is

valid

iff

-LRB-

F

-LRB-

-RRB-

-RRB-

is

not

valid

By

Lemma

F

-LRB-

-RRB-

is

valid

iff

F

-LRB-

-RRB-

is

satisfiable

But

F

-LRB-

-RRB-

is

the

same

as

-LRB-

F

-LRB-

-RRB-

-RRB-

which

completes

the

proof

is

-LRB-

-RRB-

To

show

correctness

it

suffices

by

Lemma

to

show

that

F

-LRB-

-RRB-

-LRB-

-RRB-

is

satisfiable

iff

F

-LRB-

-RRB-

is

satisfiable

for

some

For

the

if

assume

that

the

formula

F

-LRB-

-RRB-

is

satisfied

by

some

-LRB-

an

a

-RRB-

Lemma

then

implies

that

F

-LRB-

-RRB-

-LRB-

-RRB-

is

satisfied

by

-LRB-

an

-RRB-

For

the

only

if

assume

that

F

-LRB-

-RRB-

-LRB-

-RRB-

is

satisfied

by

-LRB-

an

-RRB-

There

must

then

exist

an

element

a

D

such

that

-LRB-

an

a

-RRB-

satisfies

By

Lemma

there

is

a

r-configuration

such

that

-LRB-

an

a

-RRB-

satisfies

F

-LRB-

-RRB-

By

restricting

to

the

first

n

variables

and

using

Lemma

together

with

the

uniqueness

part

of

Lemma

it

follows

that

is

an

extension

of

To

show

that

EVALOE

is

correct

we

have

to

show

that

it

outputs

a

formula

in

DNF

that

is

equivalent

to

OE

The

formula

output

by

EVALOE

is

clearly

in

DNF

and

it

therefore

suffices

to

show

Lemma

The

result

of

EVALOE

is

equivalent

to

the

formula

OE

Proof

Let

S

f

ng

be

the

set

of

those

configurations

for

which

of

F

-LRB-

i

-RRB-

OE

is

valid

Clearly

W

i

n

F

-LRB-

i

-RRB-

OE

is

valid

For

the

converse

let

-LRB-

ak

-RRB-

be

an

assignment

of

values

to

the

free

variables

of

OE

such

that

j

OE

-LRB-

ak

-RRB-

By

Lemma

there

exists

a

configuration

such

that

-LRB-

ak

-RRB-

satisfies

F

-LRB-

-RRB-

But

then

-LRB-

ak

-RRB-

satisfies

F

-LRB-

-RRB-

OE

and

by

Lemma

it

follows

that

S

completing

the

proof

We

still

have

to

show

that

Lemma

EVALOE

can

be

implemented

in

LOGSPACE

Proof

First

consider

Boolean-EVALOE

The

first

problem

we

have

to

address

is

that

the

algorithm

assumes

that

we

have

constructed

the

formula

OE

which

can

not

be

done

in

LOGSPACE

The

solution

is

to

use

the

query

formula

as

given

switching

to

the

database

whenever

the

query

contains

a

predicate

symbol

rather

than

copying

explicitly

the

contents

of

the

relation

at

this

point

This

can

be

easily

done

with

only

a

constant

extra

memory

cost

To

run

Boolean-EVALOE

we

need

to

store

the

current

configuration

Since

we

have

a

fixed

query

formula

we

have

a

bound

on

the

number

of

quantifiers

and

hence

on

the

maximum

size

of

the

configurations

we

have

to

consider

It

then

follows

that

we

can

store

each

configuration

in

LOGSPACE

For

a

given

configuration

we

use

a

fixed

number

of

pointers

to

find

the

subformulas

of

OE

and

perform

the

appropriate

steps

of

Boolean-EVAL

on

them

Whenever

we

encounter

a

predicate

symbol

we

use

one

pointer

to

remember

where

we

are

in

the

query

and

a

second

pointer

to

scan

the

database

as

though

it

were

part

of

the

query

formula

at

this

point

The

first

pointer

is

to

remember

where

to

return

to

after

we

reach

the

end

of

the

database

relation

Most

of

the

subcases

of

Boolean-EVAL

are

straightforward

When

we

are

considering

a

quantifier

however

we

have

to

iterate

over

all

extensions

of

We

can

do

this

in

LOGSPACE

by

considering

each

extension

to

in

turn

and

first

testing

whether

it

is

a

legal

configuration

or

not

This

shows

that

Boolean-EVAL

is

a

LOGSPACE

algorithm

The

algorithm

EVALOE

iterates

over

all

configurations

and

performs

Boolean-EVALOE

on

each

one

As

before

we

can

easily

iterate

over

all

configurations

in

LOGSPACE

and

this

shows

that

EVALOE

is

also

in

LOGSPACE

Now

we

can

show

that

Theorem

The

relational

calculus

with

dense

linear

order

inequality

constraints

can

be

evaluated

bottom-up

in

closed

form

with

LOGSPACE

data

complexity

Inflationary

Datalog

with

dense

linear

order

inequality

constraints

can

be

evaluated

bottom-up

in

closed

form

and

PTIME

data

complexity

Proof

The

first

part

of

the

theorem

follows

from

Lemmas

-LCB-

Note

that

EVALOE

proceeds

by

structural

induction

on

OE

and

all

calls

to

its

outermost

for

can

proceed

in

parallel

For

the

semantics

of

a

query

program

ss

of

Inflationary

Datalog

with

dense

linear

order

constraints

we

have

to

iterate

a

relational

calculus

formula

OEss

We

can

use

EVALOE

of

the

first

part

of

the

theorem

as

one

iteration

of

the

query

Since

the

relational

calculus

formula

is

fixed

there

are

at

most

a

polynomial

number

of

r-configurations

Since

under

inflationary

semantics

we

can

only

add

r-configurations

at

each

iteration

we

obtain

a

polynomial

time

algorithm

for

Inflationary

Datalog

with

dense

linear

order

constraints

These

iterations

proceed

in

a

bottom-up

fashion

A

final

observation

involves

the

expressive

power

of

Inflationary

Datalog

with

dense

linear

order

inequality

constraints

Theorem

Inflationary

Datalog

with

dense

linear

order

inequality

constraints

can

express

any

relational

database

query

computable

in

PTIME

-LRB-

for

a

formal

definition

of

these

queries

see

-LSB-

-RSB-

-RRB-

Proof

As

shown

in

-LSB-

-RSB-

and

-LSB-

-RSB-

the

fixpoint

queries

of

-LSB-

-RSB-

together

with

a

finite

discrete

linear

order

express

exactly

PTIME

It

follows

from

the

proofs

of

this

fact

as

well

as

the

normal

form

results

of

-LSB-

-RSB-

that

Inflationary

Datalog

with

a

finite

discrete

order

expresses

exactly

PTIME

These

simulation

proofs

can

be

easily

modified

by

making

all

programs

use

only

constants

appearing

in

the

database

Datalog

Bottom-up

Evaluation

Revisited

Let

us

now

consider

an

alternative

proof

for

the

Datalog

case

of

Theorem

The

main

idea

comes

from

the

semantics

of

Constraint

Logic

Programming

-LSB-

-RSB-

It

involves

generalizing

the

notion

of

a

Herbrand

atom

The

result

is

a

natural

bottom-up

evaluation

for

Datalog

with

dense

linear

order

constraints

Definition

Let

P

be

a

generalized

database

logic

program

that

is

a

Datalog

constraints

program

defining

the

IDB

predicates

and

a

generalized

database

defining

the

EDB

predicates

A

generalized

EDB

Herbrand

atom

is

an

EDB

predicate

symbol

with

distinct

variable

symbols

as

arguments

and

a

conjunction

of

dense

linear

order

constraints

on

these

variables

-LRB-

Note

that

these

atoms

are

generalized

tuples

-RRB-

A

generalized

IDB

Herbrand

atom

is

an

IDB

predicate

symbol

with

distinct

variable

symbols

as

arguments

and

an

r-configuration

on

these

variables

F

-LRB-

-RRB-

as

in

Definition

is

a

conjunction

of

constraints

on

these

variables

-LRB-

Note

that

these

atoms

are

generalized

tuples

of

a

special

form

-RRB-

Example

For

example

an

r-configuration

can

also

describe

a

generalized

Herbrand

atom

when

it

is

attached

to

a

predicate

symbol

Start

from

the

r-configuration

of

Example

assume

predicate

R

has

arity

and

the

database

has

only

the

constants

in

it

The

conjunction

of

constraints

F

-LRB-

-RRB-

gives

us

a

generalized

Herbrand

atom

denoted

as

R

-LRB-

-RRB-

First

let

us

observe

that

r-configurations

are

closed

under

projection

We

say

that

an

rconfiguration

is

projected

onto

a

subset

of

its

variables

when

all

the

variables

outside

this

subset

are

eliminated

i.e.

their

bounds

are

deleted

from

l

u

and

they

are

removed

from

the

ordering

f

It

is

easy

to

see

that

the

projection

of

an

r-configuration

is

an

r-configuration

of

smaller

size

The

evaluation

of

a

generalized

database

logic

program

P

starts

by

collecting

in

a

set

H

all

the

predicate

variable

and

dense

linear

constant

symbols

that

occur

in

the

program

that

is

either

in

its

rules

or

in

its

database

part

We

call

H

the

generalized

Herbrand

base

of

P

because

all

symbols

that

are

ever

used

during

our

evaluation

must

be

in

H

Since

each

program

is

by

definition

finite

H

must

be

also

a

finite

set

The

generalized

Herbrand

universe

of

P

consists

of

all

generalized

EDB

Herbrand

atoms

of

P

-LRB-

these

remain

fixed

throughout

the

evaluation

of

the

program

-RRB-

and

all

generalized

IDB

Herbrand

atoms

that

can

be

built

out

of

the

generalized

Herbrand

base

Since

there

is

a

finite

number

of

r-configurations

the

generalized

Herbrand

universe

is

finite

Its

lattice

of

subsets

is

also

finite

and

thus

complete

We

let

an

interpretation

I

of

P

be

a

subset

of

its

generalized

Herbrand

universe

containing

all

the

generalized

EDB

Herbrand

atoms

of

P

Definition

Let

P

be

a

generalized

database

logic

program

and

H

be

its

generalized

Herbrand

base

Let

I

be

an

interpretation

of

P

All

generalized

EDB

Herbrand

atoms

of

I

are

derivable

in

one

rule

firing

from

P

and

I

Generalized

IDB

Herbrand

atoms

are

derivable

in

one

rule

firing

from

P

and

I

as

follows

Choose

any

rule

Ak

C

of

P

where

Ak

are

relational

atoms

and

C

is

a

conjunction

of

dense

linear

order

constraints

Without

loss

of

generality

the

n

occurrences

of

variables

in

the

relational

atoms

are

distinct

and

any

equalities

are

part

of

C.

Choose

any

r-configuration

of

size

n

built

from

H

Check

that

F

-LRB-

-RRB-

C

is

valid

i.e.

true

for

all

values

of

the

free

variables

If

Ai

i

k

is

an

EDB

relational

atom

R

-LRB-

-RRB-

then

project

onto

its

variables

to

produce

r-configuration

i.

Check

that

for

some

generalized

EDB

Herbrand

atom

R

-LRB-

-RRB-

in

I

we

have

that

F

-LRB-

i

-RRB-

is

valid

If

Ai

i

k

is

an

IDB

relational

atom

R

-LRB-

-RRB-

then

project

onto

its

variables

to

produce

r-configuration

i.

Check

that

generalized

IDB

Herbrand

atom

R

-LRB-

-RRB-

F

-LRB-

i

-RRB-

is

in

I

If

all

tests

are

true

and

if

is

the

IDB

relational

atom

R

-LRB-

-RRB-

then

project

onto

its

variables

to

produce

r-configuration

Fire

the

rule

once

to

derive

generalized

IDB

Herbrand

atom

R

-LRB-

-RRB-

F

-LRB-

-RRB-

We

define

a

function

TP

from

interpretations

to

interpretations

as

follows

TP

-LRB-

I

-RRB-

fA

A

is

derivable

in

one

rule

firing

from

P

and

Ig

In

the

above

definition

generalized

IDB

Herbrand

atoms

are

effectively

r-configurations

and

are

treated

purely

syntactically

The

constraints

C

and

the

generalized

EDB

Herbrand

atoms

are

treated

in

a

slightly

different

fashion

This

is

because

we

avoid

transforming

them

into

disjunctions

of

r-configurations

This

could

be

done

but

would

be

rather

awkward

and

unnecessary

Let

us

comment

on

the

tests

involving

C

and

the

EDBs

-LRB-

-RRB-

Checking

that

F

-LRB-

-RRB-

C

is

valid

can

be

done

simply

by

picking

one

assignment

to

the

variables

that

satisfies

F

-LRB-

-RRB-

and

verifying

that

it

satisfies

C

This

is

by

Lemmas

and

-LRB-

-RRB-

Checking

that

for

some

generalized

EDB

Herbrand

atom

R

-LRB-

-RRB-

the

implication

F

-LRB-

i

-RRB-

is

valid

can

be

done

scanning

the

input

and

for

each

tuple

checking

as

in

-LRB-

-RRB-

above

The

reason

for

this

test

is

to

guarantee

that

the

multi-dimensional

points

of

F

-LRB-

i

-RRB-

are

points

of

the

input

generalized

EDB

relation

r

that

corresponds

to

R.

Recall

that

OEr

is

a

DNF

formula

in

fact

it

is

the

disjunction

of

all

possible

's

of

this

test

It

is

interesting

to

note

that

by

Lemmas

and

F

-LRB-

i

-RRB-

OEr

is

valid

iff

F

-LRB-

i

-RRB-

W

is

valid

iff

F

-LRB-

i

-RRB-

W

is

satisfiable

iff

for

some

F

-LRB-

i

-RRB-

is

satisfiable

iff

for

some

F

-LRB-

i

-RRB-

is

valid

We

define

a

model

M

of

P

to

be

an

interpretation

of

P

such

that

TP

-LRB-

M

-RRB-

M

P

may

have

several

models

ordered

according

to

set

inclusion

We

have

the

following

analogues

to

traditional

logic

programming

Theorem

Let

P

be

a

generalized

database

logic

program

and

LP

the

intersection

of

all

models

of

P

Then

we

have

that

LP

is

the

unique

least

model

of

P

LP

is

the

unique

least

fixpoint

of

TP

LP

can

be

produced

by

a

finite

number

of

iterations

of

the

mapping

TP

Each

generalized

Herbrand

atom

in

LP

is

derivable

by

a

finite

number

of

rule

firings

from

the

interpretation

with

empty

IDBs

LP

can

be

evaluated

bottom-up

in

PTIME

data

complexity

by

rule

firings

starting

from

the

interpretation

with

empty

IDBs

Proof

-LRB-

-RRB-

Identical

to

traditional

logic

programming

model

intersection

property

for

Horn

clauses

-LRB-

-RRB-

By

Tarski

's

theorem

on

the

complete

lattice

of

subsets

of

the

generalized

Herbrand

universe

-LRB-

-RRB-

By

the

finiteness

of

the

generalized

Herbrand

universe

-LRB-

-RRB-

and

-LRB-

-RRB-

The

arguments

are

the

same

as

for

Datalog

naive

bottom-up

evaluation

Call

generalized

naive

evaluation

the

bottom-up

evaluation

by

rule

firings

starting

from

the

interpretation

with

empty

IDBs

This

evaluation

has

the

well

defined

finite

output

LP

Is

this

generalized

database

output

the

desired

result

according

to

the

semantics

defined

in

Section

Recall

that

for

the

semantics

in

Section

we

interpreted

programs

as

mappings

from

unrestricted

relations

to

unrestricted

relations

These

semantics

are

computable

via

naive

evaluation

of

Datalog

rules

on

unrestricted

relations

The

following

theorem

expresses

the

fact

that

generalized

naive

bottom-up

evaluation

of

P

is

semantically

sound

and

complete

Namely

given

any

generalized

database

representing

input

unrestricted

relations

then

its

generalized

database

output

LP

finitely

represents

the

output

of

naive

evaluation

on

the

input

unrestricted

relations

Theorem

Let

P

be

a

generalized

database

logic

program

with

generalized

EDB

Herbrand

atoms

Let

be

the

unrestricted

relational

database

represented

by

the

generalized

database

that

is

points

-LRB-

-RRB-

If

LP

-LRB-

-RRB-

is

the

output

of

the

generalized

naive

evaluation

of

P

and

LP

-LRB-

-RRB-

is

the

output

of

the

naive

evaluation

of

P

-LSB-

-RSB-

-LRB-

with

input

instead

of

-RRB-

then

points

-LRB-

LP

-LRB-

-RRB-

-RRB-

LP

-LRB-

-RRB-

Proof

We

need

to

prove

two

directions

The

soundness

direction

that

is

any

point

p

in

points

-LRB-

LP

-LRB-

-RRB-

-RRB-

is

also

in

LP

-LRB-

-RRB-

and

the

completeness

direction

that

is

any

point

p

in

LP

-LRB-

-RRB-

is

also

in

points

-LRB-

LP

-LRB-

-RRB-

-RRB-

We

prove

each

direction

by

induction

on

the

number

of

iterations

i

of

the

two

evaluations

We

shall

show

that

for

each

i

points

-LRB-

T

iP

-LRB-

-RRB-

-RRB-

T

iP

-LRB-

-RRB-

Since

generalized

naive

evaluation

is

finite

this

also

proves

that

a

finite

number

of

iterations

suffice

for

naive

evaluation

as

well

For

i

we

have

points

-LRB-

T

P

-LRB-

-RRB-

-RRB-

points

-LRB-

-RRB-

T

P

-LRB-

-RRB-

hence

the

claim

holds

Now

we

assume

that

points

-LRB-

T

iP

-LRB-

-RRB-

-RRB-

T

iP

-LRB-

-RRB-

is

true

and

prove

the

equality

for

i

For

the

soundness

direction

let

Ak

C

be

any

rule

of

P

where

C

are

dense

linear

order

constraints

To

perform

a

rule

firing

of

generalized

naive

evaluation

assume

that

an

r-configuration

is

chosen

whose

set

of

satisfying

points

also

satisfy

C.

Suppose

that

each

of

the

projections

of

onto

the

A

's

of

the

body

are

r-configurations

whose

set

of

satisfying

points

are

also

satisfied

by

some

atoms

of

T

iP

-LRB-

-RRB-

Then

by

our

rule

application

is

projected

onto

the

head

of

the

rule

and

is

turned

into

an

atom

of

T

i

P

-LRB-

-RRB-

Now

let

p

be

any

point

within

Then

all

the

projections

of

p

are

points

By

the

induction

hypothesis

points

-LRB-

T

iP

-LRB-

-RRB-

-RRB-

T

iP

-LRB-

-RRB-

Therefore

each

projection

of

p

onto

the

A

's

of

the

body

must

be

points

within

T

iP

-LRB-

-RRB-

Hence

taking

that

collection

of

points

from

T

iP

-LRB-

-RRB-

we

can

derive

via

naive

evaluation

into

T

i

P

-LRB-

-RRB-

the

projection

of

p

on

the

rule

head

Thus

points

-LRB-

T

i

P

-LRB-

-RRB-

-RRB-

T

i

P

-LRB-

-RRB-

For

the

completeness

direction

let

Ak

C

be

any

rule

of

P

where

C

are

any

dense

linear

order

constraints

To

perform

a

rule

firing

in

naive

evaluation

assume

that

some

point

p

is

chosen

as

the

values

of

all

variables

in

the

rule

Suppose

that

the

projections

of

p

are

all

present

among

the

atoms

of

T

iP

-LRB-

-RRB-

and

hence

the

projection

of

p

on

the

head

is

derived

into

T

i

P

-LRB-

-RRB-

-LRB-

call

it

-RRB-

By

Lemma

p

satisfies

some

unique

r-configuration

Use

that

and

generalized

naive

evaluation

The

point

p

must

also

satisfy

C

and

by

induction

some

atoms

in

T

iP

-LRB-

-RRB-

By

Lemma

all

points

in

satisfy

exactly

the

same

formulas

Hence

taking

a

generalized

naive

rule

firing

can

derive

-LRB-

as

a

projection

of

onto

the

head

-RRB-

an

atom

of

T

i

P

-LRB-

-RRB-

that

contains

We

can

reason

similarly

for

each

point

proving

that

T

i

P

-LRB-

-RRB-

points

-LRB-

T

i

P

-LRB-

-RRB-

-RRB-

Datalog

Derivation

Trees

and

Parallelism

A

generalized

derivation

tree

for

generalized

Herbrand

atom

A

using

program

P

is

a

tree

whose

nodes

are

labeled

as

follows

A

labels

the

root

Every

leaf

is

labeled

by

a

generalized

EDB

Herbrand

atom

of

P

Every

internal

node

is

labeled

by

a

generalized

IDB

Herbrand

atom

B.

Let

its

children

have

labels

Bk

There

is

a

rule

in

P

and

an

r-configuration

such

that

B

is

derived

by

one

firing

of

this

rule

using

Bk

as

atoms

and

this

r-configuration

Each

generalized

derivation

tree

illustrates

one

possible

sequence

of

rule

firings

to

derive

the

label

of

its

root

An

obvious

parallel

evaluation

method

tries

all

possible

ways

of

firing

each

rule

in

every

iteration

step

Therefore

the

number

of

iteration

steps

necessary

for

this

parallel

algorithm

to

derive

an

IDB

atom

is

exactly

the

minimum

depth

generalized

derivation

tree

for

the

IDB

atom

This

observation

motivates

the

definition

of

a

generalized

polynomial

fringe

property

We

say

that

a

program

P

has

the

generalized

polynomial

fringe

property

if

each

atom

in

LP

has

a

generalized

derivation

tree

with

at

most

a

fixed

polynomial

-LRB-

in

the

size

of

the

EDB

part

of

P

-RRB-

number

of

leaves

The

-LRB-

generalized

-RRB-

polynomial

fringe

property

is

a

semantic

notion

A

natural

class

of

queries

can

be

described

purely

syntactically

by

piecewise

linear

programs

see

-LSB-

-RSB-

for

the

exact

definition

Following

-LSB-

-RSB-

one

can

show

that

piecewise

linear

programs

always

have

the

-LRB-

generalized

-RRB-

polynomial

fringe

property

Theorem

Datalog

programs

with

dense

linear

order

constraints

that

have

the

generalized

polynomial

fringe

property

can

be

evaluated

bottom-up

in

closed

form

and

NC

data

complexity

Piecewise

linear

Datalog

with

dense

linear

order

constraints

can

be

evaluated

bottom-up

in

closed

form

and

NC

data

complexity

Proof

Use

the

analysis

of

parallelism

in

Datalog

programs

by

Ullman

and

van

Gelder

-LSB-

-RSB-

by

substituting

generalized

derivation

tree

for

derivation

tree

We

close

this

section

with

the

observation

that

our

development

of

constraint

logic

programming

machinery

can

have

many

applications

For

example

various

forms

of

analysis

of

Datalog

in

logic

programming

-LRB-

e.g

stratified

or

inflationary

semantics

-RRB-

can

be

directly

translated

into

Datalog

dense

linear

order

constraints

Equality

Constraints

over

an

Infinite

Domain

Throughout

Section

we

assume

that

the

constraint

domain

D

is

the

set

of

integer

numbers

but

our

analysis

applies

to

any

countably

infinite

set

In

a

sense

we

have

a

special

case

of

Datalog

and

dense

linear

order

constraints

We

present

a

separate

analysis

because

for

dense

linear

order

constraints

we

expressed

using

but

here

we

can

not

We

are

interested

in

this

class

of

constraints

for

the

following

reason

Suppose

we

have

a

relational

database

Any

finite

relation

in

this

database

can

be

represented

as

a

set

of

equality

constraints

However

in

the

relational

data

model

there

are

unsafe

queries

for

which

the

result

is

not

finite

This

is

a

significant

restriction

in

the

relational

model

since

there

is

no

way

to

deal

with

these

queries

However

in

our

generalized

setting

the

problem

goes

away

as

long

as

the

result

has

a

finite

representation

of

the

appropriate

kind

For

the

relational

calculus

and

Datalog

applied

to

finite

relations

there

are

similar

results

in

the

literature

-LSB-

-RSB-

Our

work

extends

some

of

the

results

of

-LSB-

-RSB-

by

adding

recursion

and

handling

any

input

generalized

relation

In

fact

the

specific

number

of

constants

added

in

-LSB-

-RSB-

and

-LSB-

-RSB-

to

the

active

domain

-LRB-

the

set

of

constants

that

appear

in

the

database

and

in

the

query

-RRB-

corresponds

precisely

to

the

induction

depth

used

in

proving

our

evaluation

method

correct

We

show

similar

results

to

the

dense

linear

order

case

namely

that

the

relational

calculus

with

equality

constraints

can

be

evaluated

bottom-up

in

closed

form

and

LOGSPACE

data

complexity

and

that

inflationary

Datalog

with

equality

constraints

can

be

evaluated

bottomup

in

closed

form

and

PTIME

data

complexity

The

algorithm

and

proofs

follow

the

same

outline

as

those

in

Section

We

have

to

use

a

different

notion

of

configuration

As

in

Section

let

OE

be

the

query

formula

applied

to

the

given

database

instance

and

let

DOE

be

the

set

of

constants

that

appear

in

OE

We

will

also

have

a

special

symbol

ffi

whose

meaning

will

be

explained

below

Definition

An

e-configuration

-LRB-

e

v

-RRB-

of

size

n

consists

of

an

equivalence

relation

e

on

ng

and

a

sequence

v

-LRB-

vn

-RRB-

where

each

vi

is

in

DOE

-LSB-

fffig

such

that

If

i

e

j

then

vi

vj

and

If

vi

vj

ffi

then

i

e

j.

Consider

a

point

x

-LRB-

xn

-RRB-

The

e-configuration

that

contains

x

is

determined

as

follows

The

equivalence

relation

e

is

defined

by

i

e

j

iff

xi

xj

If

xi

is

equal

to

some

constant

v

in

DOE

we

set

vi

equal

to

v.

Otherwise

vi

will

be

the

special

symbol

ffi

whose

meaning

is

that

xi

is

not

equal

to

any

constant

in

DOE

Example

Let

DOE

be

the

set

The

sequence

-LRB-

-RRB-

is

represented

by

the

e-configuration

that

consists

of

the

equivalence

relation

e

together

with

the

sequence

v

-LRB-

ffi

ffi

ffi

-RRB-

We

now

proceed

as

in

Section

Definition

The

formula

F

-LRB-

-RRB-

with

n

free

variables

xng

that

corresponds

to

an

econfiguration

-LRB-

e

v

-RRB-

of

size

n

is

the

conjunction

of

-LRB-

-RRB-

xi

xj

whenever

i

e

j

-LRB-

-RRB-

xi

xj

whenever

-LRB-

i

e

j

-RRB-

-LRB-

-RRB-

xi

vi

whenever

vi

ffi

and

-LRB-

-RRB-

for

all

v

in

DOE

xi

v

whenever

vi

ffi

Definition

j

F

-LRB-

-RRB-

-LRB-

an

-RRB-

means

that

F

-LRB-

-RRB-

is

satisfied

by

the

assignment

of

each

ai

to

the

corresponding

variable

xi

Definition

Let

-LRB-

e

v

-RRB-

be

an

e-configuration

of

size

n

An

e-configuration

-LRB-

-RRB-

of

size

n

is

an

extension

of

iff

For

all

i

and

j

i

j

n

i

e

j

iff

i

j

and

-LRB-

vn

-RRB-

Lemma

Let

be

an

e-configuration

of

size

n

and

let

be

an

extension

of

size

n

Then

j

F

-LRB-

-RRB-

-LRB-

an

-RRB-

iff

for

some

a

j

F

-LRB-

-RRB-

-LRB-

an

a

-RRB-

Proof

Since

F

-LRB-

-RRB-

is

a

conjunction

of

some

of

the

conjuncts

in

F

-LRB-

-RRB-

F

-LRB-

-RRB-

-LRB-

an

a

-RRB-

implies

F

-LRB-

-RRB-

-LRB-

an

-RRB-

For

the

converse

If

for

some

i

i

n

let

a

ai

Otherwise

there

are

two

cases

If

is

in

DOE

let

a

Otherwise

let

a

be

some

element

of

the

domain

different

from

an

and

not

in

DOE

This

is

possible

because

there

is

an

unbounded

supply

of

integers

In

both

cases

it

follows

that

-LRB-

an

a

-RRB-

satisfies

F

-LRB-

-RRB-

Lemma

Let

be

an

e-configuration

of

size

n

Then

there

exist

domain

elements

an

such

that

j

F

-LRB-

-RRB-

-LRB-

an

-RRB-

Proof

Induction

on

the

size

of

using

Lemma

Lemma

Let

an

be

domain

elements

There

exists

a

unique

e-configuration

such

that

j

F

-LRB-

-RRB-

-LRB-

an

-RRB-

Proof

Uniqueness

follows

from

the

fact

that

if

then

F

-LRB-

-RRB-

F

-LRB-

-RRB-

is

unsatisfiable

To

show

this

suppose

that

-LRB-

an

-RRB-

satisfies

F

-LRB-

-RRB-

F

-LRB-

-RRB-

Let

-LRB-

-RRB-

and

-LRB-

-RRB-

Suppose

that

Let

i

and

j

be

such

that

i

j

but

-LRB-

i

j

-RRB-

Then

F

-LRB-

-RRB-

contains

the

conjunct

xi

xj

while

F

-LRB-

-RRB-

contains

the

conjunct

xi

xj

and

therefore

F

-LRB-

-RRB-

F

-LRB-

-RRB-

is

unsatisfiable

Suppose

that

but

Let

i

be

such

that

If

neither

nor

is

equal

to

ffi

then

F

-LRB-

-RRB-

contains

the

conjunct

xi

while

F

-LRB-

-RRB-

contains

the

conjunct

xi

If

on

the

other

hand

say

ffi

then

F

-LRB-

-RRB-

contains

the

conjunct

xi

while

F

-LRB-

-RRB-

contains

the

conjunct

xi

Existence

is

shown

by

induction

on

n

The

case

n

is

trivial

Assuming

that

the

result

holds

for

n

let

an

an

be

given

and

let

be

such

that

-LRB-

an

-RRB-

satisfies

F

-LRB-

-RRB-

We

show

how

to

extend

to

such

that

-LRB-

an

-RRB-

satisfies

F

-LRB-

-RRB-

There

are

two

cases

to

consider

for

a

an

For

some

i

a

ai

is

defined

by

j

k

iff

j

e

k

whenever

j

k

n

and

j

-LRB-

n

-RRB-

whenever

i

e

j.

Also

set

vi

If

a

ai

for

all

i

we

define

the

extension

of

e

by

-LRB-

i

n

-RRB-

for

all

i

from

to

n

If

a

DOE

then

a

otherwise

ffi

In

both

cases

j

F

-LRB-

-RRB-

-LRB-

an

-RRB-

Lemma

Let

be

a

formula

with

at

most

k

free

variables

using

only

constants

in

DOE

Let

be

an

e-configuration

of

size

k

and

j

F

-LRB-

-RRB-

-LRB-

ak

-RRB-

and

j

F

-LRB-

-RRB-

-LRB-

-RRB-

then

j

-LRB-

ak

-RRB-

j

-LRB-

-RRB-

Proof

We

show

by

induction

on

the

size

of

that

the

result

holds

for

all

e-configurations

and

all

ai

's

and

's

is

xi

c

Then

c

must

be

in

DOE

and

therefore

F

-LRB-

-RRB-

must

contain

one

of

the

conjuncts

-LRB-

xi

c

-RRB-

if

vi

c

or

-LRB-

xi

-RRB-

if

vi

c

or

-LRB-

xi

c

-RRB-

if

vi

ffi

Since

we

assume

that

distinct

constant

symbols

denote

distinct

elements

we

have

ai

c

iff

c.

If

is

of

the

form

xi

xj

or

or

the

proof

is

straightforward

If

is

-LRB-

-RRB-

then

-LRB-

ak

-RRB-

satisfies

iff

for

some

a

-LRB-

ak

a

-RRB-

satisfies

By

Lemma

there

is

an

e-configuration

such

that

-LRB-

ak

a

-RRB-

satisfies

F

-LRB-

-RRB-

It

is

easy

to

see

that

must

be

an

extension

of

Since

-LRB-

-RRB-

satisfies

F

-LRB-

-RRB-

by

Lemma

there

is

an

such

that

-LRB-

-RRB-

satisfies

F

-LRB-

-RRB-

But

then

by

the

induction

hypothesis

-LRB-

-RRB-

satisfies

and

therefore

-LRB-

-RRB-

satisfies

Lemma

Let

be

an

e-configuration

and

a

formula

using

only

the

constants

in

DOE

Then

F

-LRB-

-RRB-

is

valid

-LRB-

i.e.

true

for

all

assignments

to

the

free

variables

-RRB-

iff

F

-LRB-

-RRB-

is

satisfiable

-LRB-

i.e.

true

for

some

assignment

to

the

free

variables

-RRB-

Proof

If

formula

F

-LRB-

-RRB-

is

valid

then

Lemma

implies

that

F

-LRB-

-RRB-

is

satisfiable

On

the

other

hand

if

F

-LRB-

-RRB-

is

satisfiable

say

by

-LRB-

an

-RRB-

and

F

-LRB-

-RRB-

is

satisfied

by

-LRB-

-RRB-

then

Lemma

implies

that

is

satisfied

by

-LRB-

-RRB-

In

the

same

way

as

we

did

for

dense

linear

order

we

can

define

algorithms

EVALOE

and

Boolean-EVAL

These

algorithms

are

similar

to

those

for

dense

order

but

use

e-configurations

For

example

the

algorithm

Boolean-EVAL

takes

as

input

an

e-configuration

and

a

formula

that

uses

only

constants

in

DOE

It

returns

iff

F

-LRB-

-RRB-

is

valid

Only

the

base

cases

of

Boolean-EVAL

-LRB-

-RRB-

are

different

from

the

dense

linear

order

case

is

an

atomic

formula

xi

xj

Boolean-EVAL

-LRB-

-RRB-

returns

iff

i

e

j

in

is

an

atomic

formula

xi

c.

Boolean-EVAL

-LRB-

-RRB-

returns

iff

c

vi

in

The

proof

of

correctness

and

the

complexity

analysis

proceed

in

an

analogous

way

to

the

analysis

of

dense

linear

order

Also

for

equality

constraints

we

can

develop

constraint

logic

programming

machinery

analogous

to

that

of

the

previous

sections

Therefore

Theorem

Relational

calculus

with

equality

constraints

over

an

infinite

domain

can

be

evaluated

bottom-up

in

closed

form

and

LOGSPACE

data

complexity

Inflationary

Datalog

with

equality

constraints

over

an

infinite

domain

can

be

evaluated

bottom-up

in

closed

form

and

PTIME

data

complexity

Boolean

Equality

Constraints

In

this

section

we

describe

the

addition

of

boolean

equality

constraints

to

Datalog

The

idea

is

to

use

boolean

operations

as

shorthands

for

manipulating

various

boolean

domains

finite

or

infinite

In

order

to

make

the

material

self-contained

we

first

give

some

definitions

and

basic

facts

about

boolean

algebras

-LRB-

Section

-RRB-

In

Section

we

describe

the

syntax

and

semantics

of

a

simple

language

which

we

motivate

using

some

examples

Note

that

in

this

section

we

parameterize

the

concepts

of

database

and

general

database

This

allows

more

generality

and

more

flexibility

in

the

language

Finally

in

Section

we

describe

some

lower

bounds

related

to

bottom-up

evaluation

of

fixed

size

programs

i.e.

related

to

data

complexity

Boolean

Algebras

A

boolean

algebra

B

is

a

sextuple

D

where

D

is

a

set

are

binary

functions

is

a

unary

function

and

are

two

specific

elements

of

D

-LRB-

or

zeroary

functions

-RRB-

such

that

for

any

elements

x

y

and

z

in

D

the

following

axioms

hold

x

y

y

x

x

y

y

x

x

-LRB-

y

z

-RRB-

-LRB-

x

y

-RRB-

-LRB-

x

z

-RRB-

x

-LRB-

y

z

-RRB-

-LRB-

x

y

-RRB-

-LRB-

x

z

-RRB-

x

x

x

x

x

x

For

boolean

algebras

there

is

a

representation

theorem

known

as

Stone

's

theorem

Every

boolean

algebra

is

isomorphic

to

a

field

of

sets

and

every

finite

boolean

algebra

is

isomorphic

to

the

power

set

of

a

finite

set

Thus

there

is

a

unique

-LRB-

up

to

isomorphism

-RRB-

finite

boolean

algebra

for

every

cardinality

The

boolean

algebra

of

cardinality

is

the

one

freely

generated

by

m

generators

and

is

denoted

by

Bm

For

m

we

have

Let

hD

be

any

boolean

algebra

Then

the

structure

hD

is

called

a

boolean

ring

with

unity

if

we

define

for

any

elements

x

and

y

the

binary

function

x?y

-LRB-

exclusiveor

-RRB-

as

-LRB-

x

-RRB-

-LRB-

y

-RRB-

Because

of

one-to-one

correspondence

between

boolean

algebras

and

boolean

rings

with

unity

the

theory

here

can

be

developed

in

either

setting

-LSB-

-RSB-

In

what

follows

we

use

algebras

and

sometimes

the

exclusive-or

as

an

abbreviation

Boolean

Terms

We

use

T

-LRB-

F

V

-LSB-

C

-RRB-

for

the

set

of

terms

built

in

the

usual

way

from

F

the

set

of

function

symbols

f

V

a

set

of

variable

symbols

and

C

a

of

constant

symbols

distinct

from

Ground

terms

are

those

terms

which

do

not

have

any

variable

symbols

appearing

in

them

A

-LRB-

B

oe

-RRB-

interpretation

is

a

pair

where

B

is

a

boolean

algebra

and

oe

is

a

mapping

of

the

constant

symbols

C

to

the

elements

of

B.

For

each

t

in

T

-LRB-

F

V

-LSB-

C

-RRB-

given

a

-LRB-

B

oe

-RRB-

interpretation

and

an

element

of

B

for

each

variable

symbol

appearing

in

t

we

can

evaluate

t

in

the

usual

way

and

have

it

denote

one

element

of

B.

Boolean

equations

An

equation

between

terms

and

in

T

-LRB-

F

V

-LSB-

C

-RRB-

is

a

statement

We

say

that

-LRB-

-RRB-

is

true

in

B

oe

if

after

applying

oe

to

the

constant

symbols

in

then

for

every

substitution

of

the

variable

symbols

by

elements

of

B

and

denote

the

same

element

of

B.

-LRB-

-RRB-

is

true

in

B

if

it

is

true

in

B

oe

for

every

oe

-LRB-

-RRB-

is

true

if

it

is

true

in

every

B

A

number

of

useful

properties

hold

in

all

boolean

algebras

For

example

the

following

equations

are

true

x

-LRB-

y

z

-RRB-

-LRB-

x

y

-RRB-

z

x

-LRB-

y

z

-RRB-

-LRB-

x

y

-RRB-

z

x

-LRB-

y

z

-RRB-

-LRB-

x

y

-RRB-

z.

For

another

useful

example

Lemma

Let

t

-LRB-

zn

-RRB-

be

a

term

where

the

z

's

are

the

distinct

variable

or

constant

symbols

occuring

in

it

Then

the

following

equation

is

true

t

-LRB-

zn

-RRB-

-LRB-

t

-LRB-

zn

-RRB-

-RRB-

-LRB-

t

-LRB-

zn

-RRB-

-RRB-

Disjunctive

Normal

Form

We

use

the

convention

that

means

and

means

z.

Also

that

the

z

's

are

ordered

lexicograpically

from

to

zn

Then

we

also

may

write

the

equation

in

the

previous

lemma

as

t

-LRB-

zn

-RRB-

W

-LRB-

t

-LRB-

zn

-RRB-

-RRB-

By

repeatedly

using

the

above

lemma

and

the

nine

boolean

algebra

axioms

it

is

possible

to

transform

each

term

into

the

following

disjunctive

normal

form

t

-LRB-

zn

-RRB-

a

-LRB-

t

-LRB-

an

-RRB-

zann

-RRB-

where

W

a

denotes

the

disjunction

of

all

substitutions

for

an

The

function

determined

by

t

-LRB-

zn

-RRB-

depends

only

on

the

values

of

the

expressions

t

-LRB-

an

-RRB-

where

each

ai

is

either

or

One

can

see

that

each

of

these

expressions

has

value

either

or

Hence

it

is

possible

to

see

that

there

are

disjunctive

normal

forms

with

n

variable

and

constant

symbols

Constructing

Bm

We

give

a

simple

example

of

how

to

construct

the

free

boolean

algebra

Bm

out

of

a

set

of

m

constant

symbols

C

cmg

First

we

build

all

possible

ground

terms

Next

we

find

all

the

equivalence

classes

of

ground

terms

under

the

boolean

algebra

axioms

Each

equivalence

class

is

an

element

of

Bm

and

corresponds

to

a

disjunctive

normal

form

There

are

distinct

equivalence

classes

or

elements

of

Bm

We

call

the

constant

symbols

the

generators

-LRB-

Note

that

naively

we

would

have

used

bits

to

represent

every

element

of

Bm

but

this

way

we

can

use

log

m

bits

for

each

generator

-RRB-

Definition

A

boolean

equality

constraint

is

a

statement

of

the

form

t

-LRB-

x

c

-RRB-

B

oe

for

a

-LRB-

B

oe

-RRB-

interpretation

and

a

term

t

built

using

a

set

of

variables

x

and

a

set

of

constants

c.

-LRB-

We

sometimes

omit

subscript

oe

if

it

is

obvious

from

the

context

-RRB-

Constraint

t

-LRB-

x

c

-RRB-

B

oe

has

a

solution

if

the

formula

-LRB-

t

-LRB-

x

c

-RRB-

-RRB-

is

true

in

B

oe

-LRB-

i.e.

if

after

applying

oe

to

the

constant

symbols

in

t

there

exists

a

substitution

of

the

variable

symbols

by

elements

of

B

that

makes

t

denote

the

element

of

B

-RRB-

A

solution

of

a

constraint

is

a

substitution

that

makes

t

denote

It

is

always

enough

to

use

boolean

constraints

of

the

form

above

because

the

general

constraint

B

oe

has

the

same

solutions

as

the

constraint

B

oe

just

by

using

the

boolean

algebra

axioms

and

the

definition

of

A

useful

fact

about

solving

equations

in

boolean

algebras

is

Boole

's

Lemma

which

can

be

proven

using

the

nine

boolean

algebra

axioms

and

the

disjunctive

normal

form

described

above

Boole

's

Lemma

gives

a

simple

way

to

eliminate

existential

quantifiers

Lemma

The

boolean

equality

constraint

t

-LRB-

xn

c

-RRB-

B

oe

has

a

solution

iff

the

constraint

t

-LRB-

xn

c

-RRB-

t

-LRB-

xn

c

-RRB-

B

oe

has

a

solution

iff

V

t

-LRB-

b

c

-RRB-

is

true

in

B

oe

Furthermore

the

set

of

substitutions

for

xn

that

make

the

formula

-LRB-

t

-LRB-

xn

c

-RRB-

-RRB-

true

in

B

oe

is

the

same

as

the

set

of

solutions

of

constraint

t

-LRB-

xn

c

-RRB-

t

-LRB-

xn

c

-RRB-

B

oe

Remark

F

In

Lemma

the

expression

V

t

-LRB-

b

c

-RRB-

denotes

the

conjunction

of

all

substitutions

into

the

variable

symbols

of

t

-LRB-

xn

c

-RRB-

and

is

the

same

expression

for

all

B

oe

Note

that

the

correctness

of

Boole

's

Lemma

is

trivial

if

B

happens

to

be

otherwise

the

conjunction

may

well

be

even

if

of

its

conjuncts

are

Bottom-Up

Evaluation

of

Datalog

with

Boolean

Equality

Constraints

The

following

evaluation

method

applies

to

any

B

oe-interpretation

We

only

describe

a

language

for

the

boolean

equality

constraint

part

This

language

can

easily

be

combined

with

the

one

for

dense

linear

order

constraints

as

we

illustrate

in

the

last

example

of

this

section

Syntax

Here

for

simplicity

of

presentation

we

use

the

convention

that

the

various

occurrences

of

x

and

y

are

possibly

different

vectors

of

variables

from

the

set

xj

ylg

and

-LRB-

x

-RRB-

B

oe

and

k

-LRB-

x

y

-RRB-

B

oe

are

boolean

equality

constraints

-LRB-

-RRB-

The

facts

-LRB-

or

generalized

tuples

-RRB-

have

the

form

-LRB-

x

-RRB-

-LRB-

x

-RRB-

B

oe

-LRB-

-RRB-

The

rules

have

the

form

-LRB-

x

-RRB-

-LRB-

x

y

-RRB-

Rk

-LRB-

x

y

-RRB-

k

-LRB-

x

y

-RRB-

B

oe

In

these

facts

and

rules

all

the

x

variables

that

appear

in

the

body

appear

in

the

head

The

y

variables

appear

only

in

the

body

Semantics

A

set

of

rules

and

facts

defines

in

the

standard

fashion

a

monotone

mapping

from

relations

whose

elements

are

from

B

to

relations

whose

elements

are

from

B

The

semantics

of

this

set

of

facts

and

rules

is

the

least

fixpoint

of

this

mapping

One

constraint

suffices

per

fact/rule

Because

from

the

axioms

one

can

show

the

equivalences

-LRB-

-RRB-

a

B

oe

b

has

the

same

set

of

siolutions

as

a?b

B

oe

and

-LRB-

-RRB-

a

B

oe

and

b

B

oe

have

the

same

set

of

solutions

as

B

oe

which

has

the

same

set

of

solutions

as

a?b

-LRB-

a

b

-RRB-

B

oe

Using

these

properties

many

constraints

can

be

equivalently

replaced

by

one

Bottom-up

Evaluation

We

describe

the

firing

of

one

rule

Assume

that

we

have

either

given

EDB

or

derived

IDB

facts

Ri

-LRB-

x

y

-RRB-

i

-LRB-

x

y

-RRB-

B

oe

i

k

and

that

we

have

the

rule

-LRB-

x

-RRB-

-LRB-

x

y

-RRB-

Rk

-LRB-

x

y

-RRB-

k

-LRB-

x

y

-RRB-

B

oe

We

produce

a

quantified

description

of

the

set

of

tuples

from

B

that

such

a

firing

will

derive

fR

-LRB-

x

-RRB-

-LRB-

x

y

-RRB-

B

oe

k

-LRB-

x

y

-RRB-

B

oe

k

-LRB-

x

y

-RRB-

B

oe

g

From

the

above

remark

it

follows

that

can

be

represented

as

a

set

of

facts

that

satisfy

one

equation

except

for

the

quantified

variables

on

the

right

side

These

extra

variables

can

be

eliminated

by

using

repeatedly

Boole

's

Lemma

that

formula

-LRB-

y

x

-RRB-

is

satisfiable

in

B

oe

iff

-LRB-

x

-RRB-

-LRB-

x

-RRB-

B

oe

has

a

solution

This

allows

us

to

add

in

unquantified

single

equation

form

to

the

derived

facts

-LRB-

Note

that

here

we

use

Boole

's

Lemma

to

eliminate

quantifiers

instead

of

some

other

boolean

unification

algorithm

-RRB-

Soundness

and

Completeness

The

bottom-up

evaluation

consists

of

repeatedly

firing

rules

starting

from

the

input

-LRB-

EDB

-RRB-

facts

and

rules

Since

the

transformation

into

a

single

constraint

and

the

quantifier

elimination

preserve

the

set

of

solutions

this

bottom-up

evaluation

does

implement

the

intended

semantics

Termination

As

shown

in

the

following

theorem

this

bottom-up

evaluation

can

be

made

to

terminate

after

a

finite

set

of

firings

by

using

the

property

that

terms

can

be

rewritten

-LRB-

via

the

axioms

-RRB-

into

equivalent

disjunctive

normal

forms

We

present

now

the

simple

adder

circuit

example

of

Buttner

and

Simonis

-LSB-

-RSB-

but

in

this

case

we

use

bottom-up

evaluation

to

illustrate

the

previous

method

Example

An

adder

circuit

can

be

built

from

two

half-adder

circuits

We

define

the

operation

of

the

half-adder

by

a

simple

database

fact

using

finite

boolean

equality

constraints

over

Note

that

here

oe

is

empty

and

we

omit

it

Halfadder

-LRB-

x

y

z

w

-RRB-

x

y

z

x

y

w

where

x

and

y

are

input

variables

z

is

the

sum

and

w

is

the

carry

Turning

the

two

constraints

in

the

body

into

a

single

equivalent

one

we

have

Halfadder

-LRB-

x

y

z

w

-RRB-

-LRB-

x

y

z

-RRB-

-LRB-

-LRB-

x

y

-RRB-

w

-RRB-

The

adder

circuit

can

be

described

by

the

use

of

two

half-adder

circuits

and

an

extra

constraint

where

x

and

y

are

input

variables

c

is

carry

in

s

is

sum

and

d

is

carry

out

Adder

-LRB-

x

y

c

s

d

-RRB-

Halfadder

-LRB-

x

y

-RRB-

Halfadder

-LRB-

c

s

-RRB-

d

When

using

these

definitions

as

a

database

query

the

evaluation

proceeds

bottom-up

substituting

the

constraints

of

the

halfadder

into

the

rule

for

the

adder

That

yields

Adder

-LRB-

x

y

c

s

d

-RRB-

-LRB-

-RRB-

d

-LRB-

x

y

-RRB-

-LRB-

-LRB-

x

y

-RRB-

-RRB-

-LRB-

c

s

-RRB-

-LRB-

-LRB-

c

-RRB-

-RRB-

By

transforming

the

three

constraints

in

the

body

into

one

and

using

Boole

's

lemma

to

eliminate

we

can

transform

the

above

into

Adder

-LRB-

x

y

c

s

d

-RRB-

-LRB-

x

y

c

s

-RRB-

-LRB-

-LRB-

x

y

-RRB-

-LRB-

x

c

-RRB-

-LRB-

y

c

-RRB-

d

-RRB-

Example

Suppose

we

replace

the

variable

symbols

x

y

and

c

in

Example

by

the

constant

symbols

X

Y

C

Then

we

get

expressions

for

a

subset

of

the

variables

in

the

constraint

that

is

for

s

and

d

in

terms

of

the

constant

symbols

that

is

the

parameters

X

Y

C.

For

example

one

solution

would

be

s

X

Y

C

and

d

-LRB-

X

Y

-RRB-

-LRB-

X

C

-RRB-

-LRB-

Y

C

-RRB-

This

says

that

given

any

oe

that

interprets

X

Y

C

as

elements

of

then

s

and

d

will

be

computed

according

to

the

last

two

constraints

-LRB-

See

also

Remark

G

below

-RRB-

Next

we

show

that

this

evaluation

method

works

in

general

by

the

following

theorem

Theorem

Let

Q

be

any

query

program

of

Datalog

with

boolean

equality

constraints

over

some

B

oe

Then

Q

can

be

evaluated

bottom-up

in

closed

form

Proof

Let

v

be

the

maximum

arity

of

a

relation

in

the

given

query

program

Let

m

be

the

number

of

constant

symbols

in

the

program

and

the

input

database

Our

evaluation

method

will

consist

of

a

number

of

iterative

steps

In

each

step

we

add

all

new

facts

that

can

be

derived

from

the

already

known

facts

and

rules

By

the

proper

substitutions

of

database

facts

into

the

rules

we

get

formulas

on

the

right-hand

side

of

the

rules

From

these

formulas

we

eliminate

existentially

quantified

variables

using

Lemma

We

also

have

to

show

that

the

procedure

terminates

To

do

that

we

always

keep

every

fact

in

disjunctive

normal

form

Note

that

Lemma

does

not

introduce

any

new

constant

symbols

hence

the

number

of

constant

symbols

m

does

not

change

during

evaluation

The

quantifier

elimination

yields

constraints

that

have

up

to

m

constant

symbols

and

v

variables

That

means

that

the

constraints

for

each

relation

R

can

be

represented

by

at

most

m

facts

by

counting

only

disjunctive

normal

forms

Hence

after

each

iteration

step

every

newly

derived

constraint

can

be

compared

easily

with

facts

already

present

in

the

database

If

all

newly

derived

constraints

are

already

present

then

the

iteration

can

stop

otherwise

we

add

the

new

constraints

This

procedure

clearly

must

terminate

because

there

are

only

a

finite

number

of

facts

that

can

be

added

A

parametric

fact

-LRB-

or

parametric

generalized

tuple

-RRB-

is

a

fact

-LRB-

or

generalized

tuple

-RRB-

for

which

we

have

not

specified

either

B

or

oe

or

both

A

parametric

generalized

database

is

a

set

of

parametric

generalized

tuples

-LRB-

Note

the

difference

between

a

parametric

tuple

and

a

generalized

tuple

A

generalized

tuple

describes

the

set

of

all

tuples

that

one

gets

by

substituting

values

for

its

variables

A

parametric

tuple

describes

a

single

tuple

given

a

substitution

for

its

parameters

-RRB-

Example

As

a

simple

example

consider

the

problem

of

finding

the

parity

of

n

bits

By

considering

these

n

bits

as

parameters

a

simple

solution

would

be

the

following

Paritybit

-LRB-

x

-RRB-

x

oe

Yn

Each

different

oe

would

be

a

different

assigment

of

values

from

to

these

n

parameters

So

if

we

do

not

specify

oe

this

would

be

a

parametric

fact

Remark

G

For

the

procedure

described

in

Theorem

the

particular

B

oe

in

which

the

constants

are

interpreted

are

not

important

Syntactically

the

same

constraints

are

derived

by

the

evaluation

algorithm

for

each

B

oe

This

is

really

a

consequence

of

Lemma

and

Remark

F.

Therefore

we

have

that

The

evaluation

of

Theorem

can

be

applied

to

an

input

parametric

generalized

database

d

to

produce

an

output

parametric

generalized

database

Q

-LRB-

d

-RRB-

such

that

for

each

I

-LRB-

B

oe

-RRB-

we

have

I

-LRB-

Q

-LRB-

d

-RRB-

-RRB-

Q

-LRB-

I

-LRB-

d

-RRB-

-RRB-

From

a

practical

point

of

view

boolean

equality

constraints

can

be

added

on

top

of

the

Datalog

framework

with

dense

linear

order

that

we

already

examined

in

Section

We

can

strictly

sort

the

arguments

of

each

database

predicate

e.g.

each

argument

can

range

either

over

the

rationals

or

over

a

finite

boolean

domain

All

of

our

results

still

hold

in

this

combined

framework

Example

If

the

number

of

bits

n

of

the

previous

example

can

vary

then

we

do

not

want

to

write

a

new

program

each

time

n

changes

We

would

like

to

have

a

fixed

program

and

change

only

the

input

-LRB-

generalized

-RRB-

database

or

the

input

parametric

-LRB-

generalized

-RRB-

database

This

we

could

do

with

the

following

program

Paritybit

-LRB-

x

-RRB-

Parity

-LRB-

k

x

-RRB-

Last

-LRB-

k

-RRB-

Parity

-LRB-

i

x

-RRB-

Parity

-LRB-

j

y

-RRB-

Next

-LRB-

j

i

-RRB-

Input

-LRB-

i

z

-RRB-

x

oe

y

z

Parity

-LRB-

x

-RRB-

Input

-LRB-

z

-RRB-

where

we

use

Next

-LRB-

-RRB-

Next

-LRB-

n

n

-RRB-

Last

-LRB-

n

-RRB-

and

Input

-LRB-

-RRB-

Input

-LRB-

n

Yn

-RRB-

as

the

parametric

database

inputs

to

the

Datalog

with

boolean

and

dense

linear

order

constraints

program

We

use

in

this

example

n

explicitly

given

elements

of

the

nonboolean

domain

to

order

the

parity

computation

Then

the

program

recursively

finds

the

parity

bit

for

the

first

i

bits

for

i

n.

Data

Complexity

and

Finite

Boolean

Equality

Constraints

For

each

fixed

finite

boolean

algebra

B

fixed

Datalog

query

Q

with

constraints

over

B

oe

and

variable

input

generalized

database

d

with

constraints

over

B

oe

query

evaluation

is

a

constant

size

problem

This

is

because

Given

an

input

generalized

database

d

-LRB-

even

with

an

asymptotically

growing

number

of

constant

symbols

-RRB-

and

a

fixed

finite

boolean

algebra

B

and

a

oe

it

is

possible

to

make

the

number

of

constant

symbols

fixed

by

substituting

for

them

elements

of

B

using

oe

B

has

a

constant

number

of

elements

and

Q

has

a

constant

number

of

variables

because

they

are

fixed

So

the

constraints

can

be

eliminated

by

substituting

the

variables

with

elements

of

B

and

checking

in

which

substitutions

the

constraints

hold

Each

time

the

constraint

holds

record

the

database

tuple

implied

That

yields

a

database

that

is

equivalent

to

generalized

database

d

The

size

of

is

at

most

a

constant

Hence

the

query

evaluation

is

in

this

case

a

trivial

problem

For

example

the

adder

circuit

can

be

described

using

width

constant

size

relations

over

that

define

bit

addition

Recall

however

that

the

procedure

described

in

Theorem

is

parametric

i.e.

syntactically

the

same

constraints

are

derived

by

the

evaluation

algorithm

for

each

B

oe

This

might

be

wasteful

for

the

case

of

fixed

B

and

Q

but

it

is

a

general

method

applicable

when

B

is

part

of

the

problem

input

and

when

we

do

not

know

oe

a

priori

-LRB-

so

we

have

to

manipulate

the

constants

symbolically

-RRB-

We

now

take

an

algebra

that

is

closer

to

the

idea

of

our

parametric

evaluation

than

a

fixed

algebra

Namely

we

assume

that

the

algebra

is

part

of

the

input

In

particular

we

assume

that

the

algebra

is

Bm

where

m

is

the

number

of

constant

symbols

cm

in

the

input

database

and

the

program

In

the

rest

of

the

exposition

we

assume

that

oe

maps

cm

to

the

generators

of

Bm

so

we

omit

the

subscript

oe

in

constraints

Lemma

Let

-LRB-

xn

ym

-RRB-

be

a

term

over

variables

xn

ym

and

the

function

symbols

Let

Bm

be

the

free

boolean

algebra

generated

by

the

constant

symbols

cm

Then

the

formula

-LRB-

xn

ym

-RRB-

is

true

in

if

and

only

if

the

constraint

-LRB-

xn

cm

-RRB-

Bm

has

a

solution

Proof

Note

that

in

this

proof

we

have

to

reduce

a

decision

problem

of

size

l

to

another

decision

problem

of

size

l

where

l

is

the

size

of

the

formula

We

do

the

proof

by

rewriting

the

first

problem

to

the

second

one

using

identities

i.e.

we

will

use

iff

steps

in

the

reduction

instead

of

proving

both

directions

separately

First

we

rewrite

-LRB-

xn

ym

-RRB-

is

true

in

into

-LRB-

-LRB-

xn

ym

-RRB-

-RRB-

is

true

in

by

recognizing

that

each

variable

must

be

either

or

and

by

adding

parenthesis

for

clarity

Note

that

the

subformula

within

the

parenthesis

has

a

solution

for

the

y

's

if

and

only

if

the

conjunction

of

all

substitutions

into

the

x

variables

has

a

solution

for

the

y

's

that

is

if

and

only

if

-LRB-

V

-LRB-

xn

ym

-RRB-

-RRB-

is

true

in

The

last

formula

is

true

if

and

only

if

each

of

the

subproblems

that

result

from

substitutions

to

the

y

variables

is

true

By

using

the

identity

that

a

B

and

b

B

iff

B

the

last

formula

can

be

rewritten

as

W

-LRB-

V

-LRB-

xn

ym

-RRB-

-RRB-

is

true

in

Claim

The

last

formula

is

equivalent

to

V

-LRB-

xn

cm

-RRB-

is

true

in

Bm

-LRB-

This

claim

is

the

basis

of

Martin

and

Nipkow

's

method

-LSB-

-RSB-

but

we

can

prove

it

in

a

simple

way

as

follows

-RRB-

To

prove

the

claim

rewrite

the

term

in

the

claim

into

disjunctive

normal

form

We

get

W

-LRB-

V

-LRB-

xn

ym

-RRB-

-RRB-

cym

m

Note

that

in

Bm

the

constant

symbols

are

distinct

and

their

complements

are

distinct

from

each

other

and

from

and

-LRB-

It

is

a

well-known

fact

that

each

element

of

a

boolean

algebra

has

only

one

complement

-RRB-

Hence

of

the

last

m

conjuncts

is

either

or

a

complement

of

another

one

among

the

last

m

conjuncts

-LRB-

These

are

the

only

ways

we

can

prove

a

conjunction

to

be

equivalent

to

-RRB-

Therefore

the

conjunction

of

the

last

m

conjuncts

is

never

Hence

the

first

conjunct

which

could

be

only

or

must

be

always

if

the

whole

formula

is

in

Bm

Hence

the

formula

can

be

in

Bm

if

and

only

if

W

-LRB-

V

-LRB-

xn

ym

-RRB-

-RRB-

is

true

in

Bm

Since

we

do

n't

have

any

constant

symbols

in

the

formula

instead

of

Bm

we

may

use

which

proves

the

claim

So

far

we

have

rewritten

the

formula

to

V

-LRB-

xn

cm

-RRB-

is

true

in

Bm

Finally

we

use

Boole

's

Lemma

to

get

the

formula

-LRB-

xn

cm

-RRB-

is

true

in

Bm

which

is

equivalent

to

saying

that

-LRB-

xn

cm

-RRB-

Bm

has

a

solution

Note

that

we

can

not

guess

and

verify

a

solution

of

-LRB-

xn

cm

-RRB-

Bm

that

is

small

The

size

of

each

element

of

Bm

can

be

very

large

In

fact

Corollary

Given

as

inputs

any

t

and

m

where

t

is

any

boolean

formula

with

constant

symbols

cm

deciding

whether

t

Bm

has

a

solution

is

-LRB-

where

Bm

is

the

free

boolean

algebra

generated

by

the

constant

symbols

cm

-RRB-

Theorem

There

is

a

fixed

yes/no

query

program

Q

in

Datalog

with

finite

boolean

equality

constraints

such

that

If

for

each

input

database

d

with

constant

symbols

cm

we

take

the

oe

that

maps

these

constant

symbols

to

themselves

and

we

interpret

B

as

Bm

then

deciding

whether

Q

-LRB-

d

-RRB-

is

yes

is

Proof

Our

reduction

will

use

the

AE-quantified

boolean

formula

problem

-LRB-

see

Section

-RRB-

We

will

produce

a

yes/no

query

expressed

in

Datalog

with

boolean

equality

constraints

over

the

finite

boolean

algebra

Bm

which

is

the

free

boolean

algebra

generated

by

m

constant

symbols

such

that

the

query

answer

is

YES

iff

the

quantified

boolean

formula

-LRB-

x

y

-RRB-

is

true

in

In

this

reduction

m

O

-LRB-

j

j

-RRB-

Assume

x

xng

and

y

ypg

When

the

terms

substituted

for

the

variables

xn

are

terms

that

contain

y

variables

the

constant

symbols

and

and

the

boolean

operators

in

them

then

the

substitution

is

called

a

parametric

solution

To

check

whether

the

quantified

boolean

formula

holds

it

is

enough

to

check

whether

it

has

a

parametric

solution

-LRB-

To

see

this

suppose

that

the

formula

is

true

Then

there

is

a

solution

of

x

's

for

any

assignment

of

y

's

Take

any

xi

For

each

assignment

A

of

y

xi

is

either

or

We

could

build

a

simple

formula

that

says

if

A

then

xi

is

or

if

A

then

xi

is

as

appropriate

We

could

therefore

build

a

parametric

solution

for

each

xi

-RRB-

Our

reduction

will

proceed

in

four

steps

In

this

reduction

we

will

use

script

letters

for

constant

symbols

-LRB-

-RRB-

We

build

a

tree

circuit

for

-LRB-

x

y

-RRB-

using

some

number

of

gates

The

gates

will

be

referred

to

by

the

constants

Gtop

with

Gtop

as

the

output

gate

of

the

whole

circuit

For

each

xi

we

create

a

new

constant

symbol

Bi

and

substitute

the

x

variables

by

these

constant

symbols

while

creating

the

circuit

Similarly

for

each

yi

we

create

a

new

constant

symbol

Ai

and

substitute

the

y

variables

by

these

constant

symbols

while

creating

the

circuit

We

also

have

a

constant

YES

So

we

have

as

constants

the

A

's

B

's

G

's

and

YES

In

the

reduction

we

will

present

a

query

Q

that

uses

a

number

of

input

database

relations

called

EDB

relations

and

a

number

of

initially

empty

output

database

relations

called

IDB

relations

-LRB-

following

standard

database

terminology

-RRB-

We

have

Input

Top

Andgate

Orgate

Notgate

A

B

Next

and

Last

as

EDB

relations

We

have

as

IDB

relations

Value

Aexpr

Replace

Parametric

and

Output

The

yes/no

output

is

carried

in

Output

i.e.

yes

if

Output

-LRB-

YES

-RRB-

and

no

otherwise

Suppose

that

formula

-LRB-

x

y

-RRB-

has

a

total

of

l

occurrences

of

variables

in

it

We

enter

for

each

occurrence

of

yi

and

for

each

occurrence

of

xj

the

values

Ai

and

Bj

via

the

distinct

gates

Gl

We

create

l

database

facts

as

follows

If

the

kth

occurrence

of

a

variable

in

the

formula

is

yi

-LRB-

or

xj

-RRB-

we

create

using

the

EDB

database

predicate

Input

the

database

fact

Input

-LRB-

Gk

Ai

-RRB-

or

Input

-LRB-

Gk

Bj

-RRB-

We

also

declare

Gtop

as

the

output

gate

of

the

circuit

using

the

EDB

database

predicate

Top

Top

-LRB-

Gtop

-RRB-

Next

we

look

at

the

parse

tree

of

the

formula

We

tag

each

boolean

operator

in

the

parse

tree

by

a

new

distinct

gate

We

tag

the

operator

at

the

root

by

the

gate

Gtop

Then

for

every

i

j

k

if

an

-LRB-

or

an

or

an

-RRB-

operator

is

tagged

by

gate

Gi

and

has

as

left

child

an

operator

tagged

by

gate

Gj

and

has

as

right

child

-LRB-

for

a

and

an

gate

only

-RRB-

an

operator

tagged

by

gate

Gk

we

create

using

one

of

the

EDB

database

predicates

Andgate

Orgate

or

Notgate

the

database

fact

Andgate

-LRB-

Gi

Gj

Gk

-RRB-

or

Orgate

-LRB-

Gi

Gj

Gk

-RRB-

or

Notgate

-LRB-

Gi

Gj

-RRB-

The

term

corresponding

to

-LRB-

x

y

-RRB-

is

built

bottom-up

by

the

evaluation

method

using

the

IDB

predicate

Value

and

the

following

rules

Value

-LRB-

k

z

-RRB-

Input

-LRB-

k

z

-RRB-

Value

-LRB-

k

z

-RRB-

Andgate

-LRB-

k

i

j

-RRB-

Value

-LRB-

i

x

-RRB-

Value

-LRB-

j

y

-RRB-

z

Bm

x

y

Value

-LRB-

k

z

-RRB-

Orgate

-LRB-

k

i

j

-RRB-

Value

-LRB-

i

x

-RRB-

Value

-LRB-

j

y

-RRB-

z

Bm

x

y

Value

-LRB-

k

z

-RRB-

Notgate

-LRB-

k

i

-RRB-

Value

-LRB-

i

x

-RRB-

z

Bm

The

bottom-up

evaluation

derives

as

the

value

of

the

topmost

gate

Gtop

some

ztop

such

that

ztop

Bm

-LRB-

B

A

-RRB-

As

the

rules

are

evaluated

variables

i

j

x

y

are

eliminated

but

constant

symbols

Gtop

A

B

remain

Remark

i

We

know

that

we

have

created

Value

so

that

Value

-LRB-

Gtop

-LRB-

B

A

-RRB-

is

in

the

fixpoint

We

will

now

proceed

to

construct

in

the

fixpoint

of

Axpr

all

ground

terms

e

that

contain

only

A

's

and

the

boolean

operators

We

will

then

replace

the

B

's

in

the

fixpoint

by

all

possible

such

e

's

in

Replace

and

in

Parametric

Remark

ii

We

also

know

that

the

constraint

-LRB-

e

A

-RRB-

Bm

has

a

solution

for

the

e

's

in

Bm

if

and

only

if

-LRB-

x

A

-RRB-

is

true

in

Bm

By

Lemma

that

last

formula

is

true

if

and

only

if

-LRB-

xn

yp

-RRB-

is

true

in

which

is

our

original

quantified

boolean

formula

problem

Therefore

finding

the

e

's

corresponds

to

finding

a

parametric

solution

We

show

in

the

next

three

steps

how

a

Datalog

query

can

try

all

possible

substitutions

that

may

yield

a

parametric

solution

The

proof

will

follow

from

Remarks

i-ii

above

-LRB-

-RRB-

We

create

two

unary

EDB

database

relations

A

-LRB-

x

-RRB-

and

B

-LRB-

x

-RRB-

to

store

all

A

's

and

B

's

respectively

We

also

create

a

unary

IDB

relation

Aexpr

-LRB-

e

-RRB-

which

when

evaluated

bottom-up

will

contain

in

it

all

the

terms

of

the

algebra

which

can

be

written

with

only

A

's

and

the

boolean

operators

in

them

Aexpr

-LRB-

e

-RRB-

e

Bm

Aexpr

-LRB-

e

-RRB-

e

Bm

Aexpr

-LRB-

e

-RRB-

A

-LRB-

e

-RRB-

Aexpr

-LRB-

e

-RRB-

Aexpr

-LRB-

-RRB-

Aexpr

-LRB-

-RRB-

e

Bm

Aexpr

-LRB-

e

-RRB-

Aexpr

-LRB-

-RRB-

Aexpr

-LRB-

-RRB-

e

Bm

Aexpr

-LRB-

e

-RRB-

Aexpr

-LRB-

-RRB-

e

Bm

The

relation

Aexpr

has

many

elements

In

fact

it

will

consist

of

all

elements

that

can

be

written

using

the

boolean

operators

and

the

boolean

constant

symbols

Ap

There

are

distinct

elements

because

there

are

that

many

disjunctive

normal

forms

of

boolean

formulas

over

those

constant

symbols

This

is

large

but

still

a

finite

number

-LRB-

-RRB-

Our

main

relation

for

replacement

is

the

IDB

relation

Replace

-LRB-

a

B

c

d

-RRB-

whose

intended

meaning

is

that

if

in

term

a

all

occurrences

of

constant

B

are

substituted

by

a

term

c

which

has

only

A

's

and

the

boolean

operators

in

it

then

we

get

the

term

d

The

actual

bottom-up

replacement

can

be

done

recursively

as

follows

Replace

-LRB-

b

b

c

c

-RRB-

B

-LRB-

b

-RRB-

Aexpr

-LRB-

c

-RRB-

Replace

-LRB-

a

b

c

a

-RRB-

A

-LRB-

a

-RRB-

B

-LRB-

b

-RRB-

Aexpr

-LRB-

c

-RRB-

Replace

-LRB-

b

c

-RRB-

B

-LRB-

b

-RRB-

Aexpr

-LRB-

c

-RRB-

Replace

-LRB-

b

c

-RRB-

B

-LRB-

b

-RRB-

Aexpr

-LRB-

c

-RRB-

Replace

-LRB-

o

x

y

n

-RRB-

Replace

-LRB-

x

y

-RRB-

Replace

-LRB-

x

y

-RRB-

o

Bm

n

Bm

Replace

-LRB-

o

x

y

n

-RRB-

Replace

-LRB-

x

y

-RRB-

Replace

-LRB-

x

y

-RRB-

o

Bm

n

Bm

Replace

-LRB-

o

x

y

n

-RRB-

Replace

-LRB-

x

y

-RRB-

o

Bm

n

Bm

-LRB-

-RRB-

To

make

the

substitutions

in

sequence

for

each

Bi

we

order

the

B

constants

using

EDB

predicates

Next

and

Last

and

the

database

facts

Next

-LRB-

-RRB-

Next

-LRB-

Bi

Bi

-RRB-

Last

-LRB-

Bn

-RRB-

The

next

two

rules

using

IDB

predicate

Parametric

show

how

in

sequence

each

Bi

can

be

substituted

by

terms

containing

only

A

's

and

boolean

operators

Parametric

-LRB-

expr

-RRB-

Top

-LRB-

Gtop

-RRB-

Value

-LRB-

Gtop

expr

-RRB-

Parametric

-LRB-

new

j

-RRB-

Parametric

-LRB-

old

i

-RRB-

Next

-LRB-

i

j

-RRB-

Replace

-LRB-

old

j

y

new

-RRB-

When

the

last

B

constant

is

replaced

there

are

only

A

constants

present

within

expr

Hence

we

may

have

one

last

rule

Output

-LRB-

YES

-RRB-

Last

-LRB-

k

-RRB-

Parametric

-LRB-

expr

k

-RRB-

expr

Bm

As

shown

in

Lemma

deciding

whether

expr

Bm

is

equivalent

to

checking

whether

for

each

substitution

into

the

A

constants

the

constraint

expr

has

a

solution

Since

all

possible

substitutions

of

terms

containing

only

A

's

and

the

boolean

operators

for

the

B

's

are

tried

by

the

bottom-up

evaluation

there

is

any

output

iff

there

is

a

parametric

solution

iff

-LRB-

x

y

-RRB-

is

true

in

This

completes

the

reduction

Discussion

and

Open

Questions

In

this

paper

we

have

presented

many

examples

of

declarative

and

efficiently

evaluable

constraint

database

query

languages

Our

framework

is

based

on

a

study

of

quantifier

elimination

procedures

combined

with

a

use

of

data

complexity

A

number

of

technical

questions

remain

open

For

example

What

is

the

precise

data

complexity

of

Datalog

with

finite

boolean

equality

constraints

Also

is

it

possible

to

develop

a

similar

framework

for

discrete

linear

order

with

constants

Note

that

progress

has

been

made

recently

on

this

question

in

-LSB-

-RSB-

Discrete

order

can

be

used

to

model

temporal

databases

For

recent

developments

of

constraint-based

approaches

to

temporal

databases

we

refer

to

-LSB-

-RSB-

In

Section

we

left

open

the

complexity

of

tableau

containment

with

dense

linear

order

inequalities

This

has

been

recently

shown

in

-LSB-

-RSB-

It

would

be

very

interesting

to

study

the

implementation

of

the

declarative

and

efficiently

evaluable

languages

outlined

in

this

paper

The

results

presented

here

should

be

properly

viewed

as

positive

arguments

for

the

feasibility

of

such

an

effort

However

some

critical

research

questions

remain

-LRB-

-RRB-

Although

they

do

not

appear

as

part

of

the

relational

data

model

many

physical

access

structures

e.g.

B-tree

indexes

extendible

hashing

etc

are

critical

in

the

implementation

of

relational

databases

In

Section

we

argued

that

there

are

analogous

simple

access

structures

in

the

more

general

CQL

setting

-LRB-

provided

intervals

are

constraints

of

the

CQL

and

the

projection

of

any

generalized

tuple

on

x

is

an

interval

-RRB-

Is

it

possible

to

perform

searching

on

generalized

database

attribute

x

in

the

same

secondary

memory

access

bounds

that

one

uses

for

searching

on

relational

database

attribute

x

Note

that

when

this

problem

reduces

to

on-line

interval

maintenace

the

in-core

performance

of

priority

search

trees

is

linear

space

and

logarithmic

time

In

general

how

can

grid-files

R-trees

quad-trees

and

other

such

structures

be

used

to

speed-up

CQL

evaluation

strategies

-LRB-

-RRB-

The

technology

of

algorithms

for

logical

theories

is

still

rather

complex

but

much

progress

has

been

accomplished

in

recent

years

For

example

see

-LSB-

-RSB-

for

the

state-of-the-art

in

real

closed

fields

Are

there

interesting

special

cases

for

which

simple

algorithmic

techniques

can

be

used

These

would

be

analogous

to

the

special

treatment

of

project-select-join

query

programs

in

the

relational

model

In

particular

linear

inequality

constraints

should

be

investigated

in

a

CQL

framework

-LRB-

-RRB-

How

do

various

optimization

methods

combine

with

our

framework

This

would

involve

extending

-LSB-

-RSB-

For

some

recent

research

in

this

direction

we

refer

to

-LSB-

-RSB-

-LRB-

-RRB-

Constraint

query

languages

should

be

designed

in

an

extendible

way

For

example

this

would

make

it

possible

to

integrate

a

select

set

of

computational

geometry

algorithms

as

primitives

in

a

bottom-up

evaluation

-LRB-

-RRB-

Finally

constraint

query

languages

should

be

designed

with

features

such

as

database

types

and

complex

objects

Using

such

features

it

might

be

possible

to

pose

queries

about

the

representation

itself

and

not

only

about

the

unrestricted

relations

represented

Acknowledgements

We

would

like

to

thank

Jean-Louis

Lassez

for

his

constant

encouragement

We

would

also

like

to

thank

Moshe

Vardi

and

Allen

Van

Gelder

for

their

many

constructive

comments

on

the

material

presented

here

as

well

as

Raghu

Ramakrishnan

and

Terry

Smith

for

numerous

helpful

discussions

References

-LSB-

-RSB-

S.

Abiteboul

V.

Vianu

Procedural

and

Declarative

Database

Update

Languages

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

A.V.

Aho

Y.

Sagiv

J.D.

Ullman

Equivalences

among

Relational

Expressions

SIAM

J.

of

Computing

-LCB-

-LSB-

-RSB-

A.K.

Aylamazyan

M.M.

Gilula

A.P.

Stolboushkin

G.F.

Schwartz

Reduction

of

the

Relational

Model

with

Infinite

Domain

to

the

Case

of

Finite

Domains

Proc

USSR

Acad

of

Science

-LRB-

Doklady

-RRB-

-LRB-

-RRB-

-LCB-

-LSB-

-RSB-

M.

Baudinet

M.

Niezette

P.

Wolper

On

the

Representation

of

Infinite

Temporal

Data

and

Queries

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

R.

Bayer

E.

McCreight

Organization

of

Large

Ordered

Indexes

Acta

Informatica

-LCB-

-LSB-

-RSB-

M.

Ben-Or

D.

Kozen

J.

Reif

The

Complexity

of

Elementary

Algebra

and

Geometry

JCSS

-LCB-

-LSB-

-RSB-

A.H.

Borning

The

Programming

Language

Aspects

of

ThingLab

A

Constraint-Oriented

Simulation

Laboratory

ACM

TOPLAS

-LCB-

-LSB-

-RSB-

W.

Buttner

H.

Simonis

Embedding

Boolean

Expressions

into

Logic

Programming

Journal

of

Symbolic

Computation

-LCB-

-LSB-

-RSB-

A.K.

Chandra

D.

Harel

Structure

and

Complexity

of

Relational

Queries

JCSS

-LCB-

-LSB-

-RSB-

A.K.

Chandra

P.M.

Merlin

Optimal

Implementation

of

Conjunctive

Queries

in

Relational

Databases

Proc

ACM

STOC

-LCB-

-LSB-

-RSB-

J.

Chomicki

Polynomial

Time

Query

Processing

in

Temporal

Deductive

Databases

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

J.

Chomicki

T.

Imielinski

Relational

Specifications

of

Infinite

Query

Answers

Proc

ACM

SIGMOD

-LCB-

-LSB-

-RSB-

E.F.

Codd

A

Relational

Model

for

Large

Shared

Data

Banks

CACM

-LCB-

-LSB-

-RSB-

J.

Cohen

Constraint

Logic

Programming

Languages

CACM

-LCB-

-LSB-

-RSB-

A.

Colmerauer

An

Introduction

to

Prolog

III

CACM

-LCB-

-LSB-

-RSB-

D.

Comer

The

Ubiquitous

B-Tree

Computing

Surveys

-LCB-

-LSB-

-RSB-

M.

Dincbas

P.

Van

Hentenryck

H.

Simonis

A.

Aggoun

T.

Graf

F.

Berthier

The

Constraint

Logic

Programming

Language

CHIP

Proc

Fifth

Generation

Computer

Systems

Tokyo

Japan

-LSB-

-RSB-

J.

Ferrante

J.R.

Geiser

An

Efficient

Decision

Procedure

for

the

Theory

of

Rational

Order

Theoretical

Computer

Science

-LCB-

-LSB-

-RSB-

M.R.

Garey

D.S.

Johnson

Computers

and

Intractability

A

Guide

to

the

Theory

of

NP

-LCB-

completeness

Freeman

-LSB-

-RSB-

Y.

Gurevich

S.

Shelah

Fixed-Point

Extensions

of

First-Order

Logic

Annals

of

Pure

and

Applied

Logic

-LCB-

-LSB-

-RSB-

M.R.

Hansen

B.S.

Hansen

P.

Lucas

P.

van

Emde

Boas

Integrating

Relational

Databases

and

Constraint

Languages

Computer

Languages

-LCB-

-LSB-

-RSB-

R.

Helm

K.

Marriott

M.

Odersky

Constraint-based

Query

Optimization

for

Spatial

Databases

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

R.

Hull

J.

Su

Domain

Independence

and

the

Relational

Calculus

Technical

Report

-LCB-

University

of

Southern

California

-LSB-

-RSB-

N.

Immerman

Relational

Queries

Computable

in

Polynomial

Time

Information

and

Control

-LSB-

-RSB-

J.

Jaffar

J.L.

Lassez

Constraint

Logic

Programming

Proc

ACM

POPL

-LCB-

-LSB-

-RSB-

F.

Kabanza

J-M

Stevenne

P.

Wolper

Handling

Infinite

Temporal

Data

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

P.C.

Kanellakis

Elements

of

Relational

Database

Theory

Handbook

of

Theoretical

Computer

Science

Vol

B

chapter

-LRB-

J.

van

Leeuwen

editor

-RRB-

North-Holland

-LSB-

-RSB-

P.

C.

Kanellakis

G.

M.

Kuper

P.

Z.

Revesz

Constraint

Query

Languages

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

M.

Kifer

On

Safety

Domain

Independence

and

Capturability

of

Database

Queries

Proc

International

Conference

on

Databases

and

Knowledge

Bases

Jerusalem

Israel

-LSB-

-RSB-

A.

Klug

On

Conjunctive

Queries

Containing

Inequalities

JACM

-LCB-

-LSB-

-RSB-

P.

Kolaitis

C.H.

Papadimitriou

Why

not

Negation

by

Fixpoint

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

D.

Kozen

Complexity

of

Boolean

Algebras

Theo

Comp

Sci.

-LSB-

-RSB-

D.

Kozen

C.

Yap

Algebraic

Cell

Decomposition

in

NC

Proc

IEEE

FOCS

-LCB-

-LSB-

-RSB-

W.

Leler

Constraint

Programming

Languages

Addison

Wesley

-LSB-

-RSB-

A.

Levy

Y.

Sagiv

Constraints

and

Redundancy

in

Datalog

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

J.W.

Lloyd

Foundations

of

Logic

Programming

Springer-Verlag

-LSB-

-RSB-

Y.N.

Moschovakis

Elementary

Induction

on

Abstract

Structures

North

Holland

-LSB-

-RSB-

U.

Martin

T.

Nipkow

Unification

in

Boolean

Rings

Journal

of

Automated

Reasoning

-LSB-

-RSB-

E.

McCreight

Priority

Search

Trees

SIAM

J.

Computing

-LCB-

-LSB-

-RSB-

I.

N.

Mumick

S.

J.

Finkelstein

H.

Pirahesh

R.

Ramakrishnan

Magic

Conditions

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

F.P.

Preparata

M.I.

Shamos

Computational

Geometry

An

Introduction

Springer-Verlag

-LSB-

-RSB-

R.

Ramakrishnan

Magic

Templates

A

Spellbinding

Approach

to

Logic

Programs

Proc

International

Conference

on

Logic

Programming

-LCB-

-LSB-

-RSB-

J.

Renegar

On

the

Computational

Complexity

and

Geometry

of

the

First-order

Theory

of

the

Reals

Parts

I

-LCB-

III

Journal

of

Symbolic

Computation

-LCB-

-LSB-

-RSB-

P.Z.

Revesz

A

Closed

Form

for

Datalog

Queries

with

Integer

Order

Proc

International

Conference

on

Database

Theory

-LRB-

to

appear

in

TCS

-RRB-

-LSB-

-RSB-

H.L.

Royden

Real

Analysis

Ed.

-LSB-

-RSB-

H.

Samet

Applications

of

Spatial

Data

Structures

Computer

Graphics

Image

Processing

and

GIS

Addison-Wesley

Reading

MA

-LSB-

-RSB-

H.

Samet

The

Design

and

Analysis

of

Spatial

Data

Structures

Addison-Wesley

Reading

MA

-LSB-

-RSB-

V.A.

Saraswat

Concurrent

Constraint

Programming

Languages

PhD

thesis

Carnegie

Mellon

University

-LSB-

-RSB-

D.

Srivastava

R.

Ramakrishnan

Pushing

Constraint

Selections

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

G.L.

Steele

The

Definition

and

Implementation

of

a

Computer

Programming

Language

Based

on

Constraints

Ph.D.

thesis

MIT

AI-TR

-LSB-

-RSB-

A.

Tarski

A

Decision

Method

for

Elementary

Algebra

and

Geometry

University

of

California

Press

Berkeley

California

-LSB-

-RSB-

J.D.

Ullman

Principles

of

Database

Systems

Computer

Science

Press

Ed.

-LSB-

-RSB-

J.D.

Ullman

A.

Van

Gelder

Parallel

Complexity

of

Logical

Query

Programs

Algorithmica

-LSB-

-RSB-

R.

van

der

Meyden

The

Complexity

of

Querying

Indefinite

Data

about

Linearly

Ordered

Domains

Proc

ACM

PODS

-LCB-

-LSB-

-RSB-

P.

Van

Hentenryck

A

Logic

Language

for

Combinatorial

Optimization

Annals

of

Operations

Research

-LCB-

-LSB-

-RSB-

P.

Van

Hentenryck

Constraint

Satisfaction

in

Logic

Programming

MIT

Press

-LSB-

-RSB-

M.Y.

Vardi

The

Complexity

of

Relational

Query

Languages

Proc

ACM

STOC

-LCB-

constraint logic programming
data complexity
database queries
datalog
quantifier elimination
relational calculus
spatial databases