Skip to content

Commit

Permalink
apacheGH-38795: [Go] Fix race GetToTimeFunc for Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroshade committed Nov 20, 2023
1 parent f98a132 commit 29ccdb2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
53 changes: 26 additions & 27 deletions go/arrow/compare_test.go
Expand Up @@ -116,13 +116,13 @@ func TestTypeEqual(t *testing.T) {
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
},
index: map[string][]int{"f1": []int{0}},
index: map[string][]int{"f1": {0}},
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0}},
index: map[string][]int{"f1": {0}},
},
false, true,
},
Expand All @@ -131,13 +131,13 @@ func TestTypeEqual(t *testing.T) {
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: false},
},
index: map[string][]int{"f1": []int{0}},
index: map[string][]int{"f1": {0}},
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0}},
index: map[string][]int{"f1": {0}},
},
false, false,
},
Expand All @@ -146,13 +146,13 @@ func TestTypeEqual(t *testing.T) {
fields: []Field{
{Name: "f0", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f0": []int{0}},
index: map[string][]int{"f0": {0}},
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0}},
index: map[string][]int{"f1": {0}},
},
false, false,
},
Expand All @@ -161,14 +161,14 @@ func TestTypeEqual(t *testing.T) {
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0}},
index: map[string][]int{"f1": {0}},
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
{Name: "f2", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0}, "f2": []int{1}},
index: map[string][]int{"f1": {0}, "f2": {1}},
},
false, true,
},
Expand All @@ -177,14 +177,14 @@ func TestTypeEqual(t *testing.T) {
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0}},
index: map[string][]int{"f1": {0}},
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
{Name: "f2", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0}, "f2": []int{1}},
index: map[string][]int{"f1": {0}, "f2": {1}},
},
false, false,
},
Expand All @@ -193,13 +193,13 @@ func TestTypeEqual(t *testing.T) {
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0}},
index: map[string][]int{"f1": {0}},
},
&StructType{
fields: []Field{
{Name: "f2", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f2": []int{0}},
index: map[string][]int{"f2": {0}},
},
false, false,
},
Expand All @@ -209,14 +209,14 @@ func TestTypeEqual(t *testing.T) {
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
{Name: "f2", Type: PrimitiveTypes.Float32, Nullable: false},
},
index: map[string][]int{"f1": []int{0}, "f2": []int{1}},
index: map[string][]int{"f1": {0}, "f2": {1}},
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
{Name: "f2", Type: PrimitiveTypes.Float32, Nullable: false},
},
index: map[string][]int{"f1": []int{0}, "f2": []int{1}},
index: map[string][]int{"f1": {0}, "f2": {1}},
},
true, false,
},
Expand All @@ -226,14 +226,14 @@ func TestTypeEqual(t *testing.T) {
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
{Name: "f2", Type: PrimitiveTypes.Float32, Nullable: false},
},
index: map[string][]int{"f1": []int{0}, "f2": []int{1}},
index: map[string][]int{"f1": {0}, "f2": {1}},
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
{Name: "f2", Type: PrimitiveTypes.Float32, Nullable: false},
},
index: map[string][]int{"f1": []int{0}, "f2": []int{1}},
index: map[string][]int{"f1": {0}, "f2": {1}},
},
true, false,
},
Expand All @@ -243,15 +243,15 @@ func TestTypeEqual(t *testing.T) {
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
{Name: "f2", Type: PrimitiveTypes.Float32, Nullable: false},
},
index: map[string][]int{"f1": []int{0}, "f2": []int{1}},
index: map[string][]int{"f1": {0}, "f2": {1}},
meta: MetadataFrom(map[string]string{"k1": "v1", "k2": "v2"}),
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
{Name: "f2", Type: PrimitiveTypes.Float32, Nullable: false},
},
index: map[string][]int{"f1": []int{0}, "f2": []int{1}},
index: map[string][]int{"f1": {0}, "f2": {1}},
meta: MetadataFrom(map[string]string{"k2": "v2", "k1": "v1"}),
},
true, true,
Expand All @@ -261,14 +261,14 @@ func TestTypeEqual(t *testing.T) {
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0}},
index: map[string][]int{"f1": {0}},
meta: MetadataFrom(map[string]string{"k1": "v1"}),
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0}},
index: map[string][]int{"f1": {0}},
meta: MetadataFrom(map[string]string{"k1": "v2"}),
},
true, false,
Expand All @@ -279,14 +279,14 @@ func TestTypeEqual(t *testing.T) {
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true, Metadata: MetadataFrom(map[string]string{"k1": "v1"})},
{Name: "f2", Type: PrimitiveTypes.Float32, Nullable: false},
},
index: map[string][]int{"f1": []int{0}, "f2": []int{1}},
index: map[string][]int{"f1": {0}, "f2": {1}},
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true, Metadata: MetadataFrom(map[string]string{"k1": "v2"})},
{Name: "f2", Type: PrimitiveTypes.Float32, Nullable: false},
},
index: map[string][]int{"f1": []int{0}, "f2": []int{1}},
index: map[string][]int{"f1": {0}, "f2": {1}},
},
false, true,
},
Expand All @@ -296,14 +296,14 @@ func TestTypeEqual(t *testing.T) {
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0, 1}},
index: map[string][]int{"f1": {0, 1}},
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0, 1}},
index: map[string][]int{"f1": {0, 1}},
},
true, true,
},
Expand All @@ -313,14 +313,14 @@ func TestTypeEqual(t *testing.T) {
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
},
index: map[string][]int{"f1": []int{0, 1}},
index: map[string][]int{"f1": {0, 1}},
},
&StructType{
fields: []Field{
{Name: "f1", Type: PrimitiveTypes.Uint16, Nullable: true},
{Name: "f1", Type: PrimitiveTypes.Uint32, Nullable: true},
},
index: map[string][]int{"f1": []int{0, 1}},
index: map[string][]int{"f1": {0, 1}},
},
false, true,
},
Expand All @@ -343,7 +343,6 @@ func TestTypeEqual(t *testing.T) {
MapOf(BinaryTypes.String, &TimestampType{
Unit: 0,
TimeZone: "UTC",
loc: nil,
}),
true, false,
},
Expand Down
11 changes: 10 additions & 1 deletion go/arrow/datatype_fixedwidth.go
Expand Up @@ -19,6 +19,7 @@ package arrow
import (
"fmt"
"strconv"
"sync"
"time"

"github.com/apache/arrow/go/v15/internal/json"
Expand Down Expand Up @@ -354,6 +355,7 @@ type TimestampType struct {
TimeZone string

loc *time.Location
mx sync.RWMutex
}

func (*TimestampType) ID() Type { return TIMESTAMP }
Expand Down Expand Up @@ -386,6 +388,8 @@ func (t *TimestampType) TimeUnit() TimeUnit { return t.Unit }
// This should be called if you change the value of the TimeZone after having
// potentially called GetZone.
func (t *TimestampType) ClearCachedLocation() {
t.mx.Lock()
defer t.mx.Unlock()
t.loc = nil
}

Expand All @@ -398,10 +402,15 @@ func (t *TimestampType) ClearCachedLocation() {
// so if you change the value of TimeZone after calling this, make sure to call
// ClearCachedLocation.
func (t *TimestampType) GetZone() (*time.Location, error) {
t.mx.RLock()
if t.loc != nil {
defer t.mx.RUnlock()
return t.loc, nil
}

t.mx.RUnlock()
t.mx.Lock()
defer t.mx.Unlock()
// the TimeZone string is allowed to be either a valid tzdata string
// such as "America/New_York" or an absolute offset of the form -XX:XX
// or +XX:XX
Expand All @@ -415,7 +424,7 @@ func (t *TimestampType) GetZone() (*time.Location, error) {

if loc, err := time.LoadLocation(t.TimeZone); err == nil {
t.loc = loc
return t.loc, err
return loc, err
}

// at this point we know that the timezone isn't empty, and didn't match
Expand Down
24 changes: 24 additions & 0 deletions go/arrow/datatype_fixedwidth_test.go
Expand Up @@ -17,6 +17,7 @@
package arrow_test

import (
"sync"
"testing"
"time"

Expand Down Expand Up @@ -180,6 +181,29 @@ func TestTimestampType_GetToTimeFunc(t *testing.T) {
assert.Equal(t, "2345-12-29T19:00:00-05:00", toTimeNY(ts).Format(time.RFC3339))
}

func TestGetToTimeFuncRace(t *testing.T) {
var (
wg sync.WaitGroup
w = make(chan bool)
routineNum = 10
)

wg.Add(routineNum)
for i := 0; i < routineNum; i++ {
go func() {
defer wg.Done()

<-w

_, _ = arrow.FixedWidthTypes.Timestamp_s.(*arrow.TimestampType).GetToTimeFunc()
}()
}

close(w)

wg.Wait()
}

func TestTime32Type(t *testing.T) {
for _, tc := range []struct {
unit arrow.TimeUnit
Expand Down

0 comments on commit 29ccdb2

Please sign in to comment.