Documentation

Provenance.AggQuery

Kind-indexed general queries and their annotated semantics #

The general (non-fused) HAVING semantics: aggregate values produced by a grouping operator γ^≼ are carried through further operators – projection, join, union, additional selections – as symbolic tokens (AggValue) in dedicated columns, and compared downstream, the possible worlds of such a comparison being those of the originating group.

Kind-indexed syntax #

Queries are indexed by a column-kind vector κ : Fin n → ColKind (regular vs aggregate-token), so that the scope conditions are enforced statically and no theorem carries a well-formedness hypothesis:

Factored annotations and the σ/predsem combination #

The row annotation of the general evaluator is kept in factored form GenAnn: a concrete part base : K together with pending, a multiset of group-existence factors – one entry per γ-group whose tokens have not yet been compared, recorded as the group's occurrence-annotation list l and worth δ(⊕ l). The effective annotation of a row is base ⊗ ⊗_{l ∈ pending} δ(⊕ l) (GenAnn.finalize).

This factoring implements the replace-the-δ-factor combination rule:

∧ ↦ ⊗, ∨ ↦ ⊕ and ¬ pushed down to the atoms by De Morgan duality with comparison-operator complementation, exactly as in HavingPred and in ProvSQL. A selection whose predicate contains no aggregate atom filters classically, matching Query.evaluateAnnotated.

Scalar aggregation (aggregation without grouping, whose empty input is a real possible world in ProvSQL) is out of scope: Gamma is the grouped operator only.

inductive ColKind :

The kind of a column: a regular value or an aggregate token.

Instances For
    Dependency graph
    @[instance_reducible]
    Equations
    Dependency graph

    The value-arm kind of a column kind: prov columns hold ordinary values (as ProvSQL's uuid columns do), so their conformance arm is reg.

    Equations
    Instances For
      Dependency graph
      Dependency graph
      @[reducible, inline]
      abbrev GenValue (T K : Type) :

      A lifted column value: a regular value or an aggregate token.

      Equations
      Instances For
        Dependency graph
        structure GenAnn (K : Type) :

        The factored annotation of a row of the general evaluator: the concrete part base, and one pending group-existence factor per γ-group whose tokens have not been compared yet, recorded as the group's occurrence-annotation list.

        • base : K

          The concrete annotation accumulated so far.

        • pending : Multiset (List K)

          The occurrence-annotation lists of the uncompared groups.

        Instances For
          Dependency graph

          The effective annotation: the concrete part times the pending group-existence factors δ(⊕ l).

          Equations
          Instances For
            Dependency graph
            @[reducible, inline]
            abbrev GenRow (T K : Type) (n : ) :

            A row of the general evaluator.

            Equations
            Instances For
              Dependency graph
              @[simp]
              theorem GenAnn.finalize_of_pending_zero {K : Type} [CommSemiringWithMonus K] (b : K) :
              { base := b, pending := 0 }.finalize = b

              A row with nothing pending finalizes to its concrete part.

              Dependency graph
              @[simp]
              theorem GenAnn.finalize_gamma {K : Type} [CommSemiringWithMonus K] (l : List K) :
              { base := 1, pending := {l} }.finalize = SemiringWithMonus.delta l.sum

              An uncompared γ-row (concrete part 𝟙, its group factor pending) finalizes to δ(⊕ U) – the ProvSQL annotation of a plain GROUP BY output row.

              Dependency graph

              Terms over regular columns #

              inductive TermG (T : Type) {n : } (κ : Fin nColKind) :

              A term over the regular columns of a kind-indexed tuple: the index constructor requires its column to be regular, so terms over token columns are unrepresentable.

              • const {T : Type} {n : } {κ : Fin nColKind} : TTermG T κ
              • index {T : Type} {n : } {κ : Fin nColKind} (k : Fin n) : κ k = ColKind.regTermG T κ
              • provIndex {T : Type} {n : } {κ : Fin nColKind} (k : Fin n) : κ k = ColKind.provTermG T κ
              • cmpAgg {T : Type} {n : } {κ : Fin nColKind} (k : Fin n) : κ k = ColKind.aggCompOpTermG T κTermG T κ

                The aggregate-comparison gate (ProvSQL's provsql_having): the predicate provenance of comparing the token in column k against the term. Its faithful semantics lives in the rewritten world's term evaluator; the generic evaluators give it a total junk value, and on token-free kinds the constructor is unrepresentable.

              • chiGate {T : Type} {n : } {κ : Fin nColKind} : CompOpTermG T κTermG T κTermG T κ

                The regular-comparison indicator gate: the characteristic value χ of a comparison between two regular terms – 𝟙 if it holds on the row, 𝟘 otherwise. It is the primitive a HAVING predicate needs for its regular atoms, and like cmpAgg its faithful semantics lives in the rewritten world's term evaluator – the generic evaluators give it a total junk value. Unlike cmpAgg it carries no kind constraint, so it is representable over all-regular columns: the fragment on which the rewritten world's evaluator collapses to the plain semantics is cut out by TermG.chiFree instead.

              • add {T : Type} {n : } {κ : Fin nColKind} : TermG T κTermG T κTermG T κ
              • sub {T : Type} {n : } {κ : Fin nColKind} : TermG T κTermG T κTermG T κ
              • mul {T : Type} {n : } {κ : Fin nColKind} : TermG T κTermG T κTermG T κ
              Instances For
                Dependency graph
                def TermG.eval {T : Type} [ValueType T] {K : Type} {n : } {κ : Fin nColKind} (t : TermG T κ) (u : Tuple (GenValue T K) n) :
                T

                Evaluation of a term on a lifted tuple. On the regular columns the kind index guarantees a regular value; the token arm of collapseSum is never reached on kind-conformant tuples and merely keeps the function total.

                Equations
                Instances For
                  Dependency graph

                  Generalized selection predicates #

                  inductive GenPred (T : Type) {n : } (κ : Fin nColKind) :

                  A generalized selection predicate: regular comparisons between terms over regular columns, aggregate comparisons of one bare token column against a regular term (the constant-folded normal form), and Boolean structure.

                  Instances For
                    Dependency graph
                    def GenPred.hasAggAtom {T : Type} {n : } {κ : Fin nColKind} :
                    GenPred T κBool

                    Does the predicate contain an aggregate atom? Selections without one filter classically.

                    Equations
                    Instances For
                      Dependency graph
                      def GenPred.holds {T : Type} [ValueType T] {K : Type} {n : } {κ : Fin nColKind} (φ : GenPred T κ) (u : Tuple (GenValue T K) n) :

                      Classical (per-tuple) truth of a predicate, reading a compared token through its deterministic collapse. Used by the evaluator only on aggregate-atom-free predicates, where tokens are never consulted.

                      Equations
                      Instances For
                        Dependency graph
                        def GenPred.decHolds {T : Type} [ValueType T] {K : Type} {n : } {κ : Fin nColKind} (φ : GenPred T κ) (u : Tuple (GenValue T K) n) :

                        Structural decidability of holds.

                        Equations
                        Instances For
                          Dependency graph
                          @[instance_reducible]
                          instance GenPred.instDecidablePredTupleGenValueHolds {T : Type} [ValueType T] {K : Type} {n : } {κ : Fin nColKind} (φ : GenPred T κ) :
                          Equations
                          Dependency graph
                          def GenPred.predsem {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] {n : } {κ : Fin nColKind} (φ : GenPred T κ) (neg : Bool) (u : Tuple (GenValue T K) n) :
                          K

                          Predicate provenance of a generalized predicate on a row, with ¬ pushed down to the atoms by De Morgan duality (the neg flag): a regular atom contributes its characteristic value χ, an aggregate atom the predicate provenance predProv of the comparison over its token's group, ∧ ↦ ⊗ and ∨ ↦ ⊕ (swapped under neg), and negated atoms complement their comparison operator, as in ProvSQL.

                          Equations
                          Instances For
                            Dependency graph
                            def GenPred.comparedCols {T : Type} {n : } {κ : Fin nColKind} :
                            GenPred T κFinset (Fin n)

                            The token columns compared by the predicate's aggregate atoms.

                            Equations
                            Instances For
                              Dependency graph
                              def GenPred.entailsExistence {T : Type} {n : } {κ : Fin nColKind} :
                              GenPred T κBoolBool

                              Does the predicate provenance entail the compared groups' existence (under the polarity neg of the enclosing negations)? An aggregate atom does – its predicate provenance ranges over non-empty worlds only – while a regular atom's χ does not. A conjunction ( positively, under negation) entails as soon as one factor does; a disjunction only if every disjunct does. Mirrors ProvSQL's having_entails_group_existence: the supersede of the group-existence factor is licensed only when this holds, since e.g., agg-atom ∨ regular-atom can fire in worlds where the group is empty.

                              Equations
                              Instances For
                                Dependency graph

                                Projection columns #

                                inductive ProjCol (T : Type) {n : } (κ : Fin nColKind) :

                                One output column of a generalized projection: a regular term over the regular input columns, or a verbatim copy of a token column (no arithmetic over tokens: the normal form).

                                Instances For
                                  Dependency graph
                                  def ProjCol.kind {T : Type} {n : } {κ : Fin nColKind} :
                                  ProjCol T κColKind

                                  The kind of the output column.

                                  Equations
                                  Instances For
                                    Dependency graph
                                    def ProjCol.eval {T : Type} [ValueType T] {K : Type} {n : } {κ : Fin nColKind} (p : ProjCol T κ) (u : Tuple (GenValue T K) n) :

                                    Evaluation of a projection column on a lifted tuple.

                                    Equations
                                    Instances For
                                      Dependency graph

                                      Kind-indexed queries #

                                      def ColKind.allReg (n : ) :
                                      Fin nColKind

                                      The all-regular kind vector.

                                      Equations
                                      Instances For
                                        Dependency graph
                                        inductive AggQuery (T : Type) (n : ) :
                                        (Fin nColKind)Type

                                        Kind-indexed general queries. The index discipline enforces the scope conditions: Gamma aggregates an all-regular input, Dedup and Diff require all-regular kinds, and the projection/selection grammars never compute over tokens.

                                        Instances For
                                          Dependency graph
                                          def AggQuery.castKind {T : Type} {n : } {κ κ' : Fin nColKind} (h : κ = κ') :
                                          AggQuery T n κAggQuery T n κ'

                                          Transport a query along an equality of kind vectors (kind vectors arising from projections are rarely definitionally all-regular).

                                          Equations
                                          Instances For
                                            Dependency graph

                                            The general evaluator #

                                            def GenRow.plainTuple {T K : Type} {n : } (u : Tuple (GenValue T K) n) :
                                            Tuple T n

                                            The regular-value reading of a lifted tuple (token columns collapse; on the all-regular rows fed to Dedup, Diff and Gamma no token occurs).

                                            Equations
                                            Instances For
                                              Dependency graph
                                              def GenRow.toAnnotated {T K : Type} [CommSemiringWithMonus K] {n : } (r : GenRow T K n) :

                                              Finalize a general row into an annotated tuple: collapse the tuple to its regular reading and cash the pending group factors.

                                              Equations
                                              Instances For
                                                Dependency graph
                                                def GenRow.ofAnnotated {T K : Type} {n : } (p : AnnotatedTuple T K n) :
                                                GenRow T K n

                                                Embed an annotated tuple as a general row (all-regular, nothing pending).

                                                Equations
                                                Instances For
                                                  Dependency graph
                                                  def tokenLists {T K : Type} {n : } (u : Tuple (GenValue T K) n) :

                                                  The multiset of occurrence-annotation lists of the token columns of a tuple (used by projection to detect dropped groups).

                                                  Equations
                                                  Instances For
                                                    Dependency graph
                                                    def TermG.evalPlain {T : Type} [ValueType T] {n : } {κ : Fin nColKind} (t : TermG T κ) (u : Tuple T n) :
                                                    T
                                                    Equations
                                                    Instances For
                                                      Dependency graph

                                                      The gate-free fragment #

                                                      The indicator gate TermG.chiGate is the one term constructor whose faithful reading needs the rewritten world: it produces a provenance value out of a comparison between regular values, which the generic evaluators – having no annotation to return – can only approximate by the junk constant. The cmpAgg gate escapes the same fate only because its kind constraint keeps it off the columns the plain semantics sees. The predicates below cut out the fragment where no indicator gate occurs, on which the rewritten world's evaluator is the plain semantics (AggQuery.evaluateRew_plain).

                                                      def TermG.chiFree {n : } {T' : Type} {κ : Fin nColKind} :
                                                      TermG T' κProp

                                                      No indicator gate in a term.

                                                      Equations
                                                      Instances For
                                                        Dependency graph
                                                        def GenPred.chiFree {n : } {T' : Type} {κ : Fin nColKind} :
                                                        GenPred T' κProp

                                                        No indicator gate in a predicate.

                                                        Equations
                                                        Instances For
                                                          Dependency graph
                                                          def ProjCol.chiFree {n : } {T' : Type} {κ : Fin nColKind} :
                                                          ProjCol T' κProp

                                                          No indicator gate in a projection column.

                                                          Equations
                                                          Instances For
                                                            Dependency graph
                                                            def AggQuery.evaluate {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} :
                                                            AggQuery T n κAnnotatedDatabase T KMultiset (GenRow T K n)

                                                            The general annotated evaluator. All operators preserve the factored-annotation discipline described in the module docstring.

                                                            Equations
                                                            Instances For
                                                              Dependency graph

                                                              The final annotated relation computed by a general query: evaluate, then finalize every row.

                                                              Equations
                                                              Instances For
                                                                Dependency graph
                                                                @[simp]

                                                                Embedding then finalizing is the identity on annotated tuples.

                                                                Dependency graph

                                                                The plain evaluator #

                                                                The classical (per-instance) semantics of a general query: aggregate columns hold the computed aggregate values, and every selection filters classically – including aggregate comparisons, evaluated on the computed values. This is the semantics the data-part adequacy connects to the annotated evaluator through AggValue.collapse (the annotated side keeps classically-failing rows with annotation 𝟘, exactly as ProvSQL emits them, so adequacy is stated on the query stripped of its aggregate selections and differences, stripAgg).

                                                                def GenPred.holdsPlain {T : Type} [ValueType T] {n : } {κ : Fin nColKind} (φ : GenPred T κ) (u : Tuple T n) :

                                                                Classical truth of a predicate on a regular tuple: aggregate atoms compare the computed aggregate value of their column.

                                                                Equations
                                                                Instances For
                                                                  Dependency graph
                                                                  def GenPred.decHoldsPlain {T : Type} [ValueType T] {n : } {κ : Fin nColKind} (φ : GenPred T κ) (u : Tuple T n) :

                                                                  Structural decidability of holdsPlain.

                                                                  Equations
                                                                  Instances For
                                                                    Dependency graph
                                                                    @[instance_reducible]
                                                                    Equations
                                                                    Dependency graph
                                                                    def ProjCol.evalPlain {T : Type} [ValueType T] {n : } {κ : Fin nColKind} (p : ProjCol T κ) (u : Tuple T n) :
                                                                    T

                                                                    Plain evaluation of a projection column.

                                                                    Equations
                                                                    Instances For
                                                                      Dependency graph
                                                                      def AggQuery.evaluatePlain {T : Type} [ValueType T] {n : } {κ : Fin nColKind} :
                                                                      AggQuery T n κDatabase TRelation T n

                                                                      The plain evaluator: standard multiset semantics, with Gamma computing the aggregate of each group's full occurrence sequence (in the canonical order of Relation.groupSeq) and every selection filtering classically. Diff is the all-or-nothing difference of Query.evaluate.

                                                                      Equations
                                                                      Instances For
                                                                        Dependency graph
                                                                        def AggQuery.stripAgg {T : Type} {n : } {κ : Fin nColKind} :
                                                                        AggQuery T n κAggQuery T n κ

                                                                        Strip a general query of the constructs whose annotated data part keeps rows the classical semantics removes: differences (annotated Diff never removes tuple slots) and selections containing an aggregate atom (the annotated evaluator keeps classically-failing rows annotated 𝟘, as ProvSQL emits them). The data-part adequacy of evaluateAnnotated is stated against the plain evaluation of the stripped query, mirroring Query.stripDiff in Provenance.QueryAdequacy.

                                                                        Equations
                                                                        Instances For
                                                                          Dependency graph
                                                                          def AggQuery.noProvSum {T : Type} {n : } {κ : Fin nColKind} :
                                                                          AggQuery T n κProp

                                                                          No plan-level provenance aggregation. The possible-world metatheorems (random-world commutation, PQE) are about source queries; ProvSum is a rewriting-target operator whose deterministic group sum is not world-faithful – exactly as the classical Agg was excluded from the annotated evaluators.

                                                                          Equations
                                                                          Instances For
                                                                            Dependency graph

                                                                            Kind conformance #

                                                                            Rows produced by the general evaluator conform to the query's kind vector: regular columns hold regular values, token columns hold tokens. This is an invariant lemma, not a hypothesis: the kind-indexed syntax makes it hold by construction, and downstream theorems (the random-world commutation in particular) invoke it instead of assuming wellformedness.

                                                                            def GenValue.kindOf {T K : Type} :

                                                                            The kind of a lifted value.

                                                                            Equations
                                                                            Instances For
                                                                              Dependency graph
                                                                              theorem AggQuery.evaluate_conform {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} (q : AggQuery T n κ) (d : AnnotatedDatabase T K) (r : GenRow T K n) :
                                                                              r q.evaluate d∀ (k : Fin n), (r.1 k).kindOf = (κ k).base

                                                                              Kind conformance of the general evaluator.

                                                                              Dependency graph