Skip to content

Millisecond digits reversed when inserting DateTime64 using setLong(..) in batch #612

Closed

Description

@PHameete

When using setLong on a PreparedStatement to insert DateTime64 the millisecond digits are reversed when inserting in batch.

When inserting one row at a time this problem (i.e. no addBatch() and executeBatch() but simply executeUpdate()).

Code example:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import ru.yandex.clickhouse.ClickHouseDataSource;

public class Example {

    private static final String DROP_TABLE_SQL = """
            DROP TABLE IF EXISTS example
        """;

    private static final String CREATE_TABLE_SQL = """
            CREATE TABLE IF NOT EXISTS example (
                id UUID,
                date DateTime64(3)
            )
            ENGINE = MergeTree()
            ORDER BY (id, date)
        """;

    private static final String INSERT_SQL = """
            INSERT INTO example (id, date)
            VALUES (?, ?)
        """;

    private static final String SELECT_SQL = """
            SELECT id, date
            FROM example
            WHERE id = ?
        """;

    public static void main(String[] args) throws SQLException {
        ClickHouseDataSource dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123");
        Connection conn = dataSource.getConnection();
        PreparedStatement dropTableStmt = conn.prepareStatement(DROP_TABLE_SQL);
        dropTableStmt.executeUpdate();
        dropTableStmt.close();

        PreparedStatement createTableStmt = conn.prepareStatement(CREATE_TABLE_SQL);
        createTableStmt.executeUpdate();
        createTableStmt.close();

        long date = 1617359745321L;
        UUID id = UUID.randomUUID();
        PreparedStatement insertStmt = conn.prepareStatement(INSERT_SQL);
        insertStmt.setObject(1, id);
        insertStmt.setLong(2, date);
        insertStmt.addBatch();
        insertStmt.executeBatch();
        insertStmt.close();

        PreparedStatement selectStmt = conn.prepareStatement(SELECT_SQL);
        selectStmt.setObject(1, id);
        ResultSet rs = selectStmt.executeQuery();

        while (rs.next()) {
            long receivedDate = rs.getTimestamp(2).getTime();
            if (date != receivedDate) {
                System.out.println("Dates not equal. Original date: %d Selected date: %d".formatted(date, receivedDate));
            } else {
                System.out.println("Dates equal. Original date: %d Selected date: %d".formatted(date, receivedDate));
            }
        }

    }

}

This example will print: Dates not equal. Original date: 1617359745321 Selected date: 1617359745123 where the millisecond digits are reversed.

Replacing:

        insertStmt.addBatch();
        insertStmt.executeBatch();

with

        insertStmt.executeUpdate();

will yield: Dates equal. Original date: 1617359745321 Selected date: 1617359745321

Activity

  1. changed the title [-]Millisecond digits reversed when inserting DateTime64 as Long in batch[/-] [+]Millisecond digits reversed when inserting DateTime64 using setLong(..) in batch[/+] on Apr 2, 2021
  2. zhicwu commented on Apr 6, 2021

    @zhicwu
    Contributor

    Thanks again for reporting the issue. This might also related to 21.3. Switching to RowBinary format will fix this issue too.

  3. self-assigned this
    on Apr 6, 2021
  4. added this to the 0.3.1 release milestone on Apr 6, 2021
  5. faisalhasnain commented on Jun 15, 2021

    @faisalhasnain

    Same is the case for micro-seconds precision with DateTime64(6)

  6. volhahedranovich commented on Jul 28, 2021

    @volhahedranovich

    It should be ok, if you insert float 1617359745321/1000 instead of integer 1617359745321.

  7. added
    legacy
    on Oct 6, 2021
  8. zhicwu commented on Dec 29, 2021

    @zhicwu
    Contributor

    Now it should work.

    try (ClickHouseConnection conn = newConnection(new Properties());
            ClickHouseStatement s = conn.createStatement();) {
        s.execute("drop table if exists test_issue_612;"
                + "CREATE TABLE IF NOT EXISTS test_issue_612 (id UUID, date DateTime64(3)) ENGINE = MergeTree() ORDER BY (id, date)");
        UUID id = UUID.randomUUID();
        long value = 1617359745321L;
        try (PreparedStatement ps = conn.prepareStatement("insert into test_issue_612 values(?,?)")) {
            ps.setLong(2, value);
            ps.setObject(1, id);
            // FIXME has issue with ps.execute()
            ps.addBatch();
            ps.executeBatch();
        }
    
        try (PreparedStatement ps = conn.prepareStatement("select * from test_issue_612 where id = ?")) {
            ps.setObject(1, id);
            ResultSet rs = ps.executeQuery();
            Assert.assertTrue(rs.next());
            Assert.assertEquals(rs.getObject(1), id);
            Assert.assertEquals(rs.getObject(2), LocalDateTime.of(2021, 4, 2, 10, 35, 45, 321000000));
            Assert.assertFalse(rs.next());
        }
    }

    Update: please refer to integration test(with some correction) at here.

  9. added
    jdbc
    data-types
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugdata-typesjdbclegacy

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions