CREATE SCHEMA postgres_local_table;
SET search_path TO postgres_local_table;
CREATE TABLE local_table(a INT);
INSERT INTO local_table VALUES (1),(2),(3);
CREATE RECURSIVE VIEW recursive_view(val_1, val_2) AS
(
		VALUES(0,1)
	UNION ALL
		SELECT GREATEST(val_1,val_2),val_1 + val_2 AS local_table
	FROM
		recursive_view
	WHERE val_2 < 50
);
CREATE RECURSIVE VIEW recursive_defined_non_recursive_view(c) AS (SELECT 1 FROM local_table);
WARNING:  "view recursive_defined_non_recursive_view" has dependency to "table local_table" that is not in Citus' metadata
DETAIL:  "view recursive_defined_non_recursive_view" will be created only locally
HINT:  Distribute "table local_table" first to distribute "view recursive_defined_non_recursive_view"
CREATE TABLE ref_table(a int, b INT);
SELECT create_reference_table('ref_table');
 create_reference_table
---------------------------------------------------------------------

(1 row)

INSERT INTO ref_table VALUES (1,1);
SELECT ref_table.* FROM ref_table LEFT OUTER JOIN (SELECT * FROM recursive_view WHERE FALSE) AS sub ON FALSE;
 a | b
---------------------------------------------------------------------
 1 | 1
(1 row)

SELECT ref_table.* FROM ref_table LEFT OUTER JOIN (SELECT * FROM (SELECT 1, random() FROM local_table) as s WHERE FALSE) AS sub ON FALSE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
(1 row)

SELECT ref_table.* FROM ref_table LEFT OUTER JOIN (SELECT * FROM recursive_view WHERE FALSE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
(1 row)

SELECT ref_table.* FROM ref_table LEFT OUTER JOIN (SELECT * FROM (SELECT 1, random() FROM local_table) as s WHERE FALSE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
(1 row)

SELECT ref_table.* FROM ref_table LEFT OUTER JOIN (SELECT * FROM recursive_view WHERE TRUE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
(10 rows)

SELECT ref_table.* FROM ref_table LEFT OUTER JOIN (SELECT * FROM (SELECT 1, random() FROM local_table) as s WHERE TRUE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
 1 | 1
 1 | 1
(3 rows)

SELECT ref_table.* FROM ref_table JOIN (SELECT * FROM recursive_view WHERE FALSE) AS sub ON FALSE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table JOIN (SELECT * FROM (SELECT 1, random() FROM local_table) as s WHERE FALSE) AS sub ON FALSE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table JOIN (SELECT * FROM recursive_view WHERE FALSE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table JOIN (SELECT * FROM (SELECT 1, random() FROM local_table) as s WHERE FALSE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table JOIN (SELECT * FROM recursive_view WHERE TRUE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
 1 | 1
(10 rows)

SELECT ref_table.* FROM ref_table JOIN (SELECT * FROM (SELECT 1, random() FROM local_table) as s WHERE TRUE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
 1 | 1
 1 | 1
(3 rows)

SELECT ref_table.* FROM ref_table LEFT OUTER JOIN (SELECT * FROM recursive_defined_non_recursive_view WHERE FALSE) AS sub ON FALSE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
(1 row)

SELECT ref_table.* FROM ref_table LEFT OUTER JOIN (SELECT * FROM recursive_defined_non_recursive_view WHERE FALSE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
(1 row)

SELECT ref_table.* FROM ref_table LEFT OUTER JOIN (SELECT * FROM recursive_defined_non_recursive_view WHERE TRUE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
 1 | 1
 1 | 1
(3 rows)

SELECT ref_table.* FROM ref_table JOIN (SELECT * FROM recursive_defined_non_recursive_view WHERE FALSE) AS sub ON FALSE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table JOIN (SELECT * FROM recursive_defined_non_recursive_view WHERE FALSE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table JOIN (SELECT * FROM recursive_defined_non_recursive_view WHERE TRUE) AS sub ON TRUE ORDER BY 1,2;
 a | b
---------------------------------------------------------------------
 1 | 1
 1 | 1
 1 | 1
(3 rows)

SELECT ref_table.* FROM ref_table WHERE EXISTS (SELECT * FROM local_table l WHERE l.a = ref_table.a);
 a | b
---------------------------------------------------------------------
 1 | 1
(1 row)

SELECT ref_table.* FROM ref_table WHERE EXISTS (SELECT * FROM local_table l WHERE l.a = ref_table.a) AND false;
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table WHERE EXISTS (SELECT * FROM local_table l WHERE l.a = ref_table.a AND false);
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table WHERE EXISTS (SELECT * FROM recursive_view l WHERE l.val_1 = ref_table.a);
 a | b
---------------------------------------------------------------------
 1 | 1
(1 row)

SELECT ref_table.* FROM ref_table WHERE EXISTS (SELECT * FROM recursive_view l WHERE l.val_1 = ref_table.a) AND false;
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table WHERE EXISTS (SELECT * FROM recursive_view l WHERE l.val_1 = ref_table.a AND false);
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table WHERE EXISTS (SELECT * FROM recursive_defined_non_recursive_view l WHERE l.c = ref_table.a);
 a | b
---------------------------------------------------------------------
 1 | 1
(1 row)

SELECT ref_table.* FROM ref_table WHERE EXISTS (SELECT * FROM recursive_defined_non_recursive_view l WHERE l.c = ref_table.a) AND false;
 a | b
---------------------------------------------------------------------
(0 rows)

SELECT ref_table.* FROM ref_table WHERE EXISTS (SELECT * FROM recursive_defined_non_recursive_view l WHERE l.c = ref_table.a AND false);
 a | b
---------------------------------------------------------------------
(0 rows)

SET client_min_messages TO WARNING;
DROP SCHEMA postgres_local_table CASCADE;
