Skip to content

Commit 78509f4

Browse files
authored
Rollback switch expressions for the SQL plugin (#82349)
It was updated to Java 8 compatibility in #82274
1 parent c55a460 commit 78509f4

File tree

5 files changed

+85
-49
lines changed

5 files changed

+85
-49
lines changed

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeConverter.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -260,24 +260,32 @@ static Object convert(Object v, EsType columnType, String typeString) throws SQL
260260

261261
private static Double doubleValue(Object v) {
262262
if (v instanceof String) {
263-
return switch ((String) v) {
264-
case "NaN" -> Double.NaN;
265-
case "Infinity" -> Double.POSITIVE_INFINITY;
266-
case "-Infinity" -> Double.NEGATIVE_INFINITY;
267-
default -> Double.parseDouble((String) v);
268-
};
263+
switch ((String) v) {
264+
case "NaN":
265+
return Double.NaN;
266+
case "Infinity":
267+
return Double.POSITIVE_INFINITY;
268+
case "-Infinity":
269+
return Double.NEGATIVE_INFINITY;
270+
default:
271+
return Double.parseDouble((String) v);
272+
}
269273
}
270274
return ((Number) v).doubleValue();
271275
}
272276

273277
private static Float floatValue(Object v) {
274278
if (v instanceof String) {
275-
return switch ((String) v) {
276-
case "NaN" -> Float.NaN;
277-
case "Infinity" -> Float.POSITIVE_INFINITY;
278-
case "-Infinity" -> Float.NEGATIVE_INFINITY;
279-
default -> Float.parseFloat((String) v);
280-
};
279+
switch ((String) v) {
280+
case "NaN":
281+
return Float.NaN;
282+
case "Infinity":
283+
return Float.POSITIVE_INFINITY;
284+
case "-Infinity":
285+
return Float.NEGATIVE_INFINITY;
286+
default:
287+
return Float.parseFloat((String) v);
288+
}
281289
}
282290
return ((Number) v).floatValue();
283291
}
@@ -297,13 +305,23 @@ private static <T> T failConversion(Object value, EsType columnType, String type
297305
}
298306

299307
private static Boolean asBoolean(Object val, EsType columnType, String typeString) throws SQLException {
300-
return switch (columnType) {
301-
case BOOLEAN, BYTE, SHORT, INTEGER, LONG, FLOAT, HALF_FLOAT, SCALED_FLOAT, DOUBLE -> Boolean.valueOf(
302-
Integer.signum(((Number) val).intValue()) != 0
303-
);
304-
case KEYWORD, TEXT -> Boolean.valueOf((String) val);
305-
default -> failConversion(val, columnType, typeString, Boolean.class);
306-
};
308+
switch (columnType) {
309+
case BOOLEAN:
310+
case BYTE:
311+
case SHORT:
312+
case INTEGER:
313+
case LONG:
314+
case FLOAT:
315+
case HALF_FLOAT:
316+
case SCALED_FLOAT:
317+
case DOUBLE:
318+
return Boolean.valueOf(Integer.signum(((Number) val).intValue()) != 0);
319+
case KEYWORD:
320+
case TEXT:
321+
return Boolean.valueOf((String) val);
322+
default:
323+
return failConversion(val, columnType, typeString, Boolean.class);
324+
}
307325
}
308326

309327
private static Byte asByte(Object val, EsType columnType, String typeString) throws SQLException {

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,23 @@ public enum SqlExceptionType {
325325
NOT_SUPPORTED(SQLFeatureNotSupportedException::new);
326326

327327
public static SqlExceptionType fromRemoteFailureType(String type) {
328-
return switch (type) {
329-
case "analysis_exception", "resource_not_found_exception", "verification_exception" -> DATA;
330-
case "planning_exception", "mapping_exception" -> NOT_SUPPORTED;
331-
case "parsing_exception" -> SYNTAX;
332-
case "security_exception" -> SECURITY;
333-
case "timeout_exception" -> TIMEOUT;
334-
default -> null;
335-
};
328+
switch (type) {
329+
case "analysis_exception":
330+
case "resource_not_found_exception":
331+
case "verification_exception":
332+
return DATA;
333+
case "planning_exception":
334+
case "mapping_exception":
335+
return NOT_SUPPORTED;
336+
case "parsing_exception":
337+
return SYNTAX;
338+
case "security_exception":
339+
return SECURITY;
340+
case "timeout_exception":
341+
return TIMEOUT;
342+
default:
343+
return null;
344+
}
336345
}
337346

338347
private final Function<String, SQLException> toException;

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/RemoteFailure.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static RemoteFailure parseResponseTopLevel(JsonParser parser) throws IOE
145145
RemoteFailure exception = null;
146146

147147
/* It'd be lovely to use the high level constructs that we have in core like ObjectParser
148-
* but, alas, we aren't going to modularize those out any time soon. */
148+
* but, alas, we aren't going to modularize those out any time soon. */
149149
JsonToken token = parser.nextToken();
150150
if (token != JsonToken.START_OBJECT) {
151151
throw new IllegalArgumentException(
@@ -158,7 +158,7 @@ private static RemoteFailure parseResponseTopLevel(JsonParser parser) throws IOE
158158
fieldName = parser.getCurrentName();
159159
} else {
160160
switch (fieldName) {
161-
case "error" -> {
161+
case "error":
162162
if (token != JsonToken.START_OBJECT && token != JsonToken.VALUE_STRING) {
163163
throw new IOException(
164164
"Expected [error] to be an object or string but was [" + token + "][" + parser.getText() + "]"
@@ -170,17 +170,14 @@ private static RemoteFailure parseResponseTopLevel(JsonParser parser) throws IOE
170170
exception = parseFailure(parser);
171171
}
172172
continue;
173-
}
174-
case "status" -> {
173+
case "status":
175174
if (token != JsonToken.VALUE_NUMBER_INT) {
176175
throw new IOException("Expected [status] to be a string but was [" + token + "][" + parser.getText() + "]");
177176
}
178177
// Intentionally ignored
179178
continue;
180-
}
181-
default -> throw new IOException(
182-
"Expected one of [error, status] but got [" + fieldName + "][" + parser.getText() + "]"
183-
);
179+
default:
180+
throw new IOException("Expected one of [error, status] but got [" + fieldName + "][" + parser.getText() + "]");
184181
}
185182
}
186183
}

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/StringUtils.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,14 @@ public static List<String> findSimilar(CharSequence match, Collection<String> po
279279
}
280280

281281
public static boolean parseBoolean(String input) {
282-
return switch (input) {
283-
case "true" -> true;
284-
case "false" -> false;
285-
default -> throw new IllegalArgumentException("must be [true] or [false]");
286-
};
282+
switch (input) {
283+
case "true":
284+
return true;
285+
case "false":
286+
return false;
287+
default:
288+
throw new IllegalArgumentException("must be [true] or [false]");
289+
}
287290
}
288291

289292
public static String asHexString(byte[] content, int offset, int length) {

x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/core/TimeValue.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,23 @@ public String getStringRep() {
6464
if (duration < 0) {
6565
return Long.toString(duration);
6666
}
67-
return switch (timeUnit) {
68-
case NANOSECONDS -> duration + "nanos";
69-
case MICROSECONDS -> duration + "micros";
70-
case MILLISECONDS -> duration + "ms";
71-
case SECONDS -> duration + "s";
72-
case MINUTES -> duration + "m";
73-
case HOURS -> duration + "h";
74-
case DAYS -> duration + "d";
75-
};
67+
switch (timeUnit) {
68+
case NANOSECONDS:
69+
return duration + "nanos";
70+
case MICROSECONDS:
71+
return duration + "micros";
72+
case MILLISECONDS:
73+
return duration + "ms";
74+
case SECONDS:
75+
return duration + "s";
76+
case MINUTES:
77+
return duration + "m";
78+
case HOURS:
79+
return duration + "h";
80+
case DAYS:
81+
return duration + "d";
82+
default:
83+
throw new IllegalArgumentException("unknown time unit: " + timeUnit.name());
84+
}
7685
}
7786
}

0 commit comments

Comments
 (0)