aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/FunctionMaskingArgumentCheckVisitor.h
blob: 1f3c44f8087f9349e0b5e39996d116d3645866d3 (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
#pragma once


#include <DataTypes/Serializations/ISerialization.h>
#include <Interpreters/InDepthNodeVisitor.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>

namespace DB
{

/// Checks from bottom to top if a function's alias shadows the name
/// of one of it's arguments, e.g.
/// SELECT toString(dummy) as dummy FROM system.one GROUP BY dummy;
class FunctionMaskingArgumentCheckMatcher
{
public:
    struct Data
    {
        const String& alias;
        bool is_rejected = false;
        void reject() { is_rejected = true; }
    };

    static void visit(const ASTPtr & ast, Data & data)
    {
        if (data.is_rejected)
            return;
        if (const auto & identifier = ast->as<ASTIdentifier>())
            visit(*identifier, data);
    }

    static void visit(const ASTIdentifier & ast, Data & data)
    {
        if (ast.getAliasOrColumnName() == data.alias)
            data.reject();
    }

    static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return true; }
};

using FunctionMaskingArgumentCheckVisitor = ConstInDepthNodeVisitor<FunctionMaskingArgumentCheckMatcher, false>;

}