-
Notifications
You must be signed in to change notification settings - Fork 583
Inserting timestamps loses precision down to second #1545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
For completeness, I have tried to see if there was the same issue when using parameters or batch insert but both were good: func TestAsyncInsertPlaceholderLosesPrecision(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_placeholder_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_placeholder_loses_precision")
}()
// Insert data with multiple values
now := time.Now().UTC().Truncate(time.Microsecond)
ctx = clickhouse.Context(ctx, clickhouse.WithParameters(clickhouse.Parameters{
"id": "1",
"created_at": now.Format("2006-01-02 15:04:05.999999"),
}))
err = conn.AsyncInsert(ctx, `
INSERT INTO test_async_insert_placeholder_loses_precision (id, created_at)
VALUES ({id: Int32}, {created_at: DateTime64(6)})
`, true)
require.NoError(t, err)
// Query the data back
rows, err := conn.Query(ctx, "SELECT id, created_at FROM test_async_insert_placeholder_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)
}
func TestBatchInsertLosesPrecision(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_batch_insert_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_batch_insert_loses_precision")
}()
// Insert data with multiple values
now := time.Now().UTC().Truncate(time.Microsecond)
batch, err := conn.PrepareBatch(ctx, `INSERT INTO test_batch_insert_loses_precision (id, created_at)`)
require.NoError(t, err)
err = batch.Append(int32(1), now)
require.NoError(t, err)
err = batch.Send()
require.NoError(t, err)
// Query the data back
rows, err := conn.Query(ctx, "SELECT id, created_at FROM test_batch_insert_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)
} |
Try this:
|
Observed
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
Error log
Details
Environment
clickhouse-go
version: masterCREATE TABLE
statements for tables involved: provided in the testcaseThe text was updated successfully, but these errors were encountered: