ClickHouse/dbms/src/Dictionaries/RedisBlockInputStream.cpp

213 lines
7.7 KiB
C++
Raw Normal View History

2019-09-17 14:16:07 +00:00
#include "RedisBlockInputStream.h"
2019-09-17 14:55:09 +00:00
2019-01-15 22:08:56 +00:00
#if USE_POCO_REDIS
# include <string>
# include <vector>
# include <Poco/Redis/Array.h>
# include <Poco/Redis/Client.h>
# include <Poco/Redis/Command.h>
# include <Poco/Redis/Type.h>
# include <Columns/ColumnNullable.h>
# include <Columns/ColumnString.h>
# include <Columns/ColumnsNumber.h>
# include <IO/ReadHelpers.h>
# include <IO/WriteHelpers.h>
# include <ext/range.h>
2019-05-30 21:16:12 +00:00
2019-01-15 22:08:56 +00:00
# include "DictionaryStructure.h"
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;
2019-01-15 22:08:56 +00:00
}
RedisBlockInputStream::RedisBlockInputStream(
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_)
: 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);
}
RedisBlockInputStream::~RedisBlockInputStream() = default;
namespace
{
using ValueType = ExternalResultDescription::ValueType;
2019-05-25 00:28:09 +00:00
template <typename T>
inline void insert(IColumn & column, const String & stringValue)
2019-01-15 22:08:56 +00:00
{
assert_cast<ColumnVector<T> &>(column).insertValue(parse<T>(stringValue));
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())
throw Exception{"Type mismatch, expected not Null String", ErrorCodes::TYPE_MISMATCH};
2019-04-17 01:11:40 +00:00
String stringValue = bulk_string.value();
2019-01-15 22:08:56 +00:00
switch (type)
{
2019-09-12 14:48:28 +00:00
case ValueType::vtUInt8:
2019-05-25 00:28:09 +00:00
insert<UInt8>(column, stringValue);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtUInt16:
2019-05-25 00:28:09 +00:00
insert<UInt16>(column, stringValue);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtUInt32:
2019-05-25 00:28:09 +00:00
insert<UInt32>(column, stringValue);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtUInt64:
2019-05-25 00:28:09 +00:00
insert<UInt64>(column, stringValue);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtInt8:
2019-05-25 00:28:09 +00:00
insert<Int8>(column, stringValue);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtInt16:
2019-05-25 00:28:09 +00:00
insert<Int16>(column, stringValue);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtInt32:
2019-05-25 00:28:09 +00:00
insert<Int32>(column, stringValue);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtInt64:
2019-05-25 00:28:09 +00:00
insert<Int64>(column, stringValue);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtFloat32:
2019-05-25 00:28:09 +00:00
insert<Float32>(column, stringValue);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtFloat64:
2019-05-25 00:28:09 +00:00
insert<Float64>(column, stringValue);
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtString:
assert_cast<ColumnString &>(column).insert(parse<String>(stringValue));
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtDate:
assert_cast<ColumnUInt16 &>(column).insertValue(parse<LocalDate>(stringValue).getDayNum());
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtDateTime:
assert_cast<ColumnUInt32 &>(column).insertValue(static_cast<UInt32>(parse<LocalDateTime>(stringValue)));
2019-01-15 22:08:56 +00:00
break;
2019-09-12 14:48:28 +00:00
case ValueType::vtUUID:
assert_cast<ColumnUInt128 &>(column).insertValue(parse<UUID>(stringValue));
2019-01-15 22:08:56 +00:00
break;
}
}
}
Block RedisBlockInputStream::readImpl()
{
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
for (const auto i : ext::range(0, size))
columns[i] = description.sample_block.getByPosition(i).column->cloneEmpty();
2019-01-27 13:14:02 +00:00
const auto insertValueByIdx = [this, &columns](size_t idx, const auto & value)
{
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)
{
throw Exception{"Too low keys in request to source: " + DB::toString(keys_array.size())
+ ", expected 2 or more", ErrorCodes::LOGICAL_ERROR};
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");
for (auto it = keys_array.begin(); it != keys_array.end(); ++it)
command_for_values.addRedisType(*it);
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
2019-05-30 21:06:39 +00:00
throw Exception{"Inconsistent sizes of keys and values in Redis request",
ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH};
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
{
insertValueByIdx(0, primary_key);
insertValueByIdx(1, secondary_key);
2019-04-16 23:13:07 +00:00
insertValueByIdx(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)
throw Exception{"Inconsistent sizes of keys and values in Redis request", ErrorCodes::INTERNAL_REDIS_ERROR};
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())
{
insertValueByIdx(0, key);
insertValueByIdx(1, value);
}
2019-04-16 23:13:07 +00:00
}
cursor += need_values;
2019-01-15 22:08:56 +00:00
}
return description.sample_block.cloneWithColumns(std::move(columns));
}
}
#endif