ClickHouse/src/Functions/polygonsEquals.cpp
Nikita Mikhailov 0425d566d3 better
2021-03-11 15:08:17 +03:00

116 lines
3.2 KiB
C++

#include <Functions/FunctionFactory.h>
#include <Functions/geometryConverters.h>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <common/logger_useful.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnTuple.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnsNumber.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypeCustomGeo.h>
#include <memory>
#include <utility>
namespace DB
{
namespace ErrorCodes
{
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
extern const int BAD_ARGUMENTS;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ILLEGAL_COLUMN;
}
class FunctionPolygonsEquals : public IFunction
{
public:
static inline const char * name = "polygonsEquals";
explicit FunctionPolygonsEquals() = default;
static FunctionPtr create(const Context &)
{
return std::make_shared<FunctionPolygonsEquals>();
}
String getName() const override
{
return name;
}
bool isVariadic() const override
{
return false;
}
size_t getNumberOfArguments() const override
{
return 2;
}
DataTypePtr getReturnTypeImpl(const DataTypes &) const override
{
return std::make_shared<DataTypeUInt8>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /*result_type*/, size_t input_rows_count) const override
{
auto get_parser = [&arguments] (size_t i) {
const auto * const_col =
checkAndGetColumn<ColumnConst>(arguments[i].column.get());
bool is_const = static_cast<bool>(const_col);
return std::pair<bool, CartesianGeometryFromColumnParser>{is_const, is_const ?
makeCartesianGeometryFromColumnParser(ColumnWithTypeAndName(const_col->getDataColumnPtr(), arguments[i].type, arguments[i].name)) :
makeCartesianGeometryFromColumnParser(arguments[i])};
};
auto [is_first_polygon_const, first_parser] = get_parser(0);
auto first_container = createContainer(first_parser);
auto [is_second_polygon_const, second_parser] = get_parser(1);
auto second_container = createContainer(second_parser);
auto res_column = ColumnUInt8::create();
for (size_t i = 0; i < input_rows_count; i++)
{
if (!is_first_polygon_const || i == 0)
get(first_parser, first_container, i);
if (!is_second_polygon_const || i == 0)
get(second_parser, second_container, i);
bool equals = boost::geometry::equals(
boost::get<CartesianMultiPolygon>(first_container),
boost::get<CartesianMultiPolygon>(second_container));
res_column->insertValue(equals);
}
return res_column;
}
bool useDefaultImplementationForConstants() const override
{
return true;
}
};
void registerFunctionPolygonsEquals(FunctionFactory & factory)
{
factory.registerFunction<FunctionPolygonsEquals>();
}
}