Skip to content

Feature: support ClickHouse query compatible mode #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Common/ClickHouseCompatibleFlag.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <Common/ClickHouseCompatibleFlag.h>

#include <thread>

namespace DB
{

namespace
{
/// The SQL receiving / parsing thread can set this flag to true to enable Timeplus to run ClickHouse SQL
thread_local bool is_clickhouse_compatible_mode_ = false;
}

bool isClickHouseCompatibleMode()
{
return is_clickhouse_compatible_mode_;
}

void setClickHouseCompatibleMode(bool is_clickhouse_compatible)
{
is_clickhouse_compatible_mode_ = is_clickhouse_compatible;
}

}
7 changes: 7 additions & 0 deletions src/Common/ClickHouseCompatibleFlag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace DB
{
bool isClickHouseCompatibleMode();
void setClickHouseCompatibleMode(bool is_clickhouse_compatible);
}
17 changes: 17 additions & 0 deletions src/Core/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <base/sort.h>
#include <boost/algorithm/string.hpp>

/// proton: starts
#include <DataTypes/convertToClickHouseType.h>
/// proton: ends


namespace DB
{
Expand Down Expand Up @@ -669,6 +673,19 @@ Names Block::getDataTypeNames() const
return res;
}

Names Block::getClickHouseDataTypeNames() const
{
Names res;
res.reserve(columns());

for (const auto & elem : data)
res.push_back(elem.type->getName());

std::transform(res.begin(), res.end(), res.begin(), convertToClickHouseType);

return res;
}

std::unordered_map<String, size_t> Block::getNamesToIndexesMap() const
{
return index_by_name;
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class Block
Names getNames() const;
DataTypes getDataTypes() const;
Names getDataTypeNames() const;
Names getClickHouseDataTypeNames() const; /// Added by proton to get ClickHouse compatible data types
std::unordered_map<String, size_t> getNamesToIndexesMap() const;

/// Returns number of rows from first column in block, not equal to nullptr. If no columns, returns 0.
size_t rows() const;
Expand Down
1 change: 1 addition & 0 deletions src/Core/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ static constexpr UInt64 operator""_GiB(unsigned long long value)
\
/** proton: starts */ \
M(String, shards, "", "If not empty, only the specified shard IDs (or partition IDs if the target stream is a Kafka external stream) will be selected to be read data from. IDs are separated by comma. Example: shards='0,2'", 0) \
M(Bool, is_clickhouse_compatible, false, "When this setting is set to true, timeplus can run ClickHouse SQL", 0) \
\
/** proton: ends */ \

Expand Down
10 changes: 10 additions & 0 deletions src/DataTypes/convertToClickHouseType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <string.h>

namespace DB
{

/// Convert Timeplus (complex / compound) type names to ClickHouse data type names (recursively)
std::string convertToClickHouseType(const std::string & type_name);
}
120 changes: 120 additions & 0 deletions src/DataTypes/convertToClickhouseType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <DataTypes/convertToClickHouseType.h>

#include <absl/container/flat_hash_map.h>

namespace DB
{

namespace
{
const absl::flat_hash_map<std::string, std::string> type_map
= {{"void", "Void"},
{"bool", "Bool"},
{"int8", "Int8"},
{"int16", "Int16"},
{"int32", "Int32"},
{"int64", "Int64"},
{"uint8", "UInt8"},
{"uint16", "UInt16"},
{"uint32", "UInt32"},
{"uint64", "UInt64"},
{"int128", "Int128"},
{"uint128", "UInt128"},
{"int256", "Int256"},
{"uint256", "UInt256"},
{"float32", "Float32"},
{"float64", "Float64"},
{"string", "String"},
{"json", "JSON"},
{"fixed_string", "FixedString"},
{"datetime", "DateTime"},
{"date", "Date"},
{"date32", "Date32"},
{"datetime64", "DateTime64"},
{"array", "Array"},
{"nullable", "Nullable"},
{"tuple", "Tuple"},
{"enum", "Enum"},
{"enum8", "Enum8"},
{"enum16", "Enum16"},
{"uuid", "UUID"},
{"ipv4", "IPv4"},
{"ipv6", "IPv6"},
{"decimal", "Decimal"},
{"decimal32", "Decimal32"},
{"decimal64", "Decimal64"},
{"decimal128", "Decimal128"},
{"decimal256", "Decimal256"},
{"map", "Map"},
{"low_cardinality", "LowCardinality"},
{"point", "Point"},
{"ring", "Ring"},
{"polygon", "Polygon"},
{"multipolygon", "MultiPolygon"}};

/// This function converts the type name to uppercase
std::string convertToClickHouseType(const std::string_view input)
{
std::string result;
result.reserve(input.size() + 8);

const size_t input_size = input.size();
for (size_t i = 0; i < input_size;)
{
size_t start = i;

while (i < input_size && (std::isalnum(input[i]) || input[i] == '_'))
++i;

std::string_view type_name{input.data() + start, i - start};
if (auto it = type_map.find(type_name); it != type_map.end())
result += it->second;
else
result += type_name;

/// Processing parameter part: If there are brackets, continue splicing and parsing the content
if (i < input_size && input[i] == '(')
{
result += '(';

++i; /// Skip '('

int bracket_cnt = 1;
size_t param_start = i;

while (i < input_size && bracket_cnt > 0)
{
if (input[i] == '(')
++bracket_cnt;
else if (input[i] == ')')
--bracket_cnt;

++i;
}

/// Recursively process the parameters in parentheses
std::string_view params{input.data() + param_start, i - param_start - 1};
result += convertToClickHouseType(params);

result += ')';
}

/// Skip other non-alphanumeric characters
while (i < input_size && !std::isalnum(input[i]) && input[i] != '_')
{
result += input[i];
++i;
}
}

return result;
}

}

std::string convertToClickHouseType(const std::string & input)
{
return convertToClickHouseType(std::string_view{input});
}

}
Loading