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
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBufferFromOStream.h>
|
2011-08-12 20:39:42 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2012-03-09 03:56:12 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
2011-08-12 18:27:39 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/ParserSelectQuery.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
2011-08-12 18:27:39 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/TabSeparatedRowOutputStream.h>
|
|
|
|
#include <DataStreams/LimitBlockInputStream.h>
|
|
|
|
#include <DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <DataStreams/BlockOutputStreamFromRowOutputStream.h>
|
|
|
|
#include <DataStreams/copyData.h>
|
2011-08-12 20:39:42 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <Interpreters/ExpressionActions.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2011-08-12 18:27:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using namespace DB;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string input = "SELECT x, s1, s2, "
|
|
|
|
"/*"
|
|
|
|
"2 + x * 2, x * 2, x % 3 == 1, "
|
|
|
|
"s1 == 'abc', s1 == s2, s1 != 'abc', s1 != s2, "
|
|
|
|
"s1 < 'abc', s1 < s2, s1 > 'abc', s1 > s2, "
|
|
|
|
"s1 <= 'abc', s1 <= s2, s1 >= 'abc', s1 >= s2, "
|
|
|
|
"*/"
|
|
|
|
"s1 < s2 AND x % 3 < x % 5";
|
|
|
|
|
|
|
|
ParserSelectQuery parser;
|
2018-04-16 15:11:13 +00:00
|
|
|
ASTPtr ast = parseQuery(parser, input.data(), input.data() + input.size(), "", 0);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
formatAST(*ast, std::cerr);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2017-06-19 20:31:23 +00:00
|
|
|
Context context = Context::createGlobal();
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList columns
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
{"x", std::make_shared<DataTypeInt16>()},
|
|
|
|
{"s1", std::make_shared<DataTypeString>()},
|
|
|
|
{"s2", std::make_shared<DataTypeString>()}
|
|
|
|
};
|
|
|
|
|
|
|
|
ExpressionAnalyzer analyzer(ast, context, {}, columns);
|
|
|
|
ExpressionActionsChain chain;
|
|
|
|
analyzer.appendSelect(chain, false);
|
2017-12-01 21:13:25 +00:00
|
|
|
analyzer.appendProjectResult(chain);
|
2017-04-01 07:20:54 +00:00
|
|
|
chain.finalize();
|
|
|
|
ExpressionActionsPtr expression = chain.getLastActions();
|
|
|
|
|
|
|
|
size_t n = argc == 2 ? atoi(argv[1]) : 10;
|
|
|
|
|
|
|
|
Block block;
|
|
|
|
|
2017-12-16 06:19:21 +00:00
|
|
|
{
|
|
|
|
ColumnWithTypeAndName column;
|
|
|
|
column.name = "x";
|
|
|
|
column.type = std::make_shared<DataTypeInt16>();
|
|
|
|
auto col = ColumnInt16::create();
|
|
|
|
auto & vec_x = col->getData();
|
|
|
|
|
|
|
|
vec_x.resize(n);
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
vec_x[i] = i % 9;
|
|
|
|
|
|
|
|
column.column = std::move(col);
|
|
|
|
block.insert(column);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const char * strings[] = {"abc", "def", "abcd", "defg", "ac"};
|
|
|
|
|
2017-12-16 06:19:21 +00:00
|
|
|
{
|
|
|
|
ColumnWithTypeAndName column;
|
|
|
|
column.name = "s1";
|
|
|
|
column.type = std::make_shared<DataTypeString>();
|
|
|
|
auto col = ColumnString::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 06:19:21 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
col->insert(std::string(strings[i % 5]));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 06:19:21 +00:00
|
|
|
column.column = std::move(col);
|
|
|
|
block.insert(column);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 06:19:21 +00:00
|
|
|
{
|
|
|
|
ColumnWithTypeAndName column;
|
|
|
|
column.name = "s2";
|
|
|
|
column.type = std::make_shared<DataTypeString>();
|
|
|
|
auto col = ColumnString::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 06:19:21 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
col->insert(std::string(strings[i % 3]));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 06:19:21 +00:00
|
|
|
column.column = std::move(col);
|
|
|
|
block.insert(column);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
Stopwatch stopwatch;
|
|
|
|
stopwatch.start();
|
|
|
|
|
|
|
|
expression->execute(block);
|
|
|
|
|
|
|
|
stopwatch.stop();
|
|
|
|
std::cout << std::fixed << std::setprecision(2)
|
|
|
|
<< "Elapsed " << stopwatch.elapsedSeconds() << " sec."
|
|
|
|
<< ", " << n / stopwatch.elapsedSeconds() << " rows/sec."
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto is = std::make_shared<OneBlockInputStream>(block);
|
|
|
|
LimitBlockInputStream lis(is, 20, std::max(0, static_cast<int>(n) - 20));
|
|
|
|
WriteBufferFromOStream out_buf(std::cout);
|
2018-06-08 02:26:44 +00:00
|
|
|
RowOutputStreamPtr os_ = std::make_shared<TabSeparatedRowOutputStream>(out_buf, block, false, false, FormatSettings());
|
2018-02-19 00:45:32 +00:00
|
|
|
BlockOutputStreamFromRowOutputStream os(os_, is->getHeader());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
copyData(lis, os);
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
std::cerr << e.displayText() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-08-12 18:27:39 +00:00
|
|
|
}
|