1+ // Licensed to the Apache Software Foundation (ASF) under one
2+ // or more contributor license agreements. See the NOTICE file
3+ // distributed with this work for additional information
4+ // regarding copyright ownership. The ASF licenses this file
5+ // to you under the Apache License, Version 2.0 (the
6+ // "License"); you may not use this file except in compliance
7+ // with the License. You may obtain a copy of the License at
8+ //
9+ // http://www.apache.org/licenses/LICENSE-2.0
10+ //
11+ // Unless required by applicable law or agreed to in writing,
12+ // software distributed under the License is distributed on an
13+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+ // KIND, either express or implied. See the License for the
15+ // specific language governing permissions and limitations
16+ // under the License.
17+
18+ use std:: any:: Any ;
19+ use arrow:: array:: { ArrayRef , BooleanArray } ;
20+ use arrow:: datatypes:: { DataType , FieldRef } ;
21+ use datafusion:: common:: { DataFusionError , Result as DFResult , ScalarValue } ;
22+ use datafusion:: logical_expr:: { Accumulator , AggregateUDFImpl , EmitTo , GroupsAccumulator , ReversedUDAF , Signature } ;
23+ use datafusion:: logical_expr:: function:: { AccumulatorArgs , StateFieldsArgs } ;
24+ use datafusion:: logical_expr:: type_coercion:: aggregates:: avg_return_type;
25+ use datafusion:: logical_expr:: Volatility :: Immutable ;
26+ use crate :: { AvgDecimal , EvalMode } ;
27+
28+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
29+ pub struct AvgInt {
30+ signature : Signature ,
31+ eval_mode : EvalMode ,
32+ }
33+
34+ impl AvgInt {
35+ pub fn try_new ( data_type : DataType , eval_mode : EvalMode ) -> DFResult < Self > {
36+ match data_type {
37+ DataType :: Int8 | DataType :: Int16 | DataType :: Int32 | DataType :: Int64 => {
38+ Ok ( Self {
39+ signature : Signature :: user_defined ( Immutable ) ,
40+ eval_mode
41+ } )
42+ } ,
43+ _ => { Err ( DataFusionError :: Internal ( "inalid data type for AvgInt" . to_string ( ) ) ) }
44+ }
45+ }
46+ }
47+
48+ impl AggregateUDFImpl for AvgInt {
49+ fn as_any ( & self ) -> & dyn Any {
50+ self
51+ }
52+
53+ fn name ( & self ) -> & str {
54+ "avg"
55+ }
56+
57+ fn reverse_expr ( & self ) -> ReversedUDAF {
58+ ReversedUDAF :: Identical
59+ }
60+
61+ fn signature ( & self ) -> & Signature {
62+ & self . signature
63+ }
64+
65+ fn return_type ( & self , arg_types : & [ DataType ] ) -> datafusion:: common:: Result < DataType > {
66+ avg_return_type ( self . name ( ) , & arg_types[ 0 ] )
67+ }
68+
69+ fn is_nullable ( & self ) -> bool {
70+ true
71+ }
72+
73+ fn accumulator ( & self , acc_args : AccumulatorArgs ) -> datafusion:: common:: Result < Box < dyn Accumulator > > {
74+ todo ! ( )
75+ }
76+
77+ fn state_fields ( & self , args : StateFieldsArgs ) -> datafusion:: common:: Result < Vec < FieldRef > > {
78+ todo ! ( )
79+ }
80+
81+ fn groups_accumulator_supported ( & self , _args : AccumulatorArgs ) -> bool {
82+ false
83+ }
84+
85+ fn create_groups_accumulator ( & self , _args : AccumulatorArgs ) -> datafusion:: common:: Result < Box < dyn GroupsAccumulator > > {
86+ Ok ( Box :: new ( AvgIntGroupsAccumulator :: new ( self . eval_mode ) ) )
87+ }
88+
89+ fn default_value ( & self , data_type : & DataType ) -> datafusion:: common:: Result < ScalarValue > {
90+ todo ! ( )
91+ }
92+ }
93+
94+ struct AvgIntegerAccumulator {
95+ sum : Option < i64 > ,
96+ count : u64 ,
97+ eval_mode : EvalMode ,
98+ }
99+
100+ impl AvgIntegerAccumulator {
101+ fn new ( eval_mode : EvalMode ) -> Self {
102+ Self {
103+ sum : Some ( 0 ) ,
104+ count : 0 ,
105+ eval_mode
106+ }
107+ }
108+ }
109+
110+ impl Accumulator for AvgIntegerAccumulator {
111+
112+ }
113+
114+ struct AvgIntGroupsAccumulator {
115+
116+ }
117+
118+ impl AvgIntGroupsAccumulator {
119+
120+ }
121+
122+
123+ impl GroupsAccumulator for AvgIntGroupsAccumulator {
124+ fn update_batch ( & mut self , values : & [ ArrayRef ] , group_indices : & [ usize ] , opt_filter : Option < & BooleanArray > , total_num_groups : usize ) -> datafusion:: common:: Result < ( ) > {
125+ todo ! ( )
126+ }
127+
128+ fn evaluate ( & mut self , emit_to : EmitTo ) -> datafusion:: common:: Result < ArrayRef > {
129+ todo ! ( )
130+ }
131+
132+ fn state ( & mut self , emit_to : EmitTo ) -> datafusion:: common:: Result < Vec < ArrayRef > > {
133+ todo ! ( )
134+ }
135+
136+ fn merge_batch ( & mut self , values : & [ ArrayRef ] , group_indices : & [ usize ] , opt_filter : Option < & BooleanArray > , total_num_groups : usize ) -> datafusion:: common:: Result < ( ) > {
137+ todo ! ( )
138+ }
139+
140+ fn size ( & self ) -> usize {
141+ todo ! ( )
142+ }
143+ }
0 commit comments