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

refactor(merger): 重构AVG函数实现,重构所有rows.Rows实现的ConlumnType方法并添加测试 #223

Merged
merged 14 commits into from
Jun 11, 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
1 change: 1 addition & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- [script: 注释掉无用命令及代码、固定ci中golangci-lint的版本使其与setup.sh中版本保持一致](https://github.com/ecodeclub/eorm/pull/220)
- [doc: 修复README中不可用的贡献者指南链接](https://github.com/ecodeclub/eorm/pull/221)
- [feat(merger): 定义中立的特征表达数据、定义工厂方法根据特征数据来获取具体的merger](https://github.com/ecodeclub/eorm/pull/222)
- [refactor(merger): 重构AVG函数实现,重构所有rows.Rows实现的ConlumnType方法并添加测试](https://github.com/ecodeclub/eorm/pull/223)
## v0.0.1:
- [Init Project](https://github.com/ecodeclub/eorm/pull/1)
- [Selector Definition](https://github.com/ecodeclub/eorm/pull/2)
Expand Down
25 changes: 12 additions & 13 deletions internal/merger/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (q QuerySpec) validateGroupBy() error {
return fmt.Errorf("%w: groupby %v", ErrInvalidColumnInfo, c.Name)
}
// 清除ASC
c.ASC = false
c.Order = merger.DESC
if !slice.Contains(q.Select, c) {
return fmt.Errorf("%w: groupby %v", ErrColumnNotFoundInSelectList, c.Name)
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func (q QuerySpec) validateOrderBy() error {
return fmt.Errorf("%w: orderby %v", ErrInvalidColumnInfo, c.Name)
}
// 清除ASC
c.ASC = false
c.Order = merger.DESC
if !slice.Contains(q.Select, c) {
return fmt.Errorf("%w: orderby %v", ErrColumnNotFoundInSelectList, c.Name)
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func newAggregateMerger(origin, target QuerySpec) (merger.Merger, error) {
return aggregatemerger.NewMerger(aggregators...), nil
}

func getAggregators(origin QuerySpec, target QuerySpec) []aggregator.Aggregator {
func getAggregators(_, target QuerySpec) []aggregator.Aggregator {
var aggregators []aggregator.Aggregator
for i := 0; i < len(target.Select); i++ {
c := target.Select[i]
Expand All @@ -175,12 +175,11 @@ func getAggregators(origin QuerySpec, target QuerySpec) []aggregator.Aggregator
case "MAX":
aggregators = append(aggregators, aggregator.NewMax(c))
log.Printf("max index = %d\n", c.Index)
case "AVG":
aggregators = append(aggregators, aggregator.NewAVG(c, target.Select[i+1], target.Select[i+2]))
i += 2
log.Printf("avg index = %d\n", c.Index)
case "SUM":
if i < len(origin.Select) && strings.ToUpper(origin.Select[i].AggregateFunc) == "AVG" {
aggregators = append(aggregators, aggregator.NewAVG(c, target.Select[i+1], origin.Select[i].SelectName()))
i += 1
continue
}
aggregators = append(aggregators, aggregator.NewSum(c))
log.Printf("sum index = %d\n", c.Index)
case "COUNT":
Expand All @@ -202,22 +201,22 @@ func newOrderByMerger(origin, target QuerySpec) (merger.Merger, error) {
for i := 0; i < len(target.OrderBy); i++ {
c := target.OrderBy[i]
if i < len(origin.OrderBy) && strings.ToUpper(origin.OrderBy[i].AggregateFunc) == "AVG" {
s := sortmerger.NewSortColumn(origin.OrderBy[i].SelectName(), sortmerger.Order(origin.OrderBy[i].ASC))
s := sortmerger.NewSortColumn(origin.OrderBy[i].SelectName(), sortmerger.Order(origin.OrderBy[i].Order))
columns = append(columns, s)
i++
continue
}
s := sortmerger.NewSortColumn(c.SelectName(), sortmerger.Order(c.ASC))
s := sortmerger.NewSortColumn(c.SelectName(), sortmerger.Order(c.Order))
columns = append(columns, s)
}

var isScanAll bool
var isPreScanAll bool
if slice.Contains(target.Features, query.GroupBy) {
isScanAll = true
isPreScanAll = true
}

log.Printf("sortColumns = %#v\n", columns)
return sortmerger.NewMerger(isScanAll, columns...)
return sortmerger.NewMerger(isPreScanAll, columns...)
}

func New(origin, target QuerySpec) (merger.Merger, error) {
Expand Down
Loading
Loading