ClickHouse/src/Functions/bar.cpp

138 lines
4.5 KiB
C++
Raw Normal View History

2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
#include <Functions/FunctionFactory.h>
#include <DataTypes/DataTypeString.h>
#include <Columns/ColumnString.h>
#include <Common/UnicodeBar.h>
#include <IO/WriteHelpers.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int ARGUMENT_OUT_OF_BOUND;
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
2021-03-25 10:41:03 +00:00
extern const int BAD_ARGUMENTS;
}
2020-09-07 18:00:37 +00:00
namespace
{
/** bar(x, min, max, width) - draws a strip from the number of characters proportional to (x - min) and equal to width for x == max.
* Returns a string with nice Unicode-art bar with resolution of 1/8 part of symbol.
*/
class FunctionBar : public IFunction
{
public:
static constexpr auto name = "bar";
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr)
{
return std::make_shared<FunctionBar>();
}
String getName() const override
{
return name;
}
bool isVariadic() const override
{
return true;
}
bool isSuitableForShortCircuitArgumentsExecution(ColumnsWithTypeAndName & /*arguments*/) const override { return true; }
size_t getNumberOfArguments() const override
{
return 0;
}
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (arguments.size() != 3 && arguments.size() != 4)
throw Exception("Function " + getName()
+ " requires from 3 or 4 parameters: value, min_value, max_value, [max_width_of_bar = 80]. Passed "
+ toString(arguments.size())
+ ".",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
2021-01-02 20:40:15 +00:00
if (!isNumber(arguments[0]) || !isNumber(arguments[1]) || !isNumber(arguments[2])
|| (arguments.size() == 4 && !isNumber(arguments[3])))
throw Exception("All arguments for function " + getName() + " must be numeric.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return std::make_shared<DataTypeString>();
}
bool useDefaultImplementationForConstants() const override { return true; }
2021-01-02 20:40:15 +00:00
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {3}; }
2021-01-02 20:40:15 +00:00
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
2021-01-02 20:40:15 +00:00
/// The maximum width of the bar in characters.
Float64 max_width = 80; /// Motivated by old-school terminal size.
2021-01-02 20:40:15 +00:00
if (arguments.size() == 4)
{
const auto & max_width_column = *arguments[3].column;
if (!isColumnConst(max_width_column))
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Fourth argument for function {} must be constant", getName());
max_width = max_width_column.getFloat64(0);
}
2020-12-25 15:54:38 +00:00
if (isNaN(max_width))
throw Exception("Argument 'max_width' must not be NaN", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (max_width < 1)
2020-12-25 15:54:38 +00:00
throw Exception("Argument 'max_width' must be >= 1", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
if (max_width > 1000)
2020-12-25 15:54:38 +00:00
throw Exception("Argument 'max_width' must be <= 1000", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
2020-10-17 15:22:42 +00:00
const auto & src = *arguments[0].column;
2021-01-02 20:40:15 +00:00
size_t current_offset = 0;
2021-01-02 20:40:15 +00:00
auto res_column = ColumnString::create();
2021-01-02 20:40:15 +00:00
ColumnString::Chars & dst_chars = res_column->getChars();
ColumnString::Offsets & dst_offsets = res_column->getOffsets();
2021-01-02 20:40:15 +00:00
dst_offsets.resize(input_rows_count);
dst_chars.reserve(input_rows_count * (UnicodeBar::getWidthInBytes(max_width) + 1)); /// strings are 0-terminated.
2021-01-02 20:40:15 +00:00
for (size_t i = 0; i < input_rows_count; ++i)
{
2021-01-02 20:40:15 +00:00
Float64 width = UnicodeBar::getWidth(
src.getFloat64(i),
arguments[1].column->getFloat64(i),
arguments[2].column->getFloat64(i),
max_width);
2021-03-25 10:41:03 +00:00
if (!isFinite(width))
throw Exception("Value of width must not be NaN and Inf", ErrorCodes::BAD_ARGUMENTS);
size_t next_size = current_offset + UnicodeBar::getWidthInBytes(width) + 1;
dst_chars.resize(next_size);
UnicodeBar::render(width, reinterpret_cast<char *>(&dst_chars[current_offset]));
current_offset = next_size;
dst_offsets[i] = current_offset;
}
2021-01-02 20:40:15 +00:00
return res_column;
}
};
2020-09-07 18:00:37 +00:00
}
void registerFunctionBar(FunctionFactory & factory)
{
factory.registerFunction<FunctionBar>();
}
}