1 | #include "duckdb/main/relation/read_csv_relation.hpp" |
2 | #include "duckdb/parser/tableref/table_function_ref.hpp" |
3 | #include "duckdb/parser/tableref/basetableref.hpp" |
4 | #include "duckdb/parser/query_node/select_node.hpp" |
5 | #include "duckdb/parser/expression/star_expression.hpp" |
6 | #include "duckdb/parser/expression/columnref_expression.hpp" |
7 | #include "duckdb/parser/expression/comparison_expression.hpp" |
8 | #include "duckdb/parser/expression/constant_expression.hpp" |
9 | #include "duckdb/parser/expression/function_expression.hpp" |
10 | #include "duckdb/common/string_util.hpp" |
11 | #include "duckdb/execution/operator/persistent/buffered_csv_reader.hpp" |
12 | |
13 | namespace duckdb { |
14 | |
15 | ReadCSVRelation::ReadCSVRelation(const std::shared_ptr<ClientContext> &context, const string &csv_file, |
16 | vector<ColumnDefinition> columns_p, string alias_p) |
17 | : TableFunctionRelation(context, "read_csv" , {Value(csv_file)}, nullptr, false), alias(std::move(alias_p)), |
18 | auto_detect(false) { |
19 | |
20 | if (alias.empty()) { |
21 | alias = StringUtil::Split(input: csv_file, split: "." )[0]; |
22 | } |
23 | |
24 | columns = std::move(columns_p); |
25 | |
26 | child_list_t<Value> column_names; |
27 | for (idx_t i = 0; i < columns.size(); i++) { |
28 | column_names.push_back(x: make_pair(x: columns[i].Name(), y: Value(columns[i].Type().ToString()))); |
29 | } |
30 | |
31 | AddNamedParameter(name: "columns" , argument: Value::STRUCT(values: std::move(column_names))); |
32 | } |
33 | |
34 | ReadCSVRelation::ReadCSVRelation(const std::shared_ptr<ClientContext> &context, const string &csv_file, |
35 | BufferedCSVReaderOptions options, string alias_p) |
36 | : TableFunctionRelation(context, "read_csv_auto" , {Value(csv_file)}, nullptr, false), alias(std::move(alias_p)), |
37 | auto_detect(true) { |
38 | |
39 | if (alias.empty()) { |
40 | alias = StringUtil::Split(input: csv_file, split: "." )[0]; |
41 | } |
42 | |
43 | // Force auto_detect for this constructor |
44 | options.auto_detect = true; |
45 | BufferedCSVReader reader(*context, std::move(options)); |
46 | |
47 | auto &types = reader.GetTypes(); |
48 | auto &names = reader.GetNames(); |
49 | for (idx_t i = 0; i < types.size(); i++) { |
50 | columns.emplace_back(args: names[i], args: types[i]); |
51 | } |
52 | |
53 | AddNamedParameter(name: "auto_detect" , argument: Value::BOOLEAN(value: true)); |
54 | } |
55 | |
56 | string ReadCSVRelation::GetAlias() { |
57 | return alias; |
58 | } |
59 | |
60 | } // namespace duckdb |
61 | |