[feature](agg) support group_bitmap_xor agg function. (#13287)

support `group_bitmap_xor` agg function
This commit is contained in:
luozenglin
2022-10-17 18:40:06 +08:00
committed by GitHub
parent 87a6b1a13b
commit 207f4e559e
10 changed files with 187 additions and 1 deletions

View File

@ -117,6 +117,19 @@ void BitmapFunctions::bitmap_intersect(FunctionContext* ctx, const StringVal& sr
}
}
void BitmapFunctions::group_bitmap_xor(FunctionContext* ctx, const StringVal& src, StringVal* dst) {
if (src.is_null) {
return;
}
auto dst_bitmap = reinterpret_cast<BitmapValue*>(dst->ptr);
// zero size means the src input is a agg object
if (src.len == 0) {
(*dst_bitmap) ^= *reinterpret_cast<BitmapValue*>(src.ptr);
} else {
(*dst_bitmap) ^= BitmapValue((char*)src.ptr);
}
}
BigIntVal BitmapFunctions::bitmap_count(FunctionContext* ctx, const StringVal& src) {
if (src.is_null) {
return 0;

View File

@ -53,6 +53,7 @@ public:
// the dst value could be null
static void nullable_bitmap_init(FunctionContext* ctx, StringVal* dst);
static void bitmap_intersect(FunctionContext* ctx, const StringVal& src, StringVal* dst);
static void group_bitmap_xor(FunctionContext* ctx, const StringVal& src, StringVal* dst);
static BigIntVal bitmap_count(FunctionContext* ctx, const StringVal& src);
static BigIntVal bitmap_and_not_count(FunctionContext* ctx, const StringVal& src,
const StringVal& dst);

View File

@ -59,6 +59,14 @@ AggregateFunctionPtr create_aggregate_function_bitmap_intersect(const std::strin
argument_types);
}
AggregateFunctionPtr create_aggregate_function_group_bitmap_xor(const std::string& name,
const DataTypes& argument_types,
const Array& parameters,
const bool result_is_nullable) {
return std::make_shared<AggregateFunctionBitmapOp<AggregateFunctionGroupBitmapXorOp>>(
argument_types);
}
AggregateFunctionPtr create_aggregate_function_bitmap_union_count(const std::string& name,
const DataTypes& argument_types,
const Array& parameters,
@ -88,6 +96,7 @@ AggregateFunctionPtr create_aggregate_function_bitmap_union_int(const std::strin
void register_aggregate_function_bitmap(AggregateFunctionSimpleFactory& factory) {
factory.register_function("bitmap_union", create_aggregate_function_bitmap_union);
factory.register_function("bitmap_intersect", create_aggregate_function_bitmap_intersect);
factory.register_function("group_bitmap_xor", create_aggregate_function_group_bitmap_xor);
factory.register_function("bitmap_union_count", create_aggregate_function_bitmap_union_count);
factory.register_function("bitmap_union_count", create_aggregate_function_bitmap_union_count,
true);

View File

@ -84,6 +84,28 @@ struct AggregateFunctionBitmapIntersectOp {
}
};
struct AggregateFunctionGroupBitmapXorOp {
static constexpr auto name = "group_bitmap_xor";
static void add(BitmapValue& res, const BitmapValue& data, bool& is_first) {
if (UNLIKELY(is_first)) {
res = data;
is_first = false;
} else {
res ^= data;
}
}
static void merge(BitmapValue& res, const BitmapValue& data, bool& is_first) {
if (UNLIKELY(is_first)) {
res = data;
is_first = false;
} else {
res ^= data;
}
}
};
template <typename Op>
struct AggregateFunctionBitmapData {
BitmapValue value;

View File

@ -0,0 +1,57 @@
---
{
"title": "group_bitmap_xor",
"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.
-->
## group_bitmap_xor
### description
#### Syntax
`BITMAP GROUP_BITMAP_XOR(expr)`
Perform an xor calculation on expr, and return a new bitmap.
### example
```
mysql> select page, bitmap_to_string(user_id) from pv_bitmap;
+------+-----------------------------+
| page | bitmap_to_string(`user_id`) |
+------+-----------------------------+
| m | 4,7,8 |
| m | 1,3,6,15 |
| m | 4,7 |
+------+-----------------------------+
mysql> select page, bitmap_to_string(group_bitmap_xor(user_id)) from pv_bitmap group by page;
+------+-----------------------------------------------+
| page | bitmap_to_string(group_bitmap_xor(`user_id`)) |
+------+-----------------------------------------------+
| m | 1,3,6,8,15 |
+------+-----------------------------------------------+
```
### keywords
GROUP_BITMAP_XOR,BITMAP

View File

@ -420,6 +420,7 @@
"sql-manual/sql-functions/aggregate-functions/sum",
"sql-manual/sql-functions/aggregate-functions/max_by",
"sql-manual/sql-functions/aggregate-functions/bitmap_union",
"sql-manual/sql-functions/aggregate-functions/group_bitmap_xor",
"sql-manual/sql-functions/aggregate-functions/percentile_approx",
"sql-manual/sql-functions/aggregate-functions/stddev",
"sql-manual/sql-functions/aggregate-functions/group_concat",

View File

@ -0,0 +1,57 @@
---
{
"title": "group_bitmap_xor",
"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.
-->
## group_bitmap_xor
### description
#### Syntax
`BITMAP GROUP_BITMAP_XOR(expr)`
对expr进行 xor 计算, 返回新的bitmap。
### example
```
mysql> select page, bitmap_to_string(user_id) from pv_bitmap;
+------+-----------------------------+
| page | bitmap_to_string(`user_id`) |
+------+-----------------------------+
| m | 4,7,8 |
| m | 1,3,6,15 |
| m | 4,7 |
+------+-----------------------------+
mysql> select page, bitmap_to_string(group_bitmap_xor(user_id)) from pv_bitmap group by page;
+------+-----------------------------------------------+
| page | bitmap_to_string(group_bitmap_xor(`user_id`)) |
+------+-----------------------------------------------+
| m | 1,3,6,8,15 |
+------+-----------------------------------------------+
```
### keywords
GROUP_BITMAP_XOR,BITMAP

View File

@ -2364,6 +2364,25 @@ public class FunctionSet<T> {
"",
"",
true, false, true, true));
addBuiltin(AggregateFunction.createBuiltin("group_bitmap_xor", Lists.newArrayList(Type.BITMAP),
Type.BITMAP, Type.VARCHAR,
"_ZN5doris15BitmapFunctions11bitmap_initEPN9doris_udf15FunctionContextEPNS1_9StringValE",
"_ZN5doris15BitmapFunctions16group_bitmap_xorEPN9doris_udf15FunctionContextERKNS1_9StringValEPS4_",
"_ZN5doris15BitmapFunctions16group_bitmap_xorEPN9doris_udf15FunctionContextERKNS1_9StringValEPS4_",
"_ZN5doris15BitmapFunctions16bitmap_serializeEPN9doris_udf15FunctionContextERKNS1_9StringValE",
"_ZN5doris15BitmapFunctions16bitmap_serializeEPN9doris_udf15FunctionContextERKNS1_9StringValE",
true, false, true));
// vec group_bitmap_xor
addBuiltin(AggregateFunction.createBuiltin("group_bitmap_xor", Lists.newArrayList(Type.BITMAP),
Type.BITMAP, Type.BITMAP,
"",
"",
"",
"",
"",
true, false, true, true));
//quantile_state
addBuiltin(AggregateFunction.createBuiltin(QUANTILE_UNION, Lists.newArrayList(Type.QUANTILE_STATE),
Type.QUANTILE_STATE,

View File

@ -27,6 +27,11 @@ xian 100.0
-- !select5 --
3
-- !group_bitmap_xor --
20220201 10000
20220202 10001
20220203 10000,10001
-- !select6 --
beijing 3
xian 3

View File

@ -59,7 +59,7 @@ suite("test_aggregate_all_functions") {
// BITMAP_UNION
// BITMAP_UNION group_bitmap_xor
def tableName_03 = "pv_bitmap"
def tableName_04 = "bitmap_base"
sql "DROP TABLE IF EXISTS ${tableName_03}"
@ -112,6 +112,8 @@ suite("test_aggregate_all_functions") {
qt_select4 "select bitmap_union_count(user_id) from ${tableName_03}"
qt_select5 "select bitmap_count(bitmap_union(user_id)) FROM ${tableName_03}"
qt_group_bitmap_xor "select dt, bitmap_to_string(group_bitmap_xor(user_id_bitmap)) from ${tableName_04} group by dt order by dt"
sql "DROP TABLE IF EXISTS ${tableName_03}"
sql "DROP TABLE IF EXISTS ${tableName_04}"