Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 24, 2024
1 parent f6a49aa commit 7723e3b
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions clients/bigquery/storagewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,39 +92,48 @@ func columnsToMessageDescriptor(cols []columns.Column) (*protoreflect.MessageDes
return &messageDescriptor, nil
}

const (
NANO_SHIFT = 0
MICRO_SHIFT = 0
SECOND_SHIFT = 0
MINUTE_SHIFT = 6
HOUR_SHIFT = 12
DAY_SHIFT = 17
MONTH_SHIFT = 22
YEAR_SHIFT = 26
NANO_LENGTH = 30
MICRO_LENGTH = 20
)

// 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#L143
// See https://cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.CivilTimeEncoder
// And https://cloud.google.com/pubsub/docs/bigquery#date_time_int
func encodePacked64TimeMicros(value time.Time) int64 {
var result = int64(value.Nanosecond() / 1000)
result |= int64(value.Second()) << 20
result |= int64(value.Minute()) << 26
result |= int64(value.Hour()) << 32
return result
return int64(encodePacked32TimeSeconds(value))<<MICRO_LENGTH | int64(value.Nanosecond()/1000)
}

// 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#L92
func encodePacked32TimeSeconds(t time.Time) int {
bitFieldTimeSeconds := 0x0
bitFieldTimeSeconds |= t.Hour() << 12
bitFieldTimeSeconds |= t.Minute() << 6
bitFieldTimeSeconds |= t.Second() << 0
var bitFieldTimeSeconds int
bitFieldTimeSeconds |= t.Hour() << HOUR_SHIFT
bitFieldTimeSeconds |= t.Minute() << MINUTE_SHIFT
bitFieldTimeSeconds |= t.Second() << SECOND_SHIFT
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()) << 26
bitFieldDatetimeSeconds |= int64(dateTime.Month()) << 22
bitFieldDatetimeSeconds |= int64(dateTime.Day()) << 17
bitFieldDatetimeSeconds |= int64(encodePacked32TimeSeconds(dateTime))
bitFieldDatetimeSeconds |= int64(dateTime.Year() << YEAR_SHIFT)
bitFieldDatetimeSeconds |= int64(dateTime.Month() << MONTH_SHIFT)
bitFieldDatetimeSeconds |= int64(dateTime.Day() << DAY_SHIFT)
bitFieldDatetimeSeconds |= int64(encodePacked32TimeSeconds(dateTime.UTC()))
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) << 20) | (dateTime.UnixMicro())
return (encodePacked64DatetimeSeconds(dateTime) << MICRO_LENGTH) | int64(dateTime.Nanosecond()/1_000)
}

func rowToMessage(row map[string]any, columns []columns.Column, messageDescriptor protoreflect.MessageDescriptor) (*dynamicpb.Message, error) {
Expand Down

0 comments on commit 7723e3b

Please sign in to comment.