ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
subset.hpp
Go to the documentation of this file.
1/**
2 * @file subset.hpp
3 * @brief Enumerate tuple subsets satisfying an aggregate HAVING predicate.
4 *
5 * For aggregate provenance evaluation, ProvSQL needs to determine which
6 * subsets of base tuples produce a group-aggregate value that satisfies
7 * the HAVING condition. This header declares the @c enumerate_valid_worlds()
8 * function that enumerates all such subsets ("valid worlds") as bitmasks.
9 *
10 * A *world* is represented as a @c mask_t = @c std::vector<bool> of length
11 * @f$n@f$, where bit @f$i@f$ is @c true iff tuple @f$i@f$ is present in
12 * that world. The function returns all worlds where the aggregate of the
13 * present tuples' values satisfies the given comparison.
14 */
15#ifndef SUBSET_HPP
16#define SUBSET_HPP
17
18#include <cstdint>
19#include <string>
20#include <vector>
21
22#include "Aggregation.h"
23
24/**
25 * @brief A bitmask over @f$n@f$ tuples representing one possible world.
26 *
27 * @c mask_t[i] is @c true iff tuple @f$i@f$ is present in this world.
28 */
29using mask_t=std::vector<bool>;
30
31/**
32 * @brief Enumerate all subsets of @p n tuples satisfying an aggregate predicate.
33 *
34 * For each subset @f$W \subseteq \{0, \ldots, n-1\}@f$ of the tuples,
35 * computes the aggregate of their @p values, tests the predicate
36 * @f$\text{agg}(W) \;\mathit{op}\; \text{constant}@f$, and includes @f$W@f$
37 * in the result if the predicate holds.
38 *
39 * @param values Aggregate contribution of each tuple (one value per tuple).
40 * @param constant The constant on the right-hand side of the comparison.
41 * @param op Comparison operator (=, ≠, <, ≤, >, ≥).
42 * @param agg_kind Aggregation function (SUM, COUNT, MIN, MAX…).
43 * @param enumerate If @c false, only determine whether the set of valid worlds
44 * is an upset; the returned vector may be empty.
45 * @param upset Output: set to @c true if the set of valid worlds forms an
46 * upset (upward-closed set), @c false otherwise.
47 * @param is_scalar Scalar (no GROUP BY) aggregation: the output row exists
48 * even in the empty world, so the empty subset is also
49 * tested against the predicate.
50 * @return Vector of bitmasks, one per valid world.
51 */
52std::vector<mask_t> enumerate_valid_worlds(
53 const std::vector<long> &values,
54 long constant,
56 AggregationOperator agg_kind,
57 bool enumerate,
58 bool &upset,
59 bool is_scalar = false
60 );
61
62/**
63 * @brief Canonical per-world decision: does @c agg(present) @c op @c constant
64 * hold in the world where exactly the @p present tuples are present?
65 *
66 * The single source of truth for "value of @c agg θ k in one world", shared by
67 * the all-worlds expansion (the exhaustive enumerator builds its world set from
68 * this predicate) and by any sampler that draws one world (so sample and expand
69 * agree by construction).
70 *
71 * Enforces the **empty-group exclusion**: a world with no present tuple makes
72 * the predicate @c false (SQL evaluates HAVING only on groups that exist, so an
73 * all-absent group never satisfies it -- even for e.g. @c sum(x) >= -5 or
74 * @c min(x) <= k). Otherwise it aggregates the present tuples' @p values with
75 * @p agg_kind and applies @p op against @p constant on the integer grid (the
76 * caller having scaled numeric / decimal-float domains; see @c matchAggCmp).
77 *
78 * @param values Per-tuple aggregate contribution (scaled to a common grid).
79 * @param present Which tuples are present in this world (same length).
80 * @param constant Right-hand side of the comparison, on the same grid.
81 * @param op Comparison operator.
82 * @param agg_kind Aggregation function.
83 * @return @c true iff a non-empty group's aggregate satisfies @p op.
84 */
86 const std::vector<long> &values,
87 const mask_t &present,
88 long constant,
90 AggregationOperator agg_kind
91 );
92
93/**
94 * @brief Enumerate the non-empty worlds whose ordered present elements equal
95 * (@p want_equal true) or differ from (@p want_equal false) @p target.
96 *
97 * The general @c array_agg() HAVING pipeline: no aggregate-specific shortcut,
98 * just an exhaustive scan that compares, in each world, the array of present
99 * tuples' element texts (kept in the aggregate's input order) against the
100 * constant array. Element values are compared as their text representations,
101 * so any element type is supported for @c = / @c <>.
102 *
103 * @param vals Per-tuple element text (in the aggregate's input order).
104 * @param target The constant array's element texts.
105 * @param want_equal @c true for @c =, @c false for @c <>.
106 * @return Vector of bitmasks, one per valid world.
107 */
108std::vector<mask_t> enumerate_array_agg_worlds(
109 const std::vector<std::string> &vals,
110 const std::vector<std::string> &target,
111 bool want_equal
112 );
113
114#endif /* SUBSET_HPP */
Typed aggregation value, operator, and aggregator abstractions.
AggregationOperator
SQL aggregation functions tracked by ProvSQL.
Definition Aggregation.h:51
ComparisonOperator
SQL comparison operators used in gate_cmp circuit gates.
Definition Aggregation.h:39
std::vector< bool > mask_t
A bitmask over tuples representing one possible world.
Definition subset.hpp:29
bool agg_cmp_holds_in_world(const std::vector< long > &values, const mask_t &present, long constant, ComparisonOperator op, AggregationOperator agg_kind)
Canonical per-world decision: does agg(present) op constant hold in the world where exactly the prese...
Definition subset.cpp:360
std::vector< mask_t > enumerate_valid_worlds(const std::vector< long > &values, long constant, ComparisonOperator op, AggregationOperator agg_kind, bool enumerate, bool &upset, bool is_scalar=false)
Enumerate all subsets of n tuples satisfying an aggregate predicate.
Definition subset.cpp:425
std::vector< mask_t > enumerate_array_agg_worlds(const std::vector< std::string > &vals, const std::vector< std::string > &target, bool want_equal)
Enumerate the non-empty worlds whose ordered present elements equal (want_equal true) or differ from ...
Definition subset.cpp:465