Skip to main content

surrealdb_ast/
lib.rs

1//! # Surrealdb AST
2//!
3//! This crate is the internal library of SurrealDB. It contains the implemention of the surrealql
4//! abstract syntax tree.
5//!
6//! <section class="warning">
7//! <h3>Unstable!</h3>
8//! This crate is <b>SurrealDB internal API</b>. It does not adhere to SemVer and its API is
9//! free to change and break code even between patch versions. If you are looking for a stable
10//! interface to the SurrealDB library please have a look at
11//! <a href="https://crates.io/crates/surrealdb">the Rust SDK</a>.
12//! </section>
13
14use std::ops::Bound;
15use std::time::Duration;
16
17use chrono::{DateTime as BaseDateTime, Utc};
18use common::ids::IdSet;
19use common::span::Span;
20pub use rust_decimal::Decimal;
21pub use uuid::Uuid;
22pub type DateTime = BaseDateTime<Utc>;
23
24mod mac;
25mod types;
26pub mod vis;
27//mod visit;
28
29pub use types::{AstSpan, Node, NodeId, NodeList, NodeListId, Spanned, UniqueNode};
30
31use crate::mac::{ast_type, impl_vis_debug, impl_vis_type};
32use crate::types::library;
33
34type NodeSet<T> = IdSet<u32, T>;
35
36// Macro implementing the syntax sugar for working with the ast.
37library! {
38	/// Type containing all the possible node types.
39	#[derive(Debug)]
40	Library{
41		top_level_expr: Vec<TopLevelExpr>,
42		top_level_exprs: Vec<NodeList<TopLevelExpr>>,
43
44		transactions: Vec<Transaction>,
45
46		kill: Vec<Kill>,
47		use_stmt: Vec<Use>,
48		option_stmt: Vec<OptionStmt>,
49
50		if_stmt: Vec<If>,
51		for_stmt: Vec<For>,
52		let_stmt: Vec<Let>,
53		return_stmt: Vec<Return>,
54		info_stmt: Vec<Info>,
55		show_stmt: Vec<Show>,
56		create_stmt: Vec<Create>,
57		update_stmt: Vec<Update>,
58		upsert_stmt: Vec<Upsert>,
59		delete_stmt: Vec<Delete>,
60		relate_stmt: Vec<Relate>,
61		select_stmt: Vec<Select>,
62		insert_stmt: Vec<Insert>,
63		rebuild_stmt: Vec<Rebuild>,
64		access_stmt: Vec<Access>,
65
66		define_ns_stmt: Vec<DefineNamespace>,
67		define_db_stmt: Vec<DefineDatabase>,
68		define_function_stmt: Vec<DefineFunction>,
69		define_table_stmt: Vec<DefineTable>,
70		define_module: Vec<DefineModule>,
71		define_param: Vec<DefineParam>,
72		define_event: Vec<DefineEvent>,
73		define_field: Vec<DefineField>,
74		define_index: Vec<DefineIndex>,
75		define_bucket: Vec<DefineBucket>,
76		define_sequence: Vec<DefineSequence>,
77		define_config: Vec<DefineConfig>,
78		define_user: Vec<DefineUser>,
79		define_access: Vec<DefineAccess>,
80
81		remove_ns_stmt: Vec<RemoveNamespace>,
82		remove_db_stmt: Vec<RemoveDatabase>,
83		remove_function_stmt: Vec<RemoveFunction>,
84		remove_table_stmt: Vec<RemoveTable>,
85		remove_module: Vec<RemoveModule>,
86		remove_param: Vec<RemoveParam>,
87		remove_event: Vec<RemoveEvent>,
88		remove_field: Vec<RemoveField>,
89		remove_index: Vec<RemoveIndex>,
90		remove_bucket: Vec<RemoveBucket>,
91		remove_sequence: Vec<RemoveSequence>,
92		remove_user: Vec<RemoveUser>,
93		remove_access: Vec<RemoveAccess>,
94		remove_analyzer: Vec<RemoveAnalyzer>,
95		remove_api: Vec<RemoveApi>,
96		remove_config: Vec<RemoveConfig>,
97
98		alter_system: Vec<AlterSystem>,
99		alter_ns_stmt: Vec<AlterNamespace>,
100		alter_db_stmt: Vec<AlterDatabase>,
101		alter_table_stmt: Vec<AlterTable>,
102		alter_event_stmt: Vec<AlterEvent>,
103		alter_param_stmt: Vec<AlterParam>,
104		alter_field_stmt: Vec<AlterField>,
105		alter_index_stmt: Vec<AlterIndex>,
106		alter_sequence_stmt: Vec<AlterSequence>,
107		alter_bucket_stmt: Vec<AlterBucket>,
108		alter_analyzer_stmt: Vec<AlterAnalyzer>,
109		alter_function_stmt: Vec<AlterFunction>,
110		alter_user_stmt: Vec<AlterUser>,
111		alter_access_stmt: Vec<AlterAccess>,
112		alter_api_stmt: Vec<AlterApi>,
113		alter_config_stmt: Vec<AlterConfig>,
114
115		explain_stmt: Vec<Explain>,
116
117		order: Vec<Order>,
118		orders: Vec<NodeList<Order>>,
119
120		filter: Vec<Filter>,
121		filters: Vec<NodeList<Filter>>,
122		define_analyzer: Vec<DefineAnalyzer>,
123
124		api_action: Vec<ApiAction>,
125		api_middleware: Vec<ApiMiddleware>,
126		api_middlewares: Vec<NodeList<ApiMiddleware>>,
127		define_api: Vec<DefineApi>,
128
129		expr: Vec<Expr>,
130		exprs: Vec<NodeList<Expr>>,
131		exprs_id: Vec<NodeListId<Expr>>,
132		exprs_ids: Vec<NodeList<NodeListId<Expr>>>,
133
134		block: Vec<Block>,
135
136		builtin: Vec<Builtin>,
137		floats: Vec<Spanned<f64>>,
138		integer: Vec<Integer>,
139		decimal: Vec<Spanned<Decimal>>,
140		uuid: Vec<Spanned<Uuid>>,
141		datetime: Vec<Spanned<DateTime>>,
142		duration: Vec<Spanned<Duration>>,
143		str: Vec<StringLit>,
144		bytes: Vec<BytesLit>,
145		files: Vec<FileLit>,
146		regex: Vec<Regex>,
147		js_function: Vec<JsFunction>,
148
149		record_id: Vec<RecordId>,
150		record_id_key: Vec<RecordIdKey>,
151		record_id_key_range: Vec<RecordIdKeyRange>,
152
153		mock: Vec<Mock>,
154		closure: Vec<Closure>,
155
156		point: Vec<Point>,
157		array: Vec<Array>,
158		set: Vec<Set>,
159		object: Vec<Object>,
160		object_entry: Vec<ObjectEntry>,
161		object_entrys: Vec<NodeList<ObjectEntry>>,
162
163		binary: Vec<BinaryExpr>,
164		postfix: Vec<PostfixExpr>,
165		prefix: Vec<PrefixExpr>,
166
167		lookup_subject: Vec<LookupSubject>,
168		lookup_subjects: Vec<NodeList<LookupSubject>>,
169		lookup: Vec<Lookup>,
170		recurse: Vec<Recurse>,
171		idiom: Vec<IdiomExpr>,
172
173		destructure: Vec<Destructure>,
174		destructures: Vec<NodeList<Destructure>>,
175
176		place: Vec<Place>,
177		places: Vec<NodeList<Place>>,
178		present_place: Vec<PresentPlace>,
179		present_places: Vec<NodeList<PresentPlace>>,
180
181		assignment: Vec<Assignment>,
182		assignments: Vec<NodeList<Assignment>>,
183
184		field: Vec<Fields>,
185		list_selector: Vec<ListSelector>,
186		list_selectors: Vec<NodeList<ListSelector>>,
187		selector: Vec<Selector>,
188
189		output: Vec<Output>,
190
191		parameter: Vec<Parameter>,
192		parameters: Vec<NodeList<Parameter>>,
193
194		type_: Vec<Type>,
195		types: Vec<NodeList<Type>>,
196		prime_type: Vec<PrimeType>,
197		prime_types: Vec<NodeList<PrimeType>>,
198		lit_array_type: Vec<LitArrayType>,
199		lit_object_type: Vec<LitObjectType>,
200		lit_object_entry_type: Vec<LitObjectTypeEntry>,
201		lit_object_entry_types: Vec<NodeList<LitObjectTypeEntry>>,
202		array_like_type: Vec<ArrayLikeType>,
203		geometry_type: Vec<GeometryType>,
204		geometry_sub_type: Vec<GeometrySubType>,
205		geometry_sub_types: Vec<NodeList<GeometrySubType>>,
206		record_table_type: Vec<IdentListType>,
207
208		path: Vec<Path>,
209		paths: Vec<NodeList<Path>>,
210		path_segment: Vec<PathSegment>,
211		path_segments: Vec<NodeList<PathSegment>>,
212
213		params: Vec<Param>,
214		ident: Vec<Ident>,
215		idents: Vec<NodeList<Ident>>,
216
217		span: Vec<Span>,
218
219		#[set]
220		strings: NodeSet<String>,
221	}
222}
223pub type Ast = types::Ast<Library>;
224
225impl UniqueNode for String {}
226impl Node for f64 {}
227impl Node for Decimal {}
228impl Node for Uuid {}
229impl Node for DateTime {}
230impl Node for Duration {}
231
232ast_type! {
233	pub struct Query {
234		pub exprs: Option<NodeListId<TopLevelExpr>>,
235	}
236}
237
238ast_type! {
239	pub enum TopLevelExpr {
240		Transaction(NodeId<Transaction>),
241		Use(NodeId<Use>),
242		Option(NodeId<OptionStmt>),
243		Kill(NodeId<Kill>),
244		Show(NodeId<Show>),
245		Expr(NodeId<Expr>),
246	}
247}
248
249ast_type! {
250	pub struct Transaction {
251		pub statements: Option<NodeListId<TopLevelExpr>>,
252		pub commits: bool,
253	}
254}
255
256ast_type! {
257	pub enum KillKind {
258		Uuid(NodeId<Spanned<Uuid>>),
259		Param(NodeId<Param>)
260	}
261}
262
263ast_type! {
264	pub struct Kill {
265		pub kind: KillKind,
266	}
267}
268
269impl_vis_type! {
270	#[derive(Debug)]
271	/// The namespace and database are expressions, not identifiers: a name may
272	/// be computed, as it may anywhere else a resource is named (`USE DB $db`,
273	/// `USE DB type::string(..)`). Evaluation coerces the result to a string.
274	pub enum UseKind {
275		Namespace(NodeId<Expr>),
276		NamespaceDatabase{
277			namespace: NodeId<Expr>,
278			database: NodeId<Expr>
279		},
280		Database(NodeId<Expr>),
281	}
282}
283
284ast_type! {
285	pub struct Use {
286		pub kind: UseKind,
287	}
288}
289
290ast_type! {
291	pub struct OptionStmt {
292		pub name: NodeId<Ident>,
293		pub value: bool,
294	}
295}
296
297ast_type! {
298	pub struct If{
299		pub condition: NodeId<Expr>,
300		pub then: NodeId<Expr>,
301		pub otherwise: Option<NodeId<Expr>>,
302	}
303}
304
305ast_type! {
306	pub struct For{
307		pub param: NodeId<Param>,
308		pub range: NodeId<Expr>,
309		pub body: NodeId<Block>,
310	}
311}
312
313ast_type! {
314	pub struct Let{
315		pub param: NodeId<Param>,
316		pub ty: Option<NodeId<Type>>,
317		pub expr: NodeId<Expr>,
318	}
319}
320
321ast_type! {
322	pub struct Return{
323		pub expr: NodeId<Expr>,
324		pub fetch: Option<NodeListId<Expr>>,
325	}
326}
327
328#[derive(Debug)]
329pub enum Base {
330	Namespace,
331	Database,
332	Root,
333}
334
335impl_vis_type! {
336	#[derive(Debug)]
337	pub enum InfoKind {
338		Root,
339		Namespace{
340			version: Option<NodeId<Expr>>,
341		},
342		Database {
343			version: Option<NodeId<Expr>>,
344		},
345		Table {
346			name: NodeId<Expr>,
347			version: Option<NodeId<Expr>>,
348		},
349		User {
350			name: NodeId<Expr>,
351			base: Option<Base>,
352		},
353		Index {
354			name: NodeId<Expr>,
355			table: NodeId<Expr>,
356		},
357	}
358}
359
360ast_type! {
361	pub struct Info{
362		pub kind: InfoKind,
363		pub structure: bool,
364	}
365}
366
367ast_type! {
368	pub enum ShowTarget{
369		Database(Span),
370		Table(NodeId<Ident>)
371	}
372}
373
374ast_type! {
375	pub enum ShowSince{
376		Timestamp(NodeId<Spanned<DateTime>>),
377		VersionStamp(NodeId<Integer>),
378	}
379}
380
381ast_type! {
382	pub enum ExplainClause{
383		Base(Span),
384		Full(Span),
385	}
386}
387
388ast_type! {
389	pub struct Show{
390		pub target: ShowTarget,
391		pub since: ShowSince,
392		pub limit: Option<NodeId<Expr>>,
393	}
394}
395
396ast_type! {
397	pub enum WithIndex{
398		None(Span),
399		Some(NodeListId<Ident>)
400	}
401}
402
403ast_type! {
404	pub enum AssignmentOp{
405		Assign(Span),
406		Add(Span),
407		Subtract(Span),
408		Extend(Span),
409	}
410}
411
412ast_type! {
413	pub struct Assignment{
414		pub place: NodeId<Place>,
415		pub op: AssignmentOp,
416		pub value: NodeId<Expr>,
417	}
418}
419
420ast_type! {
421	pub enum RecordData{
422		Set(NodeListId<Assignment>),
423		Unset(NodeListId<Place>),
424		Content(NodeId<Expr>),
425		Patch(NodeId<Expr>),
426		Merge(NodeId<Expr>),
427		Replace(NodeId<Expr>),
428	}
429}
430
431ast_type! {
432	pub struct Selector{
433		pub expr: NodeId<Expr>,
434		pub alias: Option<NodeId<Place>>,
435	}
436}
437
438ast_type! {
439	pub enum ListSelector{
440		All(Span),
441		Selector(Selector),
442	}
443}
444
445ast_type! {
446	pub enum Fields{
447		Value(NodeId<Selector>),
448		List(NodeListId<ListSelector>),
449	}
450}
451
452ast_type! {
453	pub enum Output{
454		None(Span),
455		Null(Span),
456		Diff(Span),
457		After(Span),
458		Before(Span),
459		Fields(NodeId<Fields>),
460	}
461}
462
463ast_type! {
464	pub struct Create{
465		pub only: bool,
466		pub targets: NodeListId<Expr>,
467		pub data: Option<RecordData>,
468		pub output: Option<NodeId<Output>>,
469		pub version: Option<NodeId<Expr>>,
470		pub timeout: Option<NodeId<Expr>>,
471	}
472}
473
474ast_type! {
475	pub struct Delete{
476		pub only: bool,
477		pub targets: NodeListId<Expr>,
478		pub with_index: Option<WithIndex>,
479		pub condition: Option<NodeId<Expr>>,
480		pub output: Option<NodeId<Output>>,
481		pub timeout: Option<NodeId<Expr>>,
482		pub explain: Option<ExplainClause>,
483	}
484}
485
486ast_type! {
487	pub struct Update{
488		pub only: bool,
489		pub targets: NodeListId<Expr>,
490		pub with_index: Option<WithIndex>,
491		pub data: Option<RecordData>,
492		pub condition: Option<NodeId<Expr>>,
493		pub output: Option<NodeId<Output>>,
494		pub timeout: Option<NodeId<Expr>>,
495		pub explain: Option<ExplainClause>,
496	}
497}
498
499ast_type! {
500	pub struct Upsert{
501		pub only: bool,
502		pub targets: NodeListId<Expr>,
503		pub with_index: Option<WithIndex>,
504		pub data: Option<RecordData>,
505		pub condition: Option<NodeId<Expr>>,
506		pub output: Option<NodeId<Output>>,
507		pub timeout: Option<NodeId<Expr>>,
508		pub explain: Option<ExplainClause>,
509	}
510}
511
512ast_type! {
513	pub struct Relate{
514		pub only: bool,
515		pub or_update: bool,
516		pub from: NodeId<Expr>,
517		pub through: NodeId<Expr>,
518		pub to: NodeId<Expr>,
519		pub data: Option<RecordData>,
520		pub output: Option<NodeId<Output>>,
521		pub timeout: Option<NodeId<Expr>>,
522	}
523}
524
525impl_vis_type! {
526	#[derive(Debug)]
527	pub enum OrderDirection{
528		Ascending,
529		Descending,
530	}
531}
532
533ast_type! {
534	pub struct Order{
535		pub expr: NodeId<Expr>,
536		pub collate: bool,
537		pub numeric: bool,
538		pub direction: Option<OrderDirection>,
539	}
540}
541
542ast_type! {
543	pub enum OrderBy{
544		Rand(Span),
545		List(NodeListId<Order>),
546	}
547}
548
549impl_vis_type! {
550	#[derive(Debug)]
551	pub enum Group{
552		All,
553		Fields(NodeListId<Expr>),
554	}
555}
556
557ast_type! {
558	pub struct Select{
559		pub fields: NodeId<Fields>,
560		pub omit: Option<NodeListId<Expr>>,
561		pub only: bool,
562		pub from: NodeListId<Expr>,
563		pub with_index: Option<WithIndex>,
564		pub condition: Option<NodeId<Expr>>,
565		pub split: Option<NodeListId<Expr>>,
566		pub group: Option<Group>,
567		pub order: Option<OrderBy>,
568		pub start: Option<NodeId<Expr>>,
569		pub limit: Option<NodeId<Expr>>,
570		pub version: Option<NodeId<Expr>>,
571		pub timeout: Option<NodeId<Expr>>,
572		pub fetch: Option<NodeListId<Expr>>,
573		pub tempfiles: bool,
574		pub explain: Option<ExplainClause>,
575	}
576}
577
578impl_vis_type! {
579	#[derive(Debug)]
580	pub enum InsertInto{
581		Param(NodeId<Param>),
582		Table(NodeId<Ident>),
583	}
584}
585
586ast_type! {
587	pub struct InsertTuples {
588		pub places: NodeListId<Place>,
589		pub values: NodeListId<NodeListId<Expr>>,
590	}
591}
592
593impl_vis_type! {
594	#[derive(Debug)]
595	pub enum InsertData{
596		Expr(NodeId<Expr>),
597		Tuples(InsertTuples),
598	}
599}
600
601ast_type! {
602	pub struct Insert{
603		pub relation: bool,
604		pub ignore: bool,
605		pub into: Option<InsertInto>,
606		pub data: InsertData,
607		pub on_duplicate: Option<NodeListId<Assignment>>,
608		pub output: Option<NodeId<Output>>,
609		pub version: Option<NodeId<Expr>>,
610		pub timeout: Option<NodeId<Expr>>,
611	}
612}
613
614ast_type! {
615	pub struct Rebuild{
616		pub if_exists: bool,
617		pub name: NodeId<Expr>,
618		pub table: NodeId<Expr>,
619		pub concurrently: bool,
620	}
621}
622
623ast_type! {
624	pub enum AccessSubject{
625		User(NodeId<Ident>),
626		Subject(NodeId<RecordId>),
627	}
628}
629
630ast_type! {
631	pub struct AccessGrant{
632		pub subject: AccessSubject,
633	}
634}
635
636impl_vis_type! {
637	#[derive(Debug)]
638	pub enum AccessShowKind{
639		All,
640		Condition(NodeId<Expr>),
641		Grant(NodeId<Ident>),
642	}
643}
644
645ast_type! {
646	pub struct AccessShow{
647		pub kind: AccessShowKind,
648	}
649}
650
651impl_vis_type! {
652	#[derive(Debug)]
653	pub enum AccessRevokeKind{
654		All,
655		Condition(NodeId<Expr>),
656		Grant(NodeId<Ident>),
657	}
658}
659
660ast_type! {
661	pub struct AccessRevoke{
662		pub kind: AccessRevokeKind,
663	}
664}
665
666impl_vis_type! {
667	#[derive(Debug)]
668	pub enum PurgeKind{
669		Expired,
670		Revoked,
671		Both,
672	}
673}
674
675ast_type! {
676	pub struct AccessPurge{
677		pub kind: PurgeKind,
678		pub grace: NodeId<Spanned<Duration>>,
679	}
680}
681
682ast_type! {
683	pub enum AccessKind{
684		Grant(AccessGrant),
685		Show(AccessShow),
686		Revoke(AccessRevoke),
687		Purge(AccessPurge),
688	}
689}
690
691ast_type! {
692	pub struct Access{
693		pub access: NodeId<Ident>,
694		pub base : Option<Base>,
695		pub kind: AccessKind,
696	}
697}
698
699#[derive(Debug)]
700pub enum DefineKind {
701	Create,
702	IfNotExists,
703	Overwrite,
704}
705impl_vis_debug!(DefineKind);
706
707ast_type! {
708	#[derive(Clone)]
709	pub enum Permission{
710		None(Span),
711		Full(Span),
712		Where(NodeId<Expr>),
713	}
714}
715
716ast_type! {
717	pub struct DefineNamespace{
718		pub kind: DefineKind,
719		pub name: NodeId<Expr>,
720		pub comment: Option<NodeId<Expr>>,
721	}
722}
723
724ast_type! {
725	pub enum ChangeFeed{
726		Base(NodeId<Spanned<Duration>>),
727		WithOriginal(NodeId<Spanned<Duration>>),
728	}
729}
730
731ast_type! {
732	pub struct DefineDatabase{
733		pub kind: DefineKind,
734		pub name: NodeId<Expr>,
735		pub strict: bool,
736		pub changefeed: Option<ChangeFeed>,
737		pub comment: Option<NodeId<Expr>>,
738	}
739}
740
741ast_type! {
742	pub struct Parameter{
743		pub name: NodeId<Param>,
744		pub ty: Option<NodeId<Type>>,
745	}
746}
747
748ast_type! {
749	pub struct DefineFunction{
750		pub kind: DefineKind,
751		pub name: NodeId<Path>,
752		pub parameters: Option<NodeListId<Parameter>>,
753		pub return_ty: Option<NodeId<Type>>,
754		pub body: NodeId<Block>,
755		pub comment: Option<NodeId<Expr>>,
756		pub permission: Option<Permission>,
757	}
758}
759ast_type! {
760	pub enum ModuleName{
761		File(NodeId<FileLit>),
762		Path(NodeId<Path>),
763	}
764}
765
766ast_type! {
767	pub struct DefineModule{
768		pub kind: DefineKind,
769		pub subject: ModuleName,
770		pub alias: Option<NodeId<Path>>,
771		pub unsigned: bool,
772		pub comment: Option<NodeId<Expr>>,
773		pub permission: Option<Permission>,
774	}
775}
776
777ast_type! {
778	pub struct DefineParam{
779		pub kind: DefineKind,
780		pub param: NodeId<Param>,
781		pub value: Option<NodeId<Expr>>,
782		pub comment: Option<NodeId<Expr>>,
783		pub permission: Option<Permission>,
784	}
785}
786
787ast_type! {
788	pub struct RelationTable{
789		pub from: Option<NodeListId<Ident>>,
790		pub to: Option<NodeListId<Ident>>,
791		pub enforced: bool,
792	}
793}
794
795ast_type! {
796	pub enum TableKind{
797		Normal(Span),
798		Relation(RelationTable),
799		Any(Span),
800	}
801}
802
803#[derive(Debug)]
804pub enum Schema {
805	Less,
806	Full,
807}
808impl_vis_debug!(Schema);
809
810impl_vis_type! {
811	#[derive(Debug)]
812	pub struct TablePermissions{
813		pub create: Option<Permission>,
814		pub delete: Option<Permission>,
815		pub update: Option<Permission>,
816		pub select: Option<Permission>,
817	}
818}
819
820ast_type! {
821	pub struct DefineTable{
822		pub kind: DefineKind,
823		pub name: NodeId<Expr>,
824		pub comment: Option<NodeId<Expr>>,
825		pub drop: bool,
826		pub schema: Option<Schema>,
827		pub table_kind: Option<TableKind>,
828		pub permission: Option<TablePermissions>,
829		pub changefeed: Option<ChangeFeed>,
830		pub view: Option<NodeId<Select>>,
831	}
832}
833
834ast_type! {
835	pub struct ApiMiddleware{
836		pub path: NodeId<Path>,
837		pub args: Option<NodeListId<Expr>>,
838	}
839}
840
841ast_type! {
842	pub struct ApiAction{
843		pub middleware: Option<NodeListId<ApiMiddleware>>,
844		pub permission: Option<Permission>,
845		pub action: NodeId<Expr>,
846	}
847}
848
849impl_vis_type! {
850	#[derive(Debug)]
851	pub struct DefineMethodApiActions {
852		pub get: Option<NodeId<ApiAction>>,
853		pub delete: Option<NodeId<ApiAction>>,
854		pub patch: Option<NodeId<ApiAction>>,
855		pub post: Option<NodeId<ApiAction>>,
856		pub put: Option<NodeId<ApiAction>>,
857		pub trace: Option<NodeId<ApiAction>>,
858	}
859}
860
861ast_type! {
862	pub struct DefineApi{
863		pub kind: DefineKind,
864		pub path: NodeId<Expr>,
865		pub base_middleware: Option<NodeListId<ApiMiddleware>>,
866		pub base_permission: Option<Permission>,
867		pub fallback: Option<NodeId<Expr>>,
868		pub methods: DefineMethodApiActions,
869		pub comment: Option<NodeId<Expr>>,
870	}
871}
872
873ast_type! {
874	pub struct DefineEventAsync{
875		pub retry: Option<NodeId<Integer>>,
876		pub max_depth: Option<NodeId<Integer>>,
877	}
878}
879
880ast_type! {
881	pub struct DefineEvent{
882		pub kind: DefineKind,
883		pub name: NodeId<Expr>,
884		pub table: NodeId<Expr>,
885		pub condition: Option<NodeId<Expr>>,
886		pub then: NodeListId<Expr>,
887		pub comment: Option<NodeId<Expr>>,
888		pub async_: Option<DefineEventAsync>,
889	}
890}
891
892ast_type! {
893	pub enum FieldDefault{
894		Always(NodeId<Expr>),
895		Some(NodeId<Expr>),
896	}
897}
898
899impl_vis_type! {
900	#[derive(Debug)]
901	pub enum OnDelete{
902		Reject,
903		Ignore,
904		Cascade,
905		Unset,
906		Then(NodeId<Expr>),
907	}
908}
909
910impl_vis_type! {
911	#[derive(Debug)]
912	pub struct FieldPermissions{
913		pub create: Option<Permission>,
914		pub update: Option<Permission>,
915		pub select: Option<Permission>,
916	}
917}
918
919ast_type! {
920	pub struct DefineField{
921		pub kind: DefineKind,
922		pub name: NodeId<Expr>,
923		pub table: NodeId<Expr>,
924		pub ty: Option<NodeId<Type>>,
925		pub flexible: bool,
926		pub readonly: bool,
927		pub value: Option<NodeId<Expr>>,
928		pub assert: Option<NodeId<Expr>>,
929		pub computed: Option<NodeId<Expr>>,
930		pub default: Option<FieldDefault>,
931		pub permissions: Option<FieldPermissions>,
932		pub comment: Option<NodeId<Expr>>,
933		// NOTE: maybe move into own struct if `REFERENCE` gets more subclauses.
934		/// `REFERENCE ON DELETE` clause
935		pub on_delete: Option<OnDelete>
936	}
937}
938
939ast_type! {
940	pub struct CountIndex{
941		pub condition: Option<NodeId<Expr>>,
942	}
943}
944
945impl_vis_type! {
946	#[derive(Debug)]
947	pub enum FullTextScoring {
948		VectorSearch,
949		Bm25 {
950			k1: NodeId<Spanned<f64>>,
951			b: NodeId<Spanned<f64>>,
952		},
953	}
954}
955
956ast_type! {
957	pub struct FullTextIndex{
958		pub analyzer: Option<NodeId<Ident>>,
959		pub highlights: bool,
960		pub scoring: Option<FullTextScoring>,
961	}
962}
963
964#[derive(Debug)]
965pub enum VectorType {
966	F64,
967	F16,
968	F32,
969	I64,
970	I32,
971	I16,
972	I8,
973	U8,
974}
975impl_vis_debug!(VectorType);
976
977ast_type! {
978	pub struct HnswIndex{
979		pub dimension: NodeId<Integer>,
980		pub distance: Option<Distance>,
981		pub ty: Option<VectorType>,
982		pub m: Option<NodeId<Integer>>,
983		pub m0: Option<NodeId<Integer>>,
984		pub ml: Option<NodeId<Integer>>,
985		pub ef_construction: Option<NodeId<Integer>>,
986		pub extend_candidates: bool,
987		pub keep_pruned_connections: bool,
988		pub use_hashed_vector: bool,
989	}
990}
991
992ast_type! {
993	/// Parsed `DISKANN` index options from a `DEFINE INDEX` statement.
994	pub struct DiskAnnIndex{
995		/// Required vector dimension.
996		pub dimension: NodeId<Integer>,
997		/// Optional distance metric; defaults are applied by the SQL/catalog conversion layer.
998		pub distance: Option<Distance>,
999		/// Optional vector type; defaults are applied by the SQL/catalog conversion layer.
1000		pub ty: Option<VectorType>,
1001		/// Optional target graph degree.
1002		pub degree: Option<NodeId<Integer>>,
1003		/// Optional construction search list size.
1004		pub l_build: Option<NodeId<Integer>>,
1005		/// Optional DiskANN pruning alpha.
1006		pub alpha: Option<NodeId<Spanned<f64>>>,
1007		/// Whether the index uses hashed vector-document mapping keys.
1008		pub use_hashed_vector: bool,
1009	}
1010}
1011
1012ast_type! {
1013	pub enum Index{
1014		Unique(Span),
1015		Count(CountIndex),
1016		FullText(FullTextIndex),
1017		Hnsw(HnswIndex),
1018		DiskAnn(DiskAnnIndex)
1019	}
1020}
1021
1022ast_type! {
1023	pub struct DefineIndex{
1024		pub kind: DefineKind,
1025		pub name: NodeId<Expr>,
1026		pub table: NodeId<Expr>,
1027		pub fields: Option<NodeListId<Expr>>,
1028		pub comment: Option<NodeId<Expr>>,
1029		pub index: Option<Index>,
1030		pub concurrently: bool,
1031	}
1032}
1033
1034ast_type! {
1035	pub struct NgramMapper{
1036		pub min: NodeId<Integer>,
1037		pub max: NodeId<Integer>,
1038	}
1039}
1040
1041ast_type! {
1042	pub enum Filter{
1043		Ascii(Span),
1044		Lowercase(Span),
1045		Uppercase(Span),
1046		EdgeNgram(NgramMapper),
1047		Ngram(NgramMapper),
1048		Snowball(NodeId<Ident>),
1049		Mapper(NodeId<StringLit>),
1050	}
1051}
1052
1053ast_type! {
1054	pub struct DefineAnalyzer{
1055		pub kind: DefineKind,
1056		pub name: NodeId<Expr>,
1057		pub filters: Option<NodeListId<Filter>>,
1058		pub tokenizer: Option<NodeListId<Ident>>,
1059		pub function: Option<NodeId<Path>>,
1060		pub comment: Option<NodeId<Expr>>,
1061	}
1062}
1063
1064ast_type! {
1065	pub struct DefineBucket{
1066		pub kind: DefineKind,
1067		pub name: NodeId<Expr>,
1068		pub backend: Option<NodeId<Expr>>,
1069		pub permission: Option<Permission>,
1070		pub comment: Option<NodeId<Expr>>,
1071		pub readonly: bool,
1072	}
1073}
1074
1075ast_type! {
1076	pub struct DefineSequence{
1077		pub kind: DefineKind,
1078		pub name: NodeId<Expr>,
1079		pub batch: Option<NodeId<Expr>>,
1080		pub start: Option<NodeId<Expr>>,
1081		pub timeout: Option<NodeId<Expr>>,
1082	}
1083}
1084
1085ast_type! {
1086	pub struct DefineConfigApi{
1087		pub permission: Option<Permission>,
1088		pub middleware: Option<NodeListId<ApiMiddleware>>,
1089	}
1090}
1091
1092#[derive(Debug)]
1093pub enum GraphqlIntrospection {
1094	None,
1095	Auto,
1096}
1097impl_vis_debug!(GraphqlIntrospection);
1098
1099impl_vis_type! {
1100	#[derive(Debug)]
1101	pub enum TablesConfig{
1102		None,
1103		Auto,
1104		Include(NodeListId<Ident>),
1105		Exclude(NodeListId<Ident>),
1106	}
1107}
1108
1109impl_vis_type! {
1110	#[derive(Debug)]
1111	pub enum FunctionConfig{
1112		None,
1113		Auto,
1114		Include(NodeListId<Path>),
1115		Exclude(NodeListId<Path>),
1116	}
1117}
1118
1119ast_type! {
1120	pub struct DefineConfigGraphql{
1121		pub introspection: Option<GraphqlIntrospection>,
1122		pub table_config: Option<TablesConfig>,
1123		pub function_config: Option<FunctionConfig>,
1124		pub depth_limit: Option<NodeId<Integer>>,
1125		pub complexity_limit: Option<NodeId<Integer>>,
1126	}
1127}
1128
1129ast_type! {
1130	pub struct DefineConfigDefault{
1131		pub namespace: Option<NodeId<Expr>>,
1132		pub database: Option<NodeId<Expr>>,
1133	}
1134}
1135
1136ast_type! {
1137	pub enum DefineConfigKind{
1138		Api(DefineConfigApi),
1139		Graphql(DefineConfigGraphql),
1140		Default(DefineConfigDefault),
1141	}
1142}
1143
1144ast_type! {
1145	pub struct DefineConfig{
1146		pub kind: DefineKind,
1147		pub inner: DefineConfigKind,
1148	}
1149}
1150
1151ast_type! {
1152	pub enum UserSecret{
1153		PassHash(NodeId<StringLit>),
1154		PassWord(NodeId<StringLit>),
1155	}
1156}
1157
1158ast_type! {
1159	pub struct DefineUser {
1160		pub kind: DefineKind,
1161		pub name: NodeId<Expr>,
1162		pub base: Base,
1163		pub secret: Option<UserSecret>,
1164		/// SCRAM-SHA-256 verifier string (`PASSSCRAM`). Additive: may coexist with
1165		/// a `PASSHASH`/`PASSWORD` secret.
1166		pub scram: Option<NodeId<StringLit>>,
1167		pub roles: Option<NodeListId<Ident>>,
1168		pub session_duration: Option<NodeId<Expr>>,
1169		pub token_duration: Option<NodeId<Expr>>,
1170		pub comment: Option<NodeId<Expr>>,
1171	}
1172}
1173
1174impl_vis_type! {
1175	#[derive(Debug)]
1176	pub enum Algorithm{
1177		EdDSA,
1178		Es256,
1179		Es384,
1180		Es512,
1181		Hs256,
1182		Hs384,
1183		Hs512,
1184		Ps256,
1185		Ps384,
1186		Ps512,
1187		Rs256,
1188		Rs384,
1189		Rs512,
1190	}
1191}
1192
1193impl_vis_type! {
1194	#[derive(Debug)]
1195	pub enum JwtVerify{
1196		Key{
1197			algorithm: Algorithm,
1198			key: NodeId<Expr>,
1199		},
1200		Jwks{
1201			url: NodeId<Expr>,
1202		},
1203	}
1204}
1205
1206impl_vis_type! {
1207	#[derive(Debug)]
1208	pub struct JwtIssue{
1209		pub algorithm: Option<Algorithm>,
1210		pub key: Option<NodeId<Expr>>,
1211	}
1212}
1213
1214impl_vis_type! {
1215	#[derive(Debug)]
1216	pub struct Jwt{
1217		pub verify: JwtVerify,
1218		pub issue: Option<JwtIssue>,
1219	}
1220}
1221
1222impl_vis_type! {
1223	#[derive(Debug)]
1224	pub enum BearerAccessSubject{
1225		User,
1226		Record,
1227	}
1228}
1229
1230impl_vis_type! {
1231	#[derive(Debug)]
1232	pub struct BearerAccess{
1233		pub subject: BearerAccessSubject,
1234		pub jwt: Option<Jwt>
1235	}
1236}
1237
1238impl_vis_type! {
1239	#[derive(Debug)]
1240	pub struct RecordAccess{
1241		pub signup: Option<NodeId<Expr>>,
1242		pub signin: Option<NodeId<Expr>>,
1243		pub jwt: Option<Jwt>,
1244		pub refresh: bool,
1245	}
1246}
1247
1248impl_vis_type! {
1249	#[derive(Debug)]
1250	pub enum AccessType{
1251		Jwt(Jwt),
1252		Record(RecordAccess),
1253		Bearer(BearerAccess),
1254	}
1255}
1256
1257ast_type! {
1258	pub struct DefineAccess{
1259		pub kind: DefineKind,
1260		pub name: NodeId<Expr>,
1261		pub base: Base,
1262		pub comment: Option<NodeId<Expr>>,
1263		pub duration_session: Option<NodeId<Expr>>,
1264		pub duration_token: Option<NodeId<Expr>>,
1265		pub duration_grant: Option<NodeId<Expr>>,
1266		pub authenticate: Option<NodeId<Expr>>,
1267		pub ty: Option<AccessType>,
1268	}
1269}
1270
1271ast_type! {
1272	pub struct RemoveNamespace{
1273		pub if_exists: bool,
1274		pub expunge: bool,
1275		pub name: NodeId<Expr>,
1276	}
1277}
1278
1279ast_type! {
1280	pub struct RemoveDatabase{
1281		pub if_exists: bool,
1282		pub expunge: bool,
1283		pub name: NodeId<Expr>,
1284	}
1285}
1286
1287ast_type! {
1288	pub struct RemoveFunction{
1289		pub if_exists: bool,
1290		pub name: NodeId<Path>,
1291	}
1292}
1293
1294ast_type! {
1295	pub struct RemoveModule{
1296		pub if_exists: bool,
1297		pub name: NodeId<Path>,
1298	}
1299}
1300
1301ast_type! {
1302	pub struct RemoveAccess{
1303		pub if_exists: bool,
1304		pub name: NodeId<Expr>,
1305		pub base: Base,
1306	}
1307}
1308
1309ast_type! {
1310	pub struct RemoveParam{
1311		pub if_exists: bool,
1312		pub name: NodeId<Param>,
1313	}
1314}
1315
1316ast_type! {
1317	pub struct RemoveTable{
1318		pub expunge: bool,
1319		pub if_exists: bool,
1320		pub name: NodeId<Expr>,
1321	}
1322}
1323
1324ast_type! {
1325	pub struct RemoveEvent{
1326		pub if_exists: bool,
1327		pub name: NodeId<Expr>,
1328		pub table: NodeId<Expr>,
1329	}
1330}
1331
1332ast_type! {
1333	pub struct RemoveField{
1334		pub if_exists: bool,
1335		pub name: NodeId<Expr>,
1336		pub table: NodeId<Expr>,
1337	}
1338}
1339
1340ast_type! {
1341	pub struct RemoveIndex{
1342		pub if_exists: bool,
1343		pub name: NodeId<Expr>,
1344		pub table: NodeId<Expr>,
1345	}
1346}
1347
1348ast_type! {
1349	pub struct RemoveAnalyzer{
1350		pub if_exists: bool,
1351		pub name: NodeId<Expr>,
1352	}
1353}
1354
1355ast_type! {
1356	pub struct RemoveSequence{
1357		pub if_exists: bool,
1358		pub name: NodeId<Expr>,
1359	}
1360}
1361
1362ast_type! {
1363	pub struct RemoveUser{
1364		pub if_exists: bool,
1365		pub name: NodeId<Expr>,
1366		pub base: Base,
1367	}
1368}
1369
1370ast_type! {
1371	pub struct RemoveApi{
1372		pub if_exists: bool,
1373		pub name: NodeId<Expr>,
1374	}
1375}
1376
1377ast_type! {
1378	pub struct RemoveBucket{
1379		pub if_exists: bool,
1380		pub name: NodeId<Expr>,
1381	}
1382}
1383
1384impl_vis_type! {
1385	#[derive(Debug)]
1386	pub enum RemoveConfigKind{
1387		Graphql,
1388		Api,
1389		Default,
1390	}
1391}
1392
1393ast_type! {
1394	pub struct RemoveConfig{
1395		pub if_exists: bool,
1396		pub kind: RemoveConfigKind,
1397	}
1398}
1399
1400#[derive(Debug, Clone, Copy)]
1401pub enum AlterKind<T> {
1402	Drop(Span),
1403	Set(T),
1404}
1405
1406ast_type! {
1407	pub struct AlterSystem{
1408		pub query_timeout: Option<AlterKind<NodeId<Expr>>>,
1409		pub compact: bool,
1410	}
1411}
1412
1413ast_type! {
1414	pub struct AlterNamespace{
1415		pub if_exists: bool,
1416		pub name: NodeId<Expr>,
1417		pub compact: bool,
1418	}
1419}
1420
1421ast_type! {
1422	pub struct AlterDatabase{
1423		pub if_exists: bool,
1424		pub name: NodeId<Expr>,
1425		pub compact: bool,
1426	}
1427}
1428
1429ast_type! {
1430	pub struct AlterTable{
1431		pub if_exists: bool,
1432		pub name: NodeId<Expr>,
1433		pub comment: Option<AlterKind<NodeId<Expr>>>,
1434		pub changefeed: Option<AlterKind<ChangeFeed>>,
1435		pub schema: Option<Schema>,
1436		pub table_kind: Option<TableKind>,
1437		pub compact: bool,
1438		pub permissions: Option<TablePermissions>,
1439	}
1440}
1441
1442ast_type! {
1443	pub struct AlterEvent{
1444		pub if_exists: bool,
1445		pub name: NodeId<Expr>,
1446		pub table: NodeId<Expr>,
1447		pub condition: Option<AlterKind<NodeId<Expr>>>,
1448		pub then: Option<AlterKind<NodeListId<Expr>>>,
1449		pub comment: Option<AlterKind<NodeId<Expr>>>,
1450		pub async_: Option<AlterKind<DefineEventAsync>>,
1451	}
1452}
1453
1454ast_type! {
1455	pub struct AlterParam{
1456		pub if_exists: bool,
1457		pub param: NodeId<Param>,
1458		pub value: Option<NodeId<Expr>>,
1459		pub comment: Option<AlterKind<NodeId<Expr>>>,
1460		pub permissions: Option<Permission>,
1461	}
1462}
1463
1464ast_type! {
1465	pub struct AlterField{
1466		pub if_exists: bool,
1467		pub name: NodeId<Expr>,
1468		pub table: NodeId<Expr>,
1469		pub ty: Option<AlterKind<NodeId<Type>>>,
1470		pub flexible: Option<AlterKind<()>>,
1471		pub readonly: Option<AlterKind<()>>,
1472		pub value: Option<AlterKind<NodeId<Expr>>>,
1473		pub assert: Option<AlterKind<NodeId<Expr>>>,
1474		pub default: Option<AlterKind<FieldDefault>>,
1475		// NOTE: maybe move into own struct if `REFERENCE` gets more subclauses.
1476		/// `REFERENCE ON DELETE` clause
1477		pub on_delete: Option<AlterKind<OnDelete>>,
1478		pub comment: Option<AlterKind<NodeId<Expr>>>,
1479		pub permissions: Option<FieldPermissions>,
1480	}
1481}
1482
1483ast_type! {
1484	pub struct AlterBucket{
1485		pub if_exists: bool,
1486		pub name: NodeId<Expr>,
1487		pub backend: Option<AlterKind<NodeId<StringLit>>>,
1488		pub readonly: Option<AlterKind<()>>,
1489		pub permissions: Option<Permission>,
1490		pub comment: Option<AlterKind<NodeId<Expr>>>,
1491	}
1492}
1493
1494ast_type! {
1495	pub struct AlterAnalyzer{
1496		pub if_exists: bool,
1497		pub name: NodeId<Expr>,
1498		pub function: Option<AlterKind<NodeId<Path>>>,
1499		pub tokenizer: Option<AlterKind<NodeListId<Ident>>>,
1500		pub filter: Option<AlterKind<NodeListId<Filter>>>,
1501		pub comment: Option<AlterKind<NodeId<Expr>>>,
1502	}
1503}
1504
1505ast_type! {
1506	pub struct AlterIndex{
1507		pub if_exists: bool,
1508		pub name: NodeId<Expr>,
1509		pub table: NodeId<Expr>,
1510		pub comment: Option<AlterKind<NodeId<Expr>>>,
1511		pub prepare_remove: bool,
1512	}
1513}
1514
1515ast_type! {
1516	pub struct AlterSequence{
1517		pub if_exists: bool,
1518		pub name: NodeId<Expr>,
1519		pub timeout: Option<AlterKind<NodeId<Expr>>>,
1520	}
1521}
1522
1523ast_type! {
1524	pub struct AlterFunction{
1525		pub if_exists: bool,
1526		pub name: NodeId<Path>,
1527		pub parameters: Option<Option<NodeListId<Parameter>>>,
1528		pub return_ty: Option<NodeId<Type>>,
1529		pub body: Option<NodeId<Block>>,
1530		pub comment: Option<AlterKind<NodeId<Expr>>>,
1531		pub permission: Option<Permission>,
1532	}
1533}
1534
1535ast_type! {
1536	pub struct AlterUser{
1537		pub if_exists: bool,
1538		pub name: NodeId<Expr>,
1539		pub base: Base,
1540		pub secret: Option<UserSecret>,
1541		/// SCRAM-SHA-256 verifier string (`PASSSCRAM`).
1542		pub scram: Option<NodeId<StringLit>>,
1543		pub roles: Option<NodeListId<Ident>>,
1544		pub session_duration: Option<AlterKind<Spanned<Duration>>>,
1545		pub token_duration: Option<AlterKind<Spanned<Duration>>>,
1546		pub comment: Option<AlterKind<NodeId<Expr>>>,
1547	}
1548}
1549
1550ast_type! {
1551	pub struct AlterAccess{
1552		pub if_exists: bool,
1553		pub name: NodeId<Expr>,
1554		pub base: Base,
1555		pub authenticate: Option<AlterKind<NodeId<Expr>>>,
1556		pub grant_duration: Option<AlterKind<NodeId<Spanned<Duration>>>>,
1557		pub token_duration: Option<AlterKind<NodeId<Spanned<Duration>>>>,
1558		pub session_duration: Option<AlterKind<NodeId<Spanned<Duration>>>>,
1559		pub comment: Option<AlterKind<NodeId<Expr>>>,
1560	}
1561}
1562
1563impl_vis_type! {
1564	#[derive(Debug)]
1565	pub struct AlterMethodApiActions {
1566		pub get: Option<AlterKind<NodeId<ApiAction>>>,
1567		pub delete: Option<AlterKind<NodeId<ApiAction>>>,
1568		pub patch: Option<AlterKind<NodeId<ApiAction>>>,
1569		pub post: Option<AlterKind<NodeId<ApiAction>>>,
1570		pub put: Option<AlterKind<NodeId<ApiAction>>>,
1571		pub trace: Option<AlterKind<NodeId<ApiAction>>>,
1572	}
1573}
1574
1575ast_type! {
1576	pub struct AlterApi{
1577		pub if_exists: bool,
1578		pub name: NodeId<Expr>,
1579		pub fallback: Option<AlterKind<NodeId<Expr>>>,
1580		pub methods: AlterMethodApiActions,
1581		pub comment: Option<AlterKind<NodeId<Expr>>>,
1582	}
1583}
1584
1585ast_type! {
1586	pub struct AlterConfig{
1587		pub if_exists: bool,
1588		pub kind: DefineConfigKind,
1589	}
1590}
1591
1592impl_vis_type! {
1593	#[derive(Debug)]
1594	pub enum ExplainFormat{
1595		Json,
1596		Text,
1597	}
1598}
1599
1600ast_type! {
1601	pub struct Explain{
1602		pub analyze: bool,
1603		pub format: Option<ExplainFormat>,
1604		pub expr: NodeId<Expr>,
1605	}
1606}
1607
1608ast_type! {
1609	#[derive(Copy, Clone)]
1610	pub enum Expr {
1611		Covered(NodeId<Expr>),
1612
1613		Builtin(NodeId<Builtin>),
1614		Float(NodeId<Spanned<f64>>),
1615		Integer(NodeId<Integer>),
1616		Decimal(NodeId<Spanned<Decimal>>),
1617		String(NodeId<StringLit>),
1618
1619		// `@` or ommited when parsing `<-`,`<~` or similar.
1620		Document(NodeId<Span>),
1621
1622		Regex(NodeId<Regex>),
1623
1624		Uuid(NodeId<Spanned<Uuid>>),
1625		DateTime(NodeId<Spanned<DateTime>>),
1626		Duration(NodeId<Spanned<Duration>>),
1627		File(NodeId<FileLit>),
1628		Bytes(NodeId<BytesLit>),
1629
1630		Point(NodeId<Point>),
1631
1632		Array(NodeId<Array>),
1633		Object(NodeId<Object>),
1634		Set(NodeId<Set>),
1635
1636		Mock(NodeId<Mock>),
1637		Closure(NodeId<Closure>),
1638		UnboundedRange(NodeId<Span>),
1639
1640		JsFunction(NodeId<JsFunction>),
1641
1642		RecordId(NodeId<RecordId>),
1643
1644		Block(NodeId<Block>),
1645
1646		Path(NodeId<Path>),
1647		Param(NodeId<Param>),
1648
1649		Binary(NodeId<BinaryExpr>),
1650		Postfix(NodeId<PostfixExpr>),
1651		Idiom(NodeId<IdiomExpr>),
1652		Prefix(NodeId<PrefixExpr>),
1653
1654		Throw(NodeId<Expr>),
1655		If(NodeId<If>),
1656		For(NodeId<For>),
1657		Let(NodeId<Let>),
1658		Return(NodeId<Return>),
1659		Info(NodeId<Info>),
1660		Create(NodeId<Create>),
1661		Update(NodeId<Update>),
1662		Upsert(NodeId<Upsert>),
1663		Delete(NodeId<Delete>),
1664		Relate(NodeId<Relate>),
1665		Select(NodeId<Select>),
1666		Insert(NodeId<Insert>),
1667		Rebuild(NodeId<Rebuild>),
1668		Access(NodeId<Access>),
1669
1670		Sleep(NodeId<Spanned<Duration>>),
1671
1672		Continue(NodeId<Span>),
1673		Break(NodeId<Span>),
1674
1675		DefineNamespace(NodeId<DefineNamespace>),
1676		DefineDatabase(NodeId<DefineDatabase>),
1677		DefineTable(NodeId<DefineTable>),
1678		DefineFunction(NodeId<DefineFunction>),
1679		DefineModule(NodeId<DefineModule>),
1680		DefineParam(NodeId<DefineParam>),
1681		DefineApi(NodeId<DefineApi>),
1682		DefineEvent(NodeId<DefineEvent>),
1683		DefineField(NodeId<DefineField>),
1684		DefineIndex(NodeId<DefineIndex>),
1685		DefineAnalyzer(NodeId<DefineAnalyzer>),
1686		DefineBucket(NodeId<DefineBucket>),
1687		DefineSequence(NodeId<DefineSequence>),
1688		DefineConfig(NodeId<DefineConfig>),
1689		DefineUser(NodeId<DefineUser>),
1690		DefineAccess(NodeId<DefineAccess>),
1691
1692		RemoveNamespace(NodeId<RemoveNamespace>),
1693		RemoveDatabase(NodeId<RemoveDatabase>),
1694		RemoveTable(NodeId<RemoveTable>),
1695		RemoveFunction(NodeId<RemoveFunction>),
1696		RemoveModule(NodeId<RemoveModule>),
1697		RemoveParam(NodeId<RemoveParam>),
1698		RemoveApi(NodeId<RemoveApi>),
1699		RemoveEvent(NodeId<RemoveEvent>),
1700		RemoveField(NodeId<RemoveField>),
1701		RemoveIndex(NodeId<RemoveIndex>),
1702		RemoveAnalyzer(NodeId<RemoveAnalyzer>),
1703		RemoveBucket(NodeId<RemoveBucket>),
1704		RemoveSequence(NodeId<RemoveSequence>),
1705		RemoveUser(NodeId<RemoveUser>),
1706		RemoveAccess(NodeId<RemoveAccess>),
1707		RemoveConfig(NodeId<RemoveConfig>),
1708
1709		AlterSystem(NodeId<AlterSystem>),
1710		AlterNamespace(NodeId<AlterNamespace>),
1711		AlterDatabase(NodeId<AlterDatabase>),
1712		AlterTable(NodeId<AlterTable>),
1713		AlterEvent(NodeId<AlterEvent>),
1714		AlterParam(NodeId<AlterParam>),
1715		AlterField(NodeId<AlterField>),
1716		AlterIndex(NodeId<AlterIndex>),
1717		AlterSequence(NodeId<AlterSequence>),
1718		AlterBucket(NodeId<AlterBucket>),
1719		AlterAnalyzer(NodeId<AlterAnalyzer>),
1720		AlterFunction(NodeId<AlterFunction>),
1721		AlterUser(NodeId<AlterUser>),
1722		AlterAccess(NodeId<AlterAccess>),
1723		AlterApi(NodeId<AlterApi>),
1724		AlterConfig(NodeId<AlterConfig>),
1725
1726		Explain(NodeId<Explain>)
1727	}
1728}
1729
1730ast_type! {
1731	pub struct Block{
1732		pub exprs: Option<NodeListId<Expr>>,
1733	}
1734}
1735
1736ast_type! {
1737	pub enum Builtin {
1738		True(Span),
1739		False(Span),
1740		None(Span),
1741		Null(Span),
1742	}
1743}
1744
1745ast_type! {
1746	pub struct Ident {
1747		pub text: NodeId<String>,
1748	}
1749}
1750
1751#[derive(Debug, Clone, Copy, Eq, PartialEq)]
1752pub enum Sign {
1753	Plus,
1754	Minus,
1755}
1756
1757#[derive(Debug)]
1758pub struct Integer {
1759	pub value: u64,
1760	pub sign: Sign,
1761	pub span: Span,
1762}
1763impl Node for Integer {}
1764impl AstSpan for Integer {
1765	fn ast_span<L: types::NodeLibrary>(&self, _: &types::Ast<L>) -> Span {
1766		self.span
1767	}
1768}
1769
1770impl Integer {
1771	pub fn with_sign(mut self, sign: Sign) -> Self {
1772		self.sign = sign;
1773		self
1774	}
1775
1776	pub fn into_f64(&self) -> f64 {
1777		let v = self.value as f64;
1778		if let Sign::Minus = self.sign {
1779			-v
1780		} else {
1781			v
1782		}
1783	}
1784
1785	pub fn into_i64(&self) -> Option<i64> {
1786		const I64_MAX: u64 = i64::MAX as u64;
1787		const I64_NEG_MIN: u64 = (i64::MAX as u64) + 1;
1788
1789		match (self.sign, self.value) {
1790			(Sign::Plus, 0..=I64_MAX) => Some(self.value as i64),
1791			(Sign::Minus, 0..=I64_MAX) => Some(-(self.value as i64)),
1792			(Sign::Minus, I64_NEG_MIN) => Some(i64::MIN),
1793			_ => None,
1794		}
1795	}
1796}
1797
1798ast_type! {
1799	pub struct StringLit {
1800		pub text: NodeId<String>,
1801	}
1802}
1803
1804ast_type! {
1805	pub struct BytesLit{
1806		pub text: Vec<u8>,
1807	}
1808}
1809
1810ast_type! {
1811	pub struct FileLit{
1812		pub path: NodeId<String>,
1813	}
1814}
1815
1816ast_type! {
1817	pub struct Regex {
1818		pub source: NodeId<String>,
1819	}
1820}
1821
1822ast_type! {
1823	pub struct JsFunctionBody{
1824		pub source: NodeId<String>,
1825	}
1826}
1827
1828ast_type! {
1829	pub struct JsFunction{
1830		pub args: Option<NodeListId<Expr>>,
1831		pub body: JsFunctionBody,
1832	}
1833}
1834
1835ast_type! {
1836	#[derive(Copy, Clone)]
1837	pub struct RecordId{
1838		pub name: NodeId<Ident>,
1839		pub key: NodeId<RecordIdKey>,
1840	}
1841}
1842
1843ast_type! {
1844	#[derive(Copy, Clone)]
1845	pub enum RecordIdKey{
1846		String(NodeId<StringLit>),
1847		Number(Spanned<i64>),
1848		Uuid(NodeId<Spanned<Uuid>>),
1849		Object(NodeId<Object>),
1850		Array(NodeId<Array>),
1851		Range(NodeId<RecordIdKeyRange>),
1852		Generate(Spanned<RecordIdKeyGenerate>),
1853	}
1854}
1855
1856ast_type! {
1857	pub struct RecordIdKeyRange{
1858		pub start: Bound<NodeId<RecordIdKey>>,
1859		pub end: Bound<NodeId<RecordIdKey>>,
1860	}
1861}
1862
1863#[derive(Copy, Clone, Debug)]
1864pub enum RecordIdKeyGenerate {
1865	Ulid,
1866	Uuid,
1867	Rand,
1868}
1869impl_vis_debug!(RecordIdKeyGenerate);
1870
1871impl_vis_type! {
1872	#[derive(Debug)]
1873	pub enum MockKind {
1874		Integer(NodeId<Integer>),
1875		Range {
1876			start: Bound<NodeId<Integer>>,
1877			end: Bound<NodeId<Integer>>,
1878		},
1879	}
1880}
1881
1882ast_type! {
1883	pub struct Mock{
1884		pub name: NodeId<Ident>,
1885		pub kind: MockKind
1886	}
1887}
1888
1889ast_type! {
1890	pub struct Closure{
1891		pub parameters: Option<NodeListId<Parameter>>,
1892		pub output_ty: Option<NodeId<Type>>,
1893		pub body: NodeId<Expr>,
1894	}
1895}
1896
1897ast_type! {
1898	pub struct Point{
1899		pub x: f64,
1900		pub y: f64,
1901	}
1902}
1903
1904ast_type! {
1905	#[derive(Copy, Clone)]
1906	pub struct Array{
1907		pub entries: Option<NodeListId<Expr>>,
1908	}
1909}
1910
1911ast_type! {
1912	#[derive(Copy, Clone)]
1913	pub struct Set{
1914		pub entries: Option<NodeListId<Expr>>,
1915	}
1916}
1917
1918ast_type! {
1919	#[derive(Copy, Clone)]
1920	pub struct Object{
1921		pub entries: Option<NodeListId<ObjectEntry>>,
1922	}
1923}
1924
1925ast_type! {
1926	#[derive(Copy, Clone)]
1927	pub struct ObjectEntry{
1928		pub key: NodeId<String>,
1929		pub value: NodeId<Expr>,
1930	}
1931}
1932
1933ast_type! {
1934	pub struct Param {
1935		pub text: NodeId<String>,
1936	}
1937}
1938
1939impl_vis_type! {
1940	#[derive(Debug)]
1941	pub enum Distance {
1942		Chebyshev,
1943		Cosine,
1944		CosineNormalized,
1945		Euclidean,
1946		Hamming,
1947		InnerProduct,
1948		Jaccard,
1949		Manhattan,
1950		Minkowski(f64),
1951		Pearson,
1952	}
1953}
1954
1955impl_vis_type! {
1956	#[derive(Debug)]
1957	pub enum MatchesOperator {
1958		And,
1959		Or,
1960	}
1961}
1962
1963impl_vis_type! {
1964	#[derive(Debug)]
1965	pub enum BinaryOperator {
1966		/// `-`
1967		Subtract,
1968		/// `+`
1969		Add,
1970		/// `*`, `×`
1971		Multiply,
1972		/// `/`
1973		Divide,
1974		/// `%`
1975		Remainder,
1976		/// `**`
1977		Power,
1978		/// `=`
1979		Equal,
1980		/// `==`
1981		ExactEqual,
1982		/// `!=`
1983		NotEqual,
1984		/// `*=`
1985		AllEqual,
1986		/// `?=`
1987		AnyEqual,
1988
1989		/// `||`, `OR`
1990		Or,
1991		/// `&&`, `AND`
1992		And,
1993		/// `??`
1994		NullCoalescing,
1995		/// `?:`
1996		TenaryCondition,
1997
1998		/// `<`
1999		LessThan,
2000		/// `<=`
2001		LessThanEqual,
2002		/// `>`
2003		GreaterThan,
2004		/// `>=`
2005		GreaterThanEqual,
2006
2007		/// `∋`
2008		Contain,
2009		/// `∌`
2010		NotContain,
2011		/// `⊇`
2012		ContainAll,
2013		/// `⊃`
2014		ContainAny,
2015		/// `⊅`
2016		ContainNone,
2017		/// `∈`
2018		Inside,
2019		/// `∉`
2020		NotInside,
2021		/// `⊆`
2022		AllInside,
2023		/// `⊂`
2024		AnyInside,
2025		/// `⊄`
2026		NoneInside,
2027
2028		/// `OUTSIDE`
2029		Outside,
2030		/// `INTERSECTS`
2031		Intersects,
2032
2033		/// `..`
2034		Range,
2035		/// `..=`
2036		RangeInclusive,
2037		/// `>..`
2038		RangeSkip,
2039		/// `>..=`
2040		RangeSkipInclusive,
2041
2042		// `@@`
2043		Matches {
2044			reference: Option<NodeId<Integer>>,
2045			operator: Option<MatchesOperator>,
2046		},
2047		KNearestNeighbour {
2048			k: NodeId<Integer>,
2049			distance: Distance,
2050		},
2051		KTree {
2052			k: NodeId<Integer>,
2053		},
2054		KApproximate {
2055			k: NodeId<Integer>,
2056			ef: NodeId<Integer>,
2057		},
2058	}
2059}
2060
2061ast_type! {
2062	pub struct BinaryExpr {
2063		pub left: NodeId<Expr>,
2064		pub op: Spanned<BinaryOperator>,
2065		pub right: NodeId<Expr>,
2066	}
2067}
2068
2069impl_vis_type! {
2070	#[derive(Debug)]
2071	pub enum PostfixOperator {
2072		Range,
2073		RangeSkip,
2074
2075		/// .field(EXPR*)
2076		MethodCall{
2077			name: NodeId<String>,
2078			arguments: Option<NodeListId<Expr>>
2079		},
2080		/// (EXPR*)
2081		Call(Option<NodeListId<Expr>>),
2082	}
2083}
2084
2085ast_type! {
2086	pub struct PostfixExpr {
2087		pub left: NodeId<Expr>,
2088		pub op: Spanned<PostfixOperator>,
2089	}
2090}
2091
2092ast_type! {
2093	pub enum PrefixOperator {
2094		Not(Span),
2095		Negate(Span),
2096		Positive(Span),
2097		Range(Span),
2098		RangeInclusive(Span),
2099		Cast(NodeId<Type>),
2100	}
2101}
2102
2103ast_type! {
2104	pub struct PrefixExpr {
2105		pub op: PrefixOperator,
2106		pub right: NodeId<Expr>,
2107	}
2108}
2109
2110impl_vis_type! {
2111	#[derive(Debug)]
2112	pub enum DestructureOperator {
2113		/// { field.* }
2114		All,
2115		/// { field : EXPR }
2116		Expr(NodeId<Expr>),
2117		/// { field.{ .. } }
2118		Destructure(Option<NodeListId<Destructure>>),
2119	}
2120}
2121
2122ast_type! {
2123	pub struct Destructure {
2124		pub field: NodeId<Ident>,
2125		pub op: Option<Spanned<DestructureOperator>>,
2126	}
2127}
2128
2129#[derive(Debug)]
2130pub enum Direction {
2131	// `<-` or `<~`
2132	In,
2133	// `->` or `~>`
2134	Out,
2135	// `<->` or `<~>`
2136	Both,
2137}
2138impl_vis_debug!(Direction);
2139
2140ast_type! {
2141	pub struct LookupSubjectRange{
2142		pub table: NodeId<Ident>,
2143		pub range: NodeId<RecordIdKeyRange>,
2144		pub field: Option<NodeId<Ident>>,
2145	}
2146}
2147
2148ast_type! {
2149	pub struct LookupSubjectTable{
2150		pub table: NodeId<Ident>,
2151		pub field: Option<NodeId<Ident>>,
2152	}
2153}
2154
2155ast_type! {
2156	pub enum LookupSubject{
2157		Range(LookupSubjectRange),
2158		Table(LookupSubjectTable),
2159	}
2160}
2161
2162ast_type! {
2163	pub struct BasicLookup{
2164		pub from: Option<NodeListId<LookupSubject>>,
2165		pub condition:  Option<NodeId<Expr>>,
2166		pub limit: Option<NodeId<Expr>>,
2167		pub start: Option<NodeId<Expr>>,
2168		pub alias: Option<NodeId<PresentPlace>>,
2169	}
2170}
2171
2172ast_type! {
2173	pub struct SelectLookup{
2174		pub fields: NodeId<Fields>,
2175		pub only: bool,
2176		pub from: Option<NodeListId<LookupSubject>>,
2177		pub condition:  Option<NodeId<Expr>>,
2178		pub split: Option<NodeListId<Expr>>,
2179		pub group: Option<Group>,
2180		pub order: Option<OrderBy>,
2181		pub limit: Option<NodeId<Expr>>,
2182		pub start: Option<NodeId<Expr>>,
2183		pub alias: Option<NodeId<PresentPlace>>,
2184	}
2185}
2186
2187ast_type! {
2188	pub enum Lookup{
2189		Any(Span),
2190		Subject(NodeId<LookupSubject>),
2191		Basic(BasicLookup),
2192		Select(SelectLookup),
2193	}
2194}
2195
2196impl_vis_type! {
2197	#[derive(Debug)]
2198	pub enum RecurseKind{
2199		Path{
2200			inclusive: bool,
2201		},
2202		Collect{
2203			inclusive: bool,
2204		},
2205		Shortest{
2206			expects: NodeId<Expr>,
2207			inclusive: bool,
2208		},
2209	}
2210}
2211
2212ast_type! {
2213	pub struct Recurse{
2214		pub start: Bound<NodeId<Integer>>,
2215		pub end: Bound<NodeId<Integer>>,
2216		pub kind: Option<RecurseKind>,
2217		pub expr: NodeId<Expr>,
2218	}
2219}
2220
2221impl_vis_type! {
2222	#[derive(Debug)]
2223	pub enum IdiomOperator {
2224		/// `[*]` | `.*`
2225		All,
2226		/// `[$]`
2227		Last,
2228		/// `...`
2229		Flatten,
2230		/// `.field`
2231		Field(NodeId<Ident>),
2232		/// `[EXPR]`
2233		Index(NodeId<Expr>),
2234		/// `[? EXPR]` | `[WHERE EXPR\]`
2235		Where(NodeId<Expr>),
2236		/// `.?`
2237		Option,
2238		/// `.@`
2239		Repeat,
2240		/// `.{ .. }`
2241		Destructure(Option<NodeListId<Destructure>>),
2242		/// (1, $bar)
2243		Call(Option<NodeListId<Expr>>),
2244		/// `<-`, `->` or `<->`
2245		Graph{
2246			direction: Direction,
2247			lookup: NodeId<Lookup>,
2248		},
2249		/// `<~`
2250		Reference(NodeId<Lookup>),
2251		Recurse(NodeId<Recurse>),
2252	}
2253}
2254
2255ast_type! {
2256	pub struct IdiomExpr{
2257		pub left: NodeId<Expr>,
2258		pub op: Spanned<IdiomOperator>,
2259	}
2260}
2261
2262ast_type! {
2263	pub struct MemberPlace{
2264		pub lhs: NodeId<Place>,
2265		pub name: NodeId<Ident>,
2266	}
2267}
2268
2269ast_type! {
2270	pub struct IndexPlace{
2271		pub lhs: NodeId<Place>,
2272		pub index: NodeId<Expr>,
2273	}
2274}
2275
2276ast_type! {
2277	pub enum Place{
2278		Field(NodeId<Ident>),
2279		Member(MemberPlace),
2280		Index(IndexPlace),
2281		/// Marks that the rest of the place contains syntax which was allowed, but
2282		/// doesn't actually do anything, nor make real sense.
2283		Legacy(Span),
2284	}
2285}
2286
2287ast_type! {
2288	pub struct MemberPresentPlace{
2289		pub lhs: NodeId<PresentPlace>,
2290		pub name: NodeId<Ident>,
2291	}
2292}
2293
2294ast_type! {
2295	pub struct IndexPresentPlace{
2296		pub lhs: NodeId<PresentPlace>,
2297		pub index: NodeId<Expr>,
2298	}
2299}
2300
2301ast_type! {
2302	pub struct AllPresentPlace{
2303		pub lhs: NodeId<PresentPlace>,
2304	}
2305}
2306
2307ast_type! {
2308	pub struct LastPresentPlace{
2309		pub lhs: NodeId<PresentPlace>,
2310	}
2311}
2312
2313ast_type! {
2314	pub enum PresentPlace{
2315		Field(NodeId<Ident>),
2316		Member(MemberPresentPlace),
2317		Index(IndexPresentPlace),
2318		All(AllPresentPlace),
2319		Last(LastPresentPlace),
2320	}
2321}
2322
2323ast_type! {
2324	pub struct IdentListType{
2325		pub idents: Option<NodeListId<Ident>>,
2326	}
2327}
2328
2329ast_type! {
2330	pub enum GeometrySubType {
2331		Point(Span),
2332		Line(Span),
2333		Polygon(Span),
2334		MultiPoint(Span),
2335		MultiLine(Span),
2336		MultiPolygon(Span),
2337		Collection(Span),
2338	}
2339}
2340
2341ast_type! {
2342	pub struct GeometryType{
2343		pub types: Option<NodeListId<GeometrySubType>>,
2344	}
2345}
2346
2347ast_type! {
2348	pub struct ArrayLikeType{
2349		pub ty: Option<NodeId<Type>>,
2350		pub size : Option<NodeId<Integer>>,
2351	}
2352}
2353
2354ast_type! {
2355	pub struct LitObjectType{
2356		pub entries: Option<NodeListId<LitObjectTypeEntry>>
2357	}
2358}
2359
2360ast_type! {
2361	pub struct LitObjectTypeEntry{
2362		pub name: NodeId<Ident>,
2363		pub ty: NodeId<Type>,
2364	}
2365}
2366
2367ast_type! {
2368	pub struct LitArrayType{
2369		pub entries: Option<NodeListId<Type>>,
2370	}
2371}
2372
2373ast_type! {
2374	pub enum PrimeType {
2375		None(Span),
2376		Null(Span),
2377		Bool(Span),
2378		Bytes(Span),
2379		DateTime(Span),
2380		Duration(Span),
2381		Decimal(Span),
2382		Number(Span),
2383		Float(Span),
2384		Integer(Span),
2385		Object(Span),
2386		String(Span),
2387		Regex(Span),
2388		Uuid(Span),
2389		Range(Span),
2390		Function(Span),
2391		Record(NodeId<IdentListType>),
2392		Table(NodeId<IdentListType>),
2393		Geometry(NodeId<GeometryType>),
2394		Array(NodeId<ArrayLikeType>),
2395		Set(NodeId<ArrayLikeType>),
2396		File(NodeId<IdentListType>),
2397		LitBuiltin(NodeId<Builtin>),
2398		LitFloat(NodeId<Spanned<f64>>),
2399		LitInteger(NodeId<Integer>),
2400		LitString(NodeId<StringLit>),
2401		LitDecimal(NodeId<Spanned<Decimal>>),
2402		LitObject(NodeId<LitObjectType>),
2403		LitArray(NodeId<LitArrayType>),
2404		LitDuration(NodeId<Spanned<Duration>>),
2405	}
2406}
2407
2408ast_type! {
2409	pub enum Type {
2410		Any(Span),
2411		Prime(NodeListId<PrimeType>),
2412	}
2413}
2414
2415ast_type! {
2416	pub struct Version{
2417		pub major: u64,
2418		pub minor: u64,
2419		pub patch: u64,
2420	}
2421}
2422
2423ast_type! {
2424	pub enum PathSegment{
2425		Ident(NodeId<Ident>),
2426		Version(Version),
2427	}
2428}
2429
2430ast_type! {
2431	// `field`, `math::max`, `silo::module::pkg::<1.2.3>`
2432	pub struct Path{
2433		pub start: NodeId<Ident>,
2434		pub parts: Option<NodeListId<PathSegment>>,
2435	}
2436}