From 983d77dbfd7ba83c7641057f49164a9d4cf56800 Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Wed, 16 Aug 2023 15:27:30 -0700 Subject: [PATCH 1/2] [Spotless] Applying Google Code Format for integ-tests #9 (#1966) * Add spotless apply 60 files. (#328) rebase changes. Addressed PR comments. Signed-off-by: Mitchell Gale * Apply suggestions from code review Co-authored-by: Yury-Fridlyand Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale * Update integ-test/src/test/java/org/opensearch/sql/legacy/QueryIT.java Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale * Update integ-test/src/test/java/org/opensearch/sql/legacy/QueryIT.java Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale * Update integ-test/src/test/java/org/opensearch/sql/legacy/QueryIT.java Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale * Update integ-test/src/test/java/org/opensearch/sql/legacy/QueryIT.java Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale * Update integ-test/src/test/java/org/opensearch/sql/legacy/QueryIT.java Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale * Apply suggestions from code review Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale * Apply suggestions from code review Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale * Apply suggestions from code review Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale * Apply suggestions from code review Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale * Update integ-test/src/test/java/org/opensearch/sql/legacy/QueryIT.java Co-authored-by: Guian Gumpac Signed-off-by: Mitchell Gale --------- Signed-off-by: Mitchell Gale Signed-off-by: Mitchell Gale Co-authored-by: Yury-Fridlyand Co-authored-by: Guian Gumpac --- integ-test/build.gradle | 5 + .../sql/bwc/SQLBackwardsCompatibilityIT.java | 2 - .../connection/OpenSearchConnection.java | 16 +- .../sql/correctness/runner/resultset/Row.java | 6 +- .../tests/OpenSearchConnectionTest.java | 33 +- .../sql/correctness/tests/RowTest.java | 6 +- .../sql/legacy/NestedFieldQueryIT.java | 486 +++--- .../sql/legacy/ObjectFieldSelectIT.java | 63 +- .../sql/legacy/OpenSearchSQLRestTestCase.java | 134 +- .../org/opensearch/sql/legacy/OrderIT.java | 39 +- .../sql/legacy/OrdinalAliasRewriterIT.java | 219 ++- .../org/opensearch/sql/legacy/PluginIT.java | 586 ++++--- .../sql/legacy/PreparedStatementIT.java | 86 +- .../sql/legacy/PrettyFormatResponseIT.java | 328 ++-- .../sql/legacy/PrettyFormatterIT.java | 11 +- .../sql/legacy/QueryAnalysisIT.java | 105 +- .../sql/legacy/QueryFunctionsIT.java | 157 +- .../org/opensearch/sql/legacy/QueryIT.java | 1551 ++++++++++------- .../sql/legacy/RestIntegTestCase.java | 3 + .../opensearch/sql/legacy/SQLFunctionsIT.java | 666 ++++--- .../sql/legacy/SQLIntegTestCase.java | 226 +-- .../org/opensearch/sql/legacy/ShowIT.java | 4 +- .../opensearch/sql/legacy/SourceFieldIT.java | 58 +- .../opensearch/sql/ppl/NowLikeFunctionIT.java | 207 ++- .../sql/ppl/ObjectFieldOperateIT.java | 67 +- .../org/opensearch/sql/ppl/OperatorIT.java | 116 +- .../opensearch/sql/ppl/PPLIntegTestCase.java | 22 +- .../org/opensearch/sql/ppl/PPLPluginIT.java | 12 +- .../opensearch/sql/ppl/ParseCommandIT.java | 22 +- .../sql/ppl/PositionFunctionIT.java | 162 +- .../ppl/PrometheusDataSourceCommandsIT.java | 143 +- .../opensearch/sql/ppl/QueryAnalysisIT.java | 16 +- .../org/opensearch/sql/ppl/QueryStringIT.java | 27 +- .../org/opensearch/sql/ppl/RareCommandIT.java | 11 +- .../sql/ppl/RelevanceFunctionIT.java | 68 +- .../opensearch/sql/ppl/RenameCommandIT.java | 5 +- .../opensearch/sql/ppl/ResourceMonitorIT.java | 9 +- .../opensearch/sql/ppl/SearchCommandIT.java | 1 - .../org/opensearch/sql/ppl/SettingsIT.java | 9 +- .../sql/ppl/ShowDataSourcesCommandIT.java | 35 +- .../sql/ppl/SimpleQueryStringIT.java | 34 +- .../org/opensearch/sql/ppl/SortCommandIT.java | 1 - .../java/org/opensearch/sql/sql/NestedIT.java | 388 +++-- .../opensearch/sql/sql/NowLikeFunctionIT.java | 195 ++- .../org/opensearch/sql/sql/NullLiteralIT.java | 13 +- .../sql/sql/PaginationBlackboxIT.java | 37 +- .../sql/sql/PaginationFallbackIT.java | 36 +- .../sql/sql/PaginationFilterIT.java | 106 +- .../org/opensearch/sql/sql/PaginationIT.java | 55 +- .../sql/sql/PaginationWindowIT.java | 32 +- .../sql/sql/PositionFunctionIT.java | 90 +- .../sql/sql/PreparedStatementIT.java | 27 +- .../java/org/opensearch/sql/sql/QueryIT.java | 130 +- .../org/opensearch/sql/sql/QueryStringIT.java | 57 +- .../opensearch/sql/sql/QueryValidationIT.java | 36 +- .../org/opensearch/sql/sql/RawFormatIT.java | 27 +- .../sql/sql/RelevanceFunctionIT.java | 64 +- .../opensearch/sql/sql/SQLCorrectnessIT.java | 31 +- .../org/opensearch/sql/sql/ScoreQueryIT.java | 76 +- .../sql/sql/SimpleQueryStringIT.java | 47 +- 60 files changed, 3847 insertions(+), 3357 deletions(-) diff --git a/integ-test/build.gradle b/integ-test/build.gradle index b52cfd5f22..6ee9cb425e 100644 --- a/integ-test/build.gradle +++ b/integ-test/build.gradle @@ -53,6 +53,11 @@ repositories { } } +// Being ignored as a temporary measure before being removed in favour of +// spotless https://github.com/opensearch-project/sql/issues/1101 +checkstyleTest.ignoreFailures = true +checkstyleMain.ignoreFailures = true + ext { projectSubstitutions = [:] licenseFile = rootProject.file('LICENSE.TXT') diff --git a/integ-test/src/test/java/org/opensearch/sql/bwc/SQLBackwardsCompatibilityIT.java b/integ-test/src/test/java/org/opensearch/sql/bwc/SQLBackwardsCompatibilityIT.java index 799dadcd2d..f5c903d434 100644 --- a/integ-test/src/test/java/org/opensearch/sql/bwc/SQLBackwardsCompatibilityIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/bwc/SQLBackwardsCompatibilityIT.java @@ -3,10 +3,8 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.bwc; - import static org.opensearch.sql.legacy.TestUtils.createIndexByRestClient; import static org.opensearch.sql.legacy.TestUtils.isIndexExist; import static org.opensearch.sql.legacy.TestUtils.loadDataByRestClient; diff --git a/integ-test/src/test/java/org/opensearch/sql/correctness/runner/connection/OpenSearchConnection.java b/integ-test/src/test/java/org/opensearch/sql/correctness/runner/connection/OpenSearchConnection.java index 258c031b76..8a2240855f 100644 --- a/integ-test/src/test/java/org/opensearch/sql/correctness/runner/connection/OpenSearchConnection.java +++ b/integ-test/src/test/java/org/opensearch/sql/correctness/runner/connection/OpenSearchConnection.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.correctness.runner.connection; import java.io.IOException; @@ -16,18 +15,15 @@ import org.opensearch.sql.correctness.runner.resultset.DBResult; /** - * OpenSearch database connection for insertion. This class wraps JDBCConnection to delegate query method. + * OpenSearch database connection for insertion. This class wraps JDBCConnection to delegate query + * method. */ public class OpenSearchConnection implements DBConnection { - /** - * Connection via our OpenSearch JDBC driver - */ + /** Connection via our OpenSearch JDBC driver */ private final DBConnection connection; - /** - * Native OpenSearch REST client for operation unsupported by driver such as CREATE/INSERT - */ + /** Native OpenSearch REST client for operation unsupported by driver such as CREATE/INSERT */ private final RestClient client; public OpenSearchConnection(String connectionUrl, RestClient client) { @@ -112,10 +108,8 @@ private String buildBulkBody(String[] columnNames, List batch) { } } - body.append("{\"index\":{}}\n"). - append(json).append("\n"); + body.append("{\"index\":{}}\n").append(json).append("\n"); } return body.toString(); } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/Row.java b/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/Row.java index da08487a10..973ea76e71 100644 --- a/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/Row.java +++ b/integ-test/src/test/java/org/opensearch/sql/correctness/runner/resultset/Row.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.correctness.runner.resultset; import java.math.BigDecimal; @@ -15,9 +14,7 @@ import lombok.Getter; import lombok.ToString; -/** - * Row in result set. - */ +/** Row in result set. */ @EqualsAndHashCode @ToString @Getter @@ -77,5 +74,4 @@ public int compareTo(Row other) { } return 0; } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/correctness/tests/OpenSearchConnectionTest.java b/integ-test/src/test/java/org/opensearch/sql/correctness/tests/OpenSearchConnectionTest.java index 5b33884814..e5130d8fc1 100644 --- a/integ-test/src/test/java/org/opensearch/sql/correctness/tests/OpenSearchConnectionTest.java +++ b/integ-test/src/test/java/org/opensearch/sql/correctness/tests/OpenSearchConnectionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.correctness.tests; import static org.junit.Assert.assertEquals; @@ -30,14 +29,11 @@ import org.opensearch.client.RestClient; import org.opensearch.sql.correctness.runner.connection.OpenSearchConnection; -/** - * Tests for {@link OpenSearchConnection} - */ +/** Tests for {@link OpenSearchConnection} */ @RunWith(MockitoJUnitRunner.class) public class OpenSearchConnectionTest { - @Mock - private RestClient client; + @Mock private RestClient client; private OpenSearchConnection conn; @@ -63,36 +59,30 @@ public void testCreateTable() throws IOException { @Test public void testInsertData() throws IOException { - conn.insert("test", new String[] {"name"}, - Arrays.asList(new String[] {"John"}, new String[] {"Hank"})); + conn.insert( + "test", new String[] {"name"}, Arrays.asList(new String[] {"John"}, new String[] {"Hank"})); Request actual = captureActualArg(); assertEquals("POST", actual.getMethod()); assertEquals("/test/_bulk?refresh=true", actual.getEndpoint()); assertEquals( - "{\"index\":{}}\n" - + "{\"name\":\"John\"}\n" - + "{\"index\":{}}\n" - + "{\"name\":\"Hank\"}\n", - getBody(actual) - ); + "{\"index\":{}}\n{\"name\":\"John\"}\n{\"index\":{}}\n{\"name\":\"Hank\"}\n", + getBody(actual)); } @Test public void testInsertNullData() throws IOException { - conn.insert("test", new String[] {"name", "age"}, + conn.insert( + "test", + new String[] {"name", "age"}, Arrays.asList(new Object[] {null, 30}, new Object[] {"Hank", null})); Request actual = captureActualArg(); assertEquals("POST", actual.getMethod()); assertEquals("/test/_bulk?refresh=true", actual.getEndpoint()); assertEquals( - "{\"index\":{}}\n" - + "{\"age\":30}\n" - + "{\"index\":{}}\n" - + "{\"name\":\"Hank\"}\n", - getBody(actual) - ); + "{\"index\":{}}\n{\"age\":30}\n{\"index\":{}}\n{\"name\":\"Hank\"}\n", + getBody(actual)); } @Test @@ -114,5 +104,4 @@ private String getBody(Request request) throws IOException { InputStream inputStream = request.getEntity().getContent(); return CharStreams.toString(new InputStreamReader(inputStream)); } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/correctness/tests/RowTest.java b/integ-test/src/test/java/org/opensearch/sql/correctness/tests/RowTest.java index 66cc1a0500..79e134fe7b 100644 --- a/integ-test/src/test/java/org/opensearch/sql/correctness/tests/RowTest.java +++ b/integ-test/src/test/java/org/opensearch/sql/correctness/tests/RowTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.correctness.tests; import static org.junit.Assert.assertEquals; @@ -12,9 +11,7 @@ import org.junit.Test; import org.opensearch.sql.correctness.runner.resultset.Row; -/** - * Unit test {@link Row} - */ +/** Unit test {@link Row} */ public class RowTest { @Test @@ -47,5 +44,4 @@ public void shouldConsiderNullGreater() { row2.add("world"); assertEquals(1, row1.compareTo(row2)); } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/NestedFieldQueryIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/NestedFieldQueryIT.java index 378fbda937..48d053c6e1 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/NestedFieldQueryIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/NestedFieldQueryIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.hamcrest.Matchers.allOf; @@ -40,6 +39,7 @@ import org.opensearch.search.SearchHit; /** + *
  * Integration test cases for both rewriting and projection logic.
  * 

* Test result: @@ -56,13 +56,13 @@ * 4) Subquery * 5) HAVING * 6) Verification for conditions mixed with regular and nested fields + *

*/ public class NestedFieldQueryIT extends SQLIntegTestCase { private static final String FROM = "FROM " + TestsConstants.TEST_INDEX_NESTED_TYPE + " n, n.message m"; - @Override protected void init() throws Exception { loadIndex(Index.NESTED); @@ -83,188 +83,71 @@ private void queryAll(String sql) throws IOException { assertThat( query(sql), hits( - hit( - myNum(1), - someField("b"), - innerHits("message", - hit( - author("e"), - info("a") - ) - ) - ), - hit( - myNum(2), - someField("a"), - innerHits("message", - hit( - author("f"), - info("b") - ) - ) - ), - hit( - myNum(3), - someField("a"), - innerHits("message", - hit( - author("g"), - info("c") - ) - ) - ), + hit(myNum(1), someField("b"), innerHits("message", hit(author("e"), info("a")))), + hit(myNum(2), someField("a"), innerHits("message", hit(author("f"), info("b")))), + hit(myNum(3), someField("a"), innerHits("message", hit(author("g"), info("c")))), hit( myNum(4), someField("b"), - innerHits("message", - hit( - author("h"), - info("c") - ), - hit( - author("i"), - info("a") - ) - ) - ), + innerHits("message", hit(author("h"), info("c")), hit(author("i"), info("a")))), hit( myNum(new int[] {3, 4}), someField("a"), - innerHits("message", - hit( - author("zz"), - info("zz") - ) - ) - ) - ) - ); + innerHits("message", hit(author("zz"), info("zz")))))); } @Test public void singleCondition() throws IOException { assertThat( - query( - "SELECT myNum, m.author, m.info", - "WHERE m.info = 'c'" - ), + query("SELECT myNum, m.author, m.info", "WHERE m.info = 'c'"), hits( - hit( - myNum(3), - innerHits("message", - hit( - author("g"), - info("c") - ) - ) - ), - hit( - myNum(4), - innerHits("message", - hit( - author("h"), - info("c") - ) - ) - ) - ) - ); + hit(myNum(3), innerHits("message", hit(author("g"), info("c")))), + hit(myNum(4), innerHits("message", hit(author("h"), info("c")))))); } @Test public void multipleConditionsOfNestedField() throws IOException { assertThat( - query( - "SELECT someField, m.author, m.info", - "WHERE m.info = 'c' AND m.author = 'h'" - ), - hits( - hit( - someField("b"), - innerHits("message", - hit( - author("h"), - info("c") - ) - ) - ) - ) - ); + query("SELECT someField, m.author, m.info", "WHERE m.info = 'c' AND m.author = 'h'"), + hits(hit(someField("b"), innerHits("message", hit(author("h"), info("c")))))); } @Test public void multipleConditionsOfNestedFieldNoMatch() throws IOException { assertThat( - query( - "SELECT someField, m.author, m.info", - "WHERE m.info = 'c' AND m.author = 'i'" - ), - hits() - ); + query("SELECT someField, m.author, m.info", "WHERE m.info = 'c' AND m.author = 'i'"), + hits()); } @Test public void multipleConditionsOfRegularAndNestedField() throws IOException { assertThat( - query( - "SELECT myNum, m.author, m.info", - "WHERE myNum = 3 AND m.info = 'c'" - ), - hits( - hit( - myNum(3), - innerHits("message", - hit( - author("g"), - info("c") - ) - ) - ) - ) - ); + query("SELECT myNum, m.author, m.info", "WHERE myNum = 3 AND m.info = 'c'"), + hits(hit(myNum(3), innerHits("message", hit(author("g"), info("c")))))); } @Test public void multipleConditionsOfRegularOrNestedField() throws IOException { assertThat( - query( - "SELECT myNum, m.author, m.info", - "WHERE myNum = 2 OR m.info = 'c'" - ), + query("SELECT myNum, m.author, m.info", "WHERE myNum = 2 OR m.info = 'c'"), hits( - hit( - myNum(2) - ), // Note: no inner hit here because of no match in nested field - hit( - myNum(3), - innerHits("message", - hit( - author("g"), - info("c") - ) - ) - ), - hit( - myNum(4), - innerHits("message", - hit( - author("h"), - info("c") - ) - ) - ) - ) - ); + hit(myNum(2)), // Note: no inner hit here because of no match in nested field + hit(myNum(3), innerHits("message", hit(author("g"), info("c")))), + hit(myNum(4), innerHits("message", hit(author("h"), info("c")))))); } @Test public void leftJoinSelectAll() throws IOException { - String sql = "SELECT * " + - "FROM opensearch-sql_test_index_employee_nested e " + - "LEFT JOIN e.projects p"; + String sql = + "SELECT * " + + "FROM opensearch-sql_test_index_employee_nested e " + + "LEFT JOIN e.projects p"; String explain = explainQuery(sql); - assertThat(explain, containsString("{\"bool\":{\"must_not\":[{\"nested\":{\"query\":" + - "{\"exists\":{\"field\":\"projects\",\"boost\":1.0}},\"path\":\"projects\"")); + assertThat( + explain, + containsString( + "{\"bool\":{\"must_not\":[{\"nested\":{\"query\":" + + "{\"exists\":{\"field\":\"projects\",\"boost\":1.0}},\"path\":\"projects\"")); assertThat(explain, containsString("\"_source\":{\"includes\":[\"projects.*\"")); @@ -274,42 +157,50 @@ public void leftJoinSelectAll() throws IOException { @Test public void leftJoinSpecificFields() throws IOException { - String sql = "SELECT e.name, p.name, p.started_year " + - "FROM opensearch-sql_test_index_employee_nested e " + - "LEFT JOIN e.projects p"; + String sql = + "SELECT e.name, p.name, p.started_year " + + "FROM opensearch-sql_test_index_employee_nested e " + + "LEFT JOIN e.projects p"; String explain = explainQuery(sql); - assertThat(explain, containsString("{\"bool\":{\"must_not\":[{\"nested\":{\"query\":" + - "{\"exists\":{\"field\":\"projects\",\"boost\":1.0}},\"path\":\"projects\"")); + assertThat( + explain, + containsString( + "{\"bool\":{\"must_not\":[{\"nested\":{\"query\":" + + "{\"exists\":{\"field\":\"projects\",\"boost\":1.0}},\"path\":\"projects\"")); assertThat(explain, containsString("\"_source\":{\"includes\":[\"name\"],")); - assertThat(explain, + assertThat( + explain, containsString("\"_source\":{\"includes\":[\"projects.name\",\"projects.started_year\"]")); JSONObject results = executeQuery(sql); Assert.assertThat(getTotalHits(results), equalTo(4)); } - @Ignore("Comma join in left join won't pass syntax check in new ANTLR parser. " - + "Ignore for now and require to change grammar too when we want to support this case.") + @Ignore( + "Comma join in left join won't pass syntax check in new ANTLR parser. " + + "Ignore for now and require to change grammar too when we want to support this case.") @Test public void leftJoinExceptionOnExtraNestedFields() throws IOException { - String sql = "SELECT * " + - "FROM opensearch-sql_test_index_employee_nested e " + - "LEFT JOIN e.projects p, e.comments c"; + String sql = + "SELECT * " + + "FROM opensearch-sql_test_index_employee_nested e " + + "LEFT JOIN e.projects p, e.comments c"; try { String explain = explainQuery(sql); Assert.fail("Expected ResponseException, but none was thrown"); } catch (ResponseException e) { - assertThat(e.getResponse().getStatusLine().getStatusCode(), + assertThat( + e.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus())); final String entity = TestUtils.getResponseBody(e.getResponse()); - assertThat(entity, + assertThat( + entity, containsString("only single nested field is allowed as right table for LEFT JOIN")); assertThat(entity, containsString("\"type\":\"verification_exception\"")); } } - @Test public void aggregationWithoutGroupBy() throws IOException { String sql = "SELECT AVG(m.dayOfWeek) AS avgDay " + FROM; @@ -317,7 +208,9 @@ public void aggregationWithoutGroupBy() throws IOException { JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "message.dayOfWeek@NESTED"); - Assert.assertThat(((BigDecimal) aggregation.query("/avgDay/value")).doubleValue(), closeTo(3.166666666, 0.01)); + Assert.assertThat( + ((BigDecimal) aggregation.query("/avgDay/value")).doubleValue(), + closeTo(3.166666666, 0.01)); } @Test @@ -351,39 +244,36 @@ public void groupByRegularFieldAndSum() throws IOException { Assert.assertNotNull(msgInfoBuckets); Assert.assertThat(msgInfoBuckets.length(), equalTo(2)); Assert.assertThat(msgInfoBuckets.query("/0/key"), equalTo("a")); - Assert.assertThat(((BigDecimal) msgInfoBuckets.query("/0/message.dayOfWeek@NESTED/sumDay/value")).doubleValue(), + Assert.assertThat( + ((BigDecimal) msgInfoBuckets.query("/0/message.dayOfWeek@NESTED/sumDay/value")) + .doubleValue(), closeTo(9.0, 0.01)); Assert.assertThat(msgInfoBuckets.query("/1/key"), equalTo("b")); - Assert.assertThat(((BigDecimal) msgInfoBuckets.query("/1/message.dayOfWeek@NESTED/sumDay/value")).doubleValue(), + Assert.assertThat( + ((BigDecimal) msgInfoBuckets.query("/1/message.dayOfWeek@NESTED/sumDay/value")) + .doubleValue(), closeTo(10.0, 0.01)); } @Test public void nestedFiledIsNotNull() throws IOException { - String sql = "SELECT e.name " + - "FROM opensearch-sql_test_index_employee_nested as e, e.projects as p " + - "WHERE p IS NOT NULL"; + String sql = + "SELECT e.name " + + "FROM opensearch-sql_test_index_employee_nested as e, e.projects as p " + + "WHERE p IS NOT NULL"; assertThat( executeQuery(sql), hitAll( kvString("/_source/name", Is.is("Bob Smith")), - kvString("/_source/name", Is.is("Jane Smith")) - ) - ); + kvString("/_source/name", Is.is("Jane Smith")))); } // Doesn't support: aggregate function other than COUNT() @SuppressWarnings("unused") public void groupByNestedFieldAndAvg() throws IOException { - query( - "SELECT m.info, AVG(m.dayOfWeek)", - "GROUP BY m.info" - ); - query( - "SELECT m.info, AVG(myNum)", - "GROUP BY m.info" - ); + query("SELECT m.info, AVG(m.dayOfWeek)", "GROUP BY m.info"); + query("SELECT m.info, AVG(myNum)", "GROUP BY m.info"); } @Test @@ -418,10 +308,11 @@ public void groupByNestedAndRegularField() throws IOException { @Test public void countAggWithoutWhere() throws IOException { - String sql = "SELECT e.name, COUNT(p) as c " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "GROUP BY e.name " + - "HAVING c > 1"; + String sql = + "SELECT e.name, COUNT(p) as c " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "GROUP BY e.name " + + "HAVING c > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -437,11 +328,12 @@ public void countAggWithoutWhere() throws IOException { @Test public void countAggWithWhereOnParent() throws IOException { - String sql = "SELECT e.name, COUNT(p) as c " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE e.name like '%smith%' " + - "GROUP BY e.name " + - "HAVING c > 1"; + String sql = + "SELECT e.name, COUNT(p) as c " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE e.name like '%smith%' " + + "GROUP BY e.name " + + "HAVING c > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -457,11 +349,12 @@ public void countAggWithWhereOnParent() throws IOException { @Test public void countAggWithWhereOnNested() throws IOException { - String sql = "SELECT e.name, COUNT(p) as c " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING c > 1"; + String sql = + "SELECT e.name, COUNT(p) as c " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING c > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -477,11 +370,12 @@ public void countAggWithWhereOnNested() throws IOException { @Test public void countAggWithWhereOnParentOrNested() throws IOException { - String sql = "SELECT e.name, COUNT(p) as c " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE e.name like '%smith%' or p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING c > 1"; + String sql = + "SELECT e.name, COUNT(p) as c " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE e.name like '%smith%' or p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING c > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -497,11 +391,12 @@ public void countAggWithWhereOnParentOrNested() throws IOException { @Test public void countAggWithWhereOnParentAndNested() throws IOException { - String sql = "SELECT e.name, COUNT(p) as c " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE e.name like '%smith%' AND p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING c > 1"; + String sql = + "SELECT e.name, COUNT(p) as c " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE e.name like '%smith%' AND p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING c > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -517,11 +412,12 @@ public void countAggWithWhereOnParentAndNested() throws IOException { @Test public void countAggWithWhereOnNestedAndNested() throws IOException { - String sql = "SELECT e.name, COUNT(p) as c " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE p.started_year > 2000 AND p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING c > 0"; + String sql = + "SELECT e.name, COUNT(p) as c " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE p.started_year > 2000 AND p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING c > 0"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -537,11 +433,12 @@ public void countAggWithWhereOnNestedAndNested() throws IOException { @Test public void countAggWithWhereOnNestedOrNested() throws IOException { - String sql = "SELECT e.name, COUNT(p) as c " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE p.started_year > 2000 OR p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING c > 1"; + String sql = + "SELECT e.name, COUNT(p) as c " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE p.started_year > 2000 OR p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING c > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -557,11 +454,12 @@ public void countAggWithWhereOnNestedOrNested() throws IOException { @Test public void countAggOnNestedInnerFieldWithoutWhere() throws IOException { - String sql = "SELECT e.name, COUNT(p.started_year) as count " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING count > 0"; + String sql = + "SELECT e.name, COUNT(p.started_year) as count " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING count > 0"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -581,10 +479,11 @@ public void countAggOnNestedInnerFieldWithoutWhere() throws IOException { @Test public void maxAggOnNestedInnerFieldWithoutWhere() throws IOException { - String sql = "SELECT e.name, MAX(p.started_year) as max " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE p.name LIKE '%security%' " + - "GROUP BY e.name"; + String sql = + "SELECT e.name, MAX(p.started_year) as max " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE p.name LIKE '%security%' " + + "GROUP BY e.name"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -594,20 +493,27 @@ public void maxAggOnNestedInnerFieldWithoutWhere() throws IOException { Assert.assertThat(bucket.length(), equalTo(2)); Assert.assertThat(bucket.query("/0/key"), equalTo("Bob Smith")); Assert.assertThat( - ((BigDecimal) bucket.query("/0/projects.started_year@NESTED/projects.started_year@FILTER/max/value")).doubleValue(), + ((BigDecimal) + bucket.query( + "/0/projects.started_year@NESTED/projects.started_year@FILTER/max/value")) + .doubleValue(), closeTo(2015.0, 0.01)); Assert.assertThat(bucket.query("/1/key"), equalTo("Jane Smith")); Assert.assertThat( - ((BigDecimal) bucket.query("/1/projects.started_year@NESTED/projects.started_year@FILTER/max/value")).doubleValue(), + ((BigDecimal) + bucket.query( + "/1/projects.started_year@NESTED/projects.started_year@FILTER/max/value")) + .doubleValue(), closeTo(2015.0, 0.01)); } @Test public void havingCountAggWithoutWhere() throws IOException { - String sql = "SELECT e.name " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "GROUP BY e.name " + - "HAVING COUNT(p) > 1"; + String sql = + "SELECT e.name " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "GROUP BY e.name " + + "HAVING COUNT(p) > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -623,11 +529,12 @@ public void havingCountAggWithoutWhere() throws IOException { @Test public void havingCountAggWithWhereOnParent() throws IOException { - String sql = "SELECT e.name " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE e.name like '%smith%' " + - "GROUP BY e.name " + - "HAVING COUNT(p) > 1"; + String sql = + "SELECT e.name " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE e.name like '%smith%' " + + "GROUP BY e.name " + + "HAVING COUNT(p) > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -643,11 +550,12 @@ public void havingCountAggWithWhereOnParent() throws IOException { @Test public void havingCountAggWithWhereOnNested() throws IOException { - String sql = "SELECT e.name " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING COUNT(p) > 1"; + String sql = + "SELECT e.name " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING COUNT(p) > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -663,11 +571,12 @@ public void havingCountAggWithWhereOnNested() throws IOException { @Test public void havingCountAggWithWhereOnParentOrNested() throws IOException { - String sql = "SELECT e.name " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE e.name like '%smith%' or p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING COUNT(p) > 1"; + String sql = + "SELECT e.name " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE e.name like '%smith%' or p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING COUNT(p) > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -683,11 +592,12 @@ public void havingCountAggWithWhereOnParentOrNested() throws IOException { @Test public void havingCountAggWithWhereOnParentAndNested() throws IOException { - String sql = "SELECT e.name " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE e.name like '%smith%' AND p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING COUNT(p) > 1"; + String sql = + "SELECT e.name " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE e.name like '%smith%' AND p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING COUNT(p) > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -703,11 +613,12 @@ public void havingCountAggWithWhereOnParentAndNested() throws IOException { @Test public void havingCountAggWithWhereOnNestedAndNested() throws IOException { - String sql = "SELECT e.name " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE p.started_year > 2000 AND p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING COUNT(p) > 0"; + String sql = + "SELECT e.name " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE p.started_year > 2000 AND p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING COUNT(p) > 0"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -723,11 +634,12 @@ public void havingCountAggWithWhereOnNestedAndNested() throws IOException { @Test public void havingCountAggWithWhereOnNestedOrNested() throws IOException { - String sql = "SELECT e.name " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE p.started_year > 2000 OR p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING COUNT(p) > 1"; + String sql = + "SELECT e.name " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE p.started_year > 2000 OR p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING COUNT(p) > 1"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -743,11 +655,12 @@ public void havingCountAggWithWhereOnNestedOrNested() throws IOException { @Test public void havingCountAggOnNestedInnerFieldWithoutWhere() throws IOException { - String sql = "SELECT e.name " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING COUNT(p.started_year) > 0"; + String sql = + "SELECT e.name " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING COUNT(p.started_year) > 0"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -767,11 +680,12 @@ public void havingCountAggOnNestedInnerFieldWithoutWhere() throws IOException { @Test public void havingMaxAggOnNestedInnerFieldWithoutWhere() throws IOException { - String sql = "SELECT e.name " + - "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + - "WHERE p.name LIKE '%security%' " + - "GROUP BY e.name " + - "HAVING MAX(p.started_year) > 1990"; + String sql = + "SELECT e.name " + + "FROM opensearch-sql_test_index_employee_nested AS e, e.projects AS p " + + "WHERE p.name LIKE '%security%' " + + "GROUP BY e.name " + + "HAVING MAX(p.started_year) > 1990"; JSONObject result = executeQuery(sql); JSONObject aggregation = getAggregation(result, "name.keyword"); @@ -781,22 +695,28 @@ public void havingMaxAggOnNestedInnerFieldWithoutWhere() throws IOException { Assert.assertThat(bucket.length(), equalTo(2)); Assert.assertThat(bucket.query("/0/key"), equalTo("Bob Smith")); Assert.assertThat( - ((BigDecimal) bucket.query("/0/projects.started_year@NESTED/projects.started_year@FILTER/max_0/value")).doubleValue(), + ((BigDecimal) + bucket.query( + "/0/projects.started_year@NESTED/projects.started_year@FILTER/max_0/value")) + .doubleValue(), closeTo(2015.0, 0.01)); Assert.assertThat(bucket.query("/1/key"), equalTo("Jane Smith")); Assert.assertThat( - ((BigDecimal) bucket.query("/1/projects.started_year@NESTED/projects.started_year@FILTER/max_0/value")).doubleValue(), + ((BigDecimal) + bucket.query( + "/1/projects.started_year@NESTED/projects.started_year@FILTER/max_0/value")) + .doubleValue(), closeTo(2015.0, 0.01)); } /*********************************************************** - Matchers for Non-Aggregation Testing + * Matchers for Non-Aggregation Testing ***********************************************************/ @SafeVarargs private final Matcher hits(Matcher... subMatchers) { - return featureValueOf("hits", arrayContainingInAnyOrder(subMatchers), - resp -> resp.getHits().getHits()); + return featureValueOf( + "hits", arrayContainingInAnyOrder(subMatchers), resp -> resp.getHits().getHits()); } @SafeVarargs @@ -834,8 +754,7 @@ public boolean matches(Object item) { } @Override - public void describeTo(Description description) { - } + public void describeTo(Description description) {} }; } @@ -860,16 +779,15 @@ private final Matcher innerHits(String path, Matcher... in return featureValueOf( "innerHits", arrayContainingInAnyOrder(innerHitMatchers), - hit -> hit.getInnerHits().get(path).getHits() - ); + hit -> hit.getInnerHits().get(path).getHits()); } /*********************************************************** - Matchers for Aggregation Testing + * Matchers for Aggregation Testing ***********************************************************/ - private FeatureMatcher featureValueOf(String name, Matcher subMatcher, - Function getter) { + private FeatureMatcher featureValueOf( + String name, Matcher subMatcher, Function getter) { return new FeatureMatcher(subMatcher, name, name) { @Override protected U featureValueOf(T actual) { @@ -879,7 +797,7 @@ protected U featureValueOf(T actual) { } /*********************************************************** - Query Utility to Fetch Response for SQL + * Query Utility to Fetch Response for SQL ***********************************************************/ private SearchResponse query(String select, String... statements) throws IOException { @@ -889,10 +807,11 @@ private SearchResponse query(String select, String... statements) throws IOExcep private SearchResponse execute(String sql) throws IOException { final JSONObject jsonObject = executeQuery(sql); - final XContentParser parser = new JsonXContentParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - new JsonFactory().createParser(jsonObject.toString())); + final XContentParser parser = + new JsonXContentParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + new JsonFactory().createParser(jsonObject.toString())); return SearchResponse.fromXContent(parser); } @@ -904,5 +823,4 @@ private JSONObject getAggregation(final JSONObject queryResult, final String agg Assert.assertTrue(aggregations.has(aggregationName)); return aggregations.getJSONObject(aggregationName); } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/ObjectFieldSelectIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/ObjectFieldSelectIT.java index ce781123d6..3a2f48d497 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/ObjectFieldSelectIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/ObjectFieldSelectIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DEEP_NESTED; @@ -18,9 +17,8 @@ import org.opensearch.sql.legacy.utils.StringUtils; /** - * Integration test for OpenSearch object field (and nested field). - * This class is focused on simple SELECT-FROM query to ensure right column - * number and value is returned. + * Integration test for OpenSearch object field (and nested field). This class is focused on simple + * SELECT-FROM query to ensure right column number and value is returned. */ public class ObjectFieldSelectIT extends SQLIntegTestCase { @@ -36,33 +34,28 @@ public void testSelectObjectFieldItself() { verifySchema(response, schema("city", null, "object")); // Expect object field itself is returned in a single cell - verifyDataRows(response, - rows(new JSONObject( - "{\n" - + " \"name\": \"Seattle\",\n" - + " \"location\": {\"latitude\": 10.5}\n" - + "}") - ) - ); + verifyDataRows( + response, + rows( + new JSONObject( + "{\n" + + " \"name\": \"Seattle\",\n" + + " \"location\": {\"latitude\": 10.5}\n" + + "}"))); } @Test public void testSelectObjectInnerFields() { - JSONObject response = new JSONObject(query( - "SELECT city.location, city.location.latitude FROM %s")); + JSONObject response = + new JSONObject(query("SELECT city.location, city.location.latitude FROM %s")); - verifySchema(response, + verifySchema( + response, schema("city.location", null, "object"), - schema("city.location.latitude", null, "double") - ); + schema("city.location.latitude", null, "double")); // Expect inner regular or object field returned in its single cell - verifyDataRows(response, - rows( - new JSONObject("{\"latitude\": 10.5}"), - 10.5 - ) - ); + verifyDataRows(response, rows(new JSONObject("{\"latitude\": 10.5}"), 10.5)); } @Test @@ -72,15 +65,15 @@ public void testSelectNestedFieldItself() { verifySchema(response, schema("projects", null, "nested")); // Expect nested field itself is returned in a single cell - verifyDataRows(response, - rows(new JSONArray( - "[\n" - + " {\"name\": \"AWS Redshift Spectrum querying\"},\n" - + " {\"name\": \"AWS Redshift security\"},\n" - + " {\"name\": \"AWS Aurora security\"}\n" - + "]") - ) - ); + verifyDataRows( + response, + rows( + new JSONArray( + "[\n" + + " {\"name\": \"AWS Redshift Spectrum querying\"},\n" + + " {\"name\": \"AWS Redshift security\"},\n" + + " {\"name\": \"AWS Aurora security\"}\n" + + "]"))); } @Test @@ -100,10 +93,6 @@ public void testSelectObjectFieldOfArrayValuesInnerFields() { } private String query(String sql) { - return executeQuery( - StringUtils.format(sql, TEST_INDEX_DEEP_NESTED), - "jdbc" - ); + return executeQuery(StringUtils.format(sql, TEST_INDEX_DEEP_NESTED), "jdbc"); } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/OpenSearchSQLRestTestCase.java b/integ-test/src/test/java/org/opensearch/sql/legacy/OpenSearchSQLRestTestCase.java index 7b5961de35..385c9bc6ba 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/OpenSearchSQLRestTestCase.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/OpenSearchSQLRestTestCase.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static java.util.Collections.unmodifiableList; @@ -43,8 +42,9 @@ import org.opensearch.test.rest.OpenSearchRestTestCase; /** - * OpenSearch SQL integration test base class to support both security disabled and enabled OpenSearch cluster. - * Allows interaction with multiple external test clusters using OpenSearch's {@link RestClient}. + * OpenSearch SQL integration test base class to support both security disabled and enabled + * OpenSearch cluster. Allows interaction with multiple external test clusters using OpenSearch's + * {@link RestClient}. */ public abstract class OpenSearchSQLRestTestCase extends OpenSearchRestTestCase { @@ -53,17 +53,20 @@ public abstract class OpenSearchSQLRestTestCase extends OpenSearchRestTestCase { public static final String MATCH_ALL_REMOTE_CLUSTER = "*"; private static RestClient remoteClient; + /** - * A client for the running remote OpenSearch cluster configured to take test administrative actions - * like remove all indexes after the test completes + * A client for the running remote OpenSearch cluster configured to take test administrative + * actions like remove all indexes after the test completes */ private static RestClient remoteAdminClient; protected boolean isHttps() { - boolean isHttps = Optional.ofNullable(System.getProperty("https")) - .map("true"::equalsIgnoreCase).orElse(false); + boolean isHttps = + Optional.ofNullable(System.getProperty("https")) + .map("true"::equalsIgnoreCase) + .orElse(false); if (isHttps) { - //currently only external cluster is supported for security enabled testing + // currently only external cluster is supported for security enabled testing if (!Optional.ofNullable(System.getProperty("tests.rest.cluster")).isPresent()) { throw new RuntimeException( "external cluster url should be provided for security enabled testing"); @@ -77,16 +80,14 @@ protected String getProtocol() { return isHttps() ? "https" : "http"; } - /** - * Get the client to remote cluster used for ordinary api calls while writing a test. - */ + /** Get the client to remote cluster used for ordinary api calls while writing a test. */ protected static RestClient remoteClient() { return remoteClient; } /** - * Get the client to remote cluster used for test administrative actions. - * Do not use this while writing a test. Only use it for cleaning up after tests. + * Get the client to remote cluster used for test administrative actions. Do not use this while + * writing a test. Only use it for cleaning up after tests. */ protected static RestClient remoteAdminClient() { return remoteAdminClient; @@ -128,9 +129,7 @@ public void initRemoteClient() throws IOException { assert remoteAdminClient != null; } - /** - * Get a comma delimited list of [host:port] to which to send REST requests. - */ + /** Get a comma delimited list of [host:port] to which to send REST requests. */ protected String getTestRestCluster(String clusterName) { String cluster = System.getProperty("tests.rest." + clusterName + ".http_hosts"); if (cluster == null) { @@ -138,15 +137,12 @@ protected String getTestRestCluster(String clusterName) { "Must specify [tests.rest." + clusterName + ".http_hosts] system property with a comma delimited list of [host:port] " - + "to which to send REST requests" - ); + + "to which to send REST requests"); } return cluster; } - /** - * Get a comma delimited list of [host:port] for connections between clusters. - */ + /** Get a comma delimited list of [host:port] for connections between clusters. */ protected String getTestTransportCluster(String clusterName) { String cluster = System.getProperty("tests.rest." + clusterName + ".transport_hosts"); if (cluster == null) { @@ -154,8 +150,7 @@ protected String getTestTransportCluster(String clusterName) { "Must specify [tests.rest." + clusterName + ".transport_hosts] system property with a comma delimited list of [host:port] " - + "for connections between clusters" - ); + + "for connections between clusters"); } return cluster; } @@ -181,18 +176,22 @@ protected static void wipeAllOpenSearchIndices(RestClient client) throws IOExcep // include all the indices, included hidden indices. // https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-indices.html#cat-indices-api-query-params try { - Response response = client.performRequest(new Request("GET", "/_cat/indices?format=json&expand_wildcards=all")); + Response response = + client.performRequest( + new Request("GET", "/_cat/indices?format=json&expand_wildcards=all")); JSONArray jsonArray = new JSONArray(EntityUtils.toString(response.getEntity(), "UTF-8")); for (Object object : jsonArray) { JSONObject jsonObject = (JSONObject) object; String indexName = jsonObject.getString("index"); try { - // System index, mostly named .opensearch-xxx or .opendistro-xxx, are not allowed to delete + // System index, mostly named .opensearch-xxx or .opendistro-xxx, are not allowed to + // delete if (!indexName.startsWith(".opensearch") && !indexName.startsWith(".opendistro")) { client.performRequest(new Request("DELETE", "/" + indexName)); } } catch (Exception e) { - // TODO: Ignore index delete error for now. Remove this if strict check on system index added above. + // TODO: Ignore index delete error for now. Remove this if strict check on system index + // added above. LOG.warn("Failed to delete index: " + indexName, e); } } @@ -201,9 +200,8 @@ protected static void wipeAllOpenSearchIndices(RestClient client) throws IOExcep } } - protected static void configureHttpsClient(RestClientBuilder builder, Settings settings, - HttpHost httpHost) - throws IOException { + protected static void configureHttpsClient( + RestClientBuilder builder, Settings settings, HttpHost httpHost) throws IOException { Map headers = ThreadContext.buildDefaultHeaders(settings); Header[] defaultHeaders = new Header[headers.size()]; int i = 0; @@ -211,56 +209,66 @@ protected static void configureHttpsClient(RestClientBuilder builder, Settings s defaultHeaders[i++] = new BasicHeader(entry.getKey(), entry.getValue()); } builder.setDefaultHeaders(defaultHeaders); - builder.setHttpClientConfigCallback(httpClientBuilder -> { - String userName = Optional.ofNullable(System.getProperty("user")) - .orElseThrow(() -> new RuntimeException("user name is missing")); - String password = Optional.ofNullable(System.getProperty("password")) - .orElseThrow(() -> new RuntimeException("password is missing")); - BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - credentialsProvider - .setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(userName, - password.toCharArray())); - try { - final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create() - .setSslContext(SSLContextBuilder.create() - .loadTrustMaterial(null, (chains, authType) -> true) - .build()) - .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .build(); + builder.setHttpClientConfigCallback( + httpClientBuilder -> { + String userName = + Optional.ofNullable(System.getProperty("user")) + .orElseThrow(() -> new RuntimeException("user name is missing")); + String password = + Optional.ofNullable(System.getProperty("password")) + .orElseThrow(() -> new RuntimeException("password is missing")); + BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials( + new AuthScope(httpHost), + new UsernamePasswordCredentials(userName, password.toCharArray())); + try { + final TlsStrategy tlsStrategy = + ClientTlsStrategyBuilder.create() + .setSslContext( + SSLContextBuilder.create() + .loadTrustMaterial(null, (chains, authType) -> true) + .build()) + .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .build(); - return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider) - .setConnectionManager(PoolingAsyncClientConnectionManagerBuilder.create() - .setTlsStrategy(tlsStrategy) - .build()); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); + return httpClientBuilder + .setDefaultCredentialsProvider(credentialsProvider) + .setConnectionManager( + PoolingAsyncClientConnectionManagerBuilder.create() + .setTlsStrategy(tlsStrategy) + .build()); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); final String socketTimeoutString = settings.get(CLIENT_SOCKET_TIMEOUT); final TimeValue socketTimeout = - TimeValue.parseTimeValue(socketTimeoutString == null ? "60s" : socketTimeoutString, - CLIENT_SOCKET_TIMEOUT); + TimeValue.parseTimeValue( + socketTimeoutString == null ? "60s" : socketTimeoutString, CLIENT_SOCKET_TIMEOUT); builder.setRequestConfigCallback( - conf -> conf.setResponseTimeout(Timeout.ofMilliseconds(Math.toIntExact(socketTimeout.getMillis())))); + conf -> + conf.setResponseTimeout( + Timeout.ofMilliseconds(Math.toIntExact(socketTimeout.getMillis())))); if (settings.hasValue(CLIENT_PATH_PREFIX)) { builder.setPathPrefix(settings.get(CLIENT_PATH_PREFIX)); } } /** - * Initialize rest client to remote cluster, - * and create a connection to it from the coordinating cluster. + * Initialize rest client to remote cluster, and create a connection to it from the coordinating + * cluster. */ public void configureMultiClusters() throws IOException { initRemoteClient(); Request connectionRequest = new Request("PUT", "_cluster/settings"); - String connectionSetting = "{\"persistent\": {\"cluster\": {\"remote\": {\"" - + REMOTE_CLUSTER - + "\": {\"seeds\": [\"" - + getTestTransportCluster(REMOTE_CLUSTER).split(",")[0] - + "\"]}}}}}"; + String connectionSetting = + "{\"persistent\": {\"cluster\": {\"remote\": {\"" + + REMOTE_CLUSTER + + "\": {\"seeds\": [\"" + + getTestTransportCluster(REMOTE_CLUSTER).split(",")[0] + + "\"]}}}}}"; connectionRequest.setJsonEntity(connectionSetting); adminClient().performRequest(connectionRequest); } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/OrderIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/OrderIT.java index c8b4b87f69..01e989e9f0 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/OrderIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/OrderIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.hamcrest.Matchers.equalTo; @@ -76,17 +75,17 @@ public void orderByIsNull() throws IOException { assertThat(query(hits, "/0/_source/id"), equalTo("5")); // Another equivalent syntax - assertThat(explainQuery("SELECT * FROM opensearch-sql_test_index_order " + - "ORDER BY id IS NULL, id DESC"), - equalTo(explainQuery("SELECT * FROM opensearch-sql_test_index_order " + - "ORDER BY id IS NULL DESC")) - ); + assertThat( + explainQuery( + "SELECT * FROM opensearch-sql_test_index_order ORDER BY id IS NULL, id DESC"), + equalTo( + explainQuery( + "SELECT * FROM opensearch-sql_test_index_order ORDER BY id IS NULL DESC"))); } @Test public void orderByIsNotNull() throws IOException { - String query = - "SELECT id, name FROM opensearch-sql_test_index_order ORDER BY name IS NOT NULL"; + String query = "SELECT id, name FROM opensearch-sql_test_index_order ORDER BY name IS NOT NULL"; JSONArray result = getSortExplain(query); assertThat(1, equalTo(result.length())); assertThat(query(result, "/0/name.keyword/order"), equalTo("asc")); @@ -95,21 +94,24 @@ public void orderByIsNotNull() throws IOException { JSONObject response = executeQuery(query); JSONArray hits = getHits(response); assertFalse(hits.getJSONObject(0).getJSONObject("_source").has("name")); - assertThat(hits.getJSONObject(hits.length() - 1).query("/_source/name").toString(), - equalTo("f")); + assertThat( + hits.getJSONObject(hits.length() - 1).query("/_source/name").toString(), equalTo("f")); // Another equivalent syntax - assertThat(explainQuery("SELECT id, name FROM opensearch-sql_test_index_order " + - "ORDER BY name IS NOT NULL"), - equalTo(explainQuery("SELECT id, name FROM opensearch-sql_test_index_order " + - "ORDER BY name IS NOT NULL ASC")) - ); + assertThat( + explainQuery( + "SELECT id, name FROM opensearch-sql_test_index_order ORDER BY name IS NOT NULL"), + equalTo( + explainQuery( + "SELECT id, name FROM opensearch-sql_test_index_order " + + "ORDER BY name IS NOT NULL ASC"))); } @Test public void multipleOrderByWithNulls() throws IOException { String query = - "SELECT id, name FROM opensearch-sql_test_index_order ORDER BY id IS NULL, name IS NOT NULL"; + "SELECT id, name FROM opensearch-sql_test_index_order ORDER BY id IS NULL, name IS NOT" + + " NULL"; JSONArray result = getSortExplain(query); assertThat(result.length(), equalTo(2)); assertThat(query(result, "/0/id/missing"), equalTo("_last")); @@ -118,8 +120,9 @@ public void multipleOrderByWithNulls() throws IOException { @Test public void testOrderByMergeForSameField() throws IOException { - String query = "SELECT * FROM opensearch-sql_test_index_order " + - "ORDER BY id IS NULL, name DESC, id DESC, id IS NOT NULL, name IS NULL"; + String query = + "SELECT * FROM opensearch-sql_test_index_order " + + "ORDER BY id IS NULL, name DESC, id DESC, id IS NOT NULL, name IS NULL"; JSONArray result = getSortExplain(query); assertThat(2, equalTo(result.length())); assertThat(query(result, "/0/id/order"), equalTo("asc")); diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/OrdinalAliasRewriterIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/OrdinalAliasRewriterIT.java index ecec5844be..caea2aa7c6 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/OrdinalAliasRewriterIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/OrdinalAliasRewriterIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.hamcrest.Matchers.equalTo; @@ -23,125 +22,191 @@ protected void init() throws Exception { // tests query results with jdbc output @Test public void simpleGroupByOrdinal() { - String expected = executeQuery(StringUtils.format( - "SELECT lastname FROM %s AS b GROUP BY lastname LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); - String actual = executeQuery(StringUtils.format( - "SELECT lastname FROM %s AS b GROUP BY 1 LIMIT 3", TestsConstants.TEST_INDEX_ACCOUNT), - "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT lastname FROM %s AS b GROUP BY lastname LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); + String actual = + executeQuery( + StringUtils.format( + "SELECT lastname FROM %s AS b GROUP BY 1 LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); assertThat(actual, equalTo(expected)); } @Test public void multipleGroupByOrdinal() { - String expected = executeQuery(StringUtils.format( - "SELECT lastname, firstname, age FROM %s AS b GROUP BY firstname, age, lastname LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); - String actual = executeQuery(StringUtils.format( - "SELECT lastname, firstname, age FROM %s AS b GROUP BY 2, 3, 1 LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT lastname, firstname, age FROM %s AS b GROUP BY firstname, age, lastname" + + " LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); + String actual = + executeQuery( + StringUtils.format( + "SELECT lastname, firstname, age FROM %s AS b GROUP BY 2, 3, 1 LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); assertThat(actual, equalTo(expected)); } @Test public void selectFieldiWithBacticksGroupByOrdinal() { - String expected = executeQuery(StringUtils.format( - "SELECT `lastname` FROM %s AS b GROUP BY `lastname` LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); - String actual = executeQuery(StringUtils.format( - "SELECT `lastname` FROM %s AS b GROUP BY 1 LIMIT 3", TestsConstants.TEST_INDEX_ACCOUNT), - "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT `lastname` FROM %s AS b GROUP BY `lastname` LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); + String actual = + executeQuery( + StringUtils.format( + "SELECT `lastname` FROM %s AS b GROUP BY 1 LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); assertThat(actual, equalTo(expected)); } @Test public void selectFieldiWithBacticksAndTableAliasGroupByOrdinal() { - String expected = executeQuery(StringUtils.format( - "SELECT `b`.`lastname`, `age`, firstname FROM %s AS b GROUP BY `age`, `b`.`lastname` , firstname LIMIT 10", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); - String actual = executeQuery(StringUtils.format( - "SELECT `b`.`lastname`, `age`, firstname FROM %s AS b GROUP BY 2, 1, 3 LIMIT 10", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT `b`.`lastname`, `age`, firstname FROM %s AS b GROUP BY `age`," + + " `b`.`lastname` , firstname LIMIT 10", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); + String actual = + executeQuery( + StringUtils.format( + "SELECT `b`.`lastname`, `age`, firstname FROM %s AS b GROUP BY 2, 1, 3 LIMIT 10", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); assertThat(actual, equalTo(expected)); } @Test public void simpleOrderByOrdinal() { - String expected = executeQuery(StringUtils.format( - "SELECT lastname FROM %s AS b ORDER BY lastname LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); - String actual = executeQuery(StringUtils.format( - "SELECT lastname FROM %s AS b ORDER BY 1 LIMIT 3", TestsConstants.TEST_INDEX_ACCOUNT), - "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT lastname FROM %s AS b ORDER BY lastname LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); + String actual = + executeQuery( + StringUtils.format( + "SELECT lastname FROM %s AS b ORDER BY 1 LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); assertThat(actual, equalTo(expected)); } @Test public void multipleOrderByOrdinal() { - String expected = executeQuery(StringUtils.format( - "SELECT lastname, firstname, age FROM %s AS b ORDER BY firstname, age, lastname LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); - String actual = executeQuery(StringUtils.format( - "SELECT lastname, firstname, age FROM %s AS b ORDER BY 2, 3, 1 LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT lastname, firstname, age FROM %s AS b ORDER BY firstname, age, lastname" + + " LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); + String actual = + executeQuery( + StringUtils.format( + "SELECT lastname, firstname, age FROM %s AS b ORDER BY 2, 3, 1 LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); assertThat(actual, equalTo(expected)); } @Test public void selectFieldiWithBacticksOrderByOrdinal() { - String expected = executeQuery(StringUtils.format( - "SELECT `lastname` FROM %s AS b ORDER BY `lastname` LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); - String actual = executeQuery(StringUtils.format( - "SELECT `lastname` FROM %s AS b ORDER BY 1 LIMIT 3", TestsConstants.TEST_INDEX_ACCOUNT), - "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT `lastname` FROM %s AS b ORDER BY `lastname` LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); + String actual = + executeQuery( + StringUtils.format( + "SELECT `lastname` FROM %s AS b ORDER BY 1 LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); assertThat(actual, equalTo(expected)); } @Test public void selectFieldiWithBacticksAndTableAliasOrderByOrdinal() { - String expected = executeQuery(StringUtils.format( - "SELECT `b`.`lastname` FROM %s AS b ORDER BY `b`.`lastname` LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); - String actual = executeQuery(StringUtils.format( - "SELECT `b`.`lastname` FROM %s AS b ORDER BY 1 LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT `b`.`lastname` FROM %s AS b ORDER BY `b`.`lastname` LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); + String actual = + executeQuery( + StringUtils.format( + "SELECT `b`.`lastname` FROM %s AS b ORDER BY 1 LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); assertThat(actual, equalTo(expected)); } // ORDER BY IS NULL/NOT NULL @Test public void selectFieldiWithBacticksAndTableAliasOrderByOrdinalAndNull() { - String expected = executeQuery(StringUtils.format( - "SELECT `b`.`lastname`, age FROM %s AS b ORDER BY `b`.`lastname` IS NOT NULL DESC, age is NULL LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); - String actual = executeQuery(StringUtils.format( - "SELECT `b`.`lastname`, age FROM %s AS b ORDER BY 1 IS NOT NULL DESC, 2 IS NULL LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT `b`.`lastname`, age FROM %s AS b ORDER BY `b`.`lastname` IS NOT NULL DESC," + + " age is NULL LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); + String actual = + executeQuery( + StringUtils.format( + "SELECT `b`.`lastname`, age FROM %s AS b ORDER BY 1 IS NOT NULL DESC, 2 IS NULL" + + " LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc"); assertThat(actual, equalTo(expected)); } - // explain @Test public void explainSelectFieldiWithBacticksAndTableAliasGroupByOrdinal() throws IOException { - String expected = explainQuery(StringUtils.format( - "SELECT `b`.`lastname` FROM %s AS b GROUP BY `b`.`lastname` LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT)); - String actual = explainQuery(StringUtils.format( - "SELECT `b`.`lastname` FROM %s AS b GROUP BY 1 LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT)); + String expected = + explainQuery( + StringUtils.format( + "SELECT `b`.`lastname` FROM %s AS b GROUP BY `b`.`lastname` LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT)); + String actual = + explainQuery( + StringUtils.format( + "SELECT `b`.`lastname` FROM %s AS b GROUP BY 1 LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT)); assertThat(actual, equalTo(expected)); } @Test public void explainSelectFieldiWithBacticksAndTableAliasOrderByOrdinal() throws IOException { - String expected = explainQuery(StringUtils.format( - "SELECT `b`.`lastname` FROM %s AS b ORDER BY `b`.`lastname` LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT)); - String actual = explainQuery(StringUtils.format( - "SELECT `b`.`lastname` FROM %s AS b ORDER BY 1 LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT)); + String expected = + explainQuery( + StringUtils.format( + "SELECT `b`.`lastname` FROM %s AS b ORDER BY `b`.`lastname` LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT)); + String actual = + explainQuery( + StringUtils.format( + "SELECT `b`.`lastname` FROM %s AS b ORDER BY 1 LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT)); assertThat(actual, equalTo(expected)); } @@ -149,12 +214,18 @@ public void explainSelectFieldiWithBacticksAndTableAliasOrderByOrdinal() throws @Ignore("only work for legacy engine") public void explainSelectFieldiWithBacticksAndTableAliasOrderByOrdinalAndNull() throws IOException { - String expected = explainQuery(StringUtils.format( - "SELECT `b`.`lastname`, age FROM %s AS b ORDER BY `b`.`lastname` IS NOT NULL DESC, age is NULL LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT)); - String actual = explainQuery(StringUtils.format( - "SELECT `b`.`lastname`, age FROM %s AS b ORDER BY 1 IS NOT NULL DESC, 2 IS NULL LIMIT 3", - TestsConstants.TEST_INDEX_ACCOUNT)); + String expected = + explainQuery( + StringUtils.format( + "SELECT `b`.`lastname`, age FROM %s AS b ORDER BY `b`.`lastname` IS NOT NULL DESC," + + " age is NULL LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT)); + String actual = + explainQuery( + StringUtils.format( + "SELECT `b`.`lastname`, age FROM %s AS b ORDER BY 1 IS NOT NULL DESC, 2 IS NULL" + + " LIMIT 3", + TestsConstants.TEST_INDEX_ACCOUNT)); assertThat(actual, equalTo(expected)); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/PluginIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/PluginIT.java index 5f7de5d496..9cbb73cd5b 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/PluginIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/PluginIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.hamcrest.Matchers.equalTo; @@ -33,8 +32,9 @@ protected void init() throws Exception { public void sqlEnableSettingsTest() throws IOException { loadIndex(Index.ACCOUNT); updateClusterSettings(new ClusterSetting(PERSISTENT, "plugins.sql.enabled", "true")); - String query = String - .format(Locale.ROOT, "SELECT firstname FROM %s WHERE account_number=1", TEST_INDEX_ACCOUNT); + String query = + String.format( + Locale.ROOT, "SELECT firstname FROM %s WHERE account_number=1", TEST_INDEX_ACCOUNT); JSONObject queryResult = executeQuery(query); assertThat(getHits(queryResult).length(), equalTo(1)); @@ -50,16 +50,19 @@ public void sqlEnableSettingsTest() throws IOException { assertThat(queryResult.getInt("status"), equalTo(400)); JSONObject error = queryResult.getJSONObject("error"); assertThat(error.getString("reason"), equalTo("Invalid SQL query")); - assertThat(error.getString("details"), equalTo( - "Either plugins.sql.enabled or rest.action.multi.allow_explicit_index setting is false")); + assertThat( + error.getString("details"), + equalTo( + "Either plugins.sql.enabled or rest.action.multi.allow_explicit_index setting is" + + " false")); assertThat(error.getString("type"), equalTo("SQLFeatureDisabledException")); wipeAllClusterSettings(); } @Test public void sqlDeleteSettingsTest() throws IOException { - updateClusterSettings(new ClusterSetting(PERSISTENT, - Settings.Key.SQL_DELETE_ENABLED.getKeyValue(), "false")); + updateClusterSettings( + new ClusterSetting(PERSISTENT, Settings.Key.SQL_DELETE_ENABLED.getKeyValue(), "false")); String deleteQuery = StringUtils.format("DELETE FROM %s", TestsConstants.TEST_INDEX_ACCOUNT); final ResponseException exception = @@ -70,8 +73,8 @@ public void sqlDeleteSettingsTest() throws IOException { "{\n" + " \"error\": {\n" + " \"reason\": \"Invalid SQL query\",\n" - + " \"details\": \"DELETE clause is disabled by default and will be deprecated. Using " - + "the plugins.sql.delete.enabled setting to enable it\",\n" + + " \"details\": \"DELETE clause is disabled by default and will be deprecated." + + " Using the plugins.sql.delete.enabled setting to enable it\",\n" + " \"type\": \"SQLFeatureDisabledException\"\n" + " },\n" + " \"status\": 400\n" @@ -84,329 +87,355 @@ public void sqlDeleteSettingsTest() throws IOException { @Test public void sqlTransientOnlySettingTest() throws IOException { // (1) compact form - String settings = "{" + - " \"transient\": {" + - " \"plugins.query.metrics.rolling_interval\": \"80\"" + - " }" + - "}"; + String settings = + "{" + + " \"transient\": {" + + " \"plugins.query.metrics.rolling_interval\": \"80\"" + + " }" + + "}"; JSONObject actual = updateViaSQLSettingsAPI(settings); - JSONObject expected = new JSONObject("{" + - " \"acknowledged\" : true," + - " \"persistent\" : { }," + - " \"transient\" : {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\" : {" + - " \"rolling_interval\" : \"80\"" + - " }" + - " }" + - " }" + - " }" + - "}"); + JSONObject expected = + new JSONObject( + "{" + + " \"acknowledged\" : true," + + " \"persistent\" : { }," + + " \"transient\" : {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\" : {" + + " \"rolling_interval\" : \"80\"" + + " }" + + " }" + + " }" + + " }" + + "}"); assertTrue(actual.similar(expected)); // (2) partial expanded form - settings = "{" + - " \"transient\": {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics.rolling_interval\": \"75\"" + - " }" + - " }" + - " }" + - "}"; + settings = + "{" + + " \"transient\": {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics.rolling_interval\": \"75\"" + + " }" + + " }" + + " }" + + "}"; actual = updateViaSQLSettingsAPI(settings); - expected = new JSONObject("{" + - " \"acknowledged\" : true," + - " \"persistent\" : { }," + - " \"transient\" : {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\" : {" + - " \"rolling_interval\" : \"75\"" + - " }" + - " }" + - " }" + - " }" + - "}"); + expected = + new JSONObject( + "{" + + " \"acknowledged\" : true," + + " \"persistent\" : { }," + + " \"transient\" : {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\" : {" + + " \"rolling_interval\" : \"75\"" + + " }" + + " }" + + " }" + + " }" + + "}"); assertTrue(actual.similar(expected)); - // (3) full expanded form - settings = "{" + - " \"transient\": {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\": {" + - " \"rolling_interval\": \"65\"" + - " }" + - " }" + - " }" + - " }" + - "}"; + settings = + "{" + + " \"transient\": {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\": {" + + " \"rolling_interval\": \"65\"" + + " }" + + " }" + + " }" + + " }" + + "}"; actual = updateViaSQLSettingsAPI(settings); - expected = new JSONObject("{" + - " \"acknowledged\" : true," + - " \"persistent\" : { }," + - " \"transient\" : {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\" : {" + - " \"rolling_interval\" : \"65\"" + - " }" + - " }" + - " }" + - " }" + - "}"); + expected = + new JSONObject( + "{" + + " \"acknowledged\" : true," + + " \"persistent\" : { }," + + " \"transient\" : {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\" : {" + + " \"rolling_interval\" : \"65\"" + + " }" + + " }" + + " }" + + " }" + + "}"); assertTrue(actual.similar(expected)); } @Test public void sqlPersistentOnlySettingTest() throws IOException { // (1) compact form - String settings = "{" + - " \"persistent\": {" + - " \"plugins.query.metrics.rolling_interval\": \"80\"" + - " }" + - "}"; + String settings = + "{" + + " \"persistent\": {" + + " \"plugins.query.metrics.rolling_interval\": \"80\"" + + " }" + + "}"; JSONObject actual = updateViaSQLSettingsAPI(settings); - JSONObject expected = new JSONObject("{" + - " \"acknowledged\" : true," + - " \"transient\" : { }," + - " \"persistent\" : {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\" : {" + - " \"rolling_interval\" : \"80\"" + - " }" + - " }" + - " }" + - " }" + - "}"); + JSONObject expected = + new JSONObject( + "{" + + " \"acknowledged\" : true," + + " \"transient\" : { }," + + " \"persistent\" : {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\" : {" + + " \"rolling_interval\" : \"80\"" + + " }" + + " }" + + " }" + + " }" + + "}"); assertTrue(actual.similar(expected)); // (2) partial expanded form - settings = "{" + - " \"persistent\": {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics.rolling_interval\": \"75\"" + - " }" + - " }" + - " }" + - "}"; + settings = + "{" + + " \"persistent\": {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics.rolling_interval\": \"75\"" + + " }" + + " }" + + " }" + + "}"; actual = updateViaSQLSettingsAPI(settings); - expected = new JSONObject("{" + - " \"acknowledged\" : true," + - " \"transient\" : { }," + - " \"persistent\" : {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\" : {" + - " \"rolling_interval\" : \"75\"" + - " }" + - " }" + - " }" + - " }" + - "}"); + expected = + new JSONObject( + "{" + + " \"acknowledged\" : true," + + " \"transient\" : { }," + + " \"persistent\" : {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\" : {" + + " \"rolling_interval\" : \"75\"" + + " }" + + " }" + + " }" + + " }" + + "}"); assertTrue(actual.similar(expected)); - // (3) full expanded form - settings = "{" + - " \"persistent\": {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\": {" + - " \"rolling_interval\": \"65\"" + - " }" + - " }" + - " }" + - " }" + - "}"; + settings = + "{" + + " \"persistent\": {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\": {" + + " \"rolling_interval\": \"65\"" + + " }" + + " }" + + " }" + + " }" + + "}"; actual = updateViaSQLSettingsAPI(settings); - expected = new JSONObject("{" + - " \"acknowledged\" : true," + - " \"transient\" : { }," + - " \"persistent\" : {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\" : {" + - " \"rolling_interval\" : \"65\"" + - " }" + - " }" + - " }" + - " }" + - "}"); + expected = + new JSONObject( + "{" + + " \"acknowledged\" : true," + + " \"transient\" : { }," + + " \"persistent\" : {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\" : {" + + " \"rolling_interval\" : \"65\"" + + " }" + + " }" + + " }" + + " }" + + "}"); assertTrue(actual.similar(expected)); } /** - * Both transient and persistent settings are applied for same settings. - * This is similar to _cluster/settings behavior + * Both transient and persistent settings are applied for same settings. This is similar to + * _cluster/settings behavior */ @Test public void sqlCombinedSettingTest() throws IOException { - String settings = "{" + - " \"transient\": {" + - " \"plugins.query.metrics.rolling_window\": \"3700\"" + - " }," + - " \"persistent\": {" + - " \"plugins.sql.slowlog\" : \"2\"" + - " }" + - "}"; + String settings = + "{" + + " \"transient\": {" + + " \"plugins.query.metrics.rolling_window\": \"3700\"" + + " }," + + " \"persistent\": {" + + " \"plugins.sql.slowlog\" : \"2\"" + + " }" + + "}"; JSONObject actual = updateViaSQLSettingsAPI(settings); - JSONObject expected = new JSONObject("{" + - " \"acknowledged\" : true," + - " \"persistent\" : {" + - " \"plugins\" : {" + - " \"sql\" : {" + - " \"slowlog\" : \"2\"" + - " }" + - " }" + - " }," + - " \"transient\" : {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\" : {" + - " \"rolling_window\" : \"3700\"" + - " }" + - " }" + - " }" + - " }" + - "}"); + JSONObject expected = + new JSONObject( + "{" + + " \"acknowledged\" : true," + + " \"persistent\" : {" + + " \"plugins\" : {" + + " \"sql\" : {" + + " \"slowlog\" : \"2\"" + + " }" + + " }" + + " }," + + " \"transient\" : {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\" : {" + + " \"rolling_window\" : \"3700\"" + + " }" + + " }" + + " }" + + " }" + + "}"); assertTrue(actual.similar(expected)); } - /** - * Ignore all non plugins.sql settings. - * Only settings starting with plugins.sql. are affected - */ + /** Ignore all non plugins.sql settings. Only settings starting with plugins.sql. are affected */ @Test public void ignoreNonSQLSettingsTest() throws IOException { - String settings = "{" + - " \"transient\": {" + - " \"plugins.query.metrics.rolling_window\": \"3700\"," + - " \"plugins.alerting.metrics.rolling_window\": \"3700\"," + - " \"search.max_buckets\": \"10000\"," + - " \"search.max_keep_alive\": \"24h\"" + - " }," + - " \"persistent\": {" + - " \"plugins.sql.slowlog\": \"2\"," + - " \"plugins.alerting.metrics.rolling_window\": \"3700\"," + - " \"thread_pool.analyze.queue_size\": \"16\"" + - " }" + - "}"; + String settings = + "{" + + " \"transient\": {" + + " \"plugins.query.metrics.rolling_window\": \"3700\"," + + " \"plugins.alerting.metrics.rolling_window\": \"3700\"," + + " \"search.max_buckets\": \"10000\"," + + " \"search.max_keep_alive\": \"24h\"" + + " }," + + " \"persistent\": {" + + " \"plugins.sql.slowlog\": \"2\"," + + " \"plugins.alerting.metrics.rolling_window\": \"3700\"," + + " \"thread_pool.analyze.queue_size\": \"16\"" + + " }" + + "}"; JSONObject actual = updateViaSQLSettingsAPI(settings); - JSONObject expected = new JSONObject("{" + - " \"acknowledged\" : true," + - " \"persistent\" : {" + - " \"plugins\" : {" + - " \"sql\" : {" + - " \"slowlog\" : \"2\"" + - " }" + - " }" + - " }," + - " \"transient\" : {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\" : {" + - " \"rolling_window\" : \"3700\"" + - " }" + - " }" + - " }" + - " }" + - "}"); + JSONObject expected = + new JSONObject( + "{" + + " \"acknowledged\" : true," + + " \"persistent\" : {" + + " \"plugins\" : {" + + " \"sql\" : {" + + " \"slowlog\" : \"2\"" + + " }" + + " }" + + " }," + + " \"transient\" : {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\" : {" + + " \"rolling_window\" : \"3700\"" + + " }" + + " }" + + " }" + + " }" + + "}"); assertTrue(actual.similar(expected)); } @Test public void ignoreNonTransientNonPersistentSettingsTest() throws IOException { - String settings = "{" + - " \"transient\": {" + - " \"plugins.query.metrics.rolling_window\": \"3700\"" + - " }," + - " \"persistent\": {" + - " \"plugins.sql.slowlog\": \"2\"" + - " }," + - " \"hello\": {" + - " \"world\" : {" + - " \"name\" : \"John Doe\"" + - " }" + - " }" + - "}"; + String settings = + "{" + + " \"transient\": {" + + " \"plugins.query.metrics.rolling_window\": \"3700\"" + + " }," + + " \"persistent\": {" + + " \"plugins.sql.slowlog\": \"2\"" + + " }," + + " \"hello\": {" + + " \"world\" : {" + + " \"name\" : \"John Doe\"" + + " }" + + " }" + + "}"; JSONObject actual = updateViaSQLSettingsAPI(settings); - JSONObject expected = new JSONObject("{" + - " \"acknowledged\" : true," + - " \"persistent\" : {" + - " \"plugins\" : {" + - " \"sql\" : {" + - " \"slowlog\" : \"2\"" + - " }" + - " }" + - " }," + - " \"transient\" : {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\" : {" + - " \"rolling_window\" : \"3700\"" + - " }" + - " }" + - " }" + - " }" + - "}"); + JSONObject expected = + new JSONObject( + "{" + + " \"acknowledged\" : true," + + " \"persistent\" : {" + + " \"plugins\" : {" + + " \"sql\" : {" + + " \"slowlog\" : \"2\"" + + " }" + + " }" + + " }," + + " \"transient\" : {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\" : {" + + " \"rolling_window\" : \"3700\"" + + " }" + + " }" + + " }" + + " }" + + "}"); assertTrue(actual.similar(expected)); } @Test public void sqlCombinedMixedSettingTest() throws IOException { - String settings = "{" + - " \"transient\": {" + - " \"plugins.query.metrics.rolling_window\": \"3700\"" + - " }," + - " \"persistent\": {" + - " \"plugins\": {" + - " \"sql\": {" + - " \"slowlog\": \"1\"" + - " }" + - " }" + - " }," + - " \"hello\": {" + - " \"world\": {" + - " \"city\": \"Seattle\"" + - " }" + - " }" + - "}"; + String settings = + "{" + + " \"transient\": {" + + " \"plugins.query.metrics.rolling_window\": \"3700\"" + + " }," + + " \"persistent\": {" + + " \"plugins\": {" + + " \"sql\": {" + + " \"slowlog\": \"1\"" + + " }" + + " }" + + " }," + + " \"hello\": {" + + " \"world\": {" + + " \"city\": \"Seattle\"" + + " }" + + " }" + + "}"; JSONObject actual = updateViaSQLSettingsAPI(settings); - JSONObject expected = new JSONObject("{" + - " \"acknowledged\" : true," + - " \"persistent\" : {" + - " \"plugins\" : {" + - " \"sql\" : {" + - " \"slowlog\" : \"1\"" + - " }" + - " }" + - " }," + - " \"transient\" : {" + - " \"plugins\" : {" + - " \"query\" : {" + - " \"metrics\" : {" + - " \"rolling_window\" : \"3700\"" + - " }" + - " }" + - " }" + - " }" + - "}"); + JSONObject expected = + new JSONObject( + "{" + + " \"acknowledged\" : true," + + " \"persistent\" : {" + + " \"plugins\" : {" + + " \"sql\" : {" + + " \"slowlog\" : \"1\"" + + " }" + + " }" + + " }," + + " \"transient\" : {" + + " \"plugins\" : {" + + " \"query\" : {" + + " \"metrics\" : {" + + " \"rolling_window\" : \"3700\"" + + " }" + + " }" + + " }" + + " }" + + "}"); assertTrue(actual.similar(expected)); } @Test public void nonRegisteredSQLSettingsThrowException() throws IOException { - String settings = "{" + - " \"transient\": {" + - " \"plugins.sql.query.state.city\": \"Seattle\"" + - " }" + - "}"; + String settings = + "{" + + " \"transient\": {" + + " \"plugins.sql.query.state.city\": \"Seattle\"" + + " }" + + "}"; JSONObject actual; Response response = null; @@ -421,8 +450,7 @@ public void nonRegisteredSQLSettingsThrowException() throws IOException { assertThat(actual.query("/error/type"), equalTo("settings_exception")); assertThat( actual.query("/error/reason"), - equalTo("transient setting [plugins.sql.query.state.city], not recognized") - ); + equalTo("transient setting [plugins.sql.query.state.city], not recognized")); } protected static JSONObject updateViaSQLSettingsAPI(String body) throws IOException { diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/PreparedStatementIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/PreparedStatementIT.java index 88f72d1907..dd177ec1f1 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/PreparedStatementIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/PreparedStatementIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import java.io.IOException; @@ -25,27 +24,34 @@ protected void init() throws Exception { public void testPreparedStatement() throws IOException { int ageToCompare = 35; - JSONObject response = executeRequest(String.format("{\n" + - " \"query\": \"SELECT * FROM %s WHERE age > ? AND state in (?, ?) LIMIT ?\",\n" + - " \"parameters\": [\n" + - " {\n" + - " \"type\": \"integer\",\n" + - " \"value\": \"" + ageToCompare + "\"\n" + - " },\n" + - " {\n" + - " \"type\": \"string\",\n" + - " \"value\": \"TN\"\n" + - " },\n" + - " {\n" + - " \"type\": \"string\",\n" + - " \"value\": \"UT\"\n" + - " },\n" + - " {\n" + - " \"type\": \"integer\",\n" + - " \"value\": \"20\"\n" + - " }\n" + - " ]\n" + - "}", TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeRequest( + String.format( + "{\n" + + " \"query\": \"SELECT * FROM %s WHERE age > ? AND state in (?, ?) LIMIT" + + " ?\",\n" + + " \"parameters\": [\n" + + " {\n" + + " \"type\": \"integer\",\n" + + " \"value\": \"" + + ageToCompare + + "\"\n" + + " },\n" + + " {\n" + + " \"type\": \"string\",\n" + + " \"value\": \"TN\"\n" + + " },\n" + + " {\n" + + " \"type\": \"string\",\n" + + " \"value\": \"UT\"\n" + + " },\n" + + " {\n" + + " \"type\": \"integer\",\n" + + " \"value\": \"20\"\n" + + " }\n" + + " ]\n" + + "}", + TestsConstants.TEST_INDEX_ACCOUNT)); Assert.assertTrue(response.has("hits")); Assert.assertTrue(response.getJSONObject("hits").has("hits")); @@ -58,23 +64,23 @@ public void testPreparedStatement() throws IOException { } } - /* currently the integ test case will fail if run using Intellj, have to run using gradle command - * because the integ test cluster created by IntellJ has http diabled, need to spend some time later to - * figure out how to configure the integ test cluster properly. Related online resources: - * https://discuss.elastic.co/t/http-enabled-with-OpenSearchIntegTestCase/102032 - * https://discuss.elastic.co/t/help-with-OpenSearchIntegTestCase/105245 - @Override - protected Collection> nodePlugins() { - return Arrays.asList(MockTcpTransportPlugin.class); - } + /* currently the integ test case will fail if run using Intellj, have to run using gradle command + * because the integ test cluster created by IntellJ has http diabled, need to spend some time later to + * figure out how to configure the integ test cluster properly. Related online resources: + * https://discuss.elastic.co/t/http-enabled-with-OpenSearchIntegTestCase/102032 + * https://discuss.elastic.co/t/help-with-OpenSearchIntegTestCase/105245 + @Override + protected Collection> nodePlugins() { + return Arrays.asList(MockTcpTransportPlugin.class); + } - @Override - protected Settings nodeSettings(int nodeOrdinal) { - return Settings.builder().put(super.nodeSettings(nodeOrdinal)) - // .put("node.mode", "network") - .put("http.enabled", true) - //.put("http.type", "netty4") - .build(); - } - */ + @Override + protected Settings nodeSettings(int nodeOrdinal) { + return Settings.builder().put(super.nodeSettings(nodeOrdinal)) + // .put("node.mode", "network") + .put("http.enabled", true) + //.put("http.type", "netty4") + .build(); + } + */ } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/PrettyFormatResponseIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/PrettyFormatResponseIT.java index ef80098df6..70f8a3c433 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/PrettyFormatResponseIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/PrettyFormatResponseIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static java.util.stream.Collectors.toSet; @@ -30,6 +29,7 @@ import org.opensearch.client.Request; /** + *
  * PrettyFormatResponseIT will likely be excluding some of the tests written in PrettyFormatResponseTest since
  * those tests were asserting on class objects directly. These updated tests will only be making assertions based
  * on the REST response.
@@ -40,21 +40,32 @@
  * Tests from original integ tests excluded:
  * - noIndexType()
  * - withIndexType()
+ * 
*/ public class PrettyFormatResponseIT extends SQLIntegTestCase { - private static final Set allAccountFields = Sets.newHashSet( - "account_number", "balance", "firstname", "lastname", "age", "gender", "address", "employer", - "email", "city", "state" - ); + private static final Set allAccountFields = + Sets.newHashSet( + "account_number", + "balance", + "firstname", + "lastname", + "age", + "gender", + "address", + "employer", + "email", + "city", + "state"); private static final Set regularFields = Sets.newHashSet("someField", "myNum"); - private static final Set messageFields = Sets.newHashSet( - "message.dayOfWeek", "message.info", "message.author"); + private static final Set messageFields = + Sets.newHashSet("message.dayOfWeek", "message.info", "message.author"); - private static final Set messageFieldsWithNestedFunction = Sets.newHashSet( - "nested(message.dayOfWeek)", "nested(message.info)", "nested(message.author)"); + private static final Set messageFieldsWithNestedFunction = + Sets.newHashSet( + "nested(message.dayOfWeek)", "nested(message.info)", "nested(message.author)"); private static final Set commentFields = Sets.newHashSet("comment.data", "comment.likes"); @@ -83,19 +94,20 @@ protected Request getSqlRequest(String request, boolean explain) { public void wrongIndexType() throws IOException { String type = "wrongType"; try { - executeQuery(String.format(Locale.ROOT, "SELECT * FROM %s/%s", - TestsConstants.TEST_INDEX_ACCOUNT, type)); + executeQuery( + String.format( + Locale.ROOT, "SELECT * FROM %s/%s", TestsConstants.TEST_INDEX_ACCOUNT, type)); } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), - is(String.format(Locale.ROOT, "Index type %s does not exist", type))); + assertThat( + e.getMessage(), is(String.format(Locale.ROOT, "Index type %s does not exist", type))); } } @Test public void selectAll() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format(Locale.ROOT, "SELECT * FROM %s", TestsConstants.TEST_INDEX_ACCOUNT)); // This also tests that .keyword fields are ignored when SELECT * is called assertContainsColumnsInAnyOrder(getSchema(response), allAccountFields); @@ -104,9 +116,12 @@ public void selectAll() throws IOException { @Test public void selectNames() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT firstname, lastname FROM %s", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT firstname, lastname FROM %s", + TestsConstants.TEST_INDEX_ACCOUNT)); assertContainsColumns(getSchema(response), nameFields); assertContainsData(getDataRows(response), nameFields); @@ -115,13 +130,15 @@ public void selectNames() throws IOException { @Ignore("Semantic analysis takes care of this") @Test public void selectWrongField() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT wrongField FROM %s", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, "SELECT wrongField FROM %s", TestsConstants.TEST_INDEX_ACCOUNT)); assertThat(getSchema(response).length(), equalTo(0)); - // DataRows object will still get populated with SearchHits but since wrongField is not available in the Map + // DataRows object will still get populated with SearchHits but since wrongField is not + // available in the Map // each row in the response will be empty // TODO Perhaps a code change should be made to format logic to ensure a // 'datarows' length of 0 in response for this case @@ -131,9 +148,12 @@ public void selectWrongField() throws IOException { @Test @Ignore("_score tested in V2 engine - @see org.opensearch.sql.sql.ScoreQueryIT") public void selectScore() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT _score FROM %s WHERE SCORE(match_phrase(phrase, 'brown fox'))", - TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT _score FROM %s WHERE SCORE(match_phrase(phrase, 'brown fox'))", + TestsConstants.TEST_INDEX_PHRASE)); List fields = Collections.singletonList("_score"); assertContainsColumns(getSchema(response), fields); @@ -142,14 +162,14 @@ public void selectScore() throws IOException { @Test public void selectAllFromNestedWithoutFieldInFrom() throws IOException { - assertNestedFieldQueryResultContainsColumnsAndData("SELECT * FROM %s", - regularFields, fields("message", "comment")); + assertNestedFieldQueryResultContainsColumnsAndData( + "SELECT * FROM %s", regularFields, fields("message", "comment")); } @Test public void selectAllFromNestedWithFieldInFrom() throws IOException { - assertNestedFieldQueryResultContainsColumnsAndData("SELECT * FROM %s e, e.message m", - regularFields, messageFields); + assertNestedFieldQueryResultContainsColumnsAndData( + "SELECT * FROM %s e, e.message m", regularFields, messageFields); } @Test @@ -161,29 +181,27 @@ public void selectAllFromNestedWithMultipleFieldsInFrom() throws IOException { @Test public void selectAllNestedFromNestedWithFieldInFrom() throws IOException { - assertNestedFieldQueryResultContainsColumnsAndData("SELECT m.* FROM %s e, e.message m", - messageFields); + assertNestedFieldQueryResultContainsColumnsAndData( + "SELECT m.* FROM %s e, e.message m", messageFields); } @Test public void selectSpecificRegularFieldAndAllFromNestedWithFieldInFrom() throws IOException { assertNestedFieldQueryResultContainsColumnsAndData( - "SELECT e.someField, m.* FROM %s e, e.message m", - fields("someField"), messageFields); + "SELECT e.someField, m.* FROM %s e, e.message m", fields("someField"), messageFields); } /** - * Execute the query against index with nested fields and assert result contains columns and data as expected. + * Execute the query against index with nested fields and assert result contains columns and data + * as expected. */ @SafeVarargs - private final void assertNestedFieldQueryResultContainsColumnsAndData(String query, - Set... expectedFieldNames) - throws IOException { + private final void assertNestedFieldQueryResultContainsColumnsAndData( + String query, Set... expectedFieldNames) throws IOException { JSONObject response = executeQuery(String.format(Locale.ROOT, query, TestsConstants.TEST_INDEX_NESTED_TYPE)); - Set allExpectedFieldNames = Stream.of(expectedFieldNames). - flatMap(Set::stream). - collect(toSet()); + Set allExpectedFieldNames = + Stream.of(expectedFieldNames).flatMap(Set::stream).collect(toSet()); assertContainsColumnsInAnyOrder(getSchema(response), allExpectedFieldNames); assertContainsData(getDataRows(response), allExpectedFieldNames); @@ -195,24 +213,31 @@ private Set fields(String... fieldNames) { @Test public void selectNestedFields() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT nested(message.info), someField FROM %s", - TestsConstants.TEST_INDEX_NESTED_TYPE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT nested(message.info), someField FROM %s", + TestsConstants.TEST_INDEX_NESTED_TYPE)); List fields = Arrays.asList("nested(message.info)", "someField"); assertContainsColumns(getSchema(response), fields); assertContainsData(getDataRows(response), fields); - // The nested test index being used contains 5 entries but one of them has an array of 2 message objects, so + // The nested test index being used contains 5 entries but one of them has an array of 2 message + // objects, so // we check to see if the amount of data rows is 6 since that is the result after flattening assertThat(getDataRows(response).length(), equalTo(6)); } @Test public void selectNestedFieldWithWildcard() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT nested(message.*) FROM %s", - TestsConstants.TEST_INDEX_NESTED_TYPE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT nested(message.*) FROM %s", + TestsConstants.TEST_INDEX_NESTED_TYPE)); assertContainsColumnsInAnyOrder(getSchema(response), messageFieldsWithNestedFunction); assertContainsData(getDataRows(response), messageFields); @@ -221,11 +246,13 @@ public void selectNestedFieldWithWildcard() throws IOException { @Test public void selectWithWhere() throws IOException { int balanceToCompare = 30000; - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT balance " + - "FROM %s " + - "WHERE balance > %d", - TestsConstants.TEST_INDEX_ACCOUNT, balanceToCompare)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT balance " + "FROM %s " + "WHERE balance > %d", + TestsConstants.TEST_INDEX_ACCOUNT, + balanceToCompare)); /* * Previously the DataRows map was used to check specific fields but the JDBC response for "datarows" is a @@ -243,9 +270,10 @@ public void selectWithWhere() throws IOException { @Test public void groupBySingleField() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s GROUP BY age", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, "SELECT * FROM %s GROUP BY age", TestsConstants.TEST_INDEX_ACCOUNT)); List fields = Collections.singletonList("age"); assertContainsColumns(getSchema(response), fields); @@ -254,9 +282,12 @@ public void groupBySingleField() throws IOException { @Test public void groupByMultipleFields() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s GROUP BY age, balance", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s GROUP BY age, balance", + TestsConstants.TEST_INDEX_ACCOUNT)); List fields = Arrays.asList("age", "balance"); assertContainsColumns(getSchema(response), fields); @@ -265,35 +296,42 @@ public void groupByMultipleFields() throws IOException { @Ignore("only work for legacy engine") public void testSizeAndTotal() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE balance > 30000 " + - "LIMIT 5", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE balance > 30000 LIMIT 5", + TestsConstants.TEST_INDEX_ACCOUNT)); JSONArray dataRows = getDataRows(response); assertThat(dataRows.length(), equalTo(5)); - // The value to compare to here was obtained by running the query in the plugin and looking at the SearchHits + // The value to compare to here was obtained by running the query in the plugin and looking at + // the SearchHits int totalHits = response.getInt("total"); assertThat(totalHits, equalTo(402)); } @Test public void testSizeWithGroupBy() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s GROUP BY age LIMIT 5", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s GROUP BY age LIMIT 5", + TestsConstants.TEST_INDEX_ACCOUNT)); assertThat(getDataRows(response).length(), equalTo(5)); } @Test public void aggregationFunctionInSelect() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT COUNT(*) FROM %s GROUP BY age", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT COUNT(*) FROM %s GROUP BY age", + TestsConstants.TEST_INDEX_ACCOUNT)); List fields = Arrays.asList("COUNT(*)"); assertContainsColumns(getSchema(response), fields); @@ -310,9 +348,12 @@ public void aggregationFunctionInSelect() throws IOException { @Ignore("In MySQL and our new engine, the original text in SELECT is used as final column name") @Test public void aggregationFunctionInSelectCaseCheck() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT count(*) FROM %s GROUP BY age", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT count(*) FROM %s GROUP BY age", + TestsConstants.TEST_INDEX_ACCOUNT)); List fields = Arrays.asList("COUNT(*)"); assertContainsColumns(getSchema(response), fields); @@ -328,9 +369,12 @@ public void aggregationFunctionInSelectCaseCheck() throws IOException { @Ignore("only work for legacy engine") public void aggregationFunctionInSelectWithAlias() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT COUNT(*) AS total FROM %s GROUP BY age", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT COUNT(*) AS total FROM %s GROUP BY age", + TestsConstants.TEST_INDEX_ACCOUNT)); List fields = Arrays.asList("total"); assertContainsColumns(getSchema(response), fields); @@ -346,8 +390,10 @@ public void aggregationFunctionInSelectWithAlias() throws IOException { @Test public void aggregationFunctionInSelectNoGroupBy() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, "SELECT SUM(age) FROM %s", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, "SELECT SUM(age) FROM %s", TestsConstants.TEST_INDEX_ACCOUNT)); String ageSum = "SUM(age)"; assertContainsColumns(getSchema(response), Collections.singletonList(ageSum)); @@ -363,9 +409,12 @@ public void aggregationFunctionInSelectNoGroupBy() throws IOException { @Test public void multipleAggregationFunctionsInSelect() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT COUNT(*), AVG(age) FROM %s GROUP BY age", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT COUNT(*), AVG(age) FROM %s GROUP BY age", + TestsConstants.TEST_INDEX_ACCOUNT)); List fields = Arrays.asList("COUNT(*)", "AVG(age)"); assertContainsColumns(getSchema(response), fields); @@ -374,12 +423,12 @@ public void multipleAggregationFunctionsInSelect() throws IOException { @Test public void aggregationFunctionInHaving() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, - "SELECT gender " + - "FROM %s " + - "GROUP BY gender " + - "HAVING count(*) > 500", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT gender FROM %s GROUP BY gender HAVING count(*) > 500", + TestsConstants.TEST_INDEX_ACCOUNT)); String ageSum = "gender"; assertContainsColumns(getSchema(response), Collections.singletonList(ageSum)); @@ -390,20 +439,24 @@ public void aggregationFunctionInHaving() throws IOException { } /** - * This case doesn't seem to be supported by the plugin at the moment. - * Looks like the painless script of the inner function is put inside the aggregation function but - * this syntax may not be correct since it returns 0 which is the default value (since 0 is returned in - * cases like COUNT(wrongField) as well). + * This case doesn't seem to be supported by the plugin at the moment. Looks like the painless + * script of the inner function is put inside the aggregation function but this syntax may not be + * correct since it returns 0 which is the default value (since 0 is returned in cases like + * COUNT(wrongField) as well). */ -// @Test -// public void nestedAggregationFunctionInSelect() { -// String query = String.format(Locale.ROOT, "SELECT SUM(SQRT(age)) FROM age GROUP BY age", TEST_INDEX_ACCOUNT); -// } + // @Test + // public void nestedAggregationFunctionInSelect() { + // String query = String.format(Locale.ROOT, "SELECT SUM(SQRT(age)) FROM age GROUP BY age", + // TEST_INDEX_ACCOUNT); + // } @Test public void fieldsWithAlias() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT firstname AS first, age AS a FROM %s", - TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT firstname AS first, age AS a FROM %s", + TestsConstants.TEST_INDEX_ACCOUNT)); Map aliases = new HashMap<>(); aliases.put("firstname", "first"); @@ -414,25 +467,32 @@ public void fieldsWithAlias() throws IOException { @Test public void indexWithMissingFields() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT phrase, insert_time2 " + - "FROM %s " + - "WHERE match_phrase(phrase, 'brown fox')", - TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT phrase, insert_time2 " + + "FROM %s " + + "WHERE match_phrase(phrase, 'brown fox')", + TestsConstants.TEST_INDEX_PHRASE)); JSONArray dataRowEntry = getDataRows(response).getJSONArray(0); assertThat(dataRowEntry.length(), equalTo(2)); assertThat(dataRowEntry.get(0), equalTo("brown fox")); - assertThat(dataRowEntry.get(1), - equalTo(JSONObject.NULL)); // TODO See if this null check is failing + assertThat( + dataRowEntry.get(1), equalTo(JSONObject.NULL)); // TODO See if this null check is failing } @Test public void joinQuery() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT b1.balance, b1.age, b2.firstname " + - "FROM %s b1 JOIN %s b2 ON b1.age = b2.age", - TestsConstants.TEST_INDEX_ACCOUNT, TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT b1.balance, b1.age, b2.firstname " + + "FROM %s b1 JOIN %s b2 ON b1.age = b2.age", + TestsConstants.TEST_INDEX_ACCOUNT, + TestsConstants.TEST_INDEX_ACCOUNT)); List fields = Arrays.asList("b1.balance", "b1.age", "b2.firstname"); assertContainsColumns(getSchema(response), fields); @@ -441,9 +501,14 @@ public void joinQuery() throws IOException { @Test public void joinQueryWithAlias() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, "SELECT b1.balance AS bal, " + - " b1.age AS age, b2.firstname AS name FROM %s b1 JOIN %s b2 ON b1.age = b2.age", - TestsConstants.TEST_INDEX_ACCOUNT, TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT b1.balance AS bal, b1.age AS age, b2.firstname AS name FROM %s b1 JOIN %s" + + " b2 ON b1.age = b2.age", + TestsConstants.TEST_INDEX_ACCOUNT, + TestsConstants.TEST_INDEX_ACCOUNT)); Map aliases = new HashMap<>(); aliases.put("b1.balance", "bal"); @@ -456,16 +521,20 @@ public void joinQueryWithAlias() throws IOException { @Test public void joinQueryWithObjectFieldInSelect() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT c.name.firstname, d.name.lastname " + - "FROM %s c JOIN %s d ON d.hname = c.house", - TestsConstants.TEST_INDEX_GAME_OF_THRONES, - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT c.name.firstname, d.name.lastname " + + "FROM %s c JOIN %s d ON d.hname = c.house", + TestsConstants.TEST_INDEX_GAME_OF_THRONES, + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); List fields = Arrays.asList("c.name.firstname", "d.name.lastname"); assertContainsColumns(getSchema(response), fields); - // d.name.lastname is null here since entries with hname don't have a name.lastname entry, so only length is + // d.name.lastname is null here since entries with hname don't have a name.lastname entry, so + // only length is // checked JSONArray dataRows = getDataRows(response); assertThat(dataRows.length(), greaterThan(0)); @@ -476,10 +545,13 @@ public void joinQueryWithObjectFieldInSelect() throws IOException { @Test public void joinQuerySelectOnlyOnOneTable() throws Exception { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT b1.age " + - "FROM %s b1 JOIN %s b2 ON b1.firstname = b2.firstname", - TestsConstants.TEST_INDEX_ACCOUNT, TestsConstants.TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT b1.age FROM %s b1 JOIN %s b2 ON b1.firstname = b2.firstname", + TestsConstants.TEST_INDEX_ACCOUNT, + TestsConstants.TEST_INDEX_ACCOUNT)); List fields = Collections.singletonList("b1.age"); assertContainsColumns(getSchema(response), fields); @@ -508,8 +580,12 @@ private void testFieldOrder(final String[] expectedFields, final Object[] expect throws IOException { final String fields = String.join(", ", expectedFields); - final String query = String.format(Locale.ROOT, "SELECT %s FROM %s " + - "WHERE email='amberduke@pyrami.com'", fields, TestsConstants.TEST_INDEX_ACCOUNT); + final String query = + String.format( + Locale.ROOT, + "SELECT %s FROM %s WHERE email='amberduke@pyrami.com'", + fields, + TestsConstants.TEST_INDEX_ACCOUNT); final JSONObject result = executeQuery(query); for (int i = 0; i < expectedFields.length; ++i) { diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/PrettyFormatterIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/PrettyFormatterIT.java index 463a0bc6db..c81839a6e5 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/PrettyFormatterIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/PrettyFormatterIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.hamcrest.Matchers.equalTo; @@ -29,16 +28,18 @@ protected void init() throws Exception { public void assertExplainPrettyFormatted() throws IOException { String query = StringUtils.format("SELECT firstname FROM %s", TEST_INDEX_ACCOUNT); - String notPrettyExplainOutputFilePath = TestUtils.getResourceFilePath( - "src/test/resources/expectedOutput/explainIT_format_not_pretty.json"); + String notPrettyExplainOutputFilePath = + TestUtils.getResourceFilePath( + "src/test/resources/expectedOutput/explainIT_format_not_pretty.json"); String notPrettyExplainOutput = Files.toString(new File(notPrettyExplainOutputFilePath), StandardCharsets.UTF_8); assertThat(executeExplainRequest(query, ""), equalTo(notPrettyExplainOutput)); assertThat(executeExplainRequest(query, "pretty=false"), equalTo(notPrettyExplainOutput)); - String prettyExplainOutputFilePath = TestUtils.getResourceFilePath( - "src/test/resources/expectedOutput/explainIT_format_pretty.json"); + String prettyExplainOutputFilePath = + TestUtils.getResourceFilePath( + "src/test/resources/expectedOutput/explainIT_format_pretty.json"); String prettyExplainOutput = Files.toString(new File(prettyExplainOutputFilePath), StandardCharsets.UTF_8); diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/QueryAnalysisIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/QueryAnalysisIT.java index 3a58b7ffc0..62a87d3bff 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/QueryAnalysisIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/QueryAnalysisIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.hamcrest.Matchers.containsString; @@ -24,9 +23,7 @@ import org.opensearch.sql.legacy.exception.SqlFeatureNotImplementedException; import org.opensearch.sql.legacy.utils.StringUtils; -/** - * Integration test for syntax and semantic analysis against query by new ANTLR parser. - */ +/** Integration test for syntax and semantic analysis against query by new ANTLR parser. */ public class QueryAnalysisIT extends SQLIntegTestCase { @Override @@ -41,9 +38,7 @@ public void missingFromClauseShouldThrowSyntaxException() { @Test public void unsupportedOperatorShouldThrowSyntaxException() { - queryShouldThrowSyntaxException( - "SELECT * FROM opensearch-sql_test_index_bank WHERE age <=> 1" - ); + queryShouldThrowSyntaxException("SELECT * FROM opensearch-sql_test_index_bank WHERE age <=> 1"); } @Test @@ -51,8 +46,8 @@ public void nonExistingFieldNameShouldThrowSemanticException() { queryShouldThrowSemanticException( "SELECT * FROM opensearch-sql_test_index_bank WHERE balance1 = 1000", "Field [balance1] cannot be found or used here." - //"Did you mean [balance]?" - ); + // "Did you mean [balance]?" + ); } @Test @@ -60,16 +55,15 @@ public void nonExistingIndexAliasShouldThrowSemanticException() { queryShouldThrowSemanticException( "SELECT * FROM opensearch-sql_test_index_bank b WHERE a.balance = 1000", "Field [a.balance] cannot be found or used here." - //"Did you mean [b.balance]?" - ); + // "Did you mean [b.balance]?" + ); } @Test public void indexJoinNonNestedFieldShouldThrowSemanticException() { queryShouldThrowSemanticException( "SELECT * FROM opensearch-sql_test_index_bank b1, b1.firstname f1", - "Operator [JOIN] cannot work with [INDEX, KEYWORD]." - ); + "Operator [JOIN] cannot work with [INDEX, KEYWORD]."); } @Test @@ -77,8 +71,7 @@ public void scalarFunctionCallWithTypoInNameShouldThrowSemanticException() { queryShouldThrowSemanticException( "SELECT * FROM opensearch-sql_test_index_bank WHERE ABSa(age) = 1", "Function [ABSA] cannot be found or used here.", - "Did you mean [ABS]?" - ); + "Did you mean [ABS]?"); } @Test @@ -86,17 +79,16 @@ public void scalarFunctionCallWithWrongTypeArgumentShouldThrowSemanticException( queryShouldThrowSemanticException( "SELECT * FROM opensearch-sql_test_index_bank WHERE LOG(lastname) = 1", "Function [LOG] cannot work with [KEYWORD].", - "Usage: LOG(NUMBER T) -> DOUBLE or LOG(NUMBER T, NUMBER) -> DOUBLE" - ); + "Usage: LOG(NUMBER T) -> DOUBLE or LOG(NUMBER T, NUMBER) -> DOUBLE"); } @Test public void aggregateFunctionCallWithWrongNumberOfArgumentShouldThrowSemanticException() { queryShouldThrowSemanticException( - "SELECT city FROM opensearch-sql_test_index_bank GROUP BY city HAVING MAX(age, birthdate) > 1", + "SELECT city FROM opensearch-sql_test_index_bank GROUP BY city HAVING MAX(age, birthdate) >" + + " 1", "Function [MAX] cannot work with [INTEGER, DATE].", - "Usage: MAX(NUMBER T) -> T" - ); + "Usage: MAX(NUMBER T) -> T"); } @Test @@ -104,8 +96,7 @@ public void compareIntegerFieldWithBooleanShouldThrowSemanticException() { queryShouldThrowSemanticException( "SELECT * FROM opensearch-sql_test_index_bank b WHERE b.age IS FALSE", "Operator [IS] cannot work with [INTEGER, BOOLEAN].", - "Usage: Please use compatible types from each side." - ); + "Usage: Please use compatible types from each side."); } @Test @@ -113,8 +104,7 @@ public void compareNumberFieldWithStringShouldThrowSemanticException() { queryShouldThrowSemanticException( "SELECT * FROM opensearch-sql_test_index_bank b WHERE b.age >= 'test'", "Operator [>=] cannot work with [INTEGER, STRING].", - "Usage: Please use compatible types from each side." - ); + "Usage: Please use compatible types from each side."); } @Test @@ -122,43 +112,38 @@ public void compareLogFunctionCallWithNumberFieldWithStringShouldThrowSemanticEx queryShouldThrowSemanticException( "SELECT * FROM opensearch-sql_test_index_bank b WHERE LOG(b.balance) != 'test'", "Operator [!=] cannot work with [DOUBLE, STRING].", - "Usage: Please use compatible types from each side." - ); + "Usage: Please use compatible types from each side."); } @Test public void unionNumberFieldWithStringShouldThrowSemanticException() { queryShouldThrowSemanticException( - "SELECT age FROM opensearch-sql_test_index_bank" + - " UNION SELECT address FROM opensearch-sql_test_index_bank", - "Operator [UNION] cannot work with [INTEGER, TEXT]." - ); + "SELECT age FROM opensearch-sql_test_index_bank" + + " UNION SELECT address FROM opensearch-sql_test_index_bank", + "Operator [UNION] cannot work with [INTEGER, TEXT]."); } @Test public void minusBooleanFieldWithDateShouldThrowSemanticException() { queryShouldThrowSemanticException( - "SELECT male FROM opensearch-sql_test_index_bank" + - " MINUS SELECT birthdate FROM opensearch-sql_test_index_bank", - "Operator [MINUS] cannot work with [BOOLEAN, DATE]." - ); + "SELECT male FROM opensearch-sql_test_index_bank" + + " MINUS SELECT birthdate FROM opensearch-sql_test_index_bank", + "Operator [MINUS] cannot work with [BOOLEAN, DATE]."); } @Test public void useInClauseWithIncompatibleFieldTypesShouldFail() { queryShouldThrowSemanticException( - "SELECT * FROM opensearch-sql_test_index_bank WHERE male " + - " IN (SELECT 1 FROM opensearch-sql_test_index_bank)", - "Operator [IN] cannot work with [BOOLEAN, INTEGER]." - ); + "SELECT * FROM opensearch-sql_test_index_bank WHERE male " + + " IN (SELECT 1 FROM opensearch-sql_test_index_bank)", + "Operator [IN] cannot work with [BOOLEAN, INTEGER]."); } @Test public void queryWithNestedFunctionShouldFail() { queryShouldThrowFeatureNotImplementedException( "SELECT abs(log(balance)) FROM opensearch-sql_test_index_bank", - "Nested function calls like [abs(log(balance))] are not supported yet" - ); + "Nested function calls like [abs(log(balance))] are not supported yet"); } @Test @@ -170,29 +155,24 @@ public void nestedFunctionWithMathConstantAsInnerFunctionShouldPass() { public void aggregateWithFunctionAggregatorShouldFail() { queryShouldThrowFeatureNotImplementedException( "SELECT max(log(age)) FROM opensearch-sql_test_index_bank", - "Aggregation calls with function aggregator like [max(log(age))] are not supported yet" - ); + "Aggregation calls with function aggregator like [max(log(age))] are not supported yet"); } @Test public void queryWithUnsupportedFunctionShouldFail() { queryShouldThrowFeatureNotImplementedException( "SELECT balance DIV age FROM opensearch-sql_test_index_bank", - "Operator [DIV] is not supported yet" - ); + "Operator [DIV] is not supported yet"); } @Test public void useNegativeNumberConstantShouldPass() { queryShouldPassAnalysis( - "SELECT * FROM opensearch-sql_test_index_bank " + - "WHERE age > -1 AND balance < -123.456789" - ); + "SELECT * FROM opensearch-sql_test_index_bank " + + "WHERE age > -1 AND balance < -123.456789"); } - /** - * Run the query with cluster setting changed and cleaned after complete - */ + /** Run the query with cluster setting changed and cleaned after complete */ private void runWithClusterSetting(ClusterSetting setting, Runnable query) { try { updateClusterSettings(setting); @@ -201,7 +181,8 @@ private void runWithClusterSetting(ClusterSetting setting, Runnable query) { throw new IllegalStateException( StringUtils.format("Exception raised when running with cluster setting [%s]", setting)); } finally { - // Clean up or OpenSearch will throw java.lang.AssertionError: test leaves persistent cluster metadata behind + // Clean up or OpenSearch will throw java.lang.AssertionError: test leaves persistent cluster + // metadata behind try { updateClusterSettings(setting.nullify()); } catch (IOException e) { @@ -218,20 +199,19 @@ private void queryShouldThrowSemanticException(String query, String... expectedM queryShouldThrowException(query, SemanticAnalysisException.class, expectedMsgs); } - private void queryShouldThrowFeatureNotImplementedException(String query, - String... expectedMsgs) { - queryShouldThrowExceptionWithRestStatus(query, SqlFeatureNotImplementedException.class, - SERVICE_UNAVAILABLE, expectedMsgs); + private void queryShouldThrowFeatureNotImplementedException( + String query, String... expectedMsgs) { + queryShouldThrowExceptionWithRestStatus( + query, SqlFeatureNotImplementedException.class, SERVICE_UNAVAILABLE, expectedMsgs); } - private void queryShouldThrowException(String query, Class exceptionType, - String... expectedMsgs) { + private void queryShouldThrowException( + String query, Class exceptionType, String... expectedMsgs) { queryShouldThrowExceptionWithRestStatus(query, exceptionType, BAD_REQUEST, expectedMsgs); } - private void queryShouldThrowExceptionWithRestStatus(String query, Class exceptionType, - RestStatus status, - String... expectedMsgs) { + private void queryShouldThrowExceptionWithRestStatus( + String query, Class exceptionType, RestStatus status, String... expectedMsgs) { try { executeQuery(query); Assert.fail("Expected ResponseException, but none was thrown for query: " + query); @@ -244,8 +224,8 @@ private void queryShouldThrowExceptionWithRestStatus(String query, Class } } catch (IOException e) { throw new IllegalStateException( - "Unexpected IOException raised rather than expected AnalysisException for query: " + - query); + "Unexpected IOException raised rather than expected AnalysisException for query: " + + query); } } @@ -285,5 +265,4 @@ void assertBodyContains(String content) { assertThat(body, containsString(content)); } } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/QueryFunctionsIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/QueryFunctionsIT.java index c538db830f..b36144ce5f 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/QueryFunctionsIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/QueryFunctionsIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.hamcrest.Matchers.anyOf; @@ -44,10 +43,11 @@ public class QueryFunctionsIT extends SQLIntegTestCase { private static final String FROM_PHRASE = "FROM " + TEST_INDEX_PHRASE; /** + *
    * TODO Looks like Math/Date Functions test all use the same query() and execute() functions
    * TODO execute/featureValueOf/hits functions are the same as used in NestedFieldQueryIT, should refactor into util
+   * 
*/ - @Override protected void init() throws Exception { loadIndex(Index.ACCOUNT); @@ -58,63 +58,39 @@ protected void init() throws Exception { @Test public void query() throws IOException { assertThat( - query( - "SELECT state", - FROM_ACCOUNTS, - "WHERE QUERY('CA')" - ), - hits( - hasValueForFields("CA", "state") - ) - ); + query("SELECT state", FROM_ACCOUNTS, "WHERE QUERY('CA')"), + hits(hasValueForFields("CA", "state"))); } @Test public void matchQueryRegularField() throws IOException { assertThat( - query( - "SELECT firstname", - FROM_ACCOUNTS, - "WHERE MATCH_QUERY(firstname, 'Ayers')" - ), - hits( - hasValueForFields("Ayers", "firstname") - ) - ); + query("SELECT firstname", FROM_ACCOUNTS, "WHERE MATCH_QUERY(firstname, 'Ayers')"), + hits(hasValueForFields("Ayers", "firstname"))); } @Test public void matchQueryNestedField() throws IOException { SearchHit[] hits = query("SELECT comment.data", FROM_NESTED, "WHERE MATCH_QUERY(NESTED(comment.data), 'aa')") - .getHits().getHits(); + .getHits() + .getHits(); Map source = hits[0].getSourceAsMap(); // SearchHits innerHits = hits[0].getInnerHits().get("comment"); assertThat( - query( - "SELECT comment.data", - FROM_NESTED, - "WHERE MATCH_QUERY(NESTED(comment.data), 'aa')" - ), + query("SELECT comment.data", FROM_NESTED, "WHERE MATCH_QUERY(NESTED(comment.data), 'aa')"), hits( - anyOf(hasNestedField("comment", "data", "aa"), - hasNestedArrayField("comment", "data", "aa")) - ) - ); + anyOf( + hasNestedField("comment", "data", "aa"), + hasNestedArrayField("comment", "data", "aa")))); } @Test public void scoreQuery() throws IOException { assertThat( query( - "SELECT firstname", - FROM_ACCOUNTS, - "WHERE SCORE(MATCH_QUERY(firstname, 'Ayers'), 10)" - ), - hits( - hasValueForFields("Ayers", "firstname") - ) - ); + "SELECT firstname", FROM_ACCOUNTS, "WHERE SCORE(MATCH_QUERY(firstname, 'Ayers'), 10)"), + hits(hasValueForFields("Ayers", "firstname"))); } @Test @@ -123,42 +99,24 @@ public void scoreQueryWithNestedField() throws IOException { query( "SELECT comment.data", FROM_NESTED, - "WHERE SCORE(MATCH_QUERY(NESTED(comment.data), 'ab'), 10)" - ), + "WHERE SCORE(MATCH_QUERY(NESTED(comment.data), 'ab'), 10)"), hits( - //hasValueForFields("ab", "comment.data") - hasNestedField("comment", - "data", "ab") - ) - ); + // hasValueForFields("ab", "comment.data") + hasNestedField("comment", "data", "ab"))); } @Test public void wildcardQuery() throws IOException { assertThat( - query( - "SELECT city", - FROM_ACCOUNTS, - "WHERE WILDCARD_QUERY(city.keyword, 'B*')" - ), - hits( - hasFieldWithPrefix("city", "B") - ) - ); + query("SELECT city", FROM_ACCOUNTS, "WHERE WILDCARD_QUERY(city.keyword, 'B*')"), + hits(hasFieldWithPrefix("city", "B"))); } @Test public void matchPhraseQuery() throws IOException { assertThat( - query( - "SELECT phrase", - FROM_PHRASE, - "WHERE MATCH_PHRASE(phrase, 'brown fox')" - ), - hits( - hasValueForFields("brown fox", "phrase") - ) - ); + query("SELECT phrase", FROM_PHRASE, "WHERE MATCH_PHRASE(phrase, 'brown fox')"), + hits(hasValueForFields("brown fox", "phrase"))); } @Test @@ -167,12 +125,8 @@ public void multiMatchQuerySingleField() throws IOException { query( "SELECT firstname", FROM_ACCOUNTS, - "WHERE MULTI_MATCH('query'='Ayers', 'fields'='firstname')" - ), - hits( - hasValueForFields("Ayers", "firstname") - ) - ); + "WHERE MULTI_MATCH('query'='Ayers', 'fields'='firstname')"), + hits(hasValueForFields("Ayers", "firstname"))); } @Test @@ -181,36 +135,30 @@ public void multiMatchQueryWildcardField() throws IOException { query( "SELECT firstname, lastname", FROM_ACCOUNTS, - "WHERE MULTI_MATCH('query'='Bradshaw', 'fields'='*name')" - ), - hits( - hasValueForFields("Bradshaw", "firstname", "lastname") - ) - ); + "WHERE MULTI_MATCH('query'='Bradshaw', 'fields'='*name')"), + hits(hasValueForFields("Bradshaw", "firstname", "lastname"))); } @Test public void numberLiteralInSelectField() { assertTrue( - executeQuery(StringUtils.format("SELECT 234234 AS number from %s", TEST_INDEX_ACCOUNT), - "jdbc") - .contains("234234") - ); + executeQuery( + StringUtils.format("SELECT 234234 AS number from %s", TEST_INDEX_ACCOUNT), "jdbc") + .contains("234234")); assertTrue( - executeQuery(StringUtils.format("SELECT 2.34234 AS number FROM %s", TEST_INDEX_ACCOUNT), - "jdbc") - .contains("2.34234") - ); + executeQuery( + StringUtils.format("SELECT 2.34234 AS number FROM %s", TEST_INDEX_ACCOUNT), "jdbc") + .contains("2.34234")); } private final Matcher hits(Matcher subMatcher) { - return featureValueOf("hits", everyItem(subMatcher), - resp -> Arrays.asList(resp.getHits().getHits())); + return featureValueOf( + "hits", everyItem(subMatcher), resp -> Arrays.asList(resp.getHits().getHits())); } - private FeatureMatcher featureValueOf(String name, Matcher subMatcher, - Function getter) { + private FeatureMatcher featureValueOf( + String name, Matcher subMatcher, Function getter) { return new FeatureMatcher(subMatcher, name, name) { @Override protected U featureValueOf(T actual) { @@ -220,6 +168,7 @@ protected U featureValueOf(T actual) { } /** + *
    * Create Matchers for each field and its value
    * Only one of the Matchers need to match (per hit)
    * 

@@ -228,36 +177,33 @@ protected U featureValueOf(T actual) { *

* Then the value "Ayers" can be found in either the firstname or lastname field. Only one of these fields * need to satisfy the query value to be evaluated as correct expected output. - * - * @param value The value to match for a field in the sourceMap + *

+ * @param value The value to match for a field in the sourceMap * @param fields A list of fields to match */ @SafeVarargs private final Matcher hasValueForFields(String value, String... fields) { return anyOf( - Arrays.asList(fields). - stream(). - map(field -> kv(field, is(value))). - collect(Collectors.toList())); + Arrays.asList(fields).stream() + .map(field -> kv(field, is(value))) + .collect(Collectors.toList())); } private final Matcher hasFieldWithPrefix(String field, String prefix) { - return featureValueOf(field, startsWith(prefix), - hit -> (String) hit.getSourceAsMap().get(field)); + return featureValueOf( + field, startsWith(prefix), hit -> (String) hit.getSourceAsMap().get(field)); } private final Matcher hasNestedField(String path, String field, String value) { - return featureValueOf(field, is(value), - hit -> ((HashMap) hit.getSourceAsMap().get(path)).get(field)); + return featureValueOf( + field, is(value), hit -> ((HashMap) hit.getSourceAsMap().get(path)).get(field)); } private final Matcher hasNestedArrayField(String path, String field, String value) { return new BaseMatcher() { @Override - public void describeTo(Description description) { - - } + public void describeTo(Description description) {} @Override public boolean matches(Object item) { @@ -275,7 +221,7 @@ private Matcher kv(String key, Matcher valMatcher) { } /*********************************************************** - Query Utility to Fetch Response for SQL + * Query Utility to Fetch Response for SQL ***********************************************************/ private SearchResponse query(String select, String from, String... statements) @@ -286,10 +232,11 @@ private SearchResponse query(String select, String from, String... statements) private SearchResponse execute(String sql) throws IOException { final JSONObject jsonObject = executeQuery(sql); - final XContentParser parser = new JsonXContentParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - new JsonFactory().createParser(jsonObject.toString())); + final XContentParser parser = + new JsonXContentParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + new JsonFactory().createParser(jsonObject.toString())); return SearchResponse.fromXContent(parser); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/QueryIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/QueryIT.java index f99285a90b..3f684deaa9 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/QueryIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/QueryIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.hamcrest.Matchers.allOf; @@ -44,6 +43,7 @@ public class QueryIT extends SQLIntegTestCase { /** + *
    * Currently commenting out tests related to JoinType index since there is an issue with mapping.
    * 

* Also ignoring the following tests as they are failing, will require investigation: @@ -57,10 +57,11 @@ public class QueryIT extends SQLIntegTestCase { * The following tests are being ignored because subquery is still running in OpenSearch transport thread: * - twoSubQueriesTest() * - inTermsSubQueryTest() + *

*/ + static final int BANK_INDEX_MALE_TRUE = 4; - final static int BANK_INDEX_MALE_TRUE = 4; - final static int BANK_INDEX_MALE_FALSE = 3; + static final int BANK_INDEX_MALE_FALSE = 3; @Override protected void init() throws Exception { @@ -87,92 +88,87 @@ public void queryEndWithSemiColonTest() { @Test public void searchTypeTest() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, "SELECT * FROM %s LIMIT 1000", - TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, "SELECT * FROM %s LIMIT 1000", TestsConstants.TEST_INDEX_PHRASE)); Assert.assertTrue(response.has("hits")); Assert.assertEquals(6, getTotalHits(response)); } @Test public void multipleFromTest() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, - "SELECT * FROM %s, %s LIMIT 2000", - TestsConstants.TEST_INDEX_BANK, TestsConstants.TEST_INDEX_BANK_TWO)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s, %s LIMIT 2000", + TestsConstants.TEST_INDEX_BANK, + TestsConstants.TEST_INDEX_BANK_TWO)); Assert.assertTrue(response.has("hits")); Assert.assertEquals(14, getTotalHits(response)); } @Test public void selectAllWithFieldReturnsAll() throws IOException { - JSONObject response = executeQuery(StringUtils.format( - "SELECT *, age " + - "FROM %s " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK - )); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT *, age FROM %s LIMIT 5", TestsConstants.TEST_INDEX_BANK)); checkSelectAllAndFieldResponseSize(response); } @Test public void selectAllWithFieldReverseOrder() throws IOException { - JSONObject response = executeQuery(StringUtils.format( - "SELECT *, age " + - "FROM %s " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK - )); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT *, age FROM %s LIMIT 5", TestsConstants.TEST_INDEX_BANK)); checkSelectAllAndFieldResponseSize(response); } @Test public void selectAllWithMultipleFields() throws IOException { - JSONObject response = executeQuery(StringUtils.format( - "SELECT *, age, address " + - "FROM %s " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK - )); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT *, age, address FROM %s LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkSelectAllAndFieldResponseSize(response); } @Test public void selectAllWithFieldAndOrderBy() throws IOException { - JSONObject response = executeQuery(StringUtils.format( - "SELECT *, age " + - "FROM %s " + - "ORDER BY age " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK - )); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT *, age FROM %s ORDER BY age LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkSelectAllAndFieldResponseSize(response); } @Test public void selectAllWithFieldAndGroupBy() throws IOException { - JSONObject response = executeQuery(StringUtils.format( - "SELECT *, age " + - "FROM %s " + - "GROUP BY age " + - "LIMIT 10", - TestsConstants.TEST_INDEX_BANK - )); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT *, age FROM %s GROUP BY age LIMIT 10", + TestsConstants.TEST_INDEX_BANK)); checkSelectAllAndFieldAggregationResponseSize(response, "age"); } @Test public void selectAllWithFieldAndGroupByReverseOrder() throws IOException { - JSONObject response = executeQuery(StringUtils.format( - "SELECT *, age " + - "FROM %s " + - "GROUP BY age " + - "LIMIT 10", - TestsConstants.TEST_INDEX_BANK - )); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT *, age FROM %s GROUP BY age LIMIT 10", + TestsConstants.TEST_INDEX_BANK)); checkSelectAllAndFieldAggregationResponseSize(response, "age"); } @@ -180,14 +176,16 @@ public void selectAllWithFieldAndGroupByReverseOrder() throws IOException { @Test public void selectFieldWithAliasAndGroupBy() { String response = - executeQuery("SELECT lastname AS name FROM " + TEST_INDEX_ACCOUNT + " GROUP BY name", - "jdbc"); + executeQuery( + "SELECT lastname AS name FROM " + TEST_INDEX_ACCOUNT + " GROUP BY name", "jdbc"); assertThat(response, containsString("\"alias\": \"name\"")); } public void indexWithWildcardTest() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, "SELECT * FROM %s* LIMIT 1000", - TestsConstants.TEST_INDEX_BANK)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, "SELECT * FROM %s* LIMIT 1000", TestsConstants.TEST_INDEX_BANK)); Assert.assertTrue(response.has("hits")); assertThat(getTotalHits(response), greaterThan(0)); } @@ -198,8 +196,8 @@ public void selectSpecificFields() throws IOException { Set expectedSource = new HashSet<>(Arrays.asList(arr)); JSONObject response = - executeQuery(String.format(Locale.ROOT, "SELECT age, account_number FROM %s", - TEST_INDEX_ACCOUNT)); + executeQuery( + String.format(Locale.ROOT, "SELECT age, account_number FROM %s", TEST_INDEX_ACCOUNT)); assertResponseForSelectSpecificFields(response, expectedSource); } @@ -209,8 +207,9 @@ public void selectSpecificFieldsUsingTableAlias() throws IOException { Set expectedSource = new HashSet<>(Arrays.asList(arr)); JSONObject response = - executeQuery(String.format(Locale.ROOT, "SELECT a.age, a.account_number FROM %s a", - TEST_INDEX_ACCOUNT)); + executeQuery( + String.format( + Locale.ROOT, "SELECT a.age, a.account_number FROM %s a", TEST_INDEX_ACCOUNT)); assertResponseForSelectSpecificFields(response, expectedSource); } @@ -219,15 +218,18 @@ public void selectSpecificFieldsUsingTableNamePrefix() throws IOException { String[] arr = new String[] {"age", "account_number"}; Set expectedSource = new HashSet<>(Arrays.asList(arr)); - JSONObject response = executeQuery(String.format(Locale.ROOT, - "SELECT opensearch-sql_test_index_account.age, opensearch-sql_test_index_account.account_number" + - " FROM %s", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT opensearch-sql_test_index_account.age," + + " opensearch-sql_test_index_account.account_number FROM %s", + TEST_INDEX_ACCOUNT)); assertResponseForSelectSpecificFields(response, expectedSource); } - private void assertResponseForSelectSpecificFields(JSONObject response, - Set expectedSource) { + private void assertResponseForSelectSpecificFields( + JSONObject response, Set expectedSource) { JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { JSONObject hit = hits.getJSONObject(i); @@ -240,9 +242,12 @@ public void selectFieldWithSpace() throws IOException { String[] arr = new String[] {"test field"}; Set expectedSource = new HashSet<>(Arrays.asList(arr)); - JSONObject response = executeQuery(String.format(Locale.ROOT, "SELECT ['test field'] FROM %s " + - "WHERE ['test field'] IS NOT null", - TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT ['test field'] FROM %s WHERE ['test field'] IS NOT null", + TestsConstants.TEST_INDEX_PHRASE)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -259,19 +264,28 @@ public void selectAliases() throws IOException { String[] arr = new String[] {"myage", "myaccount_number"}; Set expectedSource = new HashSet<>(Arrays.asList(arr)); - JSONObject result = executeQuery(String.format(Locale.ROOT, - "SELECT age AS myage, account_number AS myaccount_number FROM %s", TEST_INDEX_ACCOUNT)); + JSONObject result = + executeQuery( + String.format( + Locale.ROOT, + "SELECT age AS myage, account_number AS myaccount_number FROM %s", + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(result); - hits.forEach(hitObj -> { - JSONObject hit = (JSONObject) hitObj; - Assert.assertEquals(expectedSource, hit.getJSONObject("_source").keySet()); - }); + hits.forEach( + hitObj -> { + JSONObject hit = (JSONObject) hitObj; + Assert.assertEquals(expectedSource, hit.getJSONObject("_source").keySet()); + }); } @Test public void useTableAliasInWhereClauseTest() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, - "SELECT * FROM %s a WHERE a.city = 'Nogal' LIMIT 1000", TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s a WHERE a.city = 'Nogal' LIMIT 1000", + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -280,8 +294,12 @@ public void useTableAliasInWhereClauseTest() throws IOException { @Test public void notUseTableAliasInWhereClauseTest() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, - "SELECT * FROM %s a WHERE city = 'Nogal' LIMIT 1000", TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s a WHERE city = 'Nogal' LIMIT 1000", + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -290,10 +308,13 @@ public void notUseTableAliasInWhereClauseTest() throws IOException { @Test public void useTableNamePrefixInWhereClauseTest() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, - "SELECT * FROM %s WHERE opensearch-sql_test_index_account.city = 'Nogal' LIMIT 1000", - TEST_INDEX_ACCOUNT - )); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE opensearch-sql_test_index_account.city = 'Nogal' LIMIT" + + " 1000", + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -302,8 +323,12 @@ public void useTableNamePrefixInWhereClauseTest() throws IOException { @Test public void equalityTest() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, - "SELECT * FROM %s WHERE city = 'Nogal' LIMIT 1000", TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE city = 'Nogal' LIMIT 1000", + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -312,9 +337,12 @@ public void equalityTest() throws IOException { @Test public void equalityTestPhrase() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, "SELECT * FROM %s WHERE " + - "match_phrase(phrase, 'quick fox here') LIMIT 1000", - TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE match_phrase(phrase, 'quick fox here') LIMIT 1000", + TestsConstants.TEST_INDEX_PHRASE)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -324,10 +352,13 @@ public void equalityTestPhrase() throws IOException { @Test public void greaterThanTest() throws IOException { int someAge = 25; - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE age > %s LIMIT 1000", - TestsConstants.TEST_INDEX_PEOPLE, - someAge)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE age > %s LIMIT 1000", + TestsConstants.TEST_INDEX_PEOPLE, + someAge)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -340,10 +371,13 @@ public void greaterThanTest() throws IOException { @Test public void greaterThanOrEqualTest() throws IOException { int someAge = 25; - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE age >= %s LIMIT 1000", - TEST_INDEX_ACCOUNT, - someAge)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE age >= %s LIMIT 1000", + TEST_INDEX_ACCOUNT, + someAge)); boolean isEqualFound = false; JSONArray hits = getHits(response); @@ -352,24 +386,27 @@ public void greaterThanOrEqualTest() throws IOException { int age = getSource(hit).getInt("age"); assertThat(age, greaterThanOrEqualTo(someAge)); - if (age == someAge) { - isEqualFound = true; - } + if (age == someAge) { + isEqualFound = true; + } } Assert.assertTrue( - String.format(Locale.ROOT, "At least one of the documents need to contains age equal to %s", - someAge), + String.format( + Locale.ROOT, "At least one of the documents need to contains age equal to %s", someAge), isEqualFound); } @Test public void lessThanTest() throws IOException { int someAge = 25; - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE age < %s LIMIT 1000", - TestsConstants.TEST_INDEX_PEOPLE, - someAge)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE age < %s LIMIT 1000", + TestsConstants.TEST_INDEX_PEOPLE, + someAge)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -382,10 +419,13 @@ public void lessThanTest() throws IOException { @Test public void lessThanOrEqualTest() throws IOException { int someAge = 25; - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE age <= %s LIMIT 1000", - TEST_INDEX_ACCOUNT, - someAge)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE age <= %s LIMIT 1000", + TEST_INDEX_ACCOUNT, + someAge)); boolean isEqualFound = false; JSONArray hits = getHits(response); @@ -394,32 +434,39 @@ public void lessThanOrEqualTest() throws IOException { int age = getSource(hit).getInt("age"); assertThat(age, lessThanOrEqualTo(someAge)); - if (age == someAge) { - isEqualFound = true; - } + if (age == someAge) { + isEqualFound = true; + } } Assert.assertTrue( - String.format(Locale.ROOT, "At least one of the documents need to contains age equal to %s", - someAge), + String.format( + Locale.ROOT, "At least one of the documents need to contains age equal to %s", someAge), isEqualFound); } @Test public void orTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE match_phrase(gender, 'F') OR match_phrase(gender, 'M') " + - "LIMIT 1000", TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE match_phrase(gender, 'F') OR match_phrase(gender, 'M') " + + "LIMIT 1000", + TEST_INDEX_ACCOUNT)); Assert.assertEquals(1000, getTotalHits(response)); } @Test public void andTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE age=32 AND gender='M' LIMIT 1000", - TestsConstants.TEST_INDEX_PEOPLE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE age=32 AND gender='M' LIMIT 1000", + TestsConstants.TEST_INDEX_PEOPLE)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -431,9 +478,12 @@ public void andTest() throws IOException { @Test public void likeTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE firstname LIKE 'amb%%' LIMIT 1000", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE firstname LIKE 'amb%%' LIMIT 1000", + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -442,9 +492,12 @@ public void likeTest() throws IOException { @Test public void notLikeTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE firstname NOT LIKE 'amb%%'", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE firstname NOT LIKE 'amb%%'", + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); Assert.assertNotEquals(0, getTotalHits(response)); @@ -456,11 +509,13 @@ public void notLikeTest() throws IOException { @Test public void regexQueryTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE dog_name = REGEXP_QUERY('sn.*', 'INTERSECTION|COMPLEMENT|EMPTY', 10000)", - TestsConstants.TEST_INDEX_DOG)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE dog_name = REGEXP_QUERY('sn.*'," + + " 'INTERSECTION|COMPLEMENT|EMPTY', 10000)", + TestsConstants.TEST_INDEX_DOG)); JSONArray hits = getHits(response); Assert.assertEquals(1, hits.length()); @@ -473,11 +528,13 @@ public void regexQueryTest() throws IOException { @Test public void negativeRegexQueryTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE NOT(dog_name = REGEXP_QUERY('sn.*', 'INTERSECTION|COMPLEMENT|EMPTY', 10000))", - TestsConstants.TEST_INDEX_DOG)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE NOT(dog_name = REGEXP_QUERY('sn.*'," + + " 'INTERSECTION|COMPLEMENT|EMPTY', 10000))", + TestsConstants.TEST_INDEX_DOG)); JSONArray hits = getHits(response); Assert.assertEquals(1, hits.length()); @@ -489,28 +546,36 @@ public void negativeRegexQueryTest() throws IOException { @Test public void doubleNotTest() throws IOException { - JSONObject response1 = executeQuery( - String.format(Locale.ROOT, - "SELECT * FROM %s WHERE NOT gender LIKE 'm' AND NOT gender LIKE 'f'", - TEST_INDEX_ACCOUNT)); + JSONObject response1 = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE NOT gender LIKE 'm' AND NOT gender LIKE 'f'", + TEST_INDEX_ACCOUNT)); Assert.assertEquals(0, getTotalHits(response1)); - JSONObject response2 = executeQuery( - String.format(Locale.ROOT, - "SELECT * FROM %s WHERE NOT gender LIKE 'm' AND gender NOT LIKE 'f'", - TEST_INDEX_ACCOUNT)); + JSONObject response2 = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE NOT gender LIKE 'm' AND gender NOT LIKE 'f'", + TEST_INDEX_ACCOUNT)); Assert.assertEquals(0, getTotalHits(response2)); - JSONObject response3 = executeQuery( - String.format(Locale.ROOT, - "SELECT * FROM %s WHERE gender NOT LIKE 'm' AND gender NOT LIKE 'f'", - TEST_INDEX_ACCOUNT)); + JSONObject response3 = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE gender NOT LIKE 'm' AND gender NOT LIKE 'f'", + TEST_INDEX_ACCOUNT)); Assert.assertEquals(0, getTotalHits(response3)); - JSONObject response4 = executeQuery( - String.format(Locale.ROOT, - "SELECT * FROM %s WHERE gender LIKE 'm' AND NOT gender LIKE 'f'", - TEST_INDEX_ACCOUNT)); + JSONObject response4 = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE gender LIKE 'm' AND NOT gender LIKE 'f'", + TEST_INDEX_ACCOUNT)); // Assert there are results and they all have gender 'm' Assert.assertNotEquals(0, getTotalHits(response4)); JSONArray hits = getHits(response4); @@ -519,16 +584,19 @@ public void doubleNotTest() throws IOException { Assert.assertEquals("m", getSource(hit).getString("gender").toLowerCase()); } - JSONObject response5 = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE NOT (gender = 'm' OR gender = 'f')", - TEST_INDEX_ACCOUNT)); + JSONObject response5 = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE NOT (gender = 'm' OR gender = 'f')", + TEST_INDEX_ACCOUNT)); Assert.assertEquals(0, getTotalHits(response5)); } @Test public void limitTest() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, "SELECT * FROM %s LIMIT 30", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery(String.format(Locale.ROOT, "SELECT * FROM %s LIMIT 30", TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); Assert.assertEquals(30, hits.length()); @@ -538,9 +606,14 @@ public void limitTest() throws IOException { public void betweenTest() throws IOException { int min = 27; int max = 30; - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE age BETWEEN %s AND %s LIMIT 1000", - TestsConstants.TEST_INDEX_PEOPLE, min, max)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE age BETWEEN %s AND %s LIMIT 1000", + TestsConstants.TEST_INDEX_PEOPLE, + min, + max)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -556,9 +629,14 @@ public void betweenTest() throws IOException { public void notBetweenTest() throws IOException { int min = 20; int max = 37; - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE age NOT BETWEEN %s AND %s LIMIT 1000", - TestsConstants.TEST_INDEX_PEOPLE, min, max)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE age NOT BETWEEN %s AND %s LIMIT 1000", + TestsConstants.TEST_INDEX_PEOPLE, + min, + max)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -575,9 +653,12 @@ public void notBetweenTest() throws IOException { @Test public void inTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT age FROM %s WHERE age IN (20, 22) LIMIT 1000", - TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT age FROM %s WHERE age IN (20, 22) LIMIT 1000", + TestsConstants.TEST_INDEX_PHRASE)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -589,10 +670,12 @@ public void inTest() throws IOException { @Test public void inTestWithStrings() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, - "SELECT phrase FROM %s WHERE phrase IN ('quick', 'fox') LIMIT 1000", - TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT phrase FROM %s WHERE phrase IN ('quick', 'fox') LIMIT 1000", + TestsConstants.TEST_INDEX_PHRASE)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -604,12 +687,15 @@ public void inTestWithStrings() throws IOException { @Test public void inTermsTestWithIdentifiersTreatedLikeStrings() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT name " + - "FROM %s " + - "WHERE name.firstname = IN_TERMS('daenerys','eddard') " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT name " + + "FROM %s " + + "WHERE name.firstname = IN_TERMS('daenerys','eddard') " + + "LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(2, getTotalHits(response)); @@ -622,12 +708,15 @@ public void inTermsTestWithIdentifiersTreatedLikeStrings() throws IOException { @Test public void inTermsTestWithStrings() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT name " + - "FROM %s " + - "WHERE name.firstname = IN_TERMS('daenerys','eddard') " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT name " + + "FROM %s " + + "WHERE name.firstname = IN_TERMS('daenerys','eddard') " + + "LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(2, getTotalHits(response)); @@ -640,12 +729,15 @@ public void inTermsTestWithStrings() throws IOException { @Test public void inTermsWithNumbers() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT name " + - "FROM %s " + - "WHERE name.ofHisName = IN_TERMS(4,2) " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT name " + + "FROM %s " + + "WHERE name.ofHisName = IN_TERMS(4,2) " + + "LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -657,10 +749,12 @@ public void inTermsWithNumbers() throws IOException { @Test public void termQueryWithNumber() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, - "SELECT name FROM %s WHERE name.ofHisName = term(4) LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT name FROM %s WHERE name.ofHisName = term(4) LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -672,12 +766,15 @@ public void termQueryWithNumber() throws IOException { @Test public void termQueryWithStringIdentifier() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT name " + - "FROM %s " + - "WHERE name.firstname = term('brandon') " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT name " + + "FROM %s " + + "WHERE name.firstname = term('brandon') " + + "LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -689,12 +786,15 @@ public void termQueryWithStringIdentifier() throws IOException { @Test public void termQueryWithStringLiteral() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT name " + - "FROM %s " + - "WHERE name.firstname = term('brandon') " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT name " + + "FROM %s " + + "WHERE name.firstname = term('brandon') " + + "LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -708,9 +808,12 @@ public void termQueryWithStringLiteral() throws IOException { // are returned as well. This may be incorrect behavior. @Test public void notInTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT age FROM %s WHERE age NOT IN (20, 22) LIMIT 1000", - TestsConstants.TEST_INDEX_PEOPLE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT age FROM %s WHERE age NOT IN (20, 22) LIMIT 1000", + TestsConstants.TEST_INDEX_PEOPLE)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -730,9 +833,12 @@ public void dateSearch() throws IOException { DateTimeFormatter formatter = DateTimeFormat.forPattern(TestsConstants.DATE_FORMAT); DateTime dateToCompare = new DateTime(2014, 8, 18, 0, 0, 0); - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT insert_time FROM %s WHERE insert_time < '2014-08-18'", - TestsConstants.TEST_INDEX_ONLINE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT insert_time FROM %s WHERE insert_time < '2014-08-18'", + TestsConstants.TEST_INDEX_ONLINE)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { JSONObject hit = hits.getJSONObject(i); @@ -740,8 +846,8 @@ public void dateSearch() throws IOException { DateTime insertTime = formatter.parseDateTime(source.getString("insert_time")); String errorMessage = - String.format(Locale.ROOT, "insert_time must be before 2014-08-18. Found: %s", - insertTime); + String.format( + Locale.ROOT, "insert_time must be before 2014-08-18. Found: %s", insertTime); Assert.assertTrue(errorMessage, insertTime.isBefore(dateToCompare)); } } @@ -751,10 +857,12 @@ public void dateSearchBraces() throws IOException { DateTimeFormatter formatter = DateTimeFormat.forPattern(TestsConstants.TS_DATE_FORMAT); DateTime dateToCompare = new DateTime(2015, 3, 15, 0, 0, 0); - JSONObject response = executeQuery( - String.format(Locale.ROOT, - "SELECT odbc_time FROM %s WHERE odbc_time < {ts '2015-03-15 00:00:00.000'}", - TestsConstants.TEST_INDEX_ODBC)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT odbc_time FROM %s WHERE odbc_time < {ts '2015-03-15 00:00:00.000'}", + TestsConstants.TEST_INDEX_ODBC)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { JSONObject hit = hits.getJSONObject(i); @@ -764,8 +872,8 @@ public void dateSearchBraces() throws IOException { DateTime insertTime = formatter.parseDateTime(insertTimeStr); String errorMessage = - String.format(Locale.ROOT, "insert_time must be before 2015-03-15. Found: %s", - insertTime); + String.format( + Locale.ROOT, "insert_time must be before 2015-03-15. Found: %s", insertTime); Assert.assertTrue(errorMessage, insertTime.isBefore(dateToCompare)); } } @@ -777,20 +885,24 @@ public void dateBetweenSearch() throws IOException { DateTime dateLimit1 = new DateTime(2014, 8, 18, 0, 0, 0); DateTime dateLimit2 = new DateTime(2014, 8, 21, 0, 0, 0); - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT insert_time " + - "FROM %s " + - "WHERE insert_time BETWEEN '2014-08-18' AND '2014-08-21' " + - "LIMIT 3", - TestsConstants.TEST_INDEX_ONLINE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT insert_time " + + "FROM %s " + + "WHERE insert_time BETWEEN '2014-08-18' AND '2014-08-21' " + + "LIMIT 3", + TestsConstants.TEST_INDEX_ONLINE)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { JSONObject hit = hits.getJSONObject(i); JSONObject source = getSource(hit); DateTime insertTime = formatter.parseDateTime(source.getString("insert_time")); - boolean isBetween = (insertTime.isAfter(dateLimit1) || insertTime.isEqual(dateLimit1)) && - (insertTime.isBefore(dateLimit2) || insertTime.isEqual(dateLimit2)); + boolean isBetween = + (insertTime.isAfter(dateLimit1) || insertTime.isEqual(dateLimit1)) + && (insertTime.isBefore(dateLimit2) || insertTime.isEqual(dateLimit2)); Assert.assertTrue("insert_time must be between 2014-08-18 and 2014-08-21", isBetween); } @@ -798,9 +910,12 @@ public void dateBetweenSearch() throws IOException { @Test public void missFilterSearch() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE insert_time2 IS missing", - TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE insert_time2 IS missing", + TestsConstants.TEST_INDEX_PHRASE)); JSONArray hits = getHits(response); Assert.assertEquals(4, getTotalHits(response)); @@ -814,9 +929,12 @@ public void missFilterSearch() throws IOException { @Test public void notMissFilterSearch() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE insert_time2 IS NOT missing", - TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE insert_time2 IS NOT missing", + TestsConstants.TEST_INDEX_PHRASE)); JSONArray hits = getHits(response); Assert.assertEquals(2, getTotalHits(response)); @@ -830,15 +948,19 @@ public void notMissFilterSearch() throws IOException { @Test public void complexConditionQuery() throws IOException { - String errorMessage = "Result does not exist to the condition " + - "(gender='m' AND (age> 25 OR account_number>5)) OR (gender='f' AND (age>30 OR account_number < 8)"; + String errorMessage = + "Result does not exist to the condition (gender='m' AND (age> 25 OR account_number>5)) OR" + + " (gender='f' AND (age>30 OR account_number < 8)"; - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE (gender='m' AND (age> 25 OR account_number>5)) " + - "OR (gender='f' AND (age>30 OR account_number < 8))", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE (gender='m' AND (age> 25 OR account_number>5)) " + + "OR (gender='f' AND (age>30 OR account_number < 8))", + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -849,7 +971,8 @@ public void complexConditionQuery() throws IOException { int age = source.getInt("age"); int accountNumber = source.getInt("account_number"); - Assert.assertTrue(errorMessage, + Assert.assertTrue( + errorMessage, (gender.equals("m") && (age > 25 || accountNumber > 5)) || (gender.equals("f") && (age > 30 || accountNumber < 8))); } @@ -857,16 +980,20 @@ public void complexConditionQuery() throws IOException { @Test public void complexNotConditionQuery() throws IOException { - String errorMessage = "Result does not exist to the condition " + - "NOT (gender='m' AND NOT (age > 25 OR account_number > 5)) " + - "OR (NOT gender='f' AND NOT (age > 30 OR account_number < 8))"; + String errorMessage = + "Result does not exist to the condition " + + "NOT (gender='m' AND NOT (age > 25 OR account_number > 5)) " + + "OR (NOT gender='f' AND NOT (age > 30 OR account_number < 8))"; - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE NOT (gender='m' AND NOT (age > 25 OR account_number > 5)) " + - "OR (NOT gender='f' AND NOT (age > 30 OR account_number < 8))", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE NOT (gender='m' AND NOT (age > 25 OR account_number > 5)) " + + "OR (NOT gender='f' AND NOT (age > 30 OR account_number < 8))", + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); Assert.assertNotEquals(0, hits.length()); @@ -878,7 +1005,8 @@ public void complexNotConditionQuery() throws IOException { int age = source.getInt("age"); int accountNumber = source.getInt("account_number"); - Assert.assertTrue(errorMessage, + Assert.assertTrue( + errorMessage, !(gender.equals("m") && !(age > 25 || accountNumber > 5)) || (!gender.equals("f") && !(age > 30 || accountNumber < 8))); } @@ -887,9 +1015,10 @@ public void complexNotConditionQuery() throws IOException { @Test @SuppressWarnings("unchecked") public void orderByAscTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT age FROM %s ORDER BY age ASC LIMIT 1000", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, "SELECT age FROM %s ORDER BY age ASC LIMIT 1000", TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); ArrayList ages = new ArrayList<>(); @@ -907,17 +1036,23 @@ public void orderByAscTest() throws IOException { @Test public void orderByDescTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT age FROM %s ORDER BY age DESC LIMIT 1000", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT age FROM %s ORDER BY age DESC LIMIT 1000", + TEST_INDEX_ACCOUNT)); assertResponseForOrderByTest(response); } @Test public void orderByDescUsingTableAliasTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT a.age FROM %s a ORDER BY a.age DESC LIMIT 1000", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT a.age FROM %s a ORDER BY a.age DESC LIMIT 1000", + TEST_INDEX_ACCOUNT)); assertResponseForOrderByTest(response); } @@ -940,13 +1075,16 @@ private void assertResponseForOrderByTest(JSONObject response) { @Test @SuppressWarnings("unchecked") public void orderByAscFieldWithSpaceTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE `test field` IS NOT null " + - "ORDER BY `test field` ASC " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE `test field` IS NOT null " + + "ORDER BY `test field` ASC " + + "LIMIT 1000", + TestsConstants.TEST_INDEX_PHRASE)); JSONArray hits = getHits(response); ArrayList testFields = new ArrayList<>(); @@ -964,195 +1102,177 @@ public void orderByAscFieldWithSpaceTest() throws IOException { @Test public void testWhereWithBoolEqualsTrue() throws IOException { - JSONObject response = executeQuery( - StringUtils.format( - "SELECT * " + - "FROM %s " + - "WHERE male = true " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK) - ); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT * FROM %s WHERE male = true LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkResponseSize(response, BANK_INDEX_MALE_TRUE); } @Test public void testWhereWithBoolEqualsTrueAndGroupBy() throws IOException { - JSONObject response = executeQuery( - StringUtils.format( - "SELECT * " + - "FROM %s " + - "WHERE male = true " + - "GROUP BY balance " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK) - ); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT * FROM %s WHERE male = true GROUP BY balance LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkAggregationResponseSize(response, BANK_INDEX_MALE_TRUE); } @Test public void testWhereWithBoolEqualsTrueAndOrderBy() throws IOException { - JSONObject response = executeQuery( - StringUtils.format( - "SELECT * " + - "FROM %s " + - "WHERE male = true " + - "ORDER BY age " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK) - ); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT * FROM %s WHERE male = true ORDER BY age LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkResponseSize(response, BANK_INDEX_MALE_TRUE); } @Test public void testWhereWithBoolIsTrue() throws IOException { - JSONObject response = executeQuery( - StringUtils.format( - "SELECT * " + - "FROM %s " + - "WHERE male IS true " + - "GROUP BY balance " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK) - ); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT * FROM %s WHERE male IS true GROUP BY balance LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkAggregationResponseSize(response, BANK_INDEX_MALE_TRUE); } @Test public void testWhereWithBoolIsNotTrue() throws IOException { - JSONObject response = executeQuery( - StringUtils.format( - "SELECT * " + - "FROM %s " + - "WHERE male IS NOT true " + - "GROUP BY balance " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK) - ); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT * " + + "FROM %s " + + "WHERE male IS NOT true " + + "GROUP BY balance " + + "LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkAggregationResponseSize(response, BANK_INDEX_MALE_FALSE); } @Test public void testWhereWithBoolEqualsFalse() throws IOException { - JSONObject response = executeQuery( - StringUtils.format( - "SELECT * " + - "FROM %s " + - "WHERE male = false " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK) - ); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT * FROM %s WHERE male = false LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkResponseSize(response, BANK_INDEX_MALE_FALSE); } @Test public void testWhereWithBoolEqualsFalseAndGroupBy() throws IOException { - JSONObject response = executeQuery( - StringUtils.format( - "SELECT * " + - "FROM %s " + - "WHERE male = false " + - "GROUP BY balance " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK) - ); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT * FROM %s WHERE male = false GROUP BY balance LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkAggregationResponseSize(response, BANK_INDEX_MALE_FALSE); } @Test public void testWhereWithBoolEqualsFalseAndOrderBy() throws IOException { - JSONObject response = executeQuery( - StringUtils.format( - "SELECT * " + - "FROM %s " + - "WHERE male = false " + - "ORDER BY age " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK) - ); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT * FROM %s WHERE male = false ORDER BY age LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkResponseSize(response, BANK_INDEX_MALE_FALSE); } @Test public void testWhereWithBoolIsFalse() throws IOException { - JSONObject response = executeQuery( - StringUtils.format( - "SELECT * " + - "FROM %s " + - "WHERE male IS false " + - "GROUP BY balance " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK) - ); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT * FROM %s WHERE male IS false GROUP BY balance LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkAggregationResponseSize(response, BANK_INDEX_MALE_FALSE); } @Test public void testWhereWithBoolIsNotFalse() throws IOException { - JSONObject response = executeQuery( - StringUtils.format( - "SELECT * " + - "FROM %s " + - "WHERE male IS NOT false " + - "GROUP BY balance " + - "LIMIT 5", - TestsConstants.TEST_INDEX_BANK) - ); + JSONObject response = + executeQuery( + StringUtils.format( + "SELECT * " + + "FROM %s " + + "WHERE male IS NOT false " + + "GROUP BY balance " + + "LIMIT 5", + TestsConstants.TEST_INDEX_BANK)); checkAggregationResponseSize(response, BANK_INDEX_MALE_TRUE); } @Test public void testMultiPartWhere() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE (firstname LIKE 'opal' OR firstname LIKE 'rodriquez') " + - "AND (state like 'oh' OR state like 'hi')", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE (firstname LIKE 'opal' OR firstname LIKE 'rodriquez') " + + "AND (state like 'oh' OR state like 'hi')", + TEST_INDEX_ACCOUNT)); Assert.assertEquals(2, getTotalHits(response)); } @Test public void testMultiPartWhere2() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE ((account_number > 200 AND account_number < 300) OR gender LIKE 'm') " + - "AND (state LIKE 'hi' OR address LIKE 'avenue')", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE ((account_number > 200 AND account_number < 300) OR gender LIKE 'm') " + + "AND (state LIKE 'hi' OR address LIKE 'avenue')", + TEST_INDEX_ACCOUNT)); Assert.assertEquals(127, getTotalHits(response)); } @Test public void testMultiPartWhere3() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE ((account_number > 25 AND account_number < 75) AND age >35 ) " + - "AND (state LIKE 'md' OR (address LIKE 'avenue' OR address LIKE 'street'))", - TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE ((account_number > 25 AND account_number < 75) AND age >35 ) " + + "AND (state LIKE 'md' OR (address LIKE 'avenue' OR address LIKE 'street'))", + TEST_INDEX_ACCOUNT)); Assert.assertEquals(7, getTotalHits(response)); } @Test public void filterPolygonTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE GEO_INTERSECTS(place,'POLYGON ((102 2, 103 2, 103 3, 102 3, 102 2))')", - TestsConstants.TEST_INDEX_LOCATION)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE GEO_INTERSECTS(place,'POLYGON ((102 2, 103 2, 103 3, 102 3, 102 2))')", + TestsConstants.TEST_INDEX_LOCATION)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -1163,10 +1283,12 @@ public void filterPolygonTest() throws IOException { @Test public void boundingBox() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, - "SELECT * FROM %s WHERE GEO_BOUNDING_BOX(center, 100.0, 1.0, 101, 0.0)", - TestsConstants.TEST_INDEX_LOCATION)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE GEO_BOUNDING_BOX(center, 100.0, 1.0, 101, 0.0)", + TestsConstants.TEST_INDEX_LOCATION)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -1177,10 +1299,12 @@ public void boundingBox() throws IOException { @Test public void geoDistance() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, - "SELECT * FROM %s WHERE GEO_DISTANCE(center, '1km', 100.5, 0.500001)", - TestsConstants.TEST_INDEX_LOCATION)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE GEO_DISTANCE(center, '1km', 100.5, 0.500001)", + TestsConstants.TEST_INDEX_LOCATION)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -1191,10 +1315,12 @@ public void geoDistance() throws IOException { @Test public void geoPolygon() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, - "SELECT * FROM %s WHERE GEO_POLYGON(center, 100,0, 100.5, 2, 101.0,0)", - TestsConstants.TEST_INDEX_LOCATION)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE GEO_POLYGON(center, 100,0, 100.5, 2, 101.0,0)", + TestsConstants.TEST_INDEX_LOCATION)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -1206,36 +1332,45 @@ public void geoPolygon() throws IOException { @Ignore @Test public void escapedCharactersCheck() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE MATCH_PHRASE(nickname, 'Daenerys \"Stormborn\"') " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE MATCH_PHRASE(nickname, 'Daenerys \"Stormborn\"') " + + "LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); Assert.assertEquals(1, getTotalHits(response)); } @Test public void complexObjectSearch() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE MATCH_PHRASE(name.firstname, 'Jaime') " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE MATCH_PHRASE(name.firstname, 'Jaime') " + + "LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); Assert.assertEquals(1, getTotalHits(response)); } @Test public void complexObjectReturnField() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT parents.father " + - "FROM %s " + - "WHERE MATCH_PHRASE(name.firstname, 'Brandon') " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT parents.father " + + "FROM %s " + + "WHERE MATCH_PHRASE(name.firstname, 'Brandon') " + + "LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(1, getTotalHits(response)); @@ -1246,14 +1381,18 @@ public void complexObjectReturnField() throws IOException { /** * TODO: Fields prefixed with @ gets converted to SQLVariantRefExpr instead of SQLIdentifierExpr - * Either change SQLVariantRefExpr to SQLIdentifierExpr - * Or handle the special case for SQLVariantRefExpr + * Either change SQLVariantRefExpr to SQLIdentifierExpr Or handle the special case for + * SQLVariantRefExpr */ @Ignore @Test public void queryWithAtFieldOnWhere() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, - "SELECT * FROM %s where @wolf = 'Summer' LIMIT 1000", TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s where @wolf = 'Summer' LIMIT 1000", + TEST_INDEX_GAME_OF_THRONES)); Assert.assertEquals(1, getTotalHits(response)); JSONObject hit = getHits(response).getJSONObject(0); Assert.assertEquals("Summer", hit.get("@wolf")); @@ -1265,19 +1404,22 @@ public void queryWithDotAtStartOfIndexName() throws Exception { TestUtils.createHiddenIndexByRestClient(client(), ".bank", null); TestUtils.loadDataByRestClient(client(), ".bank", "/src/test/resources/.bank.json"); - String response = executeQuery("SELECT education FROM .bank WHERE account_number = 12345", - "jdbc"); + String response = + executeQuery("SELECT education FROM .bank WHERE account_number = 12345", "jdbc"); Assert.assertTrue(response.contains("PhD")); } @Test public void notLikeTests() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT name " + - "FROM %s " + - "WHERE name.firstname NOT LIKE 'd%%' AND name IS NOT NULL " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT name " + + "FROM %s " + + "WHERE name.firstname NOT LIKE 'd%%' AND name IS NOT NULL " + + "LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(3, getTotalHits(response)); @@ -1286,45 +1428,49 @@ public void notLikeTests() throws IOException { JSONObject source = getSource(hit); String name = source.getJSONObject("name").getString("firstname"); - Assert - .assertFalse(String.format(Locale.ROOT, "Name [%s] should not match pattern [d%%]", name), - name.startsWith("d")); + Assert.assertFalse( + String.format(Locale.ROOT, "Name [%s] should not match pattern [d%%]", name), + name.startsWith("d")); } } @Test public void isNullTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT name " + - "FROM %s " + - "WHERE nickname IS NULL " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT name FROM %s WHERE nickname IS NULL LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); Assert.assertEquals(6, getTotalHits(response)); } @Test public void isNotNullTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT name " + - "FROM %s " + - "WHERE nickname IS NOT NULL " + - "LIMIT 1000", - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT name FROM %s WHERE nickname IS NOT NULL LIMIT 1000", + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); Assert.assertEquals(1, getTotalHits(response)); } @Test public void innerQueryTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s D " + - "WHERE holdersName IN (SELECT firstname " + - "FROM %s " + - "WHERE firstname = 'Hattie')", - TestsConstants.TEST_INDEX_DOG, TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s D " + + "WHERE holdersName IN (SELECT firstname " + + "FROM %s " + + "WHERE firstname = 'Hattie')", + TestsConstants.TEST_INDEX_DOG, + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); Assert.assertEquals(1, hits.length()); @@ -1339,19 +1485,22 @@ public void innerQueryTest() throws IOException { @Ignore @Test public void twoSubQueriesTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE holdersName IN (SELECT firstname " + - "FROM %s " + - "WHERE firstname = 'Hattie') " + - "AND age IN (SELECT name.ofHisName " + - "FROM %s " + - "WHERE name.firstname <> 'Daenerys' " + - "AND name.ofHisName IS NOT NULL) ", - TestsConstants.TEST_INDEX_DOG, - TEST_INDEX_ACCOUNT, - TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE holdersName IN (SELECT firstname " + + "FROM %s " + + "WHERE firstname = 'Hattie') " + + "AND age IN (SELECT name.ofHisName " + + "FROM %s " + + "WHERE name.firstname <> 'Daenerys' " + + "AND name.ofHisName IS NOT NULL) ", + TestsConstants.TEST_INDEX_DOG, + TEST_INDEX_ACCOUNT, + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(1, hits.length()); @@ -1366,14 +1515,18 @@ public void twoSubQueriesTest() throws IOException { @Ignore @Test public void inTermsSubQueryTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE age = IN_TERMS (SELECT name.ofHisName " + - "FROM %s " + - "WHERE name.firstname <> 'Daenerys' " + - "AND name.ofHisName IS NOT NULL)", - TestsConstants.TEST_INDEX_DOG, TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE age = IN_TERMS (SELECT name.ofHisName " + + "FROM %s " + + "WHERE name.firstname <> 'Daenerys' " + + "AND name.ofHisName IS NOT NULL)", + TestsConstants.TEST_INDEX_DOG, + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(1, hits.length()); @@ -1388,9 +1541,12 @@ public void inTermsSubQueryTest() throws IOException { @Ignore @Test public void idsQueryOneId() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE _id = IDS_QUERY(dog, 1)", - TestsConstants.TEST_INDEX_DOG)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE _id = IDS_QUERY(dog, 1)", + TestsConstants.TEST_INDEX_DOG)); JSONArray hits = getHits(response); Assert.assertEquals(1, hits.length()); @@ -1405,9 +1561,12 @@ public void idsQueryOneId() throws IOException { @Ignore @Test public void idsQueryMultipleId() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE _id = IDS_QUERY(dog, 1, 2, 3)", - TestsConstants.TEST_INDEX_DOG)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE _id = IDS_QUERY(dog, 1, 2, 3)", + TestsConstants.TEST_INDEX_DOG)); JSONArray hits = getHits(response); Assert.assertEquals(1, hits.length()); @@ -1422,14 +1581,18 @@ public void idsQueryMultipleId() throws IOException { @Ignore @Test public void idsQuerySubQueryIds() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE _id = IDS_QUERY(dog, (SELECT name.ofHisName " + - "FROM %s " + - "WHERE name.firstname <> 'Daenerys' " + - "AND name.ofHisName IS NOT NULL))", - TestsConstants.TEST_INDEX_DOG, TestsConstants.TEST_INDEX_GAME_OF_THRONES)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE _id = IDS_QUERY(dog, (SELECT name.ofHisName " + + "FROM %s " + + "WHERE name.firstname <> 'Daenerys' " + + "AND name.ofHisName IS NOT NULL))", + TestsConstants.TEST_INDEX_DOG, + TestsConstants.TEST_INDEX_GAME_OF_THRONES)); JSONArray hits = getHits(response); Assert.assertEquals(1, hits.length()); @@ -1443,18 +1606,24 @@ public void idsQuerySubQueryIds() throws IOException { @Test public void nestedEqualsTestFieldNormalField() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE nested(message.info)='b'", - TestsConstants.TEST_INDEX_NESTED_TYPE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE nested(message.info)='b'", + TestsConstants.TEST_INDEX_NESTED_TYPE)); Assert.assertEquals(1, getTotalHits(response)); } @Test public void nestedEqualsTestFieldInsideArrays() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * FROM %s WHERE nested(message.info) = 'a'", - TestsConstants.TEST_INDEX_NESTED_TYPE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE nested(message.info) = 'a'", + TestsConstants.TEST_INDEX_NESTED_TYPE)); Assert.assertEquals(2, getTotalHits(response)); } @@ -1462,106 +1631,124 @@ public void nestedEqualsTestFieldInsideArrays() throws IOException { @Ignore // Seems like we don't support nested with IN, throwing IllegalArgumentException @Test public void nestedOnInQuery() throws IOException { - JSONObject response = executeQuery(String.format(Locale.ROOT, - "SELECT * FROM %s where nested(message.info) IN ('a','b')", TEST_INDEX_NESTED_TYPE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s where nested(message.info) IN ('a','b')", + TEST_INDEX_NESTED_TYPE)); Assert.assertEquals(3, getTotalHits(response)); } @Test public void complexNestedQueryBothOnSameObject() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE nested('message', message.info = 'a' AND message.author ='i')", - TestsConstants.TEST_INDEX_NESTED_TYPE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE nested('message', message.info = 'a' AND message.author ='i')", + TestsConstants.TEST_INDEX_NESTED_TYPE)); Assert.assertEquals(1, getTotalHits(response)); } @Test public void complexNestedQueryNotBothOnSameObject() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE nested('message', message.info = 'a' AND message.author ='h')", - TestsConstants.TEST_INDEX_NESTED_TYPE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * " + + "FROM %s " + + "WHERE nested('message', message.info = 'a' AND message.author ='h')", + TestsConstants.TEST_INDEX_NESTED_TYPE)); Assert.assertEquals(0, getTotalHits(response)); } @Test public void nestedOnInTermsQuery() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT * " + - "FROM %s " + - "WHERE nested(message.info) = IN_TERMS('a', 'b')", - TestsConstants.TEST_INDEX_NESTED_TYPE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT * FROM %s WHERE nested(message.info) = IN_TERMS('a', 'b')", + TestsConstants.TEST_INDEX_NESTED_TYPE)); Assert.assertEquals(3, getTotalHits(response)); } // TODO Uncomment these after problem with loading join index is resolved -// @Test -// public void childrenEqualsTestFieldNormalField() throws IOException { -// JSONObject response = executeQuery( -// String.format(Locale.ROOT, "SELECT * " + -// "FROM %s/joinType " + -// "WHERE children(childrenType, info) = 'b'", TestsConstants.TEST_INDEX_JOIN_TYPE)); -// -// Assert.assertEquals(1, getTotalHits(response)); -// } -// -// @Test -// public void childrenOnInQuery() throws IOException { -// JSONObject response = executeQuery( -// String.format(Locale.ROOT, "SELECT * " + -// "FROM %s/joinType " + -// "WHERE children(childrenType, info) IN ('a', 'b')", -// TestsConstants.TEST_INDEX_JOIN_TYPE)); -// -// Assert.assertEquals(2, getTotalHits(response)); -// } -// -// @Test -// public void complexChildrenQueryBothOnSameObject() throws IOException { -// JSONObject response = executeQuery( -// String.format(Locale.ROOT, "SELECT * " + -// "FROM %s/joinType " + -// "WHERE children(childrenType, info = 'a' AND author ='e')", -// TestsConstants.TEST_INDEX_JOIN_TYPE)); -// -// Assert.assertEquals(1, getTotalHits(response)); -// } -// -// @Test -// public void complexChildrenQueryNotOnSameObject() throws IOException { -// JSONObject response = executeQuery( -// String.format(Locale.ROOT, "SELECT * " + -// "FROM %s/joinType " + -// "WHERE children(childrenType, info = 'a' AND author ='j')", -// TestsConstants.TEST_INDEX_JOIN_TYPE)); -// -// Assert.assertEquals(0, getTotalHits(response)); -// } -// -// @Test -// public void childrenOnInTermsQuery() throws IOException { -// JSONObject response = executeQuery( -// String.format(Locale.ROOT, "SELECT * " + -// "FROM %s/joinType " + -// "WHERE children(childrenType, info) = IN_TERMS(a, b)", -// TestsConstants.TEST_INDEX_JOIN_TYPE)); -// -// Assert.assertEquals(2, getTotalHits(response)); -// } + // @Test + // public void childrenEqualsTestFieldNormalField() throws IOException { + // JSONObject response = executeQuery( + // String.format(Locale.ROOT, "SELECT * " + + // "FROM %s/joinType " + + // "WHERE children(childrenType, info) = 'b'", + // TestsConstants.TEST_INDEX_JOIN_TYPE)); + // + // Assert.assertEquals(1, getTotalHits(response)); + // } + // + // @Test + // public void childrenOnInQuery() throws IOException { + // JSONObject response = executeQuery( + // String.format(Locale.ROOT, "SELECT * " + + // "FROM %s/joinType " + + // "WHERE children(childrenType, info) IN ('a', 'b')", + // TestsConstants.TEST_INDEX_JOIN_TYPE)); + // + // Assert.assertEquals(2, getTotalHits(response)); + // } + // + // @Test + // public void complexChildrenQueryBothOnSameObject() throws IOException { + // JSONObject response = executeQuery( + // String.format(Locale.ROOT, "SELECT * " + + // "FROM %s/joinType " + + // "WHERE children(childrenType, info = 'a' AND author + // ='e')", + // TestsConstants.TEST_INDEX_JOIN_TYPE)); + // + // Assert.assertEquals(1, getTotalHits(response)); + // } + // + // @Test + // public void complexChildrenQueryNotOnSameObject() throws IOException { + // JSONObject response = executeQuery( + // String.format(Locale.ROOT, "SELECT * " + + // "FROM %s/joinType " + + // "WHERE children(childrenType, info = 'a' AND author + // ='j')", + // TestsConstants.TEST_INDEX_JOIN_TYPE)); + // + // Assert.assertEquals(0, getTotalHits(response)); + // } + // + // @Test + // public void childrenOnInTermsQuery() throws IOException { + // JSONObject response = executeQuery( + // String.format(Locale.ROOT, "SELECT * " + + // "FROM %s/joinType " + + // "WHERE children(childrenType, info) = IN_TERMS(a, b)", + // TestsConstants.TEST_INDEX_JOIN_TYPE)); + // + // Assert.assertEquals(2, getTotalHits(response)); + // } @Ignore // the hint does not really work, NoSuchIndexException is thrown @Test public void multipleIndicesOneNotExistWithHint() throws IOException { - JSONObject response = executeQuery(String - .format(Locale.ROOT, "SELECT /*! IGNORE_UNAVAILABLE */ * FROM %s,%s ", TEST_INDEX_ACCOUNT, - "badindex")); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT /*! IGNORE_UNAVAILABLE */ * FROM %s,%s ", + TEST_INDEX_ACCOUNT, + "badindex")); Assert.assertTrue(getTotalHits(response) > 0); } @@ -1573,8 +1760,8 @@ public void multipleIndicesOneNotExistWithoutHint() throws IOException { String.format(Locale.ROOT, "SELECT * FROM %s, %s", TEST_INDEX_ACCOUNT, "badindex")); Assert.fail("Expected exception, but call succeeded"); } catch (ResponseException e) { - Assert.assertEquals(RestStatus.BAD_REQUEST.getStatus(), - e.getResponse().getStatusLine().getStatusCode()); + Assert.assertEquals( + RestStatus.BAD_REQUEST.getStatus(), e.getResponse().getStatusLine().getStatusCode()); final String entity = TestUtils.getResponseBody(e.getResponse()); Assert.assertThat(entity, containsString("\"type\": \"IndexNotFoundException\"")); } @@ -1582,29 +1769,36 @@ public void multipleIndicesOneNotExistWithoutHint() throws IOException { // TODO Find way to check routing() without SearchRequestBuilder // to properly update these tests to OpenSearchIntegTestCase format -// @Test -// public void routingRequestOneRounting() throws IOException { -// SqlElasticSearchRequestBuilder request = getRequestBuilder(String.format(Locale.ROOT, -// "SELECT /*! ROUTINGS(hey) */ * FROM %s ", TEST_INDEX_ACCOUNT)); -// SearchRequestBuilder searchRequestBuilder = (SearchRequestBuilder) request.getBuilder(); -// Assert.assertEquals("hey",searchRequestBuilder.request().routing()); -// } -// -// @Test -// public void routingRequestMultipleRountings() throws IOException { -// SqlElasticSearchRequestBuilder request = getRequestBuilder(String.format(Locale.ROOT, -// "SELECT /*! ROUTINGS(hey,bye) */ * FROM %s ", TEST_INDEX_ACCOUNT)); -// SearchRequestBuilder searchRequestBuilder = (SearchRequestBuilder) request.getBuilder(); -// Assert.assertEquals("hey,bye",searchRequestBuilder.request().routing()); -// } + // @Test + // public void routingRequestOneRounting() throws IOException { + // SqlElasticSearchRequestBuilder request = getRequestBuilder(String.format(Locale.ROOT, + // "SELECT /*! ROUTINGS(hey) */ * FROM %s ", + // TEST_INDEX_ACCOUNT)); + // SearchRequestBuilder searchRequestBuilder = (SearchRequestBuilder) request.getBuilder(); + // Assert.assertEquals("hey",searchRequestBuilder.request().routing()); + // } + // + // @Test + // public void routingRequestMultipleRountings() throws IOException { + // SqlElasticSearchRequestBuilder request = getRequestBuilder(String.format(Locale.ROOT, + // "SELECT /*! ROUTINGS(hey,bye) */ * FROM %s ", + // TEST_INDEX_ACCOUNT)); + // SearchRequestBuilder searchRequestBuilder = (SearchRequestBuilder) request.getBuilder(); + // Assert.assertEquals("hey,bye",searchRequestBuilder.request().routing()); + // } @Ignore // Getting parser error: syntax error, expect RPAREN, actual IDENTIFIER insert_time @Test public void scriptFilterNoParams() throws IOException { - JSONObject result = executeQuery(String.format(Locale.ROOT, - "SELECT insert_time FROM %s where script('doc[\\'insert_time\''].date.hourOfDay==16') " + - "and insert_time <'2014-08-21T00:00:00.000Z'", TEST_INDEX_ONLINE)); + JSONObject result = + executeQuery( + String.format( + Locale.ROOT, + "SELECT insert_time FROM %s where" + + " script('doc[\\'insert_time\''].date.hourOfDay==16') and insert_time" + + " <'2014-08-21T00:00:00.000Z'", + TEST_INDEX_ONLINE)); Assert.assertEquals(237, getTotalHits(result)); } @@ -1612,20 +1806,28 @@ public void scriptFilterNoParams() throws IOException { @Test public void scriptFilterWithParams() throws IOException { - JSONObject result = executeQuery(String.format(Locale.ROOT, - "SELECT insert_time FROM %s where script('doc[\\'insert_time\''].date.hourOfDay==x','x'=16) " + - "and insert_time <'2014-08-21T00:00:00.000Z'", TEST_INDEX_ONLINE)); + JSONObject result = + executeQuery( + String.format( + Locale.ROOT, + "SELECT insert_time FROM %s where" + + " script('doc[\\'insert_time\''].date.hourOfDay==x','x'=16) and insert_time" + + " <'2014-08-21T00:00:00.000Z'", + TEST_INDEX_ONLINE)); Assert.assertEquals(237, getTotalHits(result)); } @Test public void highlightPreTagsAndPostTags() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, - "SELECT /*! HIGHLIGHT(phrase, pre_tags : [''], post_tags : ['']) */ " + - "* FROM %s " + - "WHERE phrase LIKE 'fox' " + - "ORDER BY _score", TestsConstants.TEST_INDEX_PHRASE)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT /*! HIGHLIGHT(phrase, pre_tags : [''], post_tags : ['']) */ " + + "* FROM %s " + + "WHERE phrase LIKE 'fox' " + + "ORDER BY _score", + TestsConstants.TEST_INDEX_PHRASE)); JSONArray hits = getHits(response); for (int i = 0; i < hits.length(); i++) { @@ -1640,13 +1842,17 @@ public void highlightPreTagsAndPostTags() throws IOException { @Ignore @Test public void fieldCollapsingTest() throws IOException { - JSONObject response = executeQuery( - String.format(Locale.ROOT, "SELECT /*! COLLAPSE({\"field\":\"age\"," + - "\"inner_hits\":{\"name\": \"account\"," + - "\"size\":1," + - "\"sort\":[{\"age\":\"asc\"}]}," + - "\"max_concurrent_group_searches\": 4}) */ " + - "* FROM %s", TEST_INDEX_ACCOUNT)); + JSONObject response = + executeQuery( + String.format( + Locale.ROOT, + "SELECT /*! COLLAPSE({\"field\":\"age\"," + + "\"inner_hits\":{\"name\": \"account\"," + + "\"size\":1," + + "\"sort\":[{\"age\":\"asc\"}]}," + + "\"max_concurrent_group_searches\": 4}) */ " + + "* FROM %s", + TEST_INDEX_ACCOUNT)); JSONArray hits = getHits(response); Assert.assertEquals(21, hits.length()); @@ -1656,8 +1862,8 @@ public void fieldCollapsingTest() throws IOException { @Test public void backticksQuotedIndexNameTest() throws Exception { TestUtils.createIndexByRestClient(client(), "bank_unquote", null); - TestUtils - .loadDataByRestClient(client(), "bank", "/src/test/resources/bank_for_unquote_test.json"); + TestUtils.loadDataByRestClient( + client(), "bank", "/src/test/resources/bank_for_unquote_test.json"); JSONArray hits = getHits(executeQuery("SELECT lastname FROM `bank`")); Object responseIndex = ((JSONObject) hits.get(0)).query("/_index"); @@ -1665,39 +1871,57 @@ public void backticksQuotedIndexNameTest() throws Exception { assertEquals( executeQuery("SELECT lastname FROM bank", "jdbc"), - executeQuery("SELECT `bank`.`lastname` FROM `bank`", "jdbc") - ); + executeQuery("SELECT `bank`.`lastname` FROM `bank`", "jdbc")); assertEquals( executeQuery( - "SELECT `b`.`age` AS `AGE`, AVG(`b`.`balance`) FROM `bank` AS `b` " + - "WHERE ABS(`b`.`age`) > 20 GROUP BY `b`.`age` ORDER BY `b`.`age`", + "SELECT `b`.`age` AS `AGE`, AVG(`b`.`balance`) FROM `bank` AS `b` " + + "WHERE ABS(`b`.`age`) > 20 GROUP BY `b`.`age` ORDER BY `b`.`age`", "jdbc"), - executeQuery("SELECT b.age AS AGE, AVG(balance) FROM bank AS b " + - "WHERE ABS(age) > 20 GROUP BY b.age ORDER BY b.age", - "jdbc") - ); + executeQuery( + "SELECT b.age AS AGE, AVG(balance) FROM bank AS b " + + "WHERE ABS(age) > 20 GROUP BY b.age ORDER BY b.age", + "jdbc")); } @Test public void backticksQuotedFieldNamesTest() { - String expected = executeQuery(StringUtils.format("SELECT b.lastname FROM %s " + - "AS b ORDER BY age LIMIT 3", TestsConstants.TEST_INDEX_BANK), "jdbc"); - String quotedFieldResult = executeQuery(StringUtils.format("SELECT b.`lastname` FROM %s " + - "AS b ORDER BY age LIMIT 3", TestsConstants.TEST_INDEX_BANK), "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT b.lastname FROM %s AS b ORDER BY age LIMIT 3", + TestsConstants.TEST_INDEX_BANK), + "jdbc"); + String quotedFieldResult = + executeQuery( + StringUtils.format( + "SELECT b.`lastname` FROM %s AS b ORDER BY age LIMIT 3", + TestsConstants.TEST_INDEX_BANK), + "jdbc"); assertEquals(expected, quotedFieldResult); } @Test public void backticksQuotedAliasTest() { - String expected = executeQuery(StringUtils.format("SELECT b.lastname FROM %s " + - "AS b ORDER BY age LIMIT 3", TestsConstants.TEST_INDEX_BANK), "jdbc"); - String quotedAliasResult = executeQuery(StringUtils.format("SELECT `b`.lastname FROM %s" + - " AS `b` ORDER BY age LIMIT 3", TestsConstants.TEST_INDEX_BANK), "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT b.lastname FROM %s AS b ORDER BY age LIMIT 3", + TestsConstants.TEST_INDEX_BANK), + "jdbc"); + String quotedAliasResult = + executeQuery( + StringUtils.format( + "SELECT `b`.lastname FROM %s AS `b` ORDER BY age LIMIT 3", + TestsConstants.TEST_INDEX_BANK), + "jdbc"); String quotedAliasAndFieldResult = - executeQuery(StringUtils.format("SELECT `b`.`lastname` FROM %s " + - "AS `b` ORDER BY age LIMIT 3", TestsConstants.TEST_INDEX_BANK), "jdbc"); + executeQuery( + StringUtils.format( + "SELECT `b`.`lastname` FROM %s AS `b` ORDER BY age LIMIT 3", + TestsConstants.TEST_INDEX_BANK), + "jdbc"); assertEquals(expected, quotedAliasResult); assertEquals(expected, quotedAliasAndFieldResult); @@ -1705,19 +1929,28 @@ public void backticksQuotedAliasTest() { @Test public void backticksQuotedAliasWithSpecialCharactersTest() { - String expected = executeQuery(StringUtils.format("SELECT b.lastname FROM %s " + - "AS b ORDER BY age LIMIT 3", TestsConstants.TEST_INDEX_BANK), "jdbc"); + String expected = + executeQuery( + StringUtils.format( + "SELECT b.lastname FROM %s AS b ORDER BY age LIMIT 3", + TestsConstants.TEST_INDEX_BANK), + "jdbc"); String specialCharAliasResult = - executeQuery(StringUtils.format("SELECT `b k`.lastname FROM %s " + - "AS `b k` ORDER BY age LIMIT 3", TestsConstants.TEST_INDEX_BANK), "jdbc"); + executeQuery( + StringUtils.format( + "SELECT `b k`.lastname FROM %s AS `b k` ORDER BY age LIMIT 3", + TestsConstants.TEST_INDEX_BANK), + "jdbc"); assertEquals(expected, specialCharAliasResult); } @Test public void backticksQuotedAliasInJDBCResponseTest() { - String query = StringUtils.format("SELECT `b`.`lastname` AS `name` FROM %s AS `b` " + - "ORDER BY age LIMIT 3", TestsConstants.TEST_INDEX_BANK); + String query = + StringUtils.format( + "SELECT `b`.`lastname` AS `name` FROM %s AS `b` ORDER BY age LIMIT 3", + TestsConstants.TEST_INDEX_BANK); String response = executeQuery(query, "jdbc"); assertTrue(response.contains("\"alias\": \"name\"")); @@ -1725,10 +1958,14 @@ public void backticksQuotedAliasInJDBCResponseTest() { @Test public void caseWhenSwitchTest() throws IOException { - JSONObject response = executeQuery("SELECT CASE age " + - "WHEN 30 THEN '1' " + - "WHEN 40 THEN '2' " + - "ELSE '0' END AS cases FROM " + TEST_INDEX_ACCOUNT + " WHERE age IS NOT NULL"); + JSONObject response = + executeQuery( + "SELECT CASE age " + + "WHEN 30 THEN '1' " + + "WHEN 40 THEN '2' " + + "ELSE '0' END AS cases FROM " + + TEST_INDEX_ACCOUNT + + " WHERE age IS NOT NULL"); JSONObject hit = getHits(response).getJSONObject(0); String age = hit.query("/_source/age").toString(); String cases = age.equals("30") ? "1" : age.equals("40") ? "2" : "0"; @@ -1738,49 +1975,61 @@ public void caseWhenSwitchTest() throws IOException { @Test public void caseWhenJdbcResponseTest() { - String response = executeQuery("SELECT CASE age " + - "WHEN 30 THEN 'age is 30' " + - "WHEN 40 THEN 'age is 40' " + - "ELSE 'NA' END AS cases FROM " + TEST_INDEX_ACCOUNT + " WHERE age is not null", "jdbc"); + String response = + executeQuery( + "SELECT CASE age " + + "WHEN 30 THEN 'age is 30' " + + "WHEN 40 THEN 'age is 40' " + + "ELSE 'NA' END AS cases FROM " + + TEST_INDEX_ACCOUNT + + " WHERE age is not null", + "jdbc"); assertTrue( - response.contains("age is 30") || - response.contains("age is 40") || - response.contains("NA") - ); + response.contains("age is 30") + || response.contains("age is 40") + || response.contains("NA")); } @Ignore("This is already supported in new SQL engine") @Test public void functionInCaseFieldShouldThrowESExceptionDueToIllegalScriptInJdbc() { - String response = executeQuery( - "select case lower(firstname) when 'amber' then '1' else '2' end as cases from " + - TEST_INDEX_ACCOUNT, - "jdbc"); - queryInJdbcResponseShouldIndicateESException(response, "SearchPhaseExecutionException", + String response = + executeQuery( + "select case lower(firstname) when 'amber' then '1' else '2' end as cases from " + + TEST_INDEX_ACCOUNT, + "jdbc"); + queryInJdbcResponseShouldIndicateESException( + response, + "SearchPhaseExecutionException", "For more details, please send request for Json format"); } @Ignore("This is already supported in our new query engine") @Test public void functionCallWithIllegalScriptShouldThrowESExceptionInJdbc() { - String response = executeQuery("select log(balance + 2) from " + TEST_INDEX_BANK, - "jdbc"); - queryInJdbcResponseShouldIndicateESException(response, "SearchPhaseExecutionException", + String response = executeQuery("select log(balance + 2) from " + TEST_INDEX_BANK, "jdbc"); + queryInJdbcResponseShouldIndicateESException( + response, + "SearchPhaseExecutionException", "please send request for Json format to see the raw response from OpenSearch engine."); } - @Ignore("Goes in different route, does not call PrettyFormatRestExecutor.execute methods." + - "The performRequest method in RestClient doesn't throw any exceptions for null value fields in script") + @Ignore( + "Goes in different route, does not call PrettyFormatRestExecutor.execute methods.The" + + " performRequest method in RestClient doesn't throw any exceptions for null value" + + " fields in script") @Test public void functionArgWithNullValueFieldShouldThrowESExceptionInJdbc() { - String response = executeQuery( - "select log(balance) from " + TEST_INDEX_BANK_WITH_NULL_VALUES, "jdbc"); - queryInJdbcResponseShouldIndicateESException(response, "SearchPhaseExecutionException", + String response = + executeQuery("select log(balance) from " + TEST_INDEX_BANK_WITH_NULL_VALUES, "jdbc"); + queryInJdbcResponseShouldIndicateESException( + response, + "SearchPhaseExecutionException", "For more details, please send request for Json format"); } - private void queryInJdbcResponseShouldIndicateESException(String response, String exceptionType, - String... errMsgs) { + private void queryInJdbcResponseShouldIndicateESException( + String response, String exceptionType, String... errMsgs) { Assert.assertThat(response, containsString(exceptionType)); for (String errMsg : errMsgs) { Assert.assertThat(response, containsString(errMsg)); @@ -1803,9 +2052,21 @@ private void checkAggregationResponseSize(JSONObject response, int sizeCheck) { private void checkSelectAllAndFieldResponseSize(JSONObject response) { String[] arr = - new String[] {"account_number", "firstname", "address", "birthdate", "gender", "city", - "lastname", - "balance", "employer", "state", "age", "email", "male"}; + new String[] { + "account_number", + "firstname", + "address", + "birthdate", + "gender", + "city", + "lastname", + "balance", + "employer", + "state", + "age", + "email", + "male" + }; Set expectedSource = new HashSet<>(Arrays.asList(arr)); JSONArray hits = getHits(response); diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/RestIntegTestCase.java b/integ-test/src/test/java/org/opensearch/sql/legacy/RestIntegTestCase.java index dd48d82114..e4f1cc552d 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/RestIntegTestCase.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/RestIntegTestCase.java @@ -44,11 +44,13 @@ import org.junit.Before; import org.opensearch.client.Request; import org.opensearch.client.Response; +import org.opensearch.core.common.Strings; import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.core.rest.RestStatus; import org.opensearch.core.xcontent.XContentBuilder; /** + *
  * SQL plugin integration test base class (migrated from SQLIntegTestCase)
  * 

* The execution of order is as follows: @@ -60,6 +62,7 @@ * XXXTIT: 3) init() 5) init() *

* TODO: this base class should extends ODFERestTestCase + *

*/ public abstract class RestIntegTestCase extends OpenSearchSQLRestTestCase { diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLFunctionsIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLFunctionsIT.java index c1c1a26f4a..356b910d5f 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLFunctionsIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLFunctionsIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.hamcrest.Matchers.allOf; @@ -45,10 +44,7 @@ import org.opensearch.search.SearchHit; import org.opensearch.search.SearchHits; - -/** - * Created by allwefantasy on 8/25/16. - */ +/** Created by allwefantasy on 8/25/16. */ public class SQLFunctionsIT extends SQLIntegTestCase { @Override @@ -61,70 +57,75 @@ protected void init() throws Exception { @Test public void functionFieldAliasAndGroupByAlias() throws Exception { - String query = "SELECT " + - "floor(substring(address,0,3)*20) as key," + - "sum(age) cvalue FROM " + TEST_INDEX_ACCOUNT + " where address is not null " + - "group by key order by cvalue desc limit 10 "; + String query = + "SELECT " + + "floor(substring(address,0,3)*20) as key," + + "sum(age) cvalue FROM " + + TEST_INDEX_ACCOUNT + + " where address is not null " + + "group by key order by cvalue desc limit 10 "; final JSONObject result = executeQuery(query); - - IntStream.rangeClosed(0, 9).forEach(i -> { - Assert.assertNotNull(result.query(String.format("/aggregations/key/buckets/%d/key", i))); - Assert.assertNotNull( - result.query(String.format("/aggregations/key/buckets/%d/cvalue/value", i))); - } - ); + IntStream.rangeClosed(0, 9) + .forEach( + i -> { + Assert.assertNotNull( + result.query(String.format("/aggregations/key/buckets/%d/key", i))); + Assert.assertNotNull( + result.query(String.format("/aggregations/key/buckets/%d/cvalue/value", i))); + }); } /** * todo fix the issue. * - * @see https://github.com/opendistro-for-elasticsearch/sql/issues/59 + * @see https://github.com/opendistro-for-elasticsearch/sql/issues/59 */ @Ignore public void normalFieldAlias() throws Exception { - //here is a bug,csv field with spa - String query = "SELECT " + - "address as key,age from " + - TEST_INDEX_ACCOUNT + " where address is not null " + - "limit 10 "; + // here is a bug,csv field with spa + String query = + "SELECT " + + "address as key,age from " + + TEST_INDEX_ACCOUNT + + " where address is not null " + + "limit 10 "; - assertThat( - executeQuery(query), - hitAny(kvString("/_source/key", not(isEmptyOrNullString()))) - ); + assertThat(executeQuery(query), hitAny(kvString("/_source/key", not(isEmptyOrNullString())))); } @Test public void functionAlias() throws Exception { - //here is a bug,if only script fields are included,then all fields will return; fix later - String query = "SELECT " + - "substring(address,0,3) as key,address from " + - TEST_INDEX_ACCOUNT + " where address is not null " + - "order by address desc limit 10 "; + // here is a bug,if only script fields are included,then all fields will return; fix later + String query = + "SELECT " + + "substring(address,0,3) as key,address from " + + TEST_INDEX_ACCOUNT + + " where address is not null " + + "order by address desc limit 10 "; assertThat( executeQuery(query), - hitAny(both(kvString("/_source/address", equalTo("863 Wythe Place"))) - .and(kvString("/fields/key/0", - equalTo("863")))) - ); + hitAny( + both(kvString("/_source/address", equalTo("863 Wythe Place"))) + .and(kvString("/fields/key/0", equalTo("863"))))); } @Test public void caseChangeTest() throws IOException { - String query = "SELECT LOWER(firstname) " + - "FROM opensearch-sql_test_index_account " + - "WHERE UPPER(lastname)='DUKE' " + - "ORDER BY upper(lastname) "; + String query = + "SELECT LOWER(firstname) " + + "FROM opensearch-sql_test_index_account " + + "WHERE UPPER(lastname)='DUKE' " + + "ORDER BY upper(lastname) "; assertThat( executeQuery(query), hitAny( kvString("/_source/address", equalTo("880 Holmes Lane")), - kvString("/fields/LOWER(firstname)/0", equalTo("amber"))) - ); + kvString("/fields/LOWER(firstname)/0", equalTo("amber")))); } @Test @@ -133,23 +134,23 @@ public void caseChangeTestWithLocale() throws IOException { // "IL".toLowerCase() in a Turkish locale returns "ıl" // https://stackoverflow.com/questions/11063102/using-locales-with-javas-tolowercase-and-touppercase - String query = "SELECT LOWER(state.keyword, 'tr') " + - "FROM opensearch-sql_test_index_account " + - "WHERE account_number=1"; + String query = + "SELECT LOWER(state.keyword, 'tr') " + + "FROM opensearch-sql_test_index_account " + + "WHERE account_number=1"; assertThat( executeQuery(query), - hitAny( - kvString("/fields/LOWER(state.keyword, 'tr')/0", equalTo("ıl"))) - ); + hitAny(kvString("/fields/LOWER(state.keyword, 'tr')/0", equalTo("ıl")))); } @Test public void caseChangeWithAggregationTest() throws IOException { - String query = "SELECT UPPER(e.firstname) AS upper, COUNT(*)" + - "FROM opensearch-sql_test_index_account e " + - "WHERE LOWER(e.lastname)='duke' " + - "GROUP BY upper"; + String query = + "SELECT UPPER(e.firstname) AS upper, COUNT(*)" + + "FROM opensearch-sql_test_index_account e " + + "WHERE LOWER(e.lastname)='duke' " + + "GROUP BY upper"; assertThat( executeQuery(query), @@ -158,8 +159,10 @@ public void caseChangeWithAggregationTest() throws IOException { @Test public void castIntFieldToDoubleWithoutAliasTest() throws IOException { - String query = "SELECT CAST(age AS DOUBLE) FROM " + TestsConstants.TEST_INDEX_ACCOUNT + - " ORDER BY age DESC LIMIT 5"; + String query = + "SELECT CAST(age AS DOUBLE) FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " ORDER BY age DESC LIMIT 5"; SearchHit[] hits = query(query).getHits(); checkSuccessfulFieldCast(hits, "cast_age", "DOUBLE"); @@ -171,8 +174,9 @@ public void castIntFieldToDoubleWithoutAliasTest() throws IOException { @Test public void castIntFieldToDoubleWithAliasTest() throws IOException { String query = - "SELECT CAST(age AS DOUBLE) AS test_alias FROM " + TestsConstants.TEST_INDEX_ACCOUNT + - " ORDER BY age LIMIT 5"; + "SELECT CAST(age AS DOUBLE) AS test_alias FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " ORDER BY age LIMIT 5"; SearchHit[] hits = query(query).getHits(); checkSuccessfulFieldCast(hits, "test_alias", "DOUBLE"); @@ -183,8 +187,10 @@ public void castIntFieldToDoubleWithAliasTest() throws IOException { @Test public void castIntFieldToStringWithoutAliasTest() throws IOException { - String query = "SELECT CAST(balance AS STRING) FROM " + TestsConstants.TEST_INDEX_ACCOUNT + - " ORDER BY balance LIMIT 1"; + String query = + "SELECT CAST(balance AS STRING) FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " ORDER BY balance LIMIT 1"; SearchHit[] hits = query(query).getHits(); checkSuccessfulFieldCast(hits, "cast_balance", "STRING"); @@ -195,48 +201,51 @@ public void castIntFieldToStringWithoutAliasTest() throws IOException { @Test public void castIntFieldToStringWithAliasTest() throws IOException { - String query = "SELECT CAST(balance AS STRING) AS cast_string_alias FROM " + - TestsConstants.TEST_INDEX_ACCOUNT + - " ORDER BY cast_string_alias DESC LIMIT 1"; + String query = + "SELECT CAST(balance AS STRING) AS cast_string_alias FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " ORDER BY cast_string_alias DESC LIMIT 1"; SearchHit[] hits = query(query).getHits(); checkSuccessfulFieldCast(hits, "cast_string_alias", "STRING"); for (int i = 0; i < hits.length; ++i) { Assert.assertThat(hits[i].getFields().get("cast_string_alias").getValue(), is("9838")); } - } @Test public void castIntFieldToFloatWithoutAliasJdbcFormatTest() { - JSONObject response = executeJdbcRequest( - "SELECT CAST(balance AS FLOAT) AS cast_balance FROM " + TestsConstants.TEST_INDEX_ACCOUNT + - " ORDER BY balance DESC LIMIT 1"); + JSONObject response = + executeJdbcRequest( + "SELECT CAST(balance AS FLOAT) AS cast_balance FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " ORDER BY balance DESC LIMIT 1"); - verifySchema(response, - schema("CAST(balance AS FLOAT)", "cast_balance", "float")); + verifySchema(response, schema("CAST(balance AS FLOAT)", "cast_balance", "float")); - verifyDataRows(response, - rows(49989.0)); + verifyDataRows(response, rows(49989.0)); } @Test public void castIntFieldToFloatWithAliasJdbcFormatTest() { - JSONObject response = executeJdbcRequest( - "SELECT CAST(balance AS FLOAT) AS jdbc_float_alias " + - "FROM " + TestsConstants.TEST_INDEX_ACCOUNT + " ORDER BY jdbc_float_alias LIMIT 1"); + JSONObject response = + executeJdbcRequest( + "SELECT CAST(balance AS FLOAT) AS jdbc_float_alias " + + "FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " ORDER BY jdbc_float_alias LIMIT 1"); - verifySchema(response, - schema("CAST(balance AS FLOAT)", "jdbc_float_alias", "float")); + verifySchema(response, schema("CAST(balance AS FLOAT)", "jdbc_float_alias", "float")); - verifyDataRows(response, - rows(1011.0)); + verifyDataRows(response, rows(1011.0)); } @Test public void castIntFieldToDoubleWithoutAliasOrderByTest() throws IOException { - String query = "SELECT CAST(age AS DOUBLE) FROM " + TestsConstants.TEST_INDEX_ACCOUNT + - " ORDER BY age LIMIT 1"; + String query = + "SELECT CAST(age AS DOUBLE) FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " ORDER BY age LIMIT 1"; SearchHit[] hits = query(query).getHits(); checkSuccessfulFieldCast(hits, "cast_age", "DOUBLE"); @@ -247,148 +256,138 @@ public void castIntFieldToDoubleWithoutAliasOrderByTest() throws IOException { @Test public void castIntFieldToDoubleWithAliasOrderByTest() throws IOException { - String query = "SELECT CAST(age AS DOUBLE) AS alias FROM " + TestsConstants.TEST_INDEX_ACCOUNT + - " ORDER BY alias DESC LIMIT 1"; + String query = + "SELECT CAST(age AS DOUBLE) AS alias FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " ORDER BY alias DESC LIMIT 1"; SearchHit[] hits = query(query).getHits(); checkSuccessfulFieldCast(hits, "alias", "DOUBLE"); for (int i = 0; i < hits.length; ++i) { Assert.assertThat(hits[i].getFields().get("alias").getValue(), is(40.0)); } - } @Test public void castIntFieldToFloatWithoutAliasJdbcFormatGroupByTest() { - JSONObject response = executeJdbcRequest( - "SELECT CAST(balance AS FLOAT) FROM " + - TestsConstants.TEST_INDEX_ACCOUNT + " GROUP BY balance ORDER BY balance DESC LIMIT 5"); + JSONObject response = + executeJdbcRequest( + "SELECT CAST(balance AS FLOAT) FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " GROUP BY balance ORDER BY balance DESC LIMIT 5"); - verifySchema(response, - schema("CAST(balance AS FLOAT)", null, "float")); + verifySchema(response, schema("CAST(balance AS FLOAT)", null, "float")); - verifyDataRows(response, - rows(49989.0), - rows(49795.0), - rows(49741.0), - rows(49671.0), - rows(49587.0)); + verifyDataRows( + response, rows(49989.0), rows(49795.0), rows(49741.0), rows(49671.0), rows(49587.0)); } @Test public void castIntFieldToFloatWithAliasJdbcFormatGroupByTest() { - JSONObject response = executeJdbcRequest( - "SELECT CAST(balance AS FLOAT) AS jdbc_float_alias " - + " FROM " + TestsConstants.TEST_INDEX_ACCOUNT - + " GROUP BY jdbc_float_alias " - + " ORDER BY jdbc_float_alias ASC " - + " LIMIT 5"); + JSONObject response = + executeJdbcRequest( + "SELECT CAST(balance AS FLOAT) AS jdbc_float_alias " + + " FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " GROUP BY jdbc_float_alias " + + " ORDER BY jdbc_float_alias ASC " + + " LIMIT 5"); - verifySchema(response, - schema("CAST(balance AS FLOAT)", "jdbc_float_alias", "float")); + verifySchema(response, schema("CAST(balance AS FLOAT)", "jdbc_float_alias", "float")); - verifyDataRows(response, - rows(1011.0), - rows(10116.0), - rows(10138.0), - rows(10147.0), - rows(10178.0)); + verifyDataRows( + response, rows(1011.0), rows(10116.0), rows(10138.0), rows(10147.0), rows(10178.0)); } @Test public void castIntFieldToDoubleWithAliasJdbcFormatGroupByTest() { - JSONObject response = executeJdbcRequest( - "SELECT CAST(age AS DOUBLE) AS jdbc_double_alias " + - "FROM " + TestsConstants.TEST_INDEX_ACCOUNT + - " GROUP BY jdbc_double_alias DESC LIMIT 5"); + JSONObject response = + executeJdbcRequest( + "SELECT CAST(age AS DOUBLE) AS jdbc_double_alias " + + "FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " GROUP BY jdbc_double_alias DESC LIMIT 5"); - verifySchema(response, - schema("jdbc_double_alias", "jdbc_double_alias", "double")); + verifySchema(response, schema("jdbc_double_alias", "jdbc_double_alias", "double")); - verifyDataRows(response, - rows("31.0"), - rows("39.0"), - rows("26.0"), - rows("32.0"), - rows("35.0")); + verifyDataRows(response, rows("31.0"), rows("39.0"), rows("26.0"), rows("32.0"), rows("35.0")); } @Test public void castKeywordFieldToDatetimeWithoutAliasJdbcFormatTest() { - JSONObject response = executeJdbcRequest("SELECT CAST(date_keyword AS DATETIME) FROM " - + TestsConstants.TEST_INDEX_DATE + " ORDER BY date_keyword"); + JSONObject response = + executeJdbcRequest( + "SELECT CAST(date_keyword AS DATETIME) FROM " + + TestsConstants.TEST_INDEX_DATE + + " ORDER BY date_keyword"); verifySchema(response, schema("cast_date_keyword", null, "date")); - verifyDataRows(response, - rows("2014-08-19 07:09:13.434"), - rows("2019-09-25 02:04:13.469")); + verifyDataRows(response, rows("2014-08-19 07:09:13.434"), rows("2019-09-25 02:04:13.469")); } @Test public void castKeywordFieldToDatetimeWithAliasJdbcFormatTest() { JSONObject response = - executeJdbcRequest("SELECT CAST(date_keyword AS DATETIME) AS test_alias FROM " - + TestsConstants.TEST_INDEX_DATE + " ORDER BY date_keyword"); + executeJdbcRequest( + "SELECT CAST(date_keyword AS DATETIME) AS test_alias FROM " + + TestsConstants.TEST_INDEX_DATE + + " ORDER BY date_keyword"); verifySchema(response, schema("test_alias", null, "date")); - verifyDataRows(response, - rows("2014-08-19 07:09:13.434"), - rows("2019-09-25 02:04:13.469")); + verifyDataRows(response, rows("2014-08-19 07:09:13.434"), rows("2019-09-25 02:04:13.469")); } @Test public void castFieldToDatetimeWithWhereClauseJdbcFormatTest() { - JSONObject response = executeJdbcRequest("SELECT CAST(date_keyword AS DATETIME) FROM " - + TestsConstants.TEST_INDEX_DATE + " WHERE date_keyword IS NOT NULL ORDER BY date_keyword"); + JSONObject response = + executeJdbcRequest( + "SELECT CAST(date_keyword AS DATETIME) FROM " + + TestsConstants.TEST_INDEX_DATE + + " WHERE date_keyword IS NOT NULL ORDER BY date_keyword"); verifySchema(response, schema("cast_date_keyword", null, "date")); - verifyDataRows(response, - rows("2014-08-19 07:09:13.434"), - rows("2019-09-25 02:04:13.469")); + verifyDataRows(response, rows("2014-08-19 07:09:13.434"), rows("2019-09-25 02:04:13.469")); } @Test public void castFieldToDatetimeWithGroupByJdbcFormatTest() { JSONObject response = - executeJdbcRequest("SELECT CAST(date_keyword AS DATETIME) AS test_alias FROM " - + TestsConstants.TEST_INDEX_DATE + " GROUP BY test_alias DESC"); + executeJdbcRequest( + "SELECT CAST(date_keyword AS DATETIME) AS test_alias FROM " + + TestsConstants.TEST_INDEX_DATE + + " GROUP BY test_alias DESC"); verifySchema(response, schema("test_alias", "test_alias", "double")); - verifyDataRows(response, - rows("2014-08-19T07:09:13.434Z"), - rows("2019-09-25T02:04:13.469Z")); + verifyDataRows(response, rows("2014-08-19T07:09:13.434Z"), rows("2019-09-25T02:04:13.469Z")); } - @Test public void castBoolFieldToNumericValueInSelectClause() { JSONObject response = executeJdbcRequest( "SELECT " - + " male, " - + " CAST(male AS INT) AS cast_int, " - + " CAST(male AS LONG) AS cast_long, " - + " CAST(male AS FLOAT) AS cast_float, " - + " CAST(male AS DOUBLE) AS cast_double " - + "FROM " + TestsConstants.TEST_INDEX_BANK + " " - + "WHERE account_number = 1 OR account_number = 13" - ); - - verifySchema(response, + + " male, " + + " CAST(male AS INT) AS cast_int, " + + " CAST(male AS LONG) AS cast_long, " + + " CAST(male AS FLOAT) AS cast_float, " + + " CAST(male AS DOUBLE) AS cast_double " + + "FROM " + + TestsConstants.TEST_INDEX_BANK + + " " + + "WHERE account_number = 1 OR account_number = 13"); + + verifySchema( + response, schema("male", "boolean"), schema("CAST(male AS INT)", "cast_int", "integer"), schema("CAST(male AS LONG)", "cast_long", "long"), schema("CAST(male AS FLOAT)", "cast_float", "float"), - schema("CAST(male AS DOUBLE)", "cast_double", "double") - ); - verifyDataRows(response, - rows(true, 1, 1, 1.0, 1.0), - rows(false, 0, 0, 0.0, 0.0) - ); + schema("CAST(male AS DOUBLE)", "cast_double", "double")); + verifyDataRows(response, rows(true, 1, 1, 1.0, 1.0), rows(false, 0, 0, 0.0, 0.0)); } @Test @@ -396,90 +395,82 @@ public void castBoolFieldToNumericValueWithGroupByAlias() { JSONObject response = executeJdbcRequest( "SELECT " - + "CAST(male AS INT) AS cast_int, " - + "COUNT(*) " - + "FROM " + TestsConstants.TEST_INDEX_BANK + " " - + "GROUP BY cast_int" - ); - - verifySchema(response, + + "CAST(male AS INT) AS cast_int, " + + "COUNT(*) " + + "FROM " + + TestsConstants.TEST_INDEX_BANK + + " " + + "GROUP BY cast_int"); + + verifySchema( + response, schema("CAST(male AS INT)", "cast_int", "integer"), - schema("COUNT(*)", "integer") - ); - verifyDataRows(response, - rows(0, 3), - rows(1, 4) - ); + schema("COUNT(*)", "integer")); + verifyDataRows(response, rows(0, 3), rows(1, 4)); } @Test public void castStatementInWhereClauseGreaterThanTest() { - JSONObject response = executeJdbcRequest("SELECT balance FROM " + TEST_INDEX_ACCOUNT - + " WHERE (account_number < CAST(age AS DOUBLE)) ORDER BY balance LIMIT 5"); + JSONObject response = + executeJdbcRequest( + "SELECT balance FROM " + + TEST_INDEX_ACCOUNT + + " WHERE (account_number < CAST(age AS DOUBLE)) ORDER BY balance LIMIT 5"); verifySchema(response, schema("balance", null, "long")); - verifyDataRows(response, - rows(4180), - rows(5686), - rows(7004), - rows(7831), - rows(14127)); + verifyDataRows(response, rows(4180), rows(5686), rows(7004), rows(7831), rows(14127)); } @Test public void castStatementInWhereClauseLessThanTest() { - JSONObject response = executeJdbcRequest("SELECT balance FROM " + TEST_INDEX_ACCOUNT - + " WHERE (account_number > CAST(age AS DOUBLE)) ORDER BY balance LIMIT 5"); + JSONObject response = + executeJdbcRequest( + "SELECT balance FROM " + + TEST_INDEX_ACCOUNT + + " WHERE (account_number > CAST(age AS DOUBLE)) ORDER BY balance LIMIT 5"); verifySchema(response, schema("balance", null, "long")); - verifyDataRows(response, - rows(1011), - rows(1031), - rows(1110), - rows(1133), - rows(1172)); + verifyDataRows(response, rows(1011), rows(1031), rows(1110), rows(1133), rows(1172)); } @Test public void castStatementInWhereClauseEqualToConstantTest() { - JSONObject response = executeJdbcRequest("SELECT balance FROM " + TEST_INDEX_ACCOUNT - + " WHERE (CAST(age AS DOUBLE) = 36.0) ORDER BY balance LIMIT 5"); + JSONObject response = + executeJdbcRequest( + "SELECT balance FROM " + + TEST_INDEX_ACCOUNT + + " WHERE (CAST(age AS DOUBLE) = 36.0) ORDER BY balance LIMIT 5"); verifySchema(response, schema("balance", null, "long")); - verifyDataRows(response, - rows(1249), - rows(1463), - rows(3960), - rows(5686), - rows(6025)); + verifyDataRows(response, rows(1249), rows(1463), rows(3960), rows(5686), rows(6025)); } @Test public void castStatementInWhereClauseLessThanConstantTest() { - JSONObject response = executeJdbcRequest("SELECT balance FROM " + TEST_INDEX_ACCOUNT - + " WHERE (CAST(age AS DOUBLE) < 36.0) ORDER BY balance LIMIT 5"); + JSONObject response = + executeJdbcRequest( + "SELECT balance FROM " + + TEST_INDEX_ACCOUNT + + " WHERE (CAST(age AS DOUBLE) < 36.0) ORDER BY balance LIMIT 5"); verifySchema(response, schema("balance", null, "long")); - verifyDataRows(response, - rows(1011), - rows(1031), - rows(1110), - rows(1133), - rows(1172)); + verifyDataRows(response, rows(1011), rows(1031), rows(1110), rows(1133), rows(1172)); } /** - * Testing compilation - * Result comparison is empty then comparing different types (Date and keyword) + * Testing compilation Result comparison is empty then comparing different types (Date and + * keyword) */ @Test public void castStatementInWhereClauseDatetimeCastTest() { - JSONObject response = executeJdbcRequest("SELECT date_keyword FROM " - + TestsConstants.TEST_INDEX_DATE - + " WHERE (CAST(date_keyword AS DATETIME) = '2014-08-19T07:09:13.434Z')"); + JSONObject response = + executeJdbcRequest( + "SELECT date_keyword FROM " + + TestsConstants.TEST_INDEX_DATE + + " WHERE (CAST(date_keyword AS DATETIME) = '2014-08-19T07:09:13.434Z')"); String schema_result = "{\"name\":\"date_keyword\",\"type\":\"keyword\"}"; assertEquals(response.getJSONArray("schema").get(0).toString(), schema_result); @@ -487,30 +478,32 @@ public void castStatementInWhereClauseDatetimeCastTest() { @Test public void concat_ws_field_and_string() throws Exception { - //here is a bug,csv field with spa - String query = "SELECT " + - " concat_ws('-',age,'-') as age,address from " + - TEST_INDEX_ACCOUNT + " " + - " limit 10 "; + // here is a bug,csv field with spa + String query = + "SELECT " + + " concat_ws('-',age,'-') as age,address from " + + TEST_INDEX_ACCOUNT + + " " + + " limit 10 "; - assertThat( - executeQuery(query), - hitAny(kvString("/fields/age/0", endsWith("--"))) - ); + assertThat(executeQuery(query), hitAny(kvString("/fields/age/0", endsWith("--")))); } /** * Ignore this test case because painless doesn't allowlist String.split function. * - * @see https://www.elastic.co/guide/en/elasticsearch/painless/7.0/painless-api-reference.html + * @see https://www.elastic.co/guide/en/elasticsearch/painless/7.0/painless-api-reference.html */ @Ignore public void whereConditionLeftFunctionRightVariableEqualTest() throws Exception { - String query = "SELECT " + - " * from " + - TestsConstants.TEST_INDEX + " " + - " where split(address,' ')[0]='806' limit 1000 "; + String query = + "SELECT " + + " * from " + + TestsConstants.TEST_INDEX + + " " + + " where split(address,' ')[0]='806' limit 1000 "; assertThat(executeQuery(query).query("/hits/total"), equalTo(4)); } @@ -518,15 +511,18 @@ public void whereConditionLeftFunctionRightVariableEqualTest() throws Exception /** * Ignore this test case because painless doesn't allowlist String.split function. * - * @see https://www.elastic.co/guide/en/elasticsearch/painless/7.0/painless-api-reference.html + * @see https://www.elastic.co/guide/en/elasticsearch/painless/7.0/painless-api-reference.html */ @Ignore public void whereConditionLeftFunctionRightVariableGreatTest() throws Exception { - String query = "SELECT " + - " * from " + - TestsConstants.TEST_INDEX + " " + - " where floor(split(address,' ')[0]+0) > 805 limit 1000 "; + String query = + "SELECT " + + " * from " + + TestsConstants.TEST_INDEX + + " " + + " where floor(split(address,' ')[0]+0) > 805 limit 1000 "; assertThat(executeQuery(query).query("/hits/total"), equalTo(223)); } @@ -534,42 +530,45 @@ public void whereConditionLeftFunctionRightVariableGreatTest() throws Exception @Test public void concat_ws_fields() throws Exception { - //here is a bug,csv field with spa - String query = "SELECT " + - " concat_ws('-',age,address) as combine,address from " + - TEST_INDEX_ACCOUNT + " " + - " limit 10 "; - assertThat( - executeQuery(query), - hitAny(kvString("/fields/combine/0", containsString("-"))) - ); + // here is a bug,csv field with spa + String query = + "SELECT " + + " concat_ws('-',age,address) as combine,address from " + + TEST_INDEX_ACCOUNT + + " " + + " limit 10 "; + assertThat(executeQuery(query), hitAny(kvString("/fields/combine/0", containsString("-")))); } @Test public void functionLogs() throws Exception { - String query = "SELECT log10(100) as a, log(1) as b, log(2, 4) as c, log2(8) as d from " - + TEST_INDEX_ACCOUNT + " limit 1"; + String query = + "SELECT log10(100) as a, log(1) as b, log(2, 4) as c, log2(8) as d from " + + TEST_INDEX_ACCOUNT + + " limit 1"; assertThat( executeQuery(query), - hitAny(both(kvDouble("/fields/a/0", equalTo(Math.log10(100)))) - .and(kvDouble("/fields/b/0", equalTo(Math.log(1)))) - .and(kvDouble("/fields/c/0", closeTo(Math.log(4) / Math.log(2), 0.0001))) - .and(kvDouble("/fields/d/0", closeTo(Math.log(8) / Math.log(2), 0.0001)))) - ); + hitAny( + both(kvDouble("/fields/a/0", equalTo(Math.log10(100)))) + .and(kvDouble("/fields/b/0", equalTo(Math.log(1)))) + .and(kvDouble("/fields/c/0", closeTo(Math.log(4) / Math.log(2), 0.0001))) + .and(kvDouble("/fields/d/0", closeTo(Math.log(8) / Math.log(2), 0.0001))))); } @Test public void functionPow() throws Exception { - String query = "SELECT pow(account_number, 2) as key," + - "abs(age - 60) as new_age from " + TEST_INDEX_ACCOUNT + - " WHERE firstname = 'Virginia' and lastname='Ayala' limit 1"; + String query = + "SELECT pow(account_number, 2) as key," + + "abs(age - 60) as new_age from " + + TEST_INDEX_ACCOUNT + + " WHERE firstname = 'Virginia' and lastname='Ayala' limit 1"; assertThat( executeQuery(query), - hitAny(both(kvDouble("/fields/new_age/0", equalTo(21.0))) - .and(kvDouble("/fields/key/0", equalTo(625.0)))) - ); + hitAny( + both(kvDouble("/fields/new_age/0", equalTo(21.0))) + .and(kvDouble("/fields/key/0", equalTo(625.0))))); } @Test @@ -577,96 +576,88 @@ public void operatorSubstring() throws IOException { assertThat( executeQuery( "SELECT substring('sampleName', 1, 4) AS substring FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvString("/fields/substring/0", equalTo("samp"))) - ); + hitAny(kvString("/fields/substring/0", equalTo("samp")))); assertThat( executeQuery( "SELECT substring('sampleName', 0, 20) AS substring FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvString("/fields/substring/0", equalTo("sampleName"))) - ); + hitAny(kvString("/fields/substring/0", equalTo("sampleName")))); } @Test public void operatorLength() throws IOException { assertThat( - executeQuery("SELECT LENGTH(lastname) FROM " + TEST_INDEX_ACCOUNT + executeQuery( + "SELECT LENGTH(lastname) FROM " + + TEST_INDEX_ACCOUNT + " WHERE lastname IS NOT NULL GROUP BY LENGTH(lastname) ORDER BY LENGTH(lastname)", "jdbc"), - containsString("\"type\": \"integer\"") - ); + containsString("\"type\": \"integer\"")); assertThat( executeQuery("SELECT LENGTH('sampleName') AS length FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/length/0", equalTo(10))) - ); - + hitAny(kvInt("/fields/length/0", equalTo(10)))); } @Test public void operatorReplace() { String query = "SELECT REPLACE('elastic', 'el', 'fant') FROM " + TEST_INDEX_ACCOUNT; - assertThat( - executeQuery(query, "jdbc"), - containsString("fantastic") - ); + assertThat(executeQuery(query, "jdbc"), containsString("fantastic")); } - @Ignore("The LOCATE function is not implemented in new SQL engine. https://github" - + ".com/opensearch-project/sql/issues/74") + @Ignore( + "The LOCATE function is not implemented in new SQL engine. https://github" + + ".com/opensearch-project/sql/issues/74") public void operatorLocate() throws IOException { - String query = "SELECT LOCATE('a', lastname, 0) FROM " + TEST_INDEX_ACCOUNT - + - " WHERE lastname IS NOT NULL GROUP BY LOCATE('a', lastname, 0) ORDER BY LOCATE('a', lastname, 0)"; - assertThat( - executeQuery(query, "jdbc"), containsString("\"type\": \"integer\"") - ); + String query = + "SELECT LOCATE('a', lastname, 0) FROM " + + TEST_INDEX_ACCOUNT + + " WHERE lastname IS NOT NULL GROUP BY LOCATE('a', lastname, 0) ORDER BY LOCATE('a'," + + " lastname, 0)"; + assertThat(executeQuery(query, "jdbc"), containsString("\"type\": \"integer\"")); assertThat( executeQuery("SELECT LOCATE('a', 'sampleName', 3) AS locate FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/locate/0", equalTo(8))) - ); + hitAny(kvInt("/fields/locate/0", equalTo(8)))); assertThat( executeQuery("SELECT LOCATE('a', 'sampleName') AS locate FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/locate/0", equalTo(2))) - ); + hitAny(kvInt("/fields/locate/0", equalTo(2)))); } @Test public void rtrim() throws IOException { assertThat( executeQuery("SELECT RTRIM(' sampleName ') AS rtrim FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvString("/fields/rtrim/0", equalTo(" sampleName"))) - ); + hitAny(kvString("/fields/rtrim/0", equalTo(" sampleName")))); } @Test public void ltrim() throws IOException { assertThat( executeQuery("SELECT LTRIM(' sampleName ') AS ltrim FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvString("/fields/ltrim/0", equalTo("sampleName "))) - ); + hitAny(kvString("/fields/ltrim/0", equalTo("sampleName ")))); } - @Ignore("The ASCII function is not implemented in new SQL engine. https://github" - + ".com/opensearch-project/sql/issues/73") + @Ignore( + "The ASCII function is not implemented in new SQL engine. https://github" + + ".com/opensearch-project/sql/issues/73") public void ascii() throws IOException { assertThat( - executeQuery("SELECT ASCII(lastname) FROM " + TEST_INDEX_ACCOUNT - + - " WHERE lastname IS NOT NULL GROUP BY ASCII(lastname) ORDER BY ASCII(lastname) LIMIT 5", + executeQuery( + "SELECT ASCII(lastname) FROM " + + TEST_INDEX_ACCOUNT + + " WHERE lastname IS NOT NULL GROUP BY ASCII(lastname) ORDER BY ASCII(lastname)" + + " LIMIT 5", "jdbc"), - containsString("\"type\": \"integer\"") - ); + containsString("\"type\": \"integer\"")); assertThat( executeQuery("SELECT ASCII('sampleName') AS ascii FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/ascii/0", equalTo(115))) - ); + hitAny(kvInt("/fields/ascii/0", equalTo(115)))); } /** - * The following tests for LEFT and RIGHT are ignored because the OpenSearch client fails to parse "LEFT"/"RIGHT" in - * the integTest + * The following tests for LEFT and RIGHT are ignored because the OpenSearch client fails to parse + * "LEFT"/"RIGHT" in the integTest */ @Ignore @Test @@ -674,13 +665,11 @@ public void left() throws IOException { assertThat( executeQuery( "SELECT LEFT('sample', 2) AS left FROM " + TEST_INDEX_ACCOUNT + " ORDER BY left"), - hitAny(kvString("/fields/left/0", equalTo("sa"))) - ); + hitAny(kvString("/fields/left/0", equalTo("sa")))); assertThat( executeQuery( "SELECT LEFT('sample', 20) AS left FROM " + TEST_INDEX_ACCOUNT + " ORDER BY left"), - hitAny(kvString("/fields/left/0", equalTo("sample"))) - ); + hitAny(kvString("/fields/left/0", equalTo("sample")))); } @Ignore @@ -689,20 +678,20 @@ public void right() throws IOException { assertThat( executeQuery( "SELECT RIGHT('elastic', 3) AS right FROM " + TEST_INDEX_ACCOUNT + " ORDER BY right"), - hitAny(kvString("/fields/right/0", equalTo("tic"))) - ); + hitAny(kvString("/fields/right/0", equalTo("tic")))); assertThat( executeQuery( "SELECT RIGHT('elastic', 20) AS right FROM " + TEST_INDEX_ACCOUNT + " ORDER BY right"), - hitAny(kvString("/fields/right/0", equalTo("elastic"))) - ); + hitAny(kvString("/fields/right/0", equalTo("elastic")))); } @Test public void ifFuncShouldPassJDBC() { - JSONObject response = executeJdbcRequest( - "SELECT IF(age > 30, 'True', 'False') AS Ages FROM " + TEST_INDEX_ACCOUNT - + " WHERE age IS NOT NULL GROUP BY Ages"); + JSONObject response = + executeJdbcRequest( + "SELECT IF(age > 30, 'True', 'False') AS Ages FROM " + + TEST_INDEX_ACCOUNT + + " WHERE age IS NOT NULL GROUP BY Ages"); assertEquals("IF(age > 30, 'True', 'False')", response.query("/schema/0/name")); assertEquals("Ages", response.query("/schema/0/alias")); assertEquals("keyword", response.query("/schema/0/type")); @@ -712,35 +701,33 @@ public void ifFuncShouldPassJDBC() { public void ifFuncWithBinaryComparisonAsConditionTest() throws IOException { assertThat( executeQuery("SELECT IF(2 > 0, 'hello', 'world') AS ifTrue FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvString("/fields/ifTrue/0", equalTo("hello"))) - ); + hitAny(kvString("/fields/ifTrue/0", equalTo("hello")))); assertThat( executeQuery("SELECT IF(2 = 0, 'hello', 'world') AS ifFalse FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvString("/fields/ifFalse/0", equalTo("world"))) - ); + hitAny(kvString("/fields/ifFalse/0", equalTo("world")))); } @Test public void ifFuncWithBooleanExprInputAsConditionTest() throws IOException { assertThat( executeQuery("SELECT IF(true, 1, 0) AS ifBoolean FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/ifBoolean/0", equalTo(1))) - ); + hitAny(kvInt("/fields/ifBoolean/0", equalTo(1)))); } @Test public void ifFuncWithNullInputAsConditionTest() throws IOException { assertThat( executeQuery("SELECT IF(null, 1, 0) AS ifNull FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/ifNull/0", equalTo(0))) - ); + hitAny(kvInt("/fields/ifNull/0", equalTo(0)))); } @Test public void ifnullShouldPassJDBC() throws IOException { - JSONObject response = executeJdbcRequest( - "SELECT IFNULL(lastname, 'unknown') AS name FROM " + TEST_INDEX_ACCOUNT - + " GROUP BY name"); + JSONObject response = + executeJdbcRequest( + "SELECT IFNULL(lastname, 'unknown') AS name FROM " + + TEST_INDEX_ACCOUNT + + " GROUP BY name"); assertEquals("IFNULL(lastname, 'unknown')", response.query("/schema/0/name")); assertEquals("name", response.query("/schema/0/alias")); assertEquals("keyword", response.query("/schema/0/type")); @@ -750,27 +737,23 @@ public void ifnullShouldPassJDBC() throws IOException { public void ifnullWithNotNullInputTest() throws IOException { assertThat( executeQuery("SELECT IFNULL('sample', 'IsNull') AS ifnull FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvString("/fields/ifnull/0", equalTo("sample"))) - ); + hitAny(kvString("/fields/ifnull/0", equalTo("sample")))); } @Test public void ifnullWithNullInputTest() throws IOException { assertThat( executeQuery("SELECT IFNULL(null, 10) AS ifnull FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/ifnull/0", equalTo(10))) - ); + hitAny(kvInt("/fields/ifnull/0", equalTo(10)))); assertThat( executeQuery("SELECT IFNULL('', 10) AS ifnull FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvString("/fields/ifnull/0", equalTo(""))) - ); + hitAny(kvString("/fields/ifnull/0", equalTo("")))); } @Test public void isnullShouldPassJDBC() { JSONObject response = - executeJdbcRequest( - "SELECT ISNULL(lastname) AS name FROM " + TEST_INDEX_ACCOUNT); + executeJdbcRequest("SELECT ISNULL(lastname) AS name FROM " + TEST_INDEX_ACCOUNT); assertEquals("ISNULL(lastname)", response.query("/schema/0/name")); assertEquals("name", response.query("/schema/0/alias")); assertEquals("boolean", response.query("/schema/0/type")); @@ -780,61 +763,57 @@ public void isnullShouldPassJDBC() { public void isnullWithNotNullInputTest() throws IOException { assertThat( executeQuery("SELECT ISNULL('elastic') AS isnull FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/isnull/0", equalTo(0))) - ); + hitAny(kvInt("/fields/isnull/0", equalTo(0)))); assertThat( executeQuery("SELECT ISNULL('') AS isnull FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/isnull/0", equalTo(0))) - ); + hitAny(kvInt("/fields/isnull/0", equalTo(0)))); } @Test public void isnullWithNullInputTest() throws IOException { assertThat( executeQuery("SELECT ISNULL(null) AS isnull FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/isnull/0", equalTo(1))) - ); + hitAny(kvInt("/fields/isnull/0", equalTo(1)))); } @Test public void isnullWithMathExpr() throws IOException { assertThat( executeQuery("SELECT ISNULL(1+1) AS isnull FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/isnull/0", equalTo(0))) - ); + hitAny(kvInt("/fields/isnull/0", equalTo(0)))); assertThat( executeQuery("SELECT ISNULL(1+1*1/0) AS isnull FROM " + TEST_INDEX_ACCOUNT), - hitAny(kvInt("/fields/isnull/0", equalTo(1))) - ); + hitAny(kvInt("/fields/isnull/0", equalTo(1)))); } /** * Ignore this test case because painless doesn't allowlist String.split function. * - * @see https://www.elastic.co/guide/en/elasticsearch/painless/7.0/painless-api-reference.html + * @see https://www.elastic.co/guide/en/elasticsearch/painless/7.0/painless-api-reference.html */ @Ignore public void split_field() throws Exception { - //here is a bug,csv field with spa - String query = "SELECT " + - " split(address,' ')[0],age from " + - TestsConstants.TEST_INDEX + " where address is not null " + - " limit 10 "; + // here is a bug,csv field with spa + String query = + "SELECT " + + " split(address,' ')[0],age from " + + TestsConstants.TEST_INDEX + + " where address is not null " + + " limit 10 "; } @Test public void literal() throws Exception { - String query = "SELECT 10 " + - "from " + TEST_INDEX_ACCOUNT + " limit 1"; + String query = "SELECT 10 from " + TEST_INDEX_ACCOUNT + " limit 1"; final SearchHit[] hits = query(query).getHits(); assertThat(hits[0].getFields(), hasValue(contains(10))); } @Test public void literalWithDoubleValue() throws Exception { - String query = "SELECT 10.0 " + - "from " + TEST_INDEX_ACCOUNT + " limit 1"; + String query = "SELECT 10.0 from " + TEST_INDEX_ACCOUNT + " limit 1"; final SearchHit[] hits = query(query).getHits(); assertThat(hits[0].getFields(), hasValue(contains(10.0))); @@ -842,8 +821,7 @@ public void literalWithDoubleValue() throws Exception { @Test public void literalWithAlias() throws Exception { - String query = "SELECT 10 as key " + - "from " + TEST_INDEX_ACCOUNT + " limit 1"; + String query = "SELECT 10 as key from " + TEST_INDEX_ACCOUNT + " limit 1"; final SearchHit[] hits = query(query).getHits(); assertThat(hits.length, is(1)); @@ -852,8 +830,7 @@ public void literalWithAlias() throws Exception { @Test public void literalMultiField() throws Exception { - String query = "SELECT 1, 2 " + - "from " + TEST_INDEX_ACCOUNT + " limit 1"; + String query = "SELECT 1, 2 from " + TEST_INDEX_ACCOUNT + " limit 1"; final SearchHit[] hits = query(query).getHits(); assertThat(hits.length, is(1)); @@ -863,10 +840,11 @@ public void literalMultiField() throws Exception { private SearchHits query(String query) throws IOException { final String rsp = executeQueryWithStringOutput(query); - final XContentParser parser = new JsonXContentParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - new JsonFactory().createParser(rsp)); + final XContentParser parser = + new JsonXContentParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + new JsonFactory().createParser(rsp)); return SearchResponse.fromXContent(parser).getHits(); } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java index 58e55c4101..4479abdcc6 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static com.google.common.base.Strings.isNullOrEmpty; @@ -68,9 +67,7 @@ import org.opensearch.sql.common.setting.Settings; import org.opensearch.sql.datasource.model.DataSourceMetadata; -/** - * OpenSearch Rest integration test base for SQL testing - */ +/** OpenSearch Rest integration test base for SQL testing */ public abstract class SQLIntegTestCase extends OpenSearchSQLRestTestCase { public static final String PERSISTENT = "persistent"; @@ -102,12 +99,14 @@ protected boolean preserveClusterUponCompletion() { } /** + *
    * We need to be able to dump the jacoco coverage before cluster is shut down.
    * The new internal testing framework removed some of the gradle tasks we were listening to
    * to choose a good time to do it. This will dump the executionData to file after each test.
    * TODO: This is also currently just overwriting integTest.exec with the updated execData without
    * resetting after writing each time. This can be improved to either write an exec file per test
    * or by letting jacoco append to the file
+   * 
*/ public interface IProxy { byte[] getExecutionData(boolean reset); @@ -128,10 +127,12 @@ public static void dumpCoverage() { String serverUrl = "service:jmx:rmi:///jndi/rmi://127.0.0.1:7777/jmxrmi"; try (JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl))) { - IProxy proxy = MBeanServerInvocationHandler.newProxyInstance( - connector.getMBeanServerConnection(), new ObjectName("org.jacoco:type=Runtime"), - IProxy.class, - false); + IProxy proxy = + MBeanServerInvocationHandler.newProxyInstance( + connector.getMBeanServerConnection(), + new ObjectName("org.jacoco:type=Runtime"), + IProxy.class, + false); Path path = Paths.get(jacocoBuildPath + "/integTest.exec"); Files.write(path, proxy.getExecutionData(false)); @@ -141,9 +142,10 @@ public static void dumpCoverage() { } /** - * As JUnit JavaDoc says: - * "The @AfterClass methods declared in superclasses will be run after those of the current class." - * So this method is supposed to run before closeClients() in parent class. + * As JUnit JavaDoc says:
+ "The @AfterClass methods declared in superclasses will be run after those of the current class."
+ So this method is supposed to run before closeClients() in parent class. + * class. */ @AfterClass public static void cleanUpIndices() throws IOException { @@ -155,13 +157,16 @@ public static void cleanUpIndices() throws IOException { protected void setQuerySizeLimit(Integer limit) throws IOException { updateClusterSettings( - new ClusterSetting("transient", Settings.Key.QUERY_SIZE_LIMIT.getKeyValue(), limit.toString())); + new ClusterSetting( + "transient", Settings.Key.QUERY_SIZE_LIMIT.getKeyValue(), limit.toString())); } protected void resetQuerySizeLimit() throws IOException { updateClusterSettings( - new ClusterSetting("transient", Settings.Key.QUERY_SIZE_LIMIT.getKeyValue(), DEFAULT_QUERY_SIZE_LIMIT - .toString())); + new ClusterSetting( + "transient", + Settings.Key.QUERY_SIZE_LIMIT.getKeyValue(), + DEFAULT_QUERY_SIZE_LIMIT.toString())); } protected static void wipeAllClusterSettings() throws IOException { @@ -178,19 +183,16 @@ protected void setMaxResultWindow(String indexName, Integer window) throws IOExc } protected void resetMaxResultWindow(String indexName) throws IOException { - updateIndexSettings(indexName, - "{ \"index\": { \"max_result_window\": " + DEFAULT_MAX_RESULT_WINDOW + " } }"); + updateIndexSettings( + indexName, "{ \"index\": { \"max_result_window\": " + DEFAULT_MAX_RESULT_WINDOW + " } }"); } - /** - * Provide for each test to load test index, data and other setup work - */ - protected void init() throws Exception { - } + /** Provide for each test to load test index, data and other setup work */ + protected void init() throws Exception {} /** - * Make it thread-safe in case tests are running in parallel but does not guarantee - * if test like DeleteIT that mutates cluster running in parallel. + * Make it thread-safe in case tests are running in parallel but does not guarantee if test like + * DeleteIT that mutates cluster running in parallel. */ protected synchronized void loadIndex(Index index, RestClient client) throws IOException { String indexName = index.getName(); @@ -304,8 +306,9 @@ protected Request buildGetEndpointRequest(final String sqlQuery) { Assert.fail(utf8CharsetName + " not available"); } - final String requestUrl = String.format(Locale.ROOT, "%s?sql=%s&format=%s", QUERY_API_ENDPOINT, - urlEncodedQuery, "json"); + final String requestUrl = + String.format( + Locale.ROOT, "%s?sql=%s&format=%s", QUERY_API_ENDPOINT, urlEncodedQuery, "json"); return new Request("GET", requestUrl); } @@ -344,7 +347,8 @@ private String executeRequest(final String requestBody, final boolean isExplainQ return executeRequest(sqlRequest); } - protected static String executeRequest(final Request request, RestClient client) throws IOException { + protected static String executeRequest(final Request request, RestClient client) + throws IOException { Response response = client.performRequest(request); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); return getResponseBody(response); @@ -373,10 +377,12 @@ protected JSONObject executeCursorCloseQuery(final String cursor) throws IOExcep return new JSONObject(executeRequest(sqlRequest)); } - protected static JSONObject updateClusterSettings(ClusterSetting setting, RestClient client) throws IOException { + protected static JSONObject updateClusterSettings(ClusterSetting setting, RestClient client) + throws IOException { Request request = new Request("PUT", "/_cluster/settings"); - String persistentSetting = String.format(Locale.ROOT, - "{\"%s\": {\"%s\": %s}}", setting.type, setting.name, setting.value); + String persistentSetting = + String.format( + Locale.ROOT, "{\"%s\": {\"%s\": %s}}", setting.type, setting.name, setting.value); request.setJsonEntity(persistentSetting); RequestOptions.Builder restOptionsBuilder = RequestOptions.DEFAULT.toBuilder(); restOptionsBuilder.addHeader("Content-Type", "application/json"); @@ -413,11 +419,7 @@ ClusterSetting nullify() { @Override public String toString() { - return "ClusterSetting{" + - "type='" + type + '\'' + - ", path='" + name + '\'' + - ", value='" + value + '\'' + - '}'; + return String.format("ClusterSetting{type='%s', path='%s', value='%s'}", type, name, value); } } @@ -438,10 +440,8 @@ protected String makeRequest(String query) { } protected String makeRequest(String query, int fetch_size) { - return String.format("{\n" + - " \"fetch_size\": \"%s\",\n" + - " \"query\": \"%s\"\n" + - "}", fetch_size, query); + return String.format( + "{ \"fetch_size\": \"%s\", \"query\": \"%s\" }", fetch_size, query); } protected String makeFetchLessRequest(String query) { @@ -500,7 +500,6 @@ protected static Request getFetchDataSourceRequest(String name) { return request; } - protected static Request getDeleteDataSourceRequest(String name) { Request request = new Request("DELETE", "/_plugins/_query/_datasources" + "/" + name); RequestOptions.Builder restOptionsBuilder = RequestOptions.DEFAULT.toBuilder(); @@ -509,175 +508,196 @@ protected static Request getDeleteDataSourceRequest(String name) { return request; } - /** - * Enum for associating test index with relevant mapping and data. - */ + /** Enum for associating test index with relevant mapping and data. */ public enum Index { - ONLINE(TestsConstants.TEST_INDEX_ONLINE, - "online", - null, - "src/test/resources/online.json"), - ACCOUNT(TestsConstants.TEST_INDEX_ACCOUNT, + ONLINE(TestsConstants.TEST_INDEX_ONLINE, "online", null, "src/test/resources/online.json"), + ACCOUNT( + TestsConstants.TEST_INDEX_ACCOUNT, "account", getAccountIndexMapping(), "src/test/resources/accounts.json"), - PHRASE(TestsConstants.TEST_INDEX_PHRASE, + PHRASE( + TestsConstants.TEST_INDEX_PHRASE, "phrase", getPhraseIndexMapping(), "src/test/resources/phrases.json"), - DOG(TestsConstants.TEST_INDEX_DOG, - "dog", - getDogIndexMapping(), - "src/test/resources/dogs.json"), - DOGS2(TestsConstants.TEST_INDEX_DOG2, + DOG(TestsConstants.TEST_INDEX_DOG, "dog", getDogIndexMapping(), "src/test/resources/dogs.json"), + DOGS2( + TestsConstants.TEST_INDEX_DOG2, "dog", getDogs2IndexMapping(), "src/test/resources/dogs2.json"), - DOGS3(TestsConstants.TEST_INDEX_DOG3, + DOGS3( + TestsConstants.TEST_INDEX_DOG3, "dog", getDogs3IndexMapping(), "src/test/resources/dogs3.json"), - DOGSSUBQUERY(TestsConstants.TEST_INDEX_DOGSUBQUERY, + DOGSSUBQUERY( + TestsConstants.TEST_INDEX_DOGSUBQUERY, "dog", getDogIndexMapping(), "src/test/resources/dogsubquery.json"), - PEOPLE(TestsConstants.TEST_INDEX_PEOPLE, - "people", - null, - "src/test/resources/peoples.json"), - PEOPLE2(TestsConstants.TEST_INDEX_PEOPLE2, + PEOPLE(TestsConstants.TEST_INDEX_PEOPLE, "people", null, "src/test/resources/peoples.json"), + PEOPLE2( + TestsConstants.TEST_INDEX_PEOPLE2, "people", getPeople2IndexMapping(), "src/test/resources/people2.json"), - GAME_OF_THRONES(TestsConstants.TEST_INDEX_GAME_OF_THRONES, + GAME_OF_THRONES( + TestsConstants.TEST_INDEX_GAME_OF_THRONES, "gotCharacters", getGameOfThronesIndexMapping(), "src/test/resources/game_of_thrones_complex.json"), - SYSTEM(TestsConstants.TEST_INDEX_SYSTEM, - "systems", - null, - "src/test/resources/systems.json"), - ODBC(TestsConstants.TEST_INDEX_ODBC, + SYSTEM(TestsConstants.TEST_INDEX_SYSTEM, "systems", null, "src/test/resources/systems.json"), + ODBC( + TestsConstants.TEST_INDEX_ODBC, "odbc", getOdbcIndexMapping(), "src/test/resources/odbc-date-formats.json"), - LOCATION(TestsConstants.TEST_INDEX_LOCATION, + LOCATION( + TestsConstants.TEST_INDEX_LOCATION, "location", getLocationIndexMapping(), "src/test/resources/locations.json"), - LOCATION_TWO(TestsConstants.TEST_INDEX_LOCATION2, + LOCATION_TWO( + TestsConstants.TEST_INDEX_LOCATION2, "location2", getLocationIndexMapping(), "src/test/resources/locations2.json"), - NESTED(TestsConstants.TEST_INDEX_NESTED_TYPE, + NESTED( + TestsConstants.TEST_INDEX_NESTED_TYPE, "nestedType", getNestedTypeIndexMapping(), "src/test/resources/nested_objects.json"), - NESTED_WITHOUT_ARRAYS(TestsConstants.TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS, + NESTED_WITHOUT_ARRAYS( + TestsConstants.TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS, "nestedTypeWithoutArrays", getNestedTypeIndexMapping(), "src/test/resources/nested_objects_without_arrays.json"), - NESTED_WITH_QUOTES(TestsConstants.TEST_INDEX_NESTED_WITH_QUOTES, + NESTED_WITH_QUOTES( + TestsConstants.TEST_INDEX_NESTED_WITH_QUOTES, "nestedType", getNestedTypeIndexMapping(), "src/test/resources/nested_objects_quotes_in_values.json"), - EMPLOYEE_NESTED(TestsConstants.TEST_INDEX_EMPLOYEE_NESTED, + EMPLOYEE_NESTED( + TestsConstants.TEST_INDEX_EMPLOYEE_NESTED, "_doc", getEmployeeNestedTypeIndexMapping(), "src/test/resources/employee_nested.json"), - JOIN(TestsConstants.TEST_INDEX_JOIN_TYPE, + JOIN( + TestsConstants.TEST_INDEX_JOIN_TYPE, "joinType", getJoinTypeIndexMapping(), "src/test/resources/join_objects.json"), - UNEXPANDED_OBJECT(TestsConstants.TEST_INDEX_UNEXPANDED_OBJECT, + UNEXPANDED_OBJECT( + TestsConstants.TEST_INDEX_UNEXPANDED_OBJECT, "unexpandedObject", getUnexpandedObjectIndexMapping(), "src/test/resources/unexpanded_objects.json"), - BANK(TestsConstants.TEST_INDEX_BANK, + BANK( + TestsConstants.TEST_INDEX_BANK, "account", getBankIndexMapping(), "src/test/resources/bank.json"), - BANK_TWO(TestsConstants.TEST_INDEX_BANK_TWO, + BANK_TWO( + TestsConstants.TEST_INDEX_BANK_TWO, "account_two", getBankIndexMapping(), "src/test/resources/bank_two.json"), - BANK_WITH_NULL_VALUES(TestsConstants.TEST_INDEX_BANK_WITH_NULL_VALUES, + BANK_WITH_NULL_VALUES( + TestsConstants.TEST_INDEX_BANK_WITH_NULL_VALUES, "account_null", getBankWithNullValuesIndexMapping(), "src/test/resources/bank_with_null_values.json"), - BANK_WITH_STRING_VALUES(TestsConstants.TEST_INDEX_STRINGS, + BANK_WITH_STRING_VALUES( + TestsConstants.TEST_INDEX_STRINGS, "strings", getStringIndexMapping(), "src/test/resources/strings.json"), - BANK_CSV_SANITIZE(TestsConstants.TEST_INDEX_BANK_CSV_SANITIZE, + BANK_CSV_SANITIZE( + TestsConstants.TEST_INDEX_BANK_CSV_SANITIZE, "account", getBankIndexMapping(), "src/test/resources/bank_csv_sanitize.json"), - BANK_RAW_SANITIZE(TestsConstants.TEST_INDEX_BANK_RAW_SANITIZE, - "account", - getBankIndexMapping(), - "src/test/resources/bank_raw_sanitize.json"), - ORDER(TestsConstants.TEST_INDEX_ORDER, + BANK_RAW_SANITIZE( + TestsConstants.TEST_INDEX_BANK_RAW_SANITIZE, + "account", + getBankIndexMapping(), + "src/test/resources/bank_raw_sanitize.json"), + ORDER( + TestsConstants.TEST_INDEX_ORDER, "_doc", getOrderIndexMapping(), "src/test/resources/order.json"), - WEBLOG(TestsConstants.TEST_INDEX_WEBLOG, + WEBLOG( + TestsConstants.TEST_INDEX_WEBLOG, "weblog", getWeblogsIndexMapping(), "src/test/resources/weblogs.json"), - DATE(TestsConstants.TEST_INDEX_DATE, + DATE( + TestsConstants.TEST_INDEX_DATE, "dates", getDateIndexMapping(), "src/test/resources/dates.json"), - DATETIME(TestsConstants.TEST_INDEX_DATE_TIME, + DATETIME( + TestsConstants.TEST_INDEX_DATE_TIME, "_doc", getDateTimeIndexMapping(), "src/test/resources/datetime.json"), - NESTED_SIMPLE(TestsConstants.TEST_INDEX_NESTED_SIMPLE, + NESTED_SIMPLE( + TestsConstants.TEST_INDEX_NESTED_SIMPLE, "_doc", getNestedSimpleIndexMapping(), "src/test/resources/nested_simple.json"), - DEEP_NESTED(TestsConstants.TEST_INDEX_DEEP_NESTED, + DEEP_NESTED( + TestsConstants.TEST_INDEX_DEEP_NESTED, "_doc", getDeepNestedIndexMapping(), "src/test/resources/deep_nested_index_data.json"), - DATA_TYPE_NUMERIC(TestsConstants.TEST_INDEX_DATATYPE_NUMERIC, + DATA_TYPE_NUMERIC( + TestsConstants.TEST_INDEX_DATATYPE_NUMERIC, "_doc", getDataTypeNumericIndexMapping(), "src/test/resources/datatypes_numeric.json"), - DATA_TYPE_NONNUMERIC(TestsConstants.TEST_INDEX_DATATYPE_NONNUMERIC, + DATA_TYPE_NONNUMERIC( + TestsConstants.TEST_INDEX_DATATYPE_NONNUMERIC, "_doc", getDataTypeNonnumericIndexMapping(), "src/test/resources/datatypes.json"), - BEER(TestsConstants.TEST_INDEX_BEER, - "beer", - null, - "src/test/resources/beer.stackexchange.json"), - NULL_MISSING(TestsConstants.TEST_INDEX_NULL_MISSING, + BEER( + TestsConstants.TEST_INDEX_BEER, "beer", null, "src/test/resources/beer.stackexchange.json"), + NULL_MISSING( + TestsConstants.TEST_INDEX_NULL_MISSING, "null_missing", getMappingFile("null_missing_index_mapping.json"), "src/test/resources/null_missing.json"), - CALCS(TestsConstants.TEST_INDEX_CALCS, + CALCS( + TestsConstants.TEST_INDEX_CALCS, "calcs", getMappingFile("calcs_index_mappings.json"), "src/test/resources/calcs.json"), - DATE_FORMATS(TestsConstants.TEST_INDEX_DATE_FORMATS, + DATE_FORMATS( + TestsConstants.TEST_INDEX_DATE_FORMATS, "date_formats", getMappingFile("date_formats_index_mapping.json"), "src/test/resources/date_formats.json"), - WILDCARD(TestsConstants.TEST_INDEX_WILDCARD, + WILDCARD( + TestsConstants.TEST_INDEX_WILDCARD, "wildcard", getMappingFile("wildcard_index_mappings.json"), "src/test/resources/wildcard.json"), - DATASOURCES(TestsConstants.DATASOURCES, + DATASOURCES( + TestsConstants.DATASOURCES, "datasource", getMappingFile("datasources_index_mappings.json"), "src/test/resources/datasources.json"), - MULTI_NESTED(TestsConstants.TEST_INDEX_MULTI_NESTED_TYPE, + MULTI_NESTED( + TestsConstants.TEST_INDEX_MULTI_NESTED_TYPE, "multi_nested", getMappingFile("multi_nested.json"), "src/test/resources/multi_nested_objects.json"), - NESTED_WITH_NULLS(TestsConstants.TEST_INDEX_NESTED_WITH_NULLS, + NESTED_WITH_NULLS( + TestsConstants.TEST_INDEX_NESTED_WITH_NULLS, "multi_nested", getNestedTypeIndexMapping(), "src/test/resources/nested_with_nulls.json"); @@ -709,7 +729,5 @@ public String getMapping() { public String getDataSet() { return this.dataSet; } - - } } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/ShowIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/ShowIT.java index b28336c482..fa86bbbc22 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/ShowIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/ShowIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.hamcrest.Matchers.equalTo; @@ -20,7 +19,8 @@ public class ShowIT extends SQLIntegTestCase { @Override protected void init() { - // Note: not using the existing TEST_INDEX_* indices, since underscore in the names causes issues + // Note: not using the existing TEST_INDEX_* indices, since underscore in the names causes + // issues createEmptyIndexIfNotExist("abcdefg"); createEmptyIndexIfNotExist("abcdefghijk"); createEmptyIndexIfNotExist("abcdijk"); diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/SourceFieldIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/SourceFieldIT.java index a6a1a1cfe9..bf288262b6 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/SourceFieldIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/SourceFieldIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.legacy; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT; @@ -31,32 +30,44 @@ protected void init() throws Exception { @Test public void includeTest() throws IOException { - SearchHits response = query(String.format( - "SELECT include('*name','*ge'),include('b*'),include('*ddre*'),include('gender') FROM %s LIMIT 1000", - TEST_INDEX_ACCOUNT)); + SearchHits response = + query( + String.format( + "SELECT include('*name','*ge'),include('b*'),include('*ddre*'),include('gender')" + + " FROM %s LIMIT 1000", + TEST_INDEX_ACCOUNT)); for (SearchHit hit : response.getHits()) { Set keySet = hit.getSourceAsMap().keySet(); for (String field : keySet) { - Assert.assertTrue(field.endsWith("name") || field.endsWith("ge") || field.startsWith("b") || - field.contains("ddre") || field.equals("gender")); + Assert.assertTrue( + field.endsWith("name") + || field.endsWith("ge") + || field.startsWith("b") + || field.contains("ddre") + || field.equals("gender")); } } - } @Test public void excludeTest() throws IOException { - SearchHits response = query(String.format( - "SELECT exclude('*name','*ge'),exclude('b*'),exclude('*ddre*'),exclude('gender') FROM %s LIMIT 1000", - TEST_INDEX_ACCOUNT)); + SearchHits response = + query( + String.format( + "SELECT exclude('*name','*ge'),exclude('b*'),exclude('*ddre*'),exclude('gender')" + + " FROM %s LIMIT 1000", + TEST_INDEX_ACCOUNT)); for (SearchHit hit : response.getHits()) { Set keySet = hit.getSourceAsMap().keySet(); for (String field : keySet) { Assert.assertFalse( - field.endsWith("name") || field.endsWith("ge") || field.startsWith("b") || - field.contains("ddre") || field.equals("gender")); + field.endsWith("name") + || field.endsWith("ge") + || field.startsWith("b") + || field.contains("ddre") + || field.equals("gender")); } } } @@ -64,15 +75,18 @@ public void excludeTest() throws IOException { @Test public void allTest() throws IOException { - SearchHits response = query(String.format( - "SELECT exclude('*name','*ge'),include('b*'),exclude('*ddre*'),include('gender') FROM %s LIMIT 1000", - TEST_INDEX_ACCOUNT)); + SearchHits response = + query( + String.format( + "SELECT exclude('*name','*ge'),include('b*'),exclude('*ddre*'),include('gender')" + + " FROM %s LIMIT 1000", + TEST_INDEX_ACCOUNT)); for (SearchHit hit : response.getHits()) { Set keySet = hit.getSourceAsMap().keySet(); for (String field : keySet) { - Assert - .assertFalse(field.endsWith("name") || field.endsWith("ge") || field.contains("ddre")); + Assert.assertFalse( + field.endsWith("name") || field.endsWith("ge") || field.contains("ddre")); Assert.assertTrue(field.startsWith("b") || field.equals("gender")); } } @@ -81,11 +95,11 @@ public void allTest() throws IOException { private SearchHits query(String query) throws IOException { final JSONObject jsonObject = executeQuery(query); - final XContentParser parser = new JsonXContentParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - new JsonFactory().createParser(jsonObject.toString())); + final XContentParser parser = + new JsonXContentParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + new JsonFactory().createParser(jsonObject.toString())); return SearchResponse.fromXContent(parser).getHits(); } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/NowLikeFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/NowLikeFunctionIT.java index a330614d21..2d94dc6a3b 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/NowLikeFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/NowLikeFunctionIT.java @@ -72,8 +72,7 @@ public NowLikeFunctionIT( @Name("constValue") Boolean constValue, @Name("referenceGetter") Supplier referenceGetter, @Name("parser") BiFunction parser, - @Name("serializationPatternStr") String serializationPatternStr - ) { + @Name("serializationPatternStr") String serializationPatternStr) { this.name = name; this.hasFsp = hasFsp; this.hasShortcut = hasShortcut; @@ -85,56 +84,104 @@ public NowLikeFunctionIT( @ParametersFactory(argumentFormatting = "%1$s") public static Iterable compareTwoDates() { - return Arrays.asList($$( - $("now", false, false, true, - (Supplier) LocalDateTime::now, - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss"), - $("current_timestamp", false, false, true, - (Supplier) LocalDateTime::now, - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss"), - $("localtimestamp", false, false, true, - (Supplier) LocalDateTime::now, - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss"), - $("localtime", false, false, true, - (Supplier) LocalDateTime::now, - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss"), - $("sysdate", true, false, false, - (Supplier) LocalDateTime::now, - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss"), - $("curtime", false, false, false, - (Supplier) LocalTime::now, - (BiFunction) LocalTime::parse, - "HH:mm:ss"), - $("current_time", false, false, false, - (Supplier) LocalTime::now, - (BiFunction) LocalTime::parse, - "HH:mm:ss"), - $("curdate", false, false, false, - (Supplier) LocalDate::now, - (BiFunction) LocalDate::parse, - "uuuu-MM-dd"), - $("current_date", false, false, false, - (Supplier) LocalDate::now, - (BiFunction) LocalDate::parse, - "uuuu-MM-dd"), - $("utc_date", false, false, true, - (Supplier) (() -> utcDateTimeNow().toLocalDate()), - (BiFunction) LocalDate::parse, - "uuuu-MM-dd"), - $("utc_time", false, false, true, - (Supplier) (() -> utcDateTimeNow().toLocalTime()), - (BiFunction) LocalTime::parse, - "HH:mm:ss"), - $("utc_timestamp", false, false, true, - (Supplier) (org.opensearch.sql.sql.NowLikeFunctionIT::utcDateTimeNow), - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss") - )); + return Arrays.asList( + $$( + $( + "now", + false, + false, + true, + (Supplier) LocalDateTime::now, + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"), + $( + "current_timestamp", + false, + false, + true, + (Supplier) LocalDateTime::now, + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"), + $( + "localtimestamp", + false, + false, + true, + (Supplier) LocalDateTime::now, + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"), + $( + "localtime", + false, + false, + true, + (Supplier) LocalDateTime::now, + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"), + $( + "sysdate", + true, + false, + false, + (Supplier) LocalDateTime::now, + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"), + $( + "curtime", + false, + false, + false, + (Supplier) LocalTime::now, + (BiFunction) LocalTime::parse, + "HH:mm:ss"), + $( + "current_time", + false, + false, + false, + (Supplier) LocalTime::now, + (BiFunction) LocalTime::parse, + "HH:mm:ss"), + $( + "curdate", + false, + false, + false, + (Supplier) LocalDate::now, + (BiFunction) LocalDate::parse, + "uuuu-MM-dd"), + $( + "current_date", + false, + false, + false, + (Supplier) LocalDate::now, + (BiFunction) LocalDate::parse, + "uuuu-MM-dd"), + $( + "utc_date", + false, + false, + true, + (Supplier) (() -> utcDateTimeNow().toLocalDate()), + (BiFunction) LocalDate::parse, + "uuuu-MM-dd"), + $( + "utc_time", + false, + false, + true, + (Supplier) (() -> utcDateTimeNow().toLocalTime()), + (BiFunction) LocalTime::parse, + "HH:mm:ss"), + $( + "utc_timestamp", + false, + false, + true, + (Supplier) (org.opensearch.sql.sql.NowLikeFunctionIT::utcDateTimeNow), + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"))); } private long getDiff(Temporal sample, Temporal reference) { @@ -146,7 +193,8 @@ private long getDiff(Temporal sample, Temporal reference) { @Test public void testNowLikeFunctions() throws IOException { - var serializationPattern = new DateTimeFormatterBuilder() + var serializationPattern = + new DateTimeFormatterBuilder() .appendPattern(serializationPatternStr) .optionalStart() .appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true) @@ -156,42 +204,57 @@ public void testNowLikeFunctions() throws IOException { double delta = 2d; // acceptable time diff, secs if (reference instanceof LocalDate) delta = 1d; // Max date delta could be 1 if test runs on the very edge of two days - // We ignore probability of a test run on edge of month or year to simplify the checks + // We ignore probability of a test run on edge of month or year to simplify the checks - var calls = new ArrayList() {{ - add(name + "()"); - }}; - if (hasShortcut) - calls.add(name); - if (hasFsp) - calls.add(name + "(0)"); + var calls = + new ArrayList() { + { + add(name + "()"); + } + }; + if (hasShortcut) calls.add(name); + if (hasFsp) calls.add(name + "(0)"); // Column order is: func(), func, func(0) // shortcut ^ fsp ^ // Query looks like: // source=people2 | eval `now()`=now() | fields `now()`; - JSONObject result = executeQuery("source=" + TEST_INDEX_PEOPLE2 - + " | eval " + calls.stream().map(c -> String.format("`%s`=%s", c, c)).collect(Collectors.joining(",")) - + " | fields " + calls.stream().map(c -> String.format("`%s`", c)).collect(Collectors.joining(","))); + JSONObject result = + executeQuery( + "source=" + + TEST_INDEX_PEOPLE2 + + " | eval " + + calls.stream() + .map(c -> String.format("`%s`=%s", c, c)) + .collect(Collectors.joining(",")) + + " | fields " + + calls.stream() + .map(c -> String.format("`%s`", c)) + .collect(Collectors.joining(","))); var rows = result.getJSONArray("datarows"); JSONArray firstRow = rows.getJSONArray(0); for (int i = 0; i < rows.length(); i++) { var row = rows.getJSONArray(i); - if (constValue) - assertTrue(firstRow.similar(row)); + if (constValue) assertTrue(firstRow.similar(row)); int column = 0; - assertEquals(0, - getDiff(reference, parser.apply(row.getString(column++), serializationPattern)), delta); + assertEquals( + 0, + getDiff(reference, parser.apply(row.getString(column++), serializationPattern)), + delta); if (hasShortcut) { - assertEquals(0, - getDiff(reference, parser.apply(row.getString(column++), serializationPattern)), delta); + assertEquals( + 0, + getDiff(reference, parser.apply(row.getString(column++), serializationPattern)), + delta); } if (hasFsp) { - assertEquals(0, - getDiff(reference, parser.apply(row.getString(column), serializationPattern)), delta); + assertEquals( + 0, + getDiff(reference, parser.apply(row.getString(column), serializationPattern)), + delta); } } } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/ObjectFieldOperateIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/ObjectFieldOperateIT.java index 6178552728..501d4bcb5e 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/ObjectFieldOperateIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/ObjectFieldOperateIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.SQLIntegTestCase.Index.DEEP_NESTED; @@ -26,55 +25,45 @@ public void init() throws IOException { @Test public void select_object_field() throws IOException { - JSONObject result = executeQuery( - String.format("source=%s | " - + "fields city.name, city.location.latitude", - TEST_INDEX_DEEP_NESTED)); - verifySchema(result, - schema("city.name", "string"), - schema("city.location.latitude", "double")); - verifyDataRows(result, - rows("Seattle", 10.5)); + JSONObject result = + executeQuery( + String.format( + "source=%s | fields city.name, city.location.latitude", + TEST_INDEX_DEEP_NESTED)); + verifySchema(result, schema("city.name", "string"), schema("city.location.latitude", "double")); + verifyDataRows(result, rows("Seattle", 10.5)); } @Test public void compare_object_field_in_where() throws IOException { - JSONObject result = executeQuery( - String.format("source=%s " - + "| where city.name = 'Seattle' " - + "| fields city.name, city.location.latitude", - TEST_INDEX_DEEP_NESTED)); - verifySchema(result, - schema("city.name", "string"), - schema("city.location.latitude", "double")); - verifyDataRows(result, - rows("Seattle", 10.5)); + JSONObject result = + executeQuery( + String.format( + "source=%s " + + "| where city.name = 'Seattle' " + + "| fields city.name, city.location.latitude", + TEST_INDEX_DEEP_NESTED)); + verifySchema(result, schema("city.name", "string"), schema("city.location.latitude", "double")); + verifyDataRows(result, rows("Seattle", 10.5)); } @Test public void group_object_field_in_stats() throws IOException { - JSONObject result = executeQuery( - String.format("source=%s " - + "| stats count() by city.name", - TEST_INDEX_DEEP_NESTED)); - verifySchema(result, - schema("count()", "integer"), - schema("city.name", "string")); - verifyDataRows(result, - rows(1, "Seattle")); + JSONObject result = + executeQuery( + String.format("source=%s | stats count() by city.name", TEST_INDEX_DEEP_NESTED)); + verifySchema(result, schema("count()", "integer"), schema("city.name", "string")); + verifyDataRows(result, rows(1, "Seattle")); } @Test public void sort_by_object_field() throws IOException { - JSONObject result = executeQuery( - String.format("source=%s " - + "| sort city.name" - + "| fields city.name, city.location.latitude", - TEST_INDEX_DEEP_NESTED)); - verifySchema(result, - schema("city.name", "string"), - schema("city.location.latitude", "double")); - verifyDataRows(result, - rows("Seattle", 10.5)); + JSONObject result = + executeQuery( + String.format( + "source=%s | sort city.name | fields city.name, city.location.latitude", + TEST_INDEX_DEEP_NESTED)); + verifySchema(result, schema("city.name", "string"), schema("city.location.latitude", "double")); + verifyDataRows(result, rows("Seattle", 10.5)); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/OperatorIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/OperatorIT.java index e6ca958991..42ed08b00c 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/OperatorIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/OperatorIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; @@ -26,40 +25,28 @@ public void init() throws IOException { @Test public void testAddOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s | where age = 31 + 1 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s | where age = 31 + 1 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(32)); } @Test public void testSubtractOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s | where age = 33 - 1 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s | where age = 33 - 1 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(32)); } @Test public void testMultiplyOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s | where age = 16 * 2 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s | where age = 16 * 2 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(32)); } @Test public void testDivideOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s | where age / 2 = 16 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s | where age / 2 = 16 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(32), rows(33)); } @@ -67,9 +54,7 @@ public void testDivideOperator() throws IOException { public void testModuleOperator() throws IOException { JSONObject result = executeQuery( - String.format( - "source=%s | where age %s 32 = 0 | fields age", - TEST_INDEX_BANK, "%")); + String.format("source=%s | where age %s 32 = 0 | fields age", TEST_INDEX_BANK, "%")); verifyDataRows(result, rows(32)); } @@ -78,11 +63,9 @@ public void testArithmeticOperatorWithNullValue() throws IOException { JSONObject result = executeQuery( String.format( - "source=%s | eval f = age + 0 | fields f", - TEST_INDEX_BANK_WITH_NULL_VALUES)); + "source=%s | eval f = age + 0 | fields f", TEST_INDEX_BANK_WITH_NULL_VALUES)); verifyDataRows( - result, rows(32), rows(36), rows(28), rows(33), rows(36), rows(JSONObject.NULL), - rows(34)); + result, rows(32), rows(36), rows(28), rows(33), rows(36), rows(JSONObject.NULL), rows(34)); } @Test @@ -92,8 +75,14 @@ public void testArithmeticOperatorWithMissingValue() throws IOException { String.format( "source=%s | eval f = balance * 1 | fields f", TEST_INDEX_BANK_WITH_NULL_VALUES)); verifyDataRows( - result, rows(39225), rows(32838), rows(4180), rows(48086), rows(JSONObject.NULL), - rows(JSONObject.NULL), rows(JSONObject.NULL)); + result, + rows(39225), + rows(32838), + rows(4180), + rows(48086), + rows(JSONObject.NULL), + rows(JSONObject.NULL), + rows(JSONObject.NULL)); } @Test @@ -101,8 +90,7 @@ public void testMultipleArithmeticOperators() throws IOException { JSONObject result = executeQuery( String.format( - "source=%s | where (age+2) * 3 / 2 - 1 = 50 | fields age", - TEST_INDEX_BANK)); + "source=%s | where (age+2) * 3 / 2 - 1 = 50 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(32)); } @@ -127,14 +115,12 @@ public void testAndOperator() throws IOException { public void testOrOperator() throws IOException { JSONObject result = executeQuery( - String.format( - "source=%s | where age=32 or age=34 | fields age", TEST_INDEX_BANK)); + String.format("source=%s | where age=32 or age=34 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(32), rows(34)); result = executeQuery( - String.format( - "source=%s | where age=34 or age=32| fields age", TEST_INDEX_BANK)); + String.format("source=%s | where age=34 or age=32| fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(32), rows(34)); } @@ -158,92 +144,64 @@ public void testXorOperator() throws IOException { @Test public void testNotOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s not age > 32 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s not age > 32 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(28), rows(32)); } @Test public void testEqualOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s age = 32 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s age = 32 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(32)); - result = - executeQuery( - String.format( - "source=%s 32 = age | fields age", - TEST_INDEX_BANK)); + result = executeQuery(String.format("source=%s 32 = age | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(32)); } @Test public void testNotEqualOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s age != 32 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s age != 32 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(28), rows(33), rows(34), rows(36), rows(36), rows(39)); - result = - executeQuery( - String.format( - "source=%s 32 != age | fields age", - TEST_INDEX_BANK)); + result = executeQuery(String.format("source=%s 32 != age | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(28), rows(33), rows(34), rows(36), rows(36), rows(39)); } @Test public void testLessOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s age < 32 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s age < 32 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(28)); } @Test public void testLteOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s age <= 32 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s age <= 32 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(28), rows(32)); } @Test public void testGreaterOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s age > 36 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s age > 36 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(39)); } @Test public void testGteOperator() throws IOException { JSONObject result = - executeQuery( - String.format( - "source=%s age >= 36 | fields age", - TEST_INDEX_BANK)); + executeQuery(String.format("source=%s age >= 36 | fields age", TEST_INDEX_BANK)); verifyDataRows(result, rows(36), rows(36), rows(39)); } @Test public void testLikeFunction() throws IOException { JSONObject result = - executeQuery(String.format("source=%s like(firstname, 'Hatti_') | fields firstname", - TEST_INDEX_BANK)); + executeQuery( + String.format( + "source=%s like(firstname, 'Hatti_') | fields firstname", TEST_INDEX_BANK)); verifyDataRows(result, rows("Hattie")); } @@ -251,8 +209,8 @@ public void testLikeFunction() throws IOException { public void testBinaryPredicateWithNullValue() throws IOException { JSONObject result = executeQuery( - String.format("source=%s | where age >= 36 | fields age", - TEST_INDEX_BANK_WITH_NULL_VALUES)); + String.format( + "source=%s | where age >= 36 | fields age", TEST_INDEX_BANK_WITH_NULL_VALUES)); verifyDataRows(result, rows(36), rows(36)); } @@ -260,7 +218,8 @@ public void testBinaryPredicateWithNullValue() throws IOException { public void testBinaryPredicateWithMissingValue() throws IOException { JSONObject result = executeQuery( - String.format("source=%s | where balance > 40000 | fields balance", + String.format( + "source=%s | where balance > 40000 | fields balance", TEST_INDEX_BANK_WITH_NULL_VALUES)); verifyDataRows(result, rows(48086)); } @@ -269,12 +228,13 @@ private void queryExecutionShouldThrowExceptionDueToNullOrMissingValue( String query, String... errorMsgs) { try { executeQuery(query); - fail("Expected to throw ExpressionEvaluationException, but none was thrown for query: " - + query); + fail( + "Expected to throw ExpressionEvaluationException, but none was thrown for query: " + + query); } catch (ResponseException e) { String errorMsg = e.getMessage(); assertTrue(errorMsg.contains("ExpressionEvaluationException")); - for (String msg: errorMsgs) { + for (String msg : errorMsgs) { assertTrue(errorMsg.contains(msg)); } } catch (IOException e) { diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/PPLIntegTestCase.java b/integ-test/src/test/java/org/opensearch/sql/ppl/PPLIntegTestCase.java index bcf183e9c6..459788021d 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/PPLIntegTestCase.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/PPLIntegTestCase.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestUtils.getResponseBody; @@ -20,9 +19,7 @@ import org.opensearch.client.Response; import org.opensearch.sql.legacy.SQLIntegTestCase; -/** - * OpenSearch Rest integration test base for PPL testing. - */ +/** OpenSearch Rest integration test base for PPL testing. */ public abstract class PPLIntegTestCase extends SQLIntegTestCase { protected JSONObject executeQuery(String query) throws IOException { @@ -42,8 +39,10 @@ protected String explainQueryToString(String query) throws IOException { } protected String executeCsvQuery(String query, boolean sanitize) throws IOException { - Request request = buildRequest(query, - QUERY_API_ENDPOINT + String.format(Locale.ROOT, "?format=csv&sanitize=%b", sanitize)); + Request request = + buildRequest( + query, + QUERY_API_ENDPOINT + String.format(Locale.ROOT, "?format=csv&sanitize=%b", sanitize)); Response response = client().performRequest(request); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); return getResponseBody(response, true); @@ -65,8 +64,9 @@ protected Request buildRequest(String query, String endpoint) { protected static JSONObject updateClusterSettings(ClusterSetting setting) throws IOException { Request request = new Request("PUT", "/_cluster/settings"); - String persistentSetting = String.format(Locale.ROOT, - "{\"%s\": {\"%s\": %s}}", setting.type, setting.name, setting.value); + String persistentSetting = + String.format( + Locale.ROOT, "{\"%s\": {\"%s\": %s}}", setting.type, setting.name, setting.value); request.setJsonEntity(persistentSetting); RequestOptions.Builder restOptionsBuilder = RequestOptions.DEFAULT.toBuilder(); restOptionsBuilder.addHeader("Content-Type", "application/json"); @@ -91,11 +91,7 @@ SQLIntegTestCase.ClusterSetting nullify() { @Override public String toString() { - return "ClusterSetting{" - + "type='" + type + '\'' - + ", path='" + name + '\'' - + ", value='" + value + '\'' - + '}'; + return String.format("ClusterSetting{type='%s', path='%s', value'%s'}", type, name, value); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/PPLPluginIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/PPLPluginIT.java index df7b464118..0c638be1e7 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/PPLPluginIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/PPLPluginIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.hamcrest.Matchers.equalTo; @@ -28,8 +27,7 @@ import org.opensearch.sql.util.TestUtils; public class PPLPluginIT extends PPLIntegTestCase { - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); + @Rule public ExpectedException exceptionRule = ExpectedException.none(); private static final String PERSISTENT = "persistent"; @@ -86,9 +84,11 @@ public void sqlEnableSettingsTest() throws IOException { assertThat(result.getInt("status"), equalTo(400)); JSONObject error = result.getJSONObject("error"); assertThat(error.getString("reason"), equalTo("Invalid Query")); - assertThat(error.getString("details"), equalTo( - "Either plugins.ppl.enabled or rest.action.multi.allow_explicit_index setting is " - + "false")); + assertThat( + error.getString("details"), + equalTo( + "Either plugins.ppl.enabled or rest.action.multi.allow_explicit_index setting is " + + "false")); assertThat(error.getString("type"), equalTo("IllegalAccessException")); // reset the setting diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/ParseCommandIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/ParseCommandIT.java index 36fcb4bf3b..7f25f6f160 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/ParseCommandIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/ParseCommandIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; @@ -23,9 +22,10 @@ public void init() throws IOException { @Test public void testParseCommand() throws IOException { - JSONObject result = executeQuery( - String.format("source=%s | parse email '.+@(?.+)' | fields email, host", - TEST_INDEX_BANK)); + JSONObject result = + executeQuery( + String.format( + "source=%s | parse email '.+@(?.+)' | fields email, host", TEST_INDEX_BANK)); verifyOrder( result, rows("amberduke@pyrami.com", "pyrami.com"), @@ -39,8 +39,10 @@ public void testParseCommand() throws IOException { @Test public void testParseCommandReplaceOriginalField() throws IOException { - JSONObject result = executeQuery( - String.format("source=%s | parse email '.+@(?.+)' | fields email", TEST_INDEX_BANK)); + JSONObject result = + executeQuery( + String.format( + "source=%s | parse email '.+@(?.+)' | fields email", TEST_INDEX_BANK)); verifyOrder( result, rows("pyrami.com"), @@ -54,8 +56,12 @@ public void testParseCommandReplaceOriginalField() throws IOException { @Test public void testParseCommandWithOtherRunTimeFields() throws IOException { - JSONObject result = executeQuery(String.format("source=%s | parse email '.+@(?.+)' | " - + "eval eval_result=1 | fields host, eval_result", TEST_INDEX_BANK)); + JSONObject result = + executeQuery( + String.format( + "source=%s | parse email '.+@(?.+)' | " + + "eval eval_result=1 | fields host, eval_result", + TEST_INDEX_BANK)); verifyOrder( result, rows("pyrami.com", 1), diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/PositionFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/PositionFunctionIT.java index 59aade8bbd..a7f638b3dd 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/PositionFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/PositionFunctionIT.java @@ -13,87 +13,83 @@ import org.junit.Test; public class PositionFunctionIT extends PPLIntegTestCase { - @Override - public void init() throws IOException { - loadIndex(Index.CALCS); - } - - @Test - public void test_position_function() throws IOException { - String query = "source=" + TEST_INDEX_CALCS - + " | eval f=position('ON', str1) | fields f"; - - var result = executeQuery(query); - - assertEquals(17, result.getInt("total")); - verifyDataRows(result, - rows(7), rows(7), - rows(2), rows(0), - rows(0), rows(0), - rows(0), rows(0), - rows(0), rows(0), - rows(0), rows(0), - rows(0), rows(0), - rows(0), rows(0), - rows(0)); - } - - @Test - public void test_position_function_with_fields_only() throws IOException { - String query = "source=" + TEST_INDEX_CALCS - + " | eval f=position(str3 IN str2) | where str2 IN ('one', 'two', 'three')| fields f"; - - var result = executeQuery(query); - - assertEquals(3, result.getInt("total")); - verifyDataRows(result, rows(3), rows(0), rows(4)); - } - - @Test - public void test_position_function_with_string_literals() throws IOException { - String query = "source=" + TEST_INDEX_CALCS - + " | eval f=position('world' IN 'hello world') | where str2='one' | fields f"; - - var result = executeQuery(query); - - assertEquals(1, result.getInt("total")); - verifyDataRows(result, rows(7)); - } - - @Test - public void test_position_function_with_nulls() throws IOException { - String query = "source=" + TEST_INDEX_CALCS - + " | eval f=position('ee' IN str2) | where isnull(str2) | fields str2,f"; - - var result = executeQuery(query); - - assertEquals(4, result.getInt("total")); - verifyDataRows(result, - rows(null, null), - rows(null, null), - rows(null, null), - rows(null, null)); - } - - @Test - public void test_position_function_with_function_as_arg() throws IOException { - String query = "source=" + TEST_INDEX_CALCS - + " | eval f=position(upper(str3) IN str1) | where like(str1, 'BINDING SUPPLIES') | fields f"; - - var result = executeQuery(query); - - assertEquals(1, result.getInt("total")); - verifyDataRows(result, rows(15)); - } - - @Test - public void test_position_function_with_function_in_where_clause() throws IOException { - String query = "source=" + TEST_INDEX_CALCS - + " | where position(str3 IN str2)=1 | fields str2"; - - var result = executeQuery(query); - - assertEquals(2, result.getInt("total")); - verifyDataRows(result, rows("eight"), rows("eleven")); - } + @Override + public void init() throws IOException { + loadIndex(Index.CALCS); + } + + @Test + public void test_position_function() throws IOException { + String query = "source=" + TEST_INDEX_CALCS + " | eval f=position('ON', str1) | fields f"; + + var result = executeQuery(query); + + assertEquals(17, result.getInt("total")); + verifyDataRows( + result, rows(7), rows(7), rows(2), rows(0), rows(0), rows(0), rows(0), rows(0), rows(0), + rows(0), rows(0), rows(0), rows(0), rows(0), rows(0), rows(0), rows(0)); + } + + @Test + public void test_position_function_with_fields_only() throws IOException { + String query = + "source=" + + TEST_INDEX_CALCS + + " | eval f=position(str3 IN str2) | where str2 IN ('one', 'two', 'three')| fields f"; + + var result = executeQuery(query); + + assertEquals(3, result.getInt("total")); + verifyDataRows(result, rows(3), rows(0), rows(4)); + } + + @Test + public void test_position_function_with_string_literals() throws IOException { + String query = + "source=" + + TEST_INDEX_CALCS + + " | eval f=position('world' IN 'hello world') | where str2='one' | fields f"; + + var result = executeQuery(query); + + assertEquals(1, result.getInt("total")); + verifyDataRows(result, rows(7)); + } + + @Test + public void test_position_function_with_nulls() throws IOException { + String query = + "source=" + + TEST_INDEX_CALCS + + " | eval f=position('ee' IN str2) | where isnull(str2) | fields str2,f"; + + var result = executeQuery(query); + + assertEquals(4, result.getInt("total")); + verifyDataRows(result, rows(null, null), rows(null, null), rows(null, null), rows(null, null)); + } + + @Test + public void test_position_function_with_function_as_arg() throws IOException { + String query = + "source=" + + TEST_INDEX_CALCS + + " | eval f=position(upper(str3) IN str1) | where like(str1, 'BINDING SUPPLIES') |" + + " fields f"; + + var result = executeQuery(query); + + assertEquals(1, result.getInt("total")); + verifyDataRows(result, rows(15)); + } + + @Test + public void test_position_function_with_function_in_where_clause() throws IOException { + String query = "source=" + TEST_INDEX_CALCS + " | where position(str3 IN str2)=1 | fields str2"; + + var result = executeQuery(query); + + assertEquals(2, result.getInt("total")); + verifyDataRows(result, rows("eight"), rows("eleven")); + } } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/PrometheusDataSourceCommandsIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/PrometheusDataSourceCommandsIT.java index 011f91eed5..8d72f02e29 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/PrometheusDataSourceCommandsIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/PrometheusDataSourceCommandsIT.java @@ -40,10 +40,10 @@ public class PrometheusDataSourceCommandsIT extends PPLIntegTestCase { /** - * Integ tests are dependent on self generated metrics in prometheus instance. - * When running individual integ tests there - * is no time for generation of metrics in the test prometheus instance. - * This method gives prometheus time to generate metrics on itself. + * Integ tests are dependent on self generated metrics in prometheus instance. When running + * individual integ tests there is no time for generation of metrics in the test prometheus + * instance. This method gives prometheus time to generate metrics on itself. + * * @throws InterruptedException */ @BeforeClass @@ -54,8 +54,11 @@ protected static void metricGenerationWait() throws InterruptedException { @Override protected void init() throws InterruptedException, IOException { DataSourceMetadata createDSM = - new DataSourceMetadata("my_prometheus", DataSourceType.PROMETHEUS, - ImmutableList.of(), ImmutableMap.of("prometheus.uri", "http://localhost:9090")); + new DataSourceMetadata( + "my_prometheus", + DataSourceType.PROMETHEUS, + ImmutableList.of(), + ImmutableMap.of("prometheus.uri", "http://localhost:9090")); Request createRequest = getCreateDataSourceRequest(createDSM); Response response = client().performRequest(createRequest); Assert.assertEquals(201, response.getStatusLine().getStatusCode()); @@ -71,15 +74,15 @@ protected void deleteDataSourceMetadata() throws IOException { @Test @SneakyThrows public void testSourceMetricCommand() { - JSONObject response = - executeQuery("source=my_prometheus.prometheus_http_requests_total"); - verifySchema(response, + JSONObject response = executeQuery("source=my_prometheus.prometheus_http_requests_total"); + verifySchema( + response, schema(VALUE, "double"), - schema(TIMESTAMP, "timestamp"), - schema("handler", "string"), - schema("code", "string"), - schema("instance", "string"), - schema("job", "string")); + schema(TIMESTAMP, "timestamp"), + schema("handler", "string"), + schema("code", "string"), + schema("instance", "string"), + schema("job", "string")); Assertions.assertTrue(response.getInt("size") > 0); Assertions.assertEquals(6, response.getJSONArray("datarows").getJSONArray(0).length()); JSONArray firstRow = response.getJSONArray("datarows").getJSONArray(0); @@ -93,19 +96,20 @@ public void testSourceMetricCommand() { @SneakyThrows public void testSourceMetricCommandWithTimestamp() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String query = "source=my_prometheus.prometheus_http_requests_total | where @timestamp > '" - + format.format(new Date(System.currentTimeMillis() - 3600 * 1000)) - + "' | sort + @timestamp | head 5"; + String query = + "source=my_prometheus.prometheus_http_requests_total | where @timestamp > '" + + format.format(new Date(System.currentTimeMillis() - 3600 * 1000)) + + "' | sort + @timestamp | head 5"; - JSONObject response = - executeQuery(query); - verifySchema(response, + JSONObject response = executeQuery(query); + verifySchema( + response, schema(VALUE, "double"), - schema(TIMESTAMP, "timestamp"), - schema("handler", "string"), - schema("code", "string"), - schema("instance", "string"), - schema("job", "string")); + schema(TIMESTAMP, "timestamp"), + schema("handler", "string"), + schema("code", "string"), + schema("instance", "string"), + schema("job", "string")); // Currently, data is not injected into prometheus, // so asserting on result is not possible. Verifying only schema. } @@ -114,9 +118,12 @@ public void testSourceMetricCommandWithTimestamp() { @SneakyThrows public void testMetricAvgAggregationCommand() { JSONObject response = - executeQuery("source=`my_prometheus`.`prometheus_http_requests_total` | stats avg(@value) as `agg` by span(@timestamp, 15s), `handler`, `job`"); - verifySchema(response, - schema("agg", "double"), + executeQuery( + "source=`my_prometheus`.`prometheus_http_requests_total` | stats avg(@value) as `agg`" + + " by span(@timestamp, 15s), `handler`, `job`"); + verifySchema( + response, + schema("agg", "double"), schema("span(@timestamp,15s)", "timestamp"), schema("handler", "string"), schema("job", "string")); @@ -133,9 +140,12 @@ public void testMetricAvgAggregationCommand() { @SneakyThrows public void testMetricAvgAggregationCommandWithAlias() { JSONObject response = - executeQuery("source=my_prometheus.prometheus_http_requests_total | stats avg(@value) as agg by span(@timestamp, 15s), `handler`, job"); - verifySchema(response, - schema("agg", "double"), + executeQuery( + "source=my_prometheus.prometheus_http_requests_total | stats avg(@value) as agg by" + + " span(@timestamp, 15s), `handler`, job"); + verifySchema( + response, + schema("agg", "double"), schema("span(@timestamp,15s)", "timestamp"), schema("handler", "string"), schema("job", "string")); @@ -148,15 +158,15 @@ public void testMetricAvgAggregationCommandWithAlias() { } } - @Test @SneakyThrows public void testMetricMaxAggregationCommand() { JSONObject response = - executeQuery("source=my_prometheus.prometheus_http_requests_total | stats max(@value) by span(@timestamp, 15s)"); - verifySchema(response, - schema("max(@value)", "double"), - schema("span(@timestamp,15s)", "timestamp")); + executeQuery( + "source=my_prometheus.prometheus_http_requests_total | stats max(@value) by" + + " span(@timestamp, 15s)"); + verifySchema( + response, schema("max(@value)", "double"), schema("span(@timestamp,15s)", "timestamp")); Assertions.assertTrue(response.getInt("size") > 0); Assertions.assertEquals(2, response.getJSONArray("datarows").getJSONArray(0).length()); JSONArray firstRow = response.getJSONArray("datarows").getJSONArray(0); @@ -166,14 +176,16 @@ public void testMetricMaxAggregationCommand() { } } - @Test @SneakyThrows public void testMetricMinAggregationCommand() { JSONObject response = - executeQuery("source=my_prometheus.prometheus_http_requests_total | stats min(@value) by span(@timestamp, 15s), handler"); - verifySchema(response, - schema("min(@value)", "double"), + executeQuery( + "source=my_prometheus.prometheus_http_requests_total | stats min(@value) by" + + " span(@timestamp, 15s), handler"); + verifySchema( + response, + schema("min(@value)", "double"), schema("span(@timestamp,15s)", "timestamp"), schema("handler", "string")); Assertions.assertTrue(response.getInt("size") > 0); @@ -189,9 +201,12 @@ public void testMetricMinAggregationCommand() { @SneakyThrows public void testMetricCountAggregationCommand() { JSONObject response = - executeQuery("source=my_prometheus.prometheus_http_requests_total | stats count() by span(@timestamp, 15s), handler, job"); - verifySchema(response, - schema("count()", "integer"), + executeQuery( + "source=my_prometheus.prometheus_http_requests_total | stats count() by" + + " span(@timestamp, 15s), handler, job"); + verifySchema( + response, + schema("count()", "integer"), schema("span(@timestamp,15s)", "timestamp"), schema("handler", "string"), schema("job", "string")); @@ -208,9 +223,12 @@ public void testMetricCountAggregationCommand() { @SneakyThrows public void testMetricSumAggregationCommand() { JSONObject response = - executeQuery("source=my_prometheus.prometheus_http_requests_total | stats sum(@value) by span(@timestamp, 15s), handler, job"); - verifySchema(response, - schema("sum(@value)", "double"), + executeQuery( + "source=my_prometheus.prometheus_http_requests_total | stats sum(@value) by" + + " span(@timestamp, 15s), handler, job"); + verifySchema( + response, + schema("sum(@value)", "double"), schema("span(@timestamp,15s)", "timestamp"), schema("handler", "string"), schema("job", "string")); @@ -223,18 +241,21 @@ public void testMetricSumAggregationCommand() { } } - @Test @SneakyThrows public void testQueryRange() { long currentTimestamp = new Date().getTime(); JSONObject response = - executeQuery("source=my_prometheus.query_range('prometheus_http_requests_total'," - + ((currentTimestamp/1000)-3600) + "," + currentTimestamp/1000 + ", " + "'14'" + ")" ); - verifySchema(response, - schema(LABELS, "struct"), - schema(VALUE, "array"), - schema(TIMESTAMP, "array")); + executeQuery( + "source=my_prometheus.query_range('prometheus_http_requests_total'," + + ((currentTimestamp / 1000) - 3600) + + "," + + currentTimestamp / 1000 + + ", " + + "'14'" + + ")"); + verifySchema( + response, schema(LABELS, "struct"), schema(VALUE, "array"), schema(TIMESTAMP, "array")); Assertions.assertTrue(response.getInt("size") > 0); } @@ -243,25 +264,23 @@ public void explainQueryRange() throws Exception { String expected = loadFromFile("expectedOutput/ppl/explain_query_range.json"); assertJsonEquals( expected, - explainQueryToString("source = my_prometheus" - + ".query_range('prometheus_http_requests_total',1689281439,1689291439,14)") - ); + explainQueryToString( + "source = my_prometheus" + + ".query_range('prometheus_http_requests_total',1689281439,1689291439,14)")); } - @Test + @Test public void testExplainForQueryExemplars() throws Exception { String expected = loadFromFile("expectedOutput/ppl/explain_query_exemplars.json"); assertJsonEquals( expected, - explainQueryToString("source = my_prometheus." - + "query_exemplars('app_ads_ad_requests_total',1689228292,1689232299)") - ); + explainQueryToString( + "source = my_prometheus." + + "query_exemplars('app_ads_ad_requests_total',1689228292,1689232299)")); } String loadFromFile(String filename) throws Exception { URI uri = Resources.getResource(filename).toURI(); return new String(Files.readAllBytes(Paths.get(uri))); } - - } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/QueryAnalysisIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/QueryAnalysisIT.java index 422cc92cd2..80a89ed9c3 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/QueryAnalysisIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/QueryAnalysisIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT; @@ -79,10 +78,7 @@ public void queryShouldBeCaseInsensitiveInKeywords() { queryShouldPassSyntaxAndSemanticCheck(query); } - /** - * Commands that fail syntax analysis should throw - * {@link SyntaxCheckException}. - */ + /** Commands that fail syntax analysis should throw {@link SyntaxCheckException}. */ @Test public void queryNotStartingWithSearchCommandShouldFailSyntaxCheck() { String query = "fields firstname"; @@ -107,14 +103,12 @@ public void unsupportedAggregationFunctionShouldFailSyntaxCheck() { queryShouldThrowSyntaxException(query, "Failed to parse query due to offending symbol"); } - /** - * Commands that fail semantic analysis should throw {@link SemanticCheckException}. - */ + /** Commands that fail semantic analysis should throw {@link SemanticCheckException}. */ @Test public void nonexistentFieldShouldFailSemanticCheck() { String query = String.format("search source=%s | fields name", TEST_INDEX_ACCOUNT); - queryShouldThrowSemanticException(query, "can't resolve Symbol(namespace=FIELD_NAME, " - + "name=name) in type env"); + queryShouldThrowSemanticException( + query, "can't resolve Symbol(namespace=FIELD_NAME, name=name) in type env"); } private void queryShouldPassSyntaxAndSemanticCheck(String query) { @@ -134,7 +128,7 @@ private void queryShouldThrowSyntaxException(String query, String... messages) { } catch (ResponseException e) { String errorMsg = e.getMessage(); assertTrue(errorMsg.contains("SyntaxCheckException")); - for (String msg: messages) { + for (String msg : messages) { assertTrue(errorMsg.contains(msg)); } } catch (IOException e) { diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/QueryStringIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/QueryStringIT.java index 4ace407d72..42a637ead7 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/QueryStringIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/QueryStringIT.java @@ -27,22 +27,29 @@ public void all_fields_test() throws IOException { @Test public void mandatory_params_test() throws IOException { - String query = "source=" + TEST_INDEX_BEER + " | where query_string([\\\"Tags\\\" ^ 1.5, Title, 'Body' 4.2], 'taste')"; + String query = + "source=" + + TEST_INDEX_BEER + + " | where query_string([\\\"Tags\\\" ^ 1.5, Title, 'Body' 4.2], 'taste')"; JSONObject result = executeQuery(query); assertEquals(16, result.getInt("total")); } @Test public void all_params_test() throws IOException { - String query = "source=" + TEST_INDEX_BEER + " | where query_string(['Body', Tags, Title], 'taste beer'," - + "allow_leading_wildcard=true, enable_position_increments=true, escape=false," - + "fuzziness= 1, fuzzy_rewrite='constant_score', max_determinized_states = 10000," - + "analyzer='english', analyze_wildcard = false, quote_field_suffix = '.exact'," - + "auto_generate_synonyms_phrase_query=true, boost = 0.77," - + "quote_analyzer='standard', phrase_slop=0, rewrite='constant_score', type='best_fields'," - + "tie_breaker=0.3, time_zone='Canada/Pacific', default_operator='or'," - + "fuzzy_transpositions = false, lenient = true, fuzzy_max_expansions = 25," - + "minimum_should_match = '2<-25% 9<-3', fuzzy_prefix_length = 7)"; + String query = + "source=" + + TEST_INDEX_BEER + + " | where query_string(['Body', Tags, Title], 'taste" + + " beer',allow_leading_wildcard=true, enable_position_increments=true," + + " escape=false,fuzziness= 1, fuzzy_rewrite='constant_score', max_determinized_states" + + " = 10000,analyzer='english', analyze_wildcard = false, quote_field_suffix =" + + " '.exact',auto_generate_synonyms_phrase_query=true, boost =" + + " 0.77,quote_analyzer='standard', phrase_slop=0, rewrite='constant_score'," + + " type='best_fields',tie_breaker=0.3, time_zone='Canada/Pacific'," + + " default_operator='or',fuzzy_transpositions = false, lenient = true," + + " fuzzy_max_expansions = 25,minimum_should_match = '2<-25% 9<-3', fuzzy_prefix_length" + + " = 7)"; JSONObject result = executeQuery(query); assertEquals(49, result.getInt("total")); } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/RareCommandIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/RareCommandIT.java index f65941b8f7..e3ed1661cd 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/RareCommandIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/RareCommandIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT; @@ -30,12 +29,8 @@ public void afterTest() throws IOException { @Test public void testRareWithoutGroup() throws IOException { - JSONObject result = - executeQuery(String.format("source=%s | rare gender", TEST_INDEX_ACCOUNT)); - verifyDataRows( - result, - rows("F"), - rows("M")); + JSONObject result = executeQuery(String.format("source=%s | rare gender", TEST_INDEX_ACCOUNT)); + verifyDataRows(result, rows("F"), rows("M")); } @Test @@ -65,6 +60,4 @@ public void testRareWithGroup() throws IOException { rows("M", "KY"), rows("M", "IN")); } - - } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/RelevanceFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/RelevanceFunctionIT.java index 7c57bd5481..8e6614dfed 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/RelevanceFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/RelevanceFunctionIT.java @@ -18,11 +18,11 @@ public void init() throws IOException { @Test public void test_wildcard_simple_query_string() throws IOException { - String query1 = "SOURCE=" + TEST_INDEX_BEER - + " | WHERE simple_query_string(['Tags'], 'taste') | fields Id"; + String query1 = + "SOURCE=" + TEST_INDEX_BEER + " | WHERE simple_query_string(['Tags'], 'taste') | fields Id"; var result1 = executeQuery(query1); - String query2 = "SOURCE=" + TEST_INDEX_BEER - + " | WHERE simple_query_string(['T*'], 'taste') | fields Id"; + String query2 = + "SOURCE=" + TEST_INDEX_BEER + " | WHERE simple_query_string(['T*'], 'taste') | fields Id"; var result2 = executeQuery(query2); assertNotEquals(result2.getInt("total"), result1.getInt("total")); } @@ -33,11 +33,15 @@ public void test_wildcard_simple_query_string() throws IOException { */ @Test public void verify_flags_in_simple_query_string() throws IOException { - String query1 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE simple_query_string(['Body'], '-free', flags='NONE|PREFIX|ESCAPE')"; + String query1 = + "SOURCE=" + + TEST_INDEX_BEER + + " | WHERE simple_query_string(['Body'], '-free', flags='NONE|PREFIX|ESCAPE')"; var result1 = executeQuery(query1); - String query2 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE simple_query_string([Body], '-free', flags='NOT|AND|OR')"; + String query2 = + "SOURCE=" + + TEST_INDEX_BEER + + " | WHERE simple_query_string([Body], '-free', flags='NOT|AND|OR')"; var result2 = executeQuery(query2); assertNotEquals(result2.getInt("total"), result1.getInt("total")); @@ -53,11 +57,11 @@ public void verify_flags_in_simple_query_string() throws IOException { */ @Test public void verify_escape_in_query_string() throws IOException { - String query1 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE query_string([Title], '?', escape=true);"; + String query1 = + "SOURCE=" + TEST_INDEX_BEER + " | WHERE query_string([Title], '?', escape=true);"; var result1 = executeQuery(query1); - String query2 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE query_string([Title], '?', escape=false);"; + String query2 = + "SOURCE=" + TEST_INDEX_BEER + " | WHERE query_string([Title], '?', escape=false);"; var result2 = executeQuery(query2); assertEquals(0, result1.getInt("total")); assertEquals(8, result2.getInt("total")); @@ -70,11 +74,15 @@ public void verify_escape_in_query_string() throws IOException { */ @Test public void verify_default_operator_in_query_string() throws IOException { - String query1 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE query_string([Title], 'beer taste', default_operator='OR')"; + String query1 = + "SOURCE=" + + TEST_INDEX_BEER + + " | WHERE query_string([Title], 'beer taste', default_operator='OR')"; var result1 = executeQuery(query1); - String query2 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE query_string([Title], 'beer taste', default_operator='AND')"; + String query2 = + "SOURCE=" + + TEST_INDEX_BEER + + " | WHERE query_string([Title], 'beer taste', default_operator='AND')"; var result2 = executeQuery(query2); assertEquals(16, result1.getInt("total")); assertEquals(4, result2.getInt("total")); @@ -82,11 +90,15 @@ public void verify_default_operator_in_query_string() throws IOException { @Test public void verify_default_operator_in_simple_query_string() throws IOException { - String query1 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE simple_query_string([Title], 'beer taste', default_operator='OR')"; + String query1 = + "SOURCE=" + + TEST_INDEX_BEER + + " | WHERE simple_query_string([Title], 'beer taste', default_operator='OR')"; var result1 = executeQuery(query1); - String query2 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE simple_query_string([Title], 'beer taste', default_operator='AND')"; + String query2 = + "SOURCE=" + + TEST_INDEX_BEER + + " | WHERE simple_query_string([Title], 'beer taste', default_operator='AND')"; var result2 = executeQuery(query2); assertEquals(16, result1.getInt("total")); assertEquals(4, result2.getInt("total")); @@ -94,11 +106,11 @@ public void verify_default_operator_in_simple_query_string() throws IOException @Test public void verify_default_operator_in_multi_match() throws IOException { - String query1 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE multi_match([Title], 'beer taste', operator='OR')"; + String query1 = + "SOURCE=" + TEST_INDEX_BEER + " | WHERE multi_match([Title], 'beer taste', operator='OR')"; var result1 = executeQuery(query1); - String query2 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE multi_match([Title], 'beer taste', operator='AND')"; + String query2 = + "SOURCE=" + TEST_INDEX_BEER + " | WHERE multi_match([Title], 'beer taste', operator='AND')"; var result2 = executeQuery(query2); assertEquals(16, result1.getInt("total")); assertEquals(4, result2.getInt("total")); @@ -106,11 +118,11 @@ public void verify_default_operator_in_multi_match() throws IOException { @Test public void verify_operator_in_match() throws IOException { - String query1 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE match(Title, 'beer taste', operator='OR')"; + String query1 = + "SOURCE=" + TEST_INDEX_BEER + " | WHERE match(Title, 'beer taste', operator='OR')"; var result1 = executeQuery(query1); - String query2 = "SOURCE=" - + TEST_INDEX_BEER + " | WHERE match(Title, 'beer taste', operator='AND')"; + String query2 = + "SOURCE=" + TEST_INDEX_BEER + " | WHERE match(Title, 'beer taste', operator='AND')"; var result2 = executeQuery(query2); assertEquals(16, result1.getInt("total")); assertEquals(4, result2.getInt("total")); diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/RenameCommandIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/RenameCommandIT.java index ad1add4e12..ae06e75a06 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/RenameCommandIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/RenameCommandIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT; @@ -43,7 +42,9 @@ public void testRenameMultiField() throws IOException { verifyColumn(result, columnName("FIRSTNAME"), columnName("AGE")); } - @Ignore("Wildcard is unsupported yet. Enable once https://github.com/opensearch-project/sql/issues/787 is resolved.") + @Ignore( + "Wildcard is unsupported yet. Enable once" + + " https://github.com/opensearch-project/sql/issues/787 is resolved.") @Test public void testRenameWildcardFields() throws IOException { JSONObject result = executeQuery("source=" + TEST_INDEX_ACCOUNT + " | rename %name as %NAME"); diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/ResourceMonitorIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/ResourceMonitorIT.java index e608e94512..56b54ba748 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/ResourceMonitorIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/ResourceMonitorIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DOG; @@ -31,11 +30,11 @@ public void queryExceedResourceLimitShouldFail() throws IOException { new ClusterSetting("persistent", Settings.Key.QUERY_MEMORY_LIMIT.getKeyValue(), "1%")); String query = String.format("search source=%s age=20", TEST_INDEX_DOG); - ResponseException exception = - expectThrows(ResponseException.class, () -> executeQuery(query)); + ResponseException exception = expectThrows(ResponseException.class, () -> executeQuery(query)); assertEquals(503, exception.getResponse().getStatusLine().getStatusCode()); - assertThat(exception.getMessage(), Matchers.containsString("resource is not enough to run the" - + " query, quit.")); + assertThat( + exception.getMessage(), + Matchers.containsString("resource is not enough to run the" + " query, quit.")); // update plugins.ppl.query.memory_limit to default value 85% updateClusterSettings( diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/SearchCommandIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/SearchCommandIT.java index 2e62b464bb..5d1b0203d7 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/SearchCommandIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/SearchCommandIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/SettingsIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/SettingsIT.java index d012cce9e8..224afde4c5 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/SettingsIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/SettingsIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; @@ -26,17 +25,13 @@ public void init() throws IOException { public void testQuerySizeLimit() throws IOException { // Default setting, fetch 200 rows from source JSONObject result = - executeQuery( - String.format( - "search source=%s age>35 | fields firstname", TEST_INDEX_BANK)); + executeQuery(String.format("search source=%s age>35 | fields firstname", TEST_INDEX_BANK)); verifyDataRows(result, rows("Hattie"), rows("Elinor"), rows("Virginia")); // Fetch 1 rows from source setQuerySizeLimit(1); result = - executeQuery( - String.format( - "search source=%s age>35 | fields firstname", TEST_INDEX_BANK)); + executeQuery(String.format("search source=%s age>35 | fields firstname", TEST_INDEX_BANK)); verifyDataRows(result, rows("Hattie")); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/ShowDataSourcesCommandIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/ShowDataSourcesCommandIT.java index 4845d30033..c9c4854212 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/ShowDataSourcesCommandIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/ShowDataSourcesCommandIT.java @@ -28,10 +28,10 @@ public class ShowDataSourcesCommandIT extends PPLIntegTestCase { /** - * Integ tests are dependent on self generated metrics in prometheus instance. - * When running individual integ tests there - * is no time for generation of metrics in the test prometheus instance. - * This method gives prometheus time to generate metrics on itself. + * Integ tests are dependent on self generated metrics in prometheus instance. When running + * individual integ tests there is no time for generation of metrics in the test prometheus + * instance. This method gives prometheus time to generate metrics on itself. + * * @throws InterruptedException */ @BeforeClass @@ -42,8 +42,11 @@ protected static void metricGenerationWait() throws InterruptedException { @Override protected void init() throws InterruptedException, IOException { DataSourceMetadata createDSM = - new DataSourceMetadata("my_prometheus", DataSourceType.PROMETHEUS, - ImmutableList.of(), ImmutableMap.of("prometheus.uri", "http://localhost:9090")); + new DataSourceMetadata( + "my_prometheus", + DataSourceType.PROMETHEUS, + ImmutableList.of(), + ImmutableMap.of("prometheus.uri", "http://localhost:9090")); Request createRequest = getCreateDataSourceRequest(createDSM); Response response = client().performRequest(createRequest); Assert.assertEquals(201, response.getStatusLine().getStatusCode()); @@ -59,26 +62,14 @@ protected void deleteDataSourceMetadata() throws IOException { @Test public void testShowDataSourcesCommands() throws IOException { JSONObject result = executeQuery("show datasources"); - verifyDataRows(result, - rows("my_prometheus", "PROMETHEUS"), - rows("@opensearch", "OPENSEARCH")); - verifyColumn( - result, - columnName("DATASOURCE_NAME"), - columnName("CONNECTOR_TYPE") - ); + verifyDataRows(result, rows("my_prometheus", "PROMETHEUS"), rows("@opensearch", "OPENSEARCH")); + verifyColumn(result, columnName("DATASOURCE_NAME"), columnName("CONNECTOR_TYPE")); } @Test public void testShowDataSourcesCommandsWithWhereClause() throws IOException { JSONObject result = executeQuery("show datasources | where CONNECTOR_TYPE='PROMETHEUS'"); - verifyDataRows(result, - rows("my_prometheus", "PROMETHEUS")); - verifyColumn( - result, - columnName("DATASOURCE_NAME"), - columnName("CONNECTOR_TYPE") - ); + verifyDataRows(result, rows("my_prometheus", "PROMETHEUS")); + verifyColumn(result, columnName("DATASOURCE_NAME"), columnName("CONNECTOR_TYPE")); } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/SimpleQueryStringIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/SimpleQueryStringIT.java index 46111b902e..714557412f 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/SimpleQueryStringIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/SimpleQueryStringIT.java @@ -19,36 +19,42 @@ public void init() throws IOException { @Test public void test_simple_query_string() throws IOException { - String query = "SOURCE=" + TEST_INDEX_BEER - + " | WHERE simple_query_string([\\\"Tags\\\" ^ 1.5, Title, 'Body' 4.2], 'taste') | fields Id"; + String query = + "SOURCE=" + + TEST_INDEX_BEER + + " | WHERE simple_query_string([\\\"Tags\\\" ^ 1.5, Title, 'Body' 4.2], 'taste') |" + + " fields Id"; var result = executeQuery(query); assertEquals(16, result.getInt("total")); } @Test public void test_simple_query_string_all_params() throws IOException { - String query = "SOURCE=" + TEST_INDEX_BEER - + " | WHERE simple_query_string(['Body', Tags, Title], 'taste beer', default_operator='or'," - + "analyzer=english, analyze_wildcard = false, quote_field_suffix = '.exact'," - + "auto_generate_synonyms_phrase_query=true, boost = 0.77, flags='PREFIX'," - + "fuzzy_transpositions = false, lenient = true, fuzzy_max_expansions = 25," - + "minimum_should_match = '2<-25% 9<-3', fuzzy_prefix_length = 7) | fields Id"; + String query = + "SOURCE=" + + TEST_INDEX_BEER + + " | WHERE simple_query_string(['Body', Tags, Title], 'taste beer'," + + " default_operator='or',analyzer=english, analyze_wildcard = false," + + " quote_field_suffix = '.exact',auto_generate_synonyms_phrase_query=true, boost =" + + " 0.77, flags='PREFIX',fuzzy_transpositions = false, lenient = true," + + " fuzzy_max_expansions = 25,minimum_should_match = '2<-25% 9<-3', fuzzy_prefix_length" + + " = 7) | fields Id"; var result = executeQuery(query); assertEquals(49, result.getInt("total")); } @Test public void test_wildcard_simple_query_string() throws IOException { - String query1 = "SOURCE=" + TEST_INDEX_BEER - + " | WHERE simple_query_string(['Tags'], 'taste') | fields Id"; + String query1 = + "SOURCE=" + TEST_INDEX_BEER + " | WHERE simple_query_string(['Tags'], 'taste') | fields Id"; var result1 = executeQuery(query1); - String query2 = "SOURCE=" + TEST_INDEX_BEER - + " | WHERE simple_query_string(['T*'], 'taste') | fields Id"; + String query2 = + "SOURCE=" + TEST_INDEX_BEER + " | WHERE simple_query_string(['T*'], 'taste') | fields Id"; var result2 = executeQuery(query2); assertNotEquals(result2.getInt("total"), result1.getInt("total")); - String query3 = "source=" + TEST_INDEX_BEER - + " | where simple_query_string(['*Date'], '2014-01-22')"; + String query3 = + "source=" + TEST_INDEX_BEER + " | where simple_query_string(['*Date'], '2014-01-22')"; JSONObject result3 = executeQuery(query3); assertEquals(10, result3.getInt("total")); } diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/SortCommandIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/SortCommandIT.java index 01befa0541..c90a506252 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/SortCommandIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/SortCommandIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ppl; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/NestedIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/NestedIT.java index d3230188b7..54831cb561 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/NestedIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/NestedIT.java @@ -37,11 +37,13 @@ public void init() throws IOException { @Test public void nested_function_with_array_of_nested_field_test() { - String query = "SELECT nested(message.info), nested(comment.data) FROM " + TEST_INDEX_NESTED_TYPE; + String query = + "SELECT nested(message.info), nested(comment.data) FROM " + TEST_INDEX_NESTED_TYPE; JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifyDataRows(result, + verifyDataRows( + result, rows("c", "ab"), rows("a", "ab"), rows("b", "aa"), @@ -52,17 +54,20 @@ public void nested_function_with_array_of_nested_field_test() { @Test public void nested_function_in_select_test() { - String query = "SELECT nested(message.info), nested(comment.data), " - + "nested(message.dayOfWeek) FROM " - + TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS; + String query = + "SELECT nested(message.info), nested(comment.data), " + + "nested(message.dayOfWeek) FROM " + + TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS; JSONObject result = executeJdbcRequest(query); assertEquals(5, result.getInt("total")); - verifySchema(result, + verifySchema( + result, schema("nested(message.info)", null, "keyword"), schema("nested(comment.data)", null, "keyword"), schema("nested(message.dayOfWeek)", null, "long")); - verifyDataRows(result, + verifyDataRows( + result, rows("a", "ab", 1), rows("b", "aa", 2), rows("c", "aa", 1), @@ -74,8 +79,8 @@ public void nested_function_in_select_test() { // gets resolved @Disabled // TODO fix me when aggregation is supported public void nested_function_in_an_aggregate_function_in_select_test() { - String query = "SELECT sum(nested(message.dayOfWeek)) FROM " + - TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS; + String query = + "SELECT sum(nested(message.dayOfWeek)) FROM " + TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS; JSONObject result = executeJdbcRequest(query); verifyDataRows(result, rows(14)); } @@ -83,84 +88,67 @@ public void nested_function_in_an_aggregate_function_in_select_test() { // TODO Enable me when nested aggregation is supported @Disabled public void nested_function_with_arrays_in_an_aggregate_function_in_select_test() { - String query = "SELECT sum(nested(message.dayOfWeek)) FROM " + - TEST_INDEX_NESTED_TYPE; + String query = "SELECT sum(nested(message.dayOfWeek)) FROM " + TEST_INDEX_NESTED_TYPE; JSONObject result = executeJdbcRequest(query); verifyDataRows(result, rows(19)); } @Test public void nested_function_in_a_function_in_select_test() { - String query = "SELECT upper(nested(message.info)) FROM " + - TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS; + String query = + "SELECT upper(nested(message.info)) FROM " + TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS; JSONObject result = executeJdbcRequest(query); - verifyDataRows(result, - rows("A"), - rows("B"), - rows("C"), - rows("C"), - rows("ZZ")); + verifyDataRows(result, rows("A"), rows("B"), rows("C"), rows("C"), rows("ZZ")); } @Test public void nested_all_function_in_a_function_in_select_test() { - String query = "SELECT nested(message.*) FROM " + - TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS + " WHERE nested(message.info) = 'a'"; + String query = + "SELECT nested(message.*) FROM " + + TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS + + " WHERE nested(message.info) = 'a'"; JSONObject result = executeJdbcRequest(query); verifyDataRows(result, rows("e", 1, "a")); } @Test public void invalid_multiple_nested_all_function_in_a_function_in_select_test() { - String query = "SELECT nested(message.*), nested(message.info) FROM " + - TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS; - RuntimeException result = assertThrows( - RuntimeException.class, - () -> executeJdbcRequest(query) - ); + String query = + "SELECT nested(message.*), nested(message.info) FROM " + + TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS; + RuntimeException result = assertThrows(RuntimeException.class, () -> executeJdbcRequest(query)); assertTrue( result.getMessage().contains("IllegalArgumentException") - && result.getMessage().contains("Multiple entries with same key") - ); + && result.getMessage().contains("Multiple entries with same key")); } @Test public void nested_all_function_with_limit_test() { - String query = "SELECT nested(message.*) FROM " + - TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS + " LIMIT 3"; + String query = + "SELECT nested(message.*) FROM " + TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS + " LIMIT 3"; JSONObject result = executeJdbcRequest(query); - verifyDataRows(result, - rows("e", 1, "a"), - rows("f", 2, "b"), - rows("g", 1, "c") - ); + verifyDataRows(result, rows("e", 1, "a"), rows("f", 2, "b"), rows("g", 1, "c")); } - @Test public void nested_function_with_array_of_multi_nested_field_test() { String query = "SELECT nested(message.author.name) FROM " + TEST_INDEX_MULTI_NESTED_TYPE; JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifyDataRows(result, - rows("e"), - rows("f"), - rows("g"), - rows("h"), - rows("p"), - rows("yy")); + verifyDataRows(result, rows("e"), rows("f"), rows("g"), rows("h"), rows("p"), rows("yy")); } @Test public void nested_function_with_null_and_missing_fields_test() { - String query = "SELECT nested(message.info), nested(comment.data) FROM " - + TEST_INDEX_NESTED_WITH_NULLS; + String query = + "SELECT nested(message.info), nested(comment.data) FROM " + TEST_INDEX_NESTED_WITH_NULLS; JSONObject result = executeJdbcRequest(query); assertEquals(10, result.getInt("total")); - verifyDataRows(result, + verifyDataRows( + result, rows(null, "hh"), rows("b", "aa"), rows("c", "aa"), @@ -176,12 +164,14 @@ public void nested_function_with_null_and_missing_fields_test() { @Test public void nested_function_multiple_fields_with_matched_and_mismatched_paths_test() { String query = - "SELECT nested(message.author), nested(message.dayOfWeek), nested(message.info), nested(comment.data), " - + "nested(comment.likes) FROM " + TEST_INDEX_NESTED_TYPE; + "SELECT nested(message.author), nested(message.dayOfWeek), nested(message.info)," + + " nested(comment.data), nested(comment.likes) FROM " + + TEST_INDEX_NESTED_TYPE; JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifyDataRows(result, + verifyDataRows( + result, rows("e", 1, "a", "ab", 3), rows("f", 2, "b", "aa", 2), rows("g", 1, "c", "aa", 3), @@ -192,12 +182,12 @@ public void nested_function_multiple_fields_with_matched_and_mismatched_paths_te @Test public void nested_function_mixed_with_non_nested_type_test() { - String query = - "SELECT nested(message.info), someField FROM " + TEST_INDEX_NESTED_TYPE; + String query = "SELECT nested(message.info), someField FROM " + TEST_INDEX_NESTED_TYPE; JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifyDataRows(result, + verifyDataRows( + result, rows("a", "b"), rows("b", "a"), rows("c", "a"), @@ -209,46 +199,38 @@ public void nested_function_mixed_with_non_nested_type_test() { @Test public void nested_function_with_order_by_clause() { String query = - "SELECT nested(message.info) FROM " + TEST_INDEX_NESTED_TYPE + "SELECT nested(message.info) FROM " + + TEST_INDEX_NESTED_TYPE + " ORDER BY nested(message.info)"; JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifyDataRows(result, - rows("a"), - rows("c"), - rows("a"), - rows("b"), - rows("c"), - rows("zz")); + verifyDataRows(result, rows("a"), rows("c"), rows("a"), rows("b"), rows("c"), rows("zz")); } @Test public void nested_function_with_order_by_clause_desc() { String query = - "SELECT nested(message.info) FROM " + TEST_INDEX_NESTED_TYPE + "SELECT nested(message.info) FROM " + + TEST_INDEX_NESTED_TYPE + " ORDER BY nested(message.info, message) DESC"; JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifyDataRows(result, - rows("zz"), - rows("c"), - rows("c"), - rows("a"), - rows("b"), - rows("a")); + verifyDataRows(result, rows("zz"), rows("c"), rows("c"), rows("a"), rows("b"), rows("a")); } @Test public void nested_function_and_field_with_order_by_clause() { String query = - "SELECT nested(message.info), myNum FROM " + TEST_INDEX_NESTED_TYPE + "SELECT nested(message.info), myNum FROM " + + TEST_INDEX_NESTED_TYPE + " ORDER BY nested(message.info, message), myNum"; JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifyDataRows(result, + verifyDataRows( + result, rows("a", 1), rows("c", 4), rows("a", 4), @@ -266,9 +248,12 @@ public void nested_function_with_group_by_clause() { "SELECT count(*) FROM " + TEST_INDEX_NESTED_TYPE + " GROUP BY nested(message.info)"; JSONObject result = executeJdbcRequest(query); - assertTrue(result.getJSONObject("error").get("details").toString().contains( - "Aggregation type nested is not yet implemented" - )); + assertTrue( + result + .getJSONObject("error") + .get("details") + .toString() + .contains("Aggregation type nested is not yet implemented")); } // Nested function in HAVING clause is not yet implemented for JDBC format. This test ensures @@ -277,12 +262,19 @@ public void nested_function_with_group_by_clause() { @Test public void nested_function_with_having_clause() { String query = - "SELECT count(*) FROM " + TEST_INDEX_NESTED_TYPE + " GROUP BY myNum HAVING nested(comment.likes) > 7"; + "SELECT count(*) FROM " + + TEST_INDEX_NESTED_TYPE + + " GROUP BY myNum HAVING nested(comment.likes) > 7"; JSONObject result = executeJdbcRequest(query); - assertTrue(result.getJSONObject("error").get("details").toString().contains( - "For more details, please send request for Json format to see the raw response from OpenSearch engine." - )); + assertTrue( + result + .getJSONObject("error") + .get("details") + .toString() + .contains( + "For more details, please send request for Json format to see the raw response from" + + " OpenSearch engine.")); } @Test @@ -292,13 +284,11 @@ public void nested_function_mixed_with_non_nested_types_test() { JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifyDataRows(result, - rows("a", - new JSONObject(Map.of("south", 3, "west", "ab")), "ab"), - rows("b", - new JSONObject(Map.of("south", 5, "west", "ff")), "ff"), - rows("c", - new JSONObject(Map.of("south", 3, "west", "ll")), "ll"), + verifyDataRows( + result, + rows("a", new JSONObject(Map.of("south", 3, "west", "ab")), "ab"), + rows("b", new JSONObject(Map.of("south", 5, "west", "ff")), "ff"), + rows("c", new JSONObject(Map.of("south", 3, "west", "ll")), "ll"), rows("d", null, null), rows("i", null, null), rows("zz", null, null)); @@ -308,11 +298,13 @@ public void nested_function_mixed_with_non_nested_types_test() { public void nested_function_with_relevance_query() { String query = "SELECT nested(message.info), highlight(someField) FROM " - + TEST_INDEX_NESTED_TYPE + " WHERE match(someField, 'b')"; + + TEST_INDEX_NESTED_TYPE + + " WHERE match(someField, 'b')"; JSONObject result = executeJdbcRequest(query); assertEquals(3, result.getInt("total")); - verifyDataRows(result, + verifyDataRows( + result, rows("a", new JSONArray(List.of("b"))), rows("c", new JSONArray(List.of("b"))), rows("a", new JSONArray(List.of("b")))); @@ -322,60 +314,68 @@ public void nested_function_with_relevance_query() { public void nested_with_non_nested_type_test() { String query = "SELECT nested(someField) FROM " + TEST_INDEX_NESTED_TYPE; - Exception exception = assertThrows(RuntimeException.class, - () -> executeJdbcRequest(query)); - assertTrue(exception.getMessage().contains( - "{\n" + - " \"error\": {\n" + - " \"reason\": \"Invalid SQL query\",\n" + - " \"details\": \"Illegal nested field name: someField\",\n" + - " \"type\": \"IllegalArgumentException\"\n" + - " },\n" + - " \"status\": 400\n" + - "}" - )); + Exception exception = assertThrows(RuntimeException.class, () -> executeJdbcRequest(query)); + assertTrue( + exception + .getMessage() + .contains( + "{\n" + + " \"error\": {\n" + + " \"reason\": \"Invalid SQL query\",\n" + + " \"details\": \"Illegal nested field name: someField\",\n" + + " \"type\": \"IllegalArgumentException\"\n" + + " },\n" + + " \"status\": 400\n" + + "}")); } @Test public void nested_missing_path() { String query = "SELECT nested(message.invalid) FROM " + TEST_INDEX_MULTI_NESTED_TYPE; - Exception exception = assertThrows(RuntimeException.class, - () -> executeJdbcRequest(query)); - assertTrue(exception.getMessage().contains("" + - "{\n" + - " \"error\": {\n" + - " \"reason\": \"Invalid SQL query\",\n" + - " \"details\": \"can't resolve Symbol(namespace=FIELD_NAME, name=message.invalid) in type env\",\n" + - " \"type\": \"SemanticCheckException\"\n" + - " },\n" + - " \"status\": 400\n" + - "}" - )); + Exception exception = assertThrows(RuntimeException.class, () -> executeJdbcRequest(query)); + assertTrue( + exception + .getMessage() + .contains( + "{\n" + + " \"error\": {\n" + + " \"reason\": \"Invalid SQL query\",\n" + + " \"details\": \"can't resolve Symbol(namespace=FIELD_NAME," + + " name=message.invalid) in type env\",\n" + + " \"type\": \"SemanticCheckException\"\n" + + " },\n" + + " \"status\": 400\n" + + "}")); } @Test public void nested_missing_path_argument() { - String query = "SELECT nested(message.author.name, invalid) FROM " + TEST_INDEX_MULTI_NESTED_TYPE; + String query = + "SELECT nested(message.author.name, invalid) FROM " + TEST_INDEX_MULTI_NESTED_TYPE; - Exception exception = assertThrows(RuntimeException.class, - () -> executeJdbcRequest(query)); - assertTrue(exception.getMessage().contains("" + - "{\n" + - " \"error\": {\n" + - " \"reason\": \"Invalid SQL query\",\n" + - " \"details\": \"can't resolve Symbol(namespace=FIELD_NAME, name=invalid) in type env\",\n" + - " \"type\": \"SemanticCheckException\"\n" + - " },\n" + - " \"status\": 400\n" + - "}" - )); + Exception exception = assertThrows(RuntimeException.class, () -> executeJdbcRequest(query)); + assertTrue( + exception + .getMessage() + .contains( + "{\n" + + " \"error\": {\n" + + " \"reason\": \"Invalid SQL query\",\n" + + " \"details\": \"can't resolve Symbol(namespace=FIELD_NAME, name=invalid)" + + " in type env\",\n" + + " \"type\": \"SemanticCheckException\"\n" + + " },\n" + + " \"status\": 400\n" + + "}")); } @Test public void test_nested_where_with_and_conditional() { - String query = "SELECT nested(message.info), nested(message.author) FROM " + TEST_INDEX_NESTED_TYPE - + " WHERE nested(message, message.info = 'a' AND message.author = 'e')"; + String query = + "SELECT nested(message.info), nested(message.author) FROM " + + TEST_INDEX_NESTED_TYPE + + " WHERE nested(message, message.info = 'a' AND message.author = 'e')"; JSONObject result = executeJdbcRequest(query); assertEquals(1, result.getInt("total")); verifyDataRows(result, rows("a", "e")); @@ -383,22 +383,19 @@ public void test_nested_where_with_and_conditional() { @Test public void test_nested_in_select_and_where_as_predicate_expression() { - String query = "SELECT nested(message.info) FROM " + TEST_INDEX_NESTED_TYPE - + " WHERE nested(message.info) = 'a'"; + String query = + "SELECT nested(message.info) FROM " + + TEST_INDEX_NESTED_TYPE + + " WHERE nested(message.info) = 'a'"; JSONObject result = executeJdbcRequest(query); assertEquals(3, result.getInt("total")); - verifyDataRows( - result, - rows("a"), - rows("c"), - rows("a") - ); + verifyDataRows(result, rows("a"), rows("c"), rows("a")); } @Test public void test_nested_in_where_as_predicate_expression() { - String query = "SELECT message.info FROM " + TEST_INDEX_NESTED_TYPE - + " WHERE nested(message.info) = 'a'"; + String query = + "SELECT message.info FROM " + TEST_INDEX_NESTED_TYPE + " WHERE nested(message.info) = 'a'"; JSONObject result = executeJdbcRequest(query); assertEquals(2, result.getInt("total")); // Only first index of array is returned. Second index has 'a' @@ -407,8 +404,10 @@ public void test_nested_in_where_as_predicate_expression() { @Test public void test_nested_in_where_as_predicate_expression_with_like() { - String query = "SELECT message.info FROM " + TEST_INDEX_NESTED_TYPE - + " WHERE nested(message.info) LIKE 'a'"; + String query = + "SELECT message.info FROM " + + TEST_INDEX_NESTED_TYPE + + " WHERE nested(message.info) LIKE 'a'"; JSONObject result = executeJdbcRequest(query); assertEquals(2, result.getInt("total")); // Only first index of array is returned. Second index has 'a' @@ -417,21 +416,22 @@ public void test_nested_in_where_as_predicate_expression_with_like() { @Test public void test_nested_in_where_as_predicate_expression_with_multiple_conditions() { - String query = "SELECT message.info, comment.data, message.dayOfWeek FROM " + TEST_INDEX_NESTED_TYPE - + " WHERE nested(message.info) = 'zz' OR nested(comment.data) = 'ab' AND nested(message.dayOfWeek) >= 4"; + String query = + "SELECT message.info, comment.data, message.dayOfWeek FROM " + + TEST_INDEX_NESTED_TYPE + + " WHERE nested(message.info) = 'zz' OR nested(comment.data) = 'ab' AND" + + " nested(message.dayOfWeek) >= 4"; JSONObject result = executeJdbcRequest(query); assertEquals(2, result.getInt("total")); - verifyDataRows( - result, - rows("c", "ab", 4), - rows("zz", "aa", 6) - ); + verifyDataRows(result, rows("c", "ab", 4), rows("zz", "aa", 6)); } @Test public void test_nested_in_where_as_predicate_expression_with_relevance_query() { - String query = "SELECT comment.likes, someField FROM " + TEST_INDEX_NESTED_TYPE - + " WHERE nested(comment.likes) = 10 AND match(someField, 'a')"; + String query = + "SELECT comment.likes, someField FROM " + + TEST_INDEX_NESTED_TYPE + + " WHERE nested(comment.likes) = 10 AND match(someField, 'a')"; JSONObject result = executeJdbcRequest(query); assertEquals(1, result.getInt("total")); verifyDataRows(result, rows(10, "a")); @@ -443,11 +443,13 @@ public void nested_function_all_subfields() { JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifySchema(result, + verifySchema( + result, schema("nested(message.author)", null, "keyword"), schema("nested(message.dayOfWeek)", null, "long"), schema("nested(message.info)", null, "keyword")); - verifyDataRows(result, + verifyDataRows( + result, rows("e", 1, "a"), rows("f", 2, "b"), rows("g", 1, "c"), @@ -458,17 +460,18 @@ public void nested_function_all_subfields() { @Test public void nested_function_all_subfields_and_specified_subfield() { - String query = "SELECT nested(message.*), nested(comment.data) FROM " - + TEST_INDEX_NESTED_TYPE; + String query = "SELECT nested(message.*), nested(comment.data) FROM " + TEST_INDEX_NESTED_TYPE; JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifySchema(result, + verifySchema( + result, schema("nested(message.author)", null, "keyword"), schema("nested(message.dayOfWeek)", null, "long"), schema("nested(message.info)", null, "keyword"), schema("nested(comment.data)", null, "keyword")); - verifyDataRows(result, + verifyDataRows( + result, rows("e", 1, "a", "ab"), rows("f", 2, "b", "aa"), rows("g", 1, "c", "aa"), @@ -479,15 +482,16 @@ public void nested_function_all_subfields_and_specified_subfield() { @Test public void nested_function_all_deep_nested_subfields() { - String query = "SELECT nested(message.author.address.*) FROM " - + TEST_INDEX_MULTI_NESTED_TYPE; + String query = "SELECT nested(message.author.address.*) FROM " + TEST_INDEX_MULTI_NESTED_TYPE; JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifySchema(result, + verifySchema( + result, schema("nested(message.author.address.number)", null, "integer"), schema("nested(message.author.address.street)", null, "keyword")); - verifyDataRows(result, + verifyDataRows( + result, rows(1, "bc"), rows(2, "ab"), rows(3, "sk"), @@ -498,18 +502,19 @@ public void nested_function_all_deep_nested_subfields() { @Test public void nested_function_all_subfields_for_two_nested_fields() { - String query = "SELECT nested(message.*), nested(comment.*) FROM " - + TEST_INDEX_NESTED_TYPE; + String query = "SELECT nested(message.*), nested(comment.*) FROM " + TEST_INDEX_NESTED_TYPE; JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifySchema(result, + verifySchema( + result, schema("nested(message.author)", null, "keyword"), schema("nested(message.dayOfWeek)", null, "long"), schema("nested(message.info)", null, "keyword"), schema("nested(comment.data)", null, "keyword"), schema("nested(comment.likes)", null, "long")); - verifyDataRows(result, + verifyDataRows( + result, rows("e", 1, "a", "ab", 3), rows("f", 2, "b", "aa", 2), rows("g", 1, "c", "aa", 3), @@ -524,12 +529,14 @@ public void nested_function_all_subfields_and_non_nested_field() { JSONObject result = executeJdbcRequest(query); assertEquals(6, result.getInt("total")); - verifySchema(result, + verifySchema( + result, schema("nested(message.author)", null, "keyword"), schema("nested(message.dayOfWeek)", null, "long"), schema("nested(message.info)", null, "keyword"), schema("myNum", null, "long")); - verifyDataRows(result, + verifyDataRows( + result, rows("e", 1, "a", 1), rows("f", 2, "b", 2), rows("g", 1, "c", 3), @@ -544,17 +551,15 @@ public void nested_function_with_date_types_as_object_arrays_within_arrays_test( JSONObject result = executeJdbcRequest(query); assertEquals(11, result.getInt("total")); - verifySchema(result, - schema("nested(address.moveInDate)", null, "object") - ); - verifyDataRows(result, - rows(new JSONObject(Map.of("dateAndTime","1984-04-12 09:07:42"))), - rows(new JSONArray( - List.of( - Map.of("dateAndTime", "2023-05-03 08:07:42"), - Map.of("dateAndTime", "2001-11-11 04:07:44")) - ) - ), + verifySchema(result, schema("nested(address.moveInDate)", null, "object")); + verifyDataRows( + result, + rows(new JSONObject(Map.of("dateAndTime", "1984-04-12 09:07:42"))), + rows( + new JSONArray( + List.of( + Map.of("dateAndTime", "2023-05-03 08:07:42"), + Map.of("dateAndTime", "2001-11-11 04:07:44")))), rows(new JSONObject(Map.of("dateAndTime", "1966-03-19 03:04:55"))), rows(new JSONObject(Map.of("dateAndTime", "2011-06-01 01:01:42"))), rows(new JSONObject(Map.of("dateAndTime", "1901-08-11 04:03:33"))), @@ -563,30 +568,27 @@ public void nested_function_with_date_types_as_object_arrays_within_arrays_test( rows(new JSONObject(Map.of("dateAndTime", "1977-07-13 09:04:41"))), rows(new JSONObject(Map.of("dateAndTime", "1933-12-12 05:05:45"))), rows(new JSONObject(Map.of("dateAndTime", "1909-06-17 01:04:21"))), - rows(new JSONArray( - List.of( - Map.of("dateAndTime", "2001-11-11 04:07:44")) - ) - ) - ); + rows(new JSONArray(List.of(Map.of("dateAndTime", "2001-11-11 04:07:44"))))); } @Test public void nested_function_all_subfields_in_wrong_clause() { String query = "SELECT * FROM " + TEST_INDEX_NESTED_TYPE + " ORDER BY nested(message.*)"; - Exception exception = assertThrows(RuntimeException.class, () -> - executeJdbcRequest(query)); - - assertTrue(exception.getMessage().contains("" + - "{\n" + - " \"error\": {\n" + - " \"reason\": \"There was internal problem at backend\",\n" + - " \"details\": \"Invalid use of expression nested(message.*)\",\n" + - " \"type\": \"UnsupportedOperationException\"\n" + - " },\n" + - " \"status\": 503\n" + - "}" - )); + Exception exception = assertThrows(RuntimeException.class, () -> executeJdbcRequest(query)); + + assertTrue( + exception + .getMessage() + .contains( + "" + + "{\n" + + " \"error\": {\n" + + " \"reason\": \"There was internal problem at backend\",\n" + + " \"details\": \"Invalid use of expression nested(message.*)\",\n" + + " \"type\": \"UnsupportedOperationException\"\n" + + " },\n" + + " \"status\": 503\n" + + "}")); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/NowLikeFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/NowLikeFunctionIT.java index de3dd0fe98..547c88859e 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/NowLikeFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/NowLikeFunctionIT.java @@ -77,8 +77,7 @@ public NowLikeFunctionIT( @Name("constValue") Boolean constValue, @Name("referenceGetter") Supplier referenceGetter, @Name("parser") BiFunction parser, - @Name("serializationPatternStr") String serializationPatternStr - ) { + @Name("serializationPatternStr") String serializationPatternStr) { this.name = name; this.hasFsp = hasFsp; this.hasShortcut = hasShortcut; @@ -90,56 +89,104 @@ public NowLikeFunctionIT( @ParametersFactory(argumentFormatting = "%1$s") public static Iterable compareTwoDates() { - return Arrays.asList($$( - $("now", false, false, true, - (Supplier) LocalDateTime::now, - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss"), - $("current_timestamp", false, false, true, - (Supplier) LocalDateTime::now, - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss"), - $("localtimestamp", false, false, true, - (Supplier) LocalDateTime::now, - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss"), - $("localtime", false, false, true, - (Supplier) LocalDateTime::now, - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss"), - $("sysdate", true, false, false, - (Supplier) LocalDateTime::now, - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss"), - $("curtime", false, false, false, - (Supplier) LocalTime::now, - (BiFunction) LocalTime::parse, - "HH:mm:ss"), - $("current_time", false, false, false, - (Supplier) LocalTime::now, - (BiFunction) LocalTime::parse, - "HH:mm:ss"), - $("curdate", false, false, false, - (Supplier) LocalDate::now, - (BiFunction) LocalDate::parse, - "uuuu-MM-dd"), - $("current_date", false, false, false, - (Supplier) LocalDate::now, - (BiFunction) LocalDate::parse, - "uuuu-MM-dd"), - $("utc_date", false, false, true, - (Supplier) (() -> utcDateTimeNow().toLocalDate()), - (BiFunction) LocalDate::parse, - "uuuu-MM-dd"), - $("utc_time", false, false, true, - (Supplier) (() -> utcDateTimeNow().toLocalTime()), - (BiFunction) LocalTime::parse, - "HH:mm:ss"), - $("utc_timestamp", false, false, true, - (Supplier) (NowLikeFunctionIT::utcDateTimeNow), - (BiFunction) LocalDateTime::parse, - "uuuu-MM-dd HH:mm:ss") - )); + return Arrays.asList( + $$( + $( + "now", + false, + false, + true, + (Supplier) LocalDateTime::now, + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"), + $( + "current_timestamp", + false, + false, + true, + (Supplier) LocalDateTime::now, + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"), + $( + "localtimestamp", + false, + false, + true, + (Supplier) LocalDateTime::now, + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"), + $( + "localtime", + false, + false, + true, + (Supplier) LocalDateTime::now, + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"), + $( + "sysdate", + true, + false, + false, + (Supplier) LocalDateTime::now, + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"), + $( + "curtime", + false, + false, + false, + (Supplier) LocalTime::now, + (BiFunction) LocalTime::parse, + "HH:mm:ss"), + $( + "current_time", + false, + false, + false, + (Supplier) LocalTime::now, + (BiFunction) LocalTime::parse, + "HH:mm:ss"), + $( + "curdate", + false, + false, + false, + (Supplier) LocalDate::now, + (BiFunction) LocalDate::parse, + "uuuu-MM-dd"), + $( + "current_date", + false, + false, + false, + (Supplier) LocalDate::now, + (BiFunction) LocalDate::parse, + "uuuu-MM-dd"), + $( + "utc_date", + false, + false, + true, + (Supplier) (() -> utcDateTimeNow().toLocalDate()), + (BiFunction) LocalDate::parse, + "uuuu-MM-dd"), + $( + "utc_time", + false, + false, + true, + (Supplier) (() -> utcDateTimeNow().toLocalTime()), + (BiFunction) LocalTime::parse, + "HH:mm:ss"), + $( + "utc_timestamp", + false, + false, + true, + (Supplier) (NowLikeFunctionIT::utcDateTimeNow), + (BiFunction) LocalDateTime::parse, + "uuuu-MM-dd HH:mm:ss"))); } private long getDiff(Temporal sample, Temporal reference) { @@ -150,14 +197,14 @@ private long getDiff(Temporal sample, Temporal reference) { } public static LocalDateTime utcDateTimeNow() { - ZonedDateTime zonedDateTime = - LocalDateTime.now().atZone(TimeZone.getDefault().toZoneId()); + ZonedDateTime zonedDateTime = LocalDateTime.now().atZone(TimeZone.getDefault().toZoneId()); return zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime(); } @Test public void testNowLikeFunctions() throws IOException { - var serializationPattern = new DateTimeFormatterBuilder() + var serializationPattern = + new DateTimeFormatterBuilder() .appendPattern(serializationPatternStr) .optionalStart() .appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true) @@ -167,15 +214,16 @@ public void testNowLikeFunctions() throws IOException { double delta = 2d; // acceptable time diff, secs if (reference instanceof LocalDate) delta = 1d; // Max date delta could be 1 if test runs on the very edge of two days - // We ignore probability of a test run on edge of month or year to simplify the checks + // We ignore probability of a test run on edge of month or year to simplify the checks - var calls = new ArrayList() {{ - add(name + "()"); - }}; - if (hasShortcut) - calls.add(name); - if (hasFsp) - calls.add(name + "(0)"); + var calls = + new ArrayList() { + { + add(name + "()"); + } + }; + if (hasShortcut) calls.add(name); + if (hasFsp) calls.add(name + "(0)"); // Column order is: func(), func, func(0) // shortcut ^ fsp ^ @@ -185,20 +233,25 @@ public void testNowLikeFunctions() throws IOException { JSONArray firstRow = rows.getJSONArray(0); for (int i = 0; i < rows.length(); i++) { var row = rows.getJSONArray(i); - if (constValue) - assertTrue(firstRow.similar(row)); + if (constValue) assertTrue(firstRow.similar(row)); int column = 0; - assertEquals(0, - getDiff(reference, parser.apply(row.getString(column++), serializationPattern)), delta); + assertEquals( + 0, + getDiff(reference, parser.apply(row.getString(column++), serializationPattern)), + delta); if (hasShortcut) { - assertEquals(0, - getDiff(reference, parser.apply(row.getString(column++), serializationPattern)), delta); + assertEquals( + 0, + getDiff(reference, parser.apply(row.getString(column++), serializationPattern)), + delta); } if (hasFsp) { - assertEquals(0, - getDiff(reference, parser.apply(row.getString(column), serializationPattern)), delta); + assertEquals( + 0, + getDiff(reference, parser.apply(row.getString(column), serializationPattern)), + delta); } } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/NullLiteralIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/NullLiteralIT.java index b8bf0963b5..f885b6d4e0 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/NullLiteralIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/NullLiteralIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.sql; import static org.opensearch.sql.util.MatcherUtils.rows; @@ -34,28 +33,22 @@ public void testNullLiteralSchema() { @Test public void testNullLiteralInOperator() { - verifyDataRows( - query("SELECT NULL = NULL, NULL AND TRUE"), - rows(null, null)); + verifyDataRows(query("SELECT NULL = NULL, NULL AND TRUE"), rows(null, null)); } @Test public void testNullLiteralInFunction() { - verifyDataRows( - query("SELECT ABS(NULL), POW(2, FLOOR(NULL))"), - rows(null, null)); + verifyDataRows(query("SELECT ABS(NULL), POW(2, FLOOR(NULL))"), rows(null, null)); } @Test public void testNullLiteralInInterval() { verifyDataRows( query("SELECT INTERVAL NULL DAY, INTERVAL 60 * 60 * 24 * (NULL - FLOOR(NULL)) SECOND"), - rows(null, null) - ); + rows(null, null)); } private JSONObject query(String sql) { return new JSONObject(executeQuery(sql, "jdbc")); } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/PaginationBlackboxIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/PaginationBlackboxIT.java index e6f4e18468..84289d8f57 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/PaginationBlackboxIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/PaginationBlackboxIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.sql; import com.carrotsearch.randomizedtesting.annotations.Name; @@ -27,8 +26,7 @@ public class PaginationBlackboxIT extends SQLIntegTestCase { private final Index index; private final Integer pageSize; - public PaginationBlackboxIT(@Name("index") Index index, - @Name("pageSize") Integer pageSize) { + public PaginationBlackboxIT(@Name("index") Index index, @Name("pageSize") Integer pageSize) { this.index = index; this.pageSize = pageSize; } @@ -45,7 +43,7 @@ public static Iterable compareTwoDates() { var testData = new ArrayList(); for (var index : indices) { for (var pageSize : pageSizes) { - testData.add(new Object[] { index, pageSize }); + testData.add(new Object[] {index, pageSize}); } } return testData; @@ -64,14 +62,19 @@ public void test_pagination_blackbox() { var responseCounter = 1; this.logger.info(testReportPrefix + "first response"); - response = new JSONObject(executeFetchQuery( - String.format("select * from %s", index.getName()), pageSize, "jdbc")); + response = + new JSONObject( + executeFetchQuery( + String.format("select * from %s", index.getName()), pageSize, "jdbc")); - var cursor = response.has("cursor")? response.getString("cursor") : ""; + var cursor = response.has("cursor") ? response.getString("cursor") : ""; do { - this.logger.info(testReportPrefix - + String.format("subsequent response %d/%d", responseCounter++, (indexSize / pageSize) + 1)); - assertTrue("Paged response schema doesn't match to non-paged", + this.logger.info( + testReportPrefix + + String.format( + "subsequent response %d/%d", responseCounter++, (indexSize / pageSize) + 1)); + assertTrue( + "Paged response schema doesn't match to non-paged", schema.similar(response.getJSONArray("schema"))); rowsReturned += response.getInt("size"); @@ -88,13 +91,17 @@ public void test_pagination_blackbox() { cursor = ""; } - } while(!cursor.isEmpty()); - assertTrue("Paged response schema doesn't match to non-paged", + } while (!cursor.isEmpty()); + assertTrue( + "Paged response schema doesn't match to non-paged", schema.similar(response.getJSONArray("schema"))); - assertEquals(testReportPrefix + "Paged responses return another row count that non-paged", - indexSize, rowsReturned); - assertTrue(testReportPrefix + "Paged accumulated result has other rows than non-paged", + assertEquals( + testReportPrefix + "Paged responses return another row count that non-paged", + indexSize, + rowsReturned); + assertTrue( + testReportPrefix + "Paged accumulated result has other rows than non-paged", rows.similar(rowsPaged)); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/PaginationFallbackIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/PaginationFallbackIT.java index 213c9322e1..dfb0bb2080 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/PaginationFallbackIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/PaginationFallbackIT.java @@ -36,23 +36,24 @@ public void testSelectAll() throws IOException { @Test public void testSelectWithOpenSearchFuncInFilter() throws IOException { - var response = executeQueryTemplate( - "SELECT * FROM %s WHERE `11` = match_phrase('96')", TEST_INDEX_ONLINE); + var response = + executeQueryTemplate("SELECT * FROM %s WHERE `11` = match_phrase('96')", TEST_INDEX_ONLINE); verifyIsV2Cursor(response); } @Test public void testSelectWithHighlight() throws IOException { - var response = executeQueryTemplate( - "SELECT highlight(`11`) FROM %s WHERE match_query(`11`, '96')", TEST_INDEX_ONLINE); + var response = + executeQueryTemplate( + "SELECT highlight(`11`) FROM %s WHERE match_query(`11`, '96')", TEST_INDEX_ONLINE); verifyIsV2Cursor(response); } @Test public void testSelectWithFullTextSearch() throws IOException { - var response = executeQueryTemplate( - "SELECT * FROM %s WHERE match_phrase(`11`, '96')", TEST_INDEX_ONLINE); + var response = + executeQueryTemplate("SELECT * FROM %s WHERE match_phrase(`11`, '96')", TEST_INDEX_ONLINE); verifyIsV2Cursor(response); } @@ -64,8 +65,7 @@ public void testSelectFromIndexWildcard() throws IOException { @Test public void testSelectFromDataSource() throws IOException { - var response = executeQueryTemplate("SELECT * FROM @opensearch.%s", - TEST_INDEX_ONLINE); + var response = executeQueryTemplate("SELECT * FROM @opensearch.%s", TEST_INDEX_ONLINE); verifyIsV2Cursor(response); } @@ -77,31 +77,29 @@ public void testSelectColumnReference() throws IOException { @Test public void testSubquery() throws IOException { - var response = executeQueryTemplate("SELECT `107` from (SELECT * FROM %s)", - TEST_INDEX_ONLINE); + var response = executeQueryTemplate("SELECT `107` from (SELECT * FROM %s)", TEST_INDEX_ONLINE); verifyIsV1Cursor(response); } @Test public void testSelectExpression() throws IOException { - var response = executeQueryTemplate("SELECT 1 + 1 - `107` from %s", - TEST_INDEX_ONLINE); + var response = executeQueryTemplate("SELECT 1 + 1 - `107` from %s", TEST_INDEX_ONLINE); verifyIsV2Cursor(response); } @Test public void testGroupBy() throws IOException { // GROUP BY is not paged by either engine. - var response = executeQueryTemplate("SELECT * FROM %s GROUP BY `107`", - TEST_INDEX_ONLINE); + var response = executeQueryTemplate("SELECT * FROM %s GROUP BY `107`", TEST_INDEX_ONLINE); TestUtils.verifyNoCursor(response); } @Test public void testGroupByHaving() throws IOException { // GROUP BY is not paged by either engine. - var response = executeQueryTemplate("SELECT * FROM %s GROUP BY `107` HAVING `107` > 400", - TEST_INDEX_ONLINE); + var response = + executeQueryTemplate( + "SELECT * FROM %s GROUP BY `107` HAVING `107` > 400", TEST_INDEX_ONLINE); TestUtils.verifyNoCursor(response); } @@ -113,15 +111,13 @@ public void testLimit() throws IOException { @Test public void testLimitOffset() throws IOException { - var response = executeQueryTemplate("SELECT * FROM %s LIMIT 8 OFFSET 4", - TEST_INDEX_ONLINE); + var response = executeQueryTemplate("SELECT * FROM %s LIMIT 8 OFFSET 4", TEST_INDEX_ONLINE); verifyIsV1Cursor(response); } @Test public void testOrderBy() throws IOException { - var response = executeQueryTemplate("SELECT * FROM %s ORDER By `107`", - TEST_INDEX_ONLINE); + var response = executeQueryTemplate("SELECT * FROM %s ORDER By `107`", TEST_INDEX_ONLINE); verifyIsV2Cursor(response); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/PaginationFilterIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/PaginationFilterIT.java index 6ebc05efad..038596cf57 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/PaginationFilterIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/PaginationFilterIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.sql; import com.carrotsearch.randomizedtesting.annotations.Name; @@ -22,38 +21,48 @@ import org.opensearch.sql.legacy.TestsConstants; /** - * Test pagination with `WHERE` clause using a parametrized test. - * See constructor {@link #PaginationFilterIT} for list of parameters - * and {@link #generateParameters} and {@link #STATEMENT_TO_NUM_OF_PAGES} - * to see how these parameters are generated. + * Test pagination with `WHERE` clause using a parametrized test. See constructor {@link + * #PaginationFilterIT} for list of parameters and {@link #generateParameters} and {@link + * #STATEMENT_TO_NUM_OF_PAGES} to see how these parameters are generated. */ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) public class PaginationFilterIT extends SQLIntegTestCase { /** - * Map of the OS-SQL statement sent to SQL-plugin, and the total number - * of expected hits (on all pages) from the filtered result + * Map of the OS-SQL statement sent to SQL-plugin, and the total number of expected hits (on all + * pages) from the filtered result */ - final private static Map STATEMENT_TO_NUM_OF_PAGES = Map.of( - "SELECT * FROM " + TestsConstants.TEST_INDEX_ACCOUNT, 1000, - "SELECT * FROM " + TestsConstants.TEST_INDEX_ACCOUNT + " WHERE match(address, 'street')", 385, - "SELECT * FROM " + TestsConstants.TEST_INDEX_ACCOUNT + " WHERE match(address, 'street') AND match(city, 'Ola')", 1, - "SELECT firstname, lastname, highlight(address) FROM " + TestsConstants.TEST_INDEX_ACCOUNT + " WHERE match(address, 'street') AND match(state, 'OH')", 5, - "SELECT firstname, lastname, highlight('*') FROM " + TestsConstants.TEST_INDEX_ACCOUNT + " WHERE match(address, 'street') AND match(state, 'OH')", 5, - "SELECT * FROM " + TestsConstants.TEST_INDEX_BEER + " WHERE true", 60, - "SELECT * FROM " + TestsConstants.TEST_INDEX_BEER + " WHERE Id=10", 1, - "SELECT * FROM " + TestsConstants.TEST_INDEX_BEER + " WHERE Id + 5=15", 1, - "SELECT * FROM " + TestsConstants.TEST_INDEX_BANK, 7 - ); + private static final Map STATEMENT_TO_NUM_OF_PAGES = + Map.of( + "SELECT * FROM " + TestsConstants.TEST_INDEX_ACCOUNT, 1000, + "SELECT * FROM " + TestsConstants.TEST_INDEX_ACCOUNT + " WHERE match(address, 'street')", + 385, + "SELECT * FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " WHERE match(address, 'street') AND match(city, 'Ola')", + 1, + "SELECT firstname, lastname, highlight(address) FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " WHERE match(address, 'street') AND match(state, 'OH')", + 5, + "SELECT firstname, lastname, highlight('*') FROM " + + TestsConstants.TEST_INDEX_ACCOUNT + + " WHERE match(address, 'street') AND match(state, 'OH')", + 5, + "SELECT * FROM " + TestsConstants.TEST_INDEX_BEER + " WHERE true", 60, + "SELECT * FROM " + TestsConstants.TEST_INDEX_BEER + " WHERE Id=10", 1, + "SELECT * FROM " + TestsConstants.TEST_INDEX_BEER + " WHERE Id + 5=15", 1, + "SELECT * FROM " + TestsConstants.TEST_INDEX_BANK, 7); private final String sqlStatement; private final Integer totalHits; private final Integer pageSize; - public PaginationFilterIT(@Name("statement") String sqlStatement, - @Name("total_hits") Integer totalHits, - @Name("page_size") Integer pageSize) { + public PaginationFilterIT( + @Name("statement") String sqlStatement, + @Name("total_hits") Integer totalHits, + @Name("page_size") Integer pageSize) { this.sqlStatement = sqlStatement; this.totalHits = totalHits; this.pageSize = pageSize; @@ -72,18 +81,18 @@ public static Iterable generateParameters() { List pageSizes = List.of(5, 1000); List testData = new ArrayList(); - STATEMENT_TO_NUM_OF_PAGES.forEach((statement, totalHits) -> { - for (var pageSize : pageSizes) { - testData.add(new Object[] { statement, totalHits, pageSize }); - } - }); + STATEMENT_TO_NUM_OF_PAGES.forEach( + (statement, totalHits) -> { + for (var pageSize : pageSizes) { + testData.add(new Object[] {statement, totalHits, pageSize}); + } + }); return testData; } /** - * Test compares non-paginated results with paginated results - * To ensure that the pushdowns return the same number of hits even - * with filter WHERE pushed down + * Test compares non-paginated results with paginated results To ensure that the pushdowns return + * the same number of hits even with filter WHERE pushed down */ @Test @SneakyThrows @@ -93,7 +102,10 @@ public void test_pagination_with_where() { int totalResultsCount = nonPaginatedResponse.getInt("total"); JSONArray rows = nonPaginatedResponse.getJSONArray("datarows"); JSONArray schema = nonPaginatedResponse.getJSONArray("schema"); - var testReportPrefix = String.format("query: %s; total hits: %d; page size: %d || ", sqlStatement, totalResultsCount, pageSize); + var testReportPrefix = + String.format( + "query: %s; total hits: %d; page size: %d || ", + sqlStatement, totalResultsCount, pageSize); assertEquals(totalHits.intValue(), totalResultsCount); var rowsPaged = new JSONArray(); @@ -101,7 +113,8 @@ public void test_pagination_with_where() { var responseCounter = 1; // make first request - with a cursor - JSONObject paginatedResponse = new JSONObject(executeFetchQuery(sqlStatement, pageSize, "jdbc")); + JSONObject paginatedResponse = + new JSONObject(executeFetchQuery(sqlStatement, pageSize, "jdbc")); this.logger.info(testReportPrefix + ""); do { var cursor = paginatedResponse.has("cursor") ? paginatedResponse.getString("cursor") : null; @@ -117,27 +130,34 @@ public void test_pagination_with_where() { if (cursor != null) { assertTrue( - testReportPrefix + "Cursor returned from legacy engine", - cursor.startsWith("n:")); + testReportPrefix + "Cursor returned from legacy engine", cursor.startsWith("n:")); paginatedResponse = executeCursorQuery(cursor); - this.logger.info(testReportPrefix - + String.format("response %d/%d", responseCounter++, (totalResultsCount / pageSize) + 1)); + this.logger.info( + testReportPrefix + + String.format( + "response %d/%d", responseCounter++, (totalResultsCount / pageSize) + 1)); } else { break; } } while (true); // last page expected results: - assertEquals(testReportPrefix + "Last page", - totalHits % pageSize, paginatedResponse.getInt("size")); - assertEquals(testReportPrefix + "Last page", - totalHits % pageSize, paginatedResponse.getJSONArray("datarows").length()); + assertEquals( + testReportPrefix + "Last page", totalHits % pageSize, paginatedResponse.getInt("size")); + assertEquals( + testReportPrefix + "Last page", + totalHits % pageSize, + paginatedResponse.getJSONArray("datarows").length()); // compare paginated and non-paginated counts - assertEquals(testReportPrefix + "Paged responses returned an unexpected total", - totalResultsCount, pagedSize); - assertEquals(testReportPrefix + "Paged responses returned an unexpected rows count", - rows.length(), rowsPaged.length()); + assertEquals( + testReportPrefix + "Paged responses returned an unexpected total", + totalResultsCount, + pagedSize); + assertEquals( + testReportPrefix + "Paged responses returned an unexpected rows count", + rows.length(), + rowsPaged.length()); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/PaginationIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/PaginationIT.java index 224a1e95e4..49ef7c583e 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/PaginationIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/PaginationIT.java @@ -73,12 +73,16 @@ public void testCursorTimeout() throws IOException, InterruptedException { ResponseException exception = expectThrows(ResponseException.class, () -> executeCursorQuery(cursor)); response = new JSONObject(TestUtils.getResponseBody(exception.getResponse())); - assertEquals(response.getJSONObject("error").getString("reason"), + assertEquals( + response.getJSONObject("error").getString("reason"), "Error occurred in OpenSearch engine: all shards failed"); - assertTrue(response.getJSONObject("error").getString("details") - .contains("SearchContextMissingException[No search context found for id")); - assertEquals(response.getJSONObject("error").getString("type"), - "SearchPhaseExecutionException"); + assertTrue( + response + .getJSONObject("error") + .getString("details") + .contains("SearchContextMissingException[No search context found for id")); + assertEquals( + response.getJSONObject("error").getString("type"), "SearchPhaseExecutionException"); wipeAllClusterSettings(); } @@ -106,12 +110,16 @@ public void testCloseCursor() { ResponseException exception = expectThrows(ResponseException.class, () -> executeCursorQuery(cursor)); response = new JSONObject(TestUtils.getResponseBody(exception.getResponse())); - assertEquals(response.getJSONObject("error").getString("reason"), + assertEquals( + response.getJSONObject("error").getString("reason"), "Error occurred in OpenSearch engine: all shards failed"); - assertTrue(response.getJSONObject("error").getString("details") - .contains("SearchContextMissingException[No search context found for id")); - assertEquals(response.getJSONObject("error").getString("type"), - "SearchPhaseExecutionException"); + assertTrue( + response + .getJSONObject("error") + .getString("details") + .contains("SearchContextMissingException[No search context found for id")); + assertEquals( + response.getJSONObject("error").getString("type"), "SearchPhaseExecutionException"); } @Test @@ -134,7 +142,8 @@ public void testQueryWithOrderBy() { var cursor = response.getString("cursor"); do { assertTrue(cursor.isEmpty() || cursor.startsWith("n:")); - assertTrue("Paged response schema doesn't match to non-paged", + assertTrue( + "Paged response schema doesn't match to non-paged", schema.similar(response.getJSONArray("schema"))); rowsReturnedAsc += response.getInt("size"); @@ -151,7 +160,7 @@ public void testQueryWithOrderBy() { cursor = ""; } - } while(!cursor.isEmpty()); + } while (!cursor.isEmpty()); query = String.format("SELECT * from %s ORDER BY num1 DESC", TEST_INDEX_CALCS); response = new JSONObject(executeFetchQuery(query, 7, "jdbc")); @@ -160,7 +169,8 @@ public void testQueryWithOrderBy() { cursor = response.getString("cursor"); do { assertTrue(cursor.isEmpty() || cursor.startsWith("n:")); - assertTrue("Paged response schema doesn't match to non-paged", + assertTrue( + "Paged response schema doesn't match to non-paged", schema.similar(response.getJSONArray("schema"))); rowsReturnedDesc += response.getInt("size"); @@ -177,19 +187,22 @@ public void testQueryWithOrderBy() { cursor = ""; } - } while(!cursor.isEmpty()); + } while (!cursor.isEmpty()); - assertEquals("Paged responses return another row count that non-paged", - indexSize, rowsReturnedAsc); - assertEquals("Paged responses return another row count that non-paged", - indexSize, rowsReturnedDesc); - assertTrue("Paged accumulated result has other rows than non-paged", + assertEquals( + "Paged responses return another row count that non-paged", indexSize, rowsReturnedAsc); + assertEquals( + "Paged responses return another row count that non-paged", indexSize, rowsReturnedDesc); + assertTrue( + "Paged accumulated result has other rows than non-paged", rows.toList().containsAll(rowsPagedAsc.toList())); - assertTrue("Paged accumulated result has other rows than non-paged", + assertTrue( + "Paged accumulated result has other rows than non-paged", rows.toList().containsAll(rowsPagedDesc.toList())); for (int row = 0; row < indexSize; row++) { - assertTrue(String.format("Row %d: row order is incorrect", row), + assertTrue( + String.format("Row %d: row order is incorrect", row), rowsPagedAsc.getJSONArray(row).similar(rowsPagedDesc.getJSONArray(indexSize - row - 1))); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/PaginationWindowIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/PaginationWindowIT.java index be208cd137..246cbfc4a0 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/PaginationWindowIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/PaginationWindowIT.java @@ -40,10 +40,11 @@ public void testFetchSizeLessThanMaxResultWindow() throws IOException { } while (response.has("cursor")); numRows += response.getJSONArray("datarows").length(); - var countRows = executeJdbcRequest("SELECT COUNT(*) FROM " + TEST_INDEX_PHRASE) - .getJSONArray("datarows") - .getJSONArray(0) - .get(0); + var countRows = + executeJdbcRequest("SELECT COUNT(*) FROM " + TEST_INDEX_PHRASE) + .getJSONArray("datarows") + .getJSONArray(0) + .get(0); assertEquals(countRows, numRows); } @@ -62,10 +63,11 @@ public void testQuerySizeLimitDoesNotEffectTotalRowsReturned() throws IOExceptio response = executeCursorQuery(cursor); } while (response.has("cursor")); numRows += response.getJSONArray("datarows").length(); - var countRows = executeJdbcRequest("SELECT COUNT(*) FROM " + TEST_INDEX_PHRASE) - .getJSONArray("datarows") - .getJSONArray(0) - .get(0); + var countRows = + executeJdbcRequest("SELECT COUNT(*) FROM " + TEST_INDEX_PHRASE) + .getJSONArray("datarows") + .getJSONArray(0) + .get(0); assertEquals(countRows, numRows); assertTrue(numRows > querySizeLimit); } @@ -74,12 +76,10 @@ public void testQuerySizeLimitDoesNotEffectTotalRowsReturned() throws IOExceptio public void testQuerySizeLimitDoesNotEffectPageSize() throws IOException { setQuerySizeLimit(3); setMaxResultWindow(TEST_INDEX_PHRASE, 4); - var response - = executeQueryTemplate("SELECT * FROM %s", TEST_INDEX_PHRASE, 4); + var response = executeQueryTemplate("SELECT * FROM %s", TEST_INDEX_PHRASE, 4); assertEquals(4, response.getInt("size")); - var response2 - = executeQueryTemplate("SELECT * FROM %s", TEST_INDEX_PHRASE, 2); + var response2 = executeQueryTemplate("SELECT * FROM %s", TEST_INDEX_PHRASE, 2); assertEquals(2, response2.getInt("size")); } @@ -87,11 +87,9 @@ public void testQuerySizeLimitDoesNotEffectPageSize() throws IOException { public void testFetchSizeLargerThanResultWindowFails() throws IOException { final int window = 2; setMaxResultWindow(TEST_INDEX_PHRASE, 2); - assertThrows(ResponseException.class, - () -> executeQueryTemplate("SELECT * FROM %s", - TEST_INDEX_PHRASE, window + 1)); + assertThrows( + ResponseException.class, + () -> executeQueryTemplate("SELECT * FROM %s", TEST_INDEX_PHRASE, window + 1)); resetMaxResultWindow(TEST_INDEX_PHRASE); } - - } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/PositionFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/PositionFunctionIT.java index d0587eab7f..6a9d40e7c3 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/PositionFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/PositionFunctionIT.java @@ -26,19 +26,29 @@ protected void init() throws Exception { @Test public void position_function_test() { String query = "SELECT firstname, position('a' IN firstname) FROM %s"; - JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_PEOPLE2)); + JSONObject response = + executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_PEOPLE2)); - verifySchema(response, schema("firstname", null, "keyword"), - schema("position('a' IN firstname)", null, "integer")); + verifySchema( + response, + schema("firstname", null, "keyword"), + schema("position('a' IN firstname)", null, "integer")); assertEquals(12, response.getInt("total")); - verifyDataRows(response, - rows("Daenerys", 2), rows("Hattie", 2), - rows("Nanette", 2), rows("Dale", 2), - rows("Elinor", 0), rows("Virginia", 8), - rows("Dillard", 5), rows("Mcgee", 0), - rows("Aurelia", 7), rows("Fulton", 0), - rows("Burton", 0), rows("Josie", 0)); + verifyDataRows( + response, + rows("Daenerys", 2), + rows("Hattie", 2), + rows("Nanette", 2), + rows("Dale", 2), + rows("Elinor", 0), + rows("Virginia", 8), + rows("Dillard", 5), + rows("Mcgee", 0), + rows("Aurelia", 7), + rows("Fulton", 0), + rows("Burton", 0), + rows("Josie", 0)); } @Test @@ -46,20 +56,31 @@ public void position_function_with_nulls_test() { String query = "SELECT str2, position('ee' IN str2) FROM %s"; JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_CALCS)); - verifySchema(response, schema("str2", null, "keyword"), - schema("position('ee' IN str2)", null, "integer")); + verifySchema( + response, + schema("str2", null, "keyword"), + schema("position('ee' IN str2)", null, "integer")); assertEquals(17, response.getInt("total")); - verifyDataRows(response, - rows("one", 0), rows("two", 0), - rows("three", 4), rows(null, null), - rows("five", 0), rows("six", 0), - rows(null, null), rows("eight", 0), - rows("nine", 0), rows("ten", 0), - rows("eleven", 0), rows("twelve", 0), - rows(null, null), rows("fourteen", 6), - rows("fifteen", 5), rows("sixteen", 5), - rows(null, null)); + verifyDataRows( + response, + rows("one", 0), + rows("two", 0), + rows("three", 4), + rows(null, null), + rows("five", 0), + rows("six", 0), + rows(null, null), + rows("eight", 0), + rows("nine", 0), + rows("ten", 0), + rows("eleven", 0), + rows("twelve", 0), + rows(null, null), + rows("fourteen", 6), + rows("fifteen", 5), + rows("sixteen", 5), + rows(null, null)); } @Test @@ -86,7 +107,8 @@ public void position_function_with_only_fields_as_args_test() { @Test public void position_function_with_function_as_arg_test() { - String query = "SELECT position(upper(str3) IN str1) FROM %s WHERE str1 LIKE 'BINDING SUPPLIES'"; + String query = + "SELECT position(upper(str3) IN str1) FROM %s WHERE str1 LIKE 'BINDING SUPPLIES'"; JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_CALCS)); verifySchema(response, schema("position(upper(str3) IN str1)", null, "integer")); @@ -110,17 +132,21 @@ public void position_function_in_where_clause_test() { public void position_function_with_null_args_test() { String query1 = "SELECT str2, position(null IN str2) FROM %s WHERE str2 IN ('one')"; String query2 = "SELECT str2, position(str2 IN null) FROM %s WHERE str2 IN ('one')"; - JSONObject response1 = executeJdbcRequest(String.format(query1, TestsConstants.TEST_INDEX_CALCS)); - JSONObject response2 = executeJdbcRequest(String.format(query2, TestsConstants.TEST_INDEX_CALCS)); - - verifySchema(response1, - schema("str2", null, "keyword"), - schema("position(null IN str2)", null, "integer")); + JSONObject response1 = + executeJdbcRequest(String.format(query1, TestsConstants.TEST_INDEX_CALCS)); + JSONObject response2 = + executeJdbcRequest(String.format(query2, TestsConstants.TEST_INDEX_CALCS)); + + verifySchema( + response1, + schema("str2", null, "keyword"), + schema("position(null IN str2)", null, "integer")); assertEquals(1, response1.getInt("total")); - verifySchema(response2, - schema("str2", null, "keyword"), - schema("position(str2 IN null)", null, "integer")); + verifySchema( + response2, + schema("str2", null, "keyword"), + schema("position(str2 IN null)", null, "integer")); assertEquals(1, response2.getInt("total")); verifyDataRows(response1, rows("one", null)); diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/PreparedStatementIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/PreparedStatementIT.java index 38ff32b0d7..8200f64b66 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/PreparedStatementIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/PreparedStatementIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.sql; import org.json.JSONObject; @@ -20,16 +19,21 @@ protected void init() throws Exception { @Test public void testPreparedStatement() { - JSONObject response = new JSONObject( - executeQuery(String.format("{\n" - + " \"query\": \"SELECT state FROM %s WHERE state = ? GROUP BY state\",\n" - + " \"parameters\": [\n" - + " {\n" - + " \"type\": \"string\",\n" - + " \"value\": \"WA\"\n" - + " }\n" - + " ]\n" - + "}", TestsConstants.TEST_INDEX_ACCOUNT), "jdbc")); + JSONObject response = + new JSONObject( + executeQuery( + String.format( + "{\n" + + " \"query\": \"SELECT state FROM %s WHERE state = ? GROUP BY state\",\n" + + " \"parameters\": [\n" + + " {\n" + + " \"type\": \"string\",\n" + + " \"value\": \"WA\"\n" + + " }\n" + + " ]\n" + + "}", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc")); assertFalse(response.getJSONArray("datarows").isEmpty()); } @@ -39,5 +43,4 @@ protected String makeRequest(String query) { // Avoid wrap with "query" again return query; } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/QueryIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/QueryIT.java index e61593eb21..fd8066ea41 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/QueryIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/QueryIT.java @@ -13,72 +13,80 @@ import org.opensearch.sql.legacy.SQLIntegTestCase; public class QueryIT extends SQLIntegTestCase { - @Override - public void init() throws IOException { - loadIndex(Index.BEER); - } + @Override + public void init() throws IOException { + loadIndex(Index.BEER); + } - @Test - public void all_fields_test() throws IOException { - String query = "SELECT * FROM " - + TEST_INDEX_BEER + " WHERE query('*:taste')"; - JSONObject result = executeJdbcRequest(query); - assertEquals(16, result.getInt("total")); - } + @Test + public void all_fields_test() throws IOException { + String query = "SELECT * FROM " + TEST_INDEX_BEER + " WHERE query('*:taste')"; + JSONObject result = executeJdbcRequest(query); + assertEquals(16, result.getInt("total")); + } - @Test - public void mandatory_params_test() throws IOException { - String query = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query('Tags:taste OR Body:taste')"; - JSONObject result = executeJdbcRequest(query); - assertEquals(16, result.getInt("total")); - } + @Test + public void mandatory_params_test() throws IOException { + String query = "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query('Tags:taste OR Body:taste')"; + JSONObject result = executeJdbcRequest(query); + assertEquals(16, result.getInt("total")); + } - @Test - public void all_params_test() throws IOException { - String query = "SELECT Id FROM " + TEST_INDEX_BEER - + " WHERE query('Tags:taste', escape=false," - + "allow_leading_wildcard=true, enable_position_increments=true," - + "fuzziness= 1, fuzzy_rewrite='constant_score', max_determinized_states = 10000," - + "analyzer='standard', analyze_wildcard = false, quote_field_suffix = '.exact'," - + "auto_generate_synonyms_phrase_query=true, boost = 0.77," - + "quote_analyzer='standard', phrase_slop=0, rewrite='constant_score', type='best_fields'," - + "tie_breaker=0.3, time_zone='Canada/Pacific', default_operator='or'," - + "fuzzy_transpositions = false, lenient = true, fuzzy_max_expansions = 25," - + "minimum_should_match = '2<-25% 9<-3', fuzzy_prefix_length = 7);"; - JSONObject result = executeJdbcRequest(query); - assertEquals(8, result.getInt("total")); - } + @Test + public void all_params_test() throws IOException { + String query = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE query('Tags:taste'," + + " escape=false,allow_leading_wildcard=true," + + " enable_position_increments=true," + + " fuzziness= 1," + + " fuzzy_rewrite='constant_score'," + + " max_determinized_states = 10000," + + " analyzer='standard'," + + " analyze_wildcard = false," + + " quote_field_suffix = '.exact'," + + " auto_generate_synonyms_phrase_query=true," + + " boost = 0.77," + + " quote_analyzer='standard'," + + " phrase_slop=0," + + " rewrite='constant_score'," + + " type='best_fields'," + + " tie_breaker=0.3," + + " time_zone='Canada/Pacific'," + + " default_operator='or'," + + " fuzzy_transpositions = false," + + " lenient = true," + + " fuzzy_max_expansions = 25," + + " minimum_should_match = '2<-25% 9<-3'," + + " fuzzy_prefix_length = 7);"; + JSONObject result = executeJdbcRequest(query); + assertEquals(8, result.getInt("total")); + } - @Test - public void wildcard_test() throws IOException { - String query1 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query('Tags:taste')"; - JSONObject result1 = executeJdbcRequest(query1); - String query2 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query('*:taste')"; - JSONObject result2 = executeJdbcRequest(query2); - assertNotEquals(result2.getInt("total"), result1.getInt("total")); + @Test + public void wildcard_test() throws IOException { + String query1 = "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query('Tags:taste')"; + JSONObject result1 = executeJdbcRequest(query1); + String query2 = "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query('*:taste')"; + JSONObject result2 = executeJdbcRequest(query2); + assertNotEquals(result2.getInt("total"), result1.getInt("total")); - String query3 = "SELECT Id FROM " + TEST_INDEX_BEER - + " WHERE query('Tags:tas*');"; - JSONObject result3 = executeJdbcRequest(query3); - assertEquals(8, result3.getInt("total")); + String query3 = "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query('Tags:tas*');"; + JSONObject result3 = executeJdbcRequest(query3); + assertEquals(8, result3.getInt("total")); - String query4 = "SELECT Id FROM " + TEST_INDEX_BEER - + " WHERE query('Tags:tas?e');"; - JSONObject result4 = executeJdbcRequest(query3); - assertEquals(8, result4.getInt("total")); - } + String query4 = "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query('Tags:tas?e');"; + JSONObject result4 = executeJdbcRequest(query3); + assertEquals(8, result4.getInt("total")); + } - @Test - public void query_string_and_query_return_the_same_results_test() throws IOException { - String query1 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query('Tags:taste')"; - JSONObject result1 = executeJdbcRequest(query1); - String query2 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query_string(['Tags'],'taste')"; - JSONObject result2 = executeJdbcRequest(query2); - assertEquals(result2.getInt("total"), result1.getInt("total")); - } + @Test + public void query_string_and_query_return_the_same_results_test() throws IOException { + String query1 = "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query('Tags:taste')"; + JSONObject result1 = executeJdbcRequest(query1); + String query2 = "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query_string(['Tags'],'taste')"; + JSONObject result2 = executeJdbcRequest(query2); + assertEquals(result2.getInt("total"), result1.getInt("total")); + } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/QueryStringIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/QueryStringIT.java index 348889a0cc..3d4e08b4cd 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/QueryStringIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/QueryStringIT.java @@ -20,48 +20,65 @@ public void init() throws IOException { @Test public void all_fields_test() throws IOException { - String query = "SELECT * FROM " - + TEST_INDEX_BEER + " WHERE query_string(['*'], 'taste')"; + String query = "SELECT * FROM " + TEST_INDEX_BEER + " WHERE query_string(['*'], 'taste')"; JSONObject result = executeJdbcRequest(query); assertEquals(16, result.getInt("total")); } @Test public void mandatory_params_test() throws IOException { - String query = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query_string([\\\"Tags\\\" ^ 1.5, Title, 'Body' 4.2], 'taste')"; + String query = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE query_string([\\\"Tags\\\" ^ 1.5, Title, 'Body' 4.2], 'taste')"; JSONObject result = executeJdbcRequest(query); assertEquals(16, result.getInt("total")); } @Test public void all_params_test() throws IOException { - String query = "SELECT Id FROM " + TEST_INDEX_BEER - + " WHERE query_string(['Body', Tags, Title], 'taste beer', escape=false," - + "allow_leading_wildcard=true, enable_position_increments=true," - + "fuzziness= 1, fuzzy_rewrite='constant_score', max_determinized_states = 10000," - + "analyzer='english', analyze_wildcard = false, quote_field_suffix = '.exact'," - + "auto_generate_synonyms_phrase_query=true, boost = 0.77," - + "quote_analyzer='standard', phrase_slop=0, rewrite='constant_score', type='best_fields'," - + "tie_breaker=0.3, time_zone='Canada/Pacific', default_operator='or'," - + "fuzzy_transpositions = false, lenient = true, fuzzy_max_expansions = 25," - + "minimum_should_match = '2<-25% 9<-3', fuzzy_prefix_length = 7);"; + String query = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE query_string(['Body', Tags, Title]," + + " 'taste beer'," + + " escape=false," + + " allow_leading_wildcard=true," + + " enable_position_increments=true," + + " fuzziness= 1," + + " fuzzy_rewrite='constant_score'," + + " max_determinized_states = 10000," + + " analyzer='english'," + + " analyze_wildcard = false," + + " quote_field_suffix = '.exact'," + + " auto_generate_synonyms_phrase_query=true," + + " boost = 0.77," + + " quote_analyzer='standard'," + + " phrase_slop=0," + + " rewrite='constant_score'," + + " type='best_fields'," + + " tie_breaker=0.3," + + " time_zone='Canada/Pacific'," + + " default_operator='or'," + + " fuzzy_transpositions = false," + + " lenient = true," + + " fuzzy_max_expansions = 25," + + " minimum_should_match = '2<-25% 9<-3'," + + " fuzzy_prefix_length = 7);"; JSONObject result = executeJdbcRequest(query); assertEquals(49, result.getInt("total")); } @Test public void wildcard_test() throws IOException { - String query1 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query_string(['Tags'], 'taste')"; + String query1 = "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query_string(['Tags'], 'taste')"; JSONObject result1 = executeJdbcRequest(query1); - String query2 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query_string(['T*'], 'taste')"; + String query2 = "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query_string(['T*'], 'taste')"; JSONObject result2 = executeJdbcRequest(query2); assertNotEquals(result2.getInt("total"), result1.getInt("total")); - String query3 = "SELECT Id FROM " + TEST_INDEX_BEER - + " WHERE query_string(['*Date'], '2014-01-22');"; + String query3 = + "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query_string(['*Date'], '2014-01-22');"; JSONObject result3 = executeJdbcRequest(query3); assertEquals(10, result3.getInt("total")); } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/QueryValidationIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/QueryValidationIT.java index 5a16cd3f64..e42b68631f 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/QueryValidationIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/QueryValidationIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.sql; import static org.hamcrest.Matchers.is; @@ -25,28 +24,29 @@ import org.opensearch.sql.legacy.SQLIntegTestCase; /** - * The query validation IT only covers test for error cases that not doable in comparison test. - * For all other tests, comparison test should be favored over manual written test like this. + * The query validation IT only covers test for error cases that not doable in comparison test. For + * all other tests, comparison test should be favored over manual written test like this. */ public class QueryValidationIT extends SQLIntegTestCase { - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); + @Rule public ExpectedException exceptionRule = ExpectedException.none(); @Override protected void init() throws Exception { loadIndex(Index.ACCOUNT); } - @Ignore("Will add this validation in analyzer later. This test should be enabled once " + - "https://github.com/opensearch-project/sql/issues/910 has been resolved") + @Ignore( + "Will add this validation in analyzer later. This test should be enabled once " + + "https://github.com/opensearch-project/sql/issues/910 has been resolved") @Test public void testNonAggregatedSelectColumnMissingInGroupByClause() throws IOException { expectResponseException() .hasStatusCode(BAD_REQUEST) .hasErrorType("SemanticCheckException") - .containsMessage("Expression [state] that contains non-aggregated column " - + "is not present in group by clause") + .containsMessage( + "Expression [state] that contains non-aggregated column " + + "is not present in group by clause") .whenExecute("SELECT state FROM opensearch-sql_test_index_account GROUP BY age"); } @@ -55,8 +55,9 @@ public void testNonAggregatedSelectColumnPresentWithoutGroupByClause() throws IO expectResponseException() .hasStatusCode(BAD_REQUEST) .hasErrorType("SemanticCheckException") - .containsMessage("Explicit GROUP BY clause is required because expression [state] " - + "contains non-aggregated column") + .containsMessage( + "Explicit GROUP BY clause is required because expression [state] " + + "contains non-aggregated column") .whenExecute("SELECT state, AVG(age) FROM opensearch-sql_test_index_account"); } @@ -87,8 +88,7 @@ public ResponseExceptionAssertion expectResponseException() { /** * Response exception assertion helper to assert property value in OpenSearch ResponseException - * and Response inside. This serves as syntax sugar to improve the readability of test - * code. + * and Response inside. This serves as syntax sugar to improve the readability of test code. */ private static class ResponseExceptionAssertion { private final ExpectedException exceptionRule; @@ -100,9 +100,12 @@ private ResponseExceptionAssertion(ExpectedException exceptionRule) { } ResponseExceptionAssertion hasStatusCode(RestStatus code) { - exceptionRule.expect(featureValueOf("statusCode", is(code), - (Function) e -> - RestStatus.fromCode(e.getResponse().getStatusLine().getStatusCode()))); + exceptionRule.expect( + featureValueOf( + "statusCode", + is(code), + (Function) + e -> RestStatus.fromCode(e.getResponse().getStatusLine().getStatusCode()))); return this; } @@ -133,5 +136,4 @@ private static void execute(String query) throws IOException { client().performRequest(request); } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/RawFormatIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/RawFormatIT.java index eb693a4718..9d2861ce98 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/RawFormatIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/RawFormatIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.sql; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_RAW_SANITIZE; @@ -26,21 +25,27 @@ public void init() throws IOException { @Test public void rawFormatWithPipeFieldTest() { - String result = executeQuery( - String.format(Locale.ROOT, "SELECT firstname, lastname FROM %s", TEST_INDEX_BANK_RAW_SANITIZE), "raw"); - assertEquals(StringUtils.format( - "firstname|lastname%n" - + "+Amber JOHnny|Duke Willmington+%n" - + "-Hattie|Bond-%n" - + "=Nanette|Bates=%n" - + "@Dale|Adams@%n" - + "@Elinor|\"Ratliff|||\"%n"), + String result = + executeQuery( + String.format( + Locale.ROOT, "SELECT firstname, lastname FROM %s", TEST_INDEX_BANK_RAW_SANITIZE), + "raw"); + assertEquals( + StringUtils.format( + "firstname|lastname%n" + + "+Amber JOHnny|Duke Willmington+%n" + + "-Hattie|Bond-%n" + + "=Nanette|Bates=%n" + + "@Dale|Adams@%n" + + "@Elinor|\"Ratliff|||\"%n"), result); } @Test public void contentHeaderTest() throws IOException { - String query = String.format(Locale.ROOT, "SELECT firstname, lastname FROM %s", TEST_INDEX_BANK_RAW_SANITIZE); + String query = + String.format( + Locale.ROOT, "SELECT firstname, lastname FROM %s", TEST_INDEX_BANK_RAW_SANITIZE); String requestBody = makeRequest(query); Request sqlRequest = new Request("POST", "/_plugins/_sql?format=raw"); diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/RelevanceFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/RelevanceFunctionIT.java index 26fe735f12..755493c167 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/RelevanceFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/RelevanceFunctionIT.java @@ -24,11 +24,15 @@ public void init() throws IOException { */ @Test public void verify_flags_in_simple_query_string() throws IOException { - String query1 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE simple_query_string(['Body'], '-free', flags='NONE|PREFIX|ESCAPE')"; + String query1 = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE simple_query_string(['Body'], '-free', flags='NONE|PREFIX|ESCAPE')"; var result1 = new JSONObject(executeQuery(query1, "jdbc")); - String query2 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE simple_query_string([Body], '-free', flags='NOT|AND|OR')"; + String query2 = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE simple_query_string([Body], '-free', flags='NOT|AND|OR')"; var result2 = new JSONObject(executeQuery(query2, "jdbc")); assertNotEquals(result2.getInt("total"), result1.getInt("total")); @@ -44,11 +48,11 @@ public void verify_flags_in_simple_query_string() throws IOException { */ @Test public void verify_escape_in_query_string() throws IOException { - String query1 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query_string([Title], '?', escape=true);"; + String query1 = + "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query_string([Title], '?', escape=true);"; var result1 = new JSONObject(executeQuery(query1, "jdbc")); - String query2 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query_string([Title], '?', escape=false);"; + String query2 = + "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE query_string([Title], '?', escape=false);"; var result2 = new JSONObject(executeQuery(query2, "jdbc")); assertEquals(0, result1.getInt("total")); assertEquals(8, result2.getInt("total")); @@ -61,11 +65,15 @@ public void verify_escape_in_query_string() throws IOException { */ @Test public void verify_default_operator_in_query_string() throws IOException { - String query1 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query_string([Title], 'beer taste', default_operator='OR')"; + String query1 = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE query_string([Title], 'beer taste', default_operator='OR')"; var result1 = new JSONObject(executeQuery(query1, "jdbc")); - String query2 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE query_string([Title], 'beer taste', default_operator='AND')"; + String query2 = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE query_string([Title], 'beer taste', default_operator='AND')"; var result2 = new JSONObject(executeQuery(query2, "jdbc")); assertEquals(16, result1.getInt("total")); assertEquals(4, result2.getInt("total")); @@ -73,11 +81,15 @@ public void verify_default_operator_in_query_string() throws IOException { @Test public void verify_default_operator_in_simple_query_string() throws IOException { - String query1 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE simple_query_string([Title], 'beer taste', default_operator='OR')"; + String query1 = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE simple_query_string([Title], 'beer taste', default_operator='OR')"; var result1 = new JSONObject(executeQuery(query1, "jdbc")); - String query2 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE simple_query_string([Title], 'beer taste', default_operator='AND')"; + String query2 = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE simple_query_string([Title], 'beer taste', default_operator='AND')"; var result2 = new JSONObject(executeQuery(query2, "jdbc")); assertEquals(16, result1.getInt("total")); assertEquals(4, result2.getInt("total")); @@ -85,11 +97,15 @@ public void verify_default_operator_in_simple_query_string() throws IOException @Test public void verify_default_operator_in_multi_match() throws IOException { - String query1 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE multi_match([Title], 'beer taste', operator='OR')"; + String query1 = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE multi_match([Title], 'beer taste', operator='OR')"; var result1 = new JSONObject(executeQuery(query1, "jdbc")); - String query2 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE multi_match([Title], 'beer taste', operator='AND')"; + String query2 = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE multi_match([Title], 'beer taste', operator='AND')"; var result2 = new JSONObject(executeQuery(query2, "jdbc")); assertEquals(16, result1.getInt("total")); assertEquals(4, result2.getInt("total")); @@ -97,11 +113,11 @@ public void verify_default_operator_in_multi_match() throws IOException { @Test public void verify_operator_in_match() throws IOException { - String query1 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE match(Title, 'beer taste', operator='OR')"; + String query1 = + "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE match(Title, 'beer taste', operator='OR')"; var result1 = new JSONObject(executeQuery(query1, "jdbc")); - String query2 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE match(Title, 'beer taste', operator='AND')"; + String query2 = + "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE match(Title, 'beer taste', operator='AND')"; var result2 = new JSONObject(executeQuery(query2, "jdbc")); assertEquals(16, result1.getInt("total")); assertEquals(4, result2.getInt("total")); diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/SQLCorrectnessIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/SQLCorrectnessIT.java index 30f23547ec..6056a1c416 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/SQLCorrectnessIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/SQLCorrectnessIT.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.sql; import com.google.common.io.Resources; @@ -14,14 +13,12 @@ import java.util.function.Function; import org.junit.Test; -/** - * SQL integration test automated by comparison test framework. - */ +/** SQL integration test automated by comparison test framework. */ public class SQLCorrectnessIT extends CorrectnessTestBase { private static final String ROOT_DIR = "correctness/"; - private static final String[] EXPR_TEST_DIR = { "expressions" }; - private static final String[] QUERY_TEST_DIR = { "queries", "bugfixes" }; + private static final String[] EXPR_TEST_DIR = {"expressions"}; + private static final String[] QUERY_TEST_DIR = {"queries", "bugfixes"}; @Override protected void init() throws Exception { @@ -35,32 +32,30 @@ public void runAllTests() throws Exception { } /** - * Verify queries in files in directories with a converter to preprocess query. - * For example, for expressions it is converted to a SELECT clause before testing. + * Verify queries in files in directories with a converter to preprocess query. For example, for + * expressions it is converted to a SELECT clause before testing. */ @SuppressWarnings("UnstableApiUsage") private void verifyQueries(String[] dirs, Function converter) throws Exception { for (String dir : dirs) { Path dirPath = Paths.get(Resources.getResource(ROOT_DIR + dir).toURI()); Files.walk(dirPath) - .filter(Files::isRegularFile) - .forEach(file -> verifyQueries(file, converter)); + .filter(Files::isRegularFile) + .forEach(file -> verifyQueries(file, converter)); } } - /** - * Comment start with # - */ + /** Comment start with # */ private void verifyQueries(Path file, Function converter) { try { - String[] queries = Files.lines(file) - .filter(line -> !line.startsWith("#")) - .map(converter) - .toArray(String[]::new); + String[] queries = + Files.lines(file) + .filter(line -> !line.startsWith("#")) + .map(converter) + .toArray(String[]::new); verify(queries); } catch (IOException e) { throw new IllegalStateException("Failed to read file: " + file, e); } } - } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/ScoreQueryIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/ScoreQueryIT.java index e824b1ab2b..fdd35c47eb 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/ScoreQueryIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/ScoreQueryIT.java @@ -26,6 +26,7 @@ protected void init() throws Exception { } /** + *
    * "query" : {
    *   "from": 0,
    *   "size": 3,
@@ -83,18 +84,24 @@ protected void init() throws Exception {
    *   ],
    *   "track_scores": true
    * }
+   * 
* @throws IOException */ @Test public void scoreQueryExplainTest() throws IOException { - final String result = explainQuery(String.format(Locale.ROOT, - "select address from %s " + - "where score(matchQuery(address, 'Douglass'), 100) " + - "or score(matchQuery(address, 'Hall'), 0.5) order by _score desc limit 2", - TestsConstants.TEST_INDEX_ACCOUNT)); - Assert.assertThat(result, containsString("\\\"match\\\":{\\\"address\\\":{\\\"query\\\":\\\"Douglass\\\"")); + final String result = + explainQuery( + String.format( + Locale.ROOT, + "select address from %s " + + "where score(matchQuery(address, 'Douglass'), 100) " + + "or score(matchQuery(address, 'Hall'), 0.5) order by _score desc limit 2", + TestsConstants.TEST_INDEX_ACCOUNT)); + Assert.assertThat( + result, containsString("\\\"match\\\":{\\\"address\\\":{\\\"query\\\":\\\"Douglass\\\"")); Assert.assertThat(result, containsString("\\\"boost\\\":100.0")); - Assert.assertThat(result, containsString("\\\"match\\\":{\\\"address\\\":{\\\"query\\\":\\\"Hall\\\"")); + Assert.assertThat( + result, containsString("\\\"match\\\":{\\\"address\\\":{\\\"query\\\":\\\"Hall\\\"")); Assert.assertThat(result, containsString("\\\"boost\\\":0.5")); Assert.assertThat(result, containsString("\\\"sort\\\":[{\\\"_score\\\"")); Assert.assertThat(result, containsString("\\\"track_scores\\\":true")); @@ -102,26 +109,32 @@ public void scoreQueryExplainTest() throws IOException { @Test public void scoreQueryTest() throws IOException { - final JSONObject result = new JSONObject(executeQuery(String.format(Locale.ROOT, - "select address, _score from %s " + - "where score(matchQuery(address, 'Douglass'), 100) " + - "or score(matchQuery(address, 'Hall'), 0.5) order by _score desc limit 2", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc")); - verifySchema(result, - schema("address", null, "text"), - schema("_score", null, "float")); - verifyDataRows(result, - rows("154 Douglass Street", 650.1515), - rows("565 Hall Street", 3.2507575)); + final JSONObject result = + new JSONObject( + executeQuery( + String.format( + Locale.ROOT, + "select address, _score from %s " + + "where score(matchQuery(address, 'Douglass'), 100) " + + "or score(matchQuery(address, 'Hall'), 0.5) order by _score desc limit 2", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc")); + verifySchema(result, schema("address", null, "text"), schema("_score", null, "float")); + verifyDataRows( + result, rows("154 Douglass Street", 650.1515), rows("565 Hall Street", 3.2507575)); } @Test public void scoreQueryDefaultBoostExplainTest() throws IOException { - final String result = explainQuery(String.format(Locale.ROOT, - "select address from %s " + - "where score(matchQuery(address, 'Lane')) order by _score desc limit 2", - TestsConstants.TEST_INDEX_ACCOUNT)); - Assert.assertThat(result, containsString("\\\"match\\\":{\\\"address\\\":{\\\"query\\\":\\\"Lane\\\"")); + final String result = + explainQuery( + String.format( + Locale.ROOT, + "select address from %s " + + "where score(matchQuery(address, 'Lane')) order by _score desc limit 2", + TestsConstants.TEST_INDEX_ACCOUNT)); + Assert.assertThat( + result, containsString("\\\"match\\\":{\\\"address\\\":{\\\"query\\\":\\\"Lane\\\"")); Assert.assertThat(result, containsString("\\\"boost\\\":1.0")); Assert.assertThat(result, containsString("\\\"sort\\\":[{\\\"_score\\\"")); Assert.assertThat(result, containsString("\\\"track_scores\\\":true")); @@ -129,13 +142,16 @@ public void scoreQueryDefaultBoostExplainTest() throws IOException { @Test public void scoreQueryDefaultBoostQueryTest() throws IOException { - final JSONObject result = new JSONObject(executeQuery(String.format(Locale.ROOT, - "select address, _score from %s " + - "where score(matchQuery(address, 'Powell')) order by _score desc limit 2", - TestsConstants.TEST_INDEX_ACCOUNT), "jdbc")); - verifySchema(result, - schema("address", null, "text"), - schema("_score", null, "float")); + final JSONObject result = + new JSONObject( + executeQuery( + String.format( + Locale.ROOT, + "select address, _score from %s " + + "where score(matchQuery(address, 'Powell')) order by _score desc limit 2", + TestsConstants.TEST_INDEX_ACCOUNT), + "jdbc")); + verifySchema(result, schema("address", null, "text"), schema("_score", null, "float")); verifyDataRows(result, rows("305 Powell Street", 6.501515)); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java index 44f4e5ca9c..8742dedbc7 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java @@ -31,43 +31,60 @@ public void init() throws IOException { @Test public void test_mandatory_params() throws IOException { - String query = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE simple_query_string([\\\"Tags\\\" ^ 1.5, Title, 'Body' 4.2], 'taste')"; + String query = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE simple_query_string([\\\"Tags\\\" ^ 1.5, Title, 'Body' 4.2], 'taste')"; var result = new JSONObject(executeQuery(query, "jdbc")); assertEquals(16, result.getInt("total")); } @Test public void test_all_params() throws IOException { - String query = "SELECT Id FROM " + TEST_INDEX_BEER - + " WHERE simple_query_string(['Body', Tags, Title], 'taste beer', default_operator='or'," - + "analyzer=english, analyze_wildcard = false, quote_field_suffix = '.exact'," - + "auto_generate_synonyms_phrase_query=true, boost = 0.77, flags='PREFIX'," - + "fuzzy_transpositions = false, lenient = true, fuzzy_max_expansions = 25," - + "minimum_should_match = '2<-25% 9<-3', fuzzy_prefix_length = 7);"; + String query = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE simple_query_string(['Body', Tags, Title], " + + "'taste beer'," + + " default_operator='or'," + + " analyzer=english, " + + " analyze_wildcard = false," + + " quote_field_suffix = '.exact'," + + " auto_generate_synonyms_phrase_query=true," + + " boost = 0.77," + + " flags='PREFIX'," + + " fuzzy_transpositions = false," + + " lenient = true," + + " fuzzy_max_expansions = 25," + + " minimum_should_match = '2<-25% 9<-3'," + + " fuzzy_prefix_length = 7);"; var result = new JSONObject(executeQuery(query, "jdbc")); assertEquals(49, result.getInt("total")); } @Test public void verify_wildcard_test() throws IOException { - String query1 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE simple_query_string(['Tags'], 'taste')"; + String query1 = + "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE simple_query_string(['Tags'], 'taste')"; var result1 = new JSONObject(executeQuery(query1, "jdbc")); - String query2 = "SELECT Id FROM " - + TEST_INDEX_BEER + " WHERE simple_query_string(['T*'], 'taste')"; + String query2 = + "SELECT Id FROM " + TEST_INDEX_BEER + " WHERE simple_query_string(['T*'], 'taste')"; var result2 = new JSONObject(executeQuery(query2, "jdbc")); assertNotEquals(result2.getInt("total"), result1.getInt("total")); - String query = "SELECT Id FROM " + TEST_INDEX_BEER - + " WHERE simple_query_string(['*Date'], '2014-01-22');"; + String query = + "SELECT Id FROM " + + TEST_INDEX_BEER + + " WHERE simple_query_string(['*Date'], '2014-01-22');"; var result = new JSONObject(executeQuery(query, "jdbc")); assertEquals(10, result.getInt("total")); } @Test public void contentHeaderTest() throws IOException { - String query = "SELECT Id FROM " + TEST_INDEX_BEER + String query = + "SELECT Id FROM " + + TEST_INDEX_BEER + " WHERE simple_query_string([\\\"Tags\\\" ^ 1.5, Title, 'Body' 4.2], 'taste')"; String requestBody = makeRequest(query); From 0dfbc892c3f6f34914661122dcaf04a6a703949f Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Wed, 16 Aug 2023 15:35:35 -0700 Subject: [PATCH 2/2] [Spotless] Applying Google Code Format for prometheus files #13 (#340) (#1973) * added ignorefailures * Spotless apply on Prometheus --------- Signed-off-by: Mitchell Gale Signed-off-by: Mitchell Gale --- build.gradle | 1 + prometheus/build.gradle | 3 + .../client/PrometheusClientImpl.java | 66 +- .../constants/PrometheusFieldConstants.java | 4 +- .../QueryExemplarFunctionImplementation.java | 73 +- .../QueryRangeFunctionImplementation.java | 79 +- .../QueryExemplarsTableFunctionResolver.java | 22 +- .../QueryRangeTableFunctionResolver.java | 14 +- .../PrometheusFunctionResponseHandle.java | 12 +- .../QueryExemplarsFunctionResponseHandle.java | 22 +- .../QueryRangeFunctionResponseHandle.java | 27 +- ...ueryExemplarsFunctionTableScanBuilder.java | 9 +- ...eryExemplarsFunctionTableScanOperator.java | 42 +- .../QueryRangeFunctionTableScanBuilder.java | 5 +- .../QueryRangeFunctionTableScanOperator.java | 40 +- .../logical/PrometheusLogicalMetricAgg.java | 41 +- .../logical/PrometheusLogicalMetricScan.java | 12 +- ...PrometheusLogicalPlanOptimizerFactory.java | 17 +- .../logical/rules/MergeAggAndIndexScan.java | 21 +- .../logical/rules/MergeAggAndRelation.java | 20 +- .../logical/rules/MergeFilterAndRelation.java | 20 +- .../PrometheusQueryExemplarsRequest.java | 18 +- .../request/PrometheusQueryRequest.java | 22 +- .../PrometheusDescribeMetricRequest.java | 71 +- .../system/PrometheusListMetricsRequest.java | 45 +- .../system/PrometheusSystemRequest.java | 5 +- .../response/PrometheusResponse.java | 32 +- .../PrometheusMetricDefaultSchema.java | 11 +- .../storage/PrometheusMetricScan.java | 43 +- .../storage/PrometheusMetricTable.java | 49 +- .../storage/PrometheusStorageEngine.java | 15 +- .../storage/PrometheusStorageFactory.java | 73 +- .../storage/QueryExemplarsTable.java | 15 +- .../PrometheusDefaultImplementor.java | 76 +- .../model/PrometheusResponseFieldNames.java | 2 - .../storage/model/QueryRangeParameters.java | 1 - .../querybuilder/AggregationQueryBuilder.java | 45 +- .../SeriesSelectionQueryBuilder.java | 22 +- .../querybuilder/StepParameterResolver.java | 18 +- .../TimeRangeParametersResolver.java | 15 +- .../storage/system/PrometheusSystemTable.java | 22 +- .../system/PrometheusSystemTableScan.java | 7 +- .../system/PrometheusSystemTableSchema.java | 33 +- .../prometheus/utils/TableFunctionUtils.java | 52 +- .../client/PrometheusClientImplTest.java | 87 +- ...ryExemplarsFunctionImplementationTest.java | 68 +- .../QueryRangeFunctionImplementationTest.java | 80 +- ...eryExemplarsTableFunctionResolverTest.java | 37 +- .../QueryRangeTableFunctionResolverTest.java | 190 ++-- ...ExemplarsFunctionTableScanBuilderTest.java | 32 +- ...xemplarsFunctionTableScanOperatorTest.java | 103 +- ...ueryRangeFunctionTableScanBuilderTest.java | 23 +- ...eryRangeFunctionTableScanOperatorTest.java | 116 ++- .../logical/PrometheusLogicOptimizerTest.java | 63 +- .../PrometheusDescribeMetricRequestTest.java | 103 +- .../PrometheusListMetricsRequestTest.java | 44 +- .../storage/PrometheusMetricScanTest.java | 183 ++-- .../storage/PrometheusMetricTableTest.java | 889 +++++++++++------- .../storage/PrometheusStorageEngineTest.java | 26 +- .../storage/PrometheusStorageFactoryTest.java | 86 +- .../storage/QueryExemplarsTableTest.java | 13 +- .../StepParameterResolverTest.java | 6 +- .../TimeRangeParametersResolverTest.java | 8 +- .../system/PrometheusSystemTableScanTest.java | 3 +- .../system/PrometheusSystemTableTest.java | 46 +- .../prometheus/utils/LogicalPlanUtils.java | 42 +- .../sql/prometheus/utils/TestUtils.java | 2 +- 67 files changed, 1797 insertions(+), 1695 deletions(-) diff --git a/build.gradle b/build.gradle index a256fd1d22..290a1e1786 100644 --- a/build.gradle +++ b/build.gradle @@ -86,6 +86,7 @@ spotless { target fileTree('.') { include 'datasources/**/*.java', 'core/**/*.java', + 'prometheus/**/*.java', 'sql/**/*.java', 'common/**/*.java', 'ppl/**/*.java' diff --git a/prometheus/build.gradle b/prometheus/build.gradle index e98dfd83e4..0d915a6d4a 100644 --- a/prometheus/build.gradle +++ b/prometheus/build.gradle @@ -13,6 +13,9 @@ repositories { mavenCentral() } +checkstyleTest.ignoreFailures = true +checkstyleMain.ignoreFailures = true + dependencies { api project(':core') implementation project(':datasources') diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java index 9472be7487..2bfaaccd47 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java @@ -38,16 +38,18 @@ public PrometheusClientImpl(OkHttpClient okHttpClient, URI uri) { this.uri = uri; } - @Override public JSONObject queryRange(String query, Long start, Long end, String step) throws IOException { - String queryUrl = String.format("%s/api/v1/query_range?query=%s&start=%s&end=%s&step=%s", - uri.toString().replaceAll("/$", ""), URLEncoder.encode(query, StandardCharsets.UTF_8), - start, end, step); + String queryUrl = + String.format( + "%s/api/v1/query_range?query=%s&start=%s&end=%s&step=%s", + uri.toString().replaceAll("/$", ""), + URLEncoder.encode(query, StandardCharsets.UTF_8), + start, + end, + step); logger.debug("queryUrl: " + queryUrl); - Request request = new Request.Builder() - .url(queryUrl) - .build(); + Request request = new Request.Builder().url(queryUrl).build(); Response response = this.okHttpClient.newCall(request).execute(); JSONObject jsonObject = readResponse(response); return jsonObject.getJSONObject("data"); @@ -55,14 +57,14 @@ public JSONObject queryRange(String query, Long start, Long end, String step) th @Override public List getLabels(String metricName) throws IOException { - String queryUrl = String.format("%s/api/v1/labels?%s=%s", - uri.toString().replaceAll("/$", ""), - URLEncoder.encode("match[]", StandardCharsets.UTF_8), - URLEncoder.encode(metricName, StandardCharsets.UTF_8)); + String queryUrl = + String.format( + "%s/api/v1/labels?%s=%s", + uri.toString().replaceAll("/$", ""), + URLEncoder.encode("match[]", StandardCharsets.UTF_8), + URLEncoder.encode(metricName, StandardCharsets.UTF_8)); logger.debug("queryUrl: " + queryUrl); - Request request = new Request.Builder() - .url(queryUrl) - .build(); + Request request = new Request.Builder().url(queryUrl).build(); Response response = this.okHttpClient.newCall(request).execute(); JSONObject jsonObject = readResponse(response); return toListOfLabels(jsonObject.getJSONArray("data")); @@ -70,28 +72,26 @@ public List getLabels(String metricName) throws IOException { @Override public Map> getAllMetrics() throws IOException { - String queryUrl = String.format("%s/api/v1/metadata", - uri.toString().replaceAll("/$", "")); + String queryUrl = String.format("%s/api/v1/metadata", uri.toString().replaceAll("/$", "")); logger.debug("queryUrl: " + queryUrl); - Request request = new Request.Builder() - .url(queryUrl) - .build(); + Request request = new Request.Builder().url(queryUrl).build(); Response response = this.okHttpClient.newCall(request).execute(); JSONObject jsonObject = readResponse(response); - TypeReference>> typeRef - = new TypeReference<>() {}; + TypeReference>> typeRef = new TypeReference<>() {}; return new ObjectMapper().readValue(jsonObject.getJSONObject("data").toString(), typeRef); } @Override public JSONArray queryExemplars(String query, Long start, Long end) throws IOException { - String queryUrl = String.format("%s/api/v1/query_exemplars?query=%s&start=%s&end=%s", - uri.toString().replaceAll("/$", ""), URLEncoder.encode(query, StandardCharsets.UTF_8), - start, end); + String queryUrl = + String.format( + "%s/api/v1/query_exemplars?query=%s&start=%s&end=%s", + uri.toString().replaceAll("/$", ""), + URLEncoder.encode(query, StandardCharsets.UTF_8), + start, + end); logger.debug("queryUrl: " + queryUrl); - Request request = new Request.Builder() - .url(queryUrl) - .build(); + Request request = new Request.Builder().url(queryUrl).build(); Response response = this.okHttpClient.newCall(request).execute(); JSONObject jsonObject = readResponse(response); return jsonObject.getJSONArray("data"); @@ -100,8 +100,8 @@ public JSONArray queryExemplars(String query, Long start, Long end) throws IOExc private List toListOfLabels(JSONArray array) { List result = new ArrayList<>(); for (int i = 0; i < array.length(); i++) { - //__name__ is internal label in prometheus representing the metric name. - //Exempting this from labels list as it is not required in any of the operations. + // __name__ is internal label in prometheus representing the metric name. + // Exempting this from labels list as it is not required in any of the operations. if (!"__name__".equals(array.optString(i))) { result.add(array.optString(i)); } @@ -109,7 +109,6 @@ private List toListOfLabels(JSONArray array) { return result; } - private JSONObject readResponse(Response response) throws IOException { if (response.isSuccessful()) { JSONObject jsonObject = new JSONObject(Objects.requireNonNull(response.body()).string()); @@ -120,10 +119,9 @@ private JSONObject readResponse(Response response) throws IOException { } } else { throw new RuntimeException( - String.format("Request to Prometheus is Unsuccessful with : %s", Objects.requireNonNull( - response.body(), "Response body can't be null").string())); + String.format( + "Request to Prometheus is Unsuccessful with : %s", + Objects.requireNonNull(response.body(), "Response body can't be null").string())); } } - - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/data/constants/PrometheusFieldConstants.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/data/constants/PrometheusFieldConstants.java index 88e9df6a88..0f687b3cd1 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/data/constants/PrometheusFieldConstants.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/data/constants/PrometheusFieldConstants.java @@ -18,6 +18,6 @@ public class PrometheusFieldConstants { public static final String EXEMPLARS_KEY = "exemplars"; public static final String TRACE_ID_KEY = "traceID"; public static final String LABELS_KEY = "labels"; - public static final String TIMESTAMP_KEY = "timestamp"; - public static final String VALUE_KEY = "value"; + public static final String TIMESTAMP_KEY = "timestamp"; + public static final String VALUE_KEY = "value"; } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarFunctionImplementation.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarFunctionImplementation.java index 9d455b3cfc..bbd3a36f5f 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarFunctionImplementation.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarFunctionImplementation.java @@ -28,8 +28,8 @@ import org.opensearch.sql.prometheus.storage.QueryExemplarsTable; import org.opensearch.sql.storage.Table; -public class QueryExemplarFunctionImplementation extends FunctionExpression implements - TableFunctionImplementation { +public class QueryExemplarFunctionImplementation extends FunctionExpression + implements TableFunctionImplementation { private final FunctionName functionName; private final List arguments; @@ -39,10 +39,10 @@ public class QueryExemplarFunctionImplementation extends FunctionExpression impl * Required argument constructor. * * @param functionName name of the function - * @param arguments a list of arguments provided + * @param arguments a list of arguments provided */ - public QueryExemplarFunctionImplementation(FunctionName functionName, List arguments, - PrometheusClient prometheusClient) { + public QueryExemplarFunctionImplementation( + FunctionName functionName, List arguments, PrometheusClient prometheusClient) { super(functionName, arguments); this.functionName = functionName; this.arguments = arguments; @@ -51,10 +51,11 @@ public QueryExemplarFunctionImplementation(FunctionName functionName, List valueEnv) { - throw new UnsupportedOperationException(String.format( - "Prometheus defined function [%s] is only " - + "supported in SOURCE clause with prometheus connector catalog", - functionName)); + throw new UnsupportedOperationException( + String.format( + "Prometheus defined function [%s] is only " + + "supported in SOURCE clause with prometheus connector catalog", + functionName)); } @Override @@ -64,10 +65,15 @@ public ExprType type() { @Override public String toString() { - List args = arguments.stream() - .map(arg -> String.format("%s=%s", ((NamedArgumentExpression) arg) - .getArgName(), ((NamedArgumentExpression) arg).getValue().toString())) - .collect(Collectors.toList()); + List args = + arguments.stream() + .map( + arg -> + String.format( + "%s=%s", + ((NamedArgumentExpression) arg).getArgName(), + ((NamedArgumentExpression) arg).getValue().toString())) + .collect(Collectors.toList()); return String.format("%s(%s)", functionName, String.join(", ", args)); } @@ -79,27 +85,26 @@ public Table applyArguments() { private PrometheusQueryExemplarsRequest buildExemplarsQueryRequest(List arguments) { PrometheusQueryExemplarsRequest request = new PrometheusQueryExemplarsRequest(); - arguments.forEach(arg -> { - String argName = ((NamedArgumentExpression) arg).getArgName(); - Expression argValue = ((NamedArgumentExpression) arg).getValue(); - ExprValue literalValue = argValue.valueOf(); - switch (argName) { - case QUERY: - request - .setQuery((String) literalValue.value()); - break; - case STARTTIME: - request.setStartTime(((Number) literalValue.value()).longValue()); - break; - case ENDTIME: - request.setEndTime(((Number) literalValue.value()).longValue()); - break; - default: - throw new ExpressionEvaluationException( - String.format("Invalid Function Argument:%s", argName)); - } - }); + arguments.forEach( + arg -> { + String argName = ((NamedArgumentExpression) arg).getArgName(); + Expression argValue = ((NamedArgumentExpression) arg).getValue(); + ExprValue literalValue = argValue.valueOf(); + switch (argName) { + case QUERY: + request.setQuery((String) literalValue.value()); + break; + case STARTTIME: + request.setStartTime(((Number) literalValue.value()).longValue()); + break; + case ENDTIME: + request.setEndTime(((Number) literalValue.value()).longValue()); + break; + default: + throw new ExpressionEvaluationException( + String.format("Invalid Function Argument:%s", argName)); + } + }); return request; } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementation.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementation.java index 2d3710037a..0719bd1525 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementation.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementation.java @@ -29,8 +29,8 @@ import org.opensearch.sql.prometheus.storage.PrometheusMetricTable; import org.opensearch.sql.storage.Table; -public class QueryRangeFunctionImplementation extends FunctionExpression implements - TableFunctionImplementation { +public class QueryRangeFunctionImplementation extends FunctionExpression + implements TableFunctionImplementation { private final FunctionName functionName; private final List arguments; @@ -40,10 +40,10 @@ public class QueryRangeFunctionImplementation extends FunctionExpression impleme * Required argument constructor. * * @param functionName name of the function - * @param arguments a list of expressions + * @param arguments a list of expressions */ - public QueryRangeFunctionImplementation(FunctionName functionName, List arguments, - PrometheusClient prometheusClient) { + public QueryRangeFunctionImplementation( + FunctionName functionName, List arguments, PrometheusClient prometheusClient) { super(functionName, arguments); this.functionName = functionName; this.arguments = arguments; @@ -52,10 +52,11 @@ public QueryRangeFunctionImplementation(FunctionName functionName, List valueEnv) { - throw new UnsupportedOperationException(String.format( - "Prometheus defined function [%s] is only " - + "supported in SOURCE clause with prometheus connector catalog", - functionName)); + throw new UnsupportedOperationException( + String.format( + "Prometheus defined function [%s] is only " + + "supported in SOURCE clause with prometheus connector catalog", + functionName)); } @Override @@ -65,10 +66,15 @@ public ExprType type() { @Override public String toString() { - List args = arguments.stream() - .map(arg -> String.format("%s=%s", ((NamedArgumentExpression) arg) - .getArgName(), ((NamedArgumentExpression) arg).getValue().toString())) - .collect(Collectors.toList()); + List args = + arguments.stream() + .map( + arg -> + String.format( + "%s=%s", + ((NamedArgumentExpression) arg).getArgName(), + ((NamedArgumentExpression) arg).getValue().toString())) + .collect(Collectors.toList()); return String.format("%s(%s)", functionName, String.join(", ", args)); } @@ -80,30 +86,29 @@ public Table applyArguments() { private PrometheusQueryRequest buildQueryFromQueryRangeFunction(List arguments) { PrometheusQueryRequest prometheusQueryRequest = new PrometheusQueryRequest(); - arguments.forEach(arg -> { - String argName = ((NamedArgumentExpression) arg).getArgName(); - Expression argValue = ((NamedArgumentExpression) arg).getValue(); - ExprValue literalValue = argValue.valueOf(); - switch (argName) { - case QUERY: - prometheusQueryRequest - .setPromQl((String) literalValue.value()); - break; - case STARTTIME: - prometheusQueryRequest.setStartTime(((Number) literalValue.value()).longValue()); - break; - case ENDTIME: - prometheusQueryRequest.setEndTime(((Number) literalValue.value()).longValue()); - break; - case STEP: - prometheusQueryRequest.setStep(literalValue.value().toString()); - break; - default: - throw new ExpressionEvaluationException( - String.format("Invalid Function Argument:%s", argName)); - } - }); + arguments.forEach( + arg -> { + String argName = ((NamedArgumentExpression) arg).getArgName(); + Expression argValue = ((NamedArgumentExpression) arg).getValue(); + ExprValue literalValue = argValue.valueOf(); + switch (argName) { + case QUERY: + prometheusQueryRequest.setPromQl((String) literalValue.value()); + break; + case STARTTIME: + prometheusQueryRequest.setStartTime(((Number) literalValue.value()).longValue()); + break; + case ENDTIME: + prometheusQueryRequest.setEndTime(((Number) literalValue.value()).longValue()); + break; + case STEP: + prometheusQueryRequest.setStep(literalValue.value().toString()); + break; + default: + throw new ExpressionEvaluationException( + String.format("Invalid Function Argument:%s", argName)); + } + }); return prometheusQueryRequest; } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolver.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolver.java index a82e5a397a..78d87b0a0b 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolver.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolver.java @@ -22,9 +22,9 @@ import org.opensearch.sql.prometheus.functions.implementation.QueryExemplarFunctionImplementation; /** - * This class is for query_exemplars table function resolver {@link FunctionResolver}. - * It takes care of validating function arguments and also creating - * required {@link org.opensearch.sql.expression.function.TableFunctionImplementation} Class. + * This class is for query_exemplars table function resolver {@link FunctionResolver}. It takes care + * of validating function arguments and also creating required {@link + * org.opensearch.sql.expression.function.TableFunctionImplementation} Class. */ @RequiredArgsConstructor public class QueryExemplarsTableFunctionResolver implements FunctionResolver { @@ -41,13 +41,15 @@ public Pair resolve(FunctionSignature unreso final FunctionName functionName = FunctionName.of(QUERY_EXEMPLARS); FunctionSignature functionSignature = new FunctionSignature(FunctionName.of(QUERY_EXEMPLARS), List.of(STRING, LONG, LONG)); - FunctionBuilder functionBuilder = (functionProperties, arguments) -> { - final List argumentNames = List.of(QUERY, STARTTIME, ENDTIME); - validatePrometheusTableFunctionArguments(arguments, argumentNames); - List namedArguments = getNamedArgumentsOfTableFunction(arguments, argumentNames); - return new QueryExemplarFunctionImplementation(functionName, - namedArguments, prometheusClient); - }; + FunctionBuilder functionBuilder = + (functionProperties, arguments) -> { + final List argumentNames = List.of(QUERY, STARTTIME, ENDTIME); + validatePrometheusTableFunctionArguments(arguments, argumentNames); + List namedArguments = + getNamedArgumentsOfTableFunction(arguments, argumentNames); + return new QueryExemplarFunctionImplementation( + functionName, namedArguments, prometheusClient); + }; return Pair.of(functionSignature, functionBuilder); } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolver.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolver.java index 8bb2a2d758..8dfa12134e 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolver.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolver.java @@ -39,11 +39,14 @@ public Pair resolve(FunctionSignature unreso FunctionSignature functionSignature = new FunctionSignature(functionName, List.of(STRING, LONG, LONG, STRING)); final List argumentNames = List.of(QUERY, STARTTIME, ENDTIME, STEP); - FunctionBuilder functionBuilder = (functionProperties, arguments) -> { - validatePrometheusTableFunctionArguments(arguments, argumentNames); - List namedArguments = getNamedArgumentsOfTableFunction(arguments, argumentNames); - return new QueryRangeFunctionImplementation(functionName, namedArguments, prometheusClient); - }; + FunctionBuilder functionBuilder = + (functionProperties, arguments) -> { + validatePrometheusTableFunctionArguments(arguments, argumentNames); + List namedArguments = + getNamedArgumentsOfTableFunction(arguments, argumentNames); + return new QueryRangeFunctionImplementation( + functionName, namedArguments, prometheusClient); + }; return Pair.of(functionSignature, functionBuilder); } @@ -51,5 +54,4 @@ public Pair resolve(FunctionSignature unreso public FunctionName getFunctionName() { return FunctionName.of(QUERY_RANGE); } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/PrometheusFunctionResponseHandle.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/PrometheusFunctionResponseHandle.java index f2cefa85ec..bbc0516df6 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/PrometheusFunctionResponseHandle.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/PrometheusFunctionResponseHandle.java @@ -8,14 +8,10 @@ import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.executor.ExecutionEngine; -/** - * Handle Prometheus response. - */ +/** Handle Prometheus response. */ public interface PrometheusFunctionResponseHandle { - /** - * Return true if Prometheus response has more result. - */ + /** Return true if Prometheus response has more result. */ boolean hasNext(); /** @@ -24,8 +20,6 @@ public interface PrometheusFunctionResponseHandle { */ ExprValue next(); - /** - * Return ExecutionEngine.Schema of the Prometheus response. - */ + /** Return ExecutionEngine.Schema of the Prometheus response. */ ExecutionEngine.Schema schema(); } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryExemplarsFunctionResponseHandle.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryExemplarsFunctionResponseHandle.java index f030ce8f7a..8d1c267a90 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryExemplarsFunctionResponseHandle.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryExemplarsFunctionResponseHandle.java @@ -44,8 +44,8 @@ public QueryExemplarsFunctionResponseHandle(JSONArray responseArray) { private void constructIteratorAndSchema(JSONArray responseArray) { List columnList = new ArrayList<>(); columnList.add(new ExecutionEngine.Schema.Column(SERIES_LABELS_KEY, SERIES_LABELS_KEY, STRUCT)); - columnList.add(new ExecutionEngine.Schema.Column(EXEMPLARS_KEY, EXEMPLARS_KEY, - ExprCoreType.ARRAY)); + columnList.add( + new ExecutionEngine.Schema.Column(EXEMPLARS_KEY, EXEMPLARS_KEY, ExprCoreType.ARRAY)); this.schema = new ExecutionEngine.Schema(columnList); List result = new ArrayList<>(); for (int i = 0; i < responseArray.length(); i++) { @@ -62,7 +62,8 @@ private void constructIteratorAndSchema(JSONArray responseArray) { private ExprValue constructSeriesLabels(JSONObject seriesLabels) { LinkedHashMap seriesLabelsMap = new LinkedHashMap<>(); - seriesLabels.keySet() + seriesLabels + .keySet() .forEach(key -> seriesLabelsMap.put(key, new ExprStringValue(seriesLabels.getString(key)))); return new ExprTupleValue(seriesLabelsMap); } @@ -78,13 +79,13 @@ private ExprValue constructExemplarList(JSONArray exemplars) { private ExprValue constructExemplar(JSONObject exemplarsJSONObject) { LinkedHashMap exemplarHashMap = new LinkedHashMap<>(); - exemplarHashMap.put(LABELS_KEY, - constructLabelsInExemplar(exemplarsJSONObject.getJSONObject(LABELS_KEY))); - exemplarHashMap.put(TIMESTAMP_KEY, - new ExprTimestampValue(Instant.ofEpochMilli((long)( - exemplarsJSONObject.getDouble(TIMESTAMP_KEY) * 1000)))); - exemplarHashMap.put(VALUE_KEY, - new ExprDoubleValue(exemplarsJSONObject.getDouble(VALUE_KEY))); + exemplarHashMap.put( + LABELS_KEY, constructLabelsInExemplar(exemplarsJSONObject.getJSONObject(LABELS_KEY))); + exemplarHashMap.put( + TIMESTAMP_KEY, + new ExprTimestampValue( + Instant.ofEpochMilli((long) (exemplarsJSONObject.getDouble(TIMESTAMP_KEY) * 1000)))); + exemplarHashMap.put(VALUE_KEY, new ExprDoubleValue(exemplarsJSONObject.getDouble(VALUE_KEY))); return new ExprTupleValue(exemplarHashMap); } @@ -106,7 +107,6 @@ public ExprValue next() { return responseIterator.next(); } - @Override public ExecutionEngine.Schema schema() { return schema; diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryRangeFunctionResponseHandle.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryRangeFunctionResponseHandle.java index a3c68617e8..e10c9d7aff 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryRangeFunctionResponseHandle.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/response/QueryRangeFunctionResponseHandle.java @@ -30,9 +30,7 @@ import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.executor.ExecutionEngine; -/** - * Default implementation of QueryRangeFunctionResponseHandle. - */ +/** Default implementation of QueryRangeFunctionResponseHandle. */ public class QueryRangeFunctionResponseHandle implements PrometheusFunctionResponseHandle { private final JSONObject responseObject; @@ -62,25 +60,26 @@ private void constructIterator() { result.add(new ExprTupleValue(linkedHashMap)); } } else { - throw new RuntimeException(String.format("Unexpected Result Type: %s during Prometheus " - + "Response Parsing. 'matrix' resultType is expected", - responseObject.getString("resultType"))); + throw new RuntimeException( + String.format( + "Unexpected Result Type: %s during Prometheus " + + "Response Parsing. 'matrix' resultType is expected", + responseObject.getString("resultType"))); } this.responseIterator = result.iterator(); } - private static void extractTimestampAndValues(JSONArray values, - LinkedHashMap linkedHashMap) { + private static void extractTimestampAndValues( + JSONArray values, LinkedHashMap linkedHashMap) { List timestampList = new ArrayList<>(); List valueList = new ArrayList<>(); for (int j = 0; j < values.length(); j++) { JSONArray value = values.getJSONArray(j); - timestampList.add(new ExprTimestampValue( - Instant.ofEpochMilli((long) (value.getDouble(0) * 1000)))); + timestampList.add( + new ExprTimestampValue(Instant.ofEpochMilli((long) (value.getDouble(0) * 1000)))); valueList.add(new ExprDoubleValue(value.getDouble(1))); } - linkedHashMap.put(TIMESTAMP, - new ExprCollectionValue(timestampList)); + linkedHashMap.put(TIMESTAMP, new ExprCollectionValue(timestampList)); linkedHashMap.put(VALUE, new ExprCollectionValue(valueList)); } @@ -90,12 +89,10 @@ private void constructSchema() { private ExprValue extractLabels(JSONObject metric) { LinkedHashMap labelsMap = new LinkedHashMap<>(); - metric.keySet().forEach(key - -> labelsMap.put(key, new ExprStringValue(metric.getString(key)))); + metric.keySet().forEach(key -> labelsMap.put(key, new ExprStringValue(metric.getString(key)))); return new ExprTupleValue(labelsMap); } - private List getColumnList() { List columnList = new ArrayList<>(); columnList.add(new ExecutionEngine.Schema.Column(LABELS, LABELS, ExprCoreType.STRUCT)); diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilder.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilder.java index 8364173889..7e779eb77c 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilder.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilder.java @@ -12,9 +12,7 @@ import org.opensearch.sql.storage.TableScanOperator; import org.opensearch.sql.storage.read.TableScanBuilder; -/** - * TableScanBuilder for query_exemplars table function of prometheus connector. - */ +/** TableScanBuilder for query_exemplars table function of prometheus connector. */ @AllArgsConstructor public class QueryExemplarsFunctionTableScanBuilder extends TableScanBuilder { @@ -24,8 +22,8 @@ public class QueryExemplarsFunctionTableScanBuilder extends TableScanBuilder { @Override public TableScanOperator build() { - return new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + return new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); } // Since we are determining the schema after table scan, @@ -34,5 +32,4 @@ public TableScanOperator build() { public boolean pushDownProject(LogicalProject project) { return true; } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperator.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperator.java index 85ba6c854a..1a58429328 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperator.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperator.java @@ -22,37 +22,37 @@ import org.opensearch.sql.storage.TableScanOperator; /** - * This class is for QueryExemplars function {@link TableScanOperator}. - * This takes care of getting exemplar data from prometheus by making - * {@link PrometheusQueryExemplarsRequest}. + * This class is for QueryExemplars function {@link TableScanOperator}. This takes care of getting + * exemplar data from prometheus by making {@link PrometheusQueryExemplarsRequest}. */ @RequiredArgsConstructor public class QueryExemplarsFunctionTableScanOperator extends TableScanOperator { private final PrometheusClient prometheusClient; - @Getter - private final PrometheusQueryExemplarsRequest request; + @Getter private final PrometheusQueryExemplarsRequest request; private QueryExemplarsFunctionResponseHandle queryExemplarsFunctionResponseHandle; private static final Logger LOG = LogManager.getLogger(); @Override public void open() { super.open(); - this.queryExemplarsFunctionResponseHandle - = AccessController - .doPrivileged((PrivilegedAction) () -> { - try { - JSONArray responseArray = prometheusClient.queryExemplars( - request.getQuery(), - request.getStartTime(), request.getEndTime()); - return new QueryExemplarsFunctionResponseHandle(responseArray); - } catch (IOException e) { - LOG.error(e.getMessage()); - throw new RuntimeException( - String.format("Error fetching data from prometheus server: %s", e.getMessage())); - } - }); + this.queryExemplarsFunctionResponseHandle = + AccessController.doPrivileged( + (PrivilegedAction) + () -> { + try { + JSONArray responseArray = + prometheusClient.queryExemplars( + request.getQuery(), request.getStartTime(), request.getEndTime()); + return new QueryExemplarsFunctionResponseHandle(responseArray); + } catch (IOException e) { + LOG.error(e.getMessage()); + throw new RuntimeException( + String.format( + "Error fetching data from prometheus server: %s", e.getMessage())); + } + }); } @Override @@ -72,7 +72,9 @@ public ExprValue next() { @Override public String explain() { - return String.format(Locale.ROOT, "query_exemplars(%s, %s, %s)", + return String.format( + Locale.ROOT, + "query_exemplars(%s, %s, %s)", request.getQuery(), request.getStartTime(), request.getEndTime()); diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilder.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilder.java index 00e2191d09..2d22c0af69 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilder.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilder.java @@ -15,9 +15,8 @@ import org.opensearch.sql.storage.read.TableScanBuilder; /** - * TableScanBuilder for query_range table function of prometheus connector. - * we can merge this when we refactor for existing - * ppl queries based on prometheus connector. + * TableScanBuilder for query_range table function of prometheus connector. we can merge this when + * we refactor for existing ppl queries based on prometheus connector. */ @AllArgsConstructor public class QueryRangeFunctionTableScanBuilder extends TableScanBuilder { diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperator.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperator.java index 68b9b60643..fc3f9f9a9b 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperator.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperator.java @@ -23,9 +23,7 @@ import org.opensearch.sql.prometheus.request.PrometheusQueryRequest; import org.opensearch.sql.storage.TableScanOperator; -/** - * This a table scan operator to handle Query Range table function. - */ +/** This a table scan operator to handle Query Range table function. */ @RequiredArgsConstructor public class QueryRangeFunctionTableScanOperator extends TableScanOperator { @@ -39,19 +37,25 @@ public class QueryRangeFunctionTableScanOperator extends TableScanOperator { @Override public void open() { super.open(); - this.prometheusResponseHandle - = AccessController.doPrivileged((PrivilegedAction) () -> { - try { - JSONObject responseObject = prometheusClient.queryRange( - request.getPromQl(), - request.getStartTime(), request.getEndTime(), request.getStep()); - return new QueryRangeFunctionResponseHandle(responseObject); - } catch (IOException e) { - LOG.error(e.getMessage()); - throw new RuntimeException( - String.format("Error fetching data from prometheus server: %s", e.getMessage())); - } - }); + this.prometheusResponseHandle = + AccessController.doPrivileged( + (PrivilegedAction) + () -> { + try { + JSONObject responseObject = + prometheusClient.queryRange( + request.getPromQl(), + request.getStartTime(), + request.getEndTime(), + request.getStep()); + return new QueryRangeFunctionResponseHandle(responseObject); + } catch (IOException e) { + LOG.error(e.getMessage()); + throw new RuntimeException( + String.format( + "Error fetching data from prometheus server: %s", e.getMessage())); + } + }); } @Override @@ -71,7 +75,9 @@ public ExprValue next() { @Override public String explain() { - return String.format(Locale.ROOT, "query_range(%s, %s, %s, %s)", + return String.format( + Locale.ROOT, + "query_range(%s, %s, %s, %s)", request.getPromQl(), request.getStartTime(), request.getEndTime(), diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricAgg.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricAgg.java index f348c699a1..f7c45f6ad2 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricAgg.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricAgg.java @@ -20,10 +20,7 @@ import org.opensearch.sql.planner.logical.LogicalPlan; import org.opensearch.sql.planner.logical.LogicalPlanNodeVisitor; - -/** - * Logical Metric Scan along with aggregation Operation. - */ +/** Logical Metric Scan along with aggregation Operation. */ @Getter @ToString @EqualsAndHashCode(callSuper = false) @@ -31,37 +28,29 @@ public class PrometheusLogicalMetricAgg extends LogicalPlan { private final String metricName; - /** - * Filter Condition. - */ - @Setter - private Expression filter; + /** Filter Condition. */ + @Setter private Expression filter; - /** - * Aggregation List. - */ - @Setter - private List aggregatorList; + /** Aggregation List. */ + @Setter private List aggregatorList; - /** - * Group List. - */ - @Setter - private List groupByList; + /** Group List. */ + @Setter private List groupByList; /** * Constructor for LogicalMetricAgg Logical Plan. * - * @param metricName metricName - * @param filter filter + * @param metricName metricName + * @param filter filter * @param aggregatorList aggregatorList - * @param groupByList groupByList. + * @param groupByList groupByList. */ @Builder - public PrometheusLogicalMetricAgg(String metricName, - Expression filter, - List aggregatorList, - List groupByList) { + public PrometheusLogicalMetricAgg( + String metricName, + Expression filter, + List aggregatorList, + List groupByList) { super(ImmutableList.of()); this.metricName = metricName; this.filter = filter; diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricScan.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricScan.java index 5e07d6899f..7b28a8a6c9 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricScan.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalMetricScan.java @@ -17,8 +17,8 @@ import org.opensearch.sql.planner.logical.LogicalPlanNodeVisitor; /** - * Prometheus Logical Metric Scan Operation. - * In an optimized plan this node represents both Relation and Filter Operation. + * Prometheus Logical Metric Scan Operation. In an optimized plan this node represents both Relation + * and Filter Operation. */ @Getter @ToString @@ -27,9 +27,7 @@ public class PrometheusLogicalMetricScan extends LogicalPlan { private final String metricName; - /** - * Filter Condition. - */ + /** Filter Condition. */ private final Expression filter; /** @@ -39,8 +37,7 @@ public class PrometheusLogicalMetricScan extends LogicalPlan { * @param filter filter. */ @Builder - public PrometheusLogicalMetricScan(String metricName, - Expression filter) { + public PrometheusLogicalMetricScan(String metricName, Expression filter) { super(ImmutableList.of()); this.metricName = metricName; this.filter = filter; @@ -50,5 +47,4 @@ public PrometheusLogicalMetricScan(String metricName, public R accept(LogicalPlanNodeVisitor visitor, C context) { return visitor.visitNode(this, context); } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalPlanOptimizerFactory.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalPlanOptimizerFactory.java index 8a365b2786..ea14be0e0a 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalPlanOptimizerFactory.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicalPlanOptimizerFactory.java @@ -5,7 +5,6 @@ package org.opensearch.sql.prometheus.planner.logical; - import java.util.Arrays; import lombok.experimental.UtilityClass; import org.opensearch.sql.planner.optimizer.LogicalPlanOptimizer; @@ -13,20 +12,14 @@ import org.opensearch.sql.prometheus.planner.logical.rules.MergeAggAndRelation; import org.opensearch.sql.prometheus.planner.logical.rules.MergeFilterAndRelation; -/** - * Prometheus storage engine specified logical plan optimizer. - */ +/** Prometheus storage engine specified logical plan optimizer. */ @UtilityClass public class PrometheusLogicalPlanOptimizerFactory { - /** - * Create Prometheus storage specified logical plan optimizer. - */ + /** Create Prometheus storage specified logical plan optimizer. */ public static LogicalPlanOptimizer create() { - return new LogicalPlanOptimizer(Arrays.asList( - new MergeFilterAndRelation(), - new MergeAggAndIndexScan(), - new MergeAggAndRelation() - )); + return new LogicalPlanOptimizer( + Arrays.asList( + new MergeFilterAndRelation(), new MergeAggAndIndexScan(), new MergeAggAndRelation())); } } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndIndexScan.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndIndexScan.java index 76bc6cc840..2594b74eb5 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndIndexScan.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndIndexScan.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.planner.logical.rules; import static com.facebook.presto.matching.Pattern.typeOf; @@ -20,9 +19,7 @@ import org.opensearch.sql.prometheus.planner.logical.PrometheusLogicalMetricAgg; import org.opensearch.sql.prometheus.planner.logical.PrometheusLogicalMetricScan; -/** - * Merge Aggregation -- Relation to MetricScanAggregation. - */ +/** Merge Aggregation -- Relation to MetricScanAggregation. */ public class MergeAggAndIndexScan implements Rule { private final Capture capture; @@ -31,22 +28,18 @@ public class MergeAggAndIndexScan implements Rule { @Getter private final Pattern pattern; - /** - * Constructor of MergeAggAndIndexScan. - */ + /** Constructor of MergeAggAndIndexScan. */ public MergeAggAndIndexScan() { this.capture = Capture.newCapture(); - this.pattern = typeOf(LogicalAggregation.class) - .with(source().matching(typeOf(PrometheusLogicalMetricScan.class) - .capturedAs(capture))); + this.pattern = + typeOf(LogicalAggregation.class) + .with(source().matching(typeOf(PrometheusLogicalMetricScan.class).capturedAs(capture))); } @Override - public LogicalPlan apply(LogicalAggregation aggregation, - Captures captures) { + public LogicalPlan apply(LogicalAggregation aggregation, Captures captures) { PrometheusLogicalMetricScan indexScan = captures.get(capture); - return PrometheusLogicalMetricAgg - .builder() + return PrometheusLogicalMetricAgg.builder() .metricName(indexScan.getMetricName()) .filter(indexScan.getFilter()) .aggregatorList(aggregation.getAggregatorList()) diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndRelation.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndRelation.java index fa9b0c7206..e6170e41f9 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndRelation.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeAggAndRelation.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.planner.logical.rules; import static com.facebook.presto.matching.Pattern.typeOf; @@ -20,9 +19,7 @@ import org.opensearch.sql.planner.optimizer.Rule; import org.opensearch.sql.prometheus.planner.logical.PrometheusLogicalMetricAgg; -/** - * Merge Aggregation -- Relation to IndexScanAggregation. - */ +/** Merge Aggregation -- Relation to IndexScanAggregation. */ public class MergeAggAndRelation implements Rule { private final Capture relationCapture; @@ -31,21 +28,18 @@ public class MergeAggAndRelation implements Rule { @Getter private final Pattern pattern; - /** - * Constructor of MergeAggAndRelation. - */ + /** Constructor of MergeAggAndRelation. */ public MergeAggAndRelation() { this.relationCapture = Capture.newCapture(); - this.pattern = typeOf(LogicalAggregation.class) - .with(source().matching(typeOf(LogicalRelation.class).capturedAs(relationCapture))); + this.pattern = + typeOf(LogicalAggregation.class) + .with(source().matching(typeOf(LogicalRelation.class).capturedAs(relationCapture))); } @Override - public LogicalPlan apply(LogicalAggregation aggregation, - Captures captures) { + public LogicalPlan apply(LogicalAggregation aggregation, Captures captures) { LogicalRelation relation = captures.get(relationCapture); - return PrometheusLogicalMetricAgg - .builder() + return PrometheusLogicalMetricAgg.builder() .metricName(relation.getRelationName()) .aggregatorList(aggregation.getAggregatorList()) .groupByList(aggregation.getGroupByList()) diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeFilterAndRelation.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeFilterAndRelation.java index a99eb695be..2013938d73 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeFilterAndRelation.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/planner/logical/rules/MergeFilterAndRelation.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.planner.logical.rules; import static com.facebook.presto.matching.Pattern.typeOf; @@ -18,21 +17,18 @@ import org.opensearch.sql.planner.optimizer.Rule; import org.opensearch.sql.prometheus.planner.logical.PrometheusLogicalMetricScan; -/** - * Merge Filter -- Relation to LogicalMetricScan. - */ +/** Merge Filter -- Relation to LogicalMetricScan. */ public class MergeFilterAndRelation implements Rule { private final Capture relationCapture; private final Pattern pattern; - /** - * Constructor of MergeFilterAndRelation. - */ + /** Constructor of MergeFilterAndRelation. */ public MergeFilterAndRelation() { this.relationCapture = Capture.newCapture(); - this.pattern = typeOf(LogicalFilter.class) - .with(source().matching(typeOf(LogicalRelation.class).capturedAs(relationCapture))); + this.pattern = + typeOf(LogicalFilter.class) + .with(source().matching(typeOf(LogicalRelation.class).capturedAs(relationCapture))); } @Override @@ -41,11 +37,9 @@ public Pattern pattern() { } @Override - public LogicalPlan apply(LogicalFilter filter, - Captures captures) { + public LogicalPlan apply(LogicalFilter filter, Captures captures) { LogicalRelation relation = captures.get(relationCapture); - return PrometheusLogicalMetricScan - .builder() + return PrometheusLogicalMetricScan.builder() .metricName(relation.getRelationName()) .filter(filter.getCondition()) .build(); diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryExemplarsRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryExemplarsRequest.java index 9cf3d41522..d4eea97c48 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryExemplarsRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryExemplarsRequest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.request; import lombok.AllArgsConstructor; @@ -12,9 +11,7 @@ import lombok.NoArgsConstructor; import lombok.ToString; -/** - * Prometheus metric query request. - */ +/** Prometheus metric query request. */ @EqualsAndHashCode @Data @ToString @@ -22,19 +19,12 @@ @NoArgsConstructor public class PrometheusQueryExemplarsRequest { - /** - * PromQL. - */ + /** PromQL. */ private String query; - /** - * startTime of the query. - */ + /** startTime of the query. */ private Long startTime; - /** - * endTime of the query. - */ + /** endTime of the query. */ private Long endTime; - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryRequest.java index d287ea4d65..e24c27c52a 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/PrometheusQueryRequest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.request; import lombok.AllArgsConstructor; @@ -12,9 +11,7 @@ import lombok.NoArgsConstructor; import lombok.ToString; -/** - * Prometheus metric query request. - */ +/** Prometheus metric query request. */ @EqualsAndHashCode @Data @ToString @@ -22,24 +19,15 @@ @NoArgsConstructor public class PrometheusQueryRequest { - /** - * PromQL. - */ + /** PromQL. */ private String promQl; - /** - * startTime of the query. - */ + /** startTime of the query. */ private Long startTime; - /** - * endTime of the query. - */ + /** endTime of the query. */ private Long endTime; - /** - * step is the resolution required between startTime and endTime. - */ + /** step is the resolution required between startTime and endTime. */ private String step; - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java index 2e0d46b3e8..b6a4e3c49c 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java @@ -5,7 +5,6 @@ * */ - package org.opensearch.sql.prometheus.request.system; import static org.opensearch.sql.data.model.ExprValueUtils.stringValue; @@ -31,60 +30,63 @@ import org.opensearch.sql.prometheus.storage.PrometheusMetricDefaultSchema; /** - * Describe Metric metadata request. - * This is triggered in case of both query range table function and relation. - * In case of table function metric name is null. + * Describe Metric metadata request. This is triggered in case of both query range table function + * and relation. In case of table function metric name is null. */ @ToString(onlyExplicitlyIncluded = true) public class PrometheusDescribeMetricRequest implements PrometheusSystemRequest { private final PrometheusClient prometheusClient; - @ToString.Include - private final String metricName; + @ToString.Include private final String metricName; private final DataSourceSchemaName dataSourceSchemaName; private static final Logger LOG = LogManager.getLogger(); /** - * Constructor for Prometheus Describe Metric Request. - * In case of pass through queries like query_range function, - * metric names are optional. + * Constructor for Prometheus Describe Metric Request. In case of pass through queries like + * query_range function, metric names are optional. * - * @param prometheusClient prometheusClient. + * @param prometheusClient prometheusClient. * @param dataSourceSchemaName dataSourceSchemaName. - * @param metricName metricName. + * @param metricName metricName. */ - public PrometheusDescribeMetricRequest(PrometheusClient prometheusClient, - DataSourceSchemaName dataSourceSchemaName, - @NonNull String metricName) { + public PrometheusDescribeMetricRequest( + PrometheusClient prometheusClient, + DataSourceSchemaName dataSourceSchemaName, + @NonNull String metricName) { this.prometheusClient = prometheusClient; this.metricName = metricName; this.dataSourceSchemaName = dataSourceSchemaName; } - /** - * Get the mapping of field and type. - * Returns labels and default schema fields. + * Get the mapping of field and type. Returns labels and default schema fields. * * @return mapping of field and type. */ public Map getFieldTypes() { Map fieldTypes = new HashMap<>(); - AccessController.doPrivileged((PrivilegedAction>) () -> { - try { - prometheusClient.getLabels(metricName) - .forEach(label -> fieldTypes.put(label, ExprCoreType.STRING)); - } catch (IOException e) { - LOG.error("Error while fetching labels for {} from prometheus: {}", - metricName, e.getMessage()); - throw new RuntimeException(String.format("Error while fetching labels " - + "for %s from prometheus: %s", metricName, e.getMessage())); - } - return null; - }); + AccessController.doPrivileged( + (PrivilegedAction>) + () -> { + try { + prometheusClient + .getLabels(metricName) + .forEach(label -> fieldTypes.put(label, ExprCoreType.STRING)); + } catch (IOException e) { + LOG.error( + "Error while fetching labels for {} from prometheus: {}", + metricName, + e.getMessage()); + throw new RuntimeException( + String.format( + "Error while fetching labels " + "for %s from prometheus: %s", + metricName, e.getMessage())); + } + return null; + }); fieldTypes.putAll(PrometheusMetricDefaultSchema.DEFAULT_MAPPING.getMapping()); return fieldTypes; } @@ -93,14 +95,17 @@ public Map getFieldTypes() { public List search() { List results = new ArrayList<>(); for (Map.Entry entry : getFieldTypes().entrySet()) { - results.add(row(entry.getKey(), entry.getValue().legacyTypeName().toLowerCase(), - dataSourceSchemaName)); + results.add( + row( + entry.getKey(), + entry.getValue().legacyTypeName().toLowerCase(), + dataSourceSchemaName)); } return results; } - private ExprTupleValue row(String fieldName, String fieldType, - DataSourceSchemaName dataSourceSchemaName) { + private ExprTupleValue row( + String fieldName, String fieldType, DataSourceSchemaName dataSourceSchemaName) { LinkedHashMap valueMap = new LinkedHashMap<>(); valueMap.put("TABLE_CATALOG", stringValue(dataSourceSchemaName.getDataSourceName())); valueMap.put("TABLE_SCHEMA", stringValue(dataSourceSchemaName.getSchemaName())); diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusListMetricsRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusListMetricsRequest.java index f5d2a44340..0e6c2bb2c6 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusListMetricsRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusListMetricsRequest.java @@ -34,28 +34,33 @@ public class PrometheusListMetricsRequest implements PrometheusSystemRequest { private static final Logger LOG = LogManager.getLogger(); - @Override public List search() { - return AccessController.doPrivileged((PrivilegedAction>) () -> { - try { - Map> result = prometheusClient.getAllMetrics(); - return result.keySet() - .stream() - .map(x -> { - MetricMetadata metricMetadata = result.get(x).get(0); - return row(x, metricMetadata.getType(), - metricMetadata.getUnit(), metricMetadata.getHelp()); - }) - .collect(Collectors.toList()); - } catch (IOException e) { - LOG.error("Error while fetching metric list for from prometheus: {}", - e.getMessage()); - throw new RuntimeException(String.format("Error while fetching metric list " - + "for from prometheus: %s", e.getMessage())); - } - }); - + return AccessController.doPrivileged( + (PrivilegedAction>) + () -> { + try { + Map> result = prometheusClient.getAllMetrics(); + return result.keySet().stream() + .map( + x -> { + MetricMetadata metricMetadata = result.get(x).get(0); + return row( + x, + metricMetadata.getType(), + metricMetadata.getUnit(), + metricMetadata.getHelp()); + }) + .collect(Collectors.toList()); + } catch (IOException e) { + LOG.error( + "Error while fetching metric list for from prometheus: {}", e.getMessage()); + throw new RuntimeException( + String.format( + "Error while fetching metric list " + "for from prometheus: %s", + e.getMessage())); + } + }); } private ExprTupleValue row(String metricName, String tableType, String unit, String help) { diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusSystemRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusSystemRequest.java index e68ad22c30..6972a9390c 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusSystemRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusSystemRequest.java @@ -10,9 +10,7 @@ import java.util.List; import org.opensearch.sql.data.model.ExprValue; -/** - * Prometheus system request query to get metadata Info. - */ +/** Prometheus system request query to get metadata Info. */ public interface PrometheusSystemRequest { /** @@ -21,5 +19,4 @@ public interface PrometheusSystemRequest { * @return list of ExprValue. */ List search(); - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/response/PrometheusResponse.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/response/PrometheusResponse.java index ca250125e6..339d882f5a 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/response/PrometheusResponse.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/response/PrometheusResponse.java @@ -42,13 +42,12 @@ public class PrometheusResponse implements Iterable { /** * Constructor. * - * @param responseObject Prometheus responseObject. - * @param prometheusResponseFieldNames data model which - * contains field names for the metric measurement - * and timestamp fieldName. + * @param responseObject Prometheus responseObject. + * @param prometheusResponseFieldNames data model which contains field names for the metric + * measurement and timestamp fieldName. */ - public PrometheusResponse(JSONObject responseObject, - PrometheusResponseFieldNames prometheusResponseFieldNames) { + public PrometheusResponse( + JSONObject responseObject, PrometheusResponseFieldNames prometheusResponseFieldNames) { this.responseObject = responseObject; this.prometheusResponseFieldNames = prometheusResponseFieldNames; } @@ -66,18 +65,22 @@ public Iterator iterator() { for (int j = 0; j < values.length(); j++) { LinkedHashMap linkedHashMap = new LinkedHashMap<>(); JSONArray val = values.getJSONArray(j); - linkedHashMap.put(prometheusResponseFieldNames.getTimestampFieldName(), + linkedHashMap.put( + prometheusResponseFieldNames.getTimestampFieldName(), new ExprTimestampValue(Instant.ofEpochMilli((long) (val.getDouble(0) * 1000)))); - linkedHashMap.put(prometheusResponseFieldNames.getValueFieldName(), getValue(val, 1, - prometheusResponseFieldNames.getValueType())); + linkedHashMap.put( + prometheusResponseFieldNames.getValueFieldName(), + getValue(val, 1, prometheusResponseFieldNames.getValueType())); insertLabels(linkedHashMap, metric); result.add(new ExprTupleValue(linkedHashMap)); } } } else { - throw new RuntimeException(String.format("Unexpected Result Type: %s during Prometheus " - + "Response Parsing. 'matrix' resultType is expected", - responseObject.getString(RESULT_TYPE_KEY))); + throw new RuntimeException( + String.format( + "Unexpected Result Type: %s during Prometheus " + + "Response Parsing. 'matrix' resultType is expected", + responseObject.getString(RESULT_TYPE_KEY))); } return result.iterator(); } @@ -103,12 +106,11 @@ private String getKey(String key) { } else { return this.prometheusResponseFieldNames.getGroupByList().stream() .filter(expression -> expression.getDelegated() instanceof ReferenceExpression) - .filter(expression - -> ((ReferenceExpression) expression.getDelegated()).getAttr().equals(key)) + .filter( + expression -> ((ReferenceExpression) expression.getDelegated()).getAttr().equals(key)) .findFirst() .map(NamedExpression::getName) .orElse(key); } } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricDefaultSchema.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricDefaultSchema.java index 790189d903..f0933eee9d 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricDefaultSchema.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricDefaultSchema.java @@ -20,12 +20,11 @@ @Getter @RequiredArgsConstructor public enum PrometheusMetricDefaultSchema { - - DEFAULT_MAPPING(new ImmutableMap.Builder() - .put(TIMESTAMP, ExprCoreType.TIMESTAMP) - .put(VALUE, ExprCoreType.DOUBLE) - .build()); + DEFAULT_MAPPING( + new ImmutableMap.Builder() + .put(TIMESTAMP, ExprCoreType.TIMESTAMP) + .put(VALUE, ExprCoreType.DOUBLE) + .build()); private final Map mapping; - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScan.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScan.java index 7f75cb3c07..598e388914 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScan.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScan.java @@ -23,26 +23,19 @@ import org.opensearch.sql.prometheus.storage.model.PrometheusResponseFieldNames; import org.opensearch.sql.storage.TableScanOperator; -/** - * Prometheus metric scan operator. - */ +/** Prometheus metric scan operator. */ @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false) @ToString(onlyExplicitlyIncluded = true) public class PrometheusMetricScan extends TableScanOperator { private final PrometheusClient prometheusClient; - @EqualsAndHashCode.Include - @Getter - @Setter - @ToString.Include + @EqualsAndHashCode.Include @Getter @Setter @ToString.Include private PrometheusQueryRequest request; private Iterator iterator; - @Setter - private PrometheusResponseFieldNames prometheusResponseFieldNames; - + @Setter private PrometheusResponseFieldNames prometheusResponseFieldNames; private static final Logger LOG = LogManager.getLogger(); @@ -60,17 +53,25 @@ public PrometheusMetricScan(PrometheusClient prometheusClient) { @Override public void open() { super.open(); - this.iterator = AccessController.doPrivileged((PrivilegedAction>) () -> { - try { - JSONObject responseObject = prometheusClient.queryRange( - request.getPromQl(), - request.getStartTime(), request.getEndTime(), request.getStep()); - return new PrometheusResponse(responseObject, prometheusResponseFieldNames).iterator(); - } catch (IOException e) { - LOG.error(e.getMessage()); - throw new RuntimeException("Error fetching data from prometheus server. " + e.getMessage()); - } - }); + this.iterator = + AccessController.doPrivileged( + (PrivilegedAction>) + () -> { + try { + JSONObject responseObject = + prometheusClient.queryRange( + request.getPromQl(), + request.getStartTime(), + request.getEndTime(), + request.getStep()); + return new PrometheusResponse(responseObject, prometheusResponseFieldNames) + .iterator(); + } catch (IOException e) { + LOG.error(e.getMessage()); + throw new RuntimeException( + "Error fetching data from prometheus server. " + e.getMessage()); + } + }); } @Override diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTable.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTable.java index 4844e1f6db..1124e93608 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTable.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTable.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.prometheus.storage; import static org.opensearch.sql.prometheus.data.constants.PrometheusFieldConstants.LABELS; @@ -26,40 +25,30 @@ import org.opensearch.sql.storage.read.TableScanBuilder; /** - * Prometheus table (metric) implementation. - * This can be constructed from a metric Name - * or from PrometheusQueryRequest In case of query_range table function. + * Prometheus table (metric) implementation. This can be constructed from a metric Name or from + * PrometheusQueryRequest In case of query_range table function. */ public class PrometheusMetricTable implements Table { private final PrometheusClient prometheusClient; - @Getter - private final String metricName; - - @Getter - private final PrometheusQueryRequest prometheusQueryRequest; + @Getter private final String metricName; + @Getter private final PrometheusQueryRequest prometheusQueryRequest; - /** - * The cached mapping of field and type in index. - */ + /** The cached mapping of field and type in index. */ private Map cachedFieldTypes = null; - /** - * Constructor only with metric name. - */ + /** Constructor only with metric name. */ public PrometheusMetricTable(PrometheusClient prometheusService, @Nonnull String metricName) { this.prometheusClient = prometheusService; this.metricName = metricName; this.prometheusQueryRequest = null; } - /** - * Constructor for entire promQl Request. - */ - public PrometheusMetricTable(PrometheusClient prometheusService, - @Nonnull PrometheusQueryRequest prometheusQueryRequest) { + /** Constructor for entire promQl Request. */ + public PrometheusMetricTable( + PrometheusClient prometheusService, @Nonnull PrometheusQueryRequest prometheusQueryRequest) { this.prometheusClient = prometheusService; this.metricName = null; this.prometheusQueryRequest = prometheusQueryRequest; @@ -67,14 +56,12 @@ public PrometheusMetricTable(PrometheusClient prometheusService, @Override public boolean exists() { - throw new UnsupportedOperationException( - "Prometheus metric exists operation is not supported"); + throw new UnsupportedOperationException("Prometheus metric exists operation is not supported"); } @Override public void create(Map schema) { - throw new UnsupportedOperationException( - "Prometheus metric create operation is not supported"); + throw new UnsupportedOperationException("Prometheus metric create operation is not supported"); } @Override @@ -82,11 +69,10 @@ public Map getFieldTypes() { if (cachedFieldTypes == null) { if (metricName != null) { cachedFieldTypes = - new PrometheusDescribeMetricRequest(prometheusClient, null, - metricName).getFieldTypes(); + new PrometheusDescribeMetricRequest(prometheusClient, null, metricName).getFieldTypes(); } else { - cachedFieldTypes = new HashMap<>(PrometheusMetricDefaultSchema.DEFAULT_MAPPING - .getMapping()); + cachedFieldTypes = + new HashMap<>(PrometheusMetricDefaultSchema.DEFAULT_MAPPING.getMapping()); cachedFieldTypes.put(LABELS, ExprCoreType.STRING); } } @@ -95,8 +81,7 @@ public Map getFieldTypes() { @Override public PhysicalPlan implement(LogicalPlan plan) { - PrometheusMetricScan metricScan = - new PrometheusMetricScan(prometheusClient); + PrometheusMetricScan metricScan = new PrometheusMetricScan(prometheusClient); return plan.accept(new PrometheusDefaultImplementor(), metricScan); } @@ -105,8 +90,8 @@ public LogicalPlan optimize(LogicalPlan plan) { return PrometheusLogicalPlanOptimizerFactory.create().optimize(plan); } - //Only handling query_range function for now. - //we need to move PPL implementations to ScanBuilder in future. + // Only handling query_range function for now. + // we need to move PPL implementations to ScanBuilder in future. @Override public TableScanBuilder createScanBuilder() { if (metricName == null) { diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngine.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngine.java index 738eb023b6..29fc15e2d0 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngine.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngine.java @@ -24,10 +24,7 @@ import org.opensearch.sql.storage.Table; import org.opensearch.sql.utils.SystemIndexUtils; - -/** - * Prometheus storage engine implementation. - */ +/** Prometheus storage engine implementation. */ @RequiredArgsConstructor public class PrometheusStorageEngine implements StorageEngine { @@ -52,16 +49,14 @@ public Table getTable(DataSourceSchemaName dataSourceSchemaName, String tableNam } } - private Table resolveInformationSchemaTable(DataSourceSchemaName dataSourceSchemaName, - String tableName) { + private Table resolveInformationSchemaTable( + DataSourceSchemaName dataSourceSchemaName, String tableName) { if (SystemIndexUtils.TABLE_NAME_FOR_TABLES_INFO.equals(tableName)) { - return new PrometheusSystemTable(prometheusClient, - dataSourceSchemaName, SystemIndexUtils.TABLE_INFO); + return new PrometheusSystemTable( + prometheusClient, dataSourceSchemaName, SystemIndexUtils.TABLE_INFO); } else { throw new SemanticCheckException( String.format("Information Schema doesn't contain %s table", tableName)); } } - - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java index b3ecd25af3..edae263ce3 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java @@ -56,23 +56,20 @@ public DataSourceType getDataSourceType() { @Override public DataSource createDataSource(DataSourceMetadata metadata) { return new DataSource( - metadata.getName(), - DataSourceType.PROMETHEUS, - getStorageEngine(metadata.getProperties())); + metadata.getName(), DataSourceType.PROMETHEUS, getStorageEngine(metadata.getProperties())); } - - //Need to refactor to a separate Validator class. + // Need to refactor to a separate Validator class. private void validateDataSourceConfigProperties(Map dataSourceMetadataConfig) throws URISyntaxException { if (dataSourceMetadataConfig.get(AUTH_TYPE) != null) { - AuthenticationType authenticationType - = AuthenticationType.get(dataSourceMetadataConfig.get(AUTH_TYPE)); + AuthenticationType authenticationType = + AuthenticationType.get(dataSourceMetadataConfig.get(AUTH_TYPE)); if (AuthenticationType.BASICAUTH.equals(authenticationType)) { validateMissingFields(dataSourceMetadataConfig, Set.of(URI, USERNAME, PASSWORD)); } else if (AuthenticationType.AWSSIGV4AUTH.equals(authenticationType)) { - validateMissingFields(dataSourceMetadataConfig, Set.of(URI, ACCESS_KEY, SECRET_KEY, - REGION)); + validateMissingFields( + dataSourceMetadataConfig, Set.of(URI, ACCESS_KEY, SECRET_KEY, REGION)); } } else { validateMissingFields(dataSourceMetadataConfig, Set.of(URI)); @@ -83,20 +80,21 @@ private void validateDataSourceConfigProperties(Map dataSourceMe StorageEngine getStorageEngine(Map requiredConfig) { PrometheusClient prometheusClient; prometheusClient = - AccessController.doPrivileged((PrivilegedAction) () -> { - try { - validateDataSourceConfigProperties(requiredConfig); - return new PrometheusClientImpl(getHttpClient(requiredConfig), - new URI(requiredConfig.get(URI))); - } catch (URISyntaxException e) { - throw new IllegalArgumentException( - String.format("Invalid URI in prometheus properties: %s", e.getMessage())); - } - }); + AccessController.doPrivileged( + (PrivilegedAction) + () -> { + try { + validateDataSourceConfigProperties(requiredConfig); + return new PrometheusClientImpl( + getHttpClient(requiredConfig), new URI(requiredConfig.get(URI))); + } catch (URISyntaxException e) { + throw new IllegalArgumentException( + String.format("Invalid URI in prometheus properties: %s", e.getMessage())); + } + }); return new PrometheusStorageEngine(prometheusClient); } - private OkHttpClient getHttpClient(Map config) { OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder(); okHttpClient.callTimeout(1, TimeUnit.MINUTES); @@ -104,16 +102,19 @@ private OkHttpClient getHttpClient(Map config) { if (config.get(AUTH_TYPE) != null) { AuthenticationType authenticationType = AuthenticationType.get(config.get(AUTH_TYPE)); if (AuthenticationType.BASICAUTH.equals(authenticationType)) { - okHttpClient.addInterceptor(new BasicAuthenticationInterceptor(config.get(USERNAME), - config.get(PASSWORD))); + okHttpClient.addInterceptor( + new BasicAuthenticationInterceptor(config.get(USERNAME), config.get(PASSWORD))); } else if (AuthenticationType.AWSSIGV4AUTH.equals(authenticationType)) { - okHttpClient.addInterceptor(new AwsSigningInterceptor( - new AWSStaticCredentialsProvider( - new BasicAWSCredentials(config.get(ACCESS_KEY), config.get(SECRET_KEY))), - config.get(REGION), "aps")); + okHttpClient.addInterceptor( + new AwsSigningInterceptor( + new AWSStaticCredentialsProvider( + new BasicAWSCredentials(config.get(ACCESS_KEY), config.get(SECRET_KEY))), + config.get(REGION), + "aps")); } else { throw new IllegalArgumentException( - String.format("AUTH Type : %s is not supported with Prometheus Connector", + String.format( + "AUTH Type : %s is not supported with Prometheus Connector", config.get(AUTH_TYPE))); } } @@ -132,13 +133,14 @@ private void validateMissingFields(Map config, Set field } StringBuilder errorStringBuilder = new StringBuilder(); if (missingFields.size() > 0) { - errorStringBuilder.append(String.format( - "Missing %s fields in the Prometheus connector properties.", missingFields)); + errorStringBuilder.append( + String.format( + "Missing %s fields in the Prometheus connector properties.", missingFields)); } if (invalidLengthFields.size() > 0) { - errorStringBuilder.append(String.format( - "Fields %s exceeds more than 1000 characters.", invalidLengthFields)); + errorStringBuilder.append( + String.format("Fields %s exceeds more than 1000 characters.", invalidLengthFields)); } if (errorStringBuilder.length() > 0) { throw new IllegalArgumentException(errorStringBuilder.toString()); @@ -148,8 +150,9 @@ private void validateMissingFields(Map config, Set field private void validateURI(Map config) throws URISyntaxException { URI uri = new URI(config.get(URI)); String host = uri.getHost(); - if (host == null || (!(DomainValidator.getInstance().isValid(host) - || DomainValidator.getInstance().isValidLocalTld(host)))) { + if (host == null + || (!(DomainValidator.getInstance().isValid(host) + || DomainValidator.getInstance().isValidLocalTld(host)))) { throw new IllegalArgumentException( String.format("Invalid hostname in the uri: %s", config.get(URI))); } else { @@ -158,10 +161,10 @@ private void validateURI(Map config) throws URISyntaxException { Matcher matcher = allowHostsPattern.matcher(host); if (!matcher.matches()) { throw new IllegalArgumentException( - String.format("Disallowed hostname in the uri: %s. Validate with %s config", + String.format( + "Disallowed hostname in the uri: %s. Validate with %s config", config.get(URI), Settings.Key.DATASOURCES_URI_ALLOWHOSTS.getKeyValue())); } } } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTable.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTable.java index dcb87c2cce..9ce8ae85fb 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTable.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTable.java @@ -20,20 +20,16 @@ import org.opensearch.sql.storage.read.TableScanBuilder; /** - * This is {@link Table} for querying exemplars in prometheus Table. - * Since {@link PrometheusMetricTable} is overloaded with query_range and normal - * PPL metric queries. Created a separate table for handling - * {@link PrometheusQueryExemplarsRequest} + * This is {@link Table} for querying exemplars in prometheus Table. Since {@link + * PrometheusMetricTable} is overloaded with query_range and normal PPL metric queries. Created a + * separate table for handling {@link PrometheusQueryExemplarsRequest} */ @RequiredArgsConstructor public class QueryExemplarsTable implements Table { - @Getter - private final PrometheusClient prometheusClient; - - @Getter - private final PrometheusQueryExemplarsRequest exemplarsRequest; + @Getter private final PrometheusClient prometheusClient; + @Getter private final PrometheusQueryExemplarsRequest exemplarsRequest; @Override public Map getFieldTypes() { @@ -49,5 +45,4 @@ public PhysicalPlan implement(LogicalPlan plan) { public TableScanBuilder createScanBuilder() { return new QueryExemplarsFunctionTableScanBuilder(prometheusClient, exemplarsRequest); } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/implementor/PrometheusDefaultImplementor.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/implementor/PrometheusDefaultImplementor.java index 6d426d13c8..f83a97dc06 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/implementor/PrometheusDefaultImplementor.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/implementor/PrometheusDefaultImplementor.java @@ -29,13 +29,9 @@ import org.opensearch.sql.prometheus.storage.querybuilder.StepParameterResolver; import org.opensearch.sql.prometheus.storage.querybuilder.TimeRangeParametersResolver; -/** - * Default Implementor of Logical plan for prometheus. - */ +/** Default Implementor of Logical plan for prometheus. */ @RequiredArgsConstructor -public class PrometheusDefaultImplementor - extends DefaultImplementor { - +public class PrometheusDefaultImplementor extends DefaultImplementor { @Override public PhysicalPlan visitNode(LogicalPlan plan, PrometheusMetricScan context) { @@ -44,62 +40,64 @@ public PhysicalPlan visitNode(LogicalPlan plan, PrometheusMetricScan context) { } else if (plan instanceof PrometheusLogicalMetricAgg) { return visitIndexAggregation((PrometheusLogicalMetricAgg) plan, context); } else { - throw new IllegalStateException(StringUtils.format("unexpected plan node type %s", - plan.getClass())); + throw new IllegalStateException( + StringUtils.format("unexpected plan node type %s", plan.getClass())); } } - /** - * Implement PrometheusLogicalMetricScan. - */ - public PhysicalPlan visitIndexScan(PrometheusLogicalMetricScan node, - PrometheusMetricScan context) { + /** Implement PrometheusLogicalMetricScan. */ + public PhysicalPlan visitIndexScan( + PrometheusLogicalMetricScan node, PrometheusMetricScan context) { String query = SeriesSelectionQueryBuilder.build(node.getMetricName(), node.getFilter()); context.getRequest().setPromQl(query); setTimeRangeParameters(node.getFilter(), context); - context.getRequest() - .setStep(StepParameterResolver.resolve(context.getRequest().getStartTime(), - context.getRequest().getEndTime(), null)); + context + .getRequest() + .setStep( + StepParameterResolver.resolve( + context.getRequest().getStartTime(), context.getRequest().getEndTime(), null)); return context; } - /** - * Implement PrometheusLogicalMetricAgg. - */ - public PhysicalPlan visitIndexAggregation(PrometheusLogicalMetricAgg node, - PrometheusMetricScan context) { + /** Implement PrometheusLogicalMetricAgg. */ + public PhysicalPlan visitIndexAggregation( + PrometheusLogicalMetricAgg node, PrometheusMetricScan context) { setTimeRangeParameters(node.getFilter(), context); - context.getRequest() - .setStep(StepParameterResolver.resolve(context.getRequest().getStartTime(), - context.getRequest().getEndTime(), node.getGroupByList())); + context + .getRequest() + .setStep( + StepParameterResolver.resolve( + context.getRequest().getStartTime(), + context.getRequest().getEndTime(), + node.getGroupByList())); String step = context.getRequest().getStep(); - String seriesSelectionQuery - = SeriesSelectionQueryBuilder.build(node.getMetricName(), node.getFilter()); + String seriesSelectionQuery = + SeriesSelectionQueryBuilder.build(node.getMetricName(), node.getFilter()); - String aggregateQuery - = AggregationQueryBuilder.build(node.getAggregatorList(), - node.getGroupByList()); + String aggregateQuery = + AggregationQueryBuilder.build(node.getAggregatorList(), node.getGroupByList()); String finalQuery = String.format(aggregateQuery, seriesSelectionQuery + "[" + step + "]"); context.getRequest().setPromQl(finalQuery); - //Since prometheus response doesn't have any fieldNames in its output. - //the field names are sent to PrometheusResponse constructor via context. + // Since prometheus response doesn't have any fieldNames in its output. + // the field names are sent to PrometheusResponse constructor via context. setPrometheusResponseFieldNames(node, context); return context; } @Override - public PhysicalPlan visitRelation(LogicalRelation node, - PrometheusMetricScan context) { + public PhysicalPlan visitRelation(LogicalRelation node, PrometheusMetricScan context) { PrometheusMetricTable prometheusMetricTable = (PrometheusMetricTable) node.getTable(); String query = SeriesSelectionQueryBuilder.build(node.getRelationName(), null); context.getRequest().setPromQl(query); setTimeRangeParameters(null, context); - context.getRequest() - .setStep(StepParameterResolver.resolve(context.getRequest().getStartTime(), - context.getRequest().getEndTime(), null)); + context + .getRequest() + .setStep( + StepParameterResolver.resolve( + context.getRequest().getStartTime(), context.getRequest().getEndTime(), null)); return context; } @@ -110,8 +108,8 @@ private void setTimeRangeParameters(Expression filter, PrometheusMetricScan cont context.getRequest().setEndTime(timeRange.getSecond()); } - private void setPrometheusResponseFieldNames(PrometheusLogicalMetricAgg node, - PrometheusMetricScan context) { + private void setPrometheusResponseFieldNames( + PrometheusLogicalMetricAgg node, PrometheusMetricScan context) { Optional spanExpression = getSpanExpression(node.getGroupByList()); if (spanExpression.isEmpty()) { throw new RuntimeException( @@ -133,6 +131,4 @@ private Optional getSpanExpression(List namedE .filter(expression -> expression.getDelegated() instanceof SpanExpression) .findFirst(); } - - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/PrometheusResponseFieldNames.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/PrometheusResponseFieldNames.java index d3a6ef184f..303ace7906 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/PrometheusResponseFieldNames.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/PrometheusResponseFieldNames.java @@ -17,7 +17,6 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.expression.NamedExpression; - @Getter @Setter public class PrometheusResponseFieldNames { @@ -26,5 +25,4 @@ public class PrometheusResponseFieldNames { private ExprType valueType = DOUBLE; private String timestampFieldName = TIMESTAMP; private List groupByList; - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/QueryRangeParameters.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/QueryRangeParameters.java index 86ca99cea8..02187c5662 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/QueryRangeParameters.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/model/QueryRangeParameters.java @@ -21,5 +21,4 @@ public class QueryRangeParameters { private Long start; private Long end; private String step; - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/AggregationQueryBuilder.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/AggregationQueryBuilder.java index a141707077..540e2d8cf4 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/AggregationQueryBuilder.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/AggregationQueryBuilder.java @@ -18,49 +18,51 @@ import org.opensearch.sql.expression.span.SpanExpression; /** - * This class builds aggregation query for the given stats commands. - * In the generated query a placeholder(%s) is added in place of metric selection query - * and later replaced by metric selection query. + * This class builds aggregation query for the given stats commands. In the generated query a + * placeholder(%s) is added in place of metric selection query and later replaced by metric + * selection query. */ @NoArgsConstructor public class AggregationQueryBuilder { - private static final Set allowedStatsFunctions = Set.of( - BuiltinFunctionName.MAX.getName().getFunctionName(), - BuiltinFunctionName.MIN.getName().getFunctionName(), - BuiltinFunctionName.COUNT.getName().getFunctionName(), - BuiltinFunctionName.SUM.getName().getFunctionName(), - BuiltinFunctionName.AVG.getName().getFunctionName() - ); - + private static final Set allowedStatsFunctions = + Set.of( + BuiltinFunctionName.MAX.getName().getFunctionName(), + BuiltinFunctionName.MIN.getName().getFunctionName(), + BuiltinFunctionName.COUNT.getName().getFunctionName(), + BuiltinFunctionName.SUM.getName().getFunctionName(), + BuiltinFunctionName.AVG.getName().getFunctionName()); /** * Build Aggregation query from series selector query from expression. * * @return query string. */ - public static String build(List namedAggregatorList, - List groupByList) { + public static String build( + List namedAggregatorList, List groupByList) { if (namedAggregatorList.size() > 1) { throw new RuntimeException( "Prometheus Catalog doesn't multiple aggregations in stats command"); } - if (!allowedStatsFunctions - .contains(namedAggregatorList.get(0).getFunctionName().getFunctionName())) { - throw new RuntimeException(String.format( - "Prometheus Catalog only supports %s aggregations.", allowedStatsFunctions)); + if (!allowedStatsFunctions.contains( + namedAggregatorList.get(0).getFunctionName().getFunctionName())) { + throw new RuntimeException( + String.format( + "Prometheus Catalog only supports %s aggregations.", allowedStatsFunctions)); } StringBuilder aggregateQuery = new StringBuilder(); - aggregateQuery.append(namedAggregatorList.get(0).getFunctionName().getFunctionName()) + aggregateQuery + .append(namedAggregatorList.get(0).getFunctionName().getFunctionName()) .append(" "); if (groupByList != null && !groupByList.isEmpty()) { - groupByList = groupByList.stream() - .filter(expression -> !(expression.getDelegated() instanceof SpanExpression)) - .collect(Collectors.toList()); + groupByList = + groupByList.stream() + .filter(expression -> !(expression.getDelegated() instanceof SpanExpression)) + .collect(Collectors.toList()); if (groupByList.size() > 0) { aggregateQuery.append("by("); aggregateQuery.append( @@ -78,5 +80,4 @@ public static String build(List namedAggregatorList, .append("(%s))"); return aggregateQuery.toString(); } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java index 461b5341f8..d824fcb5b3 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/SeriesSelectionQueryBuilder.java @@ -7,7 +7,6 @@ package org.opensearch.sql.prometheus.storage.querybuilder; - import static org.opensearch.sql.prometheus.data.constants.PrometheusFieldConstants.TIMESTAMP; import java.util.stream.Collectors; @@ -19,14 +18,10 @@ import org.opensearch.sql.expression.ReferenceExpression; import org.opensearch.sql.expression.function.BuiltinFunctionName; -/** - * This class builds metric selection query from the filter condition - * and metric name. - */ +/** This class builds metric selection query from the filter condition and metric name. */ @NoArgsConstructor public class SeriesSelectionQueryBuilder { - /** * Build Prometheus series selector query from expression. * @@ -35,8 +30,8 @@ public class SeriesSelectionQueryBuilder { */ public static String build(String metricName, Expression filterCondition) { if (filterCondition != null) { - SeriesSelectionExpressionNodeVisitor seriesSelectionExpressionNodeVisitor - = new SeriesSelectionExpressionNodeVisitor(); + SeriesSelectionExpressionNodeVisitor seriesSelectionExpressionNodeVisitor = + new SeriesSelectionExpressionNodeVisitor(); String selectorQuery = filterCondition.accept(seriesSelectionExpressionNodeVisitor, null); if (selectorQuery != null) { return metricName + "{" + selectorQuery + "}"; @@ -54,9 +49,9 @@ public String visitFunction(FunctionExpression func, Object context) { .filter(StringUtils::isNotEmpty) .collect(Collectors.joining(" , ")); } else if ((BuiltinFunctionName.LTE.getName().equals(func.getFunctionName()) - || BuiltinFunctionName.GTE.getName().equals(func.getFunctionName()) - || BuiltinFunctionName.LESS.getName().equals(func.getFunctionName()) - || BuiltinFunctionName.GREATER.getName().equals(func.getFunctionName())) + || BuiltinFunctionName.GTE.getName().equals(func.getFunctionName()) + || BuiltinFunctionName.LESS.getName().equals(func.getFunctionName()) + || BuiltinFunctionName.GREATER.getName().equals(func.getFunctionName())) && ((ReferenceExpression) func.getArguments().get(0)).getAttr().equals(TIMESTAMP)) { return null; } else if (BuiltinFunctionName.EQUAL.getName().equals(func.getFunctionName())) { @@ -65,11 +60,10 @@ public String visitFunction(FunctionExpression func, Object context) { + func.getArguments().get(1); } else { throw new RuntimeException( - String.format("Prometheus Datasource doesn't support %s " - + "in where command.", + String.format( + "Prometheus Datasource doesn't support %s " + "in where command.", func.getFunctionName().getFunctionName())); } } } - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/StepParameterResolver.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/StepParameterResolver.java index 2078950a5d..4c23ea9086 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/StepParameterResolver.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/StepParameterResolver.java @@ -15,25 +15,20 @@ import org.opensearch.sql.expression.NamedExpression; import org.opensearch.sql.expression.span.SpanExpression; -/** - * This class resolves step parameter required for - * query_range api of prometheus. - */ +/** This class resolves step parameter required for query_range api of prometheus. */ @NoArgsConstructor public class StepParameterResolver { /** - * Extract step from groupByList or apply heuristic arithmetic - * on endTime and startTime. - * + * Extract step from groupByList or apply heuristic arithmetic on endTime and startTime. * * @param startTime startTime. * @param endTime endTime. * @param groupByList groupByList. * @return Step String. */ - public static String resolve(@NonNull Long startTime, @NonNull Long endTime, - List groupByList) { + public static String resolve( + @NonNull Long startTime, @NonNull Long endTime, List groupByList) { Optional spanExpression = getSpanExpression(groupByList); if (spanExpression.isPresent()) { if (StringUtils.isEmpty(spanExpression.get().getUnit().getName())) { @@ -48,7 +43,7 @@ public static String resolve(@NonNull Long startTime, @NonNull Long endTime, } private static Optional getSpanExpression( - List namedExpressionList) { + List namedExpressionList) { if (namedExpressionList == null) { return Optional.empty(); } @@ -57,7 +52,4 @@ private static Optional getSpanExpression( .map(expression -> (SpanExpression) expression.getDelegated()) .findFirst(); } - - - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/TimeRangeParametersResolver.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/TimeRangeParametersResolver.java index b462f6bafe..c7766f22d6 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/TimeRangeParametersResolver.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/querybuilder/TimeRangeParametersResolver.java @@ -22,16 +22,14 @@ @NoArgsConstructor public class TimeRangeParametersResolver extends ExpressionNodeVisitor { - private Long startTime; private Long endTime; /** - * Build Range Query Parameters from filter expression. - * If the filter condition consists of @timestamp, startTime and - * endTime are derived. or else it will be defaulted to now() and now()-1hr. - * If one of starttime and endtime are provided, the other will be derived from them - * by fixing the time range duration to 1hr. + * Build Range Query Parameters from filter expression. If the filter condition consists + * of @timestamp, startTime and endTime are derived. or else it will be defaulted to now() and + * now()-1hr. If one of starttime and endtime are provided, the other will be derived from them by + * fixing the time range duration to 1hr. * * @param filterCondition expression. * @return query string @@ -72,13 +70,10 @@ public Void visitFunction(FunctionExpression func, Object context) { } } } else { - func.getArguments() - .stream() + func.getArguments().stream() .filter(arg -> arg instanceof FunctionExpression) .forEach(arg -> visitFunction((FunctionExpression) arg, context)); } return null; } - - } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTable.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTable.java index dca946da57..b5557e7298 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTable.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTable.java @@ -5,7 +5,6 @@ package org.opensearch.sql.prometheus.storage.system; - import static org.opensearch.sql.utils.SystemIndexUtils.systemTable; import com.google.common.annotations.VisibleForTesting; @@ -25,13 +24,9 @@ import org.opensearch.sql.storage.Table; import org.opensearch.sql.utils.SystemIndexUtils; -/** - * Prometheus System Table Implementation. - */ +/** Prometheus System Table Implementation. */ public class PrometheusSystemTable implements Table { - /** - * System Index Name. - */ + /** System Index Name. */ private final Pair systemIndexBundle; private final DataSourceSchemaName dataSourceSchemaName; @@ -54,8 +49,7 @@ public PhysicalPlan implement(LogicalPlan plan) { @VisibleForTesting @RequiredArgsConstructor - public class PrometheusSystemTableDefaultImplementor - extends DefaultImplementor { + public class PrometheusSystemTableDefaultImplementor extends DefaultImplementor { @Override public PhysicalPlan visitRelation(LogicalRelation node, Object context) { @@ -67,12 +61,14 @@ private Pair buildIndexBun PrometheusClient client, String indexName) { SystemIndexUtils.SystemTable systemTable = systemTable(indexName); if (systemTable.isSystemInfoTable()) { - return Pair.of(PrometheusSystemTableSchema.SYS_TABLE_TABLES, + return Pair.of( + PrometheusSystemTableSchema.SYS_TABLE_TABLES, new PrometheusListMetricsRequest(client, dataSourceSchemaName)); } else { - return Pair.of(PrometheusSystemTableSchema.SYS_TABLE_MAPPINGS, - new PrometheusDescribeMetricRequest(client, - dataSourceSchemaName, systemTable.getTableName())); + return Pair.of( + PrometheusSystemTableSchema.SYS_TABLE_MAPPINGS, + new PrometheusDescribeMetricRequest( + client, dataSourceSchemaName, systemTable.getTableName())); } } } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScan.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScan.java index 5c0bc656fe..907e8a0c15 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScan.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScan.java @@ -13,16 +13,13 @@ import org.opensearch.sql.prometheus.request.system.PrometheusSystemRequest; import org.opensearch.sql.storage.TableScanOperator; -/** - * Prometheus table scan operator. - */ +/** Prometheus table scan operator. */ @RequiredArgsConstructor @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false) @ToString(onlyExplicitlyIncluded = true) public class PrometheusSystemTableScan extends TableScanOperator { - @EqualsAndHashCode.Include - private final PrometheusSystemRequest request; + @EqualsAndHashCode.Include private final PrometheusSystemRequest request; private Iterator iterator; diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableSchema.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableSchema.java index 668a208c79..9272731dce 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableSchema.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableSchema.java @@ -18,22 +18,23 @@ @Getter @RequiredArgsConstructor public enum PrometheusSystemTableSchema { - - SYS_TABLE_TABLES(new ImmutableMap.Builder() - .put("TABLE_CATALOG", STRING) - .put("TABLE_SCHEMA", STRING) - .put("TABLE_NAME", STRING) - .put("TABLE_TYPE", STRING) - .put("UNIT", STRING) - .put("REMARKS", STRING) - .build()), - SYS_TABLE_MAPPINGS(new ImmutableMap.Builder() - .put("TABLE_CATALOG", STRING) - .put("TABLE_SCHEMA", STRING) - .put("TABLE_NAME", STRING) - .put("COLUMN_NAME", STRING) - .put("DATA_TYPE", STRING) - .build()); + SYS_TABLE_TABLES( + new ImmutableMap.Builder() + .put("TABLE_CATALOG", STRING) + .put("TABLE_SCHEMA", STRING) + .put("TABLE_NAME", STRING) + .put("TABLE_TYPE", STRING) + .put("UNIT", STRING) + .put("REMARKS", STRING) + .build()), + SYS_TABLE_MAPPINGS( + new ImmutableMap.Builder() + .put("TABLE_CATALOG", STRING) + .put("TABLE_SCHEMA", STRING) + .put("TABLE_NAME", STRING) + .put("COLUMN_NAME", STRING) + .put("DATA_TYPE", STRING) + .build()); private final Map mapping; } diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/utils/TableFunctionUtils.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/utils/TableFunctionUtils.java index 35edc83614..24bec1ede3 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/utils/TableFunctionUtils.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/utils/TableFunctionUtils.java @@ -16,52 +16,54 @@ import org.opensearch.sql.expression.Expression; import org.opensearch.sql.expression.NamedArgumentExpression; -/** - * Utility class for common table function methods. - */ +/** Utility class for common table function methods. */ @UtilityClass public class TableFunctionUtils { /** - * Validates if function arguments are valid - * in both the cases when the arguments are passed by position or name. + * Validates if function arguments are valid in both the cases when the arguments are passed by + * position or name. * * @param arguments arguments of function provided in the input order. * @param argumentNames ordered argument names of the function. */ - public static void validatePrometheusTableFunctionArguments(List arguments, - List argumentNames) { - Boolean argumentsPassedByName = arguments.stream() - .noneMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); - Boolean argumentsPassedByPosition = arguments.stream() - .allMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); + public static void validatePrometheusTableFunctionArguments( + List arguments, List argumentNames) { + Boolean argumentsPassedByName = + arguments.stream() + .noneMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); + Boolean argumentsPassedByPosition = + arguments.stream() + .allMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); if (!(argumentsPassedByName || argumentsPassedByPosition)) { throw new SemanticCheckException("Arguments should be either passed by name or position"); } if (arguments.size() != argumentNames.size()) { throw new SemanticCheckException( - generateErrorMessageForMissingArguments(argumentsPassedByPosition, arguments, - argumentNames)); + generateErrorMessageForMissingArguments( + argumentsPassedByPosition, arguments, argumentNames)); } } /** - * Get Named Arguments of Table Function Arguments. - * If they are passed by position create new ones or else return the same arguments passed. + * Get Named Arguments of Table Function Arguments. If they are passed by position create new ones + * or else return the same arguments passed. * * @param arguments arguments of function provided in the input order. * @param argumentNames ordered argument names of the function. */ - public static List getNamedArgumentsOfTableFunction(List arguments, - List argumentNames) { - boolean argumentsPassedByPosition = arguments.stream() - .allMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); + public static List getNamedArgumentsOfTableFunction( + List arguments, List argumentNames) { + boolean argumentsPassedByPosition = + arguments.stream() + .allMatch(arg -> StringUtils.isEmpty(((NamedArgumentExpression) arg).getArgName())); if (argumentsPassedByPosition) { List namedArguments = new ArrayList<>(); for (int i = 0; i < arguments.size(); i++) { - namedArguments.add(new NamedArgumentExpression(argumentNames.get(i), - ((NamedArgumentExpression) arguments.get(i)).getValue())); + namedArguments.add( + new NamedArgumentExpression( + argumentNames.get(i), ((NamedArgumentExpression) arguments.get(i)).getValue())); } return namedArguments; } @@ -73,17 +75,17 @@ private static String generateErrorMessageForMissingArguments( List arguments, List argumentNames) { if (areArgumentsPassedByPosition) { - return String.format("Missing arguments:[%s]", + return String.format( + "Missing arguments:[%s]", String.join(",", argumentNames.subList(arguments.size(), argumentNames.size()))); } else { Set requiredArguments = new HashSet<>(argumentNames); Set providedArguments = - arguments.stream().map(expression -> ((NamedArgumentExpression) expression).getArgName()) + arguments.stream() + .map(expression -> ((NamedArgumentExpression) expression).getArgName()) .collect(Collectors.toSet()); requiredArguments.removeAll(providedArguments); return String.format("Missing arguments:[%s]", String.join(",", requiredArguments)); } } - - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java index b26a45e301..735a1a1052 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/client/PrometheusClientImplTest.java @@ -43,7 +43,6 @@ public class PrometheusClientImplTest { private MockWebServer mockWebServer; private PrometheusClient prometheusClient; - @BeforeEach void setUp() throws IOException { this.mockWebServer = new MockWebServer(); @@ -52,13 +51,13 @@ void setUp() throws IOException { new PrometheusClientImpl(new OkHttpClient(), mockWebServer.url("").uri().normalize()); } - @Test @SneakyThrows void testQueryRange() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody(getJson("query_range_response.json")); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setBody(getJson("query_range_response.json")); mockWebServer.enqueue(mockResponse); JSONObject jsonObject = prometheusClient.queryRange(QUERY, STARTTIME, ENDTIME, STEP); assertTrue(new JSONObject(getJson("query_range_result.json")).similar(jsonObject)); @@ -69,13 +68,15 @@ void testQueryRange() { @Test @SneakyThrows void testQueryRangeWith2xxStatusAndError() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody(getJson("error_response.json")); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setBody(getJson("error_response.json")); mockWebServer.enqueue(mockResponse); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, - () -> prometheusClient.queryRange(QUERY, STARTTIME, ENDTIME, STEP)); + RuntimeException runtimeException = + assertThrows( + RuntimeException.class, + () -> prometheusClient.queryRange(QUERY, STARTTIME, ENDTIME, STEP)); assertEquals("Error", runtimeException.getMessage()); RecordedRequest recordedRequest = mockWebServer.takeRequest(); verifyQueryRangeCall(recordedRequest); @@ -84,13 +85,15 @@ void testQueryRangeWith2xxStatusAndError() { @Test @SneakyThrows void testQueryRangeWithNon2xxError() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setResponseCode(400); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setResponseCode(400); mockWebServer.enqueue(mockResponse); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, - () -> prometheusClient.queryRange(QUERY, STARTTIME, ENDTIME, STEP)); + RuntimeException runtimeException = + assertThrows( + RuntimeException.class, + () -> prometheusClient.queryRange(QUERY, STARTTIME, ENDTIME, STEP)); assertTrue( runtimeException.getMessage().contains("Request to Prometheus is Unsuccessful with :")); RecordedRequest recordedRequest = mockWebServer.takeRequest(); @@ -100,16 +103,20 @@ void testQueryRangeWithNon2xxError() { @Test @SneakyThrows void testGetLabel() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody(getJson("get_labels_response.json")); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setBody(getJson("get_labels_response.json")); mockWebServer.enqueue(mockResponse); List response = prometheusClient.getLabels(METRIC_NAME); - assertEquals(new ArrayList() {{ - add("call"); - add("code"); - } - }, response); + assertEquals( + new ArrayList() { + { + add("call"); + add("code"); + } + }, + response); RecordedRequest recordedRequest = mockWebServer.takeRequest(); verifyGetLabelsCall(recordedRequest); } @@ -117,30 +124,34 @@ void testGetLabel() { @Test @SneakyThrows void testGetAllMetrics() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody(getJson("all_metrics_response.json")); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setBody(getJson("all_metrics_response.json")); mockWebServer.enqueue(mockResponse); Map> response = prometheusClient.getAllMetrics(); Map> expected = new HashMap<>(); - expected.put("go_gc_duration_seconds", - Collections.singletonList(new MetricMetadata("summary", - "A summary of the pause duration of garbage collection cycles.", ""))); - expected.put("go_goroutines", - Collections.singletonList(new MetricMetadata("gauge", - "Number of goroutines that currently exist.", ""))); + expected.put( + "go_gc_duration_seconds", + Collections.singletonList( + new MetricMetadata( + "summary", "A summary of the pause duration of garbage collection cycles.", ""))); + expected.put( + "go_goroutines", + Collections.singletonList( + new MetricMetadata("gauge", "Number of goroutines that currently exist.", ""))); assertEquals(expected, response); RecordedRequest recordedRequest = mockWebServer.takeRequest(); verifyGetAllMetricsCall(recordedRequest); } - @Test @SneakyThrows void testQueryExemplars() { - MockResponse mockResponse = new MockResponse() - .addHeader("Content-Type", "application/json; charset=utf-8") - .setBody(getJson("query_exemplars_response.json")); + MockResponse mockResponse = + new MockResponse() + .addHeader("Content-Type", "application/json; charset=utf-8") + .setBody(getJson("query_exemplars_response.json")); mockWebServer.enqueue(mockResponse); JSONArray jsonArray = prometheusClient.queryExemplars(QUERY, STARTTIME, ENDTIME); assertTrue(new JSONArray(getJson("query_exemplars_result.json")).similar(jsonArray)); diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarsFunctionImplementationTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarsFunctionImplementationTest.java index d6e4a5cef4..6009d3229c 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarsFunctionImplementationTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryExemplarsFunctionImplementationTest.java @@ -25,29 +25,31 @@ import org.opensearch.sql.prometheus.request.PrometheusQueryExemplarsRequest; import org.opensearch.sql.prometheus.storage.QueryExemplarsTable; - @ExtendWith(MockitoExtension.class) class QueryExemplarsFunctionImplementationTest { - @Mock - private PrometheusClient client; - + @Mock private PrometheusClient client; @Test void testValueOfAndTypeAndToString() { FunctionName functionName = new FunctionName("query_exemplars"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(12345))); - QueryExemplarFunctionImplementation queryExemplarFunctionImplementation - = + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(12345))); + QueryExemplarFunctionImplementation queryExemplarFunctionImplementation = new QueryExemplarFunctionImplementation(functionName, namedArgumentExpressionList, client); - UnsupportedOperationException exception = assertThrows(UnsupportedOperationException.class, - () -> queryExemplarFunctionImplementation.valueOf()); - assertEquals("Prometheus defined function [query_exemplars] is only " - + "supported in SOURCE clause with prometheus connector catalog", exception.getMessage()); - assertEquals("query_exemplars(query=\"http_latency\", starttime=12345, endtime=12345)", + UnsupportedOperationException exception = + assertThrows( + UnsupportedOperationException.class, + () -> queryExemplarFunctionImplementation.valueOf()); + assertEquals( + "Prometheus defined function [query_exemplars] is only " + + "supported in SOURCE clause with prometheus connector catalog", + exception.getMessage()); + assertEquals( + "query_exemplars(query=\"http_latency\", starttime=12345, endtime=12345)", queryExemplarFunctionImplementation.toString()); assertEquals(ExprCoreType.STRUCT, queryExemplarFunctionImplementation.type()); } @@ -55,15 +57,15 @@ void testValueOfAndTypeAndToString() { @Test void testApplyArguments() { FunctionName functionName = new FunctionName("query_exemplars"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(1234))); - QueryExemplarFunctionImplementation queryExemplarFunctionImplementation - = + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(1234))); + QueryExemplarFunctionImplementation queryExemplarFunctionImplementation = new QueryExemplarFunctionImplementation(functionName, namedArgumentExpressionList, client); - QueryExemplarsTable queryExemplarsTable - = (QueryExemplarsTable) queryExemplarFunctionImplementation.applyArguments(); + QueryExemplarsTable queryExemplarsTable = + (QueryExemplarsTable) queryExemplarFunctionImplementation.applyArguments(); assertNotNull(queryExemplarsTable.getExemplarsRequest()); PrometheusQueryExemplarsRequest request = queryExemplarsTable.getExemplarsRequest(); assertEquals("http_latency", request.getQuery()); @@ -74,17 +76,17 @@ void testApplyArguments() { @Test void testApplyArgumentsException() { FunctionName functionName = new FunctionName("query_exemplars"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("end_time", DSL.literal(1234))); - QueryExemplarFunctionImplementation queryExemplarFunctionImplementation - = + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("end_time", DSL.literal(1234))); + QueryExemplarFunctionImplementation queryExemplarFunctionImplementation = new QueryExemplarFunctionImplementation(functionName, namedArgumentExpressionList, client); - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> queryExemplarFunctionImplementation.applyArguments()); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> queryExemplarFunctionImplementation.applyArguments()); assertEquals("Invalid Function Argument:end_time", exception.getMessage()); } - - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementationTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementationTest.java index 48337e3f02..288bc35b0f 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementationTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/implementation/QueryRangeFunctionImplementationTest.java @@ -26,29 +26,31 @@ import org.opensearch.sql.prometheus.request.PrometheusQueryRequest; import org.opensearch.sql.prometheus.storage.PrometheusMetricTable; - @ExtendWith(MockitoExtension.class) class QueryRangeFunctionImplementationTest { - @Mock - private PrometheusClient client; - + @Mock private PrometheusClient client; @Test void testValueOfAndTypeAndToString() { FunctionName functionName = new FunctionName("query_range"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(12345)), - DSL.namedArgument("step", DSL.literal(14))); - QueryRangeFunctionImplementation queryRangeFunctionImplementation - = new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); - UnsupportedOperationException exception = assertThrows(UnsupportedOperationException.class, - () -> queryRangeFunctionImplementation.valueOf()); - assertEquals("Prometheus defined function [query_range] is only " - + "supported in SOURCE clause with prometheus connector catalog", exception.getMessage()); - assertEquals("query_range(query=\"http_latency\", starttime=12345, endtime=12345, step=14)", + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(12345)), + DSL.namedArgument("step", DSL.literal(14))); + QueryRangeFunctionImplementation queryRangeFunctionImplementation = + new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); + UnsupportedOperationException exception = + assertThrows( + UnsupportedOperationException.class, () -> queryRangeFunctionImplementation.valueOf()); + assertEquals( + "Prometheus defined function [query_range] is only " + + "supported in SOURCE clause with prometheus connector catalog", + exception.getMessage()); + assertEquals( + "query_range(query=\"http_latency\", starttime=12345, endtime=12345, step=14)", queryRangeFunctionImplementation.toString()); assertEquals(ExprCoreType.STRUCT, queryRangeFunctionImplementation.type()); } @@ -56,19 +58,20 @@ void testValueOfAndTypeAndToString() { @Test void testApplyArguments() { FunctionName functionName = new FunctionName("query_range"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(1234)), - DSL.namedArgument("step", DSL.literal(14))); - QueryRangeFunctionImplementation queryRangeFunctionImplementation - = new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); - PrometheusMetricTable prometheusMetricTable - = (PrometheusMetricTable) queryRangeFunctionImplementation.applyArguments(); + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(1234)), + DSL.namedArgument("step", DSL.literal(14))); + QueryRangeFunctionImplementation queryRangeFunctionImplementation = + new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); + PrometheusMetricTable prometheusMetricTable = + (PrometheusMetricTable) queryRangeFunctionImplementation.applyArguments(); assertNull(prometheusMetricTable.getMetricName()); assertNotNull(prometheusMetricTable.getPrometheusQueryRequest()); - PrometheusQueryRequest prometheusQueryRequest - = prometheusMetricTable.getPrometheusQueryRequest(); + PrometheusQueryRequest prometheusQueryRequest = + prometheusMetricTable.getPrometheusQueryRequest(); assertEquals("http_latency", prometheusQueryRequest.getPromQl().toString()); assertEquals(12345, prometheusQueryRequest.getStartTime()); assertEquals(1234, prometheusQueryRequest.getEndTime()); @@ -78,17 +81,18 @@ void testApplyArguments() { @Test void testApplyArgumentsException() { FunctionName functionName = new FunctionName("query_range"); - List namedArgumentExpressionList - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("end_time", DSL.literal(1234)), - DSL.namedArgument("step", DSL.literal(14))); - QueryRangeFunctionImplementation queryRangeFunctionImplementation - = new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> queryRangeFunctionImplementation.applyArguments()); + List namedArgumentExpressionList = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("end_time", DSL.literal(1234)), + DSL.namedArgument("step", DSL.literal(14))); + QueryRangeFunctionImplementation queryRangeFunctionImplementation = + new QueryRangeFunctionImplementation(functionName, namedArgumentExpressionList, client); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> queryRangeFunctionImplementation.applyArguments()); assertEquals("Invalid Function Argument:end_time", exception.getMessage()); } - - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolverTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolverTest.java index 3e26b46c8f..af8ebf48e2 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolverTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolverTest.java @@ -35,34 +35,34 @@ @ExtendWith(MockitoExtension.class) class QueryExemplarsTableFunctionResolverTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; - @Mock - private FunctionProperties functionProperties; + @Mock private FunctionProperties functionProperties; @Test void testResolve() { - QueryExemplarsTableFunctionResolver queryExemplarsTableFunctionResolver - = new QueryExemplarsTableFunctionResolver(client); + QueryExemplarsTableFunctionResolver queryExemplarsTableFunctionResolver = + new QueryExemplarsTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_exemplars"); - List expressions - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(12345))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - Pair resolution - = queryExemplarsTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(12345))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + Pair resolution = + queryExemplarsTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryExemplarsTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG), resolution.getKey().getParamTypeList()); FunctionBuilder functionBuilder = resolution.getValue(); - TableFunctionImplementation functionImplementation - = (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); + TableFunctionImplementation functionImplementation = + (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); assertTrue(functionImplementation instanceof QueryExemplarFunctionImplementation); - QueryExemplarsTable queryExemplarsTable - = (QueryExemplarsTable) functionImplementation.applyArguments(); + QueryExemplarsTable queryExemplarsTable = + (QueryExemplarsTable) functionImplementation.applyArguments(); assertNotNull(queryExemplarsTable.getExemplarsRequest()); PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = queryExemplarsTable.getExemplarsRequest(); @@ -70,5 +70,4 @@ void testResolve() { assertEquals(12345L, prometheusQueryExemplarsRequest.getStartTime()); assertEquals(12345L, prometheusQueryExemplarsRequest.getEndTime()); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolverTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolverTest.java index 2a36600379..48050bcb15 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolverTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/resolver/QueryRangeTableFunctionResolverTest.java @@ -37,35 +37,35 @@ @ExtendWith(MockitoExtension.class) class QueryRangeTableFunctionResolverTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; - @Mock - private FunctionProperties functionProperties; + @Mock private FunctionProperties functionProperties; @Test void testResolve() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("starttime", DSL.literal(12345)), - DSL.namedArgument("endtime", DSL.literal(12345)), - DSL.namedArgument("step", DSL.literal(14))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("starttime", DSL.literal(12345)), + DSL.namedArgument("endtime", DSL.literal(12345)), + DSL.namedArgument("step", DSL.literal(14))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); FunctionBuilder functionBuilder = resolution.getValue(); - TableFunctionImplementation functionImplementation - = (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); + TableFunctionImplementation functionImplementation = + (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); assertTrue(functionImplementation instanceof QueryRangeFunctionImplementation); - PrometheusMetricTable prometheusMetricTable - = (PrometheusMetricTable) functionImplementation.applyArguments(); + PrometheusMetricTable prometheusMetricTable = + (PrometheusMetricTable) functionImplementation.applyArguments(); assertNotNull(prometheusMetricTable.getPrometheusQueryRequest()); PrometheusQueryRequest prometheusQueryRequest = prometheusMetricTable.getPrometheusQueryRequest(); @@ -77,29 +77,31 @@ void testResolve() { @Test void testArgumentsPassedByPosition() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument(null, DSL.literal("http_latency")), - DSL.namedArgument(null, DSL.literal(12345)), - DSL.namedArgument(null, DSL.literal(12345)), - DSL.namedArgument(null, DSL.literal(14))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument(null, DSL.literal("http_latency")), + DSL.namedArgument(null, DSL.literal(12345)), + DSL.namedArgument(null, DSL.literal(12345)), + DSL.namedArgument(null, DSL.literal(14))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); FunctionBuilder functionBuilder = resolution.getValue(); - TableFunctionImplementation functionImplementation - = (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); + TableFunctionImplementation functionImplementation = + (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); assertTrue(functionImplementation instanceof QueryRangeFunctionImplementation); - PrometheusMetricTable prometheusMetricTable - = (PrometheusMetricTable) functionImplementation.applyArguments(); + PrometheusMetricTable prometheusMetricTable = + (PrometheusMetricTable) functionImplementation.applyArguments(); assertNotNull(prometheusMetricTable.getPrometheusQueryRequest()); PrometheusQueryRequest prometheusQueryRequest = prometheusMetricTable.getPrometheusQueryRequest(); @@ -109,32 +111,33 @@ void testArgumentsPassedByPosition() { assertEquals("14", prometheusQueryRequest.getStep()); } - @Test void testArgumentsPassedByNameWithDifferentOrder() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("endtime", DSL.literal(12345)), - DSL.namedArgument("step", DSL.literal(14)), - DSL.namedArgument("starttime", DSL.literal(12345))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("endtime", DSL.literal(12345)), + DSL.namedArgument("step", DSL.literal(14)), + DSL.namedArgument("starttime", DSL.literal(12345))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); FunctionBuilder functionBuilder = resolution.getValue(); - TableFunctionImplementation functionImplementation - = (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); + TableFunctionImplementation functionImplementation = + (TableFunctionImplementation) functionBuilder.apply(functionProperties, expressions); assertTrue(functionImplementation instanceof QueryRangeFunctionImplementation); - PrometheusMetricTable prometheusMetricTable - = (PrometheusMetricTable) functionImplementation.applyArguments(); + PrometheusMetricTable prometheusMetricTable = + (PrometheusMetricTable) functionImplementation.applyArguments(); assertNotNull(prometheusMetricTable.getPrometheusQueryRequest()); PrometheusQueryRequest prometheusQueryRequest = prometheusMetricTable.getPrometheusQueryRequest(); @@ -146,70 +149,81 @@ void testArgumentsPassedByNameWithDifferentOrder() { @Test void testMixedArgumentTypes() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument(null, DSL.literal(12345)), - DSL.namedArgument(null, DSL.literal(12345)), - DSL.namedArgument(null, DSL.literal(14))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument(null, DSL.literal(12345)), + DSL.namedArgument(null, DSL.literal(12345)), + DSL.namedArgument(null, DSL.literal(14))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); - SemanticCheckException exception = assertThrows(SemanticCheckException.class, - () -> resolution.getValue().apply(functionProperties, expressions)); + SemanticCheckException exception = + assertThrows( + SemanticCheckException.class, + () -> resolution.getValue().apply(functionProperties, expressions)); assertEquals("Arguments should be either passed by name or position", exception.getMessage()); } @Test void testWrongArgumentsSizeWhenPassedByName() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument("query", DSL.literal("http_latency")), - DSL.namedArgument("step", DSL.literal(12345))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument("query", DSL.literal("http_latency")), + DSL.namedArgument("step", DSL.literal(12345))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); - SemanticCheckException exception = assertThrows(SemanticCheckException.class, - () -> resolution.getValue().apply(functionProperties, expressions)); + SemanticCheckException exception = + assertThrows( + SemanticCheckException.class, + () -> resolution.getValue().apply(functionProperties, expressions)); assertEquals("Missing arguments:[endtime,starttime]", exception.getMessage()); } @Test void testWrongArgumentsSizeWhenPassedByPosition() { - QueryRangeTableFunctionResolver queryRangeTableFunctionResolver - = new QueryRangeTableFunctionResolver(client); + QueryRangeTableFunctionResolver queryRangeTableFunctionResolver = + new QueryRangeTableFunctionResolver(client); FunctionName functionName = FunctionName.of("query_range"); - List expressions - = List.of(DSL.namedArgument(null, DSL.literal("http_latency")), - DSL.namedArgument(null, DSL.literal(12345))); - FunctionSignature functionSignature = new FunctionSignature(functionName, expressions - .stream().map(Expression::type).collect(Collectors.toList())); - Pair resolution - = queryRangeTableFunctionResolver.resolve(functionSignature); + List expressions = + List.of( + DSL.namedArgument(null, DSL.literal("http_latency")), + DSL.namedArgument(null, DSL.literal(12345))); + FunctionSignature functionSignature = + new FunctionSignature( + functionName, expressions.stream().map(Expression::type).collect(Collectors.toList())); + Pair resolution = + queryRangeTableFunctionResolver.resolve(functionSignature); assertEquals(functionName, resolution.getKey().getFunctionName()); assertEquals(functionName, queryRangeTableFunctionResolver.getFunctionName()); assertEquals(List.of(STRING, LONG, LONG, STRING), resolution.getKey().getParamTypeList()); - SemanticCheckException exception = assertThrows(SemanticCheckException.class, - () -> resolution.getValue().apply(functionProperties, expressions)); + SemanticCheckException exception = + assertThrows( + SemanticCheckException.class, + () -> resolution.getValue().apply(functionProperties, expressions)); assertEquals("Missing arguments:[endtime,step]", exception.getMessage()); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilderTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilderTest.java index 6fd782b417..bb7806f824 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilderTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanBuilderTest.java @@ -7,7 +7,6 @@ package org.opensearch.sql.prometheus.functions.scan; - import static org.opensearch.sql.prometheus.constants.TestConstants.ENDTIME; import static org.opensearch.sql.prometheus.constants.TestConstants.QUERY; import static org.opensearch.sql.prometheus.constants.TestConstants.STARTTIME; @@ -22,40 +21,35 @@ public class QueryExemplarsFunctionTableScanBuilderTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; - @Mock - private LogicalProject logicalProject; + @Mock private LogicalProject logicalProject; @Test void testBuild() { - PrometheusQueryExemplarsRequest exemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest exemplarsRequest = new PrometheusQueryExemplarsRequest(); exemplarsRequest.setQuery(QUERY); exemplarsRequest.setStartTime(STARTTIME); exemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanBuilder queryExemplarsFunctionTableScanBuilder - = new QueryExemplarsFunctionTableScanBuilder(prometheusClient, exemplarsRequest); - TableScanOperator queryExemplarsFunctionTableScanOperator - = queryExemplarsFunctionTableScanBuilder.build(); + QueryExemplarsFunctionTableScanBuilder queryExemplarsFunctionTableScanBuilder = + new QueryExemplarsFunctionTableScanBuilder(prometheusClient, exemplarsRequest); + TableScanOperator queryExemplarsFunctionTableScanOperator = + queryExemplarsFunctionTableScanBuilder.build(); Assertions.assertNotNull(queryExemplarsFunctionTableScanOperator); - Assertions.assertTrue(queryExemplarsFunctionTableScanOperator - instanceof QueryExemplarsFunctionTableScanOperator); + Assertions.assertTrue( + queryExemplarsFunctionTableScanOperator instanceof QueryExemplarsFunctionTableScanOperator); } @Test void testPushProject() { - PrometheusQueryExemplarsRequest exemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest exemplarsRequest = new PrometheusQueryExemplarsRequest(); exemplarsRequest.setQuery(QUERY); exemplarsRequest.setStartTime(STARTTIME); exemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanBuilder queryExemplarsFunctionTableScanBuilder - = new QueryExemplarsFunctionTableScanBuilder(prometheusClient, exemplarsRequest); - Assertions.assertTrue(queryExemplarsFunctionTableScanBuilder - .pushDownProject(logicalProject)); + QueryExemplarsFunctionTableScanBuilder queryExemplarsFunctionTableScanBuilder = + new QueryExemplarsFunctionTableScanBuilder(prometheusClient, exemplarsRequest); + Assertions.assertTrue(queryExemplarsFunctionTableScanBuilder.pushDownProject(logicalProject)); } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperatorTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperatorTest.java index d4e31d4d1e..5b8cf34fc2 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperatorTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryExemplarsFunctionTableScanOperatorTest.java @@ -41,22 +41,21 @@ @ExtendWith(MockitoExtension.class) public class QueryExemplarsFunctionTableScanOperatorTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; @Test @SneakyThrows void testQueryResponseIterator() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); when(prometheusClient.queryExemplars(any(), any(), any())) .thenReturn(new JSONArray(getJson("query_exemplars_result.json"))); @@ -68,24 +67,28 @@ void testQueryResponseIterator() { seriesLabelsHashMap.put("service", new ExprStringValue("bar")); seriesLabelsHashMap.put("job", new ExprStringValue("prometheus")); LinkedHashMap exemplarMap = new LinkedHashMap<>(); - exemplarMap.put("labels", new ExprTupleValue(new LinkedHashMap<>() { - { - put("traceID", new ExprStringValue("EpTxMJ40fUus7aGY")); - } - }) - ); + exemplarMap.put( + "labels", + new ExprTupleValue( + new LinkedHashMap<>() { + { + put("traceID", new ExprStringValue("EpTxMJ40fUus7aGY")); + } + })); exemplarMap.put("timestamp", new ExprTimestampValue(Instant.ofEpochMilli(1600096945479L))); exemplarMap.put("value", new ExprDoubleValue(6)); List exprValueList = new ArrayList<>(); exprValueList.add(new ExprTupleValue(exemplarMap)); ExprCollectionValue exemplars = new ExprCollectionValue(exprValueList); ExprTupleValue seriesLabels = new ExprTupleValue(seriesLabelsHashMap); - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() { - { - put("seriesLabels", seriesLabels); - put("exemplars", exemplars); - } - }); + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put("seriesLabels", seriesLabels); + put("exemplars", exemplars); + } + }); assertEquals(firstRow, queryExemplarsFunctionTableScanOperator.next()); } @@ -93,15 +96,15 @@ void testQueryResponseIterator() { @Test @SneakyThrows void testEmptyQueryWithNoMatrixKeyInResultJson() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); when(prometheusClient.queryExemplars(any(), any(), any())) .thenReturn(new JSONArray(getJson("query_exemplars_empty_result.json"))); @@ -113,15 +116,15 @@ void testEmptyQueryWithNoMatrixKeyInResultJson() { @SneakyThrows void testQuerySchema() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); when(prometheusClient.queryExemplars(any(), any(), any())) .thenReturn(new JSONArray(getJson("query_exemplars_result.json"))); @@ -140,53 +143,53 @@ void testQuerySchema() { @SneakyThrows void testEmptyQueryWithException() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); when(prometheusClient.queryExemplars(any(), any(), any())) .thenThrow(new IOException("Error Message")); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, queryExemplarsFunctionTableScanOperator::open); - assertEquals("Error fetching data from prometheus server: Error Message", - runtimeException.getMessage()); + RuntimeException runtimeException = + assertThrows(RuntimeException.class, queryExemplarsFunctionTableScanOperator::open); + assertEquals( + "Error fetching data from prometheus server: Error Message", runtimeException.getMessage()); } - @Test @SneakyThrows void testExplain() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); - Assertions.assertEquals("query_exemplars(test_query, 1664767694133, 1664771294133)", + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); + Assertions.assertEquals( + "query_exemplars(test_query, 1664767694133, 1664771294133)", queryExemplarsFunctionTableScanOperator.explain()); } @Test @SneakyThrows void testClose() { - PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest prometheusQueryExemplarsRequest = + new PrometheusQueryExemplarsRequest(); prometheusQueryExemplarsRequest.setQuery(QUERY); prometheusQueryExemplarsRequest.setStartTime(STARTTIME); prometheusQueryExemplarsRequest.setEndTime(ENDTIME); - QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator - = new QueryExemplarsFunctionTableScanOperator(prometheusClient, - prometheusQueryExemplarsRequest); + QueryExemplarsFunctionTableScanOperator queryExemplarsFunctionTableScanOperator = + new QueryExemplarsFunctionTableScanOperator( + prometheusClient, prometheusQueryExemplarsRequest); queryExemplarsFunctionTableScanOperator.close(); } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilderTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilderTest.java index 8532a35395..dca79d6905 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilderTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanBuilderTest.java @@ -7,7 +7,6 @@ package org.opensearch.sql.prometheus.functions.scan; - import static org.opensearch.sql.prometheus.constants.TestConstants.ENDTIME; import static org.opensearch.sql.prometheus.constants.TestConstants.QUERY; import static org.opensearch.sql.prometheus.constants.TestConstants.STARTTIME; @@ -23,11 +22,9 @@ public class QueryRangeFunctionTableScanBuilderTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; - @Mock - private LogicalProject logicalProject; + @Mock private LogicalProject logicalProject; @Test void testBuild() { @@ -37,13 +34,13 @@ void testBuild() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanBuilder queryRangeFunctionTableScanBuilder - = new QueryRangeFunctionTableScanBuilder(prometheusClient, prometheusQueryRequest); - TableScanOperator queryRangeFunctionTableScanOperator - = queryRangeFunctionTableScanBuilder.build(); + QueryRangeFunctionTableScanBuilder queryRangeFunctionTableScanBuilder = + new QueryRangeFunctionTableScanBuilder(prometheusClient, prometheusQueryRequest); + TableScanOperator queryRangeFunctionTableScanOperator = + queryRangeFunctionTableScanBuilder.build(); Assertions.assertNotNull(queryRangeFunctionTableScanOperator); - Assertions.assertTrue(queryRangeFunctionTableScanOperator - instanceof QueryRangeFunctionTableScanOperator); + Assertions.assertTrue( + queryRangeFunctionTableScanOperator instanceof QueryRangeFunctionTableScanOperator); } @Test @@ -54,8 +51,8 @@ void testPushProject() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanBuilder queryRangeFunctionTableScanBuilder - = new QueryRangeFunctionTableScanBuilder(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanBuilder queryRangeFunctionTableScanBuilder = + new QueryRangeFunctionTableScanBuilder(prometheusClient, prometheusQueryRequest); Assertions.assertTrue(queryRangeFunctionTableScanBuilder.pushDownProject(logicalProject)); } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperatorTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperatorTest.java index b476471153..e59a2bf7c4 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperatorTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/functions/scan/QueryRangeFunctionTableScanOperatorTest.java @@ -45,8 +45,7 @@ @ExtendWith(MockitoExtension.class) class QueryRangeFunctionTableScanOperatorTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; @Test @SneakyThrows @@ -58,41 +57,63 @@ void testQueryResponseIterator() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); when(prometheusClient.queryRange(any(), any(), any(), any())) .thenReturn(new JSONObject(getJson("query_range_result.json"))); queryRangeFunctionTableScanOperator.open(); Assertions.assertTrue(queryRangeFunctionTableScanOperator.hasNext()); - LinkedHashMap labelsMap = new LinkedHashMap<>() {{ - put("instance", new ExprStringValue("localhost:9090")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("prometheus")); - }}; - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(LABELS, new ExprTupleValue(labelsMap)); - put(TIMESTAMP, new ExprCollectionValue(Collections - .singletonList(new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))))); - put(VALUE, new ExprCollectionValue(Collections.singletonList(new ExprDoubleValue(1)))); - } - }); + LinkedHashMap labelsMap = + new LinkedHashMap<>() { + { + put("instance", new ExprStringValue("localhost:9090")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("prometheus")); + } + }; + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(LABELS, new ExprTupleValue(labelsMap)); + put( + TIMESTAMP, + new ExprCollectionValue( + Collections.singletonList( + new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))))); + put( + VALUE, + new ExprCollectionValue(Collections.singletonList(new ExprDoubleValue(1)))); + } + }); assertEquals(firstRow, queryRangeFunctionTableScanOperator.next()); Assertions.assertTrue(queryRangeFunctionTableScanOperator.hasNext()); - LinkedHashMap labelsMap2 = new LinkedHashMap<>() {{ - put("instance", new ExprStringValue("localhost:9091")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("node")); - }}; - ExprTupleValue secondRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(LABELS, new ExprTupleValue(labelsMap2)); - put(TIMESTAMP, new ExprCollectionValue(Collections - .singletonList(new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))))); - put(VALUE, new ExprCollectionValue(Collections.singletonList(new ExprDoubleValue(0)))); - } - }); + LinkedHashMap labelsMap2 = + new LinkedHashMap<>() { + { + put("instance", new ExprStringValue("localhost:9091")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("node")); + } + }; + ExprTupleValue secondRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(LABELS, new ExprTupleValue(labelsMap2)); + put( + TIMESTAMP, + new ExprCollectionValue( + Collections.singletonList( + new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))))); + put( + VALUE, + new ExprCollectionValue(Collections.singletonList(new ExprDoubleValue(0)))); + } + }); assertEquals(secondRow, queryRangeFunctionTableScanOperator.next()); Assertions.assertFalse(queryRangeFunctionTableScanOperator.hasNext()); } @@ -106,16 +127,17 @@ void testEmptyQueryWithNoMatrixKeyInResultJson() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); when(prometheusClient.queryRange(any(), any(), any(), any())) .thenReturn(new JSONObject(getJson("no_matrix_query_range_result.json"))); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, queryRangeFunctionTableScanOperator::open); + RuntimeException runtimeException = + assertThrows(RuntimeException.class, queryRangeFunctionTableScanOperator::open); assertEquals( "Unexpected Result Type: vector during Prometheus Response Parsing. " - + "'matrix' resultType is expected", runtimeException.getMessage()); + + "'matrix' resultType is expected", + runtimeException.getMessage()); } @Test @@ -127,8 +149,8 @@ void testQuerySchema() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); when(prometheusClient.queryRange(any(), any(), any(), any())) .thenReturn(new JSONObject(getJson("query_range_result.json"))); @@ -150,18 +172,17 @@ void testEmptyQueryWithException() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); when(prometheusClient.queryRange(any(), any(), any(), any())) .thenThrow(new IOException("Error Message")); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, queryRangeFunctionTableScanOperator::open); - assertEquals("Error fetching data from prometheus server: Error Message", - runtimeException.getMessage()); + RuntimeException runtimeException = + assertThrows(RuntimeException.class, queryRangeFunctionTableScanOperator::open); + assertEquals( + "Error fetching data from prometheus server: Error Message", runtimeException.getMessage()); } - @Test @SneakyThrows void testExplain() { @@ -171,10 +192,11 @@ void testExplain() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); - Assertions.assertEquals("query_range(test_query, 1664767694133, 1664771294133, 14)", + Assertions.assertEquals( + "query_range(test_query, 1664767694133, 1664771294133, 14)", queryRangeFunctionTableScanOperator.explain()); } @@ -187,8 +209,8 @@ void testClose() { prometheusQueryRequest.setEndTime(ENDTIME); prometheusQueryRequest.setStep(STEP); - QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator - = new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); + QueryRangeFunctionTableScanOperator queryRangeFunctionTableScanOperator = + new QueryRangeFunctionTableScanOperator(prometheusClient, prometheusQueryRequest); queryRangeFunctionTableScanOperator.close(); } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicOptimizerTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicOptimizerTest.java index a1d1cef91d..33c48e2f2d 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicOptimizerTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/planner/logical/PrometheusLogicOptimizerTest.java @@ -32,60 +32,50 @@ @ExtendWith(MockitoExtension.class) public class PrometheusLogicOptimizerTest { - @Mock - private Table table; + @Mock private Table table; @Test void project_filter_merge_with_relation() { assertEquals( project( - indexScan("prometheus_http_total_requests", - DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200")))) - ), + indexScan( + "prometheus_http_total_requests", + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))))), optimize( project( filter( relation("prometheus_http_total_requests", table), - DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))) - )) - ) - ); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))))))); } @Test void aggregation_merge_relation() { assertEquals( project( - indexScanAgg("prometheus_http_total_requests", ImmutableList - .of(DSL.named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), + indexScanAgg( + "prometheus_http_total_requests", + ImmutableList.of(DSL.named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), ImmutableList.of(DSL.named("code", DSL.ref("code", STRING)))), DSL.named("AVG(intV)", DSL.ref("AVG(intV)", DOUBLE))), optimize( project( aggregation( relation("prometheus_http_total_requests", table), - ImmutableList - .of(DSL.named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(DSL.named("code", - DSL.ref("code", STRING)))), - DSL.named("AVG(intV)", DSL.ref("AVG(intV)", DOUBLE))) - ) - ); + ImmutableList.of(DSL.named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of(DSL.named("code", DSL.ref("code", STRING)))), + DSL.named("AVG(intV)", DSL.ref("AVG(intV)", DOUBLE))))); } - @Test void aggregation_merge_filter_relation() { assertEquals( project( - indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(DSL.named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of(DSL.named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), ImmutableList.of(DSL.named("job", DSL.ref("job", STRING)))), DSL.named("AVG(@value)", DSL.ref("AVG(@value)", DOUBLE))), optimize( @@ -94,25 +84,16 @@ void aggregation_merge_filter_relation() { filter( relation("prometheus_http_total_requests", table), DSL.and( - DSL.equal(DSL.ref("code", STRING), - DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), - DSL.literal(stringValue("/ready/")))) - ), - ImmutableList - .of(DSL.named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(DSL.named("job", - DSL.ref("job", STRING)))), - DSL.named("AVG(@value)", DSL.ref("AVG(@value)", DOUBLE))) - ) - ); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))), + ImmutableList.of(DSL.named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of(DSL.named("job", DSL.ref("job", STRING)))), + DSL.named("AVG(@value)", DSL.ref("AVG(@value)", DOUBLE))))); } - private LogicalPlan optimize(LogicalPlan plan) { final LogicalPlanOptimizer optimizer = PrometheusLogicalPlanOptimizerFactory.create(); return optimizer.optimize(plan); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusDescribeMetricRequestTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusDescribeMetricRequestTest.java index dfc9aee7dc..9add7896cf 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusDescribeMetricRequestTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusDescribeMetricRequestTest.java @@ -37,54 +37,61 @@ @ExtendWith(MockitoExtension.class) public class PrometheusDescribeMetricRequestTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; @Test @SneakyThrows void testGetFieldTypes() { - when(prometheusClient.getLabels(METRIC_NAME)).thenReturn(new ArrayList() {{ - add("call"); - add("code"); - } - }); - Map expected = new HashMap<>() {{ - put("call", ExprCoreType.STRING); - put("code", ExprCoreType.STRING); - put(VALUE, ExprCoreType.DOUBLE); - put(TIMESTAMP, ExprCoreType.TIMESTAMP); - }}; - PrometheusDescribeMetricRequest prometheusDescribeMetricRequest - = new PrometheusDescribeMetricRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); + when(prometheusClient.getLabels(METRIC_NAME)) + .thenReturn( + new ArrayList() { + { + add("call"); + add("code"); + } + }); + Map expected = + new HashMap<>() { + { + put("call", ExprCoreType.STRING); + put("code", ExprCoreType.STRING); + put(VALUE, ExprCoreType.DOUBLE); + put(TIMESTAMP, ExprCoreType.TIMESTAMP); + } + }; + PrometheusDescribeMetricRequest prometheusDescribeMetricRequest = + new PrometheusDescribeMetricRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); assertEquals(expected, prometheusDescribeMetricRequest.getFieldTypes()); verify(prometheusClient, times(1)).getLabels(METRIC_NAME); } - @Test @SneakyThrows void testGetFieldTypesWithEmptyMetricName() { - Map expected = new HashMap<>() {{ - put(VALUE, ExprCoreType.DOUBLE); - put(TIMESTAMP, ExprCoreType.TIMESTAMP); - }}; - assertThrows(NullPointerException.class, - () -> new PrometheusDescribeMetricRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "default"), - null)); + Map expected = + new HashMap<>() { + { + put(VALUE, ExprCoreType.DOUBLE); + put(TIMESTAMP, ExprCoreType.TIMESTAMP); + } + }; + assertThrows( + NullPointerException.class, + () -> + new PrometheusDescribeMetricRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "default"), null)); } - @Test @SneakyThrows void testGetFieldTypesWhenException() { when(prometheusClient.getLabels(METRIC_NAME)).thenThrow(new RuntimeException("ERROR Message")); - PrometheusDescribeMetricRequest prometheusDescribeMetricRequest - = new PrometheusDescribeMetricRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); - RuntimeException exception = assertThrows(RuntimeException.class, - prometheusDescribeMetricRequest::getFieldTypes); + PrometheusDescribeMetricRequest prometheusDescribeMetricRequest = + new PrometheusDescribeMetricRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); + RuntimeException exception = + assertThrows(RuntimeException.class, prometheusDescribeMetricRequest::getFieldTypes); verify(prometheusClient, times(1)).getLabels(METRIC_NAME); assertEquals("ERROR Message", exception.getMessage()); } @@ -93,27 +100,30 @@ void testGetFieldTypesWhenException() { @SneakyThrows void testGetFieldTypesWhenIOException() { when(prometheusClient.getLabels(METRIC_NAME)).thenThrow(new IOException("ERROR Message")); - PrometheusDescribeMetricRequest prometheusDescribeMetricRequest - = new PrometheusDescribeMetricRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); - RuntimeException exception = assertThrows(RuntimeException.class, - prometheusDescribeMetricRequest::getFieldTypes); - assertEquals("Error while fetching labels for http_requests_total" - + " from prometheus: ERROR Message", exception.getMessage()); + PrometheusDescribeMetricRequest prometheusDescribeMetricRequest = + new PrometheusDescribeMetricRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "default"), METRIC_NAME); + RuntimeException exception = + assertThrows(RuntimeException.class, prometheusDescribeMetricRequest::getFieldTypes); + assertEquals( + "Error while fetching labels for http_requests_total" + " from prometheus: ERROR Message", + exception.getMessage()); verify(prometheusClient, times(1)).getLabels(METRIC_NAME); } @Test @SneakyThrows void testSearch() { - when(prometheusClient.getLabels(METRIC_NAME)).thenReturn(new ArrayList<>() { - { - add("call"); - } - }); - PrometheusDescribeMetricRequest prometheusDescribeMetricRequest - = new PrometheusDescribeMetricRequest(prometheusClient, - new DataSourceSchemaName("test", "default"), METRIC_NAME); + when(prometheusClient.getLabels(METRIC_NAME)) + .thenReturn( + new ArrayList<>() { + { + add("call"); + } + }); + PrometheusDescribeMetricRequest prometheusDescribeMetricRequest = + new PrometheusDescribeMetricRequest( + prometheusClient, new DataSourceSchemaName("test", "default"), METRIC_NAME); List result = prometheusDescribeMetricRequest.search(); assertEquals(3, result.size()); assertEquals(expectedRow(), result.get(0)); @@ -129,5 +139,4 @@ private ExprValue expectedRow() { valueMap.put("DATA_TYPE", stringValue(ExprCoreType.STRING.legacyTypeName().toLowerCase())); return new ExprTupleValue(valueMap); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusListMetricsRequestTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusListMetricsRequestTest.java index bf5bb22e96..09f63463b5 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusListMetricsRequestTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/request/PrometheusListMetricsRequestTest.java @@ -35,45 +35,46 @@ @ExtendWith(MockitoExtension.class) public class PrometheusListMetricsRequestTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; @Test @SneakyThrows void testSearch() { Map> metricsResult = new HashMap<>(); - metricsResult.put("go_gc_duration_seconds", - Collections.singletonList(new MetricMetadata("summary", - "A summary of the pause duration of garbage collection cycles.", ""))); - metricsResult.put("go_goroutines", - Collections.singletonList(new MetricMetadata("gauge", - "Number of goroutines that currently exist.", ""))); + metricsResult.put( + "go_gc_duration_seconds", + Collections.singletonList( + new MetricMetadata( + "summary", "A summary of the pause duration of garbage collection cycles.", ""))); + metricsResult.put( + "go_goroutines", + Collections.singletonList( + new MetricMetadata("gauge", "Number of goroutines that currently exist.", ""))); when(prometheusClient.getAllMetrics()).thenReturn(metricsResult); - PrometheusListMetricsRequest prometheusListMetricsRequest - = new PrometheusListMetricsRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "information_schema")); + PrometheusListMetricsRequest prometheusListMetricsRequest = + new PrometheusListMetricsRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "information_schema")); List result = prometheusListMetricsRequest.search(); assertEquals(expectedRow(), result.get(0)); assertEquals(2, result.size()); verify(prometheusClient, times(1)).getAllMetrics(); } - @Test @SneakyThrows void testSearchWhenIOException() { when(prometheusClient.getAllMetrics()).thenThrow(new IOException("ERROR Message")); - PrometheusListMetricsRequest prometheusListMetricsRequest - = new PrometheusListMetricsRequest(prometheusClient, - new DataSourceSchemaName("prometheus", "information_schema")); - RuntimeException exception = assertThrows(RuntimeException.class, - prometheusListMetricsRequest::search); - assertEquals("Error while fetching metric list for from prometheus: ERROR Message", + PrometheusListMetricsRequest prometheusListMetricsRequest = + new PrometheusListMetricsRequest( + prometheusClient, new DataSourceSchemaName("prometheus", "information_schema")); + RuntimeException exception = + assertThrows(RuntimeException.class, prometheusListMetricsRequest::search); + assertEquals( + "Error while fetching metric list for from prometheus: ERROR Message", exception.getMessage()); verify(prometheusClient, times(1)).getAllMetrics(); } - private ExprTupleValue expectedRow() { LinkedHashMap valueMap = new LinkedHashMap<>(); valueMap.put("TABLE_CATALOG", stringValue("prometheus")); @@ -81,9 +82,8 @@ private ExprTupleValue expectedRow() { valueMap.put("TABLE_NAME", stringValue("go_gc_duration_seconds")); valueMap.put("TABLE_TYPE", stringValue("summary")); valueMap.put("UNIT", stringValue("")); - valueMap.put("REMARKS", - stringValue("A summary of the pause duration of garbage collection cycles.")); + valueMap.put( + "REMARKS", stringValue("A summary of the pause duration of garbage collection cycles.")); return new ExprTupleValue(valueMap); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScanTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScanTest.java index 9c0207853c..00ddc973bc 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScanTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricScanTest.java @@ -44,8 +44,7 @@ @ExtendWith(MockitoExtension.class) public class PrometheusMetricScanTest { - @Mock - private PrometheusClient prometheusClient; + @Mock private PrometheusClient prometheusClient; @Test @SneakyThrows @@ -60,24 +59,30 @@ void testQueryResponseIterator() { .thenReturn(new JSONObject(getJson("query_range_result.json"))); prometheusMetricScan.open(); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put(VALUE, new ExprDoubleValue(1)); - put("instance", new ExprStringValue("localhost:9090")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("prometheus")); - } - }); + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put(VALUE, new ExprDoubleValue(1)); + put("instance", new ExprStringValue("localhost:9090")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("prometheus")); + } + }); assertEquals(firstRow, prometheusMetricScan.next()); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue secondRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put("@timestamp", new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("@value", new ExprDoubleValue(0)); - put("instance", new ExprStringValue("localhost:9091")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("node")); - } - }); + ExprTupleValue secondRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put("@timestamp", new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("@value", new ExprDoubleValue(0)); + put("instance", new ExprStringValue("localhost:9091")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("node")); + } + }); assertEquals(secondRow, prometheusMetricScan.next()); Assertions.assertFalse(prometheusMetricScan.hasNext()); } @@ -85,8 +90,7 @@ void testQueryResponseIterator() { @Test @SneakyThrows void testQueryResponseIteratorWithGivenPrometheusResponseFieldNames() { - PrometheusResponseFieldNames prometheusResponseFieldNames - = new PrometheusResponseFieldNames(); + PrometheusResponseFieldNames prometheusResponseFieldNames = new PrometheusResponseFieldNames(); prometheusResponseFieldNames.setValueFieldName("count()"); prometheusResponseFieldNames.setValueType(INTEGER); prometheusResponseFieldNames.setTimestampFieldName(TIMESTAMP); @@ -101,34 +105,38 @@ void testQueryResponseIteratorWithGivenPrometheusResponseFieldNames() { .thenReturn(new JSONObject(getJson("query_range_result.json"))); prometheusMetricScan.open(); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("count()", new ExprIntegerValue(1)); - put("instance", new ExprStringValue("localhost:9090")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("prometheus")); - } - }); + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("count()", new ExprIntegerValue(1)); + put("instance", new ExprStringValue("localhost:9090")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("prometheus")); + } + }); assertEquals(firstRow, prometheusMetricScan.next()); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue secondRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("count()", new ExprIntegerValue(0)); - put("instance", new ExprStringValue("localhost:9091")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("node")); - } - }); + ExprTupleValue secondRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("count()", new ExprIntegerValue(0)); + put("instance", new ExprStringValue("localhost:9091")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("node")); + } + }); assertEquals(secondRow, prometheusMetricScan.next()); Assertions.assertFalse(prometheusMetricScan.hasNext()); } - @Test @SneakyThrows void testQueryResponseIteratorWithGivenPrometheusResponseWithLongInAggType() { - PrometheusResponseFieldNames prometheusResponseFieldNames - = new PrometheusResponseFieldNames(); + PrometheusResponseFieldNames prometheusResponseFieldNames = new PrometheusResponseFieldNames(); prometheusResponseFieldNames.setValueFieldName("testAgg"); prometheusResponseFieldNames.setValueType(LONG); prometheusResponseFieldNames.setTimestampFieldName(TIMESTAMP); @@ -143,24 +151,30 @@ void testQueryResponseIteratorWithGivenPrometheusResponseWithLongInAggType() { .thenReturn(new JSONObject(getJson("query_range_result.json"))); prometheusMetricScan.open(); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("testAgg", new ExprLongValue(1)); - put("instance", new ExprStringValue("localhost:9090")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("prometheus")); - } - }); + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("testAgg", new ExprLongValue(1)); + put("instance", new ExprStringValue("localhost:9090")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("prometheus")); + } + }); assertEquals(firstRow, prometheusMetricScan.next()); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue secondRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("testAgg", new ExprLongValue(0)); - put("instance", new ExprStringValue("localhost:9091")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("node")); - } - }); + ExprTupleValue secondRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("testAgg", new ExprLongValue(0)); + put("instance", new ExprStringValue("localhost:9091")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("node")); + } + }); assertEquals(secondRow, prometheusMetricScan.next()); Assertions.assertFalse(prometheusMetricScan.hasNext()); } @@ -168,8 +182,7 @@ void testQueryResponseIteratorWithGivenPrometheusResponseWithLongInAggType() { @Test @SneakyThrows void testQueryResponseIteratorWithGivenPrometheusResponseWithBackQuotedFieldNames() { - PrometheusResponseFieldNames prometheusResponseFieldNames - = new PrometheusResponseFieldNames(); + PrometheusResponseFieldNames prometheusResponseFieldNames = new PrometheusResponseFieldNames(); prometheusResponseFieldNames.setValueFieldName("testAgg"); prometheusResponseFieldNames.setValueType(LONG); prometheusResponseFieldNames.setTimestampFieldName(TIMESTAMP); @@ -186,29 +199,34 @@ void testQueryResponseIteratorWithGivenPrometheusResponseWithBackQuotedFieldName .thenReturn(new JSONObject(getJson("query_range_result.json"))); prometheusMetricScan.open(); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue firstRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("testAgg", new ExprLongValue(1)); - put("`instance`", new ExprStringValue("localhost:9090")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("prometheus")); - } - }); + ExprTupleValue firstRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("testAgg", new ExprLongValue(1)); + put("`instance`", new ExprStringValue("localhost:9090")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("prometheus")); + } + }); assertEquals(firstRow, prometheusMetricScan.next()); Assertions.assertTrue(prometheusMetricScan.hasNext()); - ExprTupleValue secondRow = new ExprTupleValue(new LinkedHashMap<>() {{ - put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); - put("testAgg", new ExprLongValue(0)); - put("`instance`", new ExprStringValue("localhost:9091")); - put("__name__", new ExprStringValue("up")); - put("job", new ExprStringValue("node")); - } - }); + ExprTupleValue secondRow = + new ExprTupleValue( + new LinkedHashMap<>() { + { + put(TIMESTAMP, new ExprTimestampValue(Instant.ofEpochMilli(1435781430781L))); + put("testAgg", new ExprLongValue(0)); + put("`instance`", new ExprStringValue("localhost:9091")); + put("__name__", new ExprStringValue("up")); + put("job", new ExprStringValue("node")); + } + }); assertEquals(secondRow, prometheusMetricScan.next()); Assertions.assertFalse(prometheusMetricScan.hasNext()); } - @Test @SneakyThrows void testEmptyQueryResponseIterator() { @@ -235,11 +253,12 @@ void testEmptyQueryWithNoMatrixKeyInResultJson() { when(prometheusClient.queryRange(any(), any(), any(), any())) .thenReturn(new JSONObject(getJson("no_matrix_query_range_result.json"))); - RuntimeException runtimeException - = Assertions.assertThrows(RuntimeException.class, prometheusMetricScan::open); + RuntimeException runtimeException = + Assertions.assertThrows(RuntimeException.class, prometheusMetricScan::open); assertEquals( "Unexpected Result Type: vector during Prometheus Response Parsing. " - + "'matrix' resultType is expected", runtimeException.getMessage()); + + "'matrix' resultType is expected", + runtimeException.getMessage()); } @Test @@ -253,13 +272,12 @@ void testEmptyQueryWithException() { when(prometheusClient.queryRange(any(), any(), any(), any())) .thenThrow(new IOException("Error Message")); - RuntimeException runtimeException - = assertThrows(RuntimeException.class, prometheusMetricScan::open); - assertEquals("Error fetching data from prometheus server. Error Message", - runtimeException.getMessage()); + RuntimeException runtimeException = + assertThrows(RuntimeException.class, prometheusMetricScan::open); + assertEquals( + "Error fetching data from prometheus server. Error Message", runtimeException.getMessage()); } - @Test @SneakyThrows void testExplain() { @@ -273,5 +291,4 @@ void testExplain() { + "endTime=1664771294133, step=14)", prometheusMetricScan.explain()); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java index d43c38fc68..8bdab9244b 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusMetricTableTest.java @@ -62,15 +62,14 @@ @ExtendWith(MockitoExtension.class) class PrometheusMetricTableTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; @Test @SneakyThrows void testGetFieldTypesFromMetric() { when(client.getLabels(TestConstants.METRIC_NAME)).thenReturn(List.of("label1", "label2")); - PrometheusMetricTable prometheusMetricTable - = new PrometheusMetricTable(client, TestConstants.METRIC_NAME); + PrometheusMetricTable prometheusMetricTable = + new PrometheusMetricTable(client, TestConstants.METRIC_NAME); Map expectedFieldTypes = new HashMap<>(); expectedFieldTypes.put("label1", ExprCoreType.STRING); expectedFieldTypes.put("label2", ExprCoreType.STRING); @@ -84,7 +83,7 @@ void testGetFieldTypesFromMetric() { assertNull(prometheusMetricTable.getPrometheusQueryRequest()); assertNotNull(prometheusMetricTable.getMetricName()); - //testing Caching + // testing Caching fieldTypes = prometheusMetricTable.getFieldTypes(); assertEquals(expectedFieldTypes, fieldTypes); @@ -96,8 +95,8 @@ void testGetFieldTypesFromMetric() { @Test @SneakyThrows void testGetFieldTypesFromPrometheusQueryRequest() { - PrometheusMetricTable prometheusMetricTable - = new PrometheusMetricTable(client, new PrometheusQueryRequest()); + PrometheusMetricTable prometheusMetricTable = + new PrometheusMetricTable(client, new PrometheusQueryRequest()); Map expectedFieldTypes = new HashMap<>(); expectedFieldTypes.put(VALUE, ExprCoreType.DOUBLE); expectedFieldTypes.put(TIMESTAMP, ExprCoreType.TIMESTAMP); @@ -117,14 +116,17 @@ void testImplementWithBasicMetricQuery() { new PrometheusMetricTable(client, "prometheus_http_requests_total"); List finalProjectList = new ArrayList<>(); finalProjectList.add(named("@value", ref("@value", ExprCoreType.DOUBLE))); - PhysicalPlan plan = prometheusMetricTable.implement( - project(relation("prometheus_http_requests_total", prometheusMetricTable), - finalProjectList, null)); + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + relation("prometheus_http_requests_total", prometheusMetricTable), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE), outputFields); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); PrometheusMetricScan prometheusMetricScan = @@ -133,7 +135,6 @@ void testImplementWithBasicMetricQuery() { assertEquals(3600 / 250 + "s", prometheusMetricScan.getRequest().getStep()); } - @Test void testImplementPrometheusQueryWithStatsQueryAndNoFilter() { @@ -141,16 +142,23 @@ void testImplementPrometheusQueryWithStatsQueryAndNoFilter() { new PrometheusMetricTable(client, "prometheus_http_total_requests"); // IndexScanAgg without Filter - PhysicalPlan plan = prometheusMetricTable.implement( - filter( - indexScanAgg("prometheus_http_total_requests", ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("code", DSL.ref("code", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))))); + PhysicalPlan plan = + prometheusMetricTable.implement( + filter( + indexScanAgg( + "prometheus_http_total_requests", + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("code", DSL.ref("code", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))))); assertTrue(plan.getChild().get(0) instanceof PrometheusMetricScan); PrometheusQueryRequest prometheusQueryRequest = @@ -166,28 +174,31 @@ void testImplementPrometheusQueryWithStatsQueryAndFilter() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - // IndexScanAgg with Filter - PhysicalPlan plan = prometheusMetricTable.implement( - indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s"))))); + PhysicalPlan plan = + prometheusMetricTable.implement( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s"))))); assertTrue(plan instanceof PrometheusMetricScan); PrometheusQueryRequest prometheusQueryRequest = ((PrometheusMetricScan) plan).getRequest(); assertEquals( "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", prometheusQueryRequest.getPromQl()); - } - @Test void testImplementPrometheusQueryWithStatsQueryAndFilterAndProject() { @@ -198,77 +209,99 @@ void testImplementPrometheusQueryWithStatsQueryAndFilterAndProject() { List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(DSL.named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(DSL.named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), + ImmutableList.of(DSL.named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + DSL.named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals(request.getStep(), "40s"); - assertEquals("avg by(job) (avg_over_time" + assertEquals( + "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } - @Test void testTimeRangeResolver() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - - //Both endTime and startTime are set. + // Both endTime and startTime are set. List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP)))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg by(job) (avg_over_time" + assertEquals( + "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -278,40 +311,51 @@ void testTimeRangeResolverWithOutEndTimeInFilter() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - - //Only endTime is set. + // Only endTime is set. List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg by(job) (avg_over_time" + assertEquals( + "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -321,78 +365,95 @@ void testTimeRangeResolverWithOutStartTimeInFilter() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - - //Both endTime and startTime are set. + // Both endTime and startTime are set. List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg by(job) (avg_over_time" + assertEquals( + "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } - @Test void testSpanResolverWithoutSpanExpression() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - LogicalPlan plan = project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + LogicalPlan plan = + project( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), DSL.and( DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), + fromObjectValue( + dateFormat.format(new Date(startTime)), ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), + fromObjectValue( + dateFormat.format(new Date(endTime)), ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), null), - finalProjectList, null); - RuntimeException runtimeException - = Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(plan)); - Assertions.assertEquals("Prometheus Catalog doesn't support " - + "aggregations without span expression", + finalProjectList, + null); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(plan)); + Assertions.assertEquals( + "Prometheus Catalog doesn't support " + "aggregations without span expression", runtimeException.getMessage()); } @@ -402,34 +463,41 @@ void testSpanResolverWithEmptyGroupByList() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - LogicalPlan plan = project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + LogicalPlan plan = + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of()), - finalProjectList, null); - RuntimeException runtimeException - = Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(plan)); - Assertions.assertEquals("Prometheus Catalog doesn't support " - + "aggregations without span expression", + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP)))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of()), + finalProjectList, + null); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(plan)); + Assertions.assertEquals( + "Prometheus Catalog doesn't support " + "aggregations without span expression", runtimeException.getMessage()); } @@ -439,44 +507,58 @@ void testSpanResolverWithSpanExpression() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP)))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg by(job) (avg_over_time" + assertEquals( + "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -486,35 +568,45 @@ void testExpressionWithMissingTimeUnitInSpanExpression() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - LogicalPlan logicalPlan = project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + LogicalPlan logicalPlan = + project( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), DSL.and( DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), + fromObjectValue( + dateFormat.format(new Date(startTime)), ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), + fromObjectValue( + dateFormat.format(new Date(endTime)), ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "")))), - finalProjectList, null); + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("job", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), DSL.literal(40), "")))), + finalProjectList, + null); RuntimeException exception = - Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(logicalPlan)); + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(logicalPlan)); assertEquals("Missing TimeUnit in the span expression", exception.getMessage()); } @@ -524,44 +616,57 @@ void testPrometheusQueryWithOnlySpanExpressionInGroupByList() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of( - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP)))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg (avg_over_time" + assertEquals( + "avg (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -571,44 +676,57 @@ void testStatsWithNoGroupByList() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - List finalProjectList = new ArrayList<>(); finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING))); finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); Long endTime = new Date(System.currentTimeMillis()).getTime(); Long startTime = new Date(System.currentTimeMillis() - 4800 * 1000).getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - PhysicalPlan plan = prometheusMetricTable.implement( - project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + PhysicalPlan plan = + prometheusMetricTable.implement( + project( + indexScanAgg( + "prometheus_http_total_requests", DSL.and( - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), - DSL.and(DSL.gte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(startTime)), - ExprCoreType.TIMESTAMP))), - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP)))))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("span", - DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s")))), - finalProjectList, null)); + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.and( + DSL.equal( + DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))), + DSL.and( + DSL.gte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(startTime)), + ExprCoreType.TIMESTAMP))), + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), + ExprCoreType.TIMESTAMP)))))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s")))), + finalProjectList, + null)); assertTrue(plan instanceof ProjectOperator); assertTrue(((ProjectOperator) plan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) plan).getInput()).getRequest(); assertEquals("40s", request.getStep()); - assertEquals("avg (avg_over_time" + assertEquals( + "avg (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", request.getPromQl()); List projectList = ((ProjectOperator) plan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -617,9 +735,11 @@ void testImplementWithUnexpectedLogicalNode() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); LogicalPlan plan = project(testLogicalPlanNode()); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(plan)); - assertEquals("unexpected plan node type class" + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(plan)); + assertEquals( + "unexpected plan node type class" + " org.opensearch.sql.prometheus.utils.LogicalPlanUtils$TestLogicalPlan", runtimeException.getMessage()); } @@ -629,37 +749,44 @@ void testMultipleAggregationsThrowsRuntimeException() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan plan = project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER))), - named("SUM(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING))))); - - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(plan)); - assertEquals("Prometheus Catalog doesn't multiple aggregations in stats command", + LogicalPlan plan = + project( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), + ImmutableList.of( + named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER))), + named("SUM(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of(named("job", DSL.ref("job", STRING))))); + + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(plan)); + assertEquals( + "Prometheus Catalog doesn't multiple aggregations in stats command", runtimeException.getMessage()); } - @Test void testUnSupportedAggregation() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan plan = project(indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(named("VAR_SAMP(@value)", - DSL.varSamp(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("job", DSL.ref("job", STRING))))); - - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(plan)); + LogicalPlan plan = + project( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), + ImmutableList.of( + named("VAR_SAMP(@value)", DSL.varSamp(DSL.ref("@value", INTEGER)))), + ImmutableList.of(named("job", DSL.ref("job", STRING))))); + + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusMetricTable.implement(plan)); assertTrue(runtimeException.getMessage().contains("Prometheus Catalog only supports")); } @@ -667,13 +794,16 @@ void testUnSupportedAggregation() { void testImplementWithORConditionInWhereClause() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan plan = indexScan("prometheus_http_total_requests", - DSL.or(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))); - RuntimeException exception - = assertThrows(RuntimeException.class, () -> prometheusMetricTable.implement(plan)); - assertEquals("Prometheus Datasource doesn't support or in where command.", - exception.getMessage()); + LogicalPlan plan = + indexScan( + "prometheus_http_total_requests", + DSL.or( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))); + RuntimeException exception = + assertThrows(RuntimeException.class, () -> prometheusMetricTable.implement(plan)); + assertEquals( + "Prometheus Datasource doesn't support or in where command.", exception.getMessage()); } @Test @@ -683,21 +813,26 @@ void testImplementWithRelationAndFilter() { finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP))); PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))), - finalProjectList, null); + LogicalPlan logicalPlan = + project( + indexScan( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))), + finalProjectList, + null); PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan); assertTrue(physicalPlan instanceof ProjectOperator); assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); assertEquals((3600 / 250) + "s", request.getStep()); - assertEquals("prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}", - request.getPromQl()); + assertEquals( + "prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}", request.getPromQl()); List projectList = ((ProjectOperator) physicalPlan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -710,27 +845,30 @@ void testImplementWithRelationAndTimestampFilter() { Long endTime = new Date(System.currentTimeMillis()).getTime(); PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests", - DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP))) - ), finalProjectList, null); + LogicalPlan logicalPlan = + project( + indexScan( + "prometheus_http_total_requests", + DSL.lte( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), ExprCoreType.TIMESTAMP)))), + finalProjectList, + null); PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan); assertTrue(physicalPlan instanceof ProjectOperator); assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); assertEquals((3600 / 250) + "s", request.getStep()); - assertEquals("prometheus_http_total_requests", - request.getPromQl()); + assertEquals("prometheus_http_total_requests", request.getPromQl()); List projectList = ((ProjectOperator) physicalPlan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } - @Test void testImplementWithRelationAndTimestampLTFilter() { List finalProjectList = new ArrayList<>(); @@ -740,27 +878,30 @@ void testImplementWithRelationAndTimestampLTFilter() { Long endTime = new Date(System.currentTimeMillis()).getTime(); PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests", - DSL.less(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP))) - ), finalProjectList, null); + LogicalPlan logicalPlan = + project( + indexScan( + "prometheus_http_total_requests", + DSL.less( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), ExprCoreType.TIMESTAMP)))), + finalProjectList, + null); PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan); assertTrue(physicalPlan instanceof ProjectOperator); assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); assertEquals((3600 / 250) + "s", request.getStep()); - assertEquals("prometheus_http_total_requests", - request.getPromQl()); + assertEquals("prometheus_http_total_requests", request.getPromQl()); List projectList = ((ProjectOperator) physicalPlan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } - @Test void testImplementWithRelationAndTimestampGTFilter() { List finalProjectList = new ArrayList<>(); @@ -770,23 +911,27 @@ void testImplementWithRelationAndTimestampGTFilter() { Long endTime = new Date(System.currentTimeMillis()).getTime(); PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests", - DSL.greater(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal( - fromObjectValue(dateFormat.format(new Date(endTime)), - ExprCoreType.TIMESTAMP))) - ), finalProjectList, null); + LogicalPlan logicalPlan = + project( + indexScan( + "prometheus_http_total_requests", + DSL.greater( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal( + fromObjectValue( + dateFormat.format(new Date(endTime)), ExprCoreType.TIMESTAMP)))), + finalProjectList, + null); PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan); assertTrue(physicalPlan instanceof ProjectOperator); assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan); - PrometheusQueryRequest request - = ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); + PrometheusQueryRequest request = + ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest(); assertEquals((3600 / 250) + "s", request.getStep()); - assertEquals("prometheus_http_total_requests", - request.getPromQl()); + assertEquals("prometheus_http_total_requests", request.getPromQl()); List projectList = ((ProjectOperator) physicalPlan).getProjectList(); - List outputFields - = projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); + List outputFields = + projectList.stream().map(NamedExpression::getName).collect(Collectors.toList()); assertEquals(List.of(VALUE, TIMESTAMP), outputFields); } @@ -796,10 +941,9 @@ void testOptimize() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, prometheusQueryRequest); List finalProjectList = new ArrayList<>(); - LogicalPlan inputPlan = project(relation("query_range", prometheusMetricTable), - finalProjectList, null); - LogicalPlan optimizedPlan = prometheusMetricTable.optimize( - inputPlan); + LogicalPlan inputPlan = + project(relation("query_range", prometheusMetricTable), finalProjectList, null); + LogicalPlan optimizedPlan = prometheusMetricTable.optimize(inputPlan); assertEquals(inputPlan, optimizedPlan); } @@ -810,7 +954,8 @@ void testUnsupportedOperation() { new PrometheusMetricTable(client, prometheusQueryRequest); assertThrows(UnsupportedOperationException.class, prometheusMetricTable::exists); - assertThrows(UnsupportedOperationException.class, + assertThrows( + UnsupportedOperationException.class, () -> prometheusMetricTable.create(Collections.emptyMap())); } @@ -820,25 +965,29 @@ void testImplementPrometheusQueryWithBackQuotedFieldNamesInStatsQuery() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - // IndexScanAgg with Filter - PhysicalPlan plan = prometheusMetricTable.implement( - indexScanAgg("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), - ImmutableList - .of(named("AVG(@value)", - DSL.avg(DSL.ref("@value", INTEGER)))), - ImmutableList.of(named("`job`", DSL.ref("job", STRING)), - named("span", DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), - DSL.literal(40), "s"))))); + PhysicalPlan plan = + prometheusMetricTable.implement( + indexScanAgg( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))), + ImmutableList.of(named("AVG(@value)", DSL.avg(DSL.ref("@value", INTEGER)))), + ImmutableList.of( + named("`job`", DSL.ref("job", STRING)), + named( + "span", + DSL.span( + DSL.ref("@timestamp", ExprCoreType.TIMESTAMP), + DSL.literal(40), + "s"))))); assertTrue(plan instanceof PrometheusMetricScan); PrometheusQueryRequest prometheusQueryRequest = ((PrometheusMetricScan) plan).getRequest(); assertEquals( "avg by(job) (avg_over_time" + "(prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}[40s]))", prometheusQueryRequest.getPromQl()); - } @Test @@ -848,14 +997,16 @@ void testImplementPrometheusQueryWithFilterQuery() { new PrometheusMetricTable(client, "prometheus_http_total_requests"); // IndexScanAgg without Filter - PhysicalPlan plan = prometheusMetricTable.implement( - indexScan("prometheus_http_total_requests", - DSL.and(DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))))); + PhysicalPlan plan = + prometheusMetricTable.implement( + indexScan( + "prometheus_http_total_requests", + DSL.and( + DSL.equal(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/")))))); assertTrue(plan instanceof PrometheusMetricScan); - PrometheusQueryRequest prometheusQueryRequest = - ((PrometheusMetricScan) plan).getRequest(); + PrometheusQueryRequest prometheusQueryRequest = ((PrometheusMetricScan) plan).getRequest(); assertEquals( "prometheus_http_total_requests{code=\"200\" , handler=\"/ready/\"}", prometheusQueryRequest.getPromQl()); @@ -867,15 +1018,22 @@ void testImplementPrometheusQueryWithUnsupportedFilterQuery() { PrometheusMetricTable prometheusMetricTable = new PrometheusMetricTable(client, "prometheus_http_total_requests"); - RuntimeException exception = assertThrows(RuntimeException.class, - () -> prometheusMetricTable.implement(indexScan("prometheus_http_total_requests", - DSL.and(DSL.lte(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))))); - assertEquals("Prometheus Datasource doesn't support <= in where command.", - exception.getMessage()); + RuntimeException exception = + assertThrows( + RuntimeException.class, + () -> + prometheusMetricTable.implement( + indexScan( + "prometheus_http_total_requests", + DSL.and( + DSL.lte(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal( + DSL.ref("handler", STRING), + DSL.literal(stringValue("/ready/"))))))); + assertEquals( + "Prometheus Datasource doesn't support <= in where command.", exception.getMessage()); } - @Test void testCreateScanBuilderWithQueryRangeTableFunction() { PrometheusQueryRequest prometheusQueryRequest = new PrometheusQueryRequest(); @@ -895,5 +1053,4 @@ void testCreateScanBuilderWithPPLQuery() { TableScanBuilder tableScanBuilder = prometheusMetricTable.createScanBuilder(); Assertions.assertNull(tableScanBuilder); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngineTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngineTest.java index 4e8d470373..b925fe6538 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngineTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageEngineTest.java @@ -29,8 +29,7 @@ @ExtendWith(MockitoExtension.class) class PrometheusStorageEngineTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; @Test public void getTable() { @@ -43,15 +42,12 @@ public void getTable() { @Test public void getFunctions() { PrometheusStorageEngine engine = new PrometheusStorageEngine(client); - Collection functionResolverCollection - = engine.getFunctions(); + Collection functionResolverCollection = engine.getFunctions(); assertNotNull(functionResolverCollection); assertEquals(2, functionResolverCollection.size()); Iterator iterator = functionResolverCollection.iterator(); - assertTrue( - iterator.next() instanceof QueryRangeTableFunctionResolver); - assertTrue( - iterator.next() instanceof QueryExemplarsTableFunctionResolver); + assertTrue(iterator.next() instanceof QueryRangeTableFunctionResolver); + assertTrue(iterator.next() instanceof QueryExemplarsTableFunctionResolver); } @Test @@ -65,8 +61,8 @@ public void getSystemTable() { @Test public void getSystemTableForAllTablesInfo() { PrometheusStorageEngine engine = new PrometheusStorageEngine(client); - Table table - = engine.getTable(new DataSourceSchemaName("prometheus", "information_schema"), "tables"); + Table table = + engine.getTable(new DataSourceSchemaName("prometheus", "information_schema"), "tables"); assertNotNull(table); assertTrue(table instanceof PrometheusSystemTable); } @@ -74,10 +70,12 @@ public void getSystemTableForAllTablesInfo() { @Test public void getSystemTableWithWrongInformationSchemaTable() { PrometheusStorageEngine engine = new PrometheusStorageEngine(client); - SemanticCheckException exception = assertThrows(SemanticCheckException.class, - () -> engine.getTable(new DataSourceSchemaName("prometheus", "information_schema"), - "test")); + SemanticCheckException exception = + assertThrows( + SemanticCheckException.class, + () -> + engine.getTable( + new DataSourceSchemaName("prometheus", "information_schema"), "test")); assertEquals("Information Schema doesn't contain test table", exception.getMessage()); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java index c566ccdeb4..c2e8e5325a 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java @@ -26,8 +26,7 @@ @ExtendWith(MockitoExtension.class) public class PrometheusStorageFactoryTest { - @Mock - private Settings settings; + @Mock private Settings settings; @Test void testGetConnectorType() { @@ -46,8 +45,7 @@ void testGetStorageEngineWithBasicAuth() { properties.put("prometheus.auth.type", "basicauth"); properties.put("prometheus.auth.username", "admin"); properties.put("prometheus.auth.password", "admin"); - StorageEngine storageEngine - = prometheusStorageFactory.getStorageEngine(properties); + StorageEngine storageEngine = prometheusStorageFactory.getStorageEngine(properties); Assertions.assertTrue(storageEngine instanceof PrometheusStorageEngine); } @@ -62,12 +60,10 @@ void testGetStorageEngineWithAWSSigV4Auth() { properties.put("prometheus.auth.region", "us-east-1"); properties.put("prometheus.auth.secret_key", "accessKey"); properties.put("prometheus.auth.access_key", "secretKey"); - StorageEngine storageEngine - = prometheusStorageFactory.getStorageEngine(properties); + StorageEngine storageEngine = prometheusStorageFactory.getStorageEngine(properties); Assertions.assertTrue(storageEngine instanceof PrometheusStorageEngine); } - @Test @SneakyThrows void testGetStorageEngineWithMissingURI() { @@ -77,10 +73,12 @@ void testGetStorageEngineWithMissingURI() { properties.put("prometheus.auth.region", "us-east-1"); properties.put("prometheus.auth.secret_key", "accessKey"); properties.put("prometheus.auth.access_key", "secretKey"); - IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, - () -> prometheusStorageFactory.getStorageEngine(properties)); - Assertions.assertEquals("Missing [prometheus.uri] fields " - + "in the Prometheus connector properties.", + IllegalArgumentException exception = + Assertions.assertThrows( + IllegalArgumentException.class, + () -> prometheusStorageFactory.getStorageEngine(properties)); + Assertions.assertEquals( + "Missing [prometheus.uri] fields " + "in the Prometheus connector properties.", exception.getMessage()); } @@ -93,14 +91,15 @@ void testGetStorageEngineWithMissingRegionInAWS() { properties.put("prometheus.auth.type", "awssigv4"); properties.put("prometheus.auth.secret_key", "accessKey"); properties.put("prometheus.auth.access_key", "secretKey"); - IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, - () -> prometheusStorageFactory.getStorageEngine(properties)); - Assertions.assertEquals("Missing [prometheus.auth.region] fields in the " - + "Prometheus connector properties.", + IllegalArgumentException exception = + Assertions.assertThrows( + IllegalArgumentException.class, + () -> prometheusStorageFactory.getStorageEngine(properties)); + Assertions.assertEquals( + "Missing [prometheus.auth.region] fields in the " + "Prometheus connector properties.", exception.getMessage()); } - @Test @SneakyThrows void testGetStorageEngineWithLongConfigProperties() { @@ -110,9 +109,12 @@ void testGetStorageEngineWithLongConfigProperties() { properties.put("prometheus.auth.type", "awssigv4"); properties.put("prometheus.auth.secret_key", "accessKey"); properties.put("prometheus.auth.access_key", "secretKey"); - IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, - () -> prometheusStorageFactory.getStorageEngine(properties)); - Assertions.assertEquals("Missing [prometheus.auth.region] fields in the " + IllegalArgumentException exception = + Assertions.assertThrows( + IllegalArgumentException.class, + () -> prometheusStorageFactory.getStorageEngine(properties)); + Assertions.assertEquals( + "Missing [prometheus.auth.region] fields in the " + "Prometheus connector properties." + "Fields [prometheus.uri] exceeds more than 1000 characters.", exception.getMessage()); @@ -129,13 +131,14 @@ void testGetStorageEngineWithWrongAuthType() { properties.put("prometheus.auth.region", "us-east-1"); properties.put("prometheus.auth.secret_key", "accessKey"); properties.put("prometheus.auth.access_key", "secretKey"); - IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, - () -> prometheusStorageFactory.getStorageEngine(properties)); - Assertions.assertEquals("AUTH Type : random is not supported with Prometheus Connector", - exception.getMessage()); + IllegalArgumentException exception = + Assertions.assertThrows( + IllegalArgumentException.class, + () -> prometheusStorageFactory.getStorageEngine(properties)); + Assertions.assertEquals( + "AUTH Type : random is not supported with Prometheus Connector", exception.getMessage()); } - @Test @SneakyThrows void testGetStorageEngineWithNONEAuthType() { @@ -143,8 +146,7 @@ void testGetStorageEngineWithNONEAuthType() { PrometheusStorageFactory prometheusStorageFactory = new PrometheusStorageFactory(settings); HashMap properties = new HashMap<>(); properties.put("prometheus.uri", "https://test.com"); - StorageEngine storageEngine - = prometheusStorageFactory.getStorageEngine(properties); + StorageEngine storageEngine = prometheusStorageFactory.getStorageEngine(properties); Assertions.assertTrue(storageEngine instanceof PrometheusStorageEngine); } @@ -157,8 +159,9 @@ void testGetStorageEngineWithInvalidURISyntax() { properties.put("prometheus.auth.type", "basicauth"); properties.put("prometheus.auth.username", "admin"); properties.put("prometheus.auth.password", "admin"); - RuntimeException exception = Assertions.assertThrows(RuntimeException.class, - () -> prometheusStorageFactory.getStorageEngine(properties)); + RuntimeException exception = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusStorageFactory.getStorageEngine(properties)); Assertions.assertTrue( exception.getMessage().contains("Invalid URI in prometheus properties: ")); } @@ -213,10 +216,13 @@ void createDataSourceWithInvalidHostname() { metadata.setProperties(properties); PrometheusStorageFactory prometheusStorageFactory = new PrometheusStorageFactory(settings); - RuntimeException exception = Assertions.assertThrows(RuntimeException.class, - () -> prometheusStorageFactory.createDataSource(metadata)); + RuntimeException exception = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusStorageFactory.createDataSource(metadata)); Assertions.assertTrue( - exception.getMessage().contains("Invalid hostname in the uri: http://dummyprometheus:9090")); + exception + .getMessage() + .contains("Invalid hostname in the uri: http://dummyprometheus:9090")); } @Test @@ -233,8 +239,9 @@ void createDataSourceWithInvalidIp() { metadata.setProperties(properties); PrometheusStorageFactory prometheusStorageFactory = new PrometheusStorageFactory(settings); - RuntimeException exception = Assertions.assertThrows(RuntimeException.class, - () -> prometheusStorageFactory.createDataSource(metadata)); + RuntimeException exception = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusStorageFactory.createDataSource(metadata)); Assertions.assertTrue( exception.getMessage().contains("Invalid hostname in the uri: http://231.54.11.987:9090")); } @@ -255,11 +262,15 @@ void createDataSourceWithHostnameNotMatchingWithAllowHostsConfig() { metadata.setProperties(properties); PrometheusStorageFactory prometheusStorageFactory = new PrometheusStorageFactory(settings); - RuntimeException exception = Assertions.assertThrows(RuntimeException.class, - () -> prometheusStorageFactory.createDataSource(metadata)); + RuntimeException exception = + Assertions.assertThrows( + RuntimeException.class, () -> prometheusStorageFactory.createDataSource(metadata)); Assertions.assertTrue( - exception.getMessage().contains("Disallowed hostname in the uri: http://localhost.com:9090. " - + "Validate with plugins.query.datasources.uri.allowhosts config")); + exception + .getMessage() + .contains( + "Disallowed hostname in the uri: http://localhost.com:9090. " + + "Validate with plugins.query.datasources.uri.allowhosts config")); } @Test @@ -279,5 +290,4 @@ void createDataSourceSuccessWithHostnameRestrictions() { DataSource dataSource = new PrometheusStorageFactory(settings).createDataSource(metadata); Assertions.assertTrue(dataSource.getStorageEngine() instanceof PrometheusStorageEngine); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTableTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTableTest.java index 19876d398d..7f49de981a 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTableTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/QueryExemplarsTableTest.java @@ -30,14 +30,12 @@ @ExtendWith(MockitoExtension.class) class QueryExemplarsTableTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; @Test @SneakyThrows void testGetFieldTypes() { - PrometheusQueryExemplarsRequest exemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest exemplarsRequest = new PrometheusQueryExemplarsRequest(); exemplarsRequest.setQuery(QUERY); exemplarsRequest.setStartTime(STARTTIME); exemplarsRequest.setEndTime(ENDTIME); @@ -50,8 +48,7 @@ void testGetFieldTypes() { @Test void testImplementWithBasicMetricQuery() { - PrometheusQueryExemplarsRequest exemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest exemplarsRequest = new PrometheusQueryExemplarsRequest(); exemplarsRequest.setQuery(QUERY); exemplarsRequest.setStartTime(STARTTIME); exemplarsRequest.setEndTime(ENDTIME); @@ -67,8 +64,7 @@ void testImplementWithBasicMetricQuery() { @Test void testCreateScanBuilderWithQueryRangeTableFunction() { - PrometheusQueryExemplarsRequest exemplarsRequest - = new PrometheusQueryExemplarsRequest(); + PrometheusQueryExemplarsRequest exemplarsRequest = new PrometheusQueryExemplarsRequest(); exemplarsRequest.setQuery(QUERY); exemplarsRequest.setStartTime(STARTTIME); exemplarsRequest.setEndTime(ENDTIME); @@ -77,5 +73,4 @@ void testCreateScanBuilderWithQueryRangeTableFunction() { Assertions.assertNotNull(tableScanBuilder); Assertions.assertTrue(tableScanBuilder instanceof QueryExemplarsFunctionTableScanBuilder); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/StepParameterResolverTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/StepParameterResolverTest.java index 37e24a56b5..397b7146f7 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/StepParameterResolverTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/StepParameterResolverTest.java @@ -18,9 +18,11 @@ public class StepParameterResolverTest { @Test void testNullChecks() { StepParameterResolver stepParameterResolver = new StepParameterResolver(); - Assertions.assertThrows(NullPointerException.class, + Assertions.assertThrows( + NullPointerException.class, () -> stepParameterResolver.resolve(null, new Date().getTime(), Collections.emptyList())); - Assertions.assertThrows(NullPointerException.class, + Assertions.assertThrows( + NullPointerException.class, () -> stepParameterResolver.resolve(new Date().getTime(), null, Collections.emptyList())); } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/TimeRangeParametersResolverTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/TimeRangeParametersResolverTest.java index 73839e2152..6a280b7d98 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/TimeRangeParametersResolverTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/querybuilders/TimeRangeParametersResolverTest.java @@ -21,9 +21,11 @@ public class TimeRangeParametersResolverTest { @Test void testTimeRangeParametersWithoutTimestampFilter() { TimeRangeParametersResolver timeRangeParametersResolver = new TimeRangeParametersResolver(); - Pair result = timeRangeParametersResolver.resolve( - DSL.and(DSL.less(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), - DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))); + Pair result = + timeRangeParametersResolver.resolve( + DSL.and( + DSL.less(DSL.ref("code", STRING), DSL.literal(stringValue("200"))), + DSL.equal(DSL.ref("handler", STRING), DSL.literal(stringValue("/ready/"))))); Assertions.assertNotNull(result); Assertions.assertEquals(3600, result.getSecond() - result.getFirst()); } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScanTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScanTest.java index 0d7ec4e2cc..ea299b87de 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScanTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableScanTest.java @@ -22,8 +22,7 @@ @ExtendWith(MockitoExtension.class) public class PrometheusSystemTableScanTest { - @Mock - private PrometheusSystemRequest request; + @Mock private PrometheusSystemRequest request; @Test public void queryData() { diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableTest.java index 0721f82c07..7022ca9657 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/system/PrometheusSystemTableTest.java @@ -35,51 +35,41 @@ @ExtendWith(MockitoExtension.class) public class PrometheusSystemTableTest { - @Mock - private PrometheusClient client; + @Mock private PrometheusClient client; - @Mock - private Table table; + @Mock private Table table; @Test void testGetFieldTypesOfMetaTable() { - PrometheusSystemTable systemIndex = new PrometheusSystemTable(client, - new DataSourceSchemaName("prometheus", "information_schema"), TABLE_INFO); + PrometheusSystemTable systemIndex = + new PrometheusSystemTable( + client, new DataSourceSchemaName("prometheus", "information_schema"), TABLE_INFO); final Map fieldTypes = systemIndex.getFieldTypes(); - assertThat(fieldTypes, anyOf( - hasEntry("TABLE_CATALOG", STRING) - )); - assertThat(fieldTypes, anyOf( - hasEntry("UNIT", STRING) - )); + assertThat(fieldTypes, anyOf(hasEntry("TABLE_CATALOG", STRING))); + assertThat(fieldTypes, anyOf(hasEntry("UNIT", STRING))); } @Test void testGetFieldTypesOfMappingTable() { - PrometheusSystemTable systemIndex = new PrometheusSystemTable(client, - new DataSourceSchemaName("prometheus", "information_schema"), mappingTable( - "test_metric")); + PrometheusSystemTable systemIndex = + new PrometheusSystemTable( + client, + new DataSourceSchemaName("prometheus", "information_schema"), + mappingTable("test_metric")); final Map fieldTypes = systemIndex.getFieldTypes(); - assertThat(fieldTypes, anyOf( - hasEntry("COLUMN_NAME", STRING) - )); + assertThat(fieldTypes, anyOf(hasEntry("COLUMN_NAME", STRING))); } - - @Test void implement() { - PrometheusSystemTable systemIndex = new PrometheusSystemTable(client, - new DataSourceSchemaName("prometheus", "information_schema"), TABLE_INFO); + PrometheusSystemTable systemIndex = + new PrometheusSystemTable( + client, new DataSourceSchemaName("prometheus", "information_schema"), TABLE_INFO); NamedExpression projectExpr = named("TABLE_NAME", ref("TABLE_NAME", STRING)); - final PhysicalPlan plan = systemIndex.implement( - project( - relation(TABLE_INFO, table), - projectExpr - )); + final PhysicalPlan plan = + systemIndex.implement(project(relation(TABLE_INFO, table), projectExpr)); assertTrue(plan instanceof ProjectOperator); assertTrue(plan.getChild().get(0) instanceof PrometheusSystemTableScan); } - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/LogicalPlanUtils.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/LogicalPlanUtils.java index 5fcebf52e6..570a987889 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/LogicalPlanUtils.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/LogicalPlanUtils.java @@ -19,43 +19,36 @@ public class LogicalPlanUtils { - /** - * Build PrometheusLogicalMetricScan. - */ + /** Build PrometheusLogicalMetricScan. */ public static LogicalPlan indexScan(String metricName, Expression filter) { - return PrometheusLogicalMetricScan.builder().metricName(metricName) - .filter(filter) - .build(); + return PrometheusLogicalMetricScan.builder().metricName(metricName).filter(filter).build(); } - /** - * Build PrometheusLogicalMetricAgg. - */ - public static LogicalPlan indexScanAgg(String metricName, Expression filter, - List aggregators, - List groupByList) { - return PrometheusLogicalMetricAgg.builder().metricName(metricName) + /** Build PrometheusLogicalMetricAgg. */ + public static LogicalPlan indexScanAgg( + String metricName, + Expression filter, + List aggregators, + List groupByList) { + return PrometheusLogicalMetricAgg.builder() + .metricName(metricName) .filter(filter) .aggregatorList(aggregators) .groupByList(groupByList) .build(); } - /** - * Build PrometheusLogicalMetricAgg. - */ - public static LogicalPlan indexScanAgg(String metricName, - List aggregators, - List groupByList) { - return PrometheusLogicalMetricAgg.builder().metricName(metricName) + /** Build PrometheusLogicalMetricAgg. */ + public static LogicalPlan indexScanAgg( + String metricName, List aggregators, List groupByList) { + return PrometheusLogicalMetricAgg.builder() + .metricName(metricName) .aggregatorList(aggregators) .groupByList(groupByList) .build(); } - /** - * Build PrometheusLogicalMetricAgg. - */ + /** Build PrometheusLogicalMetricAgg. */ public static LogicalPlan testLogicalPlanNode() { return new TestLogicalPlan(); } @@ -71,7 +64,4 @@ public R accept(LogicalPlanNodeVisitor visitor, C context) { return visitor.visitNode(this, null); } } - - - } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/TestUtils.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/TestUtils.java index 1683858c49..a9fcc26101 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/TestUtils.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/utils/TestUtils.java @@ -12,6 +12,7 @@ public class TestUtils { /** * Get Json document from the files in resources folder. + * * @param filename filename. * @return String. * @throws IOException IOException. @@ -21,5 +22,4 @@ public static String getJson(String filename) throws IOException { return new String( Objects.requireNonNull(classLoader.getResourceAsStream(filename)).readAllBytes()); } - }