[function](bitmap) add function alias bitmap_andnot and bitmap_andnot_count (#24771)

This commit is contained in:
TengJianPing
2023-09-22 12:18:31 +08:00
committed by GitHub
parent c8655d1dae
commit 22616d125d
15 changed files with 272 additions and 16 deletions

View File

@ -724,13 +724,14 @@ Status execute_bitmap_op_count_null_to_zero(
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 = "bitmap_and_not_count";
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; }
@ -1297,7 +1298,9 @@ void register_function_bitmap(SimpleFunctionFactory& factory) {
factory.register_function<FunctionBitmapToString>();
factory.register_function<FunctionBitmapNot>();
factory.register_function<FunctionBitmapAndNot>();
factory.register_function<FunctionBitmapAndNotCount>();
factory.register_alias(NameBitmapAndNot::name, "bitmap_andnot");
factory.register_function<FunctionBitmapAndNotCount<NameBitmapAndNotCount>>();
factory.register_alias(NameBitmapAndNotCount::name, "bitmap_andnot_count");
factory.register_function<FunctionBitmapContains>();
factory.register_function<FunctionBitmapRemove>();
factory.register_function<FunctionBitmapHasAny>();

View File

@ -430,6 +430,21 @@ TEST(function_bitmap_test, function_bitmap_and_not_count) {
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
}
TEST(function_bitmap_test, function_bitmap_and_not_count_alias) {
std::string func_name = "bitmap_andnot_count";
InputTypeSet input_types = {TypeIndex::BitMap, TypeIndex::BitMap};
BitmapValue bitmap1({1, 2, 3});
BitmapValue bitmap2({3, 4, std::numeric_limits<uint64_t>::min()});
BitmapValue bitmap3({33, 5, std::numeric_limits<uint64_t>::max()});
BitmapValue empty_bitmap;
DataSet data_set = {{{&bitmap1, &empty_bitmap}, (int64_t)3}, //1,2,3
{{&bitmap2, Null()}, (int64_t)0},
{{&bitmap2, &bitmap3}, (int64_t)3}, //0,3,4
{{&bitmap1, &bitmap2}, (int64_t)2}}; //1,2
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
}
TEST(function_bitmap_test, function_bitmap_has_all) {
std::string func_name = "bitmap_has_all";
InputTypeSet input_types = {TypeIndex::BitMap, TypeIndex::BitMap};

View File

@ -1,6 +1,6 @@
---
{
"title": "BITMAP_AND_NOT_COUNT",
"title": "BITMAP_AND_NOT_COUNT,BITMAP_ANDNOT_COUNT",
"language": "en"
}
---
@ -24,7 +24,7 @@ specific language governing permissions and limitations
under the License.
-->
## bitmap_and_not_count
## bitmap_and_not_count,bitmap_andnot_count
### description
#### Syntax
@ -46,4 +46,4 @@ mysql> select bitmap_and_not_count(bitmap_from_string('1,2,3'),bitmap_from_strin
### keywords
BITMAP_AND_NOT_COUNT,BITMAP
BITMAP_AND_NOT_COUNT,BITMAP_ANDNOT_COUNT,BITMAP

View File

@ -1,6 +1,6 @@
---
{
"title": "BITMAP_AND_NOT",
"title": "BITMAP_AND_NOT,BITMAP_ANDNOT",
"language": "en"
}
---
@ -24,7 +24,7 @@ specific language governing permissions and limitations
under the License.
-->
## bitmap_and_not
## bitmap_and_not,bitmap_andnot
### description
#### Syntax
@ -66,4 +66,4 @@ mysql> select bitmap_to_string(bitmap_and_not(bitmap_from_string('1,2,3'),NULL))
### keywords
BITMAP_AND_NOT,BITMAP
BITMAP_AND_NOT,BITMAP_ANDNOT,BITMAP

View File

@ -1,6 +1,6 @@
---
{
"title": "BITMAP_AND_NOT_COUNT",
"title": "BITMAP_AND_NOT_COUNT,BITMAP_ANDNOT_COUNT",
"language": "zh-CN"
}
---
@ -24,7 +24,7 @@ specific language governing permissions and limitations
under the License.
-->
## bitmap_and_not_count
## bitmap_and_not_count,bitmap_andnot_count
### description
#### Syntax
@ -45,4 +45,4 @@ mysql> select bitmap_and_not_count(bitmap_from_string('1,2,3'),bitmap_from_strin
### keywords
BITMAP_AND_NOT_COUNT,BITMAP
BITMAP_AND_NOT_COUNT,BITMAP_ANDNOT_COUNT,BITMAP

View File

@ -1,6 +1,6 @@
---
{
"title": "BITMAP_AND_NOT",
"title": "BITMAP_AND_NOT,BITMAP_ANDNOT",
"language": "zh-CN"
}
---
@ -24,7 +24,7 @@ specific language governing permissions and limitations
under the License.
-->
## bitmap_and_not
## bitmap_and_not,bitmap_andnot
### description
#### Syntax
@ -67,4 +67,4 @@ mysql> select bitmap_to_string(bitmap_and_not(bitmap_from_string('1,2,3'),NULL))
### keywords
BITMAP_AND_NOT,BITMAP
BITMAP_AND_NOT,BITMAP_ANDNOT,BITMAP

View File

@ -67,7 +67,9 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.BitLength;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAnd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndCount;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndNot;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndNotAlias;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndNotCount;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndNotCountAlias;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapContains;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapCount;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapEmpty;
@ -446,7 +448,9 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(BitmapAnd.class, "bitmap_and"),
scalar(BitmapAndCount.class, "bitmap_and_count"),
scalar(BitmapAndNot.class, "bitmap_and_not"),
scalar(BitmapAndNotAlias.class, "bitmap_andnot"),
scalar(BitmapAndNotCount.class, "bitmap_and_not_count"),
scalar(BitmapAndNotCountAlias.class, "bitmap_andnot_count"),
scalar(BitmapContains.class, "bitmap_contains"),
scalar(BitmapCount.class, "bitmap_count"),
scalar(BitmapEmpty.class, "bitmap_empty"),

View File

@ -0,0 +1,68 @@
// 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.
package org.apache.doris.nereids.trees.expressions.functions.scalar;
import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BitmapType;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
* ScalarFunction 'bitmap_and_not'. This class is generated by GenerateFunction.
*/
public class BitmapAndNotAlias extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, PropagateNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BitmapType.INSTANCE).args(BitmapType.INSTANCE, BitmapType.INSTANCE)
);
/**
* constructor with 2 arguments.
*/
public BitmapAndNotAlias(Expression arg0, Expression arg1) {
super("bitmap_andnot", arg0, arg1);
}
/**
* withChildren.
*/
@Override
public BitmapAndNotAlias withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 2);
return new BitmapAndNotAlias(children.get(0), children.get(1));
}
@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}
@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitBitmapAndNotAlias(this, context);
}
}

View File

@ -0,0 +1,73 @@
// 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.
package org.apache.doris.nereids.trees.expressions.functions.scalar;
import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BigIntType;
import org.apache.doris.nereids.types.BitmapType;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
* ScalarFunction 'bitmap_and_not_count'. This class is generated by GenerateFunction.
*/
public class BitmapAndNotCountAlias extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE).args(BitmapType.INSTANCE, BitmapType.INSTANCE)
);
/**
* constructor with 2 arguments.
*/
public BitmapAndNotCountAlias(Expression arg0, Expression arg1) {
super("bitmap_andnot_count", arg0, arg1);
}
/**
* withChildren.
*/
@Override
public BitmapAndNotCountAlias withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 2);
return new BitmapAndNotCountAlias(children.get(0), children.get(1));
}
@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}
@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitBitmapAndNotCountAlias(this, context);
}
@Override
public boolean nullable() {
return children().stream().anyMatch(Expression::nullable);
}
}

View File

@ -69,7 +69,9 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.BitLength;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAnd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndCount;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndNot;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndNotAlias;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndNotCount;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndNotCountAlias;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapContains;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapCount;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapEmpty;
@ -580,6 +582,14 @@ public interface ScalarFunctionVisitor<R, C> {
return visitScalarFunction(bitmapAndNotCount, context);
}
default R visitBitmapAndNotAlias(BitmapAndNotAlias bitmapAndNotAlias, C context) {
return visitScalarFunction(bitmapAndNotAlias, context);
}
default R visitBitmapAndNotCountAlias(BitmapAndNotCountAlias bitmapAndNotCountAlias, C context) {
return visitScalarFunction(bitmapAndNotCountAlias, context);
}
default R visitBitmapContains(BitmapContains bitmapContains, C context) {
return visitScalarFunction(bitmapContains, context);
}

View File

@ -1834,7 +1834,7 @@ visible_functions = {
[['bitmap_hash'], 'BITMAP', ['STRING'], 'ALWAYS_NOT_NULLABLE'],
[['bitmap_hash64'], 'BITMAP', ['STRING'], 'ALWAYS_NOT_NULLABLE'],
[['bitmap_count'], 'BIGINT', ['BITMAP'], 'ALWAYS_NOT_NULLABLE'],
[['bitmap_and_not_count'], 'BIGINT', ['BITMAP','BITMAP'], ''],
[['bitmap_and_not_count', 'bitmap_andnot_count'], 'BIGINT', ['BITMAP','BITMAP'], ''],
[['bitmap_empty'], 'BITMAP', [], 'ALWAYS_NOT_NULLABLE'],
[['bitmap_or'], 'BITMAP', ['BITMAP','BITMAP','...'], ''],
[['bitmap_or'], 'BITMAP', ['BITMAP','BITMAP'], ''],
@ -1845,7 +1845,7 @@ visible_functions = {
[['bitmap_not'], 'BITMAP', ['BITMAP','BITMAP'], ''],
[['bitmap_and'], 'BITMAP', ['BITMAP','BITMAP','...'], ''],
[['bitmap_and'], 'BITMAP', ['BITMAP','BITMAP'], ''],
[['bitmap_and_not'], 'BITMAP', ['BITMAP','BITMAP'], ''],
[['bitmap_and_not', 'bitmap_andnot'], 'BITMAP', ['BITMAP','BITMAP'], ''],
[['bitmap_to_string'], 'STRING', ['BITMAP'], ''],
[['bitmap_from_string'], 'BITMAP', ['VARCHAR'], 'ALWAYS_NULLABLE'],
[['bitmap_from_string'], 'BITMAP', ['STRING'], 'ALWAYS_NULLABLE'],

View File

@ -202,6 +202,64 @@
0
0
-- !sql_bitmap_andnot_Bitmap_Bitmap --
\N
\N
\N
\N
\N
\N
\N
\N
\N
\N
\N
\N
\N
-- !sql_bitmap_andnot_Bitmap_Bitmap_notnull --
\N
\N
\N
\N
\N
\N
\N
\N
\N
\N
\N
\N
-- !sql_bitmap_andnot_count_Bitmap_Bitmap --
0
0
0
0
0
0
0
0
0
0
0
0
0
-- !sql_bitmap_andnot_count_Bitmap_Bitmap_notnull --
0
0
0
0
0
0
0
0
0
0
0
0
-- !sql_bitmap_contains_Bitmap_BigInt --
\N
true

View File

@ -273,6 +273,12 @@ true
3 0
4 0
-- !sql_bitmap_andnot_count3 --
1 1
2 1
3 0
4 0
-- !sql_bitmap_and_count8 --
1 2
2 2
@ -336,6 +342,9 @@ true
-- !sql --
2
-- !sql_bitmap_andnot --
2
-- !sql_bitmap_and_not_count1 --
2

View File

@ -33,6 +33,12 @@ suite("nereids_scalar_fn_B") {
qt_sql_bitmap_and_not_Bitmap_Bitmap_notnull "select bitmap_and_not(to_bitmap(kbint), to_bitmap(kbint)) from fn_test_not_nullable order by kbint, kbint"
qt_sql_bitmap_and_not_count_Bitmap_Bitmap "select bitmap_and_not_count(to_bitmap(kbint), to_bitmap(kbint)) from fn_test order by kbint, kbint"
qt_sql_bitmap_and_not_count_Bitmap_Bitmap_notnull "select bitmap_and_not_count(to_bitmap(kbint), to_bitmap(kbint)) from fn_test_not_nullable order by kbint, kbint"
qt_sql_bitmap_andnot_Bitmap_Bitmap "select bitmap_andnot(to_bitmap(kbint), to_bitmap(kbint)) from fn_test order by kbint, kbint"
qt_sql_bitmap_andnot_Bitmap_Bitmap_notnull "select bitmap_andnot(to_bitmap(kbint), to_bitmap(kbint)) from fn_test_not_nullable order by kbint, kbint"
qt_sql_bitmap_andnot_count_Bitmap_Bitmap "select bitmap_andnot_count(to_bitmap(kbint), to_bitmap(kbint)) from fn_test order by kbint, kbint"
qt_sql_bitmap_andnot_count_Bitmap_Bitmap_notnull "select bitmap_andnot_count(to_bitmap(kbint), to_bitmap(kbint)) from fn_test_not_nullable order by kbint, kbint"
qt_sql_bitmap_contains_Bitmap_BigInt "select bitmap_contains(to_bitmap(kbint), kbint) from fn_test order by kbint, kbint"
qt_sql_bitmap_contains_Bitmap_BigInt_notnull "select bitmap_contains(to_bitmap(kbint), kbint) from fn_test_not_nullable order by kbint, kbint"
qt_sql_bitmap_count_Bitmap "select bitmap_count(to_bitmap(kbint)) from fn_test order by kbint"

View File

@ -386,6 +386,15 @@ suite("test_bitmap_function") {
on l.dt = r.dt
order by l.dt, count
"""
qt_sql_bitmap_andnot_count3 """
select
l.dt,
bitmap_andnot_count(l.id, r.id) count
from
test_bitmap1 l left join test_bitmap2 r
on l.dt = r.dt
order by l.dt, count
"""
qt_sql_bitmap_and_count8 """
select
l.dt,
@ -519,6 +528,7 @@ suite("test_bitmap_function") {
// BITMAP_AND_NOT
qt_sql """ select bitmap_count(bitmap_and_not(bitmap_from_string('1,2,3'),bitmap_from_string('3,4,5'))) cnt """
qt_sql_bitmap_andnot """ select bitmap_count(bitmap_andnot(bitmap_from_string('1,2,3'),bitmap_from_string('3,4,5'))) cnt """
// BITMAP_AND_NOT_COUNT
qt_sql_bitmap_and_not_count1 """ select bitmap_and_not_count(bitmap_from_string('1,2,3'),bitmap_from_string('3,4,5')) cnt """