Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

聚合函数nullable类型支持 #226

Merged
merged 6 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
- [refactor(merger): 重构AVG函数实现,重构所有rows.Rows实现的ConlumnType方法并添加测试](https://github.com/ecodeclub/eorm/pull/223)
- [feat(merger): 新增Distinct Merger](https://github.com/ecodeclub/eorm/pull/224)
- [refactor(merger): 去掉无用代码及过期注释,整理代码](https://github.com/ecodeclub/eorm/pull/225)
- [eorm: 结果集处理--聚合函数支持nullable类型的数据](https://github.com/ecodeclub/eorm/pull/226)

## v0.0.1:
- [Init Project](https://github.com/ecodeclub/eorm/pull/1)
- [Selector Definition](https://github.com/ecodeclub/eorm/pull/2)
Expand Down
102 changes: 91 additions & 11 deletions internal/merger/internal/aggregatemerger/aggregator/avg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package aggregator

import (
"database/sql"
"database/sql/driver"
"reflect"

"github.com/ecodeclub/eorm/internal/merger"
Expand All @@ -31,7 +33,7 @@ type AVG struct {
countColumnInfo merger.ColumnInfo
}

// NewAVG sumInfo是sum的信息,countInfo是count的信息,avgName用于Column方法
// NewAVG avgInfo是avg列的信息, sumInfo是sum列的信息,countInfo是count列的信息
func NewAVG(avgInfo, sumInfo, countInfo merger.ColumnInfo) *AVG {
return &AVG{
name: "AVG",
Expand All @@ -50,19 +52,14 @@ func (a *AVG) Aggregate(cols [][]any) (any, error) {
return avgFunc(cols, a.sumColumnInfo.Index, a.countColumnInfo.Index)
}

func (a *AVG) findAvgFunc(col []any) (func([][]any, int, int) (float64, error), error) {
func (a *AVG) findAvgFunc(col []any) (func([][]any, int, int) (any, error), error) {
sumIndex := a.sumColumnInfo.Index
countIndex := a.countColumnInfo.Index
if sumIndex >= len(col) || sumIndex < 0 || countIndex >= len(col) || countIndex < 0 {
return nil, errs.ErrMergerInvalidAggregateColumnIndex
}
sumKind := reflect.TypeOf(col[sumIndex]).Kind()
countKind := reflect.TypeOf(col[countIndex]).Kind()
val, ok := avgAggregateFuncMapping[[2]reflect.Kind{sumKind, countKind}]
if !ok {
return nil, errs.ErrMergerAggregateFuncNotFound
}
return val, nil

return a.avgNullableAggregator, nil
}

func (a *AVG) ColumnInfo() merger.ColumnInfo {
Expand All @@ -74,7 +71,7 @@ func (a *AVG) Name() string {
}

// avgAggregator cols就是上面Aggregate的入参cols可以参Aggregate的描述
func avgAggregator[S AggregateElement, C AggregateElement](cols [][]any, sumIndex int, countIndex int) (float64, error) {
func avgAggregator[S AggregateElement, C AggregateElement](cols [][]any, sumIndex int, countIndex int) (any, error) {
var sum S
var count C
for _, col := range cols {
Expand All @@ -86,7 +83,90 @@ func avgAggregator[S AggregateElement, C AggregateElement](cols [][]any, sumInde

}

var avgAggregateFuncMapping = map[[2]reflect.Kind]func([][]any, int, int) (float64, error){
func (a *AVG) avgNullableAggregator(cols [][]any, sumIndex int, countIndex int) (any, error) {
notNullCols := make([][]any, 0, len(cols))
var sumValKind, countValKind reflect.Kind
var sumZeroVal, countZeroVal any
for _, col := range cols {
sumZeroVal = a.getZeroVal(col, sumIndex)
if sumZeroVal != nil {
break
}
}
for _, col := range cols {
countZeroVal = a.getZeroVal(col, countIndex)
if countZeroVal != nil {
break
}
}
for _, col := range cols {
var sumVal, countVal any
var kind reflect.Kind
col, sumVal, kind = a.setColInfo(col, sumIndex, sumZeroVal)
// 需要不为nil
if kind != reflect.Invalid {
sumValKind = kind
}
col, countVal, kind = a.setColInfo(col, countIndex, countZeroVal)
// 需要不为nil
if kind != reflect.Invalid {
countValKind = kind
}
// 都为nil就没必要进行聚合函数计算了
if sumVal != nil || countVal != nil {
notNullCols = append(notNullCols, col)
}
}
if sumValKind != reflect.Invalid && countValKind != reflect.Invalid {
// 说明几个count列 或者 sum列有不为null的列
avgFunc, ok := avgAggregateFuncMapping[[2]reflect.Kind{sumValKind, countValKind}]
if !ok {
return nil, errs.ErrMergerAggregateFuncNotFound
}
return avgFunc(notNullCols, sumIndex, countIndex)
}
return sql.NullFloat64{
Valid: false,
}, nil
}

func (*AVG) getZeroVal(cols []any, index int) any {
var zeroVal any
col := cols[index]
if reflect.TypeOf(col).Kind() == reflect.Struct {
colVal, _ := col.(driver.Valuer).Value()
if colVal != nil {
zeroVal = reflect.Zero(reflect.TypeOf(colVal)).Interface()
}
} else {
zeroVal = reflect.Zero(reflect.TypeOf(col)).Interface()
}
return zeroVal
}

func (*AVG) setColInfo(col []any, index int, zeroVal any) ([]any, any, reflect.Kind) {
indexCol := col[index]
indexValKind := reflect.Invalid
indexKind := reflect.TypeOf(indexCol).Kind()
var colVal any
if indexKind == reflect.Struct {
// sum列为sql null类型
colVal, _ = col[index].(driver.Valuer).Value()
if colVal == nil {
// 如果是nil用0这些初值表示
col[index] = zeroVal
} else {
indexValKind = reflect.TypeOf(colVal).Kind()
col[index] = colVal
}
} else {
colVal = col[index]
indexValKind = reflect.TypeOf(colVal).Kind()
}
return col, colVal, indexValKind
}

var avgAggregateFuncMapping = map[[2]reflect.Kind]func([][]any, int, int) (any, error){
[2]reflect.Kind{reflect.Int, reflect.Int}: avgAggregator[int, int],
[2]reflect.Kind{reflect.Int, reflect.Int8}: avgAggregator[int, int8],
[2]reflect.Kind{reflect.Int, reflect.Int16}: avgAggregator[int, int16],
Expand Down
168 changes: 168 additions & 0 deletions internal/merger/internal/aggregatemerger/aggregator/avg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package aggregator

import (
"database/sql"
"testing"

"github.com/ecodeclub/eorm/internal/merger"
Expand Down Expand Up @@ -86,6 +87,173 @@ func TestAvg_Aggregate(t *testing.T) {
index: []int{0, 3, 10},
wantErr: errs.ErrMergerInvalidAggregateColumnIndex,
},
{
name: "count和sum均为nullable类型",
input: [][]any{
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: false,
},
sql.NullInt64{
Valid: true,
Int64: 4,
},
},
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: false,
},
sql.NullInt64{
Valid: true,
Int64: 4,
},
},
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: true,
Int64: 24,
},
sql.NullInt64{
Valid: true,
Int64: 4,
},
},
},
index: []int{0, 1, 2},
wantVal: float64(2),
},
{
name: "count和sum既有nullable又有nullable类型的",
input: [][]any{
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: true,
Int64: 4,
},
sql.NullInt64{
Valid: true,
Int64: 4,
},
},
{
sql.NullFloat64{
Valid: false,
},
int64(4),
int64(4),
},
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: false,
},
sql.NullInt64{
Valid: false,
},
},
},
index: []int{0, 1, 2},
wantVal: float64(1),
},
{
name: "sum所有列都为nil",
input: [][]any{
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: false,
},
sql.NullInt64{
Valid: true,
Int64: 4,
},
},
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: false,
},
int64(4),
},
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: false,
},
sql.NullInt64{
Valid: false,
},
},
},
index: []int{0, 1, 2},
wantVal: sql.NullFloat64{
Valid: false,
},
},
{
name: "count所有列都为nil",
input: [][]any{
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: true,
Int64: 4,
},
sql.NullInt64{
Valid: false,
},
},
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: true,
Int64: 4,
},
sql.NullInt64{
Valid: false,
},
},
{
sql.NullFloat64{
Valid: false,
},
sql.NullInt64{
Valid: false,
},
sql.NullInt64{
Valid: false,
},
},
},
index: []int{0, 1, 2},
wantVal: sql.NullFloat64{
Valid: false,
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
20 changes: 13 additions & 7 deletions internal/merger/internal/aggregatemerger/aggregator/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,11 @@ func (s *Count) Aggregate(cols [][]any) (any, error) {
return countFunc(cols, s.countInfo.Index)
}
func (s *Count) findCountFunc(col []any) (func([][]any, int) (any, error), error) {
var kind reflect.Kind
countIndex := s.countInfo.Index
if countIndex < 0 || countIndex >= len(col) {
return nil, errs.ErrMergerInvalidAggregateColumnIndex
}
kind = reflect.TypeOf(col[countIndex]).Kind()
countFunc, ok := countAggregateFuncMapping[kind]
if !ok {
return nil, errs.ErrMergerAggregateFuncNotFound
}
return countFunc, nil
return s.countNullableAggregator, nil
}

func (s *Count) ColumnInfo() merger.ColumnInfo {
Expand All @@ -70,6 +64,18 @@ func countAggregate[T AggregateElement](cols [][]any, countIndex int) (any, erro
}
return count, nil
}
func (*Count) countNullableAggregator(colsData [][]any, countIndex int) (any, error) {
notNullCols, kind := nullableAggregator(colsData, countIndex)
// 说明几个数据库里查出来的数据都为null,返回第一个null值即可
if len(notNullCols) == 0 {
return colsData[0][countIndex], nil
}
countFunc, ok := countAggregateFuncMapping[kind]
if !ok {
return nil, errs.ErrMergerAggregateFuncNotFound
}
return countFunc(notNullCols, countIndex)
}

var countAggregateFuncMapping = map[reflect.Kind]func([][]any, int) (any, error){
reflect.Int: countAggregate[int],
Expand Down
Loading
Loading