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 ) )
def test_other_literals(self): self.assert_parsed_select( 'SELECT true, false, null', tq_ast.Select([ tq_ast.SelectField(tq_ast.Literal(True), None), tq_ast.SelectField(tq_ast.Literal(False), None), tq_ast.SelectField(tq_ast.Literal(None), None) ], None, None, None, None, None, None))
def test_count_star(self): self.assert_parsed_select( 'SELECT COUNT(*), COUNT(((*))) FROM table', tq_ast.Select([ tq_ast.SelectField( tq_ast.FunctionCall('count', [tq_ast.Literal(1)]), None), tq_ast.SelectField( tq_ast.FunctionCall('count', [tq_ast.Literal(1)]), None) ], tq_ast.TableId('table', None), None, None, None, None, None))
def test_where(self): self.assert_parsed_select( 'SELECT foo + 2 FROM bar WHERE foo > 3', tq_ast.Select([ tq_ast.SelectField( tq_ast.BinaryOperator('+', tq_ast.ColumnId('foo'), tq_ast.Literal(2)), None) ], tq_ast.TableId('bar', None), tq_ast.BinaryOperator('>', tq_ast.ColumnId('foo'), tq_ast.Literal(3)), None, None, None, None))
def test_cross_join(self): self.assert_parsed_select( 'SELECT 0 FROM table1 t1 CROSS JOIN table2 t2', tq_ast.Select([tq_ast.SelectField(tq_ast.Literal(0), None)], tq_ast.CrossJoin(tq_ast.TableId('table1', 't1'), tq_ast.TableId('table2', 't2')), None, None, None, None, None))
def compile_helper(remaining_clauses): if len(remaining_clauses) == 0: return tq_ast.Literal(value=None) clause = remaining_clauses[0] return tq_ast.FunctionCall( name='if', args=[ clause.condition, clause.result_expr, compile_helper(remaining_clauses[1:]) ])
def test_group_each_by(self): self.assert_parsed_select( 'SELECT 0 FROM table GROUP EACH BY foo', tq_ast.Select([ tq_ast.SelectField(tq_ast.Literal(0), None)], tq_ast.TableId('table', None), None, [tq_ast.ColumnId('foo')], 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)], tq_ast.Join(tq_ast.TableId('table1', 't1'), tq_ast.TableId('table2', 't2'), tq_ast.BinaryOperator( '=', tq_ast.ColumnId('t1.foo'), tq_ast.ColumnId('t2.bar')), is_left_outer=False), None, None, None, None, None))
def test_string_literal(self): self.assert_parsed_select( 'SELECT "Hello" AS foo', tq_ast.Select([ tq_ast.SelectField(tq_ast.Literal('Hello'), 'foo')], 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'), tq_ast.SelectField(tq_ast.ColumnId('b'), 'bar'), tq_ast.SelectField( tq_ast.BinaryOperator('+', tq_ast.ColumnId('a'), tq_ast.Literal(1)), 'baz') ], tq_ast.TableId('test_table', None), None, None, None, None, None))
def test_redundant_commas_allowed(self): # In most cases, a comma at the end of a comma-separated list is OK. self.assert_parsed_select( 'SELECT foo IN (1, 2, 3,), bar, FROM table1, table2, ' 'GROUP BY col1, col2,', tq_ast.Select([ tq_ast.SelectField( tq_ast.FunctionCall('in', [ tq_ast.ColumnId('foo'), tq_ast.Literal(1), tq_ast.Literal(2), tq_ast.Literal(3) ]), None), tq_ast.SelectField(tq_ast.ColumnId('bar'), None) ], tq_ast.TableUnion([ tq_ast.TableId('table1', None), tq_ast.TableId('table2', None) ]), None, [tq_ast.ColumnId('col1'), tq_ast.ColumnId('col2')], None, None, None))
def p_int_literal(p): """constant : NUMBER""" p[0] = tq_ast.Literal(p[1])
def literal(value): return tq_ast.Literal(value)
def p_int_literal(p): """constant : INTEGER""" p[0] = tq_ast.Literal(p[1])
def p_float_literal(p): """constant : FLOAT""" p[0] = tq_ast.Literal(p[1])
def p_string_literal(p): """constant : STRING""" p[0] = tq_ast.Literal(p[1])
def p_true_literal(p): """constant : TRUE""" p[0] = tq_ast.Literal(True)
def p_expression_count_star(p): """expression : COUNT LPAREN parenthesized_star RPAREN""" # Treat COUNT(*) as COUNT(1). p[0] = tq_ast.FunctionCall('count', [tq_ast.Literal(1)])
def p_false_literal(p): """constant : FALSE""" p[0] = tq_ast.Literal(False)
def p_case_clause_else(p): """case_clause_else : ELSE expression""" p[0] = tq_ast.CaseClause(tq_ast.Literal(True), p[2])
def p_null_literal(p): """constant : NULL""" p[0] = tq_ast.Literal(None)