[jdbc-v1] asTempTable() external tables are loaded as RowBinary, whatever their format

未关闭 适合新手
#3,444 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
70/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
clickhouse, java
领域
backend, database

调研方向

阅读 ClickHouseStatementImpl.java 第311行附近临时外部表内容写入的位置,并将其与 ClickHouseExternalTable 的格式设置以及 ClickHouseRequest.java 第95行附近的请求默认值进行比较。在外部表测试中,除现有的 RowBinary 用例外,还要为 TabSeparated、CSV 和 JSONEachRow 添加覆盖。完成标准是临时表能正确加载每种内容格式,与不使用 asTempTable() 时的行为一致。

由索引模型根据 Issue 内容生成。

描述

bug jdbc legacy queries

Description

With the v1 JDBC driver, an external table that is built with asTempTable() and bound to {tt 'name'} is loaded as RowBinary, whatever the format of the table. The default format of ClickHouseExternalTable is TabSeparated, so a table with the default format fails. The same table without asTempTable() works. TabSeparated, CSV and JSONEachRow content all show the problem; only RowBinary content works (the only format that testQueryWithExternalTable uses with asTempTable()).

The result depends on the content:

  • content that does not parse as RowBinary: the query fails with CANNOT_READ_ALL_DATA;
  • content that parses as RowBinary (for example UInt8 or UInt32 columns in TSV or CSV): the query returns wrong values, with no error.

Root cause: ClickHouseStatementImpl.java:311 writes the content with request.write().table(tableName).data(t.getContent()) and does not set t.getFormat(), so the INSERT gets the default input format of the request, RowBinary (ClickHouseRequest.java:95). The query log shows the statement that the driver sends: INSERT INTO `ext` FORMAT RowBinary. The fix is one format(...) call on that write.

Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related:

  • #3438 (open): another external-table defect in the same asTempTable() code (getStructure()).
Steps to reproduce
  1. Use the v1 driver (-Dclickhouse.jdbc.v1=true).
  2. Bind ClickHouseExternalTable.builder().name("ext").columns("v UInt8").content(<"1\n2\n">).asTempTable().build() to SELECT * FROM {tt 'ext'}, with no format(...) (TabSeparated).
  3. Read the result: [49, 10, 50, 10]. Without asTempTable(): [1, 2].
Error Log or Exception StackTrace
ext  v UInt64 -> [42]
ext  v UInt8 -> [1, 2]
temp v UInt64 -> Code: 33. DB::Exception: Cannot read all data. Bytes read: 3. Bytes expected: 8: (at row 1)
temp v UInt8 -> [49, 10, 50, 10]
Expected Behaviour

The temporary table gets the rows of the content in the format of the table, as without asTempTable(). The server, for the same bytes in a temporary table ext (v UInt8):

$ printf '1\n2\n' | curl --data-binary @- '…/?session_id=s&query=INSERT+INTO+ext+FORMAT+TSV'        # SELECT groupArray(v) FROM ext -> [1,2]
$ printf '1\n2\n' | curl --data-binary @- '…/?session_id=s&query=INSERT+INTO+ext+FORMAT+RowBinary'  # SELECT groupArray(v) FROM ext -> [49,10,50,10]
Code Example
Reproduction program (Java, 35 lines)
import com.clickhouse.data.ClickHouseExternalTable;
import java.io.ByteArrayInputStream;
import java.sql.*;
import java.util.*;

public class TempTableFormat {
    static void run(Connection conn, String column, String tsv, boolean temp) {
        ClickHouseExternalTable.Builder b = ClickHouseExternalTable.builder().name("ext")
                .columns(column).content(new ByteArrayInputStream(tsv.getBytes())); // format: TabSeparated (default)
        if (temp) b.asTempTable();
        try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM {tt 'ext'}")) {
            ps.setObject(1, b.build());
            ResultSet rs = ps.executeQuery();
            List<String> rows = new ArrayList<>();
            while (rs.next()) rows.add(rs.getString(1));
            System.out.println((temp ? "temp " : "ext  ") + column + " -> " + rows);
        } catch (SQLException e) {
            System.out.println((temp ? "temp " : "ext  ") + column + " -> " + e.getMessage().split("\n")[0]);
        }
    }

    public static void main(String[] args) throws Exception {
        System.setProperty("clickhouse.jdbc.v1", "true");
        Properties p = new Properties();
        p.setProperty("user", "default");
        p.setProperty("password", System.getenv("CLICKHOUSE_PASSWORD"));
        p.setProperty("compress", "false"); // see #3107
        try (Connection conn = DriverManager.getConnection("jdbc:clickhouse://clickhouse:8123/default", p)) {
            for (boolean temp : new boolean[] {false, true}) {
                run(conn, "v UInt64", "42\n", temp);
                run(conn, "v UInt8", "1\n2\n", temp);
            }
        }
    }
}
Configuration
Client Configuration
System.setProperty("clickhouse.jdbc.v1", "true");
// JDBC properties: compress=false (#3107)
Environment
  • Cloud
  • Client version: main at 9934d2e88 (0.12.0-rc1-SNAPSHOT), v1 driver
  • Language version: OpenJDK 17.0.21
  • OS: Linux (Docker)
ClickHouse Server
  • ClickHouse Server version: 26.11.2.4
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: none (the driver creates the temporary table)
  • Sample data for all these tables: in the program

Classification: impact: Data integrity · area: Queries (external temporary tables)


Found by automated analysis (Polyglot AI) while working on ClickHouse/clickhouse-go#2170. Verified against a live server.

主要语言
Java
星标
1.6k
派生
657
平均合并
2 天 6 小时
30 天内合并 PR
36

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ClickHouse/clickhouse-java 的其他 Issue

查看 ClickHouse/clickhouse-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。