Skip to content

[jdbc-v2] Base implementation of RowBinaryFormatWriter support in JDBC #2316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 30, 2025

Conversation

chernser
Copy link
Contributor

@chernser chernser commented Apr 18, 2025

Summary

Adds support for RowBinaryFormatWriter and fixes some issues:

  • Implementation forks PreparedStatementImpl and uses RowBinaryFormatWriter for simple "INSERT INTO t VALUES ()" case
  • Implementation is simply collecting values as encoded bytes and writes out when batch is executed.

Additional work is needed further:

  • support "INSER INTO t (col1, col2) VALUES (?, ?)". It requires better parsing code.

Closes #2171

Checklist

Delete items not relevant to your PR:

Sorry, something went wrong.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR provides a base implementation for RowBinaryFormatWriter support in JDBC and refactors PreparedStatement to handle batch insertions with enhanced parameter handling. Key changes include:

  • Adding tests and utility methods (e.g. unQuoteTableName) to support quoting operations.
  • Refactoring statement and prepared statement implementations for better batch handling and logging.
  • Updating RowBinaryFormatWriter to implement the new ClickHouseBinaryFormatWriter interface with additional tracking features.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcUtilsTest.java Adds unit test for unQuoteTableName utility method
jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java Refactors parameter count checks and adds batch insert tests
jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java Introduces unQuoteTableName method with regex pattern
jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java Updates method accessibility and logging level improvements
jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java Refactors SQL segmentation, parameter handling and batch insert logic
jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java Adds condition for using the WriterStatementImpl for INSERT queries
client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryFormatWriter.java Implements ClickHouseBinaryFormatWriter with row count tracking and new value-setting methods
client-v2/src/main/java/com/clickhouse/client/api/data_formats/ClickHouseBinaryFormatWriter.java Introduces a new interface for binary format writers

stmt.setInt(1, rnd.nextInt());
stmt.setFloat(2, rnd.nextFloat());
stmt.setInt(3, rnd.nextInt());
stmt.addBatch();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also test with clearParameters() and also extending the data types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

"INSERT INTO \n `%s` \nVALUES (?, multiply(?, 10), ?)", // only string possible
"INSERT INTO\n `%s` \nVALUES (?, ?, ?)", // row binary writer
};
Class<?>[] impl = new Class<?>[]{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a JMH to test show check the differences in performance between two implementation (so we could explain why it is more beneficial to use the rowbinary )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is possible. With a feature flag it is possible.

@chernser chernser requested review from Copilot and mzitnik April 23, 2025 22:22
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds beta support for the RowBinaryFormatWriter in simple insert statements for the JDBC driver and fixes/improves related parsing logic. Key changes include updated statement parsing using the new StatementParser, introduction of a BETA_ROW_BINARY_WRITER driver property with accompanying implementation in PreparedStatementImpl and ConnectionImpl, and enhancements for the RowBinaryFormatWriter to manage row state and count.

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.

Show a summary per file
File Description
jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/StatementParserTest.java Added tests for various SQL types and comment handling.
jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcUtilsTest.java Added tests for unquoting table names.
jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java Updated tests to cover parameter type changes and beta batch insert functionality.
jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/StatementParser.java Refactored SQL statement parsing logic and added table name extraction.
jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/DriverProperties.java Introduced new beta property for the row binary writer.
jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java New prepareStatement logic conditionally returns WriterStatementImpl based on beta configuration.
client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryFormatWriter.java Updated RowBinaryFormatWriter to implement ClickHouseBinaryFormatWriter, managing row state and count.
client-v2/src/main/java/com/clickhouse/client/api/data_formats/ClickHouseBinaryFormatWriter.java Added new interface for binary format writers.
Comments suppressed due to low confidence (2)

client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryFormatWriter.java:90

  • Consider adding tests to cover the behavior of commitRow when no values have been set, ensuring that the rowCount is only incremented when a row is actually started.
if (rowStarted) {

jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java:370

  • Consider adding tests to verify that the beta writer path correctly selects WriterStatementImpl, and that the conditions (absence of a column list and function parameters) are properly validated.
if (config.isBetaFeatureEnabled(DriverProperties.BETA_ROW_BINARY_WRITER)) {

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot
44.9% Coverage on New Code (required ≥ 80%)
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE


if (this.statementType == StatementType.INSERT) {
if (this.statementType == StatementParser.StatementType.INSERT) {
insertIntoSQL = originalSql.substring(0, originalSql.indexOf("VALUES") + 6);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2283 a reported probleam for indexOf("VALUES")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is the problem. But with parser. This PR should not fix it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you open an issues for the parser implementation

@chernser chernser merged commit 2a9ad55 into main Apr 30, 2025
24 of 25 checks passed
@chernser chernser deleted the jdbc_v2_row_binary_writer branch April 30, 2025 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[jdbc-v2] JDBC Bulk Insert performance using RowBinary
2 participants