This is an automated email from the ASF dual-hosted git repository.

yangzhg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 1e8c458  [Function] Add BE udf bitmap_min (#2538) (#5581)
1e8c458 is described below

commit 1e8c4584abc813308f47f8dc26c6bbe469044d09
Author: Patrick <[email protected]>
AuthorDate: Thu Apr 8 09:11:32 2021 +0800

    [Function] Add BE udf bitmap_min (#2538) (#5581)
    
    this function will return the min result of the input bitmap .
---
 be/src/exprs/bitmap_function.cpp                   | 13 +++++
 be/src/exprs/bitmap_function.h                     |  1 +
 be/src/util/bitmap_value.h                         | 11 +++++
 be/test/exprs/bitmap_function_test.cpp             | 21 +++++++++
 .../sql-functions/bitmap-functions/bitmap_min.md   | 55 ++++++++++++++++++++++
 .../sql-functions/bitmap-functions/bitmap_min.md   | 55 ++++++++++++++++++++++
 gensrc/script/doris_builtins_functions.py          |  2 +
 7 files changed, 158 insertions(+)

diff --git a/be/src/exprs/bitmap_function.cpp b/be/src/exprs/bitmap_function.cpp
index 9e0cb98..8ca25aa 100644
--- a/be/src/exprs/bitmap_function.cpp
+++ b/be/src/exprs/bitmap_function.cpp
@@ -340,6 +340,19 @@ BigIntVal BitmapFunctions::bitmap_count(FunctionContext* 
ctx, const StringVal& s
     }
 }
 
+BigIntVal BitmapFunctions::bitmap_min(FunctionContext* ctx, const StringVal& 
src) {
+    if (src.is_null) {
+        return BigIntVal::null();
+    }
+
+    if (src.len == 0) {
+        return reinterpret_cast<BitmapValue*>(src.ptr)->minimum();
+    } else {
+        auto bitmap = BitmapValue((char*)src.ptr);
+        return bitmap.minimum();
+    }
+}
+
 StringVal BitmapFunctions::to_bitmap(doris_udf::FunctionContext* ctx,
                                      const doris_udf::StringVal& src) {
     BitmapValue bitmap;
diff --git a/be/src/exprs/bitmap_function.h b/be/src/exprs/bitmap_function.h
index 5fd0d96..9739b35 100644
--- a/be/src/exprs/bitmap_function.h
+++ b/be/src/exprs/bitmap_function.h
@@ -55,6 +55,7 @@ public:
     static void nullable_bitmap_init(FunctionContext* ctx, StringVal* dst);
     static void bitmap_intersect(FunctionContext* ctx, const StringVal& src, 
StringVal* dst);
     static BigIntVal bitmap_count(FunctionContext* ctx, const StringVal& src);
+    static BigIntVal bitmap_min(FunctionContext* ctx, const StringVal& str);
 
     static StringVal bitmap_serialize(FunctionContext* ctx, const StringVal& 
src);
     static StringVal to_bitmap(FunctionContext* ctx, const StringVal& src);
diff --git a/be/src/util/bitmap_value.h b/be/src/util/bitmap_value.h
index 1c5e85d..b89569a 100644
--- a/be/src/util/bitmap_value.h
+++ b/be/src/util/bitmap_value.h
@@ -1287,6 +1287,17 @@ public:
         return true;
     }
 
+    BigIntVal minimum() {
+        switch (_type) {
+        case SINGLE:
+            return BigIntVal(_sv);
+        case BITMAP:
+            return BigIntVal(_bitmap.minimum());
+        default:
+            return BigIntVal::null();
+        }
+    }
+
     // TODO limit string size to avoid OOM
     std::string to_string() const {
         std::stringstream ss;
diff --git a/be/test/exprs/bitmap_function_test.cpp 
b/be/test/exprs/bitmap_function_test.cpp
index 7407d84..5c7e57c 100644
--- a/be/test/exprs/bitmap_function_test.cpp
+++ b/be/test/exprs/bitmap_function_test.cpp
@@ -218,6 +218,27 @@ TEST_F(BitmapFunctionsTest, bitmap_count) {
     ASSERT_EQ(BigIntVal(0), null_bitmap);
 }
 
+TEST_F(BitmapFunctionsTest, bitmap_min) {
+    BigIntVal result = BitmapFunctions::bitmap_min(ctx, StringVal::null());
+    ASSERT_TRUE(result.is_null());
+
+    StringVal empty_str = convert_bitmap_to_string(ctx, BitmapValue());
+    result = BitmapFunctions::bitmap_min(ctx, empty_str);
+    ASSERT_TRUE(result.is_null());
+
+    StringVal bitmap_str = convert_bitmap_to_string(ctx, BitmapValue(1024));
+    result = BitmapFunctions::bitmap_min(ctx, bitmap_str);
+    ASSERT_EQ(BigIntVal(1024), result);
+
+    bitmap_str = convert_bitmap_to_string(ctx, BitmapValue({1024, 1}));
+    result = BitmapFunctions::bitmap_min(ctx, bitmap_str);
+    ASSERT_EQ(BigIntVal(1), result);
+
+    bitmap_str = convert_bitmap_to_string(ctx, BitmapValue({1024, 3, 2}));
+    result = BitmapFunctions::bitmap_min(ctx, bitmap_str);
+    ASSERT_EQ(BigIntVal(2), result);
+}
+
 // test intersect_count
 template <typename ValType, typename ValueType>
 void test_bitmap_intersect(FunctionContext* ctx, ValType key1, ValType key2) {
diff --git a/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_min.md 
b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_min.md
new file mode 100644
index 0000000..484f815
--- /dev/null
+++ b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_min.md
@@ -0,0 +1,55 @@
+---
+{
+    "title": "bitmap_min",
+    "language": "en"
+}
+---
+
+<!-- 
+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.
+-->
+
+# bitmap_min
+## description
+### Syntax
+
+`BIGINT BITMAP_MIN(BITMAP input)`
+
+Calculate and return the min values of a bitmap.
+
+## example
+
+```
+mysql> select bitmap_min(bitmap_from_string('')) value;
++-------+
+| value |
++-------+
+|  NULL |
++-------+
+
+mysql> select bitmap_min(bitmap_from_string('1,9999999999')) value;
++-------+
+| value |
++-------+
+|     1 |
++-------+
+```
+
+## keyword
+
+    BITMAP_MIN,BITMAP
diff --git 
a/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_min.md 
b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_min.md
new file mode 100644
index 0000000..09e8486
--- /dev/null
+++ b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_min.md
@@ -0,0 +1,55 @@
+---
+{
+    "title": "bitmap_min",
+    "language": "zh-CN"
+}
+---
+
+<!-- 
+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.
+-->
+
+# bitmap_min
+## description
+### Syntax
+
+`BIGINT BITMAP_MIN(BITMAP input)`
+
+计算并返回 bitmap 中的最小值.
+
+## example
+
+```
+mysql> select bitmap_min(bitmap_from_string('')) value;
++-------+
+| value |
++-------+
+|  NULL |
++-------+
+
+mysql> select bitmap_min(bitmap_from_string('1,9999999999')) value;
++-------+
+| value |
++-------+
+|     1 |
++-------+
+```
+
+## keyword
+
+    BITMAP_MIN,BITMAP
diff --git a/gensrc/script/doris_builtins_functions.py 
b/gensrc/script/doris_builtins_functions.py
index 26d4e3c..378e329 100755
--- a/gensrc/script/doris_builtins_functions.py
+++ b/gensrc/script/doris_builtins_functions.py
@@ -814,6 +814,8 @@ visible_functions = [
         
'_ZN5doris15BitmapFunctions15bitmap_containsEPN9doris_udf15FunctionContextERKNS1_9StringValERKNS1_9BigIntValE'],
     [['bitmap_has_any'], 'BOOLEAN', ['BITMAP','BITMAP'],
         
'_ZN5doris15BitmapFunctions14bitmap_has_anyEPN9doris_udf15FunctionContextERKNS1_9StringValES6_'],
+    [['bitmap_min'], 'BIGINT', ['BITMAP'],
+        
'_ZN5doris15BitmapFunctions10bitmap_minEPN9doris_udf15FunctionContextERKNS1_9StringValE'],
 
     # hash functions
     [['murmur_hash3_32'], 'INT', ['VARCHAR', '...'],

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

Reply via email to