Skip to content

Commit e716a9d

Browse files
jagillfacebook-github-bot
authored andcommitted
feat: Implement parent/child BingTile functions (facebookincubator#12708)
Summary: Pull Request resolved: facebookincubator#12708 These functions allow users to find the parent or children tiles of a BingTile. Reviewed By: bikramSingh91 Differential Revision: D71413543 fbshipit-source-id: bcfa7f5a175362d22ff3542d1b3e0b922eb1ff18
1 parent 7d05ec7 commit e716a9d

File tree

8 files changed

+497
-11
lines changed

8 files changed

+497
-11
lines changed

velox/docs/functions/presto/geospatial.rst

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,31 @@ for more details.
3232

3333
.. function:: bing_tile_coordinates(tile: BingTile) -> coords: row(integer,integer)
3434

35-
Returns the `x`, `y` coordinates of a given Bing tile as `row(x, y)`.
35+
Returns the `x`, `y` coordinates of a given Bing tile as `row(x, y)`.
3636

3737
.. function:: bing_tile_zoom_level(tile: BingTile) -> zoom_level: tinyint
3838

39-
Returns the zoom level of a given Bing tile.
39+
Returns the zoom level of a given Bing tile.
40+
41+
.. function:: bing_tile_parent(tile) -> parent: BingTile
42+
43+
Returns the parent of the Bing tile at one lower zoom level. Throws an
44+
exception if tile is at zoom level 0.
45+
46+
.. function:: bing_tile_parent(tile, parentZoom) -> parent: BingTile
47+
48+
Returns the parent of the Bing tile at the specified lower zoom level.
49+
Throws an exception if parentZoom is less than 0, or parentZoom is greater
50+
than the tile’s zoom.
51+
52+
.. function:: bing_tile_children(tile) -> children: array(BingTile)
53+
54+
Returns the children of the Bing tile at one higher zoom level. Throws an
55+
exception if tile is at max zoom level.
56+
57+
.. function:: bing_tile_children(tile, childZoom) -> children: array(BingTile)
58+
59+
Returns the children of the Bing tile at the specified higher zoom level.
60+
Throws an exception if childZoom is greater than the max zoom level, or
61+
childZoom is less than the tile’s zoom. The order is deterministic but not
62+
specified.

velox/expression/fuzzer/ExpressionFuzzerTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ int main(int argc, char** argv) {
118118
// BingTiles throw VeloxUserError when zoom/x/y are out of range.
119119
"bing_tile",
120120
"bing_tile_zoom_level",
121-
"bing_tile_coordinates"};
121+
"bing_tile_coordinates",
122+
"bing_tile_parent",
123+
"bing_tile_children"};
122124
size_t initialSeed = FLAGS_seed == 0 ? std::time(nullptr) : FLAGS_seed;
123125

124126
std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>

velox/functions/prestosql/GeospatialFunctions.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,84 @@ struct BingTileCoordinatesFunction {
9191
}
9292
};
9393

94+
template <typename T>
95+
struct BingTileParentFunction {
96+
VELOX_DEFINE_FUNCTION_TYPES(T);
97+
98+
FOLLY_ALWAYS_INLINE Status
99+
call(out_type<BingTile>& result, const arg_type<BingTile>& tile) {
100+
uint64_t tileInt = tile;
101+
uint8_t tileZoom = BingTileType::bingTileZoom(tile);
102+
if (FOLLY_UNLIKELY(tileZoom == 0)) {
103+
return Status::UserError(
104+
fmt::format("Cannot call bing_tile_parent on zoom 0 tile"));
105+
}
106+
auto parent = BingTileType::bingTileParent(tileInt, tileZoom - 1);
107+
if (FOLLY_UNLIKELY(parent.hasError())) {
108+
return Status::UserError(parent.error());
109+
}
110+
result = parent.value();
111+
return Status::OK();
112+
}
113+
114+
FOLLY_ALWAYS_INLINE Status call(
115+
out_type<BingTile>& result,
116+
const arg_type<BingTile>& tile,
117+
const arg_type<int8_t>& parentZoom) {
118+
uint64_t tileInt = tile;
119+
if (FOLLY_UNLIKELY(parentZoom < 0)) {
120+
return Status::UserError(
121+
fmt::format("Cannot call bing_tile_parent with negative zoom"));
122+
}
123+
auto parent = BingTileType::bingTileParent(tileInt, parentZoom);
124+
if (FOLLY_UNLIKELY(parent.hasError())) {
125+
return Status::UserError(parent.error());
126+
}
127+
result = parent.value();
128+
return Status::OK();
129+
}
130+
};
131+
132+
template <typename T>
133+
struct BingTileChildrenFunction {
134+
VELOX_DEFINE_FUNCTION_TYPES(T);
135+
136+
FOLLY_ALWAYS_INLINE Status
137+
call(out_type<Array<BingTile>>& result, const arg_type<BingTile>& tile) {
138+
uint64_t tileInt = tile;
139+
uint8_t tileZoom = BingTileType::bingTileZoom(tile);
140+
if (FOLLY_UNLIKELY(tileZoom >= BingTileType::kBingTileMaxZoomLevel)) {
141+
return Status::UserError(
142+
fmt::format("Cannot call bing_tile_children on zoom 23 tile"));
143+
}
144+
auto childrenRes = BingTileType::bingTileChildren(tileInt, tileZoom + 1);
145+
if (FOLLY_UNLIKELY(childrenRes.hasError())) {
146+
return Status::UserError(childrenRes.error());
147+
}
148+
std::vector<uint64_t> children = childrenRes.value();
149+
result.reserve(children.size());
150+
result.add_items(children);
151+
return Status::OK();
152+
}
153+
154+
FOLLY_ALWAYS_INLINE Status call(
155+
out_type<Array<BingTile>>& result,
156+
const arg_type<BingTile>& tile,
157+
const arg_type<int8_t>& childZoom) {
158+
uint64_t tileInt = tile;
159+
if (FOLLY_UNLIKELY(childZoom < 0)) {
160+
return Status::UserError(
161+
fmt::format("Cannot call bing_tile_children with negative zoom"));
162+
}
163+
auto childrenRes = BingTileType::bingTileChildren(tileInt, childZoom);
164+
if (FOLLY_UNLIKELY(childrenRes.hasError())) {
165+
return Status::UserError(childrenRes.error());
166+
}
167+
std::vector<uint64_t> children = childrenRes.value();
168+
result.reserve(children.size());
169+
result.add_items(children);
170+
return Status::OK();
171+
}
172+
};
173+
94174
} // namespace facebook::velox::functions

velox/functions/prestosql/registration/GeospatialFunctionsRegistration.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ void registerBingTileFunctions(const std::string& prefix) {
3737
BingTileCoordinatesFunction,
3838
Row<int32_t, int32_t>,
3939
BingTile>({prefix + "bing_tile_coordinates"});
40+
41+
// Parent/child tiles
42+
registerFunction<BingTileParentFunction, BingTile, BingTile>(
43+
{prefix + "bing_tile_parent"});
44+
registerFunction<BingTileParentFunction, BingTile, BingTile, int8_t>(
45+
{prefix + "bing_tile_parent"});
46+
registerFunction<BingTileChildrenFunction, Array<BingTile>, BingTile>(
47+
{prefix + "bing_tile_children"});
48+
registerFunction<BingTileChildrenFunction, Array<BingTile>, BingTile, int8_t>(
49+
{prefix + "bing_tile_children"});
4050
}
4151

4252
} // namespace

0 commit comments

Comments
 (0)