def test_multiple_select(self): self.assert_compiled_select( 'SELECT value * 3 AS foo, value, value + 1, value bar, value - 1 ' 'FROM table1', typed_ast.Select( [typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('*'), [typed_ast.ColumnRef('table1', 'value', tq_types.INT), typed_ast.Literal(3, tq_types.INT)], tq_types.INT), 'foo'), typed_ast.SelectField( typed_ast.ColumnRef('table1', 'value', tq_types.INT), 'value'), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('+'), [typed_ast.ColumnRef('table1', 'value', tq_types.INT), typed_ast.Literal(1, tq_types.INT)], tq_types.INT), 'f0_'), typed_ast.SelectField( typed_ast.ColumnRef('table1', 'value', tq_types.INT), 'bar'), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('-'), [typed_ast.ColumnRef('table1', 'value', tq_types.INT), typed_ast.Literal(1, tq_types.INT)], tq_types.INT), 'f1_')], typed_ast.Table('table1', self.table1_type_ctx), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context([ (None, 'foo', tq_types.INT), (None, 'value', tq_types.INT), (None, 'f0_', tq_types.INT), (None, 'bar', tq_types.INT), (None, 'f1_', tq_types.INT)], self.make_type_context( [('table1', 'value', tq_types.INT)] )) ) )
def test_multiple_select(self): self.assert_compiled_select( 'SELECT value * 3 AS foo, value, value + 1, value bar, value - 1 ' 'FROM table1', typed_ast.Select( [typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('*'), [typed_ast.ColumnRef('table1', 'value', tq_types.INT), typed_ast.Literal(3, tq_types.INT)], tq_types.INT), 'foo', None), typed_ast.SelectField( typed_ast.ColumnRef('table1', 'value', tq_types.INT), 'value', None), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('+'), [typed_ast.ColumnRef('table1', 'value', tq_types.INT), typed_ast.Literal(1, tq_types.INT)], tq_types.INT), 'f0_', None), typed_ast.SelectField( typed_ast.ColumnRef('table1', 'value', tq_types.INT), 'bar', None), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('-'), [typed_ast.ColumnRef('table1', 'value', tq_types.INT), typed_ast.Literal(1, tq_types.INT)], tq_types.INT), 'f1_', None)], typed_ast.Table('table1', self.table1_type_ctx), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context([ (None, 'foo', tq_types.INT), (None, 'value', tq_types.INT), (None, 'f0_', tq_types.INT), (None, 'bar', tq_types.INT), (None, 'f1_', tq_types.INT)], self.make_type_context( [('table1', 'value', tq_types.INT)] )) ) )
def test_implicitly_accessed_column(self): self.assert_compiled_select( 'SELECT table1.value FROM (SELECT value + 1 AS foo FROM table1)', typed_ast.Select([ typed_ast.SelectField( typed_ast.ColumnRef('table1', 'value', tq_types.INT), 'table1.value')], typed_ast.Select([ typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('+'), [ typed_ast.ColumnRef('table1', 'value', tq_types.INT), typed_ast.Literal(1, tq_types.INT) ], tq_types.INT ), 'foo')], typed_ast.Table('table1', self.table1_type_ctx), typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context( [(None, 'foo', tq_types.INT)], self.make_type_context( [('table1', 'value', tq_types.INT)]))), typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context( [(None, 'table1.value', tq_types.INT)], self.make_type_context( [('table1', 'value', tq_types.INT)] ))) )
def test_subquery(self): self.assert_compiled_select( 'SELECT foo, foo + 1 FROM (SELECT value + 1 AS foo FROM table1)', typed_ast.Select([ typed_ast.SelectField( typed_ast.ColumnRef(None, 'foo', tq_types.INT), 'foo', None), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('+'), [ typed_ast.ColumnRef(None, 'foo', tq_types.INT), typed_ast.Literal(1, tq_types.INT)], tq_types.INT), 'f0_', None )], typed_ast.Select( [typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('+'), [ typed_ast.ColumnRef('table1', 'value', tq_types.INT), typed_ast.Literal(1, tq_types.INT)], tq_types.INT), 'foo', None )], typed_ast.Table('table1', self.table1_type_ctx), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context( [(None, 'foo', tq_types.INT)], self.make_type_context( [('table1', 'value', tq_types.INT)] )) ), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context( [(None, 'foo', tq_types.INT), (None, 'f0_', tq_types.INT)], self.make_type_context([(None, 'foo', tq_types.INT)])) ) )
def test_subquery(self): self.assert_compiled_select( 'SELECT foo, foo + 1 FROM (SELECT value + 1 AS foo FROM table1)', typed_ast.Select([ typed_ast.SelectField( typed_ast.ColumnRef(None, 'foo', tq_types.INT), 'foo'), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('+'), [ typed_ast.ColumnRef(None, 'foo', tq_types.INT), typed_ast.Literal(1, tq_types.INT)], tq_types.INT), 'f0_' )], typed_ast.Select( [typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_binary_op('+'), [ typed_ast.ColumnRef('table1', 'value', tq_types.INT), typed_ast.Literal(1, tq_types.INT)], tq_types.INT), 'foo' )], typed_ast.Table('table1', self.table1_type_ctx), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context( [(None, 'foo', tq_types.INT)], self.make_type_context( [('table1', 'value', tq_types.INT)] )) ), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context( [(None, 'foo', tq_types.INT), (None, 'f0_', tq_types.INT)], self.make_type_context([(None, 'foo', tq_types.INT)])) ) )
def compile_BinaryOperator(self, expr, type_ctx): func = runtime.get_binary_op(expr.operator) compiled_left = self.compile_expr(expr.left, type_ctx) compiled_right = self.compile_expr(expr.right, type_ctx) result_type = func.check_types(compiled_left.type, compiled_right.type) return typed_ast.FunctionCall( func, [compiled_left, compiled_right], result_type)
def compile_BinaryOperator(self, expr, type_ctx): func = runtime.get_binary_op(expr.operator) compiled_left = self.compile_expr(expr.left, type_ctx) compiled_right = self.compile_expr(expr.right, type_ctx) result_type = func.check_types(compiled_left.type, compiled_right.type) return typed_ast.FunctionCall(func, [compiled_left, compiled_right], result_type)
def compile_BinaryOperator(self, expr, type_ctx): func = runtime.get_binary_op(expr.operator) compiled_left = self.compile_expr(expr.left, type_ctx) compiled_right = self.compile_expr(expr.right, type_ctx) try: result_type = func.check_types(compiled_left.type, compiled_right.type) except TypeError: raise CompileError('Invalid types for operator {}: {}'.format( expr.operator, [arg.type for arg in [compiled_left, compiled_right]])) return typed_ast.FunctionCall( func, [compiled_left, compiled_right], result_type)
def compile_BinaryOperator(self, expr, type_ctx): func = runtime.get_binary_op(expr.operator) compiled_left = self.compile_expr(expr.left, type_ctx) compiled_right = self.compile_expr(expr.right, type_ctx) try: result_type = func.check_types(compiled_left.type, compiled_right.type) except TypeError: raise CompileError('Invalid types for operator {}: {}'.format( expr.operator, [arg.type for arg in [compiled_left, compiled_right]])) return typed_ast.FunctionCall(func, [compiled_left, compiled_right], result_type)
def test_where(self): self.assert_compiled_select( 'SELECT value FROM table1 WHERE value > 3', typed_ast.Select( [typed_ast.SelectField( typed_ast.ColumnRef('table1', 'value', tq_types.INT), 'value')], typed_ast.Table('table1', self.table1_type_ctx), typed_ast.FunctionCall( runtime.get_binary_op('>'), [typed_ast.ColumnRef('table1', 'value', tq_types.INT), typed_ast.Literal(3, tq_types.INT)], tq_types.BOOL), None, None, self.make_type_context( [(None, 'value', tq_types.INT)], self.make_type_context( [('table1', 'value', tq_types.INT)])) ) )
def test_having(self): self.assert_compiled_select( 'SELECT value FROM table1 HAVING value > 3', typed_ast.Select( [typed_ast.SelectField( typed_ast.ColumnRef('table1', 'value', tq_types.INT), 'value')], typed_ast.Table('table1', self.table1_type_ctx), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.FunctionCall( runtime.get_binary_op('>'), [typed_ast.ColumnRef(None, 'value', tq_types.INT), typed_ast.Literal(3, tq_types.INT)], tq_types.BOOL), None, None, self.make_type_context( [(None, 'value', tq_types.INT)], self.make_type_context( [('table1', 'value', tq_types.INT)])) ) )