/
BloomFilterDataMapTestCase.scala
385 lines (367 loc) · 18.1 KB
/
BloomFilterDataMapTestCase.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
/*
* 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.cluster.sdv.generated
import org.apache.spark.sql.Row
import org.apache.spark.sql.test.util.QueryTest
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
import org.apache.carbondata.core.constants.CarbonCommonConstants
import org.apache.carbondata.core.util.CarbonProperties
class BloomFilterDataMapTestCase extends QueryTest with BeforeAndAfterEach with BeforeAndAfterAll {
override protected def beforeAll(): Unit = {
CarbonProperties.getInstance().addProperty(CarbonCommonConstants.CARBON_DATE_FORMAT,
"yyyy-MM-dd")
CarbonProperties.getInstance().addProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT,
"yyyy-MM-dd HH:mm:ss")
}
override protected def afterAll(): Unit = {
CarbonProperties.getInstance().addProperty(CarbonCommonConstants.CARBON_DATE_FORMAT,
CarbonCommonConstants.CARBON_DATE_DEFAULT_FORMAT)
CarbonProperties.getInstance().addProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT,
CarbonCommonConstants.CARBON_TIMESTAMP_DEFAULT_FORMAT)
}
private def createAllDataTypeTable(tableName: String): Unit = {
sql(s"DROP TABLE IF EXISTS $tableName")
sql(
s"""
| CREATE TABLE $tableName(
| booleanField boolean,
| shortField smallint,
| intField int,
| bigintField bigint,
| doubleField double,
| stringField string,
| timestampField timestamp,
| decimalField decimal(18,2),
| dateField date,
| charField string,
| floatField float,
| stringDictField string,
| stringSortField string,
| stringLocalDictField string,
| longStringField string
| )
| STORED BY 'carbondata'
| TBLPROPERTIES(
| 'LONG_STRING_COLUMNS'='longStringField',
| 'SORT_COLUMNS'='stringSortField',
| 'DICTIONARY_INCLUDE'='stringDictField',
| 'local_dictionary_enable'='true',
| 'local_dictionary_threshold'='10000',
| 'local_dictionary_include'='stringLocalDictField',
| 'CACHE_LEVEL'='BLOCKLET')
""".stripMargin)
}
private def loadAllDataTypeTable(tableName: String): Unit = {
sql(
s"""
| INSERT INTO TABLE $tableName VALUES
| (true,1,11,101,41.4,'string1','2015-04-23 12:01:01',12.34,'2015-04-23','aaa',1.5,'dict1','sort1','local_dict1','longstring1'),
| (false,2,12,102,42.4,'string2','2015-05-23 12:01:03',23.45,'2015-05-23','bbb',2.5,'dict2','sort2','local_dict2','longstring2'),
| (true,3,13,163,43.4,'string3','2015-07-26 12:01:06',34.56,'2015-07-26','ccc',3.5,'dict3','sort3','local_dict3','longstring3'),
| (NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)
""".stripMargin)
sql(
s"""
| INSERT INTO TABLE $tableName VALUES
| (true,${Short.MaxValue - 2},${Int.MinValue + 2},${Long.MaxValue - 2},${Double.MinValue + 2},'string1','2015-04-23 12:01:01',${Double.MinValue + 2},'2015-04-23','aaa',${Float.MaxValue - 2},'dict1','sort1','local_dict1','longstring1'),
| (false,2,12,102,42.4,'string2','2015-05-23 12:01:03',23.45,'2015-05-23','bbb',2.5,'dict2','sort2','local_dict2','longstring2'),
| (true,${Short.MinValue + 2},${Int.MaxValue - 2},${Long.MinValue + 2},${Double.MaxValue - 2},'string3','2015-07-26 12:01:06',${Double.MinValue + 2},'2015-07-26','ccc',${Float.MinValue + 2},'dict3','sort3','local_dict3','longstring3'),
| (NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)
""".stripMargin)
}
test("create bloomfilter datamap on all datatypes") {
val tableName = "all_data_types"
val dataMapName = "dm_with_all_data_types"
createAllDataTypeTable(tableName)
loadAllDataTypeTable(tableName)
// create datamap on all supported datatype
sql(
s"""
| CREATE DATAMAP $dataMapName ON TABLE $tableName
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='booleanField, shortField, intField, bigintField, doubleField, stringField, timestampField, decimalField, dateField, charField, floatField, stringDictField, stringSortField, stringLocalDictField, longStringField',
| 'BLOOM_SIZE'='6400',
| 'BLOOM_FPP'='0.001',
| 'BLOOM_COMPRESS'='TRUE')
""".stripMargin)
loadAllDataTypeTable(tableName)
checkExistence(sql(s"SHOW DATAMAP ON TABLE $tableName"), true, "bloomfilter", dataMapName)
checkAnswer(sql(s"SELECT COUNT(*) FROM $tableName"), Seq(Row(16)))
checkExistence(
sql(s"EXPLAIN SELECT * FROM $tableName WHERE booleanField=false AND shortField=2 AND intField=12 AND bigintField=102 AND doubleField=42.4 AND stringField='string2' AND timestampField='2015-05-23 12:01:03' AND decimalField=23.45 AND dateField='2015-05-23' AND charField='bbb' AND floatField=2.5 AND stringDictField='dict2' AND stringSortField='sort2' AND stringLocalDictField= 'local_dict2' AND longStringField='longstring2'"),
true, "bloomfilter", dataMapName)
checkAnswer(
sql(s"SELECT COUNT(*) FROM (SELECT * FROM $tableName WHERE booleanField=false AND shortField=2 AND intField=12 AND bigintField=102 AND stringField='string2' AND timestampField='2015-05-23 12:01:03' AND decimalField=23.45 AND dateField='2015-05-23' AND charField='bbb' AND floatField=2.5 AND stringDictField='dict2' AND stringSortField='sort2' AND stringLocalDictField= 'local_dict2' AND longStringField='longstring2') b"),
Seq(Row(4)))
sql(s"DROP TABLE IF EXISTS $tableName")
}
test("create bloomfilter datamap on all datatypes with sort columns") {
val tableName = "all_data_types_with_sort_column"
val dataMapName = "dm_with_all_data_types_with_sort_column"
sql(s"DROP TABLE IF EXISTS $tableName")
// double/float/decimal/longstring cannot be sort_columns so we ignore them in SORT_COLUMNS
sql(
s"""
| CREATE TABLE $tableName(
| booleanField boolean,
| shortField smallint,
| intField int,
| bigintField bigint,
| doubleField double,
| stringField string,
| timestampField timestamp,
| decimalField decimal(18,2),
| dateField date,
| charField string,
| floatField float,
| stringDictField string,
| stringSortField string,
| stringLocalDictField string,
| longStringField string
| )
| STORED BY 'carbondata'
| TBLPROPERTIES(
| 'LONG_STRING_COLUMNS'='longStringField',
| 'DICTIONARY_INCLUDE'='stringDictField',
| 'local_dictionary_enable'='true',
| 'local_dictionary_threshold'='10000',
| 'local_dictionary_include'='stringLocalDictField',
| 'SORT_COLUMNS'='booleanField, shortField, intField, bigintField, stringField, timestampField, dateField, charField, stringDictField, stringSortField, stringLocalDictField')
""".stripMargin)
loadAllDataTypeTable(tableName)
// create datamap on all supported datatype
sql(
s"""
| CREATE DATAMAP $dataMapName ON TABLE $tableName
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='booleanField, shortField, intField, bigintField, doubleField, stringField, timestampField, decimalField, dateField, charField, floatField, stringDictField, stringSortField, stringLocalDictField, longStringField',
| 'BLOOM_SIZE'='6400',
| 'BLOOM_FPP'='0.001',
| 'BLOOM_COMPRESS'='TRUE')
""".stripMargin)
loadAllDataTypeTable(tableName)
checkExistence(sql(s"SHOW DATAMAP ON TABLE $tableName"), true, "bloomfilter", dataMapName)
checkAnswer(sql(s"SELECT COUNT(*) FROM $tableName"), Seq(Row(16)))
checkExistence(
sql(s"EXPLAIN SELECT * FROM $tableName WHERE booleanField=false AND shortField=2 AND intField=12 AND bigintField=102 AND doubleField=42.4 AND stringField='string2' AND timestampField='2015-05-23 12:01:03' AND decimalField=23.45 AND dateField='2015-05-23' AND charField='bbb' AND floatField=2.5 AND stringDictField='dict2' AND stringSortField='sort2' AND stringLocalDictField= 'local_dict2' AND longStringField='longstring2'"),
true, "bloomfilter", dataMapName)
checkAnswer(
sql(s"SELECT COUNT(*) FROM (SELECT * FROM $tableName WHERE booleanField=false AND shortField=2 AND intField=12 AND bigintField=102 AND stringField='string2' AND timestampField='2015-05-23 12:01:03' AND decimalField=23.45 AND dateField='2015-05-23' AND charField='bbb' AND floatField=2.5 AND stringDictField='dict2' AND stringSortField='sort2' AND stringLocalDictField= 'local_dict2' AND longStringField='longstring2') b"),
Seq(Row(4)))
sql(s"DROP TABLE IF EXISTS $tableName")
}
test("test bloom datamap with empty values on index column") {
val normalTable = "normal_table"
val bloomDMSampleTable = "bloom_table"
val dataMapName = "bloom_datamap"
sql(s"DROP TABLE IF EXISTS $normalTable")
sql(s"DROP TABLE IF EXISTS $bloomDMSampleTable")
sql(s"CREATE TABLE $normalTable(c1 string, c2 int, c3 string) STORED BY 'carbondata'")
sql(s"CREATE TABLE $bloomDMSampleTable(c1 string, c2 int, c3 string) STORED BY 'carbondata'")
// load data with empty value
sql(s"INSERT INTO $normalTable SELECT '', 1, 'xxx'")
sql(s"INSERT INTO $bloomDMSampleTable SELECT '', 1, 'xxx'")
sql(s"INSERT INTO $normalTable SELECT '', null, 'xxx'")
sql(s"INSERT INTO $bloomDMSampleTable SELECT '', null, 'xxx'")
sql(
s"""
| CREATE DATAMAP $dataMapName on table $bloomDMSampleTable
| using 'bloomfilter'
| DMPROPERTIES('index_columns'='c1, c2')
""".stripMargin)
// load data with empty value
sql(s"INSERT INTO $normalTable SELECT '', 1, 'xxx'")
sql(s"INSERT INTO $bloomDMSampleTable SELECT '', 1, 'xxx'")
sql(s"INSERT INTO $normalTable SELECT '', null, 'xxx'")
sql(s"INSERT INTO $bloomDMSampleTable SELECT '', null, 'xxx'")
// query on null fields
checkAnswer(sql(s"SELECT * FROM $bloomDMSampleTable"),
sql(s"SELECT * FROM $normalTable"))
checkAnswer(sql(s"SELECT * FROM $bloomDMSampleTable WHERE c1 = null"),
sql(s"SELECT * FROM $normalTable WHERE c1 = null"))
checkAnswer(sql(s"SELECT * FROM $bloomDMSampleTable WHERE c1 = ''"),
sql(s"SELECT * FROM $normalTable WHERE c1 = ''"))
checkAnswer(sql(s"SELECT * FROM $bloomDMSampleTable WHERE isNull(c1)"),
sql(s"SELECT * FROM $normalTable WHERE isNull(c1)"))
checkAnswer(sql(s"SELECT * FROM $bloomDMSampleTable WHERE isNull(c2)"),
sql(s"SELECT * FROM $normalTable WHERE isNull(c2)"))
sql(s"DROP TABLE IF EXISTS $normalTable")
sql(s"DROP TABLE IF EXISTS $bloomDMSampleTable")
}
test("create multiple datamaps vs create on datamap on multiple columns") {
val tableName1 = "all_data_types10"
val tableName2 = "all_data_types20"
val dataMapName1 = "dm_with_all_data_types10"
val dataMapName2Prefix = "dm_with_all_data_types2"
sql(s"DROP TABLE IF EXISTS $tableName1")
sql(s"DROP TABLE IF EXISTS $tableName2")
createAllDataTypeTable(tableName1)
createAllDataTypeTable(tableName2)
loadAllDataTypeTable(tableName1)
loadAllDataTypeTable(tableName2)
// create one datamap on multiple index columns
sql(
s"""
| CREATE DATAMAP $dataMapName1 ON TABLE $tableName1
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='booleanField, shortField, intField, bigintField, doubleField, stringField, timestampField, decimalField, dateField, charField, floatField, stringDictField, stringSortField, stringLocalDictField, longStringField'
| )
""".stripMargin)
// create multiple datamaps each on one index column
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}0 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='booleanField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}1 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='shortField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}2 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='intField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}3 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='bigintField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}4 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='doubleField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}5 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='stringField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}6 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='timestampField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}7 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='decimalField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}8 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='dateField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}9 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='charField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}10 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='floatField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}11 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='stringDictField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}12 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='stringSortField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}13 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='stringLocalDictField'
| )
""".stripMargin)
sql(
s"""
| CREATE DATAMAP ${dataMapName2Prefix}14 ON TABLE $tableName2
| USING 'bloomfilter'
| DMPROPERTIES(
| 'INDEX_COLUMNS'='longStringField'
| )
""".stripMargin)
loadAllDataTypeTable(tableName1)
loadAllDataTypeTable(tableName2)
assert(sql(s"SHOW DATAMAP ON TABLE $tableName1").collect().length == 1)
assert(sql(s"SHOW DATAMAP ON TABLE $tableName2").collect().length == 15)
checkAnswer(sql(s"SELECT COUNT(*) FROM $tableName1"), sql(s"SELECT COUNT(*) FROM $tableName2"))
checkExistence(
sql(s"EXPLAIN SELECT * FROM $tableName1 WHERE booleanField=false AND shortField=2 AND intField=12 AND bigintField=102 AND doubleField=42.4 AND stringField='string2' AND timestampField='2015-05-23 12:01:03' AND decimalField=23.45 AND dateField='2015-05-23' AND charField='bbb' AND floatField=2.5 AND stringDictField='dict2' AND stringSortField='sort2' AND stringLocalDictField= 'local_dict2' AND longStringField='longstring2'"),
true, "bloomfilter", dataMapName1)
val allDataMapsOnTable2 = (0 to 14).map(p => s"$dataMapName2Prefix$p")
val existedString = (allDataMapsOnTable2 :+ "bloomfilter").toArray
checkExistence(
sql(s"EXPLAIN SELECT * FROM $tableName2 WHERE booleanField=false AND shortField=2 AND intField=12 AND bigintField=102 AND doubleField=42.4 AND stringField='string2' AND timestampField='2015-05-23 12:01:03' AND decimalField=23.45 AND dateField='2015-05-23' AND charField='bbb' AND floatField=2.5 AND stringDictField='dict2' AND stringSortField='sort2' AND stringLocalDictField= 'local_dict2' AND longStringField='longstring2'"),
true, existedString: _*)
checkAnswer(
sql(s"SELECT COUNT(*) FROM (SELECT * FROM $tableName1 WHERE booleanField=false AND shortField=2 AND intField=12 AND bigintField=102 AND stringField='string2' AND timestampField='2015-05-23 12:01:03' AND decimalField=23.45 AND dateField='2015-05-23' AND charField='bbb' AND floatField=2.5 AND stringDictField='dict2' AND stringSortField='sort2' AND stringLocalDictField= 'local_dict2' AND longStringField='longstring2') b"),
sql(s"SELECT COUNT(*) FROM (SELECT * FROM $tableName2 WHERE booleanField=false AND shortField=2 AND intField=12 AND bigintField=102 AND stringField='string2' AND timestampField='2015-05-23 12:01:03' AND decimalField=23.45 AND dateField='2015-05-23' AND charField='bbb' AND floatField=2.5 AND stringDictField='dict2' AND stringSortField='sort2' AND stringLocalDictField= 'local_dict2' AND longStringField='longstring2') b"))
sql(s"DROP TABLE IF EXISTS $tableName1")
sql(s"DROP TABLE IF EXISTS $tableName2")
}
}