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


##########
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 {

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



-- 
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