[jdbc-v2] ANTLR4 parsers reject 63 non-keyword words (schema, config, queue, ...) as names; a batch insert into such a table sends one INSERT per row
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 77/100
調査の方向性
まず ClickHouseParser.g4 のルール keyword と keywordForAlias を確認します。Issue によると、ここには列挙されたトークンと TIMEOUT が不足しています。SHARDS がどのように追加されたかを確認するため、関連する PR #3446 の変更を読み、その後 jdbc-v2 配下のパーサーテストで該当するテスト範囲を確認します。両方の ANTLR4 パーサーが、報告された名前とエイリアスの位置で列挙された単語を受け入れ、暗黙のエイリアスとしての timeout も受け入れるようになれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Description
With jdbc_sql_parser=ANTLR4 or ANTLR4_PARAMS_PARSER, a statement that uses one of 63 words as a table name, a column name, or an alias parses with errors. The server accepts all 63 words as plain names. The default parser JAVACC is not affected.
The 63 words:
allow arbitrary asynchronous blocking cancel client collections compiled condition config custom dns embedded failpoint fuzzer grpc hive https introspection jdbc jemalloc kafka listen load loading management mark mmap model mongo moves mysql nats odbc page policies postgres postgresql prometheus proxy pulling queue queues quotas rabbitmq readiness redis reduce replicas replication restart schema secrets secure shutdown sources sqlite tcp uncompressed unload views virtual wait
Results and stored rows stay correct. The visible effects for a table with such a name:
PreparedStatement.executeBatch()ofINSERT INTO queue VALUES (?)sends one INSERT for each row: 1000 rows take 1000 queries and 60 s. The same batch intoqueue_tis one query of 56 ms.- With
beta.row_binary_for_simple_insert=true, the insert does not use the RowBinary writer. PreparedStatement.getMetaData()of the insert sends aDESCRIBEof invalid SQL and logs a WARN withSyntax error.
Also, timeout as an implicit alias (FROM t timeout) parses with errors.
Related: #3428 is the same defect for shards; its PR #3446 adds only SHARDS.
Steps to reproduce
- Run the program in "Code Example" (argument: the JDBC URL; password in
CLICKHOUSE_PASSWORD). For each parser, it parses four statements, then inserts a batch of 1000 rows intoqueueand intoqueue_t. - A sweep of all 63 words in five positions (
SELECT 1 AS w,SELECT w FROM t,CREATE TABLE w ...,INSERT INTO w VALUES (?),WHERE w = ?) giveshasErrors=truefor each word and position with both ANTLR4 parsers, andfalsewithJAVACC.
Error Log or Exception StackTrace
ANTLR4: SELECT 1 AS schema -> hasErrors=true
ANTLR4: SELECT config FROM t -> hasErrors=true
ANTLR4: INSERT INTO queue VALUES (?) -> hasErrors=true
ANTLR4: SELECT * FROM (SELECT * FROM t timeout) WHERE x = ? -> hasErrors=true
ANTLR4: executeBatch of 1000 rows into queue -> 1000 INSERT queries, 61494 ms
ANTLR4: executeBatch of 1000 rows into queue_t -> 1 INSERT queries, 56 ms
ANTLR4_PARAMS_PARSER: executeBatch of 1000 rows into queue -> 1000 INSERT queries, 60445 ms
JAVACC: executeBatch of 1000 rows into queue -> 1 INSERT queries, 12 ms
ANTLR4_PARAMS_PARSER prints the same four hasErrors=true lines; JAVACC prints hasErrors=false for all four.
Expected Behaviour
None of the 63 words is a server keyword, and the server accepts each as an alias, a table name, a column name, and an implicit alias:
SELECT groupArray(keyword) FROM system.keywords WHERE upper(keyword) IN ('ALLOW', ..., 'WAIT', 'SHARDS')
['SHARDS']
-- for each word w, with no error:
SELECT 1 AS w; CREATE TABLE w (w Int32) ENGINE = Memory; INSERT INTO w VALUES (7);
SELECT w FROM w; SELECT * FROM (SELECT * FROM w w) WHERE w = 7
All three parsers parse these statements with no errors, so a batch insert is one INSERT, as for queue_t.
Root cause
The ANTLR4 lexer has a token for each word (for example SCHEMA, ClickHouseLexer.g4:301), but the parser rule keyword (ClickHouseParser.g4:1225), which lets a token be an identifier, does not list the 63 tokens, and keywordForAlias (:1540) does not list them or TIMEOUT. On a parse error, the driver drops the values list of the insert (SqlParserFacade.java:264-267), so executeBatch() runs each row as its own statement.
The fix adds the missing tokens to the two rules.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Code Example
Reproduction program (Java, 51 lines, in jdbc-v2/src/test/java)
package repro;
import com.clickhouse.jdbc.Driver;
import com.clickhouse.jdbc.internal.JdbcConfiguration;
import com.clickhouse.jdbc.internal.SqlParserFacade;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class Repro {
public static void main(String[] args) throws Exception {
String url = args.length > 0 ? args[0] : "jdbc:clickhouse://localhost:8123/default";
for (String parser : new String[]{"JAVACC", "ANTLR4", "ANTLR4_PARAMS_PARSER"}) {
SqlParserFacade p = SqlParserFacade.getParser(parser, new JdbcConfiguration(url, new Properties()));
for (String sql : new String[]{"SELECT 1 AS schema", "SELECT config FROM t", "INSERT INTO queue VALUES (?)",
"SELECT * FROM (SELECT * FROM t timeout) WHERE x = ?"}) {
System.out.println(parser + ": " + sql + " -> hasErrors=" + p.parsePreparedStatement(sql).isHasErrors());
}
Properties props = new Properties();
props.setProperty("user", "default");
props.setProperty("password", System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", ""));
props.setProperty("jdbc_sql_parser", parser);
try (Connection c = new Driver().connect(url, props); Statement s = c.createStatement()) {
for (String t : new String[]{"queue", "queue_t"}) {
s.execute("CREATE OR REPLACE TABLE " + t + " (config Int32) ENGINE = MergeTree ORDER BY config");
long t0 = System.nanoTime();
try (PreparedStatement ps = c.prepareStatement("INSERT INTO " + t + " VALUES (?)")) {
for (int i = 0; i < 1000; i++) {
ps.setInt(1, i);
ps.addBatch();
}
ps.executeBatch();
}
long ms = (System.nanoTime() - t0) / 1_000_000;
s.execute("SYSTEM FLUSH LOGS");
try (ResultSet rs = s.executeQuery("SELECT count() FROM system.query_log WHERE type = 'QueryFinish'"
+ " AND query_kind = 'Insert' AND has(tables, currentDatabase() || '." + t + "')"
+ " AND event_time > now() - INTERVAL 5 MINUTE AND query_start_time_microseconds >= "
+ "(SELECT metadata_modification_time FROM system.tables WHERE database = currentDatabase() AND name = '" + t + "')")) {
rs.next();
System.out.println(parser + ": executeBatch of 1000 rows into " + t + " -> " + rs.getLong(1)
+ " INSERT queries, " + ms + " ms");
}
}
}
}
}
}
Configuration
Client Configuration
// jdbc_sql_parser=ANTLR4 or ANTLR4_PARAMS_PARSER (default: JAVACC, not affected)
Environment
- Cloud
- Client version:
main8480ca001 (0.12.0-rc1-SNAPSHOT) - Language version: OpenJDK 17.0.21.1
- OS: Linux
ClickHouse Server
- ClickHouse Server version: 26.11.2.4
- ClickHouse Server non-default settings, if any: none
CREATE TABLEstatements for tables involved: in the program- Sample data for all these tables: in the program
Classification: impact: Performance · area: Queries (SQL parser keywords)
Found by automated analysis (Polyglot AI) while working on #3428. Verified against a live server.
- 主要言語
- Java
- スター
- 1.6k
- フォーク
- 657
- 平均マージ
- 2日 6時間
- マージ済み PR(30日)
- 36
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ClickHouse/clickhouse-java のほかの issue
-
api-diagnostics bug jdbc
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
ClickHouse/clickhouse-java#3464 ·
メンテナーはふだん 1 日以内に返信
-
tests
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
ClickHouse/clickhouse-java#3452 ·
メンテナーはふだん 1 日以内に返信
-
bug jdbc legacy queries
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
ClickHouse/clickhouse-java#3444 ·
メンテナーはふだん 1 日以内に返信
-
api-diagnostics bug client jdbc
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
ClickHouse/clickhouse-java#3439 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug client jdbc legacy queries
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
ClickHouse/clickhouse-java#3438 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
ClickHouse/clickhouse-java の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 60/100
quelea-projection/Quelea#749 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 92/100
erkamyaman/rust-anything#963 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 半日 初心者へのやさしさ 76/100
-
Deferred
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Sanne/incus-spawn#2768 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
morluto/rea#15041 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信