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