From 78509f44d1e906f715b92abffdef1c467d4eaa2a Mon Sep 17 00:00:00 2001 From: Artem Prigoda Date: Mon, 10 Jan 2022 11:14:45 +0100 Subject: [PATCH] Rollback switch expressions for the SQL plugin (#82349) It was updated to Java 8 compatibility in #82274 --- .../xpack/sql/jdbc/TypeConverter.java | 56 ++++++++++++------- .../sql/client/JreHttpUrlConnection.java | 25 ++++++--- .../xpack/sql/client/RemoteFailure.java | 13 ++--- .../xpack/sql/client/StringUtils.java | 13 +++-- .../xpack/sql/proto/core/TimeValue.java | 27 ++++++--- 5 files changed, 85 insertions(+), 49 deletions(-) diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeConverter.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeConverter.java index 2e41c52a343f9..1c0c22f3523da 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeConverter.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeConverter.java @@ -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(); } @@ -297,13 +305,23 @@ private static 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 { diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java index f2325c520b4e1..5cbb6061db08b 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java @@ -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 toException; diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/RemoteFailure.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/RemoteFailure.java index a62e011ca5b52..abf1228b5945c 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/RemoteFailure.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/RemoteFailure.java @@ -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( @@ -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() + "]" @@ -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() + "]"); } } } diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/StringUtils.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/StringUtils.java index e564464095a8c..a5d45f2e5b525 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/StringUtils.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/StringUtils.java @@ -279,11 +279,14 @@ public static List findSimilar(CharSequence match, Collection 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) { diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/core/TimeValue.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/core/TimeValue.java index ec5e2d6ed82d6..1d93ce0842780 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/core/TimeValue.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/core/TimeValue.java @@ -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()); + } } }