2020-10-10 18:37:02 +00:00
|
|
|
#pragma once
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-09 20:57:54 +00:00
|
|
|
#include <Functions/GatherUtils/GatherUtils.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionArrayPop : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2019-08-03 11:02:40 +00:00
|
|
|
FunctionArrayPop(bool pop_front_, const char * name_) : pop_front(pop_front_), name(name_) {}
|
2018-09-09 20:57:54 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isVariadic() const override { return false; }
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2018-09-09 20:57:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments[0]->onlyNull())
|
|
|
|
return arguments[0];
|
|
|
|
|
2020-10-19 21:21:10 +00:00
|
|
|
const auto * array_type = typeid_cast<const DataTypeArray *>(arguments[0].get());
|
2018-09-09 20:57:54 +00:00
|
|
|
if (!array_type)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"First argument for function {} must be an array but it has type {}.",
|
|
|
|
getName(), arguments[0]->getName());
|
2018-09-09 20:57:54 +00:00
|
|
|
|
|
|
|
return arguments[0];
|
|
|
|
}
|
|
|
|
|
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-09 20:57:54 +00:00
|
|
|
{
|
2020-10-19 21:21:10 +00:00
|
|
|
const auto & return_type = result_type;
|
2018-09-09 20:57:54 +00:00
|
|
|
|
|
|
|
if (return_type->onlyNull())
|
2020-10-19 21:21:10 +00:00
|
|
|
return return_type->createColumnConstWithDefaultValue(input_rows_count);
|
2018-09-09 20:57:54 +00:00
|
|
|
|
2020-10-19 21:21:10 +00:00
|
|
|
const auto & array_column = arguments[0].column;
|
2018-09-09 20:57:54 +00:00
|
|
|
|
|
|
|
std::unique_ptr<GatherUtils::IArraySource> source;
|
|
|
|
|
|
|
|
size_t size = array_column->size();
|
|
|
|
|
2020-10-19 21:21:10 +00:00
|
|
|
if (const auto * argument_column_array = typeid_cast<const ColumnArray *>(array_column.get()))
|
2018-09-09 20:57:54 +00:00
|
|
|
source = GatherUtils::createArraySource(*argument_column_array, false, size);
|
|
|
|
else
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "First arguments for function {} must be array.", getName());
|
2018-09-09 20:57:54 +00:00
|
|
|
|
2020-09-16 12:21:26 +00:00
|
|
|
ColumnArray::MutablePtr sink;
|
2018-09-09 20:57:54 +00:00
|
|
|
|
|
|
|
if (pop_front)
|
2020-09-16 12:21:26 +00:00
|
|
|
sink = GatherUtils::sliceFromLeftConstantOffsetUnbounded(*source, 1);
|
2018-09-09 20:57:54 +00:00
|
|
|
else
|
2020-09-16 12:21:26 +00:00
|
|
|
sink = GatherUtils::sliceFromLeftConstantOffsetBounded(*source, 0, -1);
|
2018-09-09 20:57:54 +00:00
|
|
|
|
2020-10-19 21:21:10 +00:00
|
|
|
return sink;
|
2018-09-09 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool pop_front;
|
|
|
|
const char * name;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|