Skip to content

Commit

Permalink
add IT
Browse files Browse the repository at this point in the history
Signed-off-by: panguixin <panguixin@bytedance.com>
  • Loading branch information
bugmakerrrrrr committed Jul 18, 2024
1 parent 634d577 commit 0de937f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.opensearch.sql.legacy.TestUtils.getDogs3IndexMapping;
import static org.opensearch.sql.legacy.TestUtils.getEmployeeNestedTypeIndexMapping;
import static org.opensearch.sql.legacy.TestUtils.getGameOfThronesIndexMapping;
import static org.opensearch.sql.legacy.TestUtils.getGeopointIndexMapping;
import static org.opensearch.sql.legacy.TestUtils.getJoinTypeIndexMapping;
import static org.opensearch.sql.legacy.TestUtils.getLocationIndexMapping;
import static org.opensearch.sql.legacy.TestUtils.getMappingFile;
Expand Down Expand Up @@ -724,7 +725,12 @@ public enum Index {
TestsConstants.TEST_INDEX_NESTED_WITH_NULLS,
"multi_nested",
getNestedTypeIndexMapping(),
"src/test/resources/nested_with_nulls.json");
"src/test/resources/nested_with_nulls.json"),
GEOPOINTS(
TestsConstants.TEST_INDEX_GEOPOINT,
"dates",
getGeopointIndexMapping(),
"src/test/resources/geopoints.json");

private final String name;
private final String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ public static String getDataTypeNonnumericIndexMapping() {
return getMappingFile(mappingFile);
}

public static String getGeopointIndexMapping() {
String mappingFile = "geopoint_index_mapping.json";
return getMappingFile(mappingFile);
}

public static void loadBulk(Client client, String jsonPath, String defaultIndex)
throws Exception {
System.out.println(String.format("Loading file %s into opensearch cluster", jsonPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class TestsConstants {
public static final String TEST_INDEX_WILDCARD = TEST_INDEX + "_wildcard";
public static final String TEST_INDEX_MULTI_NESTED_TYPE = TEST_INDEX + "_multi_nested";
public static final String TEST_INDEX_NESTED_WITH_NULLS = TEST_INDEX + "_nested_with_nulls";
public static final String TEST_INDEX_GEOPOINT = TEST_INDEX + "_geopoint";
public static final String DATASOURCES = ".ql-datasources";

public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
Expand Down
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);
}
}
12 changes: 12 additions & 0 deletions integ-test/src/test/resources/geopoints.json
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"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"mappings": {
"properties": {
"point": {
"type": "geo_point"
}
}
}
}

0 comments on commit 0de937f

Please sign in to comment.