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/src/granary.sql/plan.ml.html

Source file plan.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
(** Physical query plan operators for Phase 0.
    Phase 0 operators: CreateTable, Insert, SeqScan, Filter, Project.
    Extended in later phases with IndexLookup, HashJoin, Sort, etc. *)

module Cat = Granary_catalog.Catalog

type binop =
  | Eq
  | Ne
  | Lt
  | Le
  | Gt
  | Ge
  | Add
  | Sub
  | Mul
  | Div
  | And
  | Or
  | Concat
  | Mod
  | Bit_and
  | Bit_or
  | Lshift
  | Rshift
  | Like
  | Glob

type expr =
  | P_lit of Ast.literal
  | P_col of int (** column ordinal *)
  | P_binop of binop * expr * expr
  | P_not of expr
  | P_is_null of expr
  | P_is_not_null of expr
  | P_neg of expr
  | P_bitnot of expr
  | P_between of expr * expr * expr
  | P_in of expr * expr list
  | P_func of Ast.scalar_func * expr list
  | P_param of int (** 0-indexed positional parameter *)
  | P_subquery of Ast.stmt
  | P_exists of Ast.stmt
  | P_in_select of expr * Ast.stmt
  | P_case of
      { scrutinee : expr option
      ; branches : (expr * expr) list
      ; else_ : expr option
      }
  | P_cast of expr * Ast.ty
  | P_excluded_col of int (** Column reference into the proposed INSERT excluded row. *)
  | P_window_slot of int
  (** Window function slot reference — substituted to P_col(n_input_cols+i) by planner. *)
  | P_collate of expr * Ast.collation

type window_plan_item =
  { func : Ast.window_func
  ; args : expr list
  ; partition_by : expr list
  ; order_by : (expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list
  ; frame : Ast.frame_spec option
  }

type snippet_spec =
  { col_idx : int
  ; start_tag : string
  ; end_tag : string
  ; ellipsis : string
  ; n_tokens : int
  }

type op =
  | Op_create_table of
      { name : string
      ; columns : Granary_encoding.Row.column list
      ; uniq_idxs : (string * string list * Cat.idx_origin) list
      ; if_not_exists : bool
      ; 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)] *)
      ; without_rowid : bool
      ; autoincrement : bool (** #299: INTEGER PRIMARY KEY AUTOINCREMENT. *)
      }
  | Op_col_create_table of
      { name : string
      ; columns : Granary_encoding.Row.column list
      ; if_not_exists : bool
      }
  | Op_insert of
      { table_meta : Cat.table_meta
      ; ordinals : int list
      ; values : expr list list (* one sublist per VALUES row *)
      ; on_conflict : Ast.conflict_action option
      ; returning : expr list
      ; upsert_update : (string list * (int * expr) list) option
      }
  | Op_insert_select of
      { table_meta : Cat.table_meta
      ; ordinals : int list
      ; source : op
      ; on_conflict : Ast.conflict_action option
      }
  | Op_seq_scan of { table_meta : Cat.table_meta }
  | Op_filter of
      { pred : expr
      ; child : op
      }
  | Op_project of
      { ordinals : int list
      ; child : op
      }
  | Op_expr_project of
      { exprs : (expr * string option) list
      ; child : op
      }
  | Op_sort of
      { keys : (expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list
      ; child : op
      }
  | Op_limit of
      { limit : int
      ; offset : int
      ; child : op
      }
  | Op_create_index of
      { name : string
      ; table : string
      ; tree_id : int (** table's tree_id *)
      ; col_sqls : string list (** col name (plain) or expr SQL (expression) *)
      ; col_expr_flags : bool list
        (** true = expression index column, false = plain column *)
      ; where_expr : expr option
      ; where_sql : string option
      ; unique : bool
      ; columns : Granary_encoding.Row.column list
        (** columns of the target table — needed for row decoding
            during index population *)
      ; if_not_exists : bool
      }
  | Op_index_lookup of
      { table_tree : int (** table's tree_id *)
      ; idx_tree : int (** index tree_id *)
      ; col_idx : int (** column ordinal for encoding *)
      ; col_type : Granary_encoding.Row.ty
      ; lookup_val : expr (** value to look up *)
      ; table_meta : Cat.table_meta (** for row decoding *)
      }
  | Op_rowid_lookup of
      { table_meta : Cat.table_meta
      ; lookup_val : expr
      }
  | Op_col_seq_scan of { table_meta : Cat.table_meta }
  | Op_update of
      { table_meta : Cat.table_meta
      ; assignments : (int * expr) list (** [(col_ordinal, new_value_expr)] *)
      ; where : expr option
      ; order : (expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list
      ; limit : int option
      ; offset : int option
      ; indexes : Cat.index_info list
      ; returning : expr list
      }
  | Op_delete of
      { table_meta : Cat.table_meta
      ; where : expr option
      ; order : (expr * [ `Asc | `Desc ] * [ `Nulls_first | `Nulls_last ]) list
      ; limit : int option
      ; offset : int option
      ; indexes : Cat.index_info list
      ; returning : expr list
      }
  | Op_drop_table of
      { table_meta : Cat.table_meta
      ; indexes : Cat.index_info list
      }
  | Op_drop_index of { idx_info : Cat.index_info }
  | Op_nested_loop_join of
      { left : op (** left input (any op stream) *)
      ; right_meta : Cat.table_meta (** right table for row decode *)
      ; idx_tree : int (** right-side index tree id *)
      ; right_col_idx : int (** join col ordinal IN RIGHT TABLE *)
      ; left_col_idx : int (** join col ordinal in the LEFT row *)
      ; join_kind : [ `Inner | `Left ]
      ; right_col_offset : int (** = n_left_cols *)
      ; n_right_cols : int
      }
  | Op_hash_join of
      { left : op
      ; right : op
      ; left_key : int (** col ordinal in the left row *)
      ; right_key : int (** col ordinal in the right row *)
      ; join_kind : [ `Inner | `Left ]
      ; right_col_offset : int
      ; n_right_cols : int
      }
  | Op_aggregate of
      { child : op
      ; group_cols : int list
        (** column ordinals in the CHILD row.  [[]] = one big group. *)
      ; aggs : agg_spec list
      ; having : expr option
        (** evaluated on the OUTPUT row of [Op_aggregate];
            output row = [group_col0; group_col1; ...; agg1; agg2; ...]. *)
      ; proj : proj_item list
        (** projection over the aggregate output row.  Maps to the final
            row emitted to downstream operators. *)
      ; windows : window_plan_item list
        (** Post-aggregate window functions; [] for plain GROUP BY. *)
      }
  | Op_alter_table of
      { table_meta : Cat.table_meta
      ; action : Ast.alter_action
      }
  | Op_begin
  | Op_commit
  | Op_rollback
  | Op_savepoint of string
  | Op_release of string
  | Op_rollback_to of string
  | Op_create_fts_table of
      { name : string
      ; columns : string list
      }
  | Op_fts_insert of
      { fts_meta : Cat.fts_table_meta
      ; col_names : string list
      ; col_values : expr list
      ; rowid_value : expr option (** #330: explicit [rowid] from the column list *)
      }
  | Op_fts_delete of
      { fts_meta : Cat.fts_table_meta
      ; where : expr option
      }
  | Op_fts_seq_scan of
      { fts_meta : Cat.fts_table_meta
      ; where : expr option
      }
  | Op_fts_match_scan of
      { fts_meta : Cat.fts_table_meta
      ; query : Fts_query.t
      ; proj : int list
      ; include_rank : bool (** if true, append BM25 score as last projected column *)
      ; snippets : snippet_spec list
      }
  | Op_pragma_rows of { rows : Granary_encoding.Row.t list }
  | Op_pragma_get_user_version
  (** Read user_version from sys_meta at exec time; returns one row [[V_int n]]. *)
  | Op_pragma_set_user_version of { version : int64 }
  (** Write user_version to sys_meta; DDL-like, returns 0 rows. *)
  | Op_pragma_integrity_check
  (** Scan all table + index B-trees; return [["ok"]] or list of error strings. *)
  | Op_pragma_get_fk
  (** Read fk_enforcement flag from catalog; returns one row [[V_int 0|1]]. *)
  | Op_pragma_set_fk of { on : bool }
  (** Write fk_enforcement flag to catalog; DDL-like, returns 0 rows. *)
  | Op_pragma_get_recursive_triggers
  (** Read recursive_triggers flag from catalog; returns one row [[V_int 0|1]]. *)
  | Op_pragma_set_recursive_triggers of { on : bool }
  (** Write recursive_triggers flag to catalog; DDL-like, returns 0 rows. *)
  | Op_pragma_get_defer_fk
  (** Read defer_foreign_keys flag from catalog; returns one row [[V_int 0|1]]. *)
  | Op_pragma_set_defer_fk of { on : bool }
  (** Write defer_foreign_keys flag to catalog; DDL-like, returns 0 rows. *)
  | Op_pragma_wal_checkpoint
  (** Migrate WAL contents to main DB and reset; no-op outside WAL mode. *)
  | Op_pragma_get_wal_autocheckpoint (** Read per-connection auto-checkpoint threshold. *)
  | Op_pragma_set_wal_autocheckpoint of { n : int64 }
  (** Set per-connection auto-checkpoint threshold (0 disables). *)
  | Op_pragma_get_synchronous (** #298 read durability mode *)
  | Op_pragma_set_synchronous of { mode : string }
  | Op_pragma_get_wal_batch_commits
  | Op_pragma_set_wal_batch_commits of { n : int64 }
  | Op_pragma_get_wal_batch_interval_ms
  | Op_pragma_set_wal_batch_interval_ms of { n : int64 }
  | Op_vacuum (** Compact-rebuild the database file (phase 37 / #120). *)
  | Op_attach of
      { path : string
      ; schema : string
      }
  (** [ATTACH DATABASE 'path' AS schema] — phase 40 / #64.  Mutates
        the executing [Db.t]'s [attached] map; never observed by exec.ml. *)
  | Op_detach of { schema : string } (** [DETACH DATABASE schema] — phase 40 / #64. *)
  | Op_database_list
  (** [PRAGMA database_list] — phase 40 / #64.  Yields one row per
        schema: (seq INT, name TEXT, file TEXT). *)
  | Op_active_database_get (** [PRAGMA active_database] — phase 40 / #64. *)
  | Op_active_database_set of { schema : string }
  (** [PRAGMA active_database = schema] — phase 40 / #64.  Subsequent
        non-routing statements route through [t.attached] under [schema]. *)
  | Op_distinct of { child : op }
  | Op_union of
      { all : bool
      ; left : op
      ; right : op
      }
  | Op_intersect of
      { left : op
      ; right : op
      }
  | Op_except of
      { left : op
      ; right : op
      }
  | Op_const_select of { exprs : (expr * string option) list }
  | Op_window of
      { child : op
      ; windows : window_plan_item list
      ; n_input_cols : int
      }
  | Op_with_cte of
      { cte_name : string
      ; def : op
      ; query : op
      ; recursive : bool
      }
  | Op_cte_scan of
      { cte_name : string
      ; n_cols : int
      }
  | Op_create_view of
      { name : string
      ; query : Ast.stmt
      }
  | Op_create_reactive_view of
      { name : string
      ; query : Ast.stmt
      ; refresh : Ast.refresh_mode
      }
  | Op_drop_view of { name : string }
  | Op_create_trigger of
      { name : string
      ; timing : Ast.trigger_timing
      ; event : Ast.trigger_event
      ; table : string
      ; when_ : Ast.expr option
      ; body : Ast.stmt list
      }
  | Op_drop_trigger of { name : string }
  | Op_sqlite_master
  (** Virtual scan that reconstructs sqlite_master rows from catalog metadata. *)
  | Op_sqlite_sequence (** #312: virtual scan over AUTOINCREMENT counters (name, seq). *)
  | Op_seq_set of
      { table : string
      ; seq : int64
      } (** #312.1: writable sqlite_sequence SET/INSERT -> set [table]'s next_rowid. *)
  | Op_seq_reset of { table : string option }
  (** #312.1: writable sqlite_sequence DELETE -> reset [table]'s next_rowid;
      [None] (bare DELETE, no WHERE) resets every AUTOINCREMENT counter. *)
  | Op_no_op (** No-op plan node produced by IF EXISTS DROP when object not found. *)
  | Op_changes
  (** Returns rows affected by last DML. Intercepted in db.ml query — not exec.ml. *)
  | Op_last_insert_rowid
  (** Returns rowid of last INSERT. Intercepted in db.ml query — not exec.ml. *)
  | Op_total_changes
  (** Returns total rows affected since the connection was opened.
        Intercepted in db.ml query — not exec.ml. *)
  | Op_explain of
      { analyze : bool
      ; inner : op
      }

and proj_item =
  | PI_group_col of int (** project the i-th GROUP BY column (index into group_cols) *)
  | PI_agg_slot of int (** project the k-th aggregate result *)
  | PI_window_slot of int (** project the j-th post-aggregate window function result *)

and agg_spec =
  { func : Ast.agg_func
  ; col_ord : int option (** [None] means COUNT-star *)
  }

[@@@ai_disclosure "ai-generated"]
[@@@ai_model "claude-opus-4-7"]
[@@@ai_provider "Anthropic"]