ClickHouse/src/Dictionaries/RedisSource.cpp

226 lines
8.2 KiB
C++
Raw Normal View History

2021-08-12 21:58:24 +00:00
#include "RedisSource.h"
2019-09-17 14:55:09 +00:00
#include <string>
#include <vector>
2019-01-15 22:08:56 +00:00
#include <Poco/Redis/Array.h>
#include <Poco/Redis/Client.h>
#include <Poco/Redis/Command.h>
#include <Poco/Redis/Type.h>
2019-01-15 22:08:56 +00:00
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnsNumber.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
2019-01-15 22:08:56 +00:00
#include "DictionaryStructure.h"
2019-01-15 22:08:56 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int TYPE_MISMATCH;
extern const int LOGICAL_ERROR;
2019-05-30 21:06:39 +00:00
extern const int NUMBER_OF_COLUMNS_DOESNT_MATCH;
extern const int INTERNAL_REDIS_ERROR;
extern const int UNKNOWN_TYPE;
2019-01-15 22:08:56 +00:00
}
RedisSource::RedisSource(
2019-01-27 22:22:18 +00:00
const std::shared_ptr<Poco::Redis::Client> & client_,
const RedisArray & keys_,
const RedisStorageType & storage_type_,
2019-01-15 22:08:56 +00:00
const DB::Block & sample_block,
2019-09-12 14:48:28 +00:00
const size_t max_block_size_)
: SourceWithProgress(sample_block)
, client(client_), keys(keys_), storage_type(storage_type_), max_block_size{max_block_size_}
2019-01-15 22:08:56 +00:00
{
description.init(sample_block);
}
RedisSource::~RedisSource() = default;
2019-01-15 22:08:56 +00:00
namespace
{
using ValueType = ExternalResultDescription::ValueType;
2019-05-25 00:28:09 +00:00
template <typename T>
2020-03-18 03:27:32 +00:00
inline void insert(IColumn & column, const String & string_value)
2019-01-15 22:08:56 +00:00
{
2020-03-18 03:27:32 +00:00
assert_cast<ColumnVector<T> &>(column).insertValue(parse<T>(string_value));
2019-05-25 00:28:09 +00:00
}
2019-01-27 22:22:18 +00:00
void insertValue(IColumn & column, const ValueType type, const Poco::Redis::BulkString & bulk_string)
2019-05-25 00:28:09 +00:00
{
if (bulk_string.isNull())
2021-04-10 18:48:36 +00:00
throw Exception(ErrorCodes::TYPE_MISMATCH, "Type mismatch, expected not Null String");
2019-04-17 01:11:40 +00:00
2020-03-18 03:27:32 +00:00
const String & string_value = bulk_string.value();
2019-01-15 22:08:56 +00:00
switch (type)
{
2019-09-12 14:48:28 +00:00
case ValueType::vtUInt8:
2020-03-18 03:27:32 +00:00
insert<UInt8>(column, string_value);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtUInt16:
2020-03-18 03:27:32 +00:00
insert<UInt16>(column, string_value);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtUInt32:
2020-03-18 03:27:32 +00:00
insert<UInt32>(column, string_value);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtUInt64:
2020-03-18 03:27:32 +00:00
insert<UInt64>(column, string_value);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtInt8:
2020-03-18 03:27:32 +00:00
insert<Int8>(column, string_value);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtInt16:
2020-03-18 03:27:32 +00:00
insert<Int16>(column, string_value);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtInt32:
2020-03-18 03:27:32 +00:00
insert<Int32>(column, string_value);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtInt64:
2020-03-18 03:27:32 +00:00
insert<Int64>(column, string_value);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtFloat32:
2020-03-18 03:27:32 +00:00
insert<Float32>(column, string_value);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtFloat64:
2020-03-18 03:27:32 +00:00
insert<Float64>(column, string_value);
2019-01-15 22:08:56 +00:00
break;
case ValueType::vtEnum8:
case ValueType::vtEnum16:
2019-09-12 14:48:28 +00:00
case ValueType::vtString:
2020-03-18 03:27:32 +00:00
assert_cast<ColumnString &>(column).insert(parse<String>(string_value));
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtDate:
2020-03-18 03:27:32 +00:00
assert_cast<ColumnUInt16 &>(column).insertValue(parse<LocalDate>(string_value).getDayNum());
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtDateTime:
2021-03-15 19:23:27 +00:00
{
ReadBufferFromString in(string_value);
time_t time = 0;
readDateTimeText(time, in);
2021-03-15 20:40:33 +00:00
if (time < 0)
time = 0;
2021-03-15 19:23:27 +00:00
assert_cast<ColumnUInt32 &>(column).insertValue(time);
2019-01-15 22:08:56 +00:00
break;
2021-03-15 19:23:27 +00:00
}
2019-09-12 14:48:28 +00:00
case ValueType::vtUUID:
2021-05-03 22:59:38 +00:00
assert_cast<ColumnUUID &>(column).insertValue(parse<UUID>(string_value));
2019-01-15 22:08:56 +00:00
break;
default:
2021-04-10 18:48:36 +00:00
throw Exception(ErrorCodes::UNKNOWN_TYPE,
"Value of unsupported type: {}",
column.getName());
2019-01-15 22:08:56 +00:00
}
}
}
Chunk RedisSource::generate()
2019-01-15 22:08:56 +00:00
{
if (keys.isNull() || description.sample_block.rows() == 0 || cursor >= keys.size())
all_read = true;
2019-01-15 22:08:56 +00:00
if (all_read)
return {};
2019-04-16 23:13:07 +00:00
const size_t size = description.sample_block.columns();
2019-05-30 21:16:12 +00:00
MutableColumns columns(size);
2019-01-15 22:08:56 +00:00
2021-06-15 19:55:21 +00:00
for (size_t i = 0; i < size; ++i)
2019-01-15 22:08:56 +00:00
columns[i] = description.sample_block.getByPosition(i).column->cloneEmpty();
const auto insert_value_by_idx = [this, &columns](size_t idx, const auto & value)
2019-01-27 13:14:02 +00:00
{
if (description.types[idx].second)
{
ColumnNullable & column_nullable = static_cast<ColumnNullable &>(*columns[idx]);
insertValue(column_nullable.getNestedColumn(), description.types[idx].first, value);
2019-01-27 13:14:02 +00:00
column_nullable.getNullMapData().emplace_back(0);
}
else
insertValue(*columns[idx], description.types[idx].first, value);
2019-01-27 13:14:02 +00:00
};
if (storage_type == RedisStorageType::HASH_MAP)
2019-01-15 22:08:56 +00:00
{
2019-04-16 23:13:07 +00:00
size_t num_rows = 0;
for (; cursor < keys.size(); ++cursor)
2019-02-13 00:05:43 +00:00
{
const auto & keys_array = keys.get<RedisArray>(cursor);
2019-04-16 23:13:07 +00:00
if (keys_array.size() < 2)
{
2021-04-10 18:48:36 +00:00
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Too low keys in request to source: {}, expected 2 or more",
DB::toString(keys_array.size()));
2019-04-16 23:13:07 +00:00
}
2019-04-16 23:13:07 +00:00
if (num_rows + keys_array.size() - 1 > max_block_size)
break;
2019-01-15 22:08:56 +00:00
Poco::Redis::Command command_for_values("HMGET");
2020-03-09 03:14:24 +00:00
for (const auto & elem : keys_array)
command_for_values.addRedisType(elem);
auto values = client->execute<RedisArray>(command_for_values);
2019-04-16 23:13:07 +00:00
2019-06-02 01:22:06 +00:00
if (keys_array.size() != values.size() + 1) // 'HMGET' primary_key secondary_keys
2021-04-10 18:48:36 +00:00
throw Exception(ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH,
"Inconsistent sizes of keys and values in Redis request");
2019-05-30 21:06:39 +00:00
const auto & primary_key = keys_array.get<RedisBulkString>(0);
2019-05-30 21:06:39 +00:00
for (size_t i = 0; i < values.size(); ++i)
2019-04-16 23:13:07 +00:00
{
const auto & secondary_key = keys_array.get<RedisBulkString>(i + 1);
const auto & value = values.get<RedisBulkString>(i);
/// null string means 'no value for requested key'
if (!value.isNull())
2019-05-30 21:06:39 +00:00
{
insert_value_by_idx(0, primary_key);
insert_value_by_idx(1, secondary_key);
insert_value_by_idx(2, value);
2019-05-30 21:06:39 +00:00
++num_rows;
}
2019-04-16 23:13:07 +00:00
}
}
2019-01-27 22:22:18 +00:00
}
2019-04-16 23:13:07 +00:00
else
{
Poco::Redis::Command command_for_values("MGET");
size_t need_values = std::min(max_block_size, keys.size() - cursor);
for (size_t i = 0; i < need_values; ++i)
command_for_values.add(keys.get<RedisBulkString>(cursor + i));
2019-05-30 21:06:39 +00:00
auto values = client->execute<RedisArray>(command_for_values);
if (values.size() != need_values)
2021-04-10 18:48:36 +00:00
throw Exception(ErrorCodes::INTERNAL_REDIS_ERROR,
"Inconsistent sizes of keys and values in Redis request");
2019-05-30 21:06:39 +00:00
for (size_t i = 0; i < values.size(); ++i)
{
const auto & key = keys.get<RedisBulkString>(cursor + i);
const auto & value = values.get<RedisBulkString>(i);
/// Null string means 'no value for requested key'
if (!value.isNull())
{
insert_value_by_idx(0, key);
insert_value_by_idx(1, value);
}
2019-04-16 23:13:07 +00:00
}
cursor += need_values;
2019-01-15 22:08:56 +00:00
}
size_t num_rows = columns.at(0)->size();
return Chunk(std::move(columns), num_rows);
2019-01-15 22:08:56 +00:00
}
}