2017-07-21 06:35:58 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Common/typeid_cast.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <DataTypes/IDataType.h>
|
|
|
|
#include <Columns/IColumn.h>
|
2019-11-04 05:29:54 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Columns/ColumnConst.h>
|
2017-07-24 01:00:31 +00:00
|
|
|
#include <Core/Block.h>
|
|
|
|
#include <Core/ColumnNumbers.h>
|
2018-09-03 10:46:58 +00:00
|
|
|
#include <Core/callOnTypeIndex.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
|
2018-09-02 01:12:32 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-04-09 19:19:30 +00:00
|
|
|
class IFunction;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
/// Methods, that helps dispatching over real column types.
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
const Type * checkAndGetDataType(const IDataType * data_type)
|
|
|
|
{
|
|
|
|
return typeid_cast<const Type *>(data_type);
|
|
|
|
}
|
|
|
|
|
2020-04-08 05:27:46 +00:00
|
|
|
template <typename... Types>
|
|
|
|
bool checkDataTypes(const IDataType * data_type)
|
|
|
|
{
|
|
|
|
return (... || typeid_cast<const Types *>(data_type));
|
|
|
|
}
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename Type>
|
|
|
|
const ColumnConst * checkAndGetColumnConst(const IColumn * column)
|
|
|
|
{
|
2019-06-27 19:28:52 +00:00
|
|
|
if (!column || !isColumnConst(*column))
|
2017-07-21 06:35:58 +00:00
|
|
|
return {};
|
|
|
|
|
2019-08-21 02:28:04 +00:00
|
|
|
const ColumnConst * res = assert_cast<const ColumnConst *>(column);
|
2017-07-21 06:35:58 +00:00
|
|
|
|
|
|
|
if (!checkColumn<Type>(&res->getDataColumn()))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
const Type * checkAndGetColumnConstData(const IColumn * column)
|
|
|
|
{
|
|
|
|
const ColumnConst * res = checkAndGetColumnConst<Type>(column);
|
|
|
|
|
|
|
|
if (!res)
|
2017-12-08 06:46:35 +00:00
|
|
|
return {};
|
2017-07-21 06:35:58 +00:00
|
|
|
|
2017-12-08 06:46:35 +00:00
|
|
|
return static_cast<const Type *>(&res->getDataColumn());
|
2017-07-21 06:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type>
|
2017-12-01 20:21:35 +00:00
|
|
|
bool checkColumnConst(const IColumn * column)
|
2017-07-21 06:35:58 +00:00
|
|
|
{
|
|
|
|
return checkAndGetColumnConst<Type>(column);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns non-nullptr if column is ColumnConst with ColumnString or ColumnFixedString inside.
|
|
|
|
const ColumnConst * checkAndGetColumnConstStringOrFixedString(const IColumn * column);
|
|
|
|
|
|
|
|
|
|
|
|
/// Transform anything to Field.
|
|
|
|
template <typename T>
|
2018-08-24 16:35:00 +00:00
|
|
|
inline std::enable_if_t<!IsDecimalNumber<T>, Field> toField(const T & x)
|
2017-07-21 06:35:58 +00:00
|
|
|
{
|
2018-11-20 20:09:20 +00:00
|
|
|
return Field(NearestFieldType<T>(x));
|
2017-07-21 06:35:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:35:00 +00:00
|
|
|
template <typename T>
|
|
|
|
inline std::enable_if_t<IsDecimalNumber<T>, Field> toField(const T & x, UInt32 scale)
|
|
|
|
{
|
2018-11-20 20:09:20 +00:00
|
|
|
return Field(NearestFieldType<T>(x, scale));
|
2018-08-24 16:35:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
|
2017-12-09 12:23:09 +00:00
|
|
|
Columns convertConstTupleToConstantElements(const ColumnConst & column);
|
2017-07-21 06:35:58 +00:00
|
|
|
|
2021-07-12 09:18:04 +00:00
|
|
|
/// Returns nested column with corrected type if nullable
|
|
|
|
ColumnWithTypeAndName columnGetNested(const ColumnWithTypeAndName & col);
|
2017-07-23 08:40:43 +00:00
|
|
|
|
2020-10-15 16:52:25 +00:00
|
|
|
/// Returns the copy of a given columns in which each column is replaced with its respective nested
|
2017-07-24 01:00:31 +00:00
|
|
|
/// column if it is nullable.
|
2020-10-15 16:52:25 +00:00
|
|
|
ColumnsWithTypeAndName createBlockWithNestedColumns(const ColumnsWithTypeAndName & columns);
|
2017-07-24 01:00:31 +00:00
|
|
|
|
2019-04-09 19:19:30 +00:00
|
|
|
/// 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);
|
|
|
|
|
2019-12-25 14:50:13 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
2019-12-23 14:54:06 +00:00
|
|
|
struct FunctionArgumentDescriptor
|
2019-10-14 10:15:10 +00:00
|
|
|
{
|
2019-12-23 14:54:06 +00:00
|
|
|
const char * argument_name;
|
|
|
|
|
|
|
|
bool (* type_validator_func)(const IDataType &);
|
|
|
|
bool (* column_validator_func)(const IColumn &);
|
|
|
|
|
2019-10-14 10:15:10 +00:00
|
|
|
const char * expected_type_description;
|
2019-12-23 14:54:06 +00:00
|
|
|
|
2019-12-25 14:50:13 +00:00
|
|
|
/** 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;
|
2019-10-14 10:15:10 +00:00
|
|
|
};
|
|
|
|
|
2019-12-23 14:54:06 +00:00
|
|
|
using FunctionArgumentDescriptors = std::vector<FunctionArgumentDescriptor>;
|
2019-10-14 10:15:10 +00:00
|
|
|
|
2019-12-11 08:56:32 +00:00
|
|
|
/** Validate that function arguments match specification.
|
2019-12-11 14:06:34 +00:00
|
|
|
*
|
2019-12-25 14:50:13 +00:00
|
|
|
* 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.
|
2019-12-11 14:06:34 +00:00
|
|
|
*
|
|
|
|
* 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)
|
2019-12-25 14:50:13 +00:00
|
|
|
* In other words you can't omit middle optional arguments (just like in regular C++).
|
2019-12-11 08:56:32 +00:00
|
|
|
*
|
|
|
|
* If any mandatory arg is missing, throw an exception, with explicit description of expected arguments.
|
|
|
|
*/
|
2019-12-23 14:54:06 +00:00
|
|
|
void validateFunctionArgumentTypes(const IFunction & func, const ColumnsWithTypeAndName & arguments,
|
|
|
|
const FunctionArgumentDescriptors & mandatory_args,
|
|
|
|
const FunctionArgumentDescriptors & optional_args = {});
|
2019-10-14 10:15:10 +00:00
|
|
|
|
2019-11-04 05:29:54 +00:00
|
|
|
/// Checks if a list of array columns have equal offsets. Return a pair of nested columns and offsets if true, otherwise throw.
|
|
|
|
std::pair<std::vector<const IColumn *>, const ColumnArray::Offset *>
|
|
|
|
checkAndGetNestedArrayOffset(const IColumn ** columns, size_t num_arguments);
|
|
|
|
|
2021-01-21 14:42:50 +00:00
|
|
|
|
|
|
|
/// Check if two types are equal
|
|
|
|
bool areTypesEqual(const DataTypePtr & lhs, const DataTypePtr & rhs);
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
/** Return ColumnNullable of src, with null map as OR-ed null maps of args columns.
|
|
|
|
* Or ColumnConst(ColumnNullable) if the result is always NULL or if the result is constant and always not NULL.
|
|
|
|
*/
|
|
|
|
ColumnPtr wrapInNullable(const ColumnPtr & src, const ColumnsWithTypeAndName & args, const DataTypePtr & result_type, size_t input_rows_count);
|
|
|
|
|
|
|
|
struct NullPresence
|
|
|
|
{
|
|
|
|
bool has_nullable = false;
|
|
|
|
bool has_null_constant = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
NullPresence getNullPresense(const ColumnsWithTypeAndName & args);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
}
|