diff --git a/dev-tools/checkstyle_suppressions.xml b/dev-tools/checkstyle_suppressions.xml index 3f987ea4f7c..481c6861a4d 100644 --- a/dev-tools/checkstyle_suppressions.xml +++ b/dev-tools/checkstyle_suppressions.xml @@ -8,8 +8,6 @@ - - diff --git a/sql/jdbc-proto/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/ProtoUtils.java b/sql/jdbc-proto/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/ProtoUtils.java index b7708c56a01..e8ade14ab32 100644 --- a/sql/jdbc-proto/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/ProtoUtils.java +++ b/sql/jdbc-proto/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/ProtoUtils.java @@ -66,7 +66,8 @@ public class ProtoUtils { case LONGVARCHAR: result = in.readUTF(); break; - // NB: date/time is kept in its raw form since the JdbcDriver has to do calendar/timezone conversion anyway and thus the long value is relevant + // NB: date/time is kept in its raw form since the JdbcDriver has to do calendar/timezone + // conversion anyway and thus the long value is relevant case TIMESTAMP: result = in.readLong(); break; diff --git a/sql/jdbc-proto/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/QueryInitResponse.java b/sql/jdbc-proto/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/QueryInitResponse.java index 433e35130b5..285732ced80 100644 --- a/sql/jdbc-proto/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/QueryInitResponse.java +++ b/sql/jdbc-proto/src/main/java/org/elasticsearch/xpack/sql/jdbc/net/protocol/QueryInitResponse.java @@ -25,7 +25,8 @@ public class QueryInitResponse extends Response { public final List columns; public final Payload data; - public QueryInitResponse(long serverTimeQueryReceived, long serverTimeResponseSent, String requestId, List columns, Payload data) { + public QueryInitResponse(long serverTimeQueryReceived, long serverTimeResponseSent, String requestId, List columns, + Payload data) { this.serverTimeQueryReceived = serverTimeQueryReceived; this.serverTimeResponseSent = serverTimeResponseSent; this.requestId = requestId; diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/debug/Debug.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/debug/Debug.java index 2b960202c7f..8673dbc6aef 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/debug/Debug.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/debug/Debug.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.sql.jdbc.debug; import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcConfiguration; import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcException; -import org.elasticsearch.xpack.sql.jdbc.util.IOUtils; import org.elasticsearch.xpack.sql.net.client.SuppressForbidden; import java.io.OutputStreamWriter; @@ -166,7 +165,9 @@ public final class Debug { OUTPUT_REFS.remove(out); DebugLog d = OUTPUT_CACHE.remove(out); if (d != null) { - IOUtils.close(d.print); + if (d.print != null) { + d.print.close(); + } } } else { @@ -182,7 +183,9 @@ public final class Debug { // clear the streams for (DebugLog d : OUTPUT_CACHE.values()) { - IOUtils.close(d.print); + if (d.print != null) { + d.print.close(); + } } OUTPUT_CACHE.clear(); diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcConnection.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcConnection.java index 6ab825e90fa..a589612e68e 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcConnection.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcConnection.java @@ -31,6 +31,9 @@ import org.elasticsearch.xpack.sql.jdbc.debug.Debug; import org.elasticsearch.xpack.sql.jdbc.net.client.JdbcHttpClient; import org.elasticsearch.xpack.sql.net.client.util.StringUtils; +/** + * Implementation of {@link Connection} for Elasticsearch. + */ public class JdbcConnection implements Connection, JdbcWrapper { private final String url, userName; @@ -254,14 +257,16 @@ public class JdbcConnection implements Connection, JdbcWrapper { } @Override - public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) + throws SQLException { checkOpen(); checkHoldability(resultSetHoldability); return prepareStatement(sql, resultSetType, resultSetConcurrency); } @Override - public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) + throws SQLException { checkOpen(); checkHoldability(resultSetHoldability); return prepareCall(sql, resultSetType, resultSetConcurrency); diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDatabaseMetaData.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDatabaseMetaData.java index 3ca3b3451e8..06788c2683b 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDatabaseMetaData.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcDatabaseMetaData.java @@ -24,11 +24,12 @@ import java.util.List; import static org.elasticsearch.xpack.sql.net.client.util.StringUtils.EMPTY; import static org.elasticsearch.xpack.sql.net.client.util.StringUtils.hasText; -// Schema Information based on Postgres -// https://www.postgresql.org/docs/9.0/static/information-schema.html - -// Currently virtually/synthetic tables are not supported so the client returns -// empty data instead of creating a query +/** + * Implementation of {@link DatabaseMetaData} for Elasticsearch. Draws inspiration + * from + * PostgreSQL. Virtual/synthetic tables are not supported so the client returns + * empty data instead of creating a query. + */ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper { private final JdbcConnection con; @@ -650,7 +651,8 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper { } @Override - public ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern, String columnNamePattern) throws SQLException { + public ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern, String columnNamePattern) + throws SQLException { return emptySet(con.cfg, "PARAMETERS", "PROCEDURE_CAT", @@ -781,7 +783,8 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper { } @Override - public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException { + public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) + throws SQLException { List info = columnInfo("COLUMNS", "TABLE_CAT", "TABLE_SCHEM", @@ -815,7 +818,8 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper { } String cat = defaultCatalog(); - List columns = con.client.metaInfoColumns(sqlWildcardToSimplePattern(tableNamePattern), sqlWildcardToRegexPattern(columnNamePattern)); + List columns = con.client.metaInfoColumns( + sqlWildcardToSimplePattern(tableNamePattern), sqlWildcardToRegexPattern(columnNamePattern)); Object[][] data = new Object[columns.size()][]; for (int i = 0; i < data.length; i++) { data[i] = new Object[24]; @@ -886,7 +890,8 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper { } @Override - public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException { + public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, String foreignCatalog, + String foreignSchema, String foreignTable) throws SQLException { throw new SQLFeatureNotSupportedException("Cross reference not supported"); } @@ -1041,7 +1046,8 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper { } @Override - public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, String attributeNamePattern) throws SQLException { + public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, String attributeNamePattern) + throws SQLException { return emptySet(con.cfg, "ATTRIBUTES", "TYPE_CAT", @@ -1145,7 +1151,8 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper { } @Override - public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, String columnNamePattern) throws SQLException { + public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, String columnNamePattern) + throws SQLException { return emptySet(con.cfg, "FUNCTION_COLUMNS", "FUNCTION_CAT", @@ -1167,7 +1174,8 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper { } @Override - public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException { + public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) + throws SQLException { return emptySet(con.cfg, "PSEUDO_COLUMNS", "TABLE_CAT", diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/BytesArray.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/BytesArray.java index 8402b011efa..f2958b602e2 100644 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/BytesArray.java +++ b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/BytesArray.java @@ -38,10 +38,6 @@ public class BytesArray { this.size = size; } - public BytesArray(String source) { - bytes(source); - } - public BytesArray(Bytes bytes) { this(bytes.bytes(), 0, bytes.size()); } @@ -84,12 +80,6 @@ public class BytesArray { this.offset = ba.offset; } - public void bytes(String from) { - size = 0; - offset = 0; - UnicodeUtil.UTF16toUTF8(from, 0, from.length(), this.bytes, 0); - } - public void size(int size) { this.size = size; } diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/FastByteArrayOutputStream.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/FastByteArrayOutputStream.java deleted file mode 100644 index 4aff7ed132c..00000000000 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/FastByteArrayOutputStream.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.sql.jdbc.util; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * Taken from Elasticsearch - copy of org.elasticsearch.common.io.FastByteArrayOutputStream with some enhancements, mainly in allowing access to the underlying byte[]. - * - * Similar to {@link java.io.ByteArrayOutputStream} just not synced. - */ -public class FastByteArrayOutputStream extends OutputStream { - - private BytesArray data; - - /** - * Creates a new byte array output stream. The buffer capacity is - * initially 1024 bytes, though its size increases if necessary. - *

- * ES: We use 1024 bytes since we mainly use this to build json/smile - * content in memory, and rarely does the 32 byte default in ByteArrayOutputStream fits... - */ - public FastByteArrayOutputStream() { - this(1024); - } - - /** - * Creates a new byte array output stream, with a buffer capacity of - * the specified size, in bytes. - * - * @param size the initial size. - */ - public FastByteArrayOutputStream(int size) { - Assert.isTrue(size >= 0, "Negative initial size: " + size); - data = new BytesArray(size); - } - - public FastByteArrayOutputStream(BytesArray data) { - this.data = data; - } - - /** - * Writes the specified byte to this byte array output stream. - * - * @param b the byte to be written. - */ - public void write(int b) { - data.add(b); - } - - /** - * Writes len bytes from the specified byte array - * starting at offset off to this byte array output stream. - *

- * NO checks for bounds, parameters must be ok! - * - * @param b the data. - * @param off the start offset in the data. - * @param len the number of bytes to write. - */ - public void write(byte b[], int off, int len) { - data.add(b, off, len); - } - - /** - * Writes the complete contents of this byte array output stream to - * the specified output stream argument, as if by calling the output - * stream's write method using out.write(buf, 0, count). - * - * @param out the output stream to which to write the data. - * @throws IOException if an I/O error occurs. - */ - public void writeTo(OutputStream out) throws IOException { - out.write(data.bytes, 0, data.size); - } - - public BytesArray bytes() { - return data; - } - - public void setBytes(byte[] data, int size) { - this.data.bytes(data, size); - } - - /** - * Returns the current size of the buffer. - * - * @return the value of the count field, which is the number - * of valid bytes in this output stream. - * @see java.io.ByteArrayOutputStream#count - */ - public long size() { - return data.length(); - } - - /** - * Resets the count field of this byte array output - * stream to zero, so that all currently accumulated output in the - * output stream is discarded. The output stream can be used again, - * reusing the already capacity buffer space. - * - * @see java.io.ByteArrayInputStream#count - */ - public void reset() { - data.reset(); - } - - public String toString() { - return data.toString(); - } - - /** - * Closing a ByteArrayOutputStream has no effect. The methods in - * this class can be called after the stream has been closed without - * generating an IOException. - */ - public void close() throws IOException {} -} \ No newline at end of file diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/IOUtils.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/IOUtils.java deleted file mode 100644 index e4307236856..00000000000 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/IOUtils.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.sql.jdbc.util; - -import java.io.Closeable; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; -import java.util.Properties; - -public abstract class IOUtils { - - public static Properties propsFromString(String source) { - Properties copy = new Properties(); - if (source != null) { - try { - copy.load(new StringReader(source)); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - } - return copy; - } - - public static void close(Closeable closable) { - if (closable != null) { - try { - closable.close(); - } catch (IOException e) { - // silently ignore - } - } - } - - - public static String asString(InputStream in) throws IOException { - return asBytes(in).toString(); - } - - public static BytesArray asBytes(InputStream in) throws IOException { - BytesArray ba = unwrapStreamBuffer(in); - if (ba != null) { - return ba; - } - return asBytes(new BytesArray(in.available()), in); - } - - public static BytesArray asBytes(BytesArray ba, InputStream input) throws IOException { - BytesArray buf = unwrapStreamBuffer(input); - if (buf != null) { - ba.bytes(buf); - return ba; - } - - FastByteArrayOutputStream bos = new FastByteArrayOutputStream(ba); - byte[] buffer = new byte[1024]; - int read = 0; - try (InputStream in = input) { - while ((read = in.read(buffer)) != -1) { - bos.write(buffer, 0, read); - } - } finally { - // non needed but used to avoid the warnings - bos.close(); - } - return bos.bytes(); - } - - private static BytesArray unwrapStreamBuffer(InputStream in) { - if (in instanceof FastByteArrayInputStream) { - return ((FastByteArrayInputStream) in).data; - } - - return null; - } -} \ No newline at end of file diff --git a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/UnicodeUtil.java b/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/UnicodeUtil.java deleted file mode 100644 index 1e2a2a0ffce..00000000000 --- a/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/util/UnicodeUtil.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.sql.jdbc.util; - -public class UnicodeUtil { - - - public static final int UNI_SUR_HIGH_START = 0xD800; - public static final int UNI_SUR_HIGH_END = 0xDBFF; - public static final int UNI_SUR_LOW_START = 0xDC00; - public static final int UNI_SUR_LOW_END = 0xDFFF; - public static final int UNI_REPLACEMENT_CHAR = 0xFFFD; - - private static final long HALF_SHIFT = 10; - - private static final int SURROGATE_OFFSET = Character.MIN_SUPPLEMENTARY_CODE_POINT - (UNI_SUR_HIGH_START << HALF_SHIFT) - UNI_SUR_LOW_START; - - /** Encode characters from this String, starting at offset - * for length characters. Output to the destination array - * will begin at {@code outOffset}. It is the responsibility of the - * caller to make sure that the destination array is large enough. - *

- * note this method returns the final output offset (outOffset + number of bytes written) - */ - public static int UTF16toUTF8(final CharSequence s, final int offset, final int length, byte[] out, int outOffset) { - final int end = offset + length; - - int upto = outOffset; - for (int i = offset; i < end; i++) { - final int code = (int) s.charAt(i); - - if (code < 0x80) - out[upto++] = (byte) code; - else if (code < 0x800) { - out[upto++] = (byte) (0xC0 | (code >> 6)); - out[upto++] = (byte) (0x80 | (code & 0x3F)); - } - else if (code < 0xD800 || code > 0xDFFF) { - out[upto++] = (byte) (0xE0 | (code >> 12)); - out[upto++] = (byte) (0x80 | ((code >> 6) & 0x3F)); - out[upto++] = (byte) (0x80 | (code & 0x3F)); - } - else { - // surrogate pair - // confirm valid high surrogate - if (code < 0xDC00 && (i < end - 1)) { - int utf32 = (int) s.charAt(i + 1); - // confirm valid low surrogate and write pair - if (utf32 >= 0xDC00 && utf32 <= 0xDFFF) { - utf32 = (code << 10) + utf32 + SURROGATE_OFFSET; - i++; - out[upto++] = (byte) (0xF0 | (utf32 >> 18)); - out[upto++] = (byte) (0x80 | ((utf32 >> 12) & 0x3F)); - out[upto++] = (byte) (0x80 | ((utf32 >> 6) & 0x3F)); - out[upto++] = (byte) (0x80 | (utf32 & 0x3F)); - continue; - } - } - // replace unpaired surrogate or out-of-order low surrogate - // with substitution character - out[upto++] = (byte) 0xEF; - out[upto++] = (byte) 0xBF; - out[upto++] = (byte) 0xBD; - } - } - //assert matches(s, offset, length, out, upto); - return upto; - } -} diff --git a/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/DataLoader.java b/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/DataLoader.java index 657c131ed51..bba8871a77b 100644 --- a/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/DataLoader.java +++ b/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/DataLoader.java @@ -72,8 +72,8 @@ public class DataLoader { } bulk.append("}\n"); }); - client.performRequest("POST", "/test_emp/emp/_bulk", singletonMap("refresh", "true"), new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON)); - + client.performRequest("POST", "/test_emp/emp/_bulk", singletonMap("refresh", "true"), + new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON)); } private static void csvToLines(String name, CheckedBiConsumer, List, Exception> consumeLine) throws Exception { diff --git a/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/SpecBaseIntegrationTestCase.java b/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/SpecBaseIntegrationTestCase.java index d2a3c65af89..8436d0bc6fc 100644 --- a/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/SpecBaseIntegrationTestCase.java +++ b/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/SpecBaseIntegrationTestCase.java @@ -81,8 +81,11 @@ public abstract class SpecBaseIntegrationTestCase extends JdbcIntegrationTestCas } /** - * Implementations should pay attention on using {@link #executeJdbcQuery(Connection, String)} (typically for ES connections) - * and {@link #assertResults(ResultSet, ResultSet)} which takes into account logging/debugging results (through {@link #logEsResultSet()}. + * Implementations should pay attention on using + * {@link #executeJdbcQuery(Connection, String)} (typically for + * ES connections) and {@link #assertResults(ResultSet, ResultSet)} + * which takes into account logging/debugging results (through + * {@link #logEsResultSet()}. */ protected abstract void doTest() throws Throwable; diff --git a/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/SqlProtoHandler.java b/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/SqlProtoHandler.java index ef7b169c91c..cb2dcbd482f 100644 --- a/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/SqlProtoHandler.java +++ b/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/framework/SqlProtoHandler.java @@ -28,7 +28,8 @@ class SqlProtoHandler extends ProtoHandler { SqlProtoHandler(Client client) { super(client, response -> AbstractSqlServer.write(AbstractProto.CURRENT_VERSION, response)); - this.server = new JdbcServer(TestUtils.planExecutor(client), clusterName, () -> info.getNode().getName(), info.getVersion(), info.getBuild()); + this.server = new JdbcServer(TestUtils.planExecutor(client), clusterName, () -> info.getNode().getName(), info.getVersion(), + info.getBuild()); } @Override