[jdbc-v1] PreparedStatement insert silently cuts a FixedString(N) value that is longer than N bytes
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 80/100
Rechercherichtung
Das Issue nennt FixedBytesSerDe.serialize in clickhouse-data/src/main/java/com/clickhouse/data/format/BinaryDataProcessor.java und dessen Verwendung in ClickHouseRowBinaryProcessor. Lies den Serializer an den angegebenen Zeilen und füge eine Prüfung hinzu, die Werte ablehnt, die länger als die feste Länge sind. Bestätige den Abschluss, indem du überprüfst, dass der reproduzierte Insert einen Fehler verursacht und keine Zeile speichert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Description
With the v1 JDBC driver (clickhouse.jdbc.v1=true), a PreparedStatement insert of a value that is longer than N bytes into a FixedString(N) column returns success and stores only the first N bytes. The rest of the value is lost, with no error. This occurs with setString and setBytes, for INSERT INTO t VALUES (?, ?) and INSERT INTO t, and for the column types FixedString(16), Nullable(FixedString(16)), LowCardinality(FixedString(16)) and Array(FixedString(16)). jdbc-v2 and client-v2 (POJO insert, RowBinaryFormatWriter) reject the same value with an error. The same defect was reported for clickhouse-go in https://github.com/ClickHouse/clickhouse-go/issues/2181.
Steps to reproduce
- Connect with
clickhouse.jdbc.v1=true. CREATE TABLE t (id UInt8, x FixedString(16)) ENGINE = MergeTree ORDER BY id- Prepare
INSERT INTO t VALUES (?, ?), callsetInt(1, 1)andsetString(2, "this is NOT the correct length.")(31 bytes), thenexecuteUpdate(). SELECT x FROM t
Error Log or Exception StackTrace
No exception. Output of the program below (3 of 3 runs):
executeUpdate: 1
stored: [this is NOT the ]
Expected Behaviour
An error, and no row stored. The server rejects the same value in the text formats:
INSERT INTO t VALUES (1, 'this is NOT the correct length.')
Code: 131. DB::Exception: String too long for type FixedString(16): ... (TOO_LARGE_STRING_SIZE)
INSERT INTO t FORMAT TSV -- row: 1<TAB>this is NOT the correct length.
Code: 131. DB::Exception: Too large value for FixedString(16): (at row 1) ... (TOO_LARGE_STRING_SIZE)
The same program without clickhouse.jdbc.v1=true (jdbc-v2) gets this server error, and a client-v2 POJO insert throws IllegalArgumentException: length of byte array value is 31, which should NOT longer than 16.
Code Example
Reproduction program (Java, 22 lines)
import java.sql.*;
public class Repro {
public static void main(String[] args) throws Exception {
// compress=0: the v1 driver cannot read compressed responses from server 26.9+ (#3107)
String url = "jdbc:clickhouse://localhost:8123/default?clickhouse.jdbc.v1=true&compress=0";
try (Connection c = DriverManager.getConnection(url, "default", "<password>");
Statement st = c.createStatement()) {
st.execute("DROP TABLE IF EXISTS t");
st.execute("CREATE TABLE t (id UInt8, x FixedString(16)) ENGINE = MergeTree ORDER BY id");
try (PreparedStatement ps = c.prepareStatement("INSERT INTO t VALUES (?, ?)")) {
ps.setInt(1, 1);
ps.setString(2, "this is NOT the correct length."); // 31 bytes
System.out.println("executeUpdate: " + ps.executeUpdate());
}
try (ResultSet rs = st.executeQuery("SELECT x FROM t")) {
rs.next();
System.out.println("stored: [" + rs.getString(1) + "]");
}
}
}
}
Root cause
The v1 RowBinary serializer of FixedString (FixedBytesSerDe.serialize, which FixedStringSerDe inherits) copies Math.min(bytes.length, length) bytes into a buffer of N bytes, so a longer value is cut and not rejected (BinaryDataProcessor.java:485-488). ClickHouseRowBinaryProcessor uses it for each FixedString column (ClickHouseRowBinaryProcessor.java:619-621).
The fix is a length check in FixedBytesSerDe.serialize. A v1 caller that relies on the cut would then get an error, as on jdbc-v2 and client-v2.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related: #2223 asks for an opt-in cut in client-v2; it is a feature request, not this defect.
Configuration
Client Configuration
// URL parameters: clickhouse.jdbc.v1=true&compress=0
Environment
- Cloud
- Client version:
mainat 8480ca001 (0.12.0-rc1-SNAPSHOT); the serializer code is the same in v0.11.2 - Language version: OpenJDK 17.0.21
- OS: Linux
ClickHouse Server
- ClickHouse Server version: 26.11.4.11
- ClickHouse Server non-default settings, if any: none
CREATE TABLEstatements for tables involved:CREATE TABLE t (id UInt8, x FixedString(16)) ENGINE = MergeTree ORDER BY id- Sample data for all these tables: one row, the 31-byte value
this is NOT the correct length.
Classification: impact: Data integrity · area: Data types & formats (FixedString)
Found by automated analysis (Polyglot AI) while checking clickhouse-go#2181 against the other client repos. Verified against a live server.
- Vorherrschende Sprache
- Java
- Sterne
- 1.6k
- Forks
- 657
- Ø Merge
- 2 T. 6 Std.
- Gemergte PRs (30 T.)
- 36
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus ClickHouse/clickhouse-java
-
client documentation
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 86/100
ClickHouse/clickhouse-java#3469 ·
Maintainer antworten meist innerhalb von 1 Tag
-
[jdbc-v2] Statement.execute and prepareStatement throw RuntimeException, not SQLException, for two statements in one callEvtl. vergeben Ein verknüpfter Pull Request ist offen oder bereits gemergt. Offenapi-diagnostics bug jdbc
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
ClickHouse/clickhouse-java#3464 ·
Maintainer antworten meist innerhalb von 1 Tag
-
tests
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
ClickHouse/clickhouse-java#3452 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug jdbc performance queries sql-parser
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 77/100
ClickHouse/clickhouse-java#3451 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug jdbc legacy queries
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
ClickHouse/clickhouse-java#3444 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in ClickHouse/clickhouse-java
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 66/100
Card-Forge/forge#13128 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug good first issue help wanted
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
apache/flink-agents#1480 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
Beam P2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 83/100
Maintainer antworten meist innerhalb von 1 Tag
-
ai-friendly good first issue help wanted
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
CategoricalData/CQL#148 ·
Maintainer antworten meist innerhalb von 1 Tag