hamelin_lib 0.19.0

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

use std::sync::Arc;

use crate::tree::ast::clause::TableReference;
use crate::tree::ast::command::Command;
use crate::tree::ast::dataset_identifier::DatasetIdentifier;
use crate::tree::ast::identifier::{Identifier, SimpleIdentifier};
use crate::tree::ast::node::Span;
use crate::tree::ast::pipeline::Pipeline;

use super::command::*;
use super::IntoExpressionBuilder;

// Implement for all command builders
impl From<SetCommandBuilder> for Command {
    fn from(value: SetCommandBuilder) -> Self {
        value.build()
    }
}

impl From<WhereCommandBuilder> for Command {
    fn from(value: WhereCommandBuilder) -> Self {
        value.build()
    }
}

impl From<SelectCommandBuilder> for Command {
    fn from(value: SelectCommandBuilder) -> Self {
        value.build()
    }
}

impl From<DropCommandBuilder> for Command {
    fn from(value: DropCommandBuilder) -> Self {
        value.build()
    }
}

impl From<FromCommandBuilder> for Command {
    fn from(value: FromCommandBuilder) -> Self {
        value.build()
    }
}

impl From<UnionCommandBuilder> for Command {
    fn from(value: UnionCommandBuilder) -> Self {
        value.build()
    }
}

impl From<LimitCommandBuilder> for Command {
    fn from(value: LimitCommandBuilder) -> Self {
        value.build()
    }
}

impl From<WithinCommandBuilder> for Command {
    fn from(value: WithinCommandBuilder) -> Self {
        value.build()
    }
}

impl From<SortCommandBuilder> for Command {
    fn from(value: SortCommandBuilder) -> Self {
        value.build()
    }
}

impl From<ValidParseCommandBuilder> for Command {
    fn from(value: ValidParseCommandBuilder) -> Self {
        value.build()
    }
}

impl From<AggCommandBuilder> for Command {
    fn from(value: AggCommandBuilder) -> Self {
        value.build()
    }
}

impl From<WindowCommandBuilder> for Command {
    fn from(value: WindowCommandBuilder) -> Self {
        value.build()
    }
}

impl From<SuppressCommandBuilder> for Command {
    fn from(value: SuppressCommandBuilder) -> Self {
        value.build()
    }
}

impl From<AppendCommandBuilder> for Command {
    fn from(value: AppendCommandBuilder) -> Self {
        value.build()
    }
}

impl From<JoinCommandBuilderWithOn> for Command {
    fn from(value: JoinCommandBuilderWithOn) -> Self {
        value.build()
    }
}

impl From<LookupCommandBuilderWithOn> for Command {
    fn from(value: LookupCommandBuilderWithOn) -> Self {
        value.build()
    }
}

impl From<UnnestCommandBuilder> for Command {
    fn from(value: UnnestCommandBuilder) -> Self {
        value.build()
    }
}

impl From<RowsCommandBuilder> for Command {
    fn from(value: RowsCommandBuilder) -> Self {
        value.build()
    }
}

impl From<NestCommandBuilder> for Command {
    fn from(value: NestCommandBuilder) -> Self {
        value.build()
    }
}

impl From<MatchCommandBuilder> for Command {
    fn from(value: MatchCommandBuilder) -> Self {
        value.build()
    }
}

/// Builder for Pipeline
#[derive(Debug)]
pub struct PipelineBuilder {
    commands: Vec<Command>,
    span: Span,
}

impl PipelineBuilder {
    pub fn new() -> Self {
        Self {
            commands: Vec::new(),
            span: Span::NONE,
        }
    }

    /// Set the span for this pipeline
    pub fn at(mut self, span: Span) -> Self {
        self.span = span;
        self
    }

    /// Add a command to the pipeline
    pub fn command(mut self, cmd: impl Into<Command>) -> Self {
        self.commands.push(cmd.into());
        self
    }

    /// Add a SET command using a closure
    pub fn set_cmd<F>(self, f: F) -> Self
    where
        F: FnOnce(SetCommandBuilder) -> SetCommandBuilder,
    {
        self.command(f(set_command()))
    }

    /// Add a WHERE command
    pub fn where_cmd(self, condition: impl IntoExpressionBuilder) -> Self {
        self.command(where_command(condition))
    }

    /// Add a SELECT command using a closure
    pub fn select<F>(self, f: F) -> Self
    where
        F: FnOnce(SelectCommandBuilder) -> SelectCommandBuilder,
    {
        self.command(f(select_command()))
    }

    /// Add a DROP command using a closure
    pub fn drop<F>(self, f: F) -> Self
    where
        F: FnOnce(DropCommandBuilder) -> DropCommandBuilder,
    {
        self.command(f(drop_command()))
    }

    /// Add a FROM command using a closure
    pub fn from<F>(self, f: F) -> Self
    where
        F: FnOnce(FromCommandBuilder) -> FromCommandBuilder,
    {
        self.command(f(from_command()))
    }

    /// Add a UNION command using a closure
    pub fn union<F>(self, f: F) -> Self
    where
        F: FnOnce(UnionCommandBuilder) -> UnionCommandBuilder,
    {
        self.command(f(union_command()))
    }

    /// Add a LIMIT command
    pub fn limit(self, count: impl IntoExpressionBuilder) -> Self {
        self.command(limit_command(count))
    }

    /// Add a WITHIN command
    pub fn within(self, duration: impl IntoExpressionBuilder) -> Self {
        self.command(within_command(duration))
    }

    /// Add a SORT command using a closure
    pub fn sort<F>(self, f: F) -> Self
    where
        F: FnOnce(SortCommandBuilder) -> SortCommandBuilder,
    {
        self.command(f(sort_command()))
    }

    /// Add a PARSE command using a closure
    ///
    /// Examples:
    /// - `pipeline.parse(|p| p.pattern("prefix-*-suffix").identifier("field1"))`
    /// - `pipeline.parse(|p| p.pattern("*:*").identifier("key").identifier("value").nodrop(true))`
    pub fn parse<F>(self, f: F) -> Self
    where
        F: FnOnce(ParseCommandBuilder) -> ValidParseCommandBuilder,
    {
        self.command(f(parse_command()))
    }

    /// Add an AGG command using a closure
    pub fn agg<F>(self, f: F) -> Self
    where
        F: FnOnce(AggCommandBuilder) -> AggCommandBuilder,
    {
        self.command(f(agg_command()))
    }

    /// Add a WINDOW command using a closure
    pub fn window<F>(self, f: F) -> Self
    where
        F: FnOnce(WindowCommandBuilder) -> WindowCommandBuilder,
    {
        self.command(f(window_command()))
    }

    /// Add a SUPPRESS command: `interval` is required (e.g. `hours(1)`); use the closure for `BY` keys.
    pub fn suppress<F>(self, interval: impl IntoExpressionBuilder, f: F) -> Self
    where
        F: FnOnce(SuppressCommandBuilder) -> SuppressCommandBuilder,
    {
        self.command(f(suppress_command(interval)))
    }

    /// Add an APPEND command - takes table as parameter
    pub fn append(
        self,
        table: impl Into<TableReference>,
        f: impl FnOnce(AppendCommandBuilder) -> AppendCommandBuilder,
    ) -> Self {
        self.command(f(append_command(table)))
    }

    /// Add a JOIN command with a space-qualified dataset reference
    pub fn join_dataset(
        self,
        dataset: DatasetIdentifier,
        condition: impl IntoExpressionBuilder,
    ) -> Self {
        self.command(join_dataset_reference(dataset).on(condition))
    }

    /// Add a JOIN command with aliased space-qualified dataset
    pub fn join_dataset_aliased(
        self,
        alias: impl Into<SimpleIdentifier>,
        dataset: DatasetIdentifier,
        condition: impl IntoExpressionBuilder,
    ) -> Self {
        self.command(JoinCommandBuilder::table_alias_dataset(alias, dataset).on(condition))
    }

    /// Add a LOOKUP command with a space-qualified dataset reference
    pub fn lookup_dataset(
        self,
        dataset: DatasetIdentifier,
        condition: impl IntoExpressionBuilder,
    ) -> Self {
        self.command(lookup_dataset_reference(dataset).on(condition))
    }

    /// Add a JOIN command with table reference
    pub fn join(
        self,
        table: impl Into<TableReference>,
        condition: impl IntoExpressionBuilder,
    ) -> Self {
        self.command(join_table_reference(table).on(condition))
    }

    /// Add a JOIN command with aliased table
    pub fn join_aliased(
        self,
        alias: impl Into<SimpleIdentifier>,
        table: impl Into<TableReference>,
        condition: impl IntoExpressionBuilder,
    ) -> Self {
        self.command(join_table_alias(alias, table).on(condition))
    }

    /// Add a LOOKUP command with table reference
    pub fn lookup(
        self,
        table: impl Into<TableReference>,
        condition: impl IntoExpressionBuilder,
    ) -> Self {
        self.command(lookup_table_reference(table).on(condition))
    }

    /// Add a LOOKUP command with aliased table
    pub fn lookup_aliased(
        self,
        alias: impl Into<SimpleIdentifier>,
        table: impl Into<TableReference>,
        condition: impl IntoExpressionBuilder,
    ) -> Self {
        self.command(lookup_table_alias(alias, table).on(condition))
    }

    /// Add an EXPLODE command using a closure
    ///
    /// Examples:
    /// - `pipeline.explode(|e| e.expression(field_ref("items")))` - explode expression
    /// - `pipeline.explode(|e| e.assignment("item", field_ref("items")))` - explode with assignment
    pub fn explode<F>(self, f: F) -> Self
    where
        F: FnOnce(ExplodeCommandBuilder<NoClause>) -> ExplodeCommandBuilder<WithClause>,
    {
        self.command(f(explode_command()))
    }

    /// Add an UNNEST command
    pub fn unnest(self, expression: impl IntoExpressionBuilder) -> Self {
        self.command(unnest_command(expression))
    }

    /// Add a ROWS command
    pub fn rows(self, expression: impl IntoExpressionBuilder) -> Self {
        self.command(rows_command(expression))
    }

    /// Add a NEST command
    pub fn nest(self, identifier: impl Into<Identifier>) -> Self {
        self.command(nest_command(identifier))
    }

    /// Add a MATCH command using a closure
    pub fn match_cmd<F>(self, f: F) -> Self
    where
        F: FnOnce(MatchCommandBuilder) -> MatchCommandBuilder,
    {
        self.command(f(match_command()))
    }

    pub fn build(&self) -> Pipeline {
        Pipeline {
            span: self.span,
            commands: self.commands.iter().cloned().map(Arc::new).collect(),
        }
    }
}

impl From<Arc<Command>> for Command {
    fn from(cmd: Arc<Command>) -> Self {
        (*cmd).clone()
    }
}

impl From<PipelineBuilder> for Pipeline {
    fn from(value: PipelineBuilder) -> Self {
        value.build()
    }
}

impl Default for PipelineBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Create a pipeline builder
pub fn pipeline() -> PipelineBuilder {
    PipelineBuilder::new()
}

/// Create a pipeline from an expression (wraps it in a SELECT command)
pub fn pipeline_from_expression(expr: impl IntoExpressionBuilder) -> Pipeline {
    Pipeline::from(expr.into_expression_builder().build())
}