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>
|
|
|
|
|
2011-08-12 18:27:39 +00:00
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
|
|
|
|
#include <DB/Functions/FunctionsArithmetic.h>
|
2011-08-21 03:41:37 +00:00
|
|
|
#include <DB/Functions/FunctionsComparison.h>
|
2011-08-22 01:01:01 +00:00
|
|
|
#include <DB/Functions/FunctionsLogical.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>
|
2011-08-12 20:39:42 +00:00
|
|
|
#include <DB/DataStreams/copyData.h>
|
|
|
|
|
2011-08-12 18:27:39 +00:00
|
|
|
#include <DB/Interpreters/Expression.h>
|
|
|
|
|
|
|
|
|
|
|
|
void dump(DB::IAST & ast, int level = 0)
|
|
|
|
{
|
|
|
|
std::string prefix(level, ' ');
|
|
|
|
|
|
|
|
if (DB::ASTFunction * node = dynamic_cast<DB::ASTFunction *>(&ast))
|
|
|
|
{
|
|
|
|
std::cout << prefix << node << " Function, name = " << node->function->getName() << ", return types: ";
|
|
|
|
for (DB::DataTypes::const_iterator it = node->return_types.begin(); it != node->return_types.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != node->return_types.begin())
|
|
|
|
std::cout << ", ";
|
|
|
|
std::cout << (*it)->getName();
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
else if (DB::ASTIdentifier * node = dynamic_cast<DB::ASTIdentifier *>(&ast))
|
|
|
|
{
|
|
|
|
std::cout << prefix << node << " Identifier, name = " << node->name << ", type = " << node->type->getName() << std::endl;
|
|
|
|
}
|
|
|
|
else if (DB::ASTLiteral * node = dynamic_cast<DB::ASTLiteral *>(&ast))
|
|
|
|
{
|
|
|
|
std::cout << prefix << node << " Literal, " << boost::apply_visitor(DB::FieldVisitorToString(), node->value)
|
|
|
|
<< ", type = " << node->type->getName() << std::endl;
|
|
|
|
}
|
|
|
|
|
2011-08-13 21:05:18 +00:00
|
|
|
DB::ASTs children = ast.children;
|
2011-08-12 18:27:39 +00:00
|
|
|
for (DB::ASTs::iterator it = children.begin(); it != children.end(); ++it)
|
|
|
|
dump(**it, level + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-12 20:39:42 +00:00
|
|
|
class OneBlockInputStream : public DB::IBlockInputStream
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const DB::Block & block;
|
|
|
|
bool has_been_read;
|
|
|
|
public:
|
|
|
|
OneBlockInputStream(const DB::Block & block_) : block(block_), has_been_read(false) {}
|
|
|
|
|
|
|
|
DB::Block read()
|
|
|
|
{
|
|
|
|
if (!has_been_read)
|
|
|
|
{
|
|
|
|
has_been_read = true;
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return DB::Block();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-08-12 18:27:39 +00:00
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DB::ParserSelectQuery parser;
|
|
|
|
DB::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";
|
2011-08-12 18:27:39 +00:00
|
|
|
std::string expected;
|
|
|
|
|
|
|
|
const char * begin = input.data();
|
|
|
|
const char * end = begin + input.size();
|
|
|
|
const char * pos = begin;
|
|
|
|
|
|
|
|
if (parser.parse(pos, end, ast, expected))
|
|
|
|
{
|
|
|
|
std::cout << "Success." << std::endl;
|
|
|
|
DB::formatAST(*ast, std::cout);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::Context context;
|
|
|
|
context.columns["x"] = new DB::DataTypeInt16;
|
2011-08-21 03:41:37 +00:00
|
|
|
context.columns["s1"] = new DB::DataTypeString;
|
|
|
|
context.columns["s2"] = new DB::DataTypeString;
|
2011-08-22 01:01:01 +00:00
|
|
|
|
|
|
|
(*context.functions)["plus"] = new DB::FunctionPlus;
|
|
|
|
(*context.functions)["minus"] = new DB::FunctionMinus;
|
|
|
|
(*context.functions)["multiply"] = new DB::FunctionMultiply;
|
|
|
|
(*context.functions)["divide"] = new DB::FunctionDivideFloating;
|
|
|
|
(*context.functions)["intDiv"] = new DB::FunctionDivideIntegral;
|
|
|
|
(*context.functions)["modulo"] = new DB::FunctionModulo;
|
|
|
|
|
|
|
|
(*context.functions)["equals"] = new DB::FunctionEquals;
|
|
|
|
(*context.functions)["notEquals"] = new DB::FunctionNotEquals;
|
|
|
|
(*context.functions)["less"] = new DB::FunctionLess;
|
|
|
|
(*context.functions)["greater"] = new DB::FunctionGreater;
|
|
|
|
(*context.functions)["lessOrEquals"] = new DB::FunctionLessOrEquals;
|
2011-08-21 06:52:21 +00:00
|
|
|
(*context.functions)["greaterOrEquals"] = new DB::FunctionGreaterOrEquals;
|
2011-08-12 18:27:39 +00:00
|
|
|
|
2011-08-22 01:01:01 +00:00
|
|
|
(*context.functions)["and"] = new DB::FunctionAnd;
|
|
|
|
(*context.functions)["or"] = new DB::FunctionOr;
|
|
|
|
(*context.functions)["xor"] = new DB::FunctionXor;
|
|
|
|
(*context.functions)["not"] = new DB::FunctionNot;
|
|
|
|
|
2011-08-12 18:27:39 +00:00
|
|
|
DB::Expression expression(ast, context);
|
|
|
|
|
|
|
|
dump(*ast);
|
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
|
|
|
|
|
|
|
DB::Block block;
|
2011-08-21 03:41:37 +00:00
|
|
|
|
2011-08-12 20:39:42 +00:00
|
|
|
DB::ColumnWithNameAndType column_x;
|
|
|
|
column_x.name = "x";
|
|
|
|
column_x.type = new DB::DataTypeInt16;
|
|
|
|
DB::ColumnInt16 * x = new DB::ColumnInt16;
|
|
|
|
column_x.column = x;
|
2011-08-21 03:41:37 +00:00
|
|
|
std::vector<Int16> & 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"};
|
|
|
|
|
2011-08-21 03:41:37 +00:00
|
|
|
DB::ColumnWithNameAndType column_s1;
|
|
|
|
column_s1.name = "s1";
|
|
|
|
column_s1.type = new DB::DataTypeString;
|
|
|
|
column_s1.column = new DB::ColumnString;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
2011-08-21 06:52:21 +00:00
|
|
|
column_s1.column->insert(strings[i % 5]);
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
|
|
block.insert(column_s1);
|
|
|
|
|
|
|
|
DB::ColumnWithNameAndType column_s2;
|
|
|
|
column_s2.name = "s2";
|
|
|
|
column_s2.type = new DB::DataTypeString;
|
|
|
|
column_s2.column = new DB::ColumnString;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
2011-08-21 06:52:21 +00:00
|
|
|
column_s2.column->insert(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();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-12 20:39:42 +00:00
|
|
|
DB::DataTypes * data_types = new DB::DataTypes;
|
|
|
|
for (size_t i = 0; i < block.columns(); ++i)
|
|
|
|
data_types->push_back(block.getByPosition(i).type);
|
|
|
|
|
2011-08-13 23:03:07 +00:00
|
|
|
OneBlockInputStream * is = new OneBlockInputStream(block);
|
2011-08-21 06:52:21 +00:00
|
|
|
DB::LimitBlockInputStream lis(is, 20, std::max(0, static_cast<int>(n) - 20));
|
2011-08-12 20:39:42 +00:00
|
|
|
DB::WriteBufferFromOStream out_buf(std::cout);
|
|
|
|
DB::TabSeparatedRowOutputStream os(out_buf, data_types);
|
|
|
|
|
2011-08-13 23:03:07 +00:00
|
|
|
DB::copyData(lis, os);
|
2011-08-12 18:27:39 +00:00
|
|
|
}
|
|
|
|
catch (const DB::Exception & e)
|
|
|
|
{
|
|
|
|
std::cerr << e.message() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|