Class AnnotatedRelation<K>
- Type Parameters:
K- the semiring annotation type
Semiring (after Green, Karvounarakis & Tannen,
PODS 2007). This is the opt-in annotated-relation model — it is
built only when a provenance mode is active; the engine's default
Stream<Row> path is untouched and pays nothing.
The relation is held in canonical form: each distinct tuple appears
exactly once, mapped to the ⊕-combination of every
derivation that produced it, and no tuple maps to zero
(a zero annotation means "absent"). Tuple identity is the Row's own
structural equals/hashCode — the same value-equality the engine's
DISTINCT/set operators already use — so equal tuples are merged exactly as
a set relation would dedup them, but combining their annotations instead of
discarding the duplicate. Insertion order is preserved for stable output.
Choosing the semiring chooses the meaning of the annotation: the
boolean semiring
reproduces set semantics (present / absent), the
ℕ semiring
reproduces bag multiplicity, and so on. This class is the carrier model and its
defining operations — ProvenanceEvaluator threads the annotations through
the positive operators (σ/π/×/⋈/∪): lift a plain relation in,
normalise an annotated stream to canonical form, and
forget the annotations back out.
-
Method Summary
Modifier and TypeMethodDescriptionannotationOf(Row row) Returns this tuple's annotation, or the semiring'szeroif the tuple is absent from the relation.booleanisEmpty()Returns whether the relation has no present tuples.join(AnnotatedRelation<K> other, BiPredicate<Row, Row> match, Schema outputSchema, BinaryOperator<Row> concat) Join (⋈/⨝): pairs of tuples satisfyingmatch, combined byconcatand annotatedtimes(k₁, k₂)— the joint-requirement⊗.static <K> AnnotatedRelation<K> static <K> AnnotatedRelation<K> product(AnnotatedRelation<K> other, Schema outputSchema, BinaryOperator<Row> concat) Cartesian product (×): every pair of tuples, one from each side, combined byconcatand annotatedtimes(k₁, k₂).project(Schema outputSchema, UnaryOperator<Row> map) Projection (π): rewrites each tuple tooutputSchemaviamap, combining the annotations of tuples that collapse to the same output tuple with⊕(alternative derivations of one output row).rows()Forgets the annotations, yielding the present tuples as a plain row stream in insertion order — the bridge back to the engine's ordinaryStream<Row>path (and the basis for surfacing provenance without exposing it as a column).schema()Returns the schema shared by every tuple.Selection (σ): keeps each tuple satisfyingpredicatewith its annotation unchanged, and drops the rest.semiring()Returns the semiring over which this relation's annotations are combined.intsize()Returns the number of distinct present (non-zero) tuples.stream()Returns a lazy stream of the present tuples paired with their annotations, in insertion order.union(AnnotatedRelation<K> other) Union (∪): the union-compatible combination of two K-relations, with a shared tuple's annotations combined by⊕(it is derivable from either side).
-
Method Details
-
lift
Lifts a plain relation into a K-relation by annotating every input row with the semiring'sone, combining duplicate tuples with⊕. This is the canonical embedding of ordinary data: under the boolean semiring a tuple becomes simply "present", and under ℕ a tuple's annotation becomes its multiplicity in the input bag.- Type Parameters:
K- the annotation type- Parameters:
schema- the relation's schema; nevernullsemiring- the annotation semiring; nevernullrows- the plain input rows; nevernull- Returns:
- the canonical K-relation
-
normalise
public static <K> AnnotatedRelation<K> normalise(Schema schema, Semiring<K> semiring, Stream<Annotated<K>> annotated) Reduces a stream ofAnnotatedrows to canonical form: tuples that are structurally equal have their annotations combined with⊕(in encounter order), and any tuple whose combined annotation equalszerois dropped (it is absent). This is the operation that makes an annotated multiset a well-defined K-relation.- Type Parameters:
K- the annotation type- Parameters:
schema- the relation's schema; nevernullsemiring- the annotation semiring; nevernullannotated- the annotated input rows; nevernull- Returns:
- the canonical K-relation
-
schema
Returns the schema shared by every tuple.- Returns:
- the schema shared by every tuple
-
semiring
Returns the semiring over which this relation's annotations are combined.- Returns:
- the semiring over which this relation's annotations are combined
-
annotationOf
Returns this tuple's annotation, or the semiring'szeroif the tuple is absent from the relation.- Parameters:
row- the tuple to look up; nevernull- Returns:
- the tuple's annotation, or
zeroif absent
-
size
public int size()Returns the number of distinct present (non-zero) tuples.- Returns:
- the number of distinct present (non-zero) tuples
-
isEmpty
public boolean isEmpty()Returns whether the relation has no present tuples.- Returns:
- whether the relation has no present tuples
-
stream
Returns a lazy stream of the present tuples paired with their annotations, in insertion order.- Returns:
- a lazy stream of the present tuples paired with their annotations, in insertion order
-
rows
Forgets the annotations, yielding the present tuples as a plain row stream in insertion order — the bridge back to the engine's ordinaryStream<Row>path (and the basis for surfacing provenance without exposing it as a column).- Returns:
- a lazy stream of the present rows
-
select
Selection (σ): keeps each tuple satisfyingpredicatewith its annotation unchanged, and drops the rest. In semiring terms a kept tuple is multiplied byone(identity) and a dropped tuple byzero(absent), so this is simply a filter — no two surviving tuples can collide, so no⊕is needed.- Parameters:
predicate- the row predicate; nevernull- Returns:
- a K-relation of the matching tuples
-
project
Projection (π): rewrites each tuple tooutputSchemaviamap, combining the annotations of tuples that collapse to the same output tuple with⊕(alternative derivations of one output row). Under ℕ this adds the multiplicities of merged rows; under booleans it is plain duplicate elimination.- Parameters:
outputSchema- the projected schema; nevernullmap- maps an input row to its projected row; nevernull- Returns:
- the projected K-relation
-
product
public AnnotatedRelation<K> product(AnnotatedRelation<K> other, Schema outputSchema, BinaryOperator<Row> concat) Cartesian product (×): every pair of tuples, one from each side, combined byconcatand annotatedtimes(k₁, k₂). Equivalent to ajoinwhose match always holds.- Parameters:
other- the right relation; must share this relation's semiringoutputSchema- the combined schema; nevernullconcat- combines a left and right row into the output row; nevernull- Returns:
- the product K-relation
-
join
public AnnotatedRelation<K> join(AnnotatedRelation<K> other, BiPredicate<Row, Row> match, Schema outputSchema, BinaryOperator<Row> concat) Join (⋈/⨝): pairs of tuples satisfyingmatch, combined byconcatand annotatedtimes(k₁, k₂)— the joint-requirement⊗. Output tuples that coincide have their annotations⊕-combined. Under booleans this is the ordinary join; under ℕ the result multiplicity is the product of the matched multiplicities.- Parameters:
other- the right relation; must share this relation's semiringmatch- whether a left/right row pair joins; nevernulloutputSchema- the combined schema; nevernullconcat- combines a matched left and right row; nevernull- Returns:
- the join K-relation
-
union
Union (∪): the union-compatible combination of two K-relations, with a shared tuple's annotations combined by⊕(it is derivable from either side). Under booleans this is set union; under ℕ the multiplicities add (bag union). Both relations must have equal schemas and share the semiring.- Parameters:
other- the other relation; same schema and semiring- Returns:
- the union K-relation
-