From 9d6e45b7b249f9ad88adf7db9d015b1245deaab4 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Thu, 11 Jun 2020 17:19:10 +0200 Subject: [PATCH] Do not rewrite the type description for CAST Signed-off-by: Andres Taylor --- go/test/endtoend/vtgate/misc_test.go | 11 ++++++++++- go/vt/sqlparser/normalizer.go | 5 +++++ go/vt/sqlparser/normalizer_test.go | 7 +++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/go/test/endtoend/vtgate/misc_test.go b/go/test/endtoend/vtgate/misc_test.go index 35ed83489a7..95a5a52f400 100644 --- a/go/test/endtoend/vtgate/misc_test.go +++ b/go/test/endtoend/vtgate/misc_test.go @@ -19,9 +19,10 @@ package vtgate import ( "context" "fmt" - "github.com/google/go-cmp/cmp" "testing" + "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -549,6 +550,14 @@ func TestShowColumns(t *testing.T) { assertMatches(t, conn, "SHOW columns FROM `t5_null_vindex` in `ks`", expected) } +func TestCastConvert(t *testing.T) { + conn, err := mysql.Connect(context.Background(), &vtParams) + require.NoError(t, err) + defer conn.Close() + + assertMatches(t, conn, `SELECT CAST("test" AS CHAR(60))`, `[[VARCHAR("test")]]`) +} + func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) { t.Helper() qr := exec(t, conn, query) diff --git a/go/vt/sqlparser/normalizer.go b/go/vt/sqlparser/normalizer.go index 35c5aa97a20..36f2c08c253 100644 --- a/go/vt/sqlparser/normalizer.go +++ b/go/vt/sqlparser/normalizer.go @@ -73,6 +73,8 @@ func (nz *normalizer) WalkStatement(node SQLNode) (bool, error) { // Common node types that never contain SQLVals or ListArgs but create a lot of object // allocations. return false, nil + case *ConvertType: // we should not rewrite the type description + return false, nil } return true, nil } @@ -91,6 +93,9 @@ func (nz *normalizer) WalkSelect(node SQLNode) (bool, error) { case OrderBy, GroupBy: // do not make a bind var for order by column_position return false, nil + case *ConvertType: + // we should not rewrite the type description + return false, nil } return true, nil } diff --git a/go/vt/sqlparser/normalizer_test.go b/go/vt/sqlparser/normalizer_test.go index 6b4a0cca83c..bfd6003a966 100644 --- a/go/vt/sqlparser/normalizer_test.go +++ b/go/vt/sqlparser/normalizer_test.go @@ -195,6 +195,13 @@ func TestNormalize(t *testing.T) { outbv: map[string]*querypb.BindVariable{ "bv1": sqltypes.TestBindVariable([]interface{}{1, []byte("2")}), }, + }, { + // Do not normalize cast/convert types + in: `select CAST("test" AS CHAR(60))`, + outstmt: `select convert(:bv1, CHAR(60)) from dual`, + outbv: map[string]*querypb.BindVariable{ + "bv1": sqltypes.StringBindVariable("test"), + }, }} for _, tc := range testcases { stmt, err := Parse(tc.in)