From 9af0daa5f8542692d45df7a601d40516ed5836e4 Mon Sep 17 00:00:00 2001 From: currantw Date: Thu, 5 Dec 2024 10:54:21 -0800 Subject: [PATCH] More quick fixes! Signed-off-by: currantw --- .../opensearch/sql/utils/ComparisonUtil.java | 42 +++++++------------ .../org/opensearch/sql/legacy/CursorIT.java | 28 ++++++------- .../sql/legacy/cursor/DefaultCursor.java | 25 +++++------ 3 files changed, 39 insertions(+), 56 deletions(-) diff --git a/core/src/test/java/org/opensearch/sql/utils/ComparisonUtil.java b/core/src/test/java/org/opensearch/sql/utils/ComparisonUtil.java index 0d9fe80339..470e09b84a 100644 --- a/core/src/test/java/org/opensearch/sql/utils/ComparisonUtil.java +++ b/core/src/test/java/org/opensearch/sql/utils/ComparisonUtil.java @@ -46,32 +46,20 @@ public static int compare(ExprValue v1, ExprValue v2) { "invalid to call compare operation on values of different types"); } - switch ((ExprCoreType) v1.type()) { - case BYTE: - return v1.byteValue().compareTo(v2.byteValue()); - case SHORT: - return v1.shortValue().compareTo(v2.shortValue()); - case INTEGER: - return getIntegerValue(v1).compareTo(getIntegerValue(v2)); - case LONG: - return getLongValue(v1).compareTo(getLongValue(v2)); - case FLOAT: - return getFloatValue(v1).compareTo(getFloatValue(v2)); - case DOUBLE: - return getDoubleValue(v1).compareTo(getDoubleValue(v2)); - case STRING: - return getStringValue(v1).compareTo(getStringValue(v2)); - case BOOLEAN: - return v1.booleanValue().compareTo(v2.booleanValue()); - case TIME: - return v1.timeValue().compareTo(v2.timeValue()); - case DATE: - return v1.dateValue().compareTo(v2.dateValue()); - case TIMESTAMP: - return v1.timestampValue().compareTo(v2.timestampValue()); - default: - throw new ExpressionEvaluationException( - String.format("%s instances are not comparable", v1.getClass().getSimpleName())); - } + return switch ((ExprCoreType) v1.type()) { + case BYTE -> v1.byteValue().compareTo(v2.byteValue()); + case SHORT -> v1.shortValue().compareTo(v2.shortValue()); + case INTEGER -> getIntegerValue(v1).compareTo(getIntegerValue(v2)); + case LONG -> getLongValue(v1).compareTo(getLongValue(v2)); + case FLOAT -> getFloatValue(v1).compareTo(getFloatValue(v2)); + case DOUBLE -> getDoubleValue(v1).compareTo(getDoubleValue(v2)); + case STRING -> getStringValue(v1).compareTo(getStringValue(v2)); + case BOOLEAN -> v1.booleanValue().compareTo(v2.booleanValue()); + case TIME -> v1.timeValue().compareTo(v2.timeValue()); + case DATE -> v1.dateValue().compareTo(v2.dateValue()); + case TIMESTAMP -> v1.timestampValue().compareTo(v2.timestampValue()); + default -> throw new ExpressionEvaluationException( + String.format("%s instances are not comparable", v1.getClass().getSimpleName())); + }; } } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/CursorIT.java b/integ-test/src/test/java/org/opensearch/sql/legacy/CursorIT.java index e4ba593844..2d7c60466f 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/CursorIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/CursorIT.java @@ -51,7 +51,7 @@ public void invalidNegativeFetchSize() throws IOException { StringUtils.format("SELECT firstname, state FROM %s", TestsConstants.TEST_INDEX_ACCOUNT); Response response = null; try { - String queryResult = executeFetchQuery(query, -2, JDBC); + executeFetchQuery(query, -2, JDBC); } catch (ResponseException ex) { response = ex.getResponse(); } @@ -70,7 +70,7 @@ public void invalidNonNumericFetchSize() throws IOException { StringUtils.format("SELECT firstname, state FROM %s", TestsConstants.TEST_INDEX_ACCOUNT); Response response = null; try { - String queryResult = executeFetchAsStringQuery(query, "hello world", JDBC); + executeFetchAsStringQuery(query, "hello world", JDBC); } catch (ResponseException ex) { response = ex.getResponse(); } @@ -88,7 +88,7 @@ public void testExceptionOnCursorExplain() throws IOException { Request sqlRequest = getSqlRequest(cursorRequest, true); Response response = null; try { - String queryResult = executeRequest(sqlRequest); + executeRequest(sqlRequest); } catch (ResponseException ex) { response = ex.getResponse(); } @@ -222,7 +222,6 @@ public void noCursorWhenResultsLessThanFetchSize() throws IOException { public void testCursorWithPreparedStatement() throws IOException { JSONObject response = executeJDBCRequest( - String.format( "{" + "\"fetch_size\": 200," + "\"query\": \" SELECT age, state FROM %s WHERE age > ? OR state IN (?, ?)\"," @@ -241,7 +240,7 @@ public void testCursorWithPreparedStatement() throws IOException { + " }" + "]" + "}" - + TestsConstants.TEST_INDEX_ACCOUNT)); + + TestsConstants.TEST_INDEX_ACCOUNT); assertTrue(response.has(CURSOR)); verifyIsV1Cursor(response.getString(CURSOR)); } @@ -249,10 +248,10 @@ public void testCursorWithPreparedStatement() throws IOException { @Test public void testRegressionOnDateFormatChange() throws IOException { loadIndex(Index.DATETIME); - /** - * With pagination, the field should be date formatted to MySQL format as in - * - * @see PR #367PR #367 * TEST_INDEX_DATE_TIME has three docs with login_time as date field with following values * 1.2015-01-01 @@ -366,7 +365,7 @@ public void testCursorCloseAPI() throws IOException { // using the cursor after its cleared, will throw exception Response response = null; try { - JSONObject queryResult = executeCursorQuery(cursor); + executeCursorQuery(cursor); } catch (ResponseException ex) { response = ex.getResponse(); } @@ -386,7 +385,7 @@ public void invalidCursorIdNotDecodable() throws IOException { Response response = null; try { - JSONObject resp = executeCursorQuery(randomCursor); + executeCursorQuery(randomCursor); } catch (ResponseException ex) { response = ex.getResponse(); } @@ -441,14 +440,14 @@ public void noPaginationWithNonJDBCFormat() throws IOException { } @Test - public void testMalformedCursorGracefullyHandled() throws IOException { + public void testMalformedCursorGracefullyHandled() { ResponseException result = assertThrows( "Expected query with malformed cursor to raise error, but didn't", ResponseException.class, () -> executeCursorQuery("d:a11b4db33f")); assertTrue(result.getMessage().contains("Malformed cursor")); - assertEquals(result.getResponse().getStatusLine().getStatusCode(), 400); + assertEquals(400, result.getResponse().getStatusLine().getStatusCode()); } public void verifyWithAndWithoutPaginationResponse( @@ -507,8 +506,7 @@ public String executeFetchAsStringQuery(String query, String fetchSize, String r sqlRequest.setJsonEntity(requestBody); Response response = client().performRequest(sqlRequest); - String responseString = getResponseBody(response, true); - return responseString; + return getResponseBody(response, true); } private void verifyIsV1Cursor(String cursor) { diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/cursor/DefaultCursor.java b/legacy/src/main/java/org/opensearch/sql/legacy/cursor/DefaultCursor.java index 2b0de9022c..39006f66c1 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/cursor/DefaultCursor.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/cursor/DefaultCursor.java @@ -62,8 +62,7 @@ public class DefaultCursor implements Cursor { private static final String SCHEMA_COLUMNS = "c"; private static final String FIELD_ALIAS_MAP = "a"; private static final String PIT_ID = "p"; - private static final String SEARCH_REQUEST = "r"; - private static final String SORT_FIELDS = "h"; + private static final String SORT_FIELDS = "h"; private static final ObjectMapper objectMapper = new ObjectMapper(); /** @@ -263,17 +262,15 @@ private static Map fieldAliasMap(JSONObject json) { } private static List getColumnsFromSchema(JSONArray schema) { - List columns = - IntStream.range(0, schema.length()) - .mapToObj( - i -> { - JSONObject jsonColumn = schema.getJSONObject(i); - return new Schema.Column( - jsonColumn.getString("name"), - jsonColumn.optString("alias", null), - Schema.Type.valueOf(jsonColumn.getString("type").toUpperCase())); - }) - .collect(Collectors.toList()); - return columns; + return IntStream.range(0, schema.length()) + .mapToObj( + i -> { + JSONObject jsonColumn = schema.getJSONObject(i); + return new Schema.Column( + jsonColumn.getString("name"), + jsonColumn.optString("alias", null), + Schema.Type.valueOf(jsonColumn.getString("type").toUpperCase())); + }) + .collect(Collectors.toList()); } }