2013-05-24 10:49:19 +00:00
|
|
|
#include <DB/Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <DB/Parsers/ParserSelectQuery.h>
|
|
|
|
#include <DB/Parsers/formatAST.h>
|
|
|
|
|
|
|
|
using namespace DB;
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2013-05-27 14:02:55 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
|
2013-05-24 10:49:19 +00:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
std::cerr << "at least 1 argument expected" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Context context;
|
|
|
|
|
|
|
|
NamesAndTypesList columns;
|
|
|
|
for (int i = 2; i + 1 < argc; i += 2)
|
|
|
|
{
|
|
|
|
NameAndTypePair col;
|
|
|
|
col.first = argv[i];
|
|
|
|
col.second = context.getDataTypeFactory().get(argv[i + 1]);
|
|
|
|
columns.push_back(col);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParserSelectQuery parser;
|
|
|
|
const char * pos = argv[1];
|
|
|
|
const char * end = argv[1] + strlen(argv[1]);
|
|
|
|
ASTPtr root;
|
|
|
|
std::string expected;
|
|
|
|
if (!parser.parse(pos ,end, root, expected))
|
|
|
|
{
|
|
|
|
std::cerr << "expected " << expected << std::endl;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
formatAST(*root, std::cout);
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
ExpressionAnalyzer analyzer(root, context, columns);
|
|
|
|
|
2013-05-30 16:52:21 +00:00
|
|
|
Names required = analyzer.getRequiredColumns();
|
|
|
|
std::cout << "required columns:\n";
|
|
|
|
for (size_t i = 0; i < required.size(); ++i)
|
|
|
|
{
|
|
|
|
std::cout << required[i] << "\n";
|
|
|
|
}
|
|
|
|
std::cout << "\n";
|
2013-05-28 11:54:37 +00:00
|
|
|
|
2013-05-24 10:49:19 +00:00
|
|
|
if (analyzer.hasAggregation())
|
|
|
|
{
|
|
|
|
Names key_names;
|
|
|
|
AggregateDescriptions aggregates;
|
|
|
|
analyzer.getAggregateInfo(key_names, aggregates);
|
|
|
|
|
|
|
|
std::cout << "keys:\n";
|
|
|
|
for (size_t i = 0; i < key_names.size(); ++i)
|
|
|
|
std::cout << key_names[i] << "\n";
|
|
|
|
std::cout << "\n";
|
|
|
|
|
|
|
|
std::cout << "aggregates:\n";
|
|
|
|
for (size_t i = 0; i < aggregates.size(); ++i)
|
|
|
|
{
|
|
|
|
AggregateDescription desc = aggregates[i];
|
|
|
|
|
|
|
|
std::cout << desc.column_name << " = " << desc.function->getName() << " ( ";
|
|
|
|
for (size_t j = 0; j < desc.argument_names.size(); ++j)
|
|
|
|
std::cout << desc.argument_names[j] << " ";
|
|
|
|
std::cout << ")\n";
|
|
|
|
}
|
|
|
|
std::cout << "\n";
|
|
|
|
|
2013-05-28 11:54:37 +00:00
|
|
|
ExpressionActionsChain before;
|
2013-05-28 14:24:20 +00:00
|
|
|
if (analyzer.appendWhere(before))
|
|
|
|
before.addStep();
|
2013-05-28 11:54:37 +00:00
|
|
|
analyzer.appendAggregateFunctionsArguments(before);
|
|
|
|
analyzer.appendGroupBy(before);
|
2013-05-28 14:24:20 +00:00
|
|
|
before.finalize();
|
2013-05-28 11:54:37 +00:00
|
|
|
|
|
|
|
ExpressionActionsChain after;
|
2013-05-28 14:24:20 +00:00
|
|
|
if (analyzer.appendHaving(after))
|
|
|
|
after.addStep();
|
2013-05-28 11:54:37 +00:00
|
|
|
analyzer.appendSelect(after);
|
|
|
|
analyzer.appendOrderBy(after);
|
2013-05-28 14:24:20 +00:00
|
|
|
after.addStep();
|
|
|
|
analyzer.appendProjectResult(after);
|
|
|
|
after.finalize();
|
2013-05-28 11:54:37 +00:00
|
|
|
|
|
|
|
std::cout << "before aggregation:\n\n";
|
2013-05-28 14:24:20 +00:00
|
|
|
for (size_t i = 0; i < before.steps.size(); ++i)
|
2013-05-28 11:54:37 +00:00
|
|
|
{
|
2013-05-28 14:24:20 +00:00
|
|
|
std::cout << before.steps[i].actions->dumpActions();
|
2013-05-28 11:54:37 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\nafter aggregation:\n\n";
|
2013-05-28 14:24:20 +00:00
|
|
|
for (size_t i = 0; i < after.steps.size(); ++i)
|
2013-05-28 11:54:37 +00:00
|
|
|
{
|
2013-05-28 14:24:20 +00:00
|
|
|
std::cout << after.steps[i].actions->dumpActions();
|
2013-05-28 11:54:37 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2013-05-24 10:49:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-28 11:54:37 +00:00
|
|
|
std::cout << "only consts:\n\n" << analyzer.getConstActions()->dumpActions() << "\n";
|
|
|
|
|
|
|
|
if (dynamic_cast<ASTSelectQuery *>(&*root))
|
|
|
|
{
|
|
|
|
ExpressionActionsChain chain;
|
2013-05-28 14:24:20 +00:00
|
|
|
if (analyzer.appendWhere(chain))
|
|
|
|
chain.addStep();
|
2013-05-28 11:54:37 +00:00
|
|
|
analyzer.appendSelect(chain);
|
|
|
|
analyzer.appendOrderBy(chain);
|
2013-05-28 14:24:20 +00:00
|
|
|
chain.addStep();
|
|
|
|
analyzer.appendProjectResult(chain);
|
|
|
|
chain.finalize();
|
2013-05-28 11:54:37 +00:00
|
|
|
|
2013-05-28 14:24:20 +00:00
|
|
|
for (size_t i = 0; i < chain.steps.size(); ++i)
|
2013-05-28 11:54:37 +00:00
|
|
|
{
|
2013-05-28 14:24:20 +00:00
|
|
|
std::cout << chain.steps[i].actions->dumpActions();
|
2013-05-28 11:54:37 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << analyzer.getActions()->dumpActions() << "\n";
|
|
|
|
}
|
2013-05-27 14:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
std::cerr << "Exception " << e.what() << ": " << e.displayText() << "\n" << e.getStackTrace().toString();
|
|
|
|
return 3;
|
2013-05-24 10:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|