imay commented on a change in pull request #1610: Add bitmap agg type and udaf
URL: https://github.com/apache/incubator-doris/pull/1610#discussion_r316945182
 
 

 ##########
 File path: be/src/util/bitmap.h
 ##########
 @@ -248,6 +250,144 @@ class Bitmap {
   static const int64_t BIT_INDEX_MASK = 63;
 };
 
+enum BitmapDataType {
+    EMPTY = 0,
+    INT32,
+    BITMAP
+};
+
+// the wrapper class for RoaringBitmap
+// todo(kks): improve for low cardinality set
+class RoaringBitmap {
+public:
+    RoaringBitmap() {
+        _type = EMPTY;
+    }
+
+    explicit RoaringBitmap(int32_t value) {
+        _int_value = value;
+        _type = INT32;
+    }
+
+    explicit RoaringBitmap(const char* src) {
+        _type = (BitmapDataType)src[0];
+        switch (_type) {
+            case EMPTY:
+                break;
+            case INT32:
+                _int_value = *reinterpret_cast<const int32_t*>(src + 1);
+                break;
+            case BITMAP:
+                _roaring = Roaring::read(src + 1);
+        }
+    }
+
+    void update(int32_t x) {
+        if (_type == INT32) {
+            _roaring.add(_int_value);
+        }
+        _roaring.add(x);
+        _type = BITMAP;
+    }
+
+    // need change the type:
+    // EMPTY -> INT
+    // EMPTY -> BITMAP
+    // INT   -> BITMAP
+    void merge(RoaringBitmap& bitmap) {
+        switch(bitmap._type) {
+            case EMPTY:
+                return;
+            case INT32:
+                switch (_type) {
+                    case EMPTY:
+                        _int_value = bitmap._int_value;
+                        _type = INT32;
+                        break;
+                    case INT32:
+                        _roaring.add(_int_value);
+                        _roaring.add(bitmap._int_value);
+                        _type = BITMAP;
+                        break;
+                    case BITMAP:
+                        _roaring.add(bitmap._int_value);
+                }
+                return;
+            case BITMAP:
+                switch (_type) {
+                    case EMPTY:
+                        _roaring = bitmap._roaring;
+                        _type = BITMAP;
+                        break;
+                    case INT32:
+                        _roaring = bitmap._roaring;
+                        _roaring.add(_int_value);
+                        _type = BITMAP;
+                        break;
+                    case BITMAP:
+                        _roaring = _roaring | bitmap._roaring;
+                }
+                return;
+        }
+    }
+
+    int64_t cardinality() const {
+        switch (_type) {
+            case EMPTY:
+                return 0;
+            case INT32:
+                return 1;
+            case BITMAP:
+                return _roaring.cardinality();
+        }
+        return 0;
+    }
+
+    size_t size() {
+        switch (_type) {
+            case EMPTY:
+                return 1;
+            case INT32:
+                return sizeof(int32_t) + 1;
+            case BITMAP:
+                _roaring.runOptimize();
+                return _roaring.getSizeInBytes() + 1;
+        }
+        return 1;
+    }
+
+    //must call size() first
+    void serialize(char* dest) {
+        dest[0] = _type;
+        switch (_type) {
+            case EMPTY:
+                break;
+            case INT32:
+                *reinterpret_cast<int32_t*>(dest + 1) = _int_value;
 
 Review comment:
   In order to support cross platform, we should call encode_le32 in 
util/coding.h

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to