/
selectmany.go
152 lines (136 loc) · 4.1 KB
/
selectmany.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Example: Fetch many rows and allow cancel the query by Ctrl+C.
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime/pprof"
"strconv"
_ "net/http/pprof"
"runtime/debug"
sf "github.com/snowflakedb/gosnowflake"
)
var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
memprofile = flag.String("memprofile", "", "write memory profile to this file")
)
// getDSN constructs a DSN based on the test connection parameters
func getDSN() (string, *sf.Config, error) {
env := func(k string, failOnMissing bool) string {
if value := os.Getenv(k); value != "" {
return value
}
if failOnMissing {
log.Fatalf("%v environment variable is not set.", k)
}
return ""
}
account := env("SNOWFLAKE_TEST_ACCOUNT", true)
user := env("SNOWFLAKE_TEST_USER", true)
password := env("SNOWFLAKE_TEST_PASSWORD", true)
host := env("SNOWFLAKE_TEST_HOST", false)
port := env("SNOWFLAKE_TEST_PORT", false)
protocol := env("SNOWFLAKE_TEST_PROTOCOL", false)
portStr, err := strconv.Atoi(port)
if err != nil {
return "", nil, err
}
cfg := &sf.Config{
Account: account,
User: user,
Password: password,
Host: host,
Port: portStr,
Protocol: protocol,
}
dsn, err := sf.DSN(cfg)
return dsn, cfg, err
}
// run is an actual main
func run(dsn string) {
// handler interrupt signal
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
defer close(c)
signal.Notify(c, os.Interrupt)
defer func() {
signal.Stop(c)
}()
go func() {
select {
case <-c:
cancel()
case <-ctx.Done():
}
}()
db, err := sql.Open("snowflake", dsn)
if err != nil {
log.Fatalf("failed to connect. err: %v", err)
}
defer db.Close()
query := `select * from
(select 0 a union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) A,
(select 0 b union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) B,
(select 0 c union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) C,
(select 0 d union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) E,
(select 0 e union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) F,
(select 0 f union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) G,
(select 0 f union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) H`
fmt.Printf("Executing a query. It may take long. You may stop by Ctrl+C.\n")
rows, err := db.QueryContext(ctx, query)
if err != nil {
log.Fatalf("failed to run a query. %v, err: %v", query, err)
}
defer rows.Close()
var v1, v2, v3, v4, v5, v6, v7 int
fmt.Printf("Fetching the results. It may take long. You may stop by Ctrl+C.\n")
counter := 0
for rows.Next() {
if err = rows.Scan(&v1, &v2, &v3, &v4, &v5, &v6, &v7); err != nil {
log.Fatalf("failed to get result. err: %v", err)
}
if counter%10000 == 0 {
fmt.Printf("data: %v, %v, %v, %v, %v, %v, %v\n", v1, v2, v3, v4, v5, v6, v7)
}
if counter%1000000 == 0 {
debug.FreeOSMemory()
}
counter++
}
if rows.Err() != nil {
fmt.Printf("ERROR: %v\n", rows.Err())
return
}
fmt.Printf("Congrats! You have successfully run %v with Snowflake DB!\n", query)
}
func main() {
if !flag.Parsed() {
flag.Parse()
}
dsn, cfg, err := getDSN()
if err != nil {
log.Fatalf("failed to create DSN from Config: %v, err: %v", cfg, err)
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
run(dsn)
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
return
}
}