-
Notifications
You must be signed in to change notification settings - Fork 53
/
query.go
180 lines (148 loc) · 3.39 KB
/
query.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package bigquery
import (
"context"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"cloud.google.com/go/bigquery"
"github.com/rs/zerolog/log"
"google.golang.org/api/iterator"
)
func (s *BigQueryServer) QueryNDJson(query string, writer io.Writer) error {
r, w := io.Pipe()
// errChan := make(chan error)
go func() {
queryErr := s.QueryJSON(query, w)
if queryErr != nil {
w.CloseWithError(queryErr)
} else {
w.Close()
}
}()
dec := json.NewDecoder(r)
// read open bracket
_, err := dec.Token()
if err != nil {
return err
}
// TODO: stream all of this instead of decoding
// while the array contains values
for dec.More() {
var m map[string]any
err := dec.Decode(&m)
if err != nil {
return err
// log.Fatal(err)
}
j, e := json.Marshal(m)
if e != nil {
return e
}
_, err = writer.Write(j)
if err != nil {
return err
}
_, err = writer.Write([]byte{'\n'})
if err != nil {
return err
}
}
// read closing bracket
_, err = dec.Token()
if err != nil {
return err
}
return nil
}
func (b *BigQueryServer) QueryJSON(query string, writer io.Writer) error {
// NOTE: Query should be with dataset as prefix. Example: SELECT * FROM `dataset.table`
ctx := context.TODO()
q := b.conn.Query(query)
writer.Write([]byte("["))
enc := json.NewEncoder(writer)
itr, err := q.Read(ctx)
if err != nil {
log.Error().Err(err).Msg("error getting query iterator")
return err
}
firstRow := true
for {
var data map[string]bigquery.Value
err := itr.Next(&data)
if err == iterator.Done {
break
}
if !firstRow {
_, err = writer.Write([]byte(","))
if err != nil {
log.Error().Err(err).Msg("failed to write JSON array separator")
return err
}
} else {
firstRow = false
}
err = enc.Encode(data)
if err != nil {
log.Error().Err(err).Msg("failed to encode JSON")
return err
}
}
writer.Write([]byte("]"))
return nil
}
func (b *BigQueryServer) QueryCSV(query string, writer io.Writer) error {
// NOTE: Query should be with dataset as prefix. Example: SELECT * FROM `dataset.table`
ctx := context.TODO()
q := b.conn.Query(query)
enc := csv.NewWriter(writer)
columnsQuery := b.conn.Query(query)
columnsItr, err := columnsQuery.Read(ctx)
if err != nil {
log.Error().Err(err).Msg("error getting columns query iterator")
return err
}
columns := make([]string, 0)
columnsRow := make(map[string]bigquery.Value)
if err := columnsItr.Next(&columnsRow); err != nil {
log.Error().Err(err).Msg("error retrieving columns")
return err
}
for columnName := range columnsRow {
columns = append(columns, columnName)
}
if err := enc.Write(columns); err != nil {
log.Error().Err(err).Msg("error writing columns to CSV")
return err
}
dataItr, err := q.Read(ctx)
if err != nil {
log.Error().Err(err).Msg("error getting data query iterator")
return err
}
for {
var dataRow map[string]bigquery.Value
err := dataItr.Next(&dataRow)
if err == iterator.Done {
break
}
row := make([]string, len(columns))
for i, columnName := range columns {
val := dataRow[columnName]
if val == nil {
val = "null"
}
row[i] = fmt.Sprintf("%v", val)
}
if err := enc.Write(row); err != nil {
log.Error().Err(err).Msg("error writing data row to CSV")
return err
}
}
enc.Flush()
if err := enc.Error(); err != nil {
log.Error().Err(err).Msg("error flushing CSV writer")
return err
}
return nil
}