Documentation

CslibFrontier.Languages.SQL.Column

Column metadata and name resolution #

Expressions refer to columns by position, so names never affect evaluation. Names are metadata: a Column records an optional qualifier (the table name or alias), a name and a declared type, and the columns of an n-column row are Fin n → Column. Resolving a reference [q.]name against them (resolveColumn) gives the unique matching position, or unknownColumn when no column matches, or ambiguousColumn when several do. An unqualified reference matches every column of that name; a qualified one only columns with that qualifier. Names are compared exactly: a parser normalizes case before resolution.

These facts are what a translator from surface SQL to positional expressions needs to prove its output well-scoped: a resolved position matches the reference (resolveColumn_eq_ok_iff), and the two errors say precisely that nothing or more than one column matches.

It adapts and redesigns the column lookup of sql_equiv (revision 38030a6b) by Evelyn Mitchell, where rows carried their names and lookup took the first case-insensitive match; here ambiguity is an error and names are separate from rows.

Main definitions #

Main results #

A column of a table or of a query result.

  • qualifier : Option String

    The table name or alias that qualifies the column, if any.

  • name : String

    The column name.

  • type : SqlType

    The declared type: every cell of the column is NULL or a value of this type.

Instances For
    def Cslib.SQL.instDecidableEqColumn.decEq (x✝ x✝¹ : Column) :
    Decidable (x✝ = x✝¹)
    Instances For
      @[instance_reducible]

      Why a column reference does not resolve.

      • unknownColumn : ResolveError

        No column matches the reference.

      • ambiguousColumn : ResolveError

        Several columns match the reference.

      Instances For

        The column answers to the reference [q.]name: it has that name and, when the reference is qualified, that qualifier.

        Instances For
          @[instance_reducible]
          @[simp]
          theorem Cslib.SQL.Column.matches_none {c : Column} {name : String} :
          c.Matches none name ↔ c.name = name
          @[simp]
          theorem Cslib.SQL.Column.matches_some {c : Column} {q name : String} :
          c.Matches (some q) name ↔ c.name = name ∧ c.qualifier = some q
          def Cslib.SQL.resolveColumn {n : ℕ} (cols : Fin n → Column) (q : Option String) (name : String) :

          Resolve the reference [q.]name against the columns cols: the position of the only matching column, unknownColumn if none matches, ambiguousColumn if several do.

          Instances For
            theorem Cslib.SQL.resolveColumn_eq_ok_iff {n : ℕ} {cols : Fin n → Column} {q : Option String} {name : String} {i : Fin n} :
            resolveColumn cols q name = Except.ok i ↔ (cols i).Matches q name ∧ ∀ (j : Fin n), (cols j).Matches q name → j = i

            Resolution returns i exactly when column i is the only one matching the reference.

            theorem Cslib.SQL.Column.Matches.of_resolveColumn_eq_ok {n : ℕ} {cols : Fin n → Column} {q : Option String} {name : String} {i : Fin n} (h : resolveColumn cols q name = Except.ok i) :
            (cols i).Matches q name

            A resolved reference names its column: the column has the requested name, and the requested qualifier when there is one.

            theorem Cslib.SQL.resolveColumn_eq_unknownColumn_iff {n : ℕ} {cols : Fin n → Column} {q : Option String} {name : String} :
            resolveColumn cols q name = Except.error ResolveError.unknownColumn ↔ ∀ (i : Fin n), ¬(cols i).Matches q name

            Resolution fails with unknownColumn exactly when no column matches.

            theorem Cslib.SQL.resolveColumn_eq_ambiguousColumn_iff {n : ℕ} {cols : Fin n → Column} {q : Option String} {name : String} :
            resolveColumn cols q name = Except.error ResolveError.ambiguousColumn ↔ ∃ (i : Fin n) (j : Fin n), i ≠ j ∧ (cols i).Matches q name ∧ (cols j).Matches q name

            Resolution fails with ambiguousColumn exactly when two different columns match.