1 | #include "duckdb/function/table/range.hpp" |
2 | #include "duckdb/function/table_function.hpp" |
3 | #include "duckdb/function/function_set.hpp" |
4 | #include "duckdb/common/file_system.hpp" |
5 | #include "duckdb/main/config.hpp" |
6 | #include "duckdb/common/multi_file_reader.hpp" |
7 | |
8 | namespace duckdb { |
9 | |
10 | struct GlobFunctionBindData : public TableFunctionData { |
11 | vector<string> files; |
12 | }; |
13 | |
14 | static unique_ptr<FunctionData> GlobFunctionBind(ClientContext &context, TableFunctionBindInput &input, |
15 | vector<LogicalType> &return_types, vector<string> &names) { |
16 | auto result = make_uniq<GlobFunctionBindData>(); |
17 | result->files = MultiFileReader::GetFileList(context, input: input.inputs[0], name: "Globbing" , options: FileGlobOptions::ALLOW_EMPTY); |
18 | return_types.emplace_back(args: LogicalType::VARCHAR); |
19 | names.emplace_back(args: "file" ); |
20 | return std::move(result); |
21 | } |
22 | |
23 | struct GlobFunctionState : public GlobalTableFunctionState { |
24 | GlobFunctionState() : current_idx(0) { |
25 | } |
26 | |
27 | idx_t current_idx; |
28 | }; |
29 | |
30 | static unique_ptr<GlobalTableFunctionState> GlobFunctionInit(ClientContext &context, TableFunctionInitInput &input) { |
31 | return make_uniq<GlobFunctionState>(); |
32 | } |
33 | |
34 | static void GlobFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { |
35 | auto &bind_data = data_p.bind_data->Cast<GlobFunctionBindData>(); |
36 | auto &state = data_p.global_state->Cast<GlobFunctionState>(); |
37 | |
38 | idx_t count = 0; |
39 | idx_t next_idx = MinValue<idx_t>(a: state.current_idx + STANDARD_VECTOR_SIZE, b: bind_data.files.size()); |
40 | for (; state.current_idx < next_idx; state.current_idx++) { |
41 | output.data[0].SetValue(index: count, val: bind_data.files[state.current_idx]); |
42 | count++; |
43 | } |
44 | output.SetCardinality(count); |
45 | } |
46 | |
47 | void GlobTableFunction::RegisterFunction(BuiltinFunctions &set) { |
48 | TableFunction glob_function("glob" , {LogicalType::VARCHAR}, GlobFunction, GlobFunctionBind, GlobFunctionInit); |
49 | set.AddFunction(set: MultiFileReader::CreateFunctionSet(table_function: glob_function)); |
50 | } |
51 | |
52 | } // namespace duckdb |
53 | |