1#include "duckdb/common/exception.hpp"
2#include "duckdb/parser/expression/default_expression.hpp"
3#include "duckdb/parser/transformer.hpp"
4
5namespace duckdb {
6
7unique_ptr<ParsedExpression> Transformer::TransformResTarget(duckdb_libpgquery::PGResTarget &root) {
8 auto expr = TransformExpression(node: root.val);
9 if (!expr) {
10 return nullptr;
11 }
12 if (root.name) {
13 expr->alias = string(root.name);
14 }
15 return expr;
16}
17
18unique_ptr<ParsedExpression> Transformer::TransformNamedArg(duckdb_libpgquery::PGNamedArgExpr &root) {
19
20 auto expr = TransformExpression(node: PGPointerCast<duckdb_libpgquery::PGNode>(ptr: root.arg));
21 if (root.name) {
22 expr->alias = string(root.name);
23 }
24 return expr;
25}
26
27unique_ptr<ParsedExpression> Transformer::TransformExpression(duckdb_libpgquery::PGNode &node) {
28
29 auto stack_checker = StackCheck();
30
31 switch (node.type) {
32 case duckdb_libpgquery::T_PGColumnRef:
33 return TransformColumnRef(root&: PGCast<duckdb_libpgquery::PGColumnRef>(node));
34 case duckdb_libpgquery::T_PGAConst:
35 return TransformConstant(c&: PGCast<duckdb_libpgquery::PGAConst>(node));
36 case duckdb_libpgquery::T_PGAExpr:
37 return TransformAExpr(root&: PGCast<duckdb_libpgquery::PGAExpr>(node));
38 case duckdb_libpgquery::T_PGFuncCall:
39 return TransformFuncCall(root&: PGCast<duckdb_libpgquery::PGFuncCall>(node));
40 case duckdb_libpgquery::T_PGBoolExpr:
41 return TransformBoolExpr(root&: PGCast<duckdb_libpgquery::PGBoolExpr>(node));
42 case duckdb_libpgquery::T_PGTypeCast:
43 return TransformTypeCast(root&: PGCast<duckdb_libpgquery::PGTypeCast>(node));
44 case duckdb_libpgquery::T_PGCaseExpr:
45 return TransformCase(root&: PGCast<duckdb_libpgquery::PGCaseExpr>(node));
46 case duckdb_libpgquery::T_PGSubLink:
47 return TransformSubquery(root&: PGCast<duckdb_libpgquery::PGSubLink>(node));
48 case duckdb_libpgquery::T_PGCoalesceExpr:
49 return TransformCoalesce(root&: PGCast<duckdb_libpgquery::PGAExpr>(node));
50 case duckdb_libpgquery::T_PGNullTest:
51 return TransformNullTest(root&: PGCast<duckdb_libpgquery::PGNullTest>(node));
52 case duckdb_libpgquery::T_PGResTarget:
53 return TransformResTarget(root&: PGCast<duckdb_libpgquery::PGResTarget>(node));
54 case duckdb_libpgquery::T_PGParamRef:
55 return TransformParamRef(node&: PGCast<duckdb_libpgquery::PGParamRef>(node));
56 case duckdb_libpgquery::T_PGNamedArgExpr:
57 return TransformNamedArg(root&: PGCast<duckdb_libpgquery::PGNamedArgExpr>(node));
58 case duckdb_libpgquery::T_PGSQLValueFunction:
59 return TransformSQLValueFunction(node&: PGCast<duckdb_libpgquery::PGSQLValueFunction>(node));
60 case duckdb_libpgquery::T_PGSetToDefault:
61 return make_uniq<DefaultExpression>();
62 case duckdb_libpgquery::T_PGCollateClause:
63 return TransformCollateExpr(collate&: PGCast<duckdb_libpgquery::PGCollateClause>(node));
64 case duckdb_libpgquery::T_PGIntervalConstant:
65 return TransformInterval(root&: PGCast<duckdb_libpgquery::PGIntervalConstant>(node));
66 case duckdb_libpgquery::T_PGLambdaFunction:
67 return TransformLambda(node&: PGCast<duckdb_libpgquery::PGLambdaFunction>(node));
68 case duckdb_libpgquery::T_PGAIndirection:
69 return TransformArrayAccess(indirection_node&: PGCast<duckdb_libpgquery::PGAIndirection>(node));
70 case duckdb_libpgquery::T_PGPositionalReference:
71 return TransformPositionalReference(node&: PGCast<duckdb_libpgquery::PGPositionalReference>(node));
72 case duckdb_libpgquery::T_PGGroupingFunc:
73 return TransformGroupingFunction(n&: PGCast<duckdb_libpgquery::PGGroupingFunc>(node));
74 case duckdb_libpgquery::T_PGAStar:
75 return TransformStarExpression(star&: PGCast<duckdb_libpgquery::PGAStar>(node));
76 case duckdb_libpgquery::T_PGBooleanTest:
77 return TransformBooleanTest(node&: PGCast<duckdb_libpgquery::PGBooleanTest>(node));
78
79 default:
80 throw NotImplementedException("Expression type %s (%d)", NodetypeToString(type: node.type), (int)node.type);
81 }
82}
83
84unique_ptr<ParsedExpression> Transformer::TransformExpression(optional_ptr<duckdb_libpgquery::PGNode> node) {
85 if (!node) {
86 return nullptr;
87 }
88 return TransformExpression(node&: *node);
89}
90
91void Transformer::TransformExpressionList(duckdb_libpgquery::PGList &list,
92 vector<unique_ptr<ParsedExpression>> &result) {
93 for (auto node = list.head; node != nullptr; node = node->next) {
94 auto target = PGPointerCast<duckdb_libpgquery::PGNode>(ptr: node->data.ptr_value);
95
96 auto expr = TransformExpression(node&: *target);
97 result.push_back(x: std::move(expr));
98 }
99}
100
101} // namespace duckdb
102