package granary

  1. Overview
  2. Docs
Pure-OCaml SQL engine

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.0.3.tar.gz
sha256=8b18780ea373be48301d9f333925860a2f9110fc0ac28684295118d72b65a67e
sha512=25ca3c9c5e2b528704a542502e0f37dc33ba003f65622d969b8c2b800778585f8ef0cf89b36e6679832e3993e8303aecddfc662742baf7044d6afe4a796b8f11

doc/granary.sql/Granary_sql/Sema/index.html

Module Granary_sql.SemaSource

Name resolution and light type checking for Phase 0 SQL.

Sourcetype binop =
  1. | Eq
  2. | Ne
  3. | Lt
  4. | Le
  5. | Gt
  6. | Ge
  7. | Add
  8. | Sub
  9. | Mul
  10. | Div
  11. | And
  12. | Or
  13. | Concat
  14. | Mod
  15. | Bit_and
  16. | Bit_or
  17. | Lshift
  18. | Rshift
  19. | Like
  20. | Glob
Sourcetype bound_expr =
  1. | BE_lit of Ast.literal
  2. | BE_col of int
    (*

    column ordinal in the table

    *)
  3. | BE_binop of binop * bound_expr * bound_expr
  4. | BE_not of bound_expr
  5. | BE_is_null of bound_expr
  6. | BE_is_not_null of bound_expr
  7. | BE_neg of bound_expr
  8. | BE_bitnot of bound_expr
  9. | BE_between of bound_expr * bound_expr * bound_expr
    (*

    BETWEEN predicate.

    *)
  10. | BE_in of bound_expr * bound_expr list
    (*

    IN value list predicate.

    *)
  11. | BE_func of Ast.scalar_func * bound_expr list
    (*

    Scalar function call (Phase 5).

    *)
  12. | BE_param of int
    (*

    0-indexed positional parameter (?).

    *)
  13. | BE_match of Granary_catalog.Catalog.fts_table_meta * Fts_query.t
    (*

    FTS MATCH expression: table MATCH 'query'.

    *)
  14. | BE_subquery of Ast.stmt
    (*

    Scalar subquery: (SELECT ...) in expression position.

    *)
  15. | BE_exists of Ast.stmt
    (*

    EXISTS predicate: EXISTS (SELECT ...).

    *)
  16. | BE_in_select of bound_expr * Ast.stmt
    (*

    IN subquery: x IN (SELECT ...).

    *)
  17. | BE_case of {
    1. scrutinee : bound_expr option;
    2. branches : (bound_expr * bound_expr) list;
    3. else_ : bound_expr option;
    }
    (*

    CASE scrutinee WHEN ... THEN ... ELSE ... END

    *)
  18. | BE_cast of bound_expr * Ast.ty
  19. | BE_excluded_col of int
    (*

    Reference to the i-th column of the proposed INSERT row (the 'excluded' pseudo-table).

    *)
  20. | BE_window_slot of int
    (*

    Reference to the i-th window function result appended after input columns by Op_window.

    *)
  21. | BE_collate of bound_expr * Ast.collation
    (*

    expr COLLATE collation_name

    *)
Sourcetype bound_order_key = {
  1. key : bound_expr;
  2. dir : Ast.order_dir;
  3. nulls : [ `Nulls_first | `Nulls_last ] option;
}
Sourcetype window_sema = {
  1. func : Ast.window_func;
  2. args : bound_expr list;
  3. partition_by : bound_expr list;
  4. order_by : bound_order_key list;
  5. frame : Ast.frame_spec option;
}
Sourcetype agg_spec = {
  1. func : Ast.agg_func;
  2. col_ord : int option;
}

Specification of a single aggregate computation. col_ord is None for COUNT-star and Some i for COUNT(col), SUM(col), AVG(col), MIN(col), MAX(col) where i is the column ordinal in the (combined) input row.

Sourcetype agg_proj_item =
  1. | AP_group_col of int
    (*

    project the i-th GROUP BY column (index into group_cols list)

    *)
  2. | AP_agg_slot of int
    (*

    project the i-th aggregate result from the aggregate output

    *)
  3. | AP_window_slot of int
    (*

    post-aggregate window function result

    *)

Projection item in an aggregated SELECT. The output row of Op_aggregate has shape [group_col_value; agg1; agg2; ...] when GROUP BY is present, and [agg1; agg2; ...] otherwise. Projection items below describe how to compute each projected column FROM that aggregate output row.

Sourcetype bound_join = {
  1. kind : Ast.join_kind;
  2. right_meta : Granary_catalog.Catalog.table_meta;
  3. on : bound_expr;
  4. right_col_offset : int;
    (*

    ordinal of the first right-table column in the combined row

    *)
}

A bound JOIN clause. Column ordinals in on are absolute within the combined left ++ right row: left table columns occupy 0 .. n_left-1 and right table columns occupy right_col_offset .. right_col_offset + n_right - 1.

Sourcetype seq_write =
  1. | Seq_set of {
    1. table : string;
    2. seq : int64;
    }
  2. | Seq_reset of {
    1. table : string option;
      (*

      None = DELETE with no WHERE: reset all

      *)
    }

#312.1: a bound write against the synthesized sqlite_sequence table.

Sourcetype bound_stmt =
  1. | BS_no_op
  2. | BS_col_create_table of {
    1. name : string;
    2. columns : Granary_encoding.Row.column list;
    3. if_not_exists : bool;
    }
  3. | BS_create_table of {
    1. name : string;
    2. columns : Granary_encoding.Row.column list;
    3. uniq_idxs : (string * string list * Granary_catalog.Catalog.idx_origin) list;
      (*

      Auto-generated UNIQUE index specs: (index_name, col_name; ..., origin). origin is `Implicit_pk for PRIMARY KEY constraints, `Implicit_unique for UNIQUE constraints. Planner creates Op_create_index for each.

      *)
    4. if_not_exists : bool;
    5. fk_constraints : (string list * string * string list * Granary_catalog.Catalog.fk_action * Granary_catalog.Catalog.fk_action * bool) list;
      (*

      (local_cols, parent_table, parent_cols, on_delete, on_update, deferrable)

      *)
    6. without_rowid : bool;
      (*

      When true, the table's INTEGER PRIMARY KEY column's value is used directly as the rowid (no auto-allocation). Sema enforces that exactly one INTEGER PRIMARY KEY column exists.

      *)
    7. autoincrement : bool;
      (*

      #299: INTEGER PRIMARY KEY AUTOINCREMENT. Sema validates placement (single-column ascending INTEGER PK, rowid table).

      *)
    }
  4. | BS_insert of {
    1. table_meta : Granary_catalog.Catalog.table_meta;
    2. ordinals : int list;
      (*

      column ordinals for the named cols

      *)
    3. values : bound_expr list list;
    4. on_conflict : Ast.conflict_action option;
    5. returning : bound_expr list;
    6. upsert_update : (string list * (int * bound_expr) list) option;
    }
  5. | BS_insert_select of {
    1. table_meta : Granary_catalog.Catalog.table_meta;
    2. ordinals : int list;
    3. source : bound_stmt;
    4. on_conflict : Ast.conflict_action option;
    }
  6. | BS_select of {
    1. distinct : bool;
    2. table_meta : Granary_catalog.Catalog.table_meta;
    3. proj : int list;
      (*

      column ordinals to project (refer to the combined row when join is set) — used when this is NOT an aggregated query and expr_proj is empty

      *)
    4. expr_proj : (bound_expr * string option) list;
      (*

      Phase 5: non-empty when projection contains scalar functions or other arbitrary expressions. When non-empty, proj is empty and expr_proj governs the output columns.

      *)
    5. where : bound_expr option;
    6. order : bound_order_key list;
    7. limit : int option;
    8. offset : int option;
    9. joins : bound_join list;
      (*

      Phase 10: zero or more JOINs

      *)
    10. group_by : int list;
      (*

      List of GROUP BY column ordinals (in combined row). Non-empty = GROUP BY present. Empty with non-empty aggs = one big group over all rows. Empty with empty aggs = no aggregation (ordinary SELECT).

      *)
    11. aggs : agg_spec list;
      (*

      ordered list of aggregates to compute

      *)
    12. having : bound_expr option;
      (*

      HAVING predicate. Column references inside resolve against the OUTPUT row of Op_aggregate:

      • ordinals 0..k-1 = group column values (if group_by non-empty);
      • ordinals k..k+N-1 = aggregate slots.
      *)
    13. agg_proj : agg_proj_item list;
      (*

      When aggs is non-empty, this is the projection list over the aggregate output row (ignore proj). Empty otherwise.

      *)
    14. windows : window_sema list;
    15. agg_windows : window_sema list;
      (*

      Window functions computed AFTER aggregation, over the aggregated output rows.

      *)
    }
  7. | BS_create_index of {
    1. name : string;
    2. table_meta : Granary_catalog.Catalog.table_meta;
    3. col_sqls : string list;
      (*

      col name (plain) or expr SQL (expression)

      *)
    4. col_expr_flags : bool list;
      (*

      true = expression index

      *)
    5. where_expr : bound_expr option;
    6. where_ast : Ast.expr option;
    7. unique : bool;
    8. if_not_exists : bool;
    }
  8. | BS_update of {
    1. table_meta : Granary_catalog.Catalog.table_meta;
    2. assignments : (int * bound_expr) list;
      (*

      (col_ordinal, new_value_expr)

      *)
    3. where : bound_expr option;
    4. order : bound_order_key list;
    5. limit : int option;
    6. offset : int option;
    7. returning : bound_expr list;
    }
  9. | BS_delete of {
    1. table_meta : Granary_catalog.Catalog.table_meta;
    2. where : bound_expr option;
    3. order : bound_order_key list;
    4. limit : int option;
    5. offset : int option;
    6. returning : bound_expr list;
    }
  10. | BS_drop_table of {
    1. name : string;
    2. table_meta : Granary_catalog.Catalog.table_meta;
    }
  11. | BS_drop_index of {
    1. name : string;
    2. idx_info : Granary_catalog.Catalog.index_info;
    }
  12. | BS_seq_write of seq_write
    (*

    #312.1: a supported write against the synthesized sqlite_sequence table, executed as a next_rowid mutation.

    *)
  13. | BS_begin
  14. | BS_commit
  15. | BS_rollback
  16. | BS_savepoint of string
  17. | BS_release of string
  18. | BS_rollback_to of string
  19. | BS_create_fts_table of {
    1. name : string;
    2. columns : string list;
    }
  20. | BS_fts_insert of {
    1. fts_meta : Granary_catalog.Catalog.fts_table_meta;
    2. col_names : string list;
    3. col_values : bound_expr list;
    4. rowid_value : bound_expr option;
      (*

      #330: explicit rowid, if given

      *)
    }
  21. | BS_fts_delete of {
    1. fts_meta : Granary_catalog.Catalog.fts_table_meta;
    2. where : bound_expr option;
    }
  22. | BS_fts_seq_scan of {
    1. fts_meta : Granary_catalog.Catalog.fts_table_meta;
    2. where : bound_expr option;
    }
  23. | BS_fts_match_scan of {
    1. fts_meta : Granary_catalog.Catalog.fts_table_meta;
    2. query : Fts_query.t;
    3. proj : int list;
    4. include_rank : bool;
    5. snippets : Plan.snippet_spec list;
    }
  24. | BS_pragma of {
    1. kind : Ast.pragma_kind;
    }
  25. | BS_vacuum
  26. | BS_alter_table of {
    1. table_meta : Granary_catalog.Catalog.table_meta;
    2. action : Ast.alter_action;
    }
  27. | BS_compound of {
    1. op : Ast.set_op;
    2. left : bound_stmt;
    3. right : bound_stmt;
    4. order : bound_order_key list;
    5. limit : int option;
    6. offset : int option;
    }
  28. | BS_const_select of {
    1. exprs : (bound_expr * string option) list;
    }
  29. | BS_with_cte of {
    1. name : string;
    2. def : bound_stmt;
    3. query : bound_stmt;
    4. recursive : bool;
    }
  30. | BS_create_view of {
    1. name : string;
    2. query : Ast.stmt;
    }
  31. | BS_create_reactive_view of {
    1. name : string;
    2. query : Ast.stmt;
    3. refresh : Ast.refresh_mode;
    }
  32. | BS_drop_view of {
    1. name : string;
    }
  33. | BS_create_trigger of {
    1. name : string;
    2. timing : Ast.trigger_timing;
    3. event : Ast.trigger_event;
    4. table : string;
    5. when_ : Ast.expr option;
    6. body : Ast.stmt list;
    }
  34. | BS_drop_trigger of {
    1. name : string;
    }
  35. | BS_explain of {
    1. analyze : bool;
    2. inner : bound_stmt;
    }
  36. | BS_attach of {
    1. path : string;
    2. schema : string;
    }
  37. | BS_detach of {
    1. schema : string;
    }
Sourcetype error =
  1. | Unknown_table of string
  2. | Unknown_column of {
    1. table : string;
    2. column : string;
    }
  3. | Ambiguous_column of string
  4. | Type_mismatch of {
    1. expected : Granary_encoding.Row.ty;
    2. got : Granary_encoding.Row.ty;
    }
  5. | Arity_mismatch of {
    1. expected : int;
    2. got : int;
    }
  6. | Already_exists of string
  7. | Invalid_limit of string
  8. | Unsupported of string
  9. | Not_null_violation of string
  10. | Unknown_index of string
Sourceval pp_error : Format.formatter -> error -> unit

Resolve and type-check a parsed Ast.stmt against the catalog, producing a bound_stmt or a semantic error. ?views supplies view definitions so references to views resolve.

Sourceval bind_returning_params : ?views:(string, Ast.stmt) Hashtbl.t -> Granary_catalog.Catalog.t -> Ast.stmt -> (bound_stmt * (string * int) list, error) result Lwt.t

Like bind but also returns the named-parameter-to-index mapping discovered in the statement (for :name/@name placeholders).

Sourceval output_column_names : Ast.stmt -> bound_stmt -> string list

Best-effort output column names for a row-returning statement (#387), given its Ast.stmt and the matching bound_stmt. The bound form fixes the number of output columns (* expanded to the source table's columns); AST names supply SELECT aliases and bare column names where present, and a positional col_N placeholder is used for anonymous expressions.