-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support common format geo point (#2801)
--------- Signed-off-by: panguixin <panguixin@bytedance.com> (cherry picked from commit 82ef68e)
- Loading branch information
1 parent
992e2f5
commit db18ca5
Showing
10 changed files
with
256 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
integ-test/src/test/java/org/opensearch/sql/sql/GeopointFormatsIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.sql; | ||
|
||
import static org.opensearch.sql.util.MatcherUtils.rows; | ||
import static org.opensearch.sql.util.MatcherUtils.schema; | ||
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; | ||
import static org.opensearch.sql.util.MatcherUtils.verifySchema; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.sql.legacy.SQLIntegTestCase; | ||
|
||
public class GeopointFormatsIT extends SQLIntegTestCase { | ||
|
||
@Override | ||
public void init() throws Exception { | ||
loadIndex(Index.GEOPOINTS); | ||
} | ||
|
||
@Test | ||
public void testReadingGeopoints() throws IOException { | ||
String query = String.format("SELECT point FROM %s LIMIT 5", Index.GEOPOINTS.getName()); | ||
JSONObject result = executeJdbcRequest(query); | ||
verifySchema(result, schema("point", null, "geo_point")); | ||
verifyDataRows( | ||
result, | ||
rows(Map.of("lon", 74, "lat", 40.71)), | ||
rows(Map.of("lon", 74, "lat", 40.71)), | ||
rows(Map.of("lon", 74, "lat", 40.71)), | ||
rows(Map.of("lon", 74, "lat", 40.71)), | ||
rows(Map.of("lon", 74, "lat", 40.71))); | ||
} | ||
|
||
private static final double TOLERANCE = 1E-5; | ||
|
||
public void testReadingGeoHash() throws IOException { | ||
String query = String.format("SELECT point FROM %s WHERE _id='6'", Index.GEOPOINTS.getName()); | ||
JSONObject result = executeJdbcRequest(query); | ||
verifySchema(result, schema("point", null, "geo_point")); | ||
Pair<Double, Double> point = getGeoValue(result); | ||
assertEquals(40.71, point.getLeft(), TOLERANCE); | ||
assertEquals(74, point.getRight(), TOLERANCE); | ||
} | ||
|
||
private Pair<Double, Double> getGeoValue(JSONObject result) { | ||
JSONObject geoRaw = | ||
(JSONObject) ((JSONArray) ((JSONArray) result.get("datarows")).get(0)).get(0); | ||
double lat = geoRaw.getDouble("lat"); | ||
double lon = geoRaw.getDouble("lon"); | ||
return Pair.of(lat, lon); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{"index": {"_id": "1"}} | ||
{"point": {"lat": 40.71, "lon": 74.00}} | ||
{"index": {"_id": "2"}} | ||
{"point": "40.71,74.00"} | ||
{"index": {"_id": "3"}} | ||
{"point": [74.00, 40.71]} | ||
{"index": {"_id": "4"}} | ||
{"point": "POINT (74.00 40.71)"} | ||
{"index": {"_id": "5"}} | ||
{"point": {"type": "Point", "coordinates": [74.00, 40.71]}} | ||
{"index": {"_id": "6"}} | ||
{"point": "txhxegj0uyp3"} |
9 changes: 9 additions & 0 deletions
9
integ-test/src/test/resources/indexDefinitions/geopoint_index_mapping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"mappings": { | ||
"properties": { | ||
"point": { | ||
"type": "geo_point" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...rch/src/test/java/org/opensearch/sql/opensearch/data/utils/OpenSearchJsonContentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.data.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.OpenSearchParseException; | ||
|
||
public class OpenSearchJsonContentTest { | ||
@Test | ||
public void testGetValueWithIOException() throws IOException { | ||
JsonNode jsonNode = mock(JsonNode.class); | ||
JsonParser jsonParser = mock(JsonParser.class); | ||
when(jsonNode.traverse()).thenReturn(jsonParser); | ||
when(jsonParser.nextToken()).thenThrow(new IOException()); | ||
OpenSearchJsonContent content = new OpenSearchJsonContent(jsonNode); | ||
OpenSearchParseException exception = | ||
assertThrows(OpenSearchParseException.class, content::geoValue); | ||
assertTrue(exception.getMessage().contains("error parsing geo point")); | ||
} | ||
} |
Oops, something went wrong.