|
| 1 | +""" |
| 2 | +Logic for replacing `UNMASK(x) == literal` (and similar expressions) with |
| 3 | +`x == MASK(literal)`. |
| 4 | +""" |
| 5 | + |
| 6 | +__all__ = ["MaskLiteralComparisonShuttle"] |
| 7 | + |
| 8 | +import pydough.pydough_operators as pydop |
| 9 | +from pydough.relational import ( |
| 10 | + CallExpression, |
| 11 | + LiteralExpression, |
| 12 | + RelationalExpression, |
| 13 | + RelationalExpressionShuttle, |
| 14 | +) |
| 15 | +from pydough.types import ArrayType, PyDoughType, UnknownType |
| 16 | + |
| 17 | + |
| 18 | +class MaskLiteralComparisonShuttle(RelationalExpressionShuttle): |
| 19 | + """ |
| 20 | + A shuttle that recursively performs the following replacements: |
| 21 | + - `UNMASK(x) == literal` -> `x == MASK(literal)` |
| 22 | + - `literal == UNMASK(x)` -> `MASK(literal) == x` |
| 23 | + - `UNMASK(x) != literal` -> `x != MASK(literal)` |
| 24 | + - `literal != UNMASK(x)` -> `MASK(literal) != x` |
| 25 | + - `UNMASK(x) IN (literal1, ..., literalN)` -> `x IN (MASK(literal1), ..., MASK(literalN))` |
| 26 | + """ |
| 27 | + |
| 28 | + def rewrite_masked_literal_comparison( |
| 29 | + self, |
| 30 | + original_call: CallExpression, |
| 31 | + call_arg: CallExpression, |
| 32 | + literal_arg: LiteralExpression, |
| 33 | + ) -> CallExpression: |
| 34 | + """ |
| 35 | + Performs a rewrite of a comparison between a call to UNMASK and a |
| 36 | + literal, which is either equality, inequality, or containment. |
| 37 | +
|
| 38 | + Args: |
| 39 | + `original_call`: The original call expression representing the |
| 40 | + comparison. |
| 41 | + `call_arg`: The argument to the comparison that is a call to |
| 42 | + UNMASK, which is treated as the left-hand side of the comparison. |
| 43 | + `literal_arg`: The argument to the comparison that is a literal, |
| 44 | + which is treated as the right-hand side of the comparison. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + A new call expression representing the rewritten comparison, or |
| 48 | + the original call expression if no rewrite was performed. |
| 49 | + """ |
| 50 | + |
| 51 | + # Verify that the call argument is indeed an UNMASK operation, otherwise |
| 52 | + # fall back to the original. |
| 53 | + if ( |
| 54 | + not isinstance(call_arg.op, pydop.MaskedExpressionFunctionOperator) |
| 55 | + or not call_arg.op.is_unmask |
| 56 | + ): |
| 57 | + return original_call |
| 58 | + |
| 59 | + masked_literal: RelationalExpression |
| 60 | + |
| 61 | + if original_call.op in (pydop.EQU, pydop.NEQ): |
| 62 | + # If the operation is equality or inequality, we can simply wrap the |
| 63 | + # literal in a call to MASK by toggling is_unmask to False. |
| 64 | + masked_literal = CallExpression( |
| 65 | + pydop.MaskedExpressionFunctionOperator( |
| 66 | + call_arg.op.masking_metadata, False |
| 67 | + ), |
| 68 | + call_arg.data_type, |
| 69 | + [literal_arg], |
| 70 | + ) |
| 71 | + elif original_call.op == pydop.ISIN and isinstance( |
| 72 | + literal_arg.value, (list, tuple) |
| 73 | + ): |
| 74 | + # If the operation is containment, and the literal is a list/tuple, |
| 75 | + # we need to build a list by wrapping each element of the tuple in |
| 76 | + # a MASK call. |
| 77 | + inner_type: PyDoughType |
| 78 | + if isinstance(literal_arg.data_type, ArrayType): |
| 79 | + inner_type = literal_arg.data_type.elem_type |
| 80 | + else: |
| 81 | + inner_type = UnknownType() |
| 82 | + masked_literal = LiteralExpression( |
| 83 | + [ |
| 84 | + CallExpression( |
| 85 | + pydop.MaskedExpressionFunctionOperator( |
| 86 | + call_arg.op.masking_metadata, False |
| 87 | + ), |
| 88 | + call_arg.data_type, |
| 89 | + [LiteralExpression(v, inner_type)], |
| 90 | + ) |
| 91 | + for v in literal_arg.value |
| 92 | + ], |
| 93 | + original_call.data_type, |
| 94 | + ) |
| 95 | + else: |
| 96 | + # Otherwise, return the original. |
| 97 | + return original_call |
| 98 | + |
| 99 | + # Now that we have the masked literal, we can return a new call |
| 100 | + # expression with the same operators as before, but where the left hand |
| 101 | + # side argument is the expression that was being unmasked, and the right |
| 102 | + # hand side is the masked literal. |
| 103 | + return CallExpression( |
| 104 | + original_call.op, |
| 105 | + original_call.data_type, |
| 106 | + [call_arg.inputs[0], masked_literal], |
| 107 | + ) |
| 108 | + |
| 109 | + def visit_call_expression( |
| 110 | + self, call_expression: CallExpression |
| 111 | + ) -> RelationalExpression: |
| 112 | + # If the call expression is equality or inequality, dispatch to the |
| 113 | + # rewrite logic if one argument is a call expression and the other is |
| 114 | + # a literal. |
| 115 | + if call_expression.op in (pydop.EQU, pydop.NEQ): |
| 116 | + if isinstance(call_expression.inputs[0], CallExpression) and isinstance( |
| 117 | + call_expression.inputs[1], LiteralExpression |
| 118 | + ): |
| 119 | + call_expression = self.rewrite_masked_literal_comparison( |
| 120 | + call_expression, |
| 121 | + call_expression.inputs[0], |
| 122 | + call_expression.inputs[1], |
| 123 | + ) |
| 124 | + if isinstance(call_expression.inputs[1], CallExpression) and isinstance( |
| 125 | + call_expression.inputs[0], LiteralExpression |
| 126 | + ): |
| 127 | + call_expression = self.rewrite_masked_literal_comparison( |
| 128 | + call_expression, |
| 129 | + call_expression.inputs[1], |
| 130 | + call_expression.inputs[0], |
| 131 | + ) |
| 132 | + |
| 133 | + # If the call expression is containment, dispatch to the rewrite logic |
| 134 | + # if the first argument is a call expression and the second is a |
| 135 | + # literal. |
| 136 | + if ( |
| 137 | + call_expression.op == pydop.ISIN |
| 138 | + and isinstance(call_expression.inputs[0], CallExpression) |
| 139 | + and isinstance(call_expression.inputs[1], LiteralExpression) |
| 140 | + ): |
| 141 | + call_expression = self.rewrite_masked_literal_comparison( |
| 142 | + call_expression, call_expression.inputs[0], call_expression.inputs[1] |
| 143 | + ) |
| 144 | + |
| 145 | + # Regardless of whether the rewrite occurred or not, invoke the regular |
| 146 | + # logic which will recursively transform the arguments. |
| 147 | + return super().visit_call_expression(call_expression) |
0 commit comments