ClickHouse/src/Functions/byteSize.cpp

89 lines
2.8 KiB
C++
Raw Normal View History

2020-12-21 01:54:04 +00:00
#include <DataTypes/DataTypesNumber.h>
2021-01-02 22:58:10 +00:00
#include <Columns/ColumnsNumber.h>
2020-12-28 17:36:19 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
2020-12-28 17:36:19 +00:00
2020-12-21 01:54:04 +00:00
namespace DB
{
2020-12-23 19:39:02 +00:00
2020-12-21 01:54:04 +00:00
namespace
{
2021-01-02 22:58:10 +00:00
/** byteSize() - get the value size in number of bytes for accounting purposes.
2020-12-21 01:54:04 +00:00
*/
class FunctionByteSize : public IFunction
{
public:
static constexpr auto name = "byteSize";
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr)
2020-12-21 01:54:04 +00:00
{
return std::make_shared<FunctionByteSize>();
}
2020-12-28 11:29:54 +00:00
String getName() const override { return name; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForNothing() const override { return false; }
2020-12-28 11:29:54 +00:00
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
bool useDefaultImplementationForSparseColumns() const override { return false; }
2020-12-21 01:54:04 +00:00
bool isVariadic() const override { return true; }
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
2020-12-28 11:29:54 +00:00
size_t getNumberOfArguments() const override { return 0; }
2020-12-21 01:54:04 +00:00
DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
{
return std::make_shared<DataTypeUInt64>();
}
2021-01-02 22:58:10 +00:00
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
2020-12-21 01:54:04 +00:00
{
2021-01-02 22:58:10 +00:00
size_t num_args = arguments.size();
2020-12-21 01:54:04 +00:00
2021-01-02 22:58:10 +00:00
/// If the resulting size is constant, return constant column.
2020-12-21 01:54:04 +00:00
2021-01-02 22:58:10 +00:00
bool all_constant = true;
UInt64 constant_size = 0;
for (size_t arg_num = 0; arg_num < num_args; ++arg_num)
2020-12-31 17:14:21 +00:00
{
2021-01-02 22:58:10 +00:00
if (arguments[arg_num].type->isValueUnambiguouslyRepresentedInFixedSizeContiguousMemoryRegion())
2020-12-21 01:54:04 +00:00
{
2021-01-02 22:58:10 +00:00
constant_size += arguments[arg_num].type->getSizeOfValueInMemory();
2020-12-21 01:54:04 +00:00
}
2021-01-02 22:58:10 +00:00
else
2020-12-28 17:36:19 +00:00
{
2021-01-02 22:58:10 +00:00
all_constant = false;
break;
2020-12-28 17:36:19 +00:00
}
2020-12-23 19:39:02 +00:00
}
2021-01-02 22:58:10 +00:00
if (all_constant)
return result_type->createColumnConst(input_rows_count, constant_size);
2020-12-31 17:14:21 +00:00
2021-01-02 22:58:10 +00:00
auto result_col = ColumnUInt64::create(input_rows_count);
auto & vec_res = result_col->getData();
for (size_t arg_num = 0; arg_num < num_args; ++arg_num)
2020-12-31 17:14:21 +00:00
{
2021-01-02 22:58:10 +00:00
const IColumn * column = arguments[arg_num].column.get();
2020-12-31 17:14:21 +00:00
2021-01-02 22:58:10 +00:00
if (arg_num == 0)
for (size_t row_num = 0; row_num < input_rows_count; ++row_num)
vec_res[row_num] = column->byteSizeAt(row_num);
else
for (size_t row_num = 0; row_num < input_rows_count; ++row_num)
vec_res[row_num] += column->byteSizeAt(row_num);
2020-12-31 17:14:21 +00:00
}
2021-01-02 22:58:10 +00:00
return result_col;
2020-12-21 01:54:04 +00:00
}
};
}
REGISTER_FUNCTION(ByteSize)
2020-12-21 01:54:04 +00:00
{
factory.registerFunction<FunctionByteSize>();
}
}