-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance advance aggregate query (#350)
- Loading branch information
Showing
34 changed files
with
1,604 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dataset | ||
|
||
import ( | ||
"database/sql" | ||
"io" | ||
) | ||
|
||
import ( | ||
gxbig "github.com/dubbogo/gost/math/big" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
import ( | ||
"github.com/arana-db/arana/pkg/mysql/rows" | ||
"github.com/arana-db/arana/pkg/proto" | ||
"github.com/arana-db/arana/pkg/reduce" | ||
) | ||
|
||
type ReduceDataset struct { | ||
proto.Dataset | ||
Reducers map[int]reduce.Reducer // field_index -> aggregator | ||
prev []proto.Value | ||
binary bool | ||
eof bool | ||
} | ||
|
||
func (ad *ReduceDataset) Next() (proto.Row, error) { | ||
if ad.eof { | ||
return nil, io.EOF | ||
} | ||
|
||
nextRow, err := ad.Dataset.Next() | ||
if errors.Is(err, io.EOF) { | ||
if ad.prev == nil { | ||
return nil, io.EOF | ||
} | ||
|
||
ad.eof = true | ||
fields, _ := ad.Dataset.Fields() | ||
if ad.binary { | ||
return rows.NewBinaryVirtualRow(fields, ad.prev), nil | ||
} else { | ||
return rows.NewTextVirtualRow(fields, ad.prev), nil | ||
} | ||
} | ||
|
||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
fields, _ := ad.Fields() | ||
values := make([]proto.Value, len(fields)) | ||
if err = nextRow.Scan(values); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
if ad.prev == nil { | ||
ad.prev = values | ||
ad.binary = nextRow.IsBinary() | ||
return ad.Next() | ||
} | ||
|
||
var ( | ||
prevValue, nextValue *gxbig.Decimal | ||
result *gxbig.Decimal | ||
) | ||
for i := range values { | ||
red, ok := ad.Reducers[i] | ||
if !ok { | ||
continue | ||
} | ||
var ( | ||
prev = ad.prev[i] | ||
next = values[i] | ||
) | ||
|
||
if next == nil { | ||
continue | ||
} | ||
if prev == nil { | ||
ad.prev[i] = next | ||
continue | ||
} | ||
|
||
if prevValue, err = toValue(fields[i], prev); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
if nextValue, err = toValue(fields[i], next); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
if result, err = red.Decimal(prevValue, nextValue); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
ad.prev[i] = result | ||
} | ||
|
||
return ad.Next() | ||
} | ||
|
||
func toValue(field proto.Field, input interface{}) (*gxbig.Decimal, error) { | ||
switch v := input.(type) { | ||
case *gxbig.Decimal: | ||
return v, nil | ||
case string: | ||
return gxbig.NewDecFromString(v) | ||
case int64: | ||
return gxbig.NewDecFromInt(v), nil | ||
} | ||
|
||
var s sql.NullString | ||
if err := s.Scan(input); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return gxbig.NewDecFromString(s.String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package reduce | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
import ( | ||
gxbig "github.com/dubbogo/gost/math/big" | ||
) | ||
|
||
var _ Reducer = (*maxReducer)(nil) | ||
|
||
type maxReducer struct{} | ||
|
||
func (m maxReducer) Int64(prev, next int64) (int64, error) { | ||
if next > prev { | ||
return next, nil | ||
} | ||
return prev, nil | ||
} | ||
|
||
func (m maxReducer) Float64(prev, next float64) (float64, error) { | ||
if next > prev { | ||
return next, nil | ||
} | ||
return prev, nil | ||
} | ||
|
||
func (m maxReducer) Decimal(prev, next *gxbig.Decimal) (*gxbig.Decimal, error) { | ||
switch next.Compare(prev) { | ||
case 1: | ||
return next, nil | ||
default: | ||
return prev, nil | ||
} | ||
} | ||
|
||
func (m maxReducer) Time(prev, next time.Time) (time.Time, error) { | ||
if next.After(prev) { | ||
return next, nil | ||
} | ||
return prev, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package reduce | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
import ( | ||
gxbig "github.com/dubbogo/gost/math/big" | ||
) | ||
|
||
type minReducer struct{} | ||
|
||
func (minReducer) Int64(prev, next int64) (int64, error) { | ||
if next < prev { | ||
return next, nil | ||
} | ||
return prev, nil | ||
} | ||
|
||
func (minReducer) Float64(prev, next float64) (float64, error) { | ||
if next < prev { | ||
return next, nil | ||
} | ||
return prev, nil | ||
} | ||
|
||
func (minReducer) Decimal(prev, next *gxbig.Decimal) (*gxbig.Decimal, error) { | ||
switch next.Compare(prev) { | ||
case -1: | ||
return next, nil | ||
default: | ||
return prev, nil | ||
} | ||
} | ||
|
||
func (minReducer) Time(prev, next time.Time) (time.Time, error) { | ||
if next.Before(prev) { | ||
return next, nil | ||
} | ||
return prev, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package reduce | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
import ( | ||
gxbig "github.com/dubbogo/gost/math/big" | ||
) | ||
|
||
type Reducer interface { | ||
Int64(prev, next int64) (int64, error) | ||
Float64(prev, next float64) (float64, error) | ||
Decimal(prev, next *gxbig.Decimal) (*gxbig.Decimal, error) | ||
Time(prev, next time.Time) (time.Time, error) | ||
} | ||
|
||
func Max() Reducer { | ||
return maxReducer{} | ||
} | ||
|
||
func Min() Reducer { | ||
return minReducer{} | ||
} | ||
|
||
func Sum() Reducer { | ||
return sumReducer{} | ||
} |
Oops, something went wrong.