1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/common/union_by_name.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/types.hpp"
12#include "duckdb/common/vector.hpp"
13#include "duckdb/common/case_insensitive_map.hpp"
14#include "duckdb/common/helper.hpp"
15
16namespace duckdb {
17
18class UnionByName {
19public:
20 static void CombineUnionTypes(const vector<string> &new_names, const vector<LogicalType> &new_types,
21 vector<LogicalType> &union_col_types, vector<string> &union_col_names,
22 case_insensitive_map_t<idx_t> &union_names_map);
23
24 //! Union all files(readers) by their col names
25 template <class READER_TYPE, class OPTION_TYPE>
26 static vector<unique_ptr<READER_TYPE>> UnionCols(ClientContext &context, const vector<string> &files,
27 vector<LogicalType> &union_col_types,
28 vector<string> &union_col_names, OPTION_TYPE &options) {
29 vector<unique_ptr<READER_TYPE>> union_readers;
30 case_insensitive_map_t<idx_t> union_names_map;
31 for (idx_t file_idx = 0; file_idx < files.size(); ++file_idx) {
32 const auto file_name = files[file_idx];
33 auto reader = make_uniq<READER_TYPE>(context, file_name, options);
34
35 auto &col_names = reader->GetNames();
36 auto &sql_types = reader->GetTypes();
37 CombineUnionTypes(new_names: col_names, new_types: sql_types, union_col_types, union_col_names, union_names_map);
38 union_readers.push_back(std::move(reader));
39 }
40 return union_readers;
41 }
42};
43
44} // namespace duckdb
45