ClickHouse/dbms/src/Interpreters/tests/expression.cpp

131 lines
3.4 KiB
C++
Raw Normal View History

2011-08-12 18:27:39 +00:00
#include <iostream>
2011-08-13 23:03:07 +00:00
#include <iomanip>
2011-08-12 18:27:39 +00:00
2011-08-12 20:39:42 +00:00
#include <DB/IO/WriteBufferFromOStream.h>
2012-03-09 03:56:12 +00:00
#include <DB/Columns/ColumnString.h>
#include <DB/Columns/ColumnsNumber.h>
2012-03-09 03:56:12 +00:00
#include <DB/DataTypes/DataTypesNumber.h>
2012-03-09 03:56:12 +00:00
#include <DB/DataTypes/DataTypeString.h>
2011-08-12 18:27:39 +00:00
#include <DB/Parsers/ASTSelectQuery.h>
#include <DB/Parsers/ParserSelectQuery.h>
#include <DB/Parsers/formatAST.h>
#include <DB/Parsers/parseQuery.h>
2011-08-12 18:27:39 +00:00
2011-08-12 20:39:42 +00:00
#include <DB/DataStreams/TabSeparatedRowOutputStream.h>
2011-08-13 23:03:07 +00:00
#include <DB/DataStreams/LimitBlockInputStream.h>
#include <DB/DataStreams/OneBlockInputStream.h>
2013-05-04 04:05:15 +00:00
#include <DB/DataStreams/BlockOutputStreamFromRowOutputStream.h>
2011-08-12 20:39:42 +00:00
#include <DB/DataStreams/copyData.h>
#include <DB/Interpreters/ExpressionAnalyzer.h>
2016-01-13 00:37:34 +00:00
#include <DB/Interpreters/ExpressionActions.h>
Squashed commit of the following: commit e712f469a55ff34ad34b482b15cc4153b7ad7233 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:59:13 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2a002823084e3a79bffcc17d479620a68eb0644b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:58:30 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 9e06f407c8ee781ed8ddf98bdfcc31846bf2a0fe Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:55:14 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 9581620f1e839f456fa7894aa1f996d5162ac6cd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:54:22 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2a8564c68cb6cc3649fafaf401256d43c9a2e777 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:47:34 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit cf60632d78ec656be3304ef4565e859bb6ce80ba Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:40:09 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit ee3d1dc6e0c4ca60e3ac1e0c30d4b3ed1e66eca0 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:22:49 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 65592ef7116a90104fcd524b53ef8b7cf22640f2 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:18:17 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 37972c257320d3b7e7b294e0fdeffff218647bfd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:17:06 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit dd909d149974ce5bed2456de1261aa5a368fd3ff Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:16:28 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 3cf43266ca7e30adf01212b1a739ba5fe43639fd Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:15:42 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 6731a3df96d1609286e2536b6432916af7743f0f Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:13:35 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 1b5727e0d56415b7add4cb76110105358663602c Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:11:18 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit bbcf726a55685b8e72f5b40ba0bf1904bd1c0407 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:09:04 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit c03b477d5e2e65014e8906ecfa2efb67ee295af1 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:06:30 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 2986e2fb0466bc18d73693dcdded28fccc0dc66b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:05:44 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit 5d6cdef13d2e02bd5c4954983334e9162ab2635b Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:04:53 2017 +0300 Less dependencies [#CLICKHOUSE-2] commit f2b819b25ce8b2ccdcb201eefb03e1e6f5aab590 Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Sat Jan 14 11:01:47 2017 +0300 Less dependencies [#CLICKHOUSE-2]
2017-01-14 09:00:19 +00:00
#include <DB/Interpreters/Context.h>
2011-08-12 18:27:39 +00:00
int main(int argc, char ** argv)
{
using namespace DB;
2015-02-01 08:24:49 +00:00
2011-08-12 18:27:39 +00:00
try
{
2011-08-22 01:01:01 +00:00
std::string input = "SELECT x, s1, s2, "
"/*"
"2 + x * 2, x * 2, x % 3 == 1, "
2011-08-21 06:52:21 +00:00
"s1 == 'abc', s1 == s2, s1 != 'abc', s1 != s2, "
"s1 < 'abc', s1 < s2, s1 > 'abc', s1 > s2, "
2011-08-22 01:01:01 +00:00
"s1 <= 'abc', s1 <= s2, s1 >= 'abc', s1 >= s2, "
"*/"
"s1 < s2 AND x % 3 < x % 5";
2011-08-12 18:27:39 +00:00
ParserSelectQuery parser;
ASTPtr ast = parseQuery(parser, input.data(), input.data() + input.size(), "");
2011-08-12 18:27:39 +00:00
formatAST(*ast, std::cerr);
std::cerr << std::endl;
2011-08-12 18:27:39 +00:00
Context context;
2015-03-26 23:32:16 +00:00
NamesAndTypesList columns
{
{"x", std::make_shared<DataTypeInt16>()},
{"s1", std::make_shared<DataTypeString>()},
{"s2", std::make_shared<DataTypeString>()}
2015-03-26 23:32:16 +00:00
};
2015-02-01 08:24:49 +00:00
ExpressionAnalyzer analyzer(ast, context, {}, columns);
ExpressionActionsChain chain;
2014-03-28 20:32:38 +00:00
analyzer.appendSelect(chain, false);
analyzer.appendProjectResult(chain, false);
chain.finalize();
ExpressionActionsPtr expression = chain.getLastActions();
2011-08-12 20:39:42 +00:00
2011-08-13 23:03:07 +00:00
size_t n = argc == 2 ? atoi(argv[1]) : 10;
2011-08-12 20:39:42 +00:00
Block block;
2015-02-01 08:24:49 +00:00
ColumnWithTypeAndName column_x;
2011-08-12 20:39:42 +00:00
column_x.name = "x";
column_x.type = std::make_shared<DataTypeInt16>();
auto x = std::make_shared<ColumnInt16>();
2011-08-12 20:39:42 +00:00
column_x.column = x;
auto & vec_x = x->getData();
2011-08-12 20:39:42 +00:00
2011-08-21 03:41:37 +00:00
vec_x.resize(n);
2011-08-12 20:39:42 +00:00
for (size_t i = 0; i < n; ++i)
2011-08-21 03:41:37 +00:00
vec_x[i] = i;
2011-08-12 20:39:42 +00:00
block.insert(column_x);
2011-08-21 06:52:21 +00:00
const char * strings[] = {"abc", "def", "abcd", "defg", "ac"};
ColumnWithTypeAndName column_s1;
2011-08-21 03:41:37 +00:00
column_s1.name = "s1";
column_s1.type = std::make_shared<DataTypeString>();
column_s1.column = std::make_shared<ColumnString>();
2011-08-21 03:41:37 +00:00
for (size_t i = 0; i < n; ++i)
column_s1.column->insert(String(strings[i % 5]));
2011-08-21 03:41:37 +00:00
block.insert(column_s1);
ColumnWithTypeAndName column_s2;
2011-08-21 03:41:37 +00:00
column_s2.name = "s2";
column_s2.type = std::make_shared<DataTypeString>();
column_s2.column = std::make_shared<ColumnString>();
2011-08-21 03:41:37 +00:00
for (size_t i = 0; i < n; ++i)
column_s2.column->insert(String(strings[i % 3]));
2011-08-21 03:41:37 +00:00
block.insert(column_s2);
2011-08-13 23:03:07 +00:00
{
Stopwatch stopwatch;
2011-08-13 23:03:07 +00:00
stopwatch.start();
expression->execute(block);
2011-08-12 20:39:42 +00:00
2011-08-13 23:03:07 +00:00
stopwatch.stop();
std::cout << std::fixed << std::setprecision(2)
<< "Elapsed " << stopwatch.elapsedSeconds() << " sec."
<< ", " << n / stopwatch.elapsedSeconds() << " rows/sec."
2011-08-13 23:03:07 +00:00
<< std::endl;
}
2015-02-01 08:24:49 +00:00
auto is = std::make_shared<OneBlockInputStream>(block);
LimitBlockInputStream lis(is, 20, std::max(0, static_cast<int>(n) - 20));
WriteBufferFromOStream out_buf(std::cout);
RowOutputStreamPtr os_ = std::make_shared<TabSeparatedRowOutputStream>(out_buf, block);
BlockOutputStreamFromRowOutputStream os(os_);
2011-08-12 20:39:42 +00:00
copyData(lis, os);
2011-08-12 18:27:39 +00:00
}
catch (const Exception & e)
2011-08-12 18:27:39 +00:00
{
2012-11-08 18:30:49 +00:00
std::cerr << e.displayText() << std::endl;
2011-08-12 18:27:39 +00:00
}
return 0;
}