Exemplo n.º 1
0
    def test_comparison_compare_lesser(self):
        const_exp1 = ConstantValueExpression(0)
        const_exp2 = ConstantValueExpression(2)

        cmpr_exp = ComparisonExpression(ExpressionType.COMPARE_LESSER,
                                        const_exp1, const_exp2)
        self.assertEqual([True], cmpr_exp.evaluate(None).frames[0].tolist())
Exemplo n.º 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()
        )
Exemplo n.º 3
0
    def test_multiple_join_with_multiple_ON(self):
        select_query = '''SELECT table1.a FROM table1 JOIN table2
            ON table1.a = table2.a JOIN table3
            ON table3.a = table1.a WHERE table1.a <= 5'''
        parser = Parser()
        select_stmt = parser.parse(select_query)[0]
        table1_col_a = TupleValueExpression('a', 'table1')
        table2_col_a = TupleValueExpression('a', 'table2')
        table3_col_a = TupleValueExpression('a', 'table3')
        select_list = [table1_col_a]
        child_join = TableRef(
            JoinNode(TableRef(TableInfo('table1')),
                     TableRef(TableInfo('table2')),
                     predicate=ComparisonExpression(
                         ExpressionType.COMPARE_EQUAL, table1_col_a,
                         table2_col_a),
                     join_type=JoinType.INNER_JOIN))

        from_table = TableRef(
            JoinNode(child_join,
                     TableRef(TableInfo('table3')),
                     predicate=ComparisonExpression(
                         ExpressionType.COMPARE_EQUAL, table3_col_a,
                         table1_col_a),
                     join_type=JoinType.INNER_JOIN))
        where_clause = ComparisonExpression(ExpressionType.COMPARE_LEQ,
                                            table1_col_a,
                                            ConstantValueExpression(5))
        expected_stmt = SelectStatement(select_list, from_table, where_clause)
        self.assertEqual(select_stmt, expected_stmt)
Exemplo n.º 4
0
    def test_if_expr_tree_is_equal(self):
        const_exp1 = ConstantValueExpression(0)
        const_exp2 = ConstantValueExpression(0)
        columnName1 = TupleValueExpression(col_name='DATA')
        columnName2 = TupleValueExpression(col_name='DATA')

        aggr_expr1 = AggregationExpression(ExpressionType.AGGREGATION_AVG,
                                           None, columnName1)
        aggr_expr2 = AggregationExpression(ExpressionType.AGGREGATION_AVG,
                                           None, columnName2)
        cmpr_exp1 = ComparisonExpression(ExpressionType.COMPARE_NEQ,
                                         aggr_expr1, const_exp1)
        cmpr_exp2 = ComparisonExpression(ExpressionType.COMPARE_NEQ,
                                         aggr_expr2, const_exp2)

        self.assertEqual(cmpr_exp1, cmpr_exp2)
Exemplo n.º 5
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_l.col_alias = 0
        tup_val_exp_r = TupleValueExpression(col_name=1)
        tup_val_exp_r.col_alias = 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])
Exemplo n.º 6
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_l.col_alias = 0
        tup_val_exp_r = TupleValueExpression(col_name=1)
        tup_val_exp_r.col_alias = 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()
Exemplo n.º 7
0
    def test_join(self):
        select_query = '''SELECT table1.a FROM table1 JOIN table2
                    ON table1.a = table2.a; '''
        parser = Parser()
        select_stmt = parser.parse(select_query)[0]
        table1_col_a = TupleValueExpression('a', 'table1')
        table2_col_a = TupleValueExpression('a', 'table2')
        select_list = [table1_col_a]
        from_table = TableRef(
            JoinNode(TableRef(TableInfo('table1')),
                     TableRef(TableInfo('table2')),
                     predicate=ComparisonExpression(
                         ExpressionType.COMPARE_EQUAL, table1_col_a,
                         table2_col_a),
                     join_type=JoinType.INNER_JOIN))
        expected_stmt = SelectStatement(select_list, from_table)

        self.assertEqual(select_stmt, expected_stmt)
Exemplo n.º 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, name='test')
        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)
Exemplo n.º 9
0
    def test_comparison_compare_contains(self):
        const_exp1 = ConstantValueExpression([1, 2], ColumnType.NDARRAY)
        const_exp2 = ConstantValueExpression([1, 5], ColumnType.NDARRAY)
        const_exp3 = ConstantValueExpression([1, 2, 3, 4], ColumnType.NDARRAY)

        cmpr_exp1 = ComparisonExpression(ExpressionType.COMPARE_CONTAINS,
                                         const_exp3, const_exp1)

        self.assertEqual([True], cmpr_exp1.evaluate(None).frames[0].tolist())

        cmpr_exp2 = ComparisonExpression(ExpressionType.COMPARE_CONTAINS,
                                         const_exp3, const_exp2)

        self.assertEqual([False], cmpr_exp2.evaluate(None).frames[0].tolist())
Exemplo n.º 10
0
    def test_comparison_compare_geq(self):
        const_exp1 = ConstantValueExpression(1)
        const_exp2 = ConstantValueExpression(1)
        const_exp3 = ConstantValueExpression(0)

        cmpr_exp1 = ComparisonExpression(ExpressionType.COMPARE_GEQ,
                                         const_exp1, const_exp2)

        cmpr_exp2 = ComparisonExpression(ExpressionType.COMPARE_GEQ,
                                         const_exp1, const_exp3)
        # checking equal
        self.assertEqual([True], cmpr_exp1.evaluate(None).frames[0].tolist())
        # checking greater equal
        self.assertEqual([True], cmpr_exp2.evaluate(None).frames[0].tolist())
Exemplo n.º 11
0
 def visitBinaryComparisonPredicate(
         self, ctx: evaql_parser.BinaryComparisonPredicateContext):
     left = self.visit(ctx.left)
     right = self.visit(ctx.right)
     op = self.visit(ctx.comparisonOperator())
     return ComparisonExpression(op, left, right)