Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Compatibility-sensitive traits:
Compatibility-sensitive traits:

- Prepared statements are client-side SQL rendering, not server-side prepared statements. Changes to literal encoding or placeholder parsing are externally visible behavior.
- With `beta.row_binary_for_simple_insert=true`, the RowBinary writer stores the values of `setBytes`, `setAsciiStream`, `setUnicodeStream` and `setBinaryStream` byte for byte, and the values of `setCharacterStream`, `setNCharacterStream` and `setURL` as text. The length of an `InputStream` counts bytes, and the length of a `Reader` counts characters. The setter reads the stream before it returns. It fails with `SQLException` when the length is negative or the stream ends before the length.
- `DatabaseMetaData#getSchemas`, `#getTables` and `#getColumns` return the same result-set columns and values with and without `jdbc_metadata_use_show_statements`, with two exceptions for the `SHOW` statements: `getTables()` returns `null` in `REMARKS` and `TYPE_SCHEM`, because `SHOW TABLES` returns no table comment and no database engine. `getColumns()` with the `SHOW` statements sends one `DESCRIBE TABLE` query for each table that matches, and skips a database or table that fails with `UNKNOWN_TABLE`, `UNKNOWN_DATABASE`, `ACCESS_DENIED` or `DATALAKE_DATABASE_ERROR` (for example, a table with column-level grants only, which `system.columns` shows with the granted columns).
- JDBC `cluster_name` is compatibility-sensitive for cancellation behavior: when configured, `Statement.cancel()` issues `KILL QUERY ON CLUSTER '<name>'`, and when omitted it falls back to a local `KILL QUERY`.
- `Statement.cancel()` does nothing when the statement has no running query (the query is finished or not started yet). The `NOTHING_TO_KILL` error, which servers with `kill_throw_if_noop` enabled (the default since ClickHouse 26.11) return for such a `KILL QUERY`, is not thrown to the caller. Other `KILL QUERY` errors are thrown as `SQLException`.
Expand Down
8 changes: 8 additions & 0 deletions history/latest/3406.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- **[jdbc-v2]** Fixed the RowBinary writer for simple `INSERT ... VALUES (?, ...)` statements
(`beta.row_binary_for_simple_insert=true`). It dropped the value of `setBytes`, `setURL` and of all overloads of
`setAsciiStream`, `setUnicodeStream`, `setBinaryStream`, `setCharacterStream` and `setNCharacterStream`. The column
then got `NULL` or its default, or the insert failed with "An attempt to write null into not nullable column". Now
the writer stores the value: `byte[]` and `InputStream` values byte for byte, `Reader` and `URL` values as text. The
setter reads the stream before it returns. It fails with `SQLException` when the length is negative or the stream
ends before the length. (https://github.com/ClickHouse/clickhouse-java/issues/3406,
https://github.com/ClickHouse/clickhouse-java/issues/3334)
109 changes: 96 additions & 13 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class WriterStatementImpl extends PreparedStatementImpl implements Prepar

private static final Logger LOG = LoggerFactory.getLogger(WriterStatementImpl.class);

private static final int STREAM_BUFFER_SIZE = 8192;

private ByteArrayOutputStream out;
private ClickHouseBinaryFormatWriter writer;
private final TableSchema tableSchema;
Expand Down Expand Up @@ -204,7 +206,7 @@ public void setString(int parameterIndex, String x) throws SQLException {
@Override
public void setBytes(int parameterIndex, byte[] x) throws SQLException {
ensureOpen();

writer.setValue(parameterIndex, x);
}

@Override
Expand All @@ -224,69 +226,150 @@ public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {

@Override
public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
ensureOpen();
setAsciiStream(parameterIndex, x, (long) length);
}

@Override
public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
ensureOpen();

writer.setValue(parameterIndex, readBytes(parameterIndex, x, null));
}

@Override
public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
ensureOpen();

writer.setValue(parameterIndex, readBytes(parameterIndex, x, length));
}

@Override
public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
ensureOpen();
writer.setValue(parameterIndex, readBytes(parameterIndex, x, (long) length));
}

@Override
public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
ensureOpen();
setBinaryStream(parameterIndex, x, (long) length);
}


@Override
public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
ensureOpen();

writer.setValue(parameterIndex, readBytes(parameterIndex, x, null));
}

@Override
public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
ensureOpen();

writer.setValue(parameterIndex, readBytes(parameterIndex, x, length));
}

@Override
public void setCharacterStream(int parameterIndex, Reader x, int length) throws SQLException {
ensureOpen();
setCharacterStream(parameterIndex, x, (long) length);
}

@Override
public void setCharacterStream(int parameterIndex, Reader x, long length) throws SQLException {
ensureOpen();
writer.setString(parameterIndex, readChars(parameterIndex, x, length));
}

@Override
public void setCharacterStream(int parameterIndex, Reader x) throws SQLException {
ensureOpen();
writer.setString(parameterIndex, readChars(parameterIndex, x, null));
}

@Override
public void setNCharacterStream(int parameterIndex, Reader x) throws SQLException {
ensureOpen();

setCharacterStream(parameterIndex, x);
}

@Override
public void setNCharacterStream(int parameterIndex, Reader x, long length) throws SQLException {
ensureOpen();
setCharacterStream(parameterIndex, x, length);
}

/**
* Reads the bytes of a stream parameter. The writer keeps only the value, so the stream is read when
* the parameter is set.
*
* @param parameterIndex index of the parameter, for the error message
* @param x stream, or {@code null}
* @param length number of bytes to read, or {@code null} to read to the end of the stream
* @return the bytes, or {@code null} when {@code x} is {@code null}
* @throws SQLException when the length is negative, the stream ends before {@code length} bytes,
* or the stream cannot be read
*/
private static byte[] readBytes(int parameterIndex, InputStream x, Long length) throws SQLException {
if (x == null) {
return null;
}
checkStreamLength(parameterIndex, length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
byte[] buffer = new byte[STREAM_BUFFER_SIZE];
long remaining = length == null ? Long.MAX_VALUE : length;
try {
int read;
while (remaining > 0 && (read = x.read(buffer, 0, (int) Math.min(buffer.length, remaining))) != -1) {
bytes.write(buffer, 0, read);
remaining -= read;
}
} catch (IOException e) {
throw new SQLException("Failed to read the stream of parameter " + parameterIndex,
ExceptionUtils.SQL_STATE_SQL_ERROR, e);
}
checkStreamEnd(parameterIndex, length, remaining, "bytes");
return bytes.toByteArray();
}

/**
* Reads the characters of a character stream parameter. The writer keeps only the value, so the
* stream is read when the parameter is set.
*
* @param parameterIndex index of the parameter, for the error message
* @param x reader, or {@code null}
* @param length number of characters to read, or {@code null} to read to the end of the stream
* @return the characters, or {@code null} when {@code x} is {@code null}
* @throws SQLException when the length is negative, the stream ends before {@code length} characters,
* or the stream cannot be read
*/
private static String readChars(int parameterIndex, Reader x, Long length) throws SQLException {
if (x == null) {
return null;
}
checkStreamLength(parameterIndex, length);
StringBuilder chars = new StringBuilder();
char[] buffer = new char[STREAM_BUFFER_SIZE];
long remaining = length == null ? Long.MAX_VALUE : length;
try {
int read;
while (remaining > 0 && (read = x.read(buffer, 0, (int) Math.min(buffer.length, remaining))) != -1) {
chars.append(buffer, 0, read);
remaining -= read;
}
} catch (IOException e) {
throw new SQLException("Failed to read the stream of parameter " + parameterIndex,
ExceptionUtils.SQL_STATE_SQL_ERROR, e);
}
checkStreamEnd(parameterIndex, length, remaining, "characters");
return chars.toString();
}

private static void checkStreamLength(int parameterIndex, Long length) throws SQLException {
if (length != null && length < 0) {
throw new SQLException("Stream length cannot be negative: " + length + " (parameter " + parameterIndex
+ ")", ExceptionUtils.SQL_STATE_CLIENT_ERROR);
}
}

private static void checkStreamEnd(int parameterIndex, Long length, long remaining, String unit)
throws SQLException {
if (length != null && remaining > 0) {
throw new SQLException("The stream of parameter " + parameterIndex + " ended after "
+ (length - remaining) + " of " + length + " " + unit, ExceptionUtils.SQL_STATE_SQL_ERROR);
}
}

@Override
Expand Down Expand Up @@ -405,7 +488,7 @@ public void setNull(int parameterIndex, int sqlType, String typeName) throws SQL
@Override
public void setURL(int parameterIndex, URL x) throws SQLException {
ensureOpen();

writer.setString(parameterIndex, x == null ? null : x.toString());
}

@Override
Expand Down
Loading
Loading