mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-22 23:52:03 +00:00
add h3ToCenterChild function (#33313)
This commit is contained in:
parent
62441f0a0f
commit
4dd8b65a1c
@ -812,4 +812,41 @@ Result:
|
||||
└─────────────────────┘
|
||||
```
|
||||
|
||||
## h3ToCenterChild {#h3tocenterchild}
|
||||
|
||||
Returns the center child (finer) [H3](#h3index) index contained by given [H3](#h3index) at the given resolution.
|
||||
|
||||
**Syntax**
|
||||
|
||||
``` sql
|
||||
h3ToCenterChild(index, resolution)
|
||||
```
|
||||
|
||||
**Parameter**
|
||||
|
||||
- `index` — Hexagon index number. Type: [UInt64](../../../sql-reference/data-types/int-uint.md).
|
||||
- `resolution` — Index resolution. Range: `[0, 15]`. Type: [UInt8](../../../sql-reference/data-types/int-uint.md).
|
||||
|
||||
**Returned values**
|
||||
|
||||
- [H3](#h3index) index of the center child contained by given [H3](#h3index) at the given resolution.
|
||||
|
||||
Type: [UInt64](../../../sql-reference/data-types/int-uint.md).
|
||||
|
||||
**Example**
|
||||
|
||||
Query:
|
||||
|
||||
``` sql
|
||||
SELECT h3ToCenterChild(577023702256844799,1) AS centerToChild;
|
||||
```
|
||||
|
||||
Result:
|
||||
|
||||
``` text
|
||||
┌──────centerToChild─┐
|
||||
│ 581496515558637567 │
|
||||
└────────────────────┘
|
||||
```
|
||||
|
||||
[Original article](https://clickhouse.com/docs/en/sql-reference/functions/geo/h3) <!--hide-->
|
||||
|
115
src/Functions/h3ToCenterChild.cpp
Normal file
115
src/Functions/h3ToCenterChild.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
#include "config_functions.h"
|
||||
|
||||
#if USE_H3
|
||||
|
||||
#include <Columns/ColumnArray.h>
|
||||
#include <Columns/ColumnsNumber.h>
|
||||
#include <DataTypes/DataTypeArray.h>
|
||||
#include <DataTypes/DataTypesNumber.h>
|
||||
#include <Functions/FunctionFactory.h>
|
||||
#include <Functions/IFunction.h>
|
||||
#include <Common/typeid_cast.h>
|
||||
#include <IO/WriteHelpers.h>
|
||||
#include <base/range.h>
|
||||
|
||||
#include <constants.h>
|
||||
#include <h3api.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
namespace ErrorCodes
|
||||
{
|
||||
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
||||
extern const int ARGUMENT_OUT_OF_BOUND;
|
||||
extern const int ILLEGAL_COLUMN;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
class FunctionH3ToCenterChild : public IFunction
|
||||
{
|
||||
public:
|
||||
static constexpr auto name = "h3ToCenterChild";
|
||||
|
||||
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3ToCenterChild>(); }
|
||||
|
||||
std::string getName() const override { return name; }
|
||||
|
||||
size_t getNumberOfArguments() const override { return 2; }
|
||||
bool useDefaultImplementationForConstants() const override { return true; }
|
||||
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
||||
|
||||
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
||||
{
|
||||
const auto * arg = arguments[0].get();
|
||||
if (!WhichDataType(arg).isUInt64())
|
||||
throw Exception(
|
||||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
||||
"Illegal type {} of argument {} of function {}. Must be UInt64",
|
||||
arg->getName(), 1, getName());
|
||||
|
||||
arg = arguments[1].get();
|
||||
if (!WhichDataType(arg).isUInt8())
|
||||
throw Exception(
|
||||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
||||
"Illegal type {} of argument {} of function {}. Must be UInt8",
|
||||
arg->getName(), 2, getName());
|
||||
|
||||
return std::make_shared<DataTypeUInt64>();
|
||||
}
|
||||
|
||||
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
||||
{
|
||||
const auto * col_hindex = checkAndGetColumn<ColumnUInt64>(arguments[0].column.get());
|
||||
if (!col_hindex)
|
||||
throw Exception(
|
||||
ErrorCodes::ILLEGAL_COLUMN,
|
||||
"Illegal type {} of argument {} of function {}. Must be UInt64.",
|
||||
arguments[0].type->getName(),
|
||||
1,
|
||||
getName());
|
||||
const auto & data_hindex = col_hindex->getData();
|
||||
|
||||
const auto * col_resolution = checkAndGetColumn<ColumnUInt8>(arguments[1].column.get());
|
||||
if (!col_resolution)
|
||||
throw Exception(
|
||||
ErrorCodes::ILLEGAL_COLUMN,
|
||||
"Illegal type {} of argument {} of function {}. Must be UInt8.",
|
||||
arguments[0].type->getName(),
|
||||
1,
|
||||
getName());
|
||||
const auto & data_resolution = col_resolution->getData();
|
||||
|
||||
auto dst = ColumnVector<UInt64>::create();
|
||||
auto & dst_data = dst->getData();
|
||||
dst_data.resize(input_rows_count);
|
||||
|
||||
for (size_t row = 0; row < input_rows_count; ++row)
|
||||
{
|
||||
if (data_resolution[row] > MAX_H3_RES)
|
||||
throw Exception(
|
||||
ErrorCodes::ARGUMENT_OUT_OF_BOUND,
|
||||
"The argument 'resolution' ({}) of function {} is out of bounds because the maximum resolution in H3 library is {}",
|
||||
toString(data_resolution[row]),
|
||||
getName(),
|
||||
toString(MAX_H3_RES));
|
||||
|
||||
UInt64 res = cellToCenterChild(data_hindex[row], data_resolution[row]);
|
||||
|
||||
dst_data[row] = res;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
void registerFunctionH3ToCenterChild(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionH3ToCenterChild>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -36,6 +36,7 @@ void registerFunctionH3KRing(FunctionFactory &);
|
||||
void registerFunctionH3GetBaseCell(FunctionFactory &);
|
||||
void registerFunctionH3ToParent(FunctionFactory &);
|
||||
void registerFunctionH3ToChildren(FunctionFactory &);
|
||||
void registerFunctionH3ToCenterChild(FunctionFactory &);
|
||||
void registerFunctionH3IndexesAreNeighbors(FunctionFactory &);
|
||||
void registerFunctionStringToH3(FunctionFactory &);
|
||||
void registerFunctionH3ToString(FunctionFactory &);
|
||||
@ -96,6 +97,7 @@ void registerFunctionsGeo(FunctionFactory & factory)
|
||||
registerFunctionH3GetBaseCell(factory);
|
||||
registerFunctionH3ToParent(factory);
|
||||
registerFunctionH3ToChildren(factory);
|
||||
registerFunctionH3ToCenterChild(factory);
|
||||
registerFunctionH3IndexesAreNeighbors(factory);
|
||||
registerFunctionStringToH3(factory);
|
||||
registerFunctionH3ToString(factory);
|
||||
|
120
tests/queries/0_stateless/02155_h3_to_center_child.reference
Normal file
120
tests/queries/0_stateless/02155_h3_to_center_child.reference
Normal file
@ -0,0 +1,120 @@
|
||||
581496515558637567
|
||||
585996266895310847
|
||||
590499385486344191
|
||||
595002924984172543
|
||||
599506517095350271
|
||||
604010115783196671
|
||||
608513715293126655
|
||||
613017314905817087
|
||||
617520914531352575
|
||||
622024514158493695
|
||||
626528113785835519
|
||||
631031713413202431
|
||||
635535313040572479
|
||||
640038912667942919
|
||||
644542512295313408
|
||||
586018257127866367
|
||||
590521375718899711
|
||||
595024915216728063
|
||||
599528507327905791
|
||||
604032106015752191
|
||||
608535705525682175
|
||||
613039305138372607
|
||||
617542904763908095
|
||||
622046504391049215
|
||||
626550104018391039
|
||||
631053703645757951
|
||||
635557303273127999
|
||||
640060902900498439
|
||||
644564502527868928
|
||||
590524674253783039
|
||||
595028213751611391
|
||||
599531805862789119
|
||||
604035404550635519
|
||||
608539004060565503
|
||||
613042603673255935
|
||||
617546203298791423
|
||||
622049802925932543
|
||||
626553402553274367
|
||||
631057002180641279
|
||||
635560601808011327
|
||||
640064201435381767
|
||||
644567801062752256
|
||||
595028557348995071
|
||||
599532149460172799
|
||||
604035748148019199
|
||||
608539347657949183
|
||||
613042947270639615
|
||||
617546546896175103
|
||||
622050146523316223
|
||||
626553746150658047
|
||||
631057345778024959
|
||||
635560945405395007
|
||||
640064545032765447
|
||||
644568144660135936
|
||||
599532200999780351
|
||||
604035799687626751
|
||||
608539399197556735
|
||||
613042998810247167
|
||||
617546598435782655
|
||||
622050198062923775
|
||||
626553797690265599
|
||||
631057397317632511
|
||||
635560996945002559
|
||||
640064596572372999
|
||||
644568196199743488
|
||||
604035805056335871
|
||||
608539404566265855
|
||||
613043004178956287
|
||||
617546603804491775
|
||||
622050203431632895
|
||||
626553803058974719
|
||||
631057402686341631
|
||||
635561002313711679
|
||||
640064601941082119
|
||||
644568201568452608
|
||||
608539405371572223
|
||||
613043004984262655
|
||||
617546604609798143
|
||||
622050204236939263
|
||||
626553803864281087
|
||||
631057403491647999
|
||||
635561003119018047
|
||||
640064602746388487
|
||||
644568202373758976
|
||||
612640339485786111
|
||||
617143939111321599
|
||||
621647538738462719
|
||||
626151138365804543
|
||||
630654737993171455
|
||||
635158337620541503
|
||||
639661937247911943
|
||||
644165536875282432
|
||||
617143939115515903
|
||||
621647538742657023
|
||||
626151138369998847
|
||||
630654737997365759
|
||||
635158337624735807
|
||||
639661937252106247
|
||||
644165536879476736
|
||||
621647538742657023
|
||||
626151138369998847
|
||||
630654737997365759
|
||||
635158337624735807
|
||||
639661937252106247
|
||||
644165536879476736
|
||||
626151138369998847
|
||||
630654737997365759
|
||||
635158337624735807
|
||||
639661937252106247
|
||||
644165536879476736
|
||||
630654737997365759
|
||||
635158337624735807
|
||||
639661937252106247
|
||||
644165536879476736
|
||||
635158337624735807
|
||||
639661937252106247
|
||||
644165536879476736
|
||||
639661937252106247
|
||||
644165536879476736
|
||||
644165536879476736
|
135
tests/queries/0_stateless/02155_h3_to_center_child.sql
Normal file
135
tests/queries/0_stateless/02155_h3_to_center_child.sql
Normal file
@ -0,0 +1,135 @@
|
||||
-- Tags: no-fasttest
|
||||
|
||||
DROP TABLE IF EXISTS h3_indexes;
|
||||
|
||||
--Note: id column just exists to keep the test results sorted.
|
||||
-- Order is not guaranteed with h3_index or res columns as we test the same h3_index at various resolutions.
|
||||
CREATE TABLE h3_indexes (id UInt8, h3_index UInt64, res UInt8) ENGINE = Memory;
|
||||
|
||||
-- Test cases taken from fixture: https://github.com/uber/h3/blob/master/src/apps/testapps/testCellToCenterChild.c
|
||||
|
||||
INSERT INTO h3_indexes VALUES (1,577023702256844799,1);
|
||||
INSERT INTO h3_indexes VALUES (2,577023702256844799,2);
|
||||
INSERT INTO h3_indexes VALUES (3,577023702256844799,3);
|
||||
INSERT INTO h3_indexes VALUES (4,577023702256844799,4);
|
||||
INSERT INTO h3_indexes VALUES (5,577023702256844799,5);
|
||||
INSERT INTO h3_indexes VALUES (6,577023702256844799,6);
|
||||
INSERT INTO h3_indexes VALUES (7,577023702256844799,7);
|
||||
INSERT INTO h3_indexes VALUES (8,577023702256844799,8);
|
||||
INSERT INTO h3_indexes VALUES (9,577023702256844799,9);
|
||||
INSERT INTO h3_indexes VALUES (10,577023702256844799,10);
|
||||
INSERT INTO h3_indexes VALUES (11,577023702256844799,11);
|
||||
INSERT INTO h3_indexes VALUES (12,577023702256844799,12);
|
||||
INSERT INTO h3_indexes VALUES (13,577023702256844799,13);
|
||||
INSERT INTO h3_indexes VALUES (14,577023702256844799,14);
|
||||
INSERT INTO h3_indexes VALUES (15,577023702256844799,15);
|
||||
INSERT INTO h3_indexes VALUES (16,581518505791193087,2);
|
||||
INSERT INTO h3_indexes VALUES (17,581518505791193087,3);
|
||||
INSERT INTO h3_indexes VALUES (18,581518505791193087,4);
|
||||
INSERT INTO h3_indexes VALUES (19,581518505791193087,5);
|
||||
INSERT INTO h3_indexes VALUES (20,581518505791193087,6);
|
||||
INSERT INTO h3_indexes VALUES (21,581518505791193087,7);
|
||||
INSERT INTO h3_indexes VALUES (22,581518505791193087,8);
|
||||
INSERT INTO h3_indexes VALUES (23,581518505791193087,9);
|
||||
INSERT INTO h3_indexes VALUES (24,581518505791193087,10);
|
||||
INSERT INTO h3_indexes VALUES (25,581518505791193087,11);
|
||||
INSERT INTO h3_indexes VALUES (26,581518505791193087,12);
|
||||
INSERT INTO h3_indexes VALUES (27,581518505791193087,13);
|
||||
INSERT INTO h3_indexes VALUES (28,581518505791193087,14);
|
||||
INSERT INTO h3_indexes VALUES (29,581518505791193087,15);
|
||||
INSERT INTO h3_indexes VALUES (30,586021555662749695,3);
|
||||
INSERT INTO h3_indexes VALUES (31,586021555662749695,4);
|
||||
INSERT INTO h3_indexes VALUES (32,586021555662749695,5);
|
||||
INSERT INTO h3_indexes VALUES (33,586021555662749695,6);
|
||||
INSERT INTO h3_indexes VALUES (34,586021555662749695,7);
|
||||
INSERT INTO h3_indexes VALUES (35,586021555662749695,8);
|
||||
INSERT INTO h3_indexes VALUES (36,586021555662749695,9);
|
||||
INSERT INTO h3_indexes VALUES (37,586021555662749695,10);
|
||||
INSERT INTO h3_indexes VALUES (38,586021555662749695,11);
|
||||
INSERT INTO h3_indexes VALUES (39,586021555662749695,12);
|
||||
INSERT INTO h3_indexes VALUES (40,586021555662749695,13);
|
||||
INSERT INTO h3_indexes VALUES (41,586021555662749695,14);
|
||||
INSERT INTO h3_indexes VALUES (42,586021555662749695,15);
|
||||
INSERT INTO h3_indexes VALUES (43,590525017851166719,4);
|
||||
INSERT INTO h3_indexes VALUES (44,590525017851166719,5);
|
||||
INSERT INTO h3_indexes VALUES (45,590525017851166719,6);
|
||||
INSERT INTO h3_indexes VALUES (46,590525017851166719,7);
|
||||
INSERT INTO h3_indexes VALUES (47,590525017851166719,8);
|
||||
INSERT INTO h3_indexes VALUES (48,590525017851166719,9);
|
||||
INSERT INTO h3_indexes VALUES (49,590525017851166719,10);
|
||||
INSERT INTO h3_indexes VALUES (50,590525017851166719,11);
|
||||
INSERT INTO h3_indexes VALUES (51,590525017851166719,12);
|
||||
INSERT INTO h3_indexes VALUES (52,590525017851166719,13);
|
||||
INSERT INTO h3_indexes VALUES (53,590525017851166719,14);
|
||||
INSERT INTO h3_indexes VALUES (54,590525017851166719,15);
|
||||
INSERT INTO h3_indexes VALUES (55,595028608888602623,5);
|
||||
INSERT INTO h3_indexes VALUES (56,595028608888602623,6);
|
||||
INSERT INTO h3_indexes VALUES (57,595028608888602623,7);
|
||||
INSERT INTO h3_indexes VALUES (58,595028608888602623,8);
|
||||
INSERT INTO h3_indexes VALUES (59,595028608888602623,9);
|
||||
INSERT INTO h3_indexes VALUES (60,595028608888602623,10);
|
||||
INSERT INTO h3_indexes VALUES (61,595028608888602623,11);
|
||||
INSERT INTO h3_indexes VALUES (62,595028608888602623,12);
|
||||
INSERT INTO h3_indexes VALUES (63,595028608888602623,13);
|
||||
INSERT INTO h3_indexes VALUES (64,595028608888602623,14);
|
||||
INSERT INTO h3_indexes VALUES (65,595028608888602623,15);
|
||||
INSERT INTO h3_indexes VALUES (66,599532206368489471,6);
|
||||
INSERT INTO h3_indexes VALUES (67,599532206368489471,7);
|
||||
INSERT INTO h3_indexes VALUES (68,599532206368489471,8);
|
||||
INSERT INTO h3_indexes VALUES (69,599532206368489471,9);
|
||||
INSERT INTO h3_indexes VALUES (70,599532206368489471,10);
|
||||
INSERT INTO h3_indexes VALUES (71,599532206368489471,11);
|
||||
INSERT INTO h3_indexes VALUES (72,599532206368489471,12);
|
||||
INSERT INTO h3_indexes VALUES (73,599532206368489471,13);
|
||||
INSERT INTO h3_indexes VALUES (74,599532206368489471,14);
|
||||
INSERT INTO h3_indexes VALUES (75,599532206368489471,15);
|
||||
INSERT INTO h3_indexes VALUES (76,604035805861642239,7);
|
||||
INSERT INTO h3_indexes VALUES (77,604035805861642239,8);
|
||||
INSERT INTO h3_indexes VALUES (78,604035805861642239,9);
|
||||
INSERT INTO h3_indexes VALUES (79,604035805861642239,10);
|
||||
INSERT INTO h3_indexes VALUES (80,604035805861642239,11);
|
||||
INSERT INTO h3_indexes VALUES (81,604035805861642239,12);
|
||||
INSERT INTO h3_indexes VALUES (82,604035805861642239,13);
|
||||
INSERT INTO h3_indexes VALUES (83,604035805861642239,14);
|
||||
INSERT INTO h3_indexes VALUES (84,604035805861642239,15);
|
||||
INSERT INTO h3_indexes VALUES (85,608136739873095679,8);
|
||||
INSERT INTO h3_indexes VALUES (86,608136739873095679,9);
|
||||
INSERT INTO h3_indexes VALUES (87,608136739873095679,10);
|
||||
INSERT INTO h3_indexes VALUES (88,608136739873095679,11);
|
||||
INSERT INTO h3_indexes VALUES (89,608136739873095679,12);
|
||||
INSERT INTO h3_indexes VALUES (90,608136739873095679,13);
|
||||
INSERT INTO h3_indexes VALUES (91,608136739873095679,14);
|
||||
INSERT INTO h3_indexes VALUES (92,608136739873095679,15);
|
||||
INSERT INTO h3_indexes VALUES (93,612640339489980415,9);
|
||||
INSERT INTO h3_indexes VALUES (94,612640339489980415,10);
|
||||
INSERT INTO h3_indexes VALUES (95,612640339489980415,11);
|
||||
INSERT INTO h3_indexes VALUES (96,612640339489980415,12);
|
||||
INSERT INTO h3_indexes VALUES (97,612640339489980415,13);
|
||||
INSERT INTO h3_indexes VALUES (98,612640339489980415,14);
|
||||
INSERT INTO h3_indexes VALUES (99,612640339489980415,15);
|
||||
INSERT INTO h3_indexes VALUES (100,617143939115515903,10);
|
||||
INSERT INTO h3_indexes VALUES (101,617143939115515903,11);
|
||||
INSERT INTO h3_indexes VALUES (102,617143939115515903,12);
|
||||
INSERT INTO h3_indexes VALUES (103,617143939115515903,13);
|
||||
INSERT INTO h3_indexes VALUES (104,617143939115515903,14);
|
||||
INSERT INTO h3_indexes VALUES (105,617143939115515903,15);
|
||||
INSERT INTO h3_indexes VALUES (106,621647538742657023,11);
|
||||
INSERT INTO h3_indexes VALUES (107,621647538742657023,12);
|
||||
INSERT INTO h3_indexes VALUES (108,621647538742657023,13);
|
||||
INSERT INTO h3_indexes VALUES (109,621647538742657023,14);
|
||||
INSERT INTO h3_indexes VALUES (110,621647538742657023,15);
|
||||
INSERT INTO h3_indexes VALUES (111,626151138369998847,12);
|
||||
INSERT INTO h3_indexes VALUES (112,626151138369998847,13);
|
||||
INSERT INTO h3_indexes VALUES (113,626151138369998847,14);
|
||||
INSERT INTO h3_indexes VALUES (114,626151138369998847,15);
|
||||
INSERT INTO h3_indexes VALUES (115,630654737997365759,13);
|
||||
INSERT INTO h3_indexes VALUES (116,630654737997365759,14);
|
||||
INSERT INTO h3_indexes VALUES (117,630654737997365759,15);
|
||||
INSERT INTO h3_indexes VALUES (118,635158337624735807,14);
|
||||
INSERT INTO h3_indexes VALUES (119,635158337624735807,15);
|
||||
INSERT INTO h3_indexes VALUES (120,639661937252106247,15);
|
||||
|
||||
|
||||
SELECT h3ToCenterChild(h3_index,res) FROM h3_indexes ORDER BY id;
|
||||
|
||||
DROP TABLE h3_indexes;
|
Loading…
Reference in New Issue
Block a user