Skip to content

Commit

Permalink
Merge branch 'master' into support-int64
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 24, 2024
2 parents 6d886c8 + 0ca75c0 commit 7168fd7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions clients/bigquery/storagewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func encodePacked32TimeSeconds(t time.Time) int32 {
return bitFieldTimeSeconds
}

// // This is a reimplementation of https://github.com/googleapis/java-bigquerystorage/blob/f79acb5cfdd12253bca1c41551c478400120d2f9/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/CivilTimeEncoder.java#L187
// This is a reimplementation of https://github.com/googleapis/java-bigquerystorage/blob/f79acb5cfdd12253bca1c41551c478400120d2f9/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/CivilTimeEncoder.java#L187
func encodePacked64DatetimeSeconds(dateTime time.Time) int64 {
var bitFieldDatetimeSeconds int64
bitFieldDatetimeSeconds |= int64(dateTime.Year() << yearShift)
Expand All @@ -127,9 +127,9 @@ func encodePacked64DatetimeSeconds(dateTime time.Time) int64 {
return bitFieldDatetimeSeconds
}

// // This is a reimplementation of https://github.com/googleapis/java-bigquerystorage/blob/f79acb5cfdd12253bca1c41551c478400120d2f9/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/CivilTimeEncoder.java#L248
// This is a reimplementation of https://github.com/googleapis/java-bigquerystorage/blob/f79acb5cfdd12253bca1c41551c478400120d2f9/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/CivilTimeEncoder.java#L248
func encodePacked64DatetimeMicros(dateTime time.Time) int64 {
return encodePacked64DatetimeSeconds(dateTime)<<microLength | int64(dateTime.Nanosecond()/1_000)
return encodePacked64DatetimeSeconds(dateTime)<<microLength | int64(dateTime.Nanosecond()/1000)
}

func rowToMessage(row map[string]any, columns []columns.Column, messageDescriptor protoreflect.MessageDescriptor) (*dynamicpb.Message, error) {
Expand Down
44 changes: 22 additions & 22 deletions clients/bigquery/storagewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,45 +112,45 @@ func TestEncodePacked32TimeSeconds(t *testing.T) {
}

func TestEncodePacked64DatetimeSeconds(t *testing.T) {
_ts := time.Date(2024, 10, 24, 13, 1, 2, 3000000, time.UTC)
_expected := 2024<<26 + 10<<22 + 24<<17 + int64(encodePacked32TimeSeconds(_ts))
ts := time.Date(2024, 10, 24, 13, 1, 2, 3000000, time.UTC)
expected := 2024<<26 + 10<<22 + 24<<17 + int64(encodePacked32TimeSeconds(ts))

// Time
assert.Equal(t, _expected, encodePacked64DatetimeSeconds(_ts))
assert.Equal(t, _expected+1<<0, encodePacked64DatetimeSeconds(_ts.Add(time.Duration(1)*time.Second)))
assert.Equal(t, _expected+1<<6, encodePacked64DatetimeSeconds(_ts.Add(time.Duration(1)*time.Minute)))
assert.Equal(t, _expected+1<<12, encodePacked64DatetimeSeconds(_ts.Add(time.Duration(1)*time.Hour)))
assert.Equal(t, _expected+1<<12+1<<0, encodePacked64DatetimeSeconds(_ts.Add(time.Duration(1)*time.Hour+time.Duration(1)*time.Second)))
assert.Equal(t, expected, encodePacked64DatetimeSeconds(ts))
assert.Equal(t, expected+1<<0, encodePacked64DatetimeSeconds(ts.Add(time.Duration(1)*time.Second)))
assert.Equal(t, expected+1<<6, encodePacked64DatetimeSeconds(ts.Add(time.Duration(1)*time.Minute)))
assert.Equal(t, expected+1<<12, encodePacked64DatetimeSeconds(ts.Add(time.Duration(1)*time.Hour)))
assert.Equal(t, expected+1<<12+1<<0, encodePacked64DatetimeSeconds(ts.Add(time.Duration(1)*time.Hour+time.Duration(1)*time.Second)))

// Day
assert.Equal(t, _expected+1<<17, encodePacked64DatetimeSeconds(_ts.Add(time.Duration(24)*time.Hour)))
assert.Equal(t, expected+1<<17, encodePacked64DatetimeSeconds(ts.Add(time.Duration(24)*time.Hour)))
// Month
assert.Equal(t, _expected+1<<22, encodePacked64DatetimeSeconds(_ts.AddDate(0, 1, 0)))
assert.Equal(t, expected+1<<22, encodePacked64DatetimeSeconds(ts.AddDate(0, 1, 0)))
// Year
assert.Equal(t, _expected+1<<26, encodePacked64DatetimeSeconds(_ts.AddDate(1, 0, 0)))
assert.Equal(t, expected+1<<26, encodePacked64DatetimeSeconds(ts.AddDate(1, 0, 0)))
// Month and year
assert.Equal(t, _expected+1<<22+1<<26, encodePacked64DatetimeSeconds(_ts.AddDate(1, 1, 0)))
assert.Equal(t, expected+1<<22+1<<26, encodePacked64DatetimeSeconds(ts.AddDate(1, 1, 0)))
}

func TestEncodePacked64DatetimeMicros(t *testing.T) {
_ts := time.Date(2024, 10, 24, 13, 1, 2, 123456789, time.UTC)
_expected := encodePacked64DatetimeSeconds(_ts)<<20 | int64(_ts.Nanosecond()/1000)
ts := time.Date(2024, 10, 24, 13, 1, 2, 123456789, time.UTC)
expected := encodePacked64DatetimeSeconds(ts)<<20 | int64(ts.Nanosecond()/1000)

// Time
assert.Equal(t, _expected, encodePacked64DatetimeMicros(_ts))
assert.Equal(t, _expected+1<<(0+20), encodePacked64DatetimeMicros(_ts.Add(time.Duration(1)*time.Second)))
assert.Equal(t, _expected+1<<(6+20), encodePacked64DatetimeMicros(_ts.Add(time.Duration(1)*time.Minute)))
assert.Equal(t, _expected+1<<(12+20), encodePacked64DatetimeMicros(_ts.Add(time.Duration(1)*time.Hour)))
assert.Equal(t, _expected+1<<(12+20)+1<<(0+20), encodePacked64DatetimeMicros(_ts.Add(time.Duration(1)*time.Hour+time.Duration(1)*time.Second)))
assert.Equal(t, expected, encodePacked64DatetimeMicros(ts))
assert.Equal(t, expected+1<<(0+20), encodePacked64DatetimeMicros(ts.Add(time.Duration(1)*time.Second)))
assert.Equal(t, expected+1<<(6+20), encodePacked64DatetimeMicros(ts.Add(time.Duration(1)*time.Minute)))
assert.Equal(t, expected+1<<(12+20), encodePacked64DatetimeMicros(ts.Add(time.Duration(1)*time.Hour)))
assert.Equal(t, expected+1<<(12+20)+1<<(0+20), encodePacked64DatetimeMicros(ts.Add(time.Duration(1)*time.Hour+time.Duration(1)*time.Second)))

// Day
assert.Equal(t, _expected+1<<(17+20), encodePacked64DatetimeMicros(_ts.Add(time.Duration(24)*time.Hour)))
assert.Equal(t, expected+1<<(17+20), encodePacked64DatetimeMicros(ts.Add(time.Duration(24)*time.Hour)))
// Month
assert.Equal(t, _expected+1<<(22+20), encodePacked64DatetimeMicros(_ts.AddDate(0, 1, 0)))
assert.Equal(t, expected+1<<(22+20), encodePacked64DatetimeMicros(ts.AddDate(0, 1, 0)))
// Year
assert.Equal(t, _expected+1<<(26+20), encodePacked64DatetimeMicros(_ts.AddDate(1, 0, 0)))
assert.Equal(t, expected+1<<(26+20), encodePacked64DatetimeMicros(ts.AddDate(1, 0, 0)))
// Month and year
assert.Equal(t, _expected+1<<(26+20)+1<<(22+20), encodePacked64DatetimeMicros(_ts.AddDate(1, 1, 0)))
assert.Equal(t, expected+1<<(26+20)+1<<(22+20), encodePacked64DatetimeMicros(ts.AddDate(1, 1, 0)))
}

func TestRowToMessage(t *testing.T) {
Expand Down

0 comments on commit 7168fd7

Please sign in to comment.