From fb6b1de1969b960c91b09ea2a35316a030a3a2d7 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Tue, 22 Oct 2024 13:15:54 -0700 Subject: [PATCH] WIP. --- clients/bigquery/storagewrite.go | 10 ++--- clients/mssql/values.go | 4 +- clients/shared/default_value.go | 6 +-- lib/optimization/table_data.go | 4 +- lib/parquetutil/parse_values.go | 6 +-- lib/typing/ext/parse.go | 66 ++++++++++++++------------------ lib/typing/values/string.go | 6 +-- 7 files changed, 46 insertions(+), 56 deletions(-) diff --git a/clients/bigquery/storagewrite.go b/clients/bigquery/storagewrite.go index ee4a3d664..63605a827 100644 --- a/clients/bigquery/storagewrite.go +++ b/clients/bigquery/storagewrite.go @@ -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) } diff --git a/clients/mssql/values.go b/clients/mssql/values.go index 22b0d9e30..94174ceca 100644 --- a/clients/mssql/values.go +++ b/clients/mssql/values.go @@ -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) diff --git a/clients/shared/default_value.go b/clients/shared/default_value.go index 18c0c3ec3..6f9e8ddb2 100644 --- a/clients/shared/default_value.go +++ b/clients/shared/default_value.go @@ -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 { diff --git a/lib/optimization/table_data.go b/lib/optimization/table_data.go index 12b0f6c3d..bae801623 100644 --- a/lib/optimization/table_data.go +++ b/lib/optimization/table_data.go @@ -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 diff --git a/lib/parquetutil/parse_values.go b/lib/parquetutil/parse_values.go index 091ce64b9..05af0d519 100644 --- a/lib/parquetutil/parse_values.go +++ b/lib/parquetutil/parse_values.go @@ -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: diff --git a/lib/typing/ext/parse.go b/lib/typing/ext/parse.go index 909bd929f..9fbefdb85 100644 --- a/lib/typing/ext/parse.go +++ b/lib/typing/ext/parse.go @@ -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) } diff --git a/lib/typing/values/string.go b/lib/typing/values/string.go index 8c7a485cc..890c579ee 100644 --- a/lib/typing/values/string.go +++ b/lib/typing/values/string.go @@ -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)