betaFeedback

WorksComputer Science

Order-Dependent Complexity of Enumeration and Direct Access for Conjunctive Queries on Dynamic Data

Abstract

This note studies the fine-grained complexity of ranked enumeration and ranked direct access of conjunctive query answers over a dynamic database. We focus on lexicographically sorted answers, defined by an ordering on the query's free variables. From the literature, when no particular order is required, we know that when the query is self-join-free and q-hierarchical, we can build a data structure for enumeration (resp. direct access) with linear (resp. linearithmic) preprocessing and constant (resp. logarithmic) update time in the size of the database. For enumeration (resp. direct access), this data structure yields an algorithm that enumerates all query answers (resp. that returns the kk-th query answer) with constant delay (resp. logarithmic access time). Our goal is to extend these results to ranked enumeration and ranked direct access, with the ranking function defined by a lexicographic order. We define a property on the order over the variables of QQ, which we call being hierarchy-consistent. Given a user-specified query and hierarchy-consistent order, we first propose, for ranked enumeration under updates, an algorithm with linear preprocessing and logarithmic update time that enumerates all the query answers according to the user-specified order with constant delay. Moreover, for ranked direct access under updates, we propose an algorithm with linear preprocessing and logarithmic update time that returns the kk-th tuple according to the user-specified order with logarithmic access time. In contrast, given a user-specified query and non-hierarchy-consistent order, we show a lower bound for ranked direct access under updates.

Introduction

For a conjunctive query (or CQ) QQ, given a database DD, materializing the answers Q(D)Q(D) can be costly: the number of answers ∣Q(D)∣|Q(D)| can be up to ∣D∣∣Q∣|D|^{|Q|}. Producing or holding the full answer set at once is therefore often impractical. Instead, it is more natural to find approaches that allow us to access partial information about the query result without materializing it. In particular, we may want to access the kkth answer to QQ on DD, or to enumerate the answers one by one without repetitions.

These tasks are typically formalized in database theory in two ways. The first one is enumeration: rather than producing all tuples at once, an enumeration algorithm outputs them one by one, with a guarantee of bounded delay between two consecutive tuples. The second one is direct access: this is the task of outputting, on an input kk, the kkth answer for QQ over DD.

It is well-known from the work of Bagan, Durand, and Grandjean [2] that we can enumerate the answers to self-join-free CQs with linear preprocessing and constant delay in data complexity provided that these queries satisfy the so-called acyclic free-convex condition; this restriction is (conditionally) necessary within the class of self-join-free CQs, and a characterization of tractable CQs with self-joins is still open [5, 13]. What is more, Brault-Baron [1] has shown that direct access was possible with linearithmic preprocessing and logarithmic access time in data complexity. We also note the line of work on factorized databases [12], which represent query results compactly using a structure derived from the query, and enumerate the answers with constant delay when no particular order is required or when the user-defined order is compatible with this structure.

The literature has then extended these questions along two independent axes: supporting efficient updates to the underlying database (and updating the structure used for enumeration or direct access without recomputing it from scratch); and providing results in a specific order (e.g., in some lexicographic order specified by the user). The goal of the present work is to bridge these two directions. We present existing results on each of them in turn.

First, in the dynamic setting, [3] show that q-hierarchical conjunctive queries can be enumerated with constant delay and constant update time after linear preprocessing, and that for self-join-free CQs this classification is (conditionally) optimal. An analogous result for direct access is shown in [10]. Further works have investigated which guarantees could be provided for CQs beyond this class; this is not the focus of the present work, but we review these directions nevertheless. Dynamic Yannakakis [9] maintains constant-delay enumeration for all free-connex queries (i.e., more generally than q-hierarchical conjunctive queries) with a more costly update time (namely, it is linear in the input database). This was then refined in [15] with an update cost that depends on the update sequence, which becomes constant for insertion-only sequences.

We now review the different types of data structures that are used to show efficient enumeration and direct access guarantees on dynamic data; we will re-use some of them in the present work. [10] use a circuit-like structure that stores the assignments of each variable of the q-tree in AVL trees. Circuits could also describe such a structure, but they are not so convenient to maintain in the dynamic setting, given that the domain of assignments is not fixed and updates add or remove edges of the circuit. In contrast, [11] use view-trees, whose interface and implementation can be defined separately; we re-use the structure of view-trees in the present paper. In all these works, the answers are produced in an arbitrary order determined by the data structure, and ranked enumeration or ranked direct access under updates is not handled.

Second, for ranked enumeration or direct access in the static setting, where answers are output according to a ranking function, an important earlier work is [14]. For acyclic free-connex queries, they build a data structure in linearithmic data complexity, after which the answers to QQ on DD can be enumerated in any lexicographic order with logarithmic delay between consecutive tuples. Ranked direct access, which returns the kk-th answer in the user-defined order, has been studied by [6], who also build a data structure in quasilinear time, with logarithmic access time. Circuits, which are compact representations of the answers, have also been used for direct access, for conjunctive queries with negation [4], and for enumeration beyond conjunctive queries through knowledge compilation [8]. None of these works considers updates to the database.

Hence, our contribution sits at the intersection of these two lines of work: which orders can we support in the dynamic setting? Since we consider the dynamic setting, we build on the dichotomy established by [10] (and [3] for enumeration), which identifies q-hierarchical queries as the class for which linear preprocessing, constant delay or logarithmic access, and constant updates are possible under updates. We therefore restrict our attention to this class of queries, and we maintain the answers with view-trees [11].

Contributions. This note presents the preliminary results that we have obtained for ranked enumeration and ranked direct access to conjunctive queries under updates. We focus on q-hierarchical queries, because otherwise intractability holds (for self-join-free queries, and conditionally) even in the absence of ranking functions. We study lexicographic orders defined by an order on the query’s free variables, and introduce a condition on such orders, called being hierarchy-consistent. We show that, for hierarchy-consistent orders, we can perform ranked enumeration and ranked direct access following a lexicographic ordering, with the same favorable guarantees as in the case without a specific ordering, up to a logarithmic update time instead of constant update time for enumeration. By contrast, we show that, for non-hierarchy-consistent orders, a conditional intractability result rules out efficient ranked direct access.

Paper structure. The paper is organized as follows: § 2 introduces the notations and concepts necessary to understand the paper. In § 3, we define the data structure used for ranked enumeration and ranked direct access in the dynamic setting, and give an algorithm to build it. In § 4, we define a property on the user-defined order, called hierarchy-consistency, and give two algorithms for ranked enumeration and ranked direct access in the dynamic setting when the order satisfies this property, with bounds in combined complexity. In § 5, we show that when the order does not satisfy this property, ranked direct access in the dynamic setting is conditionally not achievable, in data complexity, with polynomial preprocessing, sublinear access time, and sublinear update time.

Preliminaries

Basic Notions

Database. A schema σ\sigma is a finite set of relation symbols {R1,…,Rs}\{R_1,\ldots,R_s\}, with every R∈σR \in\sigma having a fixed arity ar⁡(R)\operatorname{ar}(R). A database instance DD is of the form D={R1D,…,RsD}D = \{R_1^D,\ldots,R_s^D\} where, for each R∈σR \in\sigma, RD⊆dom⁡ar⁡(R)R^D \subseteq\operatorname{dom}^{\operatorname{ar}(R)} is a finite set of tuples (a1,…,aar⁡(R))(a_1,\ldots,a_{\operatorname{ar}(R)}), and dom⁡\operatorname{dom} is a set of constant values called the domain, on which we assume a total order. The size ∣D∣|D| of the database instance DD is the number of tuples that it stores, i.e. it is ∣D∣:=∑R∈σ∣RD∣|D| := \sum_{R \in\sigma} |R^D|.

We fix an infinite set of variables denoted by vars\mathit{vars}. For a tuple t=(a1,…,al)t = (a_1,\ldots,a_l) indexed by variables (x1,…,xl)∈varsl(x_1,\ldots,x_l) \in\mathit{vars}^l and a set X⊆{x1,…,xl}X \subseteq\{x_1,\ldots,x_l\}, we write t[X]t[X] for the restriction of tt to the variables of XX, preserving the order of tt. Conversely, for a set of variables Y⊇{x1,…,xl}Y \supseteq\{x_1,\ldots,x_l\} ordered by a total order ≺\prec that agrees with (x1,…,xl)(x_1,\ldots,x_l) on {x1,…,xl}\{x_1,\ldots,x_l\}, a tuple t′t' indexed by YY is an extension of tt if t′[{x1,…,xl}]=tt'[\{x_1,\ldots,x_l\}] = t, preserving the order ≺\prec.

Updates. We consider two types of updates: insertions and deletions. An insertion is defined as a command insert R(a1,…,aar⁡(R))\mathtt{insert}\ R(a_1,\ldots,a_{\operatorname{ar}(R)}). Applied to a database instance DD, this results in a database instance D′D' with RD′:=RD∪{(a1,…,aar⁡(R))}R^{D'} := R^D \cup\{(a_1,\ldots,a_{\operatorname{ar}(R)})\}. Similarly, a deletion is defined as a command delete R(a1,…,aar⁡(R))\mathtt{delete}\ R(a_1,\ldots,a_{\operatorname{ar}(R)}). Applied to a database instance DD, this results in a database instance D′D' with RD′:=RD∖{(a1,…,aar⁡(R))}R^{D'} := R^D \setminus\{(a_1,\ldots,a_{\operatorname{ar}(R)})\}. Note that insert\mathtt{insert} does not change DD if the inserted tuple already exists in DD; likewise, DD is unchanged when we delete a tuple that DD does not contain.

Queries. A conjunctive query (CQ) is defined as Q(xˉ):=∃yˉ(ψ1∧⋯∧ψd)Q(\bar{x}) := \exists\bar{y}(\psi_1 \land\cdots\land\psi_d) where xˉ:=(x1,…,xl)\bar{x} := (x_1,\ldots,x_l), yˉ:=(y1,…,yk)\bar{y} := (y_1,\ldots,y_k) and each ψi\psi_i is an atom of the form R(z1,…,zar⁡(R))R(z_1,\ldots,z_{\operatorname{ar}(R)}). We mainly focus on CQs that are self-join-free, written SJFCQ\mathsf{SJF CQ}, if no relation symbol occurs more than once in the query. For an atom ψ\psi or a query QQ, we write vars⁡(ψ)⊆vars\operatorname{vars}(\psi) \subseteq\mathit{vars} and vars⁡(Q)⊆vars\operatorname{vars}(Q) \subseteq\mathit{vars} for the set of variables occurring in ψ\psi and QQ, respectively. For a variable x∈vars⁡(Q)x \in\operatorname{vars}(Q), we denote by atoms⁡Q(x)\operatorname{atoms}_Q(x) the finite set of atoms in QQ in which xx appears, i.e. atoms⁡Q(x)={ψ∈Q∣x∈vars⁡(ψ)}\operatorname{atoms}_Q(x) = \{\psi\in Q \mid x \in\operatorname{vars}(\psi)\}. Moreover, the set of variables vars⁡(Q)\operatorname{vars}(Q) is partitioned into free variables free⁡(Q):={x1,…,xl}\operatorname{free}(Q) := \{x_1,\ldots,x_l\} and quantified variables {y1,…,yk}\{y_1,\ldots,y_k\}. In addition, for a CQ Q(xˉ):=∃yˉ(ψ1∧⋯∧ψd)Q(\bar{x}) := \exists\bar{y}(\psi_1 \land\cdots\land\psi_d) and a set of variables X⊆free⁡(Q)X \subseteq\operatorname{free}(Q), the projection of QQ over XX, denoted by ΠX(Q)\Pi_X(Q), is the CQ ΠX(Q):=∃yˉ∃(xˉ∖X)(ψ1∧⋯∧ψd)\Pi_X(Q) := \exists\bar{y}\exists(\bar{x} \setminus X)(\psi_1 \land\cdots\land\psi_d).

Finally, given a query Q(x1,…,xl)Q(x_1,\ldots,x_l) and a database instance DD, the set of answers to QQ over DD, i.e. the set of tuples resulting from applying QQ to DD, is defined as Q(D):={(a1,…,al)∈dom⁡l∣D⊨Q(a1,…,al)}Q(D) := \{(a_1,\ldots,a_l) \in\operatorname{dom}^l \mid D \models Q(a_1,\ldots,a_l)\}. Note that ΠX(Q)(D)={t[X]∣t∈Q(D)}\Pi_X(Q)(D) = \{t[X] \mid t \in Q(D)\}, i.e., the answers of the projection of a query over XX are the restrictions of its answers to XX.

Problem Definition

The model of computation is the RAM model with uniform cost measure and word size Θ(log⁡n)\Theta(\log n). Under this assumption, additions and multiplications on integers of size O(log⁡n)O(\log n), as well as lookup table and array accesses, take constant time.

Given this computation model, we consider two query evaluation problems throughout this paper: ranked enumeration under updates and ranked direct access under updates. To define these problems, we first need to introduce the notion of orders.

Order on answers. Let DD be a database instance, QQ a query, and ≺\prec a total order over free⁡(Q)\operatorname{free}(Q). Let (x1,…,xl)(x_1,\ldots,x_l) be the free variables of QQ listed according to this order. We define an order on answers ≺\prec on Q(D)Q(D) induced by ≺\prec, called the lexicographic order, as follows. For two answers t1,t2∈Q(D)t_1,t_2 \in Q(D), we say that t1≺t2t_1 \prec t_2 if there exists j∈[l]j \in[l] such that we have t1[xi]=t2[xi]t_1[x_i] = t_2[x_i] for all i<ji < j and we have t1[xj]<t2[xj]t_1[x_j] < t_2[x_j], where << denotes the total order on the domain values.

Enumeration. The enumeration problem consists of two phases. Given a database instance DD and a query QQ, the preprocessing phase builds a data structure DD. The enumeration phase outputs all tuples in Q(D)Q(D) without repetition, and finally emits a special value end-of-enumeration to signal the end of enumeration. We consider several extensions of this problem. First, if a total order ≺\prec over free(Q)(Q) is given before the preprocessing, the task of ranked enumeration asks us to output all tuples in Q(D)Q(D) in the lexicographic order ≺\prec on Q(D)Q(D) induced by ≺\prec. Second, if an update command (insert R(a1,…,aar⁡(R))R(a_1,\ldots,a_{\operatorname{ar}(R)}) or delete R(a1,…,aar⁡(R))R(a_1,\ldots,a_{\operatorname{ar}(R)})) is issued after the preprocessing, letting DD be the original database instance and D′D' be the database instance obtained by applying the update command to DD, then the task of enumeration under updates asks us to modify the data structure DD to a data structure D′D' that represents the updated database instance, i.e., which allows us to enumerate the tuples of Q(D′)Q(D'). If the update command occurs during the enumeration phase, we interrupt enumeration and restart it from scratch on the new database D′D'. Combining both variants, the ranked enumeration under updates is the version where we are given the database instance DD, the query QQ, and a total order ≺\prec over free(Q)(Q), and where we must be able to enumerate tuples of Q(D)Q(D) in the order ≺\prec and also support updates to DD in the sense above.

Direct access. The direct access problem consists of two phases. Given a database instance DD and a query QQ, the preprocessing phase builds a data structure DD. Then, in the access phase, we receive user queries consisting of an index k≥0k \ge0, and we must return the kthk^{\text{th}} tuple of Q(D)Q(D) in an implementation-defined order, or a special value out-of-bounds if k≥∣Q(D)∣k \ge|Q(D)|. We again extend this problem to support lexicographic orders and to support updates. First, if a total order ≺\prec over free(Q)(Q) is given with DD and QQ before the preprocessing, then the task of ranked direct access asks us to support access queries where the implementation-defined order on tuples must coincide with the lexicographic order ≺\prec on Q(D)Q(D), induced by ≺\prec. Second, the task of ranked direct access under updates asks us to support update commands (insert R(a1,…,aar⁡(R))R(a_1,\ldots,a_{\operatorname{ar}(R)}) or delete R(a1,…,aar⁡(R))R(a_1,\ldots,a_{\operatorname{ar}(R)})) after the preprocessing, and again we must modify the data structure DD so that it represents the updated database instance D′D' and can be used to access tuples in the new set of answers Q(D′)Q(D'). Combining both variants, the ranked direct access under updates problem is the version where we are given the database instance DD, the query QQ, and a total order ≺\prec over free(Q)(Q), and where we must be able to support queries on the lexicographic order induced by ≺\prec and also to support updates to DD in the sense above.

Complexity measures. For the query evaluation problems defined earlier, we measure complexity using combined complexity, i.e., as a function of the database size ∣D∣|D| and of the size ∣Q∣|Q| of the query QQ, i.e., the length of QQ when written as a word over the alphabet σ∪vars⁡(Q)∪{∃,∧,(,)}\sigma\cup\operatorname{vars}(Q) \cup\{\exists,\wedge,(,)\}.

For each query evaluation problem, we consider the preprocessing time tpt_p, the update time tut_u when supporting updates, and either the delay time tdt_d for enumeration problems or the access time tat_a for direct access problems. For ranked enumeration (with or without updates), the delay time tdt_d is the maximum time between any two consecutive outputs, before the first output, and between the last output and the special value end-of-enumeration. For ranked direct access (with or without updates), the access time tat_a is the time to return the kthk^{\text{th}} tuple or the special value out-of-bounds. Let fpf_p, fdf_d, faf_a and fuf_u be numeric functions. For ranked enumeration under updates (resp., ranked direct access under updates), an algorithm is said to be in time ⟨fp,fd,fu⟩\langle f_p,f_d,f_u\rangle (resp., ⟨fp,fa,fu⟩\langle f_p,f_a,f_u\rangle), if for every database instance DD and query QQ, we have the preprocessing time tp=O(fp(∣D∣,∣Q∣))t_p = O(f_p(|D|,|Q|)), the delay time td=O(fd(∣D∣,∣Q∣))t_d = O(f_d(|D|,|Q|)) (resp., the access time ta=O(fa(∣D∣,∣Q∣))t_a = O(f_a(|D|,|Q|))) and the update time tu=O(fu(∣D∣,∣Q∣))t_u = O(f_u(|D|,|Q|)).

Note that the task of (ranked) direct access (under updates) generalizes the task of (ranked) enumeration (under updates). Indeed, for every numeric function fp,f,fuf_p,f,f_u, if we have an algorithm in time ⟨fp,f,fu⟩\langle f_p,f,f_u\rangle for (ranked) direct access under updates, then we have an algorithm in time ⟨fp,f,fu⟩\langle f_p,f,f_u\rangle for (ranked) enumeration under updates, and likewise without updates. We can perform (ranked) enumeration (under updates) with (ranked) direct access (under updates) as follows. Once the data structure DD is built, in O(fp(∣D∣))O(f_p(|D|)), from a q-hierarchical SJFCQ QQ, a database instance DD and whenever it exists, an order ≺\prec over free(Q)(Q), we can return the kthk^{\text{th}} answer for k=1,2,3,…k=1,2,3,\ldots in several queries in the access phase, with O(f(∣D∣))O(f(|D|)) delay. Finally, whenever an update occurs, we modify DD in O(fu(∣D∣))O(f_u(|D|)), and restart queries in the access phase with k=1k=1.

Initial Observations

The complexity of enumeration and direct access of conjunctive queries on dynamic data was studied in [10], which introduced the class of q-hierarchical CQs.

We now recall the definition of this class and the lower bounds and upper bounds that it yields.

Definition 2.1. A CQ QQ is q-hierarchical if for any two variables x1,x2∈vars⁡(Q)x_1,x_2 \in\operatorname{vars}(Q) the following is satisfied:

(i) atoms⁡Q(x1)⊆atoms⁡Q(x2)\operatorname{atoms}_{Q}(x_1) \subseteq\operatorname{atoms}_{Q}(x_2) or atoms⁡Q(x1)⊇atoms⁡Q(x2)\operatorname{atoms}_{Q}(x_1) \supseteq\operatorname{atoms}_{Q}(x_2) or atoms⁡Q(x1)∩atoms⁡Q(x2)=∅\operatorname{atoms}_{Q}(x_1) \cap\operatorname{atoms}_{Q}(x_2) = \varnothing, and

(ii) if atoms⁡Q(x1)⊂atoms⁡Q(x2)\operatorname{atoms}_{Q}(x_1) \subset\operatorname{atoms}_{Q}(x_2) and x1∈free⁡(Q)x_1 \in\operatorname{free}(Q), then x2∈free⁡(Q)x_2 \in\operatorname{free}(Q).

It was shown in [10] that, for SJFCQs that are not q-hierarchical, some complexity lower bounds apply to enumeration under updates and direct access under updates, even in the non-ranked versions. These lower bounds are conditional to the OMvOMv-conjecture, which we introduce below.

OMv-conjecture. The Online Matrix-vector multiplication (OMvOMv) problem is defined as follows. We are given an n×nn \times n Boolean matrix MM, on which we allow arbitrary polynomial preprocessing. Then, a sequence of nn Boolean vectors v⃗1,…,v⃗n\vec{v}_1,\ldots,\vec{v}_n of dimension nn is given online, one vector at a time. For each i∈[n]i \in[n], we must output Mv⃗iM\vec{v}_i before receiving v⃗i+1\vec{v}_{i+1}. The complexity measure is the total running time required to output all vectors Mv⃗iM\vec{v}_i. The OMvOMv conjecture states that no algorithm solves the OMvOMv problem in time O(n3−ε)O(n^{3-\varepsilon}) for any ε>0\varepsilon> 0.

We now recall the lower bounds on enumeration under updates and direct access under updates from [10].

Theorem 2.2 ([10 Theorem 3.13]). Fix a number ε>0\varepsilon> 0 and a self-join-free conjunctive query QQ. If QQ is not q-hierarchical, there is no algorithm with polynomial preprocessing time and O(∣D∣1−ε)O(|D|^{1-\varepsilon}) update time that enumerates Q(D)Q(D) with O(∣D∣1−ε)O(|D|^{1-\varepsilon}) delay in data complexity, unless the OMvOMv-conjecture fails.

Theorem 2.3 ([10 Theorem 3.16]). Fix a number ε>0\varepsilon> 0 and a self-join-free conjunctive query QQ. If QQ is not q-hierarchical, there is no algorithm with polynomial preprocessing time and O(∣D∣1−ε)O(|D|^{1-\varepsilon}) update time that solves the direct access problem in time O(∣D∣1−ε)O(|D|^{1-\varepsilon}) delay in data complexity, unless the OMvOMv-conjecture fails.

In contrast, it was also shown in [10] that, for q-hierarchical SJFCQs, there are algorithms for efficient enumeration under updates and efficient direct access under updates.

Theorem 2.4 ([3 Theorem 3.2]). There is an algorithm that receives a q-hierarchical SJFCQ QQ and a database instance DD, and computes within O(∣Q∣O(1)∣D∣)O(|Q|^{O(1)}|D|) preprocessing time a data structure that can be updated in time O(∣Q∣O(1))O(|Q|^{O(1)}) and allows the following:

  1. enumerate Q(D)Q(D) with delay O(∣Q∣O(1))O(|Q|^{O(1)})

Theorem 2.5 ([10 Theorem 3.9]). There is an algorithm that receives a q-hierarchical SJFCQ QQ and a database instance DD and computes within preprocessing time O(∣Q∣O(1)∣D∣)log⁡∣D∣O(|Q|^{O(1)}|D|)\log|D| a data structure that can be updated in time O(∣Q∣O(1)log⁡∣D∣)O(|Q|^{O(1)}\log|D|) and allows the following:

  1. output the query result size ∣Q(D)∣|Q(D)| in time O(1)O(1) and,

  1. enumerate the tuples in Q(D)Q(D) in lexicographic order according to the implementation order with delay O(∣Q∣O(1))O(|Q|^{O(1)}) and,

  1. upon input of an arbitrary kk, takes time O(∣Q∣O(1)log⁡∣D∣)O(|Q|^{O(1)}\log|D|) to immediately output the kthk^{\mathrm{th}} answer that the enumeration procedure of (2) would have output

In the next section, we will review proof techniques used to show these results because we will extend them for our own results. In light of the lower bounds of Theorem 2.2 and Theorem 2.3, we will focus on q-hierarchical SJFCQs throughout the paper. Our goal is to investigate how this complexity picture is affected for ranked enumeration under updates and ranked direct access under updates as defined previously.

Data Structures: Q-Trees, View-Trees, and Grounded-Trees

In this section, we define the data structure built in the preprocessing phase from the q-hierarchical SJFCQ and the database instance given as input. This data structure is from [11], and we call it a grounded-tree. As we study combined complexity, we present an algorithm from [10] to build a so-called qq-tree for a given q-hierarchical SJFCQ. We then define from this the notions of view-trees and grounded-trees, and we show how to build grounded-trees from a q-hierarchical SJFCQ and a database instance.

From Q-trees to Grounded-Trees

In this part, we define the data structure built in the preprocessing by giving the definitions of q-trees, view-trees, and grounded-trees.

We recall the definition of q-trees from [10].

Definition 3.1. Let QQ be a qq-hierarchical CQCQ. A qq-tree for QQ is a directed tree TQ=(VQ,EQ)T_Q = (V_Q, E_Q) with VQ=vars⁡(Q)∪{vroot}V_Q = \operatorname{vars}(Q) \cup\{v_{\mathrm{root}}\} rooted in vrootv_{\mathrm{root}} where

  1. for all atoms ψ\psi in QQ, the set vars⁡(ψ)∪{vroot}\operatorname{vars}(\psi) \cup\{v_{\mathrm{root}}\} forms a directed path starting in TQT_Q at vrootv_{\mathrm{root}}, and

  2. free⁡(Q)∪{vroot}\operatorname{free}(Q) \cup\{v_{\mathrm{root}}\} is a connected subset in TQT_Q.

A CQCQ QQ is q-hierarchical if and only if it admits a q-tree [10 Lemma 4.2]. For each v∈VQv \in V_Q, we denote by anc⁡TQ(v)\operatorname{anc}_{T_Q}(v) the ancestors of vv in TQT_Q, including vrootv_{\mathrm{root}}. We can then define the following as in [11]:

Definition 3.2. Let QQ be a q-hierarchical SJFCQSJFCQ and TQ=(VQ,EQ)T_Q = (V_Q, E_Q) one of its q-trees. Let TQext=(VQext,EQext)T_Q^{\mathrm{ext}} = (V_Q^{\mathrm{ext}}, E_Q^{\mathrm{ext}}) be the extended q-tree of TQT_Q which is defined as follows: we have VQext=VQ∪{ψ∣ψ∈Q}V_Q^{\mathrm{ext}} = V_Q \cup\{\psi\mid\psi\in Q\} and EQext=EQ∪{(xi,ψi)∣xi∈vars⁡(ψi) with xi the farthest variable from vroot in TQ}E_Q^{\mathrm{ext}} = E_Q \cup\{(x_i,\psi_i) \mid x_i \in\operatorname{vars}(\psi_i)\text{ with }x_i\text{ the farthest variable from }v_{\mathrm{root}}\text{ in }T_Q\}. The view-tree V(TQ)V(T_Q) is the rooted tree with vertex set {Vvx∣v∈VQext}∪{V(u,v)Π∣(u,v)∈EQext}\{V_v^\mathrm{x} \mid v \in V_Q^{\mathrm{ext}}\} \cup\{V_{(u,v)}^\Pi\mid(u,v) \in E_Q^{\mathrm{ext}}\} and edge set {(Vux,V(u,v)Π),(V(u,v)Π,Vvx)∣(u,v)∈EQext,v a child of u}\{(V_u^\mathrm{x}, V_{(u,v)}^\Pi), (V_{(u,v)}^\Pi, V_v^\mathrm{x}) \mid(u,v) \in E_Q^{\mathrm{ext}}, v\text{ a child of }u\}, where each node VvxV_v^\mathrm{x} or V(u,v)ΠV_{(u,v)}^\Pi is associated to a view WvxW_v^\mathrm{x} or W(u,v)ΠW_{(u,v)}^\Pi which is a relational algebra expression defined as follows:

(i) for each leaf v∈VQextv \in V_Q^{\mathrm{ext}} labeled by an atom ψ:=R(x1,…,xar⁡(R))\psi:= R(x_1,\ldots,x_{\operatorname{ar}(R)}) then the view associated to VvxV_v^\mathrm{x} is Wvx:=ψW_v^\mathrm{x} := \psi,

(ii) for each internal vertex v∈VQextv \in V_Q^{\mathrm{ext}} with children v1,…,vkv_1,\ldots,v_k, then the view associated to VvxV_v^\mathrm{x} is Wvx:=W(v,v1)Π⋈⋯⋈W(v,vk)ΠW_v^\mathrm{x} := W_{(v,v_1)}^\Pi\bowtie\cdots\bowtie W_{(v,v_k)}^\Pi,

(iii) for each edge (u,v)∈EQext(u,v) \in E_Q^{\mathrm{ext}}, then the view associated to V(u,v)ΠV_{(u,v)}^\Pi is W(u,v)Π:=Π(anc⁡TQ(u)∪{u})∖{vroot}WvxW_{(u,v)}^\Pi:= \Pi_{(\operatorname{anc}_{T_Q}(u)\cup\{u\})\setminus\{v_{\mathrm{root}}\}}W_v^\mathrm{x}.

Given a database instance DD, we now enrich the view-tree V(TQ)V(T_Q) with the data of DD, which gives a grounded-tree VD(TQ)V_D(T_Q). Each vertex VvxV_v^\mathrm{x} (resp. V(u,v)ΠV_{(u,v)}^\Pi) in the view-tree V(TQ)V(T_Q) is associated to a table ZvxZ_v^\mathrm{x} (resp. Z(u,v)ΠZ_{(u,v)}^\Pi) such that ZvxZ_v^\mathrm{x} (resp. Z(u,v)ΠZ_{(u,v)}^\Pi) is the evaluation of WvxW_v^\mathrm{x} (resp. W(u,v)ΠW_{(u,v)}^\Pi). In particular, for every leaf VvxV_v^\mathrm{x} labeled by an atom R(x1,…,xar⁡(R))R(x_1,\ldots,x_{\operatorname{ar}(R)}), then Zvx:=RDZ_v^\mathrm{x} := R^D. The other views of VD(TQ)V_D(T_Q) are then computed bottom-up from the leaf views, following their definitions. Note that some projection views are defined on the same set of variables as their child. For instance, in Figure 1, the view W(x2,R)ΠW_{(x_2,R)}^\Pi is identical to WRxW_R^\mathrm{x}, therefore Z(x2,R)Π=ZRxZ_{(x_2,R)}^\Pi= Z_R^\mathrm{x}. We do not try to remove these redundant views, because doing so would complicate the definitions and would not improve the asymptotic complexities of our algorithms.

Representation of an extended q-tree, grounded-tree, and query result for $Q_1(D)$

Figure 1. Representation of an extended q-tree, grounded-tree, and query result for Q1(D)Q_1(D) (see Example 3.3)

In addition, whenever you have a vertex VV with associated view WW and a child ViV_i, then letting ZZ and ZiZ_i be the tables respectively associated to them, for each tuple t∈Zt \in Z, we denote by bucket⁡t(Zi)\operatorname{bucket}_t(Z_i) the set of tuples t′∈Zit' \in Z_i such that t′[free⁡(W)]=tt'[\operatorname{free}(W)] = t. Note that for a variable xix_i, the vertex VxixV_{x_i}^\mathrm{x} has several children V(xi,xj)ΠV_{(x_i,x_j)}^\Pi in V(TQ)V(T_Q). For each tuple t∈Zxixt \in Z_{x_i}^\mathrm{x} and each V(xi,xj)ΠV_{(x_i,x_j)}^\Pi of VxixV_{x_i}^\mathrm{x}, we have that bucket⁡t(Z(xi,xj)Π)\operatorname{bucket}_t(Z_{(x_i,x_j)}^\Pi) has exactly one tuple. A projection view V(xi,xj)ΠV_{(x_i,x_j)}^\Pi has one child view VxjxV_{x_j}^\mathrm{x}. For each tuple t∈Z(xi,xj)Πt \in Z_{(x_i,x_j)}^\Pi, there exists bucket⁡t(Vxjx)\operatorname{bucket}_t(V_{x_j}^\mathrm{x}) with several tuples t′t' which are extensions of tt over anc⁡TQ(xj)∪{xj}∖{vroot}\operatorname{anc}_{T_Q}(x_j) \cup\{x_j\} \setminus\{v_{\mathrm{root}}\}.

Example 3.3. Figure 1 represents an extended qq-tree and its grounded-tree for the query Q1(x1,x2)=R(x1,x2)∧S(x1,x2)∧T(x1)Q_1(x_1,x_2) = R(x_1,x_2) \land S(x_1,x_2) \land T(x_1), where by definition of a grounded-tree ZRD(x1,x2):=RZ_R^{\mathbb{D}}(x_1,x_2) := R, ZSD(x1,x2):=SZ_S^{\mathbb{D}}(x_1,x_2) := S and ZTD(x1):=TZ_T^{\mathbb{D}}(x_1) := T, and for the database instance DD given by the relations RR, SS and TT. Note that the qq-tree TQT_{Q} is the subtree of Figure 1 in black. In addition, for the tuple t=(b3)t = (b_3) shown in gray, the tuples t′∈bucket⁡(b3)(Vx2D)t' \in\operatorname{bucket}_{(b_3)}(V_{x_2}^{\mathbb{D}}) are in a lighter gray.

Data Structure Implementation

In this section, we first describe the implementation of the data structure defined previously, inspired by [10], using lookup tables and B+B^+-trees. We then present the algorithms that we use when performing the preprocessing on this data structure.

B+B^+-trees. Given a set of tuples SS and a ranking function ff that maps each tuple of SS to a value of an ordered domain, a B+B^+-tree for (S,f)(S,f) is a balanced search tree that stores SS in sorted order according to ff. More precisely, all leaves are at the same depth, and each leaf stores a bounded number of consecutive tuples in the order ff, whereas the internal nodes only store keys and pointers that guide the search for a specific tuple. Moreover, the leaves are chained in a linked list following ff. It is well known that B+B^+-trees admit algorithms allowing us to insert a tuple, delete a tuple, retrieve a tuple, and retrieve the minimum tuple for ff each in time O(log⁡∣S∣)O(\log|S|), since the height of the tree is logarithmic in the number of stored tuples. What is more, given a pointer to a tuple stored in the tree, accessing the tuple that follows it in the order induced by ff takes time O(1)O(1), by moving to the next tuple of the same leaf or, at the end of a leaf, by following the pointer to the next leaf.

Grounded-tree implementation. Let VV be a vertex of the view-tree V(TQ)V(T_Q), let WW be the view associated to VV, let ZZ be the table associated to VV, and let V′V' be the parent of VV. To navigate each table ZZ efficiently, we group all tuples t∈Zt \in Z by their restriction t′t' to free⁡(W′)\operatorname{free}(W'). We implement each table ZZ as a lookup table whose keys are all tuples t′∈Z′t' \in Z' and whose value for each t′t' is bucket⁡t′(Z)\operatorname{bucket}_{t'}(Z). For each bucket⁡t′(Z)\operatorname{bucket}_{t'}(Z), given a ranking function fbucket⁡t′(Z)f_{\operatorname{bucket}_{t'}(Z)} that maps each t∈bucket⁡t′(Z)t \in\operatorname{bucket}_{t'}(Z) to a value of an ordered domain, we store bucket⁡t′(Z)\operatorname{bucket}_{t'}(Z) in a B+B^+-tree sorted according to fbucket⁡t′(Z)f_{\operatorname{bucket}_{t'}(Z)}.

Therefore, given a table ZZ and a key t′∈Z′t' \in Z', accessing the B+B^+-tree bucket⁡t′(Z)\operatorname{bucket}_{t'}(Z) takes constant time since ZZ is a lookup table. Besides, given bucket⁡t′(Z)\operatorname{bucket}_{t'}(Z) and a ranking function fbucket⁡t′(Z)f_{\operatorname{bucket}_{t'}(Z)}, since each bucket is a B+B^+-tree for (bucket⁡t′(Z),fbucket⁡t′(Z))(\operatorname{bucket}_{t'}(Z), f_{\operatorname{bucket}_{t'}(Z)}), inserting or deleting a tuple tt in bucket⁡t′(Z)\operatorname{bucket}_{t'}(Z) while keeping it sorted according to fbucket⁡t′(Z)f_{\operatorname{bucket}_{t'}(Z)} takes time O(log⁡∣D∣)O(\log|D|). Further, given a pointer to a tuple in bucket⁡t′(Z)\operatorname{bucket}_{t'}(Z), retrieving its successor takes constant time.

Preprocessing

We next show how to build the grounded-tree from a SJFCQ QQ. To do so, we first build a q-tree TQT_Q, extend it to a view-tree V(TQ)V(T_Q), and, given a database instance DD, we enrich V(TQ)V(T_Q) into a grounded-tree with all the database facts from DD. We now recall the combined complexity of building a q-tree, given a q-hierarchical CQ.

Lemma 3.4 ([10 Lemma 4.2]). A CQ QQ is q-hierarchical if and only if QQ has a q-tree. Moreover, there is an O(∣Q∣O(1))O(|Q|^{O(1)}) algorithm that decides whether an input CQ QQ is q-hierarchical, and if so, outputs a q-tree.

Given a database instance DD, a q-hierarchical SJFCQ QQ, and a q-tree TQT_Q for QQ, we now show how to build the grounded-tree for TQT_Q in the preprocessing phase. We can show a preprocessing upper bound in O(∣D∣⋅∣Q∣)O(|D| \cdot|Q|) using the fact that we can sort in linear time in the RAM model [7].

Proposition 3.5. There exists an algorithm that, given a q-hierarchical SJFCQ QQ, one of its q-trees TQT_Q, and a database instance DD, builds the grounded-tree VD(TQ)V_D(T_Q), in which each bucket bb is sorted by a given ranking function fbf_b, in time O(∣D∣∣Q∣)O(|D||Q|).

Note that if one wants to avoid linear-time sorting algorithms, the preprocessing can instead be performed in time O(∣D∣⋅∣Q∣log⁡∣D∣)O(|D| \cdot|Q| \log|D|), using any linearithmic sorting algorithm.

We can now prove Proposition 3.5:

Proof of Proposition 3.5. For a given q-hierarchical SJFCQ QQ and one of its q-trees TQT_Q, we first compute the extended q-tree TQextT_Q^{\mathrm{ext}} in time O(∣Q∣)O(|Q|) by adding, for each atom ψ∈Q\psi\in Q, a leaf ψ\psi as a child of the farthest variable xix_i from vrootv_{\mathrm{root}} in TQT_Q such that xi∈vars⁡(ψ)x_i \in\operatorname{vars}(\psi). Afterwards, we compute the view-tree V(TQ)V(T_Q) by traversing TQextT_Q^{\mathrm{ext}} while adding Vv∞V_v^\infty for each vertex v∈TQextv \in T_Q^{\mathrm{ext}}, and V(u,v)ΠV_{(u,v)}^{\Pi} for each edge (u,v)∈TQext(u,v) \in T_Q^{\mathrm{ext}}, in time O(∣Q∣)O(|Q|). We then initialize all tables to the empty table by creating an empty table ZZ for each vertex VV of the view-tree, giving us the empty grounded tree VD(TQ)V_D(T_Q). Given a database instance DD, we then set ZR:=RDZ_R := R^D for every relation RR of DD. We then propagate the tuples bottom-up as follows. Let VV be a vertex, let ViV_i be a child of VV, let ZZ and ZiZ_i be the tables associated to VV and ViV_i, and let WW be the view associated to VV. For each table ZiZ_i, we evaluate the view WiW_i and group the tuples of ZiZ_i into buckets according to their restriction to free⁡(W)\operatorname{free}(W): each tuple t′∈Zit' \in Z_i is placed in the bucket bucket⁡t(Zi)\operatorname{bucket}_{t}(Z_i) with key t=t′[free⁡(W)]t = t'[\operatorname{free}(W)]. If WW is a projection view, then ZZ contains the keys tt of the buckets of its only child ZiZ_i. If WW is a join view, we fix one child ViV_i and scan the keys tt of the buckets of ZiZ_i; for each of them, we check in constant time per other child VjV_j that bucket⁡t(Zj)\operatorname{bucket}_{t}(Z_j) exists, and tt is added to ZZ only if all these buckets exist. Every table has at most ∣D∣|D| tuples: the leaves are relations of DD, a projection view has at most as many tuples as its child, and a join view has at most as many tuples as any of its children. Hence, a projection vertex is processed in time O(∣D∣)O(|D|), and a join vertex in time O(∣D∣)O(|D|) per child. Since the sum of the number of children over all vertices is the number of edges of the view-tree, which is O(∣Q∣)O(|Q|), all views are evaluated in time O(∣Q∣⋅∣D∣)O(|Q| \cdot|D|). Using the fact that we can sort in linear time in the RAM model [7], we then sort each bucket⁡t(Zi)\operatorname{bucket}_{t}(Z_i) according to its ranking function fbucket⁡t(Zi)f_{\operatorname{bucket}_{t}(Z_i)} and build, for each bucket, a B+B^+-tree from the sorted list in linear time. Each table ZZ contains at most ∣D∣|D| tuples in total, separated into several buckets, so this takes O(∣D∣)O(|D|) per table, and O(∣Q∣⋅∣D∣)O(|Q| \cdot|D|) overall. Hence, there exists an algorithm that builds a grounded tree VD(TQ)V_D(T_Q), in which each bucket bucket⁡t′(Z)\operatorname{bucket}_{t'}(Z) is sorted according to fbucket⁡t′(Z)f_{\operatorname{bucket}_{t'}(Z)}, in time O(∣Q∣⋅∣D∣)O(|Q| \cdot|D|). ∎

For the query evaluation problems we tackle throughout this paper, we can therefore build a grounded-tree for a q-hierarchical SJFCQ QQ in time O(∣Q∣O(1)+∣Q∣⋅∣D∣)O(|Q|^{O(1)} + |Q| \cdot|D|), i.e., linear preprocessing in data complexity, by building one of its q-trees with Lemma 3.4 and applying Proposition 3.5.

Hierarchy-Consistent Orders

We now define a notion of order on the free variables of a q-hierarchical CQ.

Definition 4.1. Let QQ be a q-hierarchical CQ. Then, QQ induces a partial order on free⁡(Q)\operatorname{free}(Q) where xi<Qxjx_i <_Q x_j if atoms⁡Q(xj)⊆atoms⁡Q(xi)\operatorname{atoms}_Q(x_j) \subseteq\operatorname{atoms}_Q(x_i). A total order ≺\prec on free⁡(Q)\operatorname{free}(Q) is hierarchy-consistent for QQ if it is a linear extension of this partial order.

Given a q-hierarchical CQ QQ, and a hierarchy-consistent order ≺\prec over free⁡(Q)\operatorname{free}(Q), we now want to perform ranked enumeration and ranked direct access. To do so, we need to build a data structure adapted to ≺\prec. We therefore want a q-tree, used to build the data structure, in which ≺\prec can be obtained easily, for instance by a topological sort. It follows from the proof of [10 Lemma 4.2] that several q-trees may represent a q-hierarchical CQ QQ. However, every hierarchy-consistent order ≺\prec cannot be obtained by a topological sort of any q-tree. Indeed, for each pair x1,x2∈vars⁡(Q)x_1,x_2 \in\operatorname{vars}(Q), whenever atoms⁡Q(x1)=atoms⁡Q(x2)\operatorname{atoms}_Q(x_1) = \operatorname{atoms}_Q(x_2) and x1∈free⁡(Q)⟺x2∈free⁡(Q)x_1 \in\operatorname{free}(Q) \Longleftrightarrow x_2 \in\operatorname{free}(Q), the ancestor-descendant relation between these variables may vary: in one q-tree x2x_2 is an ancestor of x1x_1, while in another x1x_1 is an ancestor of x2x_2, as shown in Figure 2. Thus, a hierarchy-consistent order where x1≺x2x_1 \prec x_2 cannot be obtained by a topological sort of the first q-tree. We make this clearer with an example:

Q-trees for Q

Figure 2. Q-trees for Q(x1,x2,x3,x4):=R(x1,x2,x4)∧S(x3,x4)Q(x_1,x_2,x_3,x_4) := R(x_1,x_2,x_4) \land S(x_3,x_4)

Example 4.2. Let Q(x1,x2,x3,x4):=R(x1,x2,x4)∧S(x3,x4)Q(x_1,x_2,x_3,x_4) := R(x_1,x_2,x_4) \land S(x_3,x_4) be a q-hierarchical CQ. The linear extension ≺\prec such that vroot≺x4≺x3≺x2≺x1v_{\mathrm{root}} \prec x_4 \prec x_3 \prec x_2 \prec x_1 is a topological sort of T1T_1 but cannot be obtained by a topological sort of T2T_2 in Figure 2.

Hence, in the preprocessing phase, we build a compatible q-tree for the order ≺\prec, defined as follows.

Definition 4.3. Let QQ be a q-hierarchical CQ, TQT_Q one of its q-trees, and ≺\prec a hierarchy-consistent order on free⁡(Q)\operatorname{free}(Q). We say TQT_Q is compatible with ≺\prec if ≺\prec is the restriction to free⁡(Q)\operatorname{free}(Q) of a topological order of TQT_Q.

We now show that such a q-tree exists, strengthening [10 Lemma 4.2] (which we had restated previously as Lemma 3.4).

Claim 4.4. Given a q-hierarchical CQ QQ and a hierarchy-consistent order ≺\prec on its free variables, we can build a q-tree TQT_Q compatible with ≺\prec in O(∣Q∣O(1))O(|Q|^{O(1)}).

Proof. To build such a q-tree compatible with a hierarchy-consistent order ≺\prec, we adapt the algorithm from [10 Lemma 4.2] using ≺\prec to break ties when, for x1,x2∈free⁡(Q)x_1,x_2 \in\operatorname{free}(Q), we have atoms⁡Q(x1)=atoms⁡Q(x2)\operatorname{atoms}_Q(x_1) = \operatorname{atoms}_Q(x_2). This algorithm builds a q-tree TQT_Q of QQ such that each path of TQT_Q is a path of the form vroot,x1,…,xsv_{\mathrm{root},x_1,\ldots,x_s} where atoms⁡Q(xs)⊆⋯⊆atoms⁡Q(x1)\operatorname{atoms}_Q(x_s) \subseteq\cdots\subseteq\operatorname{atoms}_Q(x_1). In this algorithm, when for two variables x1,x2∈free⁡(Q)x_1,x_2 \in\operatorname{free}(Q), we have atoms⁡Q(x1)=atoms⁡Q(x2)\operatorname{atoms}_Q(x_1) = \operatorname{atoms}_Q(x_2), we choose arbitrarily which variable will be the ancestor of the other. To get a q-tree in which ≺\prec is a topological sort, we break this tie following the order ≺\prec, i.e., when for two variables x1,x2∈free⁡(Q)x_1,x_2 \in\operatorname{free}(Q), we have atoms⁡Q(x1)=atoms⁡Q(x2)\operatorname{atoms}_Q(x_1) = \operatorname{atoms}_Q(x_2), if x1≺x2x_1 \prec x_2 then x1x_1 is an ancestor of x2x_2, otherwise x2x_2 is an ancestor of x1x_1. ∎

Before showing how to achieve ranked enumeration (resp. direct access) under updates for a hierarchy-consistent given order, we first show that, when the database changes due to the insertion or deletion of a tuple, we can update the grounded-tree in time O(log⁡∣D∣)O(\log|D|). Recall that in a grounded-tree, each bucket bb is sorted by a ranking function fbf_b. When the order ≺\prec used to build the grounded-tree is hierarchy-consistent, we define these ranking functions as the lexicographic ranking functions induced by ≺\prec.

Let VV be a non-root vertex of the grounded tree VD(TQ)V_D(T_Q), let WW be the view associated to VV, let ZZ be the table associated to VV, and let V′V' be the parent of VV with associated view W′W'. Let QQ be a q-hierarchical CQ, let ≺\prec be the hierarchy-consistent order over free⁡(Q)\operatorname{free}(Q), let TQT_Q be a q-tree for QQ compatible with ≺\prec, and let DD be a database instance. We extend ≺\prec arbitrarily to a topological order of TQT_Q over all of vars⁡(Q)\operatorname{vars}(Q). For each bucket bucket⁡t′(Z)\operatorname{bucket}_{t'}(Z) of the grounded tree VD(TQ)V_D(T_Q), the lexicographic ranking function fbucket⁡t′(Z)f_{\operatorname{bucket}_{t'}(Z)}, induced by ≺\prec over vars⁡(Q)\operatorname{vars}(Q), is simply the identity: fbucket⁡t′(Z)(t):=tf_{\operatorname{bucket}_{t'}(Z)}(t) := t for every t∈bucket⁡t′(Z)t \in\operatorname{bucket}_{t'}(Z), where the domain is the set of tuples over free⁡(W)\operatorname{free}(W) ordered lexicographically according to ≺\prec. In other words, for all t1,t2∈bucket⁡t′(Z)t_1,t_2 \in\operatorname{bucket}_{t'}(Z), we have fbucket⁡t′(Z)(t1)≺fbucket⁡t′(Z)(t2)f_{\operatorname{bucket}_{t'}(Z)}(t_1) \prec f_{\operatorname{bucket}_{t'}(Z)}(t_2) if and only if t1≺t2t_1 \prec t_2, where ≺\prec is the lexicographic order induced by ≺\prec.

Lemma 4.5. Let QQ be a q-hierarchical SJFCQ, ≺\prec a hierarchy-consistent order over free⁡(Q)\operatorname{free}(Q), TQT_Q a q-tree for QQ compatible with ≺\prec, and DD a database instance. Let VD(TQ)V_D(T_Q) be the grounded-tree for TQT_Q and DD whose buckets bb are sorted using the lexicographic ranking functions fbf_b defined by ≺\prec. Given an update command update⁡ R(a1,…,aar⁡(R))\operatorname{update}\ R(a_1,\ldots,a_{\operatorname{ar}(R)}), let D′D' be the database instance obtained by applying update⁡ R(a1,…,aar⁡(R))\operatorname{update}\ R(a_1,\ldots,a_{\operatorname{ar}(R)}) to DD. There exists an algorithm that, given VD(TQ)V_D(T_Q) and given an update update⁡ R(a1,…,aar⁡(R))\operatorname{update}\ R(a_1,\ldots,a_{\operatorname{ar}(R)}), computes in time O(∣Q∣log⁡∣D∣)O(|Q|\log|D|) the grounded tree VD′(TQ)V_{D'}(T_Q), whose buckets bb are sorted using the lexicographic ranking functions fbf_b.

Proof. Let VD(TQ)V_D(T_Q) be a grounded-tree and for each bucket of VD(TQ)V_D(T_Q), let its tuples be sorted lexicographically. Let update⁡ R(a1,…,aar⁡(R))\operatorname{update}\ R(a_1,\ldots,a_{\operatorname{ar}(R)}) be an update of a tuple t:=R(a1,…,aar⁡(R))t := R(a_1,\ldots,a_{\operatorname{ar}(R)}), and let D′D' be the database instance obtained by applying update⁡ R(a1,…,aar⁡(R))\operatorname{update}\ R(a_1,\ldots,a_{\operatorname{ar}(R)}) to DD. We build VD′(TQ)V_{D'}(T_Q) by inserting or removing database facts from VD(TQ)V_D(T_Q) as follows. We distinguish two cases depending on the kind of update.

When the update is an insertion, for the table ZRΠ(X∪Y)Z_R^\Pi(X\cup Y) with XX the set of free variables of the view W(xi,R)ΠW_{(x_i,R)}^\Pi, we insert tt in the bucket bucket⁡t[X](ZRΠ)\operatorname{bucket}_{t[X]}(Z_R^\Pi). Accessing bucket⁡t[X](ZRΠ)\operatorname{bucket}_{t[X]}(Z_R^\Pi) takes constant time, and inserting tt in bucket⁡t[X](ZRΠ)\operatorname{bucket}_{t[X]}(Z_R^\Pi) while keeping bucket⁡t[X](ZRΠ)\operatorname{bucket}_{t[X]}(Z_R^\Pi) sorted lexicographically takes logarithmic time. Then, we propagate the insertion upwards in VD(TQ)V_D(T_Q) on the path from VRΠV_R^\Pi to VrootΠV_{\mathrm{root}}^\Pi as follows. When we insert a tuple tt in a bucket bucket⁡t′(ZxjΠ)\operatorname{bucket}_{t'(Z_{x_j}^\Pi)}, with t′=t[W(xi,xj)Π]t' = t[W_{(x_i,x_j)}^\Pi], we insert t′t' in bucket⁡t′′(Z(xi,xj)Π)\operatorname{bucket}_{t''(Z_{(x_i,x_j)}^\Pi)}, where t′′=t′[free⁡(W(xi,xj)Π)]t'' = t'[\operatorname{free}(W_{(x_i,x_j)}^\Pi)]. If t′t' already exists in bucket⁡t′′(Z(xi,xj)Π)\operatorname{bucket}_{t''(Z_{(x_i,x_j)}^\Pi)}, the propagation stops. Otherwise, when we insert a tuple tt in bucket⁡t′(Z(xi,xj)Π)\operatorname{bucket}_{t'(Z_{(x_i,x_j)}^\Pi)} with t′=t[WxiΠ]t' = t[W_{x_i}^\Pi], we insert t′t' in bucket⁡t′′(ZxiΠ)\operatorname{bucket}_{t''(Z_{x_i}^\Pi)} where t′′=t′[WxiΠ]t'' = t'[W_{x_i}^\Pi], if and only if tt exists in all Z(xi,xk)ΠZ_{(x_i,x_k)}^\Pi for each child xkx_k of xix_i in the extended q-tree. It is equivalent to checking if bucket⁡t(Z(xi,xk)Π)\operatorname{bucket}_{t(Z_{(x_i,x_k)}^\Pi)} exists, which takes constant time. In the worst case, we propagate an update until VrootΠV_{\mathrm{root}}^\Pi, which takes O(∣Q∣log⁡∣D∣)O(|Q|\log|D|).

When the update is a deletion, for the table ZRΠ(X∪Y)Z_R^\Pi(X\cup Y) with XX the set of free variables of the view W(xi,R)ΠW_{(x_i,R)}^\Pi, we remove tt from the bucket bucket⁡t[X](ZRΠ)\operatorname{bucket}_{t[X]}(Z_R^\Pi). Accessing bucket⁡t[X](ZRΠ)\operatorname{bucket}_{t[X]}(Z_R^\Pi) takes constant time, and removing tt from bucket⁡t[X](ZRΠ)\operatorname{bucket}_{t[X]}(Z_R^\Pi) while keeping bucket⁡t[X](ZRΠ)\operatorname{bucket}_{t[X]}(Z_R^\Pi) sorted lexicographically takes logarithmic time. Let t:=(a1,…,aar⁡(R))t := (a_1,\ldots,a_{\operatorname{ar}(R)}) be the deleted tuple. Then, we propagate the deletion upwards in VD(TQ)V_D(T_Q) on the path from VRΠV_R^\Pi to VrootΠV_{\mathrm{root}}^\Pi as follows. When we delete a tuple tt from a bucket bucket⁡t′(ZxjΠ)\operatorname{bucket}_{t'(Z_{x_j}^\Pi)}, with t′=t[free⁡(W(xi,xj)Π)]t' = t[\operatorname{free}(W_{(x_i,x_j)}^\Pi)], if bucket⁡t′(ZxiΠ)\operatorname{bucket}_{t'(Z_{x_i}^\Pi)} becomes empty, we remove t′t' from bucket⁡t′′(Z(xi,xj)Π)\operatorname{bucket}_{t''(Z_{(x_i,x_j)}^\Pi)}, which takes time O(log⁡∣D∣)O(\log|D|). If there still exists a tuple in bucket⁡t′(VxiΠ)\operatorname{bucket}_{t'(V_{x_i}^\Pi)} the propagation stops. When we delete a tuple tt in bucket⁡t′(Z(xi,xj)Π)\operatorname{bucket}_{t'(Z_{(x_i,x_j)}^\Pi)}, with t′=t[free⁡(ZxiΠ)]t' = t[\operatorname{free}(Z_{x_i}^\Pi)], we remove t′t' from bucket⁡t′′(ZxiΠ)\operatorname{bucket}_{t''(Z_{x_i}^\Pi)}, where t′′=t′[anc⁡TQ(xi)]t'' = t'[\operatorname{anc}_{T_Q}(x_i)] if it exists; otherwise, the propagation stops. In the worst case, we propagate a deletion until VrootΠV_{\mathrm{root}}^\Pi, which takes O(∣Q∣log⁡∣D∣)O(|Q|\log|D|). Hence, an update takes O(∣Q∣log⁡∣D∣)O(|Q|\log|D|).

We can now show how to achieve ranked enumeration under updates when the order in the input is hierarchy-consistent.

Ranked Enumeration Under Updates

In this part, given a q-hierarchical SJFCQ QQ and a database instance DD, we propose an enumeration algorithm in ⟨∣D∣∣Q∣,∣Q∣,∣Q∣log⁡∣D∣⟩\langle|D||Q|, |Q|, |Q|\log|D| \rangle delay in combined complexity, i.e., linear preprocessing, logarithmic update, and constant delay times in data complexity, when the order on the free variables of QQ is hierarchy-consistent.

Theorem 4.6. There exists an algorithm that, given a q-hierarchical SJFCQ QQ, a hierarchy-consistent total order ≺\prec on its free variables, a q-tree TQT_Q for QQ compatible with ≺\prec and a database instance DD, enumerates Q(D)Q(D) in the lexicographic order defined by ≺\prec with O(∣D∣∣Q∣)O(|D||Q|) preprocessing, O(∣Q∣log⁡∣D∣)O(|Q|\log|D|) update time, and O(∣Q∣)O(|Q|) delay.

Proof. Given a q-hierarchical SJFCQ QQ, a database instance DD, and a hierarchy-consistent order ≺\prec over free⁡(Q)\operatorname{free}(Q), we apply the preprocessing procedure to build the grounded-tree VD(TQ)V_D(T_Q) using Proposition 3.5. Then, when updates arrive, we handle them using Lemma 4.5. Hence, all that remains is to explain how to perform ranked enumeration, which we now present.

Let x1,…,xlx_1,\ldots,x_l be the free variables of QQ listed according to ≺\prec. Algorithm 1 is written as nested loops over x1,…,xlx_1,\ldots,x_l for readability. In this algorithm, we traverse the grounded-tree VD(TQ)V_D(T_Q) from top to bottom to build each answer tQ(D)t_{Q(D)} of the enumeration. For a non-root variable xjx_j, the bucket bucket⁡t′(Zxj□)\operatorname{bucket}_{t'}(Z^{\square}_{x_j}) to traverse is determined by a tuple t′t' chosen in table Z(xi,xj)ΠZ^{\Pi}_{(x_i,x_j)}. Recall that there is only one tuple t′t' per bucket bucket⁡t(Z(xi,xj)Π)\operatorname{bucket}_t(Z^{\Pi}_{(x_i,x_j)}) such that t∈Zxi□t \in Z^{\square}_{x_i} and t=t′t=t', therefore bucket⁡t′(Zxj□)=bucket⁡t(Zxj□)\operatorname{bucket}_{t'}(Z^{\square}_{x_j})=\operatorname{bucket}_t(Z^{\square}_{x_j}). In the following, we traverse the grounded-tree by only visiting the join vertices Vxi⋈V^{\bowtie}_{x_i}. Moreover, since ≺\prec is hierarchy-consistent, tt is chosen before the tuple in bucket⁡t(Zxj□)\operatorname{bucket}_t(Z^{\square}_{x_j}), so bucket⁡t(Zxj□)\operatorname{bucket}_t(Z^{\square}_{x_j}) is always known when we need it.

To enumerate the first answer of Q(D)Q(D), we start from the root table Zvroot⋈Z^{\bowtie}_{v_{\mathrm{root}}}. Let t0:=()t_0:=() be the tuple of Zvroot⋈Z^{\bowtie}_{v_{\mathrm{root}}}, if it exists. Otherwise, we return end-of-enumeration. Then, for every child cc of vrootv_{\mathrm{root}} in TQT_Q, we save the bucket bucket⁡t0(Zc□)\operatorname{bucket}_{t_0}(Z^{\square}_c) in a variable bucket⁡[c]\operatorname{bucket}[c]. Afterward, for j=1,…,lj=1,\ldots,l in this order, we take the first tuple tjt_j of the bucket saved in variable bucket⁡[xj]\operatorname{bucket}[x_j], determined by the tuple chosen for its parent. At last, we output the answer built from the first tuple tlt_l of the corresponding bucket, and pause the algorithm with the command yield.

To enumerate the next answer of Q(D)Q(D), we look for the rightmost variable xix_i whose current tuple is not the last of its bucket. If there is none, the enumeration ends. Otherwise, we replace the tuple of xix_i with the following tuple of its bucket, and for each xjx_j with j>ij>i, in the order ≺\prec, we take the first tuple of the bucket determined by the tuple chosen for its parent. Only the variables after xi−1x_{i-1} change between two consecutive answers.

Getting bucket⁡t(Zxj□)\operatorname{bucket}_t(Z^{\square}_{x_j}) for a tuple tt and a variable xjx_j takes constant time, since each vertex Vxj⋈V^{\bowtie}_{x_j} is a look-up table where keys are all assignments of the free variables of WxiΠW^{\Pi}_{x_i} where xix_i is the parent of xjx_j in the extended q-tree TQxT_Q^x. Therefore, the delay to output a new tuple is at most the traversal of every join vertex of the grounded-tree until the last free variable of the order ≺\prec—i.e., the delay of Algorithm 1 is O(∣Q∣)O(|Q|). Combining the delay bound with the bounds from Proposition 3.5 and Lemma 4.5, there exists an algorithm that enumerates Q(D)Q(D) with O(∣D∣⋅∣Q∣)O(|D|\cdot|Q|) preprocessing, O(∣Q∣log⁡∣D∣)O(|Q|\log|D|) update time and O(∣Q∣)O(|Q|) delay.

Ranked Direct Access Under Updates

In this part, we first explain how to enrich the grounded-tree representing a database instance for a given hierarchy-consistent order on the free variables of a q-hierarchical SJFCQ QQ. Then, we adapt an algorithm from [6] into an algorithm in ⟨∣D∣∣Q∣,∣Q∣log⁡∣D∣,∣Q∣log⁡∣D∣⟩\langle|D||Q|,|Q|\log|D|,|Q|\log|D| \rangle that, given QQ, a hierarchy-consistent order ≺\prec, a q-tree for TQT_Q compatible with ≺\prec, and a database instance, answers ranked direct access under updates in O(∣Q∣log⁡(∣D∣))O(|Q|\log(|D|)) access time.

Enriched grounded-tree for ranked direct access under updates Let VD(TQ)V_D(T_Q) be a grounded-tree as described in § 3 for a given q-hierarchical SJFCQ QQ, a given database instance DD, and a hierarchy-consistent order ≺\prec. Recall that each bucket bb is sorted according to a lexicographic ranking function fbf_b. This ranking function is the identity: fb(t):=tf_b(t):=t for every t∈bt\in b, meaning bb is sorted lexicographically. We call VDfree(TQ)V_D^{\mathrm{free}}(T_Q) the subtree of VD(TQ)V_D(T_Q) containing all vertices ${V^{\bowtie}_{x_i}\cup V^{\Pi}_{(x_i,x_j)}\mid

Algorithm 1: Ranked enumeration according to a hierarchy-consistent order $\prec$

Input: $V_D(T_Q)$: a grounded-tree for $Q(D)$
Output: answers of $Q(D)$ in the lexicographic order induced by $\prec$
begin
    Let $x_1 < \cdots < x_l$ be the variables of $\operatorname{free}(Q)$ in the desired hierarchy-consistent order $\prec$
    for $t_0 \in Z^{\mathord{\vee}_{v_{\mathrm{root}}}}$ do
        $t_{Q(D)} \leftarrow ()$
        for $c$ child of $v_{\mathrm{root}}$ in $\operatorname{free}(Q)$ do
            bucket$[Z_c^{\mathord{\vee}}] \leftarrow$ bucket$_{t_0}(Z_c^{\mathord{\vee}})$
            for $t_1 \in$ bucket$[Z_{x_1}^{\mathord{\vee}}]$ do
                $t_{Q(D)}[x_1] \leftarrow t_1[x_1]$
                for $c$ child of $x_1$ in $\operatorname{free}(Q)$ do
                    bucket$[Z_c^{\mathord{\vee}}] \leftarrow$ bucket$_{t_1}(Z_c^{\mathord{\vee}})$
                    $\vdots$
                for $t_l \in$ bucket$[Z_{x_l}^{\mathord{\vee}}]$ do
                    $t_{Q(D)}[x_l] \leftarrow t_l[x_l]$
                    yield $t_{Q(D)}$
    yield end-of-enumeration

Let xi,xj∈free⁡(Q)∪{vroot}x_i,x_j \in\operatorname{free}(Q) \cup\{v_{\mathrm{root}}\}. As QQ is q-hierarchical, VDfree(TQ)V_D^{\mathrm{free}}(T_Q) is a connected subtree. Let VV be a vertex, let ViV_i be the parent of VV, let ZZ and ZiZ_i be the tables associated to VV and ViV_i, and let WW be the view associated to VV. For each tuple tt in a bucketti(Z)_{t_i}(Z) with ti∈Zit_i \in Z_i, we add weights wbucket⁡ti(Z)(t)w_{\operatorname{bucket}_{t_i}(Z)}(t). Note that these weights are different from the ranking function fbf_b, as they are not used to sort the tuples in a bucket. The weight wbucket⁡ti(Z)(t)w_{\operatorname{bucket}_{t_i}(Z)}(t) represents the number of different answers in Q(D)Q(D) that extend tt over the variables in the views of the subtree rooted in VV in VDfree(TQ)V_D^{\mathrm{free}}(T_Q). We compute these weights bottom-up while building the grounded-tree VD(TQ)V_D(T_Q), and when an update occurs. For leaves in the subtree VDfree(TQ)V_D^{\mathrm{free}}(T_Q), we initiate all weights to 11. When we propagate the tuple upward in VD(TQ)V_D(T_Q), as explained in Proposition 3.5 and Lemma 4.5, we compute the weight for each tuple propagated as follows. Note that in this case, the propagation of an update never stops before achieving VvrootV_{v_{\mathrm{root}}}, since we need to update weights for the parent of each vertex. This still takes O(∣Q∣log⁡∣D∣)O(|Q|\log|D|).

Let xi,xj∈free⁡(Q)∪{vroot}x_i,x_j \in\operatorname{free}(Q) \cup\{v_{\mathrm{root}}\} such that xjx_j is a child of xix_i in TQT_Q, and tit_i the tuple restricted over the variable of the view WiW_i associated to the parent ViV_i of vertex VV.

wbucket⁡ti(Z)(t):={1if Z is associated to a leaf in VDfree(TQ),∑t′wbucket⁡t′(Z(xi,xj)Π)(t′)if Z=Z(xi,xj)Π,∏xjwbucket⁡t(Z(xi,xj)Π)(t′)if Z=ZxiΠ.w_{\operatorname{bucket}_{t_i}(Z)}(t) := \begin{cases} 1 & \text{if } Z \text{ is associated to a leaf in } V_D^{\mathrm{free}}(T_Q),\\ \sum_{t'} w_{\operatorname{bucket}_{t'}(Z_{(x_i,x_j)}^{\Pi})}(t') & \text{if } Z = Z_{(x_i,x_j)}^{\Pi},\\ \prod_{x_j} w_{\operatorname{bucket}_{t}(Z_{(x_i,x_j)}^{\Pi})}(t') & \text{if } Z = Z_{x_i}^{\Pi}. \end{cases}

Therefore, if t∈Z(xi,xj)Πt \in Z_{(x_i,x_j)}^{\Pi}, its weight wb(t)w_b(t) is the sum of all the weights of the tuples t′t' that are extensions of tt in the table ZxjΠZ_{x_j}^{\Pi}. Otherwise, recall that buckett(Z(xi,xj)Π)_t(Z_{(x_i,x_j)}^{\Pi}) contains exactly one tuple. Then, if t∈ZxiΠt \in Z_{x_i}^{\Pi}, its weight wb(t)w_b(t) is the product, over all children xjx_j of xix_i, of the weight of the unique tuple txj∈t_{x_j} \in buckett(Z(xi,xj)Π)_t(Z_{(x_i,x_j)}^{\Pi}).

In addition to wb(t)w_b(t), we compute a prefix sum start⁡b(t)\operatorname{start}_b(t) for each bucket bb and each tuple t∈bt \in b. This sum is the cumulative sum of the weights of the tuples that strictly precede tt, i.e., start⁡b(t):=∑t′∈b, t′<tw(t′)\operatorname{start}_b(t) := \sum_{t' \in b,\,t' < t} w(t').

Example 4.7. We now consider the query Q2(x1,x3):=R(x1,x2)∧T(x3)Q_2(x_1,x_3) := R(x_1,x_2) \land T(x_3), where by definition of a view-tree we have ZRΠ=RDZ_R^{\Pi} = R^D and ZTΠ=TDZ_T^{\Pi} = T^D.

We enrich the view-tree V(TQ2)V(T_{Q_2}) with the weights w(t)w(t) and the prefix sum start⁡(t)\operatorname{start}(t). The result is represented in Figure 3.

Representation of q-tree, view-tree and query result for $Q_2(D)$

Figure 3. Representation of q-tree, view-tree and query result for Q2(D)Q_2(D) (see Example 4.7)

Note that, for t∈Zvroot∨t \in Z_{v_{\mathrm{root}}}^{\mathord{\vee}}, the value w(t)w(t) is the total number of tuples in Q2(D)Q_2(D).

To understand how weights and prefix sums are maintained after updates, we describe the implementation of a bucket bb. We use the B+B^+-tree described in § 3, where the tuples of bb are sorted lexicographically by their values. Each leaf stores a bounded number of consecutive tuples tt of bb, together with their weights w(t)w(t). Each internal node nn stores the sum of the values stored in its children, that is, the total weight of the tuples stored in the leaves below nn. Inserting or removing a tuple in bb, or changing its weight, takes time O(log⁡∣D∣)O(\log|D|), including the update of the sums stored in the internal nodes along the path from the leaf to the root. Propagating an update of the database to the O(∣Q∣)O(|Q|) views of the grounded-tree thus takes time O(∣Q∣log⁡∣D∣)O(|Q| \log|D|).

The prefix sums are not stored: the value start⁡b(t)\operatorname{start}_b(t) of a tuple tt is computed when needed. To compute it, we traverse the B+B^+-tree from the root to the leaf of tt. At each internal node, we add the values stored in the children located to the left of the child we visit, and, in the leaf, we add the weights of the tuples preceding tt. Since the height is O(log⁡∣D∣)O(\log|D|) and each node has a bounded number of children, computing the prefix sum of a tuple takes time O(log⁡∣D∣)O(\log|D|). Then, in a bucket bb, given a value kk, we can find in time O(log⁡∣D∣)O(\log|D|) the last tuple tt sorted according to fbf_b such that start⁡b(t)<k\operatorname{start}_b(t) < k.

Theorem 4.8. There exists an algorithm that, given a q-hierarchical SJFCQ QQ, a hierarchy-consistent total order ≺\prec on its free variables, a q-tree TQT_Q for QQ compatible with ≺\prec and a database instance DD, answers ranked direct access for Q(D)Q(D) in the lexicographic order defined by ≺\prec with O(∣D∣⋅∣Q∣)O(|D| \cdot|Q|) preprocessing, O(∣Q∣log⁡∣D∣)O(|Q| \log|D|) update time and O(∣Q∣log⁡∣D∣)O(|Q| \log|D|) access time.

Proof. This access algorithm is Algorithm 1: Lexicographic Direct-Access from [6], whose data complexity is O(log⁡(∣D∣))O(\log(|D|)). This algorithm consists of getting the right bucket for each view of the view-tree, which is done in constant time as views are implemented as look-up tables, and then picking the correct tuple in those buckets, which takes O(log⁡(∣D∣))O(\log(|D|)), as proven in [6], Lemma 3.8. In combined complexity, as we look for this tuple for each variable of the order ≺\prec, and we traverse each view of the view-tree, the complexity of the access algorithm is O(∣Q∣log⁡(∣D∣))O(|Q| \log(|D|)). Hence, combining this complexity with Proposition 3.5 and Lemma 4.5, there exists an algorithm that answers ranked direct access for Q(D)Q(D) and ≺\prec with O(∣D∣⋅∣Q∣)O(|D| \cdot|Q|) preprocessing, O(∣Q∣log⁡∣D∣)O(|Q| \log|D|) update time and O(∣Q∣log⁡∣D∣)O(|Q| \log|D|) access time. ∎

Non-Hierarchy-Consistent Orders

In this section, given a q-hierarchical SJFCQ QQ, an order ≺\prec over free⁡(Q)\operatorname{free}(Q) and a database instance DD, our goal is to develop an algorithm with O(∣D∣⋅∣Q∣)O(|D| \cdot|Q|) preprocessing and O(∣Q∣log⁡∣D∣)O(|Q| \log|D|) update time for ranked direct access under updates (resp. ranked enumeration under updates) according to ≺\prec with O(∣Q∣log⁡∣D∣)O(|Q| \log|D|) access time (resp. delay), i.e., linear preprocessing, logarithmic update, and logarithmic access (resp. delay) times in data complexity.

We will show that for ranked direct access under updates, such an algorithm conditionally does not exist.

Ranked Direct Access Under Updates

Our contribution on ranked direct access under updates relies on Theorem 2.2. The following lower bound states that efficient ranked direct access under updates cannot be achieved for a q-hierarchical SJFCQ when the given order on its free variables is allowed to be non-hierarchy-consistent. Our lower bound holds in data complexity and applies to any q-hierarchical SJFCQ and non-hierarchy-consistent order on its free variables.

Theorem 5.1. Fix a number ε>0\varepsilon> 0, ε′>0\varepsilon' > 0, a q-hierarchical SJFCQ QQ and an order ≺\prec over free⁡(Q)\operatorname{free}(Q). If ≺\prec is not hierarchy-consistent for QQ, then there is no algorithm with polynomial preprocessing time and O(∣D∣1−ε)O(|D|^{1-\varepsilon}) update time that, given a database instance DD, answers ranked direct access for Q(D)Q(D) and ≺\prec with O(∣D∣1−ε′)O(|D|^{1-\varepsilon'}) access time, assuming the OMv-conjecture in data complexity.

To prove this lower bound for ranked direct access, we first show that an efficient ranked direct access algorithm for any non-hierarchy-consistent order on any q-hierarchical SJFCQ QQ would imply efficient enumeration under updates, for some non-q-hierarchical SJFCQ Q′Q', which according to Theorem 2.2 is not possible (even without an order) when assuming the OMv-conjecture.

Lemma 5.2. Suppose there is a q-hierarchical SJFCQ QQ and a non-hierarchy-consistent order ≺\prec over free⁡(Q)\operatorname{free}(Q), such that there exists an algorithm in ⟨fp,fa,fu⟩\langle f_p, f_a, f_u\rangle where fpf_p is polynomial, faf_a and fuf_u are O(∣D∣1−ε)O(|D|^{1-\varepsilon}) for some ε\varepsilon, and that, given a database instance DD, answers ranked direct access for Q(D)Q(D) and ≺\prec. Then there exists a non-q-hierarchical SJFCQ Q′Q', such that there is an algorithm in ⟨fp,fd,fu⟩\langle f_p, f_d, f_u\rangle that, given a database instance DD, enumerates Q′(D)Q'(D) with fd=O(falog⁡∣D∣)f_d = O(f_a \log|D|).

To show this lower bound, we reduce the enumeration of a projection Q′Q' of QQ to ranked direct access under updates on QQ, where QQ is a q-hierarchical SJFCQ with a non-hierarchy-consistent order over its variables. We choose the free variables of Q′Q' so that Q′Q' is not q-hierarchical. Given a database instance DD, we enumerate the tuples of Q′(D)Q'(D) as follows: we use ranked direct access on Q(D)Q(D) to retrieve the kthk^{\mathrm{th}} answer, and then find by binary search the smallest k′>kk' > k such that the k′k' answer has a different projection on the free variables of Q′Q'. Each such answer is a new tuple of Q′(D)Q'(D), which we output. Let us give the formal proof of Lemma 5.2:

Proof of Lemma 5.2. Let DD be a database instance, QQ be a q-hierarchical SJFCQ, and ≺\prec be a non-hierarchy-consistent order over free⁡(Q)\operatorname{free}(Q). Let (x1,…,xl)(x_1,\ldots,x_l) be the free variables of QQ listed in the order of ≺\prec. Then there exist two variables xi,xj∈free⁡(Q)x_i,x_j \in\operatorname{free}(Q) such that atoms⁡Q(xi)⊆atoms⁡Q(xj)\operatorname{atoms}_Q(x_i) \subseteq\operatorname{atoms}_Q(x_j) and xi≺xjx_i \prec x_j. Let Q′:=ΠXi(Q)Q' := \Pi_{X_i}(Q) be the projection of QQ over Xi:={x1,…,xi}X_i := \{x_1,\ldots,x_i\}. Thus, free⁡(Q′)=free⁡(Q)∖{xi+1,…,xl}\operatorname{free}(Q') = \operatorname{free}(Q) \setminus\{x_{i+1},\ldots,x_l\}, with xj∈{xi+1,…,xl}x_j \in\{x_{i+1},\ldots,x_l\}. Therefore, Q′Q' is not q-hierarchical, since condition (ii) of Definition 2.1 is violated on variables xix_i and xjx_j. Indeed, atoms⁡Q(xi)⊆atoms⁡Q(xj)\operatorname{atoms}_Q(x_i) \subseteq\operatorname{atoms}_Q(x_j) and xi∈free⁡(Q′)x_i \in\operatorname{free}(Q'), whereas xj∉free⁡(Q′)x_j \notin\operatorname{free}(Q').

Suppose there exists an algorithm with update time tut_u that supports ranked direct access in time tat_a for Q(D)Q(D) and the order ≺\prec. Let N:=∣Q(D)∣N := |Q(D)| denote the number of answers of QQ on DD. Since QQ is a q-hierarchical SJFCQ, we can retrieve NN in O(1)O(1) data complexity, after a O(∣D∣)O(|D|) preprocessing, and maintain this value in O(log⁡∣D∣)O(\log|D|) update time [10 Theorem 3.9]. Let ⪯\preceq be the lexicographic order of Q(D)Q(D) induced by ≺\prec, and let (t1,…,tN)(t_1,\ldots,t_N) be the answers of Q(D)Q(D) listed according to ⪯\preceq, where each tk∈(t1,…,tN)t_k \in(t_1,\ldots,t_N) can be computed in time tat_a using the algorithm assumed in the lemma statement.

We now show how to design an efficient enumeration algorithm for Q′(D)Q'(D). We start by checking if N=0N = 0 (or if accessing t1t_1 returns out-of-bounds), in which case we output end-of-enumeration and terminate; otherwise we compute t1t_1 and output the tuple s1:=t1[Xi]s_1 := t_1[X_i]; this step takes time tat_a. Then, at each step, we compute the next tuple in the enumeration from the last outputted one. To do this, suppose we have already output the ppth tuple sp:=tk[Xi]s_p := t_k[X_i] for some k∈[N]k \in[N]. We define the next relevant rank k′:=min⁡{ℓ∈[k,N]∣tk[Xi]≠tℓ[Xi]}k' := \min\{\ell\in[k,N] \mid t_k[X_i] \ne t_\ell[X_i]\} as the next index of an answer of QQ whose projection to XiX_i changes relative to tkt_k. If no such ℓ\ell exists, the enumeration algorithm will output the special value end-of-enumeration and terminate. Otherwise, the (p+1)(p+1)th tuple of the enumeration will be set as sp+1:=tk′[Xi]s_{p+1} := t_{k'}[X_i].

All that remains is to explain how we can efficiently find sp+1s_{p+1} or detect termination. We first access tNt_N and check whether tN[Xi]=spt_N[X_i] = s_p; if so, the enumeration is finished, and the algorithm will output end-of-enumeration. Otherwise, we show how to compute the next relevant rank k′k'. To do this, we compute k′k' by performing a binary search on the interval [l,r]=[k,N][l,r] = [k,N]. At each step, let m=⌊(l+r)/2⌋m = \lfloor(l+r)/2\rfloor and access the tuple tmt_m. If tk[Xi]≺tm[Xi]t_k[X_i] \prec t_m[X_i], then k′∈[l,m]k' \in[l,m], and we set r:=mr := m. Otherwise, we have k′∈[m+1,r]k' \in[m+1,r], and we set l:=m+1l := m+1. We repeat this process, updating the interval [l,r][l,r] at each step, until we have l=rl = r. At that point, one can easily see that we must have k′=lk' = l. This binary search requires O(log⁡N)O(\log N) access calls, and therefore runs in time O(talog⁡N)O(t_a \log N).

Thus, we obtain an algorithm with update time tut_u and delay td=O(talog⁡N)t_d = O(t_a \log N) for enumerating Q′(D)Q'(D). Moreover, we have N=∣Q(D)∣≤∣D∣∣Q∣N = |Q(D)| \le|D|^{|Q|}, therefore, we obtain delay td=O(talog⁡∣D∣)t_d = O(t_a \log|D|) in data complexity.

Having proved our auxiliary result (Lemma 5.2), we are ready to conclude the proof of Theorem 5.1:

Proof of Theorem 5.1. Let DD be a database instance, QQ be a q-hierarchical SJFCQ, and ≺\prec be a non-hierarchy-consistent order over free⁡(Q)\operatorname{free}(Q) for QQ. Suppose there is ε>0\varepsilon> 0 such that there exists an algorithm with polynomial preprocessing and O(∣D∣1−ε)O(|D|^{1-\varepsilon}) update time that answers ranked direct access in O(∣D∣1−ε)O(|D|^{1-\varepsilon}) access time for Q(D)Q(D) and ≺\prec.

By Lemma 5.2, there exists a non-q-hierarchical SJFCQ Q′Q' such that there is an algorithm that enumerates Q′(D)Q'(D) with O(∣D∣1−εlog⁡∣D∣)O(|D|^{1-\varepsilon} \log|D|) delay and sublinear update time. Let ε′:=ε/2\varepsilon' := \varepsilon/2. By Theorem 2.2, as Q′Q' is not q-hierarchical, no algorithm enumerates Q′(D)Q'(D) with O(∣D∣1−ε′)O(|D|^{1-\varepsilon'}) delay and O(∣D∣1−ε′)O(|D|^{1-\varepsilon'}) update time, unless the OMv-conjecture fails. Since O(∣D∣1−εlog⁡∣D∣)=O(∣D∣1−ε′)O(|D|^{1-\varepsilon} \log|D|) = O(|D|^{1-\varepsilon'}) this contradicts the OMv-conjecture.

Therefore, unless the OMv-conjecture fails, there is no dynamic algorithm with polynomial preprocessing time and O(∣D∣1−ε)O(|D|^{1-\varepsilon}) update time that answers ranked direct access in O(∣D∣1−ε′)O(|D|^{1-\varepsilon'}) access time for a q-hierarchical SJFCQ and a non-hierarchy-consistent order on its free variables.

Ranked Enumeration Under Updates

We have shown a dichotomy for ranked direct access under updates: assuming the OMv-conjecture, there exists an algorithm for ranked direct access with complexity ∣D∣⋅∣Q∣,∣Q∣log⁡∣D∣,∣Q∣log⁡∣D∣|D| \cdot|Q|, |Q| \log|D|, |Q| \log|D| if and only if the order in input is hierarchy-consistent. For ranked enumeration, we do not have a matching lower bound. We believe in fact that the situation with ranked enumeration under updates is different, and that it remains efficient even when the order is not hierarchy-consistent, at the cost of an additional logarithmic factor in the delay in data complexity.

Our approach is to adapt the algorithm anyK-part of [14], which relies on the Lawler-Murty procedure, to the grounded tree VD(TQ)V_D(T_Q). The main difference with the static setting lies in the optimal weights of the tuples. In the static case, they are computed once, before the enumeration, by a dynamic programming pass over the tree. In our setting, we compute them during the preprocessing phase, as part of the construction of the grounded tree, and we maintain them when the database is updated. We leave the details and the analysis of this approach to a future version of the present work.

References

  1. [1]Johann Brault-Baron. De la Pertinence de l’Énumération. PhD thesis, Université Paris 7, 2013.
  2. [2]Guillaume Bagan, Arnaud Durand, and Étienne Grandjean. On Acyclic Conjunctive Queries and Constant Delay Enumeration. In International Workshop on Computer Science Logic, 2007.DOI
  3. [3]Christoph Berkholz, Jens Keppeler, and Nicole Schweikardt. Answering Conjunctive Queries under Updates. PODS, 2017.arxiv.org/abs/1702.06370
  4. [4]Florent Capelli, Nofar Carmeli, Oliver Irwin, and Sylvain Salvati. Direct Access for Conjunctive Queries with Negations. Logical Methods in Computer Science, Volume 22, Issue 1, March 2026.
  5. [5]Nofar Carmeli and Luc Segoufin. Conjunctive Queries with Self-Joins, Towards a Fine-Grained Enumeration Complexity Analysis. In PODS, 2023.
  6. [6]Nofar Carmeli, Nikolaos Tziavelis, Wolfgang Gatterbauer, Benny Kimelfeld, and Mirek Riedewald. Tractable Orders for Direct Access to Ranked Answers of Conjunctive Queries. ACM Transactions on Database Systems, 48(1), March 2023.
  7. [7]Étienne Grandjean. Sorting, Linear Time and the Satisfiability Problem. Annals of Mathematics and Artificial Intelligence, 16(1), December 1996.DOI
  8. [8]J. Hu. Ranked Enumeration over Circuits via Implicit Representations. PhD thesis, University of California, Santa Cruz, 2026. ProQuest ID: 32863524.
  9. [9]Muhammad Idris, Martin Ugarte, and Stijn Vansummeren. The Dynamic Yannakakis Algorithm: Compact and Efficient Query Processing Under Updates. SIGMOD, 2017.
  10. [10]Jens Keppeler. Answering Conjunctive Queries and FO+MOD Queries under Updates. PhD thesis, Humboldt-Universität zu Berlin, Mathematisch-Naturwissenschaftliche Fakultät, 2020.DOI
  11. [11]Ahmet Kara, Zheng Luo, Milos Nikolic, Dan Olteanu, and Haozhe Zhang. Tractable Conjunctive Queries over Static and Dynamic Relations. In ICDT, Dagstuhl, Germany, 2025.arxiv.org/abs/2404.16224
  12. [12]Dan Olteanu and Maximilian Schleich. Factorized Databases. SIGMOD Rec., 45(2), September 2016.
  13. [13]Clément Rouvroy, Nofar Carmeli, David Carral, and Luc Segoufin. Constant-Delay Enumeration of Conjunctive Queries with Self-Joins and Projections. In BDA, 2026.
  14. [14]Nikolaos Tziavelis, Wolfgang Gatterbauer, and Mirek Riedewald. Any-k Algorithms for Enumerating Ranked Answers to Conjunctive Queries. ACM Transactions on Database Systems, 51(1), 2025.arxiv.org/abs/2205.05649
  15. [15]Qichen Wang, Xiao Hu, Binyang Dai, and Ke Yi. Change Propagation Without Joins. Proc. VLDB Endow., 16(5), January 2023.DOI

Paper details

vlnc:W_fdktud8f
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
Usage

views · 9

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

  • Computer Science

Not recorded for this paper

View statement
Looking for related papers…
Contents