|
| 1 | +"""Expression To Match Converters""" |
| 2 | + |
| 3 | + |
| 4 | +class BaseConverter: |
| 5 | + """ |
| 6 | + Base class for optimizers that handle specific operations in MQL queries. |
| 7 | + """ |
| 8 | + |
| 9 | + @classmethod |
| 10 | + def convert(cls, expr): |
| 11 | + raise NotImplementedError("Subclasses must implement this method.") |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def is_simple_value(cls, value): |
| 15 | + """Is the value is a simple type (not a dict)?""" |
| 16 | + if value is None: |
| 17 | + return True |
| 18 | + if isinstance(value, str) and value.startswith("$"): |
| 19 | + return False |
| 20 | + if isinstance(value, (list, tuple, set)): |
| 21 | + return all(cls.is_simple_value(v) for v in value) |
| 22 | + # TODO: Support `$getField` conversion. |
| 23 | + return not isinstance(value, dict) |
| 24 | + |
| 25 | + |
| 26 | +class BinaryConverter(BaseConverter): |
| 27 | + """ |
| 28 | + Base class for optimizers that handle binary expression operations in MQL queries. |
| 29 | + """ |
| 30 | + |
| 31 | + operator: str |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def convert(cls, args): |
| 35 | + if isinstance(args, list) and len(args) == 2: |
| 36 | + field_expr, value = args |
| 37 | + # Check if first argument is a simple field reference. |
| 38 | + if ( |
| 39 | + isinstance(field_expr, str) |
| 40 | + and field_expr.startswith("$") |
| 41 | + and cls.is_simple_value(value) |
| 42 | + ): |
| 43 | + field_name = field_expr[1:] # Remove the $ prefix. |
| 44 | + if cls.operator == "$eq": |
| 45 | + return {field_name: value} |
| 46 | + return {field_name: {cls.operator: value}} |
| 47 | + |
| 48 | + return None |
| 49 | + |
| 50 | + |
| 51 | +class EqConverter(BinaryConverter): |
| 52 | + """Convert $eq operation to a $match compatible query. |
| 53 | +
|
| 54 | + For example:: |
| 55 | + "$expr": { |
| 56 | + {"$eq": ["$status", "active"]} |
| 57 | + } |
| 58 | + is converted to:: |
| 59 | + {"status": "active"} |
| 60 | + """ |
| 61 | + |
| 62 | + operator = "$eq" |
| 63 | + |
| 64 | + |
| 65 | +class GtConverter(BinaryConverter): |
| 66 | + """Convert $gt operation to a $match compatible query. |
| 67 | +
|
| 68 | + For example:: |
| 69 | + "$expr": { |
| 70 | + {"$gt": ["$price", 100]} |
| 71 | + } |
| 72 | + is converted to:: |
| 73 | + {"$gt": ["price", 100]} |
| 74 | + """ |
| 75 | + |
| 76 | + operator = "$gt" |
| 77 | + |
| 78 | + |
| 79 | +class GteConverter(BinaryConverter): |
| 80 | + """Convert $gte operation to a $match compatible query. |
| 81 | +
|
| 82 | + For example:: |
| 83 | + "$expr": { |
| 84 | + {"$gte": ["$price", 100]} |
| 85 | + } |
| 86 | + is converted to:: |
| 87 | + {"price": {"$gte", 100}} |
| 88 | + """ |
| 89 | + |
| 90 | + operator = "$gte" |
| 91 | + |
| 92 | + |
| 93 | +class LtConverter(BinaryConverter): |
| 94 | + """Convert $lt operation to a $match compatible query. |
| 95 | +
|
| 96 | + For example:: |
| 97 | + "$expr": { |
| 98 | + {"$lt": ["$price", 100]} |
| 99 | + } |
| 100 | + is converted to:: |
| 101 | + {"$lt": ["price", 100]} |
| 102 | + """ |
| 103 | + |
| 104 | + operator = "$lt" |
| 105 | + |
| 106 | + |
| 107 | +class LteConverter(BinaryConverter): |
| 108 | + """Convert $lte operation to a $match compatible query. |
| 109 | +
|
| 110 | + For example:: |
| 111 | + "$expr": { |
| 112 | + {"$lte": ["$price", 100]} |
| 113 | + } |
| 114 | + is converted to:: |
| 115 | + {"price": {"$lte", 100}} |
| 116 | + """ |
| 117 | + |
| 118 | + operator = "$lte" |
| 119 | + |
| 120 | + |
| 121 | +class InConverter(BaseConverter): |
| 122 | + """Convert $in operation to a $match compatible query. |
| 123 | +
|
| 124 | + For example:: |
| 125 | + "$expr": { |
| 126 | + {"$in": ["$category", ["electronics", "books"]]} |
| 127 | + } |
| 128 | + is converted to:: |
| 129 | + {"category": {"$in": ["electronics", "books"]}} |
| 130 | + """ |
| 131 | + |
| 132 | + @classmethod |
| 133 | + def convert(cls, in_args): |
| 134 | + if isinstance(in_args, list) and len(in_args) == 2: |
| 135 | + field_expr, values = in_args |
| 136 | + |
| 137 | + # Check if first argument is a simple field reference |
| 138 | + if isinstance(field_expr, str) and field_expr.startswith("$"): |
| 139 | + field_name = field_expr[1:] # Remove the $ prefix |
| 140 | + if isinstance(values, (list, tuple, set)) and all( |
| 141 | + cls.is_simple_value(v) for v in values |
| 142 | + ): |
| 143 | + return {field_name: {"$in": values}} |
| 144 | + |
| 145 | + return None |
| 146 | + |
| 147 | + |
| 148 | +class LogicalConverter(BaseConverter): |
| 149 | + """Generic for converting logical operations to a $match compatible query.""" |
| 150 | + |
| 151 | + @classmethod |
| 152 | + def convert(cls, combined_conditions): |
| 153 | + if isinstance(combined_conditions, list): |
| 154 | + optimized_conditions = [] |
| 155 | + for condition in combined_conditions: |
| 156 | + if isinstance(condition, dict) and len(condition) == 1: |
| 157 | + if optimized_condition := convert_expression(condition): |
| 158 | + optimized_conditions.append(optimized_condition) |
| 159 | + else: |
| 160 | + # Any failure should stop optimization |
| 161 | + return None |
| 162 | + if optimized_conditions: |
| 163 | + return {cls._logical_op: optimized_conditions} |
| 164 | + return None |
| 165 | + |
| 166 | + |
| 167 | +class OrConverter(LogicalConverter): |
| 168 | + """Convert $or operation to a $match compatible query. |
| 169 | +
|
| 170 | + For example:: |
| 171 | + "$expr": { |
| 172 | + "$or": [ |
| 173 | + {"$eq": ["$status", "active"]}, |
| 174 | + {"$in": ["$category", ["electronics", "books"]]}, |
| 175 | + ] |
| 176 | + } |
| 177 | + is converted to:: |
| 178 | + "$or": [ |
| 179 | + {"status": "active"}, |
| 180 | + {"category": {"$in": ["electronics", "books"]}}, |
| 181 | + ] |
| 182 | + """ |
| 183 | + |
| 184 | + _logical_op = "$or" |
| 185 | + |
| 186 | + |
| 187 | +class AndConverter(LogicalConverter): |
| 188 | + """Convert $and operation to a $match compatible query. |
| 189 | +
|
| 190 | + For example:: |
| 191 | + "$expr": { |
| 192 | + "$and": [ |
| 193 | + {"$eq": ["$status", "active"]}, |
| 194 | + {"$in": ["$category", ["electronics", "books"]]}, |
| 195 | + {"$eq": ["$verified", True]}, |
| 196 | + ] |
| 197 | + } |
| 198 | + is converted to:: |
| 199 | + "$and": [ |
| 200 | + {"status": "active"}, |
| 201 | + {"category": {"$in": ["electronics", "books"]}}, |
| 202 | + {"verified": True}, |
| 203 | + ] |
| 204 | + """ |
| 205 | + |
| 206 | + _logical_op = "$and" |
| 207 | + |
| 208 | + |
| 209 | +OPTIMIZABLE_OPS = { |
| 210 | + "$eq": EqConverter, |
| 211 | + "$in": InConverter, |
| 212 | + "$and": AndConverter, |
| 213 | + "$or": OrConverter, |
| 214 | + "$gt": GtConverter, |
| 215 | + "$gte": GteConverter, |
| 216 | + "$lt": LtConverter, |
| 217 | + "$lte": LteConverter, |
| 218 | +} |
| 219 | + |
| 220 | + |
| 221 | +def convert_expression(expr): |
| 222 | + """ |
| 223 | + Optimize an MQL expression by extracting optimizable conditions. |
| 224 | +
|
| 225 | + Args: |
| 226 | + expr: Dictionary containing the MQL expression |
| 227 | +
|
| 228 | + Returns: |
| 229 | + Optimized match condition or None if not optimizable |
| 230 | + """ |
| 231 | + if isinstance(expr, dict) and len(expr) == 1: |
| 232 | + op = next(iter(expr.keys())) |
| 233 | + if op in OPTIMIZABLE_OPS: |
| 234 | + return OPTIMIZABLE_OPS[op].convert(expr[op]) |
| 235 | + return None |
0 commit comments