#pragma once #include #include #include #include #include #include #include #include #include namespace DB { class IFunction; /// Methods, that helps dispatching over real column types. template const Type * checkAndGetDataType(const IDataType * data_type) { return typeid_cast(data_type); } template bool checkDataTypes(const IDataType * data_type) { return (... || typeid_cast(data_type)); } template const ColumnConst * checkAndGetColumnConst(const IColumn * column) { if (!column || !isColumnConst(*column)) return {}; const ColumnConst * res = assert_cast(column); if (!checkColumn(&res->getDataColumn())) return {}; return res; } template const Type * checkAndGetColumnConstData(const IColumn * column) { const ColumnConst * res = checkAndGetColumnConst(column); if (!res) return {}; return static_cast(&res->getDataColumn()); } template bool checkColumnConst(const IColumn * column) { return checkAndGetColumnConst(column); } /// Returns non-nullptr if column is ColumnConst with ColumnString or ColumnFixedString inside. const ColumnConst * checkAndGetColumnConstStringOrFixedString(const IColumn * column); /// Transform anything to Field. template inline std::enable_if_t, Field> toField(const T & x) { return Field(NearestFieldType(x)); } template inline std::enable_if_t, Field> toField(const T & x, UInt32 scale) { return Field(NearestFieldType(x, scale)); } Columns convertConstTupleToConstantElements(const ColumnConst & column); /// Returns the copy of a given block in which each column specified in /// the "arguments" parameter is replaced with its respective nested /// column if it is nullable. Block createBlockWithNestedColumns(const Block & block, const ColumnNumbers & args); /// Similar function as above. Additionally transform the result type if needed. Block createBlockWithNestedColumns(const Block & block, const ColumnNumbers & args, size_t result); /// Checks argument type at specified index with predicate. /// throws if there is no argument at specified index or if predicate returns false. void validateArgumentType(const IFunction & func, const DataTypes & arguments, size_t argument_index, bool (* validator_func)(const IDataType &), const char * expected_type_description); /** Simple validator that is used in conjunction with validateFunctionArgumentTypes() to check if function arguments are as expected * * Also it is used to generate function description when arguments do not match expected ones. * Any field can be null: * `argument_name` - if not null, reported via type check errors. * `expected_type_description` - if not null, reported via type check errors. * `type_validator_func` - if not null, used to validate data type of function argument. * `column_validator_func` - if not null, used to validate column of function argument. */ struct FunctionArgumentDescriptor { const char * argument_name; bool (* type_validator_func)(const IDataType &); bool (* column_validator_func)(const IColumn &); const char * expected_type_description; /** Validate argument type and column. * * Returns non-zero error code if: * Validator != nullptr && (Value == nullptr || Validator(*Value) == false) * For: * Validator is either `type_validator_func` or `column_validator_func` * Value is either `data_type` or `column` respectively. * ILLEGAL_TYPE_OF_ARGUMENT if type validation fails * */ int isValid(const DataTypePtr & data_type, const ColumnPtr & column) const; }; using FunctionArgumentDescriptors = std::vector; /** Validate that function arguments match specification. * * Designed to simplify argument validation for functions with variable arguments * (e.g. depending on result type or other trait). * First, checks that number of arguments is as expected (including optional arguments). * Second, checks that mandatory args present and have valid type. * Third, checks optional arguents types, skipping ones that are missing. * * Please note that if you have several optional arguments, like f([a, b, c]), * only these calls are considered valid: * f(a) * f(a, b) * f(a, b, c) * * But NOT these: f(a, c), f(b, c) * In other words you can't omit middle optional arguments (just like in regular C++). * * If any mandatory arg is missing, throw an exception, with explicit description of expected arguments. */ void validateFunctionArgumentTypes(const IFunction & func, const ColumnsWithTypeAndName & arguments, const FunctionArgumentDescriptors & mandatory_args, const FunctionArgumentDescriptors & optional_args = {}); /// Checks if a list of array columns have equal offsets. Return a pair of nested columns and offsets if true, otherwise throw. std::pair, const ColumnArray::Offset *> checkAndGetNestedArrayOffset(const IColumn ** columns, size_t num_arguments); }