1#include "duckdb/common/exception.hpp"
2#include "duckdb/parser/expression/columnref_expression.hpp"
3#include "duckdb/parser/expression/star_expression.hpp"
4#include "duckdb/parser/expression/table_star_expression.hpp"
5#include "duckdb/parser/transformer.hpp"
6
7using namespace duckdb;
8using namespace std;
9
10unique_ptr<ParsedExpression> Transformer::TransformColumnRef(PGColumnRef *root) {
11 auto fields = root->fields;
12 switch ((reinterpret_cast<PGNode *>(fields->head->data.ptr_value))->type) {
13 case T_PGString: {
14 if (fields->length < 1 || fields->length > 2) {
15 throw ParserException("Unexpected field length");
16 }
17 string column_name, table_name;
18 if (fields->length == 1) {
19 column_name = string(reinterpret_cast<PGValue *>(fields->head->data.ptr_value)->val.str);
20 return make_unique<ColumnRefExpression>(column_name, table_name);
21 } else {
22 table_name = string(reinterpret_cast<PGValue *>(fields->head->data.ptr_value)->val.str);
23 auto col_node = reinterpret_cast<PGNode *>(fields->head->next->data.ptr_value);
24 switch (col_node->type) {
25 case T_PGString: {
26 column_name = string(reinterpret_cast<PGValue *>(col_node)->val.str);
27 return make_unique<ColumnRefExpression>(column_name, table_name);
28 }
29 case T_PGAStar: {
30 return make_unique<TableStarExpression>(table_name);
31 }
32 default:
33 throw NotImplementedException("ColumnRef not implemented!");
34 }
35 }
36 }
37 case T_PGAStar: {
38 return make_unique<StarExpression>();
39 }
40 default:
41 break;
42 }
43 throw NotImplementedException("ColumnRef not implemented!");
44}
45