1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/function/cast/cast_function_set.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/function/cast/default_casts.hpp"
12
13namespace duckdb {
14struct MapCastInfo;
15struct MapCastNode;
16
17typedef BoundCastInfo (*bind_cast_function_t)(BindCastInput &input, const LogicalType &source,
18 const LogicalType &target);
19typedef int64_t (*implicit_cast_cost_t)(const LogicalType &from, const LogicalType &to);
20
21struct GetCastFunctionInput {
22 GetCastFunctionInput(optional_ptr<ClientContext> context = nullptr) : context(context) {
23 }
24 GetCastFunctionInput(ClientContext &context) : context(&context) {
25 }
26
27 optional_ptr<ClientContext> context;
28};
29
30struct BindCastFunction {
31 BindCastFunction(bind_cast_function_t function,
32 unique_ptr<BindCastInfo> info = nullptr); // NOLINT: allow implicit cast
33
34 bind_cast_function_t function;
35 unique_ptr<BindCastInfo> info;
36};
37
38class CastFunctionSet {
39public:
40 CastFunctionSet();
41
42public:
43 DUCKDB_API static CastFunctionSet &Get(ClientContext &context);
44 DUCKDB_API static CastFunctionSet &Get(DatabaseInstance &db);
45
46 //! Returns a cast function (from source -> target)
47 //! Note that this always returns a function - since a cast is ALWAYS possible if the value is NULL
48 DUCKDB_API BoundCastInfo GetCastFunction(const LogicalType &source, const LogicalType &target,
49 GetCastFunctionInput &input);
50 //! Returns the implicit cast cost of casting from source -> target
51 //! -1 means an implicit cast is not possible
52 DUCKDB_API int64_t ImplicitCastCost(const LogicalType &source, const LogicalType &target);
53 //! Register a new cast function from source to target
54 DUCKDB_API void RegisterCastFunction(const LogicalType &source, const LogicalType &target, BoundCastInfo function,
55 int64_t implicit_cast_cost = -1);
56 DUCKDB_API void RegisterCastFunction(const LogicalType &source, const LogicalType &target,
57 bind_cast_function_t bind, int64_t implicit_cast_cost = -1);
58
59private:
60 vector<BindCastFunction> bind_functions;
61 //! If any custom cast functions have been defined using RegisterCastFunction, this holds the map
62 optional_ptr<MapCastInfo> map_info;
63
64private:
65 void RegisterCastFunction(const LogicalType &source, const LogicalType &target, MapCastNode node);
66};
67
68} // namespace duckdb
69