Skip to content

Commit 3a29554

Browse files
SNOW-911238: Add WithOriginalTimestamp context switch (#918)
SNOW-911238: Add WithOriginalTimestamp context switch
1 parent bec4641 commit 3a29554

10 files changed

+645
-232
lines changed

arrow_chunk.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (arc *arrowResultChunk) decodeArrowBatch(scd *snowflakeChunkDownloader) (*[
5757
for arc.reader.Next() {
5858
rawRecord := arc.reader.Record()
5959

60-
record, err := arrowToRecord(rawRecord, arc.allocator, scd.RowSet.RowType, arc.loc)
60+
record, err := arrowToRecord(scd.ctx, rawRecord, arc.allocator, scd.RowSet.RowType, arc.loc)
6161
if err != nil {
6262
return nil, err
6363
}

chunk_downloader.go

+3
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ func (scd *snowflakeChunkDownloader) startArrowBatches() error {
279279
idx: 0,
280280
scd: scd,
281281
funcDownloadHelper: scd.FuncDownloadHelper,
282+
loc: loc,
282283
}
283284
// decode first chunk if possible
284285
if firstArrowChunk.allocator != nil {
@@ -293,6 +294,7 @@ func (scd *snowflakeChunkDownloader) startArrowBatches() error {
293294
idx: i,
294295
scd: scd,
295296
funcDownloadHelper: scd.FuncDownloadHelper,
297+
loc: loc,
296298
}
297299
}
298300
return nil
@@ -708,6 +710,7 @@ type ArrowBatch struct {
708710
scd *snowflakeChunkDownloader
709711
funcDownloadHelper func(context.Context, *snowflakeChunkDownloader, int) error
710712
ctx context.Context
713+
loc *time.Location
711714
}
712715

713716
// WithContext sets the context which will be used for this ArrowBatch.

cmd/arrow/batches/arrow_batches.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/apache/arrow/go/v12/arrow/memory"
1212
"log"
1313
"sync"
14+
"time"
1415

1516
sf "github.com/snowflakedb/gosnowflake"
1617
)
@@ -20,10 +21,11 @@ type sampleRecord struct {
2021
workerID int
2122
number int32
2223
string string
24+
ts *time.Time
2325
}
2426

2527
func (s sampleRecord) String() string {
26-
return fmt.Sprintf("batchID: %v, workerID: %v, number: %v, string: %v", s.batchID, s.workerID, s.number, s.string)
28+
return fmt.Sprintf("batchID: %v, workerID: %v, number: %v, string: %v, ts: %v", s.batchID, s.workerID, s.number, s.string, s.ts)
2729
}
2830

2931
func main() {
@@ -48,8 +50,14 @@ func main() {
4850
log.Fatalf("failed to create DSN from Config: %v, err: %v", cfg, err)
4951
}
5052

51-
ctx := sf.WithArrowAllocator(sf.WithArrowBatches(context.Background()), memory.DefaultAllocator)
52-
query := "SELECT SEQ4(), 'example ' || (SEQ4() * 2) FROM TABLE(GENERATOR(ROWCOUNT=>30000))"
53+
ctx :=
54+
sf.WithOriginalTimestamp(
55+
sf.WithArrowAllocator(
56+
sf.WithArrowBatches(context.Background()), memory.DefaultAllocator))
57+
58+
query := "SELECT SEQ4(), 'example ' || (SEQ4() * 2), " +
59+
" TO_TIMESTAMP_NTZ('9999-01-01 13:13:13.' || LPAD(SEQ4(),9,'0')) ltz " +
60+
" FROM TABLE(GENERATOR(ROWCOUNT=>30000))"
5361

5462
db, err := sql.Open("snowflake", dsn)
5563
if err != nil {
@@ -88,7 +96,7 @@ func main() {
8896
}
8997
sampleRecordsPerBatch[batchID] = make([]sampleRecord, batches[batchID].GetRowCount())
9098
totalRowID := 0
91-
convertFromColumnsToRows(records, sampleRecordsPerBatch, batchID, workerId, totalRowID)
99+
convertFromColumnsToRows(records, sampleRecordsPerBatch, batchID, workerId, totalRowID, batches[batchID])
92100
}
93101
}(&waitGroup, batchIds, workerID)
94102
}
@@ -110,14 +118,15 @@ func main() {
110118
}
111119

112120
func convertFromColumnsToRows(records *[]arrow.Record, sampleRecordsPerBatch [][]sampleRecord, batchID int,
113-
workerID int, totalRowID int) {
121+
workerID int, totalRowID int, batch *sf.ArrowBatch) {
114122
for _, record := range *records {
115123
for rowID, intColumn := range record.Column(0).(*array.Int32).Int32Values() {
116124
sampleRecord := sampleRecord{
117125
batchID: batchID,
118126
workerID: workerID,
119127
number: intColumn,
120128
string: record.Column(1).(*array.String).Value(rowID),
129+
ts: batch.ArrowSnowflakeTimestampToTime(record, 2, rowID),
121130
}
122131
sampleRecordsPerBatch[batchID][totalRowID] = sampleRecord
123132
totalRowID++

connection.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ func (sc *snowflakeConn) queryContextInternal(
409409
rows.addDownloader(populateChunkDownloader(ctx, sc, data.Data))
410410
}
411411

412-
rows.ChunkDownloader.start()
412+
err = rows.ChunkDownloader.start()
413413
return rows, err
414414
}
415415

0 commit comments

Comments
 (0)