Skip to content

Commit

Permalink
Implement ReverseUTF8/Reverse function push down (pingcap#5233)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhenhuan authored Jul 7, 2022
1 parent 3e8df4b commit 5295223
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dbms/src/Flash/Coprocessor/DAGUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,8 @@ const std::unordered_map<tipb::ScalarFuncSig, String> scalar_func_map({
//{tipb::ScalarFuncSig::Quote, "cast"},
//{tipb::ScalarFuncSig::Repeat, "cast"},
{tipb::ScalarFuncSig::Replace, "replaceAll"},
//{tipb::ScalarFuncSig::ReverseUTF8, "cast"},
//{tipb::ScalarFuncSig::Reverse, "cast"},
{tipb::ScalarFuncSig::ReverseUTF8, "reverseUTF8"},
{tipb::ScalarFuncSig::Reverse, "reverse"},
{tipb::ScalarFuncSig::RightUTF8, "rightUTF8"},
//{tipb::ScalarFuncSig::Right, "cast"},
{tipb::ScalarFuncSig::RpadUTF8, "rpadUTF8"},
Expand Down
120 changes: 120 additions & 0 deletions dbms/src/Functions/tests/gtest_strings_reverse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2022 PingCAP, Ltd.
//
// 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.

#include <Functions/FunctionFactory.h>
#include <Functions/FunctionsString.h>
#include <Interpreters/Context.h>
#include <TestUtils/FunctionTestUtils.h>
#include <TestUtils/TiFlashTestBasic.h>

#include <string>
#include <vector>

#pragma GCC diagnostic pop

namespace DB::tests
{
class StringReverse : public DB::tests::FunctionTest
{
protected:
static ColumnWithTypeAndName toVec(const std::vector<String> & v)
{
return createColumn<String>(v);
}

static ColumnWithTypeAndName toNullableVec(const std::vector<std::optional<String>> & v)
{
return createColumn<Nullable<String>>(v);
}

static ColumnWithTypeAndName toConst(const String & s)
{
return createConstColumn<String>(1, s);
}
};
// test reverse
TEST_F(StringReverse, stringReverseTest)
try
{
std::vector<String> candidate_strings = {"one week's time test", "abcdef", "abcabc", "moc.pacgnip"};
std::vector<String> reversed_strings = {"tset emit s'keew eno", "fedcba", "cbacba", "pingcap.com"};

// test vector
ASSERT_COLUMN_EQ(
toVec(reversed_strings),
executeFunction(
"reverse",
toVec(candidate_strings)));

// test nullable
ASSERT_COLUMN_EQ(
toNullableVec({"", " ", {}, "pacgnip"}),
executeFunction(
"reverse",
toNullableVec({"", " ", {}, "pingcap"})));

// test const
ASSERT_COLUMN_EQ(
toConst("pacgnip"),
executeFunction(
"reverse",
toConst("pingcap")));

// test null
ASSERT_COLUMN_EQ(
toConst({}),
executeFunction(
"reverse",
toConst({})));
}
CATCH

// test reverseUTF8
TEST_F(StringReverse, stringReverseUTF8Test)
try
{
std::vector<String> candidate_strings = {"one week's time test", "abc测试def", "abcテストabc", "ѐёђѓєѕіїјљњћќѝўџ", "+ѐ-ё*ђ/ѓ!є@ѕ#і$@ї%ј……љ&њ(ћ)ќ¥ѝ#ў@џ!^", "αβγδεζηθικλμνξοπρστυφχψωσ", "▲α▼βγ➨δε☎ζη✂θι€κλ♫μν✓ξο✚πρ℉στ♥υφ♖χψ♘ω★σ✕", "թփձջրչճժծքոեռտըւիօպասդֆգհյկլխզղցվբնմշ"};
std::vector<String> reversed_strings = {"tset emit s'keew eno", "fed试测cba", "cbaトステcba", "џўѝќћњљјїіѕєѓђёѐ", "^!џ@ў#ѝ¥ќ)ћ(њ&љ……ј%ї@$і#ѕ@є!ѓ/ђ*ё-ѐ+", "σωψχφυτσρποξνμλκιθηζεδγβα", "✕σ★ω♘ψχ♖φυ♥τσ℉ρπ✚οξ✓νμ♫λκ€ιθ✂ηζ☎εδ➨γβ▼α▲", "շմնբվցղզխլկյհգֆդսապօիւըտռեոքծժճչրջձփթ"};

// test vector
ASSERT_COLUMN_EQ(
toVec(reversed_strings),
executeFunction(
"reverseUTF8",
toVec(candidate_strings)));

// test nullable
ASSERT_COLUMN_EQ(
toNullableVec({"", " ", {}, "pacgnip"}),
executeFunction(
"reverseUTF8",
toNullableVec({"", " ", {}, "pingcap"})));

// test const
ASSERT_COLUMN_EQ(
toConst("pacgnip"),
executeFunction(
"reverseUTF8",
toConst("pingcap")));

// test null
ASSERT_COLUMN_EQ(
toConst({}),
executeFunction(
"reverseUTF8",
toConst({})));
}
CATCH

} // namespace DB::tests
44 changes: 44 additions & 0 deletions tests/fullstack-test/expr/reverse.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2022 PingCAP, Ltd.
#
# 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.

mysql> drop table if exists test.t;
mysql> create table if not exists test.t(a varchar(256));


mysql> insert into test.t values('one week’s time test');
mysql> insert into test.t values('abc测试def');
mysql> insert into test.t values('abcテストabc');
mysql> insert into test.t values('ѐёђѓєѕіїјљњћќѝўџ');
mysql> insert into test.t values('+ѐ-ё*ђ/ѓ!є@ѕ#і@ї%ј……љ&њ(ћ)ќ¥ѝ#ў@џ!^');
mysql> insert into test.t values('αβγδεζηθικλμνξοπρστυφχψωσ');
mysql> insert into test.t values('▲α▼βγ➨δε☎ζη✂θι€κλ♫μν✓ξο✚πρ℉στ♥υφ♖χψ♘ω★σ✕');
mysql> insert into test.t values('թփձջրչճժծքոեռտըւիօպասդֆգհյկլխզղցվբնմշ');
mysql> insert into test.t values(NULL);
mysql> alter table test.t set tiflash replica 1;
func> wait_table test t

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select reverse(a) from test.t;
+-------------------------------------------------------------------------------------------------+
| reverse(a) |
+-------------------------------------------------------------------------------------------------+
| tset emit s’keew eno |
| fed试测cba |
| cbaトステcba |
| џўѝќћњљјїіѕєѓђёѐ |
| ^!џ@ў#ѝ¥ќ)ћ(њ&љ……ј%ї@і#ѕ@є!ѓ/ђ*ё-ѐ+ |
| σωψχφυτσρποξνμλκιθηζεδγβα |
| ✕σ★ω♘ψχ♖φυ♥τσ℉ρπ✚οξ✓νμ♫λκ€ιθ✂ηζ☎εδ➨γβ▼α▲ |
| շմնբվցղզխլկյհգֆդսապօիւըտռեոքծժճչրջձփթ |
| NULL |
+-------------------------------------------------------------------------------------------------+

0 comments on commit 5295223

Please sign in to comment.