forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathITableFunctionFileLike.h
More file actions
108 lines (85 loc) · 3.93 KB
/
ITableFunctionFileLike.h
File metadata and controls
108 lines (85 loc) · 3.93 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
#include <TableFunctions/ITableFunction.h>
#include <Core/Names.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/IAST_fwd.h>
#include <Storages/checkAndGetLiteralArgument.h>
#include <Interpreters/evaluateConstantExpression.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
class ColumnsDescription;
class Context;
/*
* function(source, [format, structure, compression_method]) - creates a temporary storage from formatted source
*/
class ITableFunctionFileLike : public ITableFunction
{
public:
static constexpr auto max_number_of_arguments = 4;
static constexpr auto signature = " - filename\n"
" - filename, format\n"
" - filename, format, structure\n"
" - filename, format, structure, compression_method\n";
virtual String getSignature() const
{
return signature;
}
bool needStructureHint() const override { return structure == "auto"; }
void setStructureHint(const ColumnsDescription & structure_hint_) override { structure_hint = structure_hint_; }
bool supportsReadingSubsetOfColumns(const ContextPtr & context) override;
NameSet getVirtualsToCheckBeforeUsingStructureHint() const override;
static size_t getMaxNumberOfArguments() { return max_number_of_arguments; }
static void updateStructureAndFormatArgumentsIfNeeded(ASTs & args, const String & structure, const String & format, const ContextPtr & context, bool with_structure)
{
if (args.empty() || args.size() > getMaxNumberOfArguments())
throw Exception(ErrorCodes::LOGICAL_ERROR, "Expected 1 to {} arguments in table function, got {}", getMaxNumberOfArguments(), args.size());
auto format_literal = std::make_shared<ASTLiteral>(format);
auto structure_literal = std::make_shared<ASTLiteral>(structure);
for (auto & arg : args)
arg = evaluateConstantExpressionOrIdentifierAsLiteral(arg, context);
/// f(filename)
if (args.size() == 1)
{
args.push_back(format_literal);
if (with_structure)
args.push_back(structure_literal);
}
/// f(filename, format)
else if (args.size() == 2)
{
if (checkAndGetLiteralArgument<String>(args[1], "format") == "auto")
args.back() = format_literal;
if (with_structure)
args.push_back(structure_literal);
}
/// f(filename, format, structure) or f(filename, format, structure, compression) or f(filename, format, compression)
else if (args.size() >= 3)
{
if (checkAndGetLiteralArgument<String>(args[1], "format") == "auto")
args[1] = format_literal;
if (with_structure && checkAndGetLiteralArgument<String>(args[2], "structure") == "auto")
args[2] = structure_literal;
}
}
protected:
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
virtual void parseArgumentsImpl(ASTs & args, const ContextPtr & context);
virtual void parseFirstArguments(const ASTPtr & arg, const ContextPtr & context);
virtual std::optional<String> tryGetFormatFromFirstArgument();
String filename;
String format = "auto";
String structure = "auto";
String compression_method = "auto";
ColumnsDescription structure_hint;
private:
StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override;
virtual StoragePtr getStorage(
const String & source, const String & format, const ColumnsDescription & columns, ContextPtr global_context,
const std::string & table_name, const String & compression_method, bool is_insert_query) const = 0;
bool hasStaticStructure() const override { return structure != "auto"; }
};
}