@@ -428,32 +428,22 @@ def _get_aggregate_expressions(self, expr):
428
428
stack .extend (expr .get_source_expressions ())
429
429
430
430
def get_project_fields (self , columns = None , ordering = None ):
431
- fields = {}
431
+ fields = defaultdict ( dict )
432
432
for name , expr in columns or []:
433
+ collection = expr .alias if isinstance (expr , Col ) else None
433
434
try :
434
- column = expr .target .column
435
- except AttributeError :
436
- # Generate the MQL for an annotation.
437
- try :
438
- fields [name ] = expr .as_mql (self , self .connection )
439
- except EmptyResultSet :
440
- fields [name ] = Value (False ).as_mql (self , self .connection )
441
- except FullResultSet :
442
- fields [name ] = Value (True ).as_mql (self , self .connection )
443
- else :
444
- # If name != column, then this is an annotatation referencing
445
- # another column.
446
- fields [name ] = 1 if name == column else f"${ column } "
447
- if fields :
448
- # Add related fields.
449
- for alias in self .query .alias_map :
450
- if self .query .alias_refcount [alias ] and self .collection_name != alias :
451
- fields [alias ] = 1
435
+ fields [collection ][name ] = expr .as_mql (self , self .connection )
436
+ except EmptyResultSet :
437
+ fields [collection ][name ] = Value (False ).as_mql (self , self .connection )
438
+ except FullResultSet :
439
+ fields [collection ][name ] = Value (True ).as_mql (self , self .connection )
440
+ # Unwrap annotations.
441
+ fields .update (fields .pop (None , {}))
442
+ # Unwrap main collection's fields.
443
+ fields .update (fields .pop (self .collection_name , {}))
444
+ if fields and ordering :
452
445
# Add order_by() fields.
453
- for alias , expression in ordering or []:
454
- nested_entity = alias .split ("." , 1 )[0 ] if "." in alias else None
455
- if alias not in fields and nested_entity not in fields :
456
- fields [alias ] = expression .as_mql (self , self .connection )
446
+ fields .update ({alias : expr .as_mql (self , self .connection ) for alias , expr in ordering })
457
447
return fields
458
448
459
449
def _get_ordering (self ):
0 commit comments