70
70
# TEST THAT HAVE THOSE OPERATOR.
71
71
72
72
73
- def cast (self , compiler , connection , ** extra ): # noqa: ARG001
73
+ def cast (self , compiler , connection , as_path = False ): # noqa: ARG001
74
74
output_type = connection .data_types [self .output_field .get_internal_type ()]
75
75
lhs_mql = process_lhs (self , compiler , connection , as_path = False )[0 ]
76
76
if max_length := self .output_field .max_length :
@@ -82,50 +82,63 @@ def cast(self, compiler, connection, **extra): # noqa: ARG001
82
82
lhs_mql = {"$convert" : {"input" : lhs_mql , "to" : output_type }}
83
83
if decimal_places := getattr (self .output_field , "decimal_places" , None ):
84
84
lhs_mql = {"$trunc" : [lhs_mql , decimal_places ]}
85
+
85
86
return lhs_mql
86
87
87
88
88
89
def concat (self , compiler , connection , as_path = False ):
89
90
return self .get_source_expressions ()[0 ].as_mql (compiler , connection , as_path = as_path )
90
91
91
92
92
- def concat_pair (self , compiler , connection , as_path = False ): # noqa: ARG001
93
+ def concat_pair (self , compiler , connection , as_path = False ):
93
94
# null on either side results in null for expression, wrap with coalesce.
94
95
coalesced = self .coalesce ()
96
+ if as_path :
97
+ return {"$expr" : super (ConcatPair , coalesced ).as_mql (compiler , connection , as_path = False )}
95
98
return super (ConcatPair , coalesced ).as_mql (compiler , connection , as_path = False )
96
99
97
100
98
- def cot (self , compiler , connection , as_path = False ): # noqa: ARG001
99
- lhs_mql = process_lhs (self , compiler , connection , as_path = False )
101
+ def cot (self , compiler , connection , as_path = False ):
102
+ lhs_mql = process_lhs (self , compiler , connection , as_path = as_path )
103
+ if as_path :
104
+ return {"$expr" : {"$divide" : [1 , {"$tan" : lhs_mql }]}}
100
105
return {"$divide" : [1 , {"$tan" : lhs_mql }]}
101
106
102
107
103
- def extract (self , compiler , connection , ** extra ): # noqa: ARG001
104
- lhs_mql = process_lhs (self , compiler , connection )
108
+ def extract (self , compiler , connection , as_path = False ):
109
+ lhs_mql = process_lhs (self , compiler , connection , as_path = as_path )
105
110
operator = EXTRACT_OPERATORS .get (self .lookup_name )
106
111
if operator is None :
107
112
raise NotSupportedError (f"{ self .__class__ .__name__ } is not supported." )
108
113
if timezone := self .get_tzname ():
109
114
lhs_mql = {"date" : lhs_mql , "timezone" : timezone }
110
- return {f"${ operator } " : lhs_mql }
115
+ expr = {f"${ operator } " : lhs_mql }
116
+ if as_path :
117
+ return {"$expr" : expr }
118
+ return expr
111
119
112
120
113
- def func (self , compiler , connection , ** extra ): # noqa: ARG001
114
- lhs_mql = process_lhs (self , compiler , connection )
121
+ def func (self , compiler , connection , as_path = False ):
122
+ lhs_mql = process_lhs (self , compiler , connection , as_path = as_path )
115
123
if self .function is None :
116
124
raise NotSupportedError (f"{ self } may need an as_mql() method." )
117
125
operator = MONGO_OPERATORS .get (self .__class__ , self .function .lower ())
126
+ if as_path :
127
+ return {"$expr" : {f"${ operator } " : lhs_mql }}
118
128
return {f"${ operator } " : lhs_mql }
119
129
120
130
121
131
def left (self , compiler , connection , as_path = False ): # noqa: ARG001
122
132
return self .get_substr ().as_mql (compiler , connection , as_path = False )
123
133
124
134
125
- def length (self , compiler , connection , as_path = False ): # noqa: ARG001
135
+ def length (self , compiler , connection , as_path = False ):
126
136
# Check for null first since $strLenCP only accepts strings.
127
137
lhs_mql = process_lhs (self , compiler , connection , as_path = False )
128
- return {"$cond" : {"if" : {"$eq" : [lhs_mql , None ]}, "then" : None , "else" : {"$strLenCP" : lhs_mql }}}
138
+ expr = {"$cond" : {"if" : {"$eq" : [lhs_mql , None ]}, "then" : None , "else" : {"$strLenCP" : lhs_mql }}}
139
+ if as_path :
140
+ return {"$expr" : expr }
141
+ return expr
129
142
130
143
131
144
def log (self , compiler , connection , as_path = False ): # noqa: ARG001
@@ -139,12 +152,15 @@ def now(self, compiler, connection, as_path=False): # noqa: ARG001
139
152
return "$$NOW"
140
153
141
154
142
- def null_if (self , compiler , connection , as_path = False ): # noqa: ARG001
155
+ def null_if (self , compiler , connection , as_path = False ):
143
156
"""Return None if expr1==expr2 else expr1."""
144
157
expr1 , expr2 = (
145
158
expr .as_mql (compiler , connection , as_path = False ) for expr in self .get_source_expressions ()
146
159
)
147
- return {"$cond" : {"if" : {"$eq" : [expr1 , expr2 ]}, "then" : None , "else" : expr1 }}
160
+ expr = {"$cond" : {"if" : {"$eq" : [expr1 , expr2 ]}, "then" : None , "else" : expr1 }}
161
+ if as_path :
162
+ return {"$expr" : expr }
163
+ return expr
148
164
149
165
150
166
def preserve_null (operator ):
0 commit comments