aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/TableFunctions/ITableFunctionDataLake.h
blob: e9ecb3d5771e75373e6da18e31d3434ed03a26e5 (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
#pragma once

#include "clickhouse_config.h"

#if USE_AWS_S3

#    include <Access/Common/AccessFlags.h>
#    include <Formats/FormatFactory.h>
#    include <Interpreters/Context.h>
#    include <Interpreters/parseColumnsListForTableFunction.h>
#    include <Storages/IStorage.h>
#    include <TableFunctions/ITableFunction.h>

namespace DB
{

template <typename Name, typename Storage, typename TableFunction>
class ITableFunctionDataLake : public TableFunction
{
public:
    static constexpr auto name = Name::name;
    std::string getName() const override { return name; }

protected:
    StoragePtr executeImpl(
        const ASTPtr & /*ast_function*/,
        ContextPtr context,
        const std::string & table_name,
        ColumnsDescription /*cached_columns*/,
        bool /*is_insert_query*/) const override
    {
        ColumnsDescription columns;
        if (TableFunction::configuration.structure != "auto")
            columns = parseColumnsListFromString(TableFunction::configuration.structure, context);

        StoragePtr storage = std::make_shared<Storage>(
            TableFunction::configuration, context, StorageID(TableFunction::getDatabaseName(), table_name),
            columns, ConstraintsDescription{}, String{}, std::nullopt);

        storage->startup();
        return storage;
    }

    const char * getStorageTypeName() const override { return Storage::name; }

    ColumnsDescription getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const override
    {
        if (TableFunction::configuration.structure == "auto")
        {
            context->checkAccess(TableFunction::getSourceAccessType());
            return Storage::getTableStructureFromData(TableFunction::configuration, std::nullopt, context);
        }

        return parseColumnsListFromString(TableFunction::configuration.structure, context);
    }

    void parseArguments(const ASTPtr & ast_function, ContextPtr context) override
    {
        /// Set default format to Parquet if it's not specified in arguments.
        TableFunction::configuration.format = "Parquet";
        TableFunction::parseArguments(ast_function, context);
    }
};
}

#endif