Documentation

CslibFrontier.Languages.SQL.Row

SQL rows and their types #

A row of an n-column table is Row n := Fin n → Datum: columns are referenced by position, so a reference to a missing column cannot be written. Rows are written ![d₀, d₁, …], and their equality is decidable through Mathlib's instance for functions on a Fintype, which the kernel evaluates: decide compares concrete rows, also rows computed by reindexing.

The type information that expressions are checked against is a typing context Γ : Fin n → SqlType, and a row conforms to it when every cell is NULL or a value of its column's type (Row.Conforms).

The row of a join is Fin.append of the two input rows, so the left columns are Fin.castAdd and the right columns Fin.natAdd; a joined row conforms to the appended context exactly when both inputs conform (Row.conforms_append_iff).

It adapts and redesigns the rows of sql_equiv (revision 38030a6b) by Evelyn Mitchell, which were lists of name–value pairs whose schema came from the data; here positions are fixed by the type and names are separate metadata (Cslib.SQL.Column).

Main definitions #

Main results #

@[reducible, inline]
abbrev Cslib.SQL.Row (n : ℕ) :

A row of an n-column table, by position. It is an abbrev, so rows are functions: ![…], Fin.append and Fin.cons build them, and they inherit decidable equality.

Instances For
    def Cslib.SQL.Row.Conforms {n : ℕ} (Γ : Fin n → SqlType) (r : Row n) :

    The row conforms to the typing context Γ: every cell is NULL or a value of its column's type.

    Instances For
      @[instance_reducible]
      instance Cslib.SQL.Row.instDecidableConforms {n : ℕ} (Γ : Fin n → SqlType) (r : Row n) :
      theorem Cslib.SQL.Row.conforms_null {n : ℕ} (Γ : Fin n → SqlType) :
      Conforms Γ fun (x : Fin n) => none

      The all-NULL row conforms to every typing context.

      theorem Cslib.SQL.Row.Conforms.comp {n m : ℕ} {Γ : Fin n → SqlType} {r : Row n} (h : Conforms Γ r) (ρ : Fin m → Fin n) :
      Conforms (Γ ∘ ρ) (r ∘ ρ)

      A row conforms after reindexing when it conforms before.

      theorem Cslib.SQL.Row.conforms_append_iff {n m : ℕ} {Γ : Fin n → SqlType} {Δ : Fin m → SqlType} {r : Row n} {s : Row m} :

      A joined row conforms to the joined typing context exactly when both sides conform.