Algorithm Search⌘K

Definitionadvanced

Domain-independent query

Also called safe query, safe-range query.

A calculus query whose answer depends only on the database and not on the domain of values. The property is undecidable; the safe-range queries are a decidable subclass with the same power.

A query of the relational calculus is domain independent when its answer on a database is the same for every domain of values that contains the constants of the database and of the query. Such a query asks only about the data: it cannot return a value that is in neither, and its answer does not grow when the domain grows. The property is undecidable (Di Paola 1969), so database theory uses the safe-range formulas of Abiteboul, Hull and Vianu, a syntactic class that one checks by a pass over the formula and that expresses the same queries. By Codd's theorem, these are the queries of relational algebra.

Definition

The notation follows the relational calculus page, under set semantics. A query is Q={(x1,…,xk)∣φ}Q = \{(x_1, \ldots, x_k) \mid \varphi\} over a schema, II is an instance, and Qd(I)Q_{\mathbf{d}}(I) is the answer when the variables range over a set d\mathbf{d} with adom(Q,I)⊆d\mathrm{adom}(Q, I) \subseteq \mathbf{d}.

Domain independence (semantic). QQ is domain independent if for every instance II and every two sets d,d′\mathbf{d}, \mathbf{d}' with adom(Q,I)⊆d,d′⊆dom\mathrm{adom}(Q, I) \subseteq \mathbf{d}, \mathbf{d}' \subseteq \mathbf{dom},

Qd(I)=Qd′(I).Q_{\mathbf{d}}(I) = Q_{\mathbf{d}'}(I).

Then Q(I)Q(I) denotes this common answer. It is a subset of adom(Q,I)k\mathrm{adom}(Q, I)^k, since the answer for d=adom(Q,I)\mathbf{d} = \mathrm{adom}(Q, I) is. Codd's range-coupled calculus and Di Paola's "definite formulas" are earlier names for the same idea.

Safe-range (syntactic). First put φ\varphi in safe-range normal form (SRNF): rename bound variables so that no variable is bound twice or both bound and free; replace ∀x ψ\forall x\, \psi by ¬∃x ¬ψ\neg \exists x\, \neg \psi and ψ→χ\psi \rightarrow \chi by ¬ψ∨χ\neg \psi \lor \chi; push each negation down with De Morgan's laws and ¬¬ψ=ψ\neg\neg\psi = \psi, until it stands in front of an atom or of an ∃\exists; flatten nested ∧\land and ∨\lor. Then compute the set rr(φ)\mathrm{rr}(\varphi) of range-restricted variables, or the failure value ⊥\bot, by these rules (any rule that meets ⊥\bot in a part returns ⊥\bot):

  • rr(R(e1,…,ek))\mathrm{rr}(R(e_1, \ldots, e_k)) is the set of variables among e1,…,eke_1, \ldots, e_k;
  • rr(x=c)={x}\mathrm{rr}(x = c) = \{x\} for a constant cc, and rr(x=y)=∅\mathrm{rr}(x = y) = \emptyset for variables;
  • rr(ψ∧x=y)\mathrm{rr}(\psi \land x = y) is rr(ψ)∪{x,y}\mathrm{rr}(\psi) \cup \{x, y\} if xx or yy is in rr(ψ)\mathrm{rr}(\psi), and rr(ψ)\mathrm{rr}(\psi) if not; for other conjunctions, rr(ψ∧χ)=rr(ψ)∪rr(χ)\mathrm{rr}(\psi \land \chi) = \mathrm{rr}(\psi) \cup \mathrm{rr}(\chi);
  • rr(ψ∨χ)=rr(ψ)∩rr(χ)\mathrm{rr}(\psi \lor \chi) = \mathrm{rr}(\psi) \cap \mathrm{rr}(\chi);
  • rr(¬ψ)=∅\mathrm{rr}(\neg \psi) = \emptyset;
  • rr(∃x ψ)=rr(ψ)∖{x}\mathrm{rr}(\exists x\, \psi) = \mathrm{rr}(\psi) \setminus \{x\} if x∈rr(ψ)x \in \mathrm{rr}(\psi), and ⊥\bot if not.

QQ is safe-range if rr(SRNF(φ))=free(φ)\mathrm{rr}(\mathrm{SRNF}(\varphi)) = \mathrm{free}(\varphi). This is the definition of AHV, Section 5.4. A variable is range restricted when every way to make the formula true binds it to a value of an atom or a constant: that is why a disjunction keeps only the variables that both sides restrict, and a negation restricts none.

The two facts. Every safe-range query is domain independent, by induction on the SRNF: a subformula with rr=X\mathrm{rr} = X is false under every valuation that maps a variable of XX outside the active domain, and each quantifier ranges only over values that some atom supplies. Conversely, every domain-independent query is equivalent to a safe-range query, because it equals an algebra expression, and each algebra expression translates into a safe-range formula. Both facts are parts of the proof of Codd's theorem.

Undecidability. No algorithm decides whether a calculus query is domain independent. Here is a reduction from the finite satisfiability of first-order sentences, which is undecidable by Trakhtenbrot's theorem (AHV, Chapter 6). Let ψ\psi be a sentence over a schema with a relation of arity at least 2, and add a unary relation name DD. Let ψD\psi^{D} be ψ\psi with each quantifier restricted to DD: ∃x χ\exists x\, \chi becomes ∃x (D(x)∧χ)\exists x\, (D(x) \land \chi) and ∀x χ\forall x\, \chi becomes ∀x (D(x)→χ)\forall x\, (D(x) \rightarrow \chi). Let

Qψ={ (z)∣∃y D(y)∧ψD }.Q_\psi = \{\, (z) \mid \exists y\, D(y) \land \psi^{D} \,\}.

The sentence ∃y D(y)∧ψD\exists y\, D(y) \land \psi^{D} does not depend on the domain, because all its quantifiers range over DD. If ψ\psi has no finite model, it is false in every instance, and QψQ_\psi returns ∅\emptyset on every domain, so QψQ_\psi is domain independent. If ψ\psi has a finite model AA, let II hold the relations of AA and $I(D) = $ the universe of AA. Then the sentence is true in II, and Qd(I)=dQ_{\mathbf{d}}(I) = \mathbf{d} for every d\mathbf{d}, so QψQ_\psi is not domain independent. So QψQ_\psi is domain independent if and only if ψ\psi has no finite model. In contrast, the test "is QQ safe-range" is one pass over the SRNF, with set operations on the free variables, so it takes polynomial time in the size of the formula.

Examples and non-examples

Use the schema Takes(student,course)\mathrm{Takes}(\mathit{student}, \mathit{course}), Course(course)\mathrm{Course}(\mathit{course}) of the relational calculus page, and its instance with five constants.

  • Safe-range. {(s)∣Takes(s,db)}\{(s) \mid \mathrm{Takes}(s, \mathit{db})\}: the atom gives rr={s}\mathrm{rr} = \{s\}, which equals the free variables.
  • Safe-range with a universal quantifier. {(s)∣∃c Takes(s,c)∧∀c (Course(c)→Takes(s,c))}\{(s) \mid \exists c\, \mathrm{Takes}(s, c) \land \forall c\, (\mathrm{Course}(c) \rightarrow \mathrm{Takes}(s, c))\}. After renaming the second cc to c′c', the SRNF is ∃c Takes(s,c)∧¬∃c′ (Course(c′)∧¬Takes(s,c′))\exists c\, \mathrm{Takes}(s, c) \land \neg \exists c'\, (\mathrm{Course}(c') \land \neg \mathrm{Takes}(s, c')). Inside, rr(Course(c′)∧¬Takes(s,c′))={c′}\mathrm{rr}(\mathrm{Course}(c') \land \neg \mathrm{Takes}(s, c')) = \{c'\}, so the inner ∃c′\exists c' gives ∅\emptyset and the negation gives ∅\emptyset. The first conjunct gives {s}\{s\}. The union is {s}\{s\}, the free variables. The query is safe-range, and its answer is {ann}\{\mathit{ann}\} on every domain.
  • Not safe-range and not domain independent. {(s)∣¬Takes(s,db)}\{(s) \mid \neg \mathrm{Takes}(s, \mathit{db})\} has rr=∅≠{s}\mathrm{rr} = \emptyset \neq \{s\}. {(x,y)∣Takes(x,db)∨Course(y)}\{(x, y) \mid \mathrm{Takes}(x, \mathit{db}) \lor \mathrm{Course}(y)\} has rr={x}∩{y}=∅\mathrm{rr} = \{x\} \cap \{y\} = \emptyset. {(s)∣∀c Takes(s,c)}\{(s) \mid \forall c\, \mathrm{Takes}(s, c)\} has SRNF ¬∃c ¬Takes(s,c)\neg \exists c\, \neg \mathrm{Takes}(s, c), and the rule for ∃c\exists c fails, since c∉rr(¬Takes(s,c))=∅c \notin \mathrm{rr}(\neg \mathrm{Takes}(s, c)) = \emptyset. The relational calculus page shows that each of the three answers changes with the domain.
  • Domain independent but not safe-range. {(x)∣R(x)∨(x=x∧¬(x=x))}\{(x) \mid R(x) \lor (x = x \land \neg (x = x))\}. The second disjunct is false for every value, so the query is equivalent to {(x)∣R(x)}\{(x) \mid R(x)\} and is domain independent. But rr(x=x∧¬(x=x))=∅\mathrm{rr}(x = x \land \neg(x = x)) = \emptyset, so rr\mathrm{rr} of the disjunction is {x}∩∅=∅\{x\} \cap \emptyset = \emptyset, and the query is not safe-range. Safe-range is a sufficient condition, not a necessary one; it only promises that an equivalent safe-range query exists.
  • Active domain semantics. Any query, safe or not, has a domain-independent version: relativize every free and bound variable to the formula adom(x)\mathrm{adom}(x), the disjunction of ∃…R(…,x,…)\exists \ldots R(\ldots, x, \ldots) over each relation and position and of x=cx = c over each constant of QQ. On the instance above, {(s)∣adom(s)∧¬Takes(s,db)}\{(s) \mid \mathrm{adom}(s) \land \neg \mathrm{Takes}(s, \mathit{db})\} returns {cal,db,os}\{\mathit{cal}, \mathit{db}, \mathit{os}\} on every domain. This is how a system can evaluate any formula, but the answer is often not the intended one.
  • Relational calculus and algebra. The definitions of the semantics are on relational calculus, and the equivalence with the algebra is Codd's theorem.
  • Other syntactic classes. Ullman's safe formulas (Ullman 1988, Chapter 3) are another syntactic class of domain-independent formulas, with other rules for the same aim. The book shows that they also express every query of the algebra.
  • Range restriction in rules. A conjunctive query and a Datalog rule must have each head variable in a positive body atom. That is the same condition for the positive fragment, and it makes each rule domain independent.
  • Other words "safe". In probabilistic databases, a "safe plan" is a plan that computes the right probabilities by independence rules (see probabilistic database). That notion has nothing to do with domain independence.
  • Decision problems. Domain independence is undecidable for the same reason that satisfiability and containment of calculus queries are undecidable: all three encode finite satisfiability, see Church's theorem for the unrestricted case and undecidability for the general idea.

References

  1. Serge Abiteboul, Richard Hull and Victor Vianu, Foundations of Databases, Addison-Wesley (1995), Section 5.3 (domain independence and the active domain semantics), Section 5.4 (safe-range normal form, the range restriction rules and safe-range queries), Chapter 6 (undecidability results for the calculus)
  2. Robert A. Di Paola, The recursive unsolvability of the decision problem for the class of definite formulas, Journal of the ACM 16(2), 324 to 327 (1969), there is no algorithm that decides whether a first-order formula is definite (domain independent)
  3. E. F. Codd, Relational completeness of data base sublanguages, in R. Rustin (ed.), Data Base Systems, Courant Computer Science Symposia 6, Prentice-Hall (1972), 65 to 98, the range-coupled tuple calculus, whose queries are domain independent by construction
  4. Jeffrey D. Ullman, Principles of Database and Knowledge-Base Systems, Volume I, Computer Science Press (1988), Chapter 3 (the relational calculus, domain independence and a syntactic class of safe formulas)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/CYWMQ4/text.yaml