aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/LiveView/StorageLiveView.cpp
blob: aec2405b97364804ec256b5cdefb68d6cbaa620f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/* Copyright (c) 2018 BlackBerry Limited

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTWatchQuery.h>
#include <Parsers/ASTLiteral.h>
#include <Interpreters/Context.h>
#include <Interpreters/InterpreterSelectQuery.h>
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
#include <Processors/Sources/BlocksSource.h>
#include <Processors/Sinks/EmptySink.h>
#include <Processors/Transforms/MaterializingTransform.h>
#include <Processors/Executors/PullingAsyncPipelineExecutor.h>
#include <Processors/Executors/PipelineExecutor.h>
#include <Processors/Transforms/SquashingChunksTransform.h>
#include <QueryPipeline/QueryPipelineBuilder.h>
#include <QueryPipeline/QueryPlanResourceHolder.h>
#include <Common/logger_useful.h>
#include <Common/typeid_cast.h>
#include <Common/SipHash.h>
#include <base/hex.h>

#include <Storages/LiveView/StorageLiveView.h>
#include <Storages/LiveView/LiveViewSource.h>
#include <Storages/LiveView/LiveViewSink.h>
#include <Storages/LiveView/LiveViewEventsSource.h>
#include <Storages/LiveView/StorageBlocks.h>

#include <Storages/StorageFactory.h>
#include <Interpreters/DatabaseAndTableWithAlias.h>
#include <Interpreters/DatabaseCatalog.h>
#include <Interpreters/getTableExpressions.h>
#include <Interpreters/AddDefaultDatabaseVisitor.h>
#include <Access/Common/AccessFlags.h>
#include <Processors/Sources/SourceFromSingleChunk.h>

#include <Analyzer/QueryTreeBuilder.h>
#include <Analyzer/QueryTreePassManager.h>
#include <Analyzer/TableNode.h>
#include <Analyzer/QueryNode.h>
#include <Analyzer/Utils.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int INCORRECT_QUERY;
    extern const int TABLE_WAS_NOT_DROPPED;
    extern const int QUERY_IS_NOT_SUPPORTED_IN_LIVE_VIEW;
    extern const int SUPPORT_IS_DISABLED;
    extern const int UNSUPPORTED_METHOD;
}

namespace
{

Pipes blocksToPipes(BlocksPtrs blocks, Block & sample_block)
{
    Pipes pipes;
    for (auto & blocks_for_source : *blocks)
        pipes.emplace_back(std::make_shared<BlocksSource>(blocks_for_source, sample_block));

    return pipes;
}

SelectQueryDescription buildSelectQueryDescription(const ASTPtr & select_query, const ContextPtr & context, std::string replace_table_name = {})
{
    ASTPtr inner_query = select_query;
    std::optional<StorageID> dependent_table_storage_id;

    while (true)
    {
        auto * inner_select_with_union_query = inner_query->as<ASTSelectWithUnionQuery>();

        if (inner_select_with_union_query)
        {
            if (inner_select_with_union_query->list_of_selects->children.size() != 1)
                throw Exception(ErrorCodes::QUERY_IS_NOT_SUPPORTED_IN_LIVE_VIEW, "UNION is not supported for LIVE VIEW");

            inner_query = inner_select_with_union_query->list_of_selects->children[0];
        }

        auto * inner_select_query = inner_query->as<ASTSelectQuery>();
        if (!inner_select_query)
            throw Exception(DB::ErrorCodes::QUERY_IS_NOT_SUPPORTED_IN_LIVE_VIEW,
                "LIVE VIEWs are only supported for queries from tables, "
                "but there is no table name in select query.");

        if (auto db_and_table = getDatabaseAndTable(*inner_select_query, 0))
        {
            String select_database_name = db_and_table->database;
            String select_table_name = db_and_table->table;

            if (select_database_name.empty())
            {
                select_database_name = context->getCurrentDatabase();
                db_and_table->database = select_database_name;
                AddDefaultDatabaseVisitor visitor(context, select_database_name);
                visitor.visit(*inner_select_query);
            }

            if (replace_table_name.empty())
            {
                dependent_table_storage_id = StorageID(select_database_name, select_table_name);
            }
            else
            {
                inner_select_query->replaceDatabaseAndTable("", replace_table_name);
                dependent_table_storage_id = StorageID("", replace_table_name);
            }

            break;
        }
        else if (auto subquery = extractTableExpression(*inner_select_query, 0))
        {
            inner_query = subquery;
        }
        else
        {
            break;
        }
    }

    if (!dependent_table_storage_id)
    {
        /// If the table is not specified - use the table `system.one`
        dependent_table_storage_id = StorageID("system", "one");
    }

    SelectQueryDescription result;
    result.select_table_id = *dependent_table_storage_id;
    result.select_query = select_query;
    result.inner_query = std::move(inner_query);

    return result;
}

struct SelectQueryTreeDescription
{
    QueryTreeNodePtr select_query_node;
    QueryTreeNodePtr inner_query_node;
    QueryTreeNodePtr dependent_table_node;
};

SelectQueryTreeDescription buildSelectQueryTreeDescription(const ASTPtr & select_query, const ContextPtr & context)
{
    auto select_query_node = buildQueryTree(select_query, context);

    QueryTreePassManager query_tree_pass_manager(context);
    addQueryTreePasses(query_tree_pass_manager);
    query_tree_pass_manager.run(select_query_node);

    QueryTreeNodePtr inner_query_node = select_query_node;
    QueryTreeNodePtr dependent_table_node;

    while (true)
    {
        auto & query_node = inner_query_node->as<QueryNode &>();

        auto left_table_expression = extractLeftTableExpression(query_node.getJoinTree());
        auto left_table_expression_node_type = left_table_expression->getNodeType();

        if (left_table_expression_node_type == QueryTreeNodeType::QUERY)
        {
            inner_query_node = left_table_expression;
        }
        else if (left_table_expression_node_type == QueryTreeNodeType::TABLE)
        {
            dependent_table_node = left_table_expression;
            break;
        }
        else if (left_table_expression_node_type == QueryTreeNodeType::TABLE_FUNCTION)
        {
            break;
        }
        else
        {
            throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
                "LiveView does not support UNION {} subquery",
                left_table_expression->formatASTForErrorMessage());
        }
    }

    return {std::move(select_query_node), std::move(inner_query_node), std::move(dependent_table_node)};
}

}

StorageLiveView::StorageLiveView(
    const StorageID & table_id_,
    ContextPtr context_,
    const ASTCreateQuery & query,
    const ColumnsDescription & columns_,
    const String & comment)
    : IStorage(table_id_)
    , WithContext(context_->getGlobalContext())
{
    live_view_context = Context::createCopy(getContext());
    live_view_context->makeQueryContext();

    log = &Poco::Logger::get("StorageLiveView (" + table_id_.database_name + "." + table_id_.table_name + ")");

    StorageInMemoryMetadata storage_metadata;
    storage_metadata.setColumns(columns_);
    if (!comment.empty())
        storage_metadata.setComment(comment);

    setInMemoryMetadata(storage_metadata);

    if (!query.select)
        throw Exception(ErrorCodes::INCORRECT_QUERY, "SELECT query is not specified for {}", getName());

    auto select_query_clone = query.select->clone();
    select_query_description = buildSelectQueryDescription(select_query_clone, getContext());

    DatabaseCatalog::instance().addViewDependency(select_query_description.select_table_id, table_id_);

    if (query.live_view_periodic_refresh)
    {
        is_periodically_refreshed = true;
        periodic_live_view_refresh = Seconds {*query.live_view_periodic_refresh};
    }

    blocks_ptr = std::make_shared<BlocksPtr>();
    blocks_metadata_ptr = std::make_shared<BlocksMetadataPtr>();
    active_ptr = std::make_shared<bool>(true);

    periodic_refresh_task = getContext()->getSchedulePool().createTask("LiveViewPeriodicRefreshTask",
        [this]
        {
            try
            {
                periodicRefreshTaskFunc();
            }
            catch (...)
            {
                tryLogCurrentException(log, "Exception in LiveView periodic refresh task in BackgroundSchedulePool");
            }
        });
    periodic_refresh_task->deactivate();
}

StorageLiveView::~StorageLiveView()
{
    shutdown();
}

NamesAndTypesList StorageLiveView::getVirtuals() const
{
    return NamesAndTypesList{
        NameAndTypePair("_version", std::make_shared<DataTypeUInt64>())
    };
}

void StorageLiveView::checkTableCanBeDropped([[ maybe_unused ]] ContextPtr query_context) const
{
    auto table_id = getStorageID();
    auto view_ids = DatabaseCatalog::instance().getDependentViews(table_id);
    if (!view_ids.empty())
    {
        StorageID view_id = *view_ids.begin();
        throw Exception(ErrorCodes::TABLE_WAS_NOT_DROPPED, "Table has dependency {}", view_id);
    }
}

void StorageLiveView::drop()
{
    auto table_id = getStorageID();
    DatabaseCatalog::instance().removeViewDependency(select_query_description.select_table_id, table_id);

    std::lock_guard lock(mutex);
    condition.notify_all();
}

void StorageLiveView::startup()
{
    if (is_periodically_refreshed)
        periodic_refresh_task->activate();
}

void StorageLiveView::shutdown()
{
    shutdown_called = true;

    if (is_periodically_refreshed)
        periodic_refresh_task->deactivate();

    DatabaseCatalog::instance().removeViewDependency(select_query_description.select_table_id, getStorageID());
}

Pipe StorageLiveView::read(
    const Names & /*column_names*/,
    const StorageSnapshotPtr & /*storage_snapshot*/,
    SelectQueryInfo & /*query_info*/,
    ContextPtr /*context*/,
    QueryProcessingStage::Enum /*processed_stage*/,
    const size_t /*max_block_size*/,
    const size_t /*num_streams*/)
{
    std::lock_guard lock(mutex);

    if (!(*blocks_ptr))
    {
        refreshImpl(lock);
    }
    else if (is_periodically_refreshed)
    {
        Seconds current_time = std::chrono::duration_cast<Seconds>(std::chrono::system_clock::now().time_since_epoch());
        Seconds blocks_time = std::chrono::duration_cast<Seconds>(getBlocksTime(lock).time_since_epoch());

        if ((current_time - periodic_live_view_refresh) >= blocks_time)
            refreshImpl(lock);
    }

    return Pipe(std::make_shared<BlocksSource>(*blocks_ptr, getHeader()));
}

Pipe StorageLiveView::watch(
    const Names & /*column_names*/,
    const SelectQueryInfo & query_info,
    ContextPtr local_context,
    QueryProcessingStage::Enum & processed_stage,
    size_t /*max_block_size*/,
    const size_t /*num_streams*/)
{
    ASTWatchQuery & query = typeid_cast<ASTWatchQuery &>(*query_info.query);

    bool has_limit = false;
    UInt64 limit = 0;
    Pipe reader;

    if (query.limit_length)
    {
        has_limit = true;
        limit = typeid_cast<ASTLiteral &>(*query.limit_length).value.safeGet<UInt64>();
    }

    if (query.is_watch_events)
        reader = Pipe(std::make_shared<LiveViewEventsSource>(
            std::static_pointer_cast<StorageLiveView>(shared_from_this()),
            blocks_ptr, blocks_metadata_ptr, active_ptr, has_limit, limit,
            local_context->getSettingsRef().live_view_heartbeat_interval.totalSeconds()));
    else
        reader = Pipe(std::make_shared<LiveViewSource>(
            std::static_pointer_cast<StorageLiveView>(shared_from_this()),
            blocks_ptr, blocks_metadata_ptr, active_ptr, has_limit, limit,
            local_context->getSettingsRef().live_view_heartbeat_interval.totalSeconds()));

    {
        std::lock_guard lock(mutex);

        if (!(*blocks_ptr))
            refreshImpl(lock);

        if (is_periodically_refreshed)
            scheduleNextPeriodicRefresh(lock);
    }

    processed_stage = QueryProcessingStage::Complete;
    return reader;
}

void StorageLiveView::writeBlock(const Block & block, ContextPtr local_context)
{
    auto output = std::make_shared<LiveViewSink>(*this);

    /// Check if live view has any readers if not
    /// just reset blocks to empty and do nothing else
    /// When first reader comes the blocks will be read.
    {
        std::lock_guard lock(mutex);
        if (!hasActiveUsers(lock))
        {
            reset(lock);
            return;
        }
    }

    bool is_block_processed = false;
    Pipes from;
    BlocksPtr new_mergeable_blocks = std::make_shared<Blocks>();

    {
        std::lock_guard lock(mutex);
        if (!mergeable_blocks || mergeable_blocks->blocks->size() >= local_context->getGlobalContext()->getSettingsRef().max_live_view_insert_blocks_before_refresh)
        {
            mergeable_blocks = collectMergeableBlocks(local_context, lock);
            from = blocksToPipes(mergeable_blocks->blocks, mergeable_blocks->sample_block);
            is_block_processed = true;
        }
    }

    if (!is_block_processed)
    {
        Pipes pipes;
        pipes.emplace_back(std::make_shared<SourceFromSingleChunk>(block));

        auto creator = [&](const StorageID & blocks_id_global)
        {
            auto parent_metadata = getDependentTableStorage()->getInMemoryMetadataPtr();
            return StorageBlocks::createStorage(
                blocks_id_global, parent_metadata->getColumns(),
                std::move(pipes), QueryProcessingStage::FetchColumns);
        };
        TemporaryTableHolder blocks_storage(local_context, creator);

        QueryPipelineBuilder builder;

        if (local_context->getSettingsRef().allow_experimental_analyzer)
        {
            auto select_description = buildSelectQueryTreeDescription(select_query_description.inner_query, local_context);
            if (select_description.dependent_table_node)
            {
                auto storage = blocks_storage.getTable();
                auto storage_snapshot = storage->getStorageSnapshot(storage->getInMemoryMetadataPtr(), local_context);
                auto replacement_table_expression = std::make_shared<TableNode>(std::move(storage),
                    TableLockHolder{},
                    std::move(storage_snapshot));

                select_description.inner_query_node = select_description.inner_query_node->cloneAndReplace(
                    select_description.dependent_table_node,
                    std::move(replacement_table_expression));
            }

            InterpreterSelectQueryAnalyzer interpreter(select_description.inner_query_node,
                local_context,
                SelectQueryOptions(QueryProcessingStage::WithMergeableState));
            builder = interpreter.buildQueryPipeline();
        }
        else
        {
            InterpreterSelectQuery interpreter(select_query_description.inner_query,
                local_context,
                blocks_storage.getTable(),
                blocks_storage.getTable()->getInMemoryMetadataPtr(),
                QueryProcessingStage::WithMergeableState);
            builder = interpreter.buildQueryPipeline();
        }

        builder.addSimpleTransform([&](const Block & cur_header)
        {
            return std::make_shared<MaterializingTransform>(cur_header);
        });

        auto pipeline = QueryPipelineBuilder::getPipeline(std::move(builder));
        PullingAsyncPipelineExecutor executor(pipeline);
        Block this_block;

        while (executor.pull(this_block))
            new_mergeable_blocks->push_back(this_block);

        if (new_mergeable_blocks->empty())
            return;

        {
            std::lock_guard lock(mutex);
            mergeable_blocks->blocks->push_back(new_mergeable_blocks);
            from = blocksToPipes(mergeable_blocks->blocks, mergeable_blocks->sample_block);
        }
    }

    auto pipeline = completeQuery(std::move(from));
    pipeline.addChain(Chain(std::move(output)));
    pipeline.setSinks([&](const Block & cur_header, Pipe::StreamType)
    {
        return std::make_shared<EmptySink>(cur_header);
    });

    auto executor = pipeline.execute();
    executor->execute(pipeline.getNumThreads(), local_context->getSettingsRef().use_concurrency_control);
}

void StorageLiveView::refresh()
{
    std::lock_guard lock(mutex);
    refreshImpl(lock);
}

void StorageLiveView::refreshImpl(const std::lock_guard<std::mutex> & lock)
{
    if (getNewBlocks(lock))
        condition.notify_all();
}

Block StorageLiveView::getHeader() const
{
    std::lock_guard lock(sample_block_lock);

    if (!sample_block)
    {
        if (live_view_context->getSettingsRef().allow_experimental_analyzer)
        {
            sample_block = InterpreterSelectQueryAnalyzer::getSampleBlock(select_query_description.select_query,
                live_view_context,
                SelectQueryOptions(QueryProcessingStage::Complete));
        }
        else
        {
            auto & select_with_union_query = select_query_description.select_query->as<ASTSelectWithUnionQuery &>();
            auto select_query = select_with_union_query.list_of_selects->children.at(0)->clone();
            sample_block = InterpreterSelectQuery(select_query,
                live_view_context,
                SelectQueryOptions(QueryProcessingStage::Complete)).getSampleBlock();
        }

        sample_block.insert({DataTypeUInt64().createColumnConst(
            sample_block.rows(), 0)->convertToFullColumnIfConst(),
            std::make_shared<DataTypeUInt64>(),
            "_version"});

        /// convert all columns to full columns
        /// in case some of them are constant
        for (size_t i = 0; i < sample_block.columns(); ++i)
        {
            sample_block.safeGetByPosition(i).column = sample_block.safeGetByPosition(i).column->convertToFullColumnIfConst();
        }
    }

    return sample_block;
}

StoragePtr StorageLiveView::getDependentTableStorage() const
{
    return DatabaseCatalog::instance().getTable(select_query_description.select_table_id, getContext());
}

ASTPtr StorageLiveView::getInnerBlocksQuery()
{
    std::lock_guard lock(sample_block_lock);
    if (!inner_blocks_query)
    {
        auto & select_with_union_query = select_query_description.select_query->as<ASTSelectWithUnionQuery &>();
        auto blocks_query = select_with_union_query.list_of_selects->children.at(0)->clone();

        if (!live_view_context->getSettingsRef().allow_experimental_analyzer)
        {
            /// Rewrite inner query with right aliases for JOIN.
            /// It cannot be done in constructor or startup() because InterpreterSelectQuery may access table,
            /// which is not loaded yet during server startup, so we do it lazily
            InterpreterSelectQuery(blocks_query, live_view_context, SelectQueryOptions().modify().analyze()); // NOLINT
        }

        auto table_id = getStorageID();
        std::string replace_table_name = table_id.table_name + "_blocks";
        inner_blocks_query = buildSelectQueryDescription(blocks_query, getContext(), replace_table_name).select_query;
    }

    return inner_blocks_query->clone();
}

MergeableBlocksPtr StorageLiveView::collectMergeableBlocks(ContextPtr local_context, const std::lock_guard<std::mutex> &) const
{
    MergeableBlocksPtr new_mergeable_blocks = std::make_shared<MergeableBlocks>();
    BlocksPtrs new_blocks = std::make_shared<std::vector<BlocksPtr>>();
    BlocksPtr base_blocks = std::make_shared<Blocks>();

    QueryPipelineBuilder builder;

    if (local_context->getSettingsRef().allow_experimental_analyzer)
    {
        InterpreterSelectQueryAnalyzer interpreter(select_query_description.inner_query,
            local_context,
            SelectQueryOptions(QueryProcessingStage::WithMergeableState));
        builder = interpreter.buildQueryPipeline();
    }
    else
    {
        InterpreterSelectQuery interpreter(select_query_description.inner_query->clone(),
            local_context,
            SelectQueryOptions(QueryProcessingStage::WithMergeableState), Names());
        builder = interpreter.buildQueryPipeline();
    }

    builder.addSimpleTransform([&](const Block & cur_header)
    {
        return std::make_shared<MaterializingTransform>(cur_header);
    });

    new_mergeable_blocks->sample_block = builder.getHeader();

    auto pipeline = QueryPipelineBuilder::getPipeline(std::move(builder));
    PullingAsyncPipelineExecutor executor(pipeline);
    Block this_block;

    while (executor.pull(this_block))
        base_blocks->push_back(this_block);

    new_blocks->push_back(base_blocks);
    new_mergeable_blocks->blocks = new_blocks;

    return new_mergeable_blocks;
}

/// Complete query using input streams from mergeable blocks
QueryPipelineBuilder StorageLiveView::completeQuery(Pipes pipes)
{
    auto block_context = Context::createCopy(getContext());
    block_context->makeQueryContext();

    QueryPlanResourceHolder resource_holder;
    resource_holder.interpreter_context.push_back(block_context);

    auto creator = [&](const StorageID & blocks_id_global)
    {
        auto parent_table_metadata = getDependentTableStorage()->getInMemoryMetadataPtr();
        return StorageBlocks::createStorage(
            blocks_id_global, parent_table_metadata->getColumns(),
            std::move(pipes), QueryProcessingStage::WithMergeableState);
    };

    TemporaryTableHolder blocks_storage_table_holder(getContext(), creator);

    QueryPipelineBuilder builder;

    if (block_context->getSettingsRef().allow_experimental_analyzer)
    {
        auto select_description = buildSelectQueryTreeDescription(select_query_description.select_query, block_context);

        if (select_description.dependent_table_node)
        {
            auto storage = blocks_storage_table_holder.getTable();
            auto storage_snapshot = storage->getStorageSnapshot(storage->getInMemoryMetadataPtr(), block_context);
            auto replacement_table_expression = std::make_shared<TableNode>(std::move(storage),
                TableLockHolder{},
                std::move(storage_snapshot));

            select_description.select_query_node = select_description.select_query_node->cloneAndReplace(
                select_description.dependent_table_node,
                std::move(replacement_table_expression));
        }

        InterpreterSelectQueryAnalyzer interpreter(select_description.select_query_node,
            block_context,
            SelectQueryOptions(QueryProcessingStage::Complete));
        builder = interpreter.buildQueryPipeline();
    }
    else
    {
        auto inner_blocks_query_ = getInnerBlocksQuery();
        block_context->addExternalTable(getBlocksTableName(), std::move(blocks_storage_table_holder));
        InterpreterSelectQuery interpreter(inner_blocks_query_,
            block_context,
            StoragePtr(),
            nullptr,
            SelectQueryOptions(QueryProcessingStage::Complete));
        builder = interpreter.buildQueryPipeline();
    }

    builder.addSimpleTransform([&](const Block & cur_header)
    {
        return std::make_shared<MaterializingTransform>(cur_header);
    });

    /// Squashing is needed here because the view query can generate a lot of blocks
    /// even when only one block is inserted into the parent table (e.g. if the query is a GROUP BY
    /// and two-level aggregation is triggered).
    builder.addSimpleTransform([&](const Block & cur_header)
    {
        return std::make_shared<SquashingChunksTransform>(
            cur_header,
            getContext()->getSettingsRef().min_insert_block_size_rows,
            getContext()->getSettingsRef().min_insert_block_size_bytes);
    });

    builder.addResources(std::move(resource_holder));

    return builder;
}

bool StorageLiveView::getNewBlocks(const std::lock_guard<std::mutex> & lock)
{
    SipHash hash;
    BlocksPtr new_blocks = std::make_shared<Blocks>();
    BlocksMetadataPtr new_blocks_metadata = std::make_shared<BlocksMetadata>();

    /// can't set mergeable_blocks here or anywhere else outside the writeIntoLiveView function
    /// as there could be a race condition when the new block has been inserted into
    /// the source table by the PushingToViews chain and this method
    /// called before writeIntoLiveView function is called which can lead to
    /// the same block added twice to the mergeable_blocks leading to
    /// inserted data to be duplicated
    auto new_mergeable_blocks = collectMergeableBlocks(live_view_context, lock);
    Pipes from = blocksToPipes(new_mergeable_blocks->blocks, new_mergeable_blocks->sample_block);
    auto builder = completeQuery(std::move(from));
    auto pipeline = QueryPipelineBuilder::getPipeline(std::move(builder));

    PullingAsyncPipelineExecutor executor(pipeline);
    Block block;
    while (executor.pull(block))
    {
        if (block.rows() == 0)
            continue;

        /// calculate hash before virtual column is added
        block.updateHash(hash);
        /// add result version meta column
        block.insert({DataTypeUInt64().createColumnConst(
            block.rows(), getBlocksVersion(lock) + 1)->convertToFullColumnIfConst(),
            std::make_shared<DataTypeUInt64>(),
            "_version"});
        new_blocks->push_back(block);
    }

    const auto key = hash.get128();

    /// Update blocks only if hash keys do not match
    /// NOTE: hash could be different for the same result
    ///       if blocks are not in the same order
    bool updated = false;
    {
        if (getBlocksHashKey(lock) != getHexUIntLowercase(key))
        {
            if (new_blocks->empty())
            {
                new_blocks->push_back(getHeader());
            }
            new_blocks_metadata->hash = getHexUIntLowercase(key);
            new_blocks_metadata->version = getBlocksVersion(lock) + 1;
            new_blocks_metadata->time = std::chrono::system_clock::now();

            (*blocks_ptr) = new_blocks;
            (*blocks_metadata_ptr) = new_blocks_metadata;

            updated = true;
        }
        else
        {
            new_blocks_metadata->hash = getBlocksHashKey(lock);
            new_blocks_metadata->version = getBlocksVersion(lock);
            new_blocks_metadata->time = std::chrono::system_clock::now();

            (*blocks_metadata_ptr) = new_blocks_metadata;
        }
    }
    return updated;
}

void StorageLiveView::periodicRefreshTaskFunc()
{
    LOG_TRACE(log, "periodic refresh task");

    std::lock_guard lock(mutex);

    if (hasActiveUsers(lock))
        scheduleNextPeriodicRefresh(lock);
}

void StorageLiveView::scheduleNextPeriodicRefresh(const std::lock_guard<std::mutex> & lock)
{
    Seconds current_time = std::chrono::duration_cast<Seconds>(std::chrono::system_clock::now().time_since_epoch());
    Seconds blocks_time = std::chrono::duration_cast<Seconds>(getBlocksTime(lock).time_since_epoch());

    if ((current_time - periodic_live_view_refresh) >= blocks_time)
    {
        refreshImpl(lock);
        blocks_time = std::chrono::duration_cast<Seconds>(getBlocksTime(lock).time_since_epoch());
    }
    current_time = std::chrono::duration_cast<Seconds>(std::chrono::system_clock::now().time_since_epoch());

    auto next_refresh_time = blocks_time + periodic_live_view_refresh;

    if (current_time >= next_refresh_time)
        periodic_refresh_task->scheduleAfter(0);
    else
    {
        auto schedule_time = std::chrono::duration_cast<MilliSeconds> (next_refresh_time - current_time);
        periodic_refresh_task->scheduleAfter(static_cast<size_t>(schedule_time.count()));
    }
}

void registerStorageLiveView(StorageFactory & factory)
{
    factory.registerStorage("LiveView", [](const StorageFactory::Arguments & args)
    {
        if (!args.attach && !args.getLocalContext()->getSettingsRef().allow_experimental_live_view)
            throw Exception(ErrorCodes::SUPPORT_IS_DISABLED,
                            "Experimental LIVE VIEW feature is not enabled (the setting 'allow_experimental_live_view')");

        return std::make_shared<StorageLiveView>(args.table_id, args.getLocalContext(), args.query, args.columns, args.comment);
    });
}

}