forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sys_bitmap_function.py
More file actions
984 lines (880 loc) · 35.9 KB
/
Copy pathtest_sys_bitmap_function.py
File metadata and controls
984 lines (880 loc) · 35.9 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
#!/bin/env pyth
# -*- coding: utf-8 -*-
# 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.
############################################################################
#
# @file test_sys_bitmap_function.py
# @date 2020-02-20
#
#############################################################################
"""
测试bitmap data type
"""
import os
import sys
import time
import random
sys.path.append("../../")
from lib import palo_config
from lib import palo_client
from lib import util
from lib import palo_job
from lib import common
import palo_logger
import palo_exception
client = None
#日志 异常 对象
LOG = palo_logger.Logger.getLogger()
L = palo_logger.StructedLogMessage
PaloClientException = palo_exception.PaloException
config = palo_config.config
compare = 'test_query_qa.test'
file_dir = os.path.abspath(os.path.join(os.path.dirname(__file__)))
broker_info = palo_config.broker_info
def setup_module():
"""
setUp
"""
global client
client = palo_client.get_client(config.fe_host, config.fe_query_port)
def wait_end(database_name):
"""
wait to finished
"""
ret = True
print('waitint for load...')
state = None
while ret:
job_list = client.get_load_job_list(database_name=database_name)
state = palo_job.LoadJob(job_list[-1]).get_state()
# print(state)
if state == "FINISHED" or state == "CANCELLED":
print(state)
ret = False
time.sleep(1)
assert state == "FINISHED"
def execute(line):
"""execte sql"""
print(line)
palo_result = client.execute(line)
print(palo_result)
return palo_result
def init(db_name, table_name):
"""
create db, table, bulk load, batch load
Args:
db_name:
table_name:
create_sql:
key_column:
Returns:
"""
line = 'DROP DATABASE IF EXISTS %s' % db_name
client.execute(line)
client.create_database(db_name)
client.use(db_name)
# client.execute('drop table if exists %s' % table_name)
line = 'CREATE TABLE %s (\
`id` int COMMENT "", \
`id1` tinyint COMMENT "", \
`c_float` float SUM COMMENT "", \
`bitmap_set1` bitmap bitmap_union COMMENT "", \
`bitmap_set2` bitmap bitmap_union COMMENT "", \
`bitmap_set3` bitmap bitmap_union COMMENT "" \
) ENGINE=OLAP \
DISTRIBUTED BY HASH(`id`, `id1`) BUCKETS 5 \
PROPERTIES ( \
"storage_type" = "COLUMN" \
);' % table_name
execute(line)
line = 'insert into %s select k4, k1, k9, bitmap_hash(k9), bitmap_hash(k9),' \
' bitmap_hash(k3) from %s' % (table_name, compare)
execute(line)
# wait_end(db_name)
def init_for_intersect(db_name, table_name):
"""
create db, table, bulk load, batch load
Args:
db_name:
table_name:
create_sql:
key_column:
Returns:
"""
line = 'DROP DATABASE IF EXISTS %s' % db_name
client.execute(line)
client.create_database(db_name)
client.use(db_name)
# client.execute('drop table if exists %s' % table_name)
line = """
CREATE TABLE `pv_bitmap` (
`dt` int(11) NULL COMMENT "",
`page` varchar(10) NULL COMMENT "",
`user_id_bitmap` bitmap BITMAP_UNION NULL COMMENT ""
) ENGINE=OLAP
AGGREGATE KEY(`dt`, `page`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`dt`) BUCKETS 2;
"""
execute(line)
line = """
CREATE TABLE `pv_base` (
`dt` int(11) NULL COMMENT "",
`page` varchar(10) NULL COMMENT "",
`user_id_str` varchar(40) NULL COMMENT ""
) ENGINE=OLAP
DUPLICATE KEY(`dt`, `page`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`dt`) BUCKETS 2;
"""
execute(line)
file_path = palo_config.gen_remote_file_path('sys/bitmap_load/bitmap_test.data')
set_list = ['user_id_bitmap = to_bitmap(id)']
column_name_list = ['dt', 'page', 'id']
data_desc_list = palo_client.LoadDataInfo(file_path,
'pv_bitmap', column_name_list=column_name_list, set_list=set_list)
assert client.batch_load(util.get_label(), data_desc_list, is_wait=True, broker=broker_info, max_filter_ratio=0.7)
column_name_list = ['dt', 'page', 'user_id_str']
data_desc_list = palo_client.LoadDataInfo(file_path,
'pv_base', column_name_list=column_name_list)
assert client.batch_load(util.get_label(), data_desc_list, is_wait=True, broker=broker_info, max_filter_ratio=0.7)
def test_bitmap_and():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_and",
"describe": "test_bitmap_and",
"tag": "function,p1"
}
"""
"""
test_bitmap_and
计算两个输入bitmap的交集,返回新的bitmap.
:return:
"""
line = "select bitmap_to_string(bitmap_and(to_bitmap(NULL), to_bitmap(2))) cnt"
r = execute(line)
assert r[0][0] == '', "expect:NULL, actual:%s" % r[0][0]
line = "select bitmap_to_string(bitmap_and(to_bitmap(NULL), bitmap_hash(2))) cnt"
r = execute(line)
assert r[0][0] == '', "expect:NULL, actual:%s" % r[0][0]
line = "select bitmap_count(bitmap_and(to_bitmap(NULL), bitmap_hash(2))) cnt"
r = execute(line)
assert r[0][0] == 0, "expect:0, actual:%s" % r[0][0]
line = "select bitmap_count(bitmap_and(to_bitmap(NULL), bitmap_hash(NULL))) cnt"
r = execute(line)
assert r[0][0] == 0, "expect:0, actual:%s" % r[0][0]
db_name, table_name, invalied_name_1 = util.gen_name_list()
init(db_name, table_name)
client.use(db_name)
line1 = 'select bitmap_union_count(bitmap_and(bitmap_set1,bitmap_set2)) from {0}'.format(table_name)
ret1 = execute(line1)
line2 = 'select count(distinct(bitmap_to_string(bitmap_set1))) from {0}' \
' where bitmap_to_string(bitmap_set1)=bitmap_to_string(bitmap_set2)'.format(table_name)
ret2 = execute(line2)
assert ret1 == ret2
line1 = 'select bitmap_union_count(bitmap_and(bitmap_set1,bitmap_set3)) from {0}'.format(table_name)
ret1 = execute(line1)
line2 = 'select count(distinct(bitmap_to_string(bitmap_set1))) from {0}' \
' where bitmap_to_string(bitmap_set1)=bitmap_to_string(bitmap_set3)'.format(table_name)
ret2 = execute(line2)
assert ret1 == ret2
client.clean(db_name)
def test_bitmap_empty():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_empty",
"describe": "test_bitmap_empty",
"tag": "function,p1"
}
"""
"""
test_bitmap_empty
返回一个空bitmap
:return:
"""
line = "select bitmap_count(bitmap_empty());"
r = execute(line)
assert r[0][0] == 0
line = "select bitmap_to_string(bitmap_empty());"
r = execute(line)
assert r[0][0] == ''
# todo 增加load的case
def test_bitmap_or():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_or",
"describe": "test_bitmap_or",
"tag": "function,p1"
}
"""
"""
test_bitmap_or
计算两个输入bitmap的并集,返回新的bitmap.
:return:
"""
# line = "select bitmap_to_string(bitmap_or(to_bitmap(NULL), to_bitmap(2))) cnt"
# r = execute(line)
# assert r[0][0] == '2'
# line = "select bitmap_to_string(bitmap_or(to_bitmap(NULL), bitmap_hash(2))) cnt"
# r = execute(line)
# assert r[0][0] == '2'
# todo 目前会出core
# line = "select bitmap_count(bitmap_and(to_bitmap(NULL), bitmap_hash(2))) cnt"
# r = execute(line)
# assert r[0][0] == 0
# line = "select bitmap_count(bitmap_or(to_bitmap(NULL), bitmap_hash(NULL))) cnt"
# r = execute(line)
# assert r[0][0] == 0
db_name, table_name, invalied_name_1 = util.gen_name_list()
init(db_name, table_name)
client.use(db_name)
line1 = 'select bitmap_union_count(bitmap_or(bitmap_set1,bitmap_set2)) from {0}'.format(table_name)
ret1 = execute(line1)
line2 = """with tmp as (
select bitmap_to_string(bitmap_set1) as a from {0}
union select bitmap_to_string(bitmap_set2) as a from {0})
select count(distinct(a)) from tmp limit 1""".format(table_name)
ret2 = execute(line2)
assert ret1 == ret2
line1 = 'select bitmap_union_count(bitmap_or(bitmap_set1,bitmap_set3)) from {0}'.format(table_name)
ret1 = execute(line1)
line2 = """with tmp as (
select bitmap_to_string(bitmap_set1) as a from {0}
union select bitmap_to_string(bitmap_set3) as a from {0})
select count(distinct(a)) from tmp limit 1""".format(table_name)
ret2 = execute(line2)
assert ret1 == ret2
client.clean(db_name)
def test_bitmap_count_udaf():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_count_udaf",
"describe": "test_bitmap_count_udaf",
"tag": "function,p1"
}
"""
"""test_bitmap_count_udaf"""
db_name, table_name, invalied_name_1 = util.gen_name_list()
init_for_intersect(db_name, table_name)
user_id_str_limit = 'user_id_str>0 and user_id_str<18446744073709551615'
line = 'select dt,page,bitmap_union_count(user_id_bitmap) from pv_bitmap group by dt,page order by dt,page'
ret1 = client.execute(line)
line = 'select dt,page,bitmap_count(bitmap_union(user_id_bitmap)) from pv_bitmap group by dt,page order by dt,page'
ret2 = client.execute(line)
line = 'select dt,page,count(user_id_str) from pv_base where {0}' \
' group by dt,page order by dt,page'.format(user_id_str_limit)
ret3 = client.execute(line)
assert ret1 == ret2
assert ret1 == ret3
line = 'select dt,bitmap_union_count(user_id_bitmap) from pv_bitmap group by dt order by dt'
ret1 = client.execute(line)
line = 'select dt,bitmap_count(bitmap_union(user_id_bitmap)) from pv_bitmap group by dt order by dt'
ret2 = client.execute(line)
line = 'select dt,count(user_id_str) from pv_base where {0}' \
' group by dt order by dt'.format(user_id_str_limit)
ret3 = client.execute(line)
assert ret1 == ret2
assert ret1 == ret3
line = 'select dt,page,bitmap_union_count(user_id_bitmap) from pv_bitmap group by dt,page order by dt,page'
ret1 = client.execute(line)
line = 'select dt,page,bitmap_count(bitmap_union(user_id_bitmap)) from pv_bitmap' \
' group by dt,page order by dt,page'
ret2 = client.execute(line)
line = 'select dt,page,count(user_id_str) from pv_base where {0}' \
' group by dt,page order by dt,page'.format(user_id_str_limit)
ret3 = client.execute(line)
assert ret1 == ret2
assert ret1 == ret3
client.clean(db_name)
def test_bitmap_count_udaf2():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_count_udaf2",
"describe": "test_bitmap_count_udaf2",
"tag": "function,p1"
}
"""
"""test_bitmap_count_udaf2"""
db_name, table_name, invalied_name_1 = util.gen_name_list()
line = 'DROP DATABASE IF EXISTS %s' % db_name
client.execute(line)
client.create_database(db_name)
client.use(db_name)
line = 'CREATE TABLE test_bitmap_tb ( id int COMMENT "", id1 tinyint COMMENT "", c_float float SUM COMMENT "",' \
' bitmap_set bitmap BITMAP_UNION COMMENT "" ) ENGINE=OLAP' \
' DISTRIBUTED BY HASH(id, id1) BUCKETS 5 PROPERTIES ( "storage_type" = "COLUMN" );'
client.execute(line)
line = 'insert into test_bitmap_tb select k3, k1, k9, TO_BITMAP(k3) from test_query_qa.baseall;'
client.execute(line)
line = 'select id,id1,c_float,bitmap_count(bitmap_set) from test_bitmap_tb where id=2147483647;'
ret2 = client.execute(line)
for i in range(len(ret2)):
assert ret2[i][3] == 1
line = 'select id,id1,c_float,bitmap_count(bitmap_set) from test_bitmap_tb where id=-2147483647;'
ret3 = client.execute(line)
# "to_bitmap(-2147483647) should be 0"
for i in range(len(ret2)):
assert ret3[i][3] == 0, 'expect: 0, actural: %s' % ret3[i][3]
client.clean(db_name)
def test_intersect_count_udaf():
"""
{
"title": "test_sys_bitmap_function.test_intersect_count_udaf",
"describe": "test_intersect_count_udaf",
"tag": "function,p1"
}
"""
"""test_intersect_count_udaf"""
db_name, table_name, invalied_name_1 = util.gen_name_list()
init_for_intersect(db_name, table_name)
user_id_str_limit = 'user_id_str>0 and user_id_str<18446744073709551615'
date_list = ['20191001', '20191002', '20191003', '20191004', '20191005']
d1, d2, d3 = random.sample(date_list, 3)
line = """
select intersect_count(user_id_bitmap, dt, '{0}') as first_day,
intersect_count(user_id_bitmap, dt, '{1}') as second_day,
intersect_count(user_id_bitmap, dt, '{2}') as third_day
from pv_bitmap
where dt in ('{0}', '{1}', '{2}');
""".format(d1, d2, d3)
ret1 = client.execute(line)
line = """
with t1 as (select count(distinct(user_id_str)) from pv_base where dt='{0}' and {3}),
t2 as (select count(distinct(user_id_str)) from pv_base where dt='{1}' and {3}),
t3 as (select count(distinct(user_id_str)) from pv_base where dt='{2}' and {3})
select * from t1, t2, t3;
""".format(d1, d2, d3, user_id_str_limit)
ret2 = client.execute(line)
assert ret1 == ret2
line = """
select intersect_count(user_id_bitmap, dt, '{0}', '{1}', '{2}') as retention
from pv_bitmap
where dt in ('{0}', '{1}', '{2}');
""".format(d1, d2, d3)
ret1 = client.execute(line)
line = """
with t1 as (select distinct(user_id_str) from pv_base where dt='{0}' and {3}),
t2 as (select distinct(user_id_str) from pv_base where dt='{1}' and {3}),
t3 as (select distinct(user_id_str) from pv_base where dt='{2}' and {3})
select count(t1.user_id_str)
from t1 join t2 join t3
on t1.user_id_str = t2.user_id_str and t1.user_id_str = t3.user_id_str;
""".format(d1, d2, d3, user_id_str_limit)
ret2 = client.execute(line)
assert ret1 == ret2
client.clean(db_name)
def test_bitmap_with_rollup():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_with_rollup",
"describe": "test_bitmap_with_rollup",
"tag": "function,p1"
}
"""
"""test_bitmap_with_rollup"""
db_name, table_name, invalied_name_1 = util.gen_name_list()
init_for_intersect(db_name, table_name)
tb = 'pv_bitmap'
rollup_table_name = tb + '_index'
column_name_list = ['dt', 'user_id_bitmap']
client.create_rollup_table(tb, rollup_table_name, column_name_list, is_wait=True)
time.sleep(5)
line = 'select dt, bitmap_union_count(user_id_bitmap) from %s group by dt' % (tb,)
rolllup = common.get_explain_rollup(client, line)
assert rollup_table_name in rolllup, 'expect %s, actural %s' % (rollup_table_name, rolllup)
client.clean(db_name)
def test_bitmap_contains():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_contains",
"describe": "判断一个bitmap中是否包含指定的数值",
"tag": "function,p1"
}
"""
line = "select bitmap_contains(to_bitmap(4), 1), bitmap_contains(to_bitmap(1), 1), \
bitmap_contains(to_bitmap(0), 0), bitmap_contains(to_bitmap(0), 1)"
ret = execute(line)
assert ret == ((0, 1, 1, 0), )
line = "select bitmap_contains(bitmap_from_string('4, 5, 6, 7'), 4), \
bitmap_contains(bitmap_from_string('4, 5, 6, 7'), 8), \
bitmap_contains(bitmap_from_string('4, 4, 4, 4'), 4), \
bitmap_contains(bitmap_from_string('423, 5'), 4), \
bitmap_contains(bitmap_from_string('4, 00'), 0)"
ret = execute(line)
assert ret == ((1, 0, 1, 0, 1), )
line = " select bitmap_contains(to_bitmap(9), 964291337)"
assert execute(line) == ((0, ), )
def test_bitmap_from_string():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_from_string",
"describe": "判断一个bitmap中是否包含指定的数值",
"tag": "function,p1"
}
"""
line = "select bitmap_to_string(bitmap_from_string('4, 5, 6')), bitmap_to_string(bitmap_from_string('0'))"
ret = execute(line)
assert ret == (('4,5,6', '0'), )
line = "select bitmap_to_string(bitmap_from_string('abc')), bitmap_to_string(bitmap_from_string(NULL))"
ret = execute(line)
assert ret == ((None, None), )
def test_bitmap_has_any():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_has_any",
"describe": "判断两个bitmap是否有交集",
"tag": "function,p1"
}
"""
line = "select bitmap_has_any(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_has_any(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((0, ), )
line = "select bitmap_has_any(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3'))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_has_any(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3,4,5,6'))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_has_any(bitmap_from_string('1'), bitmap_from_string('1,1,1'))"
ret = execute(line)
assert ret == ((1, ), )
def test_bitmap_not():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_not",
"describe": "第一个bitmap减去第二个bitmap",
"tag": "function,p1"
}
"""
line = "select bitmap_to_string(bitmap_not(bitmap_from_string('1,2,3,4'), bitmap_from_string('4')))"
ret = execute(line)
assert ret == (('1,2,3', ), )
line = "select bitmap_to_string(bitmap_not(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6')))"
ret = execute(line)
assert ret == (('1,2,3', ), )
line = "select bitmap_to_string(bitmap_not(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6')))"
ret = execute(line)
assert ret == (('1,2,3', ), )
line = "select bitmap_to_string(bitmap_not(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3')))"
ret = execute(line)
assert ret == (('', ), )
line = "select bitmap_to_string(bitmap_not(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3,4,5,6')))"
ret = execute(line)
assert ret == (('', ), )
line = "select bitmap_to_string(bitmap_not(bitmap_from_string('1'), bitmap_from_string('1,1,1')))"
ret = execute(line)
assert ret == (('', ), )
db_name, table_name, invalied_name_1 = util.gen_name_list()
init(db_name, table_name)
client.use(db_name)
line1 = 'select bitmap_union_count(bitmap_not(bitmap_set1,bitmap_set2)) from %s' % table_name
ret1 = execute(line1)
line2 = 'select count(distinct(bitmap_to_string(bitmap_set1))) - bitmap_union_count(bitmap_and(bitmap_set1, \
bitmap_set2)) from %s' % table_name
ret2 = execute(line2)
assert ret1 == ret2, "bitmap_not function failed"
client.clean(db_name)
def test_bitmap_xor():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_xor",
"describe": "返回两个bitmap异或的结果",
"tag": "function,p1"
}
"""
line = "select bitmap_to_string(bitmap_xor(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6')))"
ret = execute(line)
assert ret == (('1,2,3,5,6', ), )
line = "select bitmap_to_string(bitmap_xor(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6')))"
ret = execute(line)
assert ret == (('1,2,3,4,5,6', ), )
line = "select bitmap_to_string(bitmap_xor(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3')))"
ret = execute(line)
assert ret == (('', ), )
line = "select bitmap_to_string(bitmap_xor(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3,4,5,6')))"
ret = execute(line)
assert ret == (('4,5,6', ), )
line = "select bitmap_to_string(bitmap_xor(bitmap_from_string('1'), bitmap_from_string('1,1,1')))"
ret = execute(line)
assert ret == (('', ), )
def test_bitmap_min():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_min",
"describe": "计算并返回bitmap中的最小值",
"tag": "function,p1"
}
"""
line = "select bitmap_min(bitmap_from_string(''))"
ret = execute(line)
assert ret == ((None, ), )
line = "select bitmap_min(bitmap_from_string('10,99,100,1000,1,9999'))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_min(bitmap_from_string('9,9,9,9,10,9'))"
ret = execute(line)
assert ret == ((9, ), )
def test_bitmap_union_int():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_union_int",
"describe": "计算整型列的去重值",
"tag": "function,p1"
}
"""
db_name, table_name, invalied_name_1 = util.gen_name_list()
init_for_intersect(db_name, table_name)
user_id_str_limit = 'user_id_str>0 and user_id_str<18446744073709551615'
line = 'select bitmap_union_int(cast(user_id_str as int)) from pv_base where %s group by dt,page order by dt,page' \
% user_id_str_limit
ret1 = client.execute(line)
line = 'select count(distinct cast(user_id_str as int)) from pv_base where %s group by dt,page order by dt,page' \
% user_id_str_limit
ret2 = client.execute(line)
assert ret1 == ret2
client.clean(db_name)
def test_bitmap_union():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_union",
"describe": "返回一组bitmap的并集",
"tag": "function,p1"
}
"""
db_name, table_name, invalied_name_1 = util.gen_name_list()
init_for_intersect(db_name, table_name)
user_id_str_limit = 'user_id_str>0 and user_id_str<18446744073709551615'
line = 'select bitmap_count(bitmap_union(user_id_bitmap)) from pv_bitmap group by dt,page order by dt,page'
ret1 = client.execute(line)
line = 'select count(distinct user_id_str) from pv_base where %s group by dt,page order by dt,page' \
% user_id_str_limit
ret2 = client.execute(line)
assert ret1 == ret2
client.clean(db_name)
def test_bitmap_intersect():
"""
{
"title": "test_sys_bitmap_function.test_bitmap_intersect",
"describe": "返回一组bitmap的交集",
"tag": "function,p1"
}
"""
db_name, table_name, invalied_name_1 = util.gen_name_list()
init_for_intersect(db_name, table_name)
user_id_str_limit = 'user_id_str>0 and user_id_str<18446744073709551615'
line = 'select count(*) from (select user_id_str from pv_base where dt="20191003" and page="baidu" \
union all select user_id_str from pv_base where dt="20191001" and page="baidu") a where %s' \
% user_id_str_limit
ret1 = client.execute(line)
line = 'select bitmap_count(bitmap_intersect(x)) from (select bitmap_union(user_id_bitmap) x from pv_bitmap \
where dt in ("20191003", "20191001") and page="baidu") a'
ret2 = client.execute(line)
assert ret1 == ret2
client.clean(db_name)
def test_bitmap_or_count():
"""
{
"title": "test_sys_bitmap_function:test_bitmap_or_count",
"describe": "计算两个输入bitmap的并集:返回并集的个数",
"tag": "function,p1"
}
"""
line = "select bitmap_or_count(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((6, ), )
line = "select bitmap_or_count(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((6, ), )
line = "select bitmap_or_count(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3'))"
ret = execute(line)
assert ret == ((3, ), )
line = "select bitmap_or_count(bitmap_from_string(''), bitmap_from_string('1,2,3,4,5,6'))"
ret = execute(line)
assert ret == ((6, ), )
line = "select bitmap_or_count(bitmap_from_string('1,1'), bitmap_from_string('1,1,1,1,1,1,1'))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_or_count(bitmap_from_string('101,110,100,1110'), bitmap_from_string('1,0,110,101,11111'))"
ret = execute(line)
assert ret == ((7, ), )
def test_bitmap_and_count():
"""
{
"title": "test_sys_bitmap_function:test_bitmap_and_count",
"describe": "计算两个输入bitmap的交集,返回交集的个数",
"tag": "function,p1"
}
"""
line = "select bitmap_and_count(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_and_count(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((0, ), )
line = "select bitmap_and_count(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3'))"
ret = execute(line)
assert ret == ((3, ), )
line = "select bitmap_and_count(bitmap_from_string(''), bitmap_from_string('1,2,3,4,5,6'))"
ret = execute(line)
assert ret == ((0, ), )
line = "select bitmap_and_count(bitmap_from_string('1,1'), bitmap_from_string('1,1,1,1,1,1,1'))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_and_count(bitmap_from_string('101,110,100,1110'), bitmap_from_string('1,0,110,101,11111'))"
ret = execute(line)
assert ret == ((2, ), )
def test_bitmap_xor_count():
"""
{
"title": "test_sys_bitmap_function:test_bitmap_xor_count",
"describe": "将两个bitmap进行异或操作,返回交集的个数",
"tag": "function,p1"
}
"""
line = "select bitmap_xor_count(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((5, ), )
line = "select bitmap_xor_count(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((6, ), )
line = "select bitmap_xor_count(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3'))"
ret = execute(line)
assert ret == ((0, ), )
line = "select bitmap_xor_count(bitmap_from_string(''), bitmap_from_string('1,2,3,4,5,6'))"
ret = execute(line)
assert ret == ((6, ), )
line = "select bitmap_xor_count(bitmap_from_string('1,1'), bitmap_from_string('1,1,1,1,1,1,1'))"
ret = execute(line)
assert ret == ((0, ), )
line = "select bitmap_xor_count(bitmap_from_string('101,110,100,1110'), bitmap_from_string('1,0,110,101,11111'))"
ret = execute(line)
assert ret == ((5, ), )
def test_bitmap_and_not():
"""
{
"title": "test_sys_bitmap_function:test_bitmap_and_not",
"describe": "将两个bitmap进行与非操作并返回计算结果",
"tag": "function,p1"
}
"""
line_1 = "select bitmap_to_string(bitmap_and_not(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6')))"
ret_1 = execute(line_1)
line_2 = "select bitmap_to_string(bitmap_not(bitmap_from_string('1,2,3,4'), \
bitmap_and(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))))"
ret_2 = execute(line_2)
assert ret_1 == ret_2
line_1 = "select bitmap_to_string(bitmap_and_not(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3')))"
ret_1 = execute(line_1)
line_2 = "select bitmap_to_string(bitmap_not(bitmap_from_string('1,2,3'), bitmap_and(bitmap_from_string('1,2,3'),\
bitmap_from_string('1,2,3'))))"
ret_2 = execute(line_2)
assert ret_1 == ret_2
line_1 = "select bitmap_to_string(bitmap_and_not(bitmap_from_string(''), bitmap_from_string('1,2,3,4,5,6')))"
ret_1 = execute(line_1)
line_2 = "select bitmap_to_string(bitmap_not(bitmap_from_string(''), bitmap_and(bitmap_from_string(''), \
bitmap_from_string('1,2,3,4,5,6'))))"
ret_2 = execute(line_2)
assert ret_1 == ret_2
line_1 = "select bitmap_to_string(bitmap_and_not(bitmap_from_string('1,1'), bitmap_from_string('1,1,1,1,1,1,1')))"
ret_1 = execute(line_1)
line_2 = "select bitmap_to_string(bitmap_not(bitmap_from_string('1,1'), bitmap_and(bitmap_from_string('1,1'), \
bitmap_from_string('1,1,1,1,1,1,1'))))"
ret_2 = execute(line_2)
assert ret_1 == ret_2
line_1 = "select bitmap_to_string(bitmap_and_not(bitmap_from_string('101,110,100,1110'), \
bitmap_from_string('1,0,110,101,11111')))"
ret_1 = execute(line_1)
line_2 = "select bitmap_to_string(bitmap_not(bitmap_from_string('101,110,100,1110'), \
bitmap_and(bitmap_from_string('101,110,100,1110'), bitmap_from_string('1,0,110,101,11111'))))"
ret_2 = execute(line_2)
assert ret_1 == ret_2
def test_bitmap_and_not_count():
"""
{
"title": "test_sys_bitmap_function:test_bitmap_and_not_count",
"describe": "将两个bitmap进行与非操作并返回计算返回的大小",
"tag": "function,p1"
}
"""
line = "select bitmap_and_not_count(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((3, ), )
line = "select bitmap_and_not_count(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((3, ), )
line = "select bitmap_and_not_count(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3'))"
ret = execute(line)
assert ret == ((0, ), )
line = "select bitmap_and_not_count(bitmap_from_string(''), bitmap_from_string('1,2,3,4,5,6'))"
ret = execute(line)
assert ret == ((0, ), )
line = "select bitmap_and_not_count(bitmap_from_string('1,1'), bitmap_from_string('1,1,1,1,1,1,1'))"
ret = execute(line)
assert ret == ((0, ), )
line = "select bitmap_and_not_count(bitmap_from_string('101,110,100,1110'), \
bitmap_from_string('1,0,110,101,11111'))"
ret = execute(line)
assert ret == ((2, ), )
def test_bitmap_has_all():
"""
{
"title": "test_sys_bitmap_function:test_bitmap_has_all",
"describe": "如果第一个bitmap包含第二个bitmap的全部元素,则返回true。如果第二个bitmap包含的元素为空,返回true",
"tag": "function,p1"
}
"""
line = "select bitmap_has_all(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((0, ), )
line = "select bitmap_has_all(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6'))"
ret = execute(line)
assert ret == ((0, ), )
line = "select bitmap_has_all(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3'))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_has_all(bitmap_from_string('1,2,3,4,5,6'), bitmap_from_string(''))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_has_all(bitmap_from_string('1,2,3,4,5,6'), bitmap_from_string('6,5,4,3,2'))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_has_all(bitmap_from_string('1,1'), bitmap_from_string('1,1,1,1,1,1,1'))"
ret = execute(line)
assert ret == ((1, ), )
line = "select bitmap_has_all(bitmap_from_string(''), bitmap_from_string(''))"
ret = execute(line)
assert ret == ((1, ), )
def test_bitmap_max():
"""
{
"title": "test_sys_bitmap_function:test_bitmap_max",
"describe": "计算并返回bitmap中的最大值",
"tag": "function,p1"
}
"""
line = "select bitmap_max(bitmap_from_string(''))"
ret = execute(line)
assert ret == ((None, ), )
line = "select bitmap_max(bitmap_from_string('10,99,100,1000,1,9999'))"
ret = execute(line)
assert ret == ((9999, ), )
line = "select bitmap_max(bitmap_from_string('9,9,9,9,8,9'))"
ret = execute(line)
assert ret == ((9, ), )
line = "select bitmap_max(bitmap_from_string('3,3,3,3,3,3,3,3,3'))"
ret = execute(line)
assert ret == ((3, ), )
def test_bitmap_subset_in_range():
"""
{
"title": "test_sys_bitmap_function:test_bitmap_subset_in_range",
"describe": "返回bitmap指定范围内的子集",
"tag": "function,p1"
}
"""
line = "select bitmap_to_string(bitmap_subset_in_range(bitmap_from_string(''), 1, 10))"
ret = execute(line)
assert ret == (('', ), )
line = "select bitmap_to_string(bitmap_subset_in_range(bitmap_from_string('1,2,3,4,5'), 1, 5))"
ret = execute(line)
assert ret == (('1,2,3,4', ), )
line = "select bitmap_to_string(bitmap_subset_in_range(bitmap_from_string('1,2,3,4,5'), 2, 6))"
ret = execute(line)
assert ret == (('2,3,4,5', ), )
line = "select bitmap_to_string(bitmap_subset_in_range(bitmap_from_string('1,2,3,4,5'), 6, 10))"
ret = execute(line)
assert ret == (('', ), )
line = "select bitmap_to_string(bitmap_subset_in_range(bitmap_from_string('1,2,3,4,5'), 6, 1))"
ret = execute(line)
assert ret == ((None, ), )
line = "select bitmap_to_string(bitmap_subset_in_range(bitmap_from_string('1,2,3,4,5'), 5, 5))"
ret = execute(line)
assert ret == ((None, ), )
line = "select bitmap_to_string(bitmap_subset_in_range(bitmap_from_string('1,2,3,4,5,5,3,2,7,8,2,5'), 2, 6))"
ret = execute(line)
assert ret == (('2,3,4,5', ), )
def test_bitmap_subset_limit():
"""
{
"title": "test_sys_bitmap_function:test_bitmap_subset_limit",
"describe": "生成子bitmap",
"tag": "function,p1"
}
"""
line = "select bitmap_to_string(bitmap_subset_limit(bitmap_from_string(''), 1, 10))"
ret = execute(line)
assert ret == (('', ), )
line = "select bitmap_to_string(bitmap_subset_limit(bitmap_from_string('1,2,3,4,5'), 1, 4))"
ret = execute(line)
assert ret == (('1,2,3,4', ), )
line = "select bitmap_to_string(bitmap_subset_limit(bitmap_from_string('1,2,3,4,5'), 2, 6))"
ret = execute(line)
assert ret == (('2,3,4,5', ), )
line = "select bitmap_to_string(bitmap_subset_limit(bitmap_from_string('1,2,3,4,5'), 6, 10))"
ret = execute(line)
assert ret == (('', ), )
line = "select bitmap_to_string(bitmap_subset_limit(bitmap_from_string('8,8,9,10,6,7'), 3, 3))"
ret = execute(line)
assert ret == (('6,7,8', ), )
line = "select bitmap_to_string(bitmap_subset_limit(bitmap_from_string('1,2,3,4,5'), 5, 5))"
ret = execute(line)
assert ret == (('5', ), )
line = "select bitmap_to_string(bitmap_subset_limit(bitmap_from_string('1,2,3,4,5,5,3,2,7,8,2,5'), 2, 6))"
ret = execute(line)
assert ret == (('2,3,4,5,7,8', ), )
def test_sub_bitmap():
"""
{
"title": "test_sys_bitmap_function:test_sub_bitmap",
"describe": "截取bitmap,返回子集",
"tag": "function,p1"
}
"""
line = "select bitmap_to_string(sub_bitmap(bitmap_from_string(''), 1, 10))"
ret = execute(line)
assert ret == ((None, ), )
line = "select bitmap_to_string(sub_bitmap(bitmap_from_string('1,2,3,4,5'), 1, 4))"
ret = execute(line)
assert ret == (('2,3,4,5', ), )
line = "select bitmap_to_string(sub_bitmap(bitmap_from_string('1,0,1,2,3,1,5'), 0, 3))"
ret = execute(line)
assert ret == (('0,1,2', ), )
line = "select bitmap_to_string(sub_bitmap(bitmap_from_string('1,0,1,2,3,1,5'), -3, 2))"
ret = execute(line)
assert ret == (('2,3', ), )
line = "select bitmap_to_string(sub_bitmap(bitmap_from_string('1,0,1,2,3,1,5'), 2, 100))"
ret = execute(line)
assert ret == (('2,3,5', ), )
line = "select bitmap_to_string(sub_bitmap(bitmap_from_string('2,1,5,4,3'), 2, 2))"
ret = execute(line)
assert ret == (('3,4', ), )
line = "select bitmap_to_string(sub_bitmap(bitmap_from_string('6,4,5,2,6,7,8,1,2,3'), 2, 6))"
ret = execute(line)
assert ret == (('3,4,5,6,7,8', ), )
if __name__ == '__main__':
setup_module()
tb = 'test_sys_bitmap_function_test_bitmap_with_rollup_db.pv_bitmap'
line = 'select dt, bitmap_union_count(user_id_bitmap) from %s group by dt' % (tb,)
rollup = common.get_explain_rollup(client, line)
print(rollup)
# test_sc_add_bitmap_column()
# test_sc_drop_bitmap_column()
# test_sc_modified_bitmap_column()