Skip to main content

eventql_parser/typing/
mod.rs

1use crate::StrRef;
2use serde::Serialize;
3
4pub mod analysis;
5
6/// A reference to a type stored in the [`TypeArena`](crate::arena::TypeArena).
7#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
8pub struct TypeRef(pub(crate) usize);
9
10/// A reference to a record definition stored in the [`TypeArena`](crate::arena::TypeArena).
11#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
12pub struct Record(pub(crate) usize);
13
14/// A reference to a function argument type list stored in the [`TypeArena`](crate::arena::TypeArena).
15#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
16pub struct ArgsRef(pub(crate) usize);
17/// Represents function argument types with optional parameter support.
18///
19/// This type allows defining functions that have both required and optional parameters.
20/// The `needed` field specifies how many arguments are required, while `values` contains
21/// all possible argument types (both required and optional).
22///
23#[derive(Debug, Serialize, PartialEq, Eq, Hash, Clone, Copy)]
24pub struct FunArgs {
25    /// All argument types, including both required and optional parameters
26    pub values: ArgsRef,
27    /// Number of required arguments (must be <= values.len())
28    pub needed: usize,
29}
30
31/// Type information for expressions.
32///
33/// This enum represents the type of expressions in the EventQL type system.
34/// Types can be inferred during semantic analysis or left as `Unspecified`.
35#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize)]
36pub enum Type {
37    /// Type has not been determined yet
38    #[default]
39    Unspecified,
40    /// Numeric type (f64)
41    Number,
42    /// String type
43    String,
44    /// Boolean type
45    Bool,
46    /// Array type
47    Array(TypeRef),
48    /// Record (object) type
49    Record(Record),
50    /// Subject pattern type
51    Subject,
52    /// Function type with support for optional parameters.
53    ///
54    /// The `args` field uses [`FunArgs`] to support both required and optional parameters.
55    /// Optional parameters are indicated when `args.needed < args.values.len()`.
56    App {
57        /// Function argument types, supporting optional parameters
58        args: FunArgs,
59        /// Return type of the function
60        result: TypeRef,
61        /// Whether this is an aggregate function (operates on grouped data)
62        aggregate: bool,
63    },
64    /// Date type (e.g., `2026-01-03`)
65    ///
66    /// Used when a field is explicitly converted to a date using the `AS DATE` syntax.
67    Date,
68    /// Time type (e.g., `13:45:39`)
69    ///
70    /// Used when a field is explicitly converted to a time using the `AS TIME` syntax.
71    Time,
72    /// DateTime type (e.g., `2026-01-01T13:45:39Z`)
73    ///
74    /// Used when a field is explicitly converted to a datetime using the `AS DATETIME` syntax.
75    DateTime,
76    /// Custom type not defined in the EventQL reference
77    ///
78    /// Used when a field is converted to a custom type registered in the analysis options.
79    /// The string contains the custom type name as it appears in the query.
80    ///
81    /// # Examples
82    ///
83    /// ```
84    /// use eventql_parser::Session;
85    ///
86    /// let mut session = Session::builder()
87    ///     .declare_custom_type("CustomTimestamp")
88    ///     .build();
89    /// let query = session.parse("FROM e IN events PROJECT INTO { ts: e.data.timestamp as CustomTimestamp }").unwrap();
90    /// let typed_query = session.run_static_analysis(query).unwrap();
91    /// ```
92    Custom(StrRef),
93}
94
95impl Type {
96    /// Returns the inner [`Record`] reference, panicking if this is not a `Type::Record`.
97    pub fn as_record_or_panic(&self) -> Record {
98        if let Self::Record(r) = self {
99            return *r;
100        }
101
102        panic!("expected record type, got {:?}", self);
103    }
104}