ClickHouse/src/Functions/svg.cpp

111 lines
3.0 KiB
C++
Raw Normal View History

2020-06-21 19:32:39 +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
{
2021-01-19 17:16:10 +00:00
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
2020-06-21 19:32:39 +00:00
extern const int TOO_MANY_ARGUMENTS_FOR_FUNCTION;
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
}
class FunctionSvg : public IFunction
{
public:
static inline const char * name = "svg";
explicit FunctionSvg() {}
static FunctionPtr create(const Context &)
{
return std::make_shared<FunctionSvg>();
}
String getName() const override
{
return name;
}
bool isVariadic() const override
{
return true;
}
size_t getNumberOfArguments() const override
{
return 2;
}
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (arguments.size() > 2)
{
throw Exception("Too many arguments", ErrorCodes::TOO_MANY_ARGUMENTS_FOR_FUNCTION);
}
2021-01-19 17:16:10 +00:00
else if (arguments.empty())
{
2020-06-21 19:32:39 +00:00
throw Exception("Too few arguments", ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION);
}
2021-01-19 12:02:56 +00:00
else if (arguments.size() == 2 && checkAndGetDataType<DataTypeString>(arguments[1].get()) == nullptr)
2020-06-21 19:32:39 +00:00
{
throw Exception("Second argument should be String",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
return std::make_shared<DataTypeString>();
}
2021-01-18 23:51:34 +00:00
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /*result_type*/, size_t input_rows_count) const override
2020-06-21 19:32:39 +00:00
{
2021-01-18 23:51:34 +00:00
const auto * const_col = checkAndGetColumn<ColumnConst>(arguments[0].column.get());
2020-06-21 19:32:39 +00:00
auto parser = const_col ?
2021-01-19 14:52:53 +00:00
makeGeometryFromColumnParser<CartesianPoint>(ColumnWithTypeAndName(const_col->getDataColumnPtr(), arguments[0].type, arguments[0].name)) :
makeGeometryFromColumnParser<CartesianPoint>(arguments[0]);
2020-06-21 19:32:39 +00:00
bool geo_column_is_const = static_cast<bool>(const_col);
auto res_column = ColumnString::create();
auto container = createContainer(parser);
bool has_style = arguments.size() > 1;
ColumnPtr style;
2021-01-19 17:16:10 +00:00
if (has_style)
2021-01-18 23:51:34 +00:00
style = arguments[1].column;
2020-06-21 19:32:39 +00:00
for (size_t i = 0; i < input_rows_count; i++)
{
2021-01-19 17:16:10 +00:00
/// FIXME
std::stringstream str; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
2020-06-21 19:32:39 +00:00
if (!geo_column_is_const || i == 0)
get(parser, container, i);
str << boost::geometry::svg(container, has_style ? style->getDataAt(i).toString() : "");
std::string serialized = str.str();
res_column->insertData(serialized.c_str(), serialized.size());
}
2021-01-18 23:51:34 +00:00
return res_column;
2020-06-21 19:32:39 +00:00
}
bool useDefaultImplementationForConstants() const override
{
return true;
}
};
void registerFunctionSvg(FunctionFactory & factory)
{
factory.registerFunction<FunctionSvg>();
}
}