1#include <iostream>
2#include <iomanip>
3
4#include <IO/WriteBufferFromOStream.h>
5#include <IO/ReadHelpers.h>
6
7#include <Storages/System/StorageSystemNumbers.h>
8
9#include <DataStreams/LimitBlockInputStream.h>
10#include <DataStreams/ExpressionBlockInputStream.h>
11#include <Formats/FormatFactory.h>
12#include <DataStreams/copyData.h>
13
14#include <DataTypes/DataTypesNumber.h>
15
16#include <Parsers/ParserSelectQuery.h>
17#include <Parsers/parseQuery.h>
18
19#include <Interpreters/SyntaxAnalyzer.h>
20#include <Interpreters/ExpressionAnalyzer.h>
21#include <Interpreters/ExpressionActions.h>
22#include <Interpreters/Context.h>
23
24
25int main(int argc, char ** argv)
26try
27{
28 using namespace DB;
29
30 size_t n = argc == 2 ? parse<UInt64>(argv[1]) : 10ULL;
31
32 std::string input = "SELECT number, number / 3, number * number";
33
34 ParserSelectQuery parser;
35 ASTPtr ast = parseQuery(parser, input.data(), input.data() + input.size(), "", 0);
36
37 Context context = Context::createGlobal();
38 context.makeGlobalContext();
39
40 NamesAndTypesList source_columns = {{"number", std::make_shared<DataTypeUInt64>()}};
41 auto syntax_result = SyntaxAnalyzer(context, {}).analyze(ast, source_columns);
42 SelectQueryExpressionAnalyzer analyzer(ast, syntax_result, context);
43 ExpressionActionsChain chain(context);
44 analyzer.appendSelect(chain, false);
45 analyzer.appendProjectResult(chain);
46 chain.finalize();
47 ExpressionActionsPtr expression = chain.getLastActions();
48
49 StoragePtr table = StorageSystemNumbers::create("numbers", false);
50
51 Names column_names;
52 column_names.push_back("number");
53
54 QueryProcessingStage::Enum stage = table->getQueryProcessingStage(context);
55
56 BlockInputStreamPtr in;
57 in = table->read(column_names, {}, context, stage, 8192, 1)[0];
58 in = std::make_shared<ExpressionBlockInputStream>(in, expression);
59 in = std::make_shared<LimitBlockInputStream>(in, 10, std::max(static_cast<Int64>(0), static_cast<Int64>(n) - 10));
60
61 WriteBufferFromOStream out1(std::cout);
62 BlockOutputStreamPtr out = FormatFactory::instance().getOutput("TabSeparated", out1, expression->getSampleBlock(), context);
63
64 {
65 Stopwatch stopwatch;
66 stopwatch.start();
67
68 copyData(*in, *out);
69
70 stopwatch.stop();
71 std::cout << std::fixed << std::setprecision(2)
72 << "Elapsed " << stopwatch.elapsedSeconds() << " sec."
73 << ", " << n / stopwatch.elapsedSeconds() << " rows/sec."
74 << std::endl;
75 }
76
77 return 0;
78}
79catch (const DB::Exception & e)
80{
81 std::cerr << e.what() << ", " << e.displayText() << std::endl;
82 throw;
83}
84
85