-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmod.rs
605 lines (561 loc) · 22.3 KB
/
mod.rs
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// 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.
//! Physical expressions for window functions
use crate::error::{DataFusionError, Result};
use crate::physical_plan::{
aggregates,
expressions::{
cume_dist, dense_rank, lag, lead, percent_rank, rank, Literal, NthValue, Ntile,
PhysicalSortExpr, RowNumber,
},
type_coercion::coerce,
udaf, ExecutionPlan, PhysicalExpr,
};
use crate::scalar::ScalarValue;
use arrow::datatypes::Schema;
use arrow_schema::{SchemaRef, SortOptions};
use datafusion_expr::{
window_function::{signature_for_built_in, BuiltInWindowFunction, WindowFunction},
WindowFrame,
};
use datafusion_physical_expr::window::{
BuiltInWindowFunctionExpr, SlidingAggregateWindowExpr,
};
use std::borrow::Borrow;
use std::convert::TryInto;
use std::sync::Arc;
mod bounded_window_agg_exec;
mod window_agg_exec;
pub use bounded_window_agg_exec::BoundedWindowAggExec;
pub use bounded_window_agg_exec::PartitionSearchMode;
use datafusion_common::utils::longest_consecutive_prefix;
use datafusion_physical_expr::equivalence::OrderingEquivalenceBuilder;
use datafusion_physical_expr::expressions::Column;
use datafusion_physical_expr::utils::{convert_to_expr, get_indices_of_matching_exprs};
pub use datafusion_physical_expr::window::{
BuiltInWindowExpr, PlainAggregateWindowExpr, WindowExpr,
};
use datafusion_physical_expr::{OrderingEquivalenceProperties, PhysicalSortRequirement};
pub use window_agg_exec::WindowAggExec;
/// Create a physical expression for window function
pub fn create_window_expr(
fun: &WindowFunction,
name: String,
args: &[Arc<dyn PhysicalExpr>],
partition_by: &[Arc<dyn PhysicalExpr>],
order_by: &[PhysicalSortExpr],
window_frame: Arc<WindowFrame>,
input_schema: &Schema,
) -> Result<Arc<dyn WindowExpr>> {
Ok(match fun {
WindowFunction::AggregateFunction(_) | WindowFunction::AggregateUDF(_) => {
let aggregate = match fun {
WindowFunction::AggregateFunction(fun) => aggregates::create_aggregate_expr(fun, false, args, input_schema, name)?,
WindowFunction::AggregateUDF(fun) => udaf::create_aggregate_expr(fun.as_ref(), args, input_schema, name)?,
_ => unreachable!()
};
if !window_frame.start_bound.is_unbounded() {
Arc::new(SlidingAggregateWindowExpr::new(
aggregate,
partition_by,
order_by,
window_frame,
))
} else {
Arc::new(PlainAggregateWindowExpr::new(
aggregate,
partition_by,
order_by,
window_frame,
))
}
}
WindowFunction::BuiltInWindowFunction(fun) => Arc::new(BuiltInWindowExpr::new(
create_built_in_window_expr(fun, args, input_schema, name)?,
partition_by,
order_by,
window_frame,
)),
})
}
fn get_scalar_value_from_args(
args: &[Arc<dyn PhysicalExpr>],
index: usize,
) -> Result<Option<ScalarValue>> {
Ok(if let Some(field) = args.get(index) {
let tmp = field
.as_any()
.downcast_ref::<Literal>()
.ok_or_else(|| DataFusionError::NotImplemented(
format!("There is only support Literal types for field at idx: {index} in Window Function"),
))?
.value()
.clone();
Some(tmp)
} else {
None
})
}
fn create_built_in_window_expr(
fun: &BuiltInWindowFunction,
args: &[Arc<dyn PhysicalExpr>],
input_schema: &Schema,
name: String,
) -> Result<Arc<dyn BuiltInWindowFunctionExpr>> {
Ok(match fun {
BuiltInWindowFunction::RowNumber => Arc::new(RowNumber::new(name)),
BuiltInWindowFunction::Rank => Arc::new(rank(name)),
BuiltInWindowFunction::DenseRank => Arc::new(dense_rank(name)),
BuiltInWindowFunction::PercentRank => Arc::new(percent_rank(name)),
BuiltInWindowFunction::CumeDist => Arc::new(cume_dist(name)),
BuiltInWindowFunction::Ntile => {
let coerced_args = coerce(args, input_schema, &signature_for_built_in(fun))?;
let n: i64 = get_scalar_value_from_args(&coerced_args, 0)?
.ok_or_else(|| {
DataFusionError::Execution(
"NTILE requires at least 1 argument".to_string(),
)
})?
.try_into()?;
let n: u64 = n as u64;
Arc::new(Ntile::new(name, n))
}
BuiltInWindowFunction::Lag => {
let coerced_args = coerce(args, input_schema, &signature_for_built_in(fun))?;
let arg = coerced_args[0].clone();
let data_type = args[0].data_type(input_schema)?;
let shift_offset = get_scalar_value_from_args(&coerced_args, 1)?
.map(|v| v.try_into())
.and_then(|v| v.ok());
let default_value = get_scalar_value_from_args(&coerced_args, 2)?;
Arc::new(lag(name, data_type, arg, shift_offset, default_value))
}
BuiltInWindowFunction::Lead => {
let coerced_args = coerce(args, input_schema, &signature_for_built_in(fun))?;
let arg = coerced_args[0].clone();
let data_type = args[0].data_type(input_schema)?;
let shift_offset = get_scalar_value_from_args(&coerced_args, 1)?
.map(|v| v.try_into())
.and_then(|v| v.ok());
let default_value = get_scalar_value_from_args(&coerced_args, 2)?;
Arc::new(lead(name, data_type, arg, shift_offset, default_value))
}
BuiltInWindowFunction::NthValue => {
let coerced_args = coerce(args, input_schema, &signature_for_built_in(fun))?;
let arg = coerced_args[0].clone();
let n = coerced_args[1]
.as_any()
.downcast_ref::<Literal>()
.unwrap()
.value();
let n: i64 = n
.clone()
.try_into()
.map_err(|e| DataFusionError::Execution(format!("{e:?}")))?;
let n: u32 = n as u32;
let data_type = args[0].data_type(input_schema)?;
Arc::new(NthValue::nth(name, arg, data_type, n)?)
}
BuiltInWindowFunction::FirstValue => {
let arg =
coerce(args, input_schema, &signature_for_built_in(fun))?[0].clone();
let data_type = args[0].data_type(input_schema)?;
Arc::new(NthValue::first(name, arg, data_type))
}
BuiltInWindowFunction::LastValue => {
let arg =
coerce(args, input_schema, &signature_for_built_in(fun))?[0].clone();
let data_type = args[0].data_type(input_schema)?;
Arc::new(NthValue::last(name, arg, data_type))
}
})
}
pub(crate) fn calc_requirements<
T: Borrow<Arc<dyn PhysicalExpr>>,
S: Borrow<PhysicalSortExpr>,
>(
partition_by_exprs: impl IntoIterator<Item = T>,
orderby_sort_exprs: impl IntoIterator<Item = S>,
) -> Option<Vec<PhysicalSortRequirement>> {
let mut sort_reqs = partition_by_exprs
.into_iter()
.map(|partition_by| {
PhysicalSortRequirement::new(partition_by.borrow().clone(), None)
})
.collect::<Vec<_>>();
for element in orderby_sort_exprs.into_iter() {
let PhysicalSortExpr { expr, options } = element.borrow();
if !sort_reqs.iter().any(|e| e.expr.eq(expr)) {
sort_reqs.push(PhysicalSortRequirement::new(expr.clone(), Some(*options)));
}
}
// Convert empty result to None. Otherwise wrap result inside Some()
(!sort_reqs.is_empty()).then_some(sort_reqs)
}
/// This function calculates the indices such that when partition by expressions reordered with this indices
/// resulting expressions define a preset for existing ordering.
// For instance, if input is ordered by a, b, c and PARTITION BY b, a is used
// This vector will be [1, 0]. It means that when we iterate b,a columns with the order [1, 0]
// resulting vector (a, b) is a preset of the existing ordering (a, b, c).
pub(crate) fn get_ordered_partition_by_indices(
partition_by_exprs: &[Arc<dyn PhysicalExpr>],
input: &Arc<dyn ExecutionPlan>,
) -> Vec<usize> {
let input_ordering = input.output_ordering().unwrap_or(&[]);
let input_ordering_exprs = convert_to_expr(input_ordering);
let equal_properties = || input.equivalence_properties();
let input_places = get_indices_of_matching_exprs(
&input_ordering_exprs,
partition_by_exprs,
equal_properties,
);
let mut partition_places = get_indices_of_matching_exprs(
partition_by_exprs,
&input_ordering_exprs,
equal_properties,
);
partition_places.sort();
let first_n = longest_consecutive_prefix(partition_places);
input_places[0..first_n].to_vec()
}
pub(crate) fn window_ordering_equivalence(
schema: &SchemaRef,
input: &Arc<dyn ExecutionPlan>,
window_expr: &[Arc<dyn WindowExpr>],
) -> OrderingEquivalenceProperties {
// We need to update the schema, so we can not directly use
// `input.ordering_equivalence_properties()`.
let mut builder = OrderingEquivalenceBuilder::new(schema.clone())
.with_equivalences(input.equivalence_properties())
.with_existing_ordering(input.output_ordering().map(|elem| elem.to_vec()))
.extend(input.ordering_equivalence_properties());
for expr in window_expr {
if let Some(builtin_window_expr) =
expr.as_any().downcast_ref::<BuiltInWindowExpr>()
{
// Only the built-in `RowNumber` window function introduces a new
// ordering:
if builtin_window_expr
.get_built_in_func_expr()
.as_any()
.is::<RowNumber>()
{
if let Some((idx, field)) =
schema.column_with_name(builtin_window_expr.name())
{
let column = Column::new(field.name(), idx);
let options = SortOptions {
descending: false,
nulls_first: false,
}; // ASC, NULLS LAST
let rhs = PhysicalSortExpr {
expr: Arc::new(column) as _,
options,
};
builder.add_equal_conditions(vec![rhs]);
}
}
}
}
builder.build()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::physical_plan::aggregates::AggregateFunction;
use crate::physical_plan::expressions::col;
use crate::physical_plan::file_format::CsvExec;
use crate::physical_plan::{collect, ExecutionPlan};
use crate::prelude::SessionContext;
use crate::test::exec::{assert_strong_count_converges_to_zero, BlockingExec};
use crate::test::{self, assert_is_pending, csv_exec_sorted};
use arrow::array::*;
use arrow::compute::SortOptions;
use arrow::datatypes::{DataType, Field, SchemaRef};
use arrow::record_batch::RecordBatch;
use datafusion_common::cast::as_primitive_array;
use datafusion_expr::{create_udaf, Accumulator, Volatility};
use futures::FutureExt;
fn create_test_schema(partitions: usize) -> Result<(Arc<CsvExec>, SchemaRef)> {
let csv = test::scan_partitioned_csv(partitions)?;
let schema = csv.schema();
Ok((csv, schema))
}
fn create_test_schema2() -> Result<SchemaRef> {
let a = Field::new("a", DataType::Int32, true);
let b = Field::new("b", DataType::Int32, true);
let c = Field::new("c", DataType::Int32, true);
let d = Field::new("d", DataType::Int32, true);
let e = Field::new("e", DataType::Int32, true);
let schema = Arc::new(Schema::new(vec![a, b, c, d, e]));
Ok(schema)
}
/// make PhysicalSortExpr with default options
fn sort_expr(name: &str, schema: &Schema) -> PhysicalSortExpr {
sort_expr_options(name, schema, SortOptions::default())
}
/// PhysicalSortExpr with specified options
fn sort_expr_options(
name: &str,
schema: &Schema,
options: SortOptions,
) -> PhysicalSortExpr {
PhysicalSortExpr {
expr: col(name, schema).unwrap(),
options,
}
}
#[tokio::test]
async fn test_get_partition_by_ordering() -> Result<()> {
let test_schema = create_test_schema2()?;
// Columns a,c are nullable whereas b,d are not nullable.
// Source is sorted by a ASC NULLS FIRST, b ASC NULLS FIRST, c ASC NULLS FIRST, d ASC NULLS FIRST
// Column e is not ordered.
let sort_exprs = vec![
sort_expr("a", &test_schema),
sort_expr("b", &test_schema),
sort_expr("c", &test_schema),
sort_expr("d", &test_schema),
];
// Input is ordered by a,b,c,d
let input = csv_exec_sorted(&test_schema, sort_exprs, true);
let test_data = vec![
(vec!["a", "b"], vec![0, 1]),
(vec!["b", "a"], vec![1, 0]),
(vec!["b", "a", "c"], vec![1, 0, 2]),
(vec!["d", "b", "a"], vec![2, 1]),
(vec!["d", "e", "a"], vec![2]),
];
for (pb_names, expected) in test_data {
let pb_exprs = pb_names
.iter()
.map(|name| col(name, &test_schema))
.collect::<Result<Vec<_>>>()?;
assert_eq!(
get_ordered_partition_by_indices(&pb_exprs, &input),
expected
);
}
Ok(())
}
#[tokio::test]
async fn test_calc_requirements() -> Result<()> {
let schema = create_test_schema2()?;
let test_data = vec![
// PARTITION BY a, ORDER BY b ASC NULLS FIRST
(
vec!["a"],
vec![("b", true, true)],
vec![("a", None), ("b", Some((true, true)))],
),
// PARTITION BY a, ORDER BY a ASC NULLS FIRST
(vec!["a"], vec![("a", true, true)], vec![("a", None)]),
// PARTITION BY a, ORDER BY b ASC NULLS FIRST, c DESC NULLS LAST
(
vec!["a"],
vec![("b", true, true), ("c", false, false)],
vec![
("a", None),
("b", Some((true, true))),
("c", Some((false, false))),
],
),
// PARTITION BY a, c, ORDER BY b ASC NULLS FIRST, c DESC NULLS LAST
(
vec!["a", "c"],
vec![("b", true, true), ("c", false, false)],
vec![("a", None), ("c", None), ("b", Some((true, true)))],
),
];
for (pb_params, ob_params, expected_params) in test_data {
let mut partitionbys = vec![];
for col_name in pb_params {
partitionbys.push(col(col_name, &schema)?);
}
let mut orderbys = vec![];
for (col_name, descending, nulls_first) in ob_params {
let expr = col(col_name, &schema)?;
let options = SortOptions {
descending,
nulls_first,
};
orderbys.push(PhysicalSortExpr { expr, options });
}
let mut expected: Option<Vec<PhysicalSortRequirement>> = None;
for (col_name, reqs) in expected_params {
let options = reqs.map(|(descending, nulls_first)| SortOptions {
descending,
nulls_first,
});
let expr = col(col_name, &schema)?;
let res = PhysicalSortRequirement::new(expr, options);
if let Some(expected) = &mut expected {
expected.push(res);
} else {
expected = Some(vec![res]);
}
}
assert_eq!(calc_requirements(partitionbys, orderbys), expected);
}
Ok(())
}
#[tokio::test]
async fn window_function_with_udaf() -> Result<()> {
#[derive(Debug)]
struct MyCount(i64);
impl Accumulator for MyCount {
fn state(&self) -> Result<Vec<ScalarValue>> {
Ok(vec![ScalarValue::Int64(Some(self.0))])
}
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
let array = &values[0];
self.0 += (array.len() - array.null_count()) as i64;
Ok(())
}
fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
let counts: &Int64Array = arrow::array::as_primitive_array(&states[0]);
if let Some(c) = &arrow::compute::sum(counts) {
self.0 += *c;
}
Ok(())
}
fn evaluate(&self) -> Result<ScalarValue> {
Ok(ScalarValue::Int64(Some(self.0)))
}
fn size(&self) -> usize {
std::mem::size_of_val(self)
}
}
let my_count = create_udaf(
"my_count",
DataType::Int64,
Arc::new(DataType::Int64),
Volatility::Immutable,
Arc::new(|_| Ok(Box::new(MyCount(0)))),
Arc::new(vec![DataType::Int64]),
);
let session_ctx = SessionContext::new();
let task_ctx = session_ctx.task_ctx();
let (input, schema) = create_test_schema(1)?;
let window_exec = Arc::new(WindowAggExec::try_new(
vec![create_window_expr(
&WindowFunction::AggregateUDF(Arc::new(my_count)),
"my_count".to_owned(),
&[col("c3", &schema)?],
&[],
&[],
Arc::new(WindowFrame::new(false)),
schema.as_ref(),
)?],
input,
schema.clone(),
vec![],
)?);
let result: Vec<RecordBatch> = collect(window_exec, task_ctx).await?;
assert_eq!(result.len(), 1);
let n_schema_fields = schema.fields().len();
let columns = result[0].columns();
let count: &Int64Array = as_primitive_array(&columns[n_schema_fields])?;
assert_eq!(count.value(0), 100);
assert_eq!(count.value(99), 100);
Ok(())
}
#[tokio::test]
async fn window_function() -> Result<()> {
let session_ctx = SessionContext::new();
let task_ctx = session_ctx.task_ctx();
let (input, schema) = create_test_schema(1)?;
let window_exec = Arc::new(WindowAggExec::try_new(
vec![
create_window_expr(
&WindowFunction::AggregateFunction(AggregateFunction::Count),
"count".to_owned(),
&[col("c3", &schema)?],
&[],
&[],
Arc::new(WindowFrame::new(false)),
schema.as_ref(),
)?,
create_window_expr(
&WindowFunction::AggregateFunction(AggregateFunction::Max),
"max".to_owned(),
&[col("c3", &schema)?],
&[],
&[],
Arc::new(WindowFrame::new(false)),
schema.as_ref(),
)?,
create_window_expr(
&WindowFunction::AggregateFunction(AggregateFunction::Min),
"min".to_owned(),
&[col("c3", &schema)?],
&[],
&[],
Arc::new(WindowFrame::new(false)),
schema.as_ref(),
)?,
],
input,
schema.clone(),
vec![],
)?);
let result: Vec<RecordBatch> = collect(window_exec, task_ctx).await?;
assert_eq!(result.len(), 1);
let n_schema_fields = schema.fields().len();
let columns = result[0].columns();
// c3 is small int
let count: &Int64Array = as_primitive_array(&columns[n_schema_fields])?;
assert_eq!(count.value(0), 100);
assert_eq!(count.value(99), 100);
let max: &Int8Array = as_primitive_array(&columns[n_schema_fields + 1])?;
assert_eq!(max.value(0), 125);
assert_eq!(max.value(99), 125);
let min: &Int8Array = as_primitive_array(&columns[n_schema_fields + 2])?;
assert_eq!(min.value(0), -117);
assert_eq!(min.value(99), -117);
Ok(())
}
#[tokio::test]
async fn test_drop_cancel() -> Result<()> {
let session_ctx = SessionContext::new();
let task_ctx = session_ctx.task_ctx();
let schema =
Arc::new(Schema::new(vec![Field::new("a", DataType::Float32, true)]));
let blocking_exec = Arc::new(BlockingExec::new(Arc::clone(&schema), 1));
let refs = blocking_exec.refs();
let window_agg_exec = Arc::new(WindowAggExec::try_new(
vec![create_window_expr(
&WindowFunction::AggregateFunction(AggregateFunction::Count),
"count".to_owned(),
&[col("a", &schema)?],
&[],
&[],
Arc::new(WindowFrame::new(false)),
schema.as_ref(),
)?],
blocking_exec,
schema,
vec![],
)?);
let fut = collect(window_agg_exec, task_ctx);
let mut fut = fut.boxed();
assert_is_pending(&mut fut);
drop(fut);
assert_strong_count_converges_to_zero(refs).await;
Ok(())
}
}