package granary

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Granary_sql.PlanSource

Physical query-plan IR.

The planner (Planner) lowers a bound Ast.stmt into an op tree, which the executor (Exec) walks to produce rows. Plan expressions (expr) differ from Ast.expr in that column references are resolved to integer ordinals. Every type is exposed concretely because the planner constructs and the executor matches on the full structure.

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 expr =
  1. | P_lit of Ast.literal
  2. | P_col of int
    (*

    column ordinal

    *)
  3. | P_binop of binop * expr * expr
  4. | P_not of expr
  5. | P_is_null of expr
  6. | P_is_not_null of expr
  7. | P_neg of expr
  8. | P_bitnot of expr
  9. | P_between of expr * expr * expr
  10. | P_in of expr * expr list
  11. | P_func of Ast.scalar_func * expr list
  12. | P_param of int
    (*

    0-indexed positional parameter

    *)
  13. | P_subquery of Ast.stmt
  14. | P_exists of Ast.stmt
  15. | P_in_select of expr * Ast.stmt
  16. | P_case of {
    1. scrutinee : expr option;
    2. branches : (expr * expr) list;
    3. else_ : expr option;
    }
  17. | P_cast of expr * Ast.ty
  18. | P_excluded_col of int
    (*

    Column reference into the proposed INSERT excluded row.

    *)
  19. | P_window_slot of int
    (*

    Window function slot reference — substituted to P_col(n_input_cols+i) by planner.

    *)
  20. | P_collate of expr * Ast.collation
Sourcetype window_plan_item = {
  1. func : Ast.window_func;
  2. args : expr list;
  3. partition_by : expr list;
  4. order_by : (expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list;
  5. frame : Ast.frame_spec option;
}
Sourcetype snippet_spec = {
  1. col_idx : int;
  2. start_tag : string;
  3. end_tag : string;
  4. ellipsis : string;
  5. n_tokens : int;
}
Sourcetype op =
  1. | Op_create_table of {
    1. name : string;
    2. columns : Granary_encoding.Row.column list;
    3. uniq_idxs : (string * string list * Cat.idx_origin) list;
    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;
    7. autoincrement : bool;
      (*

      #299: INTEGER PRIMARY KEY AUTOINCREMENT.

      *)
    }
  2. | Op_col_create_table of {
    1. name : string;
    2. columns : Granary_encoding.Row.column list;
    3. if_not_exists : bool;
    }
    (*

    DDL: CREATE TABLE name (...) USING COLUMNSTORE. Registers the table in the catalog with Columnar storage.

    *)
  3. | Op_insert of {
    1. table_meta : Cat.table_meta;
    2. ordinals : int list;
    3. values : expr list list;
    4. on_conflict : Ast.conflict_action option;
    5. returning : expr list;
    6. upsert_update : (string list * (int * expr) list) option;
    }
  4. | Op_insert_select of {
    1. table_meta : Cat.table_meta;
    2. ordinals : int list;
    3. source : op;
    4. on_conflict : Ast.conflict_action option;
    }
  5. | Op_seq_scan of {
    1. table_meta : Cat.table_meta;
    }
  6. | Op_filter of {
    1. pred : expr;
    2. child : op;
    }
  7. | Op_project of {
    1. ordinals : int list;
    2. child : op;
    }
  8. | Op_expr_project of {
    1. exprs : (expr * string option) list;
    2. child : op;
    }
  9. | Op_sort of {
    1. keys : (expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list;
    2. child : op;
    }
  10. | Op_limit of {
    1. limit : int;
    2. offset : int;
    3. child : op;
    }
  11. | Op_create_index of {
    1. name : string;
    2. table : string;
    3. tree_id : int;
      (*

      table's tree_id

      *)
    4. col_sqls : string list;
      (*

      col name (plain) or expr SQL (expression)

      *)
    5. col_expr_flags : bool list;
      (*

      true = expression index column, false = plain column

      *)
    6. where_expr : expr option;
    7. where_sql : string option;
    8. unique : bool;
    9. columns : Granary_encoding.Row.column list;
      (*

      columns of the target table — needed for row decoding during index population

      *)
    10. if_not_exists : bool;
    }
  12. | Op_index_lookup of {
    1. table_tree : int;
      (*

      table's tree_id

      *)
    2. idx_tree : int;
      (*

      index tree_id

      *)
    3. col_idx : int;
      (*

      column ordinal for encoding

      *)
    4. col_type : Granary_encoding.Row.ty;
    5. lookup_val : expr;
      (*

      value to look up

      *)
    6. table_meta : Cat.table_meta;
      (*

      for row decoding

      *)
    }
  13. | Op_rowid_lookup of {
    1. table_meta : Cat.table_meta;
    2. lookup_val : expr;
      (*

      #243 (T1): point lookup on an INTEGER PRIMARY KEY rowid alias — a single table-tree seek by the integer key, no index.

      *)
    }
  14. | Op_col_seq_scan of {
    1. table_meta : Cat.table_meta;
    }
    (*

    Scan a columnar table; emits one Row.t per stored row.

    *)
  15. | Op_update of {
    1. table_meta : Cat.table_meta;
    2. assignments : (int * expr) list;
      (*

      (col_ordinal, new_value_expr)

      *)
    3. where : expr option;
    4. order : (expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list;
    5. limit : int option;
    6. offset : int option;
    7. indexes : Cat.index_info list;
    8. returning : expr list;
    }
  16. | Op_delete of {
    1. table_meta : Cat.table_meta;
    2. where : expr option;
    3. order : (expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list;
    4. limit : int option;
    5. offset : int option;
    6. indexes : Cat.index_info list;
    7. returning : expr list;
    }
  17. | Op_drop_table of {
    1. table_meta : Cat.table_meta;
    2. indexes : Cat.index_info list;
    }
  18. | Op_drop_index of {
    1. idx_info : Cat.index_info;
    }
  19. | Op_nested_loop_join of {
    1. left : op;
      (*

      left input (any op stream)

      *)
    2. right_meta : Cat.table_meta;
      (*

      right table for row decode

      *)
    3. idx_tree : int;
      (*

      right-side index tree id

      *)
    4. right_col_idx : int;
      (*

      join col ordinal IN RIGHT TABLE

      *)
    5. left_col_idx : int;
      (*

      join col ordinal in the LEFT row

      *)
    6. join_kind : [ `Inner | `Left ];
    7. right_col_offset : int;
      (*

      = n_left_cols

      *)
    8. n_right_cols : int;
    }
  20. | Op_hash_join of {
    1. left : op;
    2. right : op;
    3. left_key : int;
      (*

      col ordinal in the left row

      *)
    4. right_key : int;
      (*

      col ordinal in the right row

      *)
    5. join_kind : [ `Inner | `Left ];
    6. right_col_offset : int;
    7. n_right_cols : int;
    }
  21. | Op_aggregate of {
    1. child : op;
    2. group_cols : int list;
      (*

      column ordinals in the CHILD row. [] = one big group.

      *)
    3. aggs : agg_spec list;
    4. having : expr option;
      (*

      evaluated on the OUTPUT row of Op_aggregate; output row = group_col0; group_col1; ...; agg1; agg2; ....

      *)
    5. proj : proj_item list;
      (*

      projection over the aggregate output row. Maps to the final row emitted to downstream operators.

      *)
    6. windows : window_plan_item list;
      (*

      Post-aggregate window functions; for plain GROUP BY.

      *)
    }
  22. | Op_alter_table of {
    1. table_meta : Cat.table_meta;
    2. action : Ast.alter_action;
    }
  23. | Op_begin
  24. | Op_commit
  25. | Op_rollback
  26. | Op_savepoint of string
  27. | Op_release of string
  28. | Op_rollback_to of string
  29. | Op_create_fts_table of {
    1. name : string;
    2. columns : string list;
    }
  30. | Op_fts_insert of {
    1. fts_meta : Cat.fts_table_meta;
    2. col_names : string list;
    3. col_values : expr list;
    4. rowid_value : expr option;
      (*

      #330: explicit rowid from the column list

      *)
    }
  31. | Op_fts_delete of {
    1. fts_meta : Cat.fts_table_meta;
    2. where : expr option;
    }
  32. | Op_fts_seq_scan of {
    1. fts_meta : Cat.fts_table_meta;
    2. where : expr option;
    }
  33. | Op_fts_match_scan of {
    1. fts_meta : Cat.fts_table_meta;
    2. query : Fts_query.t;
    3. proj : int list;
    4. include_rank : bool;
      (*

      if true, append BM25 score as last projected column

      *)
    5. snippets : snippet_spec list;
    }
  34. | Op_pragma_rows of {
    1. rows : Granary_encoding.Row.t list;
    }
  35. | Op_pragma_get_user_version
    (*

    Read user_version from sys_meta at exec time; returns one row [V_int n].

    *)
  36. | Op_pragma_set_user_version of {
    1. version : int64;
    }
    (*

    Write user_version to sys_meta; DDL-like, returns 0 rows.

    *)
  37. | Op_pragma_integrity_check
    (*

    Scan all table + index B-trees; return ["ok"] or list of error strings.

    *)
  38. | Op_pragma_get_fk
    (*

    Read fk_enforcement flag from catalog; returns one row [V_int 0|1].

    *)
  39. | Op_pragma_set_fk of {
    1. on : bool;
    }
    (*

    Write fk_enforcement flag to catalog; DDL-like, returns 0 rows.

    *)
  40. | Op_pragma_get_recursive_triggers
    (*

    Read recursive_triggers flag from catalog; returns one row [V_int 0|1].

    *)
  41. | Op_pragma_set_recursive_triggers of {
    1. on : bool;
    }
    (*

    Write recursive_triggers flag to catalog; DDL-like, returns 0 rows.

    *)
  42. | Op_pragma_get_defer_fk
    (*

    Read defer_foreign_keys flag from catalog; returns one row [V_int 0|1].

    *)
  43. | Op_pragma_set_defer_fk of {
    1. on : bool;
    }
    (*

    Write defer_foreign_keys flag to catalog; DDL-like, returns 0 rows.

    *)
  44. | Op_pragma_wal_checkpoint
    (*

    Migrate WAL contents to main DB and reset; no-op outside WAL mode.

    *)
  45. | Op_pragma_get_wal_autocheckpoint
    (*

    Read per-connection auto-checkpoint threshold.

    *)
  46. | Op_pragma_set_wal_autocheckpoint of {
    1. n : int64;
    }
    (*

    Set per-connection auto-checkpoint threshold (0 disables).

    *)
  47. | Op_pragma_get_synchronous
    (*

    #298 read durability mode

    *)
  48. | Op_pragma_set_synchronous of {
    1. mode : string;
    }
  49. | Op_pragma_get_wal_batch_commits
  50. | Op_pragma_set_wal_batch_commits of {
    1. n : int64;
    }
  51. | Op_pragma_get_wal_batch_interval_ms
  52. | Op_pragma_set_wal_batch_interval_ms of {
    1. n : int64;
    }
  53. | Op_vacuum
    (*

    Compact-rebuild the database file (phase 37 / #120).

    *)
  54. | Op_attach of {
    1. path : string;
    2. schema : string;
    }
    (*

    ATTACH DATABASE 'path' AS schema — phase 40 / #64. Mutates the executing Db.t's attached map; never observed by exec.ml.

    *)
  55. | Op_detach of {
    1. schema : string;
    }
    (*

    DETACH DATABASE schema — phase 40 / #64.

    *)
  56. | Op_database_list
    (*

    PRAGMA database_list — phase 40 / #64. Yields one row per schema: (seq INT, name TEXT, file TEXT).

    *)
  57. | Op_active_database_get
    (*

    PRAGMA active_database — phase 40 / #64.

    *)
  58. | Op_active_database_set of {
    1. schema : string;
    }
    (*

    PRAGMA active_database = schema — phase 40 / #64. Subsequent non-routing statements route through t.attached under schema.

    *)
  59. | Op_distinct of {
    1. child : op;
    }
  60. | Op_union of {
    1. all : bool;
    2. left : op;
    3. right : op;
    }
  61. | Op_intersect of {
    1. left : op;
    2. right : op;
    }
  62. | Op_except of {
    1. left : op;
    2. right : op;
    }
  63. | Op_const_select of {
    1. exprs : (expr * string option) list;
    }
  64. | Op_window of {
    1. child : op;
    2. windows : window_plan_item list;
    3. n_input_cols : int;
    }
  65. | Op_with_cte of {
    1. cte_name : string;
    2. def : op;
    3. query : op;
    4. recursive : bool;
    }
  66. | Op_cte_scan of {
    1. cte_name : string;
    2. n_cols : int;
    }
  67. | Op_create_view of {
    1. name : string;
    2. query : Ast.stmt;
    }
  68. | Op_create_reactive_view of {
    1. name : string;
    2. query : Ast.stmt;
    3. refresh : Ast.refresh_mode;
    }
  69. | Op_drop_view of {
    1. name : string;
    }
  70. | Op_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;
    }
  71. | Op_drop_trigger of {
    1. name : string;
    }
  72. | Op_sqlite_master
    (*

    Virtual scan that reconstructs sqlite_master rows from catalog metadata.

    *)
  73. | Op_sqlite_sequence
    (*

    #312: virtual scan over AUTOINCREMENT counters (name, seq).

    *)
  74. | Op_seq_set of {
    1. table : string;
    2. seq : int64;
    }
    (*

    #312.1: writable sqlite_sequence SET/INSERT -> set table's next_rowid.

    *)
  75. | Op_seq_reset of {
    1. table : string option;
    }
    (*

    #312.1: writable sqlite_sequence DELETE -> reset table's next_rowid; None (bare DELETE, no WHERE) resets every AUTOINCREMENT counter.

    *)
  76. | Op_no_op
    (*

    No-op plan node produced by IF EXISTS DROP when object not found.

    *)
  77. | Op_changes
    (*

    Returns rows affected by last DML. Intercepted in db.ml query — not exec.ml.

    *)
  78. | Op_last_insert_rowid
    (*

    Returns rowid of last INSERT. Intercepted in db.ml query — not exec.ml.

    *)
  79. | Op_total_changes
    (*

    Returns total rows affected since the connection was opened. Intercepted in db.ml query — not exec.ml.

    *)
  80. | Op_explain of {
    1. analyze : bool;
    2. inner : op;
    }
Sourceand proj_item =
  1. | PI_group_col of int
    (*

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

    *)
  2. | PI_agg_slot of int
    (*

    project the k-th aggregate result

    *)
  3. | PI_window_slot of int
    (*

    project the j-th post-aggregate window function result

    *)
Sourceand agg_spec = {
  1. func : Ast.agg_func;
  2. col_ord : int option;
    (*

    None means COUNT-star

    *)
}