ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
safe_query_cert.h
Go to the documentation of this file.
1/**
2 * @file safe_query_cert.h
3 * @brief Tractability certificate for the inversion-free UCQ(OBDD) path.
4 *
5 * Shared between the C planner side (@c src/safe_query.c, which builds the
6 * certificate from a query-level analysis) and the C++ evaluation side
7 * (@c src/probability_evaluate.cpp, which reads it back from the annotation
8 * gate's @c extra and routes probability evaluation through the
9 * structured-d-DNNF builder).
10 *
11 * The planner produces the certificate (the @c SafeCert "recipe") from the
12 * detector and stamps it on the per-row provenance root; the compact serialise
13 * / parse helpers below are the annotation-gate carrier the evaluator reads.
14 */
15#ifndef SAFE_QUERY_CERT_H
16#define SAFE_QUERY_CERT_H
17
18#include <stddef.h> /* size_t */
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/** @brief Kind of tractability certificate carried on the provenance root. */
25typedef enum SafeCertKind {
26 CERT_NONE = 0, ///< No certificate.
27 CERT_INVERSION_FREE = 1 ///< Inversion-free UCQ(OBDD) over TID inputs.
29
30/**
31 * @brief Query-derived order recipe for the structured-d-DNNF builder.
32 *
33 * The recipe is the static, query-level half of the Prop. 4.5 order: the class
34 * topological order (root class first), a relation-symbol tie-break rank per
35 * atom, and the per-atom column->class anchor map. Combined at evaluation time
36 * with the per-input keys (carried on the input annotation gates), it yields a
37 * total order consistent with Prop. 4.5. Arrays are @c palloc'd in the current
38 * memory context.
39 */
40typedef struct SafeCert {
42
43 int nclasses; ///< Number of (compacted) equivalence classes.
44 int root_class; ///< Compacted id of the root class (touches every atom).
45 int natoms; ///< Number of atoms (range-table entries).
46
47 int *class_topo_order; ///< Length @c nclasses: classes in @c G_prec topological order, root first.
48 int *atom_relation_rank; ///< Length @c natoms: relation-symbol tie-break rank per atom.
49
50 int maxarity; ///< Stride of @c atom_col_class (max columns per atom seen).
51 int *atom_col_class; ///< Flattened [natoms][maxarity]: compacted class anchored at (atom, column), or -1.
52} SafeCert;
53
54/**
55 * @brief Discriminator prefixes for the annotation gate's @c extra payload.
56 *
57 * One transparent annotation gate type carries both roles, disambiguated by the
58 * first byte of @c extra: a serialised @c SafeCert recipe on the root, and a
59 * per-input order key on each certified input leaf.
60 */
61#define SAFE_CERT_EXTRA_PREFIX_RECIPE 'C' ///< Root: serialised SafeCert recipe.
62#define SAFE_CERT_EXTRA_PREFIX_KEY 'K' ///< Input: per-variable order key.
63
64/**
65 * @brief Serialise a @c SafeCert recipe to a compact, @c C-prefixed string
66 * (palloc'd in the current memory context). Inverse of @c safe_cert_parse.
67 */
68extern char *safe_cert_serialise(const SafeCert *cert);
69
70/**
71 * @brief Parse a @c C-prefixed recipe string (as produced by
72 * @c safe_cert_serialise and read back from an annotation gate's
73 * @c extra) into a palloc'd @c SafeCert. Returns NULL if @p str is
74 * NULL, not @c C-prefixed, or malformed.
75 */
76extern SafeCert *safe_cert_parse(const char *str);
77
78/**
79 * @brief Per-input order key carried on an input leaf's annotation gate.
80 *
81 * The structured-d-DNNF builder (@c StructuredDNNFBuilder) needs, per Boolean
82 * variable, its position in the query hierarchy: @c root the root-class value
83 * (one independent block per value), @c sec the secondary-class value (one tile
84 * per value within a block), and @c factor which quantified factor the atom
85 * belongs to, or @c SAFE_CERT_GUARD_FACTOR for a self-join atom shared by every
86 * factor of its tile. This is the wire form of @c StructuredDNNFBuilder's
87 * @c InputKey.
88 *
89 * @c root and @c sec are the tuple's column values as text (the canonical output
90 * of the column type's I/O function), carried verbatim so the key works for any
91 * column type, not just integers. The builder uses them only for grouping
92 * (equal text => same block / tile) and a consistent total order, so an exact,
93 * type-faithful text rendering is all that is required; @c root / @c sec point
94 * into the parsed wire string and are @em not NUL-terminated (use the lengths).
95 */
96#define SAFE_CERT_GUARD_FACTOR (-1) /* factor of a shared self-join guard */
97
98typedef struct SafeCertKey {
99 const char *root; /* root-class value text (block); root_len bytes */
100 size_t root_len;
101 const char *sec; /* secondary-class value text (tile); sec_len bytes */
102 size_t sec_len;
103 int factor; /* factor id, or SAFE_CERT_GUARD_FACTOR for a shared guard */
105
106/**
107 * @brief Serialise a per-input order key to a compact @c K-prefixed string
108 * (palloc'd). Wire form:
109 * @c "K<factor> <root_len>:<root><sec_len>:<sec>" -- the byte-length
110 * prefixes keep arbitrary value text unambiguous. Inverse of
111 * @c safe_cert_key_parse. (The planner builds the equivalent string per
112 * row via @c inversion_free_key; this C form is for the evaluator and
113 * tests.)
114 */
115extern char *safe_cert_key_serialise(const char *root, size_t root_len,
116 const char *sec, size_t sec_len,
117 int factor);
118
119/**
120 * @brief Parse a @c K-prefixed order-key string into @p out. On success
121 * @c out->root / @c out->sec point into @p str (valid for its lifetime).
122 * Returns @c false if @p str is NULL, not @c K-prefixed, or malformed
123 * (@p out untouched).
124 */
125extern bool safe_cert_key_parse(const char *str, SafeCertKey *out);
126
127#ifdef __cplusplus
128}
129#endif
130
131#endif /* SAFE_QUERY_CERT_H */
SafeCertKind
Kind of tractability certificate carried on the provenance root.
@ CERT_NONE
No certificate.
@ CERT_INVERSION_FREE
Inversion-free UCQ(OBDD) over TID inputs.
char * safe_cert_key_serialise(const char *root, size_t root_len, const char *sec, size_t sec_len, int factor)
Serialise a per-input order key to a compact K-prefixed string (palloc'd).
SafeCert * safe_cert_parse(const char *str)
Parse a C-prefixed recipe string (as produced by safe_cert_serialise and read back from an annotation...
char * safe_cert_serialise(const SafeCert *cert)
Serialise a SafeCert recipe to a compact, C-prefixed string (palloc'd in the current memory context).
bool safe_cert_key_parse(const char *str, SafeCertKey *out)
Parse a K-prefixed order-key string into out.
const char * sec
const char * root
Query-derived order recipe for the structured-d-DNNF builder.
int nclasses
Number of (compacted) equivalence classes.
int * atom_col_class
Flattened [natoms][maxarity]: compacted class anchored at (atom, column), or -1.
int root_class
Compacted id of the root class (touches every atom).
int natoms
Number of atoms (range-table entries).
SafeCertKind kind
int maxarity
Stride of atom_col_class (max columns per atom seen).
int * class_topo_order
Length nclasses: classes in G_prec topological order, root first.
int * atom_relation_rank
Length natoms: relation-symbol tie-break rank per atom.