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 3 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
100 changes: 90 additions & 10 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 Down Expand Up @@ -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 sumEmptyVal, countEmptyVal any

for _, col := range cols {
sumEmptyVal = a.getEmptyVal(col, sumIndex)
if sumEmptyVal != nil {
break
}
}

for _, col := range cols {
countEmptyVal = a.getEmptyVal(col, countIndex)
if countEmptyVal != nil {
break
}
}

for _, col := range cols {
sumCol := col[sumIndex]
countCol := col[countIndex]
sumKind := reflect.TypeOf(sumCol).Kind()
countKind := reflect.TypeOf(countCol).Kind()
var sumVal, countVal any
if sumKind == reflect.Struct {
// sum列为sql null类型
sumVal, _ = col[sumIndex].(driver.Valuer).Value()
if sumVal == nil {
// 如果是nil用0表示
col[sumIndex] = sumEmptyVal
} else {
sumValKind = reflect.TypeOf(sumVal).Kind()
col[sumIndex] = sumVal
}
} else {
sumVal = col[sumIndex]
sumValKind = reflect.TypeOf(sumVal).Kind()
}
if countKind == reflect.Struct {
countVal, _ = col[countIndex].(driver.Valuer).Value()
if countVal == nil {
col[countIndex] = countEmptyVal
} else {
countValKind = reflect.TypeOf(countVal).Kind()
col[countIndex] = countVal
}
} else {
countVal = col[countIndex]
countValKind = reflect.TypeOf(countVal).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) getEmptyVal(cols []any, index int) any {
var emptyVal any
col := cols[index]
if reflect.TypeOf(col).Kind() == reflect.Struct {
colVal, _ := col.(driver.Valuer).Value()
if colVal != nil {
emptyVal = reflect.Zero(reflect.TypeOf(colVal)).Interface()
}
} else {
emptyVal = reflect.Zero(reflect.TypeOf(col)).Interface()
}
return emptyVal
}

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