1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/function/macro_function.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/parser/query_node.hpp"
12#include "duckdb/function/function.hpp"
13#include "duckdb/main/client_context.hpp"
14#include "duckdb/planner/binder.hpp"
15#include "duckdb/planner/expression_binder.hpp"
16#include "duckdb/parser/expression/constant_expression.hpp"
17
18namespace duckdb {
19
20enum class MacroType : uint8_t { VOID_MACRO = 0, TABLE_MACRO = 1, SCALAR_MACRO = 2 };
21
22class MacroFunction {
23public:
24 explicit MacroFunction(MacroType type);
25
26 //! The type
27 MacroType type;
28 //! The positional parameters
29 vector<unique_ptr<ParsedExpression>> parameters;
30 //! The default parameters and their associated values
31 unordered_map<string, unique_ptr<ParsedExpression>> default_parameters;
32
33public:
34 virtual ~MacroFunction() {
35 }
36
37 void CopyProperties(MacroFunction &other) const;
38
39 virtual unique_ptr<MacroFunction> Copy() const = 0;
40
41 static string ValidateArguments(MacroFunction &macro_function, const string &name,
42 FunctionExpression &function_expr,
43 vector<unique_ptr<ParsedExpression>> &positionals,
44 unordered_map<string, unique_ptr<ParsedExpression>> &defaults);
45
46 virtual string ToSQL(const string &schema, const string &name) const;
47
48 void Serialize(Serializer &serializer) const;
49 static unique_ptr<MacroFunction> Deserialize(Deserializer &deserializer);
50
51protected:
52 virtual void SerializeInternal(FieldWriter &writer) const = 0;
53
54public:
55 template <class TARGET>
56 TARGET &Cast() {
57 if (type != TARGET::TYPE) {
58 throw InternalException("Failed to cast macro to type - macro type mismatch");
59 }
60 return reinterpret_cast<TARGET &>(*this);
61 }
62
63 template <class TARGET>
64 const TARGET &Cast() const {
65 if (type != TARGET::TYPE) {
66 throw InternalException("Failed to cast macro to type - macro type mismatch");
67 }
68 return reinterpret_cast<const TARGET &>(*this);
69 }
70};
71
72} // namespace duckdb
73