2021-10-27 23:10:39 +00:00
|
|
|
#include "config_functions.h"
|
2021-07-06 21:45:45 +00:00
|
|
|
|
|
|
|
#if USE_S2_GEOMETRY
|
2021-04-28 22:16:45 +00:00
|
|
|
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/range.h>
|
2021-04-28 22:16:45 +00:00
|
|
|
|
2021-07-06 13:20:52 +00:00
|
|
|
#include "s2_fwd.h"
|
2021-04-28 22:16:45 +00:00
|
|
|
|
|
|
|
class S2CellId;
|
|
|
|
|
2021-07-06 11:34:33 +00:00
|
|
|
namespace DB
|
2021-04-28 22:16:45 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2021-07-12 11:49:17 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2021-04-28 22:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2021-07-08 15:31:39 +00:00
|
|
|
/**
|
|
|
|
* Returns a point (longitude, latitude) in degrees
|
|
|
|
*/
|
2021-04-28 22:16:45 +00:00
|
|
|
class FunctionS2ToGeo : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2021-07-08 15:31:39 +00:00
|
|
|
static constexpr auto name = "s2ToGeo";
|
2021-04-28 22:16:45 +00:00
|
|
|
|
|
|
|
static FunctionPtr create(ContextPtr)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionS2ToGeo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2021-08-10 11:31:15 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
|
|
|
|
2021-04-28 22:16:45 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
const auto * arg = arguments[0].get();
|
|
|
|
|
2021-07-08 21:10:00 +00:00
|
|
|
if (!WhichDataType(arg).isUInt64())
|
2021-04-28 22:16:45 +00:00
|
|
|
throw Exception(
|
2021-07-06 17:10:53 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of argument {} of function {}. Must be Float64",
|
|
|
|
arg->getName(), 1, getName());
|
2021-04-28 22:16:45 +00:00
|
|
|
|
2021-07-07 15:27:57 +00:00
|
|
|
DataTypePtr element = std::make_shared<DataTypeFloat64>();
|
2021-04-28 22:16:45 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeTuple>(DataTypes{element, element});
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
|
|
|
{
|
|
|
|
const auto * col_id = arguments[0].column.get();
|
|
|
|
|
2021-07-08 15:31:39 +00:00
|
|
|
auto col_longitude = ColumnFloat64::create();
|
|
|
|
auto col_latitude = ColumnFloat64::create();
|
2021-04-28 22:16:45 +00:00
|
|
|
|
2021-07-08 15:31:39 +00:00
|
|
|
auto & longitude = col_longitude->getData();
|
|
|
|
longitude.reserve(input_rows_count);
|
2021-04-28 22:16:45 +00:00
|
|
|
|
2021-07-08 15:31:39 +00:00
|
|
|
auto & latitude = col_latitude->getData();
|
|
|
|
latitude.reserve(input_rows_count);
|
2021-04-28 22:16:45 +00:00
|
|
|
|
2021-07-06 11:34:33 +00:00
|
|
|
for (const auto row : collections::range(0, input_rows_count))
|
2021-04-28 22:16:45 +00:00
|
|
|
{
|
2021-07-12 11:49:17 +00:00
|
|
|
const auto id = S2CellId(col_id->getUInt(row));
|
2021-04-28 22:16:45 +00:00
|
|
|
|
2021-07-12 11:49:17 +00:00
|
|
|
if (!id.is_valid())
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Point is not valid");
|
|
|
|
|
|
|
|
S2Point point = id.ToPoint();
|
2021-04-28 22:16:45 +00:00
|
|
|
S2LatLng ll(point);
|
2021-07-06 11:34:33 +00:00
|
|
|
|
2021-07-08 15:31:39 +00:00
|
|
|
longitude.emplace_back(ll.lng().degrees());
|
|
|
|
latitude.emplace_back(ll.lat().degrees());
|
2021-04-28 22:16:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 15:31:39 +00:00
|
|
|
return ColumnTuple::create(Columns{std::move(col_longitude), std::move(col_latitude)});
|
2021-04-28 22:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-04 18:03:22 +00:00
|
|
|
void registerFunctionS2ToGeo(FunctionFactory & factory)
|
2021-04-28 22:16:45 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionS2ToGeo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2021-07-06 21:45:45 +00:00
|
|
|
|
|
|
|
#endif
|