2017-04-01 09:19:00 +00:00
# include <Storages/MergeTree/MergeTreeDataWriter.h>
# include <Storages/MergeTree/MergedBlockOutputStream.h>
2022-04-08 18:56:08 +00:00
# include <Storages/MergeTree/DataPartStorageOnDisk.h>
2020-04-10 09:25:52 +00:00
# include <Columns/ColumnConst.h>
2017-08-19 18:11:20 +00:00
# include <Common/HashTable/HashMap.h>
2019-03-29 09:45:55 +00:00
# include <Common/Exception.h>
2020-05-09 21:24:15 +00:00
# include <Disks/createVolume.h>
2017-08-19 18:11:20 +00:00
# include <Interpreters/AggregationCommon.h>
2020-05-20 20:16:32 +00:00
# include <Interpreters/Context.h>
2021-02-10 14:12:49 +00:00
# include <Interpreters/InterpreterSelectQuery.h>
2022-02-14 19:50:08 +00:00
# include <Interpreters/MergeTreeTransaction.h>
2017-04-01 09:19:00 +00:00
# include <IO/HashingWriteBuffer.h>
2019-04-15 09:30:45 +00:00
# include <DataTypes/DataTypeDateTime.h>
# include <DataTypes/DataTypeDate.h>
2021-04-23 23:56:26 +00:00
# include <DataTypes/ObjectUtils.h>
2019-03-29 09:33:39 +00:00
# include <IO/WriteHelpers.h>
2019-04-15 09:30:45 +00:00
# include <Common/typeid_cast.h>
2021-10-15 10:11:57 +00:00
# include <Processors/TTL/ITTLAlgorithm.h>
2014-03-13 17:44:00 +00:00
2020-09-01 10:49:53 +00:00
# include <Parsers/queryToString.h>
2016-10-24 02:02:37 +00:00
2020-11-13 07:54:05 +00:00
# include <Processors/Merges/Algorithms/ReplacingSortedAlgorithm.h>
# include <Processors/Merges/Algorithms/MergingSortedAlgorithm.h>
# include <Processors/Merges/Algorithms/CollapsingSortedAlgorithm.h>
# include <Processors/Merges/Algorithms/SummingSortedAlgorithm.h>
# include <Processors/Merges/Algorithms/AggregatingSortedAlgorithm.h>
# include <Processors/Merges/Algorithms/VersionedCollapsingAlgorithm.h>
# include <Processors/Merges/Algorithms/GraphiteRollupSortedAlgorithm.h>
2021-02-10 14:12:49 +00:00
# include <Processors/Sources/SourceFromSingleChunk.h>
2020-11-13 07:54:05 +00:00
2016-10-24 02:02:37 +00:00
namespace ProfileEvents
{
extern const Event MergeTreeDataWriterBlocks ;
extern const Event MergeTreeDataWriterBlocksAlreadySorted ;
extern const Event MergeTreeDataWriterRows ;
extern const Event MergeTreeDataWriterUncompressedBytes ;
extern const Event MergeTreeDataWriterCompressedBytes ;
2021-02-10 14:12:49 +00:00
extern const Event MergeTreeDataProjectionWriterBlocks ;
extern const Event MergeTreeDataProjectionWriterBlocksAlreadySorted ;
extern const Event MergeTreeDataProjectionWriterRows ;
extern const Event MergeTreeDataProjectionWriterUncompressedBytes ;
extern const Event MergeTreeDataProjectionWriterCompressedBytes ;
2016-10-24 02:02:37 +00:00
}
2014-03-13 17:44:00 +00:00
namespace DB
{
2018-11-22 21:19:58 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR ;
2019-03-29 09:45:55 +00:00
extern const int TOO_MANY_PARTS ;
2018-11-22 21:19:58 +00:00
}
2017-08-19 18:11:20 +00:00
namespace
2014-03-13 17:44:00 +00:00
{
2017-04-01 07:20:54 +00:00
2017-08-19 18:11:20 +00:00
void buildScatterSelector (
2017-12-13 01:27:53 +00:00
const ColumnRawPtrs & columns ,
2017-08-31 19:56:43 +00:00
PODArray < size_t > & partition_num_to_first_row ,
2019-03-29 09:33:39 +00:00
IColumn : : Selector & selector ,
size_t max_parts )
2017-08-19 18:11:20 +00:00
{
2017-08-31 19:56:43 +00:00
/// Use generic hashed variant since partitioning is unlikely to be a bottleneck.
2017-08-19 18:11:20 +00:00
using Data = HashMap < UInt128 , size_t , UInt128TrivialHash > ;
Data partitions_map ;
2017-04-01 07:20:54 +00:00
2017-08-19 18:11:20 +00:00
size_t num_rows = columns [ 0 ] - > size ( ) ;
size_t partitions_count = 0 ;
for ( size_t i = 0 ; i < num_rows ; + + i )
{
Data : : key_type key = hash128 ( i , columns . size ( ) , columns ) ;
2019-08-20 09:58:44 +00:00
typename Data : : LookupResult it ;
2017-08-19 18:11:20 +00:00
bool inserted ;
partitions_map . emplace ( key , it , inserted ) ;
if ( inserted )
{
2019-03-29 09:33:39 +00:00
if ( max_parts & & partitions_count > = max_parts )
2019-03-29 09:45:55 +00:00
throw Exception ( " Too many partitions for single INSERT block (more than " + toString ( max_parts ) + " ). The limit is controlled by 'max_partitions_per_insert_block' setting. Large number of partitions is a common misconception. It will lead to severe negative performance impact, including slow server startup, slow INSERT queries and slow SELECT queries. Recommended total number of partitions for a table is under 1000..10000. Please note, that partitioning is not intended to speed up SELECT queries (ORDER BY key is sufficient to make range queries fast). Partitions are intended for data manipulation (DROP PARTITION, etc). " , ErrorCodes : : TOO_MANY_PARTS ) ;
2019-03-29 09:33:39 +00:00
2017-08-31 19:56:43 +00:00
partition_num_to_first_row . push_back ( i ) ;
2019-10-29 15:16:51 +00:00
it - > getMapped ( ) = partitions_count ;
2017-08-19 18:11:20 +00:00
+ + partitions_count ;
/// Optimization for common case when there is only one partition - defer selector initialization.
if ( partitions_count = = 2 )
{
selector = IColumn : : Selector ( num_rows ) ;
std : : fill ( selector . begin ( ) , selector . begin ( ) + i , 0 ) ;
}
}
if ( partitions_count > 1 )
2019-10-29 15:16:51 +00:00
selector [ i ] = it - > getMapped ( ) ;
2017-08-19 18:11:20 +00:00
}
}
2017-04-01 07:20:54 +00:00
2019-04-15 09:30:45 +00:00
/// Computes ttls and updates ttl infos
2020-05-25 17:07:14 +00:00
void updateTTL (
2020-05-28 15:33:44 +00:00
const TTLDescription & ttl_entry ,
2019-12-19 11:46:43 +00:00
IMergeTreeDataPart : : TTLInfos & ttl_infos ,
2019-11-20 08:08:04 +00:00
DB : : MergeTreeDataPartTTLInfo & ttl_info ,
2020-09-24 11:42:41 +00:00
const Block & block ,
2020-05-25 17:07:14 +00:00
bool update_part_min_max_ttls )
2019-04-15 09:30:45 +00:00
{
2021-01-12 16:42:49 +00:00
auto ttl_column = ITTLAlgorithm : : executeExpressionAndGetColumn ( ttl_entry . expression , block , ttl_entry . result_column ) ;
2019-04-15 09:30:45 +00:00
2021-01-12 16:42:49 +00:00
if ( const ColumnUInt16 * column_date = typeid_cast < const ColumnUInt16 * > ( ttl_column . get ( ) ) )
2019-04-15 09:30:45 +00:00
{
const auto & date_lut = DateLUT : : instance ( ) ;
for ( const auto & val : column_date - > getData ( ) )
ttl_info . update ( date_lut . fromDayNum ( DayNum ( val ) ) ) ;
}
2021-01-12 16:42:49 +00:00
else if ( const ColumnUInt32 * column_date_time = typeid_cast < const ColumnUInt32 * > ( ttl_column . get ( ) ) )
2019-04-15 09:30:45 +00:00
{
for ( const auto & val : column_date_time - > getData ( ) )
ttl_info . update ( val ) ;
}
2021-01-12 16:42:49 +00:00
else if ( const ColumnConst * column_const = typeid_cast < const ColumnConst * > ( ttl_column . get ( ) ) )
2019-11-27 10:09:07 +00:00
{
if ( typeid_cast < const ColumnUInt16 * > ( & column_const - > getDataColumn ( ) ) )
{
const auto & date_lut = DateLUT : : instance ( ) ;
ttl_info . update ( date_lut . fromDayNum ( DayNum ( column_const - > getValue < UInt16 > ( ) ) ) ) ;
}
else if ( typeid_cast < const ColumnUInt32 * > ( & column_const - > getDataColumn ( ) ) )
{
ttl_info . update ( column_const - > getValue < UInt32 > ( ) ) ;
}
else
throw Exception ( " Unexpected type of result TTL column " , ErrorCodes : : LOGICAL_ERROR ) ;
}
2019-04-15 09:30:45 +00:00
else
2019-11-27 10:09:07 +00:00
throw Exception ( " Unexpected type of result TTL column " , ErrorCodes : : LOGICAL_ERROR ) ;
2019-04-15 09:30:45 +00:00
2019-11-29 05:41:09 +00:00
if ( update_part_min_max_ttls )
ttl_infos . updatePartMinMaxTTL ( ttl_info . min , ttl_info . max ) ;
2019-04-15 09:30:45 +00:00
}
2017-08-19 18:11:20 +00:00
}
2022-02-01 10:36:51 +00:00
void MergeTreeDataWriter : : TemporaryPart : : finalize ( )
{
for ( auto & stream : streams )
stream . finalizer . finish ( ) ;
}
2021-05-21 16:14:01 +00:00
BlocksWithPartition MergeTreeDataWriter : : splitBlockIntoParts (
2022-03-31 13:26:32 +00:00
const Block & block , size_t max_parts , const StorageMetadataPtr & metadata_snapshot , ContextPtr context )
2017-08-19 18:11:20 +00:00
{
BlocksWithPartition result ;
if ( ! block | | ! block . rows ( ) )
return result ;
2017-08-16 19:24:50 +00:00
2020-06-17 14:32:25 +00:00
metadata_snapshot - > check ( block , true ) ;
2017-04-01 07:20:54 +00:00
2020-06-17 10:34:23 +00:00
if ( ! metadata_snapshot - > hasPartitionKey ( ) ) /// Table is not partitioned.
2014-03-13 17:44:00 +00:00
{
2021-05-21 01:17:18 +00:00
result . emplace_back ( Block ( block ) , Row { } ) ;
2017-08-19 18:11:20 +00:00
return result ;
2014-03-13 17:44:00 +00:00
}
2017-04-01 07:20:54 +00:00
2017-08-19 18:11:20 +00:00
Block block_copy = block ;
2021-05-21 16:14:01 +00:00
/// After expression execution partition key columns will be added to block_copy with names regarding partition function.
2021-05-24 23:39:56 +00:00
auto partition_key_names_and_types = MergeTreePartition : : executePartitionByExpression ( metadata_snapshot , block_copy , context ) ;
2017-04-01 07:20:54 +00:00
2017-12-13 01:27:53 +00:00
ColumnRawPtrs partition_columns ;
2021-05-24 23:39:56 +00:00
partition_columns . reserve ( partition_key_names_and_types . size ( ) ) ;
for ( const auto & element : partition_key_names_and_types )
2018-02-21 17:05:21 +00:00
partition_columns . emplace_back ( block_copy . getByName ( element . name ) . column . get ( ) ) ;
2017-04-01 07:20:54 +00:00
2017-08-31 19:56:43 +00:00
PODArray < size_t > partition_num_to_first_row ;
2017-08-19 18:11:20 +00:00
IColumn : : Selector selector ;
2019-03-29 09:33:39 +00:00
buildScatterSelector ( partition_columns , partition_num_to_first_row , selector , max_parts ) ;
2017-04-01 07:20:54 +00:00
2017-08-31 19:56:43 +00:00
size_t partitions_count = partition_num_to_first_row . size ( ) ;
2017-08-19 18:11:20 +00:00
result . reserve ( partitions_count ) ;
2017-04-01 07:20:54 +00:00
2017-08-19 18:11:20 +00:00
auto get_partition = [ & ] ( size_t num )
{
2017-08-31 19:56:43 +00:00
Row partition ( partition_columns . size ( ) ) ;
2017-08-19 18:11:20 +00:00
for ( size_t i = 0 ; i < partition_columns . size ( ) ; + + i )
2022-04-15 23:15:40 +00:00
partition [ i ] = ( * partition_columns [ i ] ) [ partition_num_to_first_row [ num ] ] ;
2017-08-19 18:11:20 +00:00
return partition ;
} ;
2017-04-01 07:20:54 +00:00
2017-08-19 18:11:20 +00:00
if ( partitions_count = = 1 )
2014-03-13 17:44:00 +00:00
{
2017-08-19 18:11:20 +00:00
/// A typical case is when there is one partition (you do not need to split anything).
2018-02-21 15:43:24 +00:00
/// NOTE: returning a copy of the original block so that calculated partition key columns
/// do not interfere with possible calculated primary key columns of the same name.
result . emplace_back ( Block ( block ) , get_partition ( 0 ) ) ;
2017-08-19 18:11:20 +00:00
return result ;
}
2017-04-01 07:20:54 +00:00
2017-08-19 18:11:20 +00:00
for ( size_t i = 0 ; i < partitions_count ; + + i )
result . emplace_back ( block . cloneEmpty ( ) , get_partition ( i ) ) ;
2017-04-01 07:20:54 +00:00
2017-08-19 18:11:20 +00:00
for ( size_t col = 0 ; col < block . columns ( ) ; + + col )
{
2017-12-15 20:48:46 +00:00
MutableColumns scattered = block . getByPosition ( col ) . column - > scatter ( partitions_count , selector ) ;
2017-08-19 18:11:20 +00:00
for ( size_t i = 0 ; i < partitions_count ; + + i )
result [ i ] . block . getByPosition ( col ) . column = std : : move ( scattered [ i ] ) ;
2014-03-13 17:44:00 +00:00
}
2017-04-01 07:20:54 +00:00
2017-08-19 18:11:20 +00:00
return result ;
2014-03-13 17:44:00 +00:00
}
2021-11-26 14:45:56 +00:00
Block MergeTreeDataWriter : : mergeBlock (
const Block & block ,
SortDescription sort_description ,
const Names & partition_key_columns ,
IColumn : : Permutation * & permutation ,
const MergeTreeData : : MergingParams & merging_params )
2020-11-13 07:54:05 +00:00
{
size_t block_size = block . rows ( ) ;
auto get_merging_algorithm = [ & ] ( ) - > std : : shared_ptr < IMergingAlgorithm >
{
2021-11-26 14:45:56 +00:00
switch ( merging_params . mode )
2020-11-13 07:54:05 +00:00
{
/// There is nothing to merge in single block in ordinary MergeTree
case MergeTreeData : : MergingParams : : Ordinary :
return nullptr ;
case MergeTreeData : : MergingParams : : Replacing :
return std : : make_shared < ReplacingSortedAlgorithm > (
2021-11-26 14:45:56 +00:00
block , 1 , sort_description , merging_params . version_column , block_size + 1 ) ;
2020-11-13 07:54:05 +00:00
case MergeTreeData : : MergingParams : : Collapsing :
return std : : make_shared < CollapsingSortedAlgorithm > (
2021-11-26 14:45:56 +00:00
block , 1 , sort_description , merging_params . sign_column ,
2022-05-09 19:13:02 +00:00
false , block_size + 1 , & Poco : : Logger : : get ( " MergeTreeDataWriter " ) ) ;
2020-11-13 07:54:05 +00:00
case MergeTreeData : : MergingParams : : Summing :
return std : : make_shared < SummingSortedAlgorithm > (
2021-11-26 14:45:56 +00:00
block , 1 , sort_description , merging_params . columns_to_sum ,
2020-11-17 16:56:36 +00:00
partition_key_columns , block_size + 1 ) ;
2020-11-13 07:54:05 +00:00
case MergeTreeData : : MergingParams : : Aggregating :
2020-11-17 16:56:36 +00:00
return std : : make_shared < AggregatingSortedAlgorithm > ( block , 1 , sort_description , block_size + 1 ) ;
2020-11-13 07:54:05 +00:00
case MergeTreeData : : MergingParams : : VersionedCollapsing :
return std : : make_shared < VersionedCollapsingAlgorithm > (
2021-11-26 14:45:56 +00:00
block , 1 , sort_description , merging_params . sign_column , block_size + 1 ) ;
2020-11-13 07:54:05 +00:00
case MergeTreeData : : MergingParams : : Graphite :
return std : : make_shared < GraphiteRollupSortedAlgorithm > (
2021-11-26 14:45:56 +00:00
block , 1 , sort_description , block_size + 1 , merging_params . graphite_params , time ( nullptr ) ) ;
2020-11-13 07:54:05 +00:00
}
2022-10-07 19:20:14 +00:00
UNREACHABLE ( ) ;
2020-11-13 07:54:05 +00:00
} ;
auto merging_algorithm = get_merging_algorithm ( ) ;
if ( ! merging_algorithm )
return block ;
Chunk chunk ( block . getColumns ( ) , block_size ) ;
IMergingAlgorithm : : Input input ;
input . set ( std : : move ( chunk ) ) ;
2020-12-04 16:25:30 +00:00
input . permutation = permutation ;
2020-11-13 07:54:05 +00:00
IMergingAlgorithm : : Inputs inputs ;
inputs . push_back ( std : : move ( input ) ) ;
merging_algorithm - > initialize ( std : : move ( inputs ) ) ;
IMergingAlgorithm : : Status status = merging_algorithm - > merge ( ) ;
2020-12-04 16:25:30 +00:00
/// Check that after first merge merging_algorithm is waiting for data from input 0.
if ( status . required_source ! = 0 )
2020-12-09 15:07:58 +00:00
throw Exception ( " Logical error: required source after the first merge is not 0. " , ErrorCodes : : LOGICAL_ERROR ) ;
2020-12-04 16:25:30 +00:00
status = merging_algorithm - > merge ( ) ;
/// Check that merge is finished.
if ( ! status . is_finished )
2020-12-09 15:07:58 +00:00
throw Exception ( " Logical error: merge is not finished after the second merge. " , ErrorCodes : : LOGICAL_ERROR ) ;
2020-11-20 10:38:53 +00:00
2020-11-13 07:54:05 +00:00
/// Merged Block is sorted and we don't need to use permutation anymore
2020-12-04 16:25:30 +00:00
permutation = nullptr ;
2020-11-13 07:54:05 +00:00
return block . cloneWithColumns ( status . chunk . getColumns ( ) ) ;
}
2022-02-01 10:36:51 +00:00
MergeTreeDataWriter : : TemporaryPart MergeTreeDataWriter : : writeTempPart (
2021-02-10 14:12:49 +00:00
BlockWithPartition & block_with_partition , const StorageMetadataPtr & metadata_snapshot , ContextPtr context )
2014-03-13 17:44:00 +00:00
{
2022-02-01 10:36:51 +00:00
TemporaryPart temp_part ;
2017-08-18 19:46:26 +00:00
Block & block = block_with_partition . block ;
2017-08-16 19:24:50 +00:00
2022-03-31 13:26:32 +00:00
auto columns = metadata_snapshot - > getColumns ( ) . getAllPhysical ( ) . filter ( block . getNames ( ) ) ;
2022-02-09 00:18:53 +00:00
2022-03-31 13:26:32 +00:00
for ( auto & column : columns )
if ( isObject ( column . type ) )
column . type = block . getByName ( column . name ) . type ;
2017-08-16 19:24:50 +00:00
static const String TMP_PREFIX = " tmp_insert_ " ;
/// This will generate unique name in scope of current server process.
Int64 temp_index = data . insert_increment . get ( ) ;
2021-09-16 21:19:58 +00:00
auto minmax_idx = std : : make_shared < IMergeTreeDataPart : : MinMaxIndex > ( ) ;
minmax_idx - > update ( block , data . getMinMaxColumnsNames ( metadata_snapshot - > getPartitionKey ( ) ) ) ;
2017-08-21 15:35:29 +00:00
2017-09-11 17:55:41 +00:00
MergeTreePartition partition ( std : : move ( block_with_partition . partition ) ) ;
2014-03-13 17:44:00 +00:00
2020-06-17 10:34:23 +00:00
MergeTreePartInfo new_part_info ( partition . getID ( metadata_snapshot - > getPartitionKey ( ) . sample_block ) , temp_index , temp_index , 0 ) ;
2017-08-25 20:41:45 +00:00
String part_name ;
2017-09-07 16:21:06 +00:00
if ( data . format_version < MERGE_TREE_DATA_MIN_FORMAT_VERSION_WITH_CUSTOM_PARTITIONING )
2017-08-25 20:41:45 +00:00
{
2021-09-16 21:19:58 +00:00
DayNum min_date ( minmax_idx - > hyperrectangle [ data . minmax_idx_date_column_pos ] . left . get < UInt64 > ( ) ) ;
DayNum max_date ( minmax_idx - > hyperrectangle [ data . minmax_idx_date_column_pos ] . right . get < UInt64 > ( ) ) ;
2014-03-13 17:44:00 +00:00
2017-08-25 20:41:45 +00:00
const auto & date_lut = DateLUT : : instance ( ) ;
2015-08-17 21:09:36 +00:00
2020-04-12 22:00:03 +00:00
auto min_month = date_lut . toNumYYYYMM ( min_date ) ;
auto max_month = date_lut . toNumYYYYMM ( max_date ) ;
2015-08-17 21:09:36 +00:00
2017-08-25 20:41:45 +00:00
if ( min_month ! = max_month )
2018-11-22 21:19:58 +00:00
throw Exception ( " Logical error: part spans more than one month. " , ErrorCodes : : LOGICAL_ERROR ) ;
2014-03-13 17:44:00 +00:00
2017-08-25 20:41:45 +00:00
part_name = new_part_info . getPartNameV0 ( min_date , max_date ) ;
}
else
part_name = new_part_info . getPartName ( ) ;
2022-08-09 21:16:08 +00:00
String part_dir = TMP_PREFIX + part_name ;
temp_part . temporary_directory_lock = data . getTemporaryPartDirectoryHolder ( part_dir ) ;
2020-11-13 07:54:05 +00:00
/// If we need to calculate some columns to sort.
if ( metadata_snapshot - > hasSortingKey ( ) | | metadata_snapshot - > hasSecondaryIndices ( ) )
data . getSortingKeyAndSkipIndicesExpression ( metadata_snapshot ) - > execute ( block ) ;
Names sort_columns = metadata_snapshot - > getSortingKeyColumns ( ) ;
SortDescription sort_description ;
size_t sort_columns_size = sort_columns . size ( ) ;
sort_description . reserve ( sort_columns_size ) ;
for ( size_t i = 0 ; i < sort_columns_size ; + + i )
2022-04-04 12:17:15 +00:00
sort_description . emplace_back ( sort_columns [ i ] , 1 , 1 ) ;
2020-11-13 07:54:05 +00:00
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataWriterBlocks ) ;
/// Sort
IColumn : : Permutation * perm_ptr = nullptr ;
IColumn : : Permutation perm ;
if ( ! sort_description . empty ( ) )
{
if ( ! isAlreadySorted ( block , sort_description ) )
{
stableGetPermutation ( block , sort_description , perm ) ;
perm_ptr = & perm ;
}
else
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataWriterBlocksAlreadySorted ) ;
}
Names partition_key_columns = metadata_snapshot - > getPartitionKey ( ) . column_names ;
2021-02-10 14:12:49 +00:00
if ( context - > getSettingsRef ( ) . optimize_on_insert )
2021-11-26 14:45:56 +00:00
block = mergeBlock ( block , sort_description , partition_key_columns , perm_ptr , data . merging_params ) ;
2020-11-13 07:54:05 +00:00
2019-10-31 10:40:11 +00:00
/// Size of part would not be greater than block.bytes() + epsilon
2019-04-01 18:41:19 +00:00
size_t expected_size = block . bytes ( ) ;
2021-02-12 14:02:04 +00:00
/// If optimize_on_insert is true, block may become empty after merge.
/// There is no need to create empty part.
if ( expected_size = = 0 )
2022-02-01 10:36:51 +00:00
return temp_part ;
2021-02-12 14:02:04 +00:00
2019-12-19 13:10:57 +00:00
DB : : IMergeTreeDataPart : : TTLInfos move_ttl_infos ;
2020-06-17 13:39:26 +00:00
const auto & move_ttl_entries = metadata_snapshot - > getMoveTTLs ( ) ;
2020-05-25 17:07:14 +00:00
for ( const auto & ttl_entry : move_ttl_entries )
2019-11-29 05:41:09 +00:00
updateTTL ( ttl_entry , move_ttl_infos , move_ttl_infos . moves_ttl [ ttl_entry . result_column ] , block , false ) ;
2019-04-01 18:41:19 +00:00
2020-10-05 16:41:46 +00:00
ReservationPtr reservation = data . reserveSpacePreferringTTLRules ( metadata_snapshot , expected_size , move_ttl_infos , time ( nullptr ) , 0 , true ) ;
2020-05-09 21:24:15 +00:00
VolumePtr volume = data . getStoragePolicy ( ) - > getVolume ( 0 ) ;
2022-04-12 18:59:49 +00:00
VolumePtr data_part_volume = createVolumeFromReservation ( reservation , volume ) ;
2019-11-25 20:19:43 +00:00
2022-04-08 18:56:08 +00:00
auto data_part_storage = std : : make_shared < DataPartStorageOnDisk > (
2022-04-12 18:59:49 +00:00
data_part_volume ,
data . relative_data_path ,
TMP_PREFIX + part_name ) ;
2022-10-22 22:51:59 +00:00
data_part_storage - > beginTransaction ( ) ;
2019-11-25 20:19:43 +00:00
2019-11-25 11:06:59 +00:00
auto new_data_part = data . createPart (
2020-02-13 14:42:48 +00:00
part_name ,
data . choosePartType ( expected_size , block . rows ( ) ) ,
new_part_info ,
2022-04-08 18:56:08 +00:00
data_part_storage ) ;
2019-09-10 08:56:27 +00:00
2020-11-02 18:52:50 +00:00
if ( data . storage_settings . get ( ) - > assign_part_uuids )
new_data_part - > uuid = UUIDHelpers : : generateV4 ( ) ;
2021-10-29 17:21:02 +00:00
const auto & data_settings = data . getSettings ( ) ;
SerializationInfo : : Settings settings { data_settings - > ratio_of_defaults_for_sparse_serialization , true } ;
SerializationInfoByName infos ( columns , settings ) ;
infos . add ( block ) ;
2022-07-27 14:05:16 +00:00
new_data_part - > setColumns ( columns , infos ) ;
2021-03-09 14:46:52 +00:00
new_data_part - > rows_count = block . rows ( ) ;
2017-09-05 12:12:55 +00:00
new_data_part - > partition = std : : move ( partition ) ;
2017-08-21 15:35:29 +00:00
new_data_part - > minmax_idx = std : : move ( minmax_idx ) ;
2014-07-17 10:44:17 +00:00
new_data_part - > is_temp = true ;
2021-01-26 13:29:45 +00:00
SyncGuardPtr sync_guard ;
2020-04-14 19:47:19 +00:00
if ( new_data_part - > isStoredOnDisk ( ) )
2017-06-25 00:01:10 +00:00
{
2020-04-14 19:47:19 +00:00
/// The name could be non-unique in case of stale files from previous runs.
2022-10-23 03:29:26 +00:00
String full_path = new_data_part - > getDataPartStorage ( ) . getFullPath ( ) ;
2020-04-14 19:47:19 +00:00
2022-10-23 03:29:26 +00:00
if ( new_data_part - > getDataPartStorage ( ) . exists ( ) )
2020-04-14 19:47:19 +00:00
{
2022-06-30 12:12:45 +00:00
LOG_WARNING ( log , " Removing old temporary directory {} " , full_path ) ;
2022-10-22 22:51:59 +00:00
data_part_storage - > removeRecursive ( ) ;
2020-04-14 19:47:19 +00:00
}
2017-06-25 00:01:10 +00:00
2022-10-22 22:51:59 +00:00
data_part_storage - > createDirectories ( ) ;
2020-06-26 21:55:48 +00:00
2020-08-24 13:09:23 +00:00
if ( data . getSettings ( ) - > fsync_part_directory )
2022-06-28 11:19:30 +00:00
{
const auto disk = data_part_volume - > getDisk ( ) ;
2021-01-26 13:29:45 +00:00
sync_guard = disk - > getDirectorySyncGuard ( full_path ) ;
2022-06-28 11:19:30 +00:00
}
2020-08-24 13:09:23 +00:00
}
2017-05-16 15:40:32 +00:00
2020-09-24 11:42:41 +00:00
if ( metadata_snapshot - > hasRowsTTL ( ) )
updateTTL ( metadata_snapshot - > getRowsTTL ( ) , new_data_part - > ttl_infos , new_data_part - > ttl_infos . table_ttl , block , true ) ;
2020-12-25 14:52:46 +00:00
for ( const auto & ttl_entry : metadata_snapshot - > getGroupByTTLs ( ) )
updateTTL ( ttl_entry , new_data_part - > ttl_infos , new_data_part - > ttl_infos . group_by_ttl [ ttl_entry . result_column ] , block , true ) ;
2021-01-13 14:04:27 +00:00
for ( const auto & ttl_entry : metadata_snapshot - > getRowsWhereTTLs ( ) )
2021-01-11 23:07:21 +00:00
updateTTL ( ttl_entry , new_data_part - > ttl_infos , new_data_part - > ttl_infos . rows_where_ttl [ ttl_entry . result_column ] , block , true ) ;
2020-09-24 11:42:41 +00:00
for ( const auto & [ name , ttl_entry ] : metadata_snapshot - > getColumnTTLs ( ) )
updateTTL ( ttl_entry , new_data_part - > ttl_infos , new_data_part - > ttl_infos . columns_ttl [ name ] , block , true ) ;
const auto & recompression_ttl_entries = metadata_snapshot - > getRecompressionTTLs ( ) ;
for ( const auto & ttl_entry : recompression_ttl_entries )
updateTTL ( ttl_entry , new_data_part - > ttl_infos , new_data_part - > ttl_infos . recompression_ttl [ ttl_entry . result_column ] , block , false ) ;
new_data_part - > ttl_infos . update ( move_ttl_infos ) ;
2017-08-01 20:07:16 +00:00
/// This effectively chooses minimal compression method:
/// either default lz4 or compression method with zero thresholds on absolute and relative part size.
2021-04-10 23:33:54 +00:00
auto compression_codec = data . getContext ( ) - > chooseCompressionCodec ( 0 , 0 ) ;
2017-07-31 11:05:49 +00:00
2020-05-28 12:37:05 +00:00
const auto & index_factory = MergeTreeIndexFactory : : instance ( ) ;
2022-10-22 22:51:59 +00:00
auto out = std : : make_unique < MergedBlockOutputStream > ( new_data_part , metadata_snapshot , columns ,
2022-04-08 11:34:40 +00:00
index_factory . getMany ( metadata_snapshot - > getSecondaryIndices ( ) ) , compression_codec ,
context - > getCurrentTransaction ( ) , false , false , context - > getWriteSettings ( ) ) ;
2021-04-15 21:47:11 +00:00
2022-02-01 10:36:51 +00:00
out - > writeWithPermutation ( block , perm_ptr ) ;
2021-08-26 11:01:15 +00:00
for ( const auto & projection : metadata_snapshot - > getProjections ( ) )
{
auto projection_block = projection . calculate ( block , context ) ;
if ( projection_block . rows ( ) )
2022-02-01 10:36:51 +00:00
{
2022-10-22 22:51:59 +00:00
auto proj_temp_part = writeProjectionPart ( data , log , projection_block , projection , new_data_part . get ( ) ) ;
2022-02-01 10:36:51 +00:00
new_data_part - > addProjectionPart ( projection . name , std : : move ( proj_temp_part . part ) ) ;
for ( auto & stream : proj_temp_part . streams )
temp_part . streams . emplace_back ( std : : move ( stream ) ) ;
}
2021-08-26 11:01:15 +00:00
}
2022-05-25 14:54:49 +00:00
2022-03-21 08:52:48 +00:00
auto finalizer = out - > finalizePartAsync (
new_data_part ,
data_settings - > fsync_after_insert ,
2022-05-16 20:09:11 +00:00
nullptr , nullptr ) ;
2022-02-01 10:36:51 +00:00
temp_part . part = new_data_part ;
temp_part . streams . emplace_back ( TemporaryPart : : Stream { . stream = std : : move ( out ) , . finalizer = std : : move ( finalizer ) } ) ;
2017-01-02 20:52:30 +00:00
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataWriterRows , block . rows ( ) ) ;
2016-04-23 02:39:40 +00:00
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataWriterUncompressedBytes , block . bytes ( ) ) ;
2020-03-23 13:32:02 +00:00
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataWriterCompressedBytes , new_data_part - > getBytesOnDisk ( ) ) ;
2016-04-23 02:39:40 +00:00
2022-02-01 10:36:51 +00:00
return temp_part ;
2014-03-13 17:44:00 +00:00
}
2022-02-01 10:36:51 +00:00
MergeTreeDataWriter : : TemporaryPart MergeTreeDataWriter : : writeProjectionPartImpl (
2021-11-26 14:45:56 +00:00
const String & part_name ,
2021-08-26 11:01:15 +00:00
MergeTreeDataPartType part_type ,
const String & relative_path ,
bool is_temp ,
2022-10-24 14:44:22 +00:00
IMergeTreeDataPart * parent_part ,
2021-08-26 11:01:15 +00:00
const MergeTreeData & data ,
2021-02-10 14:12:49 +00:00
Poco : : Logger * log ,
Block block ,
2021-11-26 14:45:56 +00:00
const ProjectionDescription & projection )
2021-02-10 14:12:49 +00:00
{
2022-02-01 10:36:51 +00:00
TemporaryPart temp_part ;
2021-11-26 14:45:56 +00:00
const StorageMetadataPtr & metadata_snapshot = projection . metadata ;
2021-08-26 11:01:15 +00:00
MergeTreePartInfo new_part_info ( " all " , 0 , 0 , 0 ) ;
2022-10-22 22:51:59 +00:00
2022-10-23 03:29:26 +00:00
auto projection_part_storage = parent_part - > getDataPartStorage ( ) . getProjection ( relative_path ) ;
2021-08-26 11:01:15 +00:00
auto new_data_part = data . createPart (
part_name ,
part_type ,
new_part_info ,
2022-04-12 18:59:49 +00:00
projection_part_storage ,
2021-08-26 11:01:15 +00:00
parent_part ) ;
2022-04-12 18:59:49 +00:00
2021-08-26 11:01:15 +00:00
new_data_part - > is_temp = is_temp ;
2021-02-10 14:12:49 +00:00
NamesAndTypesList columns = metadata_snapshot - > getColumns ( ) . getAllPhysical ( ) . filter ( block . getNames ( ) ) ;
2021-10-29 17:21:02 +00:00
SerializationInfo : : Settings settings { data . getSettings ( ) - > ratio_of_defaults_for_sparse_serialization , true } ;
SerializationInfoByName infos ( columns , settings ) ;
infos . add ( block ) ;
2022-07-27 14:05:16 +00:00
new_data_part - > setColumns ( columns , infos ) ;
2021-02-10 14:12:49 +00:00
if ( new_data_part - > isStoredOnDisk ( ) )
{
/// The name could be non-unique in case of stale files from previous runs.
2022-04-12 18:59:49 +00:00
if ( projection_part_storage - > exists ( ) )
2021-02-10 14:12:49 +00:00
{
2022-04-12 18:59:49 +00:00
LOG_WARNING ( log , " Removing old temporary directory {} " , projection_part_storage - > getFullPath ( ) ) ;
2022-10-22 22:51:59 +00:00
projection_part_storage - > removeRecursive ( ) ;
2021-02-10 14:12:49 +00:00
}
2022-10-22 22:51:59 +00:00
projection_part_storage - > createDirectories ( ) ;
2021-02-10 14:12:49 +00:00
}
/// If we need to calculate some columns to sort.
if ( metadata_snapshot - > hasSortingKey ( ) | | metadata_snapshot - > hasSecondaryIndices ( ) )
data . getSortingKeyAndSkipIndicesExpression ( metadata_snapshot ) - > execute ( block ) ;
Names sort_columns = metadata_snapshot - > getSortingKeyColumns ( ) ;
SortDescription sort_description ;
size_t sort_columns_size = sort_columns . size ( ) ;
sort_description . reserve ( sort_columns_size ) ;
for ( size_t i = 0 ; i < sort_columns_size ; + + i )
2022-04-04 12:17:15 +00:00
sort_description . emplace_back ( sort_columns [ i ] , 1 , 1 ) ;
2021-02-10 14:12:49 +00:00
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataProjectionWriterBlocks ) ;
/// Sort
IColumn : : Permutation * perm_ptr = nullptr ;
IColumn : : Permutation perm ;
if ( ! sort_description . empty ( ) )
{
if ( ! isAlreadySorted ( block , sort_description ) )
{
stableGetPermutation ( block , sort_description , perm ) ;
perm_ptr = & perm ;
}
else
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataProjectionWriterBlocksAlreadySorted ) ;
}
2021-11-26 14:45:56 +00:00
if ( projection . type = = ProjectionDescription : : Type : : Aggregate )
{
MergeTreeData : : MergingParams projection_merging_params ;
projection_merging_params . mode = MergeTreeData : : MergingParams : : Aggregating ;
block = mergeBlock ( block , sort_description , { } , perm_ptr , projection_merging_params ) ;
}
2021-02-10 14:12:49 +00:00
/// This effectively chooses minimal compression method:
/// either default lz4 or compression method with zero thresholds on absolute and relative part size.
auto compression_codec = data . getContext ( ) - > chooseCompressionCodec ( 0 , 0 ) ;
2022-02-01 10:36:51 +00:00
auto out = std : : make_unique < MergedBlockOutputStream > (
2021-02-10 14:12:49 +00:00
new_data_part ,
metadata_snapshot ,
columns ,
2022-02-01 10:36:51 +00:00
MergeTreeIndices { } ,
2022-02-14 19:50:08 +00:00
compression_codec ,
2022-05-16 20:09:11 +00:00
NO_TRANSACTION_PTR ,
false , false , data . getContext ( ) - > getWriteSettings ( ) ) ;
2021-02-10 14:12:49 +00:00
2022-02-01 10:36:51 +00:00
out - > writeWithPermutation ( block , perm_ptr ) ;
auto finalizer = out - > finalizePartAsync ( new_data_part , false ) ;
temp_part . part = new_data_part ;
temp_part . streams . emplace_back ( TemporaryPart : : Stream { . stream = std : : move ( out ) , . finalizer = std : : move ( finalizer ) } ) ;
2021-02-10 14:12:49 +00:00
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataProjectionWriterRows , block . rows ( ) ) ;
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataProjectionWriterUncompressedBytes , block . bytes ( ) ) ;
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataProjectionWriterCompressedBytes , new_data_part - > getBytesOnDisk ( ) ) ;
2022-02-01 10:36:51 +00:00
return temp_part ;
2021-02-10 14:12:49 +00:00
}
2022-02-01 10:36:51 +00:00
MergeTreeDataWriter : : TemporaryPart MergeTreeDataWriter : : writeProjectionPart (
2022-04-26 19:08:00 +00:00
MergeTreeData & data ,
Poco : : Logger * log ,
Block block ,
2022-04-12 18:59:49 +00:00
const ProjectionDescription & projection ,
2022-10-24 14:44:22 +00:00
IMergeTreeDataPart * parent_part )
2021-02-10 14:12:49 +00:00
{
String part_name = projection . name ;
2021-08-26 11:01:15 +00:00
MergeTreeDataPartType part_type ;
2022-03-14 14:42:09 +00:00
if ( parent_part - > getType ( ) = = MergeTreeDataPartType : : InMemory )
2021-08-26 11:01:15 +00:00
{
2022-03-14 14:42:09 +00:00
part_type = MergeTreeDataPartType : : InMemory ;
2021-08-26 11:01:15 +00:00
}
else
{
/// Size of part would not be greater than block.bytes() + epsilon
size_t expected_size = block . bytes ( ) ;
// just check if there is enough space on parent volume
2022-10-23 03:29:26 +00:00
data . reserveSpace ( expected_size , parent_part - > getDataPartStorage ( ) ) ;
2021-08-26 11:01:15 +00:00
part_type = data . choosePartTypeOnDisk ( expected_size , block . rows ( ) ) ;
}
2021-02-10 14:12:49 +00:00
2021-08-26 11:01:15 +00:00
return writeProjectionPartImpl (
part_name ,
part_type ,
part_name + " .proj " /* relative_path */ ,
false /* is_temp */ ,
parent_part ,
data ,
log ,
block ,
2021-11-26 14:45:56 +00:00
projection ) ;
2021-02-10 14:12:49 +00:00
}
2021-08-26 11:01:15 +00:00
/// This is used for projection materialization process which may contain multiple stages of
/// projection part merges.
2022-02-01 10:36:51 +00:00
MergeTreeDataWriter : : TemporaryPart MergeTreeDataWriter : : writeTempProjectionPart (
2021-02-10 14:12:49 +00:00
MergeTreeData & data ,
Poco : : Logger * log ,
Block block ,
const ProjectionDescription & projection ,
2022-10-24 14:44:22 +00:00
IMergeTreeDataPart * parent_part ,
2021-02-10 14:12:49 +00:00
size_t block_num )
{
String part_name = fmt : : format ( " {}_{} " , projection . name , block_num ) ;
2021-08-26 11:01:15 +00:00
MergeTreeDataPartType part_type ;
2022-03-14 14:42:09 +00:00
if ( parent_part - > getType ( ) = = MergeTreeDataPartType : : InMemory )
2021-08-26 11:01:15 +00:00
{
2022-03-14 14:42:09 +00:00
part_type = MergeTreeDataPartType : : InMemory ;
2021-08-26 11:01:15 +00:00
}
else
{
/// Size of part would not be greater than block.bytes() + epsilon
size_t expected_size = block . bytes ( ) ;
// just check if there is enough space on parent volume
2022-10-23 03:29:26 +00:00
data . reserveSpace ( expected_size , parent_part - > getDataPartStorage ( ) ) ;
2021-08-26 11:01:15 +00:00
part_type = data . choosePartTypeOnDisk ( expected_size , block . rows ( ) ) ;
}
return writeProjectionPartImpl (
2021-02-10 14:12:49 +00:00
part_name ,
2021-08-26 11:01:15 +00:00
part_type ,
2021-09-29 10:05:57 +00:00
part_name + " .tmp_proj " /* relative_path */ ,
2021-08-26 11:01:15 +00:00
true /* is_temp */ ,
parent_part ,
data ,
log ,
block ,
2021-11-26 14:45:56 +00:00
projection ) ;
2021-08-26 11:01:15 +00:00
}
2021-02-10 14:12:49 +00:00
2022-02-01 10:36:51 +00:00
MergeTreeDataWriter : : TemporaryPart MergeTreeDataWriter : : writeInMemoryProjectionPart (
2021-08-26 11:01:15 +00:00
const MergeTreeData & data ,
Poco : : Logger * log ,
Block block ,
const ProjectionDescription & projection ,
2022-10-24 14:44:22 +00:00
IMergeTreeDataPart * parent_part )
2021-08-26 11:01:15 +00:00
{
return writeProjectionPartImpl (
projection . name ,
2022-03-14 14:42:09 +00:00
MergeTreeDataPartType : : InMemory ,
2021-08-26 11:01:15 +00:00
projection . name + " .proj " /* relative_path */ ,
false /* is_temp */ ,
parent_part ,
data ,
log ,
block ,
2021-11-26 14:45:56 +00:00
projection ) ;
2021-02-10 14:12:49 +00:00
}
2014-03-13 17:44:00 +00:00
}