forked from apache/carbondata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestSegmentReading.scala
368 lines (356 loc) · 16.9 KB
/
TestSegmentReading.scala
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package org.apache.carbondata.spark.testsuite.segmentreading
import org.apache.spark.sql.Row
import org.apache.spark.sql.test.util.QueryTest
import org.scalatest.BeforeAndAfterAll
import org.apache.carbondata.spark.exception.MalformedCarbonCommandException
/**
* Created by rahul on 19/9/17.
*/
class TestSegmentReading extends QueryTest with BeforeAndAfterAll {
override def beforeAll(): Unit = {
cleanAllTable()
sql(
"create table carbon_table(empno int, empname String, designation String, doj Timestamp," +
"workgroupcategory int, workgroupcategoryname String, deptno int, deptname String," +
"projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int," +
"utilization int,salary int) STORED BY 'org.apache.carbondata.format'")
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
sql(
s"""LOAD DATA local inpath '$resourcesPath/data1.csv' INTO TABLE carbon_table OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
}
private def cleanAllTable(): Unit = {
sql("drop table if exists carbon_table")
sql("drop table if exists carbon_table_join")
sql("drop table if exists carbon_table_update")
sql("drop table if exists carbon_table_delete")
sql("drop table if exists carbon_table_show_seg")
sql("drop table if exists carbon_table_compact")
sql("drop table if exists carbon_table_alter")
sql("drop table if exists carbon_table_alter_new")
sql("drop table if exists carbon_table_recreate")
}
override def afterAll(): Unit = {
cleanAllTable()
}
test("test SET -V for segment reading property") {
try {
checkExistence(sql("SET -v"), true, "Property to configure the list of segments to query.")
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test count(*) for segment reading property") {
try {
sql("SET carbon.input.segments.default.carbon_table=1")
checkAnswer(sql("select count(*) from carbon_table"), Seq(Row(10)))
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test SET propertyname for segment reading property") {
try {
sql("SET carbon.input.segments.default.carbon_table=1")
checkAnswer(sql("SET carbon.input.segments.default.carbon_table"),
Seq(Row("carbon.input.segments.default.carbon_table", "1"))
)
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("set valid segments and query from table") {
try {
checkAnswer(sql("select count(*) from carbon_table"), Seq(Row(20)))
sql("SET carbon.input.segments.default.carbon_table=1")
checkAnswer(sql("select count(*) from carbon_table"), Seq(Row(10)))
sql("SET carbon.input.segments.default.carbon_table=*")
checkAnswer(sql("select count(*) from carbon_table"), Seq(Row(20)))
sql("SET carbon.input.segments.default.carbon_table=0")
checkAnswer(sql("select count(*) from carbon_table"), Seq(Row(10)))
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test Multiple times set segment") {
try {
sql("SET carbon.input.segments.default.carbon_table=0")
checkAnswer(sql("select count(*) from carbon_table"), Seq(Row(10)))
sql("SET carbon.input.segments.default.carbon_table=1")
checkAnswer(sql("select count(*) from carbon_table"), Seq(Row(10)))
sql("SET carbon.input.segments.default.carbon_table=1,0,1")
checkAnswer(sql("select count(*) from carbon_table"), Seq(Row(20)))
sql("SET carbon.input.segments.default.carbon_table=2,0")
checkAnswer(sql("select count(*) from carbon_table"), Seq(Row(10)))
val trapped = intercept[Exception] {
sql("SET carbon.input.segments.default.carbon_table=2,a")
}
val trappedAgain = intercept[Exception] {
sql("SET carbon.input.segments.default.carbon_table=,")
}
assert(trapped.getMessage
.equalsIgnoreCase(
"carbon.input.segments.<database_name>.<table_name> value range is not valid"))
assert(trappedAgain.getMessage
.equalsIgnoreCase("carbon.input.segments.<database_name>.<table_name> value can't be empty."))
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test filter with segment reading"){
try {
sql("SET carbon.input.segments.default.carbon_table=*")
checkAnswer(sql("select count(empno) from carbon_table where empno = 15"),Seq(Row(2)))
sql("SET carbon.input.segments.default.carbon_table=1")
checkAnswer(sql("select count(empno) from carbon_table where empno = 15"),Seq(Row(1)))
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test group by with segment reading") {
try {
sql("SET carbon.input.segments.default.carbon_table=*")
checkAnswer(sql("select empno,count(empname) from carbon_table where empno = 15 group by empno"),Seq(Row(15,2)))
sql("SET carbon.input.segments.default.carbon_table=1")
checkAnswer(sql("select empno,count(empname) from carbon_table where empno = 15 group by empno"),Seq(Row(15,1)))
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test join with segment reading"){
try {
sql("SET carbon.input.segments.default.carbon_table=*")
sql("drop table if exists carbon_table_join")
sql(
"create table carbon_table_join(empno int, empname String, designation String, doj Timestamp," +
"workgroupcategory int, workgroupcategoryname String, deptno int, deptname String," +
"projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int," +
"utilization int,salary int) STORED BY 'org.apache.carbondata.format'")
sql("insert into carbon_table_join select * from carbon_table").show()
checkAnswer(sql("select count(a.empno) from carbon_table a inner join carbon_table_join b on a.empno = b.empno"),Seq(Row(22)))
sql("SET carbon.input.segments.default.carbon_table=1")
checkAnswer(sql("select count(a.empno) from carbon_table a inner join carbon_table_join b on a.empno = b.empno"),Seq(Row(11)))
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test aggregation with segment reading") {
try {
sql("SET carbon.input.segments.default.carbon_table=*")
checkAnswer(sql("select sum(empno) from carbon_table"), Seq(Row(1411)))
sql("SET carbon.input.segments.default.carbon_table=1")
checkAnswer(sql("select sum(empno) from carbon_table"), Seq(Row(1256)))
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test update query with segment reading"){
try {
sql("drop table if exists carbon_table_update")
sql(
"create table carbon_table_update(empno int, empname String, designation String, doj Timestamp," +
"workgroupcategory int, workgroupcategoryname String, deptno int, deptname String," +
"projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int," +
"utilization int,salary int) STORED BY 'org.apache.carbondata.format'")
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_update OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
sql(
s"""LOAD DATA local inpath '$resourcesPath/data1.csv' INTO TABLE carbon_table_update OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
sql("SET carbon.input.segments.default.carbon_table=1")
intercept[MalformedCarbonCommandException]{
sql("update carbon_table_update a set(a.empname) = (select b.empname from carbon_table b where a.empno=b.empno)").show()
}
sql("SET carbon.input.segments.default.carbon_table=*")
sql("SET carbon.input.segments.default.carbon_table_update=1")
intercept[MalformedCarbonCommandException]{
sql("update carbon_table_update a set(a.empname) = (select b.empname from carbon_table b where a.empno=b.empno)").show()
}
sql("SET carbon.input.segments.default.carbon_table=*")
sql("SET carbon.input.segments.default.carbon_table_update=*")
checkAnswer(sql("select count(*) from carbon_table_update where empname='rahul'"), Seq(Row(0)))
sql("update carbon_table_update a set(a.empname) = ('rahul')").show()
checkAnswer(sql("select count(*) from carbon_table_update where empname='rahul'"), Seq(Row(20)))
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test delete query with segment reading"){
try {
sql("drop table if exists carbon_table_delete")
sql(
"create table carbon_table_delete(empno int, empname String, designation String, doj Timestamp," +
"workgroupcategory int, workgroupcategoryname String, deptno int, deptname String," +
"projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int," +
"utilization int,salary int) STORED BY 'org.apache.carbondata.format'")
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_delete OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_delete OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
sql("SET carbon.input.segments.default.carbon_table=*")
sql("SET carbon.input.segments.default.carbon_table=1")
intercept[MalformedCarbonCommandException]{
sql("delete from carbon_table_delete where empno IN (select empno from carbon_table where empname='ayushi')").show()
}
sql("SET carbon.input.segments.default.carbon_table_delete=1")
intercept[MalformedCarbonCommandException]{
sql("delete from carbon_table_delete where empno IN (select empno from carbon_table where empname='ayushi')").show()
}
sql("SET carbon.input.segments.default.carbon_table=*")
sql("SET carbon.input.segments.default.carbon_table_delete=*")
checkAnswer(sql("select count(*) from carbon_table_delete"), Seq(Row(20)))
sql("delete from carbon_table_delete where empno IN (select empno from carbon_table where empname='ayushi')").show()
checkAnswer(sql("select count(*) from carbon_table_delete"), Seq(Row(18)))
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test show segments"){
try {
sql("drop table if exists carbon_table_show_seg")
sql(
"create table carbon_table_show_seg(empno int, empname String, designation String, doj Timestamp," +
"workgroupcategory int, workgroupcategoryname String, deptno int, deptname String," +
"projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int," +
"utilization int,salary int) STORED BY 'org.apache.carbondata.format'")
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_show_seg OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_show_seg OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
sql("alter table carbon_table_show_seg compact 'major'")
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_show_seg OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
val df = sql("SHOW SEGMENTS for table carbon_table_show_seg")
val col = df.collect().map{
row => Row(row.getString(0),row.getString(1),row.getString(4))
}.toSeq
assert(col.equals(Seq(Row("2","Success","NA"),
Row("1","Compacted","0.1"),
Row("0.1","Success","NA"),
Row("0","Compacted","0.1"))))
}
finally {
sql("SET carbon.input.segments.default.carbon_table=*")
}
}
test("test segment reading after compaction"){
sql("drop table if exists carbon_table_compact")
sql(
"create table carbon_table_compact(empno int, empname String, designation String, doj Timestamp," +
"workgroupcategory int, workgroupcategoryname String, deptno int, deptname String," +
"projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int," +
"utilization int,salary int) STORED BY 'org.apache.carbondata.format'")
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_compact OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_compact OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
sql("alter table carbon_table_compact compact 'major'")
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_compact OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".stripMargin)
checkAnswer(sql("select count(*) from carbon_table_compact"),Seq(Row(30)))
sql(" SET carbon.input.segments.default.carbon_table_compact=0.1")
checkAnswer(sql("select count(*) from carbon_table_compact"),Seq(Row(20)))
}
test("set segment id then alter table name and check select query") {
try {
sql("drop table if exists carbon_table_alter")
sql("drop table if exists carbon_table_alter_new")
sql(
"create table carbon_table_alter(empno int, empname String, designation String, doj " +
"Timestamp," +
"workgroupcategory int, workgroupcategoryname String, deptno int, deptname String," +
"projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int," +
"utilization int,salary int) STORED BY 'org.apache.carbondata.format'")
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_alter OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".
stripMargin)
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_alter OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".
stripMargin)
checkAnswer(sql("select count(*) from carbon_table_alter"),
Seq(Row(20)))
sql(
"SET carbon.input.segments.default.carbon_table_alter=1")
checkAnswer(sql(
"select count(*) from carbon_table_alter"), Seq(Row(10)))
sql(
"alter table carbon_table_alter rename to carbon_table_alter_new")
checkAnswer(sql(
"select count(*) from carbon_table_alter_new")
, Seq(Row(20)))
}
finally {
sql(
"SET carbon.input.segments.default.carbon_table=*")
}
}
test("drop and recreate table to check segment reading") {
try {
sql("drop table if exists carbon_table_recreate")
sql(
"create table carbon_table_recreate(empno int, empname String, designation String, doj " +
"Timestamp," +
"workgroupcategory int, workgroupcategoryname String, deptno int, deptname String," +
"projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int," +
"utilization int,salary int) STORED BY 'org.apache.carbondata.format'")
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_recreate OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".
stripMargin)
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_recreate OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".
stripMargin)
checkAnswer(sql("select count(*) from carbon_table_recreate"),
Seq(Row(20)))
sql(
"SET carbon.input.segments.default.carbon_table_recreate=1")
checkAnswer(sql(
"select count(*) from carbon_table_recreate"), Seq(Row(10)))
sql("drop table if exists carbon_table_recreate")
sql(
"create table carbon_table_recreate(empno int, empname String, designation String, doj " +
"Timestamp," +
"workgroupcategory int, workgroupcategoryname String, deptno int, deptname String," +
"projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int," +
"utilization int,salary int) STORED BY 'org.apache.carbondata.format'")
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_recreate OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".
stripMargin)
sql(
s"""LOAD DATA local inpath '$resourcesPath/data.csv' INTO TABLE carbon_table_recreate OPTIONS
|('DELIMITER'= ',', 'QUOTECHAR'= '\"')""".
stripMargin)
checkAnswer(sql(
"select count(*) from carbon_table_recreate"), Seq(Row(10)))
}
finally {
sql(
"SET carbon.input.segments.default.carbon_table=*")
}
}
}