1#include <iostream>
2#include <iomanip>
3
4#include <IO/WriteBufferFromOStream.h>
5
6#include <Columns/ColumnString.h>
7#include <Columns/ColumnsNumber.h>
8
9#include <DataTypes/DataTypesNumber.h>
10#include <DataTypes/DataTypeString.h>
11
12#include <Parsers/ASTSelectQuery.h>
13#include <Parsers/ParserSelectQuery.h>
14#include <Parsers/formatAST.h>
15#include <Parsers/parseQuery.h>
16
17#include <Formats/FormatFactory.h>
18#include <DataStreams/LimitBlockInputStream.h>
19#include <DataStreams/OneBlockInputStream.h>
20#include <DataStreams/copyData.h>
21
22#include <Interpreters/SyntaxAnalyzer.h>
23#include <Interpreters/ExpressionAnalyzer.h>
24#include <Interpreters/ExpressionActions.h>
25#include <Interpreters/Context.h>
26
27
28int main(int argc, char ** argv)
29{
30 using namespace DB;
31
32 try
33 {
34 std::string input = "SELECT x, s1, s2, "
35 "/*"
36 "2 + x * 2, x * 2, x % 3 == 1, "
37 "s1 == 'abc', s1 == s2, s1 != 'abc', s1 != s2, "
38 "s1 < 'abc', s1 < s2, s1 > 'abc', s1 > s2, "
39 "s1 <= 'abc', s1 <= s2, s1 >= 'abc', s1 >= s2, "
40 "*/"
41 "s1 < s2 AND x % 3 < x % 5";
42
43 ParserSelectQuery parser;
44 ASTPtr ast = parseQuery(parser, input.data(), input.data() + input.size(), "", 0);
45
46 formatAST(*ast, std::cerr);
47 std::cerr << std::endl;
48
49 Context context = Context::createGlobal();
50 context.makeGlobalContext();
51 NamesAndTypesList columns
52 {
53 {"x", std::make_shared<DataTypeInt16>()},
54 {"s1", std::make_shared<DataTypeString>()},
55 {"s2", std::make_shared<DataTypeString>()}
56 };
57
58 auto syntax_result = SyntaxAnalyzer(context, {}).analyze(ast, columns);
59 SelectQueryExpressionAnalyzer analyzer(ast, syntax_result, context);
60 ExpressionActionsChain chain(context);
61 analyzer.appendSelect(chain, false);
62 analyzer.appendProjectResult(chain);
63 chain.finalize();
64 ExpressionActionsPtr expression = chain.getLastActions();
65
66 size_t n = argc == 2 ? atoi(argv[1]) : 10;
67
68 Block block;
69
70 {
71 ColumnWithTypeAndName column;
72 column.name = "x";
73 column.type = std::make_shared<DataTypeInt16>();
74 auto col = ColumnInt16::create();
75 auto & vec_x = col->getData();
76
77 vec_x.resize(n);
78 for (size_t i = 0; i < n; ++i)
79 vec_x[i] = i % 9;
80
81 column.column = std::move(col);
82 block.insert(column);
83 }
84
85 const char * strings[] = {"abc", "def", "abcd", "defg", "ac"};
86
87 {
88 ColumnWithTypeAndName column;
89 column.name = "s1";
90 column.type = std::make_shared<DataTypeString>();
91 auto col = ColumnString::create();
92
93 for (size_t i = 0; i < n; ++i)
94 col->insert(std::string(strings[i % 5]));
95
96 column.column = std::move(col);
97 block.insert(column);
98 }
99
100 {
101 ColumnWithTypeAndName column;
102 column.name = "s2";
103 column.type = std::make_shared<DataTypeString>();
104 auto col = ColumnString::create();
105
106 for (size_t i = 0; i < n; ++i)
107 col->insert(std::string(strings[i % 3]));
108
109 column.column = std::move(col);
110 block.insert(column);
111 }
112
113 {
114 Stopwatch stopwatch;
115 stopwatch.start();
116
117 expression->execute(block);
118
119 stopwatch.stop();
120 std::cout << std::fixed << std::setprecision(2)
121 << "Elapsed " << stopwatch.elapsedSeconds() << " sec."
122 << ", " << n / stopwatch.elapsedSeconds() << " rows/sec."
123 << std::endl;
124 }
125
126 auto is = std::make_shared<OneBlockInputStream>(block);
127 LimitBlockInputStream lis(is, 20, std::max(0, static_cast<int>(n) - 20));
128 WriteBufferFromOStream out_buf(std::cout);
129 BlockOutputStreamPtr out = FormatFactory::instance().getOutput("TabSeparated", out_buf, block, context);
130
131 copyData(lis, *out);
132 }
133 catch (const Exception & e)
134 {
135 std::cerr << e.displayText() << std::endl;
136 }
137
138 return 0;
139}
140