Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add h3_cell_to_latlng #3

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/java/com/foursquare/presto/h3/CellToLatLngFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.foursquare.presto.h3;

import static com.facebook.presto.common.type.DoubleType.DOUBLE;

import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.block.BlockBuilder;
import com.facebook.presto.common.type.StandardTypes;
import com.facebook.presto.spi.function.Description;
import com.facebook.presto.spi.function.ScalarFunction;
import com.facebook.presto.spi.function.SqlNullable;
import com.facebook.presto.spi.function.SqlType;
import com.uber.h3core.util.LatLng;

/**
* Wraps {@link com.uber.h3core.H3Core#cellToLatLng(long)}. Produces a row of latitude, longitude
* degrees.
*/
public final class CellToLatLngFunction {
@ScalarFunction(value = "h3_cell_to_latlng")
@Description("Convert H3 index to degrees lat/lng")
@SqlNullable
@SqlType("ARRAY(DOUBLE)")
public static Block cellToLatLng(@SqlType(StandardTypes.BIGINT) long h3) {
try {
LatLng latLng = H3Plugin.h3.cellToLatLng(h3);
// TODO: It would be nice to return this as a ROW(lat DOUBLE, lng DOUBLE)
// but that is blocked on https://github.com/prestodb/presto/issues/18494
// (determining how to build the Block to return)
BlockBuilder blockBuilder = DOUBLE.createFixedSizeBlockBuilder(2);
DOUBLE.writeDouble(blockBuilder, latLng.lat);
DOUBLE.writeDouble(blockBuilder, latLng.lng);
return blockBuilder.build();
} catch (Exception e) {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my info: Is it faster to catch the exception, or faster to validate the input?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching exceptions is generally very slow. uber/h3-java#107 tracks adding a no-throw API for performance.

}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/foursquare/presto/h3/H3Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static int longToInt(long l) {
@Override
public Set<Class<?>> getFunctions() {
return ImmutableSet.<Class<?>>builder()
.add(LatLngToCellFunction.class, CellToParentFunction.class)
.add(LatLngToCellFunction.class, CellToLatLngFunction.class, CellToParentFunction.class)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.foursquare.presto.h3;

import static com.foursquare.presto.h3.H3PluginTest.assertQueryResults;
import static com.foursquare.presto.h3.H3PluginTest.createQueryRunner;

import com.facebook.presto.testing.QueryRunner;
import com.google.common.collect.ImmutableList;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@TestInstance(Lifecycle.PER_CLASS)
public class CellToLatLngFunctionTest {
@Test
public void testCellToLatLng() {
try (QueryRunner queryRunner = createQueryRunner()) {
assertQueryResults(
queryRunner,
"SELECT h3_cell_to_latlng(578536630256664575)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Does Presto support any hexidecimal syntax? In general I think we should prefer human-readable cell indexes in tests if possible (easier to debug, etc).

Copy link
Contributor Author

@isaacbrodsky isaacbrodsky Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of a way to embed a hex literal integer in Presto SQL. (You could use a string literal and from_base)

ImmutableList.of(
ImmutableList.of(ImmutableList.of(2.300882111626747, -5.245390296777327))));

assertQueryResults(
queryRunner,
"SELECT h3_cell_to_latlng(null)",
ImmutableList.of(Collections.singletonList(null)));
assertQueryResults(
queryRunner,
"SELECT h3_cell_to_latlng(-1)",
ImmutableList.of(Collections.singletonList(null)));
}
}
}