Skip to content

Commit b31b12d

Browse files
authored
executor: fix warning message for insert ignore with binary type (#59844)
close #31639
1 parent 949fc53 commit b31b12d

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

pkg/executor/batch_checker.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package executor
1616

1717
import (
1818
"context"
19+
"strings"
1920

2021
"github.com/pingcap/errors"
2122
"github.com/pingcap/tidb/pkg/errno"
@@ -29,6 +30,7 @@ import (
2930
"github.com/pingcap/tidb/pkg/table/tables"
3031
"github.com/pingcap/tidb/pkg/tablecodec"
3132
"github.com/pingcap/tidb/pkg/types"
33+
"github.com/pingcap/tidb/pkg/util"
3234
"github.com/pingcap/tidb/pkg/util/chunk"
3335
"github.com/pingcap/tidb/pkg/util/codec"
3436
"github.com/pingcap/tidb/pkg/util/logutil"
@@ -265,6 +267,17 @@ func dataToStrings(data []types.Datum) ([]string, error) {
265267
if err != nil {
266268
return nil, errors.Trace(err)
267269
}
270+
if datum.Kind() == types.KindBytes || datum.Kind() == types.KindMysqlBit || datum.Kind() == types.KindBinaryLiteral {
271+
// Same as MySQL, remove all 0x00 on the tail,
272+
// but keep one 0x00 at least.
273+
if datum.Kind() == types.KindBytes {
274+
str = strings.TrimRight(str, string(rune(0x00)))
275+
if len(str) == 0 {
276+
str = string(rune(0x00))
277+
}
278+
}
279+
str = util.FmtNonASCIIPrintableCharToHex(str, len(str), true)
280+
}
268281
strs = append(strs, str)
269282
}
270283
return strs, nil

tests/integrationtest/r/executor/insert.result

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,3 +2183,42 @@ id col1
21832183
4
21842184
5
21852185
6
2186+
drop table if exists t1;
2187+
create table t1 (id binary(20) unique);
2188+
INSERT IGNORE INTO t1 VALUES (X'0e6b4234fd0b08d4c4ec656529d94df02b37c472');
2189+
INSERT IGNORE INTO t1 VALUES (X'0e6b4234fd0b08d4c4ec656529d94df02b37c472');
2190+
show warnings;
2191+
Level Code Message
2192+
Warning 1062 Duplicate entry '\x0EkB4\xFD\x0B\x08\xD4\xC4\xECee)\xD9M\xF0+7\xC4r' for key 't1.id'
2193+
INSERT IGNORE INTO t1 VALUES (X'');
2194+
INSERT IGNORE INTO t1 VALUES (X'');
2195+
show warnings;
2196+
Level Code Message
2197+
Warning 1062 Duplicate entry '\x00' for key 't1.id'
2198+
INSERT IGNORE INTO t1 VALUES (X'7f000000');
2199+
INSERT IGNORE INTO t1 VALUES (X'7f000000');
2200+
show warnings;
2201+
Level Code Message
2202+
Warning 1062 Duplicate entry '\x7F' for key 't1.id'
2203+
drop table if exists t1;
2204+
create table t1 (id bit(20) unique);
2205+
INSERT IGNORE INTO t1 VALUES (10);
2206+
INSERT IGNORE INTO t1 VALUES (10);
2207+
show warnings;
2208+
Level Code Message
2209+
Warning 1062 Duplicate entry '\x00\x00\x0A' for key 't1.id'
2210+
INSERT IGNORE INTO t1 VALUES (65536);
2211+
INSERT IGNORE INTO t1 VALUES (65536);
2212+
show warnings;
2213+
Level Code Message
2214+
Warning 1062 Duplicate entry '\x01\x00\x00' for key 't1.id'
2215+
INSERT IGNORE INTO t1 VALUES (35);
2216+
INSERT IGNORE INTO t1 VALUES (35);
2217+
show warnings;
2218+
Level Code Message
2219+
Warning 1062 Duplicate entry '\x00\x00#' for key 't1.id'
2220+
INSERT IGNORE INTO t1 VALUES (127);
2221+
INSERT IGNORE INTO t1 VALUES (127);
2222+
show warnings;
2223+
Level Code Message
2224+
Warning 1062 Duplicate entry '\x00\x00\x7F' for key 't1.id'

tests/integrationtest/t/executor/insert.test

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,3 +1649,32 @@ update t1 set col1 = null where id = 3;
16491649
show warnings;
16501650
insert ignore t1 VALUES (4, 4) ON DUPLICATE KEY UPDATE col1 = null;
16511651
select * from t1;
1652+
1653+
# TestIssue31639
1654+
drop table if exists t1;
1655+
create table t1 (id binary(20) unique);
1656+
INSERT IGNORE INTO t1 VALUES (X'0e6b4234fd0b08d4c4ec656529d94df02b37c472');
1657+
INSERT IGNORE INTO t1 VALUES (X'0e6b4234fd0b08d4c4ec656529d94df02b37c472');
1658+
show warnings;
1659+
INSERT IGNORE INTO t1 VALUES (X'');
1660+
INSERT IGNORE INTO t1 VALUES (X'');
1661+
show warnings;
1662+
INSERT IGNORE INTO t1 VALUES (X'7f000000');
1663+
INSERT IGNORE INTO t1 VALUES (X'7f000000');
1664+
show warnings;
1665+
1666+
drop table if exists t1;
1667+
create table t1 (id bit(20) unique);
1668+
INSERT IGNORE INTO t1 VALUES (10);
1669+
INSERT IGNORE INTO t1 VALUES (10);
1670+
show warnings;
1671+
INSERT IGNORE INTO t1 VALUES (65536);
1672+
INSERT IGNORE INTO t1 VALUES (65536);
1673+
show warnings;
1674+
INSERT IGNORE INTO t1 VALUES (35);
1675+
INSERT IGNORE INTO t1 VALUES (35);
1676+
show warnings;
1677+
INSERT IGNORE INTO t1 VALUES (127);
1678+
INSERT IGNORE INTO t1 VALUES (127);
1679+
show warnings;
1680+

0 commit comments

Comments
 (0)