|
| 1 | +#include "core_functions/scalar/generic_functions.hpp" |
| 2 | +#include "duckdb/planner/expression/bound_constant_expression.hpp" |
| 3 | +#include "duckdb/planner/expression/bound_function_expression.hpp" |
| 4 | +#include "duckdb/parser/expression/constant_expression.hpp" |
| 5 | +#include "duckdb/planner/binder.hpp" |
| 6 | +#include "duckdb/parser/expression/type_expression.hpp" |
| 7 | +#include "duckdb/execution/expression_executor.hpp" |
| 8 | + |
| 9 | +namespace duckdb { |
| 10 | + |
| 11 | +//---------------------------------------------------------------------------------------------------------------------- |
| 12 | +// typeof function |
| 13 | +//---------------------------------------------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +static void TypeOfFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
| 16 | + Value v(args.data[0].GetType().ToString()); |
| 17 | + result.Reference(v); |
| 18 | +} |
| 19 | + |
| 20 | +static unique_ptr<Expression> BindTypeOfFunctionExpression(FunctionBindExpressionInput &input) { |
| 21 | + auto &return_type = input.children[0]->return_type; |
| 22 | + if (return_type.id() == LogicalTypeId::UNKNOWN || return_type.id() == LogicalTypeId::SQLNULL) { |
| 23 | + // parameter - unknown return type |
| 24 | + return nullptr; |
| 25 | + } |
| 26 | + // emit a constant expression |
| 27 | + return make_uniq<BoundConstantExpression>(Value(return_type.ToString())); |
| 28 | +} |
| 29 | + |
| 30 | +ScalarFunction TypeOfFun::GetFunction() { |
| 31 | + auto fun = ScalarFunction({LogicalType::ANY}, LogicalType::VARCHAR, TypeOfFunction); |
| 32 | + fun.SetNullHandling(FunctionNullHandling::SPECIAL_HANDLING); |
| 33 | + fun.SetBindExpressionCallback(BindTypeOfFunctionExpression); |
| 34 | + return fun; |
| 35 | +} |
| 36 | + |
| 37 | +//---------------------------------------------------------------------------------------------------------------------- |
| 38 | +// get_type function |
| 39 | +//---------------------------------------------------------------------------------------------------------------------- |
| 40 | +// This is like "typeof", except returns LogicalType::TYPE instead of VARCHAR |
| 41 | + |
| 42 | +static void GetTypeFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
| 43 | + auto v = Value::TYPE(args.data[0].GetType()); |
| 44 | + result.Reference(v); |
| 45 | +} |
| 46 | + |
| 47 | +static unique_ptr<Expression> BindGetTypeFunctionExpression(FunctionBindExpressionInput &input) { |
| 48 | + auto &return_type = input.children[0]->return_type; |
| 49 | + if (return_type.id() == LogicalTypeId::UNKNOWN || return_type.id() == LogicalTypeId::SQLNULL) { |
| 50 | + // parameter - unknown return type |
| 51 | + return nullptr; |
| 52 | + } |
| 53 | + // emit a constant expression |
| 54 | + return make_uniq<BoundConstantExpression>(Value::TYPE(return_type)); |
| 55 | +} |
| 56 | + |
| 57 | +ScalarFunction GetTypeFun::GetFunction() { |
| 58 | + auto fun = ScalarFunction({LogicalType::ANY}, LogicalType::VARCHAR, GetTypeFunction); |
| 59 | + fun.SetNullHandling(FunctionNullHandling::SPECIAL_HANDLING); |
| 60 | + fun.SetBindExpressionCallback(BindGetTypeFunctionExpression); |
| 61 | + return fun; |
| 62 | +} |
| 63 | + |
| 64 | +//---------------------------------------------------------------------------------------------------------------------- |
| 65 | +// make_type function |
| 66 | +//---------------------------------------------------------------------------------------------------------------------- |
| 67 | +static void MakeTypeFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
| 68 | + throw InvalidInputException("make_type function can only be used in constant expressions"); |
| 69 | +} |
| 70 | + |
| 71 | +static unique_ptr<Expression> BindMakeTypeFunctionExpression(FunctionBindExpressionInput &input) { |
| 72 | + vector<pair<string, Value>> args; |
| 73 | + |
| 74 | + // Evaluate all arguments to constant values |
| 75 | + for (auto &child : input.children) { |
| 76 | + string name = child->alias; |
| 77 | + if (!child->IsFoldable()) { |
| 78 | + throw BinderException("make_type function arguments must be constant expressions"); |
| 79 | + } |
| 80 | + auto val = ExpressionExecutor::EvaluateScalar(input.context, *child); |
| 81 | + args.emplace_back(name, val); |
| 82 | + } |
| 83 | + |
| 84 | + if (args.empty()) { |
| 85 | + throw BinderException("make_type function requires at least one argument"); |
| 86 | + } |
| 87 | + |
| 88 | + if (args.front().second.type() != LogicalType::VARCHAR) { |
| 89 | + throw BinderException("make_type function first argument must be the type name as VARCHAR"); |
| 90 | + } |
| 91 | + |
| 92 | + vector<unique_ptr<ParsedExpression>> type_args; |
| 93 | + for (idx_t i = 1; i < args.size(); i++) { |
| 94 | + auto &arg = args[i]; |
| 95 | + auto result = make_uniq<ConstantExpression>(arg.second); |
| 96 | + result->SetAlias(arg.first); |
| 97 | + |
| 98 | + type_args.push_back(std::move(result)); |
| 99 | + } |
| 100 | + |
| 101 | + auto type_name = args.front().second.GetValue<string>(); |
| 102 | + auto qualified_name = QualifiedName::Parse(type_name); |
| 103 | + |
| 104 | + auto unbound_type = LogicalType::UNBOUND(make_uniq<TypeExpression>(qualified_name.catalog, qualified_name.schema, |
| 105 | + qualified_name.name, std::move(type_args))); |
| 106 | + |
| 107 | + // Bind the unbound type |
| 108 | + auto binder = Binder::CreateBinder(input.context); |
| 109 | + binder->BindLogicalType(unbound_type); |
| 110 | + return make_uniq<BoundConstantExpression>(Value::TYPE(unbound_type)); |
| 111 | +} |
| 112 | + |
| 113 | +ScalarFunction MakeTypeFun::GetFunction() { |
| 114 | + auto fun = ScalarFunction({LogicalType::VARCHAR}, LogicalType::TYPE(), MakeTypeFunction); |
| 115 | + fun.SetNullHandling(FunctionNullHandling::SPECIAL_HANDLING); |
| 116 | + fun.SetBindExpressionCallback(BindMakeTypeFunctionExpression); |
| 117 | + fun.varargs = LogicalType::ANY; |
| 118 | + return fun; |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace duckdb |
0 commit comments