hamelin_lib 0.10.13

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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Expression builder system for constructing AST and TypedAST nodes
//!
//! This module provides a fluent API for building expression trees without
//! duplicating type checking or validation logic. All intelligence is delegated
//! to the existing `TypedExpression::from_ast_with_context` implementation.

use crate::tree::{
    ast::{expression::Expression, identifier::Identifier},
    typed_ast::expression::TypedExpression,
};

mod access;
mod collections;
mod functions;
mod lambda;
mod literal;
mod operators;
mod special;

// Command, Pipeline, Query, and Identifier builders
pub mod command;
pub mod identifier;
pub mod pipeline;
pub mod query;

// Re-export commonly used builders
pub use command::{
    agg_command, append_command, distinct_command, drop_command, explode_command, from_command,
    join_table_alias, join_table_reference, limit_command, lookup_table_alias,
    lookup_table_reference, match_command, nest_command, parse_command, pattern_quantified,
    pattern_with_alias, pattern_with_alias_and_quantifier, quantifier_any_number,
    quantifier_at_least_one, quantifier_exactly, quantifier_zero_or_one, rows_command,
    select_command, set_command, sort_command, table_ref, union_command, unnest_command,
    where_command, window_command, within_command, AggCommandBuilder, AppendCommandBuilder,
    DistinctCommandBuilder, DropCommandBuilder, ExplodeCommandBuilder, FromCommandBuilder,
    JoinCommandBuilder, LimitCommandBuilder, LookupCommandBuilder, MatchCommandBuilder,
    NestCommandBuilder, NoClause, ParseCommandBuilder, ParseCommandBuilderWithPattern,
    RowsCommandBuilder, SelectCommandBuilder, SetCommandBuilder, SortCommandBuilder,
    UnionCommandBuilder, UnnestCommandBuilder, ValidParseCommandBuilder, WhereCommandBuilder,
    WindowCommandBuilder, WithClause, WithinCommandBuilder,
};
pub use identifier::{ident, IdentifierBuilder};
pub use pipeline::{pipeline, pipeline_from_expression, PipelineBuilder};
pub use query::{query, IntoPipelineRc, QueryBuilder, QueryBuilderWithMain};

pub use access::{
    // Convenience functions
    field,
    index,
    // Builders
    FieldLookupBuilder,
    IndexAccessBuilder,
};
pub use collections::{
    // Convenience functions
    array,
    pair,
    struct_literal,
    tuple,
    // Builders
    ArrayLiteralBuilder,
    PairLiteralBuilder,
    StructLiteralBuilder,
    TupleLiteralBuilder,
};
pub use functions::{
    // Convenience functions
    call,
    FunctionCallBuilder,
};
pub use lambda::{lambda, lambda1, lambda2, LambdaBuilder, LambdaBuilderWithBody};
pub use literal::{
    BoolLiteralBuilder, DecimalLiteralBuilder, DoubleLiteralBuilder, IntLiteralBuilder,
    IntervalLiteralBuilder, NullLiteralBuilder, StringLiteralBuilder,
};
pub use operators::{
    // Convenience functions
    add,
    and,
    divide,
    eq,
    gt,
    gte,
    in_,
    is_not_null,
    is_null,
    lt,
    lte,
    modulo,
    multiply,
    ne,
    negate,
    not,
    not_in,
    or,
    subtract,
    BinaryOperatorBuilder,
    UnaryPostfixOperatorBuilder,
    UnaryPrefixOperatorBuilder,
};
pub use special::{
    at,
    at_day,
    at_hour,
    at_minute,
    at_month,
    at_second,
    at_week,
    at_year,
    // Convenience functions
    cast,
    error,
    field_ref,
    // Builders
    CastBuilder,
    ErrorBuilder,
    FieldReferenceBuilder,
    TsTruncBuilder,
};

/// Builder for configuring expression translation options with fluent API
///
/// This stores the AST and allows customizing the context used for type checking.
/// The builder is consumed when `.build()` or `.build_with_errors()` is called.
///
/// Core trait for all expression builders
pub trait ExpressionBuilder: std::fmt::Debug {
    /// Build an untyped AST Expression
    fn build(&self) -> Expression;
}

/// Trait for types that can be converted into expression builders
pub trait IntoExpressionBuilder {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder>;
}

// Implement IntoExpressionBuilder for primitives
impl IntoExpressionBuilder for i64 {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        Box::new(IntLiteralBuilder::new(self))
    }
}

impl IntoExpressionBuilder for i32 {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        Box::new(IntLiteralBuilder::new(self as i64))
    }
}

impl IntoExpressionBuilder for f64 {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        Box::new(DoubleLiteralBuilder::new(self))
    }
}

impl IntoExpressionBuilder for f32 {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        Box::new(DoubleLiteralBuilder::new(self as f64))
    }
}

impl IntoExpressionBuilder for &str {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        Box::new(StringLiteralBuilder::new(self))
    }
}

impl IntoExpressionBuilder for String {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        Box::new(StringLiteralBuilder::new(self))
    }
}

impl IntoExpressionBuilder for bool {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        Box::new(BoolLiteralBuilder::new(self))
    }
}

// Implement IntoExpressionBuilder for Rust range types
// This allows writing `days(1)..days(7)` directly
use std::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
use std::sync::Arc;

impl<T> IntoExpressionBuilder for Range<T>
where
    T: ExpressionBuilder + IntoExpressionBuilder,
{
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        use crate::tree::ast::ops::BinaryOp;
        Box::new(BinaryOperatorBuilder::new(
            BinaryOp::Range,
            self.start,
            self.end,
        ))
    }
}

impl<T> IntoExpressionBuilder for RangeFrom<T>
where
    T: ExpressionBuilder + IntoExpressionBuilder,
{
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        use crate::tree::ast::ops::BinaryOp;
        // `start..` becomes `start..NULL`
        Box::new(BinaryOperatorBuilder::new(
            BinaryOp::Range,
            self.start,
            null(),
        ))
    }
}

impl<T> IntoExpressionBuilder for RangeInclusive<T>
where
    T: ExpressionBuilder + IntoExpressionBuilder,
{
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        use crate::tree::ast::ops::BinaryOp;
        let (start, end) = self.into_inner();
        Box::new(BinaryOperatorBuilder::new(
            BinaryOp::RangeInclusive,
            start,
            end,
        ))
    }
}

impl<T> IntoExpressionBuilder for RangeToInclusive<T>
where
    T: ExpressionBuilder + IntoExpressionBuilder,
{
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        use crate::tree::ast::ops::BinaryOp;
        // `..=end` becomes `NULL..=end`
        Box::new(BinaryOperatorBuilder::new(
            BinaryOp::RangeInclusive,
            null(),
            self.end,
        ))
    }
}

impl<T> IntoExpressionBuilder for RangeTo<T>
where
    T: ExpressionBuilder + IntoExpressionBuilder,
{
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        use crate::tree::ast::ops::BinaryOp;
        // `..end` becomes `NULL..end`
        Box::new(BinaryOperatorBuilder::new(
            BinaryOp::Range,
            null(),
            self.end,
        ))
    }
}

impl IntoExpressionBuilder for RangeFull {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        use crate::tree::ast::expression::UnboundRangeLiteral;
        use crate::tree::ast::node::Span;
        // `..` becomes UnboundRangeLiteral
        Box::new(ExpressionWrapper(Expression {
            span: Span::NONE,
            kind: UnboundRangeLiteral.into(),
        }))
    }
}

// Implement IntoExpressionBuilder for Box<dyn ExpressionBuilder>
impl IntoExpressionBuilder for Box<dyn ExpressionBuilder> {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        self
    }
}

// Implement IntoExpressionBuilder for specific builder types
// We can't do a blanket impl because it conflicts with other impls

// Implement IntoExpressionBuilder for existing Expression
impl IntoExpressionBuilder for Expression {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        Box::new(ExpressionWrapper(self))
    }
}

// Implement IntoExpressionBuilder for Arc<Expression>
impl IntoExpressionBuilder for Arc<Expression> {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        Box::new(ExpressionWrapper((*self).clone()))
    }
}

// Implement IntoExpressionBuilder for existing TypedExpression
impl IntoExpressionBuilder for TypedExpression {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        // Extract the AST from the typed expression
        Box::new(ExpressionWrapper((*self.ast).clone()))
    }
}

// Implement IntoExpressionBuilder for tree Identifier
impl IntoExpressionBuilder for Identifier {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        match self {
            // Simple identifier -> field reference
            Identifier::Simple(simple) => Box::new(field_ref(simple.as_str())),
            // Compound identifier -> nested field access (e.g., a.b.c -> field(field(field_ref("a"), "b"), "c"))
            Identifier::Compound(compound) => {
                // Start with the first part as a field reference
                let mut result: Box<dyn ExpressionBuilder> =
                    Box::new(field_ref(compound.first().as_str()));

                // Chain field accesses for the remaining parts
                for part in compound.rest_parts() {
                    result = Box::new(field(result, part.as_str()));
                }

                result
            }
        }
    }
}

// Also support &Identifier for convenience
impl IntoExpressionBuilder for &Identifier {
    fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
        self.clone().into_expression_builder()
    }
}

// Wrapper for existing Expression to implement ExpressionBuilder
#[derive(Debug)]
struct ExpressionWrapper(Expression);

impl ExpressionBuilder for ExpressionWrapper {
    fn build(&self) -> Expression {
        self.0.clone()
    }
}

// Convenience functions for creating builders

/// Create an integer literal
pub fn int(value: i64) -> IntLiteralBuilder {
    IntLiteralBuilder::new(value)
}

/// Create a double literal
pub fn double(value: f64) -> DoubleLiteralBuilder {
    DoubleLiteralBuilder::new(value)
}

/// Create a string literal
pub fn string(value: impl Into<String>) -> StringLiteralBuilder {
    StringLiteralBuilder::new(value)
}

/// Create a boolean literal
pub fn boolean(value: bool) -> BoolLiteralBuilder {
    BoolLiteralBuilder::new(value)
}

/// Create a null literal
pub fn null() -> NullLiteralBuilder {
    NullLiteralBuilder::new()
}

/// Create a decimal literal from string representation like "3.14"
pub fn decimal(value: &str) -> Result<DecimalLiteralBuilder, String> {
    DecimalLiteralBuilder::from_string(value)
}

/// Create a decimal literal from components
pub fn decimal_from_parts(
    unscaled_value: i128,
    precision: u32,
    scale: u32,
) -> DecimalLiteralBuilder {
    DecimalLiteralBuilder::new(unscaled_value, precision, scale)
}

// Interval literal convenience functions
use crate::tree::ast::expression::IntervalUnit;

/// Create an interval literal builder
pub fn interval(value: i64, unit: IntervalUnit) -> IntervalLiteralBuilder {
    IntervalLiteralBuilder::new(value, unit)
}

/// Create a millisecond interval
pub fn milliseconds(value: i64) -> IntervalLiteralBuilder {
    IntervalLiteralBuilder::new(value, IntervalUnit::Millisecond)
}

/// Create a second interval
pub fn seconds(value: i64) -> IntervalLiteralBuilder {
    IntervalLiteralBuilder::new(value, IntervalUnit::Second)
}

/// Create a minute interval
pub fn minutes(value: i64) -> IntervalLiteralBuilder {
    IntervalLiteralBuilder::new(value, IntervalUnit::Minute)
}

/// Create an hour interval
pub fn hours(value: i64) -> IntervalLiteralBuilder {
    IntervalLiteralBuilder::new(value, IntervalUnit::Hour)
}

/// Create a day interval
pub fn days(value: i64) -> IntervalLiteralBuilder {
    IntervalLiteralBuilder::new(value, IntervalUnit::Day)
}

/// Create a week interval
pub fn weeks(value: i64) -> IntervalLiteralBuilder {
    IntervalLiteralBuilder::new(value, IntervalUnit::Week)
}

/// Create a month interval
pub fn months(value: i64) -> IntervalLiteralBuilder {
    IntervalLiteralBuilder::new(value, IntervalUnit::Month)
}

/// Create a quarter interval
pub fn quarters(value: i64) -> IntervalLiteralBuilder {
    IntervalLiteralBuilder::new(value, IntervalUnit::Quarter)
}

/// Create a year interval
pub fn years(value: i64) -> IntervalLiteralBuilder {
    IntervalLiteralBuilder::new(value, IntervalUnit::Year)
}

#[cfg(test)]
mod tests;