def test_multi_part_join(self): self.assert_parsed_select( 'SELECT t1.foo, t2.bar, t3.baz ' 'FROM table1 t1 LEFT OUTER JOIN EACH table2 t2 ON t1.id = t2.id ' 'JOIN table3 t3 ON t3.id = t1.id', tq_ast.Select( [ tq_ast.SelectField(tq_ast.ColumnId('t1.foo'), None, None), tq_ast.SelectField(tq_ast.ColumnId('t2.bar'), None, None), tq_ast.SelectField(tq_ast.ColumnId('t3.baz'), None, None) ], tq_ast.Join(tq_ast.TableId('table1', 't1'), [ tq_ast.PartialJoin( tq_ast.TableId('table2', 't2'), tq_ast.JoinType.LEFT_OUTER, tq_ast.BinaryOperator('=', tq_ast.ColumnId('t1.id'), tq_ast.ColumnId('t2.id')), ), tq_ast.PartialJoin( tq_ast.TableId('table3', 't3'), tq_ast.JoinType.INNER, tq_ast.BinaryOperator('=', tq_ast.ColumnId('t3.id'), tq_ast.ColumnId('t1.id')), ), ]), None, None, None, None, None, None))
def test_parens(self): self.assert_parsed_select( 'SELECT 2 + (3 * 4)', tq_ast.Select([ tq_ast.SelectField( tq_ast.BinaryOperator( '+', literal(2), tq_ast.BinaryOperator('*', literal(3), literal(4))), None, None) ], None, None, None, None, None, None, None))
def test_arithmetic_operator_parsing(self): self.assert_parsed_select( 'SELECT 1 * 2 + 3 / 4', tq_ast.Select([ tq_ast.SelectField( tq_ast.BinaryOperator( '+', tq_ast.BinaryOperator('*', literal(1), literal(2)), tq_ast.BinaryOperator('/', literal(3), literal(4))), None, None) ], None, None, None, None, None, None, None))
def test_having(self): self.assert_parsed_select( 'SELECT foo + 2 FROM bar HAVING foo > 3', tq_ast.Select([ tq_ast.SelectField( tq_ast.BinaryOperator('+', tq_ast.ColumnId('foo'), tq_ast.Literal(2)), None, None) ], tq_ast.TableId('bar', None), None, None, tq_ast.BinaryOperator('>', tq_ast.ColumnId('foo'), tq_ast.Literal(3)), None, None, None))
def test_multi_clause_case(self): self.assert_parsed_select( 'SELECT CASE WHEN x = 4 THEN 16 WHEN x = 5 THEN 25 END', tq_ast.Select([ tq_ast.SelectField( tq_ast.CaseExpression([ tq_ast.CaseClause( tq_ast.BinaryOperator('=', tq_ast.ColumnId('x'), tq_ast.Literal(4)), tq_ast.Literal(16)), tq_ast.CaseClause( tq_ast.BinaryOperator('=', tq_ast.ColumnId('x'), tq_ast.Literal(5)), tq_ast.Literal(25)), ]), None, None) ], None, None, None, None, None, None, None))
def test_select_comparison(self): self.assert_parsed_select( 'SELECT foo = bar FROM baz', tq_ast.Select([ tq_ast.SelectField( tq_ast.BinaryOperator('=', tq_ast.ColumnId('foo'), tq_ast.ColumnId('bar')), None, None) ], tq_ast.TableId('baz', None), None, None, None, None, None, None))
def test_multiple_select(self): self.assert_parsed_select( 'SELECT a AS foo, b bar, a + 1 baz FROM test_table', tq_ast.Select([ tq_ast.SelectField(tq_ast.ColumnId('a'), 'foo', None), tq_ast.SelectField(tq_ast.ColumnId('b'), 'bar', None), tq_ast.SelectField( tq_ast.BinaryOperator('+', tq_ast.ColumnId('a'), tq_ast.Literal(1)), 'baz', None) ], tq_ast.TableId('test_table', None), None, None, None, None, None, None))
def test_join_each(self): self.assert_parsed_select( 'SELECT 0 FROM table1 t1 JOIN EACH table2 t2 ON t1.foo = t2.bar', tq_ast.Select( [tq_ast.SelectField(tq_ast.Literal(0), None, None)], tq_ast.Join(tq_ast.TableId('table1', 't1'), [ tq_ast.PartialJoin( tq_ast.TableId('table2', 't2'), tq_ast.JoinType.INNER, tq_ast.BinaryOperator('=', tq_ast.ColumnId('t1.foo'), tq_ast.ColumnId('t2.bar')), ), ]), None, None, None, None, None, None))
def test_operator_precedence(self): self.assert_parsed_select( 'SELECT 2 + 3 * 4 + 5', tq_ast.Select([ tq_ast.SelectField( tq_ast.BinaryOperator( '+', tq_ast.BinaryOperator( '+', literal(2), tq_ast.BinaryOperator('*', literal(3), literal(4))), literal(5)), None, None) ], None, None, None, None, None, None, None)) self.assert_parsed_select( 'SELECT FALSE OR "xyz" CONTAINS "y"', tq_ast.Select([ tq_ast.SelectField( tq_ast.BinaryOperator( 'or', literal(False), tq_ast.BinaryOperator('contains', literal('xyz'), literal('y'))), None, None) ], None, None, None, None, None, None, None))
def p_expression_binary(p): """expression : expression PLUS expression | expression MINUS expression | expression STAR expression | expression DIVIDED_BY expression | expression MOD expression | expression EQUALS expression | expression NOT_EQUAL expression | expression GREATER_THAN expression | expression LESS_THAN expression | expression GREATER_THAN_OR_EQUAL expression | expression LESS_THAN_OR_EQUAL expression | expression AND expression | expression OR expression | expression CONTAINS expression """ p[0] = tq_ast.BinaryOperator(p[2], p[1], p[3])