From e2d53dc87b1d9c2d0f11059a1af216c78f12536f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E8=AF=A3?= Date: Sun, 1 Feb 2026 13:09:10 +0800 Subject: [PATCH] Fix: Support readonly=1 users by conditionally setting query settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: Users with readonly=1 cannot use clickhouse-native-jdbc because the driver unconditionally sets max_result_rows and result_overflow_mode settings, which violates the readonly=1 restriction. Solution: Only set these settings when maxRows > 0 (explicitly requested). This allows readonly=1 users to use the driver while preserving setMaxRows() functionality for other users. Benefits: - ✅ Supports readonly=1 users (recommended production config) - ✅ Maintains backward compatibility - ✅ Minimal code change (5 lines) - ✅ Follows principle: "don't set what you don't need" Tested: - ✅ Works with readonly=1 users (without setMaxRows) - ✅ Works with SQL LIMIT (recommended approach) - ✅ setMaxRows() still works for readonly=0 and readonly=2 Related issues: - yandex/clickhouse-jdbc#324 - ClickHouse/clickhouse-java#2441 - grafana/clickhouse-datasource#325 - dbeaver#38280 --- .../housepower/jdbc/statement/ClickHouseStatement.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/statement/ClickHouseStatement.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/statement/ClickHouseStatement.java index 2be870d4..5e4d9e65 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/statement/ClickHouseStatement.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/statement/ClickHouseStatement.java @@ -76,8 +76,12 @@ public boolean execute(String query) throws SQLException { public int executeUpdate(String query) throws SQLException { return ExceptionUtil.rethrowSQLException(() -> { - cfg.settings().put(SettingKey.max_result_rows, maxRows); - cfg.settings().put(SettingKey.result_overflow_mode, "break"); + // 仅在明确设置了 maxRows > 0 时才配置 settings + // 这样 readonly=1 用户可以正常使用(只要不调用 setMaxRows()) + if (maxRows > 0) { + cfg.settings().put(SettingKey.max_result_rows, maxRows); + cfg.settings().put(SettingKey.result_overflow_mode, "break"); + } extractDBAndTableName(query); Matcher matcher = VALUES_REGEX.matcher(query);