1#include "duckdb/parser/expression/case_expression.hpp"
2#include "duckdb/parser/expression/comparison_expression.hpp"
3#include "duckdb/parser/expression/constant_expression.hpp"
4#include "duckdb/parser/transformer.hpp"
5
6namespace duckdb {
7
8unique_ptr<ParsedExpression> Transformer::TransformCase(duckdb_libpgquery::PGCaseExpr &root) {
9 auto case_node = make_uniq<CaseExpression>();
10 auto root_arg = TransformExpression(node: PGPointerCast<duckdb_libpgquery::PGNode>(ptr: root.arg));
11 for (auto cell = root.args->head; cell != nullptr; cell = cell->next) {
12 CaseCheck case_check;
13
14 auto w = PGPointerCast<duckdb_libpgquery::PGCaseWhen>(ptr: cell->data.ptr_value);
15 auto test_raw = TransformExpression(node: PGPointerCast<duckdb_libpgquery::PGNode>(ptr: w->expr));
16 unique_ptr<ParsedExpression> test;
17 if (root_arg) {
18 case_check.when_expr =
19 make_uniq<ComparisonExpression>(args: ExpressionType::COMPARE_EQUAL, args: root_arg->Copy(), args: std::move(test_raw));
20 } else {
21 case_check.when_expr = std::move(test_raw);
22 }
23 case_check.then_expr = TransformExpression(node: PGPointerCast<duckdb_libpgquery::PGNode>(ptr: w->result));
24 case_node->case_checks.push_back(x: std::move(case_check));
25 }
26
27 if (root.defresult) {
28 case_node->else_expr = TransformExpression(node: PGPointerCast<duckdb_libpgquery::PGNode>(ptr: root.defresult));
29 } else {
30 case_node->else_expr = make_uniq<ConstantExpression>(args: Value(LogicalType::SQLNULL));
31 }
32 return std::move(case_node);
33}
34
35} // namespace duckdb
36