-
Notifications
You must be signed in to change notification settings - Fork 562
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
batch.AppendStruct() converting int64 to DateTime is unsupported #803
Comments
This should work. I'll patch if it doesn't. Thanks for reporting @Meppo |
Note "INSERT INTO benchmark VALUES" is not equivalent to the AppendStruct interface. The latter uses a batch and sends blocks for high performance - the former simply sends a query (and shouldn't be used for anything other than occasional small inserts). The data is untyped in the latter case - its just a query. Append is the equivalent to AppendStruct. We generally don't support inserting int64 into Date - we do support int64 to DateTime64. Its a reasonable thing to support but note the range bounds of DateTime ([1970-01-01 00:00:00, 2106-02-07 06:28:15]) - if we assume you are specifying a UNIX timestamp in seconds, you can easily go out of bounds. |
Will be closed by #807 |
Thanks the reply . it's working when use the 1. data source(json)# Col3 : unix timestamp seconds
{
"Col1": 1,
"Col2": "Hello world",
"Col3": 1667343639
} 2. receive json and unmarkshaltype row struct {
Col1 uint64
Col2 string
Col3 int64 // unix timestamp seconds
} 3. write data to clickhouse // append success, but time result is not expected
err = batch.AppendStruct(row) examplepackage main
import (
"context"
"encoding/json"
"log"
"github.com/ClickHouse/clickhouse-go/v2"
)
const ddl = `
CREATE TABLE benchmark (
Col1 UInt64
, Col2 String
, Col3 DateTime64
) Engine Memory
`
//Col3 : unix timestamp seconds
const data = `{
"Col1": 1,
"Col2": "Hello world",
"Col3": 1667343639
}`
type row struct {
Col1 uint64
Col2 string
Col3 int64 // unix timestamp seconds
}
// batch.AppendStruct convert int64 -> datetime64 success, but Col3 result is not expected
func appendstruct(conn clickhouse.Conn, r *row) error {
batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO benchmark")
if err != nil {
return err
}
err = batch.AppendStruct(r)
if err != nil {
return err
}
return batch.Send()
}
func main() {
var (
ctx = context.Background()
conn, err = clickhouse.Open(&clickhouse.Options{
Addr: []string{"localhost:9000"},
Auth: clickhouse.Auth{
Database: "default",
Username: "default",
Password: "",
},
})
)
defer conn.Close()
if err != nil {
log.Fatal(err)
}
if err := conn.Exec(ctx, "DROP TABLE IF EXISTS benchmark"); err != nil {
log.Fatal(err)
}
if err := conn.Exec(ctx, ddl); err != nil {
log.Fatal(err)
}
var r row
if err := json.Unmarshal([]byte(data), &r); err != nil {
log.Fatal(err)
}
if err := appendstruct(conn, &r); err != nil {
log.Fatal(err)
}
} result:) select * from benchmark;
SELECT *
FROM benchmark
Query id: acfb0c2c-90c4-4039-95da-148578972e97
┌─Col1─┬─Col2────────┬────────────────────Col3─┐
│ 1 │ Hello world │ 1970-01-20 15:09:03.639 │
└──────┴─────────────┴─────────────────────────┘ solutionfunc appendstruct(conn clickhouse.Conn, r *row) error {
...
// change sec => ms
r.Col3 = r.Col3*1000
err = batch.AppendStruct(r)
...
} it's a inconvenien to change Col3 |
I added a pr for support for int64 for datetime.it assumes secs. |
conn.Exec("insert into ...)
support converting int64 to DateTime, butbatch.AppendStruct()
not support ...JSONEachRow
also support convertingint
toDateTime
environment
example code
The text was updated successfully, but these errors were encountered: