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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
|
2017-07-24 01:00:31 +00:00
|
|
|
/// 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.
|
2018-01-19 02:15:10 +00:00
|
|
|
Block createBlockWithNestedColumns(const Block & block, const ColumnNumbers & args);
|
2017-07-24 01:00:31 +00:00
|
|
|
|
|
|
|
/// Similar function as above. Additionally transform the result type if needed.
|
2018-01-19 02:15:10 +00:00
|
|
|
Block createBlockWithNestedColumns(const Block & block, const ColumnNumbers & args, size_t result);
|
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-11 14:06:34 +00:00
|
|
|
// Simple validator that is used in conjunction with validateFunctionArgumentTypes() to check if function arguments are as expected.
|
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
|
|
|
|
|
|
|
bool 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
|
|
|
*
|
|
|
|
* Designed to simplify argument validation
|
|
|
|
* for functions with variable arguments (e.g. depending on result type or other trait).
|
|
|
|
* first, checks that mandatory args present and have valid type.
|
|
|
|
* second, 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 skip
|
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);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
}
|