Skip to content

Inserting timestamps loses precision down to second #1545

Open
@txomon

Description

@txomon

Observed

  1. Created a table with DateTime64(6) type
  2. Inserted timestamps with nanosecond precision using InsertAsync()
  3. Queries returned timestamps with second precision

Expected behaviour

When inserting timestamps, using Insert into testtable values (?,?) format the timestamps lose precision.

This fact is not documented nor explicit, causing likely bugs.

Code example

import (
	"context"
	"testing"
	"time"

	"github.com/ClickHouse/clickhouse-go/v2"
	"github.com/stretchr/testify/require"
)

func TestAsyncInsertQuestionmarkLosesPrecision(t *testing.T) {
	t.Parallel()
	ctx := context.Background()

	// Parse DSN and create connection
	opt, err := clickhouse.ParseDSN("clickhouse://user:password@clickhouse:9000/default?secure=false&skip_verify=true")
	require.NoError(t, err)

	conn, err := clickhouse.Open(opt)
	require.NoError(t, err)
	defer conn.Close()

	// Create test table
	err = conn.Exec(ctx, `
		CREATE TABLE IF NOT EXISTS test_async_insert_questionmark_loses_precision (
			id Int32,
			created_at DateTime64(6),
		) ENGINE = MergeTree()
		ORDER BY id
	`)
	require.NoError(t, err)

	// Clean up table after test
	defer func() {
		_ = conn.Exec(ctx, "DROP TABLE IF EXISTS test_async_insert_questionmark_loses_precision")
	}()

	// Insert data with multiple values
	now := time.Now().UTC().Truncate(time.Microsecond)
	err = conn.AsyncInsert(ctx, `
		INSERT INTO test_async_insert_questionmark_loses_precision (id, created_at)
		VALUES (?, ?)
	`, true, int32(1), now)
	require.NoError(t, err)

	// Query the data back
	rows, err := conn.Query(ctx, "SELECT id, created_at FROM test_async_insert_questionmark_loses_precision WHERE id = 1")
	require.NoError(t, err)
	defer rows.Close()

	// Read the results
	var (
		id        int32
		createdAt time.Time
	)

	require.True(t, rows.Next())
	err = rows.Scan(&id, &createdAt)
	require.NoError(t, err)

	// Verify the data matches what we inserted
	require.Equal(t, int32(1), id)
	require.Equal(t, now, createdAt)
}

Error log


Expected :time.Date(2025, time.April, 21, 11, 43, 4, 352652000, time.UTC)
Actual   :time.Date(2025, time.April, 21, 11, 43, 4, 0, time.UTC)

Details

Environment

  • clickhouse-go version: master
  • Interface: ClickHouse API
  • Go version: 1.24
  • Operating system: linux
  • ClickHouse version: doesn't matter
  • Is it a ClickHouse Cloud? no
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: provided in the testcase
  • Sample data for all these tables, use clickhouse-obfuscator if necessary

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions