Skip to content

Commit 887948c

Browse files
committed
feat(query): use bitmap and or to combine bools
1 parent 7e12654 commit 887948c

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

src/query/functions-v2/src/scalars/boolean.rs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use common_expression::types::NullableType;
1919
use common_expression::vectorize_2_arg;
2020
use common_expression::FunctionProperty;
2121
use common_expression::FunctionRegistry;
22+
use common_expression::Value;
23+
use common_expression::ValueRef;
2224

2325
pub fn register(registry: &mut FunctionRegistry) {
2426
registry.register_1_arg::<BooleanType, BooleanType, _, _>(
@@ -34,7 +36,7 @@ pub fn register(registry: &mut FunctionRegistry) {
3436
);
3537

3638
// special function to combine the filter efficiently
37-
registry.register_2_arg::<BooleanType, BooleanType, BooleanType, _, _>(
39+
registry.register_passthrough_nullable_2_arg::<BooleanType, BooleanType, BooleanType, _, _>(
3840
"and_filters",
3941
FunctionProperty::default(),
4042
|lhs, rhs| {
@@ -43,7 +45,23 @@ pub fn register(registry: &mut FunctionRegistry) {
4345
has_true: lhs.has_true && rhs.has_true,
4446
})
4547
},
46-
|lhs, rhs| lhs & rhs,
48+
|lhs, rhs, _| match (lhs, rhs) {
49+
(ValueRef::Scalar(flag), other) => {
50+
if flag {
51+
Ok(other.to_owned())
52+
} else {
53+
Ok(Value::Scalar(false))
54+
}
55+
}
56+
(other, ValueRef::Scalar(flag)) => {
57+
if flag {
58+
Ok(other.to_owned())
59+
} else {
60+
Ok(Value::Scalar(false))
61+
}
62+
}
63+
(ValueRef::Column(a), ValueRef::Column(b)) => Ok(Value::Column(&a & &b)),
64+
},
4765
);
4866

4967
registry.register_2_arg_core::<BooleanType, BooleanType, BooleanType, _, _>(
@@ -55,7 +73,23 @@ pub fn register(registry: &mut FunctionRegistry) {
5573
has_true: lhs.has_true && rhs.has_true,
5674
})
5775
},
58-
vectorize_2_arg::<BooleanType, BooleanType, BooleanType>(|lhs, rhs| lhs & rhs),
76+
|lhs, rhs, _| match (lhs, rhs) {
77+
(ValueRef::Scalar(flag), other) => {
78+
if flag {
79+
Ok(other.to_owned())
80+
} else {
81+
Ok(Value::Scalar(false))
82+
}
83+
}
84+
(other, ValueRef::Scalar(flag)) => {
85+
if flag {
86+
Ok(other.to_owned())
87+
} else {
88+
Ok(Value::Scalar(false))
89+
}
90+
}
91+
(ValueRef::Column(a), ValueRef::Column(b)) => Ok(Value::Column(&a & &b)),
92+
},
5993
);
6094

6195
registry.register_2_arg_core::<BooleanType, BooleanType, BooleanType, _, _>(
@@ -67,7 +101,23 @@ pub fn register(registry: &mut FunctionRegistry) {
67101
has_true: lhs.has_true || rhs.has_true,
68102
})
69103
},
70-
vectorize_2_arg::<BooleanType, BooleanType, BooleanType>(|lhs, rhs| lhs | rhs),
104+
|lhs, rhs, _| match (lhs, rhs) {
105+
(ValueRef::Scalar(flag), other) => {
106+
if flag {
107+
Ok(Value::Scalar(true))
108+
} else {
109+
Ok(other.to_owned())
110+
}
111+
}
112+
(other, ValueRef::Scalar(flag)) => {
113+
if flag {
114+
Ok(Value::Scalar(true))
115+
} else {
116+
Ok(other.to_owned())
117+
}
118+
}
119+
(ValueRef::Column(a), ValueRef::Column(b)) => Ok(Value::Column(&a | &b)),
120+
},
71121
);
72122

73123
// https://en.wikibooks.org/wiki/Structured_Query_Language/NULLs_and_the_Three_Valued_Logic
@@ -93,7 +143,7 @@ pub fn register(registry: &mut FunctionRegistry) {
93143
vectorize_2_arg::<NullableType<BooleanType>, NullableType<BooleanType>, NullableType<BooleanType>>(|lhs, rhs| {
94144
let lhs_v = lhs.is_some();
95145
let rhs_v = rhs.is_some();
96-
let valid = (lhs_v & rhs_v) | (lhs_v & lhs.unwrap_or_default()) | (rhs_v & rhs.unwrap_or_default());
146+
let valid = (lhs_v & rhs_v) | (lhs == Some(true)) | (rhs == Some(true));
97147
if valid {
98148
Some(lhs.unwrap_or_default() & rhs.unwrap_or_default())
99149
} else {
@@ -122,9 +172,7 @@ pub fn register(registry: &mut FunctionRegistry) {
122172
None
123173
},
124174
vectorize_2_arg::<NullableType<BooleanType>, NullableType<BooleanType>, NullableType<BooleanType>>(|lhs, rhs| {
125-
let lhs_v = lhs.is_some();
126-
let rhs_v = rhs.is_some();
127-
let valid = (lhs_v & rhs_v) | (lhs.unwrap_or_default() | rhs.unwrap_or_default());
175+
let valid = (lhs.is_some() & rhs.is_some()) | lhs.unwrap_or_default() | rhs.unwrap_or_default();
128176
if valid {
129177
Some(lhs.unwrap_or_default() | rhs.unwrap_or_default())
130178
} else {

0 commit comments

Comments
 (0)