serramatutu commented on code in PR #558:
URL: https://github.com/apache/arrow-go/pull/558#discussion_r2758752868


##########
arrow/extensions/timestamp_with_offset.go:
##########
@@ -0,0 +1,568 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package extensions
+
+import (
+       "errors"
+       "fmt"
+       "math"
+       "reflect"
+       "strings"
+       "time"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/arrow-go/v18/arrow/array"
+       "github.com/apache/arrow-go/v18/arrow/memory"
+       "github.com/apache/arrow-go/v18/internal/json"
+)
+
+// TimestampWithOffsetType represents a timestamp column that stores a 
timezone offset per row instead of
+// applying the same timezone offset to the entire column.
+type TimestampWithOffsetType struct {
+       arrow.ExtensionBase
+}
+
+func isOffsetTypeOk(offsetType arrow.DataType) bool {
+       switch offsetType := offsetType.(type) {
+       case *arrow.Int16Type:
+               return true
+       case *arrow.DictionaryType:
+               return arrow.IsInteger(offsetType.IndexType.ID()) && 
arrow.TypeEqual(offsetType.ValueType, arrow.PrimitiveTypes.Int16)
+       case *arrow.RunEndEncodedType:
+               return offsetType.ValidRunEndsType(offsetType.RunEnds()) && 
+                       arrow.TypeEqual(offsetType.Encoded(), 
arrow.PrimitiveTypes.Int16)
+                       // FIXME: Technically this should be non-nullable, but 
a Arrow IPC does not deserialize
+                       // ValueNullable properly, so enforcing this here would 
always fail when reading from an IPC
+                       // stream
+                       // !offsetType.ValueNullable
+       default:
+               return false
+       }
+}
+
+// Whether the storageType is compatible with TimestampWithOffset.
+//
+// Returns (time_unit, offset_type, ok). If ok is false, time_unit and 
offset_type are garbage.
+func isDataTypeCompatible(storageType arrow.DataType) (arrow.TimeUnit, 
arrow.DataType, bool) {
+       timeUnit := arrow.Second
+       offsetType := arrow.PrimitiveTypes.Int16
+       switch t := storageType.(type) {
+       case *arrow.StructType:
+               if t.NumFields() != 2 {
+                       return timeUnit, offsetType, false
+               }
+
+               maybeTimestamp := t.Field(0)
+               maybeOffset := t.Field(1)
+
+               timestampOk := false
+               switch timestampType := maybeTimestamp.Type.(type) {
+               case *arrow.TimestampType:
+                       if timestampType.TimeZone == "UTC" {
+                               timestampOk = true
+                               timeUnit = timestampType.TimeUnit()
+                       }
+               default:
+               }
+
+               offsetOk := isOffsetTypeOk(maybeOffset.Type)
+
+               ok := maybeTimestamp.Name == "timestamp" &&
+                       timestampOk &&
+                       !maybeTimestamp.Nullable &&
+                       maybeOffset.Name == "offset_minutes" &&
+                       offsetOk &&
+                       !maybeOffset.Nullable
+
+               return timeUnit, maybeOffset.Type, ok
+       default:
+               return timeUnit, offsetType, false
+       }
+}
+
+// NewTimestampWithOffsetType creates a new TimestampWithOffsetType with the 
underlying storage type set correctly to
+// Struct(timestamp=Timestamp(T, "UTC"), offset_minutes=O), where T is any 
TimeUnit and O is a valid offset type.
+//
+// The error will be populated if the data type is not a valid encoding of the 
offsets field.
+func NewTimestampWithOffsetType(unit arrow.TimeUnit, offsetType 
arrow.DataType) (*TimestampWithOffsetType, error) {
+       if !isOffsetTypeOk(offsetType) {
+               return nil, errors.New(fmt.Sprintf("Invalid offset type %s", 
offsetType))
+       }
+
+       return &TimestampWithOffsetType{
+               ExtensionBase: arrow.ExtensionBase{
+                       Storage: arrow.StructOf(
+                               arrow.Field{
+                                       Name: "timestamp",
+                                       Type: &arrow.TimestampType{
+                                               Unit:     unit,
+                                               TimeZone: "UTC",
+                                       },
+                                       Nullable: false,
+                               },
+                               arrow.Field{
+                                       Name:     "offset_minutes",
+                                       Type:     offsetType,
+                                       Nullable: false,
+                               },
+                       ),
+               },
+       }, nil
+}
+
+
+// NewTimestampWithOffsetTypePrimitiveEncoded creates a new 
TimestampWithOffsetType with the underlying storage type set correctly to
+// Struct(timestamp=Timestamp(T, "UTC"), offset_minutes=Int16), where T is any 
TimeUnit.
+func NewTimestampWithOffsetTypePrimitiveEncoded(unit arrow.TimeUnit) 
*TimestampWithOffsetType {
+       v, _ := NewTimestampWithOffsetType(unit, arrow.PrimitiveTypes.Int16)
+       // SAFETY: This should never error as Int16 is always a valid offset 
type
+
+       return v
+}
+
+// NewTimestampWithOffsetType creates a new TimestampWithOffsetType with the 
underlying storage type set correctly to
+// Struct(timestamp=Timestamp(T, "UTC"), offset_minutes=Dictionary(I, Int16)), 
where T is any TimeUnit and I is a
+// valid Dictionary index type.
+//
+// The error will be populated if the index is not a valid dictionary-encoding 
index type.
+func NewTimestampWithOffsetTypeDictionaryEncoded(unit arrow.TimeUnit, index 
arrow.DataType) (*TimestampWithOffsetType, error) {
+       offsetType := arrow.DictionaryType{
+               IndexType: index,
+               ValueType: arrow.PrimitiveTypes.Int16,
+               Ordered:   false,
+       }
+       return NewTimestampWithOffsetType(unit, &offsetType)
+}
+
+// NewTimestampWithOffsetType creates a new TimestampWithOffsetType with the 
underlying storage type set correctly to
+// Struct(timestamp=Timestamp(T, "UTC"), offset_minutes=RunEndEncoded(E, 
Int16)), where T is any TimeUnit and E is a
+// valid run-ends type.
+//
+// The error will be populated if runEnds is not a valid run-end encoding 
run-ends type.
+func NewTimestampWithOffsetTypeRunEndEncoded(unit arrow.TimeUnit, runEnds 
arrow.DataType) (*TimestampWithOffsetType, error) {
+       offsetType := arrow.RunEndEncodedOf(runEnds, arrow.PrimitiveTypes.Int16)
+       if !offsetType.ValidRunEndsType(runEnds) {
+               return nil, errors.New(fmt.Sprintf("Invalid run-ends type %s", 
runEnds))
+       }
+
+       return NewTimestampWithOffsetType(unit, offsetType)
+}
+
+
+func (b *TimestampWithOffsetType) ArrayType() reflect.Type {
+       return reflect.TypeOf(TimestampWithOffsetArray{})
+}
+
+func (b *TimestampWithOffsetType) ExtensionName() string { return 
"arrow.timestamp_with_offset" }
+
+func (b *TimestampWithOffsetType) String() string {
+       return fmt.Sprintf("extension<%s>", b.ExtensionName())
+}
+
+func (e *TimestampWithOffsetType) MarshalJSON() ([]byte, error) {
+       return []byte(fmt.Sprintf(`{"name":"%s","metadata":%s}`, 
e.ExtensionName(), e.Serialize())), nil
+}
+
+func (b *TimestampWithOffsetType) Serialize() string { return "" }
+
+func (b *TimestampWithOffsetType) Deserialize(storageType arrow.DataType, data 
string) (arrow.ExtensionType, error) {
+       timeUnit, offsetType, ok := isDataTypeCompatible(storageType)
+       if !ok {
+               return nil, fmt.Errorf("invalid storage type for 
TimestampWithOffsetType: %s", storageType.Name())
+       }
+
+       v, _ := NewTimestampWithOffsetType(timeUnit, offsetType)
+       // SAFETY: the offsetType has already been checked by 
isDataTypeCompatible, so we can ignore the error
+
+       return v, nil
+}
+
+func (b *TimestampWithOffsetType) ExtensionEquals(other arrow.ExtensionType) 
bool {
+       return b.ExtensionName() == other.ExtensionName()
+}

Review Comment:
   Fixed: 
https://github.com/apache/arrow-go/pull/558/changes/2762d64b50221ac82ed0a22231eb8478427823ed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to