Skip to content

Commit 5273584

Browse files
mrhhsgpull[bot]
authored andcommitted
[fix](olap) deletion statement with space conditions did not take effect (#20349)
Deletion statement like this: delete from tb where k1 = ' '; The rows whose k1's value is ' ' will not be deleted.
1 parent c077b18 commit 5273584

4 files changed

Lines changed: 146 additions & 8 deletions

File tree

‎be/src/olap/delete_handler.cpp‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ std::string DeleteHandler::construct_sub_predicates(const TCondition& condition)
110110
} else if (op == "!*=") {
111111
op = "!=";
112112
}
113-
condition_str = condition.column_name + op + condition.condition_values[0];
113+
condition_str = condition.column_name + op + "'" + condition.condition_values[0] + "'";
114114
}
115115
return condition_str;
116116
}
@@ -218,7 +218,7 @@ bool DeleteHandler::_parse_condition(const std::string& condition_str, TConditio
218218
// group2: ((?:=)|(?:!=)|(?:>>)|(?:<<)|(?:>=)|(?:<=)|(?:\*=)|(?:IS)) matches "="
219219
// group3: ((?:[\s\S]+)?) matches "1597751948193618247 and length(source)<1;\n;\n"
220220
const char* const CONDITION_STR_PATTERN =
221-
R"((\w+)\s*((?:=)|(?:!=)|(?:>>)|(?:<<)|(?:>=)|(?:<=)|(?:\*=)|(?:IS))\s*((?:[\s\S]+)?))";
221+
R"((\w+)\s*((?:=)|(?:!=)|(?:>>)|(?:<<)|(?:>=)|(?:<=)|(?:\*=)|(?:IS))\s*('((?:[\s\S]+)?)'|(?:[\s\S]+)?))";
222222
regex ex(CONDITION_STR_PATTERN);
223223
if (regex_match(condition_str, what, ex)) {
224224
if (condition_str.size() != what[0].str().size()) {
@@ -238,7 +238,11 @@ bool DeleteHandler::_parse_condition(const std::string& condition_str, TConditio
238238
}
239239
condition->column_name = what[1].str();
240240
condition->condition_op = what[2].str();
241-
condition->condition_values.push_back(what[3].str());
241+
if (what[4].matched) { // match string with single quotes, eg. a = 'b'
242+
condition->condition_values.push_back(what[4].str());
243+
} else { // match string without quote, compat with old conditions, eg. a = b
244+
condition->condition_values.push_back(what[3].str());
245+
}
242246

243247
return true;
244248
}

‎be/test/olap/delete_handler_test.cpp‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,12 @@ TEST_F(TestDeleteConditionHandler, StoreCondSucceed) {
366366

367367
// 验证存储在header中的过滤条件正确
368368
EXPECT_EQ(size_t(6), del_pred.sub_predicates_size());
369-
EXPECT_STREQ("k1=1", del_pred.sub_predicates(0).c_str());
370-
EXPECT_STREQ("k2>>3", del_pred.sub_predicates(1).c_str());
371-
EXPECT_STREQ("k3<=5", del_pred.sub_predicates(2).c_str());
369+
EXPECT_STREQ("k1='1'", del_pred.sub_predicates(0).c_str());
370+
EXPECT_STREQ("k2>>'3'", del_pred.sub_predicates(1).c_str());
371+
EXPECT_STREQ("k3<='5'", del_pred.sub_predicates(2).c_str());
372372
EXPECT_STREQ("k4 IS NULL", del_pred.sub_predicates(3).c_str());
373-
EXPECT_STREQ("k5=7", del_pred.sub_predicates(4).c_str());
374-
EXPECT_STREQ("k12!=9", del_pred.sub_predicates(5).c_str());
373+
EXPECT_STREQ("k5='7'", del_pred.sub_predicates(4).c_str());
374+
EXPECT_STREQ("k12!='9'", del_pred.sub_predicates(5).c_str());
375375

376376
EXPECT_EQ(size_t(1), del_pred.in_predicates_size());
377377
EXPECT_FALSE(del_pred.in_predicates(0).is_not_in());
@@ -899,6 +899,36 @@ class TestDeleteHandler : public testing::Test {
899899
std::string _json_rowset_meta;
900900
};
901901

902+
TEST_F(TestDeleteHandler, ValueWithQuote) {
903+
DeletePredicatePB del_predicate;
904+
del_predicate.add_sub_predicates("k1='b'");
905+
del_predicate.add_sub_predicates("k1='b");
906+
del_predicate.add_sub_predicates("k1=b'");
907+
del_predicate.add_sub_predicates("k1=''b'");
908+
del_predicate.add_sub_predicates("k1='b''");
909+
del_predicate.add_sub_predicates("k1=''b''");
910+
del_predicate.set_version(2);
911+
912+
add_delete_predicate(del_predicate, 2);
913+
914+
auto res = _delete_handler.init(tablet->tablet_schema(), tablet->delete_predicates(), 5);
915+
EXPECT_EQ(Status::OK(), res);
916+
_delete_handler.finalize();
917+
}
918+
919+
TEST_F(TestDeleteHandler, ValueWithoutQuote) {
920+
DeletePredicatePB del_predicate;
921+
del_predicate.add_sub_predicates("k1=b");
922+
del_predicate.add_sub_predicates("k2=123");
923+
del_predicate.set_version(2);
924+
925+
add_delete_predicate(del_predicate, 2);
926+
927+
auto res = _delete_handler.init(tablet->tablet_schema(), tablet->delete_predicates(), 5);
928+
EXPECT_EQ(Status::OK(), res);
929+
_delete_handler.finalize();
930+
}
931+
902932
TEST_F(TestDeleteHandler, InitSuccess) {
903933
Status res;
904934
std::vector<TCondition> conditions;

‎regression-test/data/delete_p0/test_delete.out‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,48 @@ abcdef 2022-08-12 2022-08-16T12:11:11 2022-08-16T12:11:11.111 2022-08-12 2022-08
4545

4646
-- !sql10 --
4747

48+
-- !check_data --
49+
1
50+
2
51+
3
52+
4
53+
abc 5
54+
'd 6
55+
'e' 7
56+
f' 8
57+
58+
-- !check_data2 --
59+
1
60+
3
61+
4
62+
abc 5
63+
'd 6
64+
'e' 7
65+
f' 8
66+
67+
-- !check_data3 --
68+
1
69+
3
70+
abc 5
71+
'd 6
72+
'e' 7
73+
f' 8
74+
75+
-- !check_data4 --
76+
1
77+
3
78+
abc 5
79+
'e' 7
80+
f' 8
81+
82+
-- !check_data5 --
83+
1
84+
3
85+
abc 5
86+
f' 8
87+
88+
-- !check_data6 --
89+
1
90+
3
91+
abc 5
92+

‎regression-test/suites/delete_p0/test_delete.groovy‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,63 @@ suite("test_delete") {
118118
qt_sql9 """select * from tb_test1;"""
119119
sql """ delete from tb_test1 where DT = '20221001'; """
120120
qt_sql10 """select * from tb_test1;"""
121+
122+
sql """ DROP TABLE IF EXISTS delete_test_tb """
123+
sql """
124+
CREATE TABLE `delete_test_tb` (
125+
`k1` varchar(30) NULL,
126+
`v1` varchar(30) NULL
127+
)
128+
UNIQUE KEY(`k1`)
129+
DISTRIBUTED BY HASH(`k1`) BUCKETS 4
130+
PROPERTIES
131+
(
132+
"replication_num" = "1"
133+
);
134+
"""
135+
136+
sql """
137+
insert into delete_test_tb values
138+
(' ', '1'), (' ', '2'), (' ', '3'), (' ', '4'),
139+
('abc', '5'), ("'d", '6'), ("'e'", '7'), ("f'", '8');
140+
"""
141+
142+
qt_check_data """
143+
select k1, v1 from delete_test_tb order by v1;
144+
"""
145+
146+
sql """
147+
delete from delete_test_tb where k1 = ' ';
148+
"""
149+
qt_check_data2 """
150+
select k1, v1 from delete_test_tb order by v1;
151+
"""
152+
153+
sql """
154+
delete from delete_test_tb where k1 = ' ';
155+
"""
156+
qt_check_data3 """
157+
select k1, v1 from delete_test_tb order by v1;
158+
"""
159+
160+
sql """
161+
delete from delete_test_tb where k1 = "'d";
162+
"""
163+
qt_check_data4 """
164+
select k1, v1 from delete_test_tb order by v1;
165+
"""
166+
167+
sql """
168+
delete from delete_test_tb where k1 = "'e'";
169+
"""
170+
qt_check_data5 """
171+
select k1, v1 from delete_test_tb order by v1;
172+
"""
173+
174+
sql """
175+
delete from delete_test_tb where k1 = "f'";
176+
"""
177+
qt_check_data6 """
178+
select k1, v1 from delete_test_tb order by v1;
179+
"""
121180
}

0 commit comments

Comments
 (0)