2018-10-11 19:28:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-10-19 20:36:35 +00:00
|
|
|
#include <Core/Block.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2018-12-10 13:02:45 +00:00
|
|
|
#include <Interpreters/InDepthNodeVisitor.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2018-10-11 19:28:59 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-12-10 13:19:09 +00:00
|
|
|
class ASTSubquery;
|
|
|
|
class ASTFunction;
|
|
|
|
struct ASTTableExpression;
|
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
|
|
|
|
*
|
2022-01-20 12:35:22 +00:00
|
|
|
* A replacement occurs during query analysis, and not during the main runtime, so
|
|
|
|
* the query result can be used for the index in the table.
|
2018-10-11 19:28:59 +00:00
|
|
|
*
|
|
|
|
* 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:
|
2019-02-22 15:45:47 +00:00
|
|
|
using Visitor = InDepthNodeVisitor<ExecuteScalarSubqueriesMatcher, true>;
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
struct Data : public WithContext
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
2018-12-10 13:02:45 +00:00
|
|
|
size_t subquery_depth;
|
2019-10-19 20:36:35 +00:00
|
|
|
Scalars & scalars;
|
2022-01-17 18:32:55 +00:00
|
|
|
Scalars & local_scalars;
|
2020-04-30 17:07:34 +00:00
|
|
|
bool only_analyze;
|
2018-12-10 13:02:45 +00:00
|
|
|
};
|
2018-10-11 19:28:59 +00:00
|
|
|
|
2018-12-10 13:19:09 +00:00
|
|
|
static bool needChildVisit(ASTPtr & node, const ASTPtr &);
|
2019-02-22 13:33:56 +00:00
|
|
|
static void visit(ASTPtr & ast, Data & data);
|
2018-12-10 13:02:45 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void visit(const ASTSubquery & subquery, ASTPtr & ast, Data & data);
|
2019-02-22 13:33:56 +00:00
|
|
|
static void visit(const ASTFunction & func, ASTPtr & ast, Data & data);
|
2018-10-11 19:28:59 +00:00
|
|
|
};
|
|
|
|
|
2019-02-22 15:45:47 +00:00
|
|
|
using ExecuteScalarSubqueriesVisitor = ExecuteScalarSubqueriesMatcher::Visitor;
|
2018-12-10 13:02:45 +00:00
|
|
|
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|