148 lines
4.2 KiB
Go
148 lines
4.2 KiB
Go
// Copyright 2022 PingCAP, Inc.
|
|
//
|
|
// 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 core
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/pingcap/tidb/parser"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestParameterize(t *testing.T) {
|
|
sctx := MockContext()
|
|
cases := []struct {
|
|
sql string
|
|
paramSQL string
|
|
params []interface{}
|
|
}{
|
|
{
|
|
"select * from t where a<10",
|
|
"SELECT * FROM `t` WHERE `a`<?",
|
|
[]interface{}{int64(10)},
|
|
},
|
|
{
|
|
"select * from t",
|
|
"SELECT * FROM `t`",
|
|
[]interface{}{},
|
|
},
|
|
{
|
|
"select * from t where a<10 and b<20 and c=30 and d>40",
|
|
"SELECT * FROM `t` WHERE `a`<? AND `b`<? AND `c`=? AND `d`>?",
|
|
[]interface{}{int64(10), int64(20), int64(30), int64(40)},
|
|
},
|
|
{
|
|
"select * from t where a='a' and b='bbbbbbbbbbbbbbbbbbbbbbbb'",
|
|
"SELECT * FROM `t` WHERE `a`=? AND `b`=?",
|
|
[]interface{}{"a", "bbbbbbbbbbbbbbbbbbbbbbbb"},
|
|
},
|
|
{
|
|
"select 1, 2, 3 from t where a<10",
|
|
"SELECT 1,2,3 FROM `t` WHERE `a`<?",
|
|
[]interface{}{int64(10)},
|
|
},
|
|
{
|
|
"select a+1 from t where a<10",
|
|
"SELECT a+1 FROM `t` WHERE `a`<?",
|
|
[]interface{}{int64(10)},
|
|
},
|
|
{
|
|
`select a+ "a b c" from t`,
|
|
"SELECT a+ \"a b c\" FROM `t`",
|
|
[]interface{}{},
|
|
},
|
|
{
|
|
`select a + 'a b c'+"x" from t`,
|
|
"SELECT a + 'a b c'+\"x\" FROM `t`", // keep the original format for select fields
|
|
[]interface{}{},
|
|
},
|
|
{
|
|
`select a + 'a b c'+"x" as 'xxx' from t`,
|
|
"SELECT a + 'a b c'+\"x\" as 'xxx' FROM `t`", // keep the original format for select fields
|
|
[]interface{}{},
|
|
},
|
|
{
|
|
`insert into t (a, B, c) values (1, 2, 3), (4, 5, 6)`,
|
|
"INSERT INTO `t` (`a`,`B`,`c`) VALUES (?,?,?),(?,?,?)",
|
|
[]interface{}{int64(1), int64(2), int64(3), int64(4), int64(5), int64(6)},
|
|
},
|
|
{
|
|
`select * from t where a < date_format('2020-02-02', '%Y-%m-%d')`,
|
|
"SELECT * FROM `t` WHERE `a`<date_format(?, '%Y-%m-%d')",
|
|
[]interface{}{"2020-02-02"},
|
|
},
|
|
{
|
|
"select * from `txu#p#p1`",
|
|
"SELECT * FROM `txu#p#p1`",
|
|
[]interface{}{},
|
|
},
|
|
|
|
// keep the original format for limit clauses
|
|
{
|
|
`select * from t limit 10`,
|
|
"SELECT * FROM `t` LIMIT 10",
|
|
[]interface{}{},
|
|
},
|
|
{
|
|
`select * from t limit 10, 20`,
|
|
"SELECT * FROM `t` LIMIT 10,20",
|
|
[]interface{}{},
|
|
},
|
|
// TODO: more test cases
|
|
}
|
|
|
|
for _, c := range cases {
|
|
stmt, err := parser.New().ParseOneStmt(c.sql, "", "")
|
|
require.Nil(t, err)
|
|
paramSQL, params, err := ParameterizeAST(context.Background(), sctx, stmt)
|
|
require.Nil(t, err)
|
|
require.Equal(t, c.paramSQL, paramSQL)
|
|
require.Equal(t, len(c.params), len(params))
|
|
for i := range params {
|
|
require.Equal(t, c.params[i], params[i].Datum.GetValue())
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkParameterizeSelect(b *testing.B) {
|
|
paymentSelectCustomerForUpdate := `SELECT c_first, c_middle, c_last, c_street_1, c_street_2, c_city, c_state, c_zip, c_phone,
|
|
c_credit, c_credit_lim, c_discount, c_balance, c_since FROM customer WHERE c_w_id = ? AND c_d_id = ?AND c_id = ? FOR UPDATE`
|
|
stmt, err := parser.New().ParseOneStmt(paymentSelectCustomerForUpdate, "", "")
|
|
require.Nil(b, err)
|
|
sctx := MockContext()
|
|
_, _, err = ParameterizeAST(context.Background(), sctx, stmt)
|
|
require.Nil(b, err)
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
ParameterizeAST(context.Background(), sctx, stmt)
|
|
}
|
|
}
|
|
|
|
func BenchmarkParameterizeInsert(b *testing.B) {
|
|
paymentInsertHistory := `INSERT INTO history (h_c_d_id, h_c_w_id, h_c_id, h_d_id, h_w_id, h_date, h_amount, h_data) VALUES (1, 2, 3, 4, 5, 6, 7, 8)`
|
|
stmt, err := parser.New().ParseOneStmt(paymentInsertHistory, "", "")
|
|
require.Nil(b, err)
|
|
sctx := MockContext()
|
|
_, _, err = ParameterizeAST(context.Background(), sctx, stmt)
|
|
require.Nil(b, err)
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
ParameterizeAST(context.Background(), sctx, stmt)
|
|
}
|
|
}
|