Skip to content

Commit

Permalink
format codes
Browse files Browse the repository at this point in the history
Signed-off-by: YangKeao <yangkeao@chunibyo.icu>
  • Loading branch information
YangKeao committed Jul 3, 2022
1 parent 2f6d92b commit dfff46b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
39 changes: 23 additions & 16 deletions dbms/src/Functions/FunctionsString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4910,7 +4910,8 @@ class FunctionFormatWithLocale : public IFunction
}
};

class FunctionHexStr : public IFunction {
class FunctionHexStr : public IFunction
{
public:
static constexpr auto name = "hexStr";
FunctionHexStr() = default;
Expand All @@ -4928,23 +4929,27 @@ class FunctionHexStr : public IFunction {
{
if (!arguments[0]->isString())
throw Exception(
fmt::format("Illegal type {} of first argument of function {}", arguments[0]->getName(), getName()),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
fmt::format("Illegal type {} of first argument of function {}", arguments[0]->getName(), getName()),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return std::make_shared<DataTypeString>();
}

void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) const override
{
const ColumnPtr & column = block.getByPosition(arguments[0]).column;
if (const auto * col = checkAndGetColumn<ColumnString>(column.get())) {
if (const auto * col = checkAndGetColumn<ColumnString>(column.get()))
{
auto col_res = ColumnString::create();
vector(col->getChars(), col->getOffsets(), col_res->getChars(), col_res->getOffsets());
block.getByPosition(result).column = std::move(col_res);
} else if (const auto * col = checkAndGetColumn<ColumnFixedString>(column.get())) {
}
else if (const auto * col = checkAndGetColumn<ColumnFixedString>(column.get()))
{
auto col_res = ColumnFixedString::create(col->getN() * 2);
vectorFixed(col->getChars(), col->getN(), col_res->getChars());
block.getByPosition(result).column = std::move(col_res);
} else
}
else
throw Exception(
fmt::format("Illegal column {} of argument of function {}", block.getByPosition(arguments[0]).column->getName(), getName()),
ErrorCodes::ILLEGAL_COLUMN);
Expand All @@ -4964,9 +4969,9 @@ class FunctionHexStr : public IFunction {
res_offsets.resize(size);

ColumnString::Offset prev_offset = 0;
for(size_t i = 0; i < size;++i)
for (size_t i = 0; i < size; ++i)
{
for (size_t j = prev_offset; j < offsets[i] - 1;++j)
for (size_t j = prev_offset; j < offsets[i] - 1; ++j)
{
ColumnString::Offset pos = j * 2 - i;
UInt8 byte = data[j];
Expand All @@ -4976,8 +4981,8 @@ class FunctionHexStr : public IFunction {
// the last element written by the previous loop is:
// `(offsets[i] - 2) * 2 - i + 1 = offsets[2] * 2 - i - 3`
// then the zero should be written to `offsets[2] * 2 - i - 2`
res_data[offsets[i]*2 - i - 2] = 0;
res_offsets[i] = offsets[i]*2 - i - 1;
res_data[offsets[i] * 2 - i - 2] = 0;
res_offsets[i] = offsets[i] * 2 - i - 1;

prev_offset = offsets[i];
}
Expand All @@ -4989,7 +4994,7 @@ class FunctionHexStr : public IFunction {
res_data.resize(data.size() * 2);

for (size_t i = 0; i < size; ++i)
for (size_t j = i * length; j < (i+1) * length; ++j)
for (size_t j = i * length; j < (i + 1) * length; ++j)
{
ColumnString::Offset pos = j * 2;
UInt8 byte = data[j];
Expand All @@ -4999,7 +5004,8 @@ class FunctionHexStr : public IFunction {
}
};

class FunctionHexInt : public IFunction {
class FunctionHexInt : public IFunction
{
public:
static constexpr auto name = "hexInt";
FunctionHexInt() = default;
Expand All @@ -5017,8 +5023,8 @@ class FunctionHexInt : public IFunction {
{
if (!arguments[0]->isNumber())
throw Exception(
fmt::format("Illegal type {} of first argument of function {}", arguments[0]->getName(), getName()),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
fmt::format("Illegal type {} of first argument of function {}", arguments[0]->getName(), getName()),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return std::make_shared<DataTypeString>();
}

Expand All @@ -5040,6 +5046,7 @@ class FunctionHexInt : public IFunction {
throw Exception(fmt::format("Illegal argument of function {}", getName()), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
}

private:
template <typename IntType>
bool executeHexInt(
Expand All @@ -5064,11 +5071,11 @@ class FunctionHexInt : public IFunction {
res_offsets.resize(size);

size_t prev_res_offset = 0;
for(size_t i=0;i<size;++i)
for (size_t i = 0; i < size; ++i)
{
UInt64 number = col->getUInt(i);

int print_size = sprintf(reinterpret_cast<char*>(&res_chars[prev_res_offset]), "%lX", number);
int print_size = sprintf(reinterpret_cast<char *>(&res_chars[prev_res_offset]), "%lX", number);
res_chars[prev_res_offset + print_size] = 0;
// Add the size of printed string and a tailing zero
prev_res_offset += print_size + 1;
Expand Down
30 changes: 14 additions & 16 deletions dbms/src/Functions/tests/gtest_strings_hexint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,37 @@
#include <vector>


namespace DB {
namespace tests {
class HexIntTest : public DB::tests::FunctionTest {
namespace DB
{
namespace tests
{
class HexIntTest : public DB::tests::FunctionTest
{
};

TEST_F(HexIntTest, hexint_all_unit_Test)
try {
const String &func_name = "hexInt";
try
{
const String & func_name = "hexInt";

ASSERT_COLUMN_EQ(
createColumn<Nullable<UInt64>>({std::nullopt, 12345, 0xFFFFFFFFFFFFFFFF}),
executeFunction(
func_name,
createColumn<Nullable<String>>({std::nullopt, "3039", "FFFFFFFFFFFFFFFF"})
)
);
createColumn<Nullable<String>>({std::nullopt, "3039", "FFFFFFFFFFFFFFFF"})));

ASSERT_COLUMN_EQ(
createColumn<Nullable<Int64>>({12345, -12345}),
executeFunction(
func_name,
createColumn<Nullable<String>>({"3039", "FFFFFFFFFFFFCFC7"})
)
);
createColumn<Nullable<String>>({"3039", "FFFFFFFFFFFFCFC7"})));

ASSERT_COLUMN_EQ(
createColumn<Nullable<Int8>>({255, 1}),
executeFunction(
func_name,
createColumn<Nullable<String>>({"FF", "1"})
)
);
createColumn<Nullable<String>>({"FF", "1"})));
}
CATCH
}
}
} // namespace tests
} // namespace DB
22 changes: 12 additions & 10 deletions dbms/src/Functions/tests/gtest_strings_hexstr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@
#include <vector>


namespace DB {
namespace tests {
class HexStrTest : public DB::tests::FunctionTest {
namespace DB
{
namespace tests
{
class HexStrTest : public DB::tests::FunctionTest
{
};

TEST_F(HexStrTest, hexstr_all_unit_Test)
try {
const String &func_name = "hexStr";
try
{
const String & func_name = "hexStr";

ASSERT_COLUMN_EQ(
createColumn<Nullable<String>>({"7777772E70696E676361702E636F6D", "61626364", "E6B58BE8AF95E6B58BE8AF95E6B58BE8AF95E6B58BE8AF9561626364E6B58BE8AF95", std::nullopt}),
executeFunction(
func_name,
createColumn<Nullable<String>>({"www.pingcap.com", "abcd", "测试测试测试测试abcd测试", std::nullopt})
)
);
createColumn<Nullable<String>>({"www.pingcap.com", "abcd", "测试测试测试测试abcd测试", std::nullopt})));
}
CATCH
}
}
} // namespace tests
} // namespace DB

0 comments on commit dfff46b

Please sign in to comment.