Nofar Carmeli
email: nofar.carmeli@inria.fr

Nikolaos Tziavelis
email: tziavelis.n@northeastern.edu

Wolfgang Gatterbauer
email: w.gatterbauer@northeastern.edu

Benny Kimelfeld
email: bennyk@cs.technion.ac.il

Mirek Riedewald
email: m.riedewald@northeastern.edu

Tractable Orders for Direct Access to Ranked Answers of Conjunctive Queries

Keywords: CCS Concepts:, Theory of computation → Database theory, Complexity classes, Database query languages (principles), Database query processing conjunctive queries, direct access, ranking function, answer orderings, query classification

ou non, émanant des établissements d'enseignement et de recherche français ou étrangers, des laboratoires publics ou privés.

INTRODUCTION

When can we support direct access to a ranked list of answers to a database query without (and considerably faster than) materializing all answers? To illustrate the concrete instantiation of this question, assume the following simple relational schema for information about activities of residents in the context of pandemic spread:

Visits(person, age, city) Cases(city, date, #cases)

Here, the relation Visits mentions, for each person in the database, the cities that the person visits regularly (e.g., for work and for visiting relatives) and the age of the person (for risk assessment); the relation Cases specifies the number of new infection cases in specific cities at specific dates (a measure that is commonly used for spread assessment albeit being sensitive to the amount of testing).

for Visits Cases ordered by #cases and then by age? Interestingly, we will show that the answer is negative: it is impossible to build in quasilinear time a direct-access structure with logarithmic access time for that lexicographic order. Getting back to the question posed at the beginning of this section, in this paper we embark on the challenge of identifying, for each CQ, the orders that allow for efficiently constructing a directaccess structure. We adopt the tractability yardstick of quasilinear construction (preprocessing) time and logarithmic access time. In addition, we focus on two types of orders: lexicographic orders, and scoring by the sum of attribute weights.

As aforesaid, some of the orders that we study are intractable. To understand the root cause of the hardness, we consider another task that allows us to narrow our question to a considerably weaker guarantee. Our notion of tractability so far requires the construction of a structure in quasilinear time that allows direct access in logarithmic time. In particular, if our goal is to compute just a single quantile, say the 𝑘 th answer, then it should take quasilinear time. Computing a single quantile is known as the selection problem [START_REF] Blum | Time bounds for selection[END_REF]. The question we ask is to what extent selection is a weaker requirement than direct access In other words, do we get more tractable cases if we lift the requirement to construct a data structure and instead ask for quasilinear time per access?

In some situations, we might be able to avoid hardness through a more careful inspection of the integrity constraints that the database guarantees on the source relations. For example, it turns out that we can construct in quasilinear time a direct-access structure for Visits Cases ordered by #cases and then by age if we assume that each city occurs at most once in Cases (i.e., for each city we have a report for a single day). Hence, it may be the case that an ordered CQ is classified as intractable (with respect to the guarantees that we seek), but it becomes tractable if we are allowed to assume that the input database satisfies some integrity constraints such as key constraints or more general Functional Dependencies (FDs). Moreover, FDs are so common that ignoring them implies that we often miss opportunities of fast algorithms. This angle arises regardless of answer ordering, and indeed, Carmeli and Kröll [START_REF] Carmeli | Enumeration Complexity of Conjunctive Queries with Functional Dependencies[END_REF] showed precisely how the class of (self-join-free) CQs with tractable enumeration extends in the presence of FDs. Accordingly, we extend our study on ranked direct access and the selection problem to incorporate FDs, and aim to classify every combination of (a) CQ, (b) order over the answers, and (c) set of FDs.

Contributions. Before we describe the results that we establish in this manuscript, let us illustrate them on an example.

Example 1.1. Figure 2 depicts an example database and different orderings of the answers to the 2-path CQ 𝑄 (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧). The question we ask is whether the median (e.g., the 3rd answer in the example) or in general, the answer in any index can be computed efficiently as the database size 𝑛 grows. Tractable direct access requires O (polylog 𝑛) per access after O (𝑛 polylog 𝑛) preprocessing, while tractable selection requires O (𝑛 polylog 𝑛) for a single access. We compare the impact of different orders, projections, and FDs on the example 2-path CQ.

• LEX ⟨𝑥, 𝑦, 𝑧⟩: Direct access is tractable.

• LEX ⟨𝑥, 𝑧, 𝑦⟩: Direct access is intractable because the lexicographic order "does not agree" with the query structure, which we capture through the concept of a disruptive trio that we introduce later on. However, selection is tractable. • LEX ⟨𝑥, 𝑧⟩: Direct access is intractable because the variables in the partial lexicographic order are not "connected" in a particular way. We will define this as not being 𝐿-connex for a lexicographic order 𝐿. However, selection is again tractable. • LEX ⟨𝑥, 𝑧⟩ and 𝑦 projected away: Selection is now intractable because the query is not free-connex.

Free-connex

Tractable ≡ < 1, 𝑛 polylog 𝑛 > Fig. 1. Overview of our results for lexicographic (LEX) orders and sum-of-weights (SUM) orders. CQs without self-joins (SJ-free) are classified based on the tractability of the direct access problem (left) and the selection problem (right). The 𝐿-connex property applies only to lexicographic orders 𝐿 (the precise definitions are given in Section 2). All tractable cases extend to CQs with self-joins. The sizes of the ellipses are arbitrary and do not correspond to the size or importance of the classes. • LEX ⟨𝑥, 𝑧, 𝑦⟩, with the FD 𝑅 : 𝑦 → 𝑥 or the FD 𝑆 : 𝑦 → 𝑧: Direct access is tractable as a consequence of earlier work on enumeration with FDs [START_REF] Carmeli | Enumeration Complexity of Conjunctive Queries with Functional Dependencies[END_REF]. • LEX ⟨𝑥, 𝑧, 𝑦⟩, with the FD 𝑅 : 𝑥 → 𝑦: Direct access is tractable with the techniques that we develop in this paper. Intuitively, the FD implies that the order is equivalent to the tractable order ⟨𝑥, 𝑦, 𝑧⟩. • LEX ⟨𝑥, 𝑧, 𝑦⟩, with the FD 𝑆 : 𝑧 → 𝑦: Direct access is intractable since the FD does not help in this case. • SUM 𝑥 + 𝑦 + 𝑧: Direct access is intractable because it would allow us to solve the 3SUM problem in subquadratic time, yet selection is tractable. • SUM 𝑥 + 𝑦 and 𝑧 projected away: Direct access is tractable because all the free variables are contained in 𝑅. This means that we can produce the sorted list of answers during preprocessing. • SUM 𝑥 +𝑧 and 𝑦 projected away: Selection is intractable because the query is not free-connex.

R 𝑥 𝑦

(1) Our first main result is an algorithm for direct access for lexicographic orders, including ones that are not achievable by past structures. We further show that within the class of CQs without self-joins, our algorithm covers all the tractable cases (in the sense adopted here), and we establish a decidable and easy-to-test classification of the lexicographic orders over the free variables into tractable and intractable ones. For instance, in the case of Visits Cases the lexicographic order (#cases, age, city, date, person) is intractable. It is classified as such because #cases and age are non-neighbours (i.e., do not co-occur in the same atom), but city, which comes after #cases and age in the order, is a neighbour of both. This is what we call a disruptive trio. The lexicographic order (#cases, age) is also intractable since the query Visits Cases is not {#cases, age}-connex (a similar condition to being free-connex, but for the subset of the variables that appear in the lexicographic order instead of the free ones). In contrast, the lexicographic order (#cases, city, age) is tractable. We also show that within the tractable side, the structure we construct allows for inverted access in constant time.

Our classification is proved in two steps. We begin by considering the complete lexicographic orders (that involve all free variables). We show that for free-connex CQs without self-joins, the absence of a disruptive trio is a sufficient and necessary condition for tractability. We then generalize to partial lexicographic orders 𝐿 where the ordering is determined only by a subset of the free variables. There, the condition is that there is no disruptive trio and that the query is 𝐿-connex. Interestingly, it turns out that a partial lexicographic order is tractable if and only if it is the prefix of a complete tractable lexicographic order.

(2) Next, we study the selection problem for lexicographic orders and show that being free-connex is a sufficient and necessary condition for a linear-time solution in the case of CQs without self-joins. In particular, there are ordered queries with tractable selection but intractable direct access, namely free-connex CQs without self-joins where we have a disruptive trio or lack the property of being 𝐿-connex.

(3) A lexicographic order is a special case of an ordering by the sum of attribute weights, where every database value is mapped to some weight. Hence, a natural question is which CQs have a tractable direct access by the order of sum. For example, what about Visits Cases with the order (𝛼•#cases + 𝛽•age)? It is easy to see that this order is intractable because the lexicographic order (#cases, age) is intractable. In fact, it is easy to show that an order by sum is intractable whenever there exists an intractable lexicographic order (e.g., there is a disruptive trio). However, we will show that the situation is worse: the only tractable case is the one where the CQ is free-connex and there is an atom that contains all of the free variables. In particular, ranked direct access by sum is intractable already for the Cartesian product 𝑄 (𝑐 1 , 𝑑, 𝑥, 𝑝, 𝑎, 𝑐 2 ) :-Visits(𝑝, 𝑎, 𝑐 1 ), Cases(𝑐 2 , 𝑑, 𝑥), even though every lexicographic order is tractable (according to our aforementioned classification). This daunting hardness also emphasizes how ranked direct access is fundamentally harder than ranked enumeration where, in the case of the sum of attributes, the answers of every free-connex CQ can be enumerated with logarithmic delay after a linear preprocessing time [START_REF] Tziavelis | Any-k Algorithms for Enumerating Ranked Answers to Conjunctive Queries[END_REF].

(4) Next, we study the selection problem for the sum of weights, and establish the following dichotomy in complexity (again assuming fine-grained hypotheses): the selection problem can be solved in O (𝑛 log 𝑛) time, where 𝑛 is the size of the database, if and only if the hypergraph of the CQ restricted to the free variables contains at most two maximal hyperedges (w.r.t. containment). The tractable side is applicable even in the presence of self-joins, and it is achieved by adopting an algorithm by Frederickson and Johnson [START_REF] Frederickson | Generalized Selection and Ranking: Sorted Matrices[END_REF] originally developed for selection on sorted matrices. For illustration, the selection problem is solvable in quasilinear time for the query Visits Cases ordered by sum.

(5) Lastly, we study the implication of FDs on our results, and generalize all of them to incorporate a set of unary FDs (i.e., FDs with a single attribute on the left-hand side). Like previous works on FDs on enumeration [START_REF] Carmeli | Enumeration Complexity of Conjunctive Queries with Functional Dependencies[END_REF], deletion propagation [START_REF] Kimelfeld | A dichotomy in the complexity of deletion propagation with functional dependencies[END_REF], resilience [START_REF] Freire | The Complexity of Resilience and Responsibility for Self-Join-Free Conjunctive Queries[END_REF], and probabilistic inference [START_REF] Gatterbauer | Approximate Lifted Inference with Probabilistic Databases[END_REF], we use the notion of an extended CQ to reason about the tractability of a CQ under the presence of FDs. The idea is that by looking at the structure of the extended CQ (without FDs), then we are able to classify the original CQ together with the FDs. While this works in a relatively straightforward way for the case of sum, the case of lexicographic orders is more involved since the FDs may interact with the order in non-trivial ways. To extend our dichotomy results for lexicographic orders to incorporate FDs, we show how the extension of a CQ and order may also result in a reordering of the variables. Then, tractability is decided by the extended CQ together with the reordered lexicographic order.

Overview of results. We summarize our results (excluding the dichotomies under the presence of FDs) in Figure 1 with different colors indicating the tractability of the studied orders, namely lexicographic (LEX) and sum-of-weights (SUM) orders. For both direct access and selection, we obtain the precise picture of the orders and CQs without self-joins that are tractable according to our yardstick: O (𝑛 polylog 𝑛) preprocessing and O (polylog 𝑛) per access for direct access (conveniently denoted as ⟨𝑛 polylog 𝑛, polylog 𝑛⟩ for ⟨preprocessing, access⟩) and O (𝑛 polylog 𝑛) for selection (or ⟨1, 𝑛 log 𝑛⟩). Finally, we show how the results are affected by every set of unary FDs (not depicted in Figure 1); in other words, we extend our dichotomies to incorporate the FDs of the underlying schema under the restriction that all FDs have a single attribute on the premise. We leave the case of more general FDs open for future research.

Comparison to an earlier conference version.

A preliminary version of this manuscript appeared in a conference proceedings [START_REF] Carmeli | Tractable Orders for Direct Access to Ranked Answers of Conjunctive Queries[END_REF]. Compared to that version, this manuscript includes significant extensions and improvements. First, we added an investigation on the complexity of the selection problem with lexicographic orders, establishing a complete dichotomy result (Theorem 6.1 and all of Section 6). Second, we extended a dichotomy from the conference version to include self-join-free CQs beyond full CQs for the selection problem by SUM, so it now covers all self-join-free CQs (with projections), thereby resolving the corresponding open question from the conference paper (Section 7.4). Third, we extended our results to cover unary FDs (Section 8). Fourth, we have clarified the relationship between the disruptive trio and the concept of an elimination order (Remark 1). Fifth and last, we made considerable simplifications and improvements in previous components, including the proof of hardness of direct access for lexicographic orders (Lemma 3.13) and several proofs for the SUM selection (Section 7).

Applicability. It is important to note that while our positive results are stated over a limited class of queries (a fragment of acyclic CQs), there are some implications beyond this class that are immediate yet significant. In particular, we can use known techniques that reduce other CQs to a tractable form and then apply our direct-access or selection algorithms. As an example, a hypertree decomposition can be used to transform a cyclic CQ to an acyclic form by paying a non-linear overhead during preprocessing [START_REF] Gottlob | Hypertree Decompositions: Questions and Answers[END_REF]. As another example, a CQ with inequality (<) predicates can be reduced to a CQ without inequalities by paying only a polylogarithmic-factor increase in the size of the database [START_REF] Tziavelis | Beyond Equi-joins: Ranking, Enumeration and Factorization[END_REF].

Outline. The remainder of the manuscript is organized as follows. Section 2 gives the necessary background. In Section 3, we consider direct access by lexicographic orders that include all the free variables, and Section 4 extends the results to partial ones. We move on to the (for the most part) negative results for direct access by sum orders in Section 5. We study the selection problem for lexicographic orders and sum in Section 6 and Section 7, respectively. We extend our results to incorporate unary FDs in Section 8 and, lastly, conclude and give future directions in Section 9.

, Vol. 1, No. 1, Article . Publication date: November 2023.

PRELIMINARIES

Basic Notions

Database. A schema S is a set of relational symbols {𝑅 1 , . . . , 𝑅 𝑚 }. We use ar(𝑅) for the arity of a relational symbol 𝑅. A database instance 𝐼 contains a finite relation 𝑅 𝐼 ⊆ dom ar(𝑅) for each 𝑅 ∈ S, where dom is a set of constant values called the domain. When 𝐼 is clear, we simply use 𝑅 instead of 𝑅 𝐼 . We use 𝑛 for the size of the database, i.e., the total number of tuples.

Queries. A conjunctive query (CQ) 𝑄 over schema S is an expression of the form 𝑄 (X 𝑓 ) :-𝑅 1 (X 1 ), . . . , 𝑅 ℓ (X ℓ ), where the tuples X 𝑓 , X 1 , . . . , X ℓ hold variables, every variable in X 𝑓 appears in some X 1 , . . . , X ℓ , and {𝑅 1 , . . . , 𝑅 ℓ } ⊆ S. Each 𝑅 𝑖 (X 𝑖 ) is called an atom of the query 𝑄, and the set of all atoms is denoted by atoms(𝑄). We use var(𝑒) or var(𝑄) for the set of variables that appear in an atom 𝑒 or query 𝑄, respectively. 5 The variables X 𝑓 are called free and are denoted by free(𝑄). A CQ is full if free(𝑄) = var(𝑄) and Boolean if free(𝑄) = ∅. Sometimes, we say that CQs that are not full have projections. A repeated occurrence of a relational symbol is a self-join and if no self-joins exist, a CQ is called self-join-free. A homomorphism from a CQ 𝑄 to a database 𝐼 is a mapping of var(𝑄) to constants from dom, such that every atom of 𝑄 maps to a tuple in the database 𝐼 . A query answer is such a homomorphism followed by a projection on the free variables. The answer to a Boolean CQ is whether such a homomorphism exists. The set of query answers is 𝑄 (𝐼 ) and we use 𝑞 ∈ 𝑄 (𝐼 ) for a query answer. For an atom 𝑅(X) of a CQ, we say that a tuple 𝑡 ∈ 𝑅 assigns a value 𝑐 to a variable 𝑥 and denote it as 𝑡 [𝑥] = 𝑐 if for every index 𝑖 such that X[𝑖] = 𝑥 we have that 𝑡 [𝑖] = 𝑐. The active domain of a variable 𝑥 is the subset of dom that 𝑥 can be assigned from the database 𝐼 .

Hypergraphs. A hypergraph H = (𝑉 , 𝐸) is a set 𝑉 of vertices and a set 𝐸 of subsets of 𝑉 called hyperedges. Two vertices in a hypergraph are neighbors if they appear in the same edge. A path of H is a sequence of vertices such that every two succeeding vertices are neighbors. A chordless path is a path in which no two non-succeeding vertices appear in the same hyperedge (in particular, no vertex appears twice). A join tree of a hypergraph H = (𝑉 , 𝐸) is a tree 𝑇 where the nodes 6 are the hyperedges of H and the running intersection property holds, namely: for all 𝑢 ∈ 𝑉 the set {𝑒 ∈ 𝐸 | 𝑢 ∈ 𝑒} forms a (connected) subtree in 𝑇 . An equivalent phrasing of the running intersection property is that given two nodes 𝑒 1 , 𝑒 2 of the tree, for any node 𝑒 3 on the simple path between them, we have that 𝑒 1 ∩ 𝑒 2 ⊆ 𝑒 3 . A hypergraph H is acyclic if there exists a join tree for H . We associate a hypergraph H (𝑄) = (𝑉 , 𝐸) to a CQ 𝑄 where the vertices are the variables of 𝑄, and every atom of 𝑄 corresponds to a hyperedge with the same set of variables. Stated differently, 𝑉 = var(𝑄) and 𝐸 = {var(𝑒)|𝑒 ∈ atoms(𝑄)}. With a slight abuse of notation, we identify atoms of 𝑄 with hyperedges of H (𝑄). A CQ 𝑄 is acyclic if H (𝑄) is acyclic, otherwise it is cyclic. The free-restricted hypergraph H free (𝑄) is the restriction of H (𝑄) = (𝑉 , 𝐸) on the nodes that correspond to free variables, i.e., H free (𝑄) = (free(𝑄), {𝑒 ∩ free(𝑄)|𝑒 ∈ 𝐸}).

Free-connex CQs. A hypergraph H ′ is an inclusive extension of H if every edge of H appears in H ′ , and every edge of H ′ is a subset of some edge in H . Given a subset X of the vertices of H , a tree 𝑇 is an ext-X-connex tree (i.e., extension-X-connex tree) for a hypergraph H if: (1) 𝑇 is a join tree of an inclusive extension of H , and (2) there is a subtree7 𝑇 ′ of 𝑇 that contains exactly the vertices X [START_REF] Bagan | On Acyclic Conjunctive Queries and Constant Delay Enumeration[END_REF]. As an example, Figure 3b depicts an ext-{𝑣 1 , 𝑣 2 }-connex tree for a query 𝑄 3 . We say that a hypergraph is X-connex if it has an ext-X-connex tree [START_REF] Bagan | On Acyclic Conjunctive Queries and Constant Delay Enumeration[END_REF]. A hypergraph is X-connex iff it is acyclic and it remains acyclic after the addition of a hyperedge containing exactly X [START_REF] Berkholz | Constant delay enumeration for conjunctive queries: a tutorial[END_REF][START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF]. Given a hypergraph H and a subset X of its vertices, an X-path is a chordless path (𝑥, 𝑧 1 , . . . , 𝑧 𝑘 , 𝑦) in H with 𝑘 ≥ 1, such that 𝑥, 𝑦 ∈ X, and 𝑧 1 , . . . , 𝑧 𝑘 ∉ X. A hypergraph is X-connex iff it has no X-path [START_REF] Bagan | On Acyclic Conjunctive Queries and Constant Delay Enumeration[END_REF]. A CQ 𝑄 is free-connex if H (𝑄) is free(𝑄)-connex [START_REF] Bagan | On Acyclic Conjunctive Queries and Constant Delay Enumeration[END_REF]. Note that a free-connex CQ is necessarily acyclic. 8 An implication of the characterization given above is that it suffices to find a join-tree for an inclusive extension of a hypergraph H to infer that H is acyclic.

To simplify notation, we also say that a CQ is 𝐿-connex for a (partial) lexicographic order 𝐿 if the CQ is X-connex for the set of variables X that appear in 𝐿. Generalizing the notion of an inclusive extension, we say that a hypergraph H ′ is inclusion equivalent to H if every hyperedge of H is a subset of some hyperedge of H ′ and vice versa. For example, the two hypergraphs with hyperedges 𝐸 1 = {{𝑥, 𝑦}, {𝑦, 𝑧}} and 𝐸 2 = {{𝑥, 𝑦}, {𝑦, 𝑧}, {𝑧}} are inclusion equivalent because {𝑧} is a subset of {𝑦, 𝑧} and every hyperedge is trivially a subset of itself.

Problem Definitions

Orders over Answers. For a CQ 𝑄 and database instance 𝐼 , we assume a total order ⪯ over the query answers 𝑄 (𝐼 ). We consider two types of orders in this paper:

(1) LEX: Assuming that the domain values are ordered, a lexicographic order 𝐿 is an ordering of free(𝑄) such that ⪯ compares two query answers 𝑞 1 , 𝑞 2 on the value of the first variable in 𝐿, then on the second (if they are equal on the first), and so on [START_REF] Harzheim | Ordered sets[END_REF]. A lexicographic order is called partial if the variables in 𝐿 are a subset of free(𝑄).

(2) SUM: The second type of order assumes given weight functions that assign real-valued weights to the domain values of each variable. More precisely, for each variable 𝑥, we define a function 𝑤 𝑥 : dom → R. Then, the query answers are ordered by a weight which is computed by aggregating the weights of the assigned values of free variables. In a sum-ofweights order, denoted by SUM, we have the weight of each query answer 𝑞 ∈ 𝑄 (𝐼 ) to be 𝑤 𝑄 (𝑞) = 𝑥 ∈free(𝑄) 𝑤 𝑥 (𝑞(𝑥)) and 𝑞 1 ⪯ 𝑞 2 implies that 𝑤 𝑄 (𝑞 1 ) ≤ 𝑤 𝑄 (𝑞 2 ). We emphasize that we allow only free variables to have weights, otherwise different semantics for the query answers are possible [START_REF] Tziavelis | Any-k Algorithms for Enumerating Ranked Answers to Conjunctive Queries[END_REF]. To simplify notation, we sometimes refer to all 𝑤 𝑥 , 𝑥 ∈ free(𝑄) and 𝑤 𝑄 together as one weight function 𝑤. Attribute Weights vs. Tuple Weights for SUM. Notice that in the definition above, we assume that the input weights are assigned to the domain values of the attributes. Alternatively, the input weights could be assigned to the relation tuples, a convention that has been used in past work on ranked enumeration [START_REF] Tziavelis | Optimal Algorithms for Ranked Enumeration of Answers to Full Conjunctive Queries[END_REF]. Since there are several reasonable semantics for interpreting a tupleweight ranking for CQs with projections and/or self-joins [START_REF] Tziavelis | Any-k Algorithms for Enumerating Ranked Answers to Conjunctive Queries[END_REF], we elect to present our results for the case of attribute weights. We note that our results directly extend to the case of tuple weights for full self-join-free CQs where the semantics are clear. On the one hand, attribute weights can easily be transformed to tuple weights in linear time such that the weights of the query answers remain the same. This works by assigning each variable to one of the atoms that it appears in, and computing the weight of a tuple by aggregating the weights of the assigned attribute values. Therefore, our hardness results for SUM orders directly extend to the case of tuple weights. On the other hand, our positive results on direct access (Section 5), selection (Section 7.2) and their extension to the case of FDs (Section 8.1) rely on algorithms that innately operate on tuple weights, thus we cover those cases too.

Direct Access vs. Selection. We now define two problems that both directly access ordered query answers. Since our goal is to classify the combination of CQs and orders by their tractability, we let those two define the problem. Specifically, a problem is defined by a CQ 𝑄 and a family of orders Π. The reason that we use a family of orders in the problem definition is that for the case of SUM, we do not distinguish between different weight functions in our classification. For LEX, we always consider the family of orders to contain only one specific (partial) lexicographic order. Definition 2.1 (Direct Access). Let 𝑄 be a CQ and Π a family of total orders. The problem of direct access by Π takes as an input a database 𝐼 and an order ≺ from Π and constructs a data structure (called the preprocessing phase) which then allows access to a query answer 𝑞 ∈ 𝑄 (𝐼 ) at any index 𝑘 of the (implicit) array of query answers sorted by ≺.

The essence of direct access is that after the preprocessing phase, we need to be able to support multiple such accesses. Notably, the values of 𝑘 that are going to be requested afterward are not known during preprocessing.

Definition 2.2 (Selection).

Let 𝑄 be a CQ and Π a family of total orders. The problem of selection by Π takes as an input a database 𝐼 , an order ≺ from Π, and asks for the query answer 𝑞 ∈ 𝑄 (𝐼 ) at index 𝑘 of the (implicit) array of query answers sorted by ≺.

The problem of selection [START_REF] Blum | Time bounds for selection[END_REF][START_REF] Floyd | Expected Time Bounds for Selection[END_REF][START_REF] Frederickson | An Optimal Algorithm for Selection in a Min-Heap[END_REF] is a computationally easier task that requires only a single direct access, hence does not make a distinction between preprocessing and access phases. A special case of the problem is finding the median query answer.

For both problems, if the index 𝑘 exceeds the total number of answers, the returned answer is "out-of-bound".

Complexity Framework and Sorting

We measure asymptotic complexity in terms of the size of the database 𝑛, while the size of the query is considered a constant. If the time for preprocessing is O 𝑓 (𝑛) and the time for each access is O 𝑔(𝑛) , we denote that as ⟨𝑓 (𝑛), 𝑔(𝑛)⟩, where 𝑓 , 𝑔 are functions from N to R. Note that by definition, the problem of selection asks for a ⟨1, 𝑔(𝑛)⟩ solution.

Our goal for both problems is to achieve efficient access in time significantly smaller than (the worst case) |𝑄 (𝐼 )|. For direct access, we consider the problem tractable if ⟨𝑛 log 𝑛, log 𝑛⟩ is possible, and for selection ⟨1, 𝑛 log 𝑛⟩.

The model of computation is the standard RAM model with uniform cost measure. In particular, it allows for linear time construction of lookup tables, which can be accessed in constant time. We would like to point out that some past works [START_REF] Bagan | On Acyclic Conjunctive Queries and Constant Delay Enumeration[END_REF][START_REF] Carmeli | Answering (Unions of) Conjunctive Queries Using Random Access and Random-Order Enumeration[END_REF] have assumed that in certain variants of the model, sorting can be done in linear time [START_REF] Grandjean | Sorting, linear time and the satisfiability problem[END_REF]. Since we consider problems related to summation and sorting [START_REF] Frederickson | Generalized Selection and Ranking: Sorted Matrices[END_REF] where a linear-time sort would improve otherwise optimal bounds, we adopt a more standard assumption that sorting is comparison-based and possible only in quasilinear time. As a consequence, some upper bounds mentioned in this paper are weaker than the original sources which assumed linear-time sorting [START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF][START_REF] Carmeli | Answering (Unions of) Conjunctive Queries Using Random Access and Random-Order Enumeration[END_REF].

Hardness Hypotheses

All the lower bounds we prove are conditional on one or multiple of the following four hypotheses. Hypothesis 1 (sparseBMM). Two Boolean matrices 𝐴 and 𝐵, represented as lists of their non-zero entries, cannot be multiplied in time 𝑚 1+𝑜 (1) , where 𝑚 is the number of non-zero entries in 𝐴, 𝐵, and 𝐴𝐵.

A consequence of this hypothesis is that we cannot answer the query 𝑄 (𝑥, 𝑧) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧) with quasilinear preprocessing and polylogarithmic delay. In more general terms, any self-joinfree acyclic non-free-connex CQ cannot be enumerated with quasilinear 9 preprocessing time and polylogarithmic delay assuming the sparseBMM hypothesis [START_REF] Bagan | On Acyclic Conjunctive Queries and Constant Delay Enumeration[END_REF][START_REF] Berkholz | Constant delay enumeration for conjunctive queries: a tutorial[END_REF].

Hypothesis 2 (Hypercliqe [START_REF] Abboud | Popular Conjectures Imply Strong Lower Bounds for Dynamic Problems[END_REF][START_REF] Lincoln | Tight Hardness for Shortest Cycles and Paths in Sparse Graphs[END_REF]). For every 𝑘 ≥ 2, there is no 𝑂 (𝑚 polylog 𝑚) algorithm for deciding the existence of a (𝑘+1, 𝑘)-hyperclique in a 𝑘-uniform hypergraph with 𝑚 hyperedges, where a (𝑘+1, 𝑘)-hyperclique is a set of 𝑘+1 vertices such that every subset of 𝑘 elements is a hyperedge.

When 𝑘 = 2, this follows from the "𝛿-Triangle" hypothesis [START_REF] Abboud | Popular Conjectures Imply Strong Lower Bounds for Dynamic Problems[END_REF]. This is the hypothesis that we cannot detect a triangle in a graph in linear time [START_REF] Alon | Finding and Counting Given Length Cycles[END_REF]. When 𝑘 ≥ 3, this is a special case of the "(ℓ, 𝑘)-Hyperclique" hypothesis [START_REF] Lincoln | Tight Hardness for Shortest Cycles and Paths in Sparse Graphs[END_REF]. A known consequence is that Boolean cyclic and self-join-free CQs cannot be answered in quasilinear 9 time [START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF]. As a result, cyclic and self-join-free CQs do not admit enumeration with quasilinear preprocessing time and polylogarithmic delay assuming the Hypercliqe hypothesis [START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF]. Hypothesis 3 (3sum [START_REF] Baran | Subquadratic Algorithms for 3SUM[END_REF][START_REF] Patrascu | Towards polynomial lower bounds for dynamic problems[END_REF]). Deciding whether there exist 𝑎 ∈ 𝐴, 𝑏 ∈ 𝐵, 𝑐 ∈ 𝐶 from three sets of integers 𝐴, 𝐵, 𝐶, each of size Ω(𝑚), such that 𝑎 + 𝑏 + 𝑐 = 0 cannot be done in time 𝑂 (𝑚 2-𝜖 ) for any 𝜖 > 0.

In its simplest form, the 3sum problem asks for three distinct real numbers 𝑎, 𝑏, 𝑐 from a set 𝑆 with 𝑚 elements that satisfy 𝑎 + 𝑏 + 𝑐 = 0. There is a simple 𝑂 (𝑚 2 ) algorithm for the problem, but it is conjectured that in general, no truly subquadratic solution exists [START_REF] Patrascu | Towards polynomial lower bounds for dynamic problems[END_REF]. The significance of this conjecture has been highlighted by many conditional lower bounds for problems in computational geometry [START_REF] Gajentaan | On a class of O(n2) problems in computational geometry[END_REF] and within the class P in general [START_REF] Vassilevska | Hardness of Easy Problems: Basing Hardness on Popular Conjectures such as the Strong Exponential Time Hypothesis (Invited Talk)[END_REF]. Note that the problem remains hard even for integers provided that they are sufficiently large (i.e., in the order of 𝑂 (𝑛 3 )) [START_REF] Patrascu | Towards polynomial lower bounds for dynamic problems[END_REF]. The hypothesis we use here has three different sets of numbers, but it is equivalent [START_REF] Baran | Subquadratic Algorithms for 3SUM[END_REF]. This lower bound has been confirmed in some restricted models of computation [START_REF] Ailon | Lower Bounds for Linear Degeneracy Testing[END_REF][START_REF] Erickson | Lower Bounds for Linear Satisfiability Problems[END_REF]. Hypothesis 4 (Seth [START_REF] Impagliazzo | On the Complexity of K-SAT[END_REF]). For the satisfiability problem with 𝑚 variables and 𝑘 variables per clause (𝑘-SAT), if 𝑠 𝑘 is the infimum of the real numbers 𝛿 for which 𝑘-SAT admits an O (2 𝛿𝑚 ) algorithm, then lim 𝑘→∞ 𝑠 𝑘 = 1 Intuitively, the Strong Exponential Time Hypothesis (Seth) states that the best possible algorithms for 𝑘-SAT approach O (2 𝑚 ) running time when 𝑘 goes to infinity. Seth implies that the 𝑘-Dominating Set problem on a graph with 𝑚 vertices cannot be solved in O (𝑚 2-𝜖 ) for 𝑘 ≥ 3 and any constant 𝜖 [START_REF] Pătraşcu | On the Possibility of Faster SAT Algorithms[END_REF]. Based on that, it can be shown that counting the answers to a self-join-free acyclic CQ that is not free-connex cannot be done in O (𝑛 2-𝜖 ′ ) for any constant 𝜖 ′ [START_REF] Mengel | A short note on the counting complexity of conjunctive queries[END_REF].

Known Results for CQs

Eliminating Projection. We now provide some background that relates to the efficient handling of CQs. For a query with projections, a standard strategy is to reduce it to an equivalent one where techniques for acyclic full CQs can be leveraged. The following proposition, which is widely known and used [START_REF] Berkholz | Constant delay enumeration for conjunctive queries: a tutorial[END_REF], shows that this is possible for free-connex CQs. Proposition 2.3 (Folklore). Given a database instance 𝐼 , a CQ 𝑄, a join tree 𝑇 of an inclusive extension of 𝑄, and a subtree 𝑇 ′ of 𝑇 that contains all the free variables, we can compute in linear time a database instance 𝐼 ′ over the schema of a CQ 𝑄 ′ that consists of the nodes of 𝑇 ′ such that 𝑄 (𝐼 ) = 𝑄 ′ (𝐼 ′ ) and

|𝐼 ′ | ≤ |𝐼 |.
This reduction is done by first creating a relation for every node in 𝑇 using projections of existing relations, then performing the classic semi-join reduction by Yannakakis [START_REF] Yannakakis | Algorithms for Acyclic Database Schemes[END_REF] to filter the relations of 𝑇 ′ according to the relations of 𝑇 , and then we can simply ignore all relations that do not appear in 𝑇 ′ and obtain the same answers. Afterward, they can be handled efficiently, e.g. their answers can be enumerated with constant delay [START_REF] Bagan | On Acyclic Conjunctive Queries and Constant Delay Enumeration[END_REF]. We refer the reader to recent tutorials [START_REF] Berkholz | Constant delay enumeration for conjunctive queries: a tutorial[END_REF][START_REF] Durand | Fine-Grained Complexity Analysis of Queries: From Decision to Counting and Enumeration[END_REF] for an intuitive illustration of the idea.

Ranked enumeration. Enumerating the answers to a CQ in ranked order is a special case of direct access where the accessed indexes are consecutive integers starting from 0. As it was recently shown [START_REF] Tziavelis | Optimal Join Algorithms Meet Top-k[END_REF], ranked enumeration for CQs is intimately connected to classic algorithms on finding the 𝑘 th shortest path in a graph. In contrast to direct access, ranked enumeration by SUM orders (which also includes lexicographic orderings as a special case) is possible with logarithmic delay after a linear-time preprocessing phase for all free-connex CQs [START_REF] Tziavelis | Optimal Algorithms for Ranked Enumeration of Answers to Full Conjunctive Queries[END_REF]. In contrast, as we will show, that is not the case for direct access. Existing ranked-enumeration algorithms rely on priority queue structures that compare a minimal number of candidate answers to produce the ranked answers one-by-one in order. There is no straightforward way to extend them to the task of direct access where we may skip over a large number of answers to get to an arbitrary index 𝑘.

Direct Access. Carmeli et al. [START_REF] Carmeli | Answering (Unions of) Conjunctive Queries Using Random Access and Random-Order Enumeration[END_REF] devise a direct access structure (called "random access") and use it to uniformly sample CQ answers (called "random-order enumeration"). While it leverages the idea of using count statistics on the input tuples to navigate the space of query answers that had also been used in prior work on sampling [START_REF] Zhao | Random Sampling over Joins Revisited[END_REF], it decouples it from the random order requirement and advances it into direct access. The separation into a direct access component and a random permutation (of indices) generated externally also allows sampling without replacement which was not possible before. This direct access algorithm is also a significant simplification over a prior one by Brault-Baron [START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF]. We emphasize that even though these algorithms do not explicitly discuss the order of the answers, a closer look shows that they internally use and produce some lexicographic order.

Theorem 2.4 ( [START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF][START_REF] Carmeli | Answering (Unions of) Conjunctive Queries Using Random Access and Random-Order Enumeration[END_REF]). Let 𝑄 be a CQ. If 𝑄 is free-connex, then direct access (in some order) is possible in ⟨𝑛 log 𝑛, log 𝑛⟩. Otherwise, if it is also self-join-free, then direct access (in any order) is not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩, assuming sparseBMM and Hyperclique. [START_REF] Keppeler | Answering Conjunctive Queries and FO+MOD Queries under Updates[END_REF] suggests another direct-access solution by lexicographic order, which also supports efficient insertion and deletion of input tuples. Given these additional requirements, the supported CQs are more limited, and are only a subset of free-connex CQs called 𝑞-hierarchical [START_REF] Berkholz | Answering Conjunctive Queries under Updates[END_REF]. This is a subclass of the well-known hierarchical queries with an additional restriction on the existential variables. As an example, the following CQs are not 𝑞-hierarchical even though they are free-connex: 𝑄 1 (𝑥, 𝑦) :-𝑅 1 (𝑥), 𝑅 2 (𝑥, 𝑦), 𝑅 3 (𝑦) and 𝑄 2 (𝑥) :-𝑅 1 (𝑥, 𝑦), 𝑅 2 (𝑦). For these queries, direct access is not supported by the solution of Keppeler [START_REF] Keppeler | Answering Conjunctive Queries and FO+MOD Queries under Updates[END_REF], even though it is possible without the update requirements (as we show in Section 3).

Recent work by Keppeler

All previous direct-access solutions of which we are aware have two gaps compared to this work: (1) they do not discuss which lexicographic orders (given by orderings of the free variables) are supported; (2) they do not support all possible lexicographic orders. We conclude this section with a short survey of existing solutions and their supported orders.

All prior direct-access solutions use some component that depends on the query structure and constrains the supported orders. The algorithm of Carmeli et al. [START_REF] Carmeli | Answering (Unions of) Conjunctive Queries Using Random Access and Random-Order Enumeration[END_REF]Algorithm 3] assumes that a join tree is given with the CQ, and the lexicographic order is imposed by the join tree. Specifically, it is an ordering of the variables achieved by a preorder depth-first traversal of the tree. As a result, it does not support any order that requires jumping back-and-forth between different branches of the tree. In particular, it does not support 𝑄 3 (𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 ) :-𝑅(𝑣 1 , 𝑣 3 ), 𝑆 (𝑣 2 , 𝑣 4 ) with the lexicographic order given by the increasing variable indices (we adopt this convention for all the examples below). We show how to handle this CQ and order in detail in Example 3.5. The algorithm of Brault-Baron [START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF]Algorithm 4.3] assumes that an elimination order is given along with the CQ. The resulting lexicographic order is affected by that elimination order, but is not exactly the same. This solution suffers from similar restrictions, and it does not support 𝑄 3 either. The algorithm of Keppeler [START_REF] Keppeler | Answering Conjunctive Queries and FO+MOD Queries under Updates[END_REF] assumes that a 𝑞-tree is given with the CQ, and the possible lexicographic orders are affected by this tree. Unlike the two earlier mentioned approaches, this algorithm can interleave variables from different atoms, yet cannot support some orders that are possible for the previous algorithms. As an example, it does not support 𝑄 4 (𝑣 1 , 𝑣 2 , 𝑣 3 ) :-𝑅 1 (𝑣 1 , 𝑣 2 ), 𝑅 2 (𝑣 2 , 𝑣 3 ) as 𝑣 2 is highest in the hierarchy (the atoms containing it strictly subsume the atoms containing any other variable) and so it is necessarily the first variable in the q-tree and in the ordering produced.

Finally, we should mention that there exist queries and orders that require both jumping back-and-forth in the join tree and visiting the variables in an order different than any hierarchy. As a result, these are not supported by any previous solution. Two such examples are

𝑄 5 (𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 , 𝑣 5 ) :-𝑅 1 (𝑣 1 , 𝑣 3 ), 𝑅 2 (𝑣 3 , 𝑣 4 ), 𝑅 3 (𝑣 2 , 𝑣 5 ) and 𝑄 6 (𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 , 𝑣 5 ) :-𝑅 1 (𝑣 1 , 𝑣 2 , 𝑣 4 ), 𝑅 2 (𝑣 2 , 𝑣 3 , 𝑣 5 ).
In Section 3, we provide an algorithm that supports both of these CQs.

DIRECT ACCESS BY LEXICOGRAPHIC ORDERS

In this section, we answer the following question: for which underlying lexicographic orders can we achieve "tractable" direct access to ranked CQ answers, i.e. with quasilinear preprocessing and polylogarithmic time per answer? Example 3.1 (No direct access). Consider the lexicographic order 𝐿 = ⟨𝑣 1 , 𝑣 2 , 𝑣 3 ⟩ for the query 𝑄 (𝑣 1 , 𝑣 2 , 𝑣 3 ) :-𝑅(𝑣 1 , 𝑣 3 ), 𝑆 (𝑣 3 , 𝑣 2 ). Direct access to the query answers according to that order would allow us to "jump over" the 𝑣 3 values via binary search and essentially enumerate the answers to 𝑄 ′ (𝑣 1 , 𝑣 2 ) :-𝑅(𝑣 1 , 𝑣 3 ), 𝑆 (𝑣 3 , 𝑣 2 ). However, we know that 𝑄 ′ is not free-connex and that is impossible to achieve enumeration with quasilinear preprocessing and polylogarithmic delay (if sparseBMM holds). Therefore, the bounds we are hoping for are out of reach for the given query and order. The core difficulty is that the joining variable 𝑣 3 appears after the other two in the lexicographic order.

We formalize this notion of "variable in the middle" in order to detect similar situations in more complex queries.

Definition 3.2 (Disruptive Trio).

Let 𝑄 be a CQ and 𝐿 a lexicographic order of its free variables. We say that three free variables 𝑣 1 , 𝑣 2 , 𝑣 3 are a disruptive trio in 𝑄 with respect to 𝐿 if 𝑣 1 and 𝑣 2 are not neighbors (i.e. they do not appear together in an atom), 𝑣 3 is a neighbor of both 𝑣 1 and 𝑣 2 , and 𝑣 3 appears after 𝑣 1 and 𝑣 2 in 𝐿.

As it turns out, for free-connex and self-join-free CQs, the tractable CQs are precisely captured by this simple criterion. Regarding self-join-free CQs that are not free-connex, their known intractability of enumeration implies that direct access is also intractable. This leads to the following dichotomy: Theorem 3.3 (Direct Access by LEX). Let 𝑄 be a CQ and 𝐿 be a lexicographic order.

• If 𝑄 is free-connex and does not have a disruptive trio with respect to 𝐿, then direct access by 𝐿 is possible in ⟨𝑛 log 𝑛, log 𝑛⟩. • Otherwise, if 𝑄 is also self-join-free, then direct access by 𝐿 is not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩ assuming sparseBMM and Hyperclique.

, Vol. 1, No. 1, Article . Publication date: November 2023.

Remark 1. Assume we are given a full CQ, and the lexicographic order we want to achieve is ⟨𝑣 1 , . . . , 𝑣 𝑚 ⟩. It was shown (in the context of ranked enumeration by lexicographic orders) that the absence of disruptive trios is equivalent to the existence of a reverse (𝛼-)elimination order of the variables [START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF]Theorem 15]. That is, we need there to exist an atom that contains 𝑣 𝑚 and all of its neighbors (variables that share an atom with 𝑣 𝑚 ), and if we remove 𝑣 𝑚 from the query, 𝑣 1 , . . . , 𝑣 𝑚-1 should recursively be a reverse elimination order. For the base case, when 𝑚 = 1, 𝑣 1 constitutes a reverse-elimination order.

Remark 2. On the positive side of Theorem 3.3, the preprocessing time is dominated by sorting the input relations, which we assume requires O (𝑛 log 𝑛) time. If we assume instead that sorting takes linear time (as assumed in some related work [START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF][START_REF] Carmeli | Answering (Unions of) Conjunctive Queries Using Random Access and Random-Order Enumeration[END_REF][START_REF] Grandjean | Sorting, linear time and the satisfiability problem[END_REF]), then the time required for preprocessing is only O (𝑛) instead of O (𝑛 log 𝑛).

In Section 3.1, we provide an algorithm for this problem for full acyclic CQs that have a particular join tree that we call layered. Then, we show how to find such a layered join tree whenever there is no disruptive trio in Section 3.2. In Section 3.3, we explain how to adapt our solution for CQs with projections, and in Section 3.4 we prove a lower bound which establishes that our algorithm applies to all cases where direct access is tractable.

Layer-Based Algorithm

Before we explain the algorithm, we first define one of its main components. A layered join tree is a join tree where each node belongs to a layer. The layer number is the last position of any of its variables in the lexicographic order. Intuitively, "peeling" off the outermost (largest) layers must result in a valid join tree (for a hypergraph with fewer variables). To find such a join tree for a CQ 𝑄, we may have to introduce hyperedges that are contained in those of H (𝑄) (this corresponds to taking the projection of a relation) or remove hyperedges of H (𝑄) that are contained in others (this corresponds to filtering relations that contain a superset of the variables). Thus, we define the layered join tree with respect to a hypergraph that is inclusion equivalent (recall the definition of an inclusion equivalent hypergraph from Section 2.1). Definition 3.4 (Layered Join Tree). Let 𝑄 be a full acyclic CQ, and let 𝐿 = ⟨𝑣 1 , . . . , 𝑣 𝑓 ⟩ be a lexicographic order. A layered join tree for 𝑄 with respect to 𝐿 is a join tree of a hypergraph that is inclusion equivalent to H (𝑄) where (1) every node 𝑉 of the tree is assigned to layer max{𝑖 | 𝑣 𝑖 ∈ 𝑉 }, (2) there is exactly one node for each layer, and (3) for all 𝑗 ≤ 𝑓 the induced subgraph with only the nodes that belong to the first 𝑗 layers is a tree.

Example 3.5. Consider the CQ 𝑄 3 (𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 ) :-𝑅(𝑣 1 , 𝑣 3 ), 𝑆 (𝑣 2 , 𝑣 4 ) and the lexicographic order ⟨𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 ⟩. To support that order, we first find an inclusion equivalent hypergraph, shown in Figure 3a. Notice that we added two hyperedges that are strictly contained in the existing ones, and obtained a hypergraph corresponding to 𝑅(𝑣 1 , 𝑣 3 ), 𝑅 ′ (𝑣 1 ), 𝑆 (𝑣 2 , 𝑣 4 ), 𝑆 ′ (𝑣 2 ). A layered join tree constructed from that hypergraph is depicted in Figure 3b. There are four layers, one for each node of the join tree. The layer of the node containing {𝑣 1 , 𝑣 3 } is 3 because 𝑣 3 appears after 𝑣 1 in the order and it is the third variable. If we remove the last layer, then we obtain a layered join tree for the induced hypergraph where the last variable 𝑣 4 is removed.

We now describe an algorithm that takes as an input a CQ 𝑄, a lexicographic order 𝐿, and a corresponding layered join tree and provides direct access to the query answers after a preprocessing phase. For preprocessing, we leverage a construction from Carmeli et al. [START_REF] Carmeli | Answering (Unions of) Conjunctive Queries Using Random Access and Random-Order Enumeration[END_REF]Algorithm 2] and apply it to our layered join tree. For completeness, we briefly explain how it works below. Subsequently, we describe the access phase that takes into account the layers of the tree to accommodate the , Vol.

3 (𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 ) :-𝑅(𝑣 1 , 𝑣 3 ), 𝑆 (𝑣 2 , 𝑣 4 ) and order ⟨𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 ⟩.
provided lexicographic order. We emphasize that the way we access the structure is different than that of the past work [START_REF] Carmeli | Answering (Unions of) Conjunctive Queries Using Random Access and Random-Order Enumeration[END_REF], and that this allows support of lexicographic orders that were impossible for the previous access routine (e.g. the order in Example 3.5).

Preprocessing. The preprocessing phase (1) creates a relation for every node of the tree, (2) removes dangling tuples, (3) sorts the relations, (4) partitions the relations into buckets, and ( 5) uses dynamic programming on the tree to compute and store certain counts 10 . After preprocessing, we are guaranteed that for all 𝑖, the node of layer 𝑖 has a corresponding relation where each tuple participates in at least one query answer; this relation is partitioned into buckets by the assignment of the variables preceding 𝑖. In each bucket, we sort the tuples lexicographically by 𝑣 𝑖 . Each tuple is given a weight that indicates the number of different answers this tuple agrees with when only joining its subtree. The weight of each bucket is the sum of its tuple weights. We denote both by the function weight. Moreover, for every tuple 𝑡, we compute the sum of weights of the preceding tuples in the bucket, denoted by start(𝑡). We use end(𝑡) for the sum that corresponds to the tuple following 𝑡 in the same bucket; if 𝑡 is last, we set this to be the bucket weight. If we think of the query answers in the subtree sorted in the order of 𝑣 𝑖 values, then start and end distribute the indices between 0 and the bucket weight to tuples. The number of indices within the range of each tuple corresponds to its weight.

Example 3.6 (Continued). The result of the preprocessing phase on an example database for our query 𝑄 3 is shown in Figure 4. Notice that 𝑅 has been split into two buckets according to the values of its parent 𝑅 ′ , one for value 𝑎 1 and one for 𝑎 2 . For tuple (𝑎 1 ) ∈ 𝑅 ′ , we have weight((𝑎 1 )) = 8 because this is the number of answers that agree on that value in its subtree: the left subtree has 2 such answers which can be combined with any of the 4 possible answers of the right subtree. The start index of tuple (𝑏 1 , 𝑑 3 ) ∈ 𝑆 is the sum of the previous weights within the bucket:

start((𝑏 1 , 𝑑 3 )) = weight((𝑏 1 , 𝑑 1 )) + weight((𝑏 1 , 𝑑 2 )) = 1 + 1 = 2.
Not shown in the figure is that every bucket stores the sum of weights it contains.

Access. The access phase works by going through the tree layer by layer. When resolving a layer 𝑖, we select a tuple from its corresponding relation, which sets a value for the 𝑖 th variable in 𝐿, and also determines a bucket for each child. Then, we conceptually erase the node of layer 𝑖 and its outgoing edges.

𝑅

𝑤 𝑠 e The access algorithm maintains a directed forest and an assignment to a prefix of the variables. Each tree in the forest represents the answers obtained by joining its relations. Each root contains a single bucket that agrees with the already assigned values, thus every answer agrees on the prefix. Due to the running intersection property, different trees cannot share unassigned variables. As a consequence, any combination of answers from different trees can be added to the prefix assignment to form an answer to 𝑄. The answers obtained this way are exactly the answers to 𝑄 that agree with the already set assignment. Since we start with a layered join tree, we are guaranteed that at each step, the next layer (which corresponds to the variable following the prefix for which we have an assignment) appears as a root in the forest.

𝑎 1 𝑐 1 1 0 1 𝑎 1 𝑐 2 1 1 2 𝑎 2 𝑐 2 1 0 1 𝑎 2 𝑐 3 1 1 2 𝑆 𝑤 𝑠 e 𝑏 1 𝑑 1 1 0 1 𝑏 1 𝑑 2 1 1 2 𝑏 1 𝑑 3 1 2 3 𝑏 2 𝑑 4 1 0 1
Recall that from the preprocessing phase, the weight of each root is the number of answers in its tree. When we are at layer 𝑖, we have to take into account the weights of all the other roots in order to compute the number of query answers for a particular tuple. More specifically, the number of answers to 𝑄 containing the already selected attributes (smaller than 𝑖) and some 𝑣 𝑖 value contained in a tuple is found by multiplying the tuple weight with the weights of all other roots. That is because the answers from all trees can be combined into a query answer. Let 𝑡 be the selected tuple when resolving the 𝑖 th layer. The number of answers to 𝑄 that have a value of 𝐿[𝑖] smaller than that of 𝑡 and a value of 𝐿[ 𝑗] equal to that of 𝑡 for all 𝑗 < 𝑖 is then:

𝑡 ′ weight(𝑡 ′ ) 𝑟 ∈roots weight(𝑟 )
where 𝑡 ′ ranges over tuples preceding 𝑡 in its bucket. Denote by factor the product of all root weights. Then we can rewrite as:

𝑡 ′ weight(𝑡 ′ ) 𝑟 ∈roots weight(𝑟 ) = start(𝑡) • factor .
Therefore, when resolving layer 𝑖 we select the last tuple 𝑡 such that the index we want to access is at least start(𝑡) • factor.

Algorithm 1 summarizes the process we described where 𝑘 is the index to be accessed and 𝑓 is the number of variables. Iteration 𝑖 resolves layer 𝑖. Pointers to the selected buckets from the roots are kept in a bucket array. The product of the weights of all roots is kept in a factor variable. In each iteration, the variable 𝑘 is updated to the index that should be accessed among the answers that agree with the already selected attribute values. Note that bucket[𝑖] is always initialized when accessed since layer 𝑖 is guaranteed to be a child of a smaller layer.

Example 3.7 (Continued). We demonstrate how the access algorithm works for index 𝑘 = 12. When resolving 𝑅 ′ , the tuple (𝑎 2 ) is chosen since 8 • 1 ≤ 12 < 16 • 1; then, the single bucket in 𝑆 ′ and the bucket containing 𝑎 2 in 𝑅 are selected. The next iteration resolves 𝑆 ′ . When it reaches 13 return the answer agreeing with the selected tuples line 7, 𝑘 = 12 -8 = 4 and factor = 2. As 0 • 2 ≤ 4 < 3 • 2, the tuple (𝑏 1 ) is selected. Next, 𝑅 is resolved, which we depict in Figure 5. The current index is 𝑘 = 4 -0 = 4. The weights of the other roots (only 𝑆 here) gives us factor = 3. To make our choice in 𝑅, we multiply the weights of the tuples by factor = 3. Then, we find that the index 𝑘 we are looking for falls into the range of (𝑎 2 , 𝑐 3 ) because 1

• 3 ≤ 4 < 2 • 3. Next, 𝑆 is resolved, 𝑘 = 4 -1 • 3 = 1, and factor = 1. As 1 • 1 ≤ 1 < 2 • 1, the tuple (𝑏 1 , 𝑑 2 ) is selected. Overall, answer number 12 (the 13 th answer) is (𝑎 2 , 𝑏 1 , 𝑐 3 , 𝑑 2 ).
Lemma 3.8. Let 𝑄 be a full acyclic CQ, and 𝐿 = ⟨𝑣 1 , . . . , 𝑣 𝑓 ⟩ be a lexicographic order. If there is a layered join tree for 𝑄 with respect to 𝐿, then direct access is possible in ⟨𝑛 log 𝑛, log 𝑛⟩.

Proof. The correctness of Algorithm 1 follows from the discussion above. For the time complexity, note that it contains a constant number of operations (assuming the number of attributes 𝑓 is fixed). Line 7 can be done in logarithmic time using binary search, while all other operations only require constant time in the RAM model. Thus, we obtain direct access in logarithmic time per answer after the quasilinear preprocessing (dominated by sorting). □ Remark 3 (Inverted access). A straightforward adaptation of Algorithm 1 can be used to achieve inverted access: given a query result as the input, we return its index according to the lexicographic order. Algorithm 2 is almost the same as Algorithm 1 except that the choices in each iteration are made according to the given answer and the corresponding index is constructed (instead of the opposite). The algorithm runs in constant time per answer since every operation can be done within that time (unlike Algorithm 1, there is no need for binary search here).

Another adaptation of Algorithm 2 can give us a form of inverted access for the cases when the given answer does not exist. That is, instead of returning "not-an-answer", we want to return the next answer in the lexicographic order. The first time a tuple 𝑡 is not found in Line 7 of Algorithm 2, we select the first tuple in the bucket that is larger than 𝑡, and in all following iterations, we always select the first tuple in the bucket. If there is no tuple larger than 𝑡, we revert the previous iteration and select the next tuple there (compared to what we selected before). If no such tuple exists, we again revert the previous iteration and so on. If there are no previous iterations, we were asked to access a tuple larger than the last answer, so we return an appropriate message. The algorithm described here takes logarithmic time, as we can use binary search to find the tuple following our target tuple in each bucket.

𝑏 2 1 3 𝑅 𝑤 𝑠 𝑎 1 𝑐 1 1 0 𝑎 1 𝑐 2 1 1 𝑎 2 𝑐 2 1 0 𝑎 2 𝑐 3 1 1 𝑆 𝑤 𝑠 𝑏 1 𝑑 1 1 0 𝑏 1 𝑑 2 1 1 𝑏 1 𝑑 3 1 2 𝑏 2 𝑑 4 1 0 Weight of bucket = 1 + 1 + 1 = 3 1 * 3 answers 1 * 3 answers 𝑘 = 4

Finding Layered Join Trees

We now have an algorithm that can be applied whenever we have a layered join tree. We next show that the existence of such a join tree relies on the disruptive trio condition we introduced earlier. In particular, if no disruptive trio exists, we are able to construct a layered join tree for full acyclic CQs. Lemma 3.9. Let 𝑄 be a full acyclic CQ, and 𝐿 be a lexicographic order. If 𝑄 does not have a disruptive trio with respect to 𝐿, then there is a layered join tree for 𝑄 with respect to 𝐿.

Proof. We show by induction on 𝑖 that there exists a layered join tree for the hypergraph containing the hyperedges {𝑉 ∩ {𝑣 1 , . . . , 𝑣 𝑖 } | 𝑉 ∈ atoms(𝑄)} with respect to the prefix of 𝐿 containing its first 𝑖 elements. The induction base is the tree that contains the node {𝑣 1 } and no edges.

In the inductive step, we assume a layered join tree with 𝑖 -1 layers for {𝑉 ∩ {𝑣 1 , . . . , 𝑣 𝑖-1 } | 𝑉 ∈ atoms(𝑄)}, and we build a layer on top of it. Denote by V the sets of {𝑉 ∩ {𝑣 1 , . . . , 𝑣 𝑖 } | 𝑉 ∈ atoms(𝑄)} that contain 𝑣 𝑖 (these are the sets that need to be included in the new layer). First note that V is acyclic. Indeed, by the running intersection property, the join tree for H (𝑄) has a subtree with all the nodes that contain 𝑣 𝑖 . By taking this subtree and projecting out all variables that occur after 𝑣 𝑖 in 𝐿, we get a join tree for an inclusion equivalent hypergraph to V, and its existence proves that V is acyclic.

We next claim that some set in V contains all the others; that is, there exists 𝑉 𝑚 ∈ V such that for all 𝑉 ∈ V, we have that 𝑉 ⊆ 𝑉 𝑚 . Consider a join tree for V. Every variable 𝑣 ∈ V defines a subtree 𝑇 𝑣 induced by the nodes that contain this variable. If two variables 𝑥, 𝑦 are neighbors, their subtrees 𝑇 𝑥 ,𝑇 𝑦 share a node. It is known that every collection of subtrees of a tree satisfies the Helly property [START_REF] Charles | Algorithmic Graph Theory and Perfect Graphs[END_REF]: if every two subtrees share a node, then some node is shared by all subtrees. In particular, since V is acyclic, if every two variables of V are neighbors, then some element of V contains all variables that appear in (elements of) V. Thus, if, by way of contradiction, there is no such 𝑉 𝑚 , there exist two non-neighboring variables 𝑣 𝑎 and 𝑣 𝑏 that appear in (elements of) V. Since 𝑣 𝑖 appears in all elements of V, this means that there exist 𝑉 𝑎 , 𝑉 𝑏 ∈ V with {𝑣 𝑎 , 𝑣 𝑖 } ⊆ 𝑉 𝑎 and {𝑣 𝑏 , 𝑣 𝑖 } ⊆ 𝑉 𝑏 . Since 𝑣 𝑎 and 𝑣 𝑏 are not neighbors, these three variables are a disruptive trio with respect to 𝐿: 𝑣 𝑎 and 𝑣 𝑏 are both neighbors of the later variable 𝑣 𝑖 . The existence of a disruptive trio contradicts the assumption of the lemma we are proving, and so we conclude that there is 𝑉 𝑚 ∈ V such that for all 𝑉 ∈ V, we have that 𝑉 ⊆ 𝑉 𝑚 .

With 𝑉 𝑚 at hand, we can now add the additional layer to the tree given by the inductive hypothesis. By the inductive hypothesis, the layered join tree with 𝑖 -1 layers contains the hyperedge 𝑉 𝑚 ∩ {𝑣 1 , . . . , 𝑣 𝑖-1 } = 𝑉 𝑚 \ {𝑣 𝑖 }. We insert 𝑉 𝑚 with an edge to the node containing 𝑉 𝑚 \ {𝑣 𝑖 }. This results in the join tree we need: (1) the hyperedges {𝑉 ∩ {𝑣 1 , . . . , 𝑣 𝑖 } | 𝑉 ∈ atoms(𝑄)} are all contained in nodes, since the ones that do not appear in the tree from the inductive hypothesis are contained in the new node; (2) it is a tree since we add one leaf to an existing tree; and (3) the running intersection property holds since the added node is connected to all of its variables that already appear in the tree. □ Lemmas 3.8 and 3.9 give a direct-access algorithm for full acyclic CQs and lexicographic orders without disruptive trios.

Supporting Projection

Next, we show how to support CQs that have projections. A free-connex CQ can be efficiently reduced to a full acyclic CQ using Proposition 2.3. We next show that the resulting CQ contains no disruptive trio if the original CQ does not. Lemma 3.10. Given a database instance 𝐼 , a free-connex CQ 𝑄, and a lexicographic order 𝐿 with no disruptive trio with respect to 𝐿, we can compute in linear time a database instance 𝐼 ′ and a full acyclic CQ 𝑄 ′ with no disruptive trio with respect to 𝐿 such that 𝑄 ′ (𝐼 ′ ) = 𝑄 (𝐼 ), |𝐼 ′ | ≤ |𝐼 |, and 𝑄 ′ does not depend on 𝐼 or 𝐼 ′ .

Proof. Let 𝑄 be a free-connex CQ, and let 𝑇 be an ext-free(𝑄)-connex tree for 𝑄 where 𝑇 ′ is the subtree of 𝑇 that contains exactly the free variables.

First, we claim that two free variables are neighbors in 𝑇 iff they are neighbors in 𝑇 ′ . The "if" direction is immediate since 𝑇 ′ is contained in 𝑇 . We show the other direction. Let 𝑢 and 𝑣 be free variables of 𝑄 that are neighbors in 𝑇 . That is, there is a node 𝑉 𝑇 in 𝑇 that contains them both. Consider the unique path from 𝑉 to any node in 𝑇 ′ such that only the last node on the path, which we denote 𝑉 𝑇 ′ , is in 𝑇 ′ . Since both variables appear in 𝑇 ′ and in 𝑉 , by the running intersection property, both variables appear in 𝑉 𝑇 ′ . Thus, 𝑢 and 𝑣 are also neighbors in 𝑇 ′ .

Since the definition of disruptive trios depends only on neighboring pairs of free variables, an immediate consequence of the claim from the previous paragraph is that there is a disruptive trio in 𝑇 iff there is a disruptive trio in 𝑇 ′ . Next, we can simply use Proposition 2.3 to reduce 𝑄 to the full acyclic CQ where the atoms are exactly the nodes of 𝑇 ′ . □ By combining Lemmas 3.8 to 3.10, we conclude an efficient algorithm for free-connex CQs and orders with no disruptive trios. The next lemma summarizes our results so far. Lemma 3.11. Let 𝑄 be a free-connex CQ, and 𝐿 be a lexicographic order. If 𝑄 does not have a disruptive trio with respect to 𝐿, direct access by 𝐿 is possible in ⟨𝑛 log 𝑛, log 𝑛⟩.

Lower Bound for Conjunctive Queries

Next, we show that our algorithm supports all tractable cases (for self-join-free CQs); we prove that all unsupported cases are intractable. We base our hardness results on the known hardness of enumeration for non-free-connex CQs [START_REF] Bagan | Computing the jth solution of a firstorder query[END_REF][START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF] through a reduction that uses direct access to enumerate the answers projected on a prefix of the variables. Adapting our notation to enumeration, we say that an enumeration problem is in ⟨𝑝 (𝑛), 𝑓 (𝑛)⟩ if there is an algorithm that solves this problem with 𝑝 (𝑛) time before the first answer and 𝑓 (𝑛) time between consecutive answers. Lemma 3.12. Let 𝑄 be a self-join-free CQ, 𝐿 be a lexicographic order, and 𝑄 ′ be the same as 𝑄 but with free variables 𝐿 ′ for some prefix 𝐿 ′ of 𝐿. If direct access for 𝑄 by 𝐿 is possible in ⟨𝑝 (𝑛), 𝑓 (𝑛)⟩ for some functions 𝑝, 𝑓 , then enumeration of the answers to 𝑄 ′ is possible in ⟨𝑝 (𝑛), 𝑓 (𝑛) log 𝑛⟩.

Proof. We show how to enumerate the unique assignments of the free variables of 𝑄 ′ given the direct access algorithm for 𝑄. First we perform the preprocessing step in O (𝑝 (𝑛)). Then, we perform the following starting with 𝑖 = 0 and until there are no more answers. We access the answer at index 𝑖 and print its assignment to the variables 𝐿 ′ . Then, we set 𝑖 to be the index of the next answer which assigns different values to 𝐿 ′ and repeat. Finding the next index can be done with a logarithmic number of direct access calls using binary search.

□

We now exploit that for CQs with disruptive trios, we can always find a prefix that is not connex. Therefore, enumerating the query answers projected on that prefix via direct access leads to the enumeration of a non-free-connex CQ, where existing lower bounds apply. Lemma 3.13. Let 𝑄 be a self-join-free acyclic CQ, and 𝐿 be a lexicographic order. If 𝑄 has a disruptive trio with respect to 𝐿, then direct access by 𝐿 is not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩, assuming sparseBMM. 11 Proof. Let 𝑣 1 , 𝑣 2 , 𝑣 3 be a disruptive trio in 𝐿. We take 𝐿 ′ to be the prefix of 𝐿 that ends in 𝑣 2 . Then, 𝑣 1 , 𝑣 3 , 𝑣 2 is an 𝐿 ′ -path or in other words, the hypergraph of 𝑄 is not 𝐿 ′ -connex. Now, we define a new CQ 𝑄 ′ so that it has the same body as 𝑄 but its free variables are 𝐿 ′ . Thus, 𝑄 ′ is acyclic but not free-connex. Assuming that direct access for 𝑄 is possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩, we use Lemma 3.12 to enumerate the answers of 𝑄 ′ in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩, which is known to contradict sparseBMM [START_REF] Bagan | Computing the jth solution of a firstorder query[END_REF] .

□ 11 In fact, this lemma holds also for cyclic CQs, as it can be shown that Boolean matrix multiplication can be encoded in any CQ that contains a free-path regardless of its acyclicity. However, this is not formally stated in previous work, and we prefer not to complicate the proof with the technical details of the reduction. We chose here to limit the statement to acyclic CQs as cyclic CQs are already known to be hard if we assume Hypercliqe. The direct reduction that applies also to cyclic CQs can be found in the conference version of this article [START_REF] Carmeli | Tractable Orders for Direct Access to Ranked Answers of Conjunctive Queries[END_REF].

, Vol. 1, No. 1, Article . Publication date: November 2023.

By combining Lemma 3.11 and Lemma 3.13 together with the known hardness results for nonfree-connex CQs (Theorem 2.4), we prove the dichotomy given in Theorem 3.3: direct access by a lexicographic order for a self-join-free CQ is possible with quasilinear preprocessing and polylogarithmic time per answer if and only if the query is free-connex and does not have a disruptive trio with respect to the required order.

DIRECT ACCESS BY PARTIAL LEXICOGRAPHIC ORDERS

We now investigate the case where the desired lexicographic order is partial, i.e., it contains only some of the free variables. This means that there is no particular order requirement for the rest of the variables. One way to achieve direct access to a partial order is to complete it into a full lexicographic order and then leverage the results of the previous section. If such completion is impossible, we have to consider cases where tie-breaking between the non-ordered variables is done in an arbitrary way. However, we will show in this section that the tractable partial orders are precisely those that can be completed into a full lexicographic order. In particular, we will prove the following dichotomy which also gives an easy-to-detect criterion for the tractability of direct access.

Theorem 4.1. Let 𝑄 be a CQ and 𝐿 be a partial lexicographic order.

• If 𝑄 is free-connex and 𝐿-connex and does not have a disruptive trio with respect to 𝐿, then direct access by 𝐿 is possible in ⟨𝑛 log 𝑛, log 𝑛⟩. • Otherwise, if 𝑄 is also self-join-free, then direct access by 𝐿 is not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩, assuming sparseBMM and Hyperclique.

Example 4.2. Consider the CQ 𝑄 :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧). If the free variables are exactly 𝑥 and 𝑧, then the query is not free-connex, and so it is intractable. Next assume that all variables are free. If 𝐿 = ⟨𝑥, 𝑧⟩, then the query is not 𝐿-connex, and so it is intractable. If 𝐿 = ⟨𝑥, 𝑧, 𝑦⟩, then 𝑥, 𝑧, 𝑦 is a disruptive trio, thus the query is intractable. However, if 𝐿 = ⟨𝑥, 𝑦, 𝑧⟩ or 𝐿 = ⟨𝑧, 𝑦⟩, then the query is free-connex, 𝐿-connex and has no disruptive trio, so it is tractable.

Tractable Cases

For the positive side, we can solve our problem efficiently if the CQ is free-connex and there is a completion of the lexicographic order to all free variables with no disruptive trio. Lemma 4.4 identifies these cases with a connexity criterion. To prove it, we first need a way to combine two different connexity properties. The proof of the following proposition uses ideas from a proof of the characterization of free-connex CQs in terms of the acyclicity of the hypergraph obtained by including a hyperedge with the free variables [START_REF] Berkholz | Constant delay enumeration for conjunctive queries: a tutorial[END_REF]. Proposition 4.3. If a CQ 𝑄 is both 𝐿 1 -connex and 𝐿 2 -connex where 𝐿 2 ⊆ 𝐿 1 , then there exists a join tree 𝑇 of an inclusive extension of 𝑄 with a subtree 𝑇 1 containing exactly the variables 𝐿 1 and a subtree 𝑇 2 of 𝑇 1 contains exactly the variables 𝐿 2 .

Proof. We describe a construction of the required tree. Figure 6 demonstrates our construction. We use two different characterizations of connexity. Since 𝑄 is 𝐿 2 -connex, it has an ext-𝐿 2 -connex tree 𝑇 2 . Since 𝑄 is 𝐿 1 -connex, there is a join-tree 𝑇 1 for the atoms of 𝑄 and its head. Let 𝑇 2 [𝐿 1 ] be 𝑇 2 where the variables that are not in 𝐿 1 are deleted from all nodes. That is, for every node 𝑉 ∈ 𝑇 2 , its variables are replaced with var(𝑉 ) ∩ 𝐿 1 . Denote by V all neighbors of the head in 𝑇 1 , and denote by 𝑇 - 1 the graph 𝑇 1 after the deletion of the head node. want. Such a node exists in 𝑇 2 [𝐿 1 ] since every node of 𝑇 - 1 represents an atom of 𝑄, and every atom of 𝑄 is contained in some node of 𝑇 2 . The subtree 𝑇 2 [𝐿 1 ] contains exactly 𝑉 1 , and since this subtree comes from an ext-𝐿 2 -connex tree, it has a subtree containing exactly 𝐿 1 . It is easy to verify that the result is a tree, and we can show that the running intersection property holds in the united graph since it holds for 𝑇 1 and 𝑇 2 . □

We are now in a position to show the following: Lemma 4.4. Let 𝑄 be a CQ and 𝐿 be a partial lexicographic order. If 𝑄 is free-connex and 𝐿-connex and does not have a disruptive trio with respect to 𝐿, then there is an ordering 𝐿 + of free(𝑄) that starts with 𝐿 such that 𝑄 has no disruptive trio with respect to 𝐿 + .

Proof. According to Proposition 4.3, there is a join tree 𝑇 (of an inclusive extension of 𝑄) with a subtree 𝑇 free containing exactly the free variables, and a subtree 𝑇 𝐿 of 𝑇 free containing exactly the 𝐿 variables. We assume that 𝑇 𝐿 contains at least one node; otherwise (this can only happen in case 𝐿 is empty), we can introduce a node with no variables to all of 𝑇 , 𝑇 free and 𝑇 𝐿 and connect it to any one node of 𝑇 free . We describe a process of extending 𝐿 while traversing 𝑇 free . Consider the nodes of 𝑇 𝐿 as handled, and initialize 𝐿 + = 𝐿. Then, repeatedly handle a neighbor of a handled node until all nodes are handled. When handling a node, append to 𝐿 + all of its free variables that are not already there. Since the join tree is connected and includes all query variables, 𝐿 + will necessarily contain free(𝑄) at the end of the process. We prove by induction that 𝑄 has no disruptive trio w.r.t any prefix of 𝐿 + . The base case is guaranteed by the premises of this lemma since 𝐿 (hence all of its prefixes) has no disruptive trio.

Let 𝑣 𝑝 be a new variable added to a prefix 𝑣 1 , . . . , 𝑣 𝑝-1 of 𝐿 + . Let 𝑇 + be the subtree of 𝑇 free with the handled nodes when adding 𝑣 𝑝 to 𝐿 + and let 𝑉 ∉ 𝑇 + be the node being handled. Note that, since 𝑣 𝑝 is being added, 𝑣 𝑝 ∈ 𝑉 but 𝑣 𝑝 is not in any node of 𝑇 + .

We first claim that every neighbor 𝑣 𝑖 of 𝑣 𝑝 with 𝑖 < 𝑝 is in 𝑉 . Our arguments are illustrated in Figure 7. Since 𝑣 𝑖 and 𝑣 𝑝 are neighbors, they appear together in a node 𝑉 𝑖,𝑝 outside of 𝑇 + . Let 𝑉 𝑖 be a node in 𝑇 + containing 𝑣 𝑖 (such a node exists since 𝑣 𝑖 appears before 𝑣 𝑝 in 𝐿 + ). Consider the path from 𝑉 𝑖,𝑝 to 𝑉 𝑖 . Let 𝑉 ℓ be the last node of this path not in 𝑇 + . If 𝑉 ℓ ≠ 𝑉 , the path between 𝑉 ℓ and 𝑉 goes only through nodes of 𝑇 + (except for the end-points). Thus, concatenating the path from 𝑉 𝑖,𝑝 to 𝑉 ℓ with the path from 𝑉 ℓ to 𝑉 results in a simple path. By the running intersection property, all nodes on this path contain 𝑣 𝑝 . In particular, the node following 𝑉 ℓ contains 𝑣 𝑝 in contradiction to the fact that 𝑣 𝑝 does not appear in 𝑇 + . Therefore, 𝑉 ℓ = 𝑉 . By the running intersection property, since 𝑉 is on the path between 𝑉 𝑖 and 𝑉 𝑖,𝑝 , we have that 𝑉 contains 𝑣 𝑖 .

We now prove the induction step. We know by the inductive hypothesis that 𝑣 1 , . . . , 𝑣 𝑝-1 have no disruptive trio. Assume by way of contradiction that appending 𝑣 𝑝 introduces a disruptive trio. Then, there are two variables 𝑣 𝑖 , 𝑣 𝑗 with 𝑖 < 𝑗 < 𝑝 such that 𝑣 𝑖 , 𝑣 𝑝 are neighbors, 𝑣 𝑗 , 𝑣 𝑝 are neighbors, but 𝑣 𝑖 , 𝑣 𝑗 are not neighbors. As we proved, since 𝑣 𝑖 and 𝑣 𝑗 are neighbors of 𝑣 𝑝 preceding it, we have that all three of them appear in the handled node 𝑉 . This is a contradiction to the fact that 𝑣 𝑖 and 𝑣 𝑗 are not neighbors. □

The positive side of Theorem 4.1 is obtained by combining Lemma 4.4 with Theorem 3.3.

Intractable Cases

For the negative part, we prove a generalization of Lemma 3.13. Recall that according to Lemma 3.12, we can use lexicographic direct access to enumerate the answers to a CQ with a prefix of the ordered free variables. Similarly to Section 3.4, our goal is to find a "bad" prefix that does not allow efficient enumeration. For non-𝐿-connex CQs, this is easy since 𝐿 itself is such a prefix. Lemma 4.5. Let 𝑄 be an acyclic self-join free CQ and 𝐿 be a partial lexicographic order. If 𝑄 has a disruptive trio or 𝑄 is not 𝐿-connex, then there exists a self-join-free acyclic non-free-connex CQ 𝑄 ′ such that: if direct access for 𝑄 is possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩, then enumeration for 𝑄 ′ is possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩.

Proof. If 𝑄 is not 𝐿-connex, we use Lemma 3.12 with 𝐿 ′ = 𝐿. If 𝐿 has a disruptive trio 𝑣 1 , 𝑣 2 , 𝑣 3 , we take 𝐿 ′ to be the prefix of 𝐿 that ends in 𝑣 2 . Then, 𝑣 1 , 𝑣 3 , 𝑣 2 is an 𝐿 ′ -path, meaning that the body of 𝑄 is not 𝐿 ′ -connex. Thus, we can use Lemma 3.12 in that case too. □

It is known that, assuming sparseBMM, self-join-free non-free-connex CQs cannot be answered with polylogarithmic time per answer after quasilinear preprocessing time. Thus, we conclude from Lemma 4.5 that self-join-free acyclic CQs with disruptive trios or that are not 𝐿-connex do not have partial lexicographic direct access within these time bounds either. The case that 𝑄 is cyclic is hard since even finding any answer for cyclic CQs is not possible efficiently assuming Hypercliqe.

DIRECT ACCESS BY SUM OF WEIGHTS

We now consider direct access for the more general orderings based on SUM (the sum of freevariable weights). As with lexicographic orderings, we are able to exhaustively classify tractability for the self-join-free CQs, even those with projections. We will show that direct access for SUM is significantly harder and tractable only for a small class of queries.

Overview of Results

The main result of this section is a dichotomy for direct access by SUM orders: Theorem 5.1 (Direct Access by SUM). Let 𝑄 be a CQ.

• If 𝑄 is acyclic and an atom of 𝑄 contains all the free variables, then direct access by SUM is possible in ⟨𝑛 log 𝑛, 1⟩. • Otherwise, if 𝑄 is also self-join-free, direct access by SUM is not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩, assuming 3sum and Hyperclique.

For the positive part of the above theorem, we will see that we are able to materialize the query answers and keep them in a sorted array that supports direct access in constant time. The proof of the negative part requires the query answers to express certain combinations of weights. If the query contains independent free variables, then its answers may contain all possible combinations of their corresponding attribute weights. We will thus rely on this independence measure to identify hard cases. Definition 5.2 (Independent free variables). A set of vertices 𝑉 𝑖 ⊆ 𝑉 of a hypergraph H (𝑉 , 𝐸) is called independent iff no pair of these vertices appears in the same hyperedge, i.e., |𝑉 𝑖 ∩ 𝑒 | ≤ 1 for all 𝑒 ∈ 𝐸. For a CQ 𝑄, we denote by 𝛼 free (𝑄) the maximum number of variables among free(𝑄) that are independent in H (𝑄).

Intuitively, we can construct a database instance where each independent free variable is assigned to 𝑛 different domain values with 𝑛 different weights. By appropriately choosing the assignment of the other variables, all possible 𝑛 𝛼 free (𝑄) combinations of these weights will appear in the query answers. Providing direct access then implies that we can retrieve these sums in ranked order. We later use this to show that direct access on certain CQs allows us to solve 3sum efficiently.

Example 5.3. For 𝑄 (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧),𝑇 (𝑧, 𝑢), we have 𝛼 free (𝑄) = 2, namely for variables {𝑥, 𝑧}. Let the binary relation 𝑅 be [1, 𝑛] × {0}, i.e., the cross product between the set of values from 1 to 𝑛 with the single value 0. If we also set 𝑆 = {0} × [1, 𝑛] and 𝑇 = [1, 𝑛] × {0}, then the query answers are the 𝑛 2 assignments of (𝑥, 𝑦, 𝑧) to [1, 𝑛] × [1, 𝑛] × {0}. The 𝑛 values of 𝑥 and 𝑧 can be respectively assigned to any real-valued weights such that direct access on 𝑄 retrieves their 𝑖 th sum in ranked order.

Our independence measure 𝛼 free (𝑄) is related to the classification of Theorem 5.1 in the following way: Lemma 5.4. For an acyclic CQ 𝑄, an atom contains all the free variables iff 𝛼 free (𝑄) ≤ 1.

Proof. The "only if" part of 𝛼 free (𝑄) > 1 follows immediately from Definition 5.2. For 𝛼 free (𝑄) = 1 and acyclic query 𝑄, we prove that there is an atom 𝑅 𝑓 (X 𝑓 ) which contains all the free variables. First note that for |free(𝑄)| = 1 this is trivially true. For |free(𝑄)| > 1, let 𝑉 be a node in the join tree (corresponding to some atom of 𝑄) that contains the maximum number of

Query condition

Direct access Complexity Reason acyclic 𝛼 free (𝑄) = 1 possible in ⟨𝑛 log 𝑛, 1⟩ Lemma 5.9 acyclic 𝛼 free (𝑄) = 2 not possible in ⟨𝑛 2-𝜖 , 𝑛 1-𝜖 ⟩ 3sum acyclic 𝛼 free (𝑄) ≥ 3 not possible in ⟨𝑛 2-𝜖 , 𝑛 2-𝜖 ⟩ 3sum cyclic not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩ Hypercliqe free variables and assume for the sake of contradiction that there exists a free variable 𝑦 with 𝑦 ∉ 𝑉 . We use V 𝑦 to denote the set of nodes in the join tree that contain variable 𝑦; thus 𝑉 ∉ V 𝑦 . From 𝑄 being acyclic follows that the nodes in V 𝑦 form a connected graph and there exists a node 𝑉 ′ that lies on every path from 𝑉 to a node in V 𝑦 . Since 𝛼 free (𝑄) = 1, each variable 𝑥 ∈ 𝑉 must appear together with 𝑦 in some query atom, implying that 𝑥 appears in some node 𝑉 ′′ ∈ V 𝑦 . From that and the running intersection property follows that 𝑥 must also appear in 𝑉 ′ since 𝑉 ′ lies on the path from 𝑉 to any such 𝑉 ′′ . Hence 𝑉 ′ contains 𝑦 and all the 𝑉 variables, violating the maximality assumption for 𝑉 .

For 𝛼 free (𝑄) = 0, 𝑄 is a Boolean query and any atom trivially contains the empty set. □ Therefore, the dichotomy of Theorem 5.1 can equivalently be stated using 𝛼 free (𝑄) ≤ 1 as a criterion. We chose to use the other criterion (all free variables contained in one atom) in the statement of our theorem statement as it is more straightforward to check. In the next section, we proceed to prove our theorem by showing intractability for all queries with 𝛼 free (𝑄) > 1 and a straight-forward algorithm for 𝛼 free (𝑄) ≤ 1.

Proofs

For the hardness results, we rely mainly on the 3sum hypothesis. To more easily relate our directaccess problem to 3sum, which asks for the existence of a particular sum of weights, it is useful to define an auxiliary problem: Definition 5.5 (weight lookup). Given a CQ 𝑄 and a weight function 𝑤 over its possible answers, weight lookup takes as an input a database 𝐼 and 𝜆 ∈ R, and returns the first index of a query answer 𝑞 ∈ 𝑄 (𝐼 ) with 𝑤 (𝑞) = 𝜆 in the array of answers sorted by 𝑤 or "none" if no such answer exists.

The following lemma associates direct access with weight lookup via binary search on the query answers: Lemma 5.6. For a CQ 𝑄, if the 𝑘 th query answer ordered by a weight function 𝑤 can be directly accessed in O (𝑔(𝑛)) time for every 𝑘, then weight lookup for 𝑄 and 𝑤 can be performed in O (𝑔(𝑛) log 𝑛).

Proof. We use binary search on the sorted array of query answers. Each direct access returns a query answer whose weight can be computed in O (1). Thus, in a logarithmic number of accesses we can find the first occurrence of the desired weight. Since the number of answers is polynomial in 𝑛, the number of accesses is O (log 𝑛) and each one takes O (𝑔(𝑛)) time. □ Lemma 5.6 implies that whenever we are able to support efficient direct access on the sorted array of query answers, weight lookup increases time complexity only by a logarithmic factor, i.e., it is also efficient. The main idea behind our reductions is that via weight lookups on a CQ with an appropriately constructed database, we can decide the existence of a zero-sum triplet over three distinct sets of numbers, thus hardness follows from 3sum. First, we consider the case of three independent variables that are free. These three variables are able to simulate a three-way Cartesian product in the query answers. This allows us to directly encode the 3sum triplets using attribute weights, obtaining a lower bound for direct access. Lemma 5.7. If a CQ 𝑄 is self-join-free and 𝛼 free (𝑄) ≥ 3, then direct access by SUM is not possible in ⟨𝑛 2-𝜖 , 𝑛 2-𝜖 ⟩ for any 𝜖 > 0 assuming 3sum.

Proof. Assume for the sake of contradiction that the lemma does not hold. We show that this would imply an 𝑂 (𝑛 2-𝜖 )-time algorithm for 3sum. To this end, consider an instance of 3sum with integer sets 𝐴, 𝐵, and 𝐶 of size 𝑛, given as arrays. We reduce 3sum to direct access over the appropriate query and input instance by using a construction similar to Example 5.3. Let 𝑥, 𝑦, and 𝑧 be free and independent variables of 𝑄, which exist because 𝛼 free (𝑄) ≥ 3. We create a database instance where 𝑥, 𝑦, and 𝑧 take on each value in [1, 𝑛], while all the other attributes have value 0. This ensures that 𝑄 has exactly 𝑛 3 answers-one for each (𝑥, 𝑦, 𝑧) combination in [1, 𝑛] 3 , no matter the number of atoms and the variables they contain. To see this, note that since 𝑥, 𝑦, and 𝑧 are independent, no pair of them appears together in an atom. Also, since 𝑄 is self-join-free, each relation appears once in the query, hence contains at most one of 𝑥, 𝑦, and 𝑧. Thus each relation either contains 1 tuple (if neither 𝑥, 𝑦, nor 𝑧 is present) or 𝑛 tuples (if one of 𝑥, 𝑦, or 𝑧 is present). No matter on which attributes these relations are joined (including Cartesian products), the output result is always the "same" set [1, 𝑛] 3 × {0} 𝑓 of size 𝑛 3 , where 𝑓 is the number of free variables other than 𝑥, 𝑦, and 𝑧. (We use the term "same" loosely for the sake of simplicity. Clearly, for different values of 𝑓 the query-result schema changes, e.g., consider Example 5.3 with 𝑧 removed from the head. However, this only affects the number of additional 0s in each of the 𝑛 3 answer tuples, therefore it does not impact our construction.)

For the reduction from 3sum, weights are assigned to the attribute values as

𝑤 𝑥 (𝑖) = 𝐴[𝑖], 𝑤 𝑦 (𝑖) = 𝐵 [𝑖], 𝑤 𝑧 (𝑖) = 𝐶 [𝑖], 𝑖 ∈ [1, 𝑛],
and 𝑤 𝑢 (0) = 0 for all other attributes 𝑢. By our weight assignment, the weights of the answers are 𝐴[𝑖] + 𝐵 [ 𝑗] +𝐶 [𝑘], 𝑖, 𝑗, 𝑘 ∈ [1, 𝑛], and thus in one-to-one correspondence with the possible value combinations in the 3sum problem. We first perform the preprocessing for direct access in 𝑂 (𝑛 2-𝜖 ), which enables direct access to any position in the sorted array of query answers in 𝑂 (𝑛 2-𝜖 ). By Lemma 5.6, weight lookup for a query result with zero weight is possible in 𝑂 (𝑛 2-𝜖 log 𝑛). Thus, we answer the original 3sum problem in 𝑂 (𝑛 2-𝜖 ′ ) for any 0 < 𝜖 ′ < 𝜖, violating the 3sum hypothesis. □ For queries that do not have three independent free variables, we need a slightly different construction. We show next that two variables are sufficient to encode partial 3sum solutions (i.e., pairs of elements), enabling a full solution of 3sum via weight lookups. This yields a weaker lower bound than Lemma 5.7, but still is sufficient to prove intractability according to our yardstick. Lemma 5.8. If a CQ 𝑄 is self-join-free and 𝛼 free (𝑄) = 2, then direct access by SUM is not possible in ⟨𝑛 2-𝜖 , 𝑛 1-𝜖 ⟩ for any 𝜖 > 0 assuming 3sum.

Proof. We show that a counterexample query would violate the 3sum hypothesis. Let 𝐴, 𝐵, and 𝐶 be three integer arrays of a 3sum instance of size 𝑛. We construct a database instance with attribute weights like in the proof of Lemma 5.7, but now with only 2 free and independent variables 𝑥 and 𝑦. Hence the weights of the 𝑛 2 query results are in one-to-one correspondence with the corresponding sums 𝐴[𝑖] + 𝐵 [ 𝑗], 𝑖, 𝑗 ∈ [1, 𝑛]. We run the preprocessing phase for direct access in 𝑂 (𝑛 2-𝜖 ), which allows us to access the sorted array of query results in 𝑂 (𝑛 1-𝜖 ). A special case of Lemma 5.8 is closely related to the problem of selection in 𝑋 + 𝑌 [START_REF] Donald | Selecting the Kth element in 𝑋 + 𝑌 and 𝑋 1 + 𝑋 2 + • • • + 𝑋 𝑚[END_REF], where we want to access the 𝑘 th smallest sum of pairs between two sets 𝑋 and 𝑌 . This is equivalent to accessing the answers to 𝑄 𝑋𝑌 (𝑥, 𝑦) :-𝑅(𝑥), 𝑆 (𝑦) by a SUM order. It has been shown that if 𝑋 and 𝑌 are given sorted, then selection (single access) is possible even in linear time [START_REF] Frederickson | Generalized Selection and Ranking: Sorted Matrices[END_REF][START_REF] Mirzaian | Selection in X + Y and matrices with sorted rows and columns[END_REF]. Thus, for 𝑄 𝑋𝑌 direct access by SUM is possible in ⟨𝑛 log 𝑛, 𝑛⟩ if we sort the relations during the preprocessing phase. Compared to our ⟨𝑛 2-𝜖 , 1 -𝜖⟩ lower bound (see also Figure 8), notice that even though the preprocessing of this algorithm is lower (asymptotically), the access time is not sublinear (𝑒𝑝𝑠𝑖𝑙𝑜𝑛 = 0).

So far, we have covered all self-join-free CQs with 𝛼 free (𝑄) > 1, which, by Lemma 5.4, proves the negative part of Theorem 5.1. Next, we show that the remaining acyclic CQs (those with 𝛼 free (𝑄) ≤ 1 or equivalently, an atom containing all the free variables) are tractable. For these queries, a single relation contains all the answers, so direct access can easily be supported by reducing, projecting, and sorting that relation. Lemma 5.9. If a CQ 𝑄 is acyclic and an atom contains all the free variables, then direct access by SUM is possible in ⟨𝑛 log 𝑛, 1⟩.

Proof. Since all free variables appear in one atom 𝑅 𝑓 (X 𝑓 ), we can apply a linear-time semi-join reduction as in the Yannakakis algorithm [START_REF] Yannakakis | Algorithms for Acyclic Database Schemes[END_REF] to remove the dangling tuples, and then compute the query answers by projecting 𝑅 on the free variables. Then, we sort the query answers by the sum of weights, which takes total time O (𝑛 log 𝑛) for preprocessing. We maintain the sorted answers in an array, which enables constant-time direct access to individual answers in ranked order. □

We now combine these lemmas with the fact that Boolean self-join-free cyclic CQs cannot be answered in O (𝑛 polylog 𝑛) time assuming Hypercliqe, completing the proof of Theorem 5.1.

SELECTION BY LEXICOGRAPHIC ORDERS

We next investigate the tractability of a simpler version of the problem: When is selection, i.e., direct access to a single query answer, possible in quasilinear time? In this section, we answer this question for lexicographic orders and in Section 7 we move to the case of SUM. Unlike direct-access, we show that selection can be efficiently achieved for any lexicographic order, as long as the query is free-connex. Our main result in this setting is summarized below: Theorem 6.1 (Selection by LEX). Let 𝑄 be a CQ and 𝐿 be a partial lexicographic order.

• If 𝑄 is free-connex, then selection by 𝐿 is possible in ⟨1, 𝑛⟩.

• Otherwise, if 𝑄 is also self-join-free, then selection by 𝐿 is not possible in ⟨1, 𝑛 polylog 𝑛⟩, assuming Seth and Hyperclique.

Our theorem shows that when we limit ourselves to the problem of selection, the tractability of the problem depends only on the query structure and is independent of the lexicographic order. Example 6.2. Recall that direct access by 𝐿 is intractable for 𝑄 (𝑣 1 , 𝑣 2 , 𝑣 3 ) :-𝑅(𝑣 1 , 𝑣 3 ), 𝑆 (𝑣 3 , 𝑣 2 ) with 𝐿 being the lexicographic order ⟨𝑣 1 , 𝑣 2 , 𝑣 3 ⟩ or the partial lexicographic order ⟨𝑣 1 , 𝑣 2 ⟩. The former contains a disruptive trio while the latter is not 𝐿-connex. However, selection is tractable in both cases. Still, if we project out the middle variable 𝑣 3 and the head of the CQ is 𝑄 (𝑣 1 , 𝑣 2 ), then the CQ is not free-connex and thus, selection becomes intractable for any lexicographic order.

For the negative part of Theorem 6.1, we reduce the problem of selection to that of counting query answers. to 𝑣 𝑖 , denoted by weight(𝑐 𝑟 ). Then, we find 𝑗 such that 𝑗-1 𝑟 =1 weight(𝑐 𝑟 ) ≤ 𝑘 < 𝑗 𝑟 =1 weight(𝑐 𝑟 ) and select the value 𝑐 𝑗 for 𝑣 𝑖 . This computation can be done in O (𝑛) without sorting if we use a weighted selection algorithm [START_REF] Donald | Selecting the Kth element in 𝑋 + 𝑌 and 𝑋 1 + 𝑋 2 + • • • + 𝑋 𝑚[END_REF]. We proceed to filter all relations according to the 𝑣 𝑖 = 𝑐 𝑗 assignment, update 𝑘 to 𝑘 -𝑗-1 𝑟 =1 weight(𝑐 𝑟 ), and continue iteratively with 𝑖 + 1. In each iteration, the value for another variable is determined, where 𝑗-1 𝑟 =1 weight(𝑐 𝑟 ) answers contain a strictly smaller value for the variable, and the next iterations break the tie between the weight(𝑐 𝑗 ) answers that have this value. For the running time, each iteration takes linear time and we have a constant number of iterations (one iteration for every free variable). □

SELECTION BY SUM OF WEIGHTS

We now move on to the problem of selection by SUM order. Given that direct access by this order with quasilinear preprocessing and polylogarithmic delay is possible only in very few cases, it is a natural question to ask how the tractability landscape changes when considering the simpler task of selection.

Overview of Results

We show that the simplifications move only a narrow class of queries to the tractable side. For example, the 2-path query 𝑄 2 (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧) is tractable for selection, even though it is not for direct access. On the other hand, the 3-path query 𝑄 3 (𝑥, 𝑦, 𝑧, 𝑢) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧),𝑇 (𝑧, 𝑢) remains intractable. Given that 𝑄 2 and 𝑄 3 both have two free and independent variables, a different criterion than that of Section 5 (𝛼 free (𝑄) or number of atoms containing the free variables) is needed for classification. To this end, we use hypergraph H free (𝑄). Recall that it is the restriction of the query hypergraph H (𝑄) to the free variables, i.e., all the other variables are removed. The number of maximal hyperedges of a query 𝑄 is mh(𝑄) = mh(H (𝑄)) and the number of free-maximal hyperedges of 𝑄 is fmh(𝑄) = mh(H free (𝑄)).

Example 7.2. For 𝑄 (𝑥, 𝑧, 𝑤) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧),𝑇 (𝑧, 𝑤), 𝑈 (𝑥), we have mh(𝑄) = 3 because 𝑈 is contained in 𝑅 and fmh(𝑄) = 2 because after removing the existentially-quantified 𝑦, the remainder of the 𝑆-hyperedge is contained in 𝑇 . Remark 4. For any CQ 𝑄 we have 𝛼 free (𝑄) ≤ fmh(𝑄). This follows from the fact that each independent variable must appear in a maximal hyperedge and that each hyperedge cannot contain more than 1 independent variable by definition. Note also that the condition 𝛼 free (𝑄) ≤ 1 is equivalent to fmh(𝑄) ≤ 1, giving us a third possible way to express the criterion of Theorem 5.1 for direct access.

We summarize the results of this section in the following theorem, which classifies CQs 𝑄 based on fmh(𝑄): Theorem 7.3 (Selection by SUM). Let 𝑄 be a CQ.

• If 𝑄 is free-connex and fmh(𝑄) ≤ 2, then selection by SUM is possible in ⟨1, 𝑛 log 𝑛⟩.

• Otherwise, if 𝑄 is also self-join-free, then selection by SUM is not possible in ⟨1, 𝑛 polylog 𝑛⟩. assuming 3sum, Hyperclique, and Seth.

Example 7.4. For the query 𝑄 2 (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧) we have already shown in Section 5 that direct access by SUM is intractable. However, given that it has two maximal hyperedges, only one access (or a constant number of them) is in fact possible in O (𝑛 log 𝑛). The situation does not change for 𝑄 ′ 3 (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧),𝑇 (𝑧, 𝑢) because the hyperedge of 𝑇 is contained in 𝑆 in the free-restricted hypergraph. However, 𝑄 3 (𝑥, 𝑦, 𝑧, 𝑢) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧),𝑇 (𝑧, 𝑢) which keeps the variable 𝑢 in the answers is intractable for selection because now 𝑇 corresponds to a free-maximal hyperedge.

Before proving Theorem 7.3, we first introduce some necessary concepts and prove a useful lemma.

Absorbed atoms and variables. We say that an atom 𝑒 is absorbed by an atom 𝑒 ′ ≠ 𝑒 if 𝑉 ⊆ 𝑉 ′ where 𝑉 and 𝑉 ′ are their sets of variables respectively. Additionally, we say that a variable 𝑣 is absorbed by a variable 𝑢 ≠ 𝑣 if (1) they appear in exactly the same atoms and (2) it is not the case that 𝑣 is free and 𝑢 is not free. As evident from Theorem 7.3, adding to a query atoms or variables that are absorbed by existing ones does not affect the complexity of selection. We prove this claim first and use it later in our analysis in order to treat queries that contain absorbed atoms or variables.

Definition 7.5 (Maximal Contraction).

A query 𝑄 ′ is a contraction of 𝑄 if we can obtain 𝑄 ′ by iteratively removing absorbed atoms and variables, one at a time. 𝑄 𝑚 is a maximal contraction of 𝑄 if it is a contraction and there is no contraction of 𝑄 𝑚 .

Note that the number of atoms of a maximal contraction 𝑄 𝑚 of 𝑄 is mh(𝑄).

Example 7.6. Consider 𝑄 (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑢, 𝑦), 𝑆 (𝑦),𝑇 (𝑦, 𝑧), 𝑈 (𝑥, 𝑢, 𝑦). Here, 𝑆 (𝑦) is absorbed by 𝑅(𝑥, 𝑢, 𝑦) and 𝑈 (𝑥, 𝑢, 𝑦), and the latter two absorb each other. Additionally, the free variable 𝑥 absorbs 𝑢 since these two variables appear together in 𝑅 and 𝑈 . Thus, a maximal contraction of 𝑄 is 𝑄 𝑚 (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑦),𝑇 (𝑦, 𝑧), which is unique up to renaming. The number of maximal hyperdges of 𝑄 is mh(𝑄) = 2. Lemma 7.7. Selection for a CQ 𝑄 by SUM is possible in ⟨1, 𝑔(𝑛)⟩ if selection for a maximal contraction 𝑄 𝑚 of 𝑄 by SUM is possible in ⟨1, 𝑔(𝑛)⟩. The converse is also true if 𝑄 is self-join-free.

Proof. For the "if" direction, we use selection on 𝑄 𝑚 to solve selection on 𝑄. We can remove absorbed atoms from 𝑄 after making sure that the tuples in the database satisfy those atoms. Thus, to remove an atom 𝑆 (Y) which is absorbed by 𝑅(X), we filter the relation 𝑅 based on the tuples of 𝑆. To remove a variable 𝑣 that is absorbed by 𝑢, in all relations that contain both 𝑢 and 𝑣 we "pack" them together: We remove 𝑣 and replace the 𝑢-values by values that represent the pair (𝑢, 𝑣) and assign to it the weight 𝑤 (𝑢) + 𝑤 (𝑣). (Note that we assign 𝑤 (𝑧) = 0 for all variables 𝑧 that are not free.) After separating any packed variables, 𝑄 𝑚 over the modified database has the same answers as 𝑄 over the original one and the weights are preserved.

For the "only if" direction, we create an extended database where the answers to 𝑄 are the same as those of 𝑄 𝑚 over the original database. For each step of the contraction, we make a modification of the database. If an atom 𝑆 (Y) was removed because it was absorbed by another atom 𝑅(X), then we create the relation 𝑆 by copying 𝜋 Y (𝑅). Note that we are allowed to create 𝑆 without restrictions because 𝑄 has no self-joins, hence the database does not already contain the relation. If a variable 𝑣 was removed because it was absorbed by another variable 𝑢, then we extend all the relations that 𝑢 appears in with another attribute 𝑣 that takes the constant ⊥ value everywhere and has weight 𝑤 𝑣 (⊥) = 0. After projecting away the new variable, this construction does not change the query answers or their weights.

The above reductions take linear time, which is dominated by 𝑔(𝑛) since 𝑔(𝑛) is trivially in Ω(𝑛) for the selection problem. □

To prove Theorem 7.3 we first limit our attention to the class of full CQs (for them mh(𝑄) = fmh(𝑄)) and prove the positive part in Section 7.2 and the negative part in Section 7.3. We then extend those results to more general CQs with projections in Section 7.4.

Tractability Proofs for Full CQs

In this section, we provide tractability results for full CQs with mh(𝑄) ≤ 2. First, we consider the trivial case of mh(𝑄) = 1 where the maximal contraction of 𝑄 has only one atom. The lemma below is a direct consequence of the linear-time array selection algorithm of Blum et al. [START_REF] Blum | Time bounds for selection[END_REF]. Lemma 7.8. For a full CQ 𝑄 with mh(𝑄) = 1, selection by SUM is possible in ⟨1, 𝑛⟩.

Proof. By Lemma 7.7, it suffices to solve selection on the query 𝑄 (𝑥) :-𝑅(𝑥), which is a maximal contraction of all queries with mh(𝑄) = 1, up to renaming. Trivially, the weights of the single attribute can also be viewed as tuple weights. Thus, applying linear-time selection [START_REF] Blum | Time bounds for selection[END_REF] on the tuples of 𝑅 gives us the 𝑘 th smallest query answer. □

For the mh(𝑄) = 2 case, we rely on an algorithm by Frederickson and Johnson [START_REF] Frederickson | Generalized Selection and Ranking: Sorted Matrices[END_REF], which generalizes selection on the X+Y problem. If the two sets 𝑋 and 𝑌 are given sorted, then the pairwise sums can be represented as a sorted matrix. A sorted matrix 𝑀 contains a sequence of non-decreasing elements in every row and every column. For the 𝑋 + 𝑌 problem, a cell 𝑀 [𝑖, 𝑗] contains the sum 𝑋 [𝑖] + 𝑌 [ 𝑗]. Even though the matrix 𝑀 has quadratically many cells, there is no need to construct it in advance given that we can compute each cell in constant time. Selection on a union of such matrices {𝑀 1 , . . . , 𝑀 ℓ } asks for the 𝑘 th smallest element among the cells of all matrices. Theorem 7.9 ( [START_REF] Frederickson | Generalized Selection and Ranking: Sorted Matrices[END_REF]). Selection on a union of sorted matrices {𝑀 1 , . . . , 𝑀 ℓ }, where 𝑀 𝑚 has dimension 𝑝 𝑚 × 𝑞 𝑚 with 𝑝 𝑚 ≥ 𝑞 𝑚 , is possible in time O ( ℓ 𝑚=1 𝑞 𝑚 log(2𝑝 𝑚 /𝑞 𝑚 )). Leveraging this algorithm, we provide our next positive result: Lemma 7.10. For a full CQ 𝑄 with mh(𝑄) = 2, selection by SUM is possible in ⟨1, 𝑛 log 𝑛⟩.

Proof. The maximal contraction of full CQs with mh(𝑄) = 2 is 𝑄 1 (𝑥, 𝑧) :-𝑅(𝑥), 𝑆 (𝑧) or 𝑄 2 (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧), up to renaming. Thus by Lemma 7.7, it is enough to prove an O (𝑛 log 𝑛) bound for these two queries. As before, we turn the attribute weights into tuple weights. For 𝑄 1 , the attribute weights are trivially tuple weights and for 𝑄 2 , we assign each attribute weight to only one relation to avoid double-counting. Thus, for 𝑄 2 we compute 𝑤 (𝑟 ) = 𝑤 𝑥 (𝑟 [𝑥]) + 𝑤 𝑦 (𝑟 [𝑦]) and 𝑤 (𝑠) = 𝑤 𝑧 (𝑠 [𝑧]) for all 𝑟 ∈ 𝑅 and 𝑠 ∈ 𝑆, respectively. Since the query is full, the weights of the query answers are in one-to-one correspondence with the pairwise sums of weights of tuples from 𝑅 and 𝑆.

For 𝑄 2 , we group the 𝑅 and 𝑆 tuples by their 𝑦 values: we create ℓ buckets of tuples where all tuples 𝑡 within a bucket have equal 𝑡 [𝑦] values. This can be done in linear time. For 𝑄 1 , we place all tuples in a single bucket. For each assignment of a 𝑦 value (no assignment for the case of 𝑄 1 ), the query answers with those values are formed by the Cartesian product of 𝑅 and 𝑆 tuples inside that bucket. Also, if the size of bucket 𝑚 is 𝑛 𝑚 , then

𝑛 1 + • • • + 𝑛 ℓ = |𝑅| + |𝑆 | = O (𝑛).
We sort the tuples in each bucket (separately for each relation) according to their weight in O (𝑛 log 𝑛) time. Assume 𝑅 𝑚 and 𝑆 𝑚 are the partitions of 𝑅 and 𝑆 in bucket 𝑚 and 𝑅 𝑚 [𝑖] denotes the 𝑖 th tuple of 𝑅 𝑚 in sorted order (equivalently for 𝑆 𝑚 [ 𝑗]). We define a union of sorted matrices {𝑀 1 , . . . , 𝑀 ℓ } by setting for each bucket 𝑚:

𝑀 𝑚 [𝑖, 𝑗] = 𝑤 (𝑅 𝑚 [𝑖]) + 𝑤 (𝑆 𝑚 [ 𝑗]) if |𝑅 𝑚 | ≥ |𝑆 𝑚 | or 𝑀 𝑚 [𝑖, 𝑗] = 𝑤 (𝑆 𝑚 [𝑖]) + 𝑤 (𝑅 𝑚 [ 𝑗]
) otherwise (this distinction is needed simply to conform with the way Theorem 7.9 is stated). Selection on these matrices is equivalent to selection on the query

/𝑞 𝑚 )) ⊆ O ( ℓ 𝑚=1 𝑞 𝑚 • 2𝑝 𝑚 /𝑞 𝑚 ) = O ( ℓ 𝑚=1 𝑝 𝑚 ) = O ( ℓ 𝑚=1 𝑛 𝑚 ) = O (𝑛).
Overall, the time spent is O (𝑛 log 𝑛) because of sorting. □

Intractability Proofs for Full CQs

Though selection is a special case of direct access, we show that for most full CQs, time O (𝑛 polylog 𝑛) is still unattainable. We start from the cases covered by Lemma 5.7. To extend that result to the selection problem, note that a selection algorithm can be repeatedly applied for solving direct access. For queries with three free and independent variables, an O (𝑛 2-𝜖 ) selection algorithm would imply a ⟨1, 𝑛 2-𝜖 ⟩ direct-access algorithm, which we showed to be impossible. Therefore, the following immediately follows from Lemma 5.7:

Corollary 7.11. If a full CQ 𝑄 is self-join-free and 𝛼 free (𝑄) ≥ 3, then selection by SUM is not possible in ⟨1, 𝑛 2-𝜖 ⟩ for any 𝜖 > 0 assuming 3sum. This leaves only a small fraction of full acyclic CQs to be covered: queries with two or fewer independent variables and three or more maximal hyperedges. We next show that these queries all contain a length-3 chordless path 12 , a property that we will use in order to prove a lower bound. Lemma 7.12. The hypergraph of the maximal contraction of any full acyclic CQ with 𝛼 free (𝑄) < 3 and mh(𝑄) > 2 contains a chordless path of four variables.

Proof. First, for 𝛼 free (𝑄) = 1, we have by Lemma 5.4 that an atom contains all free variables, thus mh(𝑄) = 1. For the case of 𝛼 free (𝑄) = 2, let 𝑥, 𝑦 be independent variables. We distinguish two cases.

The first case is that each of 𝑥 and 𝑦 appear in exactly one maximal hyperedge. Denote the maximal hyperedge containing 𝑦 by 𝑒 𝑦 and the maximal hyperedge containing 𝑥 by 𝑒 𝑥 . Since there are at least 3 maximal edges, there is a hyperedge 𝑒 0 such that 𝑥, 𝑦 ∉ 𝑒 0 . Since 𝑒 0 is not absorbed by 𝑒 𝑦 , there exists 𝑎 ∈ 𝑒 0 and 𝑎 ∉ 𝑒 𝑦 . Thus, 𝑎 and 𝑦 are not neighbors. Since {𝑎, 𝑥, 𝑦} is not an independent set, we conclude that 𝑎 and 𝑥 are neighbors, and because only one maximal hyperedge can contain 𝑥, we get that 𝑎 ∈ 𝑒 𝑥 . Similarly, since 𝑒 0 is not absorbed by 𝑒 𝑥 , we conclude that there exists a neighbor 𝑏 of 𝑦 such that 𝑏 ∈ 𝑒 0 , 𝑏 ∉ 𝑒 𝑥 . Overall, we have a chordless path 𝑥 -𝑎 -𝑏 -𝑦.

The second case is that 𝑥 or 𝑦 (or both) appear in at least two hyperedges. Assume WLOG that two maximal hyperedges contain 𝑥. According to the claim we shall prove next, this means that there exist non-neighbors 𝑎,𝑏 that are both neighbors of 𝑥. Then, since {𝑎, 𝑏, 𝑦} is not an independent set, we have that 𝑦 is a neighbor of 𝑎 or 𝑏. Assume WLOG it is 𝑎. Then, we have a path 𝑦 -𝑎 -𝑥 -𝑏 such that 𝑦 and 𝑥 are not neighbors and 𝑎 and 𝑏 are not neighbors. We conclude that also 𝑦 and 𝑏 are not neighbors, otherwise this path is a chordless cycle contradicting acyclicity.

We now prove the claim that if two maximal hyperedges in an acyclic hypergraph both contain a node 𝑥, then there exists a chordless path 𝑎 -𝑥 -𝑏 for some vertices 𝑎, 𝑏. If there are two nonneighboring neighbors of 𝑥, then we are done. For the sake of contradiction, assume that there are no such neighbors. We prove with induction on the number of neighbors that for every 𝑘 neighbors of 𝑥, there exists a hyperedge that contains all of them and 𝑥. For the base of the induction, consider 2 neighbors of 𝑥. By our assumption, they must necessarily be neighbors. Since the graph is acyclic, every triangle must be covered by a hyperedge, so there exists a hyperedge containing both of them and 𝑥. For the inductive step, consider 𝑘 neighbors of 𝑥. By the induction hypothesis, every subset of these neighbors of size 𝑘 -1 appears in a hyperedge together with 𝑥. If there is no hyperedge that contains all 𝑘 of them, then these neighbors (without 𝑥) form a (𝑘, 𝑘 -1)-hyperclique contradicting acyclicity. If there is, then these 𝑘 neighbors along with 𝑥 form a (𝑘 + 1, 𝑘)-hyperclique. This contradicts acyclicity unless there is a hyperedge containing all 𝑘 variables and 𝑥. This concludes the induction. Now consider all neighbors of 𝑥. By the induction, one edge contains all of them and 𝑥. This contradicts the fact that 𝑥 appears in two maximal hyperedges.

□

Now that we established the precise form of the queries we want to classify, we proceed to prove their intractability. We approach this in a different way than the other hardness proofs: instead of relying on the 3sum hypothesis, we instead show that tractable selection would lead to unattainable bounds for Boolean cyclic queries. Lemma 7.13. If a full CQ 𝑄 is self-join-free and the hypergraph of its maximal contraction contains a chordless path of four variables, then selection by SUM is not possible in ⟨1, 𝑛 polylog 𝑛⟩ assuming Hyperclique.

Proof. We will show that if selection for 𝑄 can be done in O (𝑛 polylog 𝑛), then the Boolean triangle query can be evaluated in the same time bound, which contradicts the Hypercliqe hypothesis. Let 𝑄 △ () :-𝑅 ′ (𝑥 ′ , 𝑦 ′ ), 𝑆 ′ (𝑦 ′ , 𝑧 ′ ),𝑇 ′ (𝑧 ′ , 𝑥 ′ ) be a query over a database 𝐼 ′ of size O (𝑛). We will construct a database 𝐼 for 𝑄 so that weight lookup (see Definition 5.5) for 𝑄 over 𝐼 will allow us to answer 𝑄 △ over 𝐼 ′ .

Let 𝑥 -𝑦 -𝑧 -𝑢 be the chordless path in the hypergraph of the maximal contraction of 𝑄. This implies that there are atoms 𝑅(𝑥, 𝑦, X 𝑅 ), 𝑆 (𝑦, 𝑧, X 𝑆 ),𝑇 (𝑧, 𝑢, X 𝑇 ) in 𝑄. We construct a database 𝐼 where in all relations, we let 𝑥 and 𝑢 respectively take all the values that 𝑥 ′ can take in 𝐼 ′ . We repeat the same for 𝑦 with 𝑦 ′ and 𝑧 with 𝑧 ′ . The values that all the other variables can take in 𝐼 are set to a fixed domain value ⊥. Because the 𝑥 -𝑦 -𝑧 -𝑢 path is chordless, we know that the pair 𝑥 -𝑧 never appears in a single atom, and so is the case for 𝑥 -𝑢 and 𝑦 -𝑢. Therefore, the size of each relation in 𝐼 is bounded by |𝑅 ′ | or |𝑆 ′ | or |𝑇 ′ | and the size of 𝐼 is thus O (𝑛). Now consider a query answer 𝑞 ∈ 𝑄 (𝐼 ). If 𝜋 𝑢 (𝑞) = 𝜋 𝑥 (𝑞), then 𝜋 𝑥 𝑦𝑧 (𝑞) has to satisfy all three atoms of 𝑄 △ . This is because an (𝑥, 𝑦) pair of values has to satisfy 𝜋 𝑥 𝑦 (𝑅) which contains precisely the tuples of 𝑅 ′ , and similarly for (𝑦, 𝑧) with 𝜋 𝑦𝑧 (𝑆). For a (𝑧, 𝑥) pair of values, these are the same as (𝑧, 𝑢) and satisfy 𝜋 𝑧𝑢 (𝑇 ) contains precisely the tuples of 𝑇 ′ .

We now assign weights as follows: If dom ⊆ R, then 𝑤 𝑥 (𝑖) = 𝑖, 𝑤 𝑢 (𝑖) = -𝑖, and for all other variables 𝑡, 𝑤 𝑡 (𝑖) = 0. Otherwise, it is also easy to assign 𝑤 𝑥 and 𝑤 𝑢 in a way s.t. 𝑤 𝑥 (𝑖) = 𝑤 𝑥 ( 𝑗) if and only if 𝑖 = 𝑗 and 𝑤 𝑢 (𝑖) = -𝑤 𝑥 (𝑖). This is done by maintaining a lookup table for all the domain values that we map to some arbitrary real number. Then, we perform weight lookup for 𝑄 to identify if a query result with zero weight exists. If it does for some result 𝑞, then 𝑤 𝑥 (𝜋 𝑥 (𝑞)) +. . .+𝑤 𝑢 (𝜋 𝑢 (𝑢)) = 0 hence 𝜋 𝑥 (𝑞) = 𝜋 𝑢 (𝑞) and 𝑄 △ is true, otherwise it is false. If the time to access the sorted array of 𝑄-answers takes O (𝑛 polylog 𝑛), then by Lemma 5.6 weight lookup also takes O (𝑛 polylog 𝑛), contradicting Hypercliqe. □

For full CQs, the negative part of Theorem 7.3 for acyclic queries is proved by combining Corollary 7.11 and Lemma 7.13 together with Lemma 7.12 and Lemma 7.7 which show that we cover all queries. For self-join-free cyclic CQs, we once again resort to the hardness of their Boolean version based on Hypercliqe.

So far we have proved the restriction of Theorem 7. • Otherwise, if 𝑄 is also self-join-free, then selection by SUM is not possible in ⟨1, 𝑛 polylog 𝑛⟩. assuming 3sum and Hyperclique.

CQs with Projections

To complete the proof of Theorem 7.3, we now show how the results from Sections 7.2 and 7.3 generalize to free-connex CQs. For that purpose, we mainly rely on Proposition 2.3, which allows us to reduce a free-connex CQ to a full CQ. One difficulty that we encounter is that the full CQ that we obtain by this reduction may not necessarily be unique, as it depends on the join tree of the inclusion extension that we choose to use. For instance, the inclusion extension allows us to include unary hyperedges {𝑥 } for all query variables 𝑥, if these do not exist already. Thus, the following definition will be useful:

Definition 7.15 (Reduced Full CQs). For a free-connex CQ 𝑄, RF(𝑄) is the set of all possible full CQs we can obtain by the reduction of Proposition 2.3.

We now show that for our purposes, the tractability of a free-connex CQ 𝑄 coincides with that of any of the CQs in RF(𝑄). Lemma 7.16. Let 𝑄 be a free-connex CQ and 𝑄 ′ ∈ RF(𝑄). Selection for 𝑄 by SUM is possible in ⟨1, 𝑔(𝑛)⟩ if selection for 𝑄 ′ by SUM is possible in ⟨1, 𝑔(𝑛)⟩. The converse is also true if 𝑄 is self-join-free.

Proof. The "if" direction is trivial from Proposition 2.3: If 𝑄 is over a database 𝐼 , we can use 𝑄 ′ over a modified database 𝐼 ′ to obtain exactly the same answers.

For the "only if" direction, we use selection on 𝑄 to answer selection on 𝑄 ′ . If 𝑄 ′ is over a database 𝐼 ′ , then we construct a modified database 𝐼 for 𝑄 as follows. We copy all the relations of 𝐼 ′ into 𝐼 and for every existential variable of 𝑄, we add an attribute to the corresponding relations that takes the same ⊥ value in all tuples. The weight of all the new attributes is set to 0 for ⊥. Now, the answers to 𝑄 over 𝐼 are the same as those of 𝑄 ′ over 𝐼 ′ if we ignore all the ⊥ values from the answers.

The above reductions take linear time, which is dominated by 𝑔(𝑛) since 𝑔(𝑛) is trivially in Ω(𝑛) for the selection problem. □ From Lemma 7.14, we can decide the tractability of any (self-join-free) full CQ in RF(𝑄) by the number of its maximal hyperedges. We now connect this measure to the free-maximal-hyperedges fmh(𝑄), which is a measure easily computable from the original query 𝑄. Lemma 7.17. For a free-connex CQ 𝑄, fmh(𝑄) = mh(𝑄 ′ ) for any 𝑄 ′ ∈ RF(𝑄).

Proof. Let 𝑇 be the ext-free(𝑄)-connex tree used to derive 𝑄 ′ and 𝑇 ′ be the connected subtree containing exactly the free variables of 𝑄 (which are also the free variables of 𝑄 ′ ). Recall that the nodes of 𝑇 ′ correspond to the atoms of 𝑄 ′ .

First, we prove that every node of 𝑇 ′ is a subset of some hyperedge of H free (𝑄) and as a result fmh(𝑄) ≤ mh(𝑄 ′ ). Consider a node of 𝑇 ′ and let 𝑉 be the corresponding set of variables. Note that 𝑉 are all free variables since they appear in 𝑇 ′ . Since 𝑇 ′ is a subtree of 𝑇 , which in turn is a join-tree of an inclusive extension of H (𝑄), there must exist an atom in 𝑄 that contains 𝑉 . Let the set of variables of that atom be 𝑉 ∪ 𝑉 1 ∪ 𝑋 , for some disjoint sets 𝑉 , 𝑉 1 , 𝑋 , where 𝑉 1 are free variables and 𝑋 are existential. By the definition of H free (𝑄), there must exist a hyperedge 𝑉 ∪ 𝑉 1 in H free (𝑄).

Second, we prove that every maximal hyperedge in H free (𝑄) is a subset of some node of 𝑇 ′ and as a result mh(𝑄 ′ ) ≤ fmh(𝑄). Let 𝑉 be the (free) variables of a hyperedge of H free (𝑄). Then, there must exist an atom 𝑒 in 𝑄 that contains the 𝑉 variables. If 𝑒 corresponds to a node in 𝑇 ′ , then we are done. Otherwise, 𝑒 corresponds to a node in 𝑇 \ 𝑇 ′ . Let 𝑉 ′ be the first node of 𝑇 ′ on the path from 𝑉 to some node in 𝑇 ′ . A path between 𝑉 and any node of 𝑇 ′ must necessarily pass through 𝑉 ′ , otherwise 𝑇 would contain a cycle. Since 𝑉 contains only free variables, each one of them has to appear in some node of 𝑇 ′ and by the running intersection property, in all the nodes on the path to 𝑉 . Therefore, 𝑉 ′ contains all the variables of 𝑉 . □ Lemmas 7.16 and 7.17 allow us to generalize the positive part of Lemma 7.14 from full CQs to free-connex CQs by simply replacing the mh(𝑄) measure with fmh(𝑄). For the negative part, we need three ingredients to cover the class of self-join-free CQs: The case of free-connex CQs with fmh(𝑄) > 2 is also covered by the lemmas above. For acyclic CQs that are not free-connex, we use Lemma 6.4 which applies to any ranking function, including SUM. Finally, the intractability of cyclic CQs follows from Hypercliqe.

FUNCTIONAL DEPENDENCIES

In this section, we extend our results to apply to databases that are constrained by Functional Dependencies (FDs). From the point of view of a CQ, if the allowed input databases are restricted to satisfy an FD, an assignment to some of the variables uniquely determines the assignment to another variable. Our positive results are not affected by this, since an algorithm can simply ignore the FDs. However, certain CQs that were previously intractable may now become tractable in the presence of FDs. Our goal is to investigate how the tractability landscape changes for all four variants we have investigated so far: direct access and selection by LEX or SUM orders.

Concepts and Notation for FDs. In this section, we assume that the database schema S is extended with FDs of the form 𝑅 : 𝐴 → 𝐵, where 𝐴 and 𝐵 are sets of integers. This means that if the tuples of relation 𝑅 agree on the attributes indexed by 𝐴, then they also agree on those indexed by 𝐵. Though FDs are usually defined directly on the schema of the database, we express them from now on using the query variables for convenience. More specifically, we assume that an FD has the form 𝑅 : X → Y for some atom 𝑅(Z) where X, Y ⊆ Z. We now briefly explain why this assumption can be done without loss of generality. There are two factors that can render such a notation not well-defined. The first is self-joins. However, our negative results apply only to self-join-free CQs regardless of this issue, and positive results for self-join-free CQs naturally extend to CQs with self joins: Self-joins can be reduced in linear time to a self-join-free form by replacing the 𝑖 th occurrence of a relational symbol 𝑅 with a fresh relational symbol 𝑅 𝑖 and then copying relation 𝑅 into 𝑅 𝑖 . The second factor is repeated appearances of a variable in an atom (e.g., 𝑅(𝑥, 𝑥)). Such an appearance can be eliminated in a linear-time preprocessing step that performs the selection on the relation and then removes the duplicate variable. For the other direction of the equivalence, the projected relation can be transformed to one with the repeated variable by duplicating the relevant column in the relation. We say that an FD X → Y is satisfied by an input database 𝐼 if for all tuples 𝑡 1 , 𝑡 2 ∈ 𝑅 𝐼 we have that if

𝑡 1 [𝑥] = 𝑡 2 [𝑥], ∀𝑥 ∈ X, then 𝑡 1 [𝑦] = 𝑡 2 [𝑦], ∀𝑦 ∈ Y.
For such an FD, we sometimes say that X implies Y. WLOG, we assume that all FDs are of the form 𝑅 : X → 𝑦 where 𝑦 is a single variable because we can replace an FD of the form 𝑅 : X → Y with a set of FDs {𝑅 : X → 𝑦 | 𝑦 ∈ Y}. If |X| = 1, we say that the FD is unary. In this paper, we focus only on unary FDs.

Complexity and Reductions. In all previous sections, the computational problem we were considering was defined by a CQ and we were interested in the worst-case complexity over all possible input databases. Now, the problem is defined by a CQ 𝑄 and a set of FDs Δ. Thus the input is limited to databases that satisfy all the FDs Δ. Therefore, to determine the hardness of a problem, we need to consider the complexity of the combination of CQ and FDs. In particular, the results in the previous sections apply when the given FD set is empty. Our reductions from now on are from a certain CQ and FD set to another CQ and another FD set. Definition 8.1 (Exact Reduction). We say that there is an exact reduction from a CQ 𝑄 with FDs Δ to a CQ 𝑄 ′ with FDs Δ ′ if for every database 𝐼 that satisfies Δ:

(1) We can construct a database 𝐼 ′ in O (|𝐼 |) that satisfies the FDs Δ ′ .

(2) There is a bijection 𝜏 from 𝑄 ′ (𝐼 ′ ) to 𝑄 (𝐼 ) that is computable in O (1). Additionally, we say that an exact reduction is weight-preserving if for every weight function 𝑤 given for 𝑄, there is a weight function 𝑤 ′ for 𝑄 ′ such that 𝑤 (𝜏 (𝑞 ′ )) = 𝑤 ′ (𝑞 ′ ), ∀𝑞 ′ ∈ 𝑄 ′ (𝐼 ′ ).

Known Results. Carmeli and Kröll [START_REF] Carmeli | Enumeration Complexity of Conjunctive Queries with Functional Dependencies[END_REF] reasoned about the complexity of enumerating the answers to a CQ by looking at an equivalent extended CQ over an extended schema and FDs. We recall the definition of this extension here 13 and then proceed to use it for our classification. Definition 8.2 (FD-extension [START_REF] Carmeli | Enumeration Complexity of Conjunctive Queries with Functional Dependencies[END_REF]). Given a self-join free CQ 𝑄 and a set of FDs Δ, we define two types of extension steps. For an FD 𝑅 : X → 𝑦:

(1) If X ⊆ Z for some atom 𝑆 (Z) and 𝑦 ∉ Z, then increase the arity of 𝑆 by one, replace 𝑆 (Z) with 𝑆 (Z, 𝑦), and add 𝑆 : X → 𝑦 to the FD set. (2) If X ⊆ free(𝑄) and 𝑦 ∉ free(𝑄), then add 𝑦 to free(𝑄). The FD-extension of 𝑄 and Δ is a CQ 𝑄 + and a set of FDs Δ + that are obtained as the fixpoint of the above two extension steps.

Example 8.3. Consider the CQ 𝑄 2𝑃 (𝑥, 𝑧) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧). This CQ is not free-connex, therefore selection (or direct access) is not possible by any order (Lemma 6.4). Now, if we know that the database satisfies the FD 𝑆 : 𝑦 → 𝑧, we can take the unique 𝑧-value for every 𝑦 from 𝑆 and add it to every tuple of 𝑅, while preserving the same query answers. Thus, we can extend the CQ to 𝑄 + 2𝑃 (𝑥, 𝑧) :-𝑅(𝑥, 𝑦, 𝑧), 𝑆 (𝑦, 𝑧) and add the FD 𝑅 : 𝑦 → 𝑧. While the original CQ was not free-connex, notice that 𝑄 + 2𝑃 now is. This makes it tractable for all the tasks that we consider in this article since it is acyclic and 𝑅 contains all the free variables (see Section 5).

Similarly, the FD-extension may transform a cyclic CQ into an acyclic one. For 𝑄 △ (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧),𝑇 (𝑧, 𝑥) with the FD 𝑆 : 𝑦 → 𝑧, we get the FD-extension 𝑄 + △ (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑦, 𝑧), 𝑆 (𝑦, 𝑧),𝑇 (𝑧, 𝑥) with the additional FD 𝑅 : 𝑦 → 𝑧 which is acyclic because 𝑅 contains all the variables. Like 𝑄 + 2𝑃 (𝑥, 𝑧), we have that 𝑄 + △ is tractable for all the tasks that we consider. This is despite the fact that the cyclic 𝑄 △ without FDs is intractable for all of these tasks.

Previous work [START_REF] Carmeli | Enumeration Complexity of Conjunctive Queries with Functional Dependencies[END_REF] showed exact reductions between the original CQ (with the original FDs) and its extension (with the extended FDs) in both directions, proving that the two tasks are essentially equivalent for the task of enumeration. Theorem 8.4 ([12]). Let 𝑄 be a self-join free CQ with FDs Δ. There are exact reductions between 𝑄 with Δ and 𝑄 + with Δ + in both directions.

This theorem alone implies that, if 𝑄 + has a tractable structure (free-connex for enumeration), then the original CQ 𝑄 is tractable too. That is because according to the definition of an exact reduction, an instance for the extended schema can be built in linear time, and answers to the extension can be translated back to answers of the original CQ in constant time per answer.

We restate this result below in a slightly more general way, adding the fact that these reductions are actually weight-preserving. Intuitively, this means that any SUM ordering of the answers in one problem can be preserved through the reduction. Lemma 8.5. Let 𝑄 be a self-join free CQ with FDs Δ. There are weight-preserving exact reductions between 𝑄 with Δ and 𝑄 + with Δ + in both directions.

Proof. The bijections between the answers in the exact reductions do not change the values of the free variables of 𝑄. For the reduction from the query to its extension, we set the weights of the new free variables to zero. For the reduction from the extension to the original query, if the extension has a free variable 𝑦 that is existential in the original query, then some free variable 𝑥 in the original query implies 𝑦. To maintain the contribution of the 𝑦-weight in the query answers, we simply increase the weight of every domain value of 𝑥 by adding the weight of the corresponding domain value of 𝑦 (i.e., the one that is implied by the FD). □

Proving lower bounds using the extension is more tricky. If the extension has a structure that is known to make a CQ intractable (e.g. not free-connex for enumeration), it is not necessarily the case that it is intractable together with the FDs. Still, Carmeli and Kröll [START_REF] Carmeli | Enumeration Complexity of Conjunctive Queries with Functional Dependencies[END_REF] were able to prove lower bounds for the cases where the extension is not free-connex. We proceed to use the same technique that they used in order to prove a more general statement.

Eliminating FDs. Our goal is to show that for the extension of a self-join-free CQ, unary FDs essentially do not affect our classification. This result is similar in spirit to other results in database theory where the complexities of problems with FDs were also identified to be the complexity of the original complexity criterion applied to the FD extension after removing the FDs [START_REF] Freire | The Complexity of Resilience and Responsibility for Self-Join-Free Conjunctive Queries[END_REF][START_REF] Gatterbauer | Dissociation and propagation for approximate lifted inference with standard relational database management systems[END_REF][START_REF] Kimelfeld | A dichotomy in the complexity of deletion propagation with functional dependencies[END_REF]. To achieve that, we reduce a CQ without FDs to the same CQ with FDs, under the condition that the CQ and the FDs we reduce to are the extension of some CQ. Lemma 8.6. Let 𝑄 be a self-join free CQ with unary FDs Δ. There is a weight-preserving exact reduction from 𝑄 + without FDs to 𝑄 + over Δ + .

Proof. We are given a database instance 𝐼 for 𝑄 + that does not necessarily satisfy any FDs and we construct another database instance 𝐼 ′ with roughly the same answers (there is a bijection 𝜏 such that 𝑄 + (𝐼 ) = 𝜏 (𝑄 + (𝐼 ′ ))) that satisfies the extended FDs Δ + . Given a variable 𝑣, denote by X 𝑣 the set of all variables that are transitively implied by 𝑣. By the definition of the extension, for every variable 𝑣, every atom of 𝑄 + that contains 𝑣 also contains all X 𝑣 variables. For every tuple in the relation that corresponds to such an atom, we replace the 𝑣-value with the concatenation of X 𝑣 -values. We set the weight of a concatenated value to be equal to the weight of the 𝑣 variable as it was in the original database 𝐼 . This construction can be done in linear time, and the resulting database satisfies the FDs.

We now claim that this construction preserves the answers through a bijection. Note that for every free variable 𝑣, by the definition of the extension, the variables in X 𝑣 are all free. Every answer to the original problem gives an answer to our construction by assigning every free variable 𝑣 to the concatenation of the assignments of X 𝑣 . Every answer to our construction gives an answer to the original problem by keeping only the value that corresponds to the original variable for every free variable. In both cases, the weights of the query answers are the same in the two instances. □ Example 8.7. We illustrate the reduction above through the CQ 𝑄 (𝑥, 𝑧, 𝑢) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧),𝑇 (𝑧, 𝑢) with FD 𝑇 : 𝑧 → 𝑢. The FD-extension is 𝑄 + (𝑥, 𝑧, 𝑢) :-𝑅(𝑥, 𝑦), 𝑆 ′ (𝑦, 𝑧, 𝑢),𝑇 (𝑧, 𝑢) with extended FDs 𝑇 : 𝑧 → 𝑢 and 𝑆 ′ : 𝑧 → 𝑢. Notice that 𝑄 + , like 𝑄, is not free-connex. From that structural property of 𝑄 + , we know that without any FDs it is intractable, e.g., for the task of selection by Lemma 7.16. The reduction shows that this is still the case, even if we take the extended FDs into account.

Concretely, the reduction takes a database that does not satisfy the extended FDs and constructs another database that does. For example, 𝑆 ′ could contain tuples middle attribute (corresponding to 𝑧) does not imply the third attribute (corresponding to 𝑢). To modify this database, we replace the 𝑧-values by values that pack 𝑧 and 𝑢 together as (𝑧, 𝑢). Conceptually, and abusing the notation a little, 𝑆 ′ (𝑦, 𝑧, 𝑢) is further replaced with 𝑆 ′′ (𝑦, (𝑧, 𝑢), 𝑢) and the whole query with 𝑄 + ′ (𝑥, (𝑧, 𝑢), 𝑢) :-𝑅(𝑥, 𝑦), 𝑆 ′′ (𝑦, (𝑧, 𝑢), 𝑢),𝑇 ′ ((𝑧, 𝑢), 𝑢). The aforementioned two tuples of 𝑆 ′ are thus replaced by (1, (1, 1), 1) and (1, (1, 2), 2). Now, the third attribute is trivially dependent on the middle one because it is contained in the latter. This reduction is only possible because, being an FD-extension of 𝑄, 𝑄 + always contains 𝑢 in all of its atoms that contain 𝑧. The weights of the query answers are preserved if we only keep the weight of 𝑧 in the concatenated (𝑧, 𝑢) values, i.e., 𝑤 𝑧 ((1, 1)) = 𝑤 𝑧 (1) and 𝑤 𝑧 ((1, 2)) = 𝑤 𝑧 [START_REF] Abboud | Popular Conjectures Imply Strong Lower Bounds for Dynamic Problems[END_REF].

Note that a reduction in the opposite direction is trivial since, given an instance that satisfies the FDs, it is also a valid instance for the instance without the FDs. Thus, this lemma proves that the two problems are equivalent. By combining this fact with the equivalence of CQs and their FD-extensions (Lemma 8.5), we obtain the following result that is also useful for lower bounds and allows us to classify queries based on the structure of their extension. Theorem 8.8. Let 𝑄 be a self-join free CQ with unary FDs Δ. There are weight-preserving exact reductions between 𝑄 over Δ and 𝑄 + without FDs in both directions.

Sum of Weights

For a sum-of-weights order, applying a weight-preserving exact reduction cannot reorder the query answers since their weight is preserved. More formally, the classes of self-join-free CQs that are tractable for selection and direct access are both closed under weight-preserving exact reductions. Therefore, Theorem 8.8 immediately proves that for both problems, we can classify self-join-free CQs by their FD-extension. Theorem 8.9. Let 𝑄 be a CQ with unary FDs Δ.

• If 𝑄 + is acyclic and an atom of 𝑄 + contains all the free variables, then direct access for 𝑄 by SUM is possible in ⟨𝑛 log 𝑛, 1⟩. • Otherwise, if 𝑄 is also self-join-free, direct access for 𝑄 by SUM is not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩, assuming 3sum and Hyperclique.

Theorem 8.10. Let 𝑄 be a CQ with unary FDs Δ.

• If 𝑄 + is free-connex and fmh(𝑄 + ) ≤ 2, then selection for 𝑄 by SUM is possible in ⟨1, 𝑛 log 𝑛⟩.

• Otherwise, if 𝑄 is also self-join-free, then selection for 𝑄 by SUM is not possible in ⟨1, 𝑛 polylog 𝑛⟩, assuming 3sum, Hyperclique, and Seth.

Lexicographic Orders

We now move on to lexicographic orders, where we also provide dichotomies for unary FDs. The analysis gets more intriguing compared to SUM since the FDs may interact with the given lexicographic order in non-trivial ways that have not been explored by any previous work we know of. Like before, we use as our main tool an extension of the query according to the FDs and show an equivalence between the extension and the original problem with respect to tractability. The key difference now is that the extension may also reorder the variables in the lexicographic order according to the FDs.

Tractability Results

. Our positive results rely on two ingredients. The first ingredient is to have a reduction that preserves precisely the same lexicographic order that we begin with. This is necessary because if the query answers after the reduction follow a different lexicographic order,

We next prove two important properties for the FD-reordered extension: Lemma 8.15. For every variable 𝑣 of an order 𝐿 + of an FD-reordered extension, all variables transitively implied by 𝑣 that appear after 𝑣 in 𝐿 + have to appear consecutively after 𝑣 in 𝐿 + .

Proof. The property holds at the end of the process due to the transitivity of implication. Consider a variable 𝑦 implied by 𝑣 that appears after 𝑣 in 𝐿 + and assume there is a variable 𝑥 that is in-between 𝑣 and 𝑦 but is not implied by 𝑣. We can further assume that 𝑥 is the first variable in the order with that property. Now, 𝑥 must have been inserted at that position when handling another variable 𝑧 that is in-between 𝑣 and 𝑥 in 𝐿 + and that 𝑧 → 𝑥. Since 𝑥 is the first one not implied by 𝑣, we have that 𝑣 → 𝑧 which gives us 𝑣 → 𝑥, contradicting our assumption. □

We next show that the reordering of the variables gives the same result because of the extended FDs Δ + . As a consequence, we can study the complexity of the query with the FD-reordering. Lemma 8.16. Given a self-join-free CQ 𝑄, a set of unary FDs Δ and a partial lexicographic order 𝐿, for every database 𝐼 that satisfies Δ + , ordering 𝑄 + (𝐼 ) by 𝐿 is the same as ordering it by 𝐿 + .

Proof. Once the value for a variable 𝑣 is set, a variable implied by 𝑣 can have at most one possible value, so as long as it comes after 𝑣 in the order, its exact position or whether it is free cannot influence the answer ordering. Therefore, the reordered extension is equivalent to the original extension.

□

We now have what we need in order to show that if the reordered extension has a tractable structure, then we can conclude tractability for the original problem. Specifically, if the CQ 𝑄 + of the FD-reordered extension is free-connex, 𝐿 + -connex and has no disruptive trio, then we know it is tractable with respect to direct access by Theorem 4.1. Lemma 8.16 shows that direct access to the extension by 𝐿 + is the same as direct access by 𝐿, which is the order that we want. Finally, we use the fact that the reduction given in Lemma 8.5 preserves lexicographic orders to conclude that, given an input to 𝑄, we can construct an FD-reordered extension where tractable direct access for 𝑄 + by 𝐿 + will give us tractable direct access for 𝑄 by 𝐿. Following the same process for selection and using Theorem 6.1, we conclude that if the CQ 𝑄 + is free-connex, then selection for 𝑄 by 𝐿 is tractable.

Intractability Results

. We begin by some negative results that can easily be inferred from the results we have already proved in this paper or by past work. Lemma 8.17. Let 𝑄 be a self-join-free CQ with unary FDs Δ. If 𝑄 + is not free-connex then for any ranking function, direct access for 𝑄 is not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩ assuming sparseBMM and Hyperclique, and selection for 𝑄 is not possible in ⟨1, 𝑛 polylog 𝑛⟩ assuming Seth and Hyperclique.

Proof. For direct access, the impossibility is implied by the hardness of enumeration. If we could have direct access for 𝑄, then we can also have enumeration with the same time bounds. Then, by the exact reduction of Theorem 8.8, we would be able to enumerate the answers to the non-free-connex CQ 𝑄 + with no FDs with quasilinear preprocessing and polylogarithmic delay, which is known to contradict sparseBMM or Hypercliqe [START_REF] Bagan | Computing the jth solution of a firstorder query[END_REF][START_REF] Brault-Baron | De la pertinence de l'énumération: complexité en logiques propositionnelle et du premier ordre[END_REF].

For selection, we use Lemma 6.3 together with the simple observation that an exact reduction preserves the number of query answers. Irrespective of the order, Theorem 8.8 tells us that counting the answers to 𝑄 with Δ gives us the count of query answers of 𝑄 + with no FDs. Since a logarithmic number of selections can be used to find the latter, the count of answers of the non-free-connex claim that the first variable implying 𝑏 does not appear before the first variable implying 𝑎. Indeed, consider the first variable 𝑣 𝑏 implying 𝑏. If 𝑣 𝑏 appears before 𝑎, by Lemma 8.15, 𝑣 𝑏 also implies 𝑎. So, the first variable that implies 𝑎 is 𝑣 𝑏 or a variable before it. As a consequence, due to the reordering, the first variable implying a value that appears after 𝑣 2 appears after all first variables implying values before (and including) 𝑣 2 .

We can now claim that we can enumerate the answers to 𝑄 ′ without FDs using lexicographic direct access to 𝑄 + with FDs. We use the same construction as we did in Lemma 8.6 by assigning each variable a concatenation of the variables it implies, except that now we need to be careful about the order in which we concatenate: we start with any variables in 𝐿 + , ordered by 𝐿 + . The constructed database satisfies the FDs. It is only left to use binary search, similarly to Lemma 3.12, in order to enumerate the distinct values of the variables of 𝐿 ′ . Due to the previous paragraph, we know that these appear as a prefix, before the first value of a variable after 𝑣 2 .

□

The results of this section regarding lexicographic orders are summarized as follows.

Theorem 8.21. Let 𝑄 be a CQ with unary FDs Δ and 𝐿 be a partial lexicographic order.

• If 𝑄 + is free-connex and 𝐿 + -connex and does not have a disruptive trio with respect to 𝐿 + , then direct access for 𝑄 by 𝐿 is possible in ⟨𝑛 log 𝑛, log 𝑛⟩. • Otherwise, if 𝑄 is also self-join-free, then direct access for 𝑄 by 𝐿 is not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩, assuming sparseBMM and Hyperclique.

Theorem 8.22. Let 𝑄 be a CQ with unary FDs Δ and 𝐿 be a partial lexicographic order.

• If 𝑄 + is free-connex, then selection for 𝑄 by 𝐿 is possible in ⟨1, 𝑛⟩.

• Otherwise, if 𝑄 is also self-join-free, then selection for 𝑄 by 𝐿 is not possible in ⟨1, 𝑛 polylog 𝑛⟩, assuming Seth and Hyperclique.

A Note on General FDs

We discussed only unary FDs, where a single variable implies another. The positive side of our results also holds for general FDs where a combination of variables may imply a variable. We simply need to take the general form of the extension (given an FD 𝑥 1 , . . . , 𝑥 𝑚 → 𝑦, we add 𝑦 wherever all of 𝑥 1 , . . . , 𝑥 𝑚 appear). If the extension has a tractable form, Lemma 8.5 and Lemma 8.12 show that the original query is tractable too. However, extending the negative results requires a much more intricate analysis that goes beyond the scope of this work. Already for enumeration, even though Carmeli and Kröll [START_REF] Carmeli | Enumeration Complexity of Conjunctive Queries with Functional Dependencies[END_REF] showed a classificiation for general FDs when the extension is acyclic, the cyclic case is not resolved, and they provide a specific example of a CQ and FDs where the complexity is unknown.

CONCLUSIONS

We investigated the task of constructing a direct-access data structure to the output of a query with an ordering over the answers, as well as the restriction of the problem to accessing a single answer (the selection problem). We presented algorithms for fragments of the class of CQs for lexicographic and sum-of-weights orders. The direct access algorithms take quasilinear construction time in the size of the database, and logarithmic time for access. For selection, our algorithms take quasilinear or even linear time. We further showed that within the class of CQs without selfjoins, our algorithms cover all the cases where these complexity guarantees are feasible, assuming conventional hypotheses in the theory of fine-grained complexity. We were also able to precisely capture how the frontier of tractability changes under the presence of unary FDs. This work opens up several directions for future work, including the generalization to more expressive queries (CQs with self-joins, union of CQs, negation, etc.), other kinds of orders (e.g., min/max over the tuple entries), and a continuum of complexity guarantees (beyond ⟨quasilinear, logarithmic time⟩).

Generalizing the question posed at the beginning of the Introduction, we view this work as part of a bigger challenge that continues the line of research on factorized representations in databases [START_REF] Olteanu | Factorized Databases[END_REF][START_REF] Olteanu | Factorised representations of query results: size bounds and readability[END_REF]: how can we represent the output of a query in a way that, compared to the explicit representation, is fundamentally more compact and efficiently computable, yet equally useful to downstream operations?

A NOMENCLATURE

  LEX ordering ⟨𝑥, 𝑧, 𝑦⟩ Q 𝑥 𝑦 𝑧 𝑥 + 𝑦 + 𝑧 #

Fig. 2 .

 2 Fig. 2. Example 1.1: An example input database (a) and possible orderings of the answers to the query 𝑄 (𝑥, 𝑦, 𝑧) :-𝑅(𝑥, 𝑦), 𝑆 (𝑦, 𝑧). The orderings in (b) and (c) use two different lexicographic orders (LEX), while the ordering in (d) uses a sum-of-weights order where the weights are assumed to be identical to the attribute values.

, Vol. 1 ,

 1 No. 1, Article . Publication date: November 2023.

Fig. 3 .

 3 Fig.3. Constructing a layered join tree for the query 𝑄 3 (𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 ) :-𝑅(𝑣 1 , 𝑣 3 ), 𝑆 (𝑣 2 , 𝑣 4 ) and order ⟨𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 ⟩.

Fig. 4 .

 4 Fig. 4. Example 3.6: The result of the preprocessing phase on 𝑄 3 , the layered join tree (Figure 3b) and an example database. The weight, start index, and end index for each tuple are abbreviated in the figure as 𝑤, 𝑠, and 𝑒 respectively.

Algorithm 1 :

 1 Lexicographic Direct-Access 1 if 𝑘 ≥ weight(root) then 2 return "out-of-bound" 3 bucket[1] = root 4 factor = weight(root) 5 for i=1,. . . ,f do 6 factor = factor/weight(bucket[𝑖]) 7 pick 𝑡 ∈ bucket[𝑖] s.t. start(𝑡) • factor ≤ 𝑘 < end(𝑡) • factor 8 𝑘 = 𝑘start(𝑡) • factor 9 for child 𝑉 of layer 𝑖 do 10 get the bucket 𝑏 ∈ 𝑉 agreeing with the selected tuples 11 bucket[layer(𝑉 )] = 𝑏 12 factor = factor • weight(𝑏)

Fig. 5 .Algorithm 2 :

 52 Fig. 5. Example 3.7: Illustration of an iteration of the access phase where layer 3 corresponding to 𝑅 is resolved.

, Vol. 1 ,

 1 No. 1, Article . Publication date: November 2023.

Fig. 7 .

 7 Fig. 7. The induction step in Lemma 4.4

, Vol. 1 ,

 1 No. 1, Article . Publication date: November 2023.

Fig. 8 .

 8 Fig. 8. Possibility of direct access by sum of weights for acyclic self-join-free conjunctive queries.

  For each value 𝐶 [𝑘] in 𝐶, we perform a weight lookup on 𝑄 for weight -𝐶 [𝑘], which takes time 𝑂 (𝑛 1-𝜖 log 𝑛) (Lemma 5.6). If that returns a valid index, then there exists a pair (𝑖, 𝑗) of 𝐴 and 𝐵 with sum 𝐴[𝑖] + 𝐵 [ 𝑗] = -𝐶 [𝑘], which implies 𝐴[𝑖] + 𝐵 [ 𝑗] + 𝐶 [𝑘] = 0; otherwise no such pair exists. Since there are 𝑛 values in 𝐶, total time complexity is O (𝑛 • 𝑛 1-𝜖 log 𝑛) = O (𝑛 2-𝜖 log 𝑛). This procedure solves 3sum in 𝑂 (𝑛 2-𝜖 ′ ) for any 0 < 𝜖 ′ < 𝜖, violating the 3sum hypothesis. □ , Vol. 1, No. 1, Article . Publication date: November 2023.

, Vol. 1 ,

 1 No. 1, Article . Publication date: November 2023.

Definition 7 . 1 (

 71 Maximal Hyperedges). For a hypergraph H = (𝑉 , 𝐸), we denote the number of maximal hyperedges w.r.t. containment by mh(H ), i.e., mh(H ) = |{𝑒 ∈ 𝐸 | 𝑒 ′ ∈ 𝐸 : 𝑒 ⊂ 𝑒 ′ }|.

, Vol. 1 ,

 1 No. 1, Article . Publication date: November 2023.

  (1, 1, 1) and (1, 1, 2) where the , Vol. 1, No. 1, Article . Publication date: November 2023.

  (b) A layered join tree for 𝑄 3 w.r.t. the lexicographic order.

	𝑣 1	𝑅	𝑣 3	1	𝑣 1		𝑅′
	𝑅′			𝑣 1 , 𝑣 3	𝑅	2	𝑣 2	𝑆′
		𝑆			3
	𝑣 2		𝑣 4			4	𝑣 2 , 𝑣 4	𝑆
	𝑆′
	(a) A hypergraph that is inclusion equivalent to
	H (𝑄 3 ).

1, No. 1, Article . Publication date: November 2023.

  Taking both 𝑇 2 [𝐿 1 ] and 𝑇 - 1 and connecting every node 𝑉 1 ∈ V with a node 𝑉 2 of 𝑇 2 [𝐿 1 ] such that var(𝑉 1 ) ∩ 𝐿 1 = var(𝑉 2 ) gives us the tree we

			𝑇 1 :	𝑥𝑦𝑧		𝑇 2 :	𝑦
							𝐿 2
			𝑥𝑦𝑎	𝑦𝑧𝑏	𝑥𝑦𝑎	𝑦𝑧𝑏
				𝑦𝑧𝑑	𝑏𝑐		𝑦𝑧𝑑	𝑏𝑐
							𝑦
							𝐿 2
			𝑥𝑦𝑎	𝑦𝑧𝑏	𝑥𝑦	𝑦𝑧		𝐿 1
				𝑦𝑧𝑑	𝑏𝑐		𝑦𝑧
	Fig.	6. Example	for	the	construction	from	Proposition	4.3	for	the	CQ

, Vol. 1, No. 1, Article . Publication date: November 2023.

𝑄 (𝑥, 𝑦, 𝑧) :-𝑅 1 (𝑥, 𝑦, 𝑎), 𝑅 2 (𝑦, 𝑧, 𝑏), 𝑅 3 (𝑏, 𝑐), 𝑅 4 (𝑦, 𝑧, 𝑑) with 𝐿 1 = {𝑥, 𝑦, 𝑧} and 𝐿 2 = {𝑦}.

  If 𝑣 𝑖 is a neighbor of 𝑣 𝑝 with 𝑖 < 𝑛, then 𝑣 𝑖 ∈ 𝑉 .

	𝑇 +	𝑉 𝑖	𝑣 𝑖		𝑇 +		𝑉 𝑖	𝑣 𝑖
			𝑣 𝑝
	𝑉	𝑣 𝑝	𝑉 ℓ	𝑣 𝑝	𝑣 𝑝 , 𝑣 𝑖	𝑉 = 𝑉 ℓ
			𝑉 𝑖,𝑝	𝑣 𝑖 , 𝑣 𝑝	𝑣 𝑖 , 𝑣 𝑝	𝑉 𝑖,𝑝
	We get a contradiction in
	the case where 𝑉 ≠ 𝑉 ℓ .

  of 𝑄. By Theorem 7.9, if matrix 𝑀 𝑚 has dimension 𝑝 𝑚 × 𝑞 𝑚 with 𝑝 𝑚 ≥ 𝑞 𝑚 , we can achieve selection in O ( ℓ 𝑚=1 𝑞 𝑚 log(2𝑝 𝑚

, Vol. 1, No. 1, Article . Publication date: November 2023.

answers

  3 to full CQs as summarized below. Lemma 7.14. Let 𝑄 be a full CQ.• If mh(𝑄) ≤ 2, then selection by SUM is possible in ⟨1, 𝑛 log 𝑛⟩.

, Vol. 1, No. 1, Article . Publication date: November 2023.

  SymbolDefinition 𝑅, 𝑆,𝑇 , 𝑈 , 𝑅 1 , 𝑅 2 relation 𝑒, 𝑉 , 𝑉 1 , 𝑉 2 atom/hyperedge/node of join tree S schema 𝐼 database (instance) 𝑛 size of 𝐼 (number of tuples) dom domain X, Y, Z list of variables/attributes 𝑡 tuple 𝑥, 𝑦, 𝑧, 𝑢, 𝑣 1 , 𝑣 2 variable 𝑄 CQ 𝑞 ∈ 𝑄 (𝐼 ) query answer of CQ 𝑄 over database 𝐼 𝑄 (𝐼 ) set of answers of 𝑄 over 𝐼 𝜋 X (𝑅) projection of 𝑅 on X X 𝑓 , free(𝑄) free variables of query 𝑄 var(𝑄), var(𝑒) variables of query or atom atoms(𝑄) set of query atoms H (𝑄) = (𝑉 , 𝐸) hypergraph associated with query 𝑄 H free (𝑄) = (𝑉 , 𝐸) restriction of H (𝑄) to free variables only 𝑇 join tree V set of join tree nodes 𝛼 free (𝑄) maximum number of independent free variables of 𝑄 mh(𝑄) number of maximal hyperedges (with respect to containment) in H (𝑄) fmh(𝑄) number of free-maximal hyperedges = maximal hyperedges in H free (𝑄) 𝐿 = ⟨𝑣 1 , . . . , 𝑣 𝑚 ⟩ lexicographic order of variables 𝑤 𝑥 weight function for variable 𝑥: dom → R 𝑤 𝑄 weight function for query answers 𝑤 short form for all 𝑤 𝑥 and 𝑤 𝑄

	Σ𝑤	sum-of-weights order
	𝜆	a real-valued weight
	⪯	total order over query answers
	Π	family of orders
	order	a binary relation as in partial/total order
	ordering	a sorted list according to an order
	𝑅 : X → Y	FD where X implies Y in 𝑅
	Δ	set of FDs

⟨𝑛 log 𝑛, log 𝑛⟩ direct access with O (𝑛 log 𝑛) preprocessing and O (log 𝑛) per access ⟨1, 𝑛 log 𝑛⟩ selection in O (𝑛 log 𝑛)

, Vol. 1, No. 1, Article . Publication date: November 2023.

We use 𝑒 for atoms because of the natural analogy to hyperedges in hypergraphs associated with a query 𝑄.

To make a clear distinction between the vertices of a hypergraph and those of its join tree, we call the latter nodes.

By subtree, we mean any connected subgraph of the tree. , Vol. 1, No. 1, Article . Publication date: November 2023.

Free-connex CQs are sometimes called in the literature free-connex acyclic CQs[START_REF] Bagan | On Acyclic Conjunctive Queries and Constant Delay Enumeration[END_REF]. For cyclic CQs, the free-connex property is only defined with respect to a particular hypertree decomposition[START_REF] Bagan | On Acyclic Conjunctive Queries and Constant Delay Enumeration[END_REF], and not for the CQs themselves. Thus, we choose to omit the word acyclic when referring to free-connex acyclic CQs. , Vol. 1, No. 1, Article . Publication date: November 2023.

Works in the literature[START_REF] Bagan | Computing the jth solution of a firstorder query[END_REF][START_REF] Berkholz | Constant delay enumeration for conjunctive queries: a tutorial[END_REF][START_REF] Carmeli | Answering (Unions of) Conjunctive Queries Using Random Access and Random-Order Enumeration[END_REF] typically phrase this as linear, yet any logarithmic factor increase is still covered by the hypotheses., Vol. 1, No. 1, Article . Publication date: November 2023.

The same count statistics are also leveraged in[START_REF] Zhao | Random Sampling over Joins Revisited[END_REF] Sect. 4.2] in the context of sampling. , Vol. 1, No. 1, Article . Publication date: November 2023.

The conference version of this paper[START_REF] Carmeli | Tractable Orders for Direct Access to Ranked Answers of Conjunctive Queries[END_REF] erroneously claims that the maximal contraction of these CQs is the 3-path query 𝑄 (𝑥, 𝑦, 𝑧, 𝑢) :-𝑅 (𝑥, 𝑦), 𝑆 (𝑦, 𝑧),𝑇 (𝑧, 𝑢). This is not correct because for example, 𝑄 ′ (𝑥, 𝑦, 𝑧, 𝑢, 𝑏) :-𝑅 (𝑥, 𝑏, 𝑦), 𝑆 (𝑦, 𝑏, 𝑧),𝑇 (𝑧, 𝑏, 𝑢) also satisfies 𝛼 free (𝑄 ′ ) < 3 and mh(𝑄 ′ ) > 2. However, as we show here, the same reduction that was used for the 3-path query 𝑄 can also work for any of these CQs. , Vol. 1, No. 1, Article . Publication date: November 2023.

The original definition is more involved since it also applies to self-joins. We only give the restriction of the definition required for our purposes. We also state it for general FDs instead of unary FDs, which is useful for our discussion in Section 8.3 , Vol. 1, No. 1, Article . Publication date: November 2023.

ACKNOWLEDGMENTS

We thank the anonymous reviewers for their constructive comments and Florent Capelli for his input on simplifying the proof of Lemma 6.6. Nofar Carmeli and Nikolaos Tziavelis were supported by Google PhD Fellowships. Nofar Carmeli and Benny Kimelfeld were supported by the German Research Foundation (DFG) Project 412400621 (DIP program). Nikolaos Tziavelis, Wolfgang Gatterbauer, and Mirek Riedewald were supported by the National Science Foundation (NSF) under award number IIS-1956096. Wolfgang Gatterbauer was supported by NSF under award number CAREER IIS-1762268. Nofar Carmeli was supported by the French government under management of Agence Nationale de la Recherche as part of the "Investissements d'avenir" program, reference ANR-19-P3IA-0001 (PRAIRIE 3IA Institute).

Lemma 6.3. For a CQ 𝑄, if selection by some ranking function is possible in ⟨1, 𝑓 (𝑛)⟩ for a function 𝑓 , then counting the answers to the CQ 𝑄 is possible in O (𝑓 (𝑛) log 𝑛).

Proof. We reduce the counting problem to the selection problem under any ranking function. If the number of relations in the query is ℓ, then an upper bound on the number of answers is 𝑛 ℓ . We use selection to determine whether any index contains an answer. With binary search on the range of indices [0, 𝑛 ℓ ), we can find the smallest index that does not correspond to an answer. This process requires only O (log 𝑛 ℓ ) = O (log 𝑛) selections since ℓ is constant. □

We can now exploit lower bounds based on Seth. The proof does not rely on the properties of lexicographic orders and thus captures any possible ordering of the query answers. Lemma 6.4. If a self-join-free CQ 𝑄 is not free-connex, then selection by any ranking function is not possible in ⟨1, 𝑛 polylog 𝑛⟩ assuming Seth and Hyperclique.

Proof. We use the fact that, assuming Seth, the answers to a self-join-free and acyclic non-freeconnex CQ cannot be counted in O (𝑛 2-𝜖 ) for any constant 𝜖 [START_REF] Mengel | A short note on the counting complexity of conjunctive queries[END_REF]. By Lemma 6.3, if selection is possible in O (𝑛 polylog 𝑛), then we can also count the number of query answers in O (𝑛 polylog 𝑛), contradicting our hypothesis. Cyclic CQs are covered by the hardness of Boolean self-join-free cyclic CQs based on Hypercliqe, completing the proof. □

For the remainder of this section, we give a selection algorithm that together with Lemma 6.4 completes the proof of Theorem 6.1

Lexicographic Selection Algorithm

We first claim that, for any free variable in a free-connex CQ, we can efficiently compute the histogram of its assignments in the query answers. This is essentially equivalent to a group-by query that groups the query answers based on a single variable, and then counts how many answers fall within each group. Lemma 6.5. Let 𝑄 be a free-connex CQ and 𝑣 ∈ free(𝑄). Given an input database 𝐼 , we can compute in linear time how many answers in 𝑄 (𝐼 ) assign 𝑐 to 𝑣 for each value 𝑐 in the active domain of 𝑣.

Proof. Following Proposition 2.3, we can transform the problem to an equivalent problem with a full acyclic CQ 𝑄 ′ . We then take a join-tree for 𝑄 ′ , identify a node 𝑉 𝑝 containing 𝑣, and introduce a new node 𝑉 𝑟 as a neighbor of 𝑉 𝑝 . We associate 𝑉 𝑟 with the single variable 𝑣, assign 𝑉 𝑟 with a unary relation that contains the active domain of 𝑣, and set 𝑉 𝑟 to be the root of the tree. Then, we follow the preprocessing explained in Section 3.1 over this tree. By the end of this preprocessing, each tuple is given a weight that indicates the number of different answers that this tuple agrees with when only joining its subtree. Thus, the weights for 𝑉 𝑟 will contain the desired values. □

This count guides our selection algorithm as it iteratively chooses an assignment for the next variable in the lexicographic order. Comparing the desired index with the count, it chooses an appropriate value for the next variable, filters the remaining relations according to the chosen value, and continues with the next variable. Lemma 6.6. Let 𝑄 be a free-connex CQ and 𝐿 be a partial lexicographic order. Then, selection by 𝐿 is possible in ⟨1, 𝑛⟩.

Proof. Let ⟨𝑣 1 , . . . , 𝑣 𝑚 ⟩ be a completion of 𝐿 to a full lexicographic order, and let 𝑘 be the index we want to access. We perform the following starting with 𝑖 = 1. Let 𝑐 1 , . . . , 𝑐 𝑚 be the ordered values in the active domain of 𝑣 𝑖 (the algorithm does not sort them because that would already take O (𝑛 log 𝑛)). We use Lemma 6.5 to count, for each 𝑐 𝑟 , the number of answers that assign 𝑐 𝑟 then we cannot conclude anything about the position of a query answer in the original problem. Thus, we define a stricter notion of an exact reduction. Definition 8.11 (Lex-preserving exact reduction). Consider a CQ 𝑄 and FDs Δ, and a CQ 𝑄 ′ and FDs Δ ′ such that the free variables of 𝑄 are also free variables in 𝑄 ′ . An exact reduction via a bijection 𝜏 from 𝑄 and Δ to 𝑄 ′ and Δ ′ is called lex-preserving if, for every partial lexicographic order 𝐿 for 𝑄 and for all query answers 𝑞 1 , 𝑞 2 of 𝑄 ′ , we have that 𝑞 1 ≺ ′ 𝑞 2 iff 𝜏 (𝑞 1 ) ⪯ 𝜏 (𝑞 2 ) where ⪯ and ⪯ ′ are orders implied by 𝐿.

In a lex-preserving exact reduction, we have the guarantee that even though the query answers might not be exactly the same, those that are in a 1-1 correspondence will be placed in the same position when ordered. Thus, such a reduction allows us to solve direct access or selection on a different problem and translate the answers back to the original problem. Analogously to weightpreserving exact reductions for SUM, our notions of tractability for lexicographic orders are preserved under lex-preserving exact reductions. Now notice that the forward reduction from 𝑄 with Δ to 𝑄 + with Δ + in Lemma 8.5 is a lexpreserving exact reduction. This is because as we argued before, the reduction to the extension does not change the values of any of the free variables. The following lemma is, similarly to Lemma 8.5, a slight generalization of the result of Carmeli and Kröll [START_REF] Carmeli | Enumeration Complexity of Conjunctive Queries with Functional Dependencies[END_REF], this time suited to lexicographic orders. Lemma 8.12. Let 𝑄 be a self-join free CQ with FDs Δ. There is a lex-preserving exact reduction from 𝑄 with Δ to 𝑄 + with Δ + . Importantly, the other direction of Lemma 8.12 is not true in general, since the FD-extension may contain additional free variables that do not appear in the original query 𝑄. Similarly, the reduction in Lemma 8.6 is also not lex-preserving. As an example, consider the simple example of 𝑄 (𝑣 1 , 𝑣 2 , 𝑣 3 ) :-𝑅(𝑣 1 , 𝑣 2 , 𝑣 3 ) with the FD 𝑅 : 𝑣 1 → 𝑣 3 . Sorting the constructed instance by ⟨𝑣 1 , 𝑣 2 , 𝑣 3 ⟩ will actually result in an ordering according to ⟨(𝑣 1 , 𝑣 3 ), 𝑣 2 , 𝑣 3 ⟩ which is the same as ⟨𝑣 1 , 𝑣 3 , 𝑣 2 ⟩. That is precisely the reason why proving lower bounds for the case of lexicographic orders is not as straightforward as the SUM case.

Going back to the positive side, we can now use Lemma 8.12 to reduce our problem to its extension. But in order to cover all tractable cases, we need to modify the FD-extension so that the FDs are also taken into account in the lexicographic order. In particular, if a variable 𝑢 is implied by a variable 𝑣 and 𝑢 comes after 𝑣 in the order, then 𝑢 is placed right after 𝑣 in the reordering. Definition 8.13 (FD-reordered extension). Given a self-join-free CQ 𝑄, a set of unary FDs Δ and a partial lexicographic order 𝐿, their FD-reordered extension is a CQ 𝑄 + , a set of unary FDs Δ + and a partial lexicographic order 𝐿 + . We take 𝑄 + and Δ + to be as defined in Definition 8.2, and 𝐿 + is obtained from 𝐿 by applying the following reordering step iteratively from index 𝑖 = 0 until we reach the end of the list 𝐿: Find all variables X 𝐿 [𝑖 ] that are transitively implied by 𝐿[𝑖] and place them in consecutive indexes starting from 𝑖 + 1. Note that 𝐿 may grow in this process to contain variables that are free in 𝑄 + though they are not free in 𝑄.

Example 8.14. Consider the CQ 𝑄 (𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 ) :-𝑅(𝑣 1 , 𝑣 3 ), 𝑆 (𝑣 3 , 𝑣 2 ),𝑇 (𝑣 2 , 𝑣 4 ) with the FD 𝑅 : 𝑣 1 → 𝑣 3 and the lexicographic order 𝐿 = ⟨𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 ⟩. This order contains the disruptive trio 𝑣 1 , 𝑣 2 , 𝑣 3 and is intractable for direct access according to the results of Section 3. When applying the FD-extension, we get that 𝑄 + = 𝑄 because 𝑣 1 already appears with 𝑣 3 in 𝑅. For the FD-reordered extension, we reorder the lexicographic order into 𝐿 = ⟨𝑣 1 , 𝑣 3 , 𝑣 2 , 𝑣 4 ⟩, which contains no disruptive trio and is tractable for direct access.

CQ 𝑄 + can be found in quasilinear time, contradicting Seth (if 𝑄 + is acyclic) or Hypercliqe (if 𝑄 + is cyclic). □

It is left to handle the cases that the reordering of the extension is free-connex acyclic but has a disruptive trio or is not 𝐿 + -connex. The case that the extension is not 𝐿 + -connex can be shown using a reduction from enumeration for a non-free-connex CQ (Lemma 3.12) as we did in Section 4.2. Lemma 8.18. Let 𝑄 be a self-join-free CQ with unary FDs Δ, and let 𝐿 be a partial lexicographic order. If 𝑄 + is acyclic but not 𝐿 + -connex, then direct access for 𝑄 by 𝐿 is not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩ assuming sparseBMM.

Proof. Using Lemma 3.12, it is enough to find a prefix 𝐿 ′ of 𝐿 + that is closed under implication such that the extension 𝑄 + is not 𝐿 ′ -connex. We simply take 𝐿 ′ = 𝐿 + . Then, we conclude that there is efficient enumeration for the acyclic but not free-connex 𝑄 + with 𝐿 ′ as free variables. Since 𝐿 ′ is closed under implication, 𝑄 + is an extension of itself, so we can use Lemma 8.6 to conclude that there exists efficient enumeration to 𝑄 + with 𝐿 ′ as free variables even without FDs. This is a contradiction as it is not free-connex. □

The case where the reordered extension has a disruptive trio 𝑣 1 , 𝑣 2 , 𝑣 3 is slightly more intricate as we cannot directly use Lemma 8.6. One might hope that, as we did in the proof of Lemma 3.13, we would be able to take a prefix of 𝐿 + that ends in 𝑣 2 and then apply Lemma 8.6. However, that is not always possible here because we have the additional restriction that the prefix we pick has to be closed under implication. This is required so that the CQ we obtain when we restrict the free variables to the prefix is a valid FD-extension. Unfortunately, a prefix that includes 𝑣 2 but not 𝑣 3 and is closed under implication does not necessarily exist. That is the case when some variable implies both 𝑣 2 and 𝑣 3 .

Example 8.19. Consider the CQ 𝑄 (𝑣 1 , 𝑣 2 ) :-𝑅(𝑣 1 , 𝑣 3 ), 𝑆 (𝑣 3 , 𝑣 2 ) with the FD 𝑆 : 𝑣 2 → 𝑣 3 and the lexicographic order 𝐿 = ⟨𝑣 1 , 𝑣 2 ⟩. The extended reordering is 𝐿 + = ⟨𝑣 1 , 𝑣 2 , 𝑣 3 ⟩ which contains the disruptive trio 𝑣 1 , 𝑣 2 , 𝑣 3 . To reuse our previous approach, we would want to claim that using lexicographic direct access to 𝑄 + , we can enumerate the CQ with only 𝑣 1 , 𝑣 2 as free variables (which happens to be in this case the same as 𝑄 that we started with). However, this is not a contradiction because 𝑄 is not known to be hard for enumeration as it has FDs and it is not an extension (𝑣 2 implies 𝑣 3 while 𝑣 2 is free and 𝑣 3 is existential). In fact, we cannot find any prefix 𝐿 ′ of 𝐿 + that is closed under implication such that 𝑄 + is not 𝐿 ′ -connex. To circumvent this issue, we encode the enumeration of 𝑄 without FDs into the extension by combining the binary search approach from Lemma 3.12 with the concatenation reduction from Lemma 8.6. The difference is that before we used binary search to enumerate a prefix of the free variables, and now this prefix might stop in the middle of a variable with concatenated values. Thus, 𝑣 2 will be assigned the values (𝑣 2 , 𝑣 3 ), and we will use binary search to skip over the 𝑣 3 values. Lemma 8.20. Let 𝑄 be a self-join-free CQ with unary FDs Δ, and let 𝐿 be a partial lexicographic order. If 𝑄 + is acyclic and 𝐿 + contains a disruptive trio in 𝑄 + , then direct access for 𝑄 by 𝐿 is not possible in ⟨𝑛 polylog 𝑛, polylog 𝑛⟩ assuming sparseBMM.

Proof. Consider a disruptive trio 𝑣 1 , 𝑣 2 , 𝑣 3 in 𝑄 + with respect to 𝐿 + . Let 𝐿 ′ be the prefix of 𝐿 + ending in 𝑣 2 , and let 𝑄 ′ be the query with the body of 𝑄 + and the free variables 𝐿 ′ . As 𝑣 1 , 𝑣 3 , 𝑣 2 is an 𝐿 ′ -path, we know that 𝑄 ′ is acyclic but not free-connex, and so it cannot be enumerated efficiently without FDs assuming sparseBMM.

We first claim that the reordering 𝐿 + is stable with respect to the first occurrences of implying variables. More precisely, let 𝑎 and 𝑏 be variables in 𝐿 + such that 𝑎 appears before 𝑏 in 𝐿 + . We