2018-10-11 19:28:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Parsers/DumpASTNode.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class Context;
|
|
|
|
class ASTSubquery;
|
|
|
|
class ASTFunction;
|
|
|
|
struct ASTTableExpression;
|
|
|
|
|
|
|
|
|
|
|
|
/** Replace subqueries that return exactly one row
|
|
|
|
* ("scalar" subqueries) to the corresponding constants.
|
|
|
|
*
|
|
|
|
* If the subquery returns more than one column, it is replaced by a tuple of constants.
|
|
|
|
*
|
|
|
|
* Features
|
|
|
|
*
|
|
|
|
* A replacement occurs during query analysis, and not during the main runtime.
|
|
|
|
* This means that the progress indicator will not work during the execution of these requests,
|
|
|
|
* and also such queries can not be aborted.
|
|
|
|
*
|
|
|
|
* But the query result can be used for the index in the table.
|
|
|
|
*
|
|
|
|
* Scalar subqueries are executed on the request-initializer server.
|
|
|
|
* The request is sent to remote servers with already substituted constants.
|
|
|
|
*/
|
|
|
|
class ExecuteScalarSubqueriesVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExecuteScalarSubqueriesVisitor(const Context & context_, size_t subquery_depth_, std::ostream * ostr_ = nullptr)
|
|
|
|
: context(context_),
|
|
|
|
subquery_depth(subquery_depth_),
|
|
|
|
visit_depth(0),
|
|
|
|
ostr(ostr_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void visit(ASTPtr & ast) const
|
|
|
|
{
|
2018-11-01 17:07:20 +00:00
|
|
|
if (!tryVisit<ASTSubquery>(ast) &&
|
|
|
|
!tryVisit<ASTTableExpression>(ast) &&
|
|
|
|
!tryVisit<ASTFunction>(ast))
|
2018-10-11 19:28:59 +00:00
|
|
|
visitChildren(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Context & context;
|
|
|
|
size_t subquery_depth;
|
|
|
|
mutable size_t visit_depth;
|
|
|
|
std::ostream * ostr;
|
|
|
|
|
2018-11-01 17:07:20 +00:00
|
|
|
void visit(const ASTSubquery & subquery, ASTPtr & ast) const;
|
|
|
|
void visit(const ASTFunction & func, ASTPtr & ast) const;
|
|
|
|
void visit(const ASTTableExpression &, ASTPtr &) const;
|
2018-10-11 19:28:59 +00:00
|
|
|
|
|
|
|
void visitChildren(ASTPtr & ast) const
|
|
|
|
{
|
|
|
|
for (auto & child : ast->children)
|
|
|
|
visit(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-11-01 17:07:20 +00:00
|
|
|
bool tryVisit(ASTPtr & ast) const
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
2018-10-16 19:00:05 +00:00
|
|
|
if (const T * t = typeid_cast<const T *>(ast.get()))
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
2018-11-01 17:07:20 +00:00
|
|
|
DumpASTNode dump(*ast, ostr, visit_depth, "executeScalarSubqueries");
|
|
|
|
visit(*t, ast);
|
2018-10-11 19:28:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|