Skip to content

Commit

Permalink
SNOW-857660: Init rows location once
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pfus committed Aug 9, 2023
1 parent ad2d6fe commit 1105d9f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion location.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func getCurrentLocation(params map[string]*string) *time.Location {
loc := time.Now().Location()
var err error
paramsMutex.Lock()
if tz, ok := params["timezone"]; ok {
if tz, ok := params["timezone"]; ok && tz != nil {
loc, err = time.LoadLocation(*tz)
if err != nil {
loc = time.Now().Location()
Expand Down
47 changes: 47 additions & 0 deletions location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ package gosnowflake

import (
"errors"
"fmt"
"reflect"
"testing"
"time"
)

type tcLocation struct {
Expand Down Expand Up @@ -97,3 +100,47 @@ func TestWithOffsetString(t *testing.T) {
}
}
}

func TestGetCurrentLocation(t *testing.T) {
specificTz := "Mexico/BajaSur"
specificLoc, err := time.LoadLocation(specificTz)
if err != nil {
t.Fatalf("Cannot initialize specific timezone location")
}
incorrectTz := "Not/exists"
testcases := []struct {
params map[string]*string
loc *time.Location
}{
{
params: map[string]*string{},
loc: time.Now().Location(),
},
{
params: map[string]*string{
"timezone": nil,
},
loc: time.Now().Location(),
},
{
params: map[string]*string{
"timezone": &specificTz,
},
loc: specificLoc,
},
{
params: map[string]*string{
"timezone": &incorrectTz,
},
loc: time.Now().Location(),
},
}
for _, tc := range testcases {
t.Run(fmt.Sprintf("%v", tc.loc), func(t *testing.T) {
loc := getCurrentLocation(tc.params)
if !reflect.DeepEqual(*loc, *tc.loc) {
t.Fatalf("location mismatch. expected: %v, got: %v", tc.loc, loc)
}
})
}
}
14 changes: 9 additions & 5 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ type snowflakeRows struct {
status queryStatus
err error
errChannel chan error
location *time.Location
}

func (rows *snowflakeRows) getLocation() *time.Location {
if rows.location == nil && rows.sc != nil && rows.sc.cfg != nil {
rows.location = getCurrentLocation(rows.sc.cfg.Params)
}
return rows.location
}

type snowflakeValue interface{}
Expand Down Expand Up @@ -184,11 +192,7 @@ func (rows *snowflakeRows) Next(dest []driver.Value) (err error) {
for i, n := 0, len(row.RowSet); i < n; i++ {
// could move to chunk downloader so that each go routine
// can convert data
var loc *time.Location
if rows.sc != nil {
loc = getCurrentLocation(rows.sc.cfg.Params)
}
err = stringToValue(&dest[i], rows.ChunkDownloader.getRowType()[i], row.RowSet[i], loc)
err = stringToValue(&dest[i], rows.ChunkDownloader.getRowType()[i], row.RowSet[i], rows.getLocation())
if err != nil {
return err
}
Expand Down

0 comments on commit 1105d9f

Please sign in to comment.