mailru/go-clickhouse

Datatime value seems overflow?

Closed this issue · 1 comments

hi, I found that when the value of DateTime in the Clickhouse is large, for example, 2050-01-01 00:00:00, the value from the driver is wrong, which looks like an overflow problem.
The code to reproduce the problem:

package main

import (
	"database/sql"
	"github.com/ClickHouse/clickhouse-go"
	"log"
	"time"
)

func main() {
	connect, err := sql.Open("clickhouse", "tcp://localhost:9000?debug=true&user=default")
	if err != nil {
		log.Fatal(err)
	}
	if err := connect.Ping(); err != nil {
		log.Fatal(err)
	}

	_, err = connect.Exec(`
		CREATE TABLE IF NOT EXISTS example (
			country_code FixedString(2),
			os_id        UInt8,
			browser_id   UInt8,
			categories   Array(Int16),
			action_time  DateTime
		) engine=Memory
	`)

	if err != nil {
		log.Fatal(err)
	}

	tx, err := connect.Begin()
	if err != nil {
		log.Fatal(err)
	}
	stmt, err := tx.Prepare(`
		INSERT INTO example (
			country_code,
			os_id,
			browser_id,
			categories,
			action_time
		) VALUES (
			?, ?, ?, ?, ?, ?
		)`)

	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < 10; i++ {
		if _, err := stmt.Exec(
			"RU",
			10+i,
			100+i,
			clickhouse.Array([]int16{1, 2, 3}),
			time.Now().Add(time.Hour * 24 * 365 * 20),   //almost 20 years later
		); err != nil {
			log.Fatal(err)
		}
	}

	if err := tx.Commit(); err != nil {
		log.Fatal(err)
	}

	rows, err := connect.Query(`
		SELECT
			country_code,
			os_id,
			browser_id,
			categories,
			action_time
		FROM
			example`)

	if err != nil {
		log.Fatal(err)
	}

	for rows.Next() {
		var (
			country               string
			os, browser           uint8
			categories            []int16
			actionTime time.Time
		)
		if err := rows.Scan(
			&country,
			&os,
			&browser,
			&categories,
			&actionTime,
		); err != nil {
			log.Fatal(err)
		}
		log.Printf("country: %s, os: %d, browser: %d, categories: %v, action_day: %s",
			country, os, browser, categories, actionTime,
		)
	}
}

and the output:

2021/09/16 11:31:26 country: RU, os: 10, browser: 100, categories: [1 2 3], action_day: 1905-08-06 03:03:10 +0000 UTC
2021/09/16 11:31:26 country: RU, os: 11, browser: 101, categories: [1 2 3], action_day: 1905-08-06 03:03:10 +0000 UTC
2021/09/16 11:31:26 country: RU, os: 12, browser: 102, categories: [1 2 3], action_day: 1905-08-06 03:03:10 +0000 UTC
2021/09/16 11:31:26 country: RU, os: 13, browser: 103, categories: [1 2 3], action_day: 1905-08-06 03:03:10 +0000 UTC
2021/09/16 11:31:26 country: RU, os: 14, browser: 104, categories: [1 2 3], action_day: 1905-08-06 03:03:10 +0000 UTC
2021/09/16 11:31:26 country: RU, os: 15, browser: 105, categories: [1 2 3], action_day: 1905-08-06 03:03:10 +0000 UTC
2021/09/16 11:31:26 country: RU, os: 16, browser: 106, categories: [1 2 3], action_day: 1905-08-06 03:03:10 +0000 UTC
2021/09/16 11:31:26 country: RU, os: 17, browser: 107, categories: [1 2 3], action_day: 1905-08-06 03:03:10 +0000 UTC
2021/09/16 11:31:26 country: RU, os: 18, browser: 108, categories: [1 2 3], action_day: 1905-08-06 03:03:10 +0000 UTC
2021/09/16 11:31:26 country: RU, os: 19, browser: 109, categories: [1 2 3], action_day: 1905-08-06 03:03:10 +0000 UTC

You are using https://github.com/Clickhouse/Clickhouse-go this library uses the HTTP Clickhouse interface and does not have this problem.
The other library has this problem as it uses time.Unix for datetime construction