2017-04-01 09:19:00 +00:00
# include <Storages/MergeTree/MergeTreeDataWriter.h>
# include <Storages/MergeTree/MergedBlockOutputStream.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>
2017-08-19 18:11:20 +00:00
# include <Interpreters/AggregationCommon.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>
2019-03-29 09:33:39 +00:00
# include <IO/WriteHelpers.h>
2017-01-21 04:24:28 +00:00
# include <Poco/File.h>
2019-04-15 09:30:45 +00:00
# include <Common/typeid_cast.h>
2014-03-13 17:44:00 +00:00
2016-10-24 02:02:37 +00:00
namespace ProfileEvents
{
2017-04-01 07:20:54 +00:00
extern const Event MergeTreeDataWriterBlocks ;
extern const Event MergeTreeDataWriterBlocksAlreadySorted ;
extern const Event MergeTreeDataWriterRows ;
extern const Event MergeTreeDataWriterUncompressedBytes ;
extern const Event MergeTreeDataWriterCompressedBytes ;
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
2019-11-20 08:08:04 +00:00
void updateTTL ( const MergeTreeData : : TTLEntry & 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 ,
2019-11-29 05:41:09 +00:00
Block & block , bool update_part_min_max_ttls )
2019-04-15 09:30:45 +00:00
{
2019-11-27 18:39:47 +00:00
bool remove_column = false ;
2019-04-15 09:30:45 +00:00
if ( ! block . has ( ttl_entry . result_column ) )
2019-11-27 18:39:47 +00:00
{
2019-04-15 09:30:45 +00:00
ttl_entry . expression - > execute ( block ) ;
2019-11-27 18:39:47 +00:00
remove_column = true ;
}
2019-04-15 09:30:45 +00:00
const auto & current = block . getByName ( ttl_entry . result_column ) ;
const IColumn * column = current . column . get ( ) ;
if ( const ColumnUInt16 * column_date = typeid_cast < const ColumnUInt16 * > ( column ) )
{
const auto & date_lut = DateLUT : : instance ( ) ;
for ( const auto & val : column_date - > getData ( ) )
ttl_info . update ( date_lut . fromDayNum ( DayNum ( val ) ) ) ;
}
else if ( const ColumnUInt32 * column_date_time = typeid_cast < const ColumnUInt32 * > ( column ) )
{
for ( const auto & val : column_date_time - > getData ( ) )
ttl_info . update ( val ) ;
}
2019-11-27 10:09:07 +00:00
else if ( const ColumnConst * column_const = typeid_cast < const ColumnConst * > ( column ) )
{
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-11-26 08:02:48 +00:00
2019-11-27 18:39:47 +00:00
if ( remove_column )
block . erase ( ttl_entry . result_column ) ;
2019-04-15 09:30:45 +00:00
}
2017-08-19 18:11:20 +00:00
}
2019-03-29 09:33:39 +00:00
BlocksWithPartition MergeTreeDataWriter : : splitBlockIntoParts ( const Block & block , size_t max_parts )
2017-08-19 18:11:20 +00:00
{
BlocksWithPartition result ;
if ( ! block | | ! block . rows ( ) )
return result ;
2017-08-16 19:24:50 +00:00
2017-08-19 18:11:20 +00:00
data . check ( block , true ) ;
block . checkNumberOfRows ( ) ;
2017-04-01 07:20:54 +00:00
2018-11-06 18:25:36 +00:00
if ( ! data . partition_key_expr ) /// Table is not partitioned.
2017-04-01 07:20:54 +00:00
{
2017-08-19 18:11:20 +00:00
result . emplace_back ( Block ( block ) , Row ( ) ) ;
return result ;
2017-04-01 07:20:54 +00:00
}
2017-08-19 18:11:20 +00:00
Block block_copy = block ;
2018-11-06 18:25:36 +00:00
data . partition_key_expr - > execute ( block_copy ) ;
2017-04-01 07:20:54 +00:00
2017-12-13 01:27:53 +00:00
ColumnRawPtrs partition_columns ;
2018-02-21 17:05:21 +00:00
partition_columns . reserve ( data . partition_key_sample . columns ( ) ) ;
for ( const ColumnWithTypeAndName & element : data . partition_key_sample )
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 )
2017-08-31 19:56:43 +00:00
partition [ i ] = Field ( ( * 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 )
2017-04-01 07:20:54 +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 ] ) ;
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
}
2017-08-18 19:46:26 +00:00
MergeTreeData : : MutableDataPartPtr MergeTreeDataWriter : : writeTempPart ( BlockWithPartition & block_with_partition )
2014-03-13 17:44:00 +00:00
{
2017-08-18 19:46:26 +00:00
Block & block = block_with_partition . block ;
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 ( ) ;
2019-11-05 11:53:22 +00:00
IMergeTreeDataPart : : MinMaxIndex minmax_idx ;
2017-08-21 15:35:29 +00:00
minmax_idx . update ( block , data . minmax_idx_columns ) ;
2017-09-11 17:55:41 +00:00
MergeTreePartition partition ( std : : move ( block_with_partition . partition ) ) ;
2014-03-13 17:44:00 +00:00
2019-01-25 15:17:12 +00:00
MergeTreePartInfo new_part_info ( partition . getID ( data . partition_key_sample ) , 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
{
2020-03-10 14:56:55 +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 ( ) ;
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 ( ) ;
2019-12-19 13:10:57 +00:00
DB : : IMergeTreeDataPart : : TTLInfos move_ttl_infos ;
2019-11-28 06:44:26 +00:00
for ( const auto & ttl_entry : data . 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
2019-11-25 20:19:43 +00:00
NamesAndTypesList columns = data . getColumns ( ) . getAllPhysical ( ) . filter ( block . getNames ( ) ) ;
2019-12-12 11:10:18 +00:00
ReservationPtr reservation = data . reserveSpacePreferringTTLRules ( expected_size , move_ttl_infos , time ( nullptr ) ) ;
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 ,
2019-11-22 12:51:00 +00:00
reservation - > getDisk ( ) ,
2019-11-21 16:10:22 +00:00
TMP_PREFIX + part_name ) ;
2019-09-10 08:56:27 +00:00
2020-02-13 14:42:48 +00:00
new_data_part - > setColumns ( columns ) ;
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 ) ;
2017-04-01 07:20:54 +00:00
new_data_part - > is_temp = true ;
2014-07-17 10:44:17 +00:00
2017-06-25 00:01:10 +00:00
/// The name could be non-unique in case of stale files from previous runs.
2020-02-28 17:14:55 +00:00
String full_path = new_data_part - > getFullRelativePath ( ) ;
2017-06-25 00:01:10 +00:00
2020-02-28 17:14:55 +00:00
if ( new_data_part - > disk - > exists ( full_path ) )
2017-06-25 00:01:10 +00:00
{
2020-02-28 17:14:55 +00:00
LOG_WARNING ( log , " Removing old temporary directory " + fullPath ( new_data_part - > disk , full_path ) ) ;
new_data_part - > disk - > removeRecursive ( full_path ) ;
2017-06-25 00:01:10 +00:00
}
2020-02-28 17:14:55 +00:00
new_data_part - > disk - > createDirectories ( full_path ) ;
2017-05-16 15:40:32 +00:00
2018-02-19 17:31:30 +00:00
/// If we need to calculate some columns to sort.
2019-02-05 14:50:25 +00:00
if ( data . hasSortingKey ( ) | | data . hasSkipIndices ( ) )
data . sorting_key_and_skip_indices_expr - > execute ( block ) ;
2014-03-13 17:44:00 +00:00
2018-11-09 19:01:39 +00:00
Names sort_columns = data . sorting_key_columns ;
2018-06-30 21:35:01 +00:00
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 )
sort_description . emplace_back ( block . getPositionByName ( sort_columns [ i ] ) , 1 , 1 ) ;
2014-03-14 17:03:52 +00:00
2017-04-01 07:20:54 +00:00
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataWriterBlocks ) ;
2016-04-23 02:39:40 +00:00
2019-10-17 16:01:28 +00:00
/// Sort
2017-04-01 07:20:54 +00:00
IColumn : : Permutation * perm_ptr = nullptr ;
IColumn : : Permutation perm ;
2018-10-11 14:53:23 +00:00
if ( ! sort_description . empty ( ) )
2017-04-01 07:20:54 +00:00
{
2018-06-30 21:35:01 +00:00
if ( ! isAlreadySorted ( block , sort_description ) )
2017-04-01 07:20:54 +00:00
{
2018-06-30 21:35:01 +00:00
stableGetPermutation ( block , sort_description , perm ) ;
2017-04-01 07:20:54 +00:00
perm_ptr = & perm ;
}
else
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataWriterBlocksAlreadySorted ) ;
}
2014-03-13 17:44:00 +00:00
2020-01-22 19:52:55 +00:00
if ( data . hasRowsTTL ( ) )
updateTTL ( data . rows_ttl_entry , new_data_part - > ttl_infos , new_data_part - > ttl_infos . table_ttl , block , true ) ;
2019-04-15 09:30:45 +00:00
2019-11-26 08:02:48 +00:00
for ( const auto & [ name , ttl_entry ] : data . column_ttl_entries_by_name )
2019-11-29 05:41:09 +00:00
updateTTL ( ttl_entry , new_data_part - > ttl_infos , new_data_part - > ttl_infos . columns_ttl [ name ] , block , true ) ;
2019-04-15 09:30:45 +00:00
2019-10-28 23:01:07 +00:00
new_data_part - > ttl_infos . update ( move_ttl_infos ) ;
2019-04-15 09:30:45 +00:00
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.
2019-01-04 12:10:00 +00:00
auto compression_codec = data . global_context . chooseCompressionCodec ( 0 , 0 ) ;
2017-07-31 11:05:49 +00:00
2020-04-10 13:36:51 +00:00
MergedBlockOutputStream out ( new_data_part , columns , data . skip_indices , compression_codec ) ;
2015-03-14 02:36:39 +00:00
2017-04-01 07:20:54 +00:00
out . writePrefix ( ) ;
out . writeWithPermutation ( block , perm_ptr ) ;
2017-08-30 19:03:19 +00:00
out . writeSuffixAndFinalizePart ( new_data_part ) ;
2014-03-13 17:44:00 +00:00
2017-04-01 07:20:54 +00:00
ProfileEvents : : increment ( ProfileEvents : : MergeTreeDataWriterRows , block . rows ( ) ) ;
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
2017-04-01 07:20:54 +00:00
return new_data_part ;
2014-03-13 17:44:00 +00:00
}
}