Skip to content

Commit f5029f7

Browse files
committed
test: Add E2E tests for geo functions
1 parent 8bfb3f3 commit f5029f7

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

presto-native-execution/src/test/java/com/facebook/presto/nativeworker/NativeQueryRunnerUtils.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,48 @@ public static void createBucketedLineitemAndOrders(QueryRunner queryRunner)
487487
queryRunner.execute("INSERT INTO orders_bucketed SELECT orderkey, custkey, orderstatus, '2021-12-21' FROM tpch.tiny.orders");
488488
}
489489
}
490+
491+
public static void createCoordinates(QueryRunner queryRunner)
492+
{
493+
queryRunner.execute("DROP TABLE IF EXISTS coordinates");
494+
queryRunner.execute("CREATE TABLE coordinates (lat1 double, lon1 double, lat2 double, lon2 double, zoom integer, isvalid boolean)");
495+
queryRunner.execute("INSERT INTO coordinates VALUES " +
496+
"(0.0, 0.0, NULL, NULL, 0, true), " +
497+
"(30.12, 60, NULL, NULL, 1, true), " +
498+
"(30.12, 60, NULL, NULL, 15, true), " +
499+
"(30.12, 60, NULL, NULL, 23, true), " +
500+
"(-85.05112878, -180, NULL, NULL, 1, true), " +
501+
"(-85.05112878, -180, NULL, NULL, 3, true), " +
502+
"(-85.05112878, -180, NULL, NULL, 15, true), " +
503+
"(0.0, 180.0, NULL, NULL, 2, true), " +
504+
"(0.0, -180.0, NULL, NULL, 2, true), " +
505+
"(85.0, 0.0, NULL, NULL, 3, true), " +
506+
"(-85.0, 0.0, NULL, NULL, 3, true), " +
507+
"(85.0, 180.0, NULL, NULL, 0, true), " +
508+
"(-85.0, -180.0, NULL, NULL, 0, true), " +
509+
"(45.0, 90.0, NULL, NULL, 10, true), " +
510+
"(-45.0, -90.0, NULL, NULL, 10, true), " +
511+
"(40.7128, -74.0060, NULL, NULL, 15, true), " +
512+
"(51.5074, -0.1278, NULL, NULL, 20, true), " +
513+
"(90.0, 0.0, NULL, NULL, 1, false), " +
514+
"(0.0, 200.0, NULL, NULL, 1, false), " +
515+
"(0.0, 0.0, NULL, NULL, 24, false), " +
516+
"(0.0, 0.0, NULL, NULL, -1, false), " +
517+
"(0.0, 0.0, NULL, NULL, 2, true), " +
518+
"(40.7128, -74.0060, NULL, NULL, 5, true), " +
519+
"(90.0, 0.0, NULL, NULL, 2, false), " +
520+
"(0.0, -200.0, NULL, NULL, 2, false), " +
521+
"(0.0, 0.0, NULL, NULL, -1, false), " +
522+
"(90.0, 0.0, -90.0, 0.0, NULL, true), " +
523+
"(0.0, 179.0, 0.0, -179.0, NULL, true), " +
524+
"(37.7749, -122.4194, 37.7750, -122.4195, NULL, true), " +
525+
"(0.0, 0.0, 0.0, 180.0, NULL, true), " +
526+
"(40.7128, -74.0060, 51.5074, -0.1278, NULL, true), " +
527+
"(-33.8688, 151.2093, 35.6762, 139.6503, NULL, true), " +
528+
"(37.7749, -122.4194, 34.0522, -118.2437, NULL, true), " +
529+
"(10.0, 0.0, -10.0, 0.0, NULL, true), " +
530+
"(45.0, -5.0, 45.0, 5.0, NULL, true), " +
531+
"(100.0, 0.0, 0.0, 0.0, NULL, false), " +
532+
"(0.0, 200.0, 0.0, 0.0, NULL, false)");
533+
}
490534
}

presto-native-tests/src/test/java/com/facebook/presto/nativetests/TestPrestoNativeGeospatial.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,25 @@
1818
import com.facebook.presto.testing.ExpectedQueryRunner;
1919
import com.facebook.presto.testing.QueryRunner;
2020
import com.facebook.presto.tests.AbstractTestQueryFramework;
21+
import com.google.common.collect.ImmutableList;
2122
import org.intellij.lang.annotations.Language;
2223
import org.testng.annotations.Test;
2324

25+
import java.util.UUID;
26+
27+
import static com.facebook.presto.common.type.BigintType.BIGINT;
28+
import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createCoordinates;
29+
import static java.lang.String.format;
30+
2431
public class TestPrestoNativeGeospatial
2532
extends AbstractTestQueryFramework
2633
{
34+
@Override
35+
protected void createTables()
36+
{
37+
QueryRunner queryRunner = (QueryRunner) getExpectedQueryRunner();
38+
createCoordinates(queryRunner);
39+
}
2740
@Override
2841
protected QueryRunner createQueryRunner()
2942
throws Exception
@@ -58,4 +71,73 @@ public void setSessionNativeUseVeloxGeospatialJoin()
5871
.build();
5972
assertQuery(actualSession, query);
6073
}
74+
75+
@Test
76+
public void testBingTileAt()
77+
{
78+
assertQuery("SELECT bing_tile_at(lat1, lon1, zoom) FROM coordinates WHERE isvalid = true");
79+
assertQueryFails("SELECT bing_tile_at(lat1, lon1, zoom) FROM coordinates WHERE lat1 = 90.0 AND zoom = 1",
80+
"(?s).*Latitude.*outside of valid range.*-85.05112878.*85.05112878.*");
81+
assertQueryFails("SELECT bing_tile_at(lat1, lon1, zoom) FROM coordinates WHERE lon1 = 200.0 AND zoom = 1",
82+
"(?s).*Longitude.*outside of valid range.*-180.*180.*");
83+
assertQueryFails("SELECT bing_tile_at(lat1, lon1, zoom) FROM coordinates WHERE zoom = 24",
84+
"(?s).*zoom.*23.*");
85+
assertQueryFails("SELECT bing_tile_at(lat1, lon1, zoom) FROM coordinates WHERE zoom = -1",
86+
"(?s).*zoom.*negative.*");
87+
}
88+
89+
@Test
90+
public void testBingTilePolygon()
91+
{
92+
String tmpTableName = generateRandomTableName();
93+
getQueryRunner().execute(format("CREATE TABLE %s " +
94+
"(x integer, y integer, z integer, isvalid boolean)", tmpTableName));
95+
getQueryRunner().execute(format("INSERT INTO %s VALUES " +
96+
"(0, 0, 1, true)," +
97+
"(3, 3, 3, true)," +
98+
"(7, 7, 4, true)," +
99+
"(0, 0, 5, true)," +
100+
"(31, 31, 5, true)," +
101+
"(100, 100, 10, true)," +
102+
"(2, 0, 1, false) ", tmpTableName));
103+
assertQuery(format("SELECT bing_tile_polygon(BING_TILE(x, y, z)) FROM %s WHERE isvalid = true", tmpTableName));
104+
assertQueryFails(format("SELECT bing_tile_polygon(BING_TILE(x, y, z)) FROM %s WHERE isvalid = false", tmpTableName),
105+
"(?s).*X coordinate.*greater than max coordinate.*zoom.*");
106+
}
107+
108+
@Test
109+
public void testBingTilesAround()
110+
{
111+
assertQuery("SELECT cardinality(bing_tiles_around(lat1, lon1, zoom)) FROM coordinates WHERE isvalid = true");
112+
assertQueryFails("SELECT bing_tiles_around(lat1, lon1, zoom) FROM coordinates WHERE lat1 = 90.0 AND zoom = 2",
113+
"(?s).*Latitude.*outside of valid range.*-85.05112878.*85.05112878.*");
114+
assertQueryFails("SELECT bing_tiles_around(lat1, lon1, zoom) FROM coordinates WHERE lon1 = -200.0 AND zoom = 2",
115+
"(?s).*Longitude.*outside of valid range.*-180.*180.*");
116+
assertQueryFails("SELECT bing_tiles_around(lat1, lon1, zoom) FROM coordinates WHERE zoom = -1",
117+
"(?s).*zoom.*negative.*");
118+
}
119+
120+
@Test
121+
public void testGreatCircleDistance()
122+
{
123+
assertQuery("SELECT great_circle_distance(lat1, lon1, lat2, lon2) FROM coordinates WHERE lat2 IS NOT NULL AND isvalid = true");
124+
assertQueryFails("SELECT great_circle_distance(lat1, lon1, lat2, lon2) FROM coordinates WHERE lat1 = 100.0",
125+
"(?s).*Latitude.*range.*-90.*90.*");
126+
assertQueryFails("SELECT great_circle_distance(lat1, lon1, lat2, lon2) FROM coordinates WHERE lon1 = 200.0",
127+
"(?s).*longitude.*range.*-180.*180.*");
128+
}
129+
130+
private String generateRandomTableName()
131+
{
132+
String tableName = "tmp_presto_" + UUID.randomUUID().toString().replace("-", "");
133+
// Clean up if the temporary named table already exists.
134+
dropTableIfExists(tableName);
135+
return tableName;
136+
}
137+
138+
private void dropTableIfExists(String tableName)
139+
{
140+
// An ugly workaround for the lack of getExpectedQueryRunner()
141+
computeExpected(format("DROP TABLE IF EXISTS %s", tableName), ImmutableList.of(BIGINT));
142+
}
61143
}

presto-tests/src/main/java/com/facebook/presto/tests/TestingPrestoClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import static com.facebook.presto.common.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE;
8282
import static com.facebook.presto.common.type.TinyintType.TINYINT;
8383
import static com.facebook.presto.common.type.VarbinaryType.VARBINARY;
84+
import static com.facebook.presto.geospatial.type.BingTileType.BING_TILE;
8485
import static com.facebook.presto.testing.MaterializedResult.DEFAULT_PRECISION;
8586
import static com.facebook.presto.type.IntervalDayTimeType.INTERVAL_DAY_TIME;
8687
import static com.facebook.presto.type.IntervalYearMonthType.INTERVAL_YEAR_MONTH;
@@ -250,6 +251,9 @@ else if (IPADDRESS.equals(type)) {
250251
else if (IPPREFIX.equals(type)) {
251252
return value;
252253
}
254+
else if (BING_TILE.equals(type)) {
255+
return value;
256+
}
253257
else if (type instanceof ArrayType) {
254258
return ((List<Object>) value).stream()
255259
.map(element -> convertToRowValue(((ArrayType) type).getElementType(), element))

0 commit comments

Comments
 (0)