43#include "catalog/pg_enum.h"
44#include "catalog/pg_type.h"
45#include "utils/array.h"
46#include "utils/catcache.h"
47#include "utils/syscache.h"
48#include "utils/lsyscache.h"
49#include "access/htup_details.h"
56#include <unordered_map>
68 std::unordered_map<Oid, unsigned>
rank;
71 CatCList *list = SearchSysCacheList1(ENUMTYPOIDNAME, ObjectIdGetDatum(enum_oid));
72 std::vector<std::pair<float4, Oid> > labels;
73 for(
int i = 0; i < list->n_members; ++i) {
74 HeapTuple tup = &list->members[i]->tuple;
75 Form_pg_enum en = (Form_pg_enum) GETSTRUCT(tup);
76#if PG_VERSION_NUM >= 120000
77 Oid label_oid = en->oid;
79 Oid label_oid = HeapTupleGetOid(tup);
81 labels.emplace_back(en->enumsortorder, label_oid);
83 ReleaseCatCacheList(list);
84 std::sort(labels.begin(), labels.end());
85 for(
const auto &l : labels) {
87 oids.push_back(l.second);
112 auto it =
labels.rank.find(label);
113 if(it ==
labels.rank.end())
114 throw std::runtime_error(
"Subset: value is not a label of the enum type");
115 return uint64_t(1) << it->second;
122 const std::size_t n =
labels.oids.size();
124 throw std::runtime_error(
"Subset: enum type has no labels");
126 throw std::runtime_error(
"Subset: an enum type of at most 63 labels is "
127 "supported, one bit per label");
128 universe = n == 64 ? ~uint64_t(0) : (uint64_t(1) << n) - 1;
133 Oid array_type = get_array_type(
enum_oid);
134 if(!OidIsValid(array_type))
135 throw std::runtime_error(
"Subset: enum type has no array type");
152 for(
auto x : v) r |= x;
157 for(
auto x : v) r &= x;
194 Datum arr = OidInputFunctionCall(
array_in_func,
const_cast<char *
>(str),
196 ArrayType *a = DatumGetArrayTypeP(arr);
203 get_typlenbyvalalign(
enum_oid, &typlen, &typbyval, &typalign);
204 deconstruct_array(a,
enum_oid, typlen, typbyval, typalign,
205 &elems, &nulls, &nelems);
206 for(
int i = 0; i < nelems; ++i)
208 r |=
bit_of(DatumGetObjectId(elems[i]));
210 Oid label = DatumGetObjectId(
Abstract semiring interface for provenance evaluation.
Abstract base class for (m-)semirings.
virtual bool absorptive() const override
Return true if this semiring is absorptive ( for all ).
virtual bool mul_idempotent() const override
Return true if is idempotent: .
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
virtual value_type times(const std::vector< value_type > &v) const override
Apply the multiplicative operation to a list of values.
virtual bool exclusive() const override
Return true if the semiring is exclusive: .
Mode
How a mapping value is read (see the file comment).
Subset(Oid enum_type_oid, Mode mode_)
value_type parse_leaf(const char *str) const
Parse a mapping value: a single label, or an array of labels.
virtual value_type plus(const std::vector< value_type > &v) const override
Apply the additive operation to a list of values.
virtual value_type zero() const override
Return the additive identity .
virtual value_type one() const override
Return the multiplicative identity .
virtual bool mul_sub_left_distributive() const override
Return true if distributes over the monus on the left: for all .
static constexpr uint64_t UNRESTRICTED
The bit of the unrestricted world, in CONSENT mode.
virtual value_type delta(value_type x) const override
Apply the operator.
const std::vector< Oid > & label_oids() const
The labels, in enumsortorder.
virtual bool compatibleWithBooleanRewrite() const override
A Boolean algebra receives a homomorphism from the Boolean functions, so the safe-query rewriting is ...
uint64_t bit_of(Oid label) const
The labels of an enum type, in enumsortorder: their OIDs, and the rank of each OID.
std::vector< Oid > oids
label OIDs, by rank
std::unordered_map< Oid, unsigned > rank
rank of each label OID