def test_case(self): self.assert_compiled_select( 'SELECT CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 END', typed_ast.Select( select_fields=[ typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('if'), [ typed_ast.Literal(True, tq_types.BOOL), typed_ast.Literal(1, tq_types.INT), typed_ast.FunctionCall( runtime.get_func('if'), [ typed_ast.Literal(False, tq_types.BOOL), typed_ast.Literal(2, tq_types.INT), typed_ast.Literal(None, tq_types.NONETYPE), ], tq_types.INT) ], tq_types.INT), 'f0_', None) ], table=typed_ast.NoTable(), where_expr=typed_ast.Literal(True, tq_types.BOOL), group_set=None, having_expr=typed_ast.Literal(True, tq_types.BOOL), orderings=None, limit=None, type_ctx=self.make_type_context( [(None, 'f0_', tq_types.INT)], self.make_type_context([]))))
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_select_grouped_and_non_grouped_fields(self): self.assert_compiled_select( 'SELECT value, SUM(value2) FROM table1 GROUP BY value', typed_ast.Select([ typed_ast.SelectField( typed_ast.ColumnRef('table1', 'value', tq_types.INT), 'value'), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('sum'), [typed_ast.ColumnRef('table1', 'value2', tq_types.INT)], tq_types.INT), 'f0_')], typed_ast.Table('table1', self.table1_type_ctx), typed_ast.Literal(True, tq_types.BOOL), typed_ast.GroupSet( alias_groups={'value'}, field_groups=[] ), None, self.make_type_context( [(None, 'value', tq_types.INT), (None, 'f0_', tq_types.INT)], self.make_type_context( [('table1', 'value', tq_types.INT)])) ) )
def test_within_clause(self): self.assert_compiled_select( 'SELECT r1.s, COUNT(r1.s) WITHIN r1 AS num_s_in_r1 ' 'FROM record_table', typed_ast.Select( select_fields=[ typed_ast.SelectField( typed_ast.ColumnRef('record_table', 'r1.s', tq_types.STRING), 'r1.s', None), typed_ast.SelectField(typed_ast.FunctionCall( runtime.get_func('count'), [typed_ast.ColumnRef('record_table', 'r1.s', tq_types.STRING)], tq_types.INT ), 'num_s_in_r1', 'r1')], table=typed_ast.Table('record_table', self.record_table_type_ctx), where_expr=typed_ast.Literal(True, tq_types.BOOL), group_set=typed_ast.GroupSet(set(), []), having_expr=typed_ast.Literal(True, tq_types.BOOL), orderings=None, limit=None, type_ctx=self.make_type_context( [(None, 'r1.s', tq_types.STRING), (None, 'num_s_in_r1', tq_types.INT)], self.make_type_context([]))))
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 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_UnaryOperator(self, expr, type_ctx): func = runtime.get_unary_op(expr.operator) compiled_val = self.compile_expr(expr.expr, type_ctx) try: result_type = func.check_types(compiled_val.type) except TypeError: raise CompileError('Invalid type for operator {}: {}'.format( expr.operator, [compiled_val.type])) return typed_ast.FunctionCall(func, [compiled_val], result_type)
def test_function_calls(self): self.assert_compiled_select( 'SELECT ABS(-3), POW(2, 3), NOW()', typed_ast.Select([ typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('abs'), [typed_ast.FunctionCall( runtime.get_unary_op('-'), [typed_ast.Literal(3, tq_types.INT)], tq_types.INT )], tq_types.INT), 'f0_', None), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('pow'), [ typed_ast.Literal(2, tq_types.INT), typed_ast.Literal(3, tq_types.INT)], tq_types.INT ), 'f1_', None ), typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_func('now'), [], tq_types.INT ), 'f2_', None )], typed_ast.NoTable(), typed_ast.Literal(True, tq_types.BOOL), None, typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context([ (None, 'f0_', tq_types.INT), (None, 'f1_', tq_types.INT), (None, 'f2_', tq_types.INT)], self.make_type_context([])) ) )
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_unary_operator(self): self.assert_compiled_select( 'SELECT -5', typed_ast.Select( [typed_ast.SelectField( typed_ast.FunctionCall( runtime.get_unary_op('-'), [typed_ast.Literal(5, tq_types.INT)], tq_types.INT), 'f0_' )], typed_ast.NoTable(), typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context( [(None, 'f0_', tq_types.INT)], self.make_type_context([])) ) )
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 compile_UnaryOperator(self, expr, type_ctx): func = runtime.get_unary_op(expr.operator) compiled_val = self.compile_expr(expr.expr, type_ctx) result_type = func.check_types(compiled_val.type) return typed_ast.FunctionCall(func, [compiled_val], result_type)