---
title: Maximally Contained Rewritings
url: https://www.emergentmind.com/topics/maximally-contained-rewritings
type: topic
---

# Maximally Contained Rewritings

Maximally contained rewritings (MCRs) are a central concept in the theory of query answering using views, particularly for conjunctive queries with arithmetic comparisons (CQACs). An MCR encodes all possible certain answers to a target query, leveraging contained rewritings over a specified set of views and is uniquely determined within a union-of-CQACs (U-CQAC) query language under the open-world assumption (OWA). This framework is critical in understanding both algorithmic feasibility and theoretical limitations in the presence of arithmetic constraints, especially those involving semi-interval (SI) comparisons.

## 1. Formal Framework and Definitions

Let
\[
Q\quad:\quad h(\overline X)\;:-\;e_1(\overline X_1),\dots,e_k(\overline X_k),\;\beta_Q
\]
be a CQAC query over base schema $\cal R$, where $e_i$ are relational atoms and $\beta_Q$ is a conjunction of arithmetic comparisons. A set of CQAC views $\V = \{V_1, \ldots, V_n\}$ is assumed, with each $V_j$ similarly defined.

A **rewriting** $R$ of $Q$ using $\V$ is any CQAC whose atoms mention only predicates $V_j$. $R$ is a **contained rewriting** if, for every instance $I \subseteq \bigcup_j V_j(D)$ (for some database $D$),
\[
R(I) \subseteq Q(D).
\]
A **maximally contained rewriting** $R_{\sf MCR}$ is a contained rewriting such that for every other contained rewriting $R$ using $\V$ in language $\cal L$,
\[
R \sqsubseteq R_{\sf MCR}.
\]
Equivalently, $R_{\sf MCR}$ is defined by the (possibly infinite) union of all contained rewritings:
\[
R_{\sf MCR} = \bigsqcup \{ R \mid R \text{ is a contained rewriting of } Q \text{ using } \V \}.
\]
This MCR captures precisely the certain answers to $Q$ with respect to view instance $I$, i.e., $R_{\sf MCR}(I) = \mathsf{certain}(Q, I)$, where
\[
\mathsf{certain}(Q, I) = \bigcap_{\substack{D': I \subseteq \V(D')}} Q(D').
\]
This characterization assures that MCRs do not overgenerate answers and are maximal among all contained rewritings in their expressive class [2509.10138].

## 2. Containment and Expansion in CQACs

The containment property crucial for MCR construction is based on view-expansion. For a rewriting $R$ with body view atoms $V_{j_1}, \dots, V_{j_\ell}$, its **expansion** $R^{\sf exp}$ is obtained by unfolding each $V_{j_r}$ using its CQAC definition and suitably renaming variables. Containment then reduces to query containment in the CQAC sense:
\[
R \text{ is contained in } Q \Longleftrightarrow R^{\sf exp} \sqsubseteq Q.
\]
Maximal containment requires that $R_{\sf MCR}$ subsumes every other contained rewriting, which corresponds to returning the intersection (across base instances) of what $Q$ could return given the constraints imposed by the views, in all completions consistent with the available view data.

## 3. Algorithmic Construction of MCRs

While Minicon-style algorithms suffice for plain conjunctive queries, CQACs introduce arithmetic complications. The paper identifies the prominent tractable subclass of CQACs—when $Q$ is in the class $\mathsf{RSI1}$ (restricted to SI comparisons and at most one right-SI comparison).

The construction follows a two-stage process:

1. **Datalog$^{AC}$ Transformation**: A $\mathsf{RSI1}$ CQAC $Q$ is transformed into a Datalog$^{AC}$ program $Q^{\sf Datalog}$ by encoding SI comparisons as unary predicates (e.g., $I_{\geq c_0}(X)$), including mapping rules for SI-ACs, coupling rules enforcing their Boolean structure, link rules for constants in comparisons, and transitive closure rules for the order predicate $U(\_,\_)$.
2. **Inverse-Rule MCR Construction**: Each view is similarly transformed into a CQ using the new predicate symbols; existing Datalog MCR algorithms (e.g., the inverse-rule method) are then applied to compute the MCR in Datalog$^{AC}$. The result is reverse-transformed back to a union of CQACs by interpreting the unary/binary predicates as their associated arithmetic comparison constraints.

Pseudocode summary:
```plaintext
function Build_MCR_RSI1(Q, Views V):
  // Stage I: build Datalog^AC
  Qd := TransformToDatalogAC(Q)
  Vcq := { TransformToCQ(view) for view in V }
  AddTransitiveClosureRules(Qd)
  // Stage II: compute MCR in Datalog^AC
  RcqMCR := InverseRule_MCR(Qd, Vcq)
  // Reverse-transform back to CQAC
  return ReverseTransform(RcqMCR)
```
The MCR constructed in this way is shown to be unique and correct for $\mathsf{RSI1}$ queries with respect to the original CQAC views [2509.10138, Theorems 8.8 & 8.9].

## 4. Computational Complexity

The complexity of containment and MCR computation depends critically on the structure of arithmetic comparisons in $Q$ and the views. The following table (from Table 1 in the data) summarizes key dichotomies:

| CQACs Allowed in $Q_2$   | CQACs Allowed in $Q_1$         | Complexity               |
|--------------------------|-------------------------------|--------------------------|
| arbitrary CQAC           | one AC + equality             | NP                       |
| arbitrary CQAC           | closed left-SI only           | NP                       |
| arbitrary CQAC           | SI + one RSI                  | NP                       |
| contains OSI, $\neq$     | OSI only                      | $\Pi^p_2$-complete       |
| etc.                     | etc.                          | $\Pi^p_2$-complete       |

For queries where $Q_1$ (the containing query) uses at most one SI comparison or only (closed) left-SI or right-SI, containment is in NP. With open SI comparisons (OSI) or combination with inequality ($\neq$), the problem becomes $\Pi_2^p$-complete via reduction from $\forall\exists$-QBF [2509.10138, Theorems 4.1, 4.2, 5.1, 5.3]. The NP-membership proofs rely on the fact that only one containment mapping may be active, allowing for efficient certification (Lemma 5.4, Proposition 6.2).

## 5. MCRs and the Certain Answers Semantics

For CQACs, MCRs precisely capture all certain answers under the open-world assumption. That is, for any $Q$, $\V$, and view-instance $I$:
\[
R_{\sf MCR}(I) = \mathsf{certain}(Q, I).
\]
This result holds for the language of U-CQAC, including possibly infinite unions. The proof is by maximality and containment: $R_{\sf MCR}$ includes every possible contained rewriting—thus capturing all certain answers—and does not admit any superfluous ones. In cases where MCRs can be effectively constructed and are finite, the evaluation is in polynomial time with respect to the data ($I$), as each CQAC can be evaluated in PTIME data-complexity [2509.10138, Theorem 7.1, Section 7].

## 6. Illustrative Examples

Two representative examples illustrate the mechanics and minimality property of MCRs:

**Example 1:**
\[
Q(): e(X,Y),\ e(Y,Z),\ X \ge 5,\ Z \le 5
\]
Views:
\[
V_1(Z): e(X,Y),e(Y,Z), X \ge 5;\quad V_2(X): e(X,Y),e(Y,Z), Z \le 5
\]
No single view suffices. The MCR in U-CQAC is the union:
\[
\begin{aligned}
R_1():\ &V_1(Z),\ Z \le 5 \\
R_2():\ &V_2(X),\ X \ge 5 \\
R_3():\ &V_1(Z),\ V_2(X),\ X \ge Z
\end{aligned}
\]
Each $R_i$ is contained; any other contained rewriting is subsumed. $R_{\sf MCR}(I)$ thus recovers all $(x,z)$ with $x \ge 5, z \le 5$, and $\exists y$ in $I$.

**Example 2:**
\[
Q(): p(A), A < 4
\]
\[
V_2(Y,Z): p(X), s(Y,Z), Y \le X, X \le Z
\]
A contained (but non-minimal) rewriting:
\[
R(Y_1): V_2(Y_1,Z_1), V_2(Y_2,Z_2), Z_1 \le Y_2, Y_1 \ge Z_2, Y_1 < 4
\]
Rectified to (by adding implied comparisons), yielding a minimal MCR:
\[
R'(X): V_2(X, X),\ X < 4
\]
Algorithmic construction via Datalog$^{AC}$ discovers such minimal self-join rewritings.

## 7. Significance and Research Context

MCRs provide a canonical solution for answering queries using views in the presence of arithmetic comparisons, particularly under the OWA and for data integration settings. The precise tractability boundaries for containment and certain answer computation, especially for SI and OSI comparisons, distinguish this framework from earlier purely relational cases. This aligns MCR computation for CQACs with seminal work by Chandra & Merlin (1977), Klug (1988), and others on query containment and rewriting, but uniquely addresses cases with arithmetic constraints [2509.10138].

The correct and efficient computation of MCRs remains an active area in database theory, as highlighted in recent work by Afrati and Damigos and foundational approaches to inverse-rule rewriting (Duschka & Wagner, 1997). This suggests significant ongoing relevance for applications in probabilistic databases, information integration, and robust data exchange frameworks.

Source: https://www.emergentmind.com/topics/maximally-contained-rewritings