1#include <Interpreters/SubqueryForSet.h>
2#include <Interpreters/InterpreterSelectWithUnionQuery.h>
3#include <Interpreters/Join.h>
4#include <Interpreters/MergeJoin.h>
5#include <DataStreams/LazyBlockInputStream.h>
6
7namespace DB
8{
9
10void SubqueryForSet::makeSource(std::shared_ptr<InterpreterSelectWithUnionQuery> & interpreter,
11 NamesWithAliases && joined_block_aliases_)
12{
13 joined_block_aliases = std::move(joined_block_aliases_);
14 source = std::make_shared<LazyBlockInputStream>(interpreter->getSampleBlock(),
15 [interpreter]() mutable { return interpreter->execute().in; });
16
17 sample_block = source->getHeader();
18 renameColumns(sample_block);
19}
20
21void SubqueryForSet::renameColumns(Block & block)
22{
23 for (const auto & name_with_alias : joined_block_aliases)
24 {
25 if (block.has(name_with_alias.first))
26 {
27 auto pos = block.getPositionByName(name_with_alias.first);
28 auto column = block.getByPosition(pos);
29 block.erase(pos);
30 column.name = name_with_alias.second;
31 block.insert(std::move(column));
32 }
33 }
34}
35
36void SubqueryForSet::setJoinActions(ExpressionActionsPtr actions)
37{
38 actions->execute(sample_block);
39 joined_block_actions = actions;
40}
41
42bool SubqueryForSet::insertJoinedBlock(Block & block)
43{
44 renameColumns(block);
45
46 if (joined_block_actions)
47 joined_block_actions->execute(block);
48
49 return join->addJoinedBlock(block);
50}
51
52void SubqueryForSet::setTotals()
53{
54 if (join && source)
55 join->setTotals(source->getTotals());
56}
57
58}
59