aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/ExecuteScalarSubqueriesVisitor.h
blob: 7e2e06d8f8ee4a0a1fec2e3ef01f1f65ada5aaf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once

#include <Core/Block.h>
#include <Interpreters/Context_fwd.h>
#include <Interpreters/InDepthNodeVisitor.h>
#include <Common/typeid_cast.h>

namespace DB
{

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, so
    * 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 ExecuteScalarSubqueriesMatcher
{
public:
    using Visitor = InDepthNodeVisitor<ExecuteScalarSubqueriesMatcher, true>;

    struct Data : public WithContext
    {
        size_t subquery_depth;
        Scalars & scalars;
        Scalars & local_scalars;
        bool only_analyze;
        bool is_create_parameterized_view;
        bool replace_only_to_literals;
        std::optional<size_t> max_literal_size;
    };

    static bool needChildVisit(ASTPtr & node, const ASTPtr &);
    static void visit(ASTPtr & ast, Data & data);

private:
    static void visit(const ASTSubquery & subquery, ASTPtr & ast, Data & data);
    static void visit(const ASTFunction & func, ASTPtr & ast, Data & data);
};

using ExecuteScalarSubqueriesVisitor = ExecuteScalarSubqueriesMatcher::Visitor;

bool worthConvertingScalarToLiteral(const Block & scalar, std::optional<size_t> max_literal_size);

}