Documentation

Provenance.AggQueryHavingRewriting

The rewritten world's evaluator: tokens as ordinary column values #

ProvSQL evaluates rewritten plans over a value universe that contains, next to the regular values and the provenance identifiers, the aggregate tokens produced by its provsql_agg gate; the provsql_having gate then reads a token and produces the predicate provenance of an aggregate comparison. The formal counterpart is the evaluator AggQuery.evaluateRew defined here: it runs a rewritten query (a AggQuery over the composite value type T ⊕ K) over rows Tuple (GenValue (T ⊕ K) K) n – the lifted-column carrier of the general evaluator, instantiated at the composite value type – with the kind vector saying which columns hold tokens.

The rewriting rules built on this evaluator live downstream: Provenance.AggQueryGroupRewriting (the bare grouping and the HAVING site) and Provenance.AggQueryClosure (the compositional closure).

Terms and predicates in the rewritten world #

def Sum.annPart {T K : Type} [CommSemiringWithMonus K] :
T KK

The annotation part of a composite value (𝟘 on data values: a malformed provenance read carries no worlds).

Equations
Instances For
    Dependency graph
    def TermG.evalRew {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} :
    TermG (T K) κTuple (GenValue (T K) K) nT K

    Term evaluation in the rewritten world: as TermG.eval on the value-reading constructors, with the cmpAgg gate interpreted by the predicate provenance of the token against the comparison term, and the chiGate gate by the characteristic value of its comparison.

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

      Projection-column evaluation in the rewritten world.

      Equations
      Instances For
        Dependency graph
        def GenPred.holdsRew {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} :
        GenPred (T K) κTuple (GenValue (T K) K) nProp

        Classical truth of a predicate in the rewritten world (compared tokens read through their deterministic collapse, as in GenPred.holds).

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

          Structural decidability of holdsRew.

          Equations
          Instances For
            Dependency graph
            @[instance_reducible]
            Equations
            Dependency graph

            The evaluator #

            def AggQuery.evaluateRew {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} :
            AggQuery (T K) n κDatabase (T K)Multiset (Tuple (GenValue (T K) K) n)

            The rewritten world's evaluator: plain multiset semantics over token-bearing rows. Value-kinded operators act through the inl embedding; GammaTok builds tokens and the group guard; the gates inside terms are interpreted by predProv and Having.chi.

            Equations
            Instances For
              Dependency graph

              Agreement with the plain semantics off the gates #

              def AggQuery.noGammaTok {T' : Type} {n : } {κ : Fin nColKind} :
              AggQuery T' n κProp

              No token-building grouping: together with gate-freeness, this cuts out the fragment on which the rewritten world's evaluator is the plain semantics through the inl embedding.

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

                No indicator gate anywhere in a query's terms and predicates.

                Equations
                Instances For
                  Dependency graph
                  theorem TermG.evalRew_inl {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} (t : TermG (T K) κ) :
                  t.chiFree∀ (u : Tuple (T K) n), (t.evalRew fun (k : Fin n) => Sum.inl (u k)) = t.evalPlain u

                  On inl-embedded rows a gate-free term evaluates in the rewritten world as its plain evaluation – including the cmpAgg gate, whose junk reading 𝟘 is definitionally the composite zero on a row with no token. The indicator gate has no such escape: it returns a genuine annotation, which is why it is excluded here.

                  Dependency graph
                  theorem ProjCol.evalRew_inl {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} (p : ProjCol (T K) κ) (hp : p.chiFree) (u : Tuple (T K) n) :
                  (p.evalRew fun (k : Fin n) => Sum.inl (u k)) = Sum.inl (p.evalPlain u)

                  Gate-free projection columns on inl-embedded rows evaluate to the embedded plain reading.

                  Dependency graph
                  theorem GenPred.holdsRew_inl {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} (φ : GenPred (T K) κ) :
                  φ.chiFree∀ (u : Tuple (T K) n), (φ.holdsRew fun (k : Fin n) => Sum.inl (u k)) φ.holdsPlain u

                  Gate-free predicates on inl-embedded rows hold as their plain reading.

                  Dependency graph
                  theorem Multiset.map_product_map {α : Type u_1} {β : Type u_2} {α' : Type u_3} {β' : Type u_4} (f : αα') (g : ββ') (s : Multiset α) (t : Multiset β) :
                  (map f s).product (map g t) = map (Prod.map f g) (s.product t)

                  Maps push through the multiset product.

                  Dependency graph
                  theorem map_plainTuple_map_inl {T K : Type} {m : } (X : Multiset (Tuple (T K) m)) :
                  Multiset.map (fun (u : Tuple (GenValue (T K) K) m) => GenRow.plainTuple u) (Multiset.map (fun (t : Tuple (T K) m) (k : Fin m) => Sum.inl (t k)) X) = X

                  Collapsing inl-embedded rows is the identity.

                  Dependency graph
                  theorem AggQuery.evaluateRew_plain {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} (q : AggQuery (T K) n κ) (_hq : q.noGammaTok) (_hc : q.chiFree) (D : Database (T K)) :
                  q.evaluateRew D = Multiset.map (fun (t : Tuple (T K) n) (k : Fin n) => Sum.inl (t k)) (q.evaluatePlain D)

                  Plain agreement. Off the token-building operator, the rewritten world's evaluator is the plain semantics through the inl embedding.

                  Dependency graph

                  The fused predicate provenance under the composite embedding #

                  The rewritten site groups composite rows – the inl-embedded data with the annotation appended as the provenance column – while the annotated site groups the original annotated tuples. The fused predicate provenance is invariant under this embedding: comparisons restrict along inl, the lifted aggregate computes on the embedded values, and the occurrence annotations are read off unchanged.

                  Lift a sequence aggregate to the composite domain (junk on the annotation arm, faithful on inl-embedded values).

                  Equations
                  Instances For
                    Dependency graph

                    The lifted aggregate on inl-embedded values.

                    Dependency graph
                    theorem CompOp.eval_inl {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [HasAltLinearOrder K] (op : CompOp) (x y : T) :
                    op.eval (Sum.inl x) (Sum.inl y) op.eval x y

                    Comparison operators restrict along the inl embedding.

                    Dependency graph
                    theorem Having.chi_inl {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [HasAltLinearOrder K] (op : CompOp) (x y : T) :
                    chi op (Sum.inl x) (Sum.inl y) = chi op x y

                    The comparison indicator restricts along the inl embedding.

                    Dependency graph

                    The classical rewriting stays off the token operators #

                    theorem AggQuery.rewriting_noGammaTok {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} (q : AggQuery T n κ) (hq : q.classical) :

                    The classical rewriting emits no token-building grouping.

                    Dependency graph
                    theorem AggQuery.rewriting_chiFree {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} (q : AggQuery T n κ) (hq : q.classical) :

                    The classical rewriting emits no indicator gate: its terms are column reads, their / combinations, and composite casts of the source terms – the gate is introduced only by the HAVING site.

                    Dependency graph

                    The group sequence under the composite embedding #

                    theorem AnnotatedTuple.toComposite_coord {T K : Type} {m : } (p : AnnotatedTuple T K m) (j : Fin (m + 1)) :
                    p.toComposite j = if h : j < m then Sum.inl (p.1 j, h) else Sum.inr p.2

                    Coordinates of the composite embedding of an annotated tuple.

                    Dependency graph
                    theorem Sum.inl_lt_inl_composite {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [HasAltLinearOrder K] (x y : T) :
                    inl x < inl y x < y

                    The composite order restricts to the value order on inl.

                    Dependency graph
                    theorem Sum.inr_lt_inr_composite {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [HasAltLinearOrder K] (x y : K) :
                    inr x < inr y x < y

                    The composite order restricts to the alternative order on inr.

                    Dependency graph
                    theorem Having.havingGroup_toComposite {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [HasAltLinearOrder K] {m n₁ : } (is : Tuple (Fin m) n₁) (r : AnnotatedRelation T K m) (g : Tuple T n₁) :
                    (havingGroup (fun (k : Fin n₁) => Fin.castLE (is k)) (Multiset.map (fun (p : AnnotatedTuple T K m) => (p.toComposite, p.2)) r) fun (k : Fin n₁) => Sum.inl (g k)) = List.map (fun (p : AnnotatedTuple T K m) => (p.toComposite, p.2)) (havingGroup is r g)

                    The group sequence under the composite embedding: embedding the relation and the key inl-wise embeds the group sequence. The embedding is monotone for the sort's tie-break order (data columns compare on the inl arm, the appended provenance column and the annotation both by the alternative order), and sorted lists of the same multiset are unique.

                    Dependency graph

                    Reading a rewritten evaluation back as an annotated relation #

                    Mapping a key-only function over a grouped relation is mapping it over the deduplicated keys (the accumulated annotations are unread).

                    Dependency graph
                    theorem AggQuery.rewriting_provRel {T : Type} [ValueType T] {K : Type} [CommSemiringWithMonus K] [DecidableEq K] [HasAltLinearOrder K] {n : } {κ : Fin nColKind} (q : AggQuery T n κ) (hq : q.classical) (d : AnnotatedDatabase T K) :

                    The rewritten world reads back as an annotated relation. Pairing the collapsed data columns of the rewritten evaluation of a classical rewriting with the annotation read off its provenance column recovers the composite embedding of the classical annotated semantics – the input the token-building groupings of the rewritten world consume.

                    Dependency graph