-
Notifications
You must be signed in to change notification settings - Fork 3
/
bulk_api_test.go
223 lines (191 loc) · 5.45 KB
/
bulk_api_test.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package exasol
import (
"bytes"
"fmt"
)
func (s *testSuite) TestBulkInsert() {
exa := s.exaConn
exa.Execute("CREATE TABLE foo ( id INT, val CHAR(1) )")
data := bytes.NewBufferString("1,a\n2,b\n3,c")
s.exaConn.Conf.SuppressError = true
// Should fail
err := exa.BulkInsert(s.qschema, "ASDF", data)
if s.Error(err) {
s.Contains(err.Error(), "ASDF")
}
// Should succeed
err = exa.BulkInsert(s.qschema, "FOO", data)
s.Nil(err)
got, err := exa.FetchSlice("SELECT * FROM foo ORDER BY id")
if s.NoError(err) {
expect := [][]interface{}{
{float64(1), "a"},
{float64(2), "b"},
{float64(3), "c"},
}
s.Equal(expect, got)
}
}
func (s *testSuite) TestBulkExecute() {
exa := s.exaConn
exa.Execute("CREATE TABLE foo ( id INT, val CHAR(1) )")
data := bytes.NewBufferString("1,\"a\"\n2,\"b\"\n3,\"c\"")
s.exaConn.Conf.SuppressError = true
// Should fail
err := exa.BulkExecute("ASDF", data)
if s.Error(err) {
s.Contains(err.Error(), "ASDF")
}
// Should succeed
err = exa.BulkExecute("IMPORT INTO [test].FOO FROM CSV AT '%s' FILE 'data.csv'", data)
s.Nil(err)
got, err := exa.FetchSlice("SELECT * FROM foo ORDER BY id")
if s.NoError(err) {
expect := [][]interface{}{
{float64(1), "a"},
{float64(2), "b"},
{float64(3), "c"},
}
s.Equal(expect, got)
}
}
func (s *testSuite) TestBulkSelect() {
exa := s.exaConn
exa.Execute("CREATE TABLE foo ( id INT, val CHAR(1) )")
exa.Execute("INSERT INTO foo VALUES (1,'a'),(2,'b'),(3,'c')")
data := &bytes.Buffer{}
s.exaConn.Conf.SuppressError = true
// Should fail
err := exa.BulkSelect(s.qschema, "ASDF", data)
if s.Error(err) {
s.Contains(err.Error(), "not found")
}
// Should succeed
err = exa.BulkSelect(s.qschema, "FOO", data)
if s.NoError(err) {
s.Equal("1,a\n2,b\n3,c\n", data.String())
}
}
func (s *testSuite) TestBulkQuery() {
exa := s.exaConn
exa.Execute("CREATE TABLE foo ( id INT, val CHAR(1) )")
exa.Execute("INSERT INTO foo VALUES (1,'a'),(2,'b'),(3,'c')")
data := &bytes.Buffer{}
s.exaConn.Conf.SuppressError = true
// Should fail
err := exa.BulkQuery("ASDF", data)
if s.Error(err) {
s.Contains(err.Error(), "syntax error")
}
// Should succeed
err = exa.BulkQuery(`
EXPORT (
SELECT id, val
FROM foo
ORDER BY id
) INTO CSV AT '%s'
FILE 'data.csv'
`, data)
if s.NoError(err) {
s.Equal("1,a\n2,b\n3,c\n", data.String())
}
}
func (s *testSuite) TestStreamInsert() {
s.execute(`CREATE TABLE foo ( id INT, val VARCHAR(10) )`)
numRows := 1000
data := make(chan []byte, numRows)
for i := 1; i <= numRows; i++ {
data <- []byte(fmt.Sprintf("%d,'%d'\n", i, i+10))
}
close(data)
// Should fail
s.exaConn.Conf.SuppressError = true
err := s.exaConn.StreamInsert(s.qschema, "asdf", data)
if s.Error(err) {
s.Contains(err.Error(), "not found")
}
// Should succeed
err = s.exaConn.StreamInsert(s.qschema, "foo", data)
s.Nil(err)
got := s.fetch(`SELECT COUNT(*), MIN(id), MAX(id) FROM foo`)
expect := [][]interface{}{{float64(numRows), float64(1), float64(numRows)}}
s.Equal(expect, got, "Correctly stream-inserted")
}
func (s *testSuite) TestStreamExecute() {
s.execute(`CREATE TABLE foo ( id INT, val VARCHAR(10) )`)
numRows := 1000
data := make(chan []byte, numRows)
for i := 1; i <= numRows; i++ {
data <- []byte(fmt.Sprintf("%d,'%d'\n", i, i+10))
}
close(data)
// Should fail
s.exaConn.Conf.SuppressError = true
err := s.exaConn.StreamExecute(`ASDF`, data)
if s.Error(err) {
s.Contains(err.Error(), "syntax error")
}
// Should succeed
err = s.exaConn.StreamExecute("IMPORT INTO [test].FOO FROM CSV AT '%s' FILE 'data.csv'", data)
s.Nil(err)
got := s.fetch(`SELECT COUNT(*), MIN(id), MAX(id) FROM foo`)
expect := [][]interface{}{{float64(numRows), float64(1), float64(numRows)}}
s.Equal(expect, got, "Correctly stream-inserted")
}
func (s *testSuite) TestStreamSelect() {
s.execute(`CREATE TABLE foo ( id INT, val CLOB )`)
s.execute(`INSERT INTO foo VALUES (1,'a'),(2,'b'),(3,'c')`)
// Should fail
s.exaConn.Conf.SuppressError = true
rows := s.exaConn.StreamSelect(s.qschema, "asdf")
var csv string
for d := range rows.Data {
csv += string(d)
}
if s.Error(rows.Error) {
s.Contains(rows.Error.Error(), "not found")
}
s.Equal("", csv, "Nothing streamed")
s.Equal(int64(0), rows.BytesRead)
// Should succeed
rows = s.exaConn.StreamSelect(s.qschema, "FOO")
s.Nil(rows.Error)
for d := range rows.Data {
csv += string(d)
}
rows.Close()
s.Equal("1,a\n2,b\n3,c\n", csv, "Streamed a select")
s.Equal(int64(12), rows.BytesRead)
}
func (s *testSuite) TestStreamQuery() {
s.execute(`CREATE TABLE foo ( id INT, val INT )`)
// Inserts 300K rows
s.execute(`INSERT INTO foo SELECT row_number() over() c, local.c FROM dual CONNECT BY LEVEL <= 3e5`)
// Should fail
s.exaConn.Conf.SuppressError = true
rows := s.exaConn.StreamQuery("asdf")
var csv string
for d := range rows.Data {
csv += string(d)
}
if s.Error(rows.Error) {
s.Contains(rows.Error.Error(), "syntax error")
}
s.Equal("", csv, "Nothing streamed")
s.Equal(int64(0), rows.BytesRead)
// Should succeed
rows = s.exaConn.StreamQuery(fmt.Sprintf(`
EXPORT ( SELECT t.id, t.val, NULL FROM %s.foo AS t ORDER BY t.id DESC NULLS LAST)
INTO CSV AT '%%s' FILE 'data.csv'
DELIMIT = NEVER
COLUMN SEPARATOR = '0x00'
`, s.qschema))
for d := range rows.Data {
csv += string(d)
rows.Pool.Put(d)
}
rows.Close()
s.Equal("300000\x00300000\x00\n29999", csv[:20], "Beginning ok")
s.Equal("2\x002\x00\n1\x001\x00\n", csv[len(csv)-10:], "End ok")
s.Equal(int64(4277790), rows.BytesRead)
}