@@ -19,6 +19,8 @@ use common_expression::types::NullableType;
19
19
use common_expression:: vectorize_2_arg;
20
20
use common_expression:: FunctionProperty ;
21
21
use common_expression:: FunctionRegistry ;
22
+ use common_expression:: Value ;
23
+ use common_expression:: ValueRef ;
22
24
23
25
pub fn register ( registry : & mut FunctionRegistry ) {
24
26
registry. register_1_arg :: < BooleanType , BooleanType , _ , _ > (
@@ -34,7 +36,7 @@ pub fn register(registry: &mut FunctionRegistry) {
34
36
) ;
35
37
36
38
// 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 , _ , _ > (
38
40
"and_filters" ,
39
41
FunctionProperty :: default ( ) ,
40
42
|lhs, rhs| {
@@ -43,7 +45,23 @@ pub fn register(registry: &mut FunctionRegistry) {
43
45
has_true : lhs. has_true && rhs. has_true ,
44
46
} )
45
47
} ,
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
+ } ,
47
65
) ;
48
66
49
67
registry. register_2_arg_core :: < BooleanType , BooleanType , BooleanType , _ , _ > (
@@ -55,7 +73,23 @@ pub fn register(registry: &mut FunctionRegistry) {
55
73
has_true : lhs. has_true && rhs. has_true ,
56
74
} )
57
75
} ,
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
+ } ,
59
93
) ;
60
94
61
95
registry. register_2_arg_core :: < BooleanType , BooleanType , BooleanType , _ , _ > (
@@ -67,7 +101,23 @@ pub fn register(registry: &mut FunctionRegistry) {
67
101
has_true : lhs. has_true || rhs. has_true ,
68
102
} )
69
103
} ,
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
+ } ,
71
121
) ;
72
122
73
123
// https://en.wikibooks.org/wiki/Structured_Query_Language/NULLs_and_the_Three_Valued_Logic
@@ -93,7 +143,7 @@ pub fn register(registry: &mut FunctionRegistry) {
93
143
vectorize_2_arg :: < NullableType < BooleanType > , NullableType < BooleanType > , NullableType < BooleanType > > ( |lhs, rhs| {
94
144
let lhs_v = lhs. is_some ( ) ;
95
145
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 ) ) ;
97
147
if valid {
98
148
Some ( lhs. unwrap_or_default ( ) & rhs. unwrap_or_default ( ) )
99
149
} else {
@@ -122,9 +172,7 @@ pub fn register(registry: &mut FunctionRegistry) {
122
172
None
123
173
} ,
124
174
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 ( ) ;
128
176
if valid {
129
177
Some ( lhs. unwrap_or_default ( ) | rhs. unwrap_or_default ( ) )
130
178
} else {
0 commit comments