ClickHouse/src/Functions/geometryConverters.cpp

87 lines
2.4 KiB
C++
Raw Normal View History

2020-06-14 17:04:10 +00:00
#include <Functions/geometryConverters.h>
2020-06-07 16:47:56 +00:00
#include <DataTypes/DataTypeCustomGeo.h>
2020-06-07 12:33:49 +00:00
2020-06-07 16:47:56 +00:00
namespace DB {
2020-06-07 13:42:09 +00:00
namespace {
2020-06-07 14:58:34 +00:00
size_t getArrayDepth(DataTypePtr data_type, size_t max_depth)
2020-06-07 13:42:09 +00:00
{
size_t depth = 0;
2020-06-07 16:47:56 +00:00
while (data_type && isArray(data_type) && depth != max_depth + 1)
2020-06-07 13:42:09 +00:00
{
2020-06-07 16:47:56 +00:00
depth++;
2020-06-07 13:42:09 +00:00
data_type = static_cast<const DataTypeArray &>(*data_type).getNestedType();
}
2020-06-07 16:47:56 +00:00
return depth;
2020-06-07 13:42:09 +00:00
}
2020-06-07 16:04:35 +00:00
class ContainerCreator : public boost::static_visitor<Float64Geometry>
{
public:
template <class T>
Float64Geometry operator()(const T & parser) const
{
return parser.createContainer();
}
};
class Getter : public boost::static_visitor<void>
{
public:
2020-06-14 17:04:10 +00:00
constexpr Getter(Float64Geometry & container_, size_t i_)
2020-06-07 16:04:35 +00:00
: container(container_)
, i(i_)
{}
template <class T>
void operator()(const T & parser) const
{
parser.get(container, i);
}
private:
Float64Geometry & container;
size_t i;
};
2020-06-07 18:26:18 +00:00
template <class DataType, class Parser>
Parser makeParser(const ColumnWithTypeAndName & col)
2020-06-07 16:47:56 +00:00
{
2020-06-07 17:28:05 +00:00
auto wanted_data_type = DataType::nestedDataType();
2020-06-14 17:04:10 +00:00
ColumnPtr casted = castColumn(col, DataType::nestedDataType());
2020-06-07 16:47:56 +00:00
if (!casted)
{
throw Exception("Failed to cast " + col.type->getName() + " to " + wanted_data_type->getName(), ErrorCodes::ILLEGAL_COLUMN);
}
2020-06-14 17:04:10 +00:00
return Parser(std::move(casted));
2020-06-07 18:26:18 +00:00
}
2020-06-07 16:47:56 +00:00
}
2020-06-07 13:42:09 +00:00
GeometryFromColumnParser makeGeometryFromColumnParser(const ColumnWithTypeAndName & col)
2020-06-07 12:33:49 +00:00
{
2020-06-07 14:58:34 +00:00
switch (getArrayDepth(col.type, 3)) {
2020-06-07 18:26:18 +00:00
case 0: return makeParser<DataTypeCustomPointSerialization, Float64PointFromColumnParser>(col);
case 1: return makeParser<DataTypeCustomRingSerialization, Float64RingFromColumnParser>(col);
case 2: return makeParser<DataTypeCustomPolygonSerialization, Float64PolygonFromColumnParser>(col);
case 3: return makeParser<DataTypeCustomMultiPolygonSerialization, Float64MultiPolygonFromColumnParser>(col);
2020-06-07 14:28:46 +00:00
default: throw Exception("Cannot parse geometry from column with type " + col.type->getName()
+ ", array depth is too big", ErrorCodes::ILLEGAL_COLUMN);
2020-06-07 13:42:09 +00:00
}
2020-06-07 12:33:49 +00:00
}
2020-06-07 16:04:35 +00:00
Float64Geometry createContainer(const GeometryFromColumnParser & parser)
{
static ContainerCreator creator;
return boost::apply_visitor(creator, parser);
}
void get(const GeometryFromColumnParser & parser, Float64Geometry & container, size_t i)
{
boost::apply_visitor(Getter(container, i), parser);
}
2020-06-07 12:33:49 +00:00
}