@@ -24,15 +24,14 @@ def convert_expr_to_match(self, expr):
24
24
return [{"$match" : {}}]
25
25
26
26
expr_content = expr_query ["$expr" ]
27
- match_conditions = []
28
- remaining_expr_conditions = []
29
27
30
28
# Handle the expression content
31
- self ._process_expression (expr_content , match_conditions , remaining_expr_conditions )
29
+ match_conditions , remaining_expr_conditions = self ._process_expression (expr_content )
32
30
33
31
# If there are remaining conditions that couldn't be optimized,
34
32
# keep them in an $expr
35
33
if remaining_expr_conditions :
34
+ print (f"Remaining conditions: { remaining_expr_conditions } , match_conditions: { match_conditions } " )
36
35
if len (remaining_expr_conditions ) == 1 :
37
36
expr_conditions = {"$expr" : remaining_expr_conditions [0 ]}
38
37
else :
@@ -44,17 +43,18 @@ def convert_expr_to_match(self, expr):
44
43
else :
45
44
match_conditions .append ({"$match" : expr_conditions })
46
45
46
+ print (f"Original expr: { expr_query } , optimized expr: { match_conditions } " )
47
47
return match_conditions
48
48
49
- def _process_expression (self , expr , match_conditions , remaining_conditions ):
49
+ def _process_expression (self , expr ):
50
50
"""
51
51
Process an expression and extract optimizable conditions.
52
52
53
53
Args:
54
54
expr: The expression to process
55
- match_conditions: List to append optimized match conditions
56
- remaining_conditions: List to append non-optimizable conditions
57
55
"""
56
+ match_conditions = []
57
+ remaining_conditions = []
58
58
if isinstance (expr , dict ):
59
59
# Check if this is an $and operation
60
60
has_and = "$and" in expr
@@ -63,13 +63,17 @@ def _process_expression(self, expr, match_conditions, remaining_conditions):
63
63
# If they fail, they should failover to a remaining conditions list
64
64
# There's probably a better way to do this, but this is a start
65
65
if has_and :
66
- self ._process_logical_conditions (
67
- "$and" , expr ["$and" ], match_conditions , remaining_conditions
66
+ and_match_conditions , and_remaining_conditions = self ._process_logical_conditions (
67
+ "$and" , expr ["$and" ]
68
68
)
69
+ match_conditions .extend (and_match_conditions )
70
+ remaining_conditions .extend (and_remaining_conditions )
69
71
if has_or :
70
- self ._process_logical_conditions (
71
- "$or" , expr ["$or" ], match_conditions , remaining_conditions
72
+ or_match_conditions , or_remaining_conditions = self ._process_logical_conditions (
73
+ "$or" , expr ["$or" ]
72
74
)
75
+ match_conditions .extend (or_match_conditions )
76
+ remaining_conditions .extend (or_remaining_conditions )
73
77
if not has_and and not has_or :
74
78
# Process single condition
75
79
optimized = convert_expression (expr )
@@ -80,19 +84,20 @@ def _process_expression(self, expr, match_conditions, remaining_conditions):
80
84
else :
81
85
# Can't optimize
82
86
remaining_conditions .append (expr )
87
+ return match_conditions , remaining_conditions
83
88
84
89
def _process_logical_conditions (
85
- self , logical_op , logical_conditions , match_conditions , remaining_conditions
90
+ self , logical_op , logical_conditions
86
91
):
87
92
"""
88
93
Process conditions within a logical array.
89
94
90
95
Args:
91
96
logical_conditions: List of conditions within logical operator
92
- match_conditions: List to append optimized match conditions
93
- remaining_conditions: List to append non-optimizable conditions
94
97
"""
95
98
optimized_conditions = []
99
+ match_conditions = []
100
+ remaining_conditions = []
96
101
for condition in logical_conditions :
97
102
if isinstance (condition , dict ):
98
103
if optimized := convert_expression (condition ):
@@ -101,4 +106,8 @@ def _process_logical_conditions(
101
106
remaining_conditions .append (condition )
102
107
else :
103
108
remaining_conditions .append (condition )
104
- match_conditions .append ({"$match" : {logical_op : optimized_conditions }})
109
+ if optimized_conditions :
110
+ match_conditions .append ({"$match" : {logical_op : optimized_conditions }})
111
+ else :
112
+ remaining_conditions = [{logical_op : logical_conditions }]
113
+ return match_conditions , remaining_conditions
0 commit comments