Skip to main content

relate_parser/ast/
show.rs

1//! `SHOW`/`TERMINATE`/administration clause nodes and the shared `YIELD` list.
2
3use super::*;
4
5/// See `Clause::AlterDatabase`.
6#[derive(Debug, Clone, PartialEq)]
7pub struct AlterDatabaseClause {
8    pub name: SchemaName,
9    pub if_exists: bool,
10    /// The `SET DEFAULT LANGUAGE CYPHER <n>` digits (e.g. `5`, `25`, or an
11    /// unsupported value like `6`), unvalidated at parse time (CIP-170):
12    /// an out-of-range version here is a *runtime*
13    /// data exception (`22N04`), unlike the CIP-150 statement-preamble
14    /// `CYPHER <version>` prefix, which rejects at compile time.
15    pub default_language: u32,
16}
17
18/// One `YIELD <name> [AS <alias>]` item of a procedure call or catalog
19/// command. `alias` is `None` when no `AS` was written, so "no alias" is
20/// represented by the optional itself rather than an empty-string sentinel
21/// in a parallel array.
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct YieldItem {
24    pub name: SmallName,
25    pub alias: Option<SmallName>,
26}
27
28impl YieldItem {
29    /// The name this item binds in the enclosing scope: the alias when one
30    /// was written, else the yielded column name. A `SmallName` copy (the
31    /// inline arm is a `u64` move, the spilled arm a refcount bump,
32    /// ADR-0509 §1) — consumers bridging into a `String`-keyed scope map
33    /// pay the text conversion at that one feed site.
34    pub fn effective_name(&self) -> SmallName {
35        self.alias.clone().unwrap_or_else(|| self.name.clone())
36    }
37}
38
39/// A clause's `YIELD` tail, modeled once: the explicit items in source
40/// order plus whether `YIELD *` was written. Empty `items` with `all ==
41/// false` means no `YIELD` at all (the grammar never reads an empty
42/// explicit list).
43#[derive(Debug, Clone, PartialEq, Eq, Default)]
44pub struct YieldList {
45    pub items: Vec<YieldItem>,
46    pub all: bool,
47}
48
49/// A clause's yield mode, derived once from its [`YieldList`]: `YIELD *`
50/// projects every column, a bare clause its default table, and an explicit
51/// list its named (post-alias) columns. Consumers select their behavior
52/// from this instead of each re-testing `(all, is_empty)`.
53#[derive(Debug, Clone, Copy, PartialEq, Eq)]
54pub enum YieldMode {
55    /// `YIELD *`: every column of the command.
56    Star,
57    /// No `YIELD` at all: the command's default table.
58    Default,
59    /// An explicit `YIELD` list: the named, post-alias columns.
60    Named,
61}
62
63impl YieldList {
64    /// The clause's yield mode — the one derivation.
65    pub fn mode(&self) -> YieldMode {
66        if self.all {
67            YieldMode::Star
68        } else if self.items.is_empty() {
69            YieldMode::Default
70        } else {
71            YieldMode::Named
72        }
73    }
74
75    /// Whether any `YIELD` tail was written (an explicit list or `*`).
76    pub fn present(&self) -> bool {
77        !matches!(self.mode(), YieldMode::Default)
78    }
79
80    /// Each item's raw yielded name, in source order.
81    pub fn names(&self) -> impl Iterator<Item = &SmallName> {
82        self.items.iter().map(|item| &item.name)
83    }
84
85    /// Each item's raw name, optional alias, and effective (post-alias)
86    /// name, in source order. Consumers that need both sides of the alias
87    /// rule read them from this one iterator instead of re-deriving the
88    /// precedence.
89    pub fn pairs(&self) -> impl Iterator<Item = (&SmallName, Option<&SmallName>, SmallName)> {
90        self.items
91            .iter()
92            .map(|item| (&item.name, item.alias.as_ref(), item.effective_name()))
93    }
94
95    /// Each item's effective (post-alias) name, in source order.
96    pub fn effective_names(&self) -> impl Iterator<Item = SmallName> + '_ {
97        self.items.iter().map(YieldItem::effective_name)
98    }
99}
100
101/// `SHOW [ALL|RANGE|POINT|TEXT|FULLTEXT|VECTOR|LOOKUP] INDEXES [YIELD ...]
102/// [WHERE ...]` (CIP-11). Mirrors `ShowConstraintsClause`; `kind_filter`
103/// is `None` for bare `SHOW INDEXES`/`SHOW ALL INDEXES` (every index kind).
104#[derive(Debug, Clone, PartialEq)]
105pub struct ShowIndexesClause {
106    pub kind_filter: Option<IndexKind>,
107    pub yields: YieldList,
108    pub where_expr: Option<Expr>,
109    /// `ORDER BY`/`SKIP`/`LIMIT` written directly on the `YIELD` (before
110    /// any trailing `WHERE`), same paging sub-grammar and rationale as
111    /// `ShowConstraintsClause::stages`.
112    pub stages: Vec<PageStage>,
113}
114
115/// `SHOW FUNCTIONS [YIELD ...] [WHERE ...]` — the built-in function catalog
116/// (CIP-245/CIP-247/CIP-263 discoverability scenarios). A minimal, dedicated
117/// clause rather than a generic SHOW-command framework: only the function
118/// catalog is modeled, not indexes/constraints/databases (out of this
119/// batch's scope). `where_expr` is embedded here (rather than requiring a
120/// standalone `WHERE` clause, which this AST has no variant for — WHERE is
121/// always embedded in the clause that produces its rows, matching
122/// `MatchClause`) so `SHOW FUNCTIONS YIELD name WHERE name = 'x' RETURN
123/// name` parses as a single clause producing already-filtered rows.
124#[derive(Debug, Clone, PartialEq)]
125pub struct ShowFunctionsClause {
126    pub yields: YieldList,
127    pub where_expr: Option<Expr>,
128    /// The YIELD's own paging sub-grammar, written between the YIELD list
129    /// and the trailing `WHERE` (live Neo4j
130    /// accepts that order for every SHOW command).
131    pub stages: Vec<PageStage>,
132    /// `[ALL | BUILT IN | USER DEFINED]` scope filter (CIP-84/CIP-157
133    /// `SHOW BUILT IN FUNCTIONS` / `SHOW USER DEFINED FUNCTIONS`
134    /// scenarios). `All` (the default) and `BuiltIn` are equivalent for
135    /// Relate, which ships only built-in functions; `UserDefined`
136    /// always yields an empty catalog (no way to register a UDF).
137    pub scope: FunctionScope,
138    /// `EXECUTABLE [BY <user>]`, when written.
139    pub executable: Option<ExecutableBy>,
140}
141
142/// The `EXECUTABLE BY <user>` selector of `SHOW FUNCTIONS`/`SHOW
143/// PROCEDURES`: the commands a user may execute. The bare `EXECUTABLE` form
144/// applies no privilege filter, so it is the same state as writing no
145/// selector at all (`None` on the clause) and has no variant of its own.
146#[derive(Debug, Clone, PartialEq)]
147pub enum ExecutableBy {
148    /// `EXECUTABLE BY <user>` -- the named user.
149    User(String),
150    /// `EXECUTABLE BY CURRENT USER` -- the session's own user.
151    Current,
152}
153
154/// Scope filter for `SHOW [ALL | BUILT IN | USER DEFINED] FUNCTIONS`
155/// (CIP-84/CIP-157). See `ShowFunctionsClause::scope`.
156#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
157pub enum FunctionScope {
158    #[default]
159    All,
160    BuiltIn,
161    UserDefined,
162}
163
164/// `SHOW [<type filter>] CONSTRAINTS [YIELD ...] [WHERE ...]` (CIP-85's
165/// observation surface, needed by CIP-100 to expose a property type
166/// constraint's normalized `propertyType`; the `<type filter>` grammar is
167/// CIP-62/CIP-64/CIP-74's `[ALL] [NODE|REL[ATIONSHIP]] [PROPERTY]?
168/// (UNIQUE[NESS]|KEY|EXIST[ENCE]|TYPE)` surface). Mirrors
169/// `ShowFunctionsClause` otherwise.
170#[derive(Debug, Clone, PartialEq)]
171pub struct ShowConstraintsClause {
172    pub yields: YieldList,
173    pub where_expr: Option<Expr>,
174    pub type_filter: ConstraintTypeFilter,
175    /// `ORDER BY`/`SKIP`/`LIMIT` written directly on the `YIELD` (before
176    /// any trailing `WHERE`) as part of the `YIELD`'s own paging
177    /// sub-grammar, e.g. `YIELD name ORDER BY
178    /// name WHERE ... RETURN ...` -- distinct from a `SHOW ... YIELD ...`
179    /// followed by a separate top-level `ORDER BY`/`RETURN ... ORDER BY`
180    /// clause, which parses as its own `StandaloneOrderBy`/`Return` clause
181    /// instead and never populates this field.
182    pub stages: Vec<PageStage>,
183}
184
185/// The entity half of a `SHOW <type filter> CONSTRAINTS` filter -- `Any`
186/// for bare `SHOW CONSTRAINTS`/`SHOW ALL CONSTRAINTS` or a kind-only
187/// filter with no `NODE`/`REL[ATIONSHIP]` prefix.
188#[derive(Debug, Clone, Copy, PartialEq, Eq)]
189pub enum ConstraintEntityFilter {
190    Any,
191    Node,
192    Relationship,
193}
194
195/// The constraint-kind half of a `SHOW <type filter> CONSTRAINTS` filter.
196/// `None` (on `ConstraintTypeFilter::kind`) means every kind (bare `SHOW
197/// CONSTRAINTS`/`SHOW ALL CONSTRAINTS`/`SHOW NODE CONSTRAINTS`).
198#[derive(Debug, Clone, Copy, PartialEq, Eq)]
199pub enum ConstraintKindFilter {
200    /// `UNIQUE`/`UNIQUENESS`, optionally `PROPERTY`-prefixed (CIP-74) --
201    /// matches `constraint_type == "unique"`.
202    Unique,
203    /// `KEY` -- matches `constraint_type == "key"`. Never combines with
204    /// `PROPERTY`.
205    Key,
206    /// `TYPE`, optionally `PROPERTY`-prefixed -- matches `constraint_type
207    /// == "propertyType"` (CIP-85/CIP-100).
208    PropertyType,
209    /// `EXIST`/`EXISTENCE` -- property existence plus the four
210    /// label/endpoint-label kinds (CIP-187's filtering table: a bare
211    /// `EXIST[ENCE]`/`NODE EXIST[ENCE]` matches `NODE_LABEL_EXISTENCE`
212    /// too).
213    Existence,
214    /// `PROPERTY EXIST[ENCE]` (CIP-187) -- property existence only
215    /// (`constraint_type == "exists"`), never the label/endpoint-label
216    /// kinds the bare [`Self::Existence`] also admits. Its own variant —
217    /// the grammar distinguishes the two spellings at parse time, so the
218    /// AST carries the distinction typed, never as a side-channel bool
219    /// beside the kind.
220    PropertyExistence,
221}
222
223/// `SHOW <type filter> CONSTRAINTS` -- `entity` narrows by `entityType`,
224/// `kind` narrows by constraint kind. `ConstraintTypeFilter::all()` (empty
225/// entity, no kind) is the default for bare `SHOW CONSTRAINTS`/`SHOW ALL
226/// CONSTRAINTS`, matching every constraint.
227#[derive(Debug, Clone, Copy, PartialEq, Eq)]
228pub struct ConstraintTypeFilter {
229    pub entity: ConstraintEntityFilter,
230    pub kind: Option<ConstraintKindFilter>,
231}
232
233impl ConstraintTypeFilter {
234    pub fn all() -> Self {
235        ConstraintTypeFilter {
236            entity: ConstraintEntityFilter::Any,
237            kind: None,
238        }
239    }
240}
241
242/// `SHOW DATABASE[S] [<filter>] [YIELD ...] [WHERE ...]` (CIP-42/CIP-68).
243#[derive(Debug, Clone, PartialEq)]
244pub struct ShowDatabasesClause {
245    pub filter: DatabaseFilter,
246    pub yields: YieldList,
247    pub where_expr: Option<Expr>,
248    pub stages: Vec<PageStage>,
249}
250
251/// Which database(s) a `SHOW DATABASE[S]` clause reports (CIP-68's
252/// `DEFAULT`/`HOME` singular-only forms).
253#[derive(Debug, Clone, PartialEq)]
254pub enum DatabaseFilter {
255    /// Bare `SHOW DATABASE[S]` -- every database.
256    All,
257    /// `SHOW DEFAULT DATABASE` -- singular only, CIP-68 rejects the plural
258    /// `SHOW DEFAULT DATABASES`.
259    Default,
260    /// `SHOW HOME DATABASE` -- singular only, same restriction as `Default`.
261    Home,
262    /// `SHOW DATABASE[S] <name>` -- one named database.
263    Named(SchemaName),
264}
265
266/// `SHOW [ALL|POPULATED] ROLE[S] [WITH USER[S]] [YIELD ...] [WHERE ...]`
267/// (CIP-68).
268#[derive(Debug, Clone, PartialEq)]
269pub struct ShowRolesClause {
270    pub modifier: RoleModifier,
271    /// `WITH USER[S]` -- when true, the catalog carries one row per
272    /// (role, member) pair (adding the `member` column) instead of one row
273    /// per role.
274    pub with_users: bool,
275    pub yields: YieldList,
276    pub where_expr: Option<Expr>,
277    pub stages: Vec<PageStage>,
278}
279
280/// The `[ALL|POPULATED]` modifier of `SHOW ROLE[S]` -- `Any` (bare `SHOW
281/// ROLE[S]`) and `All` (`SHOW ALL ROLE[S]`) report the identical row set
282/// (every role); `Populated` narrows to roles with at least one member.
283#[derive(Debug, Clone, Copy, PartialEq, Eq)]
284pub enum RoleModifier {
285    Any,
286    All,
287    Populated,
288}
289
290/// `SHOW SERVER[S] [YIELD ...] [WHERE ...]` (CIP-68).
291#[derive(Debug, Clone, PartialEq)]
292pub struct ShowServersClause {
293    pub yields: YieldList,
294    pub where_expr: Option<Expr>,
295    pub stages: Vec<PageStage>,
296}
297
298/// `SHOW USER[S] [YIELD ...] [WHERE ...]` / `SHOW CURRENT USER` (CIP-68).
299#[derive(Debug, Clone, PartialEq)]
300pub struct ShowUsersClause {
301    /// `SHOW CURRENT USER` -- singular only, CIP-68 rejects the plural
302    /// `SHOW CURRENT USERS`.
303    pub current_only: bool,
304    pub yields: YieldList,
305    pub where_expr: Option<Expr>,
306    pub stages: Vec<PageStage>,
307}
308
309/// `SHOW PROCEDURE[S] [YIELD ...] [WHERE ...]` (CIP-68 grammar
310/// foundation).
311#[derive(Debug, Clone, PartialEq)]
312pub struct ShowProceduresClause {
313    pub yields: YieldList,
314    pub where_expr: Option<Expr>,
315    pub stages: Vec<PageStage>,
316    /// `EXECUTABLE [BY <user>]`, when written.
317    pub executable: Option<ExecutableBy>,
318}
319
320/// `SHOW SETTING[S] [<names>] [YIELD ...] [WHERE ...]` (CIP-68 grammar
321/// foundation). `names` is the optional expression naming the setting(s) to
322/// filter to: a STRING or a LIST<STRING> (or a `null`/parameter resolving to
323/// either). The executor selects from its own fixed settings catalog -- a
324/// null selects nothing, an empty list means "no filter" (CIP-253), and any
325/// other shape is a `TypeError`.
326#[derive(Debug, Clone, PartialEq)]
327pub struct ShowSettingsClause {
328    pub names: Option<Expr>,
329    pub yields: YieldList,
330    pub where_expr: Option<Expr>,
331    pub stages: Vec<PageStage>,
332}
333
334/// `SHOW TRANSACTION[S] [<id-list>] [YIELD ...] [WHERE ...]` (CIP-68
335/// grammar foundation). `ids` is the optional comma-separated list of
336/// transaction-id expressions to filter to (empty = no filter) --
337/// Relate has no live-transaction catalog, so it's parsed but never
338/// changes the (always empty) result.
339#[derive(Debug, Clone, PartialEq)]
340pub struct ShowTransactionsClause {
341    pub ids: Vec<Expr>,
342    pub yields: YieldList,
343    pub where_expr: Option<Expr>,
344    pub stages: Vec<PageStage>,
345}
346
347/// `TERMINATE TRANSACTION[S] <id-list> [YIELD ...]` (CIP-68 grammar
348/// foundation). Since `ShowTransactions` never reports a live transaction,
349/// every requested id is reported as not found.
350#[derive(Debug, Clone, PartialEq)]
351pub struct TerminateTransactionsClause {
352    pub ids: Vec<Expr>,
353    pub yields: YieldList,
354}