2018-10-11 19:28:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Common/typeid_cast.h>
|
2018-11-10 20:09:07 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Parsers/ASTSubquery.h>
|
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
2018-12-10 13:02:45 +00:00
|
|
|
#include <Interpreters/InDepthNodeVisitor.h>
|
2018-10-11 19:28:59 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-11-06 13:28:22 +00:00
|
|
|
/// Visitors consist of functions with unified interface 'void visit(Casted & x, ASTPtr & y)', there x is y, successfully casted to Casted.
|
|
|
|
/// Both types and fuction could have const specifiers. The second argument is used by visitor to replaces AST node (y) if needed.
|
2018-10-11 19:28:59 +00:00
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
2018-12-10 13:02:45 +00:00
|
|
|
class ExecuteScalarSubqueriesMatcher
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-12-10 13:02:45 +00:00
|
|
|
struct Data
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
2018-12-10 13:02:45 +00:00
|
|
|
const Context & context;
|
|
|
|
size_t subquery_depth;
|
|
|
|
};
|
2018-10-11 19:28:59 +00:00
|
|
|
|
2018-12-10 13:02:45 +00:00
|
|
|
static constexpr const char * label = "ExecuteScalarSubqueries";
|
2018-10-11 19:28:59 +00:00
|
|
|
|
2018-12-10 13:02:45 +00:00
|
|
|
static bool needChildVisit(ASTPtr & node, const ASTPtr &)
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
2018-12-10 13:02:45 +00:00
|
|
|
/// Processed
|
|
|
|
if (typeid_cast<ASTSubquery *>(node.get()) ||
|
|
|
|
typeid_cast<ASTFunction *>(node.get()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/// Don't descend into subqueries in FROM section
|
|
|
|
if (typeid_cast<ASTTableExpression *>(node.get()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 13:02:45 +00:00
|
|
|
static std::vector<ASTPtr *> visit(ASTPtr & ast, Data & data)
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
2018-12-10 13:02:45 +00:00
|
|
|
if (auto * t = typeid_cast<ASTSubquery *>(ast.get()))
|
|
|
|
visit(*t, ast, data);
|
|
|
|
if (auto * t = typeid_cast<ASTFunction *>(ast.get()))
|
|
|
|
return visit(*t, ast, data);
|
|
|
|
return {};
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|
2018-12-10 13:02:45 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void visit(const ASTSubquery & subquery, ASTPtr & ast, Data & data);
|
|
|
|
static std::vector<ASTPtr *> visit(const ASTFunction & func, ASTPtr & ast, Data & data);
|
2018-10-11 19:28:59 +00:00
|
|
|
};
|
|
|
|
|
2018-12-10 13:02:45 +00:00
|
|
|
using ExecuteScalarSubqueriesVisitor = InDepthNodeVisitor<ExecuteScalarSubqueriesMatcher, true>;
|
|
|
|
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|