Skip to content

Commit

Permalink
Implement Space function push down pingcap#5113
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhenhuan committed Jun 27, 2022
1 parent 73e708c commit 4275ae2
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dbms/src/Flash/Coprocessor/DAGUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ const std::unordered_map<tipb::ScalarFuncSig, String> scalar_func_map({
//{tipb::ScalarFuncSig::Right, "cast"},
{tipb::ScalarFuncSig::RpadUTF8, "rpadUTF8"},
{tipb::ScalarFuncSig::Rpad, "rpad"},
//{tipb::ScalarFuncSig::Space, "cast"},
{tipb::ScalarFuncSig::Space, "space"},
{tipb::ScalarFuncSig::Strcmp, "strcmp"},
{tipb::ScalarFuncSig::Substring2ArgsUTF8, "substringUTF8"},
{tipb::ScalarFuncSig::Substring3ArgsUTF8, "substringUTF8"},
Expand Down
47 changes: 47 additions & 0 deletions dbms/src/Functions/FunctionsString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4188,6 +4188,53 @@ class FunctionLength : public IFunction
private:
};

class FunctionSpace : public IFunction
{
public:
static constexpr auto name = "space";
FunctionSpace() = default;

static FunctionPtr create(const Context & /*context*/)
{
return std::make_shared<FunctionSpace>();
}

std::string getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }

DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (arguments.size() != 1)
throw Exception(
fmt::format("Number of arguments for function {} doesn't match: passed {}, should be 1.", getName(), arguments.size()),
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);

return std::make_shared<DataTypeString>();
}

void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) const override
{
const ColumnPtr c0_col = block.getByPosition(arguments[0]).column;

Field res_field;
const ColumnConst * column_space_num = checkAndGetColumn<ColumnConst>(c0_col.get());
Int64 space_num = 0;

if (column_space_num)
{
space_num = column_space_num->getInt(0);
}
auto col_res = ColumnString::create();
col_res->reserve(space_num);

std::string res_string(space_num, ' ');
col_res->insert(res_string);
block.getByPosition(result).column = std::move(col_res);
}

private:
};

class FunctionPosition : public IFunction
{
public:
Expand Down
74 changes: 74 additions & 0 deletions dbms/src/Functions/tests/gtest_strings_space.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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 <Columns/ColumnString.h>
#include <Columns/ColumnsNumber.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypesNumber.h>
#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 push
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <Poco/Types.h>

#pragma GCC diagnostic pop

namespace DB
{
namespace tests
{
class StringSpace : public DB::tests::FunctionTest
{
public:
static constexpr auto func_name = "space";

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

// test string and string
TEST_F(StringReverse, strAndStrTest)
try
{
ASSERT_COLUMN_EQ(
toVec({" ", "", " "}),
executeFunction(
func_name,
toVec({2, 0, 10})
));
}
CATCH

// test NULL
TEST_F(StringLength, nullTest)
{
ASSERT_COLUMN_EQ(
toVec({"", {}}),
executeFunction(
func_name,
toVec({"", {}})));
}

} // namespace tests
} // namespace DB
59 changes: 59 additions & 0 deletions tests/fullstack-test/expr/space.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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 char(20), b int, c double);

mysql> insert into test.t values('www.pingcap.com', 12345, 123.45);
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 space(40) from test.t;
+------------------------------------------+
| space(40) |
+------------------------------------------+
| |
| |
| |
+------------------------------------------+


mysql> select space('') from test.t;
+-----------+
| space('') |
+-----------+
| |
| |
| |
+-----------+

mysql> select space(NULL) from test.t;
+-------------+
| space(NULL) |
+-------------+
| NULL |
| NULL |
| NULL |
+-------------+

mysql> explain select space(40) from test.t;
+---------------------------+---------+--------------+---------------+----------------------------------------------------+
| id | estRows | task | access object | operator info |
+---------------------------+---------+--------------+---------------+----------------------------------------------------+
| TableReader_9 | 3.00 | root | | data:ExchangeSender_8 |
| └─ExchangeSender_8 | 3.00 | mpp[tiflash] | | ExchangeType: PassThrough |
| └─Projection_4 | 3.00 | mpp[tiflash] | | ->Column#3 |
| └─TableFullScan_7 | 3.00 | mpp[tiflash] | table:t | keep order:false, stats:pseudo |
+---------------------------+---------+--------------+---------------+----------------------------------------------------+

0 comments on commit 4275ae2

Please sign in to comment.