2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2018-10-10 18:37:54 +00:00
|
|
|
#include <DataTypes/DataTypesDecimal.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2017-12-07 01:57:26 +00:00
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
2019-02-10 17:40:52 +00:00
|
|
|
#include <DataTypes/NumberTraits.h>
|
|
|
|
#include <DataTypes/getLeastSupertype.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnVector.h>
|
2018-10-10 18:37:54 +00:00
|
|
|
#include <Columns/ColumnDecimal.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
2021-08-13 08:18:34 +00:00
|
|
|
#include <Columns/MaskOperations.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2018-01-10 15:45:05 +00:00
|
|
|
#include <Functions/GatherUtils/Algorithms.h>
|
2018-12-02 02:47:47 +00:00
|
|
|
#include <Functions/FunctionIfBase.h>
|
2019-02-03 08:41:25 +00:00
|
|
|
#include <Interpreters/castColumn.h>
|
2021-08-13 08:18:34 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2017-08-04 23:18:51 +00:00
|
|
|
|
2012-09-24 02:12:59 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2018-10-10 18:37:54 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2018-10-10 18:37:54 +00:00
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2018-01-10 15:45:05 +00:00
|
|
|
using namespace GatherUtils;
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/** Selection function by condition: if(cond, then, else).
|
2012-09-24 04:11:55 +00:00
|
|
|
* cond - UInt8
|
2017-05-27 15:45:25 +00:00
|
|
|
* then, else - numeric types for which there is a general type, or dates, datetimes, or strings, or arrays of these types.
|
2012-09-24 02:12:59 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-07 10:55:55 +00:00
|
|
|
template <typename ArrayCond, typename ArrayA, typename ArrayB, typename ArrayResult, typename ResultType>
|
2022-04-15 23:15:40 +00:00
|
|
|
inline void fillVectorVector(const ArrayCond & cond, const ArrayA & a, const ArrayB & b, ArrayResult & res)
|
2021-06-07 10:55:55 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
|
|
|
bool a_is_short = a.size() < size;
|
|
|
|
bool b_is_short = b.size() < size;
|
|
|
|
|
2021-08-10 11:31:15 +00:00
|
|
|
if (a_is_short && b_is_short)
|
2021-06-07 10:55:55 +00:00
|
|
|
{
|
2021-08-10 11:31:15 +00:00
|
|
|
size_t a_index = 0, b_index = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
res[i] = cond[i] ? static_cast<ResultType>(a[a_index++]) : static_cast<ResultType>(b[b_index++]);
|
|
|
|
}
|
|
|
|
else if (a_is_short)
|
|
|
|
{
|
|
|
|
size_t a_index = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
res[i] = cond[i] ? static_cast<ResultType>(a[a_index++]) : static_cast<ResultType>(b[i]);
|
|
|
|
}
|
|
|
|
else if (b_is_short)
|
|
|
|
{
|
|
|
|
size_t b_index = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
res[i] = cond[i] ? static_cast<ResultType>(a[i]) : static_cast<ResultType>(b[b_index++]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
res[i] = cond[i] ? static_cast<ResultType>(a[i]) : static_cast<ResultType>(b[i]);
|
2021-06-07 10:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ArrayCond, typename ArrayA, typename B, typename ArrayResult, typename ResultType>
|
2022-04-15 23:15:40 +00:00
|
|
|
inline void fillVectorConstant(const ArrayCond & cond, const ArrayA & a, B b, ArrayResult & res)
|
2021-06-07 10:55:55 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
|
|
|
bool a_is_short = a.size() < size;
|
2021-08-10 11:31:15 +00:00
|
|
|
if (a_is_short)
|
|
|
|
{
|
|
|
|
size_t a_index = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
res[i] = cond[i] ? static_cast<ResultType>(a[a_index++]) : static_cast<ResultType>(b);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
res[i] = cond[i] ? static_cast<ResultType>(a[i]) : static_cast<ResultType>(b);
|
|
|
|
}
|
2021-06-07 10:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ArrayCond, typename A, typename ArrayB, typename ArrayResult, typename ResultType>
|
2022-04-15 23:15:40 +00:00
|
|
|
inline void fillConstantVector(const ArrayCond & cond, A a, const ArrayB & b, ArrayResult & res)
|
2021-06-07 10:55:55 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
|
|
|
bool b_is_short = b.size() < size;
|
2021-08-10 11:31:15 +00:00
|
|
|
if (b_is_short)
|
|
|
|
{
|
|
|
|
size_t b_index = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
res[i] = cond[i] ? static_cast<ResultType>(a) : static_cast<ResultType>(b[b_index++]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
res[i] = cond[i] ? static_cast<ResultType>(a) : static_cast<ResultType>(b[i]);
|
|
|
|
}
|
2021-06-07 10:55:55 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 12:30:43 +00:00
|
|
|
template <typename A, typename B, typename ResultType>
|
2012-09-24 02:12:59 +00:00
|
|
|
struct NumIfImpl
|
|
|
|
{
|
2018-10-10 18:37:54 +00:00
|
|
|
using ArrayCond = PaddedPODArray<UInt8>;
|
2020-08-19 11:52:17 +00:00
|
|
|
using ArrayA = typename ColumnVector<A>::Container;
|
|
|
|
using ArrayB = typename ColumnVector<B>::Container;
|
2018-10-10 18:37:54 +00:00
|
|
|
using ColVecResult = ColumnVector<ResultType>;
|
2021-06-07 10:55:55 +00:00
|
|
|
using ArrayResult = typename ColVecResult::Container;
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
static ColumnPtr vectorVector(const ArrayCond & cond, const ArrayA & a, const ArrayB & b, UInt32)
|
2012-09-24 02:12:59 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
2018-10-10 18:37:54 +00:00
|
|
|
auto col_res = ColVecResult::create(size);
|
2021-06-07 10:55:55 +00:00
|
|
|
ArrayResult & res = col_res->getData();
|
|
|
|
fillVectorVector<ArrayCond, ArrayA, ArrayB, ArrayResult, ResultType>(cond, a, b, res);
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res;
|
2012-09-24 02:12:59 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
static ColumnPtr vectorConstant(const ArrayCond & cond, const ArrayA & a, B b, UInt32)
|
2012-09-24 02:12:59 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
2018-10-10 18:37:54 +00:00
|
|
|
auto col_res = ColVecResult::create(size);
|
2021-06-07 10:55:55 +00:00
|
|
|
ArrayResult & res = col_res->getData();
|
|
|
|
fillVectorConstant<ArrayCond, ArrayA, B, ArrayResult, ResultType>(cond, a, b, res);
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res;
|
2012-09-24 02:12:59 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
static ColumnPtr constantVector(const ArrayCond & cond, A a, const ArrayB & b, UInt32)
|
2012-09-24 02:12:59 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
2018-10-10 18:37:54 +00:00
|
|
|
auto col_res = ColVecResult::create(size);
|
2021-06-07 10:55:55 +00:00
|
|
|
ArrayResult & res = col_res->getData();
|
|
|
|
fillConstantVector<ArrayCond, A, ArrayB, ArrayResult, ResultType>(cond, a, b, res);
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res;
|
2012-09-24 02:12:59 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
static ColumnPtr constantConstant(const ArrayCond & cond, A a, B b, UInt32)
|
2012-09-24 02:12:59 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
2018-10-10 18:37:54 +00:00
|
|
|
auto col_res = ColVecResult::create(size);
|
2021-06-07 10:55:55 +00:00
|
|
|
ArrayResult & res = col_res->getData();
|
2018-10-10 18:37:54 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2021-01-26 19:04:03 +00:00
|
|
|
res[i] = cond[i] ? static_cast<ResultType>(a) : static_cast<ResultType>(b);
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res;
|
2018-10-10 18:37:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename A, typename B, typename R>
|
|
|
|
struct NumIfImpl<Decimal<A>, Decimal<B>, Decimal<R>>
|
|
|
|
{
|
|
|
|
using ResultType = Decimal<R>;
|
|
|
|
using ArrayCond = PaddedPODArray<UInt8>;
|
2020-08-19 11:52:17 +00:00
|
|
|
using ArrayA = typename ColumnDecimal<Decimal<A>>::Container;
|
|
|
|
using ArrayB = typename ColumnDecimal<Decimal<B>>::Container;
|
2018-10-10 18:37:54 +00:00
|
|
|
using ColVecResult = ColumnDecimal<ResultType>;
|
2020-10-11 19:20:20 +00:00
|
|
|
using Block = ColumnsWithTypeAndName;
|
2021-06-07 10:55:55 +00:00
|
|
|
using ArrayResult = typename ColVecResult::Container;
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
static ColumnPtr vectorVector(const ArrayCond & cond, const ArrayA & a, const ArrayB & b, UInt32 scale)
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
|
|
|
auto col_res = ColVecResult::create(size, scale);
|
2021-06-07 10:55:55 +00:00
|
|
|
ArrayResult & res = col_res->getData();
|
|
|
|
fillVectorVector<ArrayCond, ArrayA, ArrayB, ArrayResult, ResultType>(cond, a, b, res);
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res;
|
2018-10-10 18:37:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
static ColumnPtr vectorConstant(const ArrayCond & cond, const ArrayA & a, B b, UInt32 scale)
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
|
|
|
auto col_res = ColVecResult::create(size, scale);
|
2021-06-07 10:55:55 +00:00
|
|
|
ArrayResult & res = col_res->getData();
|
|
|
|
fillVectorConstant<ArrayCond, ArrayA, B, ArrayResult, ResultType>(cond, a, b, res);
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res;
|
2018-10-10 18:37:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
static ColumnPtr constantVector(const ArrayCond & cond, A a, const ArrayB & b, UInt32 scale)
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
|
|
|
auto col_res = ColVecResult::create(size, scale);
|
2021-06-07 10:55:55 +00:00
|
|
|
ArrayResult & res = col_res->getData();
|
|
|
|
fillConstantVector<ArrayCond, A, ArrayB, ArrayResult, ResultType>(cond, a, b, res);
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res;
|
2018-10-10 18:37:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
static ColumnPtr constantConstant(const ArrayCond & cond, A a, B b, UInt32 scale)
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
|
|
|
size_t size = cond.size();
|
|
|
|
auto col_res = ColVecResult::create(size, scale);
|
2021-06-07 10:55:55 +00:00
|
|
|
ArrayResult & res = col_res->getData();
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2012-09-24 02:12:59 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
2013-10-08 12:30:43 +00:00
|
|
|
res[i] = cond[i] ? static_cast<ResultType>(a) : static_cast<ResultType>(b);
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res;
|
2013-10-08 12:30:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-24 04:11:55 +00:00
|
|
|
|
2021-05-07 08:43:06 +00:00
|
|
|
class FunctionIf : public FunctionIfBase
|
2013-10-08 12:30:43 +00:00
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
public:
|
|
|
|
static constexpr auto name = "if";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionIf>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
2013-10-08 12:30:43 +00:00
|
|
|
private:
|
|
|
|
template <typename T0, typename T1>
|
2020-11-17 13:24:45 +00:00
|
|
|
static UInt32 decimalScale(const ColumnsWithTypeAndName & arguments [[maybe_unused]])
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
2021-09-10 11:49:22 +00:00
|
|
|
if constexpr (is_decimal<T0> && is_decimal<T1>)
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
UInt32 left_scale = getDecimalScale(*arguments[1].type);
|
|
|
|
UInt32 right_scale = getDecimalScale(*arguments[2].type);
|
2018-10-10 18:37:54 +00:00
|
|
|
if (left_scale != right_scale)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Conditional functions with different Decimal scales");
|
2018-10-10 18:37:54 +00:00
|
|
|
return left_scale;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return std::numeric_limits<UInt32>::max();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T0, typename T1, typename ColVecT0, typename ColVecT1>
|
2020-10-19 13:42:14 +00:00
|
|
|
ColumnPtr executeRightType(
|
2021-06-15 03:54:24 +00:00
|
|
|
[[maybe_unused]] const ColumnUInt8 * cond_col,
|
|
|
|
[[maybe_unused]] const ColumnsWithTypeAndName & arguments,
|
|
|
|
[[maybe_unused]] const ColVecT0 * col_left) const
|
2013-10-08 12:30:43 +00:00
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfIf<T0, T1>::Type;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-06-15 03:54:24 +00:00
|
|
|
if constexpr (std::is_same_v<ResultType, NumberTraits::Error>)
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
2021-06-15 03:54:24 +00:00
|
|
|
return nullptr;
|
2018-10-10 18:37:54 +00:00
|
|
|
}
|
2021-06-15 03:54:24 +00:00
|
|
|
else
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
2021-06-15 03:54:24 +00:00
|
|
|
const IColumn * col_right_untyped = arguments[2].column.get();
|
|
|
|
UInt32 scale = decimalScale<T0, T1>(arguments);
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2021-06-15 03:54:24 +00:00
|
|
|
if (const auto * col_right_vec = checkAndGetColumn<ColVecT1>(col_right_untyped))
|
|
|
|
{
|
|
|
|
return NumIfImpl<T0, T1, ResultType>::vectorVector(
|
|
|
|
cond_col->getData(), col_left->getData(), col_right_vec->getData(), scale);
|
|
|
|
}
|
|
|
|
else if (const auto * col_right_const = checkAndGetColumnConst<ColVecT1>(col_right_untyped))
|
|
|
|
{
|
|
|
|
return NumIfImpl<T0, T1, ResultType>::vectorConstant(
|
|
|
|
cond_col->getData(), col_left->getData(), col_right_const->template getValue<T1>(), scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-10-08 12:30:43 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-10-10 18:37:54 +00:00
|
|
|
template <typename T0, typename T1, typename ColVecT0, typename ColVecT1>
|
2020-10-19 13:42:14 +00:00
|
|
|
ColumnPtr executeConstRightType(
|
2021-06-15 03:54:24 +00:00
|
|
|
[[maybe_unused]] const ColumnUInt8 * cond_col,
|
|
|
|
[[maybe_unused]] const ColumnsWithTypeAndName & arguments,
|
|
|
|
[[maybe_unused]] const ColumnConst * col_left) const
|
2013-10-08 12:30:43 +00:00
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfIf<T0, T1>::Type;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-06-15 03:54:24 +00:00
|
|
|
if constexpr (std::is_same_v<ResultType, NumberTraits::Error>)
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
2021-06-15 03:54:24 +00:00
|
|
|
return nullptr;
|
2018-10-10 18:37:54 +00:00
|
|
|
}
|
2021-06-15 03:54:24 +00:00
|
|
|
else
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
2021-06-15 03:54:24 +00:00
|
|
|
const IColumn * col_right_untyped = arguments[2].column.get();
|
|
|
|
UInt32 scale = decimalScale<T0, T1>(arguments);
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2021-06-15 03:54:24 +00:00
|
|
|
if (const auto * col_right_vec = checkAndGetColumn<ColVecT1>(col_right_untyped))
|
|
|
|
{
|
|
|
|
return NumIfImpl<T0, T1, ResultType>::constantVector(
|
|
|
|
cond_col->getData(), col_left->template getValue<T0>(), col_right_vec->getData(), scale);
|
|
|
|
}
|
|
|
|
else if (const auto * col_right_const = checkAndGetColumnConst<ColVecT1>(col_right_untyped))
|
|
|
|
{
|
|
|
|
return NumIfImpl<T0, T1, ResultType>::constantConstant(
|
|
|
|
cond_col->getData(), col_left->template getValue<T0>(), col_right_const->template getValue<T1>(), scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-24 02:12:59 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-10-10 18:37:54 +00:00
|
|
|
template <typename T0, typename T1, typename ColVecT0, typename ColVecT1>
|
2020-10-19 13:42:14 +00:00
|
|
|
ColumnPtr executeRightTypeArray(
|
2017-12-02 03:34:45 +00:00
|
|
|
[[maybe_unused]] const ColumnUInt8 * cond_col,
|
2020-11-17 13:24:45 +00:00
|
|
|
[[maybe_unused]] const ColumnsWithTypeAndName & arguments,
|
2020-10-19 13:42:14 +00:00
|
|
|
[[maybe_unused]] const DataTypePtr result_type,
|
2018-04-24 07:16:39 +00:00
|
|
|
[[maybe_unused]] const ColumnArray * col_left_array,
|
2020-07-21 13:58:07 +00:00
|
|
|
[[maybe_unused]] size_t input_rows_count) const
|
2015-06-16 21:42:18 +00:00
|
|
|
{
|
2021-06-15 03:54:24 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfIf<T0, T1>::Type;
|
|
|
|
|
|
|
|
if constexpr (std::is_same_v<ResultType, NumberTraits::Error>)
|
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2021-06-15 03:54:24 +00:00
|
|
|
}
|
2020-07-21 21:15:19 +00:00
|
|
|
else
|
2017-12-02 02:47:12 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const IColumn * col_right_untyped = arguments[2].column.get();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-04-22 08:31:10 +00:00
|
|
|
if (const auto * col_right_array = checkAndGetColumn<ColumnArray>(col_right_untyped))
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
|
|
|
const ColVecT1 * col_right_vec = checkAndGetColumn<ColVecT1>(&col_right_array->getData());
|
2017-12-02 02:47:12 +00:00
|
|
|
if (!col_right_vec)
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
auto res = result_type->createColumn();
|
2020-09-16 12:36:54 +00:00
|
|
|
auto & arr_res = assert_cast<ColumnArray &>(*res);
|
2017-08-05 03:15:47 +00:00
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
conditional(
|
|
|
|
NumericArraySource<T0>(*col_left_array),
|
|
|
|
NumericArraySource<T1>(*col_right_array),
|
2020-09-16 12:36:54 +00:00
|
|
|
NumericArraySink<ResultType>(arr_res.getData(), arr_res.getOffsets(), input_rows_count),
|
2017-12-02 02:47:12 +00:00
|
|
|
cond_col->getData());
|
2017-12-16 04:29:34 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return res;
|
2017-12-02 02:47:12 +00:00
|
|
|
}
|
2020-04-22 08:31:10 +00:00
|
|
|
else if (const auto * col_right_const_array = checkAndGetColumnConst<ColumnArray>(col_right_untyped))
|
2017-12-02 02:47:12 +00:00
|
|
|
{
|
|
|
|
const ColumnArray * col_right_const_array_data = checkAndGetColumn<ColumnArray>(&col_right_const_array->getDataColumn());
|
2018-10-10 18:37:54 +00:00
|
|
|
if (!checkColumn<ColVecT1>(&col_right_const_array_data->getData()))
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
auto res = result_type->createColumn();
|
2020-09-16 12:36:54 +00:00
|
|
|
auto & arr_res = assert_cast<ColumnArray &>(*res);
|
2017-08-05 03:15:47 +00:00
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
conditional(
|
|
|
|
NumericArraySource<T0>(*col_left_array),
|
|
|
|
ConstSource<NumericArraySource<T1>>(*col_right_const_array),
|
2020-09-16 12:36:54 +00:00
|
|
|
NumericArraySink<ResultType>(arr_res.getData(), arr_res.getOffsets(), input_rows_count),
|
2017-12-02 02:47:12 +00:00
|
|
|
cond_col->getData());
|
2017-12-16 04:29:34 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return res;
|
2017-12-02 02:47:12 +00:00
|
|
|
}
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2021-06-15 03:54:24 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-16 21:42:18 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-10-10 18:37:54 +00:00
|
|
|
template <typename T0, typename T1, typename ColVecT0, typename ColVecT1>
|
2020-10-19 13:42:14 +00:00
|
|
|
ColumnPtr executeConstRightTypeArray(
|
2017-12-02 03:36:52 +00:00
|
|
|
[[maybe_unused]] const ColumnUInt8 * cond_col,
|
2020-11-17 13:24:45 +00:00
|
|
|
[[maybe_unused]] const ColumnsWithTypeAndName & arguments,
|
2020-10-19 13:42:14 +00:00
|
|
|
[[maybe_unused]] const DataTypePtr & result_type,
|
2018-04-24 07:16:39 +00:00
|
|
|
[[maybe_unused]] const ColumnConst * col_left_const_array,
|
2020-07-21 13:58:07 +00:00
|
|
|
[[maybe_unused]] size_t input_rows_count) const
|
2015-06-16 21:42:18 +00:00
|
|
|
{
|
2021-06-15 03:54:24 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfIf<T0, T1>::Type;
|
|
|
|
|
|
|
|
if constexpr (std::is_same_v<ResultType, NumberTraits::Error>)
|
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2021-06-15 03:54:24 +00:00
|
|
|
}
|
2020-07-21 21:15:19 +00:00
|
|
|
else
|
2015-06-16 21:42:18 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const IColumn * col_right_untyped = arguments[2].column.get();
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2020-04-22 08:31:10 +00:00
|
|
|
if (const auto * col_right_array = checkAndGetColumn<ColumnArray>(col_right_untyped))
|
2017-12-02 02:47:12 +00:00
|
|
|
{
|
2018-10-10 18:37:54 +00:00
|
|
|
const ColVecT1 * col_right_vec = checkAndGetColumn<ColVecT1>(&col_right_array->getData());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
if (!col_right_vec)
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-08-05 03:15:47 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
auto res = result_type->createColumn();
|
2020-09-16 12:36:54 +00:00
|
|
|
auto & arr_res = assert_cast<ColumnArray &>(*res);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
conditional(
|
|
|
|
ConstSource<NumericArraySource<T0>>(*col_left_const_array),
|
|
|
|
NumericArraySource<T1>(*col_right_array),
|
2020-09-16 12:36:54 +00:00
|
|
|
NumericArraySink<ResultType>(arr_res.getData(), arr_res.getOffsets(), input_rows_count),
|
2017-12-02 02:47:12 +00:00
|
|
|
cond_col->getData());
|
2017-12-16 04:29:34 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return res;
|
2017-12-02 02:47:12 +00:00
|
|
|
}
|
2020-04-22 08:31:10 +00:00
|
|
|
else if (const auto * col_right_const_array = checkAndGetColumnConst<ColumnArray>(col_right_untyped))
|
2017-12-02 02:47:12 +00:00
|
|
|
{
|
|
|
|
const ColumnArray * col_right_const_array_data = checkAndGetColumn<ColumnArray>(&col_right_const_array->getDataColumn());
|
2018-10-10 18:37:54 +00:00
|
|
|
if (!checkColumn<ColVecT1>(&col_right_const_array_data->getData()))
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
auto res = result_type->createColumn();
|
2020-09-16 12:36:54 +00:00
|
|
|
auto & arr_res = assert_cast<ColumnArray &>(*res);
|
2017-08-05 03:15:47 +00:00
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
conditional(
|
|
|
|
ConstSource<NumericArraySource<T0>>(*col_left_const_array),
|
|
|
|
ConstSource<NumericArraySource<T1>>(*col_right_const_array),
|
2020-09-16 12:36:54 +00:00
|
|
|
NumericArraySink<ResultType>(arr_res.getData(), arr_res.getOffsets(), input_rows_count),
|
2017-12-02 02:47:12 +00:00
|
|
|
cond_col->getData());
|
2017-12-16 04:29:34 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return res;
|
2017-12-02 02:47:12 +00:00
|
|
|
}
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2021-06-15 03:54:24 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-05 03:15:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 18:37:54 +00:00
|
|
|
template <typename T0, typename T1>
|
2021-06-15 03:54:24 +00:00
|
|
|
ColumnPtr executeTyped(
|
|
|
|
const ColumnUInt8 * cond_col, const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const
|
2013-10-08 12:30:43 +00:00
|
|
|
{
|
2021-09-10 11:49:22 +00:00
|
|
|
using ColVecT0 = ColumnVectorOrDecimal<T0>;
|
|
|
|
using ColVecT1 = ColumnVectorOrDecimal<T1>;
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
const IColumn * col_left_untyped = arguments[1].column.get();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
ColumnPtr right_column = nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-04-22 08:31:10 +00:00
|
|
|
if (const auto * col_left = checkAndGetColumn<ColVecT0>(col_left_untyped))
|
2015-06-16 21:42:18 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
right_column = executeRightType<T0, T1, ColVecT0, ColVecT1>(cond_col, arguments, col_left);
|
2015-06-16 21:42:18 +00:00
|
|
|
}
|
2020-04-22 08:31:10 +00:00
|
|
|
else if (const auto * col_const_left = checkAndGetColumnConst<ColVecT0>(col_left_untyped))
|
2013-10-08 12:30:43 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
right_column = executeConstRightType<T0, T1, ColVecT0, ColVecT1>(cond_col, arguments, col_const_left);
|
2015-06-16 21:42:18 +00:00
|
|
|
}
|
2020-04-22 08:31:10 +00:00
|
|
|
else if (const auto * col_arr_left = checkAndGetColumn<ColumnArray>(col_left_untyped))
|
2015-06-16 21:42:18 +00:00
|
|
|
{
|
2018-10-10 18:37:54 +00:00
|
|
|
if (auto col_arr_left_elems = checkAndGetColumn<ColVecT0>(&col_arr_left->getData()))
|
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
right_column = executeRightTypeArray<T0, T1, ColVecT0, ColVecT1>(
|
|
|
|
cond_col, arguments, result_type, col_arr_left, input_rows_count);
|
2018-10-10 18:37:54 +00:00
|
|
|
}
|
2015-06-16 21:42:18 +00:00
|
|
|
}
|
2020-04-22 08:31:10 +00:00
|
|
|
else if (const auto * col_const_arr_left = checkAndGetColumnConst<ColumnArray>(col_left_untyped))
|
2015-06-16 21:42:18 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
if (checkColumn<ColVecT0>(&assert_cast<const ColumnArray &>(col_const_arr_left->getDataColumn()).getData()))
|
2018-10-10 18:37:54 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
right_column = executeConstRightTypeArray<T0, T1, ColVecT0, ColVecT1>(
|
|
|
|
cond_col, arguments, result_type, col_const_arr_left, input_rows_count);
|
2018-10-10 18:37:54 +00:00
|
|
|
}
|
2013-10-08 12:30:43 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return right_column;
|
2013-10-08 12:30:43 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
static ColumnPtr executeString(const ColumnUInt8 * cond_col, const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type)
|
2012-09-24 04:11:55 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const IColumn * col_then_untyped = arguments[1].column.get();
|
|
|
|
const IColumn * col_else_untyped = arguments[2].column.get();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnString * col_then = checkAndGetColumn<ColumnString>(col_then_untyped);
|
|
|
|
const ColumnString * col_else = checkAndGetColumn<ColumnString>(col_else_untyped);
|
|
|
|
const ColumnFixedString * col_then_fixed = checkAndGetColumn<ColumnFixedString>(col_then_untyped);
|
|
|
|
const ColumnFixedString * col_else_fixed = checkAndGetColumn<ColumnFixedString>(col_else_untyped);
|
2017-08-04 23:18:51 +00:00
|
|
|
const ColumnConst * col_then_const = checkAndGetColumnConst<ColumnString>(col_then_untyped);
|
|
|
|
const ColumnConst * col_else_const = checkAndGetColumnConst<ColumnString>(col_else_untyped);
|
|
|
|
const ColumnConst * col_then_const_fixed = checkAndGetColumnConst<ColumnFixedString>(col_then_untyped);
|
|
|
|
const ColumnConst * col_else_const_fixed = checkAndGetColumnConst<ColumnFixedString>(col_else_untyped);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-04 23:18:51 +00:00
|
|
|
const PaddedPODArray<UInt8> & cond_data = cond_col->getData();
|
|
|
|
size_t rows = cond_data.size();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
if (isFixedString(result_type))
|
2017-08-04 23:18:51 +00:00
|
|
|
{
|
|
|
|
/// The result is FixedString.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
auto col_res_untyped = result_type->createColumn();
|
2019-08-21 02:28:04 +00:00
|
|
|
ColumnFixedString * col_res = assert_cast<ColumnFixedString *>(col_res_untyped.get());
|
2017-08-04 23:18:51 +00:00
|
|
|
auto sink = FixedStringSink(*col_res, rows);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-04 23:18:51 +00:00
|
|
|
if (col_then_fixed && col_else_fixed)
|
|
|
|
conditional(FixedStringSource(*col_then_fixed), FixedStringSource(*col_else_fixed), sink, cond_data);
|
|
|
|
else if (col_then_fixed && col_else_const_fixed)
|
|
|
|
conditional(FixedStringSource(*col_then_fixed), ConstSource<FixedStringSource>(*col_else_const_fixed), sink, cond_data);
|
|
|
|
else if (col_then_const_fixed && col_else_fixed)
|
|
|
|
conditional(ConstSource<FixedStringSource>(*col_then_const_fixed), FixedStringSource(*col_else_fixed), sink, cond_data);
|
|
|
|
else if (col_then_const_fixed && col_else_const_fixed)
|
2020-06-28 21:10:34 +00:00
|
|
|
conditional(ConstSource<FixedStringSource>(*col_then_const_fixed),
|
|
|
|
ConstSource<FixedStringSource>(*col_else_const_fixed), sink, cond_data);
|
|
|
|
else
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res_untyped;
|
2017-08-04 23:18:51 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
if (isString(result_type))
|
2017-08-04 23:18:51 +00:00
|
|
|
{
|
|
|
|
/// The result is String.
|
2020-06-28 21:10:34 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
auto col_res = ColumnString::create();
|
2017-08-04 23:18:51 +00:00
|
|
|
auto sink = StringSink(*col_res, rows);
|
|
|
|
|
|
|
|
if (col_then && col_else)
|
|
|
|
conditional(StringSource(*col_then), StringSource(*col_else), sink, cond_data);
|
|
|
|
else if (col_then && col_else_const)
|
|
|
|
conditional(StringSource(*col_then), ConstSource<StringSource>(*col_else_const), sink, cond_data);
|
|
|
|
else if (col_then_const && col_else)
|
|
|
|
conditional(ConstSource<StringSource>(*col_then_const), StringSource(*col_else), sink, cond_data);
|
|
|
|
else if (col_then_const && col_else_const)
|
|
|
|
conditional(ConstSource<StringSource>(*col_then_const), ConstSource<StringSource>(*col_else_const), sink, cond_data);
|
|
|
|
else if (col_then && col_else_fixed)
|
|
|
|
conditional(StringSource(*col_then), FixedStringSource(*col_else_fixed), sink, cond_data);
|
|
|
|
else if (col_then_fixed && col_else)
|
|
|
|
conditional(FixedStringSource(*col_then_fixed), StringSource(*col_else), sink, cond_data);
|
|
|
|
else if (col_then_const && col_else_fixed)
|
|
|
|
conditional(ConstSource<StringSource>(*col_then_const), FixedStringSource(*col_else_fixed), sink, cond_data);
|
|
|
|
else if (col_then_fixed && col_else_const)
|
|
|
|
conditional(FixedStringSource(*col_then_fixed), ConstSource<StringSource>(*col_else_const), sink, cond_data);
|
|
|
|
else if (col_then && col_else_const_fixed)
|
|
|
|
conditional(StringSource(*col_then), ConstSource<FixedStringSource>(*col_else_const_fixed), sink, cond_data);
|
|
|
|
else if (col_then_const_fixed && col_else)
|
|
|
|
conditional(ConstSource<FixedStringSource>(*col_then_const_fixed), StringSource(*col_else), sink, cond_data);
|
|
|
|
else if (col_then_const && col_else_const_fixed)
|
|
|
|
conditional(ConstSource<StringSource>(*col_then_const), ConstSource<FixedStringSource>(*col_else_const_fixed), sink, cond_data);
|
|
|
|
else if (col_then_const_fixed && col_else_const)
|
|
|
|
conditional(ConstSource<FixedStringSource>(*col_then_const_fixed), ConstSource<StringSource>(*col_else_const), sink, cond_data);
|
2020-06-29 22:49:23 +00:00
|
|
|
else if (col_then_fixed && col_else_fixed)
|
2020-06-28 21:10:34 +00:00
|
|
|
conditional(FixedStringSource(*col_then_fixed), FixedStringSource(*col_else_fixed), sink, cond_data);
|
|
|
|
else if (col_then_fixed && col_else_const_fixed)
|
|
|
|
conditional(FixedStringSource(*col_then_fixed), ConstSource<FixedStringSource>(*col_else_const_fixed), sink, cond_data);
|
|
|
|
else if (col_then_const_fixed && col_else_fixed)
|
|
|
|
conditional(ConstSource<FixedStringSource>(*col_then_const_fixed), FixedStringSource(*col_else_fixed), sink, cond_data);
|
|
|
|
else if (col_then_const_fixed && col_else_const_fixed)
|
|
|
|
conditional(ConstSource<FixedStringSource>(*col_then_const_fixed),
|
|
|
|
ConstSource<FixedStringSource>(*col_else_const_fixed), sink, cond_data);
|
|
|
|
else
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return col_res;
|
2015-06-17 21:10:19 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-08-05 03:50:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
static ColumnPtr executeGenericArray(const ColumnUInt8 * cond_col, const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type)
|
2017-08-05 03:50:13 +00:00
|
|
|
{
|
2017-08-05 04:12:15 +00:00
|
|
|
/// For generic implementation, arrays must be of same type.
|
2020-10-19 13:42:14 +00:00
|
|
|
if (!arguments[1].type->equals(*arguments[2].type))
|
|
|
|
return nullptr;
|
2017-08-05 04:12:15 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
const IColumn * col_then_untyped = arguments[1].column.get();
|
|
|
|
const IColumn * col_else_untyped = arguments[2].column.get();
|
2017-08-05 03:50:13 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnArray * col_arr_then = checkAndGetColumn<ColumnArray>(col_then_untyped);
|
|
|
|
const ColumnArray * col_arr_else = checkAndGetColumn<ColumnArray>(col_else_untyped);
|
|
|
|
const ColumnConst * col_arr_then_const = checkAndGetColumnConst<ColumnArray>(col_then_untyped);
|
|
|
|
const ColumnConst * col_arr_else_const = checkAndGetColumnConst<ColumnArray>(col_else_untyped);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-05 03:50:13 +00:00
|
|
|
const PaddedPODArray<UInt8> & cond_data = cond_col->getData();
|
|
|
|
size_t rows = cond_data.size();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-05 03:50:13 +00:00
|
|
|
if ((col_arr_then || col_arr_then_const)
|
|
|
|
&& (col_arr_else || col_arr_else_const))
|
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
auto res = result_type->createColumn();
|
2020-04-22 08:31:10 +00:00
|
|
|
auto * col_res = assert_cast<ColumnArray *>(res.get());
|
2017-08-05 03:50:13 +00:00
|
|
|
|
|
|
|
if (col_arr_then && col_arr_else)
|
2020-09-16 12:40:18 +00:00
|
|
|
conditional(GenericArraySource(*col_arr_then), GenericArraySource(*col_arr_else), GenericArraySink(col_res->getData(), col_res->getOffsets(), rows), cond_data);
|
2017-08-05 03:50:13 +00:00
|
|
|
else if (col_arr_then && col_arr_else_const)
|
2020-09-16 12:40:18 +00:00
|
|
|
conditional(GenericArraySource(*col_arr_then), ConstSource<GenericArraySource>(*col_arr_else_const), GenericArraySink(col_res->getData(), col_res->getOffsets(), rows), cond_data);
|
2017-08-05 03:50:13 +00:00
|
|
|
else if (col_arr_then_const && col_arr_else)
|
2020-09-16 12:40:18 +00:00
|
|
|
conditional(ConstSource<GenericArraySource>(*col_arr_then_const), GenericArraySource(*col_arr_else), GenericArraySink(col_res->getData(), col_res->getOffsets(), rows), cond_data);
|
2015-06-17 21:10:19 +00:00
|
|
|
else if (col_arr_then_const && col_arr_else_const)
|
2020-09-16 12:40:18 +00:00
|
|
|
conditional(ConstSource<GenericArraySource>(*col_arr_then_const), ConstSource<GenericArraySource>(*col_arr_else_const), GenericArraySink(col_res->getData(), col_res->getOffsets(), rows), cond_data);
|
2015-06-17 21:10:19 +00:00
|
|
|
else
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return res;
|
2015-06-17 21:10:19 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2012-09-24 04:11:55 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-02-05 19:39:26 +00:00
|
|
|
ColumnPtr executeTuple(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const
|
2016-07-09 03:54:57 +00:00
|
|
|
{
|
|
|
|
/// Calculate function for each corresponding elements of tuples.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
const ColumnWithTypeAndName & arg1 = arguments[1];
|
|
|
|
const ColumnWithTypeAndName & arg2 = arguments[2];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-09 12:23:09 +00:00
|
|
|
Columns col1_contents;
|
|
|
|
Columns col2_contents;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-09 12:23:09 +00:00
|
|
|
if (const ColumnTuple * tuple1 = typeid_cast<const ColumnTuple *>(arg1.column.get()))
|
2019-03-25 01:43:54 +00:00
|
|
|
col1_contents = tuple1->getColumnsCopy();
|
2017-07-21 06:35:58 +00:00
|
|
|
else if (const ColumnConst * const_tuple = checkAndGetColumnConst<ColumnTuple>(arg1.column.get()))
|
2017-12-09 12:23:09 +00:00
|
|
|
col1_contents = convertConstTupleToConstantElements(*const_tuple);
|
2016-07-10 07:24:24 +00:00
|
|
|
else
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-09 12:23:09 +00:00
|
|
|
if (const ColumnTuple * tuple2 = typeid_cast<const ColumnTuple *>(arg2.column.get()))
|
2019-03-25 01:43:54 +00:00
|
|
|
col2_contents = tuple2->getColumnsCopy();
|
2017-07-21 06:35:58 +00:00
|
|
|
else if (const ColumnConst * const_tuple = checkAndGetColumnConst<ColumnTuple>(arg2.column.get()))
|
2017-12-09 12:23:09 +00:00
|
|
|
col2_contents = convertConstTupleToConstantElements(*const_tuple);
|
2016-07-10 07:24:24 +00:00
|
|
|
else
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-09 03:54:57 +00:00
|
|
|
const DataTypeTuple & type1 = static_cast<const DataTypeTuple &>(*arg1.type);
|
|
|
|
const DataTypeTuple & type2 = static_cast<const DataTypeTuple &>(*arg2.type);
|
2021-02-05 19:39:26 +00:00
|
|
|
const DataTypeTuple & tuple_result = static_cast<const DataTypeTuple &>(*result_type);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
ColumnsWithTypeAndName temporary_columns(3);
|
|
|
|
temporary_columns[0] = arguments[0];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-09 03:54:57 +00:00
|
|
|
size_t tuple_size = type1.getElements().size();
|
2017-12-08 00:50:25 +00:00
|
|
|
Columns tuple_columns(tuple_size);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-09 03:54:57 +00:00
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
temporary_columns[1] = {col1_contents[i], type1.getElements()[i], {}};
|
|
|
|
temporary_columns[2] = {col2_contents[i], type2.getElements()[i], {}};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-02-05 19:39:26 +00:00
|
|
|
tuple_columns[i] = executeImpl(temporary_columns, tuple_result.getElements()[i], input_rows_count);
|
2016-07-09 03:54:57 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return ColumnTuple::create(tuple_columns);
|
2016-07-09 03:54:57 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
static ColumnPtr executeGeneric(
|
2020-11-17 13:24:45 +00:00
|
|
|
const ColumnUInt8 * cond_col, const ColumnsWithTypeAndName & arguments, size_t input_rows_count)
|
2019-02-03 08:41:25 +00:00
|
|
|
{
|
|
|
|
/// Convert both columns to the common type (if needed).
|
2020-10-19 13:42:14 +00:00
|
|
|
const ColumnWithTypeAndName & arg1 = arguments[1];
|
|
|
|
const ColumnWithTypeAndName & arg2 = arguments[2];
|
2019-02-03 08:41:25 +00:00
|
|
|
|
2021-06-12 15:10:25 +00:00
|
|
|
DataTypePtr common_type = getLeastSupertype(DataTypes{arg1.type, arg2.type});
|
2019-02-03 08:41:25 +00:00
|
|
|
|
2020-04-14 21:05:45 +00:00
|
|
|
ColumnPtr col_then = castColumn(arg1, common_type);
|
|
|
|
ColumnPtr col_else = castColumn(arg2, common_type);
|
2019-02-03 08:41:25 +00:00
|
|
|
|
|
|
|
MutableColumnPtr result_column = common_type->createColumn();
|
|
|
|
result_column->reserve(input_rows_count);
|
|
|
|
|
2019-06-27 19:28:52 +00:00
|
|
|
bool then_is_const = isColumnConst(*col_then);
|
|
|
|
bool else_is_const = isColumnConst(*col_else);
|
2019-02-03 08:41:25 +00:00
|
|
|
|
2021-06-07 10:55:55 +00:00
|
|
|
bool then_is_short = col_then->size() < cond_col->size();
|
|
|
|
bool else_is_short = col_else->size() < cond_col->size();
|
|
|
|
|
2019-02-03 08:41:25 +00:00
|
|
|
const auto & cond_array = cond_col->getData();
|
|
|
|
|
|
|
|
if (then_is_const && else_is_const)
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const IColumn & then_nested_column = assert_cast<const ColumnConst &>(*col_then).getDataColumn();
|
|
|
|
const IColumn & else_nested_column = assert_cast<const ColumnConst &>(*col_else).getDataColumn();
|
2019-02-03 08:41:25 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
|
|
|
if (cond_array[i])
|
|
|
|
result_column->insertFrom(then_nested_column, 0);
|
|
|
|
else
|
|
|
|
result_column->insertFrom(else_nested_column, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (then_is_const)
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const IColumn & then_nested_column = assert_cast<const ColumnConst &>(*col_then).getDataColumn();
|
2019-02-03 08:41:25 +00:00
|
|
|
|
2021-06-07 10:55:55 +00:00
|
|
|
size_t else_index = 0;
|
2019-02-03 08:41:25 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
|
|
|
if (cond_array[i])
|
|
|
|
result_column->insertFrom(then_nested_column, 0);
|
|
|
|
else
|
2021-06-07 10:55:55 +00:00
|
|
|
result_column->insertFrom(*col_else, else_is_short ? else_index++ : i);
|
2019-02-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (else_is_const)
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const IColumn & else_nested_column = assert_cast<const ColumnConst &>(*col_else).getDataColumn();
|
2019-02-03 08:41:25 +00:00
|
|
|
|
2021-06-07 10:55:55 +00:00
|
|
|
size_t then_index = 0;
|
2019-02-03 08:41:25 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
|
|
|
if (cond_array[i])
|
2021-06-07 10:55:55 +00:00
|
|
|
result_column->insertFrom(*col_then, then_is_short ? then_index++ : i);
|
2019-02-03 08:41:25 +00:00
|
|
|
else
|
|
|
|
result_column->insertFrom(else_nested_column, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-07 10:55:55 +00:00
|
|
|
size_t then_index = 0, else_index = 0;
|
2019-02-03 08:41:25 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2021-06-07 10:55:55 +00:00
|
|
|
{
|
|
|
|
if (cond_array[i])
|
|
|
|
result_column->insertFrom(*col_then, then_is_short ? then_index++ : i);
|
|
|
|
else
|
|
|
|
result_column->insertFrom(*col_else, else_is_short ? else_index++ : i);
|
|
|
|
}
|
2019-02-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return result_column;
|
2019-02-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 03:54:24 +00:00
|
|
|
ColumnPtr executeForConstAndNullableCondition(
|
|
|
|
const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/) const
|
2017-03-06 06:46:45 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const ColumnWithTypeAndName & arg_cond = arguments[0];
|
2017-12-09 10:14:45 +00:00
|
|
|
bool cond_is_null = arg_cond.column->onlyNull();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-23 21:06:32 +00:00
|
|
|
ColumnPtr not_const_condition = arg_cond.column;
|
|
|
|
bool cond_is_const = false;
|
|
|
|
bool cond_is_true = false;
|
|
|
|
bool cond_is_false = false;
|
|
|
|
if (const auto * const_arg = checkAndGetColumn<ColumnConst>(*arg_cond.column))
|
2017-03-06 06:46:45 +00:00
|
|
|
{
|
2020-06-23 21:06:32 +00:00
|
|
|
cond_is_const = true;
|
|
|
|
not_const_condition = const_arg->getDataColumnPtr();
|
|
|
|
ColumnPtr data_column = const_arg->getDataColumnPtr();
|
|
|
|
if (const auto * const_nullable_arg = checkAndGetColumn<ColumnNullable>(*data_column))
|
|
|
|
{
|
|
|
|
data_column = const_nullable_arg->getNestedColumnPtr();
|
|
|
|
if (!data_column->empty())
|
|
|
|
cond_is_null = const_nullable_arg->getNullMapData()[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data_column->empty())
|
|
|
|
{
|
|
|
|
cond_is_true = !cond_is_null && checkAndGetColumn<ColumnUInt8>(*data_column)->getBool(0);
|
|
|
|
cond_is_false = !cond_is_null && !cond_is_true;
|
|
|
|
}
|
2017-03-06 06:46:45 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
const auto & column1 = arguments[1];
|
|
|
|
const auto & column2 = arguments[2];
|
2020-06-23 21:06:32 +00:00
|
|
|
|
|
|
|
if (cond_is_true)
|
2020-10-19 13:42:14 +00:00
|
|
|
return castColumn(column1, result_type);
|
2020-06-23 21:06:32 +00:00
|
|
|
else if (cond_is_false || cond_is_null)
|
2020-10-19 13:42:14 +00:00
|
|
|
return castColumn(column2, result_type);
|
2020-06-23 21:06:32 +00:00
|
|
|
|
|
|
|
if (const auto * nullable = checkAndGetColumn<ColumnNullable>(*not_const_condition))
|
|
|
|
{
|
|
|
|
ColumnPtr new_cond_column = nullable->getNestedColumnPtr();
|
|
|
|
size_t column_size = arg_cond.column->size();
|
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
if (checkAndGetColumn<ColumnUInt8>(*new_cond_column))
|
2020-06-23 21:06:32 +00:00
|
|
|
{
|
|
|
|
auto nested_column_copy = new_cond_column->cloneResized(new_cond_column->size());
|
|
|
|
typeid_cast<ColumnUInt8 *>(nested_column_copy.get())->applyZeroMap(nullable->getNullMapData());
|
|
|
|
new_cond_column = std::move(nested_column_copy);
|
|
|
|
|
|
|
|
if (cond_is_const)
|
|
|
|
new_cond_column = ColumnConst::create(new_cond_column, column_size);
|
|
|
|
}
|
|
|
|
else
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of {} condition", arg_cond.column->getName(), getName());
|
2020-06-23 21:06:32 +00:00
|
|
|
|
2020-10-14 14:04:50 +00:00
|
|
|
ColumnsWithTypeAndName temporary_columns
|
2017-03-06 06:46:45 +00:00
|
|
|
{
|
2020-06-23 21:06:32 +00:00
|
|
|
{ new_cond_column, removeNullable(arg_cond.type), arg_cond.name },
|
|
|
|
column1,
|
|
|
|
column2,
|
2017-03-06 06:46:45 +00:00
|
|
|
};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return executeImpl(temporary_columns, result_type, new_cond_column->size());
|
2017-03-06 06:46:45 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-03-06 06:46:45 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-06-07 10:55:55 +00:00
|
|
|
template <typename AnyColumnPtr>
|
|
|
|
static ColumnPtr materializeColumnIfConst(const AnyColumnPtr & column)
|
2017-03-06 21:33:10 +00:00
|
|
|
{
|
2018-12-21 16:00:07 +00:00
|
|
|
return column->convertToFullColumnIfConst();
|
2017-03-06 21:33:10 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 04:29:34 +00:00
|
|
|
static ColumnPtr makeNullableColumnIfNot(const ColumnPtr & column)
|
2017-03-06 06:46:45 +00:00
|
|
|
{
|
2020-06-18 10:18:28 +00:00
|
|
|
auto materialized = materializeColumnIfConst(column);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-18 10:18:28 +00:00
|
|
|
if (isColumnNullable(*materialized))
|
|
|
|
return materialized;
|
|
|
|
|
|
|
|
return ColumnNullable::create(materialized, ColumnUInt8::create(column->size(), 0));
|
2017-03-06 06:46:45 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-09-11 09:23:31 +00:00
|
|
|
/// Return nested column recursively removing Nullable, examples:
|
|
|
|
/// Nullable(size = 1, Int32(size = 1), UInt8(size = 1)) -> Int32(size = 1)
|
|
|
|
/// Const(size = 0, Nullable(size = 1, Int32(size = 1), UInt8(size = 1))) ->
|
|
|
|
/// Const(size = 0, Int32(size = 1))
|
|
|
|
static ColumnPtr recursiveGetNestedColumnWithoutNullable(const ColumnPtr & column)
|
2017-03-06 06:46:45 +00:00
|
|
|
{
|
2020-04-22 08:31:10 +00:00
|
|
|
if (const auto * nullable = checkAndGetColumn<ColumnNullable>(*column))
|
2020-09-11 09:23:31 +00:00
|
|
|
{
|
|
|
|
/// Nullable cannot contain Nullable
|
2019-06-26 17:20:33 +00:00
|
|
|
return nullable->getNestedColumnPtr();
|
2020-09-11 09:23:31 +00:00
|
|
|
}
|
|
|
|
else if (const auto * column_const = checkAndGetColumn<ColumnConst>(*column))
|
|
|
|
{
|
|
|
|
/// Save Constant, but remove Nullable
|
|
|
|
return ColumnConst::create(recursiveGetNestedColumnWithoutNullable(column_const->getDataColumnPtr()), column->size());
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-06 06:46:45 +00:00
|
|
|
return column;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeForNullableThenElse(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const
|
2017-03-06 06:46:45 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const ColumnWithTypeAndName & arg_cond = arguments[0];
|
|
|
|
const ColumnWithTypeAndName & arg_then = arguments[1];
|
|
|
|
const ColumnWithTypeAndName & arg_else = arguments[2];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-04-22 08:31:10 +00:00
|
|
|
const auto * then_is_nullable = checkAndGetColumn<ColumnNullable>(*arg_then.column);
|
|
|
|
const auto * else_is_nullable = checkAndGetColumn<ColumnNullable>(*arg_else.column);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-06 06:46:45 +00:00
|
|
|
if (!then_is_nullable && !else_is_nullable)
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-06 06:46:45 +00:00
|
|
|
/** Calculate null mask of result and nested column separately.
|
|
|
|
*/
|
|
|
|
ColumnPtr result_null_mask;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-06 06:46:45 +00:00
|
|
|
{
|
2020-10-14 14:04:50 +00:00
|
|
|
ColumnsWithTypeAndName temporary_columns(
|
2017-03-06 06:46:45 +00:00
|
|
|
{
|
|
|
|
arg_cond,
|
|
|
|
{
|
|
|
|
then_is_nullable
|
2019-06-26 17:20:33 +00:00
|
|
|
? then_is_nullable->getNullMapColumnPtr()
|
2018-04-24 07:16:39 +00:00
|
|
|
: DataTypeUInt8().createColumnConstWithDefaultValue(input_rows_count),
|
2017-03-06 06:46:45 +00:00
|
|
|
std::make_shared<DataTypeUInt8>(),
|
|
|
|
""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
else_is_nullable
|
2019-06-26 17:20:33 +00:00
|
|
|
? else_is_nullable->getNullMapColumnPtr()
|
2018-04-24 07:16:39 +00:00
|
|
|
: DataTypeUInt8().createColumnConstWithDefaultValue(input_rows_count),
|
2017-03-06 06:46:45 +00:00
|
|
|
std::make_shared<DataTypeUInt8>(),
|
|
|
|
""
|
|
|
|
}
|
|
|
|
});
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
result_null_mask = executeImpl(temporary_columns, std::make_shared<DataTypeUInt8>(), input_rows_count);
|
2017-03-06 06:46:45 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-06 06:46:45 +00:00
|
|
|
ColumnPtr result_nested_column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-06 06:46:45 +00:00
|
|
|
{
|
2020-10-14 14:04:50 +00:00
|
|
|
ColumnsWithTypeAndName temporary_columns(
|
2017-03-06 06:46:45 +00:00
|
|
|
{
|
|
|
|
arg_cond,
|
|
|
|
{
|
2020-09-11 09:23:31 +00:00
|
|
|
recursiveGetNestedColumnWithoutNullable(arg_then.column),
|
2017-12-09 07:32:32 +00:00
|
|
|
removeNullable(arg_then.type),
|
2017-03-06 06:46:45 +00:00
|
|
|
""
|
|
|
|
},
|
|
|
|
{
|
2020-09-11 09:23:31 +00:00
|
|
|
recursiveGetNestedColumnWithoutNullable(arg_else.column),
|
2017-12-09 07:32:32 +00:00
|
|
|
removeNullable(arg_else.type),
|
2017-03-06 06:46:45 +00:00
|
|
|
""
|
|
|
|
}
|
|
|
|
});
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
result_nested_column = executeImpl(temporary_columns, removeNullable(result_type), temporary_columns.front().column->size());
|
2017-03-06 06:46:45 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return ColumnNullable::create(
|
2017-03-07 20:11:39 +00:00
|
|
|
materializeColumnIfConst(result_nested_column), materializeColumnIfConst(result_null_mask));
|
2017-03-06 06:46:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeForNullThenElse(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const
|
2017-12-08 02:00:11 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const ColumnWithTypeAndName & arg_cond = arguments[0];
|
|
|
|
const ColumnWithTypeAndName & arg_then = arguments[1];
|
|
|
|
const ColumnWithTypeAndName & arg_else = arguments[2];
|
2017-12-08 02:00:11 +00:00
|
|
|
|
2017-12-09 10:14:45 +00:00
|
|
|
bool then_is_null = arg_then.column->onlyNull();
|
|
|
|
bool else_is_null = arg_else.column->onlyNull();
|
2017-12-08 02:00:11 +00:00
|
|
|
|
|
|
|
if (!then_is_null && !else_is_null)
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-12-08 02:00:11 +00:00
|
|
|
|
|
|
|
if (then_is_null && else_is_null)
|
2020-10-19 13:42:14 +00:00
|
|
|
return result_type->createColumnConstWithDefaultValue(input_rows_count);
|
2017-12-08 02:00:11 +00:00
|
|
|
|
2021-06-07 10:55:55 +00:00
|
|
|
bool then_is_short = arg_then.column->size() < arg_cond.column->size();
|
|
|
|
bool else_is_short = arg_else.column->size() < arg_cond.column->size();
|
|
|
|
|
2017-12-08 02:00:11 +00:00
|
|
|
const ColumnUInt8 * cond_col = typeid_cast<const ColumnUInt8 *>(arg_cond.column.get());
|
|
|
|
const ColumnConst * cond_const_col = checkAndGetColumnConst<ColumnVector<UInt8>>(arg_cond.column.get());
|
|
|
|
|
|
|
|
/// If then is NULL, we create Nullable column with null mask OR-ed with condition.
|
|
|
|
if (then_is_null)
|
|
|
|
{
|
2022-03-21 14:27:25 +00:00
|
|
|
ColumnPtr arg_else_column;
|
|
|
|
/// In case when arg_else column type differs with result
|
|
|
|
/// column type we should cast it to result type.
|
|
|
|
if (removeNullable(arg_else.type)->getName() != removeNullable(result_type)->getName())
|
|
|
|
arg_else_column = castColumn(arg_else, result_type);
|
|
|
|
else
|
|
|
|
arg_else_column = arg_else.column;
|
|
|
|
|
2017-12-08 02:00:11 +00:00
|
|
|
if (cond_col)
|
|
|
|
{
|
2022-11-11 14:49:49 +00:00
|
|
|
arg_else_column = arg_else_column->convertToFullColumnIfConst();
|
2021-06-07 10:55:55 +00:00
|
|
|
auto result_column = IColumn::mutate(std::move(arg_else_column));
|
|
|
|
if (else_is_short)
|
|
|
|
result_column->expand(cond_col->getData(), true);
|
2022-03-21 14:27:25 +00:00
|
|
|
if (isColumnNullable(*result_column))
|
2017-12-08 02:00:11 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnNullable &>(*result_column).applyNullMap(assert_cast<const ColumnUInt8 &>(*arg_cond.column));
|
2020-10-19 13:42:14 +00:00
|
|
|
return result_column;
|
2017-12-08 02:00:11 +00:00
|
|
|
}
|
|
|
|
else
|
2021-06-07 10:55:55 +00:00
|
|
|
return ColumnNullable::create(materializeColumnIfConst(result_column), arg_cond.column);
|
2017-12-08 02:00:11 +00:00
|
|
|
}
|
|
|
|
else if (cond_const_col)
|
|
|
|
{
|
2017-12-16 04:29:34 +00:00
|
|
|
if (cond_const_col->getValue<UInt8>())
|
2020-10-19 13:42:14 +00:00
|
|
|
return result_type->createColumn()->cloneResized(input_rows_count);
|
2017-12-16 04:29:34 +00:00
|
|
|
else
|
2022-03-21 14:27:25 +00:00
|
|
|
return makeNullableColumnIfNot(arg_else_column);
|
2017-12-08 02:00:11 +00:00
|
|
|
}
|
|
|
|
else
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}. "
|
|
|
|
"Must be ColumnUInt8 or ColumnConstUInt8.", arg_cond.column->getName(), getName());
|
2017-12-08 02:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If else is NULL, we create Nullable column with null mask OR-ed with negated condition.
|
|
|
|
if (else_is_null)
|
|
|
|
{
|
2022-03-21 14:27:25 +00:00
|
|
|
ColumnPtr arg_then_column;
|
|
|
|
/// In case when arg_then column type differs with result
|
|
|
|
/// column type we should cast it to result type.
|
|
|
|
if (removeNullable(arg_then.type)->getName() != removeNullable(result_type)->getName())
|
|
|
|
arg_then_column = castColumn(arg_then, result_type);
|
|
|
|
else
|
|
|
|
arg_then_column = arg_then.column;
|
|
|
|
|
2017-12-08 02:00:11 +00:00
|
|
|
if (cond_col)
|
|
|
|
{
|
2022-11-11 14:49:49 +00:00
|
|
|
arg_then_column = arg_then_column->convertToFullColumnIfConst();
|
2021-06-07 10:55:55 +00:00
|
|
|
auto result_column = IColumn::mutate(std::move(arg_then_column));
|
|
|
|
if (then_is_short)
|
|
|
|
result_column->expand(cond_col->getData(), false);
|
2017-12-08 02:00:11 +00:00
|
|
|
|
2022-03-21 14:27:25 +00:00
|
|
|
if (isColumnNullable(*result_column))
|
2017-12-08 02:00:11 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnNullable &>(*result_column).applyNegatedNullMap(assert_cast<const ColumnUInt8 &>(*arg_cond.column));
|
2020-10-19 13:42:14 +00:00
|
|
|
return result_column;
|
2017-12-08 02:00:11 +00:00
|
|
|
}
|
|
|
|
else
|
2021-06-08 13:55:07 +00:00
|
|
|
{
|
|
|
|
size_t size = input_rows_count;
|
|
|
|
const auto & null_map_data = cond_col->getData();
|
|
|
|
|
|
|
|
auto negated_null_map = ColumnUInt8::create();
|
|
|
|
auto & negated_null_map_data = negated_null_map->getData();
|
|
|
|
negated_null_map_data.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
negated_null_map_data[i] = !null_map_data[i];
|
|
|
|
|
2021-06-07 10:55:55 +00:00
|
|
|
return ColumnNullable::create(materializeColumnIfConst(result_column), std::move(negated_null_map));
|
2021-06-08 13:55:07 +00:00
|
|
|
}
|
2017-12-08 02:00:11 +00:00
|
|
|
}
|
|
|
|
else if (cond_const_col)
|
|
|
|
{
|
2017-12-16 04:29:34 +00:00
|
|
|
if (cond_const_col->getValue<UInt8>())
|
2022-03-21 14:27:25 +00:00
|
|
|
return makeNullableColumnIfNot(arg_then_column);
|
2017-12-16 04:29:34 +00:00
|
|
|
else
|
2020-10-19 13:42:14 +00:00
|
|
|
return result_type->createColumn()->cloneResized(input_rows_count);
|
2017-12-08 02:00:11 +00:00
|
|
|
}
|
|
|
|
else
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}. "
|
|
|
|
"Must be ColumnUInt8 or ColumnConstUInt8.", arg_cond.column->getName(), getName());
|
2017-12-08 02:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return nullptr;
|
2017-12-08 02:00:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 09:53:48 +00:00
|
|
|
static void executeShortCircuitArguments(ColumnsWithTypeAndName & arguments)
|
|
|
|
{
|
2021-12-20 11:49:05 +00:00
|
|
|
int last_short_circuit_argument_index = checkShortCircuitArguments(arguments);
|
2021-06-15 09:53:48 +00:00
|
|
|
if (last_short_circuit_argument_index == -1)
|
|
|
|
return;
|
|
|
|
|
2022-06-17 11:44:49 +00:00
|
|
|
executeColumnIfNeeded(arguments[0]);
|
|
|
|
|
2021-06-15 09:53:48 +00:00
|
|
|
/// Check if condition is const or null to not create full mask from it.
|
|
|
|
if ((isColumnConst(*arguments[0].column) || arguments[0].column->onlyNull()) && !arguments[0].column->empty())
|
|
|
|
{
|
|
|
|
bool value = arguments[0].column->getBool(0);
|
|
|
|
executeColumnIfNeeded(arguments[1], !value);
|
|
|
|
executeColumnIfNeeded(arguments[2], value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
IColumn::Filter mask(arguments[0].column->size(), 1);
|
2021-08-10 11:31:15 +00:00
|
|
|
auto mask_info = extractMask(mask, arguments[0].column);
|
2021-06-15 09:53:48 +00:00
|
|
|
maskedExecute(arguments[1], mask, mask_info);
|
2021-08-10 11:31:15 +00:00
|
|
|
inverseMask(mask, mask_info);
|
|
|
|
maskedExecute(arguments[2], mask, mask_info);
|
2021-06-15 09:53:48 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 02:12:59 +00:00
|
|
|
public:
|
2015-10-11 23:36:45 +00:00
|
|
|
String getName() const override
|
2012-09-24 02:12:59 +00:00
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
return name;
|
2012-09-24 02:12:59 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-12-29 19:38:10 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 3; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-24 01:00:31 +00:00
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
2022-11-18 14:56:06 +00:00
|
|
|
bool useDefaultImplementationForNothing() const override { return false; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isShortCircuit(ShortCircuitSettings & settings, size_t /*number_of_arguments*/) const override
|
2021-06-07 10:55:55 +00:00
|
|
|
{
|
2021-06-22 16:21:23 +00:00
|
|
|
settings.enable_lazy_execution_for_first_argument = false;
|
|
|
|
settings.enable_lazy_execution_for_common_descendants_of_arguments = false;
|
|
|
|
settings.force_enable_lazy_execution = false;
|
2021-06-07 10:55:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2019-10-02 17:51:00 +00:00
|
|
|
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
|
2022-12-20 22:07:59 +00:00
|
|
|
bool canBeExecutedOnLowCardinalityDictionary() const override { return false; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Get result types by argument types. If the function does not apply to these arguments, throw an exception.
|
2016-07-06 09:47:55 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2012-09-24 02:12:59 +00:00
|
|
|
{
|
2017-12-09 06:32:22 +00:00
|
|
|
if (arguments[0]->onlyNull())
|
2019-05-10 10:42:00 +00:00
|
|
|
return arguments[2];
|
2017-12-08 04:52:38 +00:00
|
|
|
|
2017-12-07 12:09:55 +00:00
|
|
|
if (arguments[0]->isNullable())
|
2019-05-10 10:42:00 +00:00
|
|
|
return getReturnTypeImpl({
|
|
|
|
removeNullable(arguments[0]), arguments[1], arguments[2]});
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!WhichDataType(arguments[0]).isUInt8())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of first argument (condition) of function if. "
|
|
|
|
"Must be UInt8.", arguments[0]->getName());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-06-12 15:10:25 +00:00
|
|
|
return getLeastSupertype(DataTypes{arguments[1], arguments[2]});
|
2012-09-24 02:12:59 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-06-07 10:55:55 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & args, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2012-09-24 02:12:59 +00:00
|
|
|
{
|
2022-03-02 17:22:12 +00:00
|
|
|
ColumnsWithTypeAndName arguments = args;
|
2021-06-07 10:55:55 +00:00
|
|
|
executeShortCircuitArguments(arguments);
|
2020-10-19 13:42:14 +00:00
|
|
|
ColumnPtr res;
|
|
|
|
if ( (res = executeForConstAndNullableCondition(arguments, result_type, input_rows_count))
|
|
|
|
|| (res = executeForNullThenElse(arguments, result_type, input_rows_count))
|
|
|
|
|| (res = executeForNullableThenElse(arguments, result_type, input_rows_count)))
|
|
|
|
return res;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
const ColumnWithTypeAndName & arg_cond = arguments[0];
|
|
|
|
const ColumnWithTypeAndName & arg_then = arguments[1];
|
|
|
|
const ColumnWithTypeAndName & arg_else = arguments[2];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-02-03 08:41:25 +00:00
|
|
|
/// A case for identical then and else (pointers are the same).
|
|
|
|
if (arg_then.column.get() == arg_else.column.get())
|
|
|
|
{
|
|
|
|
/// Just point result to them.
|
2020-10-19 13:42:14 +00:00
|
|
|
return arg_then.column;
|
2019-02-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 06:46:45 +00:00
|
|
|
const ColumnUInt8 * cond_col = typeid_cast<const ColumnUInt8 *>(arg_cond.column.get());
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnConst * cond_const_col = checkAndGetColumnConst<ColumnVector<UInt8>>(arg_cond.column.get());
|
2017-03-06 06:46:45 +00:00
|
|
|
ColumnPtr materialized_cond_col;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2012-09-24 02:12:59 +00:00
|
|
|
if (cond_const_col)
|
|
|
|
{
|
2017-03-06 06:46:45 +00:00
|
|
|
if (arg_then.type->equals(*arg_else.type))
|
2013-10-08 12:30:43 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
return cond_const_col->getValue<UInt8>()
|
2016-07-09 03:54:57 +00:00
|
|
|
? arg_then.column
|
|
|
|
: arg_else.column;
|
2013-10-08 12:30:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
materialized_cond_col = cond_const_col->convertToFullColumn();
|
2016-05-28 05:31:36 +00:00
|
|
|
cond_col = typeid_cast<const ColumnUInt8 *>(&*materialized_cond_col);
|
2013-10-08 12:30:43 +00:00
|
|
|
}
|
2012-09-24 02:12:59 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-10-10 18:37:54 +00:00
|
|
|
if (!cond_col)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}. "
|
|
|
|
"Must be ColumnUInt8 or ColumnConstUInt8.", arg_cond.column->getName(), getName());
|
2018-10-10 18:37:54 +00:00
|
|
|
|
|
|
|
auto call = [&](const auto & types) -> bool
|
|
|
|
{
|
|
|
|
using Types = std::decay_t<decltype(types)>;
|
|
|
|
using T0 = typename Types::LeftType;
|
|
|
|
using T1 = typename Types::RightType;
|
|
|
|
|
2021-06-15 03:54:24 +00:00
|
|
|
res = executeTyped<T0, T1>(cond_col, arguments, result_type, input_rows_count);
|
|
|
|
return res != nullptr;
|
2018-10-10 18:37:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TypeIndex left_id = arg_then.type->getTypeId();
|
|
|
|
TypeIndex right_id = arg_else.type->getTypeId();
|
|
|
|
|
2020-04-22 08:31:10 +00:00
|
|
|
if (const auto * left_array = checkAndGetDataType<DataTypeArray>(arg_then.type.get()))
|
2018-10-10 18:37:54 +00:00
|
|
|
left_id = left_array->getNestedType()->getTypeId();
|
|
|
|
|
2020-04-22 08:31:10 +00:00
|
|
|
if (const auto * right_array = checkAndGetDataType<DataTypeArray>(arg_else.type.get()))
|
|
|
|
right_id = right_array->getNestedType()->getTypeId();
|
2018-10-10 18:37:54 +00:00
|
|
|
|
2019-02-03 08:41:25 +00:00
|
|
|
if (!(callOnBasicTypes<true, true, true, false>(left_id, right_id, call)
|
2021-05-06 15:45:58 +00:00
|
|
|
|| (res = executeTyped<UUID, UUID>(cond_col, arguments, result_type, input_rows_count))
|
2020-10-19 13:42:14 +00:00
|
|
|
|| (res = executeString(cond_col, arguments, result_type))
|
|
|
|
|| (res = executeGenericArray(cond_col, arguments, result_type))
|
2021-02-05 19:39:26 +00:00
|
|
|
|| (res = executeTuple(arguments, result_type, input_rows_count))))
|
2019-02-03 08:41:25 +00:00
|
|
|
{
|
2020-10-20 13:11:57 +00:00
|
|
|
return executeGeneric(cond_col, arguments, input_rows_count);
|
2019-02-03 08:41:25 +00:00
|
|
|
}
|
2020-10-20 13:11:57 +00:00
|
|
|
|
|
|
|
return res;
|
2012-09-24 02:12:59 +00:00
|
|
|
}
|
2023-06-03 11:16:59 +00:00
|
|
|
|
|
|
|
ColumnPtr getConstantResultForNonConstArguments(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type) const override
|
|
|
|
{
|
|
|
|
const ColumnWithTypeAndName & arg_cond = arguments[0];
|
2023-06-04 12:02:46 +00:00
|
|
|
if (!arg_cond.column || !isColumnConst(*arg_cond.column))
|
2023-06-03 11:16:59 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
const ColumnConst * cond_const_col = checkAndGetColumnConst<ColumnVector<UInt8>>(arg_cond.column.get());
|
2023-06-05 07:32:46 +00:00
|
|
|
if (!cond_const_col)
|
|
|
|
return {};
|
|
|
|
|
2023-06-03 11:16:59 +00:00
|
|
|
bool condition_value = cond_const_col->getValue<UInt8>();
|
|
|
|
|
|
|
|
const ColumnWithTypeAndName & arg_then = arguments[1];
|
|
|
|
const ColumnWithTypeAndName & arg_else = arguments[2];
|
|
|
|
const ColumnWithTypeAndName & potential_const_column = condition_value ? arg_then : arg_else;
|
|
|
|
|
|
|
|
if (!potential_const_column.column || !isColumnConst(*potential_const_column.column))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto result = castColumn(potential_const_column, result_type);
|
2023-06-04 12:02:46 +00:00
|
|
|
if (!isColumnConst(*result))
|
2023-06-03 11:16:59 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2012-09-24 02:12:59 +00:00
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(If)
|
2016-08-12 14:06:58 +00:00
|
|
|
{
|
2022-08-27 20:06:03 +00:00
|
|
|
factory.registerFunction<FunctionIf>({}, FunctionFactory::CaseInsensitive);
|
2018-12-02 02:47:47 +00:00
|
|
|
}
|
2016-08-12 14:06:58 +00:00
|
|
|
|
2012-09-24 02:12:59 +00:00
|
|
|
}
|