Example #1
0
    def test_logical_or(self):
        tpl_exp = TupleValueExpression(0)
        const_exp = ConstantValueExpression(1)

        comparison_expression_left = ComparisonExpression(
            ExpressionType.COMPARE_EQUAL, tpl_exp, const_exp)
        tpl_exp = TupleValueExpression(0)
        const_exp = ConstantValueExpression(1)
        comparison_expression_right = ComparisonExpression(
            ExpressionType.COMPARE_GREATER, tpl_exp, const_exp)
        logical_expr = LogicalExpression(ExpressionType.LOGICAL_OR,
                                         comparison_expression_left,
                                         comparison_expression_right)

        frame_1 = Frame(1, np.ones((1, 1)), None)
        frame_2 = Frame(2, 2 * np.ones((1, 1)), None)
        frame_3 = Frame(3, 3 * np.ones((1, 1)), None)
        input_batch = FrameBatch(frames=[
            frame_1,
            frame_2,
            frame_3,
        ],
                                 info=None)

        expected_value = [[True], [True], [True]]
        output_value = logical_expr.evaluate(input_batch)
        self.assertEqual(expected_value, output_value)
Example #2
0
    def test_logical_or(self):
        const_exp1 = ConstantValueExpression(1)
        const_exp2 = ConstantValueExpression(1)

        comparison_expression_left = ComparisonExpression(
            ExpressionType.COMPARE_EQUAL,
            const_exp1,
            const_exp2
        )
        const_exp1 = ConstantValueExpression(1)
        const_exp2 = ConstantValueExpression(2)
        comparison_expression_right = ComparisonExpression(
            ExpressionType.COMPARE_GREATER,
            const_exp1,
            const_exp2
        )
        logical_expr = LogicalExpression(
            ExpressionType.LOGICAL_OR,
            comparison_expression_left,
            comparison_expression_right
        )
        self.assertEqual(
            [True],
            logical_expr.evaluate(None).frames[0].tolist()
        )
Example #3
0
    def test_short_circuiting_or_partial(self):
        # tests whether right-hand side is partially executed with or
        tup_val_exp_l = TupleValueExpression(col_name=0)
        tup_val_exp_r = TupleValueExpression(col_name=1)

        comp_exp_l = ComparisonExpression(
            ExpressionType.COMPARE_EQUAL,
            tup_val_exp_l,
            tup_val_exp_r
        )
        comp_exp_r = Mock(spec=ComparisonExpression)
        comp_exp_r.evaluate = Mock(return_value=Mock(frames=[[True], [False]]))

        logical_exp = LogicalExpression(
            ExpressionType.LOGICAL_OR,
            comp_exp_l,
            comp_exp_r
        )

        tuples = Batch(pd.DataFrame(
            {0: [1, 2, 3, 4], 1: [5, 6, 3, 4]}))
        self.assertEqual(
            [True, False, True, True],
            logical_exp.evaluate(tuples).frames[0].tolist()
        )
        comp_exp_r.evaluate.assert_called_once_with(tuples, mask=[0, 1])
Example #4
0
    def test_short_circuiting_or_complete(self):
        # tests whether right-hand side is bypassed completely with or
        tup_val_exp_l = TupleValueExpression(col_name=0)
        tup_val_exp_r = TupleValueExpression(col_name=1)

        comp_exp_l = ComparisonExpression(
            ExpressionType.COMPARE_EQUAL,
            tup_val_exp_l,
            tup_val_exp_r
        )
        comp_exp_r = Mock(spec=ComparisonExpression)

        logical_exp = LogicalExpression(
            ExpressionType.LOGICAL_OR,
            comp_exp_l,
            comp_exp_r
        )

        tuples = Batch(pd.DataFrame(
            {0: [1, 2, 3], 1: [1, 2, 3]}))
        self.assertEqual(
            [True, True, True],
            logical_exp.evaluate(tuples).frames[0].tolist()
        )
        comp_exp_r.evaluate.assert_not_called()
Example #5
0
def visitLogicalExpression(self, ctx: evaql_parser.LogicalExpressionContext):
    if len(ctx.children) < 3:
        # error scenario, should have 3 children
        return None
    left = self.visit(ctx.getChild(0))
    op = self.visit(ctx.getChild(1))
    right = self.visit(ctx.getChild(2))
    return LogicalExpression(op, left, right)
Example #6
0
    def test_logical_not(self):
        const_exp1 = ConstantValueExpression(0)
        const_exp2 = ConstantValueExpression(1)

        comparison_expression_right = ComparisonExpression(
            ExpressionType.COMPARE_GREATER, const_exp1, const_exp2)
        logical_expr = LogicalExpression(ExpressionType.LOGICAL_NOT, None,
                                         comparison_expression_right)
        self.assertEqual([True], logical_expr.evaluate(None))
Example #7
0
    def test_logical_not(self):
        tpl_exp = TupleValueExpression(0)
        const_exp = ConstantValueExpression(1)

        comparison_expression_right = ComparisonExpression(
            ExpressionType.COMPARE_GREATER, tpl_exp, const_exp)
        logical_expr = LogicalExpression(ExpressionType.LOGICAL_NOT, None,
                                         comparison_expression_right)
        tuple1 = [[1], 2, 3]
        self.assertEqual([True], logical_expr.evaluate(tuple1, None))
Example #8
0
    def test_should_return_false_for_unequal_expressions(self):
        const_exp1 = ConstantValueExpression(0)
        const_exp2 = ConstantValueExpression(1)
        func_expr = FunctionExpression(lambda x: x + 1)
        cmpr_exp = ComparisonExpression(ExpressionType.COMPARE_NEQ, const_exp1,
                                        const_exp2)
        tuple_expr = TupleValueExpression(col_name='id')
        aggr_expr = AggregationExpression(ExpressionType.AGGREGATION_MAX, None,
                                          tuple_expr)
        logical_expr = LogicalExpression(ExpressionType.LOGICAL_OR, cmpr_exp,
                                         cmpr_exp)

        self.assertNotEqual(const_exp1, const_exp2)
        self.assertNotEqual(cmpr_exp, const_exp1)
        self.assertNotEqual(func_expr, cmpr_exp)
        self.assertNotEqual(tuple_expr, aggr_expr)
        self.assertNotEqual(aggr_expr, tuple_expr)
        self.assertNotEqual(tuple_expr, cmpr_exp)
        self.assertNotEqual(logical_expr, cmpr_exp)