Skip to content

Commit

Permalink
Rollback switch expressions for the SQL plugin (elastic#82349)
Browse files Browse the repository at this point in the history
It was updated to Java 8 compatibility in elastic#82274
  • Loading branch information
arteam authored Jan 10, 2022
1 parent c55a460 commit 78509f4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,32 @@ static Object convert(Object v, EsType columnType, String typeString) throws SQL

private static Double doubleValue(Object v) {
if (v instanceof String) {
return switch ((String) v) {
case "NaN" -> Double.NaN;
case "Infinity" -> Double.POSITIVE_INFINITY;
case "-Infinity" -> Double.NEGATIVE_INFINITY;
default -> Double.parseDouble((String) v);
};
switch ((String) v) {
case "NaN":
return Double.NaN;
case "Infinity":
return Double.POSITIVE_INFINITY;
case "-Infinity":
return Double.NEGATIVE_INFINITY;
default:
return Double.parseDouble((String) v);
}
}
return ((Number) v).doubleValue();
}

private static Float floatValue(Object v) {
if (v instanceof String) {
return switch ((String) v) {
case "NaN" -> Float.NaN;
case "Infinity" -> Float.POSITIVE_INFINITY;
case "-Infinity" -> Float.NEGATIVE_INFINITY;
default -> Float.parseFloat((String) v);
};
switch ((String) v) {
case "NaN":
return Float.NaN;
case "Infinity":
return Float.POSITIVE_INFINITY;
case "-Infinity":
return Float.NEGATIVE_INFINITY;
default:
return Float.parseFloat((String) v);
}
}
return ((Number) v).floatValue();
}
Expand All @@ -297,13 +305,23 @@ private static <T> T failConversion(Object value, EsType columnType, String type
}

private static Boolean asBoolean(Object val, EsType columnType, String typeString) throws SQLException {
return switch (columnType) {
case BOOLEAN, BYTE, SHORT, INTEGER, LONG, FLOAT, HALF_FLOAT, SCALED_FLOAT, DOUBLE -> Boolean.valueOf(
Integer.signum(((Number) val).intValue()) != 0
);
case KEYWORD, TEXT -> Boolean.valueOf((String) val);
default -> failConversion(val, columnType, typeString, Boolean.class);
};
switch (columnType) {
case BOOLEAN:
case BYTE:
case SHORT:
case INTEGER:
case LONG:
case FLOAT:
case HALF_FLOAT:
case SCALED_FLOAT:
case DOUBLE:
return Boolean.valueOf(Integer.signum(((Number) val).intValue()) != 0);
case KEYWORD:
case TEXT:
return Boolean.valueOf((String) val);
default:
return failConversion(val, columnType, typeString, Boolean.class);
}
}

private static Byte asByte(Object val, EsType columnType, String typeString) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,23 @@ public enum SqlExceptionType {
NOT_SUPPORTED(SQLFeatureNotSupportedException::new);

public static SqlExceptionType fromRemoteFailureType(String type) {
return switch (type) {
case "analysis_exception", "resource_not_found_exception", "verification_exception" -> DATA;
case "planning_exception", "mapping_exception" -> NOT_SUPPORTED;
case "parsing_exception" -> SYNTAX;
case "security_exception" -> SECURITY;
case "timeout_exception" -> TIMEOUT;
default -> null;
};
switch (type) {
case "analysis_exception":
case "resource_not_found_exception":
case "verification_exception":
return DATA;
case "planning_exception":
case "mapping_exception":
return NOT_SUPPORTED;
case "parsing_exception":
return SYNTAX;
case "security_exception":
return SECURITY;
case "timeout_exception":
return TIMEOUT;
default:
return null;
}
}

private final Function<String, SQLException> toException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static RemoteFailure parseResponseTopLevel(JsonParser parser) throws IOE
RemoteFailure exception = null;

/* It'd be lovely to use the high level constructs that we have in core like ObjectParser
* but, alas, we aren't going to modularize those out any time soon. */
* but, alas, we aren't going to modularize those out any time soon. */
JsonToken token = parser.nextToken();
if (token != JsonToken.START_OBJECT) {
throw new IllegalArgumentException(
Expand All @@ -158,7 +158,7 @@ private static RemoteFailure parseResponseTopLevel(JsonParser parser) throws IOE
fieldName = parser.getCurrentName();
} else {
switch (fieldName) {
case "error" -> {
case "error":
if (token != JsonToken.START_OBJECT && token != JsonToken.VALUE_STRING) {
throw new IOException(
"Expected [error] to be an object or string but was [" + token + "][" + parser.getText() + "]"
Expand All @@ -170,17 +170,14 @@ private static RemoteFailure parseResponseTopLevel(JsonParser parser) throws IOE
exception = parseFailure(parser);
}
continue;
}
case "status" -> {
case "status":
if (token != JsonToken.VALUE_NUMBER_INT) {
throw new IOException("Expected [status] to be a string but was [" + token + "][" + parser.getText() + "]");
}
// Intentionally ignored
continue;
}
default -> throw new IOException(
"Expected one of [error, status] but got [" + fieldName + "][" + parser.getText() + "]"
);
default:
throw new IOException("Expected one of [error, status] but got [" + fieldName + "][" + parser.getText() + "]");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,14 @@ public static List<String> findSimilar(CharSequence match, Collection<String> po
}

public static boolean parseBoolean(String input) {
return switch (input) {
case "true" -> true;
case "false" -> false;
default -> throw new IllegalArgumentException("must be [true] or [false]");
};
switch (input) {
case "true":
return true;
case "false":
return false;
default:
throw new IllegalArgumentException("must be [true] or [false]");
}
}

public static String asHexString(byte[] content, int offset, int length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,23 @@ public String getStringRep() {
if (duration < 0) {
return Long.toString(duration);
}
return switch (timeUnit) {
case NANOSECONDS -> duration + "nanos";
case MICROSECONDS -> duration + "micros";
case MILLISECONDS -> duration + "ms";
case SECONDS -> duration + "s";
case MINUTES -> duration + "m";
case HOURS -> duration + "h";
case DAYS -> duration + "d";
};
switch (timeUnit) {
case NANOSECONDS:
return duration + "nanos";
case MICROSECONDS:
return duration + "micros";
case MILLISECONDS:
return duration + "ms";
case SECONDS:
return duration + "s";
case MINUTES:
return duration + "m";
case HOURS:
return duration + "h";
case DAYS:
return duration + "d";
default:
throw new IllegalArgumentException("unknown time unit: " + timeUnit.name());
}
}
}

0 comments on commit 78509f4

Please sign in to comment.