Skip to content

Commit

Permalink
executor: fix warning message for insert ignore with binary type (#59844
Browse files Browse the repository at this point in the history
)

close #31639
  • Loading branch information
Defined2014 authored Feb 28, 2025
1 parent 949fc53 commit b31b12d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/executor/batch_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package executor

import (
"context"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/pkg/errno"
Expand All @@ -29,6 +30,7 @@ import (
"github.com/pingcap/tidb/pkg/table/tables"
"github.com/pingcap/tidb/pkg/tablecodec"
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util"
"github.com/pingcap/tidb/pkg/util/chunk"
"github.com/pingcap/tidb/pkg/util/codec"
"github.com/pingcap/tidb/pkg/util/logutil"
Expand Down Expand Up @@ -265,6 +267,17 @@ func dataToStrings(data []types.Datum) ([]string, error) {
if err != nil {
return nil, errors.Trace(err)
}
if datum.Kind() == types.KindBytes || datum.Kind() == types.KindMysqlBit || datum.Kind() == types.KindBinaryLiteral {
// Same as MySQL, remove all 0x00 on the tail,
// but keep one 0x00 at least.
if datum.Kind() == types.KindBytes {
str = strings.TrimRight(str, string(rune(0x00)))
if len(str) == 0 {
str = string(rune(0x00))
}
}
str = util.FmtNonASCIIPrintableCharToHex(str, len(str), true)
}
strs = append(strs, str)
}
return strs, nil
Expand Down
39 changes: 39 additions & 0 deletions tests/integrationtest/r/executor/insert.result
Original file line number Diff line number Diff line change
Expand Up @@ -2183,3 +2183,42 @@ id col1
4
5
6
drop table if exists t1;
create table t1 (id binary(20) unique);
INSERT IGNORE INTO t1 VALUES (X'0e6b4234fd0b08d4c4ec656529d94df02b37c472');
INSERT IGNORE INTO t1 VALUES (X'0e6b4234fd0b08d4c4ec656529d94df02b37c472');
show warnings;
Level Code Message
Warning 1062 Duplicate entry '\x0EkB4\xFD\x0B\x08\xD4\xC4\xECee)\xD9M\xF0+7\xC4r' for key 't1.id'
INSERT IGNORE INTO t1 VALUES (X'');
INSERT IGNORE INTO t1 VALUES (X'');
show warnings;
Level Code Message
Warning 1062 Duplicate entry '\x00' for key 't1.id'
INSERT IGNORE INTO t1 VALUES (X'7f000000');
INSERT IGNORE INTO t1 VALUES (X'7f000000');
show warnings;
Level Code Message
Warning 1062 Duplicate entry '\x7F' for key 't1.id'
drop table if exists t1;
create table t1 (id bit(20) unique);
INSERT IGNORE INTO t1 VALUES (10);
INSERT IGNORE INTO t1 VALUES (10);
show warnings;
Level Code Message
Warning 1062 Duplicate entry '\x00\x00\x0A' for key 't1.id'
INSERT IGNORE INTO t1 VALUES (65536);
INSERT IGNORE INTO t1 VALUES (65536);
show warnings;
Level Code Message
Warning 1062 Duplicate entry '\x01\x00\x00' for key 't1.id'
INSERT IGNORE INTO t1 VALUES (35);
INSERT IGNORE INTO t1 VALUES (35);
show warnings;
Level Code Message
Warning 1062 Duplicate entry '\x00\x00#' for key 't1.id'
INSERT IGNORE INTO t1 VALUES (127);
INSERT IGNORE INTO t1 VALUES (127);
show warnings;
Level Code Message
Warning 1062 Duplicate entry '\x00\x00\x7F' for key 't1.id'
Expand Down
29 changes: 29 additions & 0 deletions tests/integrationtest/t/executor/insert.test
Original file line number Diff line number Diff line change
Expand Up @@ -1649,3 +1649,32 @@ update t1 set col1 = null where id = 3;
show warnings;
insert ignore t1 VALUES (4, 4) ON DUPLICATE KEY UPDATE col1 = null;
select * from t1;

# TestIssue31639
drop table if exists t1;
create table t1 (id binary(20) unique);
INSERT IGNORE INTO t1 VALUES (X'0e6b4234fd0b08d4c4ec656529d94df02b37c472');
INSERT IGNORE INTO t1 VALUES (X'0e6b4234fd0b08d4c4ec656529d94df02b37c472');
show warnings;
INSERT IGNORE INTO t1 VALUES (X'');
INSERT IGNORE INTO t1 VALUES (X'');
show warnings;
INSERT IGNORE INTO t1 VALUES (X'7f000000');
INSERT IGNORE INTO t1 VALUES (X'7f000000');
show warnings;

drop table if exists t1;
create table t1 (id bit(20) unique);
INSERT IGNORE INTO t1 VALUES (10);
INSERT IGNORE INTO t1 VALUES (10);
show warnings;
INSERT IGNORE INTO t1 VALUES (65536);
INSERT IGNORE INTO t1 VALUES (65536);
show warnings;
INSERT IGNORE INTO t1 VALUES (35);
INSERT IGNORE INTO t1 VALUES (35);
show warnings;
INSERT IGNORE INTO t1 VALUES (127);
INSERT IGNORE INTO t1 VALUES (127);
show warnings;

0 comments on commit b31b12d

Please sign in to comment.