Merge pull request #2742 from citusdata/fix_2739_outer_join_subquery_error

Also check rewrittenQuery jointree for outer join
pull/2654/head
Demur Rumed 2019-06-04 07:52:59 -07:00 committed by GitHub
commit 5cc8049caa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 1 deletions

View File

@ -142,6 +142,19 @@ ShouldUseSubqueryPushDown(Query *originalQuery, Query *rewrittenQuery)
return true;
}
/*
* Original query may not have an outer join while rewritten query does.
* We should push down in this case.
* An example of this is https://github.com/citusdata/citus/issues/2739
* where postgres pulls-up the outer-join in the subquery.
*/
if (FindNodeCheck((Node *) rewrittenQuery->jointree, IsOuterJoinExpr))
{
/* Assert what _should_ be only situation this occurs in. */
Assert(JoinTreeContainsSubquery(originalQuery));
return true;
}
/*
* Some unsupported join clauses in logical planner
* may be supported by subquery pushdown planner.

View File

@ -462,6 +462,25 @@ SELECT DISTINCT ON (t1.user_id) t1.user_id, t2.value_1, t2.value_2, t2.value_3
ORDER BY 1 DESC, 2 DESC, 3 DESC, 4 DESC
LIMIT 5;
ERROR: complex joins are only supported when all distributed tables are joined on their distribution columns with equal operator
-- outer joins as subqueries should work
-- https://github.com/citusdata/citus/issues/2739
SELECT user_id, value_1, event_type
FROM (
SELECT a.user_id, a.value_1, b.event_type
FROM users_table a
LEFT JOIN events_table b ON a.user_id = b.user_id
) lo
ORDER BY 1, 2, 3
LIMIT 5;
user_id | value_1 | event_type
---------+---------+------------
1 | 1 | 0
1 | 1 | 0
1 | 1 | 1
1 | 1 | 1
1 | 1 | 2
(5 rows)
-- inner joins on reference tables with functions works
SELECT DISTINCT ON (t1.user_id) t1.user_id, t2.value_1, t2.value_2, t2.value_3
FROM events_table t1

View File

@ -957,7 +957,7 @@ $$);
DEBUG: skipping recursive planning for the subquery since it contains references to outer queries
DEBUG: skipping recursive planning for the subquery since it contains references to outer queries
DEBUG: skipping recursive planning for the subquery since it contains references to outer queries
ERROR: cannot perform distributed planning on this query
ERROR: complex joins are only supported when all distributed tables are joined on their distribution columns with equal operator
-- similar to the above, make sure that we skip recursive plannig when
-- the subquery contains only intermediate results
SELECT *

View File

@ -314,6 +314,17 @@ SELECT DISTINCT ON (t1.user_id) t1.user_id, t2.value_1, t2.value_2, t2.value_3
ORDER BY 1 DESC, 2 DESC, 3 DESC, 4 DESC
LIMIT 5;
-- outer joins as subqueries should work
-- https://github.com/citusdata/citus/issues/2739
SELECT user_id, value_1, event_type
FROM (
SELECT a.user_id, a.value_1, b.event_type
FROM users_table a
LEFT JOIN events_table b ON a.user_id = b.user_id
) lo
ORDER BY 1, 2, 3
LIMIT 5;
-- inner joins on reference tables with functions works
SELECT DISTINCT ON (t1.user_id) t1.user_id, t2.value_1, t2.value_2, t2.value_3
FROM events_table t1