//===----------------------------------------------------------------------===// // DuckDB // // duckdb/function/function.hpp // // //===----------------------------------------------------------------------===// #pragma once #include "duckdb/common/named_parameter_map.hpp" #include "duckdb/common/types/data_chunk.hpp" #include "duckdb/common/unordered_set.hpp" #include "duckdb/main/external_dependencies.hpp" #include "duckdb/parser/column_definition.hpp" namespace duckdb { class CatalogEntry; class Catalog; class ClientContext; class Expression; class ExpressionExecutor; class Transaction; class AggregateFunction; class AggregateFunctionSet; class CopyFunction; class PragmaFunction; class PragmaFunctionSet; class ScalarFunctionSet; class ScalarFunction; class TableFunctionSet; class TableFunction; class SimpleFunction; struct PragmaInfo; //! The default null handling is NULL in, NULL out enum class FunctionNullHandling : uint8_t { DEFAULT_NULL_HANDLING = 0, SPECIAL_HANDLING = 1 }; enum class FunctionSideEffects : uint8_t { NO_SIDE_EFFECTS = 0, HAS_SIDE_EFFECTS = 1 }; struct FunctionData { DUCKDB_API virtual ~FunctionData(); DUCKDB_API virtual unique_ptr Copy() const = 0; DUCKDB_API virtual bool Equals(const FunctionData &other) const = 0; DUCKDB_API static bool Equals(const FunctionData *left, const FunctionData *right); template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); return reinterpret_cast(*this); } // FIXME: this function should be removed in the future template TARGET &CastNoConst() const { return const_cast(reinterpret_cast(*this)); } }; struct TableFunctionData : public FunctionData { // used to pass on projections to table functions that support them. NB, can contain COLUMN_IDENTIFIER_ROW_ID vector column_ids; DUCKDB_API virtual ~TableFunctionData(); DUCKDB_API unique_ptr Copy() const override; DUCKDB_API bool Equals(const FunctionData &other) const override; }; struct PyTableFunctionData : public TableFunctionData { //! External dependencies of this table function unique_ptr external_dependency; }; struct FunctionParameters { vector values; named_parameter_map_t named_parameters; }; //! Function is the base class used for any type of function (scalar, aggregate or simple function) class Function { public: DUCKDB_API explicit Function(string name); DUCKDB_API virtual ~Function(); //! The name of the function string name; //! Additional Information to specify function from it's name string extra_info; public: //! Returns the formatted string name(arg1, arg2, ...) DUCKDB_API static string CallToString(const string &name, const vector &arguments); //! Returns the formatted string name(arg1, arg2..) -> return_type DUCKDB_API static string CallToString(const string &name, const vector &arguments, const LogicalType &return_type); //! Returns the formatted string name(arg1, arg2.., np1=a, np2=b, ...) DUCKDB_API static string CallToString(const string &name, const vector &arguments, const named_parameter_type_map_t &named_parameters); //! Used in the bind to erase an argument from a function DUCKDB_API static void EraseArgument(SimpleFunction &bound_function, vector> &arguments, idx_t argument_index); }; class SimpleFunction : public Function { public: DUCKDB_API SimpleFunction(string name, vector arguments, LogicalType varargs = LogicalType(LogicalTypeId::INVALID)); DUCKDB_API ~SimpleFunction() override; //! The set of arguments of the function vector arguments; //! The set of original arguments of the function - only set if Function::EraseArgument is called //! Used for (de)serialization purposes vector original_arguments; //! The type of varargs to support, or LogicalTypeId::INVALID if the function does not accept variable length //! arguments LogicalType varargs; public: DUCKDB_API virtual string ToString() const; DUCKDB_API bool HasVarArgs() const; }; class SimpleNamedParameterFunction : public SimpleFunction { public: DUCKDB_API SimpleNamedParameterFunction(string name, vector arguments, LogicalType varargs = LogicalType(LogicalTypeId::INVALID)); DUCKDB_API ~SimpleNamedParameterFunction() override; //! The named parameters of the function named_parameter_type_map_t named_parameters; public: DUCKDB_API string ToString() const override; DUCKDB_API bool HasNamedParameters() const; }; class BaseScalarFunction : public SimpleFunction { public: DUCKDB_API BaseScalarFunction(string name, vector arguments, LogicalType return_type, FunctionSideEffects side_effects, LogicalType varargs = LogicalType(LogicalTypeId::INVALID), FunctionNullHandling null_handling = FunctionNullHandling::DEFAULT_NULL_HANDLING); DUCKDB_API ~BaseScalarFunction() override; //! Return type of the function LogicalType return_type; //! Whether or not the function has side effects (e.g. sequence increments, random() functions, NOW()). Functions //! with side-effects cannot be constant-folded. FunctionSideEffects side_effects; //! How this function handles NULL values FunctionNullHandling null_handling; public: DUCKDB_API hash_t Hash() const; DUCKDB_API string ToString() const override; }; } // namespace duckdb