Skip to content

Commit

Permalink
cmd: make explain_test can test collation-related tests with new-coll…
Browse files Browse the repository at this point in the history
…ation disabled (#31385)

close #31384
  • Loading branch information
wjhuang2016 authored Jan 10, 2022
1 parent 8dfad92 commit 28c5074
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 14 deletions.
26 changes: 26 additions & 0 deletions cmd/explaintest/disable_new_collation.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2022 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

lease = "0"
mem-quota-query = 34359738368
host = "127.0.0.1"

[status]
status-host = "127.0.0.1"

[performance]
stats-lease = "0"

[experimental]
allow-expression-index = true
25 changes: 19 additions & 6 deletions cmd/explaintest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ import (
const dbName = "test"

var (
logLevel string
port uint
statusPort uint
record bool
create bool
logLevel string
port uint
statusPort uint
record bool
create bool
collationDisable bool
)

func init() {
Expand All @@ -53,6 +54,7 @@ func init() {
flag.UintVar(&statusPort, "status", 10080, "tidb server status port [default: 10080]")
flag.BoolVar(&record, "record", false, "record the test output in the result file")
flag.BoolVar(&create, "create", false, "create and import data into table, and save json file of stats")
flag.BoolVar(&collationDisable, "collation-disable", false, "run collation related-test with new-collation disabled")
}

var mdb *sql.DB
Expand Down Expand Up @@ -565,7 +567,15 @@ func (t *tester) testFileName() string {

func (t *tester) resultFileName() string {
// test and result must be in current ./r, the same as MySQL
return fmt.Sprintf("./r/%s.result", t.name)
name := t.name
if strings.HasPrefix(name, "collation") {
if collationDisable {
name = name + "_disabled"
} else {
name = name + "_enabled"
}
}
return fmt.Sprintf("./r/%s.result", name)
}

func (t *tester) checkLastResult() error {
Expand Down Expand Up @@ -606,6 +616,9 @@ func loadAllTests() ([]string, error) {
// the test file must have a suffix .test
name := f.Name()
if strings.HasSuffix(name, ".test") {
if collationDisable && !strings.HasPrefix(name, "collation") {
continue
}
name = strings.TrimSuffix(name, ".test")

if create && !strings.HasSuffix(name, "_stats") {
Expand Down
89 changes: 89 additions & 0 deletions cmd/explaintest/r/collation_check_use_collation_disabled.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
create database collation_check_use_collation;
use collation_check_use_collation;
CREATE TABLE `t` (
`a` char(10) DEFAULT NULL
);
CREATE TABLE `t1` (
`a` char(10) COLLATE utf8mb4_general_ci DEFAULT NULL
);
insert into t values ("A");
insert into t1 values ("a");
select a as a_col from t where t.a = all (select a collate utf8mb4_general_ci from t1);
a_col
select a as a_col from t where t.a != any (select a collate utf8mb4_general_ci from t1);
a_col
A
select a as a_col from t where t.a <= all (select a collate utf8mb4_general_ci from t1);
a_col
A
select a as a_col from t where t.a <= any (select a collate utf8mb4_general_ci from t1);
a_col
A
select a as a_col from t where t.a = (select a collate utf8mb4_general_ci from t1);
a_col
drop table if exists t;
create table t(a enum('a', 'b'), b varchar(20));
insert into t values ("a", "b");
select * from t where a in (a);
a b
a b
drop table if exists t;
create table t(a enum('a', 'b') charset utf8mb4 collate utf8mb4_general_ci, b varchar(20));
insert into t values ("b", "c");
insert into t values ("B", "b");
Error 1265: Data truncated for column 'a' at row 1
select * from t where 'B' collate utf8mb4_general_ci in (a);
a b
select * from t where 'B' collate utf8mb4_bin in (a);
a b
select * from t where 'B' collate utf8mb4_bin in (a, b);
a b
select * from t where 'B' collate utf8mb4_bin in (a, "a", 1);
a b
select * from t where 'B' collate utf8mb4_bin in (a, "B", 1);
a b
b c
select * from t where 1 in (a);
a b
select * from t where 2 in (a);
a b
b c
select * from t where 1 in (a, 0);
a b
drop table if exists t;
create table t(a set('a', 'b'), b varchar(20));
insert into t values ("a", "b");
select * from t where a in (a);
a b
a b
drop table if exists t;
create table t(a set('a', 'b') charset utf8mb4 collate utf8mb4_general_ci, b varchar(20));
insert into t values ("b", "c");
insert into t values ("B", "b");
Error 1265: Data truncated for column 'a' at row 1
select * from t where 'B' collate utf8mb4_general_ci in (a);
a b
select * from t where 'B' collate utf8mb4_bin in (a);
a b
select * from t where 'B' collate utf8mb4_bin in (a, b);
a b
select * from t where 'B' collate utf8mb4_bin in (a, "a", 1);
a b
select * from t where 'B' collate utf8mb4_bin in (a, "B", 1);
a b
b c
select * from t where 1 in (a);
a b
select * from t where 2 in (a);
a b
b c
select * from t where 1 in (a, 0);
a b
drop table if exists tbl_2;
create table tbl_2 ( col_20 bigint not null , col_21 smallint not null , col_22 decimal(24,10) default null , col_23 tinyint default 71 not null , col_24 bigint not null , col_25 tinyint default 18 , col_26 varchar(330) collate utf8_bin not null , col_27 char(77) collate utf8mb4_unicode_ci , col_28 char(46) collate utf8_general_ci not null , col_29 smallint unsigned not null , primary key idx_13 ( col_27(5) ) , key idx_14 ( col_24 ) , unique key idx_15 ( col_23,col_21,col_28,col_29,col_24 ) ) collate utf8_bin ;
insert ignore into tbl_2 values ( 5888267793391993829,5371,94.63,-109,5728076076919247337,89,'WUicqUTgdGJcjbC','SapBPqczTWWSN','xUSwH',49462 );
select col_25 from tbl_2 where ( tbl_2.col_27 > 'nSWYrpTH' or not( tbl_2.col_27 between 'CsWIuxlSjU' and 'SfwoyjUEzgg' ) ) and ( tbl_2.col_23 <= -95);
col_25
select col_25 from tbl_2 use index(primary) where ( tbl_2.col_27 > 'nSWYrpTH' or not( tbl_2.col_27 between 'CsWIuxlSjU' and 'SfwoyjUEzgg' ) ) and ( tbl_2.col_23 <= -95);
col_25
use test
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ CREATE TABLE `t` (
CREATE TABLE `t1` (
`a` char(10) COLLATE utf8mb4_general_ci DEFAULT NULL
);
insert into t values ("a");
insert into t1 values ("A");
insert into t values ("A");
insert into t1 values ("a");
select a as a_col from t where t.a = all (select a collate utf8mb4_general_ci from t1);
a_col
a
A
select a as a_col from t where t.a != any (select a collate utf8mb4_general_ci from t1);
a_col
select a as a_col from t where t.a <= all (select a collate utf8mb4_general_ci from t1);
a_col
a
A
select a as a_col from t where t.a <= any (select a collate utf8mb4_general_ci from t1);
a_col
a
A
select a as a_col from t where t.a = (select a collate utf8mb4_general_ci from t1);
a_col
a
A
drop table if exists t;
create table t(a enum('a', 'b'), b varchar(20));
insert into t values ("a", "b");
Expand All @@ -30,9 +30,11 @@ a b
a b
drop table if exists t;
create table t(a enum('a', 'b') charset utf8mb4 collate utf8mb4_general_ci, b varchar(20));
insert into t values ("b", "c");
insert into t values ("B", "b");
select * from t where 'B' collate utf8mb4_general_ci in (a);
a b
b c
b b
select * from t where 'B' collate utf8mb4_bin in (a);
a b
Expand All @@ -42,11 +44,13 @@ select * from t where 'B' collate utf8mb4_bin in (a, "a", 1);
a b
select * from t where 'B' collate utf8mb4_bin in (a, "B", 1);
a b
b c
b b
select * from t where 1 in (a);
a b
select * from t where 2 in (a);
a b
b c
b b
select * from t where 1 in (a, 0);
a b
Expand All @@ -58,9 +62,11 @@ a b
a b
drop table if exists t;
create table t(a set('a', 'b') charset utf8mb4 collate utf8mb4_general_ci, b varchar(20));
insert into t values ("b", "c");
insert into t values ("B", "b");
select * from t where 'B' collate utf8mb4_general_ci in (a);
a b
b c
b b
select * from t where 'B' collate utf8mb4_bin in (a);
a b
Expand All @@ -70,11 +76,13 @@ select * from t where 'B' collate utf8mb4_bin in (a, "a", 1);
a b
select * from t where 'B' collate utf8mb4_bin in (a, "B", 1);
a b
b c
b b
select * from t where 1 in (a);
a b
select * from t where 2 in (a);
a b
b c
b b
select * from t where 1 in (a, 0);
a b
Expand Down
File renamed without changes.
41 changes: 41 additions & 0 deletions cmd/explaintest/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,45 @@ if [ ! -z "$race" ]; then
cat $explain_test_log
exit 1
fi

kill -9 $SERVER_PID

echo "run collation tests when new-collation disabled"
echo "start tidb-server, log file: $explain_test_log"
if [ "${TIDB_TEST_STORE_NAME}" = "tikv" ]; then
$tidb_server -P "$port" -status "$status" -config disable_new_collation.toml -store tikv -path "${TIKV_PATH}" >> $explain_test_log 2>&1 &
SERVER_PID=$!
else
$tidb_server -P "$port" -status "$status" -config disable_new_collation.toml -store unistore -path "" >> $explain_test_log 2>&1 &
SERVER_PID=$!
fi
echo "tidb-server(PID: $SERVER_PID) started"

sleep 5

if [ $record -eq 1 ]; then
if [ "$record_case" = 'all' ]; then
echo "record all collation cases"
$explain_test -port "$port" -status "$status" --record --log-level=error --collation-disable=true
else
echo "record result for case: \"$record_case\""
$explain_test -port "$port" -status "$status" --record $record_case --log-level=error --collation-disable=true
fi
elif [ $create -eq 1 ]; then
if [ "$create_case" = 'all' ]; then
echo "create all collation cases"
$explain_test -port "$port" -status "$status" --create --log-level=error --collation-disable=true
else
echo "create result for case: \"$create_case\""
$explain_test -port "$port" -status "$status" --create $create_case --log-level=error --collation-disable=true
fi
else
if [ -z "$tests" ]; then
echo "run all collation explain test cases"
else
echo "run explain test cases: $tests"
fi
$explain_test -port "$port" -status "$status" --log-level=error $tests --collation-disable=true
fi

echo "explaintest end"
12 changes: 10 additions & 2 deletions cmd/explaintest/t/collation_check_use_collation.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ CREATE TABLE `t` (
CREATE TABLE `t1` (
`a` char(10) COLLATE utf8mb4_general_ci DEFAULT NULL
);
insert into t values ("a");
insert into t1 values ("A");
insert into t values ("A");
# Ignore error for the disabled new-collation case.
--error 1265
insert into t1 values ("a");
select a as a_col from t where t.a = all (select a collate utf8mb4_general_ci from t1);
select a as a_col from t where t.a != any (select a collate utf8mb4_general_ci from t1);
select a as a_col from t where t.a <= all (select a collate utf8mb4_general_ci from t1);
Expand All @@ -28,6 +30,9 @@ insert into t values ("a", "b");
select * from t where a in (a);
drop table if exists t;
create table t(a enum('a', 'b') charset utf8mb4 collate utf8mb4_general_ci, b varchar(20));
insert into t values ("b", "c");
# Ignore error for the disabled new-collation case.
--error 1265
insert into t values ("B", "b");
select * from t where 'B' collate utf8mb4_general_ci in (a);
select * from t where 'B' collate utf8mb4_bin in (a);
Expand All @@ -45,6 +50,9 @@ insert into t values ("a", "b");
select * from t where a in (a);
drop table if exists t;
create table t(a set('a', 'b') charset utf8mb4 collate utf8mb4_general_ci, b varchar(20));
insert into t values ("b", "c");
# Ignore error for the disabled new-collation case.
--error 1265
insert into t values ("B", "b");
select * from t where 'B' collate utf8mb4_general_ci in (a);
select * from t where 'B' collate utf8mb4_bin in (a);
Expand Down
File renamed without changes.

0 comments on commit 28c5074

Please sign in to comment.