1#include "duckdb/common/limits.hpp"
2#include "duckdb/parser/expression/cast_expression.hpp"
3#include "duckdb/parser/expression/constant_expression.hpp"
4#include "duckdb/parser/transformer.hpp"
5#include "duckdb/common/operator/cast_operators.hpp"
6
7namespace duckdb {
8
9unique_ptr<ParsedExpression> Transformer::TransformTypeCast(duckdb_libpgquery::PGTypeCast &root) {
10 // get the type to cast to
11 auto type_name = root.typeName;
12 LogicalType target_type = TransformTypeName(name&: *type_name);
13
14 // check for a constant BLOB value, then return ConstantExpression with BLOB
15 if (!root.tryCast && target_type == LogicalType::BLOB && root.arg->type == duckdb_libpgquery::T_PGAConst) {
16 auto c = PGPointerCast<duckdb_libpgquery::PGAConst>(ptr: root.arg);
17 if (c->val.type == duckdb_libpgquery::T_PGString) {
18 return make_uniq<ConstantExpression>(args: Value::BLOB(data: string(c->val.val.str)));
19 }
20 }
21 // transform the expression node
22 auto expression = TransformExpression(node: root.arg);
23 bool try_cast = root.tryCast;
24
25 // now create a cast operation
26 return make_uniq<CastExpression>(args&: target_type, args: std::move(expression), args&: try_cast);
27}
28
29} // namespace duckdb
30