1#include "duckdb/parser/expression/operator_expression.hpp"
2#include "duckdb/parser/transformer.hpp"
3
4namespace duckdb {
5
6// COALESCE(a,b,c) returns the first argument that is NOT NULL, so
7// rewrite into CASE(a IS NOT NULL, a, CASE(b IS NOT NULL, b, c))
8unique_ptr<ParsedExpression> Transformer::TransformCoalesce(duckdb_libpgquery::PGAExpr &root) {
9 auto coalesce_args = PGPointerCast<duckdb_libpgquery::PGList>(ptr: root.lexpr);
10 D_ASSERT(coalesce_args->length > 0); // parser ensures this already
11
12 auto coalesce_op = make_uniq<OperatorExpression>(args: ExpressionType::OPERATOR_COALESCE);
13 for (auto cell = coalesce_args->head; cell; cell = cell->next) {
14 // get the value of the COALESCE
15 auto value_expr = TransformExpression(node: PGPointerCast<duckdb_libpgquery::PGNode>(ptr: cell->data.ptr_value));
16 coalesce_op->children.push_back(x: std::move(value_expr));
17 }
18 return std::move(coalesce_op);
19}
20
21} // namespace duckdb
22