reddb_rql/core.rs
1use std::fmt;
2
3use super::builders::{GraphQueryBuilder, PathQueryBuilder, TableQueryBuilder};
4use reddb_types::catalog::CollectionModel;
5pub use reddb_types::distance::DistanceMetric;
6pub use reddb_types::queue_mode::QueueMode;
7use reddb_types::types::{SqlTypeName, Value};
8pub use reddb_types::vector_metadata::MetadataFilter;
9
10/// Root query expression
11#[derive(Debug, Clone)]
12#[allow(clippy::large_enum_variant)]
13pub enum QueryExpr {
14 /// Pure table query: SELECT ... FROM ...
15 Table(TableQuery),
16 /// Pure graph query: MATCH ... RETURN ...
17 Graph(GraphQuery),
18 /// Join between table and graph
19 Join(JoinQuery),
20 /// Path query: PATH FROM ... TO ...
21 Path(PathQuery),
22 /// Vector similarity search
23 Vector(VectorQuery),
24 /// Hybrid query combining structured and vector search
25 Hybrid(HybridQuery),
26 /// INSERT INTO table (cols) VALUES (vals)
27 Insert(InsertQuery),
28 /// UPDATE table SET col=val WHERE filter
29 Update(UpdateQuery),
30 /// DELETE FROM table WHERE filter
31 Delete(DeleteQuery),
32 /// CREATE TABLE name (columns)
33 CreateTable(CreateTableQuery),
34 /// CREATE COLLECTION name KIND kind
35 CreateCollection(CreateCollectionQuery),
36 /// CREATE VECTOR name DIM n [METRIC metric]
37 CreateVector(CreateVectorQuery),
38 /// DROP TABLE name
39 DropTable(DropTableQuery),
40 /// DROP GRAPH name
41 DropGraph(DropGraphQuery),
42 /// DROP VECTOR name
43 DropVector(DropVectorQuery),
44 /// DROP DOCUMENT name
45 DropDocument(DropDocumentQuery),
46 /// DROP KV name
47 DropKv(DropKvQuery),
48 /// DROP COLLECTION name
49 DropCollection(DropCollectionQuery),
50 /// TRUNCATE [model] name
51 Truncate(TruncateQuery),
52 /// ALTER TABLE name ADD/DROP/RENAME COLUMN
53 AlterTable(AlterTableQuery),
54 /// GRAPH subcommand (NEIGHBORHOOD, SHORTEST_PATH, etc.)
55 GraphCommand(GraphCommand),
56 /// SEARCH subcommand (SIMILAR, TEXT, HYBRID)
57 SearchCommand(SearchCommand),
58 /// ASK 'question' — RAG query with LLM synthesis
59 Ask(AskQuery),
60 /// CREATE INDEX name ON table (columns) USING type
61 CreateIndex(CreateIndexQuery),
62 /// DROP INDEX name ON table
63 DropIndex(DropIndexQuery),
64 /// Probabilistic data structure commands (HLL, SKETCH, FILTER)
65 ProbabilisticCommand(ProbabilisticCommand),
66 /// CREATE TIMESERIES name [RETENTION duration] [CHUNK_SIZE n]
67 CreateTimeSeries(CreateTimeSeriesQuery),
68 /// CREATE METRIC path TYPE kind ROLE role
69 CreateMetric(CreateMetricQuery),
70 /// ALTER METRIC path SET <ROLE|KIND|TYPE|PATH> <value>
71 AlterMetric(AlterMetricQuery),
72 /// CREATE SLO path ON metric_path TARGET t WINDOW d UNIT
73 CreateSlo(CreateSloQuery),
74 /// DROP TIMESERIES name
75 DropTimeSeries(DropTimeSeriesQuery),
76 /// CREATE QUEUE name [WORK|STANDARD|FIFO|FANOUT] [MAX_SIZE n] [PRIORITY] [WITH TTL duration]
77 CreateQueue(CreateQueueQuery),
78 /// ALTER QUEUE name SET MODE [FANOUT|WORK|STANDARD|FIFO]
79 AlterQueue(AlterQueueQuery),
80 /// DROP QUEUE name
81 DropQueue(DropQueueQuery),
82 /// Read-only queue projection: SELECT ... FROM QUEUE name
83 QueueSelect(QueueSelectQuery),
84 /// QUEUE subcommand (PUSH, POP, PEEK, LEN, PURGE, GROUP, READ, ACK, NACK)
85 QueueCommand(QueueCommand),
86 /// KV subcommand (PUT, GET, DELETE)
87 KvCommand(KvCommand),
88 /// CONFIG keyed command (PUT, GET, ROTATE, DELETE, HISTORY)
89 ConfigCommand(ConfigCommand),
90 /// CREATE TREE name IN collection ROOT ... MAX_CHILDREN n
91 CreateTree(CreateTreeQuery),
92 /// DROP TREE name IN collection
93 DropTree(DropTreeQuery),
94 /// TREE subcommand (INSERT, MOVE, DELETE, VALIDATE, REBALANCE)
95 TreeCommand(TreeCommand),
96 /// SET CONFIG key = value
97 SetConfig { key: String, value: Value },
98 /// SHOW CONFIG [prefix] [AS JSON|FORMAT JSON]
99 ShowConfig {
100 prefix: Option<String>,
101 as_json: bool,
102 },
103 /// SET SECRET key = value
104 SetSecret { key: String, value: Value },
105 /// DELETE SECRET key
106 DeleteSecret { key: String },
107 /// SHOW SECRET[S] [prefix]
108 ShowSecrets { prefix: Option<String> },
109 /// `SET TENANT 'id'` / `SET TENANT = 'id'` / `RESET TENANT`
110 ///
111 /// Session-scoped multi-tenancy handle. Populates a per-connection
112 /// thread-local that `CURRENT_TENANT()` reads and that RLS
113 /// policies combine with via `USING (tenant_id = CURRENT_TENANT())`.
114 /// `None` clears the current tenant (RESET TENANT or SET TENANT
115 /// NULL). Unlike `SetConfig` this is *not* persisted to red_config —
116 /// it lives for the connection's lifetime only.
117 SetTenant(Option<String>),
118 /// `SHOW TENANT` — returns the thread-local tenant id (or NULL).
119 ShowTenant,
120 /// EXPLAIN ALTER FOR CREATE TABLE name (...) [FORMAT JSON]
121 ///
122 /// Pure read command that diffs the embedded `CREATE TABLE`
123 /// statement against the live `CollectionContract` of the
124 /// table with the same name and returns the `ALTER TABLE`
125 /// operations that would close the gap. Never executes
126 /// anything — output is text (default) or JSON depending on
127 /// the optional `FORMAT JSON` suffix. Powers the Purple
128 /// framework's migration generator and any other client that
129 /// wants reddb to own the schema-diff rules.
130 ExplainAlter(ExplainAlterQuery),
131 /// CREATE MIGRATION name [DEPENDS ON dep1, dep2] [BATCH n ROWS] [NO ROLLBACK] body
132 CreateMigration(CreateMigrationQuery),
133 /// APPLY MIGRATION name | APPLY MIGRATION * [FOR TENANT id]
134 ApplyMigration(ApplyMigrationQuery),
135 /// ROLLBACK MIGRATION name
136 RollbackMigration(RollbackMigrationQuery),
137 /// EXPLAIN MIGRATION name
138 ExplainMigration(ExplainMigrationQuery),
139 /// `EVENTS BACKFILL collection [WHERE pred] TO queue [LIMIT n]`.
140 EventsBackfill(EventsBackfillQuery),
141 /// `EVENTS BACKFILL STATUS collection` placeholder for the status slice.
142 EventsBackfillStatus { collection: String },
143 /// Transaction control: BEGIN, COMMIT, ROLLBACK, SAVEPOINT, RELEASE, ROLLBACK TO.
144 ///
145 /// Phase 1.1 (PG parity): parser + dispatch are wired so clients (psql, JDBC, etc.)
146 /// can issue these statements without errors. Real isolation/atomicity semantics
147 /// arrive with Phase 2.3 MVCC. Until then statements behave as autocommit (each
148 /// DML is its own transaction); BEGIN/COMMIT/ROLLBACK return success but do NOT
149 /// provide rollback-on-failure guarantees across multiple statements.
150 TransactionControl(TxnControl),
151 /// Maintenance commands: VACUUM [FULL] [table], ANALYZE [table].
152 ///
153 /// Phase 1.2 (PG parity): `VACUUM` triggers segment/page flush + planner stats
154 /// refresh. `ANALYZE` refreshes planner statistics (histograms, null counts,
155 /// distinct estimates). Both accept an optional table target; omitting the
156 /// target iterates every collection.
157 MaintenanceCommand(MaintenanceCommand),
158 /// `CREATE SCHEMA [IF NOT EXISTS] name`
159 ///
160 /// Phase 1.3 (PG parity): schemas are logical namespaces stored in
161 /// `red_config` under the key `schema.{name}`. Tables created inside a
162 /// schema use `schema.table` qualified names (collection name = "schema.table").
163 CreateSchema(CreateSchemaQuery),
164 /// `DROP SCHEMA [IF EXISTS] name [CASCADE]`
165 DropSchema(DropSchemaQuery),
166 /// `CREATE SEQUENCE [IF NOT EXISTS] name [START [WITH] n] [INCREMENT [BY] n]`
167 ///
168 /// Phase 1.3 (PG parity): sequences are 64-bit monotonic counters persisted
169 /// in `red_config` under the key `sequence.{name}`. Values are produced by
170 /// the scalar functions `nextval('name')` and `currval('name')`.
171 CreateSequence(CreateSequenceQuery),
172 /// `DROP SEQUENCE [IF EXISTS] name`
173 DropSequence(DropSequenceQuery),
174 /// `COPY table FROM 'path' [WITH ...]` — CSV import (Phase 1.5 PG parity).
175 ///
176 /// Supported options: `DELIMITER c`, `HEADER [true|false]`. Rows stream
177 /// into the named collection via the `CsvImporter`.
178 CopyFrom(CopyFromQuery),
179 /// `CREATE [OR REPLACE] [MATERIALIZED] VIEW [IF NOT EXISTS] name AS SELECT ...`
180 ///
181 /// Phase 2.1 (PG parity): views are stored as `view.{name}` entries in
182 /// `red_config`. Materialized views additionally allocate a slot in the
183 /// shared `MaterializedViewCache`; `REFRESH MATERIALIZED VIEW` re-runs
184 /// the underlying query and repopulates the cache.
185 CreateView(CreateViewQuery),
186 /// `DROP [MATERIALIZED] VIEW [IF EXISTS] name`
187 DropView(DropViewQuery),
188 /// `REFRESH MATERIALIZED VIEW name`
189 ///
190 /// Re-executes the view's query and writes the result into the cache.
191 RefreshMaterializedView(RefreshMaterializedViewQuery),
192 /// `CREATE POLICY name ON table [FOR action] [TO role] USING (filter)`
193 ///
194 /// Phase 2.5 (PG parity): row-level security policy definition.
195 /// Evaluated at read time — when the table has RLS enabled, all
196 /// matching policies for the current role are combined with OR and
197 /// AND-ed into the query's WHERE clause.
198 CreatePolicy(CreatePolicyQuery),
199 /// `DROP POLICY [IF EXISTS] name ON table`
200 DropPolicy(DropPolicyQuery),
201 /// `CREATE SERVER name FOREIGN DATA WRAPPER kind OPTIONS (...)`
202 /// (Phase 3.2 PG parity). Registers a named foreign-data-wrapper
203 /// instance in the runtime's `ForeignTableRegistry`.
204 CreateServer(CreateServerQuery),
205 /// `DROP SERVER [IF EXISTS] name [CASCADE]`
206 DropServer(DropServerQuery),
207 /// `CREATE FOREIGN TABLE name (cols) SERVER srv OPTIONS (...)`
208 /// (Phase 3.2 PG parity). Makes `name` resolvable as a foreign table
209 /// via the parent server's `ForeignDataWrapper`.
210 CreateForeignTable(CreateForeignTableQuery),
211 /// `DROP FOREIGN TABLE [IF EXISTS] name`
212 DropForeignTable(DropForeignTableQuery),
213 /// `GRANT { actions | ALL [PRIVILEGES] }
214 /// ON { TABLE | SCHEMA | DATABASE | FUNCTION } object_list
215 /// TO grant_principal_list
216 /// [WITH GRANT OPTION]`
217 ///
218 /// Granular RBAC primitive layered on top of the legacy 3-role model.
219 /// See `crate::auth::privileges` for the resolution algorithm.
220 Grant(GrantStmt),
221 /// `REVOKE [GRANT OPTION FOR] { actions | ALL } ON … FROM …`
222 Revoke(RevokeStmt),
223 /// `ALTER USER name [VALID UNTIL 'ts'] [CONNECTION LIMIT n]
224 /// [ENABLE | DISABLE] [SET search_path = ...]`
225 AlterUser(AlterUserStmt),
226 /// `CREATE USER [tenant.]name [WITH] PASSWORD 'plaintext' [ROLE read|write|admin]`
227 CreateUser(CreateUserStmt),
228 // ----- IAM policy DDL (Agent #28 / IAM kernel integration) -----
229 /// `CREATE POLICY '<id>' AS '<json>'` — installs an IAM policy
230 /// document in the AuthStore. Distinct from the RLS-flavoured
231 /// `CreatePolicy(CreatePolicyQuery)` above (which uses
232 /// `CREATE POLICY name ON table ...`); the parser disambiguates
233 /// at parse time by inspecting the token after the policy name.
234 CreateIamPolicy { id: String, json: String },
235 /// `DROP POLICY '<id>'` — removes an IAM policy and its
236 /// attachments.
237 DropIamPolicy { id: String },
238 /// `ATTACH POLICY '<id>' TO USER <name>` /
239 /// `ATTACH POLICY '<id>' TO GROUP <name>`.
240 AttachPolicy {
241 policy_id: String,
242 principal: PolicyPrincipalRef,
243 },
244 /// `DETACH POLICY '<id>' FROM USER <name>` /
245 /// `DETACH POLICY '<id>' FROM GROUP <name>`.
246 DetachPolicy {
247 policy_id: String,
248 principal: PolicyPrincipalRef,
249 },
250 /// `SHOW POLICIES [FOR USER <name> | FOR GROUP <name>]`.
251 ShowPolicies { filter: Option<PolicyPrincipalRef> },
252 /// `SHOW EFFECTIVE PERMISSIONS FOR <name> [ON <kind>:<name>]`.
253 ShowEffectivePermissions {
254 user: PolicyUserRef,
255 resource: Option<PolicyResourceRef>,
256 },
257 /// Exact rank of one row in a declared leaderboard ranking.
258 RankOf(RankOfQuery),
259 /// Approximate tail rank of one row in a declared leaderboard ranking.
260 ApproxRankOf(RankOfQuery),
261 /// Exact rank-ordered range in a declared leaderboard ranking.
262 RankRange(RankRangeQuery),
263 /// `SIMULATE <name> ACTION <verb> ON <kind>:<name>`.
264 SimulatePolicy {
265 user: PolicyUserRef,
266 action: String,
267 resource: PolicyResourceRef,
268 },
269 /// `LINT POLICY '<id>'` — fetch a stored policy from the
270 /// AuthStore and run the [`PolicyLinter`](crate::auth::policy_linter)
271 /// against its serialized JSON.
272 ///
273 /// `LINT POLICY JSON '<json>'` — lint the supplied JSON document
274 /// directly without consulting the AuthStore. Issue #710.
275 LintPolicy { source: LintPolicySource },
276 /// `MIGRATE POLICY MODE TO '<target>' [DRY RUN]` — switch the
277 /// install from the legacy_rbac fallback to strict policy_only
278 /// after running the pre-flight delta simulator. With `DRY RUN`,
279 /// only the delta is returned. Without it, the migration refuses
280 /// if the delta is non-empty and otherwise mutates the
281 /// enforcement mode. Issue #714.
282 MigratePolicyMode { target: String, dry_run: bool },
283}
284
285/// Source of the policy document being linted.
286#[derive(Debug, Clone, PartialEq, Eq)]
287pub enum LintPolicySource {
288 /// Fetch the document from the AuthStore by id.
289 Id(String),
290 /// Use the supplied JSON literal verbatim.
291 Json(String),
292}
293
294/// Tenant-qualified user reference for IAM policy SQL DDL.
295#[derive(Debug, Clone, PartialEq, Eq)]
296pub struct PolicyUserRef {
297 pub tenant: Option<String>,
298 pub username: String,
299}
300
301/// Resource reference (`<kind>:<name>`) used in `SIMULATE` /
302/// `SHOW EFFECTIVE PERMISSIONS`.
303#[derive(Debug, Clone, PartialEq, Eq)]
304pub struct PolicyResourceRef {
305 pub kind: String,
306 pub name: String,
307}
308
309/// Principal target for ATTACH / DETACH / SHOW POLICIES filter.
310#[derive(Debug, Clone, PartialEq, Eq)]
311pub enum PolicyPrincipalRef {
312 User(PolicyUserRef),
313 Group(String),
314}
315
316/// `RANK OF <entity_id> IN <ranking>` canonical leaderboard read.
317///
318/// Redis-flavor `ZRANK <ranking> <entity_id>` desugars to this exact same
319/// shape, so it carries no execution semantics that the canonical rank read
320/// lacks.
321#[derive(Debug, Clone, PartialEq, Eq)]
322pub struct RankOfQuery {
323 pub ranking: String,
324 pub entity_id: u64,
325}
326
327/// `RANK RANGE <lo> TO <hi> IN <ranking>` canonical leaderboard read.
328///
329/// Redis-flavor `ZRANGE <ranking> <start> <stop> [WITHSCORES]` desugars by
330/// translating Redis' zero-based inclusive offsets to the canonical one-based
331/// rank bounds.
332#[derive(Debug, Clone, PartialEq, Eq)]
333pub struct RankRangeQuery {
334 pub ranking: String,
335 pub lo: u64,
336 pub hi: u64,
337}
338
339// ---------------------------------------------------------------------------
340// GRANT / REVOKE / ALTER USER AST
341// ---------------------------------------------------------------------------
342
343/// Object class targeted by a GRANT/REVOKE.
344#[derive(Debug, Clone, PartialEq, Eq)]
345pub enum GrantObjectKind {
346 Table,
347 Schema,
348 Database,
349 Function,
350}
351
352/// One target object in a `GRANT ... ON ... <object_list>` clause.
353///
354/// `name` follows the parser's standard `[schema.]object` shape; the
355/// optional `schema` is only populated for `Table` / `Function` and
356/// stays `None` when the user wrote a bare identifier.
357#[derive(Debug, Clone)]
358pub struct GrantObject {
359 pub schema: Option<String>,
360 pub name: String,
361}
362
363/// Principal target of a GRANT (i.e. the recipient).
364#[derive(Debug, Clone)]
365pub enum GrantPrincipalRef {
366 /// `TO username` — username may include an `@tenant` suffix.
367 User {
368 tenant: Option<String>,
369 name: String,
370 },
371 /// `TO PUBLIC`.
372 Public,
373 /// `TO GROUP groupname` (parsed today, enforcement deferred).
374 Group(String),
375}
376
377/// `GRANT` statement AST.
378#[derive(Debug, Clone)]
379pub struct GrantStmt {
380 /// Privilege keywords as the user typed them, normalised to upper
381 /// case. Matches the `Action` set in `crate::auth::privileges`. An
382 /// empty list together with `all = true` represents `ALL [PRIVILEGES]`.
383 pub actions: Vec<String>,
384 /// Optional column list — populates the AST for column-level
385 /// grants but enforcement is deferred (stretch goal).
386 pub columns: Option<Vec<String>>,
387 pub object_kind: GrantObjectKind,
388 pub objects: Vec<GrantObject>,
389 pub principals: Vec<GrantPrincipalRef>,
390 pub with_grant_option: bool,
391 /// `true` when the privilege list was `ALL [PRIVILEGES]`.
392 pub all: bool,
393}
394
395/// `REVOKE` statement AST.
396#[derive(Debug, Clone)]
397pub struct RevokeStmt {
398 pub actions: Vec<String>,
399 pub columns: Option<Vec<String>>,
400 pub object_kind: GrantObjectKind,
401 pub objects: Vec<GrantObject>,
402 pub principals: Vec<GrantPrincipalRef>,
403 /// `REVOKE GRANT OPTION FOR ...` — strips just the grant option,
404 /// keeping the underlying privilege.
405 pub grant_option_for: bool,
406 pub all: bool,
407}
408
409/// One attribute setting under `ALTER USER`.
410#[derive(Debug, Clone)]
411pub enum AlterUserAttribute {
412 ValidUntil(String),
413 ConnectionLimit(i64),
414 Enable,
415 Disable,
416 SetSearchPath(String),
417 AddGroup(String),
418 DropGroup(String),
419 /// Reset password (carry the new plaintext until the runtime
420 /// hands it to AuthStore::change_password). Out of scope for the
421 /// initial milestone — present so the parser can accept the
422 /// keyword without a follow-up grammar change.
423 Password(String),
424}
425
426/// `ALTER USER` statement AST.
427#[derive(Debug, Clone)]
428pub struct AlterUserStmt {
429 pub tenant: Option<String>,
430 pub username: String,
431 pub attributes: Vec<AlterUserAttribute>,
432}
433
434/// `CREATE USER` statement AST.
435#[derive(Debug, Clone)]
436pub struct CreateUserStmt {
437 pub tenant: Option<String>,
438 pub username: String,
439 pub password: String,
440 pub role: String,
441}
442
443#[derive(Debug, Clone)]
444pub struct CreateServerQuery {
445 pub name: String,
446 /// Wrapper kind declared in `FOREIGN DATA WRAPPER <kind>`.
447 pub wrapper: String,
448 /// Generic `(key 'value', ...)` option bag.
449 pub options: Vec<(String, String)>,
450 pub if_not_exists: bool,
451}
452
453#[derive(Debug, Clone)]
454pub struct DropServerQuery {
455 pub name: String,
456 pub if_exists: bool,
457 pub cascade: bool,
458}
459
460#[derive(Debug, Clone)]
461pub struct CreateForeignTableQuery {
462 pub name: String,
463 pub server: String,
464 pub columns: Vec<ForeignColumnDef>,
465 pub options: Vec<(String, String)>,
466 pub if_not_exists: bool,
467}
468
469#[derive(Debug, Clone)]
470pub struct ForeignColumnDef {
471 pub name: String,
472 pub data_type: String,
473 pub not_null: bool,
474}
475
476#[derive(Debug, Clone)]
477pub struct DropForeignTableQuery {
478 pub name: String,
479 pub if_exists: bool,
480}
481
482/// Row-level security policy definition.
483#[derive(Debug, Clone)]
484pub struct CreatePolicyQuery {
485 pub name: String,
486 pub table: String,
487 /// Which action this policy gates. `None` = `ALL` (applies to all four).
488 pub action: Option<PolicyAction>,
489 /// Role the policy applies to. `None` = all roles.
490 pub role: Option<String>,
491 /// Boolean predicate the row must satisfy.
492 pub using: Box<Filter>,
493 /// Entity kind this policy targets (Phase 2.5.5 RLS universal).
494 /// `CREATE POLICY p ON t ...` defaults to `Table`; writing
495 /// `ON NODES OF g` / `ON VECTORS OF v` / `ON MESSAGES OF q` /
496 /// `ON POINTS OF ts` / `ON EDGES OF g` targets the matching
497 /// non-tabular kind. The evaluator filters polices by kind so
498 /// a graph policy only gates graph reads, vector policy only
499 /// gates vector reads, etc.
500 pub target_kind: PolicyTargetKind,
501}
502
503/// Which flavour of entity a policy governs (Phase 2.5.5).
504#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
505pub enum PolicyTargetKind {
506 Table,
507 Nodes,
508 Edges,
509 Vectors,
510 Messages,
511 Points,
512 Documents,
513}
514
515impl PolicyTargetKind {
516 /// Lowercase identifier for UX — used in messages and the
517 /// `red_config.rls.policies.*` persistence key.
518 pub fn as_ident(&self) -> &'static str {
519 match self {
520 Self::Table => "table",
521 Self::Nodes => "nodes",
522 Self::Edges => "edges",
523 Self::Vectors => "vectors",
524 Self::Messages => "messages",
525 Self::Points => "points",
526 Self::Documents => "documents",
527 }
528 }
529}
530
531#[derive(Debug, Clone, Copy, PartialEq, Eq)]
532pub enum PolicyAction {
533 Select,
534 Insert,
535 Update,
536 Delete,
537}
538
539#[derive(Debug, Clone, PartialEq, Eq)]
540pub struct DropPolicyQuery {
541 pub name: String,
542 pub table: String,
543 pub if_exists: bool,
544}
545
546#[derive(Debug, Clone)]
547pub struct CreateViewQuery {
548 pub name: String,
549 /// Parsed `SELECT ...` body. Stored as a boxed `QueryExpr` so the
550 /// runtime can substitute the tree directly when a query references
551 /// this view (no re-parsing per read).
552 pub query: Box<QueryExpr>,
553 pub materialized: bool,
554 pub if_not_exists: bool,
555 /// `CREATE OR REPLACE VIEW` — overwrites any existing definition.
556 pub or_replace: bool,
557 /// `REFRESH EVERY <duration>` clause — only valid on materialized
558 /// views. When set, a background scheduler ticks the view at this
559 /// cadence. `None` means refresh-on-demand only (slice 9 behaviour).
560 /// Issue #583 slice 10.
561 pub refresh_every_ms: Option<u64>,
562 /// `WITH RETENTION <duration>` clause — only valid on materialized
563 /// views (issue #584 slice 12). Opts the view's backing storage
564 /// into a retention policy independent of the source. Persisted on
565 /// the view definition; the physical sweep is applied to the
566 /// MV's backing rows once slice-9's row-storage follow-up lands.
567 /// `None` means the view is unaffected by source retention.
568 pub retention_duration_ms: Option<u64>,
569}
570
571#[derive(Debug, Clone, PartialEq, Eq)]
572pub struct DropViewQuery {
573 pub name: String,
574 pub materialized: bool,
575 pub if_exists: bool,
576}
577
578#[derive(Debug, Clone, PartialEq, Eq)]
579pub struct RefreshMaterializedViewQuery {
580 pub name: String,
581}
582
583#[derive(Debug, Clone, PartialEq, Eq)]
584pub struct CopyFromQuery {
585 pub table: String,
586 pub path: String,
587 pub format: CopyFormat,
588 pub delimiter: Option<char>,
589 pub has_header: bool,
590}
591
592#[derive(Debug, Clone, Copy, PartialEq, Eq)]
593pub enum CopyFormat {
594 Csv,
595}
596
597#[derive(Debug, Clone, PartialEq, Eq)]
598pub struct CreateSchemaQuery {
599 pub name: String,
600 pub if_not_exists: bool,
601}
602
603#[derive(Debug, Clone, PartialEq, Eq)]
604pub struct DropSchemaQuery {
605 pub name: String,
606 pub if_exists: bool,
607 pub cascade: bool,
608}
609
610#[derive(Debug, Clone, PartialEq, Eq)]
611pub struct CreateSequenceQuery {
612 pub name: String,
613 pub if_not_exists: bool,
614 /// First value produced by `nextval`. Default 1.
615 pub start: i64,
616 /// Added to the current value on each `nextval`. Default 1.
617 pub increment: i64,
618}
619
620#[derive(Debug, Clone, PartialEq, Eq)]
621pub struct DropSequenceQuery {
622 pub name: String,
623 pub if_exists: bool,
624}
625
626/// Transaction-control statement variants. See [`QueryExpr::TransactionControl`].
627#[derive(Debug, Clone, PartialEq, Eq)]
628pub enum TxnControl {
629 /// `BEGIN [WORK | TRANSACTION]`, `START TRANSACTION`
630 Begin,
631 /// `COMMIT [WORK | TRANSACTION]`, `END`
632 Commit,
633 /// `ROLLBACK [WORK | TRANSACTION]`
634 Rollback,
635 /// `SAVEPOINT name`
636 Savepoint(String),
637 /// `RELEASE [SAVEPOINT] name`
638 ReleaseSavepoint(String),
639 /// `ROLLBACK TO [SAVEPOINT] name`
640 RollbackToSavepoint(String),
641}
642
643/// Maintenance command variants. See [`QueryExpr::MaintenanceCommand`].
644#[derive(Debug, Clone, PartialEq, Eq)]
645pub enum MaintenanceCommand {
646 /// `VACUUM [FULL] [table]`
647 ///
648 /// Triggers segment compaction and planner stats refresh. `FULL` additionally
649 /// forces a full pager sync. Target `None` applies to every collection.
650 Vacuum { target: Option<String>, full: bool },
651 /// `ANALYZE [table]`
652 ///
653 /// Refreshes planner statistics (histogram, distinct estimates, null counts).
654 /// Target `None` re-analyzes every collection.
655 Analyze { target: Option<String> },
656}
657
658/// AST node for `EXPLAIN ALTER FOR <CreateTableStmt> [FORMAT JSON]`.
659///
660/// `target` carries the CREATE TABLE structure exactly as the
661/// parser produces it for a regular CREATE — full reuse of
662/// `parse_create_table_body`. `format` determines whether the
663/// executor emits a `ALTER TABLE …;`-flavored text payload
664/// (the default — copy-paste friendly into the REPL) or a
665/// structured JSON object (machine-friendly).
666#[derive(Debug, Clone)]
667pub struct ExplainAlterQuery {
668 pub target: CreateTableQuery,
669 pub format: ExplainFormat,
670}
671
672/// Output format requested for an `EXPLAIN ALTER` command.
673#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
674pub enum ExplainFormat {
675 /// Plain SQL text — `ALTER TABLE …;` lines plus header
676 /// comments and rename hints. Default; copy-paste friendly.
677 #[default]
678 Sql,
679 /// Structured JSON object with `operations`,
680 /// `rename_candidates`, `summary`. Machine-friendly for
681 /// driver code (Purple migration generator, dashboards,
682 /// CLI tools).
683 Json,
684}
685
686#[derive(Debug, Clone, PartialEq)]
687pub struct CreateMigrationQuery {
688 pub name: String,
689 pub body: String,
690 pub depends_on: Vec<String>,
691 pub batch_size: Option<u64>,
692 pub no_rollback: bool,
693}
694
695#[derive(Debug, Clone, PartialEq)]
696pub struct ApplyMigrationQuery {
697 pub target: ApplyMigrationTarget,
698 pub for_tenant: Option<String>,
699}
700
701#[derive(Debug, Clone, PartialEq)]
702pub enum ApplyMigrationTarget {
703 Named(String),
704 All,
705}
706
707#[derive(Debug, Clone, PartialEq)]
708pub struct RollbackMigrationQuery {
709 pub name: String,
710}
711
712#[derive(Debug, Clone, PartialEq)]
713pub struct ExplainMigrationQuery {
714 pub name: String,
715}
716
717/// Probabilistic data structure commands
718#[derive(Debug, Clone)]
719pub enum ProbabilisticCommand {
720 // HyperLogLog
721 CreateHll {
722 name: String,
723 precision: u8,
724 if_not_exists: bool,
725 },
726 HllAdd {
727 name: String,
728 elements: Vec<String>,
729 },
730 HllCount {
731 names: Vec<String>,
732 },
733 HllMerge {
734 dest: String,
735 sources: Vec<String>,
736 },
737 HllInfo {
738 name: String,
739 },
740 DropHll {
741 name: String,
742 if_exists: bool,
743 },
744
745 // Count-Min Sketch (Fase 7)
746 CreateSketch {
747 name: String,
748 width: usize,
749 depth: usize,
750 if_not_exists: bool,
751 },
752 SketchAdd {
753 name: String,
754 element: String,
755 count: u64,
756 },
757 SketchCount {
758 name: String,
759 element: String,
760 },
761 SketchMerge {
762 dest: String,
763 sources: Vec<String>,
764 },
765 SketchInfo {
766 name: String,
767 },
768 DropSketch {
769 name: String,
770 if_exists: bool,
771 },
772
773 // Cuckoo Filter (Fase 8)
774 CreateFilter {
775 name: String,
776 capacity: usize,
777 if_not_exists: bool,
778 },
779 FilterAdd {
780 name: String,
781 element: String,
782 },
783 FilterCheck {
784 name: String,
785 element: String,
786 },
787 FilterDelete {
788 name: String,
789 element: String,
790 },
791 FilterCount {
792 name: String,
793 },
794 FilterInfo {
795 name: String,
796 },
797 DropFilter {
798 name: String,
799 if_exists: bool,
800 },
801}
802
803/// Index type for CREATE INDEX ... USING <type>
804#[derive(Debug, Clone, PartialEq, Eq)]
805pub enum IndexMethod {
806 BTree,
807 Hash,
808 Bitmap,
809 RTree,
810}
811
812impl fmt::Display for IndexMethod {
813 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
814 match self {
815 Self::BTree => write!(f, "BTREE"),
816 Self::Hash => write!(f, "HASH"),
817 Self::Bitmap => write!(f, "BITMAP"),
818 Self::RTree => write!(f, "RTREE"),
819 }
820 }
821}
822
823/// CREATE INDEX [UNIQUE] [IF NOT EXISTS] name ON table (col1, col2, ...) [USING method]
824#[derive(Debug, Clone)]
825pub struct CreateIndexQuery {
826 pub name: String,
827 pub table: String,
828 pub columns: Vec<String>,
829 pub method: IndexMethod,
830 pub unique: bool,
831 pub if_not_exists: bool,
832}
833
834/// DROP INDEX [IF EXISTS] name ON table
835#[derive(Debug, Clone)]
836pub struct DropIndexQuery {
837 pub name: String,
838 pub table: String,
839 pub if_exists: bool,
840}
841
842/// ASK 'question' [USING provider] [MODEL 'model'] [DEPTH n] [LIMIT n] [MIN_SCORE x]
843/// [COLLECTION col] [TEMPERATURE x] [SEED n] [STRICT ON|OFF] [STREAM]
844/// [CACHE TTL '5m' | NOCACHE]
845///
846/// `temperature` and `seed` are per-query overrides resolved by the
847/// `DeterminismDecider` (issue #400). The parser merely surfaces the
848/// requested values; capability-based dropping happens at decide time.
849#[derive(Debug, Clone)]
850pub struct AskQuery {
851 /// `EXPLAIN ASK '...'` returns the retrieval/provider/cost plan
852 /// without making the LLM call.
853 pub explain: bool,
854 pub question: String,
855 /// Optional `$N` / `?` parameter slot for the question text.
856 pub question_param: Option<usize>,
857 pub provider: Option<String>,
858 pub model: Option<String>,
859 pub depth: Option<usize>,
860 pub limit: Option<usize>,
861 pub min_score: Option<f32>,
862 pub collection: Option<String>,
863 /// Per-query temperature override (`ASK '...' TEMPERATURE 0.7`).
864 /// `None` means fall back to `ask.default_temperature`.
865 pub temperature: Option<f32>,
866 /// Per-query seed override (`ASK '...' SEED 42`). `None` means the
867 /// decider derives one from `hash(question + sources_fingerprint)`.
868 pub seed: Option<u64>,
869 /// Strict citation validation is on by default. `STRICT OFF` keeps
870 /// citation diagnostics as warnings and skips retry/error handling.
871 pub strict: bool,
872 /// HTTP-only SSE response requested via `ASK '...' STREAM`.
873 pub stream: bool,
874 /// Per-query answer-cache override.
875 pub cache: AskCacheClause,
876}
877
878#[derive(Debug, Clone, PartialEq, Eq, Default)]
879pub enum AskCacheClause {
880 #[default]
881 Default,
882 CacheTtl(String),
883 NoCache,
884}
885
886impl QueryExpr {
887 /// Create a table query
888 pub fn table(name: &str) -> TableQueryBuilder {
889 TableQueryBuilder::new(name)
890 }
891
892 /// Create a graph query
893 pub fn graph() -> GraphQueryBuilder {
894 GraphQueryBuilder::new()
895 }
896
897 /// Create a path query
898 pub fn path(from: NodeSelector, to: NodeSelector) -> PathQueryBuilder {
899 PathQueryBuilder::new(from, to)
900 }
901}
902
903// ============================================================================
904// Table Query
905// ============================================================================
906
907/// Table query: SELECT columns FROM table WHERE filter ORDER BY ... LIMIT ...
908#[derive(Debug, Clone)]
909pub struct TableQuery {
910 /// Table name. Legacy slot — still populated even when `source`
911 /// is set to a subquery so existing call sites that read
912 /// `query.table.as_str()` keep compiling. When `source` is
913 /// `Some(TableSource::Subquery(…))`, this field holds a synthetic
914 /// sentinel name (`"__subq_NNNN"`) that runtime code must never
915 /// resolve against the real schema registry.
916 pub table: String,
917 /// Fase 2 Week 3: structured table source. `None` means the
918 /// legacy `table` field is authoritative. `Some(Name)` is the
919 /// same information as `table` but in typed form. `Some(Subquery)`
920 /// wires a `(SELECT …) AS alias` in a FROM position — the Fase
921 /// 1.7 unlock.
922 pub source: Option<TableSource>,
923 /// Optional table alias
924 pub alias: Option<String>,
925 /// Canonical SQL select list.
926 pub select_items: Vec<SelectItem>,
927 /// Columns to select (empty = all)
928 pub columns: Vec<Projection>,
929 /// Canonical SQL WHERE clause.
930 pub where_expr: Option<super::Expr>,
931 /// Filter condition
932 pub filter: Option<Filter>,
933 /// Canonical SQL GROUP BY items.
934 pub group_by_exprs: Vec<super::Expr>,
935 /// GROUP BY fields
936 pub group_by: Vec<String>,
937 /// Canonical SQL HAVING clause.
938 pub having_expr: Option<super::Expr>,
939 /// HAVING filter (applied after grouping)
940 pub having: Option<Filter>,
941 /// Order by clauses
942 pub order_by: Vec<OrderByClause>,
943 /// Limit
944 pub limit: Option<u64>,
945 /// User-supplied-parameter slot for `LIMIT $N`. Set by the parser
946 /// when the LIMIT clause references `$N`/`?` instead of a literal;
947 /// cleared by the binder (`user_params::bind`) after substituting
948 /// the parameter into `limit`. Mirrors the `limit_param` slot on
949 /// `SearchCommand` variants — see #361 slice 11.
950 pub limit_param: Option<usize>,
951 /// Offset
952 pub offset: Option<u64>,
953 /// User-supplied-parameter slot for `OFFSET $N`. Same lifecycle as
954 /// `limit_param`. See #361 slice 11.
955 pub offset_param: Option<usize>,
956 /// WITH EXPAND options (graph traversal, cross-ref following)
957 pub expand: Option<ExpandOptions>,
958 /// Time-travel anchor. When present the executor resolves this
959 /// to an MVCC xid and evaluates the query against that snapshot
960 /// instead of the current one. Mirrors git's `AS OF` semantics.
961 pub as_of: Option<AsOfClause>,
962 /// `SESSIONIZE BY <actor> GAP <duration> [ORDER BY <ts>]` operator
963 /// (issue #585 slice 8). When present, the executor annotates each
964 /// result row with a `session_id` column. `actor_col` / `gap_ms`
965 /// may be `None` when the source collection's descriptor (slice 1
966 /// `SESSION_KEY` / `SESSION_GAP`) supplies the defaults; one
967 /// without the other resolved at execution time is the typed
968 /// `MissingSessionKey` error.
969 pub sessionize: Option<SessionizeClause>,
970 /// `SELECT DISTINCT` projection quantifier. When `true` the executor
971 /// deduplicates the projected output row-set (over the projected
972 /// columns) before ORDER BY / LIMIT. `DISTINCT` inside an aggregate
973 /// argument (`COUNT(DISTINCT x)`) is unrelated and lives on the
974 /// aggregate call, not here.
975 pub distinct: bool,
976}
977
978/// `SESSIONIZE BY <actor_col> GAP <duration> [ORDER BY <ts_col>]`.
979#[derive(Debug, Clone, Default)]
980pub struct SessionizeClause {
981 /// Explicit `BY <ident>`. `None` means "default from descriptor's
982 /// `SESSION_KEY`" — resolved at execution time.
983 pub actor_col: Option<String>,
984 /// Explicit `GAP <duration>` in milliseconds. `None` means
985 /// "default from descriptor's `SESSION_GAP`".
986 pub gap_ms: Option<u64>,
987 /// Explicit `ORDER BY <ident>` immediately after `GAP`. When
988 /// `None` the executor falls back to the collection's timestamp
989 /// column (the same resolution as `retention_filter`).
990 pub order_col: Option<String>,
991}
992
993/// Source spec for `AS OF` — parsed form sits in `TableQuery`, then
994/// `vcs_resolve_as_of` turns it into an MVCC xid at execute time.
995#[derive(Debug, Clone)]
996pub enum AsOfClause {
997 /// Explicit commit hash literal: `AS OF COMMIT '<hex>'`.
998 Commit(String),
999 /// Branch or ref: `AS OF BRANCH 'main'` or `AS OF 'refs/heads/main'`.
1000 Branch(String),
1001 /// Tag: `AS OF TAG 'v1.0'`.
1002 Tag(String),
1003 /// Unix epoch milliseconds: `AS OF TIMESTAMP 1710000000000`.
1004 TimestampMs(i64),
1005 /// Raw MVCC snapshot xid: `AS OF SNAPSHOT 12345`.
1006 Snapshot(u64),
1007}
1008
1009/// Structured FROM source for a `TableQuery`. Additive alongside the
1010/// legacy `TableQuery.table: String` slot — callers that understand
1011/// this type can branch on subqueries; callers that only read `table`
1012/// fall back to the synthetic sentinel name and, for subqueries,
1013/// produce an "unknown table" error until they migrate.
1014#[derive(Debug, Clone)]
1015pub enum TableSource {
1016 /// Plain table reference — equivalent to the legacy `String` form.
1017 Name(String),
1018 /// A subquery in FROM position: `FROM (SELECT …) AS alias`.
1019 Subquery(Box<QueryExpr>),
1020 /// A table-valued function call in FROM position, e.g.
1021 /// `FROM components(g)` (issue #795). `name` is the function
1022 /// identifier; `args` are its positional identifier arguments;
1023 /// `named_args` are `key => <f64>` named arguments such as
1024 /// `louvain(g, resolution => 0.5)` (issue #796), preserved in source
1025 /// order. Positional args always precede named args.
1026 Function {
1027 name: String,
1028 args: Vec<String>,
1029 named_args: Vec<(String, f64)>,
1030 },
1031 /// A graph-analytics table-valued function whose graph is supplied
1032 /// inline as two subqueries instead of a graph-collection reference
1033 /// (issue #799), e.g.
1034 /// `components(nodes => (SELECT id FROM hosts), edges => (SELECT src, dst FROM links))`.
1035 ///
1036 /// Structurally distinct from `Function` so the executor can tell the
1037 /// inline form from the graph-collection form. `nodes`/`edges` are the
1038 /// two materialization subqueries (the first column of `nodes` is the
1039 /// node id; the first two-or-three columns of `edges` are
1040 /// `(source, target [, weight])`). `named_args` carries any remaining
1041 /// numeric named arguments (e.g. `resolution => 0.5`).
1042 InlineGraphFunction {
1043 name: String,
1044 nodes: Box<QueryExpr>,
1045 edges: Box<QueryExpr>,
1046 named_args: Vec<(String, f64)>,
1047 },
1048}
1049
1050/// Options for WITH EXPAND clause on SELECT queries.
1051#[derive(Debug, Clone, Default)]
1052pub struct ExpandOptions {
1053 /// Expand via graph edges (WITH EXPAND GRAPH)
1054 pub graph: bool,
1055 /// Graph expansion depth (DEPTH n)
1056 pub graph_depth: usize,
1057 /// Expand via cross-references (WITH EXPAND CROSS_REFS)
1058 pub cross_refs: bool,
1059 /// Index hint from the optimizer (which index to prefer for this query)
1060 pub index_hint: Option<reddb_types::index_hint::IndexHint>,
1061}
1062
1063impl TableQuery {
1064 /// Create a new table query
1065 pub fn new(table: &str) -> Self {
1066 Self {
1067 table: table.to_string(),
1068 source: None,
1069 alias: None,
1070 select_items: Vec::new(),
1071 columns: Vec::new(),
1072 where_expr: None,
1073 filter: None,
1074 group_by_exprs: Vec::new(),
1075 group_by: Vec::new(),
1076 having_expr: None,
1077 having: None,
1078 order_by: Vec::new(),
1079 limit: None,
1080 limit_param: None,
1081 offset: None,
1082 offset_param: None,
1083 expand: None,
1084 as_of: None,
1085 sessionize: None,
1086 distinct: false,
1087 }
1088 }
1089
1090 /// Create a TableQuery that wraps a subquery in FROM position.
1091 /// The legacy `table` slot holds a synthetic sentinel so code that
1092 /// only reads `table.as_str()` errors loudly with a
1093 /// recognisable marker instead of silently treating it as a
1094 /// real collection.
1095 pub fn from_subquery(subquery: QueryExpr, alias: Option<String>) -> Self {
1096 let sentinel = match &alias {
1097 Some(a) => format!("__subq_{a}"),
1098 None => "__subq_anon".to_string(),
1099 };
1100 Self {
1101 table: sentinel,
1102 source: Some(TableSource::Subquery(Box::new(subquery))),
1103 alias,
1104 select_items: Vec::new(),
1105 columns: Vec::new(),
1106 where_expr: None,
1107 filter: None,
1108 group_by_exprs: Vec::new(),
1109 group_by: Vec::new(),
1110 having_expr: None,
1111 having: None,
1112 order_by: Vec::new(),
1113 limit: None,
1114 limit_param: None,
1115 offset: None,
1116 offset_param: None,
1117 expand: None,
1118 as_of: None,
1119 sessionize: None,
1120 distinct: false,
1121 }
1122 }
1123}
1124
1125/// Canonical SQL select item for table queries.
1126#[derive(Debug, Clone, PartialEq)]
1127pub enum SelectItem {
1128 Wildcard,
1129 Expr {
1130 expr: super::Expr,
1131 alias: Option<String>,
1132 },
1133}
1134
1135// ============================================================================
1136// Graph Query
1137// ============================================================================
1138
1139/// Graph query: MATCH pattern WHERE filter RETURN projection
1140#[derive(Debug, Clone)]
1141pub struct GraphQuery {
1142 /// Optional outer alias when used as a join source
1143 pub alias: Option<String>,
1144 /// Graph pattern to match
1145 pub pattern: GraphPattern,
1146 /// Filter condition
1147 pub filter: Option<Filter>,
1148 /// Return projections
1149 pub return_: Vec<Projection>,
1150 /// Optional row limit
1151 pub limit: Option<u64>,
1152}
1153
1154impl GraphQuery {
1155 /// Create a new graph query
1156 pub fn new(pattern: GraphPattern) -> Self {
1157 Self {
1158 alias: None,
1159 pattern,
1160 filter: None,
1161 return_: Vec::new(),
1162 limit: None,
1163 }
1164 }
1165
1166 /// Set outer alias
1167 pub fn alias(mut self, alias: &str) -> Self {
1168 self.alias = Some(alias.to_string());
1169 self
1170 }
1171}
1172
1173/// Graph pattern: collection of node and edge patterns
1174#[derive(Debug, Clone, Default)]
1175pub struct GraphPattern {
1176 /// Node patterns
1177 pub nodes: Vec<NodePattern>,
1178 /// Edge patterns connecting nodes
1179 pub edges: Vec<EdgePattern>,
1180}
1181
1182impl GraphPattern {
1183 /// Create an empty pattern
1184 pub fn new() -> Self {
1185 Self::default()
1186 }
1187
1188 /// Add a node pattern
1189 pub fn node(mut self, pattern: NodePattern) -> Self {
1190 self.nodes.push(pattern);
1191 self
1192 }
1193
1194 /// Add an edge pattern
1195 pub fn edge(mut self, pattern: EdgePattern) -> Self {
1196 self.edges.push(pattern);
1197 self
1198 }
1199}
1200
1201/// Node pattern: (alias:Type {properties})
1202#[derive(Debug, Clone)]
1203pub struct NodePattern {
1204 /// Variable alias for this node
1205 pub alias: String,
1206 /// Optional label filter. Stored as the user-supplied label string so
1207 /// the parser is registry-free; executors resolve it against the live
1208 /// [`crate::storage::engine::graph_store::LabelRegistry`].
1209 pub node_label: Option<String>,
1210 /// Property filters
1211 pub properties: Vec<PropertyFilter>,
1212}
1213
1214impl NodePattern {
1215 /// Create a new node pattern
1216 pub fn new(alias: &str) -> Self {
1217 Self {
1218 alias: alias.to_string(),
1219 node_label: None,
1220 properties: Vec::new(),
1221 }
1222 }
1223
1224 /// Set the label filter (string form — preferred).
1225 pub fn of_label(mut self, label: impl Into<String>) -> Self {
1226 self.node_label = Some(label.into());
1227 self
1228 }
1229
1230 /// Add property filter
1231 pub fn with_property(mut self, name: &str, op: CompareOp, value: Value) -> Self {
1232 self.properties.push(PropertyFilter {
1233 name: name.to_string(),
1234 op,
1235 value,
1236 });
1237 self
1238 }
1239}
1240
1241/// Edge pattern: -[alias:Type*min..max]->
1242#[derive(Debug, Clone)]
1243pub struct EdgePattern {
1244 /// Optional alias for this edge
1245 pub alias: Option<String>,
1246 /// Source node alias
1247 pub from: String,
1248 /// Target node alias
1249 pub to: String,
1250 /// Optional label filter (user-supplied string).
1251 pub edge_label: Option<String>,
1252 /// Edge direction
1253 pub direction: EdgeDirection,
1254 /// Minimum hops (for variable-length patterns)
1255 pub min_hops: u32,
1256 /// Maximum hops (for variable-length patterns)
1257 pub max_hops: u32,
1258}
1259
1260impl EdgePattern {
1261 /// Create a new edge pattern
1262 pub fn new(from: &str, to: &str) -> Self {
1263 Self {
1264 alias: None,
1265 from: from.to_string(),
1266 to: to.to_string(),
1267 edge_label: None,
1268 direction: EdgeDirection::Outgoing,
1269 min_hops: 1,
1270 max_hops: 1,
1271 }
1272 }
1273
1274 /// Set label filter (string form — preferred).
1275 pub fn of_label(mut self, label: impl Into<String>) -> Self {
1276 self.edge_label = Some(label.into());
1277 self
1278 }
1279
1280 /// Set direction
1281 pub fn direction(mut self, dir: EdgeDirection) -> Self {
1282 self.direction = dir;
1283 self
1284 }
1285
1286 /// Set hop range for variable-length patterns
1287 pub fn hops(mut self, min: u32, max: u32) -> Self {
1288 self.min_hops = min;
1289 self.max_hops = max;
1290 self
1291 }
1292
1293 /// Set alias
1294 pub fn alias(mut self, alias: &str) -> Self {
1295 self.alias = Some(alias.to_string());
1296 self
1297 }
1298}
1299
1300/// Edge direction
1301#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1302pub enum EdgeDirection {
1303 /// Outgoing: (a)-[r]->(b)
1304 Outgoing,
1305 /// Incoming: (a)<-[r]-(b)
1306 Incoming,
1307 /// Both: (a)-[r]-(b)
1308 Both,
1309}
1310
1311/// Property filter: name op value
1312#[derive(Debug, Clone)]
1313pub struct PropertyFilter {
1314 pub name: String,
1315 pub op: CompareOp,
1316 pub value: Value,
1317}
1318
1319// ============================================================================
1320// Join Query
1321// ============================================================================
1322
1323/// Join query: combines table and graph queries
1324#[derive(Debug, Clone)]
1325pub struct JoinQuery {
1326 /// Left side (typically table)
1327 pub left: Box<QueryExpr>,
1328 /// Right side (typically graph)
1329 pub right: Box<QueryExpr>,
1330 /// Join type
1331 pub join_type: JoinType,
1332 /// Join condition
1333 pub on: JoinCondition,
1334 /// Post-join filter condition
1335 pub filter: Option<Filter>,
1336 /// Post-join ordering
1337 pub order_by: Vec<OrderByClause>,
1338 /// Post-join limit
1339 pub limit: Option<u64>,
1340 /// Post-join offset
1341 pub offset: Option<u64>,
1342 /// Canonical SQL RETURN projection.
1343 pub return_items: Vec<SelectItem>,
1344 /// Post-join projection
1345 pub return_: Vec<Projection>,
1346}
1347
1348impl JoinQuery {
1349 /// Create a new join query
1350 pub fn new(left: QueryExpr, right: QueryExpr, on: JoinCondition) -> Self {
1351 Self {
1352 left: Box::new(left),
1353 right: Box::new(right),
1354 join_type: JoinType::Inner,
1355 on,
1356 filter: None,
1357 order_by: Vec::new(),
1358 limit: None,
1359 offset: None,
1360 return_items: Vec::new(),
1361 return_: Vec::new(),
1362 }
1363 }
1364
1365 /// Set join type
1366 pub fn join_type(mut self, jt: JoinType) -> Self {
1367 self.join_type = jt;
1368 self
1369 }
1370}
1371
1372/// Join type
1373#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1374pub enum JoinType {
1375 /// Inner join — only matching pairs emitted
1376 Inner,
1377 /// Left outer join — every left row, matched or padded with nulls on the right
1378 LeftOuter,
1379 /// Right outer join — every right row, matched or padded with nulls on the left
1380 RightOuter,
1381 /// Full outer join — LeftOuter ∪ RightOuter, each unmatched side padded
1382 FullOuter,
1383 /// Cross join — Cartesian product, no predicate
1384 Cross,
1385}
1386
1387/// Join condition: how to match rows with nodes
1388#[derive(Debug, Clone)]
1389pub struct JoinCondition {
1390 /// Left field (table side)
1391 pub left_field: FieldRef,
1392 /// Right field (graph side)
1393 pub right_field: FieldRef,
1394}
1395
1396impl JoinCondition {
1397 /// Create a new join condition
1398 pub fn new(left: FieldRef, right: FieldRef) -> Self {
1399 Self {
1400 left_field: left,
1401 right_field: right,
1402 }
1403 }
1404}
1405
1406/// Reference to a field (table column, node property, or edge property)
1407#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1408pub enum FieldRef {
1409 /// Table column: table.column
1410 TableColumn { table: String, column: String },
1411 /// Node property: alias.property
1412 NodeProperty { alias: String, property: String },
1413 /// Edge property: alias.property
1414 EdgeProperty { alias: String, property: String },
1415 /// Node ID: alias.id
1416 NodeId { alias: String },
1417}
1418
1419impl FieldRef {
1420 /// Create a table column reference
1421 pub fn column(table: &str, column: &str) -> Self {
1422 Self::TableColumn {
1423 table: table.to_string(),
1424 column: column.to_string(),
1425 }
1426 }
1427
1428 /// Create a node property reference
1429 pub fn node_prop(alias: &str, property: &str) -> Self {
1430 Self::NodeProperty {
1431 alias: alias.to_string(),
1432 property: property.to_string(),
1433 }
1434 }
1435
1436 /// Create a node ID reference
1437 pub fn node_id(alias: &str) -> Self {
1438 Self::NodeId {
1439 alias: alias.to_string(),
1440 }
1441 }
1442
1443 /// Create an edge property reference
1444 pub fn edge_prop(alias: &str, property: &str) -> Self {
1445 Self::EdgeProperty {
1446 alias: alias.to_string(),
1447 property: property.to_string(),
1448 }
1449 }
1450}
1451
1452// ============================================================================
1453// Path Query
1454// ============================================================================
1455
1456/// Path query: find paths between nodes
1457#[derive(Debug, Clone)]
1458pub struct PathQuery {
1459 /// Optional outer alias when used as a join source
1460 pub alias: Option<String>,
1461 /// Source node selector
1462 pub from: NodeSelector,
1463 /// Target node selector
1464 pub to: NodeSelector,
1465 /// Edge labels to traverse (empty = any). Strings are resolved against
1466 /// the runtime registry by the executor.
1467 pub via: Vec<String>,
1468 /// Maximum path length
1469 pub max_length: u32,
1470 /// Filter on paths
1471 pub filter: Option<Filter>,
1472 /// Return projections
1473 pub return_: Vec<Projection>,
1474}
1475
1476impl PathQuery {
1477 /// Create a new path query
1478 pub fn new(from: NodeSelector, to: NodeSelector) -> Self {
1479 Self {
1480 alias: None,
1481 from,
1482 to,
1483 via: Vec::new(),
1484 max_length: 10,
1485 filter: None,
1486 return_: Vec::new(),
1487 }
1488 }
1489
1490 /// Set outer alias
1491 pub fn alias(mut self, alias: &str) -> Self {
1492 self.alias = Some(alias.to_string());
1493 self
1494 }
1495
1496 /// Add an edge label constraint to traverse (string form).
1497 pub fn via_label(mut self, label: impl Into<String>) -> Self {
1498 self.via.push(label.into());
1499 self
1500 }
1501}
1502
1503/// Node selector for path queries
1504#[derive(Debug, Clone)]
1505pub enum NodeSelector {
1506 /// By node ID
1507 ById(String),
1508 /// By node label and property
1509 ByType {
1510 node_label: String,
1511 filter: Option<PropertyFilter>,
1512 },
1513 /// By table row (linked node)
1514 ByRow { table: String, row_id: u64 },
1515}
1516
1517impl NodeSelector {
1518 /// Select by node ID
1519 pub fn by_id(id: &str) -> Self {
1520 Self::ById(id.to_string())
1521 }
1522
1523 /// Select by label string (preferred).
1524 pub fn by_label(label: impl Into<String>) -> Self {
1525 Self::ByType {
1526 node_label: label.into(),
1527 filter: None,
1528 }
1529 }
1530
1531 /// Select by table row
1532 pub fn by_row(table: &str, row_id: u64) -> Self {
1533 Self::ByRow {
1534 table: table.to_string(),
1535 row_id,
1536 }
1537 }
1538}
1539
1540// ============================================================================
1541// Vector Query
1542// ============================================================================
1543
1544/// Vector similarity search query
1545///
1546/// ```text
1547/// VECTOR SEARCH embeddings
1548/// SIMILAR TO [0.1, 0.2, ..., 0.5]
1549/// WHERE metadata.source = 'nmap'
1550/// LIMIT 10
1551/// ```
1552#[derive(Debug, Clone)]
1553pub struct VectorQuery {
1554 /// Optional outer alias when used as a join source
1555 pub alias: Option<String>,
1556 /// Collection name to search
1557 pub collection: String,
1558 /// Query vector (or reference to get vector from)
1559 pub query_vector: VectorSource,
1560 /// Number of results to return
1561 pub k: usize,
1562 /// Metadata filter
1563 pub filter: Option<MetadataFilter>,
1564 /// Distance metric to use (defaults to collection's metric)
1565 pub metric: Option<DistanceMetric>,
1566 /// Include vectors in results
1567 pub include_vectors: bool,
1568 /// Include metadata in results
1569 pub include_metadata: bool,
1570 /// Minimum similarity threshold (optional)
1571 pub threshold: Option<f32>,
1572}
1573
1574impl VectorQuery {
1575 /// Create a new vector query
1576 pub fn new(collection: &str, query: VectorSource) -> Self {
1577 Self {
1578 alias: None,
1579 collection: collection.to_string(),
1580 query_vector: query,
1581 k: 10,
1582 filter: None,
1583 metric: None,
1584 include_vectors: false,
1585 include_metadata: true,
1586 threshold: None,
1587 }
1588 }
1589
1590 /// Set the number of results
1591 pub fn limit(mut self, k: usize) -> Self {
1592 self.k = k;
1593 self
1594 }
1595
1596 /// Set metadata filter
1597 pub fn with_filter(mut self, filter: MetadataFilter) -> Self {
1598 self.filter = Some(filter);
1599 self
1600 }
1601
1602 /// Include vectors in results
1603 pub fn with_vectors(mut self) -> Self {
1604 self.include_vectors = true;
1605 self
1606 }
1607
1608 /// Set similarity threshold
1609 pub fn min_similarity(mut self, threshold: f32) -> Self {
1610 self.threshold = Some(threshold);
1611 self
1612 }
1613
1614 /// Set outer alias
1615 pub fn alias(mut self, alias: &str) -> Self {
1616 self.alias = Some(alias.to_string());
1617 self
1618 }
1619}
1620
1621/// Source of query vector
1622#[derive(Debug, Clone)]
1623pub enum VectorSource {
1624 /// Literal vector values
1625 Literal(Vec<f32>),
1626 /// Text to embed (requires embedding function)
1627 Text(String),
1628 /// Reference to another vector by ID
1629 Reference { collection: String, vector_id: u64 },
1630 /// From a subquery result
1631 Subquery(Box<QueryExpr>),
1632}
1633
1634impl VectorSource {
1635 /// Create from literal vector
1636 pub fn literal(values: Vec<f32>) -> Self {
1637 Self::Literal(values)
1638 }
1639
1640 /// Create from text (to be embedded)
1641 pub fn text(s: &str) -> Self {
1642 Self::Text(s.to_string())
1643 }
1644
1645 /// Reference another vector
1646 pub fn reference(collection: &str, vector_id: u64) -> Self {
1647 Self::Reference {
1648 collection: collection.to_string(),
1649 vector_id,
1650 }
1651 }
1652}
1653
1654// ============================================================================
1655// Hybrid Query
1656// ============================================================================
1657
1658/// Hybrid query combining structured (table/graph) and vector search
1659///
1660/// ```text
1661/// FROM hosts h
1662/// JOIN VECTOR embeddings e ON h.id = e.metadata.host_id
1663/// SIMILAR TO 'ssh vulnerability'
1664/// WHERE h.os = 'Linux'
1665/// RETURN h.*, e.distance
1666/// ```
1667#[derive(Debug, Clone)]
1668pub struct HybridQuery {
1669 /// Optional outer alias when used as a join source
1670 pub alias: Option<String>,
1671 /// Structured query part (table/graph)
1672 pub structured: Box<QueryExpr>,
1673 /// Vector search part
1674 pub vector: VectorQuery,
1675 /// How to combine results
1676 pub fusion: FusionStrategy,
1677 /// Final result limit
1678 pub limit: Option<usize>,
1679}
1680
1681impl HybridQuery {
1682 /// Create a new hybrid query
1683 pub fn new(structured: QueryExpr, vector: VectorQuery) -> Self {
1684 Self {
1685 alias: None,
1686 structured: Box::new(structured),
1687 vector,
1688 fusion: FusionStrategy::Rerank { weight: 0.5 },
1689 limit: None,
1690 }
1691 }
1692
1693 /// Set fusion strategy
1694 pub fn with_fusion(mut self, fusion: FusionStrategy) -> Self {
1695 self.fusion = fusion;
1696 self
1697 }
1698
1699 /// Set result limit
1700 pub fn limit(mut self, limit: usize) -> Self {
1701 self.limit = Some(limit);
1702 self
1703 }
1704
1705 /// Set outer alias
1706 pub fn alias(mut self, alias: &str) -> Self {
1707 self.alias = Some(alias.to_string());
1708 self
1709 }
1710}
1711
1712/// Strategy for combining structured and vector search results
1713#[derive(Debug, Clone)]
1714pub enum FusionStrategy {
1715 /// Vector similarity re-ranks structured results
1716 /// weight: 0.0 = pure structured, 1.0 = pure vector
1717 Rerank { weight: f32 },
1718 /// Filter with structured query, then search vectors among filtered
1719 FilterThenSearch,
1720 /// Search vectors first, then filter with structured query
1721 SearchThenFilter,
1722 /// Reciprocal Rank Fusion
1723 /// k: RRF constant (typically 60)
1724 RRF { k: u32 },
1725 /// Intersection: only return results that match both
1726 Intersection,
1727 /// Union: return results from either (with combined scores)
1728 Union {
1729 structured_weight: f32,
1730 vector_weight: f32,
1731 },
1732}
1733
1734impl Default for FusionStrategy {
1735 fn default() -> Self {
1736 Self::Rerank { weight: 0.5 }
1737 }
1738}
1739
1740// ============================================================================
1741// DML/DDL Query Types
1742// ============================================================================
1743
1744/// Entity type qualifier for INSERT statements
1745#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1746pub enum InsertEntityType {
1747 /// Default: plain row
1748 #[default]
1749 Row,
1750 /// INSERT INTO t NODE (...)
1751 Node,
1752 /// INSERT INTO t EDGE (...)
1753 Edge,
1754 /// INSERT INTO t VECTOR (...)
1755 Vector,
1756 /// INSERT INTO t DOCUMENT (...)
1757 Document,
1758 /// INSERT INTO t KV (...)
1759 Kv,
1760}
1761
1762/// Explicit item-kind qualifier for UPDATE statements.
1763#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1764pub enum UpdateTarget {
1765 /// Default: table/document/KV row-shaped items.
1766 #[default]
1767 Rows,
1768 /// UPDATE t DOCUMENTS SET ...
1769 Documents,
1770 /// UPDATE t KV SET ...
1771 Kv,
1772 /// UPDATE t NODES SET ...
1773 Nodes,
1774 /// UPDATE t EDGES SET ...
1775 Edges,
1776}
1777
1778/// An item in a RETURNING clause: either `*` (all columns) or a named column.
1779#[derive(Debug, Clone, PartialEq)]
1780pub enum ReturningItem {
1781 /// RETURNING *
1782 All,
1783 /// RETURNING col
1784 Column(String),
1785}
1786
1787/// INSERT INTO table (columns) VALUES (row1), (row2), ... [WITH TTL duration] [WITH METADATA (k=v)]
1788#[derive(Debug, Clone)]
1789pub struct InsertQuery {
1790 /// Target table name
1791 pub table: String,
1792 /// Entity type qualifier
1793 pub entity_type: InsertEntityType,
1794 /// Column names
1795 pub columns: Vec<String>,
1796 /// Canonical SQL rows of expressions.
1797 pub value_exprs: Vec<Vec<super::Expr>>,
1798 /// Rows of values (each inner Vec is one row)
1799 pub values: Vec<Vec<Value>>,
1800 /// Optional RETURNING clause items.
1801 pub returning: Option<Vec<ReturningItem>>,
1802 /// Optional TTL in milliseconds (from WITH TTL clause)
1803 pub ttl_ms: Option<u64>,
1804 /// Optional absolute expiration (from WITH EXPIRES AT clause)
1805 pub expires_at_ms: Option<u64>,
1806 /// Optional metadata key-value pairs (from WITH METADATA clause)
1807 pub with_metadata: Vec<(String, Value)>,
1808 /// Auto-embed fields on insert (from WITH AUTO EMBED clause)
1809 pub auto_embed: Option<AutoEmbedConfig>,
1810 /// Skip event subscription emission for this statement (SUPPRESS EVENTS).
1811 pub suppress_events: bool,
1812}
1813
1814/// Configuration for automatic embedding generation on INSERT.
1815#[derive(Debug, Clone)]
1816pub struct AutoEmbedConfig {
1817 /// Fields to extract text from for embedding
1818 pub fields: Vec<String>,
1819 /// AI provider (e.g. "openai")
1820 pub provider: String,
1821 /// Optional model override
1822 pub model: Option<String>,
1823}
1824
1825/// EVENTS BACKFILL collection [WHERE pred] TO queue [LIMIT n]
1826#[derive(Debug, Clone)]
1827pub struct EventsBackfillQuery {
1828 pub collection: String,
1829 pub where_filter: Option<String>,
1830 pub target_queue: String,
1831 pub limit: Option<u64>,
1832}
1833
1834/// UPDATE table SET col=val, ... WHERE filter [WITH TTL duration] [WITH METADATA (...)]
1835#[derive(Debug, Clone)]
1836pub struct UpdateQuery {
1837 /// Target table name
1838 pub table: String,
1839 /// Explicit item-kind target. Omitted targets default to rows.
1840 pub target: UpdateTarget,
1841 /// Canonical SQL assignments.
1842 pub assignment_exprs: Vec<(String, super::Expr)>,
1843 /// Per-assignment compound operator for `SET col += expr` forms.
1844 /// `None` means ordinary `SET col = expr`.
1845 pub compound_assignment_ops: Vec<Option<super::BinOp>>,
1846 /// Best-effort literal-only cache of assignments. Non-foldable expressions
1847 /// are preserved exclusively in `assignment_exprs` and evaluated later
1848 /// against the row pre-image by the runtime.
1849 pub assignments: Vec<(String, Value)>,
1850 /// Canonical SQL WHERE clause.
1851 pub where_expr: Option<super::Expr>,
1852 /// Optional WHERE filter
1853 pub filter: Option<Filter>,
1854 /// Optional TTL in milliseconds (from WITH TTL clause)
1855 pub ttl_ms: Option<u64>,
1856 /// Optional absolute expiration (from WITH EXPIRES AT clause)
1857 pub expires_at_ms: Option<u64>,
1858 /// Optional metadata key-value pairs (from WITH METADATA clause)
1859 pub with_metadata: Vec<(String, Value)>,
1860 /// Optional RETURNING clause items.
1861 pub returning: Option<Vec<ReturningItem>>,
1862 /// Optional deterministic target ordering for limited UPDATE batches.
1863 pub order_by: Vec<OrderByClause>,
1864 /// Optional `LIMIT N` cap. Caps the number of targets the executor
1865 /// will mutate in a single statement. Required by `BATCH N ROWS`
1866 /// data migrations (#37) which run the same UPDATE body in a
1867 /// loop, advancing a checkpoint between batches.
1868 pub limit: Option<u64>,
1869 /// Skip event subscription emission for this statement (SUPPRESS EVENTS).
1870 pub suppress_events: bool,
1871}
1872
1873/// DELETE FROM table WHERE filter
1874#[derive(Debug, Clone)]
1875pub struct DeleteQuery {
1876 /// Target table name
1877 pub table: String,
1878 /// Canonical SQL WHERE clause.
1879 pub where_expr: Option<super::Expr>,
1880 /// Optional WHERE filter
1881 pub filter: Option<Filter>,
1882 /// Optional RETURNING clause items.
1883 pub returning: Option<Vec<ReturningItem>>,
1884 /// Skip event subscription emission for this statement (SUPPRESS EVENTS).
1885 pub suppress_events: bool,
1886}
1887
1888/// CREATE TABLE name (columns) or CREATE {KV|CONFIG|VAULT} name
1889#[derive(Debug, Clone)]
1890pub struct CreateTableQuery {
1891 /// Declared collection model. Defaults to Table for CREATE TABLE.
1892 pub collection_model: CollectionModel,
1893 /// Table name
1894 pub name: String,
1895 /// Column definitions
1896 pub columns: Vec<CreateColumnDef>,
1897 /// IF NOT EXISTS flag
1898 pub if_not_exists: bool,
1899 /// Optional default TTL applied to newly inserted items in this collection.
1900 pub default_ttl_ms: Option<u64>,
1901 /// Metrics rollup tiers declared by `CREATE METRICS ... DOWNSAMPLE`.
1902 /// Uses the existing time-series policy spelling: target:source:aggregation.
1903 pub metrics_rollup_policies: Vec<String>,
1904 /// Fields to prioritize in the context index (WITH CONTEXT INDEX ON (f1, f2))
1905 pub context_index_fields: Vec<String>,
1906 /// Enables the global context index for this table
1907 /// (`WITH context_index = true`). Default false — pure OLTP tables
1908 /// skip the tokenisation / 3-way RwLock write storm on every insert.
1909 /// Having `context_index_fields` non-empty also enables it implicitly.
1910 pub context_index_enabled: bool,
1911 /// When true, CREATE TABLE implicitly adds two user-visible columns
1912 /// `created_at` and `updated_at` (BIGINT unix-ms). The runtime
1913 /// populates them from `UnifiedEntity::created_at/updated_at` on
1914 /// every write; `created_at` is immutable after insert.
1915 /// Enabled via `WITH timestamps = true` in the DDL.
1916 pub timestamps: bool,
1917 /// Partitioning spec (Phase 2.2 PG parity).
1918 ///
1919 /// When present the table is the *parent* of a partition tree — every
1920 /// child partition is registered via `ALTER TABLE ... ATTACH PARTITION`.
1921 /// Phase 2.2 stops at registry-only: queries against a partitioned
1922 /// parent don't auto-rewrite as UNION yet (Phase 4 adds pruning).
1923 pub partition_by: Option<PartitionSpec>,
1924 /// Table-scoped multi-tenancy declaration (Phase 2.5.4).
1925 ///
1926 /// Syntax: `CREATE TABLE t (...) WITH (tenant_by = 'col_name')` or
1927 /// the shorthand `CREATE TABLE t (...) TENANT BY (col_name)`. The
1928 /// runtime treats the named column as the tenant discriminator and
1929 /// automatically:
1930 ///
1931 /// 1. Registers the table → column mapping so INSERTs that omit the
1932 /// column get `CURRENT_TENANT()` auto-filled.
1933 /// 2. Installs an implicit RLS policy equivalent to
1934 /// `USING (col = CURRENT_TENANT())` for SELECT/UPDATE/DELETE/INSERT.
1935 /// 3. Flips `rls_enabled_tables` on so the policy actually applies.
1936 ///
1937 /// None leaves the table non-tenant-scoped — callers manage tenancy
1938 /// manually via explicit CREATE POLICY if they want it.
1939 pub tenant_by: Option<String>,
1940 /// When true, UPDATE and DELETE on this table are rejected at
1941 /// parse time. Corresponds to `CREATE TABLE ... APPEND ONLY` or
1942 /// `WITH (append_only = true)`. Default false (mutable).
1943 pub append_only: bool,
1944 /// Declarative event subscriptions for this table. #291 stores
1945 /// metadata only; event emission is intentionally out of scope.
1946 pub subscriptions: Vec<reddb_types::catalog::SubscriptionDescriptor>,
1947 /// Analytics views declared by `CREATE GRAPH ... WITH ANALYTICS (...)`
1948 /// (issue #800). Empty for every collection model except graphs that
1949 /// opt in. Threaded into the persisted `CollectionContract` at execution
1950 /// time so each `<graph>.<output>` view is durable.
1951 pub analytics_config: Vec<reddb_types::catalog::AnalyticsViewDescriptor>,
1952 /// `CREATE VAULT ... WITH OWN MASTER KEY`: provision per-vault
1953 /// key material instead of using the cluster vault key.
1954 pub vault_own_master_key: bool,
1955}
1956
1957/// CREATE METRIC path TYPE kind ROLE role
1958/// [SOURCE <ident>] [QUERY '<text>'] [WINDOW <duration>] [TIME_FIELD <ident>]
1959///
1960/// Issue #790 — when any of the derived-metric clauses are present the
1961/// descriptor is a *derived* metric: it names the inputs that a future
1962/// execution layer would consume. v0 stores the metadata only; reads of
1963/// the metric's *output* (not its descriptor) return a structured
1964/// "not yet implemented" error.
1965#[derive(Debug, Clone)]
1966pub struct CreateMetricQuery {
1967 pub path: String,
1968 pub kind: String,
1969 pub role: String,
1970 pub source: Option<String>,
1971 pub query: Option<String>,
1972 pub window_ms: Option<u64>,
1973 pub time_field: Option<String>,
1974}
1975
1976/// ALTER METRIC path SET <field> <value>
1977///
1978/// v0 mutability:
1979/// - `set_role`: mutable — role is a semantic label (operational/kpi/sli).
1980/// - `attempted_kind`: parser captured a `SET KIND`/`SET TYPE` clause; the
1981/// runtime rejects with a clear error because kind changes alter the
1982/// metric's mathematical meaning (counter vs gauge vs histogram, etc.).
1983/// - `attempted_path`: parser captured a `SET PATH` clause; the runtime
1984/// rejects because path is the descriptor's identity.
1985#[derive(Debug, Clone)]
1986pub struct AlterMetricQuery {
1987 pub path: String,
1988 pub set_role: Option<String>,
1989 pub attempted_kind: Option<String>,
1990 pub attempted_path: Option<String>,
1991}
1992
1993/// CREATE SLO path ON metric_path TARGET t WINDOW d UNIT
1994///
1995/// Issue #791 — declared over an existing SLI-role metric descriptor.
1996/// `target` is the objective (0 < target <= 1, e.g. 0.999); `window_ms`
1997/// is the rolling window the objective is evaluated over. Burn-rate /
1998/// error-budget evaluation is deferred to later slices — v0 stores
1999/// catalog state only.
2000#[derive(Debug, Clone)]
2001pub struct CreateSloQuery {
2002 pub path: String,
2003 pub metric_path: String,
2004 pub target: f64,
2005 pub window_ms: u64,
2006}
2007
2008/// CREATE COLLECTION name KIND kind [SIGNED_BY ('pubkey_hex', ...)]
2009#[derive(Debug, Clone)]
2010pub struct CreateCollectionQuery {
2011 pub name: String,
2012 pub kind: String,
2013 pub if_not_exists: bool,
2014 pub vector_dimension: Option<usize>,
2015 pub vector_metric: Option<DistanceMetric>,
2016 /// Initial Ed25519 allowed-signer registry. Empty = unsigned collection.
2017 /// Each entry is a 32-byte Ed25519 public key. Mutable post-create via
2018 /// `ALTER COLLECTION ... ADD|REVOKE SIGNER` (see issue #520).
2019 pub allowed_signers: Vec<[u8; 32]>,
2020}
2021
2022/// CREATE VECTOR name DIM n [METRIC metric]
2023#[derive(Debug, Clone)]
2024pub struct CreateVectorQuery {
2025 pub name: String,
2026 pub dimension: usize,
2027 pub metric: DistanceMetric,
2028 pub if_not_exists: bool,
2029}
2030
2031/// `PARTITION BY RANGE|LIST|HASH (column)` clause.
2032#[derive(Debug, Clone, PartialEq, Eq)]
2033pub struct PartitionSpec {
2034 pub kind: PartitionKind,
2035 /// Partition key column(s). Simple single-column for Phase 2.2.
2036 pub column: String,
2037}
2038
2039#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2040pub enum PartitionKind {
2041 /// `PARTITION BY RANGE(col)` — children bind `FOR VALUES FROM (a) TO (b)`.
2042 Range,
2043 /// `PARTITION BY LIST(col)` — children bind `FOR VALUES IN (v1, v2, ...)`.
2044 List,
2045 /// `PARTITION BY HASH(col)` — children bind `FOR VALUES WITH (MODULUS m, REMAINDER r)`.
2046 Hash,
2047}
2048
2049/// Column definition for CREATE TABLE
2050#[derive(Debug, Clone)]
2051pub struct CreateColumnDef {
2052 /// Column name
2053 pub name: String,
2054 /// Legacy declared type string preserved for the runtime/storage pipeline.
2055 pub data_type: String,
2056 /// Structured SQL type used by the semantic layer.
2057 pub sql_type: SqlTypeName,
2058 /// NOT NULL constraint
2059 pub not_null: bool,
2060 /// DEFAULT value expression
2061 pub default: Option<String>,
2062 /// Compression level (COMPRESS:N)
2063 pub compress: Option<u8>,
2064 /// UNIQUE constraint
2065 pub unique: bool,
2066 /// PRIMARY KEY constraint
2067 pub primary_key: bool,
2068 /// Enum variant names (for ENUM type)
2069 pub enum_variants: Vec<String>,
2070 /// Array element type (for ARRAY type)
2071 pub array_element: Option<String>,
2072 /// Decimal precision (for DECIMAL type)
2073 pub decimal_precision: Option<u8>,
2074}
2075
2076/// DROP TABLE name
2077#[derive(Debug, Clone)]
2078pub struct DropTableQuery {
2079 /// Table name
2080 pub name: String,
2081 /// IF EXISTS flag
2082 pub if_exists: bool,
2083}
2084
2085/// DROP GRAPH [IF EXISTS] name
2086#[derive(Debug, Clone)]
2087pub struct DropGraphQuery {
2088 pub name: String,
2089 pub if_exists: bool,
2090}
2091
2092/// DROP VECTOR [IF EXISTS] name
2093#[derive(Debug, Clone)]
2094pub struct DropVectorQuery {
2095 pub name: String,
2096 pub if_exists: bool,
2097}
2098
2099/// DROP DOCUMENT [IF EXISTS] name
2100#[derive(Debug, Clone)]
2101pub struct DropDocumentQuery {
2102 pub name: String,
2103 pub if_exists: bool,
2104}
2105
2106/// DROP {KV|CONFIG|VAULT} [IF EXISTS] name
2107#[derive(Debug, Clone)]
2108pub struct DropKvQuery {
2109 pub name: String,
2110 pub if_exists: bool,
2111 pub model: CollectionModel,
2112}
2113
2114/// DROP COLLECTION [IF EXISTS] name
2115#[derive(Debug, Clone)]
2116pub struct DropCollectionQuery {
2117 pub name: String,
2118 pub if_exists: bool,
2119 pub model: Option<CollectionModel>,
2120}
2121
2122/// TRUNCATE {TABLE|GRAPH|VECTOR|DOCUMENT|TIMESERIES|KV|QUEUE|COLLECTION} [IF EXISTS] name
2123#[derive(Debug, Clone)]
2124pub struct TruncateQuery {
2125 pub name: String,
2126 pub model: Option<CollectionModel>,
2127 pub if_exists: bool,
2128}
2129
2130/// ALTER TABLE name operations
2131#[derive(Debug, Clone)]
2132pub struct AlterTableQuery {
2133 /// Table name
2134 pub name: String,
2135 /// Alter operations
2136 pub operations: Vec<AlterOperation>,
2137}
2138
2139/// Single ALTER TABLE operation
2140#[derive(Debug, Clone)]
2141pub enum AlterOperation {
2142 /// ADD COLUMN definition
2143 AddColumn(CreateColumnDef),
2144 /// DROP COLUMN name
2145 DropColumn(String),
2146 /// RENAME COLUMN from TO to
2147 RenameColumn { from: String, to: String },
2148 /// `ATTACH PARTITION child FOR VALUES ...` (Phase 2.2 PG parity).
2149 ///
2150 /// Binds an existing child table to the parent partitioned table.
2151 /// The `bound` string captures the raw bound expression so the
2152 /// runtime can round-trip it back into `red_config` without a
2153 /// dedicated per-kind AST.
2154 AttachPartition {
2155 child: String,
2156 /// Human-readable bound string, e.g. `FROM (2024-01-01) TO (2025-01-01)`
2157 /// or `IN (1, 2, 3)` or `WITH (MODULUS 4, REMAINDER 0)`.
2158 bound: String,
2159 },
2160 /// `DETACH PARTITION child`
2161 DetachPartition { child: String },
2162 /// `ENABLE ROW LEVEL SECURITY` (Phase 2.5 PG parity).
2163 ///
2164 /// Flips the table into RLS-enforced mode. Reads against the table
2165 /// will be filtered by every matching `CREATE POLICY` (for the
2166 /// current role) combined with `AND`.
2167 EnableRowLevelSecurity,
2168 /// `DISABLE ROW LEVEL SECURITY` — disables enforcement; policies
2169 /// remain defined but are ignored until re-enabled.
2170 DisableRowLevelSecurity,
2171 /// `ENABLE TENANCY ON (col)` (Phase 2.5.4 PG parity-ish).
2172 ///
2173 /// Retrofit a tenant-scoped declaration onto an existing table —
2174 /// registers the column, installs the auto `__tenant_iso` RLS
2175 /// policy, and flips RLS on. Equivalent to re-running
2176 /// `CREATE TABLE ... TENANT BY (col)` minus the schema creation.
2177 EnableTenancy { column: String },
2178 /// `DISABLE TENANCY` — tears down the auto-policy and clears the
2179 /// tenancy registration. User-defined policies on the table are
2180 /// untouched; RLS stays enabled if any survive.
2181 DisableTenancy,
2182 /// `SET APPEND_ONLY = true|false` — flips the catalog flag.
2183 /// Setting `true` rejects all future UPDATE/DELETE at parse-time
2184 /// guard; setting `false` re-enables them. Existing rows are
2185 /// untouched either way — this is a purely declarative switch.
2186 SetAppendOnly(bool),
2187 /// `SET VERSIONED = true|false` — opt the table into (or out of)
2188 /// Git-for-Data. Enables merge / diff / AS OF semantics against
2189 /// this collection. Works retroactively: previously-created
2190 /// rows become part of the history accessible via AS OF as long
2191 /// as their xmin is still pinned by an existing commit.
2192 SetVersioned(bool),
2193 /// `ENABLE EVENTS ...` — install or re-enable table event subscription metadata.
2194 EnableEvents(reddb_types::catalog::SubscriptionDescriptor),
2195 /// `DISABLE EVENTS` — mark all table event subscriptions disabled.
2196 DisableEvents,
2197 /// `ADD SUBSCRIPTION name TO queue [REDACT (...)] [WHERE ...]` — add a named subscription.
2198 AddSubscription {
2199 name: String,
2200 descriptor: reddb_types::catalog::SubscriptionDescriptor,
2201 },
2202 /// `DROP SUBSCRIPTION name` — remove a named subscription by name.
2203 DropSubscription { name: String },
2204 /// Issue #522 — `ALTER COLLECTION name ADD SIGNER 'hex_pubkey'`.
2205 /// Appends the key to the per-collection signer registry and
2206 /// records an `Add` entry on the `signer_history` audit log.
2207 AddSigner { pubkey: [u8; 32] },
2208 /// Issue #522 — `ALTER COLLECTION name REVOKE SIGNER 'hex_pubkey'`.
2209 /// Removes the key from the *currently allowed* set and records a
2210 /// `Revoke` entry. Past rows signed by `pubkey` remain readable
2211 /// and re-verifiable — only future inserts are rejected.
2212 RevokeSigner { pubkey: [u8; 32] },
2213 /// Issue #580 — `ALTER COLLECTION name SET RETENTION <duration>`.
2214 /// Stores a declarative retention policy on the collection contract.
2215 /// Enforcement is lazy-on-scan: reads silently filter out rows older
2216 /// than `now - duration_ms` by the collection's timestamp column.
2217 SetRetention { duration_ms: u64 },
2218 /// Issue #580 — `ALTER COLLECTION name UNSET RETENTION`.
2219 /// Removes the policy. Previously-hidden expired rows become
2220 /// readable again — the slice never physically dropped them.
2221 UnsetRetention,
2222 /// Issue #801 — `ALTER GRAPH name ADD ANALYTICS (<output> [opts] [, ...])`.
2223 /// Idempotently enables analytics outputs on an existing graph's
2224 /// `analytics_config` without recreating the collection. Adding an
2225 /// already-enabled output is a no-op (no error, no duplicate state);
2226 /// the next read of `<graph>.<output>` materializes on demand.
2227 AddAnalytics(Vec<reddb_types::catalog::AnalyticsViewDescriptor>),
2228 /// Issue #801 — `ALTER GRAPH name DROP ANALYTICS <output>`.
2229 /// Removes the output from `analytics_config`; the next read of
2230 /// `<graph>.<output>` no longer resolves. Dropping an output that is
2231 /// not currently enabled is a clean error (handled in the executor).
2232 DropAnalytics(reddb_types::catalog::AnalyticsOutput),
2233}
2234
2235// ============================================================================
2236// Shared Types
2237// ============================================================================
2238
2239/// Column/field projection
2240#[derive(Debug, Clone, PartialEq)]
2241pub enum Projection {
2242 /// Select all columns (*)
2243 All,
2244 /// Single column by name
2245 Column(String),
2246 /// Column with alias
2247 Alias(String, String),
2248 /// Function call (name, args)
2249 Function(String, Vec<Projection>),
2250 /// Expression with optional alias
2251 Expression(Box<Filter>, Option<String>),
2252 /// Field reference (for graph properties)
2253 Field(FieldRef, Option<String>),
2254 /// Window function call: `fn(args) OVER (PARTITION BY ... ORDER BY ...
2255 /// [frame])`. Carries the window specification as a sibling so the
2256 /// planner can lower it without re-parsing. No runtime in slice 7a —
2257 /// the analytics executor lands in a subsequent slice (issue #589
2258 /// follow-ups). See `super::WindowSpec`.
2259 Window {
2260 name: String,
2261 args: Vec<Projection>,
2262 window: Box<super::WindowSpec>,
2263 alias: Option<String>,
2264 },
2265}
2266
2267impl Projection {
2268 /// Create a projection from a field reference
2269 pub fn from_field(field: FieldRef) -> Self {
2270 Projection::Field(field, None)
2271 }
2272
2273 /// Create a column projection
2274 pub fn column(name: &str) -> Self {
2275 Projection::Column(name.to_string())
2276 }
2277
2278 /// Create an aliased projection
2279 pub fn with_alias(column: &str, alias: &str) -> Self {
2280 Projection::Alias(column.to_string(), alias.to_string())
2281 }
2282}
2283
2284/// Filter condition
2285#[derive(Debug, Clone, PartialEq)]
2286pub enum Filter {
2287 /// Comparison: field op value
2288 Compare {
2289 field: FieldRef,
2290 op: CompareOp,
2291 value: Value,
2292 },
2293 /// Field-to-field comparison: left.field op right.field. Used when
2294 /// WHERE / BETWEEN operands reference another column instead of a
2295 /// literal — the pre-Fase-2-parser-v2 shim for column-to-column
2296 /// predicates. Once the Expr-rewrite lands, this collapses into
2297 /// `Compare { left: Expr, op, right: Expr }`.
2298 CompareFields {
2299 left: FieldRef,
2300 op: CompareOp,
2301 right: FieldRef,
2302 },
2303 /// Expression-to-expression comparison: `lhs op rhs` where either
2304 /// side may be an arbitrary `Expr` tree (function call, CAST,
2305 /// arithmetic, nested CASE). This is the most general compare
2306 /// variant — `Compare` and `CompareFields` stay as fast-path
2307 /// specialisations because the planner / cost model / index
2308 /// selector all pattern-match on the simpler shapes. The parser
2309 /// only emits this variant when a simpler one cannot express the
2310 /// predicate.
2311 CompareExpr {
2312 lhs: super::Expr,
2313 op: CompareOp,
2314 rhs: super::Expr,
2315 },
2316 /// Logical AND
2317 And(Box<Filter>, Box<Filter>),
2318 /// Logical OR
2319 Or(Box<Filter>, Box<Filter>),
2320 /// Logical NOT
2321 Not(Box<Filter>),
2322 /// IS NULL
2323 IsNull(FieldRef),
2324 /// IS NOT NULL
2325 IsNotNull(FieldRef),
2326 /// IN (value1, value2, ...)
2327 In { field: FieldRef, values: Vec<Value> },
2328 /// BETWEEN low AND high
2329 Between {
2330 field: FieldRef,
2331 low: Value,
2332 high: Value,
2333 },
2334 /// LIKE pattern
2335 Like { field: FieldRef, pattern: String },
2336 /// STARTS WITH prefix
2337 StartsWith { field: FieldRef, prefix: String },
2338 /// ENDS WITH suffix
2339 EndsWith { field: FieldRef, suffix: String },
2340 /// CONTAINS substring
2341 Contains { field: FieldRef, substring: String },
2342}
2343
2344impl Filter {
2345 /// Create a comparison filter
2346 pub fn compare(field: FieldRef, op: CompareOp, value: Value) -> Self {
2347 Self::Compare { field, op, value }
2348 }
2349
2350 /// Combine with AND
2351 pub fn and(self, other: Filter) -> Self {
2352 Self::And(Box::new(self), Box::new(other))
2353 }
2354
2355 /// Combine with OR
2356 pub fn or(self, other: Filter) -> Self {
2357 Self::Or(Box::new(self), Box::new(other))
2358 }
2359
2360 /// Negate
2361 // Filter combinator wrapping `self` in `Filter::Not`; unrelated to
2362 // `std::ops::Not`, so that trait is intentionally not implemented.
2363 #[allow(clippy::should_implement_trait)]
2364 pub fn not(self) -> Self {
2365 Self::Not(Box::new(self))
2366 }
2367
2368 /// Bottom-up AST rewrites: OR-of-equalities → IN, AND/OR flatten.
2369 /// Inspired by MongoDB's `MatchExpression::optimize()`.
2370 /// Call on the result of `effective_table_filter()` before evaluation.
2371 pub fn optimize(self) -> Self {
2372 crate::filter_optimizer::optimize(self)
2373 }
2374}
2375
2376/// Comparison operator
2377#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2378pub enum CompareOp {
2379 /// Equal (=)
2380 Eq,
2381 /// Not equal (<> or !=)
2382 Ne,
2383 /// Less than (<)
2384 Lt,
2385 /// Less than or equal (<=)
2386 Le,
2387 /// Greater than (>)
2388 Gt,
2389 /// Greater than or equal (>=)
2390 Ge,
2391}
2392
2393impl fmt::Display for CompareOp {
2394 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2395 match self {
2396 CompareOp::Eq => write!(f, "="),
2397 CompareOp::Ne => write!(f, "<>"),
2398 CompareOp::Lt => write!(f, "<"),
2399 CompareOp::Le => write!(f, "<="),
2400 CompareOp::Gt => write!(f, ">"),
2401 CompareOp::Ge => write!(f, ">="),
2402 }
2403 }
2404}
2405
2406/// Order by clause.
2407///
2408/// Fase 2 migration: `field` is the legacy bare column reference and
2409/// remains populated for back-compat with existing callers (SPARQL /
2410/// Gremlin / Cypher translators, the planner cost model, etc.). The
2411/// new `expr` slot carries an arbitrary `Expr` tree — when present,
2412/// runtime comparators prefer it over `field`, so the parser can
2413/// emit `ORDER BY CAST(a AS INT)`, `ORDER BY a + b * 2`, etc. without
2414/// breaking the rest of the codebase.
2415///
2416/// When `expr` is `None`, the clause behaves exactly like before.
2417/// When `expr` is `Some(Expr::Column(f))`, runtime code may still use
2418/// the legacy path — it's equivalent. Constructors default `expr` to
2419/// `None` so all existing call sites stay source-compatible.
2420#[derive(Debug, Clone)]
2421pub struct OrderByClause {
2422 /// Field to order by. Left populated even when `expr` is set so
2423 /// legacy consumers (planner cardinality estimate, cost model,
2424 /// mode translators) that still pattern-match on `field` keep
2425 /// working during the Fase 2 migration.
2426 pub field: FieldRef,
2427 /// Fase 2 expression-aware sort key. When `Some`, runtime order
2428 /// comparators evaluate this expression per row and sort on the
2429 /// resulting values — unlocks `ORDER BY expr` (Fase 1.6).
2430 pub expr: Option<super::Expr>,
2431 /// Ascending or descending
2432 pub ascending: bool,
2433 /// Nulls first or last
2434 pub nulls_first: bool,
2435}
2436
2437impl OrderByClause {
2438 /// Create ascending order
2439 pub fn asc(field: FieldRef) -> Self {
2440 Self {
2441 field,
2442 expr: None,
2443 ascending: true,
2444 nulls_first: false,
2445 }
2446 }
2447
2448 /// Create descending order
2449 pub fn desc(field: FieldRef) -> Self {
2450 Self {
2451 field,
2452 expr: None,
2453 ascending: false,
2454 nulls_first: true,
2455 }
2456 }
2457
2458 /// Attach an `Expr` sort key to an existing clause. Leaves `field`
2459 /// untouched so back-compat match sites keep their pattern.
2460 pub fn with_expr(mut self, expr: super::Expr) -> Self {
2461 self.expr = Some(expr);
2462 self
2463 }
2464}
2465
2466// ============================================================================
2467// Window OVER clause (issue #589 slice 7a)
2468// ============================================================================
2469//
2470// Syntactic representation of `OVER (PARTITION BY ... ORDER BY ... [frame])`.
2471// Slice 7a: AST + parser only. No runtime / executor wiring.
2472
2473/// Frame unit: `ROWS` (physical row offset) or `RANGE` (logical value
2474/// offset). Slice 7a stores the choice but does not yet differentiate
2475/// at runtime — semantics arrive with the analytics executor.
2476#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2477pub enum WindowFrameUnit {
2478 Rows,
2479 Range,
2480}
2481
2482/// One endpoint of a frame: UNBOUNDED PRECEDING / CURRENT ROW /
2483/// PRECEDING(expr) / FOLLOWING(expr) / UNBOUNDED FOLLOWING.
2484#[derive(Debug, Clone, PartialEq)]
2485pub enum WindowFrameBound {
2486 UnboundedPreceding,
2487 UnboundedFollowing,
2488 CurrentRow,
2489 Preceding(Box<super::Expr>),
2490 Following(Box<super::Expr>),
2491}
2492
2493/// `ROWS|RANGE BETWEEN start AND end` — or the single-bound shorthand
2494/// `ROWS start` (end implied as CURRENT ROW per SQL standard). Slice 7a
2495/// represents both shapes uniformly with `end: Option<...>` so downstream
2496/// code can normalise.
2497#[derive(Debug, Clone, PartialEq)]
2498pub struct WindowFrame {
2499 pub unit: WindowFrameUnit,
2500 pub start: WindowFrameBound,
2501 pub end: Option<WindowFrameBound>,
2502}
2503
2504/// One ORDER BY item inside a window spec. Window order keys are
2505/// expression-based by SQL standard, so we carry an `Expr` directly
2506/// rather than reusing the top-level `OrderByClause` (which still has
2507/// a legacy `FieldRef` slot for the Fase 2 migration).
2508#[derive(Debug, Clone, PartialEq)]
2509pub struct WindowOrderItem {
2510 pub expr: super::Expr,
2511 pub ascending: bool,
2512 pub nulls_first: bool,
2513}
2514
2515/// Full window specification — the AST node behind `OVER (...)`.
2516/// `frame` is `None` when the user did not specify a frame clause; the
2517/// analytics executor will materialise the SQL default (RANGE UNBOUNDED
2518/// PRECEDING AND CURRENT ROW when ORDER BY is present, the full
2519/// partition otherwise) once it lands.
2520#[derive(Debug, Clone, PartialEq, Default)]
2521pub struct WindowSpec {
2522 pub partition_by: Vec<super::Expr>,
2523 pub order_by: Vec<WindowOrderItem>,
2524 pub frame: Option<WindowFrame>,
2525}
2526
2527// ============================================================================
2528// Graph Commands
2529// ============================================================================
2530
2531/// Graph analytics command issued via SQL-like syntax
2532#[derive(Debug, Clone)]
2533pub struct GraphCommandOrderBy {
2534 pub metric: String,
2535 pub ascending: bool,
2536}
2537
2538#[derive(Debug, Clone)]
2539pub enum GraphCommand {
2540 /// GRAPH NEIGHBORHOOD 'source' [DEPTH n] [DIRECTION dir] [EDGES IN ('label', ...)]
2541 Neighborhood {
2542 source: String,
2543 depth: u32,
2544 direction: String,
2545 edge_labels: Option<Vec<String>>,
2546 },
2547 /// GRAPH SHORTEST_PATH 'source' TO 'target' [ALGORITHM alg] [DIRECTION dir] [ORDER BY metric [ASC|DESC]] [LIMIT n]
2548 ShortestPath {
2549 source: String,
2550 target: String,
2551 algorithm: String,
2552 direction: String,
2553 limit: Option<u32>,
2554 order_by: Option<GraphCommandOrderBy>,
2555 },
2556 /// GRAPH TRAVERSE 'source' [STRATEGY bfs|dfs] [DEPTH n] [DIRECTION dir] [EDGES IN ('label', ...)]
2557 Traverse {
2558 source: String,
2559 strategy: String,
2560 depth: u32,
2561 direction: String,
2562 edge_labels: Option<Vec<String>>,
2563 },
2564 /// GRAPH CENTRALITY [ALGORITHM alg] [ORDER BY metric [ASC|DESC]] [LIMIT n]
2565 ///
2566 /// `limit = None` keeps the historical implicit top-100 cap. `Some(n)`
2567 /// caps the returned rows at `n`.
2568 Centrality {
2569 algorithm: String,
2570 limit: Option<u32>,
2571 order_by: Option<GraphCommandOrderBy>,
2572 },
2573 /// GRAPH COMMUNITY [ALGORITHM alg] [MAX_ITERATIONS n] [ORDER BY metric [ASC|DESC]] [LIMIT n] [RETURN ASSIGNMENTS]
2574 ///
2575 /// `return_assignments = false` (default) keeps the historical per-community
2576 /// aggregate shape (`community_id`, `size`). `true` emits one row per node
2577 /// (`node_id`, `community_id`) — the node→community map (#660).
2578 Community {
2579 algorithm: String,
2580 max_iterations: u32,
2581 limit: Option<u32>,
2582 order_by: Option<GraphCommandOrderBy>,
2583 return_assignments: bool,
2584 },
2585 /// GRAPH COMPONENTS [MODE connected|weak|strong] [ORDER BY metric [ASC|DESC]] [LIMIT n]
2586 Components {
2587 mode: String,
2588 limit: Option<u32>,
2589 order_by: Option<GraphCommandOrderBy>,
2590 },
2591 /// GRAPH CYCLES [MAX_LENGTH n]
2592 Cycles { max_length: u32 },
2593 /// GRAPH CLUSTERING
2594 Clustering,
2595 /// GRAPH TOPOLOGICAL_SORT
2596 TopologicalSort,
2597 /// GRAPH PROPERTIES ['<id-or-label>']
2598 ///
2599 /// `source = None` returns graph-wide stats. `source = Some("...")` returns
2600 /// the full property bag of a specific node, resolved via the same label
2601 /// index as `GRAPH NEIGHBORHOOD` / `GRAPH TRAVERSE` (issue #416).
2602 Properties { source: Option<String> },
2603}
2604
2605// ============================================================================
2606// Search Commands
2607// ============================================================================
2608
2609/// Search command issued via SQL-like syntax
2610#[derive(Debug, Clone)]
2611pub enum SearchCommand {
2612 /// SEARCH SIMILAR [v1, v2, ...] | $N | TEXT 'query' [COLLECTION col] [LIMIT n] [MIN_SCORE f] [USING provider]
2613 Similar {
2614 vector: Vec<f32>,
2615 text: Option<String>,
2616 provider: Option<String>,
2617 collection: String,
2618 limit: usize,
2619 min_score: f32,
2620 /// `$N` placeholder for the vector slot. `Some(idx)` when the SQL
2621 /// used `SEARCH SIMILAR $N ...`; the binder substitutes the
2622 /// user-supplied `Value::Vector` and clears this back to `None`.
2623 /// Runtime executors assert this is `None` post-bind.
2624 vector_param: Option<usize>,
2625 /// `$N` placeholder for the `LIMIT` slot (issue #361). The binder
2626 /// substitutes the user-supplied positive integer into `limit`
2627 /// and clears this back to `None`.
2628 limit_param: Option<usize>,
2629 /// `$N` placeholder for the `MIN_SCORE` slot (issue #361). The
2630 /// binder substitutes the user-supplied float into `min_score`
2631 /// and clears this back to `None`.
2632 min_score_param: Option<usize>,
2633 /// `$N` placeholder for `SEARCH SIMILAR TEXT $N` (issue #361).
2634 /// Binder substitutes the user-supplied text into `text` and
2635 /// clears this back to `None`.
2636 text_param: Option<usize>,
2637 },
2638 /// SEARCH TEXT 'query' [COLLECTION col] [LIMIT n] [FUZZY]
2639 Text {
2640 query: String,
2641 collection: Option<String>,
2642 limit: usize,
2643 fuzzy: bool,
2644 /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
2645 /// shape as `SearchCommand::Hybrid::limit_param`; the binder
2646 /// substitutes the user-supplied positive integer into `limit`
2647 /// and clears this back to `None`.
2648 limit_param: Option<usize>,
2649 },
2650 /// SEARCH HYBRID [vector] [TEXT 'query'] COLLECTION col [LIMIT n]
2651 Hybrid {
2652 vector: Option<Vec<f32>>,
2653 query: Option<String>,
2654 collection: String,
2655 limit: usize,
2656 /// `$N` placeholder for the `LIMIT` / `K` slot (issue #361).
2657 /// Same shape as `SearchCommand::Similar::limit_param`; the
2658 /// binder substitutes the user-supplied positive integer and
2659 /// clears this back to `None`.
2660 limit_param: Option<usize>,
2661 },
2662 /// SEARCH MULTIMODAL 'key_or_query' [COLLECTION col] [LIMIT n]
2663 Multimodal {
2664 query: String,
2665 collection: Option<String>,
2666 limit: usize,
2667 /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
2668 /// shape as `SearchCommand::Hybrid::limit_param`; the binder
2669 /// substitutes the user-supplied positive integer into `limit`
2670 /// and clears this back to `None`.
2671 limit_param: Option<usize>,
2672 },
2673 /// SEARCH INDEX index VALUE 'value' [COLLECTION col] [LIMIT n] [EXACT]
2674 Index {
2675 index: String,
2676 value: String,
2677 collection: Option<String>,
2678 limit: usize,
2679 exact: bool,
2680 /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
2681 /// shape as `SearchCommand::Hybrid::limit_param`; the binder
2682 /// substitutes the user-supplied positive integer into `limit`
2683 /// and clears this back to `None`.
2684 limit_param: Option<usize>,
2685 },
2686 /// SEARCH CONTEXT 'query' [FIELD field] [COLLECTION col] [LIMIT n] [DEPTH n]
2687 Context {
2688 query: String,
2689 field: Option<String>,
2690 collection: Option<String>,
2691 limit: usize,
2692 depth: usize,
2693 /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
2694 /// shape as `SearchCommand::Hybrid::limit_param`; the binder
2695 /// substitutes the user-supplied positive integer into `limit`
2696 /// and clears this back to `None`.
2697 limit_param: Option<usize>,
2698 },
2699 /// SEARCH SPATIAL RADIUS lat lon radius_km COLLECTION col COLUMN col [LIMIT n]
2700 SpatialRadius {
2701 center_lat: f64,
2702 center_lon: f64,
2703 radius_km: f64,
2704 collection: String,
2705 column: String,
2706 limit: usize,
2707 /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
2708 /// shape as `SearchCommand::Hybrid::limit_param`; the binder
2709 /// substitutes the user-supplied positive integer into `limit`
2710 /// and clears this back to `None`.
2711 limit_param: Option<usize>,
2712 },
2713 /// SEARCH SPATIAL BBOX min_lat min_lon max_lat max_lon COLLECTION col COLUMN col [LIMIT n]
2714 SpatialBbox {
2715 min_lat: f64,
2716 min_lon: f64,
2717 max_lat: f64,
2718 max_lon: f64,
2719 collection: String,
2720 column: String,
2721 limit: usize,
2722 /// `$N` placeholder for the `LIMIT` slot (issue #361). Same
2723 /// shape as `SearchCommand::Hybrid::limit_param`; the binder
2724 /// substitutes the user-supplied positive integer into `limit`
2725 /// and clears this back to `None`.
2726 limit_param: Option<usize>,
2727 },
2728 /// SEARCH SPATIAL NEAREST lat lon K n COLLECTION col COLUMN col
2729 SpatialNearest {
2730 lat: f64,
2731 lon: f64,
2732 k: usize,
2733 collection: String,
2734 column: String,
2735 /// `$N` placeholder for the `K` slot (issue #361). Same shape
2736 /// as `SearchCommand::Hybrid::limit_param`; the binder
2737 /// substitutes the user-supplied positive integer into `k`
2738 /// and clears this back to `None`.
2739 k_param: Option<usize>,
2740 },
2741}
2742
2743// ============================================================================
2744// Time-Series DDL
2745// ============================================================================
2746
2747/// CREATE TIMESERIES name [RETENTION duration] [CHUNK_SIZE n] [DOWNSAMPLE spec[, spec...]]
2748///
2749/// `CREATE HYPERTABLE` lands on the same AST with `hypertable` populated.
2750/// The TimescaleDB-style syntax (time column + chunk_interval) gives the
2751/// runtime enough to register a `HypertableSpec` alongside the
2752/// underlying collection contract, so chunk routing and TTL sweeps can
2753/// address the table without a separate DDL.
2754#[derive(Debug, Clone)]
2755pub struct CreateTimeSeriesQuery {
2756 pub name: String,
2757 pub retention_ms: Option<u64>,
2758 pub chunk_size: Option<usize>,
2759 pub downsample_policies: Vec<String>,
2760 pub if_not_exists: bool,
2761 /// When `Some`, the DDL was spelled `CREATE HYPERTABLE` and the
2762 /// runtime must register the spec with the hypertable registry.
2763 pub hypertable: Option<HypertableDdl>,
2764 /// `WITH SESSION_KEY <col>` — default partition column for the
2765 /// `SESSIONIZE` operator. Persisted on the collection contract so
2766 /// queries that omit `BY <col>` pick it up. Issue #576 slice 1.
2767 pub session_key: Option<String>,
2768 /// `SESSION_GAP <duration>` — default inactivity gap (ms) for the
2769 /// `SESSIONIZE` operator. Issue #576 slice 1.
2770 pub session_gap_ms: Option<u64>,
2771 /// `COLUMNAR` — activate columnar analytical storage (PRD #850,
2772 /// #911). When true the collection contract is built with
2773 /// `analytical_storage.columnar = true`, so sealing a chunk routes
2774 /// through `seal_chunk_with_config`'s columnar arm and emits an
2775 /// RDCC `ColumnBlock` instead of the row seal.
2776 pub columnar: bool,
2777}
2778
2779/// Hypertable-specific DDL fields — set only when the caller used
2780/// `CREATE HYPERTABLE`.
2781#[derive(Debug, Clone)]
2782pub struct HypertableDdl {
2783 /// Column that carries the nanosecond timestamp axis.
2784 pub time_column: String,
2785 /// Chunk width in nanoseconds.
2786 pub chunk_interval_ns: u64,
2787 /// Per-chunk default TTL in nanoseconds (`None` = no TTL).
2788 pub default_ttl_ns: Option<u64>,
2789}
2790
2791/// DROP TIMESERIES [IF EXISTS] name
2792#[derive(Debug, Clone)]
2793pub struct DropTimeSeriesQuery {
2794 pub name: String,
2795 pub if_exists: bool,
2796}
2797
2798// ============================================================================
2799// Queue DDL & Commands
2800// ============================================================================
2801
2802/// Default `MAX_ATTEMPTS` for `CREATE QUEUE` when omitted.
2803pub const DEFAULT_QUEUE_MAX_ATTEMPTS: u32 = 3;
2804/// Default `LOCK_DEADLINE_MS` for `CREATE QUEUE` when omitted.
2805pub const DEFAULT_QUEUE_LOCK_DEADLINE_MS: u64 = 30_000;
2806/// Default `IN_FLIGHT_CAP_PER_GROUP` for `CREATE QUEUE` when omitted.
2807pub const DEFAULT_QUEUE_IN_FLIGHT_CAP_PER_GROUP: u32 = 10_000;
2808
2809/// CREATE QUEUE name [MAX_SIZE n] [PRIORITY] [WITH TTL duration] [WITH DLQ name]
2810/// [MAX_ATTEMPTS n] [LOCK_DEADLINE_MS n] [IN_FLIGHT_CAP_PER_GROUP n]
2811/// [RETRY_DELAY duration]
2812#[derive(Debug, Clone)]
2813pub struct CreateQueueQuery {
2814 pub name: String,
2815 pub mode: QueueMode,
2816 pub priority: bool,
2817 pub max_size: Option<usize>,
2818 pub ttl_ms: Option<u64>,
2819 pub dlq: Option<String>,
2820 pub max_attempts: u32,
2821 pub lock_deadline_ms: u64,
2822 pub in_flight_cap_per_group: u32,
2823 pub if_not_exists: bool,
2824 /// Default retry delay applied to NACK-requeued messages before they
2825 /// become re-deliverable. `None` means no delay — the released
2826 /// message is immediately available again (pre-#723 behaviour).
2827 /// `Some(ms)` reuses the per-message availability machinery from
2828 /// issue #722 to defer the next delivery attempt. An authorized
2829 /// `NACK ... WITH DELAY <duration>` overrides this per-failure.
2830 pub retry_delay_ms: Option<u64>,
2831}
2832
2833/// ALTER QUEUE name SET <clause>
2834/// MODE [FANOUT|WORK|STANDARD|FIFO]
2835/// MAX_ATTEMPTS n
2836/// LOCK_DEADLINE_MS n
2837/// IN_FLIGHT_CAP_PER_GROUP n
2838/// DLQ name
2839/// RETRY_DELAY duration
2840#[derive(Debug, Clone, Default)]
2841pub struct AlterQueueQuery {
2842 pub name: String,
2843 pub mode: Option<QueueMode>,
2844 pub max_attempts: Option<u32>,
2845 pub lock_deadline_ms: Option<u64>,
2846 pub in_flight_cap_per_group: Option<u32>,
2847 pub dlq: Option<String>,
2848 /// Update the queue's default retry delay (issue #723). `Some(0)`
2849 /// clears the delay back to immediate requeue.
2850 pub retry_delay_ms: Option<u64>,
2851}
2852
2853/// DROP QUEUE [IF EXISTS] name
2854#[derive(Debug, Clone)]
2855pub struct DropQueueQuery {
2856 pub name: String,
2857 pub if_exists: bool,
2858}
2859
2860/// SELECT <columns> FROM QUEUE name [WHERE filter] [LIMIT n]
2861#[derive(Debug, Clone)]
2862pub struct QueueSelectQuery {
2863 pub queue: String,
2864 pub columns: Vec<String>,
2865 pub filter: Option<Filter>,
2866 pub limit: Option<u64>,
2867}
2868
2869/// Which end of the queue
2870#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2871pub enum QueueSide {
2872 Left,
2873 Right,
2874}
2875
2876/// Per-message delayed availability for `QUEUE PUSH` (PRD #718 / #722).
2877///
2878/// `DelayMs` is relative — the runtime resolves it against the push-time
2879/// wall clock. `AtUnixMs` is absolute — the runtime promotes it to
2880/// nanoseconds unchanged. Both ultimately surface to consumers as an
2881/// `available_at_ns` metadata field that delivery paths filter on.
2882#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2883pub enum QueueAvailability {
2884 /// Delay the first delivery by this many milliseconds from push time.
2885 DelayMs(u64),
2886 /// Make the message first-deliverable at this absolute unix-ms instant.
2887 AtUnixMs(u64),
2888}
2889
2890/// Queue operation commands
2891// The largest variant carries an inline `Filter`; boxing it would ripple
2892// to every construction and match site for a marginal stack-size win, so
2893// the size difference is accepted.
2894#[allow(clippy::large_enum_variant)]
2895#[derive(Debug, Clone)]
2896pub enum QueueCommand {
2897 Push {
2898 queue: String,
2899 value: Value,
2900 side: QueueSide,
2901 priority: Option<i32>,
2902 /// Per-message delayed availability (issue #722). `None` means the
2903 /// message is deliverable immediately. `Some(_)` resolves to an
2904 /// `available_at_ns` metadata field at push time; delivery paths
2905 /// (`QUEUE READ`, `QUEUE POP`, `QUEUE READ … WAIT`) refuse to
2906 /// deliver the message until that instant.
2907 available: Option<QueueAvailability>,
2908 },
2909 Pop {
2910 queue: String,
2911 side: QueueSide,
2912 count: usize,
2913 },
2914 Peek {
2915 queue: String,
2916 count: usize,
2917 },
2918 Len {
2919 queue: String,
2920 },
2921 Purge {
2922 queue: String,
2923 },
2924 GroupCreate {
2925 queue: String,
2926 group: String,
2927 },
2928 GroupRead {
2929 queue: String,
2930 group: Option<String>,
2931 consumer: String,
2932 count: usize,
2933 /// Optional blocking-read deadline in milliseconds (PRD #718 slice
2934 /// A: `QUEUE READ … WAIT <duration>`). `None` means classic
2935 /// non-blocking semantics. The runtime currently honors the field
2936 /// synchronously — the actual wait registry lands in slice C.
2937 wait_ms: Option<u64>,
2938 },
2939 Pending {
2940 queue: String,
2941 group: String,
2942 },
2943 Claim {
2944 queue: String,
2945 group: String,
2946 consumer: String,
2947 min_idle_ms: u64,
2948 },
2949 Ack {
2950 queue: String,
2951 // Legacy tuple handle. Empty `group` / `message_id` strings mean
2952 // the request relies solely on `delivery_id`. ADR 0026: when both
2953 // `delivery_id` and the tuple are supplied, `delivery_id` wins.
2954 group: String,
2955 message_id: String,
2956 /// Server-issued opaque base32 delivery handle (ADR 0026). When
2957 /// present, takes precedence over the legacy tuple; the tuple is
2958 /// kept for one minor release as a wire-compat bridge.
2959 delivery_id: Option<String>,
2960 },
2961 Nack {
2962 queue: String,
2963 group: String,
2964 message_id: String,
2965 delivery_id: Option<String>,
2966 /// Per-failure retry delay override (issue #723). `Some(ms)`
2967 /// requests that the failed message become re-deliverable only
2968 /// after `ms` milliseconds; takes precedence over the queue's
2969 /// default `retry_delay_ms`. Authorization is enforced at the
2970 /// runtime layer: requests from a read-only identity are
2971 /// rejected.
2972 delay_ms: Option<u64>,
2973 },
2974 Move {
2975 source: String,
2976 destination: String,
2977 filter: Option<Filter>,
2978 limit: usize,
2979 },
2980}
2981
2982// ============================================================================
2983// Tree DDL & Commands
2984// ============================================================================
2985
2986#[derive(Debug, Clone)]
2987pub struct TreeNodeSpec {
2988 pub label: String,
2989 pub node_type: Option<String>,
2990 pub properties: Vec<(String, Value)>,
2991 pub metadata: Vec<(String, Value)>,
2992 pub max_children: Option<usize>,
2993}
2994
2995#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2996pub enum TreePosition {
2997 First,
2998 Last,
2999 Index(usize),
3000}
3001
3002#[derive(Debug, Clone)]
3003pub struct CreateTreeQuery {
3004 pub collection: String,
3005 pub name: String,
3006 pub root: TreeNodeSpec,
3007 pub default_max_children: usize,
3008 pub if_not_exists: bool,
3009}
3010
3011#[derive(Debug, Clone)]
3012pub struct DropTreeQuery {
3013 pub collection: String,
3014 pub name: String,
3015 pub if_exists: bool,
3016}
3017
3018#[derive(Debug, Clone)]
3019pub enum TreeCommand {
3020 Insert {
3021 collection: String,
3022 tree_name: String,
3023 parent_id: u64,
3024 node: TreeNodeSpec,
3025 position: TreePosition,
3026 },
3027 Move {
3028 collection: String,
3029 tree_name: String,
3030 node_id: u64,
3031 parent_id: u64,
3032 position: TreePosition,
3033 },
3034 Delete {
3035 collection: String,
3036 tree_name: String,
3037 node_id: u64,
3038 },
3039 Validate {
3040 collection: String,
3041 tree_name: String,
3042 },
3043 Rebalance {
3044 collection: String,
3045 tree_name: String,
3046 dry_run: bool,
3047 },
3048}
3049
3050// ============================================================================
3051// KV DSL Commands
3052// ============================================================================
3053
3054/// KV verb commands: `KV PUT key = value [EXPIRE n] [IF NOT EXISTS]`, `KV GET key`, `KV DELETE key`
3055#[derive(Debug, Clone)]
3056pub enum KvCommand {
3057 Put {
3058 model: CollectionModel,
3059 collection: String,
3060 key: String,
3061 value: Value,
3062 /// TTL in milliseconds (from EXPIRE clause)
3063 ttl_ms: Option<u64>,
3064 tags: Vec<String>,
3065 if_not_exists: bool,
3066 },
3067 InvalidateTags {
3068 collection: String,
3069 tags: Vec<String>,
3070 },
3071 Get {
3072 model: CollectionModel,
3073 collection: String,
3074 key: String,
3075 },
3076 Unseal {
3077 collection: String,
3078 key: String,
3079 version: Option<i64>,
3080 },
3081 Rotate {
3082 collection: String,
3083 key: String,
3084 value: Value,
3085 tags: Vec<String>,
3086 },
3087 History {
3088 collection: String,
3089 key: String,
3090 },
3091 List {
3092 model: CollectionModel,
3093 collection: String,
3094 prefix: Option<String>,
3095 limit: Option<usize>,
3096 offset: usize,
3097 as_json: bool,
3098 },
3099 Purge {
3100 collection: String,
3101 key: String,
3102 },
3103 Watch {
3104 model: CollectionModel,
3105 collection: String,
3106 key: String,
3107 prefix: bool,
3108 from_lsn: Option<u64>,
3109 },
3110 Delete {
3111 model: CollectionModel,
3112 collection: String,
3113 key: String,
3114 },
3115 /// `KV INCR key [BY n] [EXPIRE dur]` — atomic increment; negative `by` = decrement.
3116 Incr {
3117 model: CollectionModel,
3118 collection: String,
3119 key: String,
3120 /// Step value; negative for DECR. Defaults to 1.
3121 by: i64,
3122 ttl_ms: Option<u64>,
3123 },
3124 /// `KV CAS key EXPECT <expected|NULL> SET <new> [EXPIRE dur]` — compare-and-set.
3125 ///
3126 /// `expected = None` means `EXPECT NULL` (key must be absent).
3127 Cas {
3128 model: CollectionModel,
3129 collection: String,
3130 key: String,
3131 /// The value the caller expects to be current; `None` = key must be absent.
3132 expected: Option<Value>,
3133 new_value: Value,
3134 ttl_ms: Option<u64>,
3135 },
3136}
3137
3138#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3139pub enum ConfigValueType {
3140 Bool,
3141 Int,
3142 String,
3143 Url,
3144 Object,
3145 Array,
3146}
3147
3148impl ConfigValueType {
3149 pub fn as_str(self) -> &'static str {
3150 match self {
3151 Self::Bool => "bool",
3152 Self::Int => "int",
3153 Self::String => "string",
3154 Self::Url => "url",
3155 Self::Object => "object",
3156 Self::Array => "array",
3157 }
3158 }
3159
3160 pub fn parse(input: &str) -> Option<Self> {
3161 match input.to_ascii_lowercase().as_str() {
3162 "bool" | "boolean" => Some(Self::Bool),
3163 "int" | "integer" => Some(Self::Int),
3164 "string" | "str" | "text" => Some(Self::String),
3165 "url" => Some(Self::Url),
3166 "object" | "json_object" => Some(Self::Object),
3167 "array" | "list" => Some(Self::Array),
3168 _ => None,
3169 }
3170 }
3171}
3172
3173#[derive(Debug, Clone)]
3174pub enum ConfigCommand {
3175 Put {
3176 collection: String,
3177 key: String,
3178 value: Value,
3179 value_type: Option<ConfigValueType>,
3180 tags: Vec<String>,
3181 },
3182 Get {
3183 collection: String,
3184 key: String,
3185 },
3186 Resolve {
3187 collection: String,
3188 key: String,
3189 },
3190 Rotate {
3191 collection: String,
3192 key: String,
3193 value: Value,
3194 value_type: Option<ConfigValueType>,
3195 tags: Vec<String>,
3196 },
3197 Delete {
3198 collection: String,
3199 key: String,
3200 },
3201 History {
3202 collection: String,
3203 key: String,
3204 },
3205 List {
3206 collection: String,
3207 prefix: Option<String>,
3208 limit: Option<usize>,
3209 offset: usize,
3210 },
3211 Watch {
3212 collection: String,
3213 key: String,
3214 prefix: bool,
3215 from_lsn: Option<u64>,
3216 },
3217 InvalidVolatileOperation {
3218 operation: String,
3219 collection: String,
3220 key: Option<String>,
3221 },
3222}
3223
3224#[cfg(test)]
3225mod tests {
3226 use super::*;
3227 use crate::ast::{Expr, Span};
3228
3229 fn table_expr(name: &str) -> QueryExpr {
3230 QueryExpr::Table(TableQuery::new(name))
3231 }
3232
3233 #[test]
3234 fn policy_target_kind_identifiers_cover_all_variants() {
3235 assert_eq!(PolicyTargetKind::Table.as_ident(), "table");
3236 assert_eq!(PolicyTargetKind::Nodes.as_ident(), "nodes");
3237 assert_eq!(PolicyTargetKind::Edges.as_ident(), "edges");
3238 assert_eq!(PolicyTargetKind::Vectors.as_ident(), "vectors");
3239 assert_eq!(PolicyTargetKind::Messages.as_ident(), "messages");
3240 assert_eq!(PolicyTargetKind::Points.as_ident(), "points");
3241 assert_eq!(PolicyTargetKind::Documents.as_ident(), "documents");
3242 }
3243
3244 #[test]
3245 fn index_method_display_names_are_sql_keywords() {
3246 assert_eq!(IndexMethod::BTree.to_string(), "BTREE");
3247 assert_eq!(IndexMethod::Hash.to_string(), "HASH");
3248 assert_eq!(IndexMethod::Bitmap.to_string(), "BITMAP");
3249 assert_eq!(IndexMethod::RTree.to_string(), "RTREE");
3250 }
3251
3252 #[test]
3253 fn table_query_defaults_and_subquery_source() {
3254 let query = TableQuery::new("hosts");
3255 assert_eq!(query.table, "hosts");
3256 assert!(query.source.is_none());
3257 assert!(query.alias.is_none());
3258 assert!(query.select_items.is_empty());
3259 assert!(!query.distinct);
3260
3261 let subquery = table_expr("inner");
3262 let wrapped = TableQuery::from_subquery(subquery, Some("h".to_string()));
3263 assert_eq!(wrapped.table, "__subq_h");
3264 assert_eq!(wrapped.alias.as_deref(), Some("h"));
3265 assert!(matches!(wrapped.source, Some(TableSource::Subquery(_))));
3266
3267 let anonymous = TableQuery::from_subquery(table_expr("inner"), None);
3268 assert_eq!(anonymous.table, "__subq_anon");
3269 assert!(anonymous.alias.is_none());
3270 }
3271
3272 #[test]
3273 fn graph_pattern_query_and_components_builders_set_fields() {
3274 let node = NodePattern::new("h").of_label("Host").with_property(
3275 "os",
3276 CompareOp::Eq,
3277 Value::text("linux"),
3278 );
3279 assert_eq!(node.alias, "h");
3280 assert_eq!(node.node_label.as_deref(), Some("Host"));
3281 assert_eq!(node.properties.len(), 1);
3282
3283 let edge = EdgePattern::new("h", "s")
3284 .alias("r")
3285 .of_label("HAS_SERVICE")
3286 .direction(EdgeDirection::Incoming)
3287 .hops(2, 4);
3288 assert_eq!(edge.alias.as_deref(), Some("r"));
3289 assert_eq!(edge.edge_label.as_deref(), Some("HAS_SERVICE"));
3290 assert_eq!(edge.direction, EdgeDirection::Incoming);
3291 assert_eq!((edge.min_hops, edge.max_hops), (2, 4));
3292
3293 let pattern = GraphPattern::new().node(node).edge(edge);
3294 assert_eq!(pattern.nodes.len(), 1);
3295 assert_eq!(pattern.edges.len(), 1);
3296
3297 let graph = GraphQuery::new(pattern).alias("g");
3298 assert_eq!(graph.alias.as_deref(), Some("g"));
3299 assert!(graph.filter.is_none());
3300 assert!(graph.return_.is_empty());
3301 }
3302
3303 #[test]
3304 fn join_condition_and_join_query_defaults() {
3305 let left = FieldRef::column("hosts", "id");
3306 let right = FieldRef::node_id("h");
3307 let condition = JoinCondition::new(left.clone(), right.clone());
3308 assert_eq!(condition.left_field, left);
3309 assert_eq!(condition.right_field, right);
3310
3311 let join = JoinQuery::new(
3312 table_expr("hosts"),
3313 table_expr("services"),
3314 condition.clone(),
3315 )
3316 .join_type(JoinType::LeftOuter);
3317 assert!(matches!(*join.left, QueryExpr::Table(_)));
3318 assert!(matches!(*join.right, QueryExpr::Table(_)));
3319 assert_eq!(join.join_type, JoinType::LeftOuter);
3320 assert_eq!(join.on.left_field, condition.left_field);
3321 assert!(join.return_items.is_empty());
3322 }
3323
3324 #[test]
3325 fn field_ref_projection_filter_and_order_builders() {
3326 let column = FieldRef::column("hosts", "ip");
3327 let node_prop = FieldRef::node_prop("h", "os");
3328 let edge_prop = FieldRef::edge_prop("r", "weight");
3329 assert!(matches!(column, FieldRef::TableColumn { .. }));
3330 assert!(matches!(node_prop, FieldRef::NodeProperty { .. }));
3331 assert!(matches!(edge_prop, FieldRef::EdgeProperty { .. }));
3332
3333 assert!(matches!(
3334 Projection::from_field(column.clone()),
3335 Projection::Field(_, None)
3336 ));
3337 assert!(matches!(
3338 Projection::column("ip"),
3339 Projection::Column(ref name) if name == "ip"
3340 ));
3341 assert!(matches!(
3342 Projection::with_alias("ip", "addr"),
3343 Projection::Alias(ref column, ref alias) if column == "ip" && alias == "addr"
3344 ));
3345
3346 let eq = Filter::compare(column.clone(), CompareOp::Eq, Value::text("127.0.0.1"));
3347 let gt = Filter::compare(node_prop, CompareOp::Gt, Value::Integer(7));
3348 let combined = eq.clone().and(gt.clone()).or(eq.clone().not());
3349 assert!(matches!(combined, Filter::Or(_, _)));
3350 assert!(matches!(gt.optimize(), Filter::Compare { .. }));
3351
3352 assert_eq!(CompareOp::Eq.to_string(), "=");
3353 assert_eq!(CompareOp::Ne.to_string(), "<>");
3354 assert_eq!(CompareOp::Lt.to_string(), "<");
3355 assert_eq!(CompareOp::Le.to_string(), "<=");
3356 assert_eq!(CompareOp::Gt.to_string(), ">");
3357 assert_eq!(CompareOp::Ge.to_string(), ">=");
3358
3359 let asc = OrderByClause::asc(column.clone());
3360 assert!(asc.ascending);
3361 assert!(!asc.nulls_first);
3362 assert!(asc.expr.is_none());
3363
3364 let desc = OrderByClause::desc(column).with_expr(Expr::lit(Value::Integer(1)));
3365 assert!(!desc.ascending);
3366 assert!(desc.nulls_first);
3367 assert!(desc.expr.is_some());
3368 }
3369
3370 #[test]
3371 fn path_and_node_selector_builders_set_expected_defaults() {
3372 let from = NodeSelector::by_id("a");
3373 let to = NodeSelector::by_row("hosts", 42);
3374 let path = PathQuery::new(from, to).alias("p").via_label("CONNECTS_TO");
3375 assert_eq!(path.alias.as_deref(), Some("p"));
3376 assert_eq!(path.via, vec!["CONNECTS_TO"]);
3377 assert_eq!(path.max_length, 10);
3378 assert!(path.filter.is_none());
3379
3380 assert!(matches!(NodeSelector::by_id("n1"), NodeSelector::ById(id) if id == "n1"));
3381 assert!(matches!(
3382 NodeSelector::by_label("Host"),
3383 NodeSelector::ByType { node_label, filter: None } if node_label == "Host"
3384 ));
3385 assert!(matches!(
3386 NodeSelector::by_row("hosts", 7),
3387 NodeSelector::ByRow { table, row_id } if table == "hosts" && row_id == 7
3388 ));
3389 }
3390
3391 #[test]
3392 fn vector_and_hybrid_builders_set_options() {
3393 let literal = VectorSource::literal(vec![0.1, 0.2]);
3394 assert!(matches!(literal, VectorSource::Literal(ref values) if values == &[0.1, 0.2]));
3395 assert!(matches!(VectorSource::text("ssh"), VectorSource::Text(ref text) if text == "ssh"));
3396 assert!(matches!(
3397 VectorSource::reference("embeddings", 9),
3398 VectorSource::Reference { collection, vector_id }
3399 if collection == "embeddings" && vector_id == 9
3400 ));
3401
3402 let vector = VectorQuery::new("embeddings", VectorSource::text("ssh"))
3403 .limit(3)
3404 .with_filter(MetadataFilter::eq("source", "nmap"))
3405 .with_vectors()
3406 .min_similarity(0.8)
3407 .alias("sim");
3408 assert_eq!(vector.collection, "embeddings");
3409 assert_eq!(vector.k, 3);
3410 assert!(vector.filter.is_some());
3411 assert!(vector.include_vectors);
3412 assert!(vector.include_metadata);
3413 assert_eq!(vector.threshold, Some(0.8));
3414 assert_eq!(vector.alias.as_deref(), Some("sim"));
3415
3416 let hybrid = HybridQuery::new(table_expr("hosts"), vector)
3417 .with_fusion(FusionStrategy::RRF { k: 60 })
3418 .limit(5)
3419 .alias("hy");
3420 assert!(matches!(*hybrid.structured, QueryExpr::Table(_)));
3421 assert_eq!(hybrid.limit, Some(5));
3422 assert_eq!(hybrid.alias.as_deref(), Some("hy"));
3423 assert!(matches!(hybrid.fusion, FusionStrategy::RRF { k: 60 }));
3424 }
3425
3426 #[test]
3427 fn window_spec_structs_are_constructible() {
3428 let order = WindowOrderItem {
3429 expr: Expr::lit(Value::Integer(1)),
3430 ascending: false,
3431 nulls_first: true,
3432 };
3433 let frame = WindowFrame {
3434 unit: WindowFrameUnit::Rows,
3435 start: WindowFrameBound::UnboundedPreceding,
3436 end: Some(WindowFrameBound::CurrentRow),
3437 };
3438 let spec = WindowSpec {
3439 partition_by: vec![Expr::lit(Value::text("tenant"))],
3440 order_by: vec![order],
3441 frame: Some(frame),
3442 };
3443 assert_eq!(spec.partition_by.len(), 1);
3444 assert_eq!(spec.order_by.len(), 1);
3445 assert!(matches!(
3446 spec.frame,
3447 Some(WindowFrame {
3448 unit: WindowFrameUnit::Rows,
3449 ..
3450 })
3451 ));
3452
3453 let default = WindowSpec::default();
3454 assert!(default.partition_by.is_empty());
3455 assert!(default.order_by.is_empty());
3456 assert!(default.frame.is_none());
3457
3458 let _ = Span::synthetic();
3459 }
3460
3461 #[test]
3462 fn config_value_type_aliases_and_display_names() {
3463 for (input, expected, as_str) in [
3464 ("bool", ConfigValueType::Bool, "bool"),
3465 ("boolean", ConfigValueType::Bool, "bool"),
3466 ("int", ConfigValueType::Int, "int"),
3467 ("integer", ConfigValueType::Int, "int"),
3468 ("str", ConfigValueType::String, "string"),
3469 ("text", ConfigValueType::String, "string"),
3470 ("url", ConfigValueType::Url, "url"),
3471 ("json_object", ConfigValueType::Object, "object"),
3472 ("list", ConfigValueType::Array, "array"),
3473 ] {
3474 let parsed = ConfigValueType::parse(input).expect("known type");
3475 assert_eq!(parsed, expected);
3476 assert_eq!(parsed.as_str(), as_str);
3477 }
3478 assert_eq!(ConfigValueType::parse("bogus"), None);
3479 }
3480}
3481
3482// ============================================================================
3483// Builders (Fluent API)
3484// ============================================================================