2019-02-10 17:40:52 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTSetQuery.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
2019-11-15 12:57:33 +00:00
|
|
|
#include <Parsers/ASTOrderByElement.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
2020-03-13 10:30:55 +00:00
|
|
|
#include <Interpreters/StorageID.h>
|
2020-11-09 16:05:40 +00:00
|
|
|
#include <IO/Operators.h>
|
2015-06-02 11:16:02 +00:00
|
|
|
|
2015-08-05 21:38:31 +00:00
|
|
|
|
2015-06-02 11:16:02 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int NOT_IMPLEMENTED;
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-02 11:16:02 +00:00
|
|
|
ASTPtr ASTSelectQuery::clone() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto res = std::make_shared<ASTSelectQuery>(*this);
|
|
|
|
res->children.clear();
|
2019-04-09 14:22:35 +00:00
|
|
|
res->positions.clear();
|
2015-06-02 11:16:02 +00:00
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
#define CLONE(expr) res->setExpression(expr, getExpression(expr, true))
|
2015-06-02 11:16:02 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** NOTE Members must clone exactly in the same order,
|
|
|
|
* in which they were inserted into `children` in ParserSelectQuery.
|
2018-02-23 08:05:21 +00:00
|
|
|
* This is important because of the children's names the identifier (getTreeHash) is compiled,
|
2017-04-02 17:37:49 +00:00
|
|
|
* which can be used for column identifiers in the case of subqueries in the IN statement.
|
|
|
|
* For distributed query processing, in case one of the servers is localhost and the other one is not,
|
|
|
|
* localhost query is executed within the process and is cloned,
|
|
|
|
* and the request is sent to the remote server in text form via TCP.
|
|
|
|
* And if the cloning order does not match the parsing order,
|
|
|
|
* then different servers will get different identifiers.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2019-04-09 14:22:35 +00:00
|
|
|
CLONE(Expression::WITH);
|
|
|
|
CLONE(Expression::SELECT);
|
|
|
|
CLONE(Expression::TABLES);
|
|
|
|
CLONE(Expression::PREWHERE);
|
|
|
|
CLONE(Expression::WHERE);
|
|
|
|
CLONE(Expression::GROUP_BY);
|
|
|
|
CLONE(Expression::HAVING);
|
2021-01-13 19:29:52 +00:00
|
|
|
CLONE(Expression::WINDOW);
|
2019-04-09 14:22:35 +00:00
|
|
|
CLONE(Expression::ORDER_BY);
|
2019-04-29 13:12:39 +00:00
|
|
|
CLONE(Expression::LIMIT_BY_OFFSET);
|
|
|
|
CLONE(Expression::LIMIT_BY_LENGTH);
|
2019-04-09 14:22:35 +00:00
|
|
|
CLONE(Expression::LIMIT_BY);
|
|
|
|
CLONE(Expression::LIMIT_OFFSET);
|
|
|
|
CLONE(Expression::LIMIT_LENGTH);
|
|
|
|
CLONE(Expression::SETTINGS);
|
2015-06-02 11:16:02 +00:00
|
|
|
|
|
|
|
#undef CLONE
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-06-02 11:16:02 +00:00
|
|
|
}
|
2015-06-25 17:38:54 +00:00
|
|
|
|
2018-02-25 00:50:53 +00:00
|
|
|
|
2020-08-20 02:01:40 +00:00
|
|
|
void ASTSelectQuery::updateTreeHashImpl(SipHash & hash_state) const
|
|
|
|
{
|
|
|
|
hash_state.update(distinct);
|
|
|
|
hash_state.update(group_by_with_totals);
|
|
|
|
hash_state.update(group_by_with_rollup);
|
|
|
|
hash_state.update(group_by_with_cube);
|
|
|
|
hash_state.update(limit_with_ties);
|
|
|
|
IAST::updateTreeHashImpl(hash_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-25 06:34:20 +00:00
|
|
|
void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const
|
2015-08-05 21:38:31 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
frame.current_select = this;
|
2018-05-17 15:17:07 +00:00
|
|
|
frame.need_parens = false;
|
2020-09-08 20:34:10 +00:00
|
|
|
frame.expression_list_prepend_whitespace = true;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string indent_str = s.one_line ? "" : std::string(4 * frame.indent, ' ');
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (with())
|
2017-08-10 14:46:46 +00:00
|
|
|
{
|
2020-09-08 20:34:10 +00:00
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "WITH" << (s.hilite ? hilite_none : "");
|
2017-08-10 14:46:46 +00:00
|
|
|
s.one_line
|
2019-04-09 14:22:35 +00:00
|
|
|
? with()->formatImpl(s, state, frame)
|
|
|
|
: with()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
|
2017-08-10 14:46:46 +00:00
|
|
|
s.ostr << s.nl_or_ws;
|
|
|
|
}
|
|
|
|
|
2020-09-08 20:34:10 +00:00
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "SELECT" << (distinct ? " DISTINCT" : "") << (s.hilite ? hilite_none : "");
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
s.one_line
|
2019-04-09 14:22:35 +00:00
|
|
|
? select()->formatImpl(s, state, frame)
|
|
|
|
: select()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (tables())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2021-05-15 09:11:13 +00:00
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "FROM" << (s.hilite ? hilite_none : "");
|
2019-04-09 14:22:35 +00:00
|
|
|
tables()->formatImpl(s, state, frame);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (prewhere())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "PREWHERE " << (s.hilite ? hilite_none : "");
|
2019-04-09 14:22:35 +00:00
|
|
|
prewhere()->formatImpl(s, state, frame);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (where())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "WHERE " << (s.hilite ? hilite_none : "");
|
2019-04-09 14:22:35 +00:00
|
|
|
where()->formatImpl(s, state, frame);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (groupBy())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-09-08 20:34:10 +00:00
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "GROUP BY" << (s.hilite ? hilite_none : "");
|
2017-04-01 07:20:54 +00:00
|
|
|
s.one_line
|
2019-04-09 14:22:35 +00:00
|
|
|
? groupBy()->formatImpl(s, state, frame)
|
|
|
|
: groupBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 16:23:28 +00:00
|
|
|
if (group_by_with_rollup)
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << (s.one_line ? "" : " ") << "WITH ROLLUP" << (s.hilite ? hilite_none : "");
|
|
|
|
|
2018-09-19 11:18:38 +00:00
|
|
|
if (group_by_with_cube)
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << (s.one_line ? "" : " ") << "WITH CUBE" << (s.hilite ? hilite_none : "");
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (group_by_with_totals)
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << (s.one_line ? "" : " ") << "WITH TOTALS" << (s.hilite ? hilite_none : "");
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (having())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "HAVING " << (s.hilite ? hilite_none : "");
|
2019-04-09 14:22:35 +00:00
|
|
|
having()->formatImpl(s, state, frame);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 19:29:52 +00:00
|
|
|
if (window())
|
|
|
|
{
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str <<
|
2021-03-23 22:40:27 +00:00
|
|
|
"WINDOW" << (s.hilite ? hilite_none : "");
|
2021-03-24 12:36:39 +00:00
|
|
|
window()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
|
2021-01-13 19:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (orderBy())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-09-08 20:34:10 +00:00
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "ORDER BY" << (s.hilite ? hilite_none : "");
|
2017-04-01 07:20:54 +00:00
|
|
|
s.one_line
|
2019-04-09 14:22:35 +00:00
|
|
|
? orderBy()->formatImpl(s, state, frame)
|
|
|
|
: orderBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 13:12:39 +00:00
|
|
|
if (limitByLength())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "LIMIT " << (s.hilite ? hilite_none : "");
|
2019-04-29 13:12:39 +00:00
|
|
|
if (limitByOffset())
|
|
|
|
{
|
|
|
|
limitByOffset()->formatImpl(s, state, frame);
|
|
|
|
s.ostr << ", ";
|
|
|
|
}
|
|
|
|
limitByLength()->formatImpl(s, state, frame);
|
2020-09-08 20:34:10 +00:00
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << " BY" << (s.hilite ? hilite_none : "");
|
2017-04-01 07:20:54 +00:00
|
|
|
s.one_line
|
2019-04-09 14:22:35 +00:00
|
|
|
? limitBy()->formatImpl(s, state, frame)
|
|
|
|
: limitBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (limitLength())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "LIMIT " << (s.hilite ? hilite_none : "");
|
2019-04-09 14:22:35 +00:00
|
|
|
if (limitOffset())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
limitOffset()->formatImpl(s, state, frame);
|
2017-04-01 07:20:54 +00:00
|
|
|
s.ostr << ", ";
|
|
|
|
}
|
2019-04-09 14:22:35 +00:00
|
|
|
limitLength()->formatImpl(s, state, frame);
|
2019-04-18 08:24:06 +00:00
|
|
|
if (limit_with_ties)
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << " WITH TIES" << (s.hilite ? hilite_none : "");
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2020-05-11 06:26:01 +00:00
|
|
|
else if (limitOffset())
|
2020-05-04 12:52:38 +00:00
|
|
|
{
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "OFFSET " << (s.hilite ? hilite_none : "");
|
|
|
|
limitOffset()->formatImpl(s, state, frame);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
if (settings())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "SETTINGS " << (s.hilite ? hilite_none : "");
|
2019-04-09 14:22:35 +00:00
|
|
|
settings()->formatImpl(s, state, frame);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-08-05 21:38:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 15:48:11 +00:00
|
|
|
|
|
|
|
/// Compatibility functions. TODO Remove.
|
|
|
|
|
|
|
|
|
|
|
|
static const ASTTableExpression * getFirstTableExpression(const ASTSelectQuery & select)
|
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!select.tables())
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
2016-07-21 15:48:11 +00:00
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
const auto & tables_in_select_query = select.tables()->as<ASTTablesInSelectQuery &>();
|
2019-03-15 16:14:13 +00:00
|
|
|
if (tables_in_select_query.children.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
2016-07-21 15:48:11 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & tables_element = tables_in_select_query.children[0]->as<ASTTablesInSelectQueryElement &>();
|
|
|
|
if (!tables_element.table_expression)
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
2016-07-21 15:48:11 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
return tables_element.table_expression->as<ASTTableExpression>();
|
2016-07-21 15:48:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:39:28 +00:00
|
|
|
static ASTTableExpression * getFirstTableExpression(ASTSelectQuery & select)
|
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!select.tables())
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
2016-07-22 20:39:28 +00:00
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
auto & tables_in_select_query = select.tables()->as<ASTTablesInSelectQuery &>();
|
2019-03-15 16:14:13 +00:00
|
|
|
if (tables_in_select_query.children.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
2016-07-22 20:39:28 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
auto & tables_element = tables_in_select_query.children[0]->as<ASTTablesInSelectQueryElement &>();
|
|
|
|
if (!tables_element.table_expression)
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
2016-07-22 20:39:28 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
return tables_element.table_expression->as<ASTTableExpression>();
|
2016-07-22 20:39:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 15:48:11 +00:00
|
|
|
static const ASTArrayJoin * getFirstArrayJoin(const ASTSelectQuery & select)
|
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!select.tables())
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
const auto & tables_in_select_query = select.tables()->as<ASTTablesInSelectQuery &>();
|
2019-03-15 16:14:13 +00:00
|
|
|
if (tables_in_select_query.children.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
const ASTArrayJoin * array_join = nullptr;
|
2019-03-15 16:14:13 +00:00
|
|
|
for (const auto & child : tables_in_select_query.children)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & tables_element = child->as<ASTTablesInSelectQueryElement &>();
|
|
|
|
if (tables_element.array_join)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!array_join)
|
2019-03-15 16:14:13 +00:00
|
|
|
array_join = tables_element.array_join->as<ASTArrayJoin>();
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
throw Exception("Support for more than one ARRAY JOIN in query is not implemented", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_join;
|
2016-07-21 15:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const ASTTablesInSelectQueryElement * getFirstTableJoin(const ASTSelectQuery & select)
|
|
|
|
{
|
2019-04-09 14:22:35 +00:00
|
|
|
if (!select.tables())
|
2019-04-18 10:39:25 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
const auto & tables_in_select_query = select.tables()->as<ASTTablesInSelectQuery &>();
|
2019-03-15 16:14:13 +00:00
|
|
|
if (tables_in_select_query.children.empty())
|
2019-04-18 10:39:25 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const ASTTablesInSelectQueryElement * joined_table = nullptr;
|
2019-03-15 16:14:13 +00:00
|
|
|
for (const auto & child : tables_in_select_query.children)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & tables_element = child->as<ASTTablesInSelectQueryElement &>();
|
|
|
|
if (tables_element.table_join)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!joined_table)
|
2019-03-15 16:14:13 +00:00
|
|
|
joined_table = &tables_element;
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2019-11-30 20:41:02 +00:00
|
|
|
throw Exception("Multiple JOIN does not support the query.", ErrorCodes::NOT_IMPLEMENTED);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return joined_table;
|
2016-07-21 15:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
ASTPtr ASTSelectQuery::sampleSize() const
|
2016-07-21 15:48:11 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
|
|
|
if (!table_expression)
|
|
|
|
return {};
|
2016-07-21 15:48:11 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return table_expression->sample_size;
|
2016-07-21 15:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
ASTPtr ASTSelectQuery::sampleOffset() const
|
2016-07-21 15:48:11 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
|
|
|
if (!table_expression)
|
|
|
|
return {};
|
2016-07-21 15:48:11 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return table_expression->sample_offset;
|
2016-07-21 15:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ASTSelectQuery::final() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
|
|
|
if (!table_expression)
|
|
|
|
return {};
|
2016-07-21 15:48:11 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return table_expression->final;
|
2016-07-21 15:48:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 12:57:33 +00:00
|
|
|
bool ASTSelectQuery::withFill() const
|
|
|
|
{
|
2021-01-31 08:24:37 +00:00
|
|
|
const ASTPtr order_by = orderBy();
|
|
|
|
if (!order_by)
|
2019-11-15 12:57:33 +00:00
|
|
|
return false;
|
|
|
|
|
2021-01-31 08:24:37 +00:00
|
|
|
for (const auto & order_expression_element : order_by->children)
|
2019-11-15 12:57:33 +00:00
|
|
|
if (order_expression_element->as<ASTOrderByElement &>().with_fill)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-21 15:48:11 +00:00
|
|
|
|
2021-06-28 09:53:54 +00:00
|
|
|
std::pair<ASTPtr, bool> ASTSelectQuery::arrayJoinExpressionList() const
|
2016-07-21 15:48:11 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ASTArrayJoin * array_join = getFirstArrayJoin(*this);
|
|
|
|
if (!array_join)
|
|
|
|
return {};
|
2016-07-21 15:48:11 +00:00
|
|
|
|
2021-06-28 09:53:54 +00:00
|
|
|
bool is_left = (array_join->kind == ASTArrayJoin::Kind::Left);
|
|
|
|
return {array_join->expression_list, is_left};
|
2016-07-21 15:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ASTTablesInSelectQueryElement * ASTSelectQuery::join() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return getFirstTableJoin(*this);
|
2016-07-21 15:48:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 07:07:00 +00:00
|
|
|
static String getTableExpressionAlias(const ASTTableExpression * table_expression)
|
|
|
|
{
|
|
|
|
if (table_expression->subquery)
|
|
|
|
return table_expression->subquery->tryGetAlias();
|
|
|
|
else if (table_expression->table_function)
|
|
|
|
return table_expression->table_function->tryGetAlias();
|
|
|
|
else if (table_expression->database_and_table_name)
|
|
|
|
return table_expression->database_and_table_name->tryGetAlias();
|
|
|
|
|
|
|
|
return String();
|
|
|
|
}
|
2016-07-22 20:39:28 +00:00
|
|
|
|
2018-07-25 12:31:47 +00:00
|
|
|
void ASTSelectQuery::replaceDatabaseAndTable(const String & database_name, const String & table_name)
|
2020-03-12 18:04:29 +00:00
|
|
|
{
|
|
|
|
assert(database_name != "_temporary_and_external_tables");
|
|
|
|
replaceDatabaseAndTable(StorageID(database_name, table_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTSelectQuery::replaceDatabaseAndTable(const StorageID & table_id)
|
2016-07-22 20:39:28 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
|
|
|
|
|
|
|
if (!table_expression)
|
|
|
|
{
|
2019-04-10 11:44:58 +00:00
|
|
|
setExpression(Expression::TABLES, std::make_shared<ASTTablesInSelectQuery>());
|
2017-04-01 07:20:54 +00:00
|
|
|
auto element = std::make_shared<ASTTablesInSelectQueryElement>();
|
|
|
|
auto table_expr = std::make_shared<ASTTableExpression>();
|
|
|
|
element->table_expression = table_expr;
|
|
|
|
element->children.emplace_back(table_expr);
|
2019-04-10 11:44:58 +00:00
|
|
|
tables()->children.emplace_back(element);
|
2017-04-01 07:20:54 +00:00
|
|
|
table_expression = table_expr.get();
|
|
|
|
}
|
2018-07-27 21:33:30 +00:00
|
|
|
|
2019-01-03 07:07:00 +00:00
|
|
|
String table_alias = getTableExpressionAlias(table_expression);
|
2020-10-26 15:49:00 +00:00
|
|
|
table_expression->database_and_table_name = std::make_shared<ASTTableIdentifier>(table_id);
|
2019-01-03 07:07:00 +00:00
|
|
|
|
|
|
|
if (!table_alias.empty())
|
|
|
|
table_expression->database_and_table_name->setAlias(table_alias);
|
2018-07-25 12:31:47 +00:00
|
|
|
}
|
2018-07-24 13:10:34 +00:00
|
|
|
|
2018-07-25 12:31:47 +00:00
|
|
|
|
|
|
|
void ASTSelectQuery::addTableFunction(ASTPtr & table_function_ptr)
|
|
|
|
{
|
|
|
|
ASTTableExpression * table_expression = getFirstTableExpression(*this);
|
|
|
|
|
|
|
|
if (!table_expression)
|
|
|
|
{
|
2019-04-10 11:44:58 +00:00
|
|
|
setExpression(Expression::TABLES, std::make_shared<ASTTablesInSelectQuery>());
|
2018-07-25 12:31:47 +00:00
|
|
|
auto element = std::make_shared<ASTTablesInSelectQueryElement>();
|
|
|
|
auto table_expr = std::make_shared<ASTTableExpression>();
|
|
|
|
element->table_expression = table_expr;
|
|
|
|
element->children.emplace_back(table_expr);
|
2019-04-10 11:44:58 +00:00
|
|
|
tables()->children.emplace_back(element);
|
2018-07-25 12:31:47 +00:00
|
|
|
table_expression = table_expr.get();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-07-27 21:33:30 +00:00
|
|
|
|
2019-01-03 07:07:00 +00:00
|
|
|
String table_alias = getTableExpressionAlias(table_expression);
|
|
|
|
/// Maybe need to modify the alias, so we should clone new table_function node
|
|
|
|
table_expression->table_function = table_function_ptr->clone();
|
2018-07-25 12:31:47 +00:00
|
|
|
table_expression->database_and_table_name = nullptr;
|
2019-01-03 07:07:00 +00:00
|
|
|
|
|
|
|
if (table_alias.empty())
|
|
|
|
table_expression->table_function->setAlias(table_alias);
|
2016-07-22 20:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 14:22:35 +00:00
|
|
|
void ASTSelectQuery::setExpression(Expression expr, ASTPtr && ast)
|
|
|
|
{
|
|
|
|
if (ast)
|
|
|
|
{
|
|
|
|
auto it = positions.find(expr);
|
|
|
|
if (it == positions.end())
|
|
|
|
{
|
|
|
|
positions[expr] = children.size();
|
|
|
|
children.emplace_back(ast);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
children[it->second] = ast;
|
|
|
|
}
|
|
|
|
else if (positions.count(expr))
|
|
|
|
{
|
|
|
|
size_t pos = positions[expr];
|
|
|
|
children.erase(children.begin() + pos);
|
|
|
|
positions.erase(expr);
|
|
|
|
for (auto & pr : positions)
|
|
|
|
if (pr.second > pos)
|
|
|
|
--pr.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr & ASTSelectQuery::getExpression(Expression expr)
|
|
|
|
{
|
|
|
|
if (!positions.count(expr))
|
|
|
|
throw Exception("Get expression before set", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
return children[positions[expr]];
|
|
|
|
}
|
|
|
|
|
2021-06-30 09:41:46 +00:00
|
|
|
void ASTSelectQuery::setFinal() // NOLINT method can be made const
|
2021-06-28 10:35:55 +00:00
|
|
|
{
|
|
|
|
auto & tables_in_select_query = tables()->as<ASTTablesInSelectQuery &>();
|
|
|
|
|
|
|
|
if (tables_in_select_query.children.empty())
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Tables list is empty, it's a bug");
|
|
|
|
|
|
|
|
auto & tables_element = tables_in_select_query.children[0]->as<ASTTablesInSelectQueryElement &>();
|
|
|
|
|
|
|
|
if (!tables_element.table_expression)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "There is no table expression, it's a bug");
|
|
|
|
|
|
|
|
tables_element.table_expression->as<ASTTableExpression &>().final = true;
|
|
|
|
}
|
|
|
|
|
2018-08-26 00:48:38 +00:00
|
|
|
}
|