Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions be/src/exprs/bitmap_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions be/src/exprs/bitmap_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class BitmapFunctions {
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);
Expand Down
11 changes: 11 additions & 0 deletions be/src/util/bitmap_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,17 @@ class BitmapValue {
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;
Expand Down
21 changes: 21 additions & 0 deletions be/test/exprs/bitmap_function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
55 changes: 55 additions & 0 deletions docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_min.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,8 @@
'_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', '...'],
Expand Down