forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
467 lines (424 loc) · 12.8 KB
/
mod.rs
File metadata and controls
467 lines (424 loc) · 12.8 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use arrow::array::{
ArrayRef, Int64Array, RecordBatch, StringArray, StructArray,
builder::{ListBuilder, StringBuilder},
};
use arrow::datatypes::{DataType, Field};
use arrow::util::pretty::{pretty_format_batches, pretty_format_columns};
use datafusion::prelude::*;
use datafusion_common::{DFSchema, ScalarValue};
use datafusion_expr::ExprFunctionExt;
use datafusion_expr::expr::NullTreatment;
use datafusion_expr::simplify::SimplifyContext;
use datafusion_functions::core::expr_ext::FieldAccessor;
use datafusion_functions_aggregate::first_last::first_value_udaf;
use datafusion_functions_aggregate::sum::sum_udaf;
use datafusion_functions_nested::expr_ext::{IndexAccessor, SliceAccessor};
use datafusion_optimizer::simplify_expressions::ExprSimplifier;
/// Tests of using and evaluating `Expr`s outside the context of a LogicalPlan
use std::sync::{Arc, LazyLock};
mod parse_sql_expr;
#[expect(clippy::needless_pass_by_value)]
mod simplification;
#[test]
fn test_octet_length() {
#[rustfmt::skip]
evaluate_expr_test(
octet_length(col("id")),
vec![
"+------+",
"| expr |",
"+------+",
"| 1 |",
"| 1 |",
"| 1 |",
"+------+",
],
);
}
#[test]
fn test_eq() {
// id = '2'
evaluate_expr_test(
col("id").eq(lit("2")),
vec![
"+-------+",
"| expr |",
"+-------+",
"| false |",
"| true |",
"| false |",
"+-------+",
],
);
}
#[test]
fn test_eq_with_coercion() {
// id = 2 (need to coerce the 2 to '2' to evaluate)
evaluate_expr_test(
col("id").eq(lit(2i32)),
vec![
"+-------+",
"| expr |",
"+-------+",
"| false |",
"| true |",
"| false |",
"+-------+",
],
);
}
#[test]
fn test_get_field() {
evaluate_expr_test(
col("props").field("a"),
vec![
"+------------+",
"| expr |",
"+------------+",
"| 2021-02-01 |",
"| 2021-02-02 |",
"| 2021-02-03 |",
"+------------+",
],
);
}
#[test]
fn test_get_field_null() {
#[rustfmt::skip]
evaluate_expr_test(
lit(ScalarValue::Null).field("a"),
vec![
"+------+",
"| expr |",
"+------+",
"| |",
"+------+",
],
);
}
#[test]
fn test_nested_get_field() {
evaluate_expr_test(
col("props")
.field("a")
.eq(lit("2021-02-02"))
.or(col("id").eq(lit(1))),
vec![
"+-------+",
"| expr |",
"+-------+",
"| true |",
"| true |",
"| false |",
"+-------+",
],
);
}
#[test]
fn test_list_index() {
#[rustfmt::skip]
evaluate_expr_test(
col("list").index(lit(1i64)),
vec![
"+------+",
"| expr |",
"+------+",
"| one |",
"| two |",
"| five |",
"+------+",
],
);
}
#[test]
fn test_list_range() {
evaluate_expr_test(
col("list").range(lit(1i64), lit(2i64)),
vec![
"+--------------+",
"| expr |",
"+--------------+",
"| [one] |",
"| [two, three] |",
"| [five] |",
"+--------------+",
],
);
}
#[tokio::test]
async fn test_aggregate_ext_order_by() {
let agg = first_value_udaf().call(vec![col("props")]);
// ORDER BY id ASC
let agg_asc = agg
.clone()
.order_by(vec![col("id").sort(true, true)])
.build()
.unwrap()
.alias("asc");
// ORDER BY id DESC
let agg_desc = agg
.order_by(vec![col("id").sort(false, true)])
.build()
.unwrap()
.alias("desc");
evaluate_agg_test(
agg_asc,
vec![
"+-----------------+",
"| asc |",
"+-----------------+",
"| {a: 2021-02-01} |",
"+-----------------+",
],
)
.await;
evaluate_agg_test(
agg_desc,
vec![
"+-----------------+",
"| desc |",
"+-----------------+",
"| {a: 2021-02-03} |",
"+-----------------+",
],
)
.await;
}
#[tokio::test]
async fn test_aggregate_ext_filter() {
let agg = first_value_udaf()
.call(vec![col("i")])
.order_by(vec![col("i").sort(true, true)])
.filter(col("i").is_not_null())
.build()
.unwrap()
.alias("val");
#[rustfmt::skip]
evaluate_agg_test(
agg,
vec![
"+-----+",
"| val |",
"+-----+",
"| 5 |",
"+-----+",
],
)
.await;
}
#[tokio::test]
async fn test_aggregate_ext_distinct() {
let agg = sum_udaf()
.call(vec![lit(5)])
// distinct sum should be 5, not 15
.distinct()
.build()
.unwrap()
.alias("distinct");
evaluate_agg_test(
agg,
vec![
"+----------+",
"| distinct |",
"+----------+",
"| 5 |",
"+----------+",
],
)
.await;
}
#[tokio::test]
async fn test_aggregate_ext_null_treatment() {
let agg = first_value_udaf()
.call(vec![col("i")])
.order_by(vec![col("i").sort(true, true)]);
let agg_respect = agg
.clone()
.null_treatment(NullTreatment::RespectNulls)
.build()
.unwrap()
.alias("respect");
let agg_ignore = agg
.null_treatment(NullTreatment::IgnoreNulls)
.build()
.unwrap()
.alias("ignore");
evaluate_agg_test(
agg_respect,
vec![
"+---------+",
"| respect |",
"+---------+",
"| |",
"+---------+",
],
)
.await;
evaluate_agg_test(
agg_ignore,
vec![
"+--------+",
"| ignore |",
"+--------+",
"| 5 |",
"+--------+",
],
)
.await;
}
#[tokio::test]
async fn test_create_physical_expr() {
// create_physical_expr does not simplify the expression
// 1 + 1
create_expr_test(lit(1i32) + lit(2i32), "1 + 2");
// However, you can run the simplifier before creating the physical
// expression. This mimics what delta.rs and other non-sql libraries do to
// create predicates
//
// 1 + 1
create_simplified_expr_test(lit(1i32) + lit(2i32), "3");
}
#[test]
fn test_create_physical_expr_nvl2() {
let batch = &TEST_BATCH;
let df_schema = DFSchema::try_from(batch.schema()).unwrap();
let ctx = SessionContext::new();
let expect_err = |expr| {
let physical_expr = ctx.create_physical_expr(expr, &df_schema).unwrap();
let err = physical_expr.evaluate(batch).unwrap_err();
assert!(
err.to_string()
.contains("nvl2 should have been simplified to case"),
"unexpected error: {err:?}"
);
};
expect_err(nvl2(col("i"), lit(1i64), lit(0i64)));
expect_err(nvl2(lit(1i64), col("i"), lit(0i64)));
}
#[tokio::test]
async fn test_create_physical_expr_coercion() {
// create_physical_expr does apply type coercion and unwrapping in cast
//
// expect the cast on the literals
// compare string function to int `id = 1`
create_expr_test(col("id").eq(lit(1i32)), "id@0 = CAST(1 AS Utf8)");
create_expr_test(lit(1i32).eq(col("id")), "CAST(1 AS Utf8) = id@0");
// compare int col to string literal `i = '202410'`
// Note this casts the column (not the field)
create_expr_test(col("i").eq(lit("202410")), "CAST(i@1 AS Utf8) = 202410");
create_expr_test(lit("202410").eq(col("i")), "202410 = CAST(i@1 AS Utf8)");
// however, when simplified the casts on i should removed
// https://github.com/apache/datafusion/issues/14944
create_simplified_expr_test(col("i").eq(lit("202410")), "CAST(i@1 AS Utf8) = 202410");
create_simplified_expr_test(lit("202410").eq(col("i")), "CAST(i@1 AS Utf8) = 202410");
}
/// Evaluates the specified expr as an aggregate and compares the result to the
/// expected result.
async fn evaluate_agg_test(expr: Expr, expected_lines: Vec<&str>) {
let ctx = SessionContext::new();
let group_expr = vec![];
let agg_expr = vec![expr];
let result = ctx
.read_batch(TEST_BATCH.clone())
.unwrap()
.aggregate(group_expr, agg_expr)
.unwrap()
.collect()
.await
.unwrap();
let result = pretty_format_batches(&result).unwrap().to_string();
let actual_lines = result.lines().collect::<Vec<_>>();
assert_eq!(
expected_lines, actual_lines,
"\n\nexpected:\n\n{expected_lines:#?}\nactual:\n\n{actual_lines:#?}\n\n"
);
}
/// Converts the `Expr` to a `PhysicalExpr`, evaluates it against the provided
/// `RecordBatch` and compares the result to the expected result.
#[expect(clippy::needless_pass_by_value)]
fn evaluate_expr_test(expr: Expr, expected_lines: Vec<&str>) {
let batch = &TEST_BATCH;
let df_schema = DFSchema::try_from(batch.schema()).unwrap();
let physical_expr = SessionContext::new()
.create_physical_expr(expr, &df_schema)
.unwrap();
let result = physical_expr.evaluate(batch).unwrap();
let array = result.into_array(1).unwrap();
let result = pretty_format_columns("expr", &[array]).unwrap().to_string();
let actual_lines = result.lines().collect::<Vec<_>>();
assert_eq!(
expected_lines, actual_lines,
"\n\nexpected:\n\n{expected_lines:#?}\nactual:\n\n{actual_lines:#?}\n\n"
);
}
/// Creates the physical expression from Expr and compares the Debug expression
/// to the expected result.
fn create_expr_test(expr: Expr, expected_expr: &str) {
let batch = &TEST_BATCH;
let df_schema = DFSchema::try_from(batch.schema()).unwrap();
let physical_expr = SessionContext::new()
.create_physical_expr(expr, &df_schema)
.unwrap();
assert_eq!(physical_expr.to_string(), expected_expr);
}
/// Creates the physical expression from Expr and runs the expr simplifier
fn create_simplified_expr_test(expr: Expr, expected_expr: &str) {
let batch = &TEST_BATCH;
let df_schema = DFSchema::try_from(batch.schema()).unwrap();
// Simplify the expression first
let simplify_context = SimplifyContext::builder()
.with_schema(Arc::new(df_schema))
.build();
let simplifier = ExprSimplifier::new(simplify_context).with_max_cycles(10);
let simplified = simplifier.simplify(expr).unwrap();
create_expr_test(simplified, expected_expr);
}
/// Returns a Batch with 3 rows and 4 columns:
///
/// id: Utf8
/// i: Int64
/// props: Struct
/// list: List<String>
static TEST_BATCH: LazyLock<RecordBatch> = LazyLock::new(|| {
let string_array: ArrayRef = Arc::new(StringArray::from(vec!["1", "2", "3"]));
let int_array: ArrayRef =
Arc::new(Int64Array::from_iter(vec![Some(10), None, Some(5)]));
// { a: "2021-02-01" } { a: "2021-02-02" } { a: "2021-02-03" }
let struct_array: ArrayRef = Arc::from(StructArray::from(vec![(
Arc::new(Field::new("a", DataType::Utf8, false)),
Arc::new(StringArray::from(vec![
"2021-02-01",
"2021-02-02",
"2021-02-03",
])) as _,
)]));
// ["one"] ["two", "three", "four"] ["five"]
let mut builder = ListBuilder::new(StringBuilder::new());
builder.append_value([Some("one")]);
builder.append_value([Some("two"), Some("three"), Some("four")]);
builder.append_value([Some("five")]);
let list_array: ArrayRef = Arc::new(builder.finish());
RecordBatch::try_from_iter(vec![
("id", string_array),
("i", int_array),
("props", struct_array),
("list", list_array),
])
.unwrap()
});