Skip to content

Commit a8019fb

Browse files
committed
handle the case the type is set for SEND_LONG_DATA param
Signed-off-by: Yang Keao <[email protected]>
1 parent ecb8a9e commit a8019fb

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

pkg/server/conn_stmt_params.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,24 @@ func parseBinaryParams(params []param.BinaryParam, boundParams [][]byte, nullBit
4040
if boundParams[i] != nil {
4141
params[i] = param.BinaryParam{
4242
Tp: mysql.TypeBlob,
43-
Val: enc.DecodeInput(boundParams[i]),
43+
Val: boundParams[i],
44+
}
45+
46+
// The legacy logic is kept: if the `paramTypes` somehow didn't contain the type information, it will be treated as
47+
// BLOB type. We didn't return `mysql.ErrMalformPacket` to keep compatibility with older versions, though it's
48+
// meaningless if every clients work properly.
49+
if (i<<1)+1 < len(paramTypes) {
50+
// Only TEXT or BLOB type will be sent through `SEND_LONG_DATA`.
51+
tp := paramTypes[i<<1]
52+
53+
switch tp {
54+
case mysql.TypeVarchar, mysql.TypeVarString, mysql.TypeString, mysql.TypeBit:
55+
params[i].Tp = tp
56+
params[i].Val = enc.DecodeInput(boundParams[i])
57+
case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
58+
params[i].Tp = tp
59+
params[i].Val = boundParams[i]
60+
}
4461
}
4562
continue
4663
}

pkg/server/internal/testserverclient/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ go_library(
2020
"@com_github_pingcap_log//:log",
2121
"@com_github_prometheus_client_model//go",
2222
"@com_github_stretchr_testify//require",
23+
"@org_golang_x_text//encoding/simplifiedchinese",
2324
"@org_uber_go_zap//:zap",
2425
],
2526
)

pkg/server/internal/testserverclient/server_client.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
dto "github.com/prometheus/client_model/go"
4949
"github.com/stretchr/testify/require"
5050
"go.uber.org/zap"
51+
"golang.org/x/text/encoding/simplifiedchinese"
5152
)
5253

5354
//revive:disable:exported
@@ -2727,4 +2728,64 @@ func (cli *TestServerClient) RunTestConnectionCount(t *testing.T) {
27272728
})
27282729
}
27292730

2731+
func (cli *TestServerClient) RunTestTypeOfSendLongData(t *testing.T) {
2732+
cli.RunTests(t, func(config *mysql.Config) {
2733+
config.MaxAllowedPacket = 1024
2734+
}, func(dbt *testkit.DBTestKit) {
2735+
ctx := context.Background()
2736+
2737+
conn, err := dbt.GetDB().Conn(ctx)
2738+
require.NoError(t, err)
2739+
_, err = conn.ExecContext(ctx, "CREATE TABLE t (j JSON);")
2740+
require.NoError(t, err)
2741+
2742+
str := `"` + strings.Repeat("a", 1024) + `"`
2743+
stmt, err := conn.PrepareContext(ctx, "INSERT INTO t VALUES (cast(? as JSON));")
2744+
require.NoError(t, err)
2745+
_, err = stmt.ExecContext(ctx, str)
2746+
require.NoError(t, err)
2747+
result, err := conn.QueryContext(ctx, "SELECT j FROM t;")
2748+
require.NoError(t, err)
2749+
2750+
for result.Next() {
2751+
var j string
2752+
require.NoError(t, result.Scan(&j))
2753+
require.Equal(t, str, j)
2754+
}
2755+
})
2756+
}
2757+
2758+
func (cli *TestServerClient) RunTestCharsetOfSendLongData(t *testing.T) {
2759+
str := strings.Repeat("你好", 1024)
2760+
enc := simplifiedchinese.GBK.NewEncoder()
2761+
gbkStr, err := enc.String(str)
2762+
require.NoError(t, err)
2763+
2764+
cli.RunTests(t, func(config *mysql.Config) {
2765+
config.MaxAllowedPacket = 1024
2766+
config.Params["charset"] = "gbk"
2767+
}, func(dbt *testkit.DBTestKit) {
2768+
ctx := context.Background()
2769+
2770+
conn, err := dbt.GetDB().Conn(ctx)
2771+
require.NoError(t, err)
2772+
_, err = conn.ExecContext(ctx, "CREATE TABLE t (t TEXT);")
2773+
require.NoError(t, err)
2774+
2775+
stmt, err := conn.PrepareContext(ctx, "INSERT INTO t VALUES (?);")
2776+
require.NoError(t, err)
2777+
_, err = stmt.ExecContext(ctx, gbkStr)
2778+
require.NoError(t, err)
2779+
2780+
result, err := conn.QueryContext(ctx, "SELECT * FROM t;")
2781+
require.NoError(t, err)
2782+
2783+
for result.Next() {
2784+
var txt string
2785+
require.NoError(t, result.Scan(&txt))
2786+
require.Equal(t, gbkStr, txt)
2787+
}
2788+
})
2789+
}
2790+
27302791
//revive:enable:exported

pkg/server/tests/commontest/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ go_test(
88
"tidb_test.go",
99
],
1010
flaky = True,
11-
shard_count = 49,
11+
shard_count = 50,
1212
deps = [
1313
"//pkg/config",
1414
"//pkg/ddl/util",

pkg/server/tests/commontest/tidb_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3082,3 +3082,13 @@ func TestConnectionCount(t *testing.T) {
30823082
ts := servertestkit.CreateTidbTestSuite(t)
30833083
ts.RunTestConnectionCount(t)
30843084
}
3085+
3086+
func TestTypeOfSendLongData(t *testing.T) {
3087+
ts := servertestkit.CreateTidbTestSuite(t)
3088+
ts.RunTestTypeOfSendLongData(t)
3089+
}
3090+
3091+
func TestCharsetOfSendLongData(t *testing.T) {
3092+
ts := servertestkit.CreateTidbTestSuite(t)
3093+
ts.RunTestCharsetOfSendLongData(t)
3094+
}

0 commit comments

Comments
 (0)