7070# TEST THAT HAVE THOSE OPERATOR.
7171
7272
73- def cast (self , compiler , connection , ** extra ): # noqa: ARG001
73+ def cast (self , compiler , connection , as_path = False ): # noqa: ARG001
7474 output_type = connection .data_types [self .output_field .get_internal_type ()]
7575 lhs_mql = process_lhs (self , compiler , connection , as_path = False )[0 ]
7676 if max_length := self .output_field .max_length :
@@ -82,50 +82,63 @@ def cast(self, compiler, connection, **extra): # noqa: ARG001
8282 lhs_mql = {"$convert" : {"input" : lhs_mql , "to" : output_type }}
8383 if decimal_places := getattr (self .output_field , "decimal_places" , None ):
8484 lhs_mql = {"$trunc" : [lhs_mql , decimal_places ]}
85+
8586 return lhs_mql
8687
8788
8889def concat (self , compiler , connection , as_path = False ):
8990 return self .get_source_expressions ()[0 ].as_mql (compiler , connection , as_path = as_path )
9091
9192
92- def concat_pair (self , compiler , connection , as_path = False ): # noqa: ARG001
93+ def concat_pair (self , compiler , connection , as_path = False ):
9394 # null on either side results in null for expression, wrap with coalesce.
9495 coalesced = self .coalesce ()
96+ if as_path :
97+ return {"$expr" : super (ConcatPair , coalesced ).as_mql (compiler , connection , as_path = False )}
9598 return super (ConcatPair , coalesced ).as_mql (compiler , connection , as_path = False )
9699
97100
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 }]}}
100105 return {"$divide" : [1 , {"$tan" : lhs_mql }]}
101106
102107
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 )
105110 operator = EXTRACT_OPERATORS .get (self .lookup_name )
106111 if operator is None :
107112 raise NotSupportedError (f"{ self .__class__ .__name__ } is not supported." )
108113 if timezone := self .get_tzname ():
109114 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
111119
112120
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 )
115123 if self .function is None :
116124 raise NotSupportedError (f"{ self } may need an as_mql() method." )
117125 operator = MONGO_OPERATORS .get (self .__class__ , self .function .lower ())
126+ if as_path :
127+ return {"$expr" : {f"${ operator } " : lhs_mql }}
118128 return {f"${ operator } " : lhs_mql }
119129
120130
121131def left (self , compiler , connection , as_path = False ): # noqa: ARG001
122132 return self .get_substr ().as_mql (compiler , connection , as_path = False )
123133
124134
125- def length (self , compiler , connection , as_path = False ): # noqa: ARG001
135+ def length (self , compiler , connection , as_path = False ):
126136 # Check for null first since $strLenCP only accepts strings.
127137 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
129142
130143
131144def log (self , compiler , connection , as_path = False ): # noqa: ARG001
@@ -139,12 +152,15 @@ def now(self, compiler, connection, as_path=False): # noqa: ARG001
139152 return "$$NOW"
140153
141154
142- def null_if (self , compiler , connection , as_path = False ): # noqa: ARG001
155+ def null_if (self , compiler , connection , as_path = False ):
143156 """Return None if expr1==expr2 else expr1."""
144157 expr1 , expr2 = (
145158 expr .as_mql (compiler , connection , as_path = False ) for expr in self .get_source_expressions ()
146159 )
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
148164
149165
150166def preserve_null (operator ):
0 commit comments