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/constant_expression.hpp" |
7 | #include "duckdb/parser/expression/function_expression.hpp" |
8 | #include "duckdb/common/string_util.hpp" |
9 | |
10 | namespace duckdb { |
11 | |
12 | ReadCSVRelation::ReadCSVRelation(ClientContext &context, string csv_file_p, vector<ColumnDefinition> columns_p, |
13 | string alias_p) |
14 | : Relation(context, RelationType::READ_CSV_RELATION), csv_file(move(csv_file_p)), alias(move(alias_p)), |
15 | columns(move(columns_p)) { |
16 | if (alias.empty()) { |
17 | alias = StringUtil::Split(csv_file, "." )[0]; |
18 | } |
19 | } |
20 | |
21 | unique_ptr<QueryNode> ReadCSVRelation::GetQueryNode() { |
22 | auto result = make_unique<SelectNode>(); |
23 | result->select_list.push_back(make_unique<StarExpression>()); |
24 | result->from_table = GetTableRef(); |
25 | return move(result); |
26 | } |
27 | |
28 | unique_ptr<TableRef> ReadCSVRelation::GetTableRef() { |
29 | auto table_ref = make_unique<TableFunctionRef>(); |
30 | table_ref->alias = alias; |
31 | vector<unique_ptr<ParsedExpression>> children; |
32 | // CSV file |
33 | children.push_back(make_unique<ConstantExpression>(SQLType::VARCHAR, Value(csv_file))); |
34 | children.push_back(make_unique<ConstantExpression>(SQLType::VARCHAR, Value("," ))); |
35 | // parameters |
36 | child_list_t<Value> column_names; |
37 | for (idx_t i = 0; i < columns.size(); i++) { |
38 | column_names.push_back(make_pair(columns[i].name, Value(SQLTypeToString(columns[i].type)))); |
39 | } |
40 | children.push_back(make_unique<ConstantExpression>(SQLType::STRUCT, Value::STRUCT(move(column_names)))); |
41 | table_ref->function = make_unique<FunctionExpression>("read_csv" , children); |
42 | return move(table_ref); |
43 | } |
44 | |
45 | string ReadCSVRelation::GetAlias() { |
46 | return alias; |
47 | } |
48 | |
49 | const vector<ColumnDefinition> &ReadCSVRelation::Columns() { |
50 | return columns; |
51 | } |
52 | |
53 | string ReadCSVRelation::ToString(idx_t depth) { |
54 | return RenderWhitespace(depth) + "Read CSV [" + csv_file + "]" ; |
55 | } |
56 | |
57 | } // namespace duckdb |
58 | |