Skip to content

Commit

Permalink
WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 22, 2024
1 parent bd5ab88 commit fb6b1de
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 56 deletions.
10 changes: 5 additions & 5 deletions clients/bigquery/storagewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,22 @@ func rowToMessage(row map[string]any, columns []columns.Column, messageDescripto
return nil, err
}

extTime, err := ext.ParseFromInterface(value, column.KindDetails.ExtendedTimeDetails.Type)
_time, err := ext.ParseFromInterface(value, column.KindDetails.ExtendedTimeDetails.Type)
if err != nil {
return nil, fmt.Errorf("failed to cast value as time.Time, value: %v, err: %w", value, err)
}

switch column.KindDetails.ExtendedTimeDetails.Type {
case ext.TimeKindType:
message.Set(field, protoreflect.ValueOfInt64(encodePacked64TimeMicros(extTime.GetTime())))
message.Set(field, protoreflect.ValueOfInt64(encodePacked64TimeMicros(_time)))
case ext.DateKindType:
daysSinceEpoch := extTime.GetTime().Unix() / (60 * 60 * 24)
daysSinceEpoch := _time.Unix() / (60 * 60 * 24)
message.Set(field, protoreflect.ValueOfInt32(int32(daysSinceEpoch)))
case ext.TimestampTZKindType:
if err := timestamppb.New(extTime.GetTime()).CheckValid(); err != nil {
if err := timestamppb.New(_time).CheckValid(); err != nil {
return nil, err
}
message.Set(field, protoreflect.ValueOfInt64(extTime.GetTime().UnixMicro()))
message.Set(field, protoreflect.ValueOfInt64(_time.UnixMicro()))
default:
return nil, fmt.Errorf("unsupported extended time details: %q", column.KindDetails.ExtendedTimeDetails.Type)
}
Expand Down
4 changes: 2 additions & 2 deletions clients/mssql/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func parseValue(colVal any, colKind columns.Column) (any, error) {
return "", err
}

extTime, err := ext.ParseFromInterface(colVal, colKind.KindDetails.ExtendedTimeDetails.Type)
_time, err := ext.ParseFromInterface(colVal, colKind.KindDetails.ExtendedTimeDetails.Type)
if err != nil {
return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: %v, err: %w", colVal, err)
}

return extTime.GetTime(), nil
return _time, nil
case typing.String.Kind:
isArray := reflect.ValueOf(colVal).Kind() == reflect.Slice
_, isMap := colVal.(map[string]any)
Expand Down
6 changes: 3 additions & 3 deletions clients/shared/default_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ func DefaultValue(column columns.Column, dialect sql.Dialect) (any, error) {
return nil, err
}

extTime, err := ext.ParseFromInterface(column.DefaultValue(), column.KindDetails.ExtendedTimeDetails.Type)
_time, err := ext.ParseFromInterface(column.DefaultValue(), column.KindDetails.ExtendedTimeDetails.Type)
if err != nil {
return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: %v, err: %w", column.DefaultValue(), err)
}

switch column.KindDetails.ExtendedTimeDetails.Type {
case ext.TimeKindType:
return sql.QuoteLiteral(extTime.String(ext.PostgresTimeFormatNoTZ)), nil
return sql.QuoteLiteral(_time.Format(ext.PostgresTimeFormatNoTZ)), nil
default:
return sql.QuoteLiteral(extTime.String(column.KindDetails.ExtendedTimeDetails.Format)), nil
return sql.QuoteLiteral(_time.Format(column.KindDetails.ExtendedTimeDetails.Format)), nil
}
case typing.EDecimal.Kind:
if column.KindDetails.ExtendedDecimalDetails.Scale() == 0 {
Expand Down
4 changes: 2 additions & 2 deletions lib/optimization/table_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ func (t *TableData) DistinctDates(colName string) ([]string, error) {
return nil, fmt.Errorf("col: %v does not exist on row: %v", colName, row)
}

extTime, err := ext.ParseFromInterface(val, ext.DateKindType)
_time, err := ext.ParseFromInterface(val, ext.DateKindType)
if err != nil {
return nil, fmt.Errorf("col: %v is not a time column, value: %v, err: %w", colName, val, err)
}

retMap[extTime.String(ext.PostgresDateFormat)] = true
retMap[_time.Format(ext.PostgresDateFormat)] = true
}

var distinctDates []string
Expand Down
6 changes: 3 additions & 3 deletions lib/parquetutil/parse_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ func ParseValue(colVal any, colKind columns.Column) (any, error) {
return "", err
}

extTime, err := ext.ParseFromInterface(colVal, colKind.KindDetails.ExtendedTimeDetails.Type)
_time, err := ext.ParseFromInterface(colVal, colKind.KindDetails.ExtendedTimeDetails.Type)
if err != nil {
return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: %v, err: %w", colVal, err)
}

if colKind.KindDetails.ExtendedTimeDetails.Type == ext.DateKindType || colKind.KindDetails.ExtendedTimeDetails.Type == ext.TimeKindType {
return extTime.String(colKind.KindDetails.ExtendedTimeDetails.Format), nil
return _time.Format(colKind.KindDetails.ExtendedTimeDetails.Format), nil
}

return extTime.GetTime().UnixMilli(), nil
return _time.UnixMilli(), nil
case typing.String.Kind:
return colVal, nil
case typing.Struct.Kind:
Expand Down
66 changes: 28 additions & 38 deletions lib/typing/ext/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,91 +20,81 @@ func ParseTimeExactMatch(layout, value string) (time.Time, error) {
return ts, nil
}

func ParseFromInterface(val any, kindType ExtendedTimeKindType) (*ExtendedTime, error) {
func ParseFromInterface(val any, kindType ExtendedTimeKindType) (time.Time, error) {
switch convertedVal := val.(type) {
case nil:
return nil, fmt.Errorf("val is nil")
case *ExtendedTime:
return time.Time{}, fmt.Errorf("val is nil")
case time.Time:
return convertedVal, nil
case *ExtendedTime:
return convertedVal.GetTime(), nil
case string:
extendedTime, err := ParseExtendedDateTime(convertedVal, kindType)
ts, err := ParseExtendedDateTime(convertedVal, kindType)
if err != nil {
return nil, fmt.Errorf("failed to parse colVal: %q, err: %w", val, err)
return time.Time{}, fmt.Errorf("failed to parse colVal: %q, err: %w", val, err)
}

return extendedTime, nil
return ts, nil
default:
return nil, fmt.Errorf("failed to parse colVal, expected type string or *ExtendedTime and got: %T", convertedVal)
return time.Time{}, fmt.Errorf("failed to parse colVal, expected type string or *ExtendedTime and got: %T", convertedVal)
}
}

func ParseExtendedDateTime(value string, kindType ExtendedTimeKindType) (*ExtendedTime, error) {
func ParseExtendedDateTime(value string, kindType ExtendedTimeKindType) (time.Time, error) {
switch kindType {
case TimestampTZKindType:
return parseDateTime(value)
case DateKindType:
// Try date first
if et, err := parseDate(value); err == nil {
return et, nil
if ts, err := parseDate(value); err == nil {
return ts, nil
}

// If that doesn't work, try timestamp
if et, err := parseDateTime(value); err == nil {
nestedKind, err := NewNestedKind(DateKindType, "")
if err != nil {
return nil, err
}

et.nestedKind = nestedKind
return et, nil
if ts, err := parseDateTime(value); err == nil {
return ts, nil
}
case TimeKindType:
// Try time first
if et, err := parseTime(value); err == nil {
return et, nil
if ts, err := parseTime(value); err == nil {
return ts, nil
}

// If that doesn't work, try timestamp
if et, err := parseDateTime(value); err == nil {
nestedKind, err := NewNestedKind(TimeKindType, "")
if err != nil {
return nil, err
}

et.nestedKind = nestedKind
return et, nil
if ts, err := parseDateTime(value); err == nil {
return ts, nil
}
}

return nil, fmt.Errorf("unsupported value: %q, kindType: %q", value, kindType)
return time.Time{}, fmt.Errorf("unsupported value: %q, kindType: %q", value, kindType)
}

func parseDateTime(value string) (*ExtendedTime, error) {
func parseDateTime(value string) (time.Time, error) {
for _, supportedDateTimeLayout := range supportedDateTimeLayouts {
if ts, err := ParseTimeExactMatch(supportedDateTimeLayout, value); err == nil {
return NewExtendedTime(ts, TimestampTZKindType, supportedDateTimeLayout), nil
return ts, nil
}
}

return nil, fmt.Errorf("unsupported value: %q", value)
return time.Time{}, fmt.Errorf("unsupported value: %q", value)
}

func parseDate(value string) (*ExtendedTime, error) {
func parseDate(value string) (time.Time, error) {
for _, supportedDateFormat := range supportedDateFormats {
if ts, err := ParseTimeExactMatch(supportedDateFormat, value); err == nil {
return NewExtendedTime(ts, DateKindType, supportedDateFormat), nil
return ts, nil
}
}

return nil, fmt.Errorf("unsupported value: %q", value)
return time.Time{}, fmt.Errorf("unsupported value: %q", value)
}

func parseTime(value string) (*ExtendedTime, error) {
func parseTime(value string) (time.Time, error) {
for _, supportedTimeFormat := range SupportedTimeFormats {
if ts, err := ParseTimeExactMatch(supportedTimeFormat, value); err == nil {
return NewExtendedTime(ts, TimeKindType, supportedTimeFormat), nil
return ts, nil
}
}

return nil, fmt.Errorf("unsupported value: %q", value)
return time.Time{}, fmt.Errorf("unsupported value: %q", value)
}
6 changes: 3 additions & 3 deletions lib/typing/values/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func ToString(colVal any, colKind typing.KindDetails) (string, error) {
return "", err
}

extTime, err := ext.ParseFromInterface(colVal, colKind.ExtendedTimeDetails.Type)
_time, err := ext.ParseFromInterface(colVal, colKind.ExtendedTimeDetails.Type)
if err != nil {
return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: %v, err: %w", colVal, err)
}

if colKind.ExtendedTimeDetails.Type == ext.TimeKindType {
return extTime.String(ext.PostgresTimeFormatNoTZ), nil
return _time.Format(ext.PostgresTimeFormatNoTZ), nil
}
return extTime.String(colKind.ExtendedTimeDetails.Format), nil
return _time.Format(colKind.ExtendedTimeDetails.Format), nil
case typing.String.Kind:
isArray := reflect.ValueOf(colVal).Kind() == reflect.Slice
_, isMap := colVal.(map[string]any)
Expand Down

0 comments on commit fb6b1de

Please sign in to comment.