forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_bitmap.cpp
More file actions
1311 lines (1179 loc) · 53.2 KB
/
Copy pathfunction_bitmap.cpp
File metadata and controls
1311 lines (1179 loc) · 53.2 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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
// This file is copied from
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/FunctionBitmap.h
// and modified by Doris
#include <absl/strings/numbers.h>
#include <absl/strings/str_split.h>
#include <glog/logging.h>
#include <stdint.h>
#include <string.h>
#include <algorithm>
#include <boost/iterator/iterator_facade.hpp>
#include <functional>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
#include <vector>
#include "common/compiler_util.h" // IWYU pragma: keep
#include "common/status.h"
#include "core/assert_cast.h"
#include "core/block/block.h"
#include "core/block/column_numbers.h"
#include "core/block/column_with_type_and_name.h"
#include "core/column/column.h"
#include "core/column/column_array.h"
#include "core/column/column_complex.h"
#include "core/column/column_const.h"
#include "core/column/column_nullable.h"
#include "core/column/column_string.h"
#include "core/column/column_vector.h"
#include "core/data_type/data_type.h"
#include "core/data_type/data_type_array.h"
#include "core/data_type/data_type_bitmap.h"
#include "core/data_type/data_type_nullable.h"
#include "core/data_type/data_type_number.h"
#include "core/data_type/data_type_string.h"
#include "core/field.h"
#include "core/types.h"
#include "core/value/bitmap_value.h"
#include "exec/common/stringop_substring.h"
#include "exec/common/util.hpp"
#include "exprs/aggregate/aggregate_function.h"
#include "exprs/function/function.h"
#include "exprs/function/function_always_not_nullable.h"
#include "exprs/function/function_bitmap_min_or_max.h"
#include "exprs/function/function_const.h"
#include "exprs/function/function_helpers.h"
#include "exprs/function/function_totype.h"
#include "exprs/function/simple_function_factory.h"
#include "util/hash/murmur_hash3.h"
#include "util/hash_util.hpp"
#include "util/string_parser.hpp"
#include "util/url_coding.h"
namespace doris {
class FunctionContext;
} // namespace doris
namespace doris {
struct BitmapEmpty {
static constexpr auto name = "bitmap_empty";
using ReturnColVec = ColumnBitmap;
static DataTypePtr get_return_type() { return std::make_shared<DataTypeBitMap>(); }
static auto init_value() { return BitmapValue {}; }
};
struct ToBitmap {
static constexpr auto name = "to_bitmap";
using ReturnType = DataTypeBitMap;
template <typename ColumnType>
static void vector(const ColumnType* col, MutableColumnPtr& col_res) {
execute<ColumnType, false>(col, nullptr, col_res);
}
template <typename ColumnType>
static void vector_nullable(const ColumnType* col, const NullMap& nullmap,
MutableColumnPtr& col_res) {
execute<ColumnType, true>(col, &nullmap, col_res);
}
template <typename ColumnType, bool arg_is_nullable>
static void execute(const ColumnType* col, const NullMap* nullmap, MutableColumnPtr& col_res) {
if constexpr (std::is_same_v<ColumnType, ColumnString>) {
const ColumnString::Chars& data = col->get_chars();
const ColumnString::Offsets& offsets = col->get_offsets();
auto* res_column = reinterpret_cast<ColumnBitmap*>(col_res.get());
auto& res_data = res_column->get_data();
size_t size = offsets.size();
for (size_t i = 0; i < size; ++i) {
if (arg_is_nullable && ((*nullmap)[i])) {
continue;
} else {
const char* raw_str = reinterpret_cast<const char*>(&data[offsets[i - 1]]);
int str_size = cast_set<int>(offsets[i] - offsets[i - 1]);
StringParser::ParseResult parse_result = StringParser::PARSE_SUCCESS;
uint64_t int_value = StringParser::string_to_unsigned_int<uint64_t>(
raw_str, str_size, &parse_result);
if (LIKELY(parse_result == StringParser::PARSE_SUCCESS)) {
res_data[i].add(int_value);
}
}
}
} else if constexpr (std::is_same_v<ColumnType, ColumnInt64>) {
auto* res_column = reinterpret_cast<ColumnBitmap*>(col_res.get());
auto& res_data = res_column->get_data();
size_t size = col->size();
for (size_t i = 0; i < size; ++i) {
if constexpr (arg_is_nullable) {
if ((*nullmap)[i]) {
continue;
}
}
if (auto value = col->get_data()[i]; value >= 0) {
res_data[i].add(value);
}
}
}
}
};
struct ToBitmapWithCheck {
static constexpr auto name = "to_bitmap_with_check";
using ReturnType = DataTypeBitMap;
template <typename ColumnType>
static Status vector(const ColumnType* col, MutableColumnPtr& col_res) {
return execute<ColumnType, false>(col, nullptr, col_res);
}
template <typename ColumnType>
static Status vector_nullable(const ColumnType* col, const NullMap& nullmap,
MutableColumnPtr& col_res) {
return execute<ColumnType, true>(col, &nullmap, col_res);
}
template <typename ColumnType, bool arg_is_nullable>
static Status execute(const ColumnType* col, const NullMap* nullmap,
MutableColumnPtr& col_res) {
if constexpr (std::is_same_v<ColumnType, ColumnString>) {
const ColumnString::Chars& data = col->get_chars();
const ColumnString::Offsets& offsets = col->get_offsets();
auto* res_column = reinterpret_cast<ColumnBitmap*>(col_res.get());
auto& res_data = res_column->get_data();
size_t size = offsets.size();
for (size_t i = 0; i < size; ++i) {
if (arg_is_nullable && ((*nullmap)[i])) {
continue;
} else {
const char* raw_str = reinterpret_cast<const char*>(&data[offsets[i - 1]]);
// The string lenght is less than 2G, so that cast the str size to int, not use size_t
int str_size = cast_set<int>(offsets[i] - offsets[i - 1]);
StringParser::ParseResult parse_result = StringParser::PARSE_SUCCESS;
uint64_t int_value = StringParser::string_to_unsigned_int<uint64_t>(
raw_str, str_size, &parse_result);
if (LIKELY(parse_result == StringParser::PARSE_SUCCESS)) {
res_data[i].add(int_value);
} else {
return Status::InvalidArgument(
"The input: {} is not valid, to_bitmap only support bigint value "
"from 0 to 18446744073709551615 currently, cannot create MV with "
"to_bitmap on column with negative values or cannot load negative "
"values to column with to_bitmap MV on it.",
std::string(raw_str, str_size));
}
}
}
} else if constexpr (std::is_same_v<ColumnType, ColumnInt64>) {
auto* res_column = reinterpret_cast<ColumnBitmap*>(col_res.get());
auto& res_data = res_column->get_data();
size_t size = col->size();
for (size_t i = 0; i < size; ++i) {
if (arg_is_nullable && ((*nullmap)[i])) {
continue;
} else {
int64_t int_value = col->get_data()[i];
if (LIKELY(int_value >= 0)) {
res_data[i].add(int_value);
} else {
return Status::InvalidArgument(
"The input: {} is not valid, to_bitmap only support bigint value "
"from 0 to 18446744073709551615 currently, cannot create MV with "
"to_bitmap on column with negative values or cannot load negative "
"values to column with to_bitmap MV on it.",
int_value);
}
}
}
} else {
return Status::InvalidArgument("not support type");
}
return Status::OK();
}
};
struct BitmapFromString {
using ArgumentType = DataTypeString;
static constexpr auto name = "bitmap_from_string";
static Status vector(const ColumnString::Chars& data, const ColumnString::Offsets& offsets,
std::vector<BitmapValue>& res, NullMap& null_map,
size_t input_rows_count) {
res.reserve(input_rows_count);
std::vector<uint64_t> bits;
if (offsets.size() == 0 && input_rows_count == 1) {
// For NULL constant
res.emplace_back();
null_map[0] = 1;
return Status::OK();
}
auto split_and_parse = [&bits](const char* raw_str, size_t str_size) {
bits.clear();
auto res = absl::StrSplit(std::string_view {raw_str, str_size}, ",", absl::SkipEmpty());
uint64_t value = 0;
for (auto s : res) {
if (!absl::SimpleAtoi(s, &value)) {
return false;
}
bits.push_back(value);
}
return true;
};
// split by comma
for (size_t i = 0; i < input_rows_count; ++i) {
const char* raw_str = reinterpret_cast<const char*>(&data[offsets[i - 1]]);
int64_t str_size = offsets[i] - offsets[i - 1];
if ((str_size > INT32_MAX) || !split_and_parse(raw_str, str_size)) {
res.emplace_back();
null_map[i] = 1;
continue;
}
res.emplace_back(bits);
}
return Status::OK();
}
};
struct NameBitmapFromBase64 {
static constexpr auto name = "bitmap_from_base64";
};
struct BitmapFromBase64 {
using ArgumentType = DataTypeString;
static constexpr auto name = "bitmap_from_base64";
static Status vector(const ColumnString::Chars& data, const ColumnString::Offsets& offsets,
std::vector<BitmapValue>& res, NullMap& null_map,
size_t input_rows_count) {
res.reserve(input_rows_count);
if (offsets.size() == 0 && input_rows_count == 1) {
// For NULL constant
res.emplace_back();
null_map[0] = 1;
return Status::OK();
}
std::string decode_buff;
size_t last_decode_buff_len = 0;
size_t curr_decode_buff_len = 0;
for (size_t i = 0; i < input_rows_count; ++i) {
const char* src_str = reinterpret_cast<const char*>(&data[offsets[i - 1]]);
size_t src_size = offsets[i] - offsets[i - 1];
if (0 != src_size % 4) {
// return Status::InvalidArgument(
// fmt::format("invalid base64: {}", std::string(src_str, src_size)));
res.emplace_back();
null_map[i] = 1;
continue;
}
curr_decode_buff_len = src_size + 3;
if (curr_decode_buff_len > last_decode_buff_len) {
decode_buff.resize(curr_decode_buff_len);
last_decode_buff_len = curr_decode_buff_len;
}
auto outlen = base64_decode(src_str, src_size, decode_buff.data());
if (outlen < 0) {
res.emplace_back();
null_map[i] = 1;
} else {
BitmapValue bitmap_val;
if (!bitmap_val.deserialize(decode_buff.data())) {
return Status::RuntimeError("bitmap_from_base64 decode failed: base64: {}",
std::string(src_str, src_size));
}
res.emplace_back(std::move(bitmap_val));
}
}
return Status::OK();
}
};
struct BitmapFromArray {
using ArgumentType = DataTypeArray;
static constexpr auto name = "bitmap_from_array";
template <typename ColumnType>
static Status vector(const ColumnArray::Offsets64& offset_column_data,
const IColumn& nested_column, const NullMap& nested_null_map,
std::vector<BitmapValue>& res, NullMap& null_map) {
const auto& nested_column_data = static_cast<const ColumnType&>(nested_column).get_data();
auto size = offset_column_data.size();
res.reserve(size);
std::vector<uint64_t> bits;
for (size_t i = 0; i < size; ++i) {
auto curr_offset = offset_column_data[i];
auto prev_offset = offset_column_data[i - 1];
for (auto j = prev_offset; j < curr_offset; ++j) {
auto data = nested_column_data[j];
// invaild value
if (UNLIKELY(data < 0) || UNLIKELY(nested_null_map[j])) {
res.emplace_back();
null_map[i] = 1;
break;
} else {
bits.push_back(data);
}
}
//input is valid value
if (!null_map[i]) {
res.emplace_back(bits);
}
bits.clear();
}
return Status::OK();
}
};
template <typename Impl>
class FunctionBitmapAlwaysNull : public IFunction {
public:
static constexpr auto name = Impl::name;
String get_name() const override { return name; }
static FunctionPtr create() { return std::make_shared<FunctionBitmapAlwaysNull>(); }
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
return make_nullable(std::make_shared<DataTypeBitMap>());
}
size_t get_number_of_arguments() const override { return 1; }
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
auto res_null_map = ColumnUInt8::create(input_rows_count, 0);
auto res_data_column = ColumnBitmap::create();
auto& null_map = res_null_map->get_data();
auto& res = res_data_column->get_data();
ColumnPtr& argument_column = block.get_by_position(arguments[0]).column;
if constexpr (std::is_same_v<typename Impl::ArgumentType, DataTypeString>) {
const auto& str_column = static_cast<const ColumnString&>(*argument_column);
const ColumnString::Chars& data = str_column.get_chars();
const ColumnString::Offsets& offsets = str_column.get_offsets();
RETURN_IF_ERROR(Impl::vector(data, offsets, res, null_map, input_rows_count));
} else if constexpr (std::is_same_v<typename Impl::ArgumentType, DataTypeArray>) {
auto argument_type = remove_nullable(
assert_cast<const DataTypeArray&>(*block.get_by_position(arguments[0]).type)
.get_nested_type());
const auto& array_column = static_cast<const ColumnArray&>(*argument_column);
const auto& offset_column_data = array_column.get_offsets();
const auto& nested_nullable_column =
static_cast<const ColumnNullable&>(array_column.get_data());
const auto& nested_column = nested_nullable_column.get_nested_column();
const auto& nested_null_map = nested_nullable_column.get_null_map_column().get_data();
switch (argument_type->get_primitive_type()) {
case PrimitiveType::TYPE_TINYINT:
RETURN_IF_ERROR(Impl::template vector<ColumnInt8>(offset_column_data, nested_column,
nested_null_map, res, null_map));
break;
case PrimitiveType::TYPE_BOOLEAN:
RETURN_IF_ERROR(Impl::template vector<ColumnUInt8>(
offset_column_data, nested_column, nested_null_map, res, null_map));
break;
case PrimitiveType::TYPE_SMALLINT:
RETURN_IF_ERROR(Impl::template vector<ColumnInt16>(
offset_column_data, nested_column, nested_null_map, res, null_map));
break;
case PrimitiveType::TYPE_INT:
RETURN_IF_ERROR(Impl::template vector<ColumnInt32>(
offset_column_data, nested_column, nested_null_map, res, null_map));
break;
case PrimitiveType::TYPE_BIGINT:
RETURN_IF_ERROR(Impl::template vector<ColumnInt64>(
offset_column_data, nested_column, nested_null_map, res, null_map));
break;
default:
return Status::RuntimeError("Illegal column {} of argument of function {}",
block.get_by_position(arguments[0]).column->get_name(),
get_name());
}
} else {
return Status::RuntimeError("Illegal column {} of argument of function {}",
block.get_by_position(arguments[0]).column->get_name(),
get_name());
}
block.get_by_position(result).column =
ColumnNullable::create(std::move(res_data_column), std::move(res_null_map));
return Status::OK();
}
};
template <int HashBits>
struct BitmapHashName {};
template <>
struct BitmapHashName<32> {
static constexpr auto name = "bitmap_hash";
};
template <>
struct BitmapHashName<64> {
static constexpr auto name = "bitmap_hash64";
};
template <int HashBits>
struct BitmapHash {
static constexpr auto name = BitmapHashName<HashBits>::name;
using ReturnType = DataTypeBitMap;
template <typename ColumnType>
static void vector(const ColumnType* col, MutableColumnPtr& col_res) {
if constexpr (std::is_same_v<ColumnType, ColumnString>) {
const ColumnString::Chars& data = col->get_chars();
const ColumnString::Offsets& offsets = col->get_offsets();
auto* res_column = reinterpret_cast<ColumnBitmap*>(col_res.get());
auto& res_data = res_column->get_data();
size_t size = offsets.size();
for (size_t i = 0; i < size; ++i) {
const char* raw_str = reinterpret_cast<const char*>(&data[offsets[i - 1]]);
size_t str_size = offsets[i] - offsets[i - 1];
if constexpr (HashBits == 32) {
uint32_t hash_value =
HashUtil::murmur_hash3_32(raw_str, str_size, HashUtil::MURMUR3_32_SEED);
res_data[i].add(hash_value);
} else {
uint64_t hash_value = 0;
murmur_hash3_x64_64(raw_str, str_size, 0, &hash_value);
res_data[i].add(hash_value);
}
}
}
}
template <typename ColumnType>
static void vector_nullable(const ColumnType* col, const NullMap& nullmap,
MutableColumnPtr& col_res) {
if constexpr (std::is_same_v<ColumnType, ColumnString>) {
const ColumnString::Chars& data = col->get_chars();
const ColumnString::Offsets& offsets = col->get_offsets();
auto* res_column = reinterpret_cast<ColumnBitmap*>(col_res.get());
auto& res_data = res_column->get_data();
size_t size = offsets.size();
for (size_t i = 0; i < size; ++i) {
if (nullmap[i]) {
continue;
} else {
const char* raw_str = reinterpret_cast<const char*>(&data[offsets[i - 1]]);
size_t str_size = offsets[i] - offsets[i - 1];
if constexpr (HashBits == 32) {
uint32_t hash_value = HashUtil::murmur_hash3_32(raw_str, str_size,
HashUtil::MURMUR3_32_SEED);
res_data[i].add(hash_value);
} else {
uint64_t hash_value = 0;
murmur_hash3_x64_64(raw_str, str_size, 0, &hash_value);
res_data[i].add(hash_value);
}
}
}
}
}
};
class FunctionBitmapCount : public IFunction {
public:
static constexpr auto name = "bitmap_count";
String get_name() const override { return name; }
static FunctionPtr create() { return std::make_shared<FunctionBitmapCount>(); }
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
return std::make_shared<DataTypeInt64>();
}
size_t get_number_of_arguments() const override { return 1; }
bool use_default_implementation_for_nulls() const override { return false; }
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
auto res_data_column = ColumnInt64::create();
auto& res = res_data_column->get_data();
auto data_null_map = ColumnUInt8::create(input_rows_count, 0);
auto& null_map = data_null_map->get_data();
auto column = block.get_by_position(arguments[0]).column;
if (auto* nullable = check_and_get_column<const ColumnNullable>(*column)) {
VectorizedUtils::update_null_map(null_map, nullable->get_null_map_data());
column = nullable->get_nested_column_ptr();
}
auto str_col = assert_cast<const ColumnBitmap*>(column.get());
const auto& col_data = str_col->get_data();
res.reserve(input_rows_count);
for (size_t i = 0; i < input_rows_count; ++i) {
if (null_map[i]) {
res.push_back(0);
continue;
}
res.push_back(col_data[i].cardinality());
}
block.replace_by_position(result, std::move(res_data_column));
return Status::OK();
}
};
struct NameBitmapNot {
static constexpr auto name = "bitmap_not";
};
template <typename LeftDataType, typename RightDataType>
struct BitmapNot {
using ResultDataType = DataTypeBitMap;
using T0 = typename LeftDataType::FieldType;
using T1 = typename RightDataType::FieldType;
using TData = std::vector<BitmapValue>;
static void vector_vector(const TData& lvec, const TData& rvec, TData& res) {
size_t size = lvec.size();
for (size_t i = 0; i < size; ++i) {
res[i] = lvec[i];
res[i] -= rvec[i];
}
}
static void vector_scalar(const TData& lvec, const BitmapValue& rval, TData& res) {
size_t size = lvec.size();
for (size_t i = 0; i < size; ++i) {
res[i] = lvec[i];
res[i] -= rval;
}
}
static void scalar_vector(const BitmapValue& lval, const TData& rvec, TData& res) {
size_t size = rvec.size();
for (size_t i = 0; i < size; ++i) {
res[i] = lval;
res[i] -= rvec[i];
}
}
};
struct NameBitmapAndNot {
static constexpr auto name = "bitmap_and_not";
};
template <typename LeftDataType, typename RightDataType>
struct BitmapAndNot {
using ResultDataType = DataTypeBitMap;
using T0 = typename LeftDataType::FieldType;
using T1 = typename RightDataType::FieldType;
using TData = std::vector<BitmapValue>;
static void vector_vector(const TData& lvec, const TData& rvec, TData& res) {
size_t size = lvec.size();
BitmapValue mid_data;
for (size_t i = 0; i < size; ++i) {
mid_data = lvec[i];
mid_data &= rvec[i];
res[i] = lvec[i];
res[i] -= mid_data;
mid_data.reset();
}
}
static void vector_scalar(const TData& lvec, const BitmapValue& rval, TData& res) {
size_t size = lvec.size();
BitmapValue mid_data;
for (size_t i = 0; i < size; ++i) {
mid_data = lvec[i];
mid_data &= rval;
res[i] = lvec[i];
res[i] -= mid_data;
mid_data.reset();
}
}
static void scalar_vector(const BitmapValue& lval, const TData& rvec, TData& res) {
size_t size = rvec.size();
BitmapValue mid_data;
for (size_t i = 0; i < size; ++i) {
mid_data = lval;
mid_data &= rvec[i];
res[i] = lval;
res[i] -= mid_data;
mid_data.reset();
}
}
};
struct NameBitmapAndNotCount {
static constexpr auto name = "bitmap_and_not_count";
};
template <typename LeftDataType, typename RightDataType>
struct BitmapAndNotCount {
using ResultDataType = DataTypeInt64;
using T0 = typename LeftDataType::FieldType;
using T1 = typename RightDataType::FieldType;
using TData = std::vector<BitmapValue>;
using ResTData = typename ColumnInt64::Container::value_type;
static void vector_vector(const TData& lvec, const TData& rvec, ResTData* res) {
size_t size = lvec.size();
BitmapValue mid_data;
for (size_t i = 0; i < size; ++i) {
mid_data = lvec[i];
mid_data &= rvec[i];
res[i] = lvec[i].andnot_cardinality(mid_data);
mid_data.reset();
}
}
static void scalar_vector(const BitmapValue& lval, const TData& rvec, ResTData* res) {
size_t size = rvec.size();
BitmapValue mid_data;
for (size_t i = 0; i < size; ++i) {
mid_data = lval;
mid_data &= rvec[i];
res[i] = lval.andnot_cardinality(mid_data);
mid_data.reset();
}
}
static void vector_scalar(const TData& lvec, const BitmapValue& rval, ResTData* res) {
size_t size = lvec.size();
BitmapValue mid_data;
for (size_t i = 0; i < size; ++i) {
mid_data = lvec[i];
mid_data &= rval;
res[i] = lvec[i].andnot_cardinality(mid_data);
mid_data.reset();
}
}
};
void update_bitmap_op_count(int64_t* __restrict count, const NullMap& null_map) {
static constexpr int64_t flags[2] = {-1, 0};
size_t size = null_map.size();
auto* __restrict null_map_data = null_map.data();
for (size_t i = 0; i < size; ++i) {
count[i] &= flags[null_map_data[i]];
}
}
// for bitmap_and_count, bitmap_xor_count and bitmap_and_not_count,
// result is 0 for rows that if any column is null value
ColumnPtr handle_bitmap_op_count_null_value(ColumnPtr& src, const Block& block,
const ColumnNumbers& args, uint32_t result,
size_t input_rows_count) {
MutableColumnPtr mutable_src = IColumn::mutate(std::move(src));
auto* nullable = assert_cast<ColumnNullable*>(mutable_src.get());
auto* src_not_nullable_mutable = &nullable->get_nested_column();
auto* __restrict count_data =
assert_cast<ColumnInt64*>(src_not_nullable_mutable)->get_data().data();
for (const auto& arg : args) {
const ColumnWithTypeAndName& elem = block.get_by_position(arg);
if (!elem.type->is_nullable()) {
continue;
}
bool is_const = is_column_const(*elem.column);
/// Const Nullable that are NULL.
if (is_const && assert_cast<const ColumnConst*>(elem.column.get())->only_null()) {
return block.get_by_position(result).type->create_column_const(
input_rows_count, Field::create_field<TYPE_BIGINT>(0));
}
if (is_const) {
continue;
}
if (const auto* nullable_column = assert_cast<const ColumnNullable*>(elem.column.get())) {
const ColumnPtr& null_map_column = nullable_column->get_null_map_column_ptr();
const NullMap& src_null_map =
assert_cast<const ColumnUInt8&>(*null_map_column).get_data();
update_bitmap_op_count(count_data, src_null_map);
}
}
return mutable_src;
}
Status execute_bitmap_op_count_null_to_zero(
FunctionContext* context, Block& block, const ColumnNumbers& arguments, uint32_t result,
size_t input_rows_count,
const std::function<Status(FunctionContext*, Block&, const ColumnNumbers&, size_t, size_t)>&
exec_impl_func) {
if (have_null_column(block, arguments)) {
auto [temporary_block, new_args, new_result] =
create_block_with_nested_columns(block, arguments, result);
RETURN_IF_ERROR(exec_impl_func(context, temporary_block, new_args, new_result,
temporary_block.rows()));
block.get_by_position(result).column = handle_bitmap_op_count_null_value(
temporary_block.get_by_position(new_result).column, block, arguments, result,
input_rows_count);
} else {
return exec_impl_func(context, block, arguments, result, input_rows_count);
}
return Status::OK();
}
template <typename FunctionName>
class FunctionBitmapAndNotCount : public IFunction {
public:
using LeftDataType = DataTypeBitMap;
using RightDataType = DataTypeBitMap;
using ResultDataType = typename BitmapAndNotCount<LeftDataType, RightDataType>::ResultDataType;
static constexpr auto name = FunctionName::name;
static FunctionPtr create() { return std::make_shared<FunctionBitmapAndNotCount>(); }
String get_name() const override { return name; }
size_t get_number_of_arguments() const override { return 2; }
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
bool return_nullable = false;
// result is nullable only when any columns is nullable for bitmap_and_not_count
for (size_t i = 0; i < arguments.size(); ++i) {
if (arguments[i]->is_nullable()) {
return_nullable = true;
break;
}
}
auto result_type = std::make_shared<ResultDataType>();
return return_nullable ? make_nullable(result_type) : result_type;
}
bool use_default_implementation_for_nulls() const override {
// for bitmap_and_not_count, result is always not null, and if the bitmap op result is null,
// the count is 0
return false;
}
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
DCHECK_EQ(arguments.size(), 2);
auto impl_func = [&](FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) {
return execute_impl_internal(context, block, arguments, result, input_rows_count);
};
return execute_bitmap_op_count_null_to_zero(context, block, arguments, result,
input_rows_count, impl_func);
}
Status execute_impl_internal(FunctionContext* context, Block& block,
const ColumnNumbers& arguments, uint32_t result,
size_t input_rows_count) const {
using ColVecResult = ColumnVector<ResultDataType::PType>;
typename ColVecResult::MutablePtr col_res = ColVecResult::create();
auto& vec_res = col_res->get_data();
vec_res.resize(block.rows());
const auto& left = block.get_by_position(arguments[0]);
auto lcol = left.column;
const auto& right = block.get_by_position(arguments[1]);
auto rcol = right.column;
if (is_column_const(*left.column)) {
BitmapAndNotCount<LeftDataType, RightDataType>::scalar_vector(
assert_cast<const ColumnBitmap&>(
assert_cast<const ColumnConst*>(lcol.get())->get_data_column())
.get_data()[0],
assert_cast<const ColumnBitmap*>(rcol.get())->get_data(), vec_res.data());
} else if (is_column_const(*right.column)) {
BitmapAndNotCount<LeftDataType, RightDataType>::vector_scalar(
assert_cast<const ColumnBitmap*>(lcol.get())->get_data(),
assert_cast<const ColumnBitmap&>(
assert_cast<const ColumnConst*>(rcol.get())->get_data_column())
.get_data()[0],
vec_res.data());
} else {
BitmapAndNotCount<LeftDataType, RightDataType>::vector_vector(
assert_cast<const ColumnBitmap*>(lcol.get())->get_data(),
assert_cast<const ColumnBitmap*>(rcol.get())->get_data(), vec_res.data());
}
auto& result_info = block.get_by_position(result);
if (result_info.type->is_nullable()) {
block.replace_by_position(
result, ColumnNullable::create(std::move(col_res),
ColumnUInt8::create(input_rows_count, 0)));
} else {
block.replace_by_position(result, std::move(col_res));
}
return Status::OK();
}
};
struct NameBitmapContains {
static constexpr auto name = "bitmap_contains";
};
template <typename LeftDataType, typename RightDataType>
struct BitmapContains {
using ResultDataType = DataTypeUInt8;
using T0 = typename LeftDataType::FieldType;
using T1 = typename RightDataType::FieldType;
using LTData = std::vector<BitmapValue>;
using RTData = typename ColumnVector<RightDataType::PType>::Container;
using ResTData = typename ColumnUInt8::Container;
static void vector_vector(const LTData& lvec, const RTData& rvec, ResTData& res) {
size_t size = lvec.size();
for (size_t i = 0; i < size; ++i) {
res[i] = lvec[i].contains(rvec[i]);
}
}
static void vector_scalar(const LTData& lvec, const T1& rval, ResTData& res) {
size_t size = lvec.size();
for (size_t i = 0; i < size; ++i) {
res[i] = lvec[i].contains(rval);
}
}
static void scalar_vector(const BitmapValue& lval, const RTData& rvec, ResTData& res) {
size_t size = rvec.size();
for (size_t i = 0; i < size; ++i) {
res[i] = lval.contains(rvec[i]);
}
}
};
struct NameBitmapRemove {
static constexpr auto name = "bitmap_remove";
};
template <typename LeftDataType, typename RightDataType>
struct BitmapRemove {
using ResultDataType = DataTypeBitMap;
using T0 = typename LeftDataType::FieldType;
using T1 = typename RightDataType::FieldType;
using LTData = std::vector<BitmapValue>;
using RTData = typename ColumnVector<RightDataType::PType>::Container;
using ResTData = std::vector<BitmapValue>;
static void vector_vector(const LTData& lvec, const RTData& rvec, ResTData& res) {
size_t size = lvec.size();
for (size_t i = 0; i < size; ++i) {
res[i] = lvec[i];
res[i].remove(rvec[i]);
}
}
static void vector_scalar(const LTData& lvec, const T1& rval, ResTData& res) {
size_t size = lvec.size();
for (size_t i = 0; i < size; ++i) {
res[i] = lvec[i];
res[i].remove(rval);
}
}
static void scalar_vector(const BitmapValue& lval, const RTData& rvec, ResTData& res) {
size_t size = rvec.size();
for (size_t i = 0; i < size; ++i) {
res[i] = lval;
res[i].remove(rvec[i]);
}
}
};
struct NameBitmapHasAny {
static constexpr auto name = "bitmap_has_any";
};
template <typename LeftDataType, typename RightDataType>
struct BitmapHasAny {
using ResultDataType = DataTypeUInt8;
using T0 = typename LeftDataType::FieldType;
using T1 = typename RightDataType::FieldType;
using TData = std::vector<BitmapValue>;
using ResTData = typename ColumnUInt8::Container;
static void vector_vector(const TData& lvec, const TData& rvec, ResTData& res) {
size_t size = lvec.size();
for (size_t i = 0; i < size; ++i) {
auto bitmap = lvec[i];
bitmap &= rvec[i];
res[i] = bitmap.cardinality() != 0;
}
}
static void vector_scalar(const TData& lvec, const BitmapValue& rval, ResTData& res) {
size_t size = lvec.size();
for (size_t i = 0; i < size; ++i) {
auto bitmap = lvec[i];
bitmap &= rval;
res[i] = bitmap.cardinality() != 0;
}
}
static void scalar_vector(const BitmapValue& lval, const TData& rvec, ResTData& res) {
size_t size = rvec.size();
for (size_t i = 0; i < size; ++i) {
auto bitmap = lval;
bitmap &= rvec[i];
res[i] = bitmap.cardinality() != 0;
}
}
};
struct NameBitmapHasAll {
static constexpr auto name = "bitmap_has_all";
};
template <typename LeftDataType, typename RightDataType>
struct BitmapHasAll {
using ResultDataType = DataTypeUInt8;
using T0 = typename LeftDataType::FieldType;
using T1 = typename RightDataType::FieldType;
using TData = std::vector<BitmapValue>;
using ResTData = typename ColumnUInt8::Container;
static void vector_vector(const TData& lvec, const TData& rvec, ResTData& res) {
size_t size = lvec.size();
for (size_t i = 0; i < size; ++i) {
uint64_t lhs_cardinality = lvec[i].cardinality();
auto bitmap = lvec[i];
bitmap |= rvec[i];
res[i] = bitmap.cardinality() == lhs_cardinality;
}
}
static void vector_scalar(const TData& lvec, const BitmapValue& rval, ResTData& res) {
size_t size = lvec.size();
for (size_t i = 0; i < size; ++i) {
uint64_t lhs_cardinality = lvec[i].cardinality();
auto bitmap = lvec[i];
bitmap |= rval;
res[i] = bitmap.cardinality() == lhs_cardinality;
}
}
static void scalar_vector(const BitmapValue& lval, const TData& rvec, ResTData& res) {
size_t size = rvec.size();
uint64_t lhs_cardinality = lval.cardinality();
for (size_t i = 0; i < size; ++i) {
auto bitmap = lval;
bitmap |= rvec[i];
res[i] = bitmap.cardinality() == lhs_cardinality;
}
}
};
struct NameBitmapToString {
static constexpr auto name = "bitmap_to_string";
};
struct BitmapToString {
using ReturnType = DataTypeString;
static constexpr auto PrimitiveTypeImpl = PrimitiveType::TYPE_BITMAP;
using Type = DataTypeBitMap::FieldType;
using ReturnColumnType = ColumnString;
using Chars = ColumnString::Chars;
using Offsets = ColumnString::Offsets;
static Status vector(const std::vector<BitmapValue>& data, Chars& chars, Offsets& offsets) {
size_t size = data.size();
offsets.resize(size);
chars.reserve(size);
for (size_t i = 0; i < size; ++i) {
StringOP::push_value_string(data[i].to_string(), i, chars, offsets);
}
return Status::OK();
}
};
struct NameBitmapToBase64 {
static constexpr auto name = "bitmap_to_base64";
};
struct BitmapToBase64 {
using ReturnType = DataTypeString;
static constexpr auto PrimitiveTypeImpl = PrimitiveType::TYPE_BITMAP;