Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6680 from planetscale/sql-cal-found-rows
Support sql_calc_found_rows with limit in sharded keyspace
- Loading branch information
Showing
17 changed files
with
621 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2020 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 vtgate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/mysql" | ||
"vitess.io/vitess/go/test/endtoend/cluster" | ||
) | ||
|
||
func TestFoundRows(t *testing.T) { | ||
defer cluster.PanicHandler(t) | ||
ctx := context.Background() | ||
conn, err := mysql.Connect(ctx, &vtParams) | ||
require.Nil(t, err) | ||
defer conn.Close() | ||
|
||
exec(t, conn, "insert into t2(id3,id4) values(1,2), (2,2), (3,3), (4,3), (5,3)") | ||
|
||
runTests := func(workload string) { | ||
assertFoundRowsValue(t, conn, "select * from t2", workload, 5) | ||
assertFoundRowsValue(t, conn, "select * from t2 limit 2", workload, 2) | ||
assertFoundRowsValue(t, conn, "select SQL_CALC_FOUND_ROWS * from t2 limit 2", workload, 5) | ||
assertFoundRowsValue(t, conn, "select SQL_CALC_FOUND_ROWS * from t2 where id3 = 4 limit 2", workload, 1) | ||
assertFoundRowsValue(t, conn, "select SQL_CALC_FOUND_ROWS * from t2 where id4 = 3 limit 2", workload, 3) | ||
assertFoundRowsValue(t, conn, "select SQL_CALC_FOUND_ROWS id4, count(id3) from t2 where id3 = 3 group by id4 limit 1", workload, 1) | ||
} | ||
|
||
runTests("oltp") | ||
exec(t, conn, "set workload = olap") | ||
runTests("olap") | ||
|
||
// cleanup test data | ||
exec(t, conn, "set workload = oltp") | ||
exec(t, conn, "delete from t2") | ||
exec(t, conn, "delete from t2_id4_idx") | ||
} | ||
|
||
func assertFoundRowsValue(t *testing.T, conn *mysql.Conn, query, workload string, count int) { | ||
exec(t, conn, query) | ||
qr := exec(t, conn, "select found_rows()") | ||
got := fmt.Sprintf("%v", qr.Rows) | ||
want := fmt.Sprintf(`[[UINT64(%d)]]`, count) | ||
assert.Equalf(t, want, got, "Workload: %s\nQuery:%s\n", workload, query) | ||
} |
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 2020 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 ( | ||
"vitess.io/vitess/go/sqltypes" | ||
querypb "vitess.io/vitess/go/vt/proto/query" | ||
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" | ||
"vitess.io/vitess/go/vt/vterrors" | ||
"vitess.io/vitess/go/vt/vtgate/evalengine" | ||
) | ||
|
||
var _ Primitive = (*SQLCalcFoundRows)(nil) | ||
|
||
//SQLCalcFoundRows is a primitive to execute limit and count query as per their individual plan. | ||
type SQLCalcFoundRows struct { | ||
LimitPrimitive Primitive | ||
CountPrimitive Primitive | ||
} | ||
|
||
//RouteType implements the Primitive interface | ||
func (s SQLCalcFoundRows) RouteType() string { | ||
return "SQLCalcFoundRows" | ||
} | ||
|
||
//GetKeyspaceName implements the Primitive interface | ||
func (s SQLCalcFoundRows) GetKeyspaceName() string { | ||
return s.LimitPrimitive.GetKeyspaceName() | ||
} | ||
|
||
//GetTableName implements the Primitive interface | ||
func (s SQLCalcFoundRows) GetTableName() string { | ||
return s.LimitPrimitive.GetTableName() | ||
} | ||
|
||
//Execute implements the Primitive interface | ||
func (s SQLCalcFoundRows) Execute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) { | ||
limitQr, err := s.LimitPrimitive.Execute(vcursor, bindVars, wantfields) | ||
if err != nil { | ||
return nil, err | ||
} | ||
countQr, err := s.CountPrimitive.Execute(vcursor, bindVars, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(countQr.Rows) != 1 || len(countQr.Rows[0]) != 1 { | ||
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "count query is not a scalar") | ||
} | ||
fr, err := evalengine.ToUint64(countQr.Rows[0][0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
vcursor.Session().SetFoundRows(fr) | ||
return limitQr, nil | ||
} | ||
|
||
//StreamExecute implements the Primitive interface | ||
func (s SQLCalcFoundRows) StreamExecute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error { | ||
err := s.LimitPrimitive.StreamExecute(vcursor, bindVars, wantfields, callback) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var fr *uint64 | ||
|
||
err = s.CountPrimitive.StreamExecute(vcursor, bindVars, wantfields, func(countQr *sqltypes.Result) error { | ||
if len(countQr.Rows) == 0 && countQr.Fields != nil { | ||
// this is the fields, which we can ignore | ||
return nil | ||
} | ||
if len(countQr.Rows) != 1 || len(countQr.Rows[0]) != 1 { | ||
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "count query is not a scalar") | ||
} | ||
toUint64, err := evalengine.ToUint64(countQr.Rows[0][0]) | ||
if err != nil { | ||
return err | ||
} | ||
fr = &toUint64 | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
if fr == nil { | ||
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "count query for SQL_CALC_FOUND_ROWS never returned a value") | ||
} | ||
vcursor.Session().SetFoundRows(*fr) | ||
return nil | ||
} | ||
|
||
//GetFields implements the Primitive interface | ||
func (s SQLCalcFoundRows) GetFields(vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) { | ||
return s.LimitPrimitive.GetFields(vcursor, bindVars) | ||
} | ||
|
||
//NeedsTransaction implements the Primitive interface | ||
func (s SQLCalcFoundRows) NeedsTransaction() bool { | ||
return s.LimitPrimitive.NeedsTransaction() | ||
} | ||
|
||
//Inputs implements the Primitive interface | ||
func (s SQLCalcFoundRows) Inputs() []Primitive { | ||
return []Primitive{s.LimitPrimitive, s.CountPrimitive} | ||
} | ||
|
||
func (s SQLCalcFoundRows) description() PrimitiveDescription { | ||
return PrimitiveDescription{ | ||
OperatorType: "SQL_CALC_FOUND_ROWS", | ||
} | ||
} |
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
Oops, something went wrong.