ClickHouse/src/Functions/svg.cpp

104 lines
2.9 KiB
C++
Raw Normal View History

2021-02-19 18:09:38 +00:00
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/geometryConverters.h>
#include <Columns/ColumnString.h>
#include <string>
#include <memory>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
2021-02-19 18:09:38 +00:00
}
class FunctionSvg : public IFunction
{
public:
static inline const char * name = "svg";
explicit FunctionSvg() = default;
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr)
2021-02-19 18:09:38 +00:00
{
return std::make_shared<FunctionSvg>();
}
String getName() const override
{
return name;
}
bool isVariadic() const override
{
return true;
}
size_t getNumberOfArguments() const override
{
return 2;
}
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
2021-02-19 18:09:38 +00:00
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (arguments.empty() || arguments.size() > 2)
2021-02-19 18:09:38 +00:00
{
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Incorrect number of arguments: expected 1 or 2 arguments");
2021-02-19 18:09:38 +00:00
}
else if (arguments.size() == 2 && checkAndGetDataType<DataTypeString>(arguments[1].get()) == nullptr)
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Second argument should be String");
2021-02-19 18:09:38 +00:00
}
return std::make_shared<DataTypeString>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /*result_type*/, size_t input_rows_count) const override
{
auto res_column = ColumnString::create();
2021-02-26 15:29:26 +00:00
bool has_style = arguments.size() > 1;
ColumnPtr style;
if (has_style)
style = arguments[1].column;
2021-02-19 18:09:38 +00:00
callOnGeometryDataType<CartesianPoint>(arguments[0].type, [&] (const auto & type)
2021-02-19 18:09:38 +00:00
{
2021-02-20 13:59:37 +00:00
using TypeConverter = std::decay_t<decltype(type)>;
using Converter = typename TypeConverter::Type;
2021-02-20 17:44:18 +00:00
2021-02-26 15:29:26 +00:00
auto figures = Converter::convert(arguments[0].column->convertToFullColumnIfConst());
2021-12-20 12:55:07 +00:00
for (size_t i = 0; i < input_rows_count; ++i)
{
std::stringstream str; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
boost::geometry::correct(figures[i]);
str << boost::geometry::svg(figures[i], has_style ? style->getDataAt(i).toString() : "");
std::string serialized = str.str();
res_column->insertData(serialized.c_str(), serialized.size());
}
2021-02-19 18:09:38 +00:00
}
);
2021-02-20 13:59:37 +00:00
2021-02-19 18:09:38 +00:00
return res_column;
}
bool useDefaultImplementationForConstants() const override
{
return true;
}
};
REGISTER_FUNCTION(Svg)
2021-02-19 18:09:38 +00:00
{
factory.registerFunction<FunctionSvg>();
2021-10-19 12:11:18 +00:00
factory.registerAlias("SVG", "svg");
2021-02-19 18:09:38 +00:00
}
}