2017-09-11 17:55:41 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreePartition.h>
|
|
|
|
#include <Storages/MergeTree/MergeTreeData.h>
|
2019-10-10 16:30:30 +00:00
|
|
|
#include <Storages/MergeTree/IMergeTreeDataPart.h>
|
2017-09-11 17:55:41 +00:00
|
|
|
#include <IO/HashingWriteBuffer.h>
|
2017-11-24 13:55:31 +00:00
|
|
|
#include <Common/FieldVisitors.h>
|
2017-09-11 17:55:41 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
2018-11-26 12:41:17 +00:00
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
2017-09-11 17:55:41 +00:00
|
|
|
#include <Common/SipHash.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Common/hex.h>
|
2019-01-25 15:17:12 +00:00
|
|
|
#include <Core/Block.h>
|
2017-09-11 17:55:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-06-09 23:58:51 +00:00
|
|
|
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
2017-09-11 17:55:41 +00:00
|
|
|
|
2021-06-09 23:58:51 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
/// This is a special visitor which is used to get partition ID.
|
|
|
|
/// Calculate hash for UUID the same way as for UInt128.
|
|
|
|
/// It worked this way until 21.5, and we cannot change it,
|
|
|
|
/// or partition ID will be different in case UUID is used in partition key.
|
|
|
|
/// (It is not recommended to use UUID as partition key).
|
|
|
|
class LegacyFieldVisitorHash : public FieldVisitorHash
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using FieldVisitorHash::FieldVisitorHash;
|
|
|
|
using FieldVisitorHash::operator();
|
|
|
|
void operator() (const UUID & x) const { FieldVisitorHash::operator()(x.toUnderType()); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
static std::unique_ptr<ReadBufferFromFileBase> openForReading(const DiskPtr & disk, const String & path)
|
2017-09-11 17:55:41 +00:00
|
|
|
{
|
2020-02-27 16:47:40 +00:00
|
|
|
return disk->readFile(path, std::min(size_t(DBMS_DEFAULT_BUFFER_SIZE), disk->getFileSize(path)));
|
2017-09-11 17:55:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 15:17:12 +00:00
|
|
|
String MergeTreePartition::getID(const MergeTreeData & storage) const
|
|
|
|
{
|
2020-06-17 10:34:23 +00:00
|
|
|
return getID(storage.getInMemoryMetadataPtr()->getPartitionKey().sample_block);
|
2019-01-25 15:17:12 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 16:22:04 +00:00
|
|
|
/// NOTE: This ID is used to create part names which are then persisted in ZK and as directory names on the file system.
|
|
|
|
/// So if you want to change this method, be sure to guarantee compatibility with existing table data.
|
2019-01-25 15:17:12 +00:00
|
|
|
String MergeTreePartition::getID(const Block & partition_key_sample) const
|
2017-09-11 17:55:41 +00:00
|
|
|
{
|
2019-01-25 15:17:12 +00:00
|
|
|
if (value.size() != partition_key_sample.columns())
|
2017-09-11 17:55:41 +00:00
|
|
|
throw Exception("Invalid partition key size: " + toString(value.size()), ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
if (value.empty())
|
2017-09-13 16:22:04 +00:00
|
|
|
return "all"; /// It is tempting to use an empty string here. But that would break directory structure in ZK.
|
2017-09-11 17:55:41 +00:00
|
|
|
|
2017-09-13 16:22:04 +00:00
|
|
|
/// In case all partition fields are represented by integral types, try to produce a human-readable ID.
|
2017-09-11 17:55:41 +00:00
|
|
|
/// Otherwise use a hex-encoded hash.
|
|
|
|
bool are_all_integral = true;
|
|
|
|
for (const Field & field : value)
|
|
|
|
{
|
|
|
|
if (field.getType() != Field::Types::UInt64 && field.getType() != Field::Types::Int64)
|
|
|
|
{
|
|
|
|
are_all_integral = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String result;
|
|
|
|
|
|
|
|
if (are_all_integral)
|
|
|
|
{
|
|
|
|
FieldVisitorToString to_string_visitor;
|
|
|
|
for (size_t i = 0; i < value.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
result += '-';
|
|
|
|
|
2019-01-25 15:17:12 +00:00
|
|
|
if (typeid_cast<const DataTypeDate *>(partition_key_sample.getByPosition(i).type.get()))
|
2018-05-25 13:29:15 +00:00
|
|
|
result += toString(DateLUT::instance().toNumYYYYMMDD(DayNum(value[i].safeGet<UInt64>())));
|
2017-09-11 17:55:41 +00:00
|
|
|
else
|
|
|
|
result += applyVisitor(to_string_visitor, value[i]);
|
2017-09-13 16:22:04 +00:00
|
|
|
|
|
|
|
/// It is tempting to output DateTime as YYYYMMDDhhmmss, but that would make partition ID
|
|
|
|
/// timezone-dependent.
|
2017-09-11 17:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SipHash hash;
|
2021-06-09 13:51:40 +00:00
|
|
|
LegacyFieldVisitorHash hashing_visitor(hash);
|
2017-09-11 17:55:41 +00:00
|
|
|
for (const Field & field : value)
|
|
|
|
applyVisitor(hashing_visitor, field);
|
|
|
|
|
|
|
|
char hash_data[16];
|
|
|
|
hash.get128(hash_data);
|
|
|
|
result.resize(32);
|
|
|
|
for (size_t i = 0; i < 16; ++i)
|
|
|
|
writeHexByteLowercase(hash_data[i], &result[2 * i]);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-09 18:32:44 +00:00
|
|
|
void MergeTreePartition::serializeText(const MergeTreeData & storage, WriteBuffer & out, const FormatSettings & format_settings) const
|
2017-09-11 17:55:41 +00:00
|
|
|
{
|
2020-06-24 14:12:44 +00:00
|
|
|
auto metadata_snapshot = storage.getInMemoryMetadataPtr();
|
|
|
|
const auto & partition_key_sample = metadata_snapshot->getPartitionKey().sample_block;
|
2020-05-20 12:16:55 +00:00
|
|
|
size_t key_size = partition_key_sample.columns();
|
2017-09-11 17:55:41 +00:00
|
|
|
|
|
|
|
if (key_size == 0)
|
|
|
|
{
|
|
|
|
writeCString("tuple()", out);
|
|
|
|
}
|
2018-10-09 18:32:44 +00:00
|
|
|
else if (key_size == 1)
|
2017-09-11 17:55:41 +00:00
|
|
|
{
|
2020-05-20 12:16:55 +00:00
|
|
|
const DataTypePtr & type = partition_key_sample.getByPosition(0).type;
|
2017-12-15 20:48:46 +00:00
|
|
|
auto column = type->createColumn();
|
2018-10-09 18:32:44 +00:00
|
|
|
column->insert(value[0]);
|
2021-03-09 14:46:52 +00:00
|
|
|
type->getDefaultSerialization()->serializeText(*column, 0, out, format_settings);
|
2017-09-11 17:55:41 +00:00
|
|
|
}
|
2018-10-09 18:32:44 +00:00
|
|
|
else
|
|
|
|
{
|
2018-11-26 12:41:17 +00:00
|
|
|
DataTypes types;
|
|
|
|
Columns columns;
|
2018-10-09 18:32:44 +00:00
|
|
|
for (size_t i = 0; i < key_size; ++i)
|
|
|
|
{
|
2020-05-20 12:16:55 +00:00
|
|
|
const auto & type = partition_key_sample.getByPosition(i).type;
|
2018-11-26 12:41:17 +00:00
|
|
|
types.push_back(type);
|
2018-10-09 18:32:44 +00:00
|
|
|
auto column = type->createColumn();
|
|
|
|
column->insert(value[i]);
|
2018-11-26 12:41:17 +00:00
|
|
|
columns.push_back(std::move(column));
|
2018-10-09 18:32:44 +00:00
|
|
|
}
|
2017-09-11 17:55:41 +00:00
|
|
|
|
2021-03-09 14:46:52 +00:00
|
|
|
auto tuple_serialization = DataTypeTuple(types).getDefaultSerialization();
|
2018-11-26 12:41:17 +00:00
|
|
|
auto tuple_column = ColumnTuple::create(columns);
|
2021-03-09 14:46:52 +00:00
|
|
|
tuple_serialization->serializeText(*tuple_column, 0, out, format_settings);
|
2018-10-09 18:32:44 +00:00
|
|
|
}
|
2017-09-11 17:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
void MergeTreePartition::load(const MergeTreeData & storage, const DiskPtr & disk, const String & part_path)
|
2017-09-11 17:55:41 +00:00
|
|
|
{
|
2020-06-17 10:34:23 +00:00
|
|
|
auto metadata_snapshot = storage.getInMemoryMetadataPtr();
|
|
|
|
if (!metadata_snapshot->hasPartitionKey())
|
2017-09-11 17:55:41 +00:00
|
|
|
return;
|
|
|
|
|
2021-05-22 16:19:56 +00:00
|
|
|
const auto & partition_key_sample = adjustPartitionKey(metadata_snapshot, storage.getContext()).sample_block;
|
2020-02-27 16:47:40 +00:00
|
|
|
auto partition_file_path = part_path + "partition.dat";
|
|
|
|
auto file = openForReading(disk, partition_file_path);
|
2020-05-20 12:16:55 +00:00
|
|
|
value.resize(partition_key_sample.columns());
|
|
|
|
for (size_t i = 0; i < partition_key_sample.columns(); ++i)
|
2021-05-22 16:19:56 +00:00
|
|
|
partition_key_sample.getByPosition(i).type->getDefaultSerialization()->deserializeBinary(value[i], *file);
|
2017-09-11 17:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
void MergeTreePartition::store(const MergeTreeData & storage, const DiskPtr & disk, const String & part_path, MergeTreeDataPartChecksums & checksums) const
|
2017-09-11 17:55:41 +00:00
|
|
|
{
|
2020-06-24 16:06:01 +00:00
|
|
|
auto metadata_snapshot = storage.getInMemoryMetadataPtr();
|
2021-05-22 16:19:56 +00:00
|
|
|
const auto & partition_key_sample = adjustPartitionKey(metadata_snapshot, storage.getContext()).sample_block;
|
2020-06-24 16:06:01 +00:00
|
|
|
store(partition_key_sample, disk, part_path, checksums);
|
2019-01-25 15:17:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
void MergeTreePartition::store(const Block & partition_key_sample, const DiskPtr & disk, const String & part_path, MergeTreeDataPartChecksums & checksums) const
|
2019-01-25 15:17:12 +00:00
|
|
|
{
|
|
|
|
if (!partition_key_sample)
|
2017-09-11 17:55:41 +00:00
|
|
|
return;
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
auto out = disk->writeFile(part_path + "partition.dat");
|
|
|
|
HashingWriteBuffer out_hashing(*out);
|
2017-09-11 17:55:41 +00:00
|
|
|
for (size_t i = 0; i < value.size(); ++i)
|
2021-05-17 14:33:04 +00:00
|
|
|
partition_key_sample.getByPosition(i).type->getDefaultSerialization()->serializeBinary(value[i], out_hashing);
|
2021-05-15 18:32:20 +00:00
|
|
|
|
2017-10-24 14:11:53 +00:00
|
|
|
out_hashing.next();
|
2017-09-11 17:55:41 +00:00
|
|
|
checksums.files["partition.dat"].file_size = out_hashing.count();
|
|
|
|
checksums.files["partition.dat"].file_hash = out_hashing.getHash();
|
2020-10-06 09:38:00 +00:00
|
|
|
out->finalize();
|
2017-09-11 17:55:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 16:14:01 +00:00
|
|
|
void MergeTreePartition::create(const StorageMetadataPtr & metadata_snapshot, Block block, size_t row, ContextPtr context)
|
2020-05-05 15:06:16 +00:00
|
|
|
{
|
2020-06-26 11:30:23 +00:00
|
|
|
if (!metadata_snapshot->hasPartitionKey())
|
2020-06-03 13:27:54 +00:00
|
|
|
return;
|
|
|
|
|
2021-05-24 23:39:56 +00:00
|
|
|
auto partition_key_names_and_types = executePartitionByExpression(metadata_snapshot, block, context);
|
|
|
|
value.resize(partition_key_names_and_types.size());
|
2020-05-05 15:06:16 +00:00
|
|
|
|
2021-05-24 23:39:56 +00:00
|
|
|
/// Executing partition_by expression adds new columns to passed block according to partition functions.
|
|
|
|
/// The block is passed by reference and is used afterwards. `moduloLegacy` needs to be substituted back
|
|
|
|
/// with just `modulo`, because it was a temporary substitution.
|
|
|
|
static constexpr auto modulo_legacy_function_name = "moduloLegacy";
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
for (const auto & element : partition_key_names_and_types)
|
2020-05-05 15:06:16 +00:00
|
|
|
{
|
2021-05-24 23:39:56 +00:00
|
|
|
auto & partition_column = block.getByName(element.name);
|
2021-05-21 16:14:01 +00:00
|
|
|
|
2021-05-24 23:39:56 +00:00
|
|
|
if (element.name.starts_with(modulo_legacy_function_name))
|
|
|
|
partition_column.name = "modulo" + partition_column.name.substr(std::strlen(modulo_legacy_function_name));
|
2021-05-21 16:14:01 +00:00
|
|
|
|
2021-05-24 23:39:56 +00:00
|
|
|
partition_column.column->get(row, value[i++]);
|
2021-05-16 09:20:59 +00:00
|
|
|
}
|
2021-05-15 18:32:20 +00:00
|
|
|
}
|
2021-05-16 09:20:59 +00:00
|
|
|
|
2021-05-24 23:39:56 +00:00
|
|
|
NamesAndTypesList MergeTreePartition::executePartitionByExpression(const StorageMetadataPtr & metadata_snapshot, Block & block, ContextPtr context)
|
2021-05-21 16:14:01 +00:00
|
|
|
{
|
|
|
|
auto adjusted_partition_key = adjustPartitionKey(metadata_snapshot, context);
|
|
|
|
adjusted_partition_key.expression->execute(block);
|
2021-05-24 23:39:56 +00:00
|
|
|
return adjusted_partition_key.sample_block.getNamesAndTypesList();
|
2021-05-21 16:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KeyDescription MergeTreePartition::adjustPartitionKey(const StorageMetadataPtr & metadata_snapshot, ContextPtr context)
|
|
|
|
{
|
|
|
|
const auto & partition_key = metadata_snapshot->getPartitionKey();
|
|
|
|
if (!partition_key.definition_ast)
|
|
|
|
return partition_key;
|
|
|
|
|
|
|
|
ASTPtr ast_copy = partition_key.definition_ast->clone();
|
|
|
|
|
|
|
|
/// Implementation of modulo function was changed from 8bit result type to 16bit. For backward compatibility partition by expression is always
|
|
|
|
/// calculated according to previous version - `moduloLegacy`.
|
|
|
|
if (KeyDescription::moduloToModuloLegacyRecursive(ast_copy))
|
|
|
|
{
|
|
|
|
auto adjusted_partition_key = KeyDescription::getKeyFromAST(ast_copy, metadata_snapshot->columns, context);
|
|
|
|
return adjusted_partition_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return partition_key;
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:55:41 +00:00
|
|
|
}
|