--
-- MULTI_NAME_RESOLUTION
--
-- There was a failure compiling queries with shadowed subquery aliases
-- https://github.com/citusdata/citus/issues/2642
CREATE SCHEMA multi_name_resolution;
SET search_path TO multi_name_resolution;
create table namenest1 (id integer primary key, user_id integer);
create table namenest2 (id integer primary key, value_2 integer);
select * from create_distributed_table('namenest1', 'id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

select * from create_reference_table('namenest2');
 create_reference_table
---------------------------------------------------------------------

(1 row)

SELECT r
FROM (
	SELECT id_deep, random() as r -- prevent pulling up the subquery
	FROM (
		namenest1
		JOIN namenest2 ON (namenest1.user_id = namenest2.value_2)
	) AS join_alias(id_deep)
) AS bar, (
	namenest1
	JOIN namenest2 ON (namenest1.user_id = namenest2.value_2)
) AS join_alias(id_deep)
WHERE bar.id_deep = join_alias.id_deep;
 r
---------------------------------------------------------------------
(0 rows)

DROP SCHEMA multi_name_resolution CASCADE;
NOTICE:  drop cascades to 3 other objects
DETAIL:  drop cascades to table namenest1
drop cascades to table namenest2
drop cascades to table namenest2_2250000000010
