Skip to content
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

fix(proto): DateTime/DateTime64 with '1970-01-01 00:00:00 UTC' #390

Merged
merged 1 commit into from
Mar 8, 2024

Conversation

YenchangChan
Copy link
Contributor

Summary

I use clickhouse-cli and clickhouse-go to execute the following SQL, but I get different results:

localhost :) select toDateTime('1970-01-01 00:00:00', 'UTC');

SELECT toDateTime('1970-01-01 00:00:00', 'UTC')

Query id: 1b43d122-7f5f-44fa-9f3b-4992bd305998

┌─toDateTime('1970-01-01 00:00:00', 'UTC')─┐
│                      1970-01-01 00:00:00 │
└──────────────────────────────────────────┘

1 row in set. Elapsed: 0.002 sec. 
func main() {
	conn, err := ConnectNative()
	if err != nil {
		panic((err))
	}

	query := "select toDateTime('1970-01-01 00:00:00', 'UTC')"
	var rows driver.Rows
	rows, _ = conn.Query(context.Background(), query)
	for rows.Next() {
		var ts time.Time
		err = rows.Scan(&ts)
		fmt.Println("ts", ts)
	}
}

func ConnectNative() (driver.Conn, error) {
	return clickhouse.Open(&clickhouse.Options{
		Addr: []string{"127.0.0.1:9000"},
	})
}

running result:

ts 0001-01-01 00:00:00 +0000 UTC

I noticed that the reason for the discrepancy in the above results is that ch-go will treat time.Time{} as time.Unix(0,0).UTC(), which is actually not the same in golang:

t1, _ := time.Parse("2006-01-02 15:04:05", "1970-01-01 00:00:00")
t2 := time.Time{}
t3, _ := time.Parse("2006-01-02 15:04:05", "1970-01-01 00:00:01")
fmt.Printf("isZero => t1: %v, t2: %v\n", t1.IsZero(), t2.IsZero())         // isZero => t1: false, t2: true
fmt.Printf("UnixTime => t1: %v, t2: %v, t3:%v\n", t1.Unix(), t2.Unix(), t3.Unix())   // UnixTime => t1: 0, t2: -62135596800, t3:1

The same issue in clickhouse-go: #1222

Checklist

Delete items not relevant to your PR:

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG
  • For significant changes, documentation in https://github.com/ClickHouse/clickhouse-docs was updated with further explanations or tutorials

@ernado ernado merged commit 470e9f1 into ClickHouse:main Mar 8, 2024
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants