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

getdozer / dozer / 4763034755

pending completion
4763034755

Pull #1460

github

GitHub
Merge 2e63b376c into c58df4a0b
Pull Request #1460: Update init.rs

1 of 1 new or added line in 1 file covered. (100.0%)

34417 of 43846 relevant lines covered (78.5%)

12365.38 hits per line

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

59.13
/dozer-sql/src/pipeline/expression/cast.rs
1
use std::fmt::{Display, Formatter};
2

3
use dozer_types::{
4
    ordered_float::OrderedFloat,
5
    types::{Field, FieldType, Record, Schema},
6
};
7

8
use crate::pipeline::errors::{FieldTypes, PipelineError};
9

10
use super::execution::{Expression, ExpressionExecutor, ExpressionType};
11

12
#[allow(dead_code)]
13
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
46✔
14
pub enum CastOperatorType {
15
    UInt,
16
    U128,
17
    Int,
18
    I128,
19
    Float,
20
    Boolean,
21
    String,
22
    Text,
23
    Binary,
24
    Decimal,
25
    Timestamp,
26
    Date,
27
    Bson,
28
}
29

30
impl Display for CastOperatorType {
31
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
×
32
        match self {
×
33
            CastOperatorType::UInt => f.write_str("CAST AS UINT"),
×
34
            CastOperatorType::U128 => f.write_str("CAST AS U128"),
×
35
            CastOperatorType::Int => f.write_str("CAST AS INT"),
×
36
            CastOperatorType::I128 => f.write_str("CAST AS I128"),
×
37
            CastOperatorType::Float => f.write_str("CAST AS FLOAT"),
×
38
            CastOperatorType::Boolean => f.write_str("CAST AS BOOLEAN"),
×
39
            CastOperatorType::String => f.write_str("CAST AS STRING"),
×
40
            CastOperatorType::Text => f.write_str("CAST AS TEXT"),
×
41
            CastOperatorType::Binary => f.write_str("CAST AS BINARY"),
×
42
            CastOperatorType::Decimal => f.write_str("CAST AS DECIMAL"),
×
43
            CastOperatorType::Timestamp => f.write_str("CAST AS TIMESTAMP"),
×
44
            CastOperatorType::Date => f.write_str("CAST AS DATE"),
×
45
            CastOperatorType::Bson => f.write_str("CAST AS BSON"),
×
46
        }
47
    }
×
48
}
49

50
impl CastOperatorType {
51
    pub(crate) fn evaluate(
95✔
52
        &self,
95✔
53
        schema: &Schema,
95✔
54
        arg: &Expression,
95✔
55
        record: &Record,
95✔
56
    ) -> Result<Field, PipelineError> {
95✔
57
        let field = arg.evaluate(record, schema)?;
95✔
58
        match self {
95✔
59
            CastOperatorType::UInt => {
60
                if let Some(value) = field.to_uint() {
3✔
61
                    Ok(Field::UInt(value))
3✔
62
                } else {
63
                    Err(PipelineError::InvalidCast {
×
64
                        from: field,
×
65
                        to: FieldType::UInt,
×
66
                    })
×
67
                }
68
            }
69
            CastOperatorType::U128 => {
70
                if let Some(value) = field.to_u128() {
3✔
71
                    Ok(Field::U128(value))
3✔
72
                } else {
73
                    Err(PipelineError::InvalidCast {
×
74
                        from: field,
×
75
                        to: FieldType::U128,
×
76
                    })
×
77
                }
78
            }
79
            CastOperatorType::Int => {
80
                if let Some(value) = field.to_int() {
53✔
81
                    Ok(Field::Int(value))
53✔
82
                } else {
83
                    Err(PipelineError::InvalidCast {
×
84
                        from: field,
×
85
                        to: FieldType::Int,
×
86
                    })
×
87
                }
88
            }
89
            CastOperatorType::I128 => {
90
                if let Some(value) = field.to_i128() {
3✔
91
                    Ok(Field::I128(value))
3✔
92
                } else {
93
                    Err(PipelineError::InvalidCast {
×
94
                        from: field,
×
95
                        to: FieldType::I128,
×
96
                    })
×
97
                }
98
            }
99
            CastOperatorType::Float => {
100
                if let Some(value) = field.to_float() {
6✔
101
                    Ok(Field::Float(OrderedFloat(value)))
6✔
102
                } else {
103
                    Err(PipelineError::InvalidCast {
×
104
                        from: field,
×
105
                        to: FieldType::Float,
×
106
                    })
×
107
                }
108
            }
109
            CastOperatorType::Boolean => {
110
                if let Some(value) = field.to_boolean() {
6✔
111
                    Ok(Field::Boolean(value))
6✔
112
                } else {
113
                    Err(PipelineError::InvalidCast {
×
114
                        from: field,
×
115
                        to: FieldType::Boolean,
×
116
                    })
×
117
                }
118
            }
119
            CastOperatorType::String => {
120
                if let Some(value) = field.to_string() {
12✔
121
                    Ok(Field::String(value))
12✔
122
                } else {
123
                    Err(PipelineError::InvalidCast {
×
124
                        from: field,
×
125
                        to: FieldType::String,
×
126
                    })
×
127
                }
128
            }
129
            CastOperatorType::Text => {
130
                if let Some(value) = field.to_text() {
9✔
131
                    Ok(Field::Text(value))
9✔
132
                } else {
133
                    Err(PipelineError::InvalidCast {
×
134
                        from: field,
×
135
                        to: FieldType::Text,
×
136
                    })
×
137
                }
138
            }
139
            CastOperatorType::Binary => {
140
                if let Some(value) = field.to_binary() {
×
141
                    Ok(Field::Binary(value.to_vec()))
×
142
                } else {
143
                    Err(PipelineError::InvalidCast {
×
144
                        from: field,
×
145
                        to: FieldType::Binary,
×
146
                    })
×
147
                }
148
            }
149
            CastOperatorType::Decimal => {
150
                if let Some(value) = field.to_decimal() {
×
151
                    Ok(Field::Decimal(value))
×
152
                } else {
153
                    Err(PipelineError::InvalidCast {
×
154
                        from: field,
×
155
                        to: FieldType::Decimal,
×
156
                    })
×
157
                }
158
            }
159
            CastOperatorType::Timestamp => {
160
                if let Some(value) = field.to_timestamp()? {
×
161
                    Ok(Field::Timestamp(value))
×
162
                } else {
163
                    Err(PipelineError::InvalidCast {
×
164
                        from: field,
×
165
                        to: FieldType::Timestamp,
×
166
                    })
×
167
                }
168
            }
169
            CastOperatorType::Date => {
170
                if let Some(value) = field.to_date()? {
×
171
                    Ok(Field::Date(value))
×
172
                } else {
173
                    Err(PipelineError::InvalidCast {
×
174
                        from: field,
×
175
                        to: FieldType::Date,
×
176
                    })
×
177
                }
178
            }
179
            CastOperatorType::Bson => {
180
                if let Some(value) = field.to_bson() {
×
181
                    Ok(Field::Bson(value.to_vec()))
×
182
                } else {
183
                    Err(PipelineError::InvalidCast {
×
184
                        from: field,
×
185
                        to: FieldType::Bson,
×
186
                    })
×
187
                }
188
            }
189
        }
190
    }
95✔
191

192
    pub(crate) fn get_return_type(
46✔
193
        &self,
46✔
194
        schema: &Schema,
46✔
195
        arg: &Expression,
46✔
196
    ) -> Result<ExpressionType, PipelineError> {
46✔
197
        let (expected_input_type, return_type) = match self {
46✔
198
            CastOperatorType::UInt => (
3✔
199
                vec![
3✔
200
                    FieldType::Int,
3✔
201
                    FieldType::String,
3✔
202
                    FieldType::UInt,
3✔
203
                    FieldType::I128,
3✔
204
                    FieldType::U128,
3✔
205
                ],
3✔
206
                FieldType::UInt,
3✔
207
            ),
3✔
208
            CastOperatorType::U128 => (
3✔
209
                vec![
3✔
210
                    FieldType::Int,
3✔
211
                    FieldType::String,
3✔
212
                    FieldType::UInt,
3✔
213
                    FieldType::I128,
3✔
214
                    FieldType::U128,
3✔
215
                ],
3✔
216
                FieldType::U128,
3✔
217
            ),
3✔
218
            CastOperatorType::Int => (
3✔
219
                vec![
3✔
220
                    FieldType::Int,
3✔
221
                    FieldType::String,
3✔
222
                    FieldType::UInt,
3✔
223
                    FieldType::I128,
3✔
224
                    FieldType::U128,
3✔
225
                ],
3✔
226
                FieldType::Int,
3✔
227
            ),
3✔
228
            CastOperatorType::I128 => (
3✔
229
                vec![
3✔
230
                    FieldType::Int,
3✔
231
                    FieldType::String,
3✔
232
                    FieldType::UInt,
3✔
233
                    FieldType::I128,
3✔
234
                    FieldType::U128,
3✔
235
                ],
3✔
236
                FieldType::I128,
3✔
237
            ),
3✔
238
            CastOperatorType::Float => (
6✔
239
                vec![
6✔
240
                    FieldType::Decimal,
6✔
241
                    FieldType::Float,
6✔
242
                    FieldType::Int,
6✔
243
                    FieldType::I128,
6✔
244
                    FieldType::String,
6✔
245
                    FieldType::UInt,
6✔
246
                    FieldType::U128,
6✔
247
                ],
6✔
248
                FieldType::Float,
6✔
249
            ),
6✔
250
            CastOperatorType::Boolean => (
6✔
251
                vec![
6✔
252
                    FieldType::Boolean,
6✔
253
                    FieldType::Decimal,
6✔
254
                    FieldType::Float,
6✔
255
                    FieldType::Int,
6✔
256
                    FieldType::I128,
6✔
257
                    FieldType::UInt,
6✔
258
                    FieldType::U128,
6✔
259
                ],
6✔
260
                FieldType::Boolean,
6✔
261
            ),
6✔
262
            CastOperatorType::String => (
13✔
263
                vec![
13✔
264
                    FieldType::Binary,
13✔
265
                    FieldType::Boolean,
13✔
266
                    FieldType::Date,
13✔
267
                    FieldType::Decimal,
13✔
268
                    FieldType::Float,
13✔
269
                    FieldType::Int,
13✔
270
                    FieldType::I128,
13✔
271
                    FieldType::String,
13✔
272
                    FieldType::Text,
13✔
273
                    FieldType::Timestamp,
13✔
274
                    FieldType::UInt,
13✔
275
                    FieldType::U128,
13✔
276
                ],
13✔
277
                FieldType::String,
13✔
278
            ),
13✔
279
            CastOperatorType::Text => (
9✔
280
                vec![
9✔
281
                    FieldType::Binary,
9✔
282
                    FieldType::Boolean,
9✔
283
                    FieldType::Date,
9✔
284
                    FieldType::Decimal,
9✔
285
                    FieldType::Float,
9✔
286
                    FieldType::Int,
9✔
287
                    FieldType::I128,
9✔
288
                    FieldType::String,
9✔
289
                    FieldType::Text,
9✔
290
                    FieldType::Timestamp,
9✔
291
                    FieldType::UInt,
9✔
292
                    FieldType::U128,
9✔
293
                ],
9✔
294
                FieldType::Text,
9✔
295
            ),
9✔
296
            CastOperatorType::Binary => (vec![FieldType::Binary], FieldType::Binary),
×
297
            CastOperatorType::Decimal => (
×
298
                vec![
×
299
                    FieldType::Decimal,
×
300
                    FieldType::Float,
×
301
                    FieldType::Int,
×
302
                    FieldType::I128,
×
303
                    FieldType::String,
×
304
                    FieldType::UInt,
×
305
                    FieldType::U128,
×
306
                ],
×
307
                FieldType::Decimal,
×
308
            ),
×
309
            CastOperatorType::Timestamp => (
×
310
                vec![FieldType::String, FieldType::Timestamp],
×
311
                FieldType::Timestamp,
×
312
            ),
×
313
            CastOperatorType::Date => (vec![FieldType::Date, FieldType::String], FieldType::Date),
×
314
            CastOperatorType::Bson => (vec![FieldType::Bson], FieldType::Bson),
×
315
        };
316

317
        let expression_type = validate_arg_type(arg, expected_input_type, schema, self, 0)?;
46✔
318
        Ok(ExpressionType {
46✔
319
            return_type,
46✔
320
            nullable: expression_type.nullable,
46✔
321
            source: expression_type.source,
46✔
322
            is_primary_key: expression_type.is_primary_key,
46✔
323
        })
46✔
324
    }
46✔
325
}
326

327
pub(crate) fn validate_arg_type(
46✔
328
    arg: &Expression,
46✔
329
    expected: Vec<FieldType>,
46✔
330
    schema: &Schema,
46✔
331
    fct: &CastOperatorType,
46✔
332
    idx: usize,
46✔
333
) -> Result<ExpressionType, PipelineError> {
46✔
334
    let arg_t = arg.get_type(schema)?;
46✔
335
    if !expected.contains(&arg_t.return_type) {
46✔
336
        Err(PipelineError::InvalidFunctionArgumentType(
×
337
            fct.to_string(),
×
338
            arg_t.return_type,
×
339
            FieldTypes::new(expected),
×
340
            idx,
×
341
        ))
×
342
    } else {
343
        Ok(arg_t)
46✔
344
    }
345
}
46✔
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