aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/TableFunctions/ITableFunctionCluster.h
blob: 4776ff44a30c85a4e9b9f3410e5b4d0b96aa5484 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once

#include "clickhouse_config.h"

#include <Interpreters/Context.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Storages/StorageS3Cluster.h>
#include <Storages/checkAndGetLiteralArgument.h>
#include <TableFunctions/ITableFunction.h>
#include <TableFunctions/TableFunctionAzureBlobStorage.h>
#include <TableFunctions/TableFunctionS3.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
    extern const int CLUSTER_DOESNT_EXIST;
    extern const int LOGICAL_ERROR;
}

/// Base class for *Cluster table functions that require cluster_name for the first argument.
template <typename Base>
class ITableFunctionCluster : public Base
{
public:
    String getName() const override = 0;
    String getSignature() const override = 0;

    static void addColumnsStructureToArguments(ASTs & args, const String & desired_structure, const ContextPtr & context)
    {
        if (args.empty())
            throw Exception(ErrorCodes::LOGICAL_ERROR, "Unexpected empty list of arguments for {}Cluster table function", Base::name);

        ASTPtr cluster_name_arg = args.front();
        args.erase(args.begin());
        Base::addColumnsStructureToArguments(args, desired_structure, context);
        args.insert(args.begin(), cluster_name_arg);
    }

protected:
    void parseArguments(const ASTPtr & ast, ContextPtr context) override
    {
        /// Clone ast function, because we can modify its arguments like removing cluster_name
        Base::parseArguments(ast->clone(), context);
    }

    void parseArgumentsImpl(ASTs & args, const ContextPtr & context) override
    {
        if (args.empty())
            throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "The signature of table function {} shall be the following:\n{}", getName(), getSignature());

        /// Evaluate only first argument, everything else will be done Base class
        args[0] = evaluateConstantExpressionOrIdentifierAsLiteral(args[0], context);

        /// Cluster name is always the first
        cluster_name = checkAndGetLiteralArgument<String>(args[0], "cluster_name");

        if (!context->tryGetCluster(cluster_name))
            throw Exception(ErrorCodes::CLUSTER_DOESNT_EXIST, "Requested cluster '{}' not found", cluster_name);

        /// Just cut the first arg (cluster_name) and try to parse other table function arguments as is
        args.erase(args.begin());

        Base::parseArgumentsImpl(args, context);
    }

    String cluster_name;
};

}