def compile_table_expr_CrossJoin(self, table_expr): compiled_table1, _ = self.compile_joined_table(table_expr.table1) compiled_table2, _ = self.compile_joined_table(table_expr.table2) result_type_ctx = type_context.TypeContext.join_contexts( [compiled_table1.type_ctx, compiled_table2.type_ctx]) return typed_ast.Join(compiled_table1, compiled_table2, [], False, result_type_ctx)
def test_multi_way_join(self): self.assert_compiled_select( 'SELECT 0 ' 'FROM table1 t1 JOIN table2 t2 ON t1.value = t2.value ' 'LEFT JOIN table3 t3 ON t2.value3 = t3.value', typed_ast.Select( select_fields=[ typed_ast.SelectField( typed_ast.Literal(0, tq_types.INT), 'f0_', None)], table=typed_ast.Join( base=typed_ast.Table('table1', self.make_type_context([ ('t1', 'value', tq_types.INT), ('t1', 'value2', tq_types.INT), ])), tables=[ (typed_ast.Table( 'table2', self.make_type_context([ ('t2', 'value', tq_types.INT), ('t2', 'value3', tq_types.INT), ])), tq_ast.JoinType.INNER), (typed_ast.Table( 'table3', self.make_type_context([ ('t3', 'value', tq_types.INT) ])), tq_ast.JoinType.LEFT_OUTER )], conditions=[ [typed_ast.JoinFields( typed_ast.ColumnRef('t1', 'value', tq_types.INT), typed_ast.ColumnRef('t2', 'value', tq_types.INT) )], [typed_ast.JoinFields( typed_ast.ColumnRef('t2', 'value3', tq_types.INT), typed_ast.ColumnRef('t3', 'value', tq_types.INT) )]], type_ctx=self.make_type_context([ ('t1', 'value', tq_types.INT), ('t1', 'value2', tq_types.INT), ('t2', 'value', tq_types.INT), ('t2', 'value3', tq_types.INT), ('t3', 'value', tq_types.INT), ]) ), 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 compile_table_expr_Join(self, table_expr): compiled_table1, alias1 = self.compile_joined_table(table_expr.table1) compiled_table2, alias2 = self.compile_joined_table(table_expr.table2) result_fields = self.compile_join_fields(compiled_table1.type_ctx, compiled_table2.type_ctx, alias1, alias2, table_expr.condition) result_type_ctx = type_context.TypeContext.join_contexts( [compiled_table1.type_ctx, compiled_table2.type_ctx]) return typed_ast.Join(compiled_table1, compiled_table2, result_fields, table_expr.is_left_outer, result_type_ctx)
def test_simple_join(self): self.assert_compiled_select( 'SELECT value2 ' 'FROM table1 t1 JOIN table2 t2 ON t1.value = t2.value', typed_ast.Select([ typed_ast.SelectField( typed_ast.ColumnRef('t1', 'value2', tq_types.INT), 'value2', None )], typed_ast.Join( typed_ast.Table('table1', self.make_type_context([ ('t1', 'value', tq_types.INT), ('t1', 'value2', tq_types.INT), ])), [(typed_ast.Table('table2', self.make_type_context([ ('t2', 'value', tq_types.INT), ('t2', 'value3', tq_types.INT), ])), tq_ast.JoinType.INNER)], [[typed_ast.JoinFields( typed_ast.ColumnRef('t1', 'value', tq_types.INT), typed_ast.ColumnRef('t2', 'value', tq_types.INT) )]], self.make_type_context([ ('t1', 'value', tq_types.INT), ('t1', 'value2', tq_types.INT), ('t2', 'value', tq_types.INT), ('t2', 'value3', 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, 'value2', tq_types.INT)], self.make_type_context([('t1', 'value2', tq_types.INT)]) ) ) )
def test_join_multiple_fields(self): self.assert_compiled_select( 'SELECT 0 ' 'FROM table1 t1 JOIN table2 t2 ' 'ON t1.value = t2.value AND t2.value3 = t1.value2', typed_ast.Select( [typed_ast.SelectField( typed_ast.Literal(0, tq_types.INT), 'f0_')], typed_ast.Join( typed_ast.Table('table1', self.make_type_context([ ('t1', 'value', tq_types.INT), ('t1', 'value2', tq_types.INT), ])), typed_ast.Table('table2', self.make_type_context([ ('t2', 'value', tq_types.INT), ('t2', 'value3', tq_types.INT), ])), [typed_ast.JoinFields( typed_ast.ColumnRef('t1', 'value', tq_types.INT), typed_ast.ColumnRef('t2', 'value', tq_types.INT) ), typed_ast.JoinFields( typed_ast.ColumnRef('t1', 'value2', tq_types.INT), typed_ast.ColumnRef('t2', 'value3', tq_types.INT) )], False, self.make_type_context([ ('t1', 'value', tq_types.INT), ('t1', 'value2', tq_types.INT), ('t2', 'value', tq_types.INT), ('t2', 'value3', tq_types.INT), ]) ), typed_ast.Literal(True, tq_types.BOOL), None, None, self.make_type_context( [(None, 'f0_', tq_types.INT)], self.make_type_context([])) ) )
def compile_table_expr_Join(self, table_expr): table_expressions = itertools.chain( [table_expr.base], (join_part.table_expr for join_part in table_expr.join_parts)) compiled_result = map(self.compile_joined_table, table_expressions) compiled_table_exprs, compiled_aliases = zip(*compiled_result) type_contexts = [ compiled_table.type_ctx for compiled_table in compiled_table_exprs ] result_fields = self.compile_join_fields( type_contexts, compiled_aliases, [join_part.condition for join_part in table_expr.join_parts], [join_part.join_type for join_part in table_expr.join_parts]) result_type_ctx = type_context.TypeContext.join_contexts(type_contexts) return typed_ast.Join( base=compiled_table_exprs[0], tables=zip(compiled_table_exprs[1:], (join_part.join_type for join_part in table_expr.join_parts)), conditions=result_fields, type_ctx=result_type_ctx)