-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
user_defined_aggregates.rs
869 lines (737 loc) · 26.4 KB
/
user_defined_aggregates.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
// 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.
//! This module contains end to end demonstrations of creating
//! user defined aggregate functions
use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use arrow::{array::AsArray, datatypes::Fields};
use arrow_array::{
types::UInt64Type, Int32Array, PrimitiveArray, StringArray, StructArray,
};
use arrow_schema::Schema;
use datafusion::dataframe::DataFrame;
use datafusion::datasource::MemTable;
use datafusion::test_util::plan_and_collect;
use datafusion::{
arrow::{
array::{ArrayRef, Float64Array, TimestampNanosecondArray},
datatypes::{DataType, Field, Float64Type, TimeUnit, TimestampNanosecondType},
record_batch::RecordBatch,
},
assert_batches_eq,
error::Result,
logical_expr::{
AccumulatorFactoryFunction, AggregateUDF, Signature, TypeSignature, Volatility,
},
physical_plan::Accumulator,
prelude::SessionContext,
scalar::ScalarValue,
};
use datafusion_common::{assert_contains, cast::as_primitive_array, exec_err};
use datafusion_expr::{
col, create_udaf, function::AccumulatorArgs, AggregateUDFImpl, GroupsAccumulator,
LogicalPlanBuilder, SimpleAggregateUDF,
};
use datafusion_functions_aggregate::average::AvgAccumulator;
/// Test to show the contents of the setup
#[tokio::test]
async fn test_setup() {
let TestContext { ctx, test_state: _ } = TestContext::new();
let sql = "SELECT * from t order by time";
let expected = [
"+-------+----------------------------+",
"| value | time |",
"+-------+----------------------------+",
"| 2.0 | 1970-01-01T00:00:00.000002 |",
"| 3.0 | 1970-01-01T00:00:00.000003 |",
"| 1.0 | 1970-01-01T00:00:00.000004 |",
"| 5.0 | 1970-01-01T00:00:00.000005 |",
"| 5.0 | 1970-01-01T00:00:00.000005 |",
"+-------+----------------------------+",
];
assert_batches_eq!(expected, &execute(&ctx, sql).await.unwrap());
}
/// Basic user defined aggregate
#[tokio::test]
async fn test_udaf() {
let TestContext { ctx, test_state } = TestContext::new();
assert!(!test_state.update_batch());
let sql = "SELECT time_sum(time) from t";
let expected = [
"+----------------------------+",
"| time_sum(t.time) |",
"+----------------------------+",
"| 1970-01-01T00:00:00.000019 |",
"+----------------------------+",
];
assert_batches_eq!(expected, &execute(&ctx, sql).await.unwrap());
// normal aggregates call update_batch
assert!(test_state.update_batch());
assert!(!test_state.retract_batch());
}
/// User defined aggregate used as a window function
#[tokio::test]
async fn test_udaf_as_window() {
let TestContext { ctx, test_state } = TestContext::new();
let sql = "SELECT time_sum(time) OVER() as time_sum from t";
let expected = [
"+----------------------------+",
"| time_sum |",
"+----------------------------+",
"| 1970-01-01T00:00:00.000019 |",
"| 1970-01-01T00:00:00.000019 |",
"| 1970-01-01T00:00:00.000019 |",
"| 1970-01-01T00:00:00.000019 |",
"| 1970-01-01T00:00:00.000019 |",
"+----------------------------+",
];
assert_batches_eq!(expected, &execute(&ctx, sql).await.unwrap());
// aggregate over the entire window function call update_batch
assert!(test_state.update_batch());
assert!(!test_state.retract_batch());
}
/// User defined aggregate used as a window function with a window frame
#[tokio::test]
async fn test_udaf_as_window_with_frame() {
let TestContext { ctx, test_state } = TestContext::new();
let sql = "SELECT time_sum(time) OVER(ORDER BY time ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as time_sum from t";
let expected = [
"+----------------------------+",
"| time_sum |",
"+----------------------------+",
"| 1970-01-01T00:00:00.000005 |",
"| 1970-01-01T00:00:00.000009 |",
"| 1970-01-01T00:00:00.000012 |",
"| 1970-01-01T00:00:00.000014 |",
"| 1970-01-01T00:00:00.000010 |",
"+----------------------------+",
];
assert_batches_eq!(expected, &execute(&ctx, sql).await.unwrap());
// user defined aggregates with window frame should be calling retract batch
assert!(test_state.update_batch());
assert!(test_state.retract_batch());
}
/// Ensure that User defined aggregate used as a window function with a window
/// frame, but that does not implement retract_batch, returns an error
#[tokio::test]
async fn test_udaf_as_window_with_frame_without_retract_batch() {
let test_state = Arc::new(TestState::new().with_error_on_retract_batch());
let TestContext { ctx, test_state: _ } = TestContext::new_with_test_state(test_state);
let sql = "SELECT time_sum(time) OVER(ORDER BY time ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as time_sum from t";
// Note if this query ever does start working
let err = execute(&ctx, sql).await.unwrap_err();
assert_contains!(err.to_string(), "This feature is not implemented: Aggregate can not be used as a sliding accumulator because `retract_batch` is not implemented: time_sum(t.time) ORDER BY [t.time ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING");
}
/// Basic query for with a udaf returning a structure
#[tokio::test]
async fn test_udaf_returning_struct() {
let TestContext { ctx, test_state: _ } = TestContext::new();
let sql = "SELECT first(value, time) from t";
let expected = [
"+------------------------------------------------+",
"| first(t.value,t.time) |",
"+------------------------------------------------+",
"| {value: 2.0, time: 1970-01-01T00:00:00.000002} |",
"+------------------------------------------------+",
];
assert_batches_eq!(expected, &execute(&ctx, sql).await.unwrap());
}
/// Demonstrate extracting the fields from a structure using a subquery
#[tokio::test]
async fn test_udaf_returning_struct_subquery() {
let TestContext { ctx, test_state: _ } = TestContext::new();
let sql = "select sq.first['value'], sq.first['time'] from (SELECT first(value, time) as first from t) as sq";
let expected = [
"+-----------------+----------------------------+",
"| sq.first[value] | sq.first[time] |",
"+-----------------+----------------------------+",
"| 2.0 | 1970-01-01T00:00:00.000002 |",
"+-----------------+----------------------------+",
];
assert_batches_eq!(expected, &execute(&ctx, sql).await.unwrap());
}
#[tokio::test]
async fn test_udaf_shadows_builtin_fn() {
let TestContext {
mut ctx,
test_state,
} = TestContext::new();
let sql = "SELECT sum(arrow_cast(time, 'Int64')) from t";
// compute with builtin `sum` aggregator
let expected = [
"+---------------------------------------+",
"| sum(arrow_cast(t.time,Utf8(\"Int64\"))) |",
"+---------------------------------------+",
"| 19000 |",
"+---------------------------------------+",
];
assert_batches_eq!(expected, &execute(&ctx, sql).await.unwrap());
// Register `TimeSum` with name `sum`. This will shadow the builtin one
let sql = "SELECT sum(time) from t";
TimeSum::register(&mut ctx, test_state.clone(), "sum");
let expected = [
"+----------------------------+",
"| sum(t.time) |",
"+----------------------------+",
"| 1970-01-01T00:00:00.000019 |",
"+----------------------------+",
];
assert_batches_eq!(expected, &execute(&ctx, sql).await.unwrap());
}
async fn execute(ctx: &SessionContext, sql: &str) -> Result<Vec<RecordBatch>> {
ctx.sql(sql).await?.collect().await
}
/// tests the creation, registration and usage of a UDAF
#[tokio::test]
async fn simple_udaf() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
let batch1 = RecordBatch::try_new(
Arc::new(schema.clone()),
vec![Arc::new(Int32Array::from(vec![1, 2, 3]))],
)?;
let batch2 = RecordBatch::try_new(
Arc::new(schema.clone()),
vec![Arc::new(Int32Array::from(vec![4, 5]))],
)?;
let ctx = SessionContext::new();
let provider = MemTable::try_new(Arc::new(schema), vec![vec![batch1], vec![batch2]])?;
ctx.register_table("t", Arc::new(provider))?;
// define a udaf, using a DataFusion's accumulator
let my_avg = create_udaf(
"my_avg",
vec![DataType::Float64],
Arc::new(DataType::Float64),
Volatility::Immutable,
Arc::new(|_| Ok(Box::<AvgAccumulator>::default())),
Arc::new(vec![DataType::UInt64, DataType::Float64]),
);
ctx.register_udaf(my_avg);
let result = ctx.sql("SELECT MY_AVG(a) FROM t").await?.collect().await?;
let expected = [
"+-------------+",
"| my_avg(t.a) |",
"+-------------+",
"| 3.0 |",
"+-------------+",
];
assert_batches_eq!(expected, &result);
Ok(())
}
#[tokio::test]
async fn deregister_udaf() -> Result<()> {
let ctx = SessionContext::new();
let my_avg = create_udaf(
"my_avg",
vec![DataType::Float64],
Arc::new(DataType::Float64),
Volatility::Immutable,
Arc::new(|_| Ok(Box::<AvgAccumulator>::default())),
Arc::new(vec![DataType::UInt64, DataType::Float64]),
);
ctx.register_udaf(my_avg);
assert!(ctx.state().aggregate_functions().contains_key("my_avg"));
ctx.deregister_udaf("my_avg");
assert!(!ctx.state().aggregate_functions().contains_key("my_avg"));
Ok(())
}
#[tokio::test]
async fn case_sensitive_identifiers_user_defined_aggregates() -> Result<()> {
let ctx = SessionContext::new();
let arr = Int32Array::from(vec![1]);
let batch = RecordBatch::try_from_iter(vec![("i", Arc::new(arr) as _)])?;
ctx.register_batch("t", batch).unwrap();
// Note capitalization
let my_avg = create_udaf(
"MY_AVG",
vec![DataType::Float64],
Arc::new(DataType::Float64),
Volatility::Immutable,
Arc::new(|_| Ok(Box::<AvgAccumulator>::default())),
Arc::new(vec![DataType::UInt64, DataType::Float64]),
);
ctx.register_udaf(my_avg);
// doesn't work as it was registered as non lowercase
let err = ctx.sql("SELECT MY_AVG(i) FROM t").await.unwrap_err();
assert!(err
.to_string()
.contains("Error during planning: Invalid function \'my_avg\'"));
// Can call it if you put quotes
let result = ctx
.sql("SELECT \"MY_AVG\"(i) FROM t")
.await?
.collect()
.await?;
let expected = [
"+-------------+",
"| MY_AVG(t.i) |",
"+-------------+",
"| 1.0 |",
"+-------------+",
];
assert_batches_eq!(expected, &result);
Ok(())
}
#[tokio::test]
async fn test_user_defined_functions_with_alias() -> Result<()> {
let ctx = SessionContext::new();
let arr = Int32Array::from(vec![1]);
let batch = RecordBatch::try_from_iter(vec![("i", Arc::new(arr) as _)])?;
ctx.register_batch("t", batch).unwrap();
let my_avg = create_udaf(
"dummy",
vec![DataType::Float64],
Arc::new(DataType::Float64),
Volatility::Immutable,
Arc::new(|_| Ok(Box::<AvgAccumulator>::default())),
Arc::new(vec![DataType::UInt64, DataType::Float64]),
)
.with_aliases(vec!["dummy_alias"]);
ctx.register_udaf(my_avg);
let expected = [
"+------------+",
"| dummy(t.i) |",
"+------------+",
"| 1.0 |",
"+------------+",
];
let result = plan_and_collect(&ctx, "SELECT dummy(i) FROM t").await?;
assert_batches_eq!(expected, &result);
let alias_result = plan_and_collect(&ctx, "SELECT dummy_alias(i) FROM t").await?;
assert_batches_eq!(expected, &alias_result);
Ok(())
}
#[tokio::test]
async fn test_groups_accumulator() -> Result<()> {
let ctx = SessionContext::new();
let arr = Int32Array::from(vec![1]);
let batch = RecordBatch::try_from_iter(vec![("a", Arc::new(arr) as _)])?;
ctx.register_batch("t", batch).unwrap();
let udaf = AggregateUDF::from(TestGroupsAccumulator {
signature: Signature::exact(vec![DataType::Float64], Volatility::Immutable),
result: 1,
});
ctx.register_udaf(udaf.clone());
let sql_df = ctx.sql("SELECT geo_mean(a) FROM t group by a").await?;
sql_df.show().await?;
Ok(())
}
#[tokio::test]
async fn test_parameterized_aggregate_udf() -> Result<()> {
let batch = RecordBatch::try_from_iter([(
"text",
Arc::new(StringArray::from(vec!["foo"])) as ArrayRef,
)])?;
let ctx = SessionContext::new();
ctx.register_batch("t", batch)?;
let t = ctx.table("t").await?;
let signature = Signature::exact(vec![DataType::Utf8], Volatility::Immutable);
let udf1 = AggregateUDF::from(TestGroupsAccumulator {
signature: signature.clone(),
result: 1,
});
let udf2 = AggregateUDF::from(TestGroupsAccumulator {
signature: signature.clone(),
result: 2,
});
let plan = LogicalPlanBuilder::from(t.into_optimized_plan()?)
.aggregate(
[col("text")],
[
udf1.call(vec![col("text")]).alias("a"),
udf2.call(vec![col("text")]).alias("b"),
],
)?
.build()?;
assert_eq!(
format!("{plan}"),
"Aggregate: groupBy=[[t.text]], aggr=[[geo_mean(t.text) AS a, geo_mean(t.text) AS b]]\n TableScan: t projection=[text]"
);
let actual = DataFrame::new(ctx.state(), plan).collect().await?;
let expected = [
"+------+---+---+",
"| text | a | b |",
"+------+---+---+",
"| foo | 1 | 2 |",
"+------+---+---+",
];
assert_batches_eq!(expected, &actual);
ctx.deregister_table("t")?;
Ok(())
}
/// Returns an context with a table "t" and the "first" and "time_sum"
/// aggregate functions registered.
///
/// "t" contains this data:
///
/// ```text
/// value | time
/// 3.0 | 1970-01-01T00:00:00.000003
/// 2.0 | 1970-01-01T00:00:00.000002
/// 1.0 | 1970-01-01T00:00:00.000004
/// 5.0 | 1970-01-01T00:00:00.000005
/// 5.0 | 1970-01-01T00:00:00.000005
/// ```
struct TestContext {
ctx: SessionContext,
test_state: Arc<TestState>,
}
impl TestContext {
fn new() -> Self {
let test_state = Arc::new(TestState::new());
Self::new_with_test_state(test_state)
}
fn new_with_test_state(test_state: Arc<TestState>) -> Self {
let value = Float64Array::from(vec![3.0, 2.0, 1.0, 5.0, 5.0]);
let time = TimestampNanosecondArray::from(vec![3000, 2000, 4000, 5000, 5000]);
let batch = RecordBatch::try_from_iter(vec![
("value", Arc::new(value) as _),
("time", Arc::new(time) as _),
])
.unwrap();
let mut ctx = SessionContext::new();
ctx.register_batch("t", batch).unwrap();
// Tell DataFusion about the "first" function
FirstSelector::register(&mut ctx);
// Tell DataFusion about the "time_sum" function
TimeSum::register(&mut ctx, Arc::clone(&test_state), "time_sum");
Self { ctx, test_state }
}
}
#[derive(Debug, Default)]
struct TestState {
/// was update_batch called?
update_batch: AtomicBool,
/// was retract_batch called?
retract_batch: AtomicBool,
/// should the udaf throw an error if retract batch is called? Can
/// only be configured at construction time.
error_on_retract_batch: bool,
}
impl TestState {
fn new() -> Self {
Default::default()
}
/// Has `update_batch` been called?
fn update_batch(&self) -> bool {
self.update_batch.load(Ordering::SeqCst)
}
/// Set the `update_batch` flag
fn set_update_batch(&self) {
self.update_batch.store(true, Ordering::SeqCst)
}
/// Has `retract_batch` been called?
fn retract_batch(&self) -> bool {
self.retract_batch.load(Ordering::SeqCst)
}
/// set the `retract_batch` flag
fn set_retract_batch(&self) {
self.retract_batch.store(true, Ordering::SeqCst)
}
/// Is this state configured to return an error on retract batch?
fn error_on_retract_batch(&self) -> bool {
self.error_on_retract_batch
}
/// Configure the test to return error on retract batch
fn with_error_on_retract_batch(mut self) -> Self {
self.error_on_retract_batch = true;
self
}
}
/// Models a user defined aggregate function that computes the a sum
/// of timestamps (not a quantity that has much real world meaning)
#[derive(Debug)]
struct TimeSum {
sum: i64,
test_state: Arc<TestState>,
}
impl TimeSum {
fn new(test_state: Arc<TestState>) -> Self {
Self { sum: 0, test_state }
}
fn register(ctx: &mut SessionContext, test_state: Arc<TestState>, name: &str) {
let timestamp_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
let input_type = vec![timestamp_type.clone()];
// Returns the same type as its input
let return_type = timestamp_type.clone();
let state_fields = vec![Field::new("sum", timestamp_type, true)];
let volatility = Volatility::Immutable;
let captured_state = Arc::clone(&test_state);
let accumulator: AccumulatorFactoryFunction =
Arc::new(move |_| Ok(Box::new(Self::new(Arc::clone(&captured_state)))));
let time_sum = AggregateUDF::from(SimpleAggregateUDF::new(
name,
input_type,
return_type,
volatility,
accumulator,
state_fields,
));
// register the selector as "time_sum"
ctx.register_udaf(time_sum)
}
}
impl Accumulator for TimeSum {
fn state(&mut self) -> Result<Vec<ScalarValue>> {
Ok(vec![self.evaluate()?])
}
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
self.test_state.set_update_batch();
assert_eq!(values.len(), 1);
let arr = &values[0];
let arr = arr.as_primitive::<TimestampNanosecondType>();
for v in arr.values().iter() {
self.sum += v;
}
Ok(())
}
fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
// merge and update is the same for time sum
self.update_batch(states)
}
fn evaluate(&mut self) -> Result<ScalarValue> {
Ok(ScalarValue::TimestampNanosecond(Some(self.sum), None))
}
fn size(&self) -> usize {
// accurate size estimates are not important for this example
42
}
fn retract_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
if self.test_state.error_on_retract_batch() {
return exec_err!("Error in Retract Batch");
}
self.test_state.set_retract_batch();
assert_eq!(values.len(), 1);
let arr = &values[0];
let arr = arr.as_primitive::<TimestampNanosecondType>();
for v in arr.values().iter() {
self.sum -= v;
}
Ok(())
}
fn supports_retract_batch(&self) -> bool {
!self.test_state.error_on_retract_batch()
}
}
/// Models a specialized timeseries aggregate function
/// called a "selector" in InfluxQL and Flux.
///
/// It returns the value and corresponding timestamp of the
/// input with the earliest timestamp as a structure.
#[derive(Debug, Clone)]
struct FirstSelector {
value: f64,
time: i64,
}
impl FirstSelector {
/// Create a new empty selector
fn new() -> Self {
Self {
value: 0.0,
time: i64::MAX,
}
}
fn register(ctx: &mut SessionContext) {
let return_type = Self::output_datatype();
let state_type = Self::state_datatypes();
let state_fields = state_type
.into_iter()
.enumerate()
.map(|(i, t)| Field::new(format!("{i}"), t, true))
.collect::<Vec<_>>();
// Possible input signatures
let signatures = vec![TypeSignature::Exact(Self::input_datatypes())];
let accumulator: AccumulatorFactoryFunction =
Arc::new(|_| Ok(Box::new(Self::new())));
let volatility = Volatility::Immutable;
let name = "first";
let first = AggregateUDF::from(SimpleAggregateUDF::new_with_signature(
name,
Signature::one_of(signatures, volatility),
return_type,
accumulator,
state_fields,
));
// register the selector as "first"
ctx.register_udaf(first)
}
/// Return the schema fields
fn fields() -> Fields {
vec![
Field::new("value", DataType::Float64, true),
Field::new(
"time",
DataType::Timestamp(TimeUnit::Nanosecond, None),
true,
),
]
.into()
}
fn output_datatype() -> DataType {
DataType::Struct(Self::fields())
}
fn input_datatypes() -> Vec<DataType> {
vec![
DataType::Float64,
DataType::Timestamp(TimeUnit::Nanosecond, None),
]
}
// Internally, keep the data types as this type
fn state_datatypes() -> Vec<DataType> {
vec![Self::output_datatype()]
}
/// Convert to a set of ScalarValues
fn to_state(&self) -> Result<ScalarValue> {
let f64arr = Arc::new(Float64Array::from(vec![self.value])) as ArrayRef;
let timearr =
Arc::new(TimestampNanosecondArray::from(vec![self.time])) as ArrayRef;
let struct_arr =
StructArray::try_new(Self::fields(), vec![f64arr, timearr], None)?;
Ok(ScalarValue::Struct(Arc::new(struct_arr)))
}
}
impl Accumulator for FirstSelector {
fn state(&mut self) -> Result<Vec<ScalarValue>> {
self.evaluate().map(|s| vec![s])
}
/// produce the output structure
fn evaluate(&mut self) -> Result<ScalarValue> {
self.to_state()
}
fn update_batch(&mut self, values: &[ArrayRef]) -> Result<()> {
// cast argumets to the appropriate type (DataFusion will type
// check these based on the declared allowed input types)
let v = as_primitive_array::<Float64Type>(&values[0])?;
let t = as_primitive_array::<TimestampNanosecondType>(&values[1])?;
// Update the actual values
for (value, time) in v.iter().zip(t.iter()) {
if let (Some(time), Some(value)) = (time, value) {
if time < self.time {
self.value = value;
self.time = time;
}
}
}
Ok(())
}
fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
// same logic is needed as in update_batch
self.update_batch(states)
}
fn size(&self) -> usize {
std::mem::size_of_val(self)
}
}
#[derive(Debug, Clone)]
struct TestGroupsAccumulator {
signature: Signature,
result: u64,
}
impl AggregateUDFImpl for TestGroupsAccumulator {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn name(&self) -> &str {
"geo_mean"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(DataType::UInt64)
}
fn accumulator(&self, _acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
// should use groups accumulator
panic!("accumulator shouldn't invoke");
}
fn groups_accumulator_supported(&self, _args: AccumulatorArgs) -> bool {
true
}
fn create_groups_accumulator(
&self,
_args: AccumulatorArgs,
) -> Result<Box<dyn GroupsAccumulator>> {
Ok(Box::new(self.clone()))
}
fn equals(&self, other: &dyn AggregateUDFImpl) -> bool {
if let Some(other) = other.as_any().downcast_ref::<TestGroupsAccumulator>() {
self.result == other.result && self.signature == other.signature
} else {
false
}
}
fn hash_value(&self) -> u64 {
let hasher = &mut DefaultHasher::new();
self.signature.hash(hasher);
self.result.hash(hasher);
hasher.finish()
}
}
impl Accumulator for TestGroupsAccumulator {
fn update_batch(&mut self, _values: &[ArrayRef]) -> Result<()> {
Ok(())
}
fn evaluate(&mut self) -> Result<ScalarValue> {
Ok(ScalarValue::from(self.result))
}
fn size(&self) -> usize {
std::mem::size_of::<u64>()
}
fn state(&mut self) -> Result<Vec<ScalarValue>> {
Ok(vec![ScalarValue::from(self.result)])
}
fn merge_batch(&mut self, _states: &[ArrayRef]) -> Result<()> {
Ok(())
}
}
impl GroupsAccumulator for TestGroupsAccumulator {
fn update_batch(
&mut self,
_values: &[ArrayRef],
_group_indices: &[usize],
_opt_filter: Option<&arrow_array::BooleanArray>,
_total_num_groups: usize,
) -> Result<()> {
Ok(())
}
fn evaluate(&mut self, _emit_to: datafusion_expr::EmitTo) -> Result<ArrayRef> {
Ok(Arc::new(PrimitiveArray::<UInt64Type>::new(
vec![self.result].into(),
None,
)) as ArrayRef)
}
fn state(&mut self, _emit_to: datafusion_expr::EmitTo) -> Result<Vec<ArrayRef>> {
Ok(vec![Arc::new(PrimitiveArray::<UInt64Type>::new(
vec![self.result].into(),
None,
)) as ArrayRef])
}
fn merge_batch(
&mut self,
_values: &[ArrayRef],
_group_indices: &[usize],
_opt_filter: Option<&arrow_array::BooleanArray>,
_total_num_groups: usize,
) -> Result<()> {
Ok(())
}
fn size(&self) -> usize {
std::mem::size_of::<u64>()
}
}