Skip to content

Commit 43099af

Browse files
authored
MDEV-24020: Trim with remove_str Fails on Spider
This patch fixes the bug that TRIM(BOTH ... FROM $str), TRIM(LEADING ... FROM $str), and TRIM(TRAILING ... FROM $str) failed with errors when executing on Spider.
1 parent 6ed4750 commit 43099af

File tree

7 files changed

+366
-9
lines changed

7 files changed

+366
-9
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--let $MASTER_1_COMMENT_P_2_1= $MASTER_1_COMMENT_P_2_1_BACKUP
2+
--let $CHILD2_1_DROP_TABLES= $CHILD2_1_DROP_TABLES_BACKUP
3+
--let $CHILD2_1_CREATE_TABLES= $CHILD2_1_CREATE_TABLES_BACKUP
4+
--let $CHILD2_1_SELECT_TABLES= $CHILD2_1_SELECT_TABLES_BACKUP
5+
--disable_warnings
6+
--disable_query_log
7+
--disable_result_log
8+
--source ../t/test_deinit.inc
9+
--enable_result_log
10+
--enable_query_log
11+
--enable_warnings
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--disable_warnings
2+
--disable_query_log
3+
--disable_result_log
4+
--source ../t/test_init.inc
5+
--enable_result_log
6+
--enable_query_log
7+
--enable_warnings
8+
--let $MASTER_1_COMMENT_P_2_1_BACKUP= $MASTER_1_COMMENT_P_2_1
9+
let $MASTER_1_COMMENT_P_2_1=
10+
PARTITION BY LIST(a % 3) (
11+
PARTITION pt1 VALUES IN (0) COMMENT='srv "s_2_1", table "ta_r2"',
12+
PARTITION pt2 VALUES IN (1) COMMENT='srv "s_2_1", table "ta_r3"',
13+
PARTITION pt3 VALUES IN (2) COMMENT='srv "s_2_1", table "ta_r4"'
14+
);
15+
--let $CHILD2_1_DROP_TABLES_BACKUP= $CHILD2_1_DROP_TABLES
16+
let $CHILD2_1_DROP_TABLES=
17+
DROP TABLE IF EXISTS ta_r2 $STR_SEMICOLON
18+
DROP TABLE IF EXISTS ta_r3 $STR_SEMICOLON
19+
DROP TABLE IF EXISTS ta_r4;
20+
--let $CHILD2_1_CREATE_TABLES_BACKUP= $CHILD2_1_CREATE_TABLES
21+
let $CHILD2_1_CREATE_TABLES=
22+
CREATE TABLE ta_r2 (
23+
a INT,
24+
b VARCHAR(30),
25+
PRIMARY KEY(a)
26+
) $CHILD2_1_ENGINE $CHILD2_1_CHARSET $STR_SEMICOLON
27+
CREATE TABLE ta_r3 (
28+
a INT,
29+
b VARCHAR(30),
30+
PRIMARY KEY(a)
31+
) $CHILD2_1_ENGINE $CHILD2_1_CHARSET $STR_SEMICOLON
32+
CREATE TABLE ta_r4 (
33+
a INT,
34+
b VARCHAR(30),
35+
PRIMARY KEY(a)
36+
) $CHILD2_1_ENGINE $CHILD2_1_CHARSET;
37+
--let $CHILD2_1_SELECT_TABLES_BACKUP= $CHILD2_1_SELECT_TABLES
38+
let $CHILD2_1_SELECT_TABLES=
39+
SELECT a, b FROM ta_r2 ORDER BY a $STR_SEMICOLON
40+
SELECT a, b FROM ta_r3 ORDER BY a $STR_SEMICOLON
41+
SELECT a, b FROM ta_r4 ORDER BY a;
42+
let $CHILD2_1_SELECT_ARGUMENT1=
43+
SELECT argument FROM mysql.general_log WHERE argument LIKE '%select %';
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
for master_1
2+
for child2
3+
child2_1
4+
child2_2
5+
child2_3
6+
for child3
7+
8+
this test is for MDEV-24020
9+
10+
drop and create databases
11+
connection master_1;
12+
CREATE DATABASE auto_test_local;
13+
USE auto_test_local;
14+
connection child2_1;
15+
CREATE DATABASE auto_test_remote;
16+
USE auto_test_remote;
17+
18+
create table and insert
19+
connection child2_1;
20+
CHILD2_1_CREATE_TABLES
21+
connection master_1;
22+
CREATE TABLE tbl_a (
23+
a INT,
24+
b VARCHAR(30),
25+
PRIMARY KEY(a)
26+
) ENGINE=Spider PARTITION BY LIST(a % 3) (
27+
PARTITION pt1 VALUES IN (0) COMMENT='srv "s_2_1", table "ta_r2"',
28+
PARTITION pt2 VALUES IN (1) COMMENT='srv "s_2_1", table "ta_r3"',
29+
PARTITION pt3 VALUES IN (2) COMMENT='srv "s_2_1", table "ta_r4"'
30+
)
31+
INSERT INTO tbl_a VALUES(10000, " abcd ");
32+
INSERT INTO tbl_a VALUES(10001, " abcd ");
33+
INSERT INTO tbl_a VALUES(10002, " abcd ");
34+
INSERT INTO tbl_a VALUES(10003, "[[[abcd][[");
35+
INSERT INTO tbl_a VALUES(10004, "[[[abcd][[");
36+
INSERT INTO tbl_a VALUES(10005, "[[[abcd][[");
37+
INSERT INTO tbl_a VALUES(10006, "[[[abcd]]");
38+
INSERT INTO tbl_a VALUES(10007, "[[[abcd]]");
39+
INSERT INTO tbl_a VALUES(10008, "[[[abcd]]");
40+
INSERT INTO tbl_a VALUES(10009, "[[[**abcd****]]");
41+
42+
test 1
43+
connection master_1;
44+
UPDATE tbl_a SET b = trim(b) WHERE a = 10000;
45+
SELECT * FROM tbl_a WHERE a = 10000;
46+
a b
47+
10000 abcd
48+
UPDATE tbl_a SET b = ltrim(b) WHERE a = 10001;
49+
SELECT * FROM tbl_a WHERE a = 10001;
50+
a b
51+
10001 abcd
52+
UPDATE tbl_a SET b = rtrim(b) WHERE a = 10002;
53+
SELECT * FROM tbl_a WHERE a = 10002;
54+
a b
55+
10002 abcd
56+
UPDATE tbl_a SET b = trim(BOTH '[' FROM b) WHERE a = 10003;
57+
SELECT * FROM tbl_a WHERE a = 10003;
58+
a b
59+
10003 abcd]
60+
UPDATE tbl_a SET b = trim(LEADING '[' FROM b) WHERE a = 10004;
61+
SELECT * FROM tbl_a WHERE a = 10004;
62+
a b
63+
10004 abcd][[
64+
UPDATE tbl_a SET b = trim(TRAILING '[' FROM b) WHERE a = 10005;
65+
SELECT * FROM tbl_a WHERE a = 10005;
66+
a b
67+
10005 [[[abcd]
68+
UPDATE tbl_a SET b = trim(LEADING '[' FROM trim(TRAILING ']' FROM b)) WHERE a = 10006;
69+
SELECT * FROM tbl_a WHERE a = 10006;
70+
a b
71+
10006 abcd
72+
UPDATE tbl_a SET b = trim(TRAILING '[' FROM trim(LEADING ']' FROM b)) WHERE a = 10007;
73+
SELECT * FROM tbl_a WHERE a = 10007;
74+
a b
75+
10007 [[[abcd]]
76+
UPDATE tbl_a SET b = trim(TRAILING ']' FROM trim(LEADING '[' FROM b)) WHERE a = 10008;
77+
SELECT * FROM tbl_a WHERE a = 10008;
78+
a b
79+
10008 abcd
80+
UPDATE tbl_a SET b = trim(BOTH '*' FROM trim(TRAILING ']' FROM trim(LEADING '[' FROM b))) WHERE a = 10009;
81+
SELECT * FROM tbl_a WHERE a = 10009;
82+
a b
83+
10009 abcd
84+
85+
deinit
86+
connection master_1;
87+
DROP DATABASE IF EXISTS auto_test_local;
88+
connection child2_1;
89+
DROP DATABASE IF EXISTS auto_test_remote;
90+
for master_1
91+
for child2
92+
child2_1
93+
child2_2
94+
child2_3
95+
for child3
96+
97+
end of test
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
!include include/default_mysqld.cnf
2+
!include ../my_1_1.cnf
3+
!include ../my_2_1.cnf
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
--source ../include/mdev_24020_init.inc
2+
--echo
3+
--echo this test is for MDEV-24020
4+
--echo
5+
--echo drop and create databases
6+
7+
--connection master_1
8+
--disable_warnings
9+
CREATE DATABASE auto_test_local;
10+
USE auto_test_local;
11+
12+
--connection child2_1
13+
CREATE DATABASE auto_test_remote;
14+
USE auto_test_remote;
15+
--enable_warnings
16+
17+
--echo
18+
--echo create table and insert
19+
20+
--connection child2_1
21+
--disable_query_log
22+
--disable_ps_protocol
23+
echo CHILD2_1_CREATE_TABLES;
24+
eval $CHILD2_1_CREATE_TABLES;
25+
--enable_ps_protocol
26+
--enable_query_log
27+
28+
--connection master_1
29+
--disable_query_log
30+
echo CREATE TABLE tbl_a (
31+
a INT,
32+
b VARCHAR(30),
33+
PRIMARY KEY(a)
34+
) $MASTER_1_ENGINE $MASTER_1_COMMENT_P_2_1;
35+
eval CREATE TABLE tbl_a (
36+
a INT,
37+
b VARCHAR(30),
38+
PRIMARY KEY(a)
39+
) $MASTER_1_ENGINE $MASTER_1_COMMENT_P_2_1;
40+
--enable_query_log
41+
INSERT INTO tbl_a VALUES(10000, " abcd ");
42+
INSERT INTO tbl_a VALUES(10001, " abcd ");
43+
INSERT INTO tbl_a VALUES(10002, " abcd ");
44+
INSERT INTO tbl_a VALUES(10003, "[[[abcd][[");
45+
INSERT INTO tbl_a VALUES(10004, "[[[abcd][[");
46+
INSERT INTO tbl_a VALUES(10005, "[[[abcd][[");
47+
INSERT INTO tbl_a VALUES(10006, "[[[abcd]]");
48+
INSERT INTO tbl_a VALUES(10007, "[[[abcd]]");
49+
INSERT INTO tbl_a VALUES(10008, "[[[abcd]]");
50+
INSERT INTO tbl_a VALUES(10009, "[[[**abcd****]]");
51+
52+
--echo
53+
--echo test 1
54+
55+
--connection master_1
56+
UPDATE tbl_a SET b = trim(b) WHERE a = 10000;
57+
SELECT * FROM tbl_a WHERE a = 10000;
58+
UPDATE tbl_a SET b = ltrim(b) WHERE a = 10001;
59+
SELECT * FROM tbl_a WHERE a = 10001;
60+
UPDATE tbl_a SET b = rtrim(b) WHERE a = 10002;
61+
SELECT * FROM tbl_a WHERE a = 10002;
62+
UPDATE tbl_a SET b = trim(BOTH '[' FROM b) WHERE a = 10003;
63+
SELECT * FROM tbl_a WHERE a = 10003;
64+
UPDATE tbl_a SET b = trim(LEADING '[' FROM b) WHERE a = 10004;
65+
SELECT * FROM tbl_a WHERE a = 10004;
66+
UPDATE tbl_a SET b = trim(TRAILING '[' FROM b) WHERE a = 10005;
67+
SELECT * FROM tbl_a WHERE a = 10005;
68+
UPDATE tbl_a SET b = trim(LEADING '[' FROM trim(TRAILING ']' FROM b)) WHERE a = 10006;
69+
SELECT * FROM tbl_a WHERE a = 10006;
70+
UPDATE tbl_a SET b = trim(TRAILING '[' FROM trim(LEADING ']' FROM b)) WHERE a = 10007;
71+
SELECT * FROM tbl_a WHERE a = 10007;
72+
UPDATE tbl_a SET b = trim(TRAILING ']' FROM trim(LEADING '[' FROM b)) WHERE a = 10008;
73+
SELECT * FROM tbl_a WHERE a = 10008;
74+
UPDATE tbl_a SET b = trim(BOTH '*' FROM trim(TRAILING ']' FROM trim(LEADING '[' FROM b))) WHERE a = 10009;
75+
SELECT * FROM tbl_a WHERE a = 10009;
76+
77+
--echo
78+
--echo deinit
79+
--disable_warnings
80+
81+
--connection master_1
82+
DROP DATABASE IF EXISTS auto_test_local;
83+
84+
--connection child2_1
85+
DROP DATABASE IF EXISTS auto_test_remote;
86+
87+
--enable_warnings
88+
--source ../include/mdev_24020_deinit.inc
89+
--echo
90+
--echo end of test

storage/spider/spd_db_conn.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@
252252
#define SPIDER_SQL_B_STR "b"
253253
#define SPIDER_SQL_B_LEN (sizeof(SPIDER_SQL_B_STR) - 1)
254254

255+
#define SPIDER_SQL_TRIM_STR "trim"
256+
#define SPIDER_SQL_TRIM_LEN sizeof(SPIDER_SQL_TRIM_STR) - 1
257+
#define SPIDER_SQL_TRIM_BOTH_STR "both "
258+
#define SPIDER_SQL_TRIM_BOTH_LEN sizeof(SPIDER_SQL_TRIM_BOTH_STR) - 1
259+
#define SPIDER_SQL_TRIM_LEADING_STR "leading "
260+
#define SPIDER_SQL_TRIM_LEADING_LEN sizeof(SPIDER_SQL_TRIM_LEADING_STR) - 1
261+
#define SPIDER_SQL_TRIM_TRAILING_STR "trailing "
262+
#define SPIDER_SQL_TRIM_TRAILING_LEN sizeof(SPIDER_SQL_TRIM_TRAILING_STR) - 1
263+
255264
#define SPIDER_SQL_INDEX_IGNORE_STR " IGNORE INDEX "
256265
#define SPIDER_SQL_INDEX_IGNORE_LEN (sizeof(SPIDER_SQL_INDEX_IGNORE_STR) - 1)
257266
#define SPIDER_SQL_INDEX_USE_STR " USE INDEX "

storage/spider/spd_db_mysql.cc

Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4181,18 +4181,122 @@ int spider_db_mbase_util::open_item_func(
41814181
) {
41824182
/* no action */
41834183
break;
4184-
} else if (func_name_length == 4 &&
4185-
!strncasecmp("rand", func_name, func_name_length) &&
4184+
} else if (func_name_length == 4)
4185+
{
4186+
if (
4187+
!strncasecmp("rand", func_name, func_name_length) &&
41864188
#ifdef SPIDER_Item_args_arg_count_IS_PROTECTED
4187-
!item_func->argument_count()
4189+
!item_func->argument_count()
41884190
#else
4189-
!item_func->arg_count
4191+
!item_func->arg_count
41904192
#endif
4191-
) {
4192-
if (str)
4193-
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4194-
DBUG_RETURN(spider_db_open_item_int(item_func, NULL, spider, str,
4195-
alias, alias_length, dbton_id, use_fields, fields));
4193+
) {
4194+
if (str)
4195+
str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4196+
DBUG_RETURN(spider_db_open_item_int(item_func, NULL, spider, str,
4197+
alias, alias_length, dbton_id, use_fields, fields));
4198+
} else if (
4199+
!strncasecmp("trim", func_name, func_name_length) &&
4200+
item_count == 2
4201+
) {
4202+
/* item_count == 1 means this TRIM() is without a remove_str */
4203+
item = item_list[0];
4204+
Item *item_tmp = item_list[1];
4205+
if (str)
4206+
{
4207+
if (item_tmp->type() == Item::STRING_ITEM)
4208+
{
4209+
/* 1. append 'TRIM(BOTH ' */
4210+
if (str->reserve(SPIDER_SQL_TRIM_LEN + SPIDER_SQL_OPEN_PAREN_LEN +
4211+
SPIDER_SQL_TRIM_BOTH_LEN))
4212+
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4213+
str->q_append(SPIDER_SQL_TRIM_STR, SPIDER_SQL_TRIM_LEN);
4214+
str->q_append(SPIDER_SQL_OPEN_PAREN_STR,
4215+
SPIDER_SQL_OPEN_PAREN_LEN);
4216+
str->q_append(SPIDER_SQL_TRIM_BOTH_STR, SPIDER_SQL_TRIM_BOTH_LEN);
4217+
/* 2. append "remove_str"*/
4218+
if ((error_num = spider_db_print_item_type(
4219+
item_tmp, NULL, spider, str, alias, alias_length, dbton_id,
4220+
use_fields, fields)))
4221+
DBUG_RETURN(error_num);
4222+
/* 3. append ' FROM ' */
4223+
if (str->reserve(SPIDER_SQL_FROM_LEN))
4224+
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4225+
str->q_append(SPIDER_SQL_FROM_STR, SPIDER_SQL_FROM_LEN);
4226+
/* 4. append `field` */
4227+
if ((error_num = spider_db_print_item_type(
4228+
item, NULL, spider, str, alias, alias_length, dbton_id,
4229+
use_fields, fields)))
4230+
DBUG_RETURN(error_num);
4231+
/* 5. append ')' */
4232+
if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN))
4233+
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4234+
str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
4235+
SPIDER_SQL_CLOSE_PAREN_LEN);
4236+
}
4237+
}
4238+
item_count -= 2;
4239+
break;
4240+
}
4241+
} else if (func_name_length == 5)
4242+
{
4243+
if (
4244+
(!strncasecmp("ltrim", func_name, func_name_length) ||
4245+
!strncasecmp("rtrim", func_name, func_name_length)) &&
4246+
(item_count == 2)
4247+
) {
4248+
/* the func_name for TRIM(LEADING ...) is LTRIM, for TRIM(TRAILING) is RTRIM */
4249+
/* item_count == 2 means this TRIM(LEADING/TRAILING ...) is with a remove_str */
4250+
item = item_list[0];
4251+
Item *item_tmp = item_list[1];
4252+
if (str)
4253+
{
4254+
if (item_tmp->type() == Item::STRING_ITEM)
4255+
{
4256+
/* 1. append 'TRIM(LEADING ' or 'TRIM(TRAILING ' */
4257+
if (func_name[0] == 'l' || func_name[0] == 'L')
4258+
{ /* ltrim */
4259+
if (str->reserve(SPIDER_SQL_TRIM_LEN + SPIDER_SQL_OPEN_PAREN_LEN +
4260+
SPIDER_SQL_TRIM_LEADING_LEN))
4261+
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4262+
str->q_append(SPIDER_SQL_TRIM_STR, SPIDER_SQL_TRIM_LEN);
4263+
str->q_append(SPIDER_SQL_OPEN_PAREN_STR,
4264+
SPIDER_SQL_OPEN_PAREN_LEN);
4265+
str->q_append(SPIDER_SQL_TRIM_LEADING_STR, SPIDER_SQL_TRIM_LEADING_LEN);
4266+
} else
4267+
{ /* rtrim */
4268+
if (str->reserve(SPIDER_SQL_TRIM_LEN + SPIDER_SQL_OPEN_PAREN_LEN +
4269+
SPIDER_SQL_TRIM_TRAILING_LEN))
4270+
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4271+
str->q_append(SPIDER_SQL_TRIM_STR, SPIDER_SQL_TRIM_LEN);
4272+
str->q_append(SPIDER_SQL_OPEN_PAREN_STR,
4273+
SPIDER_SQL_OPEN_PAREN_LEN);
4274+
str->q_append(SPIDER_SQL_TRIM_TRAILING_STR, SPIDER_SQL_TRIM_TRAILING_LEN);
4275+
}
4276+
/* 2. append "remove_str"*/
4277+
if ((error_num = spider_db_print_item_type(
4278+
item_tmp, NULL, spider, str, alias, alias_length, dbton_id,
4279+
use_fields, fields)))
4280+
DBUG_RETURN(error_num);
4281+
/* 3. append ' FROM ' */
4282+
if (str->reserve(SPIDER_SQL_FROM_LEN))
4283+
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4284+
str->q_append(SPIDER_SQL_FROM_STR, SPIDER_SQL_FROM_LEN);
4285+
/* 4. append `field` */
4286+
if ((error_num = spider_db_print_item_type(
4287+
item, NULL, spider, str, alias, alias_length, dbton_id,
4288+
use_fields, fields)))
4289+
DBUG_RETURN(error_num);
4290+
/* 5. append ')' */
4291+
if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN))
4292+
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4293+
str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
4294+
SPIDER_SQL_CLOSE_PAREN_LEN);
4295+
}
4296+
}
4297+
item_count -= 2;
4298+
break;
4299+
}
41964300
} else if (func_name_length == 6 &&
41974301
!strncasecmp("istrue", func_name, func_name_length)
41984302
) {

0 commit comments

Comments
 (0)