-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Handle DISTINCT with the new operators #13201
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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
53 changes: 53 additions & 0 deletions
53
go/test/endtoend/vtgate/queries/aggregation/distinct_test.go
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,53 @@ | ||
/* | ||
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 aggregation | ||
|
||
import ( | ||
"testing" | ||
|
||
"vitess.io/vitess/go/test/endtoend/utils" | ||
) | ||
|
||
func TestDistinct(t *testing.T) { | ||
mcmp, closer := start(t) | ||
defer closer() | ||
mcmp.Exec("insert into t3(id5,id6,id7) values(1,3,3), (2,3,4), (3,3,6), (4,5,7), (5,5,6)") | ||
mcmp.Exec("insert into t7_xxhash(uid,phone) values('1',4), ('2',4), ('3',3), ('4',1), ('5',1)") | ||
mcmp.Exec("insert into aggr_test(id, val1, val2) values(1,'a',1), (2,'A',1), (3,'b',1), (4,'c',3), (5,'c',4)") | ||
mcmp.Exec("insert into aggr_test(id, val1, val2) values(6,'d',null), (7,'e',null), (8,'E',1)") | ||
mcmp.AssertMatches("select distinct val2, count(*) from aggr_test group by val2", `[[NULL INT64(2)] [INT64(1) INT64(4)] [INT64(3) INT64(1)] [INT64(4) INT64(1)]]`) | ||
mcmp.AssertMatches("select distinct id6 from t3 join t7_xxhash on t3.id5 = t7_xxhash.phone", `[[INT64(3)] [INT64(5)]]`) | ||
} | ||
|
||
func TestDistinctIt(t *testing.T) { | ||
// tests more variations of DISTINCT | ||
mcmp, closer := start(t) | ||
defer closer() | ||
|
||
mcmp.Exec("insert into aggr_test(id, val1, val2) values(1,'a',1), (2,'A',1), (3,'b',1), (4,'c',3), (5,'c',4)") | ||
mcmp.Exec("insert into aggr_test(id, val1, val2) values(6,'d',null), (7,'e',null), (8,'E',1)") | ||
|
||
mcmp.AssertMatchesNoOrder("select distinct val1 from aggr_test", `[[VARCHAR("c")] [VARCHAR("d")] [VARCHAR("e")] [VARCHAR("a")] [VARCHAR("b")]]`) | ||
mcmp.AssertMatchesNoOrder("select distinct val2 from aggr_test", `[[INT64(1)] [INT64(4)] [INT64(3)] [NULL]]`) | ||
mcmp.AssertMatchesNoOrder("select distinct id from aggr_test", `[[INT64(1)] [INT64(2)] [INT64(3)] [INT64(5)] [INT64(4)] [INT64(6)] [INT64(7)] [INT64(8)]]`) | ||
|
||
if utils.BinaryIsAtVersion(17, "vtgate") { | ||
mcmp.AssertMatches("select /*vt+ PLANNER=Gen4 */ distinct val1 from aggr_test order by val1 desc", `[[VARCHAR("e")] [VARCHAR("d")] [VARCHAR("c")] [VARCHAR("b")] [VARCHAR("a")]]`) | ||
mcmp.AssertMatchesNoOrder("select /*vt+ PLANNER=Gen4 */ distinct val1, count(*) from aggr_test group by val1", `[[VARCHAR("a") INT64(2)] [VARCHAR("b") INT64(1)] [VARCHAR("c") INT64(2)] [VARCHAR("d") INT64(1)] [VARCHAR("e") INT64(2)]]`) | ||
mcmp.AssertMatchesNoOrder("select /*vt+ PLANNER=Gen4 */ distinct val1+val2 from aggr_test", `[[NULL] [FLOAT64(1)] [FLOAT64(3)] [FLOAT64(4)]]`) | ||
} | ||
} |
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,124 @@ | ||
/* | ||
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 operators | ||
|
||
import ( | ||
"golang.org/x/exp/slices" | ||
|
||
"vitess.io/vitess/go/vt/sqlparser" | ||
"vitess.io/vitess/go/vt/vtgate/engine" | ||
"vitess.io/vitess/go/vt/vtgate/planbuilder/operators/ops" | ||
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" | ||
) | ||
|
||
type ( | ||
Distinct struct { | ||
Source ops.Operator | ||
QP *QueryProjection | ||
Pushed bool | ||
|
||
// When offset planning, we'll fill in this field | ||
Columns []engine.CheckCol | ||
} | ||
) | ||
|
||
func (d *Distinct) planOffsets(ctx *plancontext.PlanningContext) error { | ||
columns, err := d.GetColumns() | ||
if err != nil { | ||
return err | ||
} | ||
d.Columns = nil | ||
var exprs []sqlparser.Expr | ||
for _, col := range columns { | ||
newSrc, offset, err := d.Source.AddColumn(ctx, col, true, false) | ||
if err != nil { | ||
return err | ||
} | ||
d.Source = newSrc | ||
e := d.QP.GetSimplifiedExpr(col.Expr) | ||
exprs = append(exprs, e) | ||
d.Columns = append(d.Columns, engine.CheckCol{ | ||
Col: offset, | ||
Collation: ctx.SemTable.CollationForExpr(e), | ||
}) | ||
} | ||
for i, e := range exprs { | ||
if !ctx.SemTable.NeedsWeightString(e) { | ||
continue | ||
} | ||
newSrc, offset, err := d.Source.AddColumn(ctx, aeWrap(weightStringFor(e)), true, false) | ||
if err != nil { | ||
return err | ||
} | ||
d.Source = newSrc | ||
d.Columns[i].WsCol = &offset | ||
} | ||
return nil | ||
} | ||
|
||
func (d *Distinct) Clone(inputs []ops.Operator) ops.Operator { | ||
return &Distinct{ | ||
Source: inputs[0], | ||
Columns: slices.Clone(d.Columns), | ||
QP: d.QP, | ||
Pushed: d.Pushed, | ||
} | ||
} | ||
|
||
func (d *Distinct) Inputs() []ops.Operator { | ||
return []ops.Operator{d.Source} | ||
} | ||
|
||
func (d *Distinct) SetInputs(operators []ops.Operator) { | ||
d.Source = operators[0] | ||
} | ||
|
||
func (d *Distinct) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) { | ||
newSrc, err := d.Source.AddPredicate(ctx, expr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
d.Source = newSrc | ||
return d, nil | ||
} | ||
|
||
func (d *Distinct) AddColumn(ctx *plancontext.PlanningContext, expr *sqlparser.AliasedExpr, reuseExisting, addToGroupBy bool) (ops.Operator, int, error) { | ||
newSrc, offset, err := d.Source.AddColumn(ctx, expr, reuseExisting, addToGroupBy) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
d.Source = newSrc | ||
return d, offset, nil | ||
} | ||
|
||
func (d *Distinct) GetColumns() ([]*sqlparser.AliasedExpr, error) { | ||
return d.Source.GetColumns() | ||
} | ||
|
||
func (d *Distinct) Description() ops.OpDescription { | ||
return ops.OpDescription{ | ||
OperatorType: "Distinct", | ||
} | ||
} | ||
|
||
func (d *Distinct) ShortDescription() string { | ||
return "" | ||
} | ||
|
||
func (d *Distinct) GetOrdering() ([]ops.OrderBy, error) { | ||
return d.Source.GetOrdering() | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nite: need test comments