Abstract 1 Introduction 2 Related Work 3 Preliminaries 4 A Dichotomy on (𝓖,𝓩)-Sampling 5 (𝓖,⋁𝒊=𝟏𝒎𝓩𝒊)-Sampling Algorithms 6 Random Enumeration from Sampling References Appendix A Supplementary Proofs for Section 3.2 Appendix B Correctness of Our Sampling Algorithm in Case 2 of Section 4.2 Appendix C Supplementary Content for Section 5 Appendix D Completing the Proof of Theorem 9

Acyclic Join Sampling Under Selections: Dichotomy, Union Sampling, and Enumeration

Jinchao Huang ORCID The Chinese University of Hong Kong, Hong Kong    Yufei Tao ORCID The Chinese University of Hong Kong, Hong Kong    Sibo Wang ORCID The Chinese University of Hong Kong, Hong Kong
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 O~⁢(IN) space and supports sampling in O~⁢(1) 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 O⁢(IN) space and O⁢(1) 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 Bounds
Copyright and License:
[Uncaptioned image] © Jinchao Huang, Yufei Tao, and Sibo Wang; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation → Data structures and algorithms for data management
Funding:
This work was supported in part by GRF Projects 14217322 and 14222822.
Editors:
Balder ten Cate and Maurice Funk

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 (A=a∧B=b)∨(C=c). Here, the values a, b, and c 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 x≥1, we define [x]={1,2,…,x}. All logarithms have base 2 by default. Our analysis assumes the standard word-RAM model [17] where a word has length Θ⁢(log⁡IN) with IN being the input size. Given an integer x representable using a word, we assume that a uniformly random number can be drawn from [x] in constant time.

1.1 Problem Definitions

Let 𝐚𝐭𝐭 be a set whose elements are called attributes. Given a subset U⊆𝐚𝐭𝐭, a tuple over U is a function 𝒖:U→ℕ. For each attribute X∈U, we call 𝒖⁢(X) “the value of 𝒖 under X” or simply the “X-value” of 𝒖 – such a value is assumed to fit in a word. Given a subset U′ of U, we use 𝒖⁢[U′] (note: square bracket here) to represent the tuple 𝒗 over U′ satisfying 𝒗⁢(X)=𝒖⁢(X) for all X∈U′; the tuple 𝒖⁢[U′] is called the projection of 𝒖 onto U′. We define a relation as a set R of tuples over an identical set U of attributes, where U is called the schema of R, written as 𝑠𝑐ℎ𝑒𝑚𝑎⁢(R)=U.

Let 𝒱 be a finite subset of 𝐚𝐭𝐭 and ℰ be a subset of 2𝒱. 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 X∈𝒱, the nodes in 𝒯 (a.k.a. hyperedges in ℰ) containing X form a connected subtree in 𝒯.

We refer to 𝒯 as a join tree of 𝒢.

(a) Join tree of an acyclic graph 𝒢=(𝒱,ℰ).
(b) Join tree of the graph (𝒱,ℰ∪{ACEHF}).
Figure 1: Acyclicity and connexity.
Example 1.

Consider the hypergraph 𝒢=(𝒱,ℰ) where 𝒱={A,B,…,J}, and ℰ has 5 hyperedges: e1=EGH (shortform for set {E,G,H}), e2=CDE, e3=GI, e4=ABC, and e5=EFJ. The hypergraph is acyclic, with a join tree 𝒯 given in Figure 1(a). The hyperedges containing attribute, for instance, E are e1,e2, and e5, and they form a connected subtree of 𝒯. As another example, the hyperedges containing G are e1 and e3, 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 e∈ℰ, there is a unique relation Re∈𝒬 with 𝑠𝑐ℎ𝑒𝑚𝑎⁢(Re)=e. We call 𝒢 the “schema graph of 𝒬”. The result of the join is a relation over 𝒱 defined as:

𝐽𝑜𝑖𝑛⁢(𝒬) = {tuple 𝒖 over ⁢𝒱∣∀R∈𝒬,𝒖⁢[𝑠𝑐ℎ𝑒𝑚𝑎⁢(R)]∈R}. (1)

The input size of 𝒬 is defined as IN=∑R∈𝒬|R|. If 𝒢 is acyclic, we refer to 𝒬 as an acyclic join; otherwise, it is a cyclic join.

If the relations in 𝒬 can be listed as R1,R2,…,R|𝒬|, we may also represent 𝐽𝑜𝑖𝑛⁢(𝒬) as R1⋈R2⋈…⋈R|𝒬|. If a relation, say, R1 consists of a single tuple 𝒖, we may replace it with 𝒖 in a join expression, e.g., 𝒖⋈R2⋈…⋈R|𝒬|.

(𝓖,𝓩)-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

σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) = {𝒖∈𝐽𝑜𝑖𝑛⁢(𝒬)∣∀Z∈𝒵,𝒖⁢(Z)=𝒛⁢(Z)} (2)

namely, σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) comprises the tuples 𝒖 in the join result satisfying the conjunctive condition ⋀Z∈𝒵𝒖⁢(Z)=𝒛⁢(Z). 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 O~⁢(IN) space and supports a sampling operation in O~⁢(1) time where the notation O~(.) hides a factor polylogarithmic to IN.

(𝓖,⋁𝒊=𝟏𝒎𝓩𝒊)-Sampling.

Fix a schema graph 𝒢=(𝒱,ℰ) whose size is assumed to be a constant, as well as m subsets 𝒵1, 𝒵2,…,𝒵m of 𝒱 for some m≥1. Let 𝒬 be a join instance of 𝒢. Given m tuples 𝒛1,𝒛2,…,𝒛m where 𝒛i is over 𝒵i for each i∈[m], define

σ𝒛1∨…∨𝒛m⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) = ⋃i=1mσ𝒛i⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) (3)

where σ𝒛i⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) is as defined in (2). In other words, σ𝒛1∨…∨𝒛m⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) includes all and only the tuples 𝒖 in the join result that satisfy a condition given in a disjunctive normal form: ⋁i=1m(⋀X∈𝒵i𝒖⁢(X)=𝒛i⁢(X)). The value of m is permitted to be arbitrarily greater than 2|V|, implying that 𝒵1, 𝒵2,…,𝒵m 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 𝒵1 and 𝒵2 are {job}..

Given m tuples 𝒛1,𝒛2,…,𝒛m, a (𝒢,⋁i=1m𝒵i)-sampling operation returns a tuple chosen from σ𝒛1∨…∨𝒛m⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) uniformly at random, or returns nil if σ𝒛1∨…∨𝒛m⁢(𝐽𝑜𝑖𝑛⁢(𝒬))=∅.

Problem 2.

Preprocess 𝒬 into a data structure that can support (𝒢,⋁i=1m𝒵i)-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 (𝒢,⋁i=1m𝒵i)-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 S1,S2,…,Sm be m≥2 sets whose elements are integers. Define N=∑i=1m|Si|. In the set disjointness problem, we want to preprocess these sets into a data structure of 𝒮 space such that, given any distinct integers a,b∈[m], we can report in 𝒯=o⁢(N) time whether Sa∩Sb=∅. The SSD conjecture states that 𝒮 must be Ω⁢(N2/(𝒯2⁢polylogN)).

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. The table below lists the answers to “is 𝒢 ext-𝒵-connex?” for several representative choices of 𝒵.

𝒵 ∅ E AE ACE ACEH ACEHI ACEHF
ext-Z-connex? yes yes no yes yes no yes

Figure 1(b) shows a join tree of the hypergraph (𝒱,ℰ∪{ACEHF}), which serves as evidence that (𝒱,ℰ∪{ACEHF}) 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 L be an ordering of the attributes in 𝒱: X1,X2,…,X|V|. This L defines a lexicographic order ≺ on the tuples in 𝐽𝑜𝑖𝑛⁢(𝒬): 𝒖𝟏≺𝒖𝟐 if there is an i∈[|V|] such that 𝒖𝟏⁢(Xi)<𝒖𝟐⁢(Xi) but 𝒖𝟏⁢(Xj)=𝒖𝟐⁢(Xj) for all 1≤j≤i−1. Given an integer t, a direct access (DA) query [6, 7, 12, 13] returns the t-th tuple of 𝐽𝑜𝑖𝑛⁢(𝒬) under ≺ if t∈[|𝐽𝑜𝑖𝑛⁢(𝒬)|], or nil otherwise. As shown in [12], if 𝒢 is ext-𝒵-connex, we can find an order L such that

  • ■

    the attributes of 𝒵 form a prefix of L, i.e., Xi∈𝒵 for every i∈[|𝒵|];

  • ■

    one can build a structure of O⁢(IN) space that answers any DA query in O⁢(log⁡IN) time.

Fix an arbitrary tuple 𝒛 over 𝒵. Crucially, the tuples in σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) must be consecutive under ≺ (because 𝒵 is a prefix of L). Thus, if σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬))≠∅, there exist integers t1 and t2 such that a tuple is in σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) if and only if it is the t-th tuple under ≺ for some t∈[t1,t2]. With O⁢(log⁡IN) DA queries, one can obtain the values of t1 and t2 (or certify their non-existence) via binary search. Then, a sample of σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) can be drawn by generating a random t∈[t1,t2] and issuing one more DA query. This supports a (𝒢,𝒵)-sampling operation in O⁢(log2⁡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 O⁢(IN) space in O⁢(IN) expected time such that a (𝒢,𝒵)-sampling operation on 𝒬 can be supported in O⁢(1) time.

Problem 2 ((𝓖,⋁𝒊=𝟏𝒎𝓩𝒊)-Sampling).

Our core contribution to this problem is a new algorithm for a fundamental sampling problem:

Set Union Sampling: Let S1,S2,…,Sm be m≥2 sets of elements drawn from a certain domain. Each Si (i∈[m]) supports three operations in constant time:

  1. 1.

    (size) return |Si|;

  2. 2.

    (membership) check whether a given element is in Si;

  3. 3.

    (sampling) return an element of Si chosen uniformly at random.

The goal is to sample an element from ⋃i=1mSi 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 O⁢(m) expected sample time.

Currently the fastest expected sample time [13, 4] – as will be reviewed in Section 2 – is O⁢(min⁡{m2,m⁢log2⁡N}) where N=∑i=1m|Si|, which we strictly improve. By combining Theorems 5 and 6 with additional ideas, we will prove:

Theorem 7.

Consider (𝒢,⋁i=1m𝒵i)-sampling where 𝒢 is ext-𝒵i-connex for all i∈[m]. Given a join instance 𝒬 of 𝒢, we can build a structure of O⁢(IN) space in O⁢(IN) expected time such that a (𝒢,⋁i=1m𝒵i)-sampling operation on 𝒬 can be supported in O⁢(m) expected time.

The connexity requirement is necessary for m≤IN0.49, as formally stated below:

Theorem 8.

Consider (𝒢,⋁i=1m𝒵i)-sampling where m≤IN0.49. If 𝒢 is not ext-𝒵i-connex for an arbitrary i∈[m], no structures of O~⁢(IN) space can guarantee O~⁢(m) 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 O⁢(IN) space in O⁢(IN) expected time such that, given any tuple 𝐳 over 𝒵, we can randomly enumerate σ𝐳⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) with an expected delay of O⁢(1).

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 Q is acyclic, Zhao et al. [35] described an O⁢(IN)-space structure ensuring O⁢(1) sample time. The problem becomes more challenging when 𝒬 is cyclic. Improving over [16, 19], Kim et al. [28] presented a structure of O⁢(IN) space that can guarantee a sample time of O⁢(AGM/max⁡{1,OUT}), where AGM is the join’s AGM bound [3], and OUT=|𝐽𝑜𝑖𝑛⁢(𝒬)|. For a broad class of joins with “degree constraints”, Wang and Tao [32] managed to reduce the sample time to O⁢(𝑝𝑜𝑙𝑦𝑚𝑎𝑡/max⁡{1,OUT}), 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 O⁢(m⋅(N/n)) where n=|⋃i=1mSi| and N=∑i=1m|Si| (Section 5.1 will discuss this algorithm in detail). Note that N can reach m⁢n (this happens when S1=S2=…=Sm), in which case the sample time becomes O⁢(m2). In [4], Aumuller et al. presented another algorithm to extract, in O⁢(m⁢log2⁡N) 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 O⁢(log⁡IN), after an O⁢(IN⁢log⁡IN)-time preprocessing. In Theorem 9, we settle a more general problem (i.e., random enumeration of σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬))) with an expected delay of O⁢(1). Given m acyclic joins 𝒬1,𝒬2,…,𝒬m whose results have the same schema, Carmeli et al. [13] described a way to randomly enumerate ⋃i=1m𝐽𝑜𝑖𝑛⁢(𝒬i) with an expected delay of O⁢(m2⁢log⁡IN𝑎𝑙𝑙) after an O⁢(IN𝑎𝑙𝑙⁢log⁡IN𝑎𝑙𝑙)-time preprocessing, where IN𝑎𝑙𝑙 is the sum of the input sizes of 𝒬1,…,𝒬m. Our set union sampling algorithm in Section 5.1 can be used to reduce their expected delay to O⁢(m⁢log⁡IN𝑎𝑙𝑙). Random enumeration of ⋃i=1m𝐽𝑜𝑖𝑛⁢(𝒬i) for general (cyclic) joins 𝒬1,…,𝒬m 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 𝒯+O⁢(|OUT|) time where OUT=|σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬))|, Zhao et al. [34] derived smooth tradeoffs between 𝒮 and 𝒯. In Section 6, we complement their results by identifying scenarios where 𝒮=O⁢(IN) and 𝒯=O⁢(1) are possible, provided that the time of reporting σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) is allowed to be 𝒯+O⁢(|OUT|) 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 S be a set of n elements, denoted as e1,e2,…,en, where each ei (i∈[n]) carries a positive integer weight w⁢(ei). Set W=∑i=1nw⁢(ei). A weighted sampling operation returns a random element X such that 𝐏𝐫⁢[X=ei]=w⁢(ei)/W for each i∈[n]. The alias method [31] supports such an operation in O⁢(1) time, after creating a data structure of O⁢(n) space over S in O⁢(n) 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 O⁢(IN) space and can extract a uniformly random tuple from 𝐽𝑜𝑖𝑛⁢(𝒬) in O⁢(1) time.

Take a join tree 𝒯 of the schema graph 𝒢=(𝒱,ℰ) of 𝒬. Root 𝒯 at an arbitrary node e∗ (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 e in 𝒯, we denote by 𝒯e the subtree of 𝒯 induced by all the descendants of e (note: e is a descendant of itself). Define

𝒬e={Re′∣node e′ is in 𝒯e} (4)

where Re′ is the (only) relation in 𝒬 whose schema is e′. We will refer to 𝒬e as a subjoin. The schema graph of 𝒬e is 𝒢e=(𝒱e,ℰe) where ℰe={e′∈ℰ∣node e′ is in 𝒯e} and 𝒱e=⋃e′∈ℰee′.

Given a tuple 𝒖∈Re, let us examine 𝒖⋈𝐽𝑜𝑖𝑛⁢(𝒬e), namely, the result tuples of the subjoin 𝒬e to which 𝒖 “contributes”. Define the subjoin weight of 𝒖 as

𝑠𝑢𝑏𝑗⁢-⁢we⁢(𝒖) = |𝒖⋈𝐽𝑜𝑖𝑛(𝒬e)| (5)

where 𝒬e 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 e of 𝒯, the subjoin weights of all the tuples in Re can be computed in O⁢(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 e be a node of 𝒯. We can build in O⁢(IN) expected time a structure of O⁢(IN) space that, given any 𝐮∈Re, can draw a uniformly random tuple from 𝐮⋈𝐽𝑜𝑖𝑛⁢(𝒬e) in O⁢(1) time.

It is now a simple exercise to design the overall structure for sampling from 𝐽𝑜𝑖𝑛⁢(𝒬)=𝐽𝑜𝑖𝑛⁢(𝒬e∗). Notice that 𝐽𝑜𝑖𝑛⁢(𝒬) is decomposed into a collection of sets: {𝒖⋈𝐽𝑜𝑖𝑛⁢(𝒬)∣𝒖∈Re∗}. Random sampling from 𝐽𝑜𝑖𝑛⁢(𝒬) can be performed in two steps. First, obtain a random tuple 𝑿∈Re∗ such that 𝐏𝐫[𝑿=𝒖]=|𝒖⋈𝐽𝑜𝑖𝑛(𝒬e∗)|/|𝐽𝑜𝑖𝑛(𝒬)|=𝑠𝑢𝑏𝑗-we∗(𝒖)/|𝐽𝑜𝑖𝑛(𝒬)| for each 𝒖∈Re∗. As |𝐽𝑜𝑖𝑛(𝒬)|=∑𝒖∈Re∗|𝒖⋈𝐽𝑜𝑖𝑛(𝒬e∗)|, this is an instance of weighted sampling once the 𝑠𝑢𝑏𝑗⁢-⁢we∗⁢(𝒖) value of every 𝒖∈Re∗ has been calculated from Lemma 10. An alias structure on Re∗ allows us to obtain 𝑿 in O⁢(1) time. Second, apply Lemma 11 to draw a uniformly random tuple from 𝑿⋈𝐽𝑜𝑖𝑛⁢(𝒬e∗) in O⁢(1) 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 e∈ℰ satisfying 𝒵⊆e. An edge {e1,e2} of 𝒯 is 𝒵-breakable if e1∩e2⊆𝒵 (note that e1 and e2 are hyperedges of ℰ; and {e1,e2} 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 s subtrees 𝒯1, 𝒯2, …, 𝒯s. For each i∈[s], define a hypergraph 𝒢i=(𝒱i,ℰi) with ℰi={e∈ℰ∣node e exists in ⁢𝒯i} and 𝒱i=⋃e∈ℰie. Each 𝒢i is a (𝒵,𝒯)-component of 𝒢.

Given a schema graph 𝒢=(𝒱,ℰ), we call two distinct vertices X,Y∈𝒱 neighbors if they appear together in at least one hyperedge. A path in 𝒢 is a sequence of distinct vertices X1,X2,…,Xt such that Xi and Xi+1 are neighbors for all i∈[t−1].

Definition 14.

Let 𝒢=(𝒱,ℰ) be an acyclic schema graph and 𝒵 be a subset of 𝒱. A 𝒵-path is a path Z1,X1,X2,…,Xℓ,Z2 (where ℓ≥1) such that (i) Z1 and Z2 belong to 𝒵, but they are not neighbors; (ii) Xi∉𝒵 for every i∈[ℓ].

Example 15.

Consider the schema graph 𝒢=(𝒱,ℰ) in Figure 1(a). Among the choices of 𝒵 in Example 3, 𝒢 is 𝒵-canonical only for 𝒵=∅ and 𝒵=E.

Set 𝒵 to ACEHF. The edge {ABC, CDE} of 𝒯 is 𝒵-breakable because ABC∩CDE=C is a subset of 𝒵. On the other hand, the edge {EGH, GI} is not 𝒵-breakable because EGH∩GI=G is not a subset of 𝒵. By removing the 𝒵-breakable edges, we obtain four subtrees of 𝒯: the first contains nodes e1 and e3, while every other node (i.e., e2, e4, and e5) forms a subtree by itself. Accordingly, 𝒢 has four (𝒵,𝒯)-components: 𝒢1=(EGHI,{EGH,GI}), 𝒢2=(ABC,{ABC}), 𝒢3=(CDE,{CDE}), and 𝒢4=(EFJ,{EFJ}). 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. 1.

    𝒢 is ext-𝒵-connex.

  2. 2.

    𝒢 does not have a 𝒵-path.

  3. 3.

    Let 𝒯 be an arbitrary join tree of 𝒢. Denote by s the number of (𝒵,𝒯)-components of 𝒢, and by 𝒢i=(𝒱i,ℰi) the i-th component. For every i∈[s], 𝒢i is (𝒱i∩𝒵)-canonical.

Example 17.

Continuing Example 15, for 𝒵=ACEHF, as mentioned 𝒢 has (𝒵,𝒯)-components: 𝒢1=(ABC,{ABC}), 𝒢2=(CDE,{CDE}), 𝒢3=(EFJ,{EFJ}), and 𝒢4=(EGHI,{EGH, GI}). For every 1≤i≤4, 𝒢i is (𝒱i∩𝒵)-canonical; hence, 𝒢 is ext-𝒵-connex. However, for 𝒵=ACEHI, 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 m≥2 sets S1,…,Sm. Given distinct integers a,b∈[m], a disjointness query returns whether Sa∩Sb=∅. The SSD-conjecture states that any structure promising to answer such a query in O⁢(polylogN) time must use Ω⁢(N2/polylogN) space, where N=∑i=1m|Si|.

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 O~⁢(N) space that answers a disjointness query in O~⁢(1) time, thus breaking the SSD-conjecture. Since 𝒢 is not ext-𝒵-connex, it must have a 𝒵-path by Lemma 16. Pick an arbitrary 𝒵-path: A,X1,X2,…,Xℓ,B for some ℓ≥1. Remember that no vertex on the path belongs to 𝒵 except A and B.

From S1,…,Sm (the input to set disjointness), next we create a join 𝒬 whose schema graph is 𝒢=(𝒱,ℰ). For each hyperedge e∈ℰ, construct a relation Re with schema e as follows. For each integer i∈[m] and every element x∈Si, insert a tuple 𝒖 into Re (provided that 𝒖 is not already in Re) such that (i) if A∈e, then 𝒖⁢(A)=i; (ii) if B∈e, then 𝒖⁢(B)=i; (iii) for every attribute Z∈(e∩𝒵)∖{A,B}, set 𝒖⁢(Z)=⊥ where ⊥ is a special symbol; (iv) for every attribute X∈e∖𝒵, set 𝒖⁢(X)=x. By Definition 14, no hyperedge of ℰ covers both A and B; hence, at most one step between (i) and (ii) applies. The relation Re thus designed has a size at most N.

Lemma 18.

Given distinct a,b∈[m], define a tuple 𝐳 over 𝒵 with 𝐳⁢(A)=a, 𝐳⁢(B)=b, and 𝐳⁢(Z)=⊥ for every Z∈𝒵∖{A,B}. Then, Sa∩Sb≠∅ if and only if σ𝐳⁢(𝐽𝑜𝑖𝑛⁢(𝒬))≠∅.

Proof.

Let us first prove the “if direction” (⇐). Consider an arbitrary tuple 𝒖∈σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)). Clearly, 𝒖⁢(A)=a and 𝒖⁢(B)=b. Since A,X1,X2,…,Xℓ,B is a 𝒵-path, there exist hyperedges e0,e1,…,eℓ in ℰ such that (i) {A,X1}⊆e0, (ii) {Xi,Xi+1}⊆ei for each i∈[ℓ−1], and (iii) {Xℓ,B}⊆eℓ. We argue:

𝒖⁢(X1)=𝒖⁢(X2)=…=𝒖⁢(Xℓ). (6)

To see why, fix any i∈[ℓ−1]. As 𝒖∈σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)), we know 𝒖⁢[ei]∈Rei. As Xi and Xi+1 are attributes outside Z, our construction of 𝒬 ensures 𝒖⁢(Xi)=𝒖⁢(Xi+1). The fact that this holds for every i∈[ℓ−1] proves (6). By the construction of 𝒬, 𝒖⁢(X1) is an element of Sa and 𝒖⁢(Xℓ) is an element of Sb. Therefore, (6) tells us Sa∩Sb≠∅.

Next, we prove the “only if direction” (⇒). Fix any x∈Sa∩Sb. Construct a tuple 𝒖 over 𝒱 where 𝒖⁢(A)=a,𝒖⁢(B)=b,𝒖⁢(Z)=⊥ for every Z∈𝒵∖{A,B}, and 𝒖⁢(X)=x for every X∈𝒱∖𝒵. It suffices to prove that 𝒖∈σ𝒛⁢𝐽𝑜𝑖𝑛⁢(𝒬). Indeed, this is true because, for every e∈ℰ, our construction explicitly inserts tuple 𝒖⁢[e] into Re, noticing that e cannot contain both A and B. ◀

The above lemma can be used to break the SSD-conjecture. Note that the input size of 𝒬 satisfies IN≤N⋅|ℰ|=O⁢(N), thus allowing us to build a structure of O~⁢(IN)=O~⁢(N) space on 𝒬 to support a (𝒢,𝒵)-sampling operation in O~⁢(1) time. A disjointness query parameterized by integers a,b∈[m] can be answered as follows. First, create a tuple 𝒛 over 𝒵 such that 𝒛⁢(A)=a, 𝒛⁢(B)=b, and 𝒛⁢(Z)=⊥ for every Z∈𝒵∖{A,B}. Then, issue a (𝒢,𝒵)-sampling operation and declare Sa∩Sb=∅ if and only if the operation returns nothing. This correctly answers the query in O~⁢(1) time.

We note that similar constructions were used in [5, 7] to prove dichotomies for settings different from ours. Our contribution lies in establishing connections to (𝒢,𝒵)-sampling.

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 O⁢(IN) space in O⁢(IN) expected time to support a (𝒢,𝒵)-sampling operation on 𝒬 in constant time.

Case 1: 𝓖 Is 𝓩-Canonical.

By Definition 12, in this case there is a hyperedge e∗∈ℰ satisfying 𝒵⊆e∗. Now, root 𝒯 at e∗, apply Lemma 10 to calculate the subjoin weights of all tuples in Re∗ (the relation in 𝒬 with schema e∗), and build a sampling structure of Lemma 11 by setting the symbol e there to e∗. Given a tuple 𝒛 over 𝒵, define

Re∗⁢(𝒛) = {𝒖∈Re∗∣𝒖⁢[𝒵]=𝒛⁢[𝒵]}. (7)

It is easy to verify

σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) = ⋃𝒖∈Re∗⁢(𝒛)𝒖⋈𝐽𝑜𝑖𝑛⁢(𝒬). (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 𝑿∈Re∗⁢(𝒛) such that, for each 𝒖∈Re∗⁢(𝒛), we have

𝐏𝐫[𝑿=𝒖]=|𝒖⋈𝐽𝑜𝑖𝑛(𝒬e∗)|/|σ𝒛(𝐽𝑜𝑖𝑛(𝒬))|=𝑠𝑢𝑏𝑗-we∗(𝒖)/|σ𝒛(𝐽𝑜𝑖𝑛(𝒬))|

where 𝑠𝑢𝑏𝑗⁢-⁢we∗⁢(𝒖) is the subjoin weight of 𝒖 (with respect to the rooted 𝒯); see (5). As |σ𝒛(𝐽𝑜𝑖𝑛(𝒬))|=∑𝒖∈Re∗⁢(𝒛)|𝒖⋈𝐽𝑜𝑖𝑛(𝒬e∗)|=∑𝒖∈Re∗⁢(𝒛)𝑠𝑢𝑏𝑗-we∗(𝒖), this is an instance of weighted sampling since the 𝑠𝑢𝑏𝑗⁢-⁢we∗⁢(𝒖) value of every 𝒖∈Re∗⁢(𝒛) is already available. Hence, an alias structure on Re∗⁢(𝒛) allows us to obtain 𝑿 in constant time. Second, apply Lemma 11 to draw a uniformly random tuple from 𝑿⋈𝐽𝑜𝑖𝑛⁢(𝒬e∗) in constant time.

The alias structure on Re∗⁢(𝒛) occupies O⁢(|Re∗⁢(𝒛)|) space and can be built in O⁢(|Re∗⁢(𝒛)|) time, as explained in Section 3.1. We do this for every 𝒛∈Π𝒵⁢(Re∗), where Π is projection in relational algebra. For distinct 𝒛,𝒛′∈Π𝒵⁢(Re∗), the sets Re∗⁢(𝒛) and Re∗⁢(𝒛′) are disjoint. Hence, in total, all the alias structures occupy O⁢(|Re∗|)=O⁢(IN) space and can be built in O⁢(IN) expected time (the time is expected because hashing is required to obtain the sets Re∗⁢(𝒛) of each 𝒛∈Π𝒵⁢(Re∗)).

▶ Remark.

We make an observation here that will be useful in Section 5.2. For each 𝐳∈Π𝒵⁢(Re∗), as mentioned |σ𝐳⁢(𝐽𝑜𝑖𝑛⁢(𝒬))|=∑𝐮∈Re∗⁢(𝐳)𝑠𝑢𝑏𝑗⁢-⁢we∗⁢(𝐮). Since the subjoin weights of all tuples in Re∗ are available, we can compute the sizes |σ𝐳⁢(𝐽𝑜𝑖𝑛⁢(𝒬))| of all 𝐳∈Π𝒵⁢(Re∗) by scanning Re∗ once in O⁢(IN) time. Storing all those sizes takes O⁢(|Re∗|)=O⁢(IN) extra space. As the benefit, given any tuple 𝐳 over 𝒵, we can obtain |σ𝐳⁢(𝐽𝑜𝑖𝑛⁢(𝒬))| in O⁢(1) time with hashing.

Case 2: 𝓖 Is Not 𝓩-Canonical.

Let 𝒯 be an arbitrary join tree of 𝒢. As 𝒢 is ext-𝒵-connex, by Lemma 16, 𝒯 defines a number s≥1 of (𝒵,𝒯)-components of 𝒢 – denoted as 𝒢1=(𝒱1,ℰ1),…,𝒢s=(𝒱s,ℰs), respectively – such that 𝒢i is (𝒱i∩𝒵)-canonical for every i∈[s]. For each i∈[s], define 𝒵i=𝒱i∩𝒵 and 𝒬i={Re∈𝒬∣e∈ℰi}. Note that 𝒬i is a join instance of the schema graph 𝒢i, which is 𝒵i-canonical. Denote by INi the input size of 𝒬i; we have ∑i=1sINi=IN because every relation of 𝒬 appears in exactly one of 𝒬1,…,𝒬s. For each i∈[s], we build a (𝒢i,𝒵i)-sampling structure on 𝒬i in the way explained for Case 1. All the s structures occupy O⁢(IN) space in total and can be built in O⁢(IN) expected time.

Consider now a (𝒢,𝒵)-sampling operation, which is given a tuple 𝒛 over 𝒵. For each i∈[s], defining 𝒛i=𝒛⁢[𝒱i], we perform a (𝒢i,𝒵i)-sampling operation using 𝒛i; let us assume that this operation returns 𝒖i. If 𝒖i is nil for any i, we return nil for the original (𝒢,𝒵)-sampling operation. Otherwise, return 𝒖1⋈𝒖2⋈…⋈𝒖s, which, as explained in Appendix B, must be a uniformly random tuple of σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)). The (𝒢,𝒵)-sampling operation takes O⁢(s)=O⁢(1) 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 m≥2 sets S1,S2,…,Sm, each supporting three O⁢(1)-time operations: size, membership, and sample. The objective is to draw an element from ⋃i=1mSi uniformly at random.

As before, set n=|⋃i=1mSi| and N=∑i=1m|Si|. For each element y∈⋃i=1mSi, define its inverted set as InvS⁢(y)={i∈[m]∣y∈Si}, i.e., the “ids” of the sets containing y. The degree of y is deg⁡(y)=|InvS⁢(y)|. 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 X∈[m] such that 𝐏𝐫⁢[X=i]=|Si|/N for each i∈[m]. Then, use the sampling operation to draw an element Y from SX. Finally, carry out an acceptance step:

Acceptance Step: Accept Y with probability 1/deg⁡(Y).

If accepted, Y is returned; otherwise, the algorithm repeats from scratch.

The algorithm correctly returns a uniform sample of ⋃i=1mSi. To see why, fix an arbitrary element y∈⋃i=1mSi. This element is returned if and only if three conditions are satisfied: (i) X∈InvS⁢(y), which occurs with probability |SX|/N for each X∈InvS⁢(y), (ii) Y=y, which occurs with probability 1/|SX| conditioned on X, and (iii) y is accepted, which occurs with probability 1/deg⁡(y) conditioned on y. Hence, the algorithm outputs y with probability

∑X∈InvS⁢(y)|SX|N⋅1|SX|⋅1deg⁡(y)=1N (9)

which is identical for all y∈⋃i=1mSi. As a corollary, in each repeat, the algorithm succeeds in returning a sample with probability n/N. Thus, N/n repeats are needed in expectation.

The random value X can be easily obtained in O⁢(m) time. One (logically simple) way to do so is to build an alias structure (see Section 3.2) on |S1|,|S2|,…,|Sm| – namely, the m set sizes – on the fly in O⁢(m) time (using the size operation), after which X can be extracted from the structure in O⁢(1) time. The time to obtain Y is O⁢(1) (using the sampling operation). The troublemaker, however, is the acceptance step. The standard approach [4, 13] is to query the membership of Y in each Si (i∈[m]), which costs O⁢(m) time. This renders the overall sample time O⁢(m⁢N/n) in expectation.

New Idea: Total Law of Expectation.

Our objective is to implement the acceptance step in O⁢(m⁢n/N) expected time – note that this is faster than O⁢(m) by a factor of N/n, which eventually allows us to bring the expected sample time from O⁢(m⁢N/n) down to O⁢(m).

Let us start with a fundamental fact:

Proposition 19.

Let Γ be a random variable taking values from [m], and U be a uniformly random variable over [m]. If U and Γ are independent, then 𝐏𝐫⁢[U≤Γ]=1m⁢𝐄[Γ].

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 x∈[m] and y∈Sx, generate a rand. var. Γ∈[m] with 𝐄[Γ]=m/deg⁡(y).

Later, we will explain how to achieve the above purpose in O⁢(m/deg⁡(y)) 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 X and Y): (i) generate a uniformly random variable U over [m] in constant time; (ii) generate the aforementioned random variable Γ (setting x and y to the values of X and Y, respectively) in O⁢(m/deg⁡(Y)) expected time; (iii) accept if U≤Γ.

Correctness follows from Proposition 19 (the acceptance probability is 1m⁢𝐄[Γ]=1/deg⁡(Y), as desired). For a particular x∈[m] and a particular y∈Sx, we have 𝐏𝐫⁢[X=x,Y=y]=|Sx|N⁢1|Sx|=1/N. Thus, the expected cost of the acceptance step will be at the order of

∑x∈[m]∑y∈Sx𝐏𝐫⁢[X=x,Y=y]⁢mdeg⁡(y) = ∑x∈[m]∑y∈Sx1N⁢mdeg⁡(y) (10)
= mN⁢∑y∈∪i=1mSi∑x∈InvS⁢(y)1deg⁡(y)=m⁢nN

where the last step used deg⁡(y)=|InvS⁢(y)| and n=|⋃i=1mSi|.

Generation of 𝚪.

Next, we will concentrate on the Γ-generating task defined earlier. Recall that the generation is based on a given set “id” x∈[m] and an element y∈Sx. Consider the procedure below:

In plain words, if deg⁡(y)≥2, the value F returned is a random variable giving the number of failed WoR-sampling operations before finding another set Si≠Sx containing y; otherwise, F=m−1. We prove in Appendix C:

Proposition 20.

𝐄[F]=mdeg⁡(y)−1.

Combining the above with the obvious fact that F∈[0,m−1], we can now define Γ=F+1 as the desired random variable satisfying Γ∈[m] and 𝐄[Γ]=m/deg⁡(y). The WoR-sampling operation at Line 3 can be implemented in O⁢(1) time; see, e.g., [13]. Thus, the cost of find-2nd is proportional to the value F returned. It follows from Proposition 20 that the expected cost of find-2nd is O⁢(𝐄[F])=O⁢(m/deg⁡(y)).

Total Cost of Set-Union Sampling.

Recall that, in the (original) baseline algorithm, each repeat takes O⁢(m) time; as the number of repeats is N/n expected, the total cost of taking one sample from ⋃i=1mSi is O⁢(m⁢N/n). By applying our remedy, one repeat of the baseline algorithm is now carried out in O⁢(m⁢n/N) expected time (see the analysis in (10)). The remedy does not affect the expected number of repeats (i.e., N/n), which suggests that the overall expected sample time should be O⁢(m⁢nN⋅Nn)=O⁢(m). Indeed, if 𝐄[T𝑡𝑜𝑡𝑎𝑙] represents the expected time for our algorithm to acquire a sample from ⋃i=1mSi, we can write 𝐄[T𝑡𝑜𝑡𝑎𝑙]=O⁢(m⁢n/N)+(1−n/N)⁢𝐄[T𝑡𝑜𝑡𝑎𝑙], which solves to 𝐄[T𝑡𝑜𝑡𝑎𝑙]=O⁢(m). 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 |⋃i=1mSi| rather than to sample from ⋃i=1mSi. 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 19 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 (𝒢,⋁i=1m𝒵i)-sampling problem with a reduction to set union sampling. Recall that 𝒢 is ext-𝒵i-connex for every i∈[m]. Hence, we can create a structure Υi of Theorem 5 on every (𝒢,𝒵i) that supports (𝒢,𝒵i)-sampling in O⁢(1) time. As each structure occupies O⁢(IN) space, it may appear as if the total space would be O⁢(m⋅IN). This is not true. To see why, note that every 𝒵i 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 𝒵1,𝒵2,…,𝒵m. Physically, one structure of Theorem 5 suffices for each distinct subset; hence, the total space is O⁢(IN). For similar reasons, all the structures can be constructed in O⁢(IN) expected time.

A (𝒢,⋁i=1m𝒵i)-sampling operation is given m tuples 𝒛1,…,𝒛m over 𝒵1,…,𝒵m, respectively. For each i∈[m], define Si=σ𝒛i⁢(𝐽𝑜𝑖𝑛⁢(𝒬)). The operation, essentially, aims to return a uniformly-random tuple from ⋃i=1mSi. To cast this as an instance of set union sampling, we need to implement each of the size, membership, and sampling operations in O⁢(1) time on each Si. This is trivial for two operations:

  • ■

    sampling: use Υi to perform a (𝒢,𝒵i)-sampling operation;

  • ■

    membership: given a tuple 𝒖 over 𝒱, this operation checks whether 𝒖∈σ𝒛i⁢(𝐽𝑜𝑖𝑛⁢(𝒬)). This requires checking if 𝒖⁢[e] is in the relevant relation of 𝒬 for each e∈ℰ and if 𝒖⁢(Z)=𝒛i⁢(Z) for each Z∈𝒵i. All the checking can be done in O⁢(1) time.

We can support also the size operation in O⁢(1) time by using directly Υi. 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 ⋃i=1mSi in O⁢(m) 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 a,b∈[m] and reports whether Sa∩Sb=∅). 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 S1,S2,…,Sm be m≥2 sets whose elements are integers. Define N=∑i=1m|Si|. We want to preprocess these sets into a data structure of 𝒮 space such that, given any distinct integers a,b∈[m], we can report in 𝒯=o⁢(N) expected time whether Sa∩Sb=∅. The expected SSD conjecture states that 𝒮 must be Ω⁢(N2/(𝒯2⁢polylogN)).

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 𝒮+O⁢(𝒯⁢log⁡N) space and O⁢(𝒯⁢log⁡N) deterministic query time. This establishes the claim in Lemma 21 because 𝒯≤N≤𝒮, implying 𝒮+O⁢(𝒯⁢log⁡N)=O⁢(𝒮⁢log⁡N).

Fix any distinct integers a,b∈[m]. Let 𝒜 be the algorithm associated with 𝒯 for deciding whether Sa∩Sb=∅. Denote by X the cost for 𝒜. Note that X is a random variable with 𝐄[X]≤𝒯. By Markov’s inequality, 𝐏𝐫⁢[X≥2⁢𝒯]≤1/2. Imagine running 𝒜 for 3⁢log⁡N times. The probability for all those runs to take at least 2⁢𝒯 time to terminate is at most 1/N3. In other words, with probability at least 1−1/N3, at least one run finishes within 2⁢𝒯 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 2⁢𝒯 time, it consumes at most 2⁢𝒯 words of random bits. Now, take t=3⁢log⁡N random bit sequences σ1,…,σt, each of which is 2⁢𝒯 words long. For each i∈[t], define 𝒜⁢(σi) as the deterministic algorithm that runs 𝒜 based on σi, with the modification that 𝒜⁢(σi) terminates itself after having run 𝒜 for a duration of 2⁢𝒯 time. As per our earlier discussion, with probability at least 1−1/N3, at least one of 𝒜⁢(σ1),…,𝒜⁢(σt) manages to report whether Sa∩Sb=∅.

Now, let us consider all m⁢(m−1)/2 distinct pairs of a,b∈[m]. With probability at least 1−m2/N3≥1−1/N, for every pair of a and b, at least one of 𝒜⁢(σ1),…,𝒜⁢(σt) manages to report whether Sa∩Sb=∅ – let us call {σ1,…,σt} a working set. As the probability 1−1/N is greater than 0 (because N≥m≥2), a working set must exist.

Our final set-disjointness structure consists of Υ and a working set of random-bit sequences {σ1,…,σt}, the total space of which is 𝒮+O⁢(𝒯⁢log⁡N). Given two distinct integers a,b∈[m], we run all of 𝒜⁢(σ1),…,𝒜⁢(σt), at least one of which manages to report whether Sa∩Sb=∅. The total query time is O⁢(𝒯⋅t)=O⁢(𝒯⁢log⁡N). ◀

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 O⁢(IN) space that can support a (𝒢,𝒵)-sampling operation in O⁢(IN0.49) expected time, then there is a set-disjointness structure of O⁢(N) space and O⁢(N0.49) expected query time – this will break the expected SSD conjecture.

Let us return to the context of Theorem 8 where m≤IN0.49. W.l.o.g., assume that 𝒢 is not ext-𝒵1-connex, and yet there is a structure Υ of O⁢(IN) space that can support a (𝒢,⋁i=1m𝒵i)-sampling operation in O⁢(m) expected time. We will show how to use Υ to support a (𝒢,𝒵1)-sampling operation in O⁢(IN0.49) 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 𝒛1 over 𝒵1. For each i∈[2,m], construct a dummy tuple 𝒛i over 𝒵i such that, for each Z∈𝒵i, 𝒛i⁢(Z) is a value that does not appear in the relations of 𝒬. Use Υ to perform a (𝒢,⋁i=1m𝒵i)-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 S be a set of elements whose size |S| is known. Two subroutines are at our disposal:

  • ■

    The first lists the elements of S (in an order we cannot control) within |S|⋅λ𝑟𝑒𝑝 time;

  • ■

    The second samples a uniformly random element of S with replacement within λ𝑠𝑎𝑚 time.

We will show that the elements of S can be randomly enumerated with an expected delay of O⁢(λ𝑟𝑒𝑝+λ𝑠𝑎𝑚).

Let us first describe an algorithm that produces a random permutation of S 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 S𝑠𝑒𝑒𝑛 of elements that have been found. Initially, S𝑠𝑒𝑒𝑛=∅. Then, we carry out iterations, each of which adds a new element to S𝑠𝑒𝑒𝑛. Specifically, an iteration starts by randomly sampling an element e from S. If e∉S𝑠𝑒𝑒𝑛, we add it to S𝑠𝑒𝑒𝑛 and output it. Otherwise, the iteration re-samples from S until getting an unseen element. As long as |S𝑠𝑒𝑒𝑛|≤|S|/2, an unseen element is sampled with probability at least 1/2. Hence, two samples suffice in expectation, and the cost of an iteration is O⁢(λ𝑠𝑎𝑚) expected. The first phase finishes after |S|/2 iterations. The second phase finds the entire S, and extracts (with hashing) and randomly permutes (with Fisher-Yates shuffle [22]) S∖S𝑠𝑒𝑒𝑛. The phase performs at most c⋅|S|⋅λ𝑟𝑒𝑝 atomic instructions of the RAM model for some constant c. Finally, the third phase outputs S∖S𝑠𝑒𝑒𝑛 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 S𝑠𝑒𝑒𝑛, we do not output it immediately, but instead append it to a buffer queue S𝑏𝑢𝑓. We remove and output the head element of S𝑏𝑢𝑓 every

α=⌈1+λ𝑟𝑒𝑝λ𝑠𝑎𝑚⌉

iterations so that S𝑏𝑢𝑓 still has |S|2⁢(1−1/α) elements left at the end of the first phase. In the second phase, we remove and enumerate the head of S𝑏𝑢𝑓 after every c⋅|S|⋅λ𝑟𝑒𝑝/(|S|2⁢(1−1α))=2⁢c⋅λ𝑟𝑒𝑝⋅αα−1 atomic instructions. The third phase simply enumerates the remaining elements in S𝑏𝑢𝑓 (by their queued order) and S∖S𝑠𝑒𝑒𝑛 (by their permuted order) with a constant delay. Overall, the expected delay, which is determined by the first two phases, is bounded by α⋅O⁢(λ𝑠𝑎𝑚)+2⁢c⋅λ𝑟𝑒𝑝⋅αα−1. It is rudimentary to show that the bound is O⁢(λ𝑠𝑎𝑚+λ𝑟𝑒𝑝).

Proof of Theorem 9.

Consider an input 𝒬 to the (𝒢,𝒵)-sampling problem with a feasible pair (𝒢,𝒵). Given a tuple 𝒛 over 𝒵, define S=σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)). Our structure in Section 4.2 can be used to obtain the size |S| in O⁢(1) time. Theorem 5 ensures λ𝑠𝑎𝑚=O⁢(1). As explained in Appendix D, it is possible to build a structure that uses O⁢(IN) space, can be built in O⁢(IN) expected time, and can report S in O⁢(1+|S|) time, implying λ𝑟𝑒𝑝=O⁢(1). Using the reduction described earlier, we can randomly permute σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) with an O⁢(1) 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 O~(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 10

We first review a property of acyclic joins that can be used to compute, for each tuple 𝒖∈Re, the result of 𝒖⋈𝐽𝑜𝑖𝑛⁢(𝒬e) in a manner reminiscent of Cartesian products. W.l.o.g., suppose that e has t≥1 child nodes in 𝒯 denoted as e1,e2,…,et. Define 𝒳i=e∩ei for each i∈[t]. Consider the set S× 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 S×, and (ii) 𝐮⋈𝐽𝑜𝑖𝑛⁢(𝒬e)=S×.

We are now ready to prove Lemma 10 by induction. If e (the node given in the lemma) is a leaf of the (rooted) 𝒯, then 𝒬e includes only Re. In this case, 𝐽𝑜𝑖𝑛⁢(𝒬e)=Re and 𝑠𝑢𝑏𝑗-we(𝒖)=|𝒖⋈Re|=1. Hence, the lemma holds on Re.

Now, consider e to be an internal node of 𝒯. W.l.o.g., suppose that e has t≥1 child nodes in 𝒯 denoted as e1,…,et. Assuming inductively that the lemma is correct on Rei of every i∈[t], next we will prove the correctness on Re as well. For each i∈[t], define 𝒳i=e∩ei.

Let 𝒖 be an arbitrary tuple in Re. According to Lemma 22, we have

𝑠𝑢𝑏𝑗⁢-⁢we⁢(𝒖) = ∏i=1t|Si⁢(𝒖)|

where Si⁢(𝒖)=𝒖⁢[𝒳i]⋈𝐽𝑜𝑖𝑛⁢(𝒬ei), as defined at Line 2 of children-prod. For each i∈[t], we will show how to construct in O⁢(IN) expected time a structure of O⁢(IN) space from which we can obtain |Si⁢(𝒖)| for every 𝒖∈Re in O⁢(IN) time. This will prove Lemma 10 because t=O⁢(1).

For each 𝒖∈Re, define

Rei⁢(𝒖) = {𝒗∈Rei∣𝒗⁢[𝒳i]=𝒖⁢[𝒳i]}. (11)

We can now write

|Si(𝒖)|=|𝒖[𝒳i]⋈𝐽𝑜𝑖𝑛(𝒬ei)| = |⋃𝒗∈Rei⁢(𝒖)𝒗⋈𝐽𝑜𝑖𝑛(𝒬ei)| (12)
= ∑𝒗∈Rei⁢(𝒖)|𝒗⋈𝐽𝑜𝑖𝑛(𝒬ei)|=∑𝒗∈Rei⁢(𝒖)𝑠𝑢𝑏𝑗-wei(𝒗).

We can obtain |Si⁢(𝒖)| with one scan over Rei⁢(𝒖) because the subjoin weights of the tuples in Rei have been computed (by induction). To identify Rei⁢(𝒖) for 𝒖, we create a perfect-hashing structure on Rei in O⁢(|Rei|) expected time, after which Rei⁢(𝒖) can be retrieved in O⁢(1+|Rei⁢(𝒖)|) time.

We divide Re into equivalent classes based on the tuples’ projections onto 𝒳i. If two tuples 𝒖1 and 𝒖2 are in the same equivalent class, the numbers |Si⁢(𝒖1)| and |Si⁢(𝒖2)| are identical. Hence, it suffices to compute |Si⁢(𝒖)| for one (arbitrary) representative 𝒖 of each equivalent class. On the other hand, if 𝒖1 and 𝒖2 are from different equivalent classes, the sets Rei⁢(𝒖1) and Rei⁢(𝒖2) are disjoint. Hence, we can compute the |Si⁢(𝒖)| values for all representatives in time

∑representative 𝒖 of ReO⁢(1+|Rei⁢(𝒖)|)=O⁢(|Re|+|Rei|)=O⁢(IN).

A.2 Proof of Lemma 11

We prove the lemma by induction. The base case where e is a leaf of 𝒯 is trivial and omitted.

Now, consider e to be an internal node of 𝒯. W.l.o.g., suppose that e has t≥1 child nodes in 𝒯 denoted as e1,…,et. Assuming inductively that the lemma is correct on Rei of every i∈[t], next we will prove the correctness on Re as well. For each i∈[t], define 𝒳i=e∩ei.

Let 𝒖 be an arbitrary tuple in Re. According to Lemma 22, there is a one-one correspondence between 𝒖⋈𝐽𝑜𝑖𝑛⁢(𝒬e) and S1⁢(𝒖)×S2⁢(𝒖)×…×St⁢(𝒖) where Si⁢(𝒖)=𝒖⁢[𝒳i]⋈𝐽𝑜𝑖𝑛⁢(𝒬ei), as defined at Line 2 of procedure children-prod. Hence, to take a uniform random tuple from 𝒖⋈𝐽𝑜𝑖𝑛⁢(𝒬e), we can take a uniformly random tuple from Si⁢(𝒖) for each i∈[t]. Next, we will show how to construct in O⁢(IN) expected time a structure of O⁢(IN) space from which we can sample from Si⁢(𝒖) in O⁢(1) time. This will prove Lemma 11 because t=O⁢(1).

For each 𝒖∈Re, define Rei⁢(𝒖) as in (11). We can now write

Si⁢(𝒖)=𝒖⁢[𝒳i]⋈𝐽𝑜𝑖𝑛⁢(𝒬ei) = ⋃𝒗∈Rei⁢(𝒖)𝒗⋈𝐽𝑜𝑖𝑛⁢(𝒬ei).

Random sampling from Si⁢(𝒖) can be performed in two steps. First, obtain a random tuple 𝑿∈Re⁢(𝒖) such that 𝐏𝐫[𝑿=𝒗]=|𝒗⋈𝐽𝑜𝑖𝑛(𝒬ei)|/|Si(𝒖)|=𝑠𝑢𝑏𝑗-wei(𝒗)/|Si(𝒖)| for each 𝒗∈Rei⁢(𝒖). Second, draw a uniformly random tuple from 𝑿⋈𝐽𝑜𝑖𝑛⁢(𝒬ei).

Since the second step can be supported in O⁢(1) time by induction, we focus on the first step. As |Si⁢(𝒖)|=∑𝒗∈Rei⁢(𝒖)𝑠𝑢𝑏𝑗⁢-⁢wei⁢(𝒗), the first step is an instance of weighted sampling once the 𝑠𝑢𝑏𝑗⁢-⁢wei⁢(𝒗) of every 𝒗∈Rei has been computed using Lemma 10 in O⁢(IN) expected time. An alias structure on Rei allows us to obtain 𝑿 in constant time. The structure can be built in O⁢(1+|Rei⁢(𝒖)|) time. It suffices to build such a structure for every “equivalent class” of Re (as defined in Section A.1). All those structures take O⁢(IN) time to construct in total and occupy O⁢(IN) 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 S×, and (ii) σ𝐳⁢(𝐽𝑜𝑖𝑛⁢(𝒬))=S×.

Proof.

Proof of Statement (i).

For each i∈[s], take an arbitrary tuple 𝒗i∈Si. We will first show 𝒗1⋈𝒗2⋈…⋈𝒗s≠∅. For this purpose, it suffices to prove that, for any distinct i,j∈[s] with 𝒱i∩𝒱j≠∅, it must hold that 𝒗i⁢[Y]=𝒗j⁢[Y] for any attribute Y∈𝒱i∩𝒱j.

As Y∈𝒱i (resp., Y∈𝒱j), there is a hyperedge ei∈ℰi (resp., ej∈ℰj) containing Y. As ei and ej 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 {e∘,e∙}. The connectedness property of 𝒯 ensures that Y∈e∘∩e∙. By the definition of 𝒵-breakable edge, Y must be an attribute in 𝒵, indicating that the tuple 𝒛 has a Y-value. As 𝒗i∈Si and 𝒗j∈Sj, we must have 𝒗i⁢(Y)=𝒗j⁢(Y)=𝒛⁢(Y).

To prove the statement, it remains to show that Line 4 never adds to S× the same tuple twice. Consider any two executions of Line 4: the first with 𝒗i=𝒗i1 for i∈[s], and the second with 𝒗i=𝒗i2 for i∈[s]. There exists at least one j∈[s] such that 𝒗j1≠𝒗j2. This further indicates the existence of an attribute Y∈𝒱j such that 𝒗j1⁢(Y)≠𝒗j2⁢(Y). We can now assert that 𝒗11⋈…⋈𝒗s1 and 𝒗12⋈…⋈𝒗s2 must differ in their Y-values.

Proof of Statement (ii).

We will first prove

σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬))⊆S×. (13)

Take any tuple 𝒗∈σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)). Define 𝒗i=𝒗⁢[𝒱i] for each i∈[s]. To show (13), it suffices to prove 𝒗i∈σ𝒛i⁢(𝐽𝑜𝑖𝑛⁢(𝒬i)) for every i∈[s]. We achieve the purpose by arguing that 𝒗𝒊⁢[e]∈Re for any e∈ℰi. As every relation of 𝒬i belongs to 𝒬, we know Re∈𝒬. We can thus infer 𝒗⁢[e]∈Re from 𝒗∈σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)). On the other hand, 𝒗⁢[e]=𝒗i⁢[e] because e⊆𝒱i. It thus follows that 𝒗i⁢[e]∈Re.

Next, we will prove

S×⊆σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)). (14)

For each i∈[s], take an arbitrary tuple 𝒗i∈Si. Define 𝒘=𝒗1⋈…⋈𝒗s. 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., 𝒘⁢[e]∈Re for every e∈ℰ. Indeed, as e must belong to ℰi for some i∈[s], we know 𝒘⁢[e]=𝒗i⁢[e], which is in Re because 𝒗i∈σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬i)). To prove 𝒘∈σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)), we still have to show 𝒘⁢(Z)=𝒛⁢(Z) for every Z∈𝒵. For this purpose, simply identify any i∈[s] satisfying Z∈𝒱i (this i exists because ⋃i=1s𝒱i=𝒱). That 𝒘⁢(Z)=𝒛⁢(Z) follows from the fact 𝒗i∈σ𝒛i⁢(𝐽𝑜𝑖𝑛⁢(𝒬i)).

Statement (ii) follows from (13) and (14). ◀

Appendix C Supplementary Content for Section 5

Proof of Proposition 19.

For any random variables X and Y, the total law of expectation states 𝐄[X]=𝐄[𝐄[X∣Y]]. To prove the proposition, define X to be 1 if U≤Γ or 0 otherwise; furthermore, set Y=Γ. Clearly, 𝐄[X∣Y]=𝐏𝐫⁢[U≤Γ∣Γ]=Γ/m by the independence of U and Γ. We can now derive 𝐏𝐫⁢[U≤Γ]=𝐄[X]=𝐄[𝐄[X∣Y]]=𝐄[Γ/m]=1m⁢𝐄[Γ]. ◀

Proof of Proposition 20.

If deg⁡(y)=1, then F is deterministically m−1, in which case the lemma clearly holds.

The rest of the proof considers deg⁡(y)≥2. In that scenario, F can be rephrased in the following conventional WoR-sampling setup. Suppose that we have m−1 balls, among which deg⁡(y)−1 ones are red and the rest are white. Uniformly sample a ball WoR until seeing a red ball, and F gives the number of white balls sampled in this process. It is well known (e.g., see [25]) that F follows the Negative Hypergeometric distribution with 𝐄[F]=mdeg⁡(y)−1. ◀

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 16, any join tree 𝒯 of 𝒢 defines a number s of (𝒵,𝒯)-components of 𝒢 – denoted as 𝒢1=(𝒱1,ℰ1),…,𝒢s=(𝒱s,ℰs), respectively – such that 𝒢i is (𝒱i∩𝒵)-canonical for every i∈[s]. For each i∈[s], define 𝒵i=𝒱i∩𝒵 and 𝒬i={Re∈𝒬∣e∈ℰi}. Note that 𝒬i is a join instance of 𝒢i, which is 𝒵i-canonical. For each i∈[s], the structure Υ includes a structure of Theorem 5 on every 𝒬i (i∈[s]); denote that structure as Υi. Now, consider a tuple 𝒛 over 𝒵 whose |σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬))| is to be computed. For each i∈[s], we define 𝒛i=𝒛⁢[𝒱i] and use Υi to obtain the size |σ𝒛i⁢(𝐽𝑜𝑖𝑛⁢(𝒬i))| in O⁢(1) time (this is doable because 𝒢i is 𝒵i-canonical). Finally, we return |σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬))|=∏i=1s|σ𝒛i⁢(𝐽𝑜𝑖𝑛⁢(𝒬i))|. The correctness is guaranteed by Lemma 23.

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 O⁢(IN+|𝐽𝑜𝑖𝑛⁢(𝒬)|) time. The algorithm allows one to specify a source relation R∈𝒬. In an O⁢(IN)-time preprocessing stage, it

  • ■

    converts R into a fully-reduced state, i.e., eliminating every tuple 𝒖∈R with 𝒖⋈𝐽𝑜𝑖𝑛(𝒬))=∅ (i.e., 𝒖 does not “contribute” to the join result);

  • ■

    creates a structure that, given any tuple 𝒖∈R, can report 𝒖⋈𝐽𝑜𝑖𝑛⁢(𝒬) in O(|𝒖⋈𝐽𝑜𝑖𝑛(𝒬)|) 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 O⁢(IN) space, can be built in O⁢(IN) expected time, and when given a tuple 𝒛 over 𝒵, can report σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬)) in O⁢(1+|σ𝒛⁢(𝐽𝑜𝑖𝑛⁢(𝒬))|) time.

Case 1: 𝓖 is 𝒁-Canonical.

By Definition 12, there is a hyperedge e∗∈ℰ satisfying 𝒵⊆e∗. Run the preprocessing stage of Yannakakis’s algorithm to create the aforementioned structure Υ by specifying Re∗ as the source relation (recall that this is the relation in 𝒬 whose schema is e∗). The preprocessing leaves Re∗ in a fully-reduced state. Given a tuple 𝒛 over 𝒵, we identify with hashing in constant time the set Re∗⁢(𝒛) as defined in (7). Then, for each 𝒖∈Re∗⁢(𝒛), use Υ to report 𝒖⋈𝐽𝑜𝑖𝑛⁢(𝒬) (which must be non-empty).

Case 2: 𝓖 Is Not 𝓩-Canonical.

Let 𝒯 be an arbitrary join tree 𝒯 of 𝒢, which defines a number s of (𝒵,𝒯)-components of 𝒢 – denoted as 𝒢1=(𝒱1,ℰ1),…,𝒢s=(𝒱s,ℰs), respectively – such that 𝒢i is (𝒱i∩𝒵)-canonical for every i∈[s] (see Lemma 16). For each i∈[s], define 𝒵i=𝒱i∩𝒵 and 𝒬i={Re∈𝒬∣e∈ℰi}. Note that 𝒬i is a join instance of the schema graph 𝒢i, which is 𝒵i-canonical. For each i∈[s], build a structure Υi on 𝒬i in the way explained for Case 1. Consider now a tuple 𝒛 over 𝒵. For each i∈[s], defining 𝒛i=𝒛⁢[𝒱i], we use Υi to extract Si=σ𝒛i⁢(𝐽𝑜𝑖𝑛⁢(𝒬i)). Then, for every (𝒖1,…,𝒖s)∈S1×…×Ss, output 𝒖1⋈𝒖2⋈…⋈𝒖s.