3636#include < Common/logger_useful.h>
3737#include < Common/randomSeed.h>
3838
39-
4039namespace DB
4140{
4241
@@ -66,7 +65,6 @@ void fillBufferWithRandomData(char * __restrict data, size_t size, pcg64 & rng)
6665 }
6766}
6867
69-
7068ColumnPtr fillColumnWithRandomData (const DataTypePtr type, UInt64 limit, pcg64 & rng, ContextPtr context)
7169{
7270 TypeIndex idx = type->getTypeId ();
@@ -353,6 +351,28 @@ ColumnPtr fillColumnWithRandomData(const DataTypePtr type, UInt64 limit, pcg64 &
353351 }
354352}
355353
354+ ColumnPtr
355+ fillColumnWithData (const DataTypePtr type, UInt64 limit, std::tuple<Int64, Int32, pcg64> & data, ContextPtr context, String col_name)
356+ {
357+ if (col_name == ProtonConsts::RESERVED_SHARD)
358+ {
359+ auto & shard_num = std::get<0 >(data);
360+ auto column = type->createColumnConst (limit, shard_num)->convertToFullColumnIfConst ();
361+ return column;
362+ }
363+ else if (col_name == ProtonConsts::RESERVED_EVENT_SEQUENCE_ID)
364+ {
365+ auto & sn = std::get<1 >(data);
366+ auto column = type->createColumnConst (limit, sn)->convertToFullColumnIfConst ();
367+ sn++;
368+ return column;
369+ }
370+ else
371+ {
372+ auto & rng = std::get<2 >(data);
373+ return fillColumnWithRandomData (type, limit, rng, context);
374+ }
375+ }
356376
357377class GenerateRandomSource final : public ISource
358378{
@@ -366,12 +386,12 @@ class GenerateRandomSource final : public ISource
366386 UInt64 events_per_second_,
367387 UInt64 interval_time_,
368388 bool is_streaming_,
369- UInt64 total_events_)
389+ UInt64 total_events_,
390+ size_t shard_num_)
370391 : ISource(Nested::flatten(prepareBlockToFill(block_header_)), true , ProcessorID::GenerateRandomSourceID)
371392 , block_size(block_size_)
372393 , block_full(std::move(block_header_))
373394 , our_columns(our_columns_)
374- , rng(random_seed_)
375395 , context(context_)
376396 , events_per_second(events_per_second_)
377397 , header_chunk(Nested::flatten(block_full.cloneEmpty()).getColumns(), 0 )
@@ -380,7 +400,7 @@ class GenerateRandomSource final : public ISource
380400 , log(&Poco::Logger::get (" GenerateRandSource" ))
381401 {
382402 is_streaming = is_streaming_;
383-
403+ data_generate_helper = std::make_tuple (shard_num_, 1 , pcg64 (random_seed_));
384404 if (total_events == 0 && !is_streaming)
385405 total_events = events_per_second ? events_per_second : block_size;
386406
@@ -414,8 +434,17 @@ class GenerateRandomSource final : public ISource
414434 != ProtonConsts::RESERVED_COLUMN_NAMES.end ();
415435 if (is_reserved_column || our_columns.hasDefault (elem.name ))
416436 continue ;
437+
417438 block_to_fill.insert (elem);
418439 }
440+
441+ auto dag
442+ = evaluateMissingDefaults (block_to_fill, block_full.getNamesAndTypesList (), our_columns, context, true , false , true );
443+ if (dag)
444+ {
445+ default_actions = std::make_shared<ExpressionActions>(
446+ std::move (dag), ExpressionActionsSettings::fromContext (context, CompileExpressions::yes));
447+ }
419448 }
420449
421450 String getName () const override { return " Random" ; }
@@ -493,7 +522,7 @@ class GenerateRandomSource final : public ISource
493522 Block block_to_fill_as_result (block_to_fill.cloneEmpty ());
494523
495524 for (const auto & elem : block_to_fill_as_result)
496- columns.emplace_back (fillColumnWithRandomData (elem.type , block_size_, rng , context));
525+ columns.emplace_back (fillColumnWithData (elem.type , block_size_, data_generate_helper , context, elem. name ));
497526
498527 block_to_fill_as_result.setColumns (columns);
499528
@@ -504,13 +533,8 @@ class GenerateRandomSource final : public ISource
504533 block_to_fill_as_result.insert (
505534 {ColumnConst::create (ColumnUInt8::create (1 , 0 ), block_size_), std::make_shared<DataTypeUInt8>(), " _dummy" });
506535
507- auto dag = evaluateMissingDefaults (block_to_fill_as_result, block_full.getNamesAndTypesList (), our_columns, context);
508- if (dag)
509- {
510- auto actions = std::make_shared<ExpressionActions>(
511- std::move (dag), ExpressionActionsSettings::fromContext (context, CompileExpressions::yes));
512- actions->execute (block_to_fill_as_result);
513- }
536+ if (default_actions)
537+ default_actions->execute (block_to_fill_as_result);
514538
515539 if (block_to_fill_as_result.has (ProtonConsts::RESERVED_COLUMN_NAMES[0 ])
516540 && block_to_fill_as_result.has (ProtonConsts::RESERVED_COLUMN_NAMES[1 ]))
@@ -528,7 +552,6 @@ class GenerateRandomSource final : public ISource
528552 Block block_full;
529553 Block block_to_fill;
530554 const ColumnsDescription our_columns;
531- pcg64 rng;
532555 ContextPtr context;
533556 Int64 boundary_time;
534557 UInt64 block_idx_in_window;
@@ -546,6 +569,9 @@ class GenerateRandomSource final : public ISource
546569 UInt64 total_events;
547570 UInt64 generated_events = 0 ;
548571 Poco::Logger * log;
572+ std::shared_ptr<ExpressionActions> default_actions = nullptr ;
573+ // <shard_num, sequence_num, rng>
574+ std::tuple<Int64, Int32, pcg64> data_generate_helper;
549575
550576 static Block & prepareBlockToFill (Block & block)
551577 {
@@ -717,7 +743,8 @@ Pipe StorageRandom::read(
717743 0 ,
718744 1000 ,
719745 query_info.syntax_analyzer_result ->streaming ,
720- events_share));
746+ events_share,
747+ i));
721748 }
722749
723750 pipes.emplace_back (std::make_shared<GenerateRandomSource>(
@@ -729,7 +756,8 @@ Pipe StorageRandom::read(
729756 0 ,
730757 1000 ,
731758 query_info.syntax_analyzer_result ->streaming ,
732- events_share + events_remainder));
759+ events_share + events_remainder,
760+ shards - 1 ));
733761 }
734762 else
735763 {
@@ -743,7 +771,8 @@ Pipe StorageRandom::read(
743771 eps,
744772 1000 ,
745773 query_info.syntax_analyzer_result ->streaming ,
746- max_events));
774+ max_events,
775+ 0 ));
747776 }
748777 }
749778 else
@@ -762,7 +791,8 @@ Pipe StorageRandom::read(
762791 eps_thread,
763792 interval_time,
764793 query_info.syntax_analyzer_result ->streaming ,
765- events_share));
794+ events_share,
795+ i));
766796 }
767797
768798 // / The last thread will do the remaining work
@@ -775,9 +805,17 @@ Pipe StorageRandom::read(
775805 eps_thread + remainder,
776806 interval_time,
777807 query_info.syntax_analyzer_result ->streaming ,
778- events_share + events_remainder));
808+ events_share + events_remainder,
809+ shards - 1 ));
779810 }
780811 return Pipe::unitePipes (std::move (pipes));
781812}
782813
814+ NamesAndTypesList StorageRandom::getVirtuals () const
815+ {
816+ return NamesAndTypesList {
817+ {ProtonConsts::RESERVED_EVENT_SEQUENCE_ID, std::make_shared<DataTypeInt64>()},
818+ {ProtonConsts::RESERVED_SHARD, std::make_shared<DataTypeInt32>()},
819+ };
820+ }
783821}
0 commit comments