2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
2017-08-10 14:46:46 +00:00
|
|
|
#include <Parsers/ASTWithAlias.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
2015-08-05 21:39:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2015-08-06 03:26:27 +00:00
|
|
|
|
2017-08-10 14:46:46 +00:00
|
|
|
String ASTFunction::getColumnNameImpl() const
|
2016-05-03 23:19:14 +00:00
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
WriteBufferFromOwnString wb;
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(name, wb);
|
|
|
|
|
|
|
|
if (parameters)
|
|
|
|
{
|
|
|
|
writeChar('(', wb);
|
|
|
|
for (ASTs::const_iterator it = parameters->children.begin(); it != parameters->children.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != parameters->children.begin())
|
|
|
|
writeCString(", ", wb);
|
|
|
|
writeString((*it)->getColumnName(), wb);
|
|
|
|
}
|
|
|
|
writeChar(')', wb);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeChar('(', wb);
|
|
|
|
for (ASTs::const_iterator it = arguments->children.begin(); it != arguments->children.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != arguments->children.begin())
|
|
|
|
writeCString(", ", wb);
|
|
|
|
writeString((*it)->getColumnName(), wb);
|
|
|
|
}
|
|
|
|
writeChar(')', wb);
|
2017-07-31 21:39:24 +00:00
|
|
|
return wb.str();
|
2016-05-03 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** Get the text that identifies this element. */
|
2016-05-03 23:19:14 +00:00
|
|
|
String ASTFunction::getID() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return "Function_" + name;
|
2016-05-03 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr ASTFunction::clone() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto res = std::make_shared<ASTFunction>(*this);
|
|
|
|
res->children.clear();
|
2016-05-03 23:19:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (arguments) { res->arguments = arguments->clone(); res->children.push_back(res->arguments); }
|
|
|
|
if (parameters) { res->parameters = parameters->clone(); res->children.push_back(res->parameters); }
|
2016-05-03 23:19:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2016-05-03 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 23:46:15 +00:00
|
|
|
void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
2015-08-06 03:26:27 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
FormatStateStacked nested_need_parens = frame;
|
|
|
|
FormatStateStacked nested_dont_need_parens = frame;
|
|
|
|
nested_need_parens.need_parens = true;
|
|
|
|
nested_dont_need_parens.need_parens = false;
|
|
|
|
|
|
|
|
/// Should this function to be written as operator?
|
|
|
|
bool written = false;
|
|
|
|
if (arguments && !parameters)
|
|
|
|
{
|
|
|
|
if (0 == strcmp(name.data(), "CAST"))
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << name;
|
|
|
|
|
|
|
|
settings.ostr << '(' << (settings.hilite ? hilite_none : "");
|
|
|
|
|
|
|
|
arguments->children.front()->formatImpl(settings, state, nested_need_parens);
|
|
|
|
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << " AS "
|
|
|
|
<< (settings.hilite ? hilite_none : "");
|
|
|
|
|
|
|
|
settings.ostr << (settings.hilite ? hilite_function : "")
|
|
|
|
<< typeid_cast<const ASTLiteral &>(*arguments->children.back()).value.safeGet<String>()
|
|
|
|
<< (settings.hilite ? hilite_none : "");
|
|
|
|
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << ')'
|
|
|
|
<< (settings.hilite ? hilite_none : "");
|
|
|
|
|
|
|
|
written = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments->children.size() == 1)
|
|
|
|
{
|
|
|
|
const char * operators[] =
|
|
|
|
{
|
|
|
|
"negate", "-",
|
|
|
|
"not", "NOT ",
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const char ** func = operators; *func; func += 2)
|
|
|
|
{
|
|
|
|
if (0 == strcmp(name.c_str(), func[0]))
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << func[1] << (settings.hilite ? hilite_none : "");
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** A particularly stupid case. If we have a unary minus before a literal that is a negative number
|
|
|
|
* "-(-1)" or "- -1", this can not be formatted as `--1`, since this will be interpreted as a comment.
|
|
|
|
* Instead, add a space.
|
|
|
|
* PS. You can not just ask to add parentheses - see formatImpl for ASTLiteral.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
if (name == "negate" && typeid_cast<const ASTLiteral *>(&*arguments->children[0]))
|
|
|
|
settings.ostr << ' ';
|
|
|
|
|
|
|
|
arguments->formatImpl(settings, state, nested_need_parens);
|
|
|
|
written = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** need_parens - do I need parentheses around the expression with the operator.
|
|
|
|
* They are needed only if this expression is included in another expression with the operator.
|
|
|
|
*/
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!written && arguments->children.size() == 2)
|
|
|
|
{
|
|
|
|
const char * operators[] =
|
|
|
|
{
|
|
|
|
"multiply", " * ",
|
|
|
|
"divide", " / ",
|
|
|
|
"modulo", " % ",
|
|
|
|
"plus", " + ",
|
|
|
|
"minus", " - ",
|
|
|
|
"notEquals", " != ",
|
|
|
|
"lessOrEquals", " <= ",
|
|
|
|
"greaterOrEquals", " >= ",
|
|
|
|
"less", " < ",
|
|
|
|
"greater", " > ",
|
|
|
|
"equals", " = ",
|
|
|
|
"like", " LIKE ",
|
|
|
|
"notLike", " NOT LIKE ",
|
|
|
|
"in", " IN ",
|
|
|
|
"notIn", " NOT IN ",
|
|
|
|
"globalIn", " GLOBAL IN ",
|
|
|
|
"globalNotIn", " GLOBAL NOT IN ",
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const char ** func = operators; *func; func += 2)
|
|
|
|
{
|
|
|
|
if (0 == strcmp(name.c_str(), func[0]))
|
|
|
|
{
|
|
|
|
if (frame.need_parens)
|
|
|
|
settings.ostr << '(';
|
|
|
|
arguments->children[0]->formatImpl(settings, state, nested_need_parens);
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << func[1] << (settings.hilite ? hilite_none : "");
|
|
|
|
arguments->children[1]->formatImpl(settings, state, nested_need_parens);
|
|
|
|
if (frame.need_parens)
|
|
|
|
settings.ostr << ')';
|
|
|
|
written = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!written && 0 == strcmp(name.c_str(), "arrayElement"))
|
|
|
|
{
|
|
|
|
arguments->children[0]->formatImpl(settings, state, nested_need_parens);
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << '[' << (settings.hilite ? hilite_none : "");
|
|
|
|
arguments->children[1]->formatImpl(settings, state, nested_need_parens);
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << ']' << (settings.hilite ? hilite_none : "");
|
|
|
|
written = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!written && 0 == strcmp(name.c_str(), "tupleElement"))
|
|
|
|
{
|
|
|
|
arguments->children[0]->formatImpl(settings, state, nested_need_parens);
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << "." << (settings.hilite ? hilite_none : "");
|
|
|
|
arguments->children[1]->formatImpl(settings, state, nested_need_parens);
|
|
|
|
written = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!written && 0 == strcmp(name.c_str(), "lambda"))
|
|
|
|
{
|
|
|
|
/// Special case: one-element tuple in lhs of lambda is printed as its element.
|
|
|
|
|
|
|
|
if (frame.need_parens)
|
|
|
|
settings.ostr << '(';
|
|
|
|
|
|
|
|
const ASTFunction * first_arg_func = typeid_cast<const ASTFunction *>(arguments->children[0].get());
|
|
|
|
if (first_arg_func
|
|
|
|
&& first_arg_func->name == "tuple"
|
|
|
|
&& first_arg_func->arguments
|
|
|
|
&& first_arg_func->arguments->children.size() == 1)
|
|
|
|
{
|
|
|
|
first_arg_func->arguments->children[0]->formatImpl(settings, state, nested_need_parens);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
arguments->children[0]->formatImpl(settings, state, nested_need_parens);
|
|
|
|
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << " -> " << (settings.hilite ? hilite_none : "");
|
|
|
|
arguments->children[1]->formatImpl(settings, state, nested_need_parens);
|
|
|
|
if (frame.need_parens)
|
|
|
|
settings.ostr << ')';
|
|
|
|
written = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!written && arguments->children.size() >= 2)
|
|
|
|
{
|
|
|
|
const char * operators[] =
|
|
|
|
{
|
|
|
|
"and", " AND ",
|
|
|
|
"or", " OR ",
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const char ** func = operators; *func; func += 2)
|
|
|
|
{
|
|
|
|
if (0 == strcmp(name.c_str(), func[0]))
|
|
|
|
{
|
|
|
|
if (frame.need_parens)
|
|
|
|
settings.ostr << '(';
|
|
|
|
for (size_t i = 0; i < arguments->children.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << func[1] << (settings.hilite ? hilite_none : "");
|
|
|
|
arguments->children[i]->formatImpl(settings, state, nested_need_parens);
|
|
|
|
}
|
|
|
|
if (frame.need_parens)
|
|
|
|
settings.ostr << ')';
|
|
|
|
written = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!written && arguments->children.size() >= 1 && 0 == strcmp(name.c_str(), "array"))
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << '[' << (settings.hilite ? hilite_none : "");
|
|
|
|
for (size_t i = 0; i < arguments->children.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
settings.ostr << ", ";
|
|
|
|
arguments->children[i]->formatImpl(settings, state, nested_dont_need_parens);
|
|
|
|
}
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << ']' << (settings.hilite ? hilite_none : "");
|
|
|
|
written = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!written && arguments->children.size() >= 2 && 0 == strcmp(name.c_str(), "tuple"))
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << '(' << (settings.hilite ? hilite_none : "");
|
|
|
|
for (size_t i = 0; i < arguments->children.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
settings.ostr << ", ";
|
|
|
|
arguments->children[i]->formatImpl(settings, state, nested_dont_need_parens);
|
|
|
|
}
|
|
|
|
settings.ostr << (settings.hilite ? hilite_operator : "") << ')' << (settings.hilite ? hilite_none : "");
|
|
|
|
written = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!written)
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_function : "") << name;
|
|
|
|
|
|
|
|
if (parameters)
|
|
|
|
{
|
|
|
|
settings.ostr << '(' << (settings.hilite ? hilite_none : "");
|
|
|
|
parameters->formatImpl(settings, state, nested_dont_need_parens);
|
|
|
|
settings.ostr << (settings.hilite ? hilite_function : "") << ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments)
|
|
|
|
{
|
|
|
|
settings.ostr << '(' << (settings.hilite ? hilite_none : "");
|
|
|
|
arguments->formatImpl(settings, state, nested_dont_need_parens);
|
|
|
|
settings.ostr << (settings.hilite ? hilite_function : "") << ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.ostr << (settings.hilite ? hilite_none : "");
|
|
|
|
}
|
2015-08-06 03:26:27 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 21:39:42 +00:00
|
|
|
}
|