• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

getdozer / dozer / 4105700744

pending completion
4105700744

Pull #814

github

GitHub
Merge d70d4d25f into 016b3ada5
Pull Request #814: Bump clap from 4.0.32 to 4.1.4

23457 of 37651 relevant lines covered (62.3%)

44725.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.48
/dozer-sql/src/pipeline/expression/builder.rs
1
use dozer_types::{
2
    ordered_float::OrderedFloat,
3
    types::{Field, FieldDefinition, Schema, SourceDefinition},
4
};
5

6
use sqlparser::ast::{
7
    BinaryOperator as SqlBinaryOperator, DataType, Expr as SqlExpr, Expr, Function, FunctionArg,
8
    FunctionArgExpr, Ident, TrimWhereField, UnaryOperator as SqlUnaryOperator, Value as SqlValue,
9
};
10

11
use crate::pipeline::errors::PipelineError;
12
use crate::pipeline::errors::PipelineError::{
13
    AmbiguousFieldIdentifier, IllegalFieldIdentifier, UnknownFieldIdentifier,
14
};
15
use crate::pipeline::expression::aggregate::AggregateFunctionType;
16
use crate::pipeline::expression::builder::PipelineError::InvalidArgument;
17
use crate::pipeline::expression::builder::PipelineError::InvalidExpression;
18
use crate::pipeline::expression::builder::PipelineError::InvalidOperator;
19
use crate::pipeline::expression::builder::PipelineError::InvalidValue;
20
use crate::pipeline::expression::execution::Expression;
21
use crate::pipeline::expression::execution::Expression::ScalarFunction;
22
use crate::pipeline::expression::operator::{BinaryOperatorType, UnaryOperatorType};
23
use crate::pipeline::expression::scalar::common::ScalarFunctionType;
24
use crate::pipeline::expression::scalar::string::TrimType;
25

26
use super::cast::CastOperatorType;
27

28
pub type Bypass = bool;
29

30
pub enum BuilderExpressionType {
31
    PreAggregation,
32
    Aggregation,
33
    // PostAggregation,
34
    FullExpression,
35
}
36
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1,902✔
37
pub struct NameOrAlias(pub String, pub Option<String>);
38

39
pub struct ExpressionBuilder;
40

41
impl ExpressionBuilder {
42
    pub fn build(
186✔
43
        &self,
186✔
44
        expression_type: &BuilderExpressionType,
186✔
45
        sql_expression: &SqlExpr,
186✔
46
        schema: &Schema,
186✔
47
    ) -> Result<Box<Expression>, PipelineError> {
186✔
48
        let (expression, _bypass) =
186✔
49
            self.parse_sql_expression(expression_type, sql_expression, schema)?;
186✔
50
        Ok(expression)
186✔
51
    }
186✔
52

53
    pub fn parse_sql_expression(
54
        &self,
55
        expression_type: &BuilderExpressionType,
56
        expression: &SqlExpr,
57
        schema: &Schema,
58
    ) -> Result<(Box<Expression>, bool), PipelineError> {
59
        match expression {
89✔
60
            SqlExpr::Trim {
61
                expr,
62✔
62
                trim_where,
62✔
63
                trim_what,
62✔
64
            } => self.parse_sql_trim_function(expression_type, expr, trim_where, trim_what, schema),
62✔
65
            SqlExpr::Identifier(ident) => Ok((parse_sql_column(&[ident.clone()], schema)?, false)),
2,393✔
66
            SqlExpr::CompoundIdentifier(ident) => Ok((parse_sql_column(ident, schema)?, false)),
386✔
67
            SqlExpr::Value(SqlValue::Number(n, _)) => self.parse_sql_number(n),
57✔
68
            SqlExpr::Value(SqlValue::Null) => {
69
                Ok((Box::new(Expression::Literal(Field::Null)), false))
×
70
            }
71
            SqlExpr::Value(SqlValue::SingleQuotedString(s) | SqlValue::DoubleQuotedString(s)) => {
32✔
72
                parse_sql_string(s)
32✔
73
            }
74
            SqlExpr::UnaryOp { expr, op } => {
×
75
                self.parse_sql_unary_op(expression_type, op, expr, schema)
×
76
            }
77
            SqlExpr::BinaryOp { left, op, right } => {
105✔
78
                self.parse_sql_binary_op(expression_type, left, op, right, schema)
105✔
79
            }
80
            SqlExpr::Nested(expr) => self.parse_sql_expression(expression_type, expr, schema),
16✔
81
            SqlExpr::Function(sql_function) => match expression_type {
328✔
82
                BuilderExpressionType::PreAggregation => self.parse_sql_function_pre_aggregation(
156✔
83
                    expression_type,
156✔
84
                    sql_function,
156✔
85
                    schema,
156✔
86
                    expression,
156✔
87
                ),
156✔
88
                BuilderExpressionType::Aggregation => self.parse_sql_function_aggregation(
156✔
89
                    expression_type,
156✔
90
                    sql_function,
156✔
91
                    schema,
156✔
92
                    expression,
156✔
93
                ),
156✔
94
                // ExpressionType::PostAggregation => todo!(),
95
                BuilderExpressionType::FullExpression => {
96
                    self.parse_sql_function(expression_type, sql_function, schema, expression)
16✔
97
                }
98
            },
99
            SqlExpr::Like {
100
                negated,
×
101
                expr,
×
102
                pattern,
×
103
                escape_char,
×
104
            } => self.parse_sql_like_operator(
×
105
                expression_type,
×
106
                negated,
×
107
                expr,
×
108
                pattern,
×
109
                escape_char,
×
110
                schema,
×
111
            ),
×
112
            SqlExpr::Cast { expr, data_type } => {
64✔
113
                self.parse_sql_cast_operator(expression_type, expr, data_type, schema)
64✔
114
            }
115
            _ => Err(InvalidExpression(format!("{expression:?}"))),
×
116
        }
117
    }
3,445✔
118

119
    fn parse_sql_trim_function(
62✔
120
        &self,
62✔
121
        expression_type: &BuilderExpressionType,
62✔
122
        expr: &Expr,
62✔
123
        trim_where: &Option<TrimWhereField>,
62✔
124
        trim_what: &Option<Box<Expr>>,
62✔
125
        schema: &Schema,
62✔
126
    ) -> Result<(Box<Expression>, bool), PipelineError> {
62✔
127
        let arg = self.parse_sql_expression(expression_type, expr, schema)?.0;
62✔
128
        let what = match trim_what {
62✔
129
            Some(e) => Some(self.parse_sql_expression(expression_type, e, schema)?.0),
8✔
130
            _ => None,
54✔
131
        };
132
        let typ = trim_where.as_ref().map(|e| match e {
62✔
133
            TrimWhereField::Both => TrimType::Both,
2✔
134
            TrimWhereField::Leading => TrimType::Leading,
2✔
135
            TrimWhereField::Trailing => TrimType::Trailing,
2✔
136
        });
62✔
137
        Ok((Box::new(Expression::Trim { arg, what, typ }), false))
62✔
138
    }
62✔
139

140
    fn parse_sql_function(
16✔
141
        &self,
16✔
142
        expression_type: &BuilderExpressionType,
16✔
143
        sql_function: &Function,
16✔
144
        schema: &Schema,
16✔
145
        expression: &SqlExpr,
16✔
146
    ) -> Result<(Box<Expression>, bool), PipelineError> {
16✔
147
        let name = sql_function.name.to_string().to_lowercase();
16✔
148
        if let Ok(function) = ScalarFunctionType::new(&name) {
16✔
149
            let mut arg_exprs = vec![];
15✔
150
            for arg in &sql_function.args {
37✔
151
                let r = self.parse_sql_function_arg(expression_type, arg, schema);
22✔
152
                match r {
22✔
153
                    Ok(result) => {
22✔
154
                        if result.1 {
22✔
155
                            return Ok(result);
×
156
                        } else {
22✔
157
                            arg_exprs.push(*result.0);
22✔
158
                        }
22✔
159
                    }
160
                    Err(error) => {
×
161
                        return Err(error);
×
162
                    }
163
                }
164
            }
165

166
            return Ok((
15✔
167
                Box::new(ScalarFunction {
15✔
168
                    fun: function,
15✔
169
                    args: arg_exprs,
15✔
170
                }),
15✔
171
                false,
15✔
172
            ));
15✔
173
        };
1✔
174
        if AggregateFunctionType::new(&name).is_ok() {
1✔
175
            let arg = sql_function.args.first().unwrap();
1✔
176
            let r = self.parse_sql_function_arg(expression_type, arg, schema)?;
1✔
177
            return Ok((r.0, false)); // switch bypass to true, since the argument of this Aggregation must be the final result
1✔
178
        };
×
179
        Err(InvalidExpression(format!("{expression:?}")))
×
180
    }
16✔
181

182
    fn parse_sql_function_pre_aggregation(
156✔
183
        &self,
156✔
184
        expression_type: &BuilderExpressionType,
156✔
185
        sql_function: &Function,
156✔
186
        schema: &Schema,
156✔
187
        expression: &SqlExpr,
156✔
188
    ) -> Result<(Box<Expression>, bool), PipelineError> {
156✔
189
        let name = sql_function.name.to_string().to_lowercase();
156✔
190

191
        if let Ok(function) = ScalarFunctionType::new(&name) {
156✔
192
            let mut arg_exprs = vec![];
×
193
            for arg in &sql_function.args {
×
194
                let r = self.parse_sql_function_arg(expression_type, arg, schema);
×
195
                match r {
×
196
                    Ok(result) => {
×
197
                        if result.1 {
×
198
                            return Ok(result);
×
199
                        } else {
×
200
                            arg_exprs.push(*result.0);
×
201
                        }
×
202
                    }
203
                    Err(error) => {
×
204
                        return Err(error);
×
205
                    }
206
                }
207
            }
208

209
            return Ok((
×
210
                Box::new(ScalarFunction {
×
211
                    fun: function,
×
212
                    args: arg_exprs,
×
213
                }),
×
214
                false,
×
215
            ));
×
216
        };
156✔
217
        if AggregateFunctionType::new(&name).is_ok() {
156✔
218
            let arg = sql_function.args.first().unwrap();
156✔
219
            let r = self.parse_sql_function_arg(expression_type, arg, schema)?;
156✔
220
            return Ok((r.0, true)); // switch bypass to true, since the argument of this Aggregation must be the final result
156✔
221
        };
×
222
        Err(InvalidExpression(format!("{expression:?}")))
×
223
    }
156✔
224

225
    fn parse_sql_function_aggregation(
156✔
226
        &self,
156✔
227
        expression_type: &BuilderExpressionType,
156✔
228
        sql_function: &Function,
156✔
229
        schema: &Schema,
156✔
230
        expression: &SqlExpr,
156✔
231
    ) -> Result<(Box<Expression>, bool), PipelineError> {
156✔
232
        let name = sql_function.name.to_string().to_lowercase();
156✔
233

234
        if let Ok(function) = ScalarFunctionType::new(&name) {
156✔
235
            let mut arg_exprs = vec![];
×
236
            for arg in &sql_function.args {
×
237
                let r = self.parse_sql_function_arg(expression_type, arg, schema);
×
238
                match r {
×
239
                    Ok(result) => {
×
240
                        if result.1 {
×
241
                            return Ok(result);
×
242
                        } else {
×
243
                            arg_exprs.push(*result.0);
×
244
                        }
×
245
                    }
246
                    Err(error) => {
×
247
                        return Err(error);
×
248
                    }
249
                }
250
            }
251

252
            return Ok((
×
253
                Box::new(ScalarFunction {
×
254
                    fun: function,
×
255
                    args: arg_exprs,
×
256
                }),
×
257
                false,
×
258
            ));
×
259
        };
156✔
260

261
        if let Ok(function) = AggregateFunctionType::new(&name) {
156✔
262
            let mut arg_exprs = vec![];
156✔
263
            for arg in &sql_function.args {
312✔
264
                let r = self.parse_sql_function_arg(expression_type, arg, schema);
156✔
265
                match r {
156✔
266
                    Ok(result) => {
156✔
267
                        if result.1 {
156✔
268
                            return Ok(result);
×
269
                        } else {
156✔
270
                            arg_exprs.push(*result.0);
156✔
271
                        }
156✔
272
                    }
273
                    Err(error) => {
×
274
                        return Err(error);
×
275
                    }
276
                }
277
            }
278

279
            return Ok((
156✔
280
                Box::new(Expression::AggregateFunction {
156✔
281
                    fun: function,
156✔
282
                    args: arg_exprs,
156✔
283
                }),
156✔
284
                true, // switch bypass to true, since this Aggregation must be the final result
156✔
285
            ));
156✔
286
        };
×
287

×
288
        Err(InvalidExpression(format!(
×
289
            "Unsupported Expression: {expression:?}"
×
290
        )))
×
291
    }
156✔
292

293
    fn parse_sql_function_arg(
294
        &self,
295
        expression_type: &BuilderExpressionType,
296
        argument: &FunctionArg,
297
        schema: &Schema,
298
    ) -> Result<(Box<Expression>, bool), PipelineError> {
299
        match argument {
335✔
300
            FunctionArg::Named {
301
                name: _,
302
                arg: FunctionArgExpr::Expr(arg),
×
303
            } => self.parse_sql_expression(expression_type, arg, schema),
×
304
            FunctionArg::Named {
305
                name: _,
306
                arg: FunctionArgExpr::Wildcard,
307
            } => Err(InvalidArgument(format!("{argument:?}"))),
×
308
            FunctionArg::Unnamed(FunctionArgExpr::Expr(arg)) => {
335✔
309
                self.parse_sql_expression(expression_type, arg, schema)
335✔
310
            }
311
            FunctionArg::Unnamed(FunctionArgExpr::Wildcard) => {
312
                Err(InvalidArgument(format!("{argument:?}")))
×
313
            }
314
            _ => Err(InvalidArgument(format!("{argument:?}"))),
×
315
        }
316
    }
335✔
317

318
    fn parse_sql_unary_op(
×
319
        &self,
×
320
        expression_type: &BuilderExpressionType,
×
321
        op: &SqlUnaryOperator,
×
322
        expr: &SqlExpr,
×
323
        schema: &Schema,
×
324
    ) -> Result<(Box<Expression>, Bypass), PipelineError> {
×
325
        let (arg, bypass) = self.parse_sql_expression(expression_type, expr, schema)?;
×
326
        if bypass {
×
327
            return Ok((arg, bypass));
×
328
        }
×
329

330
        let operator = match op {
×
331
            SqlUnaryOperator::Not => UnaryOperatorType::Not,
×
332
            SqlUnaryOperator::Plus => UnaryOperatorType::Plus,
×
333
            SqlUnaryOperator::Minus => UnaryOperatorType::Minus,
×
334
            _ => return Err(InvalidOperator(format!("{op:?}"))),
×
335
        };
336

337
        Ok((Box::new(Expression::UnaryOperator { operator, arg }), false))
×
338
    }
×
339

340
    fn parse_sql_binary_op(
105✔
341
        &self,
105✔
342
        expression_type: &BuilderExpressionType,
105✔
343
        left: &SqlExpr,
105✔
344
        op: &SqlBinaryOperator,
105✔
345
        right: &SqlExpr,
105✔
346
        schema: &Schema,
105✔
347
    ) -> Result<(Box<Expression>, bool), PipelineError> {
105✔
348
        let (left_op, bypass_left) = self.parse_sql_expression(expression_type, left, schema)?;
105✔
349
        if bypass_left {
105✔
350
            return Ok((left_op, bypass_left));
×
351
        }
105✔
352
        let (right_op, bypass_right) = self.parse_sql_expression(expression_type, right, schema)?;
105✔
353
        if bypass_right {
105✔
354
            return Ok((right_op, bypass_right));
×
355
        }
105✔
356

357
        let operator = match op {
105✔
358
            SqlBinaryOperator::Gt => BinaryOperatorType::Gt,
24✔
359
            SqlBinaryOperator::GtEq => BinaryOperatorType::Gte,
1✔
360
            SqlBinaryOperator::Lt => BinaryOperatorType::Lt,
16✔
361
            SqlBinaryOperator::LtEq => BinaryOperatorType::Lte,
16✔
362
            SqlBinaryOperator::Eq => BinaryOperatorType::Eq,
24✔
363
            SqlBinaryOperator::NotEq => BinaryOperatorType::Ne,
×
364

365
            SqlBinaryOperator::Plus => BinaryOperatorType::Add,
×
366
            SqlBinaryOperator::Minus => BinaryOperatorType::Sub,
×
367
            SqlBinaryOperator::Multiply => BinaryOperatorType::Mul,
×
368
            SqlBinaryOperator::Divide => BinaryOperatorType::Div,
×
369
            SqlBinaryOperator::Modulo => BinaryOperatorType::Mod,
×
370

371
            SqlBinaryOperator::And => BinaryOperatorType::And,
16✔
372
            SqlBinaryOperator::Or => BinaryOperatorType::Or,
8✔
373

374
            // BinaryOperator::BitwiseAnd => ...
375
            // BinaryOperator::BitwiseOr => ...
376
            // BinaryOperator::StringConcat => ...
377
            _ => return Err(InvalidOperator(format!("{op:?}"))),
×
378
        };
379

380
        Ok((
105✔
381
            Box::new(Expression::BinaryOperator {
105✔
382
                left: left_op,
105✔
383
                operator,
105✔
384
                right: right_op,
105✔
385
            }),
105✔
386
            false,
105✔
387
        ))
105✔
388
    }
105✔
389

390
    fn parse_sql_number(&self, n: &str) -> Result<(Box<Expression>, Bypass), PipelineError> {
57✔
391
        match n.parse::<i64>() {
57✔
392
            Ok(n) => Ok((Box::new(Expression::Literal(Field::Int(n))), false)),
57✔
393
            Err(_) => match n.parse::<f64>() {
×
394
                Ok(f) => Ok((
×
395
                    Box::new(Expression::Literal(Field::Float(OrderedFloat(f)))),
×
396
                    false,
×
397
                )),
×
398
                Err(_) => Err(InvalidValue(n.to_string())),
×
399
            },
400
        }
401
    }
57✔
402

403
    fn parse_sql_like_operator(
×
404
        &self,
×
405
        expression_type: &BuilderExpressionType,
×
406
        negated: &bool,
×
407
        expr: &Expr,
×
408
        pattern: &Expr,
×
409
        escape_char: &Option<char>,
×
410
        schema: &Schema,
×
411
    ) -> Result<(Box<Expression>, bool), PipelineError> {
×
412
        let arg = self.parse_sql_expression(expression_type, expr, schema)?;
×
413
        let pattern = self.parse_sql_expression(expression_type, pattern, schema)?;
×
414
        let like_expression = Box::new(Expression::Like {
×
415
            arg: arg.0,
×
416
            pattern: pattern.0,
×
417
            escape: *escape_char,
×
418
        });
×
419
        if *negated {
×
420
            Ok((
×
421
                Box::new(Expression::UnaryOperator {
×
422
                    operator: UnaryOperatorType::Not,
×
423
                    arg: like_expression,
×
424
                }),
×
425
                arg.1,
×
426
            ))
×
427
        } else {
428
            Ok((like_expression, arg.1))
×
429
        }
430
    }
×
431

432
    fn parse_sql_cast_operator(
64✔
433
        &self,
64✔
434
        expression_type: &BuilderExpressionType,
64✔
435
        expr: &Expr,
64✔
436
        data_type: &DataType,
64✔
437
        schema: &Schema,
64✔
438
    ) -> Result<(Box<Expression>, bool), PipelineError> {
64✔
439
        let expression = self.parse_sql_expression(expression_type, expr, schema)?;
64✔
440
        let cast_to = match data_type {
64✔
441
            DataType::Decimal(_) => CastOperatorType::Decimal,
×
442
            DataType::Binary(_) => CastOperatorType::Binary,
×
443
            DataType::Float(_) => CastOperatorType::Float,
10✔
444
            DataType::Int(_) => CastOperatorType::Int,
6✔
445
            DataType::Integer(_) => CastOperatorType::Int,
×
446
            DataType::UnsignedInt(_) => CastOperatorType::UInt,
×
447
            DataType::UnsignedInteger(_) => CastOperatorType::UInt,
×
448
            DataType::Boolean => CastOperatorType::Boolean,
12✔
449
            DataType::Date => CastOperatorType::Date,
×
450
            DataType::Timestamp(..) => CastOperatorType::Timestamp,
×
451
            DataType::Text => CastOperatorType::Text,
18✔
452
            DataType::String => CastOperatorType::String,
18✔
453
            DataType::Custom(name, ..) => {
×
454
                if name.to_string().to_lowercase() == "bson" {
×
455
                    CastOperatorType::Bson
×
456
                } else {
457
                    Err(PipelineError::InvalidFunction(format!(
×
458
                        "Unsupported Cast type {name}"
×
459
                    )))?
×
460
                }
461
            }
462
            _ => Err(PipelineError::InvalidFunction(format!(
×
463
                "Unsupported Cast type {data_type}"
×
464
            )))?,
×
465
        };
466
        Ok((
64✔
467
            Box::new(Expression::Cast {
64✔
468
                arg: expression.0,
64✔
469
                typ: cast_to,
64✔
470
            }),
64✔
471
            expression.1,
64✔
472
        ))
64✔
473
    }
64✔
474
}
475

476
pub fn fullname_from_ident(ident: &[Ident]) -> String {
66✔
477
    let mut ident_tokens = vec![];
66✔
478
    for token in ident.iter() {
66✔
479
        ident_tokens.push(token.value.clone());
66✔
480
    }
66✔
481
    ident_tokens.join(".")
66✔
482
}
66✔
483

484
fn parse_sql_string(s: &str) -> Result<(Box<Expression>, bool), PipelineError> {
32✔
485
    Ok((
32✔
486
        Box::new(Expression::Literal(Field::String(s.to_owned()))),
32✔
487
        false,
32✔
488
    ))
32✔
489
}
32✔
490

491
pub(crate) fn normalize_ident(id: &Ident) -> String {
196✔
492
    match id.quote_style {
196✔
493
        Some(_) => id.value.clone(),
×
494
        None => id.value.clone(),
196✔
495
    }
496
}
196✔
497

498
pub fn extend_schema_source_def(schema: &Schema, name: &NameOrAlias) -> Schema {
530✔
499
    let mut output_schema = schema.clone();
530✔
500
    let mut fields = vec![];
530✔
501
    for mut field in schema.clone().fields.into_iter() {
2,346✔
502
        if let Some(alias) = &name.1 {
2,346✔
503
            field.source = SourceDefinition::Alias {
816✔
504
                name: alias.to_string(),
816✔
505
            };
816✔
506
        }
1,530✔
507

508
        fields.push(field);
2,346✔
509
    }
510
    output_schema.fields = fields;
530✔
511

530✔
512
    output_schema
530✔
513
}
530✔
514

515
fn parse_sql_column(ident: &[Ident], schema: &Schema) -> Result<Box<Expression>, PipelineError> {
2,781✔
516
    let (src_field, src_table_or_alias, src_connection) = match ident.len() {
2,781✔
517
        1 => (&ident[0].value, None, None),
2,395✔
518
        2 => (&ident[1].value, Some(&ident[0].value), None),
386✔
519
        3 => (
×
520
            &ident[2].value,
×
521
            Some(&ident[1].value),
×
522
            Some(&ident[0].value),
×
523
        ),
×
524
        _ => {
525
            return Err(IllegalFieldIdentifier(
×
526
                ident
×
527
                    .iter()
×
528
                    .fold(String::new(), |a, b| a + "." + b.value.as_str()),
×
529
            ))
×
530
        }
531
    };
532

533
    let matching_by_field: Vec<(usize, &FieldDefinition)> = schema
2,781✔
534
        .fields
2,781✔
535
        .iter()
2,781✔
536
        .enumerate()
2,781✔
537
        .filter(|(_idx, f)| &f.name == src_field)
13,301✔
538
        .collect();
2,781✔
539

2,781✔
540
    match matching_by_field.len() {
2,781✔
541
        0 => Err(UnknownFieldIdentifier(
×
542
            ident
×
543
                .iter()
×
544
                .fold(String::new(), |a, b| a + "." + b.value.as_str()),
×
545
        )),
×
546
        1 => Ok(Box::new(Expression::Column {
2,589✔
547
            index: matching_by_field[0].0,
2,589✔
548
        })),
2,589✔
549
        _ => match src_table_or_alias {
192✔
550
            None => Err(AmbiguousFieldIdentifier(
×
551
                ident
×
552
                    .iter()
×
553
                    .fold(String::new(), |a, b| a + "." + b.value.as_str()),
×
554
            )),
×
555
            Some(src_table_or_alias) => {
192✔
556
                let matching_by_table_or_alias: Vec<(usize, &FieldDefinition)> = matching_by_field
192✔
557
                    .into_iter()
192✔
558
                    .filter(|(_idx, field)| match &field.source {
384✔
559
                        SourceDefinition::Alias { name } => name == src_table_or_alias,
288✔
560
                        SourceDefinition::Table {
561
                            name,
96✔
562
                            connection: _,
96✔
563
                        } => name == src_table_or_alias,
96✔
564
                        _ => false,
×
565
                    })
384✔
566
                    .collect();
192✔
567

192✔
568
                match matching_by_table_or_alias.len() {
192✔
569
                    0 => Err(UnknownFieldIdentifier(
×
570
                        ident
×
571
                            .iter()
×
572
                            .fold(String::new(), |a, b| a + "." + b.value.as_str()),
×
573
                    )),
×
574
                    1 => Ok(Box::new(Expression::Column {
192✔
575
                        index: matching_by_table_or_alias[0].0,
192✔
576
                    })),
192✔
577
                    _ => match src_connection {
×
578
                        None => Err(InvalidExpression(
×
579
                            ident
×
580
                                .iter()
×
581
                                .fold(String::new(), |a, b| a + "." + b.value.as_str()),
×
582
                        )),
×
583
                        Some(src_connection) => {
×
584
                            let matching_by_connection: Vec<(usize, &FieldDefinition)> =
×
585
                                matching_by_table_or_alias
×
586
                                    .into_iter()
×
587
                                    .filter(|(_idx, field)| match &field.source {
×
588
                                        SourceDefinition::Table {
589
                                            name: _,
590
                                            connection,
×
591
                                        } => connection == src_connection,
×
592
                                        _ => false,
×
593
                                    })
×
594
                                    .collect();
×
595

×
596
                            match matching_by_connection.len() {
×
597
                                0 => Err(UnknownFieldIdentifier(
×
598
                                    ident
×
599
                                        .iter()
×
600
                                        .fold(String::new(), |a, b| a + "." + b.value.as_str()),
×
601
                                )),
×
602
                                1 => Ok(Box::new(Expression::Column {
×
603
                                    index: matching_by_connection[0].0,
×
604
                                })),
×
605
                                _ => Err(InvalidExpression(
×
606
                                    ident
×
607
                                        .iter()
×
608
                                        .fold(String::new(), |a, b| a + "." + b.value.as_str()),
×
609
                                )),
×
610
                            }
611
                        }
612
                    },
613
                }
614
            }
615
        },
616
    }
617
}
2,781✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc