39
39
Degrees : "radiansToDegrees" ,
40
40
Greatest : "max" ,
41
41
Least : "min" ,
42
- Lower : "toLower" ,
43
42
Power : "pow" ,
44
43
Radians : "degreesToRadians" ,
45
44
Random : "rand" ,
46
- Upper : "toUpper" ,
47
45
}
48
46
EXTRACT_OPERATORS = {
49
47
ExtractDay .lookup_name : "dayOfMonth" ,
@@ -124,6 +122,22 @@ def null_if(self, compiler, connection):
124
122
return {"$cond" : {"if" : {"$eq" : [expr1 , expr2 ]}, "then" : None , "else" : expr1 }}
125
123
126
124
125
+ def perserve_null (operator ):
126
+ # If the argument is null, the function should return null, not
127
+ # $toLower/Upper's behavior of returning an empty string.
128
+ def wrapped (self , compiler , connection ):
129
+ lhs_mql = process_lhs (self , compiler , connection )
130
+ return {
131
+ "$cond" : {
132
+ "if" : {"$eq" : [lhs_mql , None ]},
133
+ "then" : None ,
134
+ "else" : {f"${ operator } " : lhs_mql },
135
+ }
136
+ }
137
+
138
+ return wrapped
139
+
140
+
127
141
def replace (self , compiler , connection ):
128
142
expression , text , replacement = process_lhs (self , compiler , connection )
129
143
return {"$replaceAll" : {"input" : expression , "find" : text , "replacement" : replacement }}
@@ -178,6 +192,7 @@ def register_functions():
178
192
Left .as_mql = left
179
193
Length .as_mql = length
180
194
Log .as_mql = log
195
+ Lower .as_mql = perserve_null ("toLower" )
181
196
LTrim .as_mql = trim ("ltrim" )
182
197
NullIf .as_mql = null_if
183
198
Replace .as_mql = replace
@@ -187,3 +202,4 @@ def register_functions():
187
202
Substr .as_mql = substr
188
203
Trim .as_mql = trim ("trim" )
189
204
TruncBase .as_mql = trunc
205
+ Upper .as_mql = perserve_null ("toUpper" )
0 commit comments