ClickHouse/dbms/src/Parsers/formatAST.cpp

308 lines
6.8 KiB
C++
Raw Normal View History

2010-06-28 13:44:42 +00:00
#include <sstream>
#include <boost/variant/static_visitor.hpp>
#include <Poco/NumberFormatter.h>
#include <mysqlxx/Manip.h>
2010-06-28 13:44:42 +00:00
2011-11-06 20:47:07 +00:00
#include <DB/IO/WriteBufferFromOStream.h>
#include <DB/IO/WriteHelpers.h>
2010-06-28 13:44:42 +00:00
#include <DB/Core/Exception.h>
#include <DB/Core/ErrorCodes.h>
#include <DB/Parsers/formatAST.h>
namespace DB
{
2011-11-07 02:10:34 +00:00
static const char * hilite_keyword = "\033[1;37m";
static const char * hilite_identifier = "\033[0;36m";
static const char * hilite_function = "\033[0;33m";
static const char * hilite_alias = "\033[0;32m";
static const char * hilite_none = "\033[0m";
void formatAST(const IAST & ast, std::ostream & s, size_t indent)
2010-06-28 13:44:42 +00:00
{
const ASTSelectQuery * select = dynamic_cast<const ASTSelectQuery *>(&ast);
if (select)
{
2011-11-07 02:10:34 +00:00
formatAST(*select, s, indent);
2010-06-28 13:44:42 +00:00
return;
}
2011-08-18 18:48:00 +00:00
const ASTCreateQuery * create = dynamic_cast<const ASTCreateQuery *>(&ast);
if (create)
{
2011-11-07 02:10:34 +00:00
formatAST(*create, s, indent);
2011-08-18 18:48:00 +00:00
return;
}
2010-06-28 13:44:42 +00:00
const ASTExpressionList * exp_list = dynamic_cast<const ASTExpressionList *>(&ast);
if (exp_list)
{
2011-11-07 02:10:34 +00:00
formatAST(*exp_list, s, indent);
2010-06-28 13:44:42 +00:00
return;
}
const ASTFunction * func = dynamic_cast<const ASTFunction *>(&ast);
if (func)
{
2011-11-07 02:10:34 +00:00
formatAST(*func, s, indent);
2010-06-28 13:44:42 +00:00
return;
}
const ASTIdentifier * id = dynamic_cast<const ASTIdentifier *>(&ast);
if (id)
{
2011-11-07 02:10:34 +00:00
formatAST(*id, s, indent);
2010-06-28 13:44:42 +00:00
return;
}
const ASTLiteral * lit = dynamic_cast<const ASTLiteral *>(&ast);
if (lit)
{
2011-11-07 02:10:34 +00:00
formatAST(*lit, s, indent);
2010-06-28 13:44:42 +00:00
return;
}
2011-08-18 18:48:00 +00:00
const ASTNameTypePair * ntp = dynamic_cast<const ASTNameTypePair *>(&ast);
if (ntp)
{
2011-11-07 02:10:34 +00:00
formatAST(*ntp, s, indent);
2011-08-18 18:48:00 +00:00
return;
}
2011-08-28 08:50:27 +00:00
const ASTAsterisk * asterisk = dynamic_cast<const ASTAsterisk *>(&ast);
if (asterisk)
{
2011-11-07 02:10:34 +00:00
formatAST(*asterisk, s, indent);
2011-08-28 08:50:27 +00:00
return;
}
2011-09-04 05:14:52 +00:00
const ASTOrderByElement * order_by_elem = dynamic_cast<const ASTOrderByElement *>(&ast);
if (order_by_elem)
{
2011-11-07 02:10:34 +00:00
formatAST(*order_by_elem, s, indent);
2011-09-04 05:14:52 +00:00
return;
}
2010-06-28 13:44:42 +00:00
throw DB::Exception("Unknown element in AST", ErrorCodes::UNKNOWN_ELEMENT_IN_AST);
}
2011-11-07 02:10:34 +00:00
void formatAST(const ASTSelectQuery & ast, std::ostream & s, size_t indent)
2010-06-28 13:44:42 +00:00
{
2011-11-07 02:10:34 +00:00
std::string indent_str(4 * indent, ' ');
s << hilite_keyword << indent_str << "SELECT " << hilite_none;
formatAST(*ast.select_expression_list, s, indent);
2011-08-28 00:31:30 +00:00
if (ast.table)
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << "\n" << indent_str << "FROM " << hilite_none;
2011-08-28 00:31:30 +00:00
if (ast.database)
{
2011-11-07 02:10:34 +00:00
formatAST(*ast.database, s, indent);
2011-08-28 00:31:30 +00:00
s << ".";
}
2011-11-07 02:10:34 +00:00
if (dynamic_cast<const ASTSelectQuery *>(&*ast.table))
{
s << "\n" << indent_str << "(\n";
formatAST(*ast.table, s, indent + 1);
s << "\n" << indent_str << ")";
}
else
formatAST(*ast.table, s, indent);
2011-08-28 00:31:30 +00:00
}
if (ast.where_expression)
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << "\n" << indent_str << "WHERE " << hilite_none;
formatAST(*ast.where_expression, s, indent);
2011-08-28 00:31:30 +00:00
}
if (ast.group_expression_list)
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << "\n" << indent_str << "GROUP BY " << hilite_none;
formatAST(*ast.group_expression_list, s, indent);
2011-08-28 00:31:30 +00:00
}
if (ast.having_expression)
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << "\n" << indent_str << "HAVING " << hilite_none;
formatAST(*ast.having_expression, s, indent);
2011-08-28 00:31:30 +00:00
}
if (ast.order_expression_list)
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << "\n" << indent_str << "ORDER BY " << hilite_none;
formatAST(*ast.order_expression_list, s, indent);
2011-08-28 00:31:30 +00:00
}
if (ast.limit_length)
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << "\n" << indent_str << "LIMIT " << hilite_none;
2011-08-28 00:31:30 +00:00
if (ast.limit_offset)
{
2011-11-07 02:10:34 +00:00
formatAST(*ast.limit_offset, s, indent);
2011-08-28 00:31:30 +00:00
s << ", ";
}
2011-11-07 02:10:34 +00:00
formatAST(*ast.limit_length, s, indent);
2011-08-28 00:31:30 +00:00
}
2011-10-30 05:19:41 +00:00
if (ast.format)
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << "\n" << indent_str << "FORMAT " << hilite_none;
formatAST(*ast.format, s, indent);
2011-10-30 05:19:41 +00:00
}
2010-06-28 13:44:42 +00:00
}
2011-11-07 02:10:34 +00:00
void formatAST(const ASTCreateQuery & ast, std::ostream & s, size_t indent)
2011-08-18 18:48:00 +00:00
{
2011-12-26 07:07:30 +00:00
if (!ast.database.empty() && ast.table.empty())
{
s << hilite_keyword << (ast.attach ? "ATTACH DATABASE " : "CREATE DATABASE ") << (ast.if_not_exists ? "IF NOT EXISTS " : "") << hilite_none
<< ast.database;
return;
}
s << hilite_keyword << (ast.attach ? "ATTACH TABLE " : "CREATE TABLE ") << (ast.if_not_exists ? "IF NOT EXISTS " : "") << hilite_none
<< (!ast.database.empty() ? ast.database + "." : "") << ast.table;
if (!ast.as_table.empty())
{
s << hilite_keyword << " AS" << hilite_none
<< (!ast.as_database.empty() ? ast.as_database + "." : "") << ast.as_table;
}
if (ast.columns)
{
s << "\n(";
formatAST(*ast.columns, s, indent + 1);
s << ")";
}
if (ast.storage)
{
s << hilite_keyword << " ENGINE" << hilite_none << " = ";
formatAST(*ast.storage, s, indent);
}
if (ast.select)
{
s << hilite_keyword << " AS\n" << hilite_none;
formatAST(*ast.select, s, indent);
}
2011-08-18 18:48:00 +00:00
}
2011-11-07 02:10:34 +00:00
void formatAST(const ASTInsertQuery & ast, std::ostream & s, size_t indent)
2011-10-30 05:19:41 +00:00
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << "INSERT INTO " << hilite_none << (!ast.database.empty() ? ast.database + "." : "") << ast.table;
2011-10-30 05:19:41 +00:00
if (ast.columns)
{
s << " (";
2011-11-07 02:10:34 +00:00
formatAST(*ast.columns, s, indent);
2011-10-30 05:19:41 +00:00
s << ")";
}
if (ast.select)
{
s << " ";
2011-11-07 02:10:34 +00:00
formatAST(*ast.select, s, indent);
2011-10-30 05:19:41 +00:00
}
else
{
if (!ast.format.empty())
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << " FORMAT " << hilite_none << ast.format;
2011-10-30 05:19:41 +00:00
}
else
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << " VALUES" << hilite_none;
2011-10-30 05:19:41 +00:00
}
}
}
2011-11-07 02:10:34 +00:00
void formatAST(const ASTExpressionList & ast, std::ostream & s, size_t indent)
2010-06-28 13:44:42 +00:00
{
for (ASTs::const_iterator it = ast.children.begin(); it != ast.children.end(); ++it)
{
if (it != ast.children.begin())
s << ", ";
2011-11-07 02:10:34 +00:00
formatAST(**it, s, indent);
2010-06-28 13:44:42 +00:00
}
}
2011-11-06 20:47:07 +00:00
static void writeAlias(const String & name, std::ostream & s)
{
2011-11-07 02:10:34 +00:00
s << hilite_keyword << " AS " << hilite_alias;
{
WriteBufferFromOStream wb(s);
writeProbablyBackQuotedString(name, wb);
}
s << hilite_none;
2011-11-06 20:47:07 +00:00
}
2011-11-07 02:10:34 +00:00
void formatAST(const ASTFunction & ast, std::ostream & s, size_t indent)
2010-06-28 13:44:42 +00:00
{
2011-11-07 02:10:34 +00:00
s << hilite_function << ast.name;
2011-08-18 18:48:00 +00:00
if (ast.arguments)
{
2011-11-07 02:10:34 +00:00
s << '(' << hilite_none;
formatAST(*ast.arguments, s, indent);
s << hilite_function << ')' << hilite_none;
2011-08-18 18:48:00 +00:00
}
2011-11-06 20:47:07 +00:00
if (!ast.alias.empty())
writeAlias(ast.alias, s);
2010-06-28 13:44:42 +00:00
}
2011-11-07 02:10:34 +00:00
void formatAST(const ASTIdentifier & ast, std::ostream & s, size_t indent)
2010-06-28 13:44:42 +00:00
{
2011-11-07 02:10:34 +00:00
s << hilite_identifier;
2011-11-06 20:47:07 +00:00
{
WriteBufferFromOStream wb(s);
writeProbablyBackQuotedString(ast.name, wb);
}
2011-11-07 02:10:34 +00:00
s << hilite_none;
2011-11-06 20:47:07 +00:00
if (!ast.alias.empty())
writeAlias(ast.alias, s);
2010-06-28 13:44:42 +00:00
}
2011-11-07 02:10:34 +00:00
void formatAST(const ASTLiteral & ast, std::ostream & s, size_t indent)
2010-06-28 13:44:42 +00:00
{
s << boost::apply_visitor(FieldVisitorToString(), ast.value);
2011-11-06 20:47:07 +00:00
if (!ast.alias.empty())
writeAlias(ast.alias, s);
2010-06-28 13:44:42 +00:00
}
2011-11-07 02:10:34 +00:00
void formatAST(const ASTNameTypePair & ast, std::ostream & s, size_t indent)
2011-08-18 18:48:00 +00:00
{
2011-12-26 07:07:30 +00:00
std::string indent_str(4 * indent, ' ');
s << indent_str << ast.name << " ";
2011-11-07 02:10:34 +00:00
formatAST(*ast.type, s, indent);
2011-12-26 07:07:30 +00:00
s << "\n";
2011-08-18 18:48:00 +00:00
}
2011-11-07 02:10:34 +00:00
void formatAST(const ASTAsterisk & ast, std::ostream & s, size_t indent)
2011-08-28 08:50:27 +00:00
{
s << "*";
}
2011-11-07 02:10:34 +00:00
void formatAST(const ASTOrderByElement & ast, std::ostream & s, size_t indent)
2011-09-04 05:14:52 +00:00
{
2011-11-07 02:10:34 +00:00
formatAST(*ast.children.front(), s, indent);
s << hilite_keyword << (ast.direction == -1 ? " DESC" : " ASC") << hilite_none;
2011-09-04 05:14:52 +00:00
}
2010-06-28 13:44:42 +00:00
}