Skip to content

Commit 44a6622

Browse files
authored
fix: implements bitmap comparison (#19038)
* impl bitmap comparison OP * cargo fmt * logic test * udpate ut GOLDENFILES * unit tests for bitmap comparison * cargo fmt * remove unnecessary comparisons: gt, gte, lt, lte * UPDATE GOLDENFILES
1 parent fcda93b commit 44a6622

File tree

6 files changed

+144
-6
lines changed

6 files changed

+144
-6
lines changed

src/query/functions/src/scalars/comparison.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use databend_common_expression::types::AnyType;
2626
use databend_common_expression::types::ArgType;
2727
use databend_common_expression::types::ArrayType;
2828
use databend_common_expression::types::Bitmap;
29+
use databend_common_expression::types::BitmapType;
2930
use databend_common_expression::types::BooleanType;
3031
use databend_common_expression::types::DataType;
3132
use databend_common_expression::types::DateType;
@@ -84,6 +85,7 @@ pub fn register(registry: &mut FunctionRegistry) {
8485
register_tuple_cmp(registry);
8586
register_like(registry);
8687
register_interval_cmp(registry);
88+
register_bitmap_cmp(registry);
8789
}
8890

8991
pub const ALL_COMP_FUNC_NAMES: &[&str] = &["eq", "noteq", "lt", "lte", "gt", "gte"];
@@ -1287,3 +1289,16 @@ fn vectorize_regexp(
12871289
}
12881290
}
12891291
}
1292+
1293+
fn register_bitmap_cmp(registry: &mut FunctionRegistry) {
1294+
registry.register_comparison_2_arg::<BitmapType, BitmapType, _, _>(
1295+
"eq",
1296+
|_, _, _| FunctionDomain::Full,
1297+
|lhs, rhs, _| lhs == rhs,
1298+
);
1299+
registry.register_comparison_2_arg::<BitmapType, BitmapType, _, _>(
1300+
"noteq",
1301+
|_, _, _| FunctionDomain::Full,
1302+
|lhs, rhs, _| lhs != rhs,
1303+
);
1304+
}

src/query/functions/tests/it/scalars/comparison.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ fn test_eq(file: &mut impl Write) {
121121
];
122122
run_ast(file, "parse_json(lhs) = parse_json(rhs)", &table);
123123
run_ast(file, "lhs = rhs", &table);
124+
125+
run_ast(
126+
file,
127+
"build_bitmap([3, 7, 9]) = build_bitmap([3, 7, 9])",
128+
&[],
129+
);
130+
run_ast(
131+
file,
132+
"build_bitmap([3, 7, 9]) = build_bitmap([3, 7, 10])",
133+
&[],
134+
);
135+
run_ast(file, "null = build_bitmap([3, 7, 10])", &[]);
124136
}
125137

126138
fn test_noteq(file: &mut impl Write) {
@@ -186,6 +198,18 @@ fn test_noteq(file: &mut impl Write) {
186198
];
187199
run_ast(file, "parse_json(lhs) != parse_json(rhs)", &table);
188200
run_ast(file, "lhs != rhs", &table);
201+
202+
run_ast(
203+
file,
204+
"build_bitmap([3, 7, 9]) != build_bitmap([3, 7, 9])",
205+
&[],
206+
);
207+
run_ast(
208+
file,
209+
"build_bitmap([3, 7, 9]) != build_bitmap([3, 7, 10])",
210+
&[],
211+
);
212+
run_ast(file, "null != build_bitmap([3, 7, 10])", &[]);
189213
}
190214

191215
fn test_lt(file: &mut impl Write) {

src/query/functions/tests/it/scalars/testdata/comparison.txt

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ candidate functions:
142142
eq(Variant, Variant) :: Boolean : unable to unify `Tuple(UInt8, String)` with `Variant`
143143
eq(Variant NULL, Variant NULL) :: Boolean NULL : unable to unify `Tuple(UInt8, String)` with `Variant`
144144
eq(String, String) :: Boolean : unable to unify `Tuple(UInt8, String)` with `String`
145-
... and 76 more
145+
... and 78 more
146146

147147

148148

@@ -312,6 +312,33 @@ evaluation (internal):
312312
+--------+--------------------------------------------------------------------------------------------------------------------+
313313

314314

315+
ast : build_bitmap([3, 7, 9]) = build_bitmap([3, 7, 9])
316+
raw expr : eq(build_bitmap(array(3, 7, 9)), build_bitmap(array(3, 7, 9)))
317+
checked expr : eq<Bitmap, Bitmap>(build_bitmap<Array(UInt8 NULL)>(CAST<Array(UInt8)>(array<T0=UInt8><T0, T0, T0>(3_u8, 7_u8, 9_u8) AS Array(UInt8 NULL))), build_bitmap<Array(UInt8 NULL)>(CAST<Array(UInt8)>(array<T0=UInt8><T0, T0, T0>(3_u8, 7_u8, 9_u8) AS Array(UInt8 NULL))))
318+
optimized expr : true
319+
output type : Boolean
320+
output domain : {TRUE}
321+
output : true
322+
323+
324+
ast : build_bitmap([3, 7, 9]) = build_bitmap([3, 7, 10])
325+
raw expr : eq(build_bitmap(array(3, 7, 9)), build_bitmap(array(3, 7, 10)))
326+
checked expr : eq<Bitmap, Bitmap>(build_bitmap<Array(UInt8 NULL)>(CAST<Array(UInt8)>(array<T0=UInt8><T0, T0, T0>(3_u8, 7_u8, 9_u8) AS Array(UInt8 NULL))), build_bitmap<Array(UInt8 NULL)>(CAST<Array(UInt8)>(array<T0=UInt8><T0, T0, T0>(3_u8, 7_u8, 10_u8) AS Array(UInt8 NULL))))
327+
optimized expr : false
328+
output type : Boolean
329+
output domain : {FALSE}
330+
output : false
331+
332+
333+
ast : null = build_bitmap([3, 7, 10])
334+
raw expr : eq(NULL, build_bitmap(array(3, 7, 10)))
335+
checked expr : eq<Bitmap NULL, Bitmap NULL>(CAST<NULL>(NULL AS Bitmap NULL), CAST<Bitmap>(build_bitmap<Array(UInt8 NULL)>(CAST<Array(UInt8)>(array<T0=UInt8><T0, T0, T0>(3_u8, 7_u8, 10_u8) AS Array(UInt8 NULL))) AS Bitmap NULL))
336+
optimized expr : NULL
337+
output type : Boolean NULL
338+
output domain : {NULL}
339+
output : NULL
340+
341+
315342
ast : '1'!='2'
316343
raw expr : noteq('1', '2')
317344
checked expr : noteq<String, String>("1", "2")
@@ -429,7 +456,7 @@ candidate functions:
429456
noteq(Variant, Variant) :: Boolean : unable to unify `Tuple(UInt8, String)` with `Variant`
430457
noteq(Variant NULL, Variant NULL) :: Boolean NULL : unable to unify `Tuple(UInt8, String)` with `Variant`
431458
noteq(String, String) :: Boolean : unable to unify `Tuple(UInt8, String)` with `String`
432-
... and 76 more
459+
... and 78 more
433460

434461

435462

@@ -544,6 +571,33 @@ evaluation (internal):
544571
+--------+------------------------------------------------------------------------------+
545572

546573

574+
ast : build_bitmap([3, 7, 9]) != build_bitmap([3, 7, 9])
575+
raw expr : noteq(build_bitmap(array(3, 7, 9)), build_bitmap(array(3, 7, 9)))
576+
checked expr : noteq<Bitmap, Bitmap>(build_bitmap<Array(UInt8 NULL)>(CAST<Array(UInt8)>(array<T0=UInt8><T0, T0, T0>(3_u8, 7_u8, 9_u8) AS Array(UInt8 NULL))), build_bitmap<Array(UInt8 NULL)>(CAST<Array(UInt8)>(array<T0=UInt8><T0, T0, T0>(3_u8, 7_u8, 9_u8) AS Array(UInt8 NULL))))
577+
optimized expr : false
578+
output type : Boolean
579+
output domain : {FALSE}
580+
output : false
581+
582+
583+
ast : build_bitmap([3, 7, 9]) != build_bitmap([3, 7, 10])
584+
raw expr : noteq(build_bitmap(array(3, 7, 9)), build_bitmap(array(3, 7, 10)))
585+
checked expr : noteq<Bitmap, Bitmap>(build_bitmap<Array(UInt8 NULL)>(CAST<Array(UInt8)>(array<T0=UInt8><T0, T0, T0>(3_u8, 7_u8, 9_u8) AS Array(UInt8 NULL))), build_bitmap<Array(UInt8 NULL)>(CAST<Array(UInt8)>(array<T0=UInt8><T0, T0, T0>(3_u8, 7_u8, 10_u8) AS Array(UInt8 NULL))))
586+
optimized expr : true
587+
output type : Boolean
588+
output domain : {TRUE}
589+
output : true
590+
591+
592+
ast : null != build_bitmap([3, 7, 10])
593+
raw expr : noteq(NULL, build_bitmap(array(3, 7, 10)))
594+
checked expr : noteq<Bitmap NULL, Bitmap NULL>(CAST<NULL>(NULL AS Bitmap NULL), CAST<Bitmap>(build_bitmap<Array(UInt8 NULL)>(CAST<Array(UInt8)>(array<T0=UInt8><T0, T0, T0>(3_u8, 7_u8, 10_u8) AS Array(UInt8 NULL))) AS Bitmap NULL))
595+
optimized expr : NULL
596+
output type : Boolean NULL
597+
output domain : {NULL}
598+
output : NULL
599+
600+
547601
ast : '1'<'2'
548602
raw expr : lt('1', '2')
549603
checked expr : lt<String, String>("1", "2")

src/query/functions/tests/it/scalars/testdata/function_list.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,8 @@ Functions overloads:
19111911
77 eq FACTORY
19121912
78 eq(Interval, Interval) :: Boolean
19131913
79 eq(Interval NULL, Interval NULL) :: Boolean NULL
1914+
80 eq(Bitmap, Bitmap) :: Boolean
1915+
81 eq(Bitmap NULL, Bitmap NULL) :: Boolean NULL
19141916
0 exp(UInt8) :: Float64
19151917
1 exp(UInt8 NULL) :: Float64 NULL
19161918
2 exp(UInt16) :: Float64
@@ -3415,6 +3417,8 @@ Functions overloads:
34153417
77 noteq FACTORY
34163418
78 noteq(Interval, Interval) :: Boolean
34173419
79 noteq(Interval NULL, Interval NULL) :: Boolean NULL
3420+
80 noteq(Bitmap, Bitmap) :: Boolean
3421+
81 noteq(Bitmap NULL, Bitmap NULL) :: Boolean NULL
34183422
0 now() :: Timestamp
34193423
0 object_construct FACTORY
34203424
0 object_construct_keep_null FACTORY

src/query/functions/tests/it/type_check/testdata/decimal.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,7 +3152,7 @@ candidate functions:
31523152
eq(Variant, Variant) :: Boolean : unable to unify `Decimal(38, 0)` with `Variant`
31533153
eq(Variant NULL, Variant NULL) :: Boolean NULL : unable to unify `Decimal(38, 0)` with `Variant`
31543154
eq(String, String) :: Boolean : unable to unify `Decimal(38, 0)` with `String`
3155-
... and 75 more
3155+
... and 77 more
31563156

31573157

31583158

@@ -3502,7 +3502,7 @@ candidate functions:
35023502
eq(Variant, Variant) :: Boolean : unable to unify `Decimal(38, 0) NULL` with `Variant`
35033503
eq(Variant NULL, Variant NULL) :: Boolean NULL : unable to unify `Decimal(38, 0)` with `Variant`
35043504
eq(String, String) :: Boolean : unable to unify `Decimal(38, 0) NULL` with `String`
3505-
... and 75 more
3505+
... and 77 more
35063506

35073507

35083508

@@ -3852,7 +3852,7 @@ candidate functions:
38523852
eq(Variant, Variant) :: Boolean : unable to unify `Decimal(76, 0)` with `Variant`
38533853
eq(Variant NULL, Variant NULL) :: Boolean NULL : unable to unify `Decimal(76, 0)` with `Variant`
38543854
eq(String, String) :: Boolean : unable to unify `Decimal(76, 0)` with `String`
3855-
... and 75 more
3855+
... and 77 more
38563856

38573857

38583858

@@ -4202,7 +4202,7 @@ candidate functions:
42024202
eq(Variant, Variant) :: Boolean : unable to unify `Decimal(76, 0) NULL` with `Variant`
42034203
eq(Variant NULL, Variant NULL) :: Boolean NULL : unable to unify `Decimal(76, 0)` with `Variant`
42044204
eq(String, String) :: Boolean : unable to unify `Decimal(76, 0) NULL` with `String`
4205-
... and 75 more
4205+
... and 77 more
42064206

42074207

42084208

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Copyright 2023 Databend Cloud
2+
##
3+
## Licensed under the Elastic License, Version 2.0 (the "License");
4+
## you may not use this file except in compliance with the License.
5+
## You may obtain a copy of the License at
6+
##
7+
## https://www.elastic.co/licensing/elastic-license
8+
##
9+
## Unless required by applicable law or agreed to in writing, software
10+
## distributed under the License is distributed on an "AS IS" BASIS,
11+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
## See the License for the specific language governing permissions and
13+
## limitations under the License.
14+
15+
statement ok
16+
create or replace database issue_19037;
17+
18+
statement ok
19+
use issue_19037;
20+
21+
statement ok
22+
create or replace table t( a int, b BITMAP);
23+
24+
statement ok
25+
create or replace stream s on table t APPEND_ONLY = false;
26+
27+
statement ok
28+
insert into t values(1, BUILD_BITMAP([3, 7, 9]));
29+
30+
query T
31+
select count() from s with consume;
32+
----
33+
1
34+
35+
statement ok
36+
update t set b = BUILD_BITMAP([3, 7, 10]) where a = 1;
37+
38+
query T
39+
select count() from s with consume;
40+
----
41+
2

0 commit comments

Comments
 (0)