forked from apache/carbondata
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestDDLForPartitionTable.scala
421 lines (383 loc) · 18.4 KB
/
TestDDLForPartitionTable.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.carbondata.spark.testsuite.partition
import org.scalatest.BeforeAndAfterAll
import org.apache.carbondata.core.constants.CarbonCommonConstants
import org.apache.carbondata.core.metadata.CarbonMetadata
import org.apache.carbondata.core.metadata.datatype.DataTypes
import org.apache.carbondata.core.metadata.encoder.Encoding
import org.apache.carbondata.core.metadata.schema.partition.PartitionType
import org.apache.carbondata.core.util.CarbonProperties
import org.apache.spark.sql.test.util.QueryTest
class TestDDLForPartitionTable extends QueryTest with BeforeAndAfterAll {
override def beforeAll = {
CarbonProperties.getInstance()
.addProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT, "yyyy-MM-dd HH:mm:ss")
.addProperty(CarbonCommonConstants.CARBON_DATE_FORMAT, "yyyy-MM-dd")
dropTable
CarbonProperties.getInstance()
.addProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT, "dd-MM-yyyy")
}
test("create partition table: hash partition") {
sql(
"""
| CREATE TABLE default.hashTable (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)
| PARTITIONED BY (empno int)
| STORED BY 'org.apache.carbondata.format'
| TBLPROPERTIES('PARTITION_TYPE'='HASH','NUM_PARTITIONS'='3')
""".stripMargin)
val carbonTable = CarbonMetadata.getInstance().getCarbonTable("default", "hashTable")
val partitionInfo = carbonTable.getPartitionInfo(carbonTable.getTableName)
assert(partitionInfo != null)
assert(partitionInfo.getColumnSchemaList.get(0).getColumnName.equalsIgnoreCase("empno"))
assert(partitionInfo.getColumnSchemaList.get(0).getDataType == DataTypes.INT)
assert(partitionInfo.getColumnSchemaList.get(0).getEncodingList.size == 0)
assert(partitionInfo.getPartitionType == PartitionType.HASH)
assert(partitionInfo.getNumPartitions == 3)
}
test("create partition table: range partition") {
sql(
"""
| CREATE TABLE default.rangeTable (empno int, empname String, designation String,
| workgroupcategory int, workgroupcategoryname String, deptno int, deptname String,
| projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int,
| utilization int,salary int)
| PARTITIONED BY (doj Timestamp)
| STORED BY 'org.apache.carbondata.format'
| TBLPROPERTIES('PARTITION_TYPE'='RANGE',
| 'RANGE_INFO'='2017-06-11 00:00:02, 2017-06-13 23:59:59', 'DICTIONARY_INCLUDE'='doj')
""".stripMargin)
val carbonTable = CarbonMetadata.getInstance().getCarbonTable("default", "rangeTable")
val partitionInfo = carbonTable.getPartitionInfo(carbonTable.getTableName)
assert(partitionInfo != null)
assert(partitionInfo.getColumnSchemaList.get(0).getColumnName.equalsIgnoreCase("doj"))
assert(partitionInfo.getColumnSchemaList.get(0).getDataType == DataTypes.TIMESTAMP)
assert(partitionInfo.getColumnSchemaList.get(0).getEncodingList.size == 3)
assert(partitionInfo.getColumnSchemaList.get(0).getEncodingList.get(0) == Encoding.DICTIONARY)
assert(partitionInfo.getColumnSchemaList.get(0).getEncodingList.get(1) == Encoding.DIRECT_DICTIONARY)
assert(partitionInfo.getColumnSchemaList.get(0).getEncodingList.get(2) == Encoding.INVERTED_INDEX)
assert(partitionInfo.getPartitionType == PartitionType.RANGE)
assert(partitionInfo.getRangeInfo.size == 2)
assert(partitionInfo.getRangeInfo.get(0).equals("2017-06-11 00:00:02"))
assert(partitionInfo.getRangeInfo.get(1).equals("2017-06-13 23:59:59"))
}
test("create partition table: list partition") {
sql(
"""
| CREATE TABLE default.listTable (empno int, empname String, designation String, doj Timestamp,
| workgroupcategoryname String, deptno int, deptname String,
| projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int,
| utilization int,salary int)
| PARTITIONED BY (workgroupcategory string)
| STORED BY 'org.apache.carbondata.format'
| TBLPROPERTIES('PARTITION_TYPE'='LIST',
| 'LIST_INFO'='0, 1, (2, 3)')
""".stripMargin)
val carbonTable = CarbonMetadata.getInstance().getCarbonTable("default", "listTable")
val partitionInfo = carbonTable.getPartitionInfo(carbonTable.getTableName)
assert(partitionInfo != null)
assert(partitionInfo.getColumnSchemaList.get(0).getColumnName.equalsIgnoreCase("workgroupcategory"))
assert(partitionInfo.getColumnSchemaList.get(0).getDataType == DataTypes.STRING)
assert(partitionInfo.getColumnSchemaList.get(0).getEncodingList.size == 1)
assert(partitionInfo.getColumnSchemaList.get(0).getEncodingList.get(0) == Encoding.INVERTED_INDEX)
assert(partitionInfo.getPartitionType == PartitionType.LIST)
assert(partitionInfo.getListInfo.size == 3)
assert(partitionInfo.getListInfo.get(0).size == 1)
assert(partitionInfo.getListInfo.get(0).get(0).equals("0"))
assert(partitionInfo.getListInfo.get(1).size == 1)
assert(partitionInfo.getListInfo.get(1).get(0).equals("1"))
assert(partitionInfo.getListInfo.get(2).size == 2)
assert(partitionInfo.getListInfo.get(2).get(0).equals("2"))
assert(partitionInfo.getListInfo.get(2).get(1).equals("3"))
}
test("create partition table: list partition with duplicate value") {
intercept[Exception] { sql(
"""
| CREATE TABLE default.listTableError (empno int, empname String, designation String, doj Timestamp,
| workgroupcategoryname String, deptno int, deptname String,
| projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int,
| utilization int,salary int)
| PARTITIONED BY (workgroupcategory string)
| STORED BY 'org.apache.carbondata.format'
| TBLPROPERTIES('PARTITION_TYPE'='LIST',
| 'LIST_INFO'='0, 1, (2, 3, 1)')
""".stripMargin) }
}
test("test exception if partition column is dropped") {
sql("drop table if exists test")
sql(
"create table test(a int, b string) partitioned by (c int) stored by 'carbondata' " +
"tblproperties('PARTITION_TYPE'='LIST','list_info'='0,10,5,20')")
intercept[Exception] { sql("alter table test drop columns(c)") }
}
test("test describe formatted for partition column") {
sql("drop table if exists des")
sql(
"""create table des(a int, b string) partitioned by (c string) stored by 'carbondata'
|tblproperties ('partition_type'='list','list_info'='1,2')""".stripMargin)
checkExistence(sql("describe formatted des"), true, "Partition Columns")
sql("drop table if exists des")
}
test("test exception if hash number is invalid") {
sql("DROP TABLE IF EXISTS test_hash_1")
val exception_test_hash_1: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_hash_1(col1 INT, col2 STRING)
| PARTITIONED BY (col3 INT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='HASH', 'NUM_PARTITIONS'='2.1')
""".stripMargin
)
}
assert(exception_test_hash_1.getMessage.contains("Invalid partition definition"))
sql("DROP TABLE IF EXISTS test_hash_2")
val exception_test_hash_2: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_hash_2(col1 INT, col2 STRING)
| PARTITIONED BY (col3 INT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='HASH', 'NUM_PARTITIONS'='abc')
""".stripMargin
)
}
assert(exception_test_hash_2.getMessage.contains("Invalid partition definition"))
sql("DROP TABLE IF EXISTS test_hash_3")
val exception_test_hash_3: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_hash_3(col1 INT, col2 STRING)
| PARTITIONED BY (col3 INT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='HASH', 'NUM_PARTITIONS'='-2.1')
""".stripMargin
)
}
assert(exception_test_hash_3.getMessage.contains("Invalid partition definition"))
}
test("test exception when values in list_info can not match partition column type") {
sql("DROP TABLE IF EXISTS test_list_int")
val exception_test_list_int: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_list_int(col1 INT, col2 STRING)
| PARTITIONED BY (col3 INT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='LIST', 'LIST_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_list_int.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_list_small")
val exception_test_list_small: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_list_small(col1 INT, col2 STRING)
| PARTITIONED BY (col3 SMALLINT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='LIST', 'LIST_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_list_small.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_list_float")
val exception_test_list_float: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_list_float(col1 INT, col2 STRING)
| PARTITIONED BY (col3 FLOAT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='LIST', 'LIST_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_list_float.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_list_double")
val exception_test_list_double: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_list_double(col1 INT, col2 STRING)
| PARTITIONED BY (col3 DOUBLE) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='LIST', 'LIST_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_list_double.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_list_bigint")
val exception_test_list_bigint: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_list_bigint(col1 INT, col2 STRING)
| PARTITIONED BY (col3 BIGINT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='LIST', 'LIST_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_list_bigint.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_list_date")
val exception_test_list_date: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_list_date(col1 INT, col2 STRING)
| PARTITIONED BY (col3 DATE) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='LIST', 'LIST_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_list_date.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_list_timestamp")
val exception_test_list_timestamp: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_list_timestamp(col1 INT, col2 STRING)
| PARTITIONED BY (col3 TIMESTAMP) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='LIST', 'LIST_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_list_timestamp.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_list_decimal")
val exception_test_list_decimal: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_list_decimal(col1 INT, col2 STRING)
| PARTITIONED BY (col3 DECIMAL(25, 4)) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='LIST', 'LIST_INFO'='23.23111,2.32')
""".stripMargin)
}
assert(exception_test_list_decimal.getMessage.contains("Invalid Partition Values"))
}
test("test exception when values in range_info can not match partition column type") {
sql("DROP TABLE IF EXISTS test_range_int")
val exception_test_range_int: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_range_int(col1 INT, col2 STRING)
| PARTITIONED BY (col3 INT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='RANGE', 'RANGE_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_range_int.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_range_smallint")
val exception_test_range_smallint: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_range_smallint(col1 INT, col2 STRING)
| PARTITIONED BY (col3 SMALLINT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='RANGE', 'RANGE_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_range_smallint.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_range_float")
val exception_test_range_float: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_range_float(col1 INT, col2 STRING)
| PARTITIONED BY (col3 FLOAT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='RANGE', 'RANGE_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_range_float.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_range_double")
val exception_test_range_double: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_range_double(col1 INT, col2 STRING)
| PARTITIONED BY (col3 DOUBLE) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='RANGE', 'RANGE_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_range_double.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_range_bigint")
val exception_test_range_bigint: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_range_bigint(col1 INT, col2 STRING)
| PARTITIONED BY (col3 BIGINT) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='RANGE', 'RANGE_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_range_bigint.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_range_date")
val exception_test_range_date: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_range_date(col1 INT, col2 STRING)
| PARTITIONED BY (col3 DATE) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='RANGE', 'RANGE_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_range_date.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_range_timestamp")
val exception_test_range_timestamp: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_range_timestamp(col1 INT, col2 STRING)
| PARTITIONED BY (col3 TIMESTAMP) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='RANGE', 'RANGE_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_range_timestamp.getMessage.contains("Invalid Partition Values"))
sql("DROP TABLE IF EXISTS test_range_decimal")
val exception_test_range_decimal: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE test_range_decimal(col1 INT, col2 STRING)
| PARTITIONED BY (col3 DECIMAL(25, 4)) STORED BY 'carbondata'
| TBLPROPERTIES('PARTITION_TYPE'='RANGE', 'RANGE_INFO'='abc,def')
""".stripMargin)
}
assert(exception_test_range_decimal.getMessage.contains("Invalid Partition Values"))
}
test("Invalid Partition Range") {
val exceptionMessage: Exception = intercept[Exception] {
sql(
"""
| CREATE TABLE default.rangeTableInvalid (empno int, empname String, designation String,
| workgroupcategory int, workgroupcategoryname String, deptno int, deptname String,
| projectcode int, projectjoindate Timestamp, projectenddate Timestamp,attendance int,
| utilization int,salary int)
| PARTITIONED BY (doj Timestamp)
| STORED BY 'org.apache.carbondata.format'
| TBLPROPERTIES('PARTITION_TYPE'='RANGE',
| 'RANGE_INFO'='2017-06-11 00:00:02')
""".stripMargin)
}
assert(exceptionMessage.getMessage
.contains("Range info must define a valid range.Please check again!"))
}
override def afterAll = {
dropTable
}
def dropTable = {
sql("drop table if exists hashTable")
sql("drop table if exists rangeTable")
sql("drop table if exists listTable")
sql("drop table if exists test")
sql("DROP TABLE IF EXISTS test_hash_1")
sql("DROP TABLE IF EXISTS test_hash_2")
sql("DROP TABLE IF EXISTS test_hash_3")
sql("DROP TABLE IF EXISTS test_list_int")
sql("DROP TABLE IF EXISTS test_list_smallint")
sql("DROP TABLE IF EXISTS test_list_bigint")
sql("DROP TABLE IF EXISTS test_list_float")
sql("DROP TABLE IF EXISTS test_list_double")
sql("DROP TABLE IF EXISTS test_list_date")
sql("DROP TABLE IF EXISTS test_list_timestamp")
sql("DROP TABLE IF EXISTS test_list_decimal")
sql("DROP TABLE IF EXISTS test_range_int")
sql("DROP TABLE IF EXISTS test_range_smallint")
sql("DROP TABLE IF EXISTS test_range_bigint")
sql("DROP TABLE IF EXISTS test_range_float")
sql("DROP TABLE IF EXISTS test_range_double")
sql("DROP TABLE IF EXISTS test_range_date")
sql("DROP TABLE IF EXISTS test_range_timestamp")
sql("DROP TABLE IF EXISTS test_range_decimal")
sql("DROP TABLE IF EXISTS rangeTableInvalid")
}
}