ClickHouse/src/Dictionaries/DictionarySourceHelpers.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
3.8 KiB
C++
Raw Normal View History

#include "DictionarySourceHelpers.h"
2017-05-25 19:21:57 +00:00
#include <Columns/ColumnsNumber.h>
#include <Core/ColumnWithTypeAndName.h>
#include <DataTypes/DataTypesNumber.h>
#include <IO/WriteHelpers.h>
#include "DictionaryStructure.h"
#include <Interpreters/Context.h>
#include <Core/Settings.h>
#include <Poco/Util/AbstractConfiguration.h>
2020-04-17 19:54:53 +00:00
#include <Common/SettingsChanges.h>
2017-05-25 19:21:57 +00:00
namespace DB
{
2021-02-28 21:04:05 +00:00
namespace ErrorCodes
{
extern const int SIZES_OF_COLUMNS_DOESNT_MATCH;
}
2017-05-25 19:21:57 +00:00
/// For simple key
2021-01-27 13:23:02 +00:00
Block blockForIds(
const DictionaryStructure & dict_struct,
const std::vector<UInt64> & ids)
2017-05-25 19:21:57 +00:00
{
auto column = ColumnUInt64::create(ids.size());
2017-05-25 19:21:57 +00:00
memcpy(column->getData().data(), ids.data(), ids.size() * sizeof(ids.front()));
2021-01-27 13:23:02 +00:00
Block block{{std::move(column), std::make_shared<DataTypeUInt64>(), (*dict_struct.id).name}};
return block;
2017-05-25 19:21:57 +00:00
}
/// For composite key
Block blockForKeys(
const DictionaryStructure & dict_struct,
const Columns & key_columns,
const std::vector<size_t> & requested_rows)
2017-05-25 19:21:57 +00:00
{
Block block;
2017-05-25 19:21:57 +00:00
for (size_t i = 0, size = key_columns.size(); i < size; ++i)
{
const ColumnPtr & source_column = key_columns[i];
size_t column_rows_size = source_column->size();
PaddedPODArray<UInt8> filter(column_rows_size, false);
for (size_t idx : requested_rows)
filter[idx] = true;
auto filtered_column = source_column->filter(filter, requested_rows.size());
block.insert({filtered_column, (*dict_struct.key)[i].type, (*dict_struct.key)[i].name});
}
2017-05-25 19:21:57 +00:00
return block;
2017-05-25 19:21:57 +00:00
}
2021-08-12 15:16:55 +00:00
SettingsChanges readSettingsFromDictionaryConfig(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix)
{
2021-08-12 15:16:55 +00:00
if (!config.has(config_prefix + ".settings"))
return {};
2020-04-17 19:54:53 +00:00
2021-08-12 15:16:55 +00:00
const auto prefix = config_prefix + ".settings";
2021-08-12 15:16:55 +00:00
Poco::Util::AbstractConfiguration::Keys config_keys;
config.keys(prefix, config_keys);
2020-04-17 19:54:53 +00:00
2021-08-12 15:16:55 +00:00
SettingsChanges changes;
2020-04-09 20:52:53 +00:00
2021-08-12 15:16:55 +00:00
for (const std::string & key : config_keys)
{
const auto value = config.getString(prefix + "." + key);
changes.emplace_back(key, value);
2020-04-17 19:54:53 +00:00
}
2021-08-12 15:16:55 +00:00
return changes;
}
ContextMutablePtr copyContextAndApplySettingsFromDictionaryConfig(
const ContextPtr & context, const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix)
{
auto context_copy = Context::createCopy(context);
auto changes = readSettingsFromDictionaryConfig(config, config_prefix);
context_copy->applySettingsChanges(changes);
return context_copy;
}
static Block transformHeader(Block header, Block block_to_add)
2021-02-28 21:04:05 +00:00
{
for (Int64 i = static_cast<Int64>(block_to_add.columns() - 1); i >= 0; --i)
header.insert(0, block_to_add.getByPosition(i).cloneEmpty());
2021-02-28 21:04:05 +00:00
return header;
}
TransformWithAdditionalColumns::TransformWithAdditionalColumns(
Block block_to_add_, const Block & header)
: ISimpleTransform(header, transformHeader(header, block_to_add_), true)
, block_to_add(std::move(block_to_add_))
2021-02-28 21:04:05 +00:00
{
}
2021-02-28 21:04:05 +00:00
void TransformWithAdditionalColumns::transform(Chunk & chunk)
{
if (chunk)
2021-02-28 21:04:05 +00:00
{
auto num_rows = chunk.getNumRows();
auto columns = chunk.detachColumns();
2021-02-28 21:04:05 +00:00
auto cut_block = block_to_add.cloneWithCutColumns(current_range_index, num_rows);
2021-02-28 21:04:05 +00:00
if (cut_block.rows() != num_rows)
2021-04-10 18:48:36 +00:00
throw Exception(ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH,
"Number of rows in block to add after cut must equal to number of rows in block from inner stream");
2021-02-28 21:04:05 +00:00
for (Int64 i = static_cast<Int64>(cut_block.columns() - 1); i >= 0; --i)
columns.insert(columns.begin(), cut_block.getByPosition(i).column);
2021-02-28 21:04:05 +00:00
current_range_index += num_rows;
chunk.setColumns(std::move(columns), num_rows);
2021-02-28 21:04:05 +00:00
}
}
String TransformWithAdditionalColumns::getName() const
2021-02-28 21:04:05 +00:00
{
return "TransformWithAdditionalColumns";
2017-05-25 19:21:57 +00:00
}
}