CREATE SCHEMA issue_6758;
SET search_path to 'issue_6758';

CREATE TABLE dist0(id int);
SELECT create_distributed_table('dist0','id');

CREATE TABLE dist1(id int);
SELECT create_distributed_table('dist1','id');

-- added to verify we fixed the issue https://github.com/citusdata/citus/issues/6758
-- generated by Citus query generator tool
SELECT
  avg(avgsub.id)
FROM
  (
    SELECT
      table_0.id
    FROM
      (
        SELECT
          table_1.id
        FROM
          (
            SELECT
              table_2.id
            FROM
              (
                SELECT
                  table_3.id
                FROM
                  (
                    VALUES
                      (838)
                  ) AS table_3(id) FULL
                  JOIN dist0 AS table_4 USING (id)
                WHERE
                  table_4.id = 3
              ) AS table_2
            WHERE
              table_2.id = 2
            ORDER BY
              id
            LIMIT
              77
          ) AS table_1
          LEFT JOIN dist0 AS table_5 USING (id)
        ORDER BY
          id
        LIMIT
          44
      ) AS table_0 FULL
      JOIN dist1 AS table_6 USING (id)
  ) AS avgsub;

DROP SCHEMA issue_6758 CASCADE;
