github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/merger/aggregatemerger/aggregator/avg.go (about)

     1  // Copyright 2021 ecodeclub
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aggregator
    16  
    17  import (
    18  	"reflect"
    19  
    20  	"github.com/ecodeclub/eorm/internal/merger"
    21  
    22  	"github.com/ecodeclub/eorm/internal/merger/internal/errs"
    23  )
    24  
    25  // AVG 用于求平均值,通过sum/count求得。
    26  // AVG 我们并不能预期在不同的数据库上,精度会不会损失,以及损失的话会有多少的损失。这很大程度上跟数据库类型,数据库驱动实现都有关
    27  type AVG struct {
    28  	sumColumnInfo   merger.ColumnInfo
    29  	countColumnInfo merger.ColumnInfo
    30  	avgName         string
    31  }
    32  
    33  // NewAVG sumInfo是sum的信息,countInfo是count的信息,avgName用于Column方法
    34  func NewAVG(sumInfo merger.ColumnInfo, countInfo merger.ColumnInfo, avgName string) *AVG {
    35  	return &AVG{
    36  		sumColumnInfo:   sumInfo,
    37  		countColumnInfo: countInfo,
    38  		avgName:         avgName,
    39  	}
    40  }
    41  
    42  func (a *AVG) Aggregate(cols [][]any) (any, error) {
    43  	// cols[0] 代表第一个sql.Rows,用于确定avgFunc
    44  	avgFunc, err := a.findAvgFunc(cols[0])
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return avgFunc(cols, a.sumColumnInfo.Index, a.countColumnInfo.Index)
    49  }
    50  
    51  func (a *AVG) findAvgFunc(col []any) (func([][]any, int, int) (float64, error), error) {
    52  	sumIndex := a.sumColumnInfo.Index
    53  	countIndex := a.countColumnInfo.Index
    54  	if sumIndex >= len(col) || sumIndex < 0 || countIndex >= len(col) || countIndex < 0 {
    55  		return nil, errs.ErrMergerInvalidAggregateColumnIndex
    56  	}
    57  	sumKind := reflect.TypeOf(col[sumIndex]).Kind()
    58  	countKind := reflect.TypeOf(col[countIndex]).Kind()
    59  	val, ok := avgAggregateFuncMapping[[2]reflect.Kind{sumKind, countKind}]
    60  	if !ok {
    61  		return nil, errs.ErrMergerAggregateFuncNotFound
    62  	}
    63  	return val, nil
    64  }
    65  
    66  func (a *AVG) ColumnName() string {
    67  	return a.avgName
    68  }
    69  
    70  // avgAggregator cols就是上面Aggregate的入参cols可以参Aggregate的描述
    71  func avgAggregator[S AggregateElement, C AggregateElement](cols [][]any, sumIndex int, countIndex int) (float64, error) {
    72  	var sum S
    73  	var count C
    74  	for _, col := range cols {
    75  		sum += col[sumIndex].(S)
    76  		count += col[countIndex].(C)
    77  	}
    78  	val := float64(sum) / float64(count)
    79  	return val, nil
    80  
    81  }
    82  
    83  var avgAggregateFuncMapping = map[[2]reflect.Kind]func([][]any, int, int) (float64, error){
    84  	[2]reflect.Kind{reflect.Int, reflect.Int}:     avgAggregator[int, int],
    85  	[2]reflect.Kind{reflect.Int, reflect.Int8}:    avgAggregator[int, int8],
    86  	[2]reflect.Kind{reflect.Int, reflect.Int16}:   avgAggregator[int, int16],
    87  	[2]reflect.Kind{reflect.Int, reflect.Int32}:   avgAggregator[int, int32],
    88  	[2]reflect.Kind{reflect.Int, reflect.Int64}:   avgAggregator[int, int64],
    89  	[2]reflect.Kind{reflect.Int, reflect.Uint}:    avgAggregator[int, uint],
    90  	[2]reflect.Kind{reflect.Int, reflect.Uint8}:   avgAggregator[int, uint8],
    91  	[2]reflect.Kind{reflect.Int, reflect.Uint16}:  avgAggregator[int, uint16],
    92  	[2]reflect.Kind{reflect.Int, reflect.Uint32}:  avgAggregator[int, uint32],
    93  	[2]reflect.Kind{reflect.Int, reflect.Uint64}:  avgAggregator[int, uint64],
    94  	[2]reflect.Kind{reflect.Int, reflect.Float32}: avgAggregator[int, float32],
    95  	[2]reflect.Kind{reflect.Int, reflect.Float64}: avgAggregator[int, float64],
    96  
    97  	[2]reflect.Kind{reflect.Int8, reflect.Int}:     avgAggregator[int8, int],
    98  	[2]reflect.Kind{reflect.Int8, reflect.Int8}:    avgAggregator[int8, int8],
    99  	[2]reflect.Kind{reflect.Int8, reflect.Int16}:   avgAggregator[int8, int16],
   100  	[2]reflect.Kind{reflect.Int8, reflect.Int32}:   avgAggregator[int8, int32],
   101  	[2]reflect.Kind{reflect.Int8, reflect.Int64}:   avgAggregator[int8, int64],
   102  	[2]reflect.Kind{reflect.Int8, reflect.Uint}:    avgAggregator[int8, uint],
   103  	[2]reflect.Kind{reflect.Int8, reflect.Uint8}:   avgAggregator[int8, uint8],
   104  	[2]reflect.Kind{reflect.Int8, reflect.Uint16}:  avgAggregator[int8, uint16],
   105  	[2]reflect.Kind{reflect.Int8, reflect.Uint32}:  avgAggregator[int8, uint32],
   106  	[2]reflect.Kind{reflect.Int8, reflect.Uint64}:  avgAggregator[int8, uint64],
   107  	[2]reflect.Kind{reflect.Int8, reflect.Float32}: avgAggregator[int8, float32],
   108  	[2]reflect.Kind{reflect.Int8, reflect.Float64}: avgAggregator[int8, float64],
   109  
   110  	[2]reflect.Kind{reflect.Int16, reflect.Int}:     avgAggregator[int16, int],
   111  	[2]reflect.Kind{reflect.Int16, reflect.Int8}:    avgAggregator[int16, int8],
   112  	[2]reflect.Kind{reflect.Int16, reflect.Int16}:   avgAggregator[int16, int16],
   113  	[2]reflect.Kind{reflect.Int16, reflect.Int32}:   avgAggregator[int16, int32],
   114  	[2]reflect.Kind{reflect.Int16, reflect.Int64}:   avgAggregator[int16, int64],
   115  	[2]reflect.Kind{reflect.Int16, reflect.Uint}:    avgAggregator[int16, uint],
   116  	[2]reflect.Kind{reflect.Int16, reflect.Uint8}:   avgAggregator[int16, uint8],
   117  	[2]reflect.Kind{reflect.Int16, reflect.Uint16}:  avgAggregator[int16, uint16],
   118  	[2]reflect.Kind{reflect.Int16, reflect.Uint32}:  avgAggregator[int16, uint32],
   119  	[2]reflect.Kind{reflect.Int16, reflect.Uint64}:  avgAggregator[int16, uint64],
   120  	[2]reflect.Kind{reflect.Int16, reflect.Float32}: avgAggregator[int16, float32],
   121  	[2]reflect.Kind{reflect.Int16, reflect.Float64}: avgAggregator[int16, float64],
   122  
   123  	[2]reflect.Kind{reflect.Int32, reflect.Int}:     avgAggregator[int16, int],
   124  	[2]reflect.Kind{reflect.Int32, reflect.Int8}:    avgAggregator[int16, int8],
   125  	[2]reflect.Kind{reflect.Int32, reflect.Int16}:   avgAggregator[int16, int16],
   126  	[2]reflect.Kind{reflect.Int32, reflect.Int32}:   avgAggregator[int16, int32],
   127  	[2]reflect.Kind{reflect.Int32, reflect.Int64}:   avgAggregator[int16, int64],
   128  	[2]reflect.Kind{reflect.Int32, reflect.Uint}:    avgAggregator[int16, uint],
   129  	[2]reflect.Kind{reflect.Int32, reflect.Uint8}:   avgAggregator[int16, uint8],
   130  	[2]reflect.Kind{reflect.Int32, reflect.Uint16}:  avgAggregator[int16, uint16],
   131  	[2]reflect.Kind{reflect.Int32, reflect.Uint32}:  avgAggregator[int16, uint32],
   132  	[2]reflect.Kind{reflect.Int32, reflect.Uint64}:  avgAggregator[int16, uint64],
   133  	[2]reflect.Kind{reflect.Int32, reflect.Float32}: avgAggregator[int16, float32],
   134  	[2]reflect.Kind{reflect.Int32, reflect.Float64}: avgAggregator[int16, float64],
   135  
   136  	[2]reflect.Kind{reflect.Int64, reflect.Int}:     avgAggregator[int64, int],
   137  	[2]reflect.Kind{reflect.Int64, reflect.Int8}:    avgAggregator[int64, int8],
   138  	[2]reflect.Kind{reflect.Int64, reflect.Int16}:   avgAggregator[int64, int16],
   139  	[2]reflect.Kind{reflect.Int64, reflect.Int32}:   avgAggregator[int64, int32],
   140  	[2]reflect.Kind{reflect.Int64, reflect.Int64}:   avgAggregator[int64, int64],
   141  	[2]reflect.Kind{reflect.Int64, reflect.Uint}:    avgAggregator[int64, uint],
   142  	[2]reflect.Kind{reflect.Int64, reflect.Uint8}:   avgAggregator[int64, uint8],
   143  	[2]reflect.Kind{reflect.Int64, reflect.Uint16}:  avgAggregator[int64, uint16],
   144  	[2]reflect.Kind{reflect.Int64, reflect.Uint32}:  avgAggregator[int64, uint32],
   145  	[2]reflect.Kind{reflect.Int64, reflect.Uint64}:  avgAggregator[int64, uint64],
   146  	[2]reflect.Kind{reflect.Int64, reflect.Float32}: avgAggregator[int64, float32],
   147  	[2]reflect.Kind{reflect.Int64, reflect.Float64}: avgAggregator[int64, float64],
   148  
   149  	[2]reflect.Kind{reflect.Uint, reflect.Int}:     avgAggregator[uint, int],
   150  	[2]reflect.Kind{reflect.Uint, reflect.Int8}:    avgAggregator[uint, int8],
   151  	[2]reflect.Kind{reflect.Uint, reflect.Int16}:   avgAggregator[uint, int16],
   152  	[2]reflect.Kind{reflect.Uint, reflect.Int32}:   avgAggregator[uint, int32],
   153  	[2]reflect.Kind{reflect.Uint, reflect.Int64}:   avgAggregator[uint, int64],
   154  	[2]reflect.Kind{reflect.Uint, reflect.Uint}:    avgAggregator[uint, uint],
   155  	[2]reflect.Kind{reflect.Uint, reflect.Uint8}:   avgAggregator[uint, uint8],
   156  	[2]reflect.Kind{reflect.Uint, reflect.Uint16}:  avgAggregator[uint, uint16],
   157  	[2]reflect.Kind{reflect.Uint, reflect.Uint32}:  avgAggregator[uint, uint32],
   158  	[2]reflect.Kind{reflect.Uint, reflect.Uint64}:  avgAggregator[uint, uint64],
   159  	[2]reflect.Kind{reflect.Uint, reflect.Float32}: avgAggregator[uint, float32],
   160  	[2]reflect.Kind{reflect.Uint, reflect.Float64}: avgAggregator[uint, float64],
   161  
   162  	[2]reflect.Kind{reflect.Uint8, reflect.Int}:     avgAggregator[uint8, int],
   163  	[2]reflect.Kind{reflect.Uint8, reflect.Int8}:    avgAggregator[uint8, int8],
   164  	[2]reflect.Kind{reflect.Uint8, reflect.Int16}:   avgAggregator[uint8, int16],
   165  	[2]reflect.Kind{reflect.Uint8, reflect.Int32}:   avgAggregator[uint8, int32],
   166  	[2]reflect.Kind{reflect.Uint8, reflect.Int64}:   avgAggregator[uint8, int64],
   167  	[2]reflect.Kind{reflect.Uint8, reflect.Uint}:    avgAggregator[uint8, uint],
   168  	[2]reflect.Kind{reflect.Uint8, reflect.Uint8}:   avgAggregator[uint8, uint8],
   169  	[2]reflect.Kind{reflect.Uint8, reflect.Uint16}:  avgAggregator[uint8, uint16],
   170  	[2]reflect.Kind{reflect.Uint8, reflect.Uint32}:  avgAggregator[uint8, uint32],
   171  	[2]reflect.Kind{reflect.Uint8, reflect.Uint64}:  avgAggregator[uint8, uint64],
   172  	[2]reflect.Kind{reflect.Uint8, reflect.Float32}: avgAggregator[uint8, float32],
   173  	[2]reflect.Kind{reflect.Uint8, reflect.Float64}: avgAggregator[uint8, float64],
   174  
   175  	[2]reflect.Kind{reflect.Uint16, reflect.Int}:     avgAggregator[uint16, int],
   176  	[2]reflect.Kind{reflect.Uint16, reflect.Int8}:    avgAggregator[uint16, int8],
   177  	[2]reflect.Kind{reflect.Uint16, reflect.Int16}:   avgAggregator[uint16, int16],
   178  	[2]reflect.Kind{reflect.Uint16, reflect.Int32}:   avgAggregator[uint16, int32],
   179  	[2]reflect.Kind{reflect.Uint16, reflect.Int64}:   avgAggregator[uint16, int64],
   180  	[2]reflect.Kind{reflect.Uint16, reflect.Uint}:    avgAggregator[uint16, uint],
   181  	[2]reflect.Kind{reflect.Uint16, reflect.Uint8}:   avgAggregator[uint16, uint8],
   182  	[2]reflect.Kind{reflect.Uint16, reflect.Uint16}:  avgAggregator[uint16, uint16],
   183  	[2]reflect.Kind{reflect.Uint16, reflect.Uint32}:  avgAggregator[uint16, uint32],
   184  	[2]reflect.Kind{reflect.Uint16, reflect.Uint64}:  avgAggregator[uint16, uint64],
   185  	[2]reflect.Kind{reflect.Uint16, reflect.Float32}: avgAggregator[uint16, float32],
   186  	[2]reflect.Kind{reflect.Uint16, reflect.Float64}: avgAggregator[uint16, float64],
   187  
   188  	[2]reflect.Kind{reflect.Uint32, reflect.Int}:     avgAggregator[uint32, int],
   189  	[2]reflect.Kind{reflect.Uint32, reflect.Int8}:    avgAggregator[uint32, int8],
   190  	[2]reflect.Kind{reflect.Uint32, reflect.Int16}:   avgAggregator[uint32, int16],
   191  	[2]reflect.Kind{reflect.Uint32, reflect.Int32}:   avgAggregator[uint32, int32],
   192  	[2]reflect.Kind{reflect.Uint32, reflect.Int64}:   avgAggregator[uint32, int64],
   193  	[2]reflect.Kind{reflect.Uint32, reflect.Uint}:    avgAggregator[uint32, uint],
   194  	[2]reflect.Kind{reflect.Uint32, reflect.Uint8}:   avgAggregator[uint32, uint8],
   195  	[2]reflect.Kind{reflect.Uint32, reflect.Uint16}:  avgAggregator[uint32, uint16],
   196  	[2]reflect.Kind{reflect.Uint32, reflect.Uint32}:  avgAggregator[uint32, uint32],
   197  	[2]reflect.Kind{reflect.Uint32, reflect.Uint64}:  avgAggregator[uint32, uint64],
   198  	[2]reflect.Kind{reflect.Uint32, reflect.Float32}: avgAggregator[uint32, float32],
   199  	[2]reflect.Kind{reflect.Uint32, reflect.Float64}: avgAggregator[uint32, float64],
   200  
   201  	[2]reflect.Kind{reflect.Uint64, reflect.Int}:     avgAggregator[uint64, int],
   202  	[2]reflect.Kind{reflect.Uint64, reflect.Int8}:    avgAggregator[uint64, int8],
   203  	[2]reflect.Kind{reflect.Uint64, reflect.Int16}:   avgAggregator[uint64, int16],
   204  	[2]reflect.Kind{reflect.Uint64, reflect.Int32}:   avgAggregator[uint64, int32],
   205  	[2]reflect.Kind{reflect.Uint64, reflect.Int64}:   avgAggregator[uint64, int64],
   206  	[2]reflect.Kind{reflect.Uint64, reflect.Uint}:    avgAggregator[uint64, uint],
   207  	[2]reflect.Kind{reflect.Uint64, reflect.Uint8}:   avgAggregator[uint64, uint8],
   208  	[2]reflect.Kind{reflect.Uint64, reflect.Uint16}:  avgAggregator[uint64, uint16],
   209  	[2]reflect.Kind{reflect.Uint64, reflect.Uint32}:  avgAggregator[uint64, uint32],
   210  	[2]reflect.Kind{reflect.Uint64, reflect.Uint64}:  avgAggregator[uint64, uint64],
   211  	[2]reflect.Kind{reflect.Uint64, reflect.Float32}: avgAggregator[uint64, float32],
   212  	[2]reflect.Kind{reflect.Uint64, reflect.Float64}: avgAggregator[uint64, float64],
   213  
   214  	[2]reflect.Kind{reflect.Float32, reflect.Int}:     avgAggregator[float32, int],
   215  	[2]reflect.Kind{reflect.Float32, reflect.Int8}:    avgAggregator[float32, int8],
   216  	[2]reflect.Kind{reflect.Float32, reflect.Int16}:   avgAggregator[float32, int16],
   217  	[2]reflect.Kind{reflect.Float32, reflect.Int32}:   avgAggregator[float32, int32],
   218  	[2]reflect.Kind{reflect.Float32, reflect.Int64}:   avgAggregator[float32, int64],
   219  	[2]reflect.Kind{reflect.Float32, reflect.Uint}:    avgAggregator[float32, uint],
   220  	[2]reflect.Kind{reflect.Float32, reflect.Uint8}:   avgAggregator[float32, uint8],
   221  	[2]reflect.Kind{reflect.Float32, reflect.Uint16}:  avgAggregator[float32, uint16],
   222  	[2]reflect.Kind{reflect.Float32, reflect.Uint32}:  avgAggregator[float32, uint32],
   223  	[2]reflect.Kind{reflect.Float32, reflect.Uint64}:  avgAggregator[float32, uint64],
   224  	[2]reflect.Kind{reflect.Float32, reflect.Float32}: avgAggregator[float32, float32],
   225  	[2]reflect.Kind{reflect.Float32, reflect.Float64}: avgAggregator[float32, float64],
   226  
   227  	[2]reflect.Kind{reflect.Float64, reflect.Int}:     avgAggregator[float64, int],
   228  	[2]reflect.Kind{reflect.Float64, reflect.Int8}:    avgAggregator[float64, int8],
   229  	[2]reflect.Kind{reflect.Float64, reflect.Int16}:   avgAggregator[float64, int16],
   230  	[2]reflect.Kind{reflect.Float64, reflect.Int32}:   avgAggregator[float64, int32],
   231  	[2]reflect.Kind{reflect.Float64, reflect.Int64}:   avgAggregator[float64, int64],
   232  	[2]reflect.Kind{reflect.Float64, reflect.Uint}:    avgAggregator[float64, uint],
   233  	[2]reflect.Kind{reflect.Float64, reflect.Uint8}:   avgAggregator[float64, uint8],
   234  	[2]reflect.Kind{reflect.Float64, reflect.Uint16}:  avgAggregator[float64, uint16],
   235  	[2]reflect.Kind{reflect.Float64, reflect.Uint32}:  avgAggregator[float64, uint32],
   236  	[2]reflect.Kind{reflect.Float64, reflect.Uint64}:  avgAggregator[float64, uint64],
   237  	[2]reflect.Kind{reflect.Float64, reflect.Float32}: avgAggregator[float64, float32],
   238  	[2]reflect.Kind{reflect.Float64, reflect.Float64}: avgAggregator[float64, float64],
   239  }