Skip to content

Commit

Permalink
More quick fixes!
Browse files Browse the repository at this point in the history
Signed-off-by: currantw <[email protected]>
  • Loading branch information
currantw committed Dec 5, 2024
1 parent ba1936c commit 9af0daa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 56 deletions.
42 changes: 15 additions & 27 deletions core/src/test/java/org/opensearch/sql/utils/ComparisonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
};
}
}
28 changes: 13 additions & 15 deletions integ-test/src/test/java/org/opensearch/sql/legacy/CursorIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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 (?, ?)\","
Expand All @@ -241,18 +240,18 @@ public void testCursorWithPreparedStatement() throws IOException {
+ " }"
+ "]"
+ "}"
+ TestsConstants.TEST_INDEX_ACCOUNT));
+ TestsConstants.TEST_INDEX_ACCOUNT);
assertTrue(response.has(CURSOR));
verifyIsV1Cursor(response.getString(CURSOR));
}

@Test
public void testRegressionOnDateFormatChange() throws IOException {
loadIndex(Index.DATETIME);
/**
* With pagination, the field should be date formatted to MySQL format as in
*
* @see <a href="https://github.com/opendistro-for-elasticsearch/sql/pull/367">PR #367</a
/*
With pagination, the field should be date formatted to MySQL format as in
@see <a href="https://github.com/opendistro-for-elasticsearch/sql/pull/367">PR #367</a
* <pre>
* TEST_INDEX_DATE_TIME has three docs with login_time as date field with following values
* 1.2015-01-01
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand Down Expand Up @@ -263,17 +262,15 @@ private static Map<String, String> fieldAliasMap(JSONObject json) {
}

private static List<Schema.Column> getColumnsFromSchema(JSONArray schema) {
List<Schema.Column> 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());
}
}

0 comments on commit 9af0daa

Please sign in to comment.