Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: fix creating partition tables fail in ANSI_QUOTES mode #35379

Merged
merged 12 commits into from
Jun 21, 2022
16 changes: 16 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3615,3 +3615,19 @@ func TestDuplicatePartitionNames(t *testing.T) {
"(PARTITION `p2` VALUES IN (2),\n" +
" PARTITION `p3` VALUES IN (3))"))
}

func TestPartitionTableWithAnsiQuotes(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database partitionWithAnsiQuotes")
defer tk.MustExec("drop database partitionWithAnsiQuotes")
tk.MustExec("use partitionWithAnsiQuotes")
tk.MustExec("SET SESSION sql_mode='ANSI_QUOTES'")
tk.MustExec(`create table t1(created_at datetime) PARTITION BY RANGE COLUMNS(created_at) (
PARTITION p0 VALUES LESS THAN ('2021-12-01 00:00:00'),
PARTITION p1 VALUES LESS THAN ('2022-01-01 00:00:00'))`)
tk.MustExec(`create table t2(created_at timestamp) PARTITION BY RANGE (unix_timestamp(created_at)) (
PARTITION p0 VALUES LESS THAN (unix_timestamp('2021-12-01 00:00:00')),
PARTITION p1 VALUES LESS THAN (unix_timestamp('2022-01-01 00:00:00')))`)
}
mjonss marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 4 additions & 1 deletion types/parser_driver/value_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ func (n *ValueExpr) Format(w io.Writer) {
case types.KindFloat64:
s = strconv.FormatFloat(n.GetFloat64(), 'e', -1, 64)
case types.KindString, types.KindBytes:
s = strconv.Quote(n.GetString())
// If sql_mode='ANSI_QUOTES', strings with double-quotes will be taken as an identifier.
// See #35281.
s = strings.Replace(n.GetString(), `'`, `''`, -1)
s = fmt.Sprintf("'%s'", s)
case types.KindMysqlDecimal:
s = n.GetMysqlDecimal().String()
case types.KindBinaryLiteral:
Expand Down
29 changes: 29 additions & 0 deletions types/parser_driver/value_expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,32 @@ func TestValueExprRestore(t *testing.T) {
})
}
}

func TestValueExprFormat(t *testing.T) {
tests := []struct {
datum types.Datum
expect string
}{
{types.NewDatum(nil), "NULL"},
{types.NewIntDatum(1), "1"},
{types.NewIntDatum(-1), "-1"},
{types.NewUintDatum(1), "1"},
{types.NewFloat32Datum(1.1), "1.1e+00"},
{types.NewFloat64Datum(1.1), "1.1e+00"},
{types.NewStringDatum("test `s't\"r."), "'test `s''t\"r.'"},
{types.NewBytesDatum([]byte("test `s't\"r.")), "'test `s''t\"r.'"},
{types.NewBinaryLiteralDatum([]byte("test `s't\"r.")), "b'11101000110010101110011011101000010000001100000011100110010011101110100001000100111001000101110'"},
{types.NewDecimalDatum(types.NewDecFromInt(321)), "321"},
{types.NewStringDatum("\\"), "'\\'"},
}

for _, test := range tests {
test := test
t.Run(test.expect, func(t *testing.T) {
var sb strings.Builder
expr := &ValueExpr{Datum: test.datum}
expr.Format(&sb)
require.Equalf(t, test.expect, sb.String(), "datum: %#v", test.datum)
})
}
}