-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} | ||
} |
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)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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))); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.