Acyclic Join Sampling Under Selections: Dichotomy, Union Sampling, and Enumeration
Abstract
Previous research on join sampling has focused on joins without selection conditions, even though such conditions are prevalent in everyday queries in database systems. Motivated by this, we undertake a systematic investigation on the complexity of sampling from the result of an acyclic join under equality conditions given only at runtime. When conditions are conjunctive, the goal is to understand when it is possible to precompute a feasible structure that uses space and supports sampling in time, where IN is the input size. We present a dichotomy to characterize (subject to a widely-accepted conjecture) the existence of such structures based on the conditions supplied and, in every feasible scenario, give an optimal structure of space and sample time. We then extend our investigation to conditions expressed in disjunctive normal form, where the core challenge reduces to the fundamental set union sampling problem. We overcome the challenge with an optimal algorithm and utilize it to develop optimal sampling structures. Our findings also lead to new results on the closely-related random enumeration problem.
Keywords and phrases:
Conjunctive Queries, Acyclic Joins, Sampling, Lower BoundsCopyright and License:
2012 ACM Subject Classification:
Theory of computation Data structures and algorithms for data managementFunding:
This work was supported in part by GRF Projects 14217322 and 14222822.Editors:
Balder ten Cate and Maurice FunkSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
Join evaluation on massive datasets often incurs prohibitive computation. A primary cause of this phenomenon is the sheer volume of the join result – whose size grows rapidly as the number of participating relations increases – because reporting each result tuple requires at least constant time. However, in many scenarios (ranging from approximate query processing and query optimization to interactive data exploration and machine learning), the goal is not to enumerate the entire join result, but rather to obtain a small representative sample. Such samples can be used to approximate aggregates, estimate selectivities, support interactive analysis, or train a learning model without materializing the full join. These practical needs have motivated the development of numerous join sampling algorithms in the theory community; see [2, 15, 8, 16, 13, 19, 28, 32, 35] and the references therein.
Previous research has focused on sampling from joins without selection conditions. This stands in stark contrast to reality, where queries nearly always include such conditions, typically in the form of equality predicates like . Here, the values , , and are unknown in advance and are supplied by a query only at runtime. Essential in narrowing down the data of interest, these predicates prompt the question of how join sampling can remain effective in the presence of conjunctive and disjunctive selection conditions. This paper addresses the question in a systematic manner.
Mathematical Conventions and Computation Model.
The notation represents the set of integers. Given an integer , we define . All logarithms have base 2 by default. Our analysis assumes the standard word-RAM model [17] where a word has length with IN being the input size. Given an integer representable using a word, we assume that a uniformly random number can be drawn from in constant time.
1.1 Problem Definitions
Let be a set whose elements are called attributes. Given a subset , a tuple over is a function . For each attribute , we call “the value of under ” or simply the “-value” of – such a value is assumed to fit in a word. Given a subset of , we use (note: square bracket here) to represent the tuple over satisfying for all ; the tuple is called the projection of onto . We define a relation as a set of tuples over an identical set of attributes, where is called the schema of , written as .
Let be a finite subset of and be a subset of . We call the hypergraph a schema graph if every attribute of appears in at least one element – a hyperedge – of . The hypergraph is acyclic [1, 33] if there exists a tree having two properties:
-
[the one-one property] every node of corresponds to one distinct hyperedge in ;
-
[the connectedness property] for each attribute , the nodes in (a.k.a. hyperedges in ) containing form a connected subtree in .
We refer to as a join tree of .
Example 1.
Consider the hypergraph where , and has 5 hyperedges: (shortform for set ), , , , and . The hypergraph is acyclic, with a join tree given in Figure 1(a). The hyperedges containing attribute, for instance, E are , and , and they form a connected subtree of . As another example, the hyperedges containing G are and , which again are connected.
Fix a schema graph . A join instance (or simply, a join) of is a set of relations such that (i) , and (ii) for each hyperedge , there is a unique relation with . We call the “schema graph of ”. The result of the join is a relation over defined as:
| (1) |
The input size of is defined as . If is acyclic, we refer to as an acyclic join; otherwise, it is a cyclic join.
If the relations in can be listed as , we may also represent as . If a relation, say, consists of a single tuple , we may replace it with in a join expression, e.g., .
-Sampling.
Fix a schema graph whose size is assumed to be a constant, together with a subset . Let be a join instance of . Given a tuple over , define
| (2) |
namely, comprises the tuples in the join result satisfying the conjunctive condition . Given a tuple over , a -sampling operation returns a tuple chosen from uniformly at random, or returns nil if .
Problem 1.
Preprocess into a data structure that can support -sampling operations, with the requirement that the sample returned by each operation must be independent of the outputs of all previous operations.
We call the above the -sampling problem. A data structure is feasible if it uses space and supports a sampling operation in time where the notation hides a factor polylogarithmic to IN.
-Sampling.
Fix a schema graph whose size is assumed to be a constant, as well as subsets of for some . Let be a join instance of . Given tuples where is over for each , define
| (3) |
where is as defined in (2). In other words, includes all and only the tuples in the join result that satisfy a condition given in a disjunctive normal form: . The value of is permitted to be arbitrarily greater than , implying that need not be distinct111For example: SELECT * FROM Payment P, TaxPayer T WHERE P.ssn = T.ssn AND (T.job = “prof” OR T.job = “lawyer”). Here, both and are {job}..
Given tuples , a -sampling operation returns a tuple chosen from uniformly at random, or returns nil if .
Problem 2.
Preprocess into a data structure that can support -sampling operations, with the requirement that the sample returned by each operation must be independent of the outputs of all previous operations.
We call the above the -sampling problem.
1.2 Our Results
Problem 1 (-Sampling).
The following is a conjecture that has been extensively used in studying fine-grained complexities; see, e.g., [14, 18, 23, 24, 29, 30] and their references.
Strong Set Disjointness (SSD) Conjecture. Let be sets whose elements are integers. Define . In the set disjointness problem, we want to preprocess these sets into a data structure of space such that, given any distinct integers , we can report in time whether . The SSD conjecture states that must be .
We investigate under the above conjecture the existence of feasible structures for Problem 1. It turns out that the answer depends on whether the schema graph of is “ext--connex”, which is a notion introduced by Bagan, Durand and Grandjean [5]:
Definition 2.
Let be a schema graph and be a subset of . We say that is ext--connex if the hypergraph is acyclic.
Example 3.
Consider again the schema graph in Example 1.1. The table below lists the answers to “is ext--connex?” for several representative choices of .
| E | AE | ACE | ACEH | ACEHI | ACEHF | ||
|---|---|---|---|---|---|---|---|
| ext--connex? | yes | yes | no | yes | yes | no | yes |
Figure 1(b) shows a join tree of the hypergraph , which serves as evidence that is acyclic; hence, is ext-ACEHF-connex.
Our dichotomy result is:
Theorem 4.
Let be an acyclic schema graph and be a subset of . Subject to the SSD-conjecture, the -sampling problem admits a feasible structure if and only if is ext--connex.
Prior to our work, ext--connexity has been used to prove dichotomies in several settings [5, 9, 10, 11, 12, 13, 21], all of which seem quite different from -sampling. Interestingly, the dichotomy in Theorem 4 implies an approach to implement feasible -sampling via “direct-access” queries. To explain, let be an ordering of the attributes in : . This defines a lexicographic order on the tuples in : if there is an such that but for all . Given an integer , a direct access (DA) query [6, 7, 12, 13] returns the -th tuple of under if , or nil otherwise. As shown in [12], if is ext--connex, we can find an order such that
-
the attributes of form a prefix of , i.e., for every ;
-
one can build a structure of space that answers any DA query in time.
Fix an arbitrary tuple over . Crucially, the tuples in must be consecutive under (because is a prefix of ). Thus, if , there exist integers and such that a tuple is in if and only if it is the -th tuple under for some . With DA queries, one can obtain the values of and (or certify their non-existence) via binary search. Then, a sample of can be drawn by generating a random and issuing one more DA query. This supports a -sampling operation in time.222We thank an anonymous reviewer for pointing out the connection to DA queries.
In this work, we show that the sample time can be reduced to constant:
Theorem 5.
Consider the -sampling problem where is ext--connex. Given a join instance of , we can build a structure of space in expected time such that a -sampling operation on can be supported in time.
Problem 2 (-Sampling).
Our core contribution to this problem is a new algorithm for a fundamental sampling problem:
Set Union Sampling: Let be sets of elements drawn from a certain domain. Each () supports three operations in constant time:
-
1.
(size) return ;
-
2.
(membership) check whether a given element is in ;
-
3.
(sampling) return an element of chosen uniformly at random.
The goal is to sample an element from uniformly at random using only these operations. The sample obtained each time must be independent of all previous samples.
We will prove:
Theorem 6.
There is a set union sampling algorithm with expected sample time.
Currently the fastest expected sample time [13, 4] – as will be reviewed in Section 2 – is where , which we strictly improve. By combining Theorems 5 and 6 with additional ideas, we will prove:
Theorem 7.
Consider -sampling where is ext--connex for all . Given a join instance of , we can build a structure of space in expected time such that a -sampling operation on can be supported in expected time.
The connexity requirement is necessary for , as formally stated below:
Theorem 8.
Consider -sampling where . If is not ext--connex for an arbitrary , no structures of space can guarantee expected sample time, subject to the SSD conjecture.
Random Enumeration.
Random enumeration of a query result produces a (uniformly) random permutation of the elements therein. An algorithm achieves a delay of if it can (i) output the first element or declare an empty result within time, and (ii) after the previous output, produce the next element or declare “no more” within an additional time. The algorithm ensures an expected delay if it satisfies the preceding requirement, except that the two “ time” occurrences are replaced with “ expected time”. By combining our sampling techniques with a new enumeration-to-sampling reduction, we prove:
Theorem 9.
Let be a schema graph and be a subset of such that is ext--connex. Given a join instance of , we can build a structure of space in expected time such that, given any tuple over , we can randomly enumerate with an expected delay of .
2 Related Work
At a high level, the objective of join sampling is to create a data structure on the input relations of a join that can be used to extract a uniformly random tuple from . The samples obtained from repetitive extractions must be mutually independent. If is acyclic, Zhao et al. [35] described an -space structure ensuring sample time. The problem becomes more challenging when is cyclic. Improving over [16, 19], Kim et al. [28] presented a structure of space that can guarantee a sample time of , where AGM is the join’s AGM bound [3], and . For a broad class of joins with “degree constraints”, Wang and Tao [32] managed to reduce the sample time to , where is the polymatroid bound [27] of , which never exceeds but can be significantly lower than the AGM bound. Recently, Capelli et al. [8] presented a different approach to match the result of [32] up to polylogarithmic factors.
We are not aware of previous work on join sampling under conjunctive or disjunctive selection predicates. Like [35], our study concentrates on acyclic joins, but the sampling problem in [35] is merely a case of -sampling (see Problem 1) where ; the result of [35] can be regarded as a special version of our Theorem 5.
The set union sampling problem in Section 1.2 has been studied in two independent articles [4, 13]. Both articles described an algorithm ensuring an expected sample time of where and (Section 5.1 will discuss this algorithm in detail). Note that can reach (this happens when ), in which case the sample time becomes . In [4], Aumuller et al. presented another algorithm to extract, in expected time, a sample that is uniform “with high probability”. Our Theorem 6 strictly improves these results.
Given an acyclic join , Carmeli et al. [13] showed how to randomly enumerate with a delay of , after an -time preprocessing. In Theorem 9, we settle a more general problem (i.e., random enumeration of ) with an expected delay of . Given acyclic joins whose results have the same schema, Carmeli et al. [13] described a way to randomly enumerate with an expected delay of after an -time preprocessing, where is the sum of the input sizes of . Our set union sampling algorithm in Section 5.1 can be used to reduce their expected delay to . Random enumeration of for general (cyclic) joins was considered in [19]; we do not delve into their results further because they are subsumed by those of [13] on acyclic joins (i.e., this work’s focus).
Join reporting (rather than sampling) under selection has been investigated in [18, 34]. To explain their findings, fix a schema graph of a constant size (note: can be cyclic), together with a non-empty subset . Consider the task of creating a data structure on a join instance of such that, when supplied with a tuple over , the structure can report efficiently. The focus of [18, 34] is to study the relationships between the space consumption – denoted as – of the structure and the time of computing . Targeting “output-sensitive” algorithms that report in time where , Zhao et al. [34] derived smooth tradeoffs between and . In Section 6, we complement their results by identifying scenarios where and are possible, provided that the time of reporting is allowed to be expected. The work of [18], on the other hand, concerns a different form of tradeoffs that do not bear direct relevance to our results.
3 Preliminaries
3.1 Weighted Sampling
Let be a set of elements, denoted as , where each () carries a positive integer weight . Set . A weighted sampling operation returns a random element such that for each . The alias method [31] supports such an operation in time, after creating a data structure of space over in time. We will refer to the structure as the alias structure.
3.2 Acyclic Join Sampling without Selections
This subsection outlines how to build a structure over an acyclic join that uses space and can extract a uniformly random tuple from in time.
Take a join tree of the schema graph of . Root at an arbitrary node (recall that each node of is a hyperedge in ) – doing so enables us to speak about the “parents” and “children” of the nodes in . Given a node in , we denote by the subtree of induced by all the descendants of (note: is a descendant of itself). Define
| (4) |
where is the (only) relation in whose schema is . We will refer to as a subjoin. The schema graph of is where and .
Given a tuple , let us examine , namely, the result tuples of the subjoin to which “contributes”. Define the subjoin weight of as
| (5) |
where is given in (4). The following is implicit from the arguments in [13, 35]. In Appendix A, we present an explicit proof for completeness.
Lemma 10.
For any node of , the subjoin weights of all the tuples in can be computed in expected time.
The next lemma, again proved in Appendix A, can be deployed to build a sampling structure at every node of .
Lemma 11.
Let be an acyclic join with schema graph , be a rooted join tree of , and be a node of . We can build in expected time a structure of space that, given any , can draw a uniformly random tuple from in time.
It is now a simple exercise to design the overall structure for sampling from . Notice that is decomposed into a collection of sets: . Random sampling from can be performed in two steps. First, obtain a random tuple such that for each . As , this is an instance of weighted sampling once the value of every has been calculated from Lemma 3.2. An alias structure on allows us to obtain in time. Second, apply Lemma 3.2 to draw a uniformly random tuple from in time.
3.3 Properties of Ext--Connexity
In this subsection, we first introduce several concepts related to ext--connexity that will aid our exposition in later sections. Then, we review two key properties of ext--connexity essential for our technical development.
Definition 12.
Let be an acyclic schema graph, be a subset of , and be a join tree of . We say that is -canonical if there is a hyperedge satisfying . An edge of is -breakable if (note that and are hyperedges of ; and represents an undirected edge in ).
Definition 13.
Let be an acyclic schema graph, be a subset of , and be a join tree of . Suppose that removing all the -breakable edges of partitions into subtrees , , …, . For each , define a hypergraph with and Each is a -component of .
Given a schema graph , we call two distinct vertices neighbors if they appear together in at least one hyperedge. A path in is a sequence of distinct vertices such that and are neighbors for all .
Definition 14.
Let be an acyclic schema graph and be a subset of . A -path is a path (where ) such that (i) and belong to , but they are not neighbors; (ii) for every .
Example 15.
Consider the schema graph in Figure 1(a). Among the choices of in Example 1.2, is -canonical only for and .
Set to ACEHF. The edge {ABC, CDE} of is -breakable because is a subset of . On the other hand, the edge {EGH, GI} is not -breakable because is not a subset of . By removing the -breakable edges, we obtain four subtrees of : the first contains nodes and , while every other node (i.e., , , and ) forms a subtree by itself. Accordingly, has four -components: , , , and . There are no -paths in .
Set instead to ACEHI. The path A, B, D, G, I is a -path.
Lemma 16 ([5, Lemma 23]).
Let be an acyclic schema graph and be a subset of . The following statements are equivalent:
-
1.
is ext--connex.
-
2.
does not have a -path.
-
3.
Let be an arbitrary join tree of . Denote by the number of -components of , and by the -th component. For every , is -canonical.
Example 17.
Continuing Example 3.3, for , as mentioned has -components: , , , and . For every , is -canonical; hence, is ext--connex. However, for , as mentioned there is a -path in , which is thus not ext--connex.
4 A Dichotomy on -Sampling
Theorem 4 involves a negative result (the “only-if direction”) and a positive result (the “if direction”). We will prove the negative result in Section 4.1. The positive result is a corollary of Theorem 5, whose proof is presented in Section 4.2.
4.1 The Only-If Direction of Theorem 4
As explained in Section 1.2, the input to the set disjointness problem comprises sets . Given distinct integers , a disjointness query returns whether . The SSD-conjecture states that any structure promising to answer such a query in time must use space, where .
Suppose that the only-if claim of Theorem 4 is false. Thus, there exist a hypergraph and a subset such that is not ext--connex but a feasible -sampling structure exists. We will show how to build a set-disjointness structure of space that answers a disjointness query in time, thus breaking the SSD-conjecture. Since is not ext--connex, it must have a -path by Lemma 3.3. Pick an arbitrary -path: for some . Remember that no vertex on the path belongs to except and .
From (the input to set disjointness), next we create a join whose schema graph is . For each hyperedge , construct a relation with schema as follows. For each integer and every element , insert a tuple into (provided that is not already in ) such that (i) if , then ; (ii) if , then ; (iii) for every attribute , set where is a special symbol; (iv) for every attribute , set . By Definition 3.3, no hyperedge of covers both and ; hence, at most one step between (i) and (ii) applies. The relation thus designed has a size at most .
Lemma 18.
Given distinct , define a tuple over with , , and for every . Then, if and only if .
Proof.
Let us first prove the “if direction” (). Consider an arbitrary tuple . Clearly, and . Since is a -path, there exist hyperedges in such that (i) , (ii) for each , and (iii) . We argue:
| (6) |
To see why, fix any . As , we know . As and are attributes outside , our construction of ensures . The fact that this holds for every proves (6). By the construction of , is an element of and is an element of . Therefore, (6) tells us .
Next, we prove the “only if direction” (). Fix any . Construct a tuple over where for every , and for every . It suffices to prove that . Indeed, this is true because, for every , our construction explicitly inserts tuple into , noticing that cannot contain both and .
The above lemma can be used to break the SSD-conjecture. Note that the input size of satisfies , thus allowing us to build a structure of space on to support a -sampling operation in time. A disjointness query parameterized by integers can be answered as follows. First, create a tuple over such that , , and for every . Then, issue a -sampling operation and declare if and only if the operation returns nothing. This correctly answers the query in time.
4.2 Proof of Theorem 5 (a.k.a. The If-Direction of Theorem 4)
Let be a schema graph and be a subset of . Given a join instance of , we want to build a structure of space in expected time to support a -sampling operation on in constant time.
Case 1: Is -Canonical.
By Definition 3.3, in this case there is a hyperedge satisfying . Now, root at , apply Lemma 3.2 to calculate the subjoin weights of all tuples in (the relation in with schema ), and build a sampling structure of Lemma 3.2 by setting the symbol there to . Given a tuple over , define
| (7) |
It is easy to verify
| (8) |
Random sampling from can be done in a two-step approach similar to what was described at the end of Section 3.2. First, obtain a random tuple such that, for each , we have
where is the subjoin weight of (with respect to the rooted ); see (5). As , this is an instance of weighted sampling since the value of every is already available. Hence, an alias structure on allows us to obtain in constant time. Second, apply Lemma 3.2 to draw a uniformly random tuple from in constant time.
The alias structure on occupies space and can be built in time, as explained in Section 3.1. We do this for every , where is projection in relational algebra. For distinct , the sets and are disjoint. Hence, in total, all the alias structures occupy space and can be built in expected time (the time is expected because hashing is required to obtain the sets of each ).
Remark.
We make an observation here that will be useful in Section 5.2. For each , as mentioned . Since the subjoin weights of all tuples in are available, we can compute the sizes of all by scanning once in time. Storing all those sizes takes extra space. As the benefit, given any tuple over , we can obtain in time with hashing.
Case 2: Is Not -Canonical.
Let be an arbitrary join tree of . As is ext--connex, by Lemma 3.3, defines a number of -components of – denoted as , respectively – such that is -canonical for every . For each , define and . Note that is a join instance of the schema graph , which is -canonical. Denote by the input size of ; we have because every relation of appears in exactly one of . For each , we build a -sampling structure on in the way explained for Case 1. All the structures occupy space in total and can be built in expected time.
Consider now a -sampling operation, which is given a tuple over . For each , defining , we perform a -sampling operation using ; let us assume that this operation returns . If is nil for any , we return nil for the original -sampling operation. Otherwise, return , which, as explained in Appendix B, must be a uniformly random tuple of . The -sampling operation takes time overall. This concludes the proof of Theorem 5.
5 -Sampling Algorithms
The crux of our solution to Problem 2 is an algorithm optimally settling the set union sampling problem (see Section 1.2). We will present this algorithm in Section 5.1 and explain in Section 5.2 how it leads to a structure for Problem 2 that establishes Theorem 7. Finally, Section 5.3 gives the proof of Theorem 8.
5.1 Set Union Sampling
Recall from Section 1.2 that, in this problem, we have a collection of sets , each supporting three -time operations: size, membership, and sample. The objective is to draw an element from uniformly at random.
As before, set and . For each element , define its inverted set as , i.e., the “ids” of the sets containing . The degree of is . Let us first describe a baseline method, which was given explicitly in [4] and implicitly in [13], before presenting our new ideas.
Baseline Method.
Draw a random value such that for each . Then, use the sampling operation to draw an element from . Finally, carry out an acceptance step:
Acceptance Step: Accept with probability .
If accepted, is returned; otherwise, the algorithm repeats from scratch.
The algorithm correctly returns a uniform sample of . To see why, fix an arbitrary element . This element is returned if and only if three conditions are satisfied: (i) , which occurs with probability for each , (ii) , which occurs with probability conditioned on , and (iii) is accepted, which occurs with probability conditioned on . Hence, the algorithm outputs with probability
| (9) |
which is identical for all . As a corollary, in each repeat, the algorithm succeeds in returning a sample with probability . Thus, repeats are needed in expectation.
The random value can be easily obtained in time. One (logically simple) way to do so is to build an alias structure (see Section 3.2) on – namely, the set sizes – on the fly in time (using the size operation), after which can be extracted from the structure in time. The time to obtain is (using the sampling operation). The troublemaker, however, is the acceptance step. The standard approach [4, 13] is to query the membership of in each (), which costs time. This renders the overall sample time in expectation.
New Idea: Total Law of Expectation.
Our objective is to implement the acceptance step in expected time – note that this is faster than by a factor of , which eventually allows us to bring the expected sample time from down to .
Let us start with a fundamental fact:
Proposition 19.
Let be a random variable taking values from , and be a uniformly random variable over . If and are independent, then .
The proposition is the total law of expectation in disguise; see Appendix C for a proof. Equipped with the above, implementing the acceptance step boils down to:
Given and , generate a rand. var. with .
Later, we will explain how to achieve the above purpose in expected time. Once done, we can perform the acceptance step as follows (recall that, prior to this, the baseline method has obtained the values of two random variables and ): (i) generate a uniformly random variable over in constant time; (ii) generate the aforementioned random variable (setting and to the values of and , respectively) in expected time; (iii) accept if .
Correctness follows from Proposition 5.1 (the acceptance probability is , as desired). For a particular and a particular , we have Thus, the expected cost of the acceptance step will be at the order of
| (10) | |||||
where the last step used and .
Generation of .
Next, we will concentrate on the -generating task defined earlier. Recall that the generation is based on a given set “id” and an element . Consider the procedure below:
In plain words, if , the value returned is a random variable giving the number of failed WoR-sampling operations before finding another set containing ; otherwise, . We prove in Appendix C:
Proposition 20.
.
Combining the above with the obvious fact that , we can now define as the desired random variable satisfying and . The WoR-sampling operation at Line 3 can be implemented in time; see, e.g., [13]. Thus, the cost of find-2nd is proportional to the value returned. It follows from Proposition 5.1 that the expected cost of find-2nd is .
Total Cost of Set-Union Sampling.
Recall that, in the (original) baseline algorithm, each repeat takes time; as the number of repeats is expected, the total cost of taking one sample from is . By applying our remedy, one repeat of the baseline algorithm is now carried out in expected time (see the analysis in (10)). The remedy does not affect the expected number of repeats (i.e., ), which suggests that the overall expected sample time should be . Indeed, if represents the expected time for our algorithm to acquire a sample from , we can write , which solves to . This completes the proof of Theorem 6.
Remark.
The proposed algorithm is reminiscent of that of Karp, Luby, and Madras [26], which was designed to estimate rather than to sample from . It can, in fact, be incorporated into their framework to perform the estimation in a slightly simpler way. Our key novelty lies in employing Proposition 5.1 for the acceptance step, while Karp, Luby, and Madras [26] used a more sophisticated method that is tailored for estimation and does not extend to sampling.
5.2 A Structure for Problem 2
Denote by a join instance of a schema graph . We prove Theorem 7 on the -sampling problem with a reduction to set union sampling. Recall that is ext--connex for every . Hence, we can create a structure of Theorem 5 on every that supports -sampling in time. As each structure occupies space, it may appear as if the total space would be . This is not true. To see why, note that every is a subset of ; as has a constant size, the number of its subsets is bounded by a constant. This means that there can be only a constant number of distinct subsets among . Physically, one structure of Theorem 5 suffices for each distinct subset; hence, the total space is . For similar reasons, all the structures can be constructed in expected time.
A -sampling operation is given tuples over , respectively. For each , define . The operation, essentially, aims to return a uniformly-random tuple from . To cast this as an instance of set union sampling, we need to implement each of the size, membership, and sampling operations in time on each . This is trivial for two operations:
-
sampling: use to perform a -sampling operation;
-
membership: given a tuple over , this operation checks whether . This requires checking if is in the relevant relation of for each and if for each . All the checking can be done in time.
We can support also the size operation in time by using directly . The ideas are similar to those explained in Section 4.2 and deferred to Appendix C. Our algorithm in Theorem 6 can now be utilized to extract a uniformly-random tuple from in expected time, thus completing the proof of Theorem 7.
5.3 Proof of Theorem 8
The SSD conjecture given in Section 1.2 concerns data structures that solve the set disjointness problem with deterministic query time (here, a “query” is given and reports whether ). To prove Theorem 8, we will first argue that a similar conjecture still stands even on set-disjointness structures with expected query time.
Expected SSD Conjecture. Let be sets whose elements are integers. Define . We want to preprocess these sets into a data structure of space such that, given any distinct integers , we can report in expected time whether . The expected SSD conjecture states that must be .
Lemma 21.
The SSD conjecture implies the expected SSD conjecture.
Proof.
Suppose that we can build a structure of space and expected query time for the set disjointness problem. We will prove the existence of a set-disjointness structure of space and deterministic query time. This establishes the claim in Lemma 5.3 because , implying .
Fix any distinct integers . Let be the algorithm associated with for deciding whether . Denote by the cost for . Note that is a random variable with . By Markov’s inequality, . Imagine running for times. The probability for all those runs to take at least time to terminate is at most . In other words, with probability at least , at least one run finishes within time.
In general, a randomized algorithm becomes deterministic once all the random bits are fixed. “Running ” is equivalent to (i) first fixing a sequence of random bits, and (ii) then executing deterministically the instructions of based on . If finishes within time, it consumes at most words of random bits. Now, take random bit sequences , each of which is words long. For each , define as the deterministic algorithm that runs based on , with the modification that terminates itself after having run for a duration of time. As per our earlier discussion, with probability at least , at least one of manages to report whether .
Now, let us consider all distinct pairs of . With probability at least , for every pair of and , at least one of manages to report whether – let us call a working set. As the probability is greater than 0 (because ), a working set must exist.
Our final set-disjointness structure consists of and a working set of random-bit sequences , the total space of which is . Given two distinct integers , we run all of , at least one of which manages to report whether . The total query time is .
Our argument in Section 4.1 is a reduction from set disjointness to -sampling where is not ext--connex, and works regardless of whether the -sampling algorithm is randomized or not. The reduction shows that if there is a structure of space that can support a -sampling operation in expected time, then there is a set-disjointness structure of space and expected query time – this will break the expected SSD conjecture.
Let us return to the context of Theorem 8 where . W.l.o.g., assume that is not ext--connex, and yet there is a structure of space that can support a -sampling operation in expected time. We will show how to use to support a -sampling operation in expected time, which, as pointed out earlier, breaks the expected SSD conjecture. In fact, this is fairly obvious. Suppose that we are given a tuple over . For each , construct a dummy tuple over such that, for each , is a value that does not appear in the relations of . Use to perform a -sampling operation and simply return the output of this operation.
6 Random Enumeration from Sampling
This section will establish a connection between random enumeration and uniform sampling and then leverage the connection to prove Theorem 9.
Reduction.
We consider a general setup. Let be a set of elements whose size is known. Two subroutines are at our disposal:
-
The first lists the elements of (in an order we cannot control) within time;
-
The second samples a uniformly random element of with replacement within time.
We will show that the elements of can be randomly enumerated with an expected delay of .
Let us first describe an algorithm that produces a random permutation of but does not ensure a small delay. The algorithm runs in three phases. In the first one, we use dynamic perfect hashing [20] to maintain a set of elements that have been found. Initially, . Then, we carry out iterations, each of which adds a new element to . Specifically, an iteration starts by randomly sampling an element from . If , we add it to and output it. Otherwise, the iteration re-samples from until getting an unseen element. As long as , an unseen element is sampled with probability at least . Hence, two samples suffice in expectation, and the cost of an iteration is expected. The first phase finishes after iterations. The second phase finds the entire , and extracts (with hashing) and randomly permutes (with Fisher-Yates shuffle [22]) . The phase performs at most atomic instructions of the RAM model for some constant . Finally, the third phase outputs by the permuted order.
We apply a de-amortization approach to implement the above algorithm with a small expected delay. In the first phase, every time an element is added to , we do not output it immediately, but instead append it to a buffer queue . We remove and output the head element of every
iterations so that still has elements left at the end of the first phase. In the second phase, we remove and enumerate the head of after every atomic instructions. The third phase simply enumerates the remaining elements in (by their queued order) and (by their permuted order) with a constant delay. Overall, the expected delay, which is determined by the first two phases, is bounded by It is rudimentary to show that the bound is .
Proof of Theorem 9.
Consider an input to the -sampling problem with a feasible pair . Given a tuple over , define . Our structure in Section 4.2 can be used to obtain the size in time. Theorem 5 ensures . As explained in Appendix D, it is possible to build a structure that uses space, can be built in expected time, and can report in time, implying . Using the reduction described earlier, we can randomly permute with an expected delay. This completes the proof of Theorem 9.
References
- [1] Serge Abiteboul, Richard Hull, and Victor Vianu. Foundations of Databases. Addison-Wesley, 1995.
- [2] Swarup Acharya, Phillip B. Gibbons, Viswanath Poosala, and Sridhar Ramaswamy. Join synopses for approximate query answering. In SIGMOD, pages 275–286, 1999. doi:10.1145/304182.304207.
- [3] Albert Atserias, Martin Grohe, and Daniel Marx. Size bounds and query plans for relational joins. SIAM J. of Comp., 42(4):1737–1767, 2013. doi:10.1137/110859440.
- [4] Martin Aumuller, Sariel Har-Peled, Sepideh Mahabadi, Rasmus Pagh, and Francesco Silvestri. Sampling a near neighbor in high dimensions – Who is the fairest of them all? TODS, 47(1):4:1–4:40, 2022. doi:10.1145/3502867.
- [5] Guillaume Bagan, Arnaud Durand, and Etienne Grandjean. On acyclic conjunctive queries and constant delay enumeration. In Computer Science Logic, pages 208–222, 2007. doi:10.1007/978-3-540-74915-8_18.
- [6] Guillaume Bagan, Arnaud Durand, Etienne Grandjean, and Frederic Olive. Computing the jth solution of a first-order query. RAIRO Theor. Informatics Appl., 42(1):147–164, 2008. doi:10.1051/ITA:2007046.
- [7] Karl Bringmann, Nofar Carmeli, and Stefan Mengel. Tight fine-grained bounds for direct access on join queries. TODS, 50(1):1:1–1:44, 2025. doi:10.1145/3707448.
- [8] Florent Capelli, Oliver Irwin, and Sylvain Salvati. A simple algorithm for worst case optimal join and sampling. In ICDT, pages 23:1–23:19, 2025. doi:10.4230/LIPIcs.ICDT.2025.23.
- [9] Nofar Carmeli and Markus Kroll. Enumeration complexity of conjunctive queries with functional dependencies. Theory Comput. Syst., 64(5):828–860, 2020. doi:10.1007/S00224-019-09937-9.
- [10] Nofar Carmeli and Markus Kroll. On the enumeration complexity of unions of conjunctive queries. TODS, 46(2):5:1–5:41, 2021. doi:10.1145/3450263.
- [11] Nofar Carmeli and Luc Segoufin. Conjunctive queries with self-joins, towards a fine-grained enumeration complexity analysis. In PODS, pages 277–289, 2023. doi:10.1145/3584372.3588667.
- [12] Nofar Carmeli, Nikolaos Tziavelis, Wolfgang Gatterbauer, Benny Kimelfeld, and Mirek Riedewald. Tractable orders for direct access to ranked answers of conjunctive queries. TODS, 48(1):1:1–1:45, 2023. doi:10.1145/3578517.
- [13] Nofar Carmeli, Shai Zeevi, Christoph Berkholz, Alessio Conte, Benny Kimelfeld, and Nicole Schweikardt. Answering (unions of) conjunctive queries using random access and random-order enumeration. TODS, 47(3):9:1–9:49, 2022. doi:10.1145/3531055.
- [14] Timothy M. Chan, Saladi Rahul, and Jie Xue. Range closest-pair search in higher dimensions. Computational Geometry, 91:101669, 2020. doi:10.1016/J.COMGEO.2020.101669.
- [15] Surajit Chaudhuri, Rajeev Motwani, and Vivek R. Narasayya. On random sampling over joins. In SIGMOD, pages 263–274, 1999. doi:10.1145/304182.304206.
- [16] Yu Chen and Ke Yi. Random sampling and size estimation over cyclic joins. In ICDT, pages 7:1–7:18, 2020. doi:10.4230/LIPIcs.ICDT.2020.7.
- [17] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. The MIT Press, 2001.
- [18] Shaleen Deep and Paraschos Koutris. Compressed representations of conjunctive query results. In PODS, pages 307–322, 2018. doi:10.1145/3196959.3196979.
- [19] Shiyuan Deng, Shangqi Lu, and Yufei Tao. On join sampling and the hardness of combinatorial output-sensitive join algorithms. In PODS, pages 99–111, 2023. doi:10.1145/3584372.3588666.
- [20] Martin Dietzfelbinger, Anna R. Karlin, Kurt Mehlhorn, Friedhelm Meyer auf der Heide, Hans Rohnert, and Robert Endre Tarjan. Dynamic perfect hashing: Upper and lower bounds. SIAM J. of Comp., 23(4):738–761, 1994. doi:10.1137/S0097539791194094.
- [21] Arnaud Durand. Fine-grained complexity analysis of queries: From decision to counting and enumeration. In PODS, pages 331–346, 2020. doi:10.1145/3375395.3389130.
- [22] Ronald Aylmer Fisher and Frank Yates. Statistical Tables for Biological, Agricultural and Medical Research. Edinburgh and London: Oliver and Boyd., 1953.
- [23] Isaac Goldstein, Tsvi Kopelowitz, Moshe Lewenstein, and Ely Porat. Conditional lower bounds for space/time tradeoffs. In WADS, pages 421–436. Springer, 2017. doi:10.1007/978-3-319-62127-2_36.
- [24] Isaac Goldstein, Moshe Lewenstein, and Ely Porat. On the hardness of set disjointness and set intersection with bounded universe. In ISAAC, pages 7:1–7:22, 2019. doi:10.4230/LIPIcs.ISAAC.2019.7.
- [25] Norman L Johnson, Adrienne W Kemp, and Samuel Kotz. Univariate Discrete Distributions. John Wiley & Sons, 2005.
- [26] Richard M. Karp, Michael Luby, and Neal Madras. Monte-carlo approximation algorithms for enumeration problems. J. Algorithms, 10(3):429–448, 1989. doi:10.1016/0196-6774(89)90038-2.
- [27] Mahmoud Abo Khamis, Hung Q. Ngo, and Dan Suciu. What do shannon-type inequalities, submodular width, and disjunctive datalog have to do with one another? In PODS, pages 429–444, 2017. doi:10.1145/3034786.3056105.
- [28] Kyoungmin Kim, Jaehyun Ha, George Fletcher, and Wook-Shin Han. Guaranteeing the (AGM/OUT) runtime for uniform sampling and size estimation over joins. In PODS, pages 113–125, 2023. doi:10.1145/3584372.3588676.
- [29] Shangqi Lu and Yufei Tao. Indexing for keyword search with structured constraints. In PODS, pages 263–275, 2023. doi:10.1145/3584372.3588663.
- [30] Mihai Patrascu and Liam Roditty. Distance oracles beyond the Thorup-Zwick bound. SIAM J. of Comp., 43(1):300–311, 2014. doi:10.1137/11084128X.
- [31] Alastair J. Walker. New fast method for generating discrete random numbers with arbitrary frequency distributions. Electronics Letters, 10(8):127–128, 1974.
- [32] Ru Wang and Yufei Tao. Join and subgraph sampling under degree constraints. JCSS, 155, 2026. doi:10.1016/J.JCSS.2025.103693.
- [33] Mihalis Yannakakis. Algorithms for acyclic database schemes. In VLDB, pages 82–94, 1981.
- [34] Hangdong Zhao, Shaleen Deep, and Paraschos Koutris. Space-time tradeoffs for conjunctive queries with access patterns. In PODS, pages 59–68, 2023. doi:10.1145/3584372.3588675.
- [35] Zhuoyue Zhao, Robert Christensen, Feifei Li, Xiao Hu, and Ke Yi. Random sampling over joins revisited. In SIGMOD, pages 1525–1539, 2018. doi:10.1145/3183713.3183739.
Appendix A Supplementary Proofs for Section 3.2
A.1 Proof of Lemma 3.2
We first review a property of acyclic joins that can be used to compute, for each tuple , the result of in a manner reminiscent of Cartesian products. W.l.o.g., suppose that has child nodes in denoted as . Define for each . Consider the set of tuples created by the procedure below:
The next lemma is a well-known property of Yannakakis’ algorithm [33].
Lemma 22 ([1, 33]).
Both statements below are true regarding children-prod: (i) Line 4 always adds a new tuple to , and (ii) .
We are now ready to prove Lemma 3.2 by induction. If (the node given in the lemma) is a leaf of the (rooted) , then includes only . In this case, and . Hence, the lemma holds on .
Now, consider to be an internal node of . W.l.o.g., suppose that has child nodes in denoted as . Assuming inductively that the lemma is correct on of every , next we will prove the correctness on as well. For each , define .
Let be an arbitrary tuple in . According to Lemma A.1, we have
where , as defined at Line 2 of children-prod. For each , we will show how to construct in expected time a structure of space from which we can obtain for every in time. This will prove Lemma 3.2 because .
For each , define
| (11) |
We can now write
| (12) | |||||
We can obtain with one scan over because the subjoin weights of the tuples in have been computed (by induction). To identify for , we create a perfect-hashing structure on in expected time, after which can be retrieved in time.
We divide into equivalent classes based on the tuples’ projections onto . If two tuples and are in the same equivalent class, the numbers and are identical. Hence, it suffices to compute for one (arbitrary) representative of each equivalent class. On the other hand, if and are from different equivalent classes, the sets and are disjoint. Hence, we can compute the values for all representatives in time
A.2 Proof of Lemma 3.2
We prove the lemma by induction. The base case where is a leaf of is trivial and omitted.
Now, consider to be an internal node of . W.l.o.g., suppose that has child nodes in denoted as . Assuming inductively that the lemma is correct on of every , next we will prove the correctness on as well. For each , define .
Let be an arbitrary tuple in . According to Lemma A.1, there is a one-one correspondence between and where , as defined at Line 2 of procedure children-prod. Hence, to take a uniform random tuple from , we can take a uniformly random tuple from for each . Next, we will show how to construct in expected time a structure of space from which we can sample from in time. This will prove Lemma 3.2 because .
For each , define as in (11). We can now write
Random sampling from can be performed in two steps. First, obtain a random tuple such that for each . Second, draw a uniformly random tuple from .
Since the second step can be supported in time by induction, we focus on the first step. As , the first step is an instance of weighted sampling once the of every has been computed using Lemma 3.2 in expected time. An alias structure on allows us to obtain in constant time. The structure can be built in time. It suffices to build such a structure for every “equivalent class” of (as defined in Section A.1). All those structures take time to construct in total and occupy space overall.
Appendix B Correctness of Our Sampling Algorithm in Case 2 of Section 4.2
Consider the following procedure:
The correctness of our sampling algorithm for Case 2 of Section 4.2 is a corollary of:
Lemma 23.
Both statements below are true about component-prod: (i) Line 4 always adds a new tuple to , and (ii) .
Proof.
Proof of Statement (i).
For each , take an arbitrary tuple . We will first show . For this purpose, it suffices to prove that, for any distinct with , it must hold that for any attribute .
As (resp., ), there is a hyperedge (resp., ) containing . As and are in different -components – recall that is a join tree of – the (unique) simple path between them on must cross at least one -breakable edge, denoted as . The connectedness property of ensures that . By the definition of -breakable edge, must be an attribute in , indicating that the tuple has a -value. As and , we must have .
To prove the statement, it remains to show that Line 4 never adds to the same tuple twice. Consider any two executions of Line 4: the first with for , and the second with for . There exists at least one such that . This further indicates the existence of an attribute such that . We can now assert that and must differ in their -values.
Proof of Statement (ii).
We will first prove
| (13) |
Take any tuple . Define for each . To show (13), it suffices to prove for every . We achieve the purpose by arguing that for any . As every relation of belongs to , we know . We can thus infer from . On the other hand, because . It thus follows that .
Next, we will prove
| (14) |
For each , take an arbitrary tuple . Define . Our proof of statement (i) has explained that cannot be empty. To prove (14), our goal is to show that . We first argue that , i.e., for every . Indeed, as must belong to for some , we know , which is in because . To prove , we still have to show for every . For this purpose, simply identify any satisfying (this exists because ). That follows from the fact .
Appendix C Supplementary Content for Section 5
Proof of Proposition 5.1.
For any random variables and , the total law of expectation states To prove the proposition, define to be 1 if or 0 otherwise; furthermore, set . Clearly, by the independence of and . We can now derive
Proof of Proposition 5.1.
If , then is deterministically , in which case the lemma clearly holds.
The rest of the proof considers . In that scenario, can be rephrased in the following conventional WoR-sampling setup. Suppose that we have balls, among which ones are red and the rest are white. Uniformly sample a ball WoR until seeing a red ball, and gives the number of white balls sampled in this process. It is well known (e.g., see [25]) that follows the Negative Hypergeometric distribution with
Supporting the Size Operation in Section 5.2.
Let be a schema graph and be a subset of such that is ext--connex. Let be a join instance of . Suppose that we have built a structure of Theorem 5 on based on the description in Section 4.2. Given any tuple over , we will show how to use to get the size in constant time. In fact, we have already discussed this in the scenario where is -canonical – see the remark in Section 4.2. Next, we consider that is not -canonical.
As is ext--connex, by Lemma 3.3, any join tree of defines a number of -components of – denoted as , respectively – such that is -canonical for every . For each , define and . Note that is a join instance of , which is -canonical. For each , the structure includes a structure of Theorem 5 on every (); denote that structure as . Now, consider a tuple over whose is to be computed. For each , we define and use to obtain the size in time (this is doable because is -canonical). Finally, we return . The correctness is guaranteed by Lemma B.
Appendix D Completing the Proof of Theorem 9
Let be an acyclic schema graph and be a join instance of . Yannakakis’ algorithm [33] outputs in time. The algorithm allows one to specify a source relation . In an -time preprocessing stage, it
-
converts into a fully-reduced state, i.e., eliminating every tuple with (i.e., does not “contribute” to the join result);
-
creates a structure that, given any tuple , can report in time.
Let be a subset of such that is ext--connex. It should have become fairly straightforward to combine the above and the discussion in Section 5.2 to obtain a structure with the following guarantees: it uses space, can be built in expected time, and when given a tuple over , can report in time.
Case 1: is -Canonical.
By Definition 3.3, there is a hyperedge satisfying . Run the preprocessing stage of Yannakakis’s algorithm to create the aforementioned structure by specifying as the source relation (recall that this is the relation in whose schema is ). The preprocessing leaves in a fully-reduced state. Given a tuple over , we identify with hashing in constant time the set as defined in (7). Then, for each , use to report (which must be non-empty).
Case 2: Is Not -Canonical.
Let be an arbitrary join tree of , which defines a number of -components of – denoted as , respectively – such that is -canonical for every (see Lemma 3.3). For each , define and . Note that is a join instance of the schema graph , which is -canonical. For each , build a structure on in the way explained for Case 1. Consider now a tuple over . For each , defining , we use to extract . Then, for every , output .
