-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Aggregation engine refactor #13378
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
Merged
harshit-gangal
merged 13 commits into
vitessio:main
from
planetscale:aggr-type-refactor
Jun 29, 2023
Merged
Aggregation engine refactor #13378
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6e843f9
fields to always change type based on aggregation function. Remove ag…
systay 3d885ae
changed some tests to only run gen4 planner as v3 and gen4 field type…
harshit-gangal 6e4807b
remove preprocess from the orderedAggregate
systay 5fb4367
extract aggregation structs and logic into own file
systay 55d7bef
remove remaining uses of PreProcess
systay afd0264
imports
systay b8f0aaa
make sure we don't cause a race condition by changing the field type
systay 3758d96
codegen
systay b9bbfdd
make sure to send in the correct types to merge
systay 00e4b4c
change test to compare with mysql instead of using bad expectations
systay 9abd449
update test expectations
systay 836d262
Merge remote-tracking branch 'upstream/main' into aggr-type-refactor
systay 675cc1f
Merge remote-tracking branch 'upstream/main' into aggr-type-refactor
systay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,288 @@ | ||
| /* | ||
| Copyright 2023 The Vitess Authors. | ||
|
|
||
| Licensed 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 engine | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
|
|
||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "vitess.io/vitess/go/mysql/collations" | ||
| "vitess.io/vitess/go/slices2" | ||
| "vitess.io/vitess/go/sqltypes" | ||
| binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" | ||
| querypb "vitess.io/vitess/go/vt/proto/query" | ||
| "vitess.io/vitess/go/vt/sqlparser" | ||
| . "vitess.io/vitess/go/vt/vtgate/engine/opcode" | ||
| "vitess.io/vitess/go/vt/vtgate/evalengine" | ||
| ) | ||
|
|
||
| // AggregateParams specify the parameters for each aggregation. | ||
| // It contains the opcode and input column number. | ||
| type AggregateParams struct { | ||
| Opcode AggregateOpcode | ||
| Col int | ||
|
|
||
| // These are used only for distinct opcodes. | ||
| KeyCol int | ||
| WCol int | ||
| WAssigned bool | ||
| CollationID collations.ID | ||
|
|
||
| Alias string `json:",omitempty"` | ||
| Expr sqlparser.Expr | ||
| Original *sqlparser.AliasedExpr | ||
|
|
||
| // This is based on the function passed in the select expression and | ||
| // not what we use to aggregate at the engine primitive level. | ||
| OrigOpcode AggregateOpcode | ||
| } | ||
|
|
||
| func (ap *AggregateParams) isDistinct() bool { | ||
| return ap.Opcode == AggregateCountDistinct || ap.Opcode == AggregateSumDistinct | ||
| } | ||
|
|
||
| func (ap *AggregateParams) preProcess() bool { | ||
| switch ap.Opcode { | ||
| case AggregateCountDistinct, AggregateSumDistinct, AggregateGtid, AggregateCount, AggregateGroupConcat: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func (ap *AggregateParams) String() string { | ||
| keyCol := strconv.Itoa(ap.Col) | ||
| if ap.WAssigned { | ||
| keyCol = fmt.Sprintf("%s|%d", keyCol, ap.WCol) | ||
| } | ||
| if ap.CollationID != collations.Unknown { | ||
| keyCol += " COLLATE " + ap.CollationID.Get().Name() | ||
| } | ||
| dispOrigOp := "" | ||
| if ap.OrigOpcode != AggregateUnassigned && ap.OrigOpcode != ap.Opcode { | ||
| dispOrigOp = "_" + ap.OrigOpcode.String() | ||
| } | ||
| if ap.Alias != "" { | ||
| return fmt.Sprintf("%s%s(%s) AS %s", ap.Opcode.String(), dispOrigOp, keyCol, ap.Alias) | ||
| } | ||
| return fmt.Sprintf("%s%s(%s)", ap.Opcode.String(), dispOrigOp, keyCol) | ||
| } | ||
|
|
||
| func (ap *AggregateParams) typ(inputType querypb.Type) querypb.Type { | ||
| opCode := ap.Opcode | ||
| if ap.OrigOpcode != AggregateUnassigned { | ||
| opCode = ap.OrigOpcode | ||
| } | ||
| typ, _ := opCode.Type(&inputType) | ||
| return typ | ||
| } | ||
|
|
||
| func convertRow( | ||
| fields []*querypb.Field, | ||
| row []sqltypes.Value, | ||
| aggregates []*AggregateParams, | ||
| ) (newRow []sqltypes.Value, curDistincts []sqltypes.Value) { | ||
| newRow = append(newRow, row...) | ||
| curDistincts = make([]sqltypes.Value, len(aggregates)) | ||
| for index, aggr := range aggregates { | ||
| switch aggr.Opcode { | ||
| case AggregateCountStar: | ||
| newRow[aggr.Col] = countOne | ||
| case AggregateCount: | ||
| val := countOne | ||
| if row[aggr.Col].IsNull() { | ||
| val = countZero | ||
| } | ||
| newRow[aggr.Col] = val | ||
| case AggregateCountDistinct: | ||
| curDistincts[index] = findComparableCurrentDistinct(row, aggr) | ||
| // Type is int64. Ok to call MakeTrusted. | ||
| if row[aggr.KeyCol].IsNull() { | ||
| newRow[aggr.Col] = countZero | ||
| } else { | ||
| newRow[aggr.Col] = countOne | ||
| } | ||
| case AggregateSum: | ||
| if row[aggr.Col].IsNull() { | ||
| break | ||
| } | ||
| var err error | ||
| newRow[aggr.Col], err = evalengine.Cast(row[aggr.Col], fields[aggr.Col].Type) | ||
| if err != nil { | ||
| newRow[aggr.Col] = sumZero | ||
| } | ||
| case AggregateSumDistinct: | ||
| curDistincts[index] = findComparableCurrentDistinct(row, aggr) | ||
| var err error | ||
| newRow[aggr.Col], err = evalengine.Cast(row[aggr.Col], fields[aggr.Col].Type) | ||
| if err != nil { | ||
| newRow[aggr.Col] = sumZero | ||
| } | ||
| case AggregateGtid: | ||
| vgtid := &binlogdatapb.VGtid{} | ||
| vgtid.ShardGtids = append(vgtid.ShardGtids, &binlogdatapb.ShardGtid{ | ||
| Keyspace: row[aggr.Col-1].ToString(), | ||
| Shard: row[aggr.Col+1].ToString(), | ||
| Gtid: row[aggr.Col].ToString(), | ||
| }) | ||
| data, _ := vgtid.MarshalVT() | ||
| val, _ := sqltypes.NewValue(sqltypes.VarBinary, data) | ||
| newRow[aggr.Col] = val | ||
| case AggregateGroupConcat: | ||
| if !row[aggr.Col].IsNull() { | ||
| newRow[aggr.Col] = sqltypes.MakeTrusted(fields[aggr.Col].Type, []byte(row[aggr.Col].ToString())) | ||
| } | ||
| } | ||
| } | ||
| return newRow, curDistincts | ||
| } | ||
|
|
||
| func merge( | ||
| fields []*querypb.Field, | ||
| row1, row2 []sqltypes.Value, | ||
| curDistincts []sqltypes.Value, | ||
| aggregates []*AggregateParams, | ||
| ) ([]sqltypes.Value, []sqltypes.Value, error) { | ||
| result := sqltypes.CopyRow(row1) | ||
| for index, aggr := range aggregates { | ||
| if aggr.isDistinct() { | ||
| if row2[aggr.KeyCol].IsNull() { | ||
| continue | ||
| } | ||
| cmp, err := evalengine.NullsafeCompare(curDistincts[index], row2[aggr.KeyCol], aggr.CollationID) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| if cmp == 0 { | ||
| continue | ||
| } | ||
| curDistincts[index] = findComparableCurrentDistinct(row2, aggr) | ||
| } | ||
|
|
||
| var err error | ||
| switch aggr.Opcode { | ||
| case AggregateCountStar: | ||
| result[aggr.Col], err = evalengine.NullSafeAdd(row1[aggr.Col], countOne, fields[aggr.Col].Type) | ||
| case AggregateCount: | ||
| val := countOne | ||
| if row2[aggr.Col].IsNull() { | ||
| val = countZero | ||
| } | ||
| result[aggr.Col], err = evalengine.NullSafeAdd(row1[aggr.Col], val, fields[aggr.Col].Type) | ||
| case AggregateSum: | ||
| value := row1[aggr.Col] | ||
| v2 := row2[aggr.Col] | ||
| if value.IsNull() && v2.IsNull() { | ||
| result[aggr.Col] = sqltypes.NULL | ||
| break | ||
| } | ||
| result[aggr.Col], err = evalengine.NullSafeAdd(value, v2, fields[aggr.Col].Type) | ||
| case AggregateMin: | ||
| result[aggr.Col], err = evalengine.Min(row1[aggr.Col], row2[aggr.Col], aggr.CollationID) | ||
| case AggregateMax: | ||
| result[aggr.Col], err = evalengine.Max(row1[aggr.Col], row2[aggr.Col], aggr.CollationID) | ||
| case AggregateCountDistinct: | ||
| result[aggr.Col], err = evalengine.NullSafeAdd(row1[aggr.Col], countOne, fields[aggr.Col].Type) | ||
| case AggregateSumDistinct: | ||
| result[aggr.Col], err = evalengine.NullSafeAdd(row1[aggr.Col], row2[aggr.Col], fields[aggr.Col].Type) | ||
| case AggregateGtid: | ||
| vgtid := &binlogdatapb.VGtid{} | ||
| rowBytes, err := row1[aggr.Col].ToBytes() | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| err = vgtid.UnmarshalVT(rowBytes) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| vgtid.ShardGtids = append(vgtid.ShardGtids, &binlogdatapb.ShardGtid{ | ||
| Keyspace: row2[aggr.Col-1].ToString(), | ||
| Shard: row2[aggr.Col+1].ToString(), | ||
| Gtid: row2[aggr.Col].ToString(), | ||
| }) | ||
| data, _ := vgtid.MarshalVT() | ||
| val, _ := sqltypes.NewValue(sqltypes.VarBinary, data) | ||
| result[aggr.Col] = val | ||
| case AggregateAnyValue: | ||
| // we just grab the first value per grouping. no need to do anything more complicated here | ||
| case AggregateGroupConcat: | ||
| if row2[aggr.Col].IsNull() { | ||
| break | ||
| } | ||
| if result[aggr.Col].IsNull() { | ||
| result[aggr.Col] = sqltypes.MakeTrusted(fields[aggr.Col].Type, []byte(row2[aggr.Col].ToString())) | ||
| break | ||
| } | ||
| concat := row1[aggr.Col].ToString() + "," + row2[aggr.Col].ToString() | ||
| result[aggr.Col] = sqltypes.MakeTrusted(fields[aggr.Col].Type, []byte(concat)) | ||
| default: | ||
| return nil, nil, fmt.Errorf("BUG: Unexpected opcode: %v", aggr.Opcode) | ||
| } | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| } | ||
| return result, curDistincts, nil | ||
| } | ||
|
|
||
| func convertFinal(current []sqltypes.Value, aggregates []*AggregateParams) ([]sqltypes.Value, error) { | ||
| result := sqltypes.CopyRow(current) | ||
| for _, aggr := range aggregates { | ||
| switch aggr.Opcode { | ||
| case AggregateGtid: | ||
| vgtid := &binlogdatapb.VGtid{} | ||
| currentBytes, err := current[aggr.Col].ToBytes() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| err = vgtid.UnmarshalVT(currentBytes) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| result[aggr.Col] = sqltypes.NewVarChar(vgtid.String()) | ||
| } | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| func convertFields(fields []*querypb.Field, aggrs []*AggregateParams) []*querypb.Field { | ||
| fields = slices2.Map(fields, func(from *querypb.Field) *querypb.Field { | ||
| return proto.Clone(from).(*querypb.Field) | ||
| }) | ||
| for _, aggr := range aggrs { | ||
| fields[aggr.Col].Type = aggr.typ(fields[aggr.Col].Type) | ||
| if aggr.Alias != "" { | ||
| fields[aggr.Col].Name = aggr.Alias | ||
| } | ||
| if aggr.isDistinct() { | ||
| // TODO: this should move to plan time | ||
| aggr.KeyCol = aggr.Col | ||
| } | ||
| } | ||
| return fields | ||
| } | ||
|
|
||
| func findComparableCurrentDistinct(row []sqltypes.Value, aggr *AggregateParams) sqltypes.Value { | ||
| curDistinct := row[aggr.KeyCol] | ||
| if aggr.WAssigned && !curDistinct.IsComparable() { | ||
| aggr.KeyCol = aggr.WCol | ||
| curDistinct = row[aggr.KeyCol] | ||
| } | ||
| return curDistinct | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.