2020-10-10 18:37:02 +00:00
|
|
|
#pragma once
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2019-11-04 14:06:22 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/NumberTraits.h>
|
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <bool is_first_line_zero>
|
|
|
|
struct FunctionRunningDifferenceName;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct FunctionRunningDifferenceName<true>
|
|
|
|
{
|
|
|
|
static constexpr auto name = "runningDifference";
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct FunctionRunningDifferenceName<false>
|
|
|
|
{
|
|
|
|
static constexpr auto name = "runningDifferenceStartingWithFirstValue";
|
|
|
|
};
|
|
|
|
|
2020-10-14 14:04:50 +00:00
|
|
|
/** Calculate difference of consecutive values in columns.
|
|
|
|
* So, result of function depends on partition of data to columnss and on order of data in columns.
|
2018-09-08 22:04:39 +00:00
|
|
|
*/
|
|
|
|
template <bool is_first_line_zero>
|
|
|
|
class FunctionRunningDifferenceImpl : public IFunction
|
|
|
|
{
|
|
|
|
private:
|
2020-10-14 14:04:50 +00:00
|
|
|
/// It is possible to track value from previous columns, to calculate continuously across all columnss. Not implemented.
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
template <typename Src, typename Dst>
|
2021-01-22 20:46:51 +00:00
|
|
|
static NO_SANITIZE_UNDEFINED void process(const PaddedPODArray<Src> & src, PaddedPODArray<Dst> & dst, const NullMap * null_map)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
size_t size = src.size();
|
|
|
|
dst.resize(size);
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/// It is possible to SIMD optimize this loop. By no need for that in practice.
|
|
|
|
|
2019-01-04 13:32:08 +00:00
|
|
|
Src prev{};
|
2018-09-08 22:04:39 +00:00
|
|
|
bool has_prev_value = false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
if (null_map && (*null_map)[i])
|
2018-12-29 00:53:11 +00:00
|
|
|
{
|
|
|
|
dst[i] = Dst{};
|
2018-09-08 22:04:39 +00:00
|
|
|
continue;
|
2018-12-29 00:53:11 +00:00
|
|
|
}
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
if (!has_prev_value)
|
|
|
|
{
|
|
|
|
dst[i] = is_first_line_zero ? 0 : src[i];
|
|
|
|
prev = src[i];
|
|
|
|
has_prev_value = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto cur = src[i];
|
2021-01-22 20:46:51 +00:00
|
|
|
/// Overflow is Ok.
|
2018-09-08 22:04:39 +00:00
|
|
|
dst[i] = static_cast<Dst>(cur) - prev;
|
|
|
|
prev = cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Result type is same as result of subtraction of argument types.
|
|
|
|
template <typename SrcFieldType>
|
|
|
|
using DstFieldType = typename NumberTraits::ResultOfSubtraction<SrcFieldType, SrcFieldType>::Type;
|
|
|
|
|
|
|
|
/// Call polymorphic lambda with tag argument of concrete field type of src_type.
|
|
|
|
template <typename F>
|
|
|
|
void dispatchForSourceType(const IDataType & src_type, F && f) const
|
|
|
|
{
|
|
|
|
WhichDataType which(src_type);
|
|
|
|
|
|
|
|
if (which.isUInt8())
|
|
|
|
f(UInt8());
|
|
|
|
else if (which.isUInt16())
|
|
|
|
f(UInt16());
|
|
|
|
else if (which.isUInt32())
|
|
|
|
f(UInt32());
|
|
|
|
else if (which.isUInt64())
|
|
|
|
f(UInt64());
|
|
|
|
else if (which.isInt8())
|
|
|
|
f(Int8());
|
|
|
|
else if (which.isInt16())
|
|
|
|
f(Int16());
|
|
|
|
else if (which.isInt32())
|
|
|
|
f(Int32());
|
|
|
|
else if (which.isInt64())
|
|
|
|
f(Int64());
|
|
|
|
else if (which.isFloat32())
|
|
|
|
f(Float32());
|
|
|
|
else if (which.isFloat64())
|
|
|
|
f(Float64());
|
|
|
|
else if (which.isDate())
|
|
|
|
f(DataTypeDate::FieldType());
|
|
|
|
else if (which.isDateTime())
|
|
|
|
f(DataTypeDateTime::FieldType());
|
|
|
|
else
|
|
|
|
throw Exception("Argument for function " + getName() + " must have numeric type.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static constexpr auto name = FunctionRunningDifferenceName<is_first_line_zero>::name;
|
|
|
|
|
|
|
|
static FunctionPtr create(const Context &)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionRunningDifferenceImpl<is_first_line_zero>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2019-01-30 02:47:26 +00:00
|
|
|
bool isStateful() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-10 14:38:08 +00:00
|
|
|
bool isDeterministic() const override { return false; }
|
2018-09-08 22:04:39 +00:00
|
|
|
bool isDeterministicInScopeOfQuery() const override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
DataTypePtr res;
|
|
|
|
dispatchForSourceType(*removeNullable(arguments[0]), [&](auto field_type_tag)
|
|
|
|
{
|
|
|
|
res = std::make_shared<DataTypeNumber<DstFieldType<decltype(field_type_tag)>>>();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (arguments[0]->isNullable())
|
|
|
|
res = makeNullable(res);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
2020-11-17 13:24:45 +00:00
|
|
|
const auto & src = arguments.at(0);
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
/// When column is constant, its difference is zero.
|
2019-06-27 19:28:52 +00:00
|
|
|
if (isColumnConst(*src.column))
|
2020-10-19 15:27:41 +00:00
|
|
|
return result_type->createColumnConstWithDefaultValue(input_rows_count);
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
auto res_column = removeNullable(result_type)->createColumn();
|
|
|
|
const auto * src_column = src.column.get();
|
2018-09-08 22:04:39 +00:00
|
|
|
ColumnPtr null_map_column = nullptr;
|
|
|
|
const NullMap * null_map = nullptr;
|
2020-10-19 15:27:41 +00:00
|
|
|
if (const auto * nullable_column = checkAndGetColumn<ColumnNullable>(src_column))
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
src_column = &nullable_column->getNestedColumn();
|
|
|
|
null_map_column = nullable_column->getNullMapColumnPtr();
|
|
|
|
null_map = &nullable_column->getNullMapData();
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatchForSourceType(*removeNullable(src.type), [&](auto field_type_tag)
|
|
|
|
{
|
|
|
|
using SrcFieldType = decltype(field_type_tag);
|
|
|
|
|
2019-08-21 02:28:04 +00:00
|
|
|
process(assert_cast<const ColumnVector<SrcFieldType> &>(*src_column).getData(),
|
|
|
|
assert_cast<ColumnVector<DstFieldType<SrcFieldType>> &>(*res_column).getData(), null_map);
|
2018-09-08 22:04:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (null_map_column)
|
2020-10-19 15:27:41 +00:00
|
|
|
return ColumnNullable::create(std::move(res_column), null_map_column);
|
2018-09-08 22:04:39 +00:00
|
|
|
else
|
2020-10-20 21:27:01 +00:00
|
|
|
return res_column;
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|