This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
engine_pilosa_test.go
209 lines (184 loc) · 4.34 KB
/
engine_pilosa_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
// +build !windows
package sqle_test
import (
"context"
"io/ioutil"
"os"
"testing"
"time"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/index/pilosa"
"github.com/src-d/go-mysql-server/test"
"github.com/stretchr/testify/require"
)
func TestIndexes(t *testing.T) {
e := newEngine(t)
tmpDir, err := ioutil.TempDir(os.TempDir(), "pilosa-test")
require.NoError(t, err)
require.NoError(t, os.MkdirAll(tmpDir, 0644))
e.Catalog.RegisterIndexDriver(pilosa.NewDriver(tmpDir))
_, _, err = e.Query(
newCtx(),
"CREATE INDEX idx_i ON mytable USING pilosa (i) WITH (async = false)",
)
require.NoError(t, err)
_, _, err = e.Query(
newCtx(),
"CREATE INDEX idx_s ON mytable USING pilosa (s) WITH (async = false)",
)
require.NoError(t, err)
_, _, err = e.Query(
newCtx(),
"CREATE INDEX idx_is ON mytable USING pilosa (i, s) WITH (async = false)",
)
require.NoError(t, err)
defer func() {
done, err := e.Catalog.DeleteIndex("mydb", "idx_i", true)
require.NoError(t, err)
<-done
done, err = e.Catalog.DeleteIndex("mydb", "idx_s", true)
require.NoError(t, err)
<-done
done, err = e.Catalog.DeleteIndex("foo", "idx_is", true)
require.NoError(t, err)
<-done
}()
testCases := []struct {
query string
expected []sql.Row
}{
{
"SELECT * FROM mytable WHERE i = 2",
[]sql.Row{
{int64(2), "second row"},
},
},
{
"SELECT * FROM mytable WHERE i > 1",
[]sql.Row{
{int64(3), "third row"},
{int64(2), "second row"},
},
},
{
"SELECT * FROM mytable WHERE i < 3",
[]sql.Row{
{int64(1), "first row"},
{int64(2), "second row"},
},
},
{
"SELECT * FROM mytable WHERE i <= 2",
[]sql.Row{
{int64(2), "second row"},
{int64(1), "first row"},
},
},
{
"SELECT * FROM mytable WHERE i >= 2",
[]sql.Row{
{int64(2), "second row"},
{int64(3), "third row"},
},
},
{
"SELECT * FROM mytable WHERE i = 2 AND s = 'second row'",
[]sql.Row{
{int64(2), "second row"},
},
},
{
"SELECT * FROM mytable WHERE i = 2 AND s = 'third row'",
([]sql.Row)(nil),
},
{
"SELECT * FROM mytable WHERE i BETWEEN 1 AND 2",
[]sql.Row{
{int64(1), "first row"},
{int64(2), "second row"},
},
},
{
"SELECT * FROM mytable WHERE i = 1 OR i = 2",
[]sql.Row{
{int64(1), "first row"},
{int64(2), "second row"},
},
},
{
"SELECT * FROM mytable WHERE i = 1 AND i = 2",
([]sql.Row)(nil),
},
{
"SELECT i as mytable_i FROM mytable WHERE mytable_i = 2",
[]sql.Row{
{int64(2)},
},
},
{
"SELECT i as mytable_i FROM mytable WHERE mytable_i > 1",
[]sql.Row{
{int64(3)},
{int64(2)},
},
},
{
"SELECT i as mytable_i, s as mytable_s FROM mytable WHERE mytable_i = 2 AND mytable_s = 'second row'",
[]sql.Row{
{int64(2), "second row"},
},
},
{
"SELECT s, SUBSTRING(s, 1, 1) AS sub_s FROM mytable WHERE sub_s = 's'",
[]sql.Row{
{"second row", "s"},
},
},
{
"SELECT count(i) AS mytable_i, SUBSTR(s, -3) AS mytable_s FROM mytable WHERE i > 0 AND mytable_s='row' GROUP BY mytable_s",
[]sql.Row{
{int64(3), "row"},
},
},
{
"SELECT mytable_i FROM (SELECT i AS mytable_i FROM mytable) as t WHERE mytable_i > 1",
[]sql.Row{
{int64(2)},
{int64(3)},
},
},
}
for _, tt := range testCases {
t.Run(tt.query, func(t *testing.T) {
require := require.New(t)
tracer := new(test.MemTracer)
ctx := sql.NewContext(context.TODO(), sql.WithTracer(tracer))
_, it, err := e.Query(ctx, tt.query)
require.NoError(err)
rows, err := sql.RowIterToRows(it)
require.NoError(err)
require.ElementsMatch(tt.expected, rows)
require.Equal("plan.ResolvedTable", tracer.Spans[len(tracer.Spans)-1])
})
}
}
func TestCreateIndex(t *testing.T) {
require := require.New(t)
e := newEngine(t)
tmpDir, err := ioutil.TempDir(os.TempDir(), "pilosa-test")
require.NoError(err)
require.NoError(os.MkdirAll(tmpDir, 0644))
e.Catalog.RegisterIndexDriver(pilosa.NewDriver(tmpDir))
_, iter, err := e.Query(newCtx(), "CREATE INDEX myidx ON mytable USING pilosa (i)")
require.NoError(err)
rows, err := sql.RowIterToRows(iter)
require.NoError(err)
require.Len(rows, 0)
defer func() {
time.Sleep(1 * time.Second)
done, err := e.Catalog.DeleteIndex("foo", "myidx", true)
require.NoError(err)
<-done
require.NoError(os.RemoveAll(tmpDir))
}()
}