-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 #9118 from planetscale/NormalizeHexValuesInQueries
Ensure that hex query predicates are normalized for planner cache
- Loading branch information
Showing
13 changed files
with
303 additions
and
20 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,92 @@ | ||
/* | ||
Copyright 2021 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 normalize | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"testing" | ||
|
||
"vitess.io/vitess/go/mysql" | ||
"vitess.io/vitess/go/test/endtoend/cluster" | ||
) | ||
|
||
var ( | ||
clusterInstance *cluster.LocalProcessCluster | ||
vtParams mysql.ConnParams | ||
KeyspaceName = "ks_normalize" | ||
Cell = "test_normalize" | ||
SchemaSQL = ` | ||
create table t1( | ||
id bigint unsigned not null, | ||
charcol char(10), | ||
vcharcol varchar(50), | ||
bincol binary(50), | ||
varbincol varbinary(50), | ||
floatcol float, | ||
deccol decimal(5,2), | ||
bitcol bit, | ||
datecol date, | ||
enumcol enum('small', 'medium', 'large'), | ||
setcol set('a', 'b', 'c'), | ||
jsoncol json, | ||
geocol geometry, | ||
primary key(id) | ||
) Engine=InnoDB; | ||
` | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
defer cluster.PanicHandler(nil) | ||
flag.Parse() | ||
|
||
exitCode := func() int { | ||
clusterInstance = cluster.NewCluster(Cell, "localhost") | ||
defer clusterInstance.Teardown() | ||
|
||
// Start topo server | ||
err := clusterInstance.StartTopo() | ||
if err != nil { | ||
return 1 | ||
} | ||
|
||
// Start keyspace | ||
keyspace := &cluster.Keyspace{ | ||
Name: KeyspaceName, | ||
SchemaSQL: SchemaSQL, | ||
} | ||
clusterInstance.VtGateExtraArgs = []string{} | ||
clusterInstance.VtTabletExtraArgs = []string{} | ||
err = clusterInstance.StartKeyspace(*keyspace, []string{"-"}, 1, false) | ||
if err != nil { | ||
return 1 | ||
} | ||
|
||
// Start vtgate | ||
err = clusterInstance.StartVtgate() | ||
if err != nil { | ||
return 1 | ||
} | ||
|
||
vtParams = mysql.ConnParams{ | ||
Host: clusterInstance.Hostname, | ||
Port: clusterInstance.VtgateMySQLPort, | ||
} | ||
return m.Run() | ||
}() | ||
os.Exit(exitCode) | ||
} |
82 changes: 82 additions & 0 deletions
82
go/test/endtoend/vtgate/queries/normalize/normalize_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,82 @@ | ||
/* | ||
Copyright 2021 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 normalize | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/mysql" | ||
"vitess.io/vitess/go/test/endtoend/vtgate/utils" | ||
) | ||
|
||
func TestNormalizeAllFields(t *testing.T) { | ||
conn, err := mysql.Connect(context.Background(), &vtParams) | ||
require.NoError(t, err) | ||
defer conn.Close() | ||
|
||
insertQuery := string(`insert into t1 values (1, "chars", "variable chars", x'73757265', 0x676F, 0.33, 9.99, 1, "1976-06-08", "small", "b", "{\"key\":\"value\"}", point(1,5))`) | ||
normalizedInsertQuery := string(`insert into t1 values (:vtg1, :vtg2, :vtg3, :vtg4, :vtg5, :vtg6, :vtg7, :vtg8, :vtg9, :vtg10, :vtg11, :vtg12, point(:vtg13, :vtg14))`) | ||
selectQuery := "select * from t1" | ||
utils.Exec(t, conn, insertQuery) | ||
qr := utils.Exec(t, conn, selectQuery) | ||
assert.Equal(t, 1, len(qr.Rows), "wrong number of table rows, expected 1 but had %d. Results: %v", len(qr.Rows), qr.Rows) | ||
|
||
// Now need to figure out the best way to check the normalized query in the planner cache... | ||
results, err := getPlanCache(fmt.Sprintf("%s:%d", clusterInstance.Hostname, clusterInstance.VtgateProcess.Port)) | ||
require.Nil(t, err) | ||
found := false | ||
for _, record := range results { | ||
key := record["Key"].(string) | ||
if key == normalizedInsertQuery { | ||
found = true | ||
break | ||
} | ||
} | ||
assert.True(t, found, "correctly normalized record not found in planner cache") | ||
} | ||
|
||
func getPlanCache(vtgateHostPort string) ([]map[string]interface{}, error) { | ||
var results []map[string]interface{} | ||
client := http.Client{ | ||
Timeout: 10 * time.Second, | ||
} | ||
resp, err := client.Get(fmt.Sprintf("http://%s/debug/query_plans", vtgateHostPort)) | ||
if err != nil { | ||
return results, err | ||
} | ||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return results, err | ||
} | ||
|
||
err = json.Unmarshal(body, &results) | ||
if err != nil { | ||
return results, err | ||
} | ||
|
||
return results, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.