-
Notifications
You must be signed in to change notification settings - Fork 124
/
arrow_batches.go
135 lines (117 loc) · 3.83 KB
/
arrow_batches.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"context"
"database/sql"
"database/sql/driver"
"flag"
"fmt"
"github.com/apache/arrow/go/v15/arrow"
"github.com/apache/arrow/go/v15/arrow/array"
"github.com/apache/arrow/go/v15/arrow/memory"
"log"
"sync"
"time"
sf "github.com/snowflakedb/gosnowflake"
)
type sampleRecord struct {
batchID int
workerID int
number int32
string string
ts *time.Time
}
func (s sampleRecord) String() string {
return fmt.Sprintf("batchID: %v, workerID: %v, number: %v, string: %v, ts: %v", s.batchID, s.workerID, s.number, s.string, s.ts)
}
func main() {
if !flag.Parsed() {
flag.Parse()
}
cfg, err := sf.GetConfigFromEnv([]*sf.ConfigParam{
{Name: "Account", EnvName: "SNOWFLAKE_TEST_ACCOUNT", FailOnMissing: true},
{Name: "User", EnvName: "SNOWFLAKE_TEST_USER", FailOnMissing: true},
{Name: "Password", EnvName: "SNOWFLAKE_TEST_PASSWORD", FailOnMissing: true},
{Name: "Host", EnvName: "SNOWFLAKE_TEST_HOST", FailOnMissing: false},
{Name: "Port", EnvName: "SNOWFLAKE_TEST_PORT", FailOnMissing: false},
{Name: "Protocol", EnvName: "SNOWFLAKE_TEST_PROTOCOL", FailOnMissing: false},
})
if err != nil {
log.Fatalf("failed to create Config, err: %v", err)
}
dsn, err := sf.DSN(cfg)
if err != nil {
log.Fatalf("failed to create DSN from Config: %v, err: %v", cfg, err)
}
ctx :=
sf.WithArrowBatchesTimestampOption(
sf.WithArrowAllocator(
sf.WithArrowBatches(context.Background()), memory.DefaultAllocator), sf.UseOriginalTimestamp)
query := "SELECT SEQ4(), 'example ' || (SEQ4() * 2), " +
" TO_TIMESTAMP_NTZ('9999-01-01 13:13:13.' || LPAD(SEQ4(),9,'0')) ltz " +
" FROM TABLE(GENERATOR(ROWCOUNT=>30000))"
db, err := sql.Open("snowflake", dsn)
if err != nil {
log.Fatalf("failed to connect. %v, err: %v", dsn, err)
}
defer db.Close()
conn, _ := db.Conn(ctx)
defer conn.Close()
var rows driver.Rows
err = conn.Raw(func(x interface{}) error {
rows, err = x.(driver.QueryerContext).QueryContext(ctx, query, nil)
return err
})
if err != nil {
log.Fatalf("unable to run the query. err: %v", err)
}
defer rows.Close()
batches, err := rows.(sf.SnowflakeRows).GetArrowBatches()
batchIds := make(chan int, 1)
maxWorkers := len(batches)
sampleRecordsPerBatch := make([][]sampleRecord, len(batches))
var waitGroup sync.WaitGroup
for workerID := 0; workerID < maxWorkers; workerID++ {
waitGroup.Add(1)
go func(waitGroup *sync.WaitGroup, batchIDs chan int, workerId int) {
defer waitGroup.Done()
for batchID := range batchIDs {
records, err := batches[batchID].Fetch()
if err != nil {
log.Fatalf("Error while fetching batch %v: %v", batchID, err)
}
sampleRecordsPerBatch[batchID] = make([]sampleRecord, batches[batchID].GetRowCount())
totalRowID := 0
convertFromColumnsToRows(records, sampleRecordsPerBatch, batchID, workerId, totalRowID, batches[batchID])
}
}(&waitGroup, batchIds, workerID)
}
for batchID := 0; batchID < len(batches); batchID++ {
batchIds <- batchID
}
close(batchIds)
waitGroup.Wait()
for _, batchSampleRecords := range sampleRecordsPerBatch {
for _, sampleRecord := range batchSampleRecords {
fmt.Println(sampleRecord)
}
}
for batchID, batch := range batches {
fmt.Printf("BatchId: %v, number of records: %v\n", batchID, batch.GetRowCount())
}
}
func convertFromColumnsToRows(records *[]arrow.Record, sampleRecordsPerBatch [][]sampleRecord, batchID int,
workerID int, totalRowID int, batch *sf.ArrowBatch) {
for _, record := range *records {
for rowID, intColumn := range record.Column(0).(*array.Int32).Int32Values() {
sampleRecord := sampleRecord{
batchID: batchID,
workerID: workerID,
number: intColumn,
string: record.Column(1).(*array.String).Value(rowID),
ts: batch.ArrowSnowflakeTimestampToTime(record, 2, rowID),
}
sampleRecordsPerBatch[batchID][totalRowID] = sampleRecord
totalRowID++
}
}
}