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-13 23:03:07 +00:00
|
|
|
#include <Poco/Stopwatch.h>
|
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>
|
|
|
|
|
2011-08-12 18:27:39 +00:00
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.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>
|
|
|
|
|
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>
|
2013-05-03 10:34:36 +00:00
|
|
|
#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>
|
|
|
|
|
2013-06-05 12:46:41 +00:00
|
|
|
#include <DB/Interpreters/ExpressionAnalyzer.h>
|
2011-08-12 18:27:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2014-06-12 00:48:56 +00:00
|
|
|
using namespace DB;
|
2015-02-01 08:24:49 +00:00
|
|
|
|
2011-08-12 18:27:39 +00:00
|
|
|
try
|
|
|
|
{
|
2014-06-12 00:48:56 +00:00
|
|
|
ParserSelectQuery parser;
|
|
|
|
ASTPtr ast;
|
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";
|
2014-06-12 00:48:56 +00:00
|
|
|
Expected expected = "";
|
2011-08-12 18:27:39 +00:00
|
|
|
|
|
|
|
const char * begin = input.data();
|
|
|
|
const char * end = begin + input.size();
|
|
|
|
const char * pos = begin;
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
if (parser.parse(pos, end, ast, max_parsed_pos, expected))
|
2011-08-12 18:27:39 +00:00
|
|
|
{
|
|
|
|
std::cout << "Success." << std::endl;
|
2014-06-12 00:48:56 +00:00
|
|
|
formatAST(*ast, std::cout);
|
2011-08-12 18:27:39 +00:00
|
|
|
std::cout << std::endl << ast->getTreeID() << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << "Failed at position " << (pos - begin) << ": "
|
|
|
|
<< mysqlxx::quote << input.substr(pos - begin, 10)
|
|
|
|
<< ", expected " << expected << "." << std::endl;
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:48:56 +00:00
|
|
|
Context context;
|
2015-03-26 23:32:16 +00:00
|
|
|
NamesAndTypesList columns
|
|
|
|
{
|
|
|
|
{"x", new DataTypeInt16},
|
|
|
|
{"s1", new DataTypeString},
|
|
|
|
{"s2", new DataTypeString}
|
|
|
|
};
|
2012-08-02 17:33:31 +00:00
|
|
|
context.setColumns(columns);
|
2015-02-01 08:24:49 +00:00
|
|
|
|
|
|
|
ExpressionAnalyzer analyzer(ast, context, context.getColumns());
|
2014-06-12 00:48:56 +00:00
|
|
|
ExpressionActionsChain chain;
|
2014-03-28 20:32:38 +00:00
|
|
|
analyzer.appendSelect(chain, false);
|
|
|
|
analyzer.appendProjectResult(chain, false);
|
2013-06-05 12:46:41 +00:00
|
|
|
chain.finalize();
|
2014-06-12 00:48:56 +00:00
|
|
|
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
|
|
|
|
2014-06-12 00:48:56 +00:00
|
|
|
Block block;
|
2015-02-01 08:24:49 +00:00
|
|
|
|
2014-06-12 00:48:56 +00:00
|
|
|
ColumnWithNameAndType column_x;
|
2011-08-12 20:39:42 +00:00
|
|
|
column_x.name = "x";
|
2014-06-12 00:48:56 +00:00
|
|
|
column_x.type = new DataTypeInt16;
|
|
|
|
ColumnInt16 * x = new ColumnInt16;
|
2011-08-12 20:39:42 +00:00
|
|
|
column_x.column = x;
|
2015-02-22 18:02:54 +00:00
|
|
|
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"};
|
|
|
|
|
2014-06-12 00:48:56 +00:00
|
|
|
ColumnWithNameAndType column_s1;
|
2011-08-21 03:41:37 +00:00
|
|
|
column_s1.name = "s1";
|
2014-06-12 00:48:56 +00:00
|
|
|
column_s1.type = new DataTypeString;
|
|
|
|
column_s1.column = new ColumnString;
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
2014-06-12 00:48:56 +00:00
|
|
|
column_s1.column->insert(String(strings[i % 5]));
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
|
|
block.insert(column_s1);
|
|
|
|
|
2014-06-12 00:48:56 +00:00
|
|
|
ColumnWithNameAndType column_s2;
|
2011-08-21 03:41:37 +00:00
|
|
|
column_s2.name = "s2";
|
2014-06-12 00:48:56 +00:00
|
|
|
column_s2.type = new DataTypeString;
|
|
|
|
column_s2.column = new ColumnString;
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
2014-06-12 00:48:56 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
Poco::Stopwatch stopwatch;
|
|
|
|
stopwatch.start();
|
|
|
|
|
2013-06-05 12:46:41 +00:00
|
|
|
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.elapsed() / 1000000.0 << " sec."
|
|
|
|
<< ", " << n * 1000000 / stopwatch.elapsed() << " rows/sec."
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2015-02-01 08:24:49 +00:00
|
|
|
|
2014-06-12 00:48:56 +00:00
|
|
|
OneBlockInputStream * is = new OneBlockInputStream(block);
|
|
|
|
LimitBlockInputStream lis(is, 20, std::max(0, static_cast<int>(n) - 20));
|
|
|
|
WriteBufferFromOStream out_buf(std::cout);
|
|
|
|
RowOutputStreamPtr os_ = new TabSeparatedRowOutputStream(out_buf, block);
|
|
|
|
BlockOutputStreamFromRowOutputStream os(os_);
|
2011-08-12 20:39:42 +00:00
|
|
|
|
2014-06-12 00:48:56 +00:00
|
|
|
copyData(lis, os);
|
2011-08-12 18:27:39 +00:00
|
|
|
}
|
2014-06-12 00:48:56 +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;
|
|
|
|
}
|