36
36
#include < Common/logger_useful.h>
37
37
#include < Common/randomSeed.h>
38
38
39
-
40
39
namespace DB
41
40
{
42
41
@@ -66,7 +65,6 @@ void fillBufferWithRandomData(char * __restrict data, size_t size, pcg64 & rng)
66
65
}
67
66
}
68
67
69
-
70
68
ColumnPtr fillColumnWithRandomData (const DataTypePtr type, UInt64 limit, pcg64 & rng, ContextPtr context)
71
69
{
72
70
TypeIndex idx = type->getTypeId ();
@@ -353,6 +351,28 @@ ColumnPtr fillColumnWithRandomData(const DataTypePtr type, UInt64 limit, pcg64 &
353
351
}
354
352
}
355
353
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
+ }
356
376
357
377
class GenerateRandomSource final : public ISource
358
378
{
@@ -366,12 +386,12 @@ class GenerateRandomSource final : public ISource
366
386
UInt64 events_per_second_,
367
387
UInt64 interval_time_,
368
388
bool is_streaming_,
369
- UInt64 total_events_)
389
+ UInt64 total_events_,
390
+ size_t shard_num_)
370
391
: ISource(Nested::flatten(prepareBlockToFill(block_header_)), true , ProcessorID::GenerateRandomSourceID)
371
392
, block_size(block_size_)
372
393
, block_full(std::move(block_header_))
373
394
, our_columns(our_columns_)
374
- , rng(random_seed_)
375
395
, context(context_)
376
396
, events_per_second(events_per_second_)
377
397
, header_chunk(Nested::flatten(block_full.cloneEmpty()).getColumns(), 0 )
@@ -380,7 +400,7 @@ class GenerateRandomSource final : public ISource
380
400
, log(&Poco::Logger::get (" GenerateRandSource" ))
381
401
{
382
402
is_streaming = is_streaming_;
383
-
403
+ data_generate_helper = std::make_tuple (shard_num_, 1 , pcg64 (random_seed_));
384
404
if (total_events == 0 && !is_streaming)
385
405
total_events = events_per_second ? events_per_second : block_size;
386
406
@@ -414,8 +434,17 @@ class GenerateRandomSource final : public ISource
414
434
!= ProtonConsts::RESERVED_COLUMN_NAMES.end ();
415
435
if (is_reserved_column || our_columns.hasDefault (elem.name ))
416
436
continue ;
437
+
417
438
block_to_fill.insert (elem);
418
439
}
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
+ }
419
448
}
420
449
421
450
String getName () const override { return " Random" ; }
@@ -493,7 +522,7 @@ class GenerateRandomSource final : public ISource
493
522
Block block_to_fill_as_result (block_to_fill.cloneEmpty ());
494
523
495
524
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 ));
497
526
498
527
block_to_fill_as_result.setColumns (columns);
499
528
@@ -504,13 +533,8 @@ class GenerateRandomSource final : public ISource
504
533
block_to_fill_as_result.insert (
505
534
{ColumnConst::create (ColumnUInt8::create (1 , 0 ), block_size_), std::make_shared<DataTypeUInt8>(), " _dummy" });
506
535
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);
514
538
515
539
if (block_to_fill_as_result.has (ProtonConsts::RESERVED_COLUMN_NAMES[0 ])
516
540
&& block_to_fill_as_result.has (ProtonConsts::RESERVED_COLUMN_NAMES[1 ]))
@@ -528,7 +552,6 @@ class GenerateRandomSource final : public ISource
528
552
Block block_full;
529
553
Block block_to_fill;
530
554
const ColumnsDescription our_columns;
531
- pcg64 rng;
532
555
ContextPtr context;
533
556
Int64 boundary_time;
534
557
UInt64 block_idx_in_window;
@@ -546,6 +569,9 @@ class GenerateRandomSource final : public ISource
546
569
UInt64 total_events;
547
570
UInt64 generated_events = 0 ;
548
571
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;
549
575
550
576
static Block & prepareBlockToFill (Block & block)
551
577
{
@@ -717,7 +743,8 @@ Pipe StorageRandom::read(
717
743
0 ,
718
744
1000 ,
719
745
query_info.syntax_analyzer_result ->streaming ,
720
- events_share));
746
+ events_share,
747
+ i));
721
748
}
722
749
723
750
pipes.emplace_back (std::make_shared<GenerateRandomSource>(
@@ -729,7 +756,8 @@ Pipe StorageRandom::read(
729
756
0 ,
730
757
1000 ,
731
758
query_info.syntax_analyzer_result ->streaming ,
732
- events_share + events_remainder));
759
+ events_share + events_remainder,
760
+ shards - 1 ));
733
761
}
734
762
else
735
763
{
@@ -743,7 +771,8 @@ Pipe StorageRandom::read(
743
771
eps,
744
772
1000 ,
745
773
query_info.syntax_analyzer_result ->streaming ,
746
- max_events));
774
+ max_events,
775
+ 0 ));
747
776
}
748
777
}
749
778
else
@@ -762,7 +791,8 @@ Pipe StorageRandom::read(
762
791
eps_thread,
763
792
interval_time,
764
793
query_info.syntax_analyzer_result ->streaming ,
765
- events_share));
794
+ events_share,
795
+ i));
766
796
}
767
797
768
798
// / The last thread will do the remaining work
@@ -775,9 +805,17 @@ Pipe StorageRandom::read(
775
805
eps_thread + remainder ,
776
806
interval_time,
777
807
query_info.syntax_analyzer_result ->streaming ,
778
- events_share + events_remainder));
808
+ events_share + events_remainder,
809
+ shards - 1 ));
779
810
}
780
811
return Pipe::unitePipes (std::move (pipes));
781
812
}
782
813
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
+ }
783
821
}
0 commit comments