diff --git a/src/postgres/src/backend/optimizer/plan/setrefs.c b/src/postgres/src/backend/optimizer/plan/setrefs.c index 88bf7468ad41..f7376b67f76a 100644 --- a/src/postgres/src/backend/optimizer/plan/setrefs.c +++ b/src/postgres/src/backend/optimizer/plan/setrefs.c @@ -606,8 +606,8 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset) fix_scan_list(root, splan->yb_rel_pushdown.quals, rtoffset, NUM_EXEC_QUAL(plan)); /* - * Index quals has to be fixed to refer index columns, not main - * table columns, so we need to index the indextlist. + * Index quals has to be fixed to refer to index columns, not + * main table columns, so we need to index the indextlist. * Also, indextlist has to be converted, as ANALYZE may use it. * Skip that if we don't have index pushdown quals. */ @@ -683,24 +683,35 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset) splan->indexqualorig = fix_scan_list(root, splan->indexqualorig, rtoffset, NUM_EXEC_QUAL(plan)); - - indexed_tlist *index_itlist; - index_itlist = build_tlist_index(splan->indextlist); - - splan->yb_idx_pushdown.quals = (List *) - fix_upper_expr(root, - (Node *) splan->yb_idx_pushdown.quals, - index_itlist, - INDEX_VAR, - rtoffset, - NUM_EXEC_QUAL(plan)); - splan->yb_idx_pushdown.colrefs = (List *) - fix_upper_expr(root, - (Node *) splan->yb_idx_pushdown.colrefs, - index_itlist, - INDEX_VAR, - rtoffset, - NUM_EXEC_TLIST(plan)); + /* + * Index quals has to be fixed to refer to index columns, not + * main table columns, so we need to index the indextlist. + * Also, indextlist has to be converted, as ANALYZE may use it. + * Skip that if we don't have index pushdown quals. + */ + if (splan->yb_idx_pushdown.quals) + { + indexed_tlist *index_itlist; + index_itlist = build_tlist_index(splan->indextlist); + splan->yb_idx_pushdown.quals = (List *) + fix_upper_expr(root, + (Node *) splan->yb_idx_pushdown.quals, + index_itlist, + INDEX_VAR, + rtoffset, + NUM_EXEC_QUAL(plan)); + splan->yb_idx_pushdown.colrefs = (List *) + fix_upper_expr(root, + (Node *) splan->yb_idx_pushdown.colrefs, + index_itlist, + INDEX_VAR, + rtoffset, + NUM_EXEC_TLIST(plan)); + splan->indextlist = + fix_scan_list(root, splan->indextlist, rtoffset, + NUM_EXEC_TLIST(plan)); + pfree(index_itlist); + } } break; case T_BitmapHeapScan: diff --git a/src/postgres/src/test/regress/expected/yb_select.out b/src/postgres/src/test/regress/expected/yb_select.out index b3b157b71e7f..00c7abdf5753 100644 --- a/src/postgres/src/test/regress/expected/yb_select.out +++ b/src/postgres/src/test/regress/expected/yb_select.out @@ -801,5 +801,30 @@ JOIN ( -> Seq Scan on site s (6 rows) +-- try with bitmap scans +/*+ Set(yb_enable_bitmapscan true) Set(enable_indexscan false) Set(enable_seqscan false) */ +explain (costs off) +SELECT s.id, aa.addresses +FROM site s +JOIN ( + SELECT + array_agg(json_build_object( + 'id', a.id, + 'site_id', a.site_id)) AS addresses + FROM address a + WHERE a.site_id = 1 + AND a.recorded_at IS NULL + ) aa ON true; + QUERY PLAN +----------------------------------------------------------------- + Nested Loop + -> Aggregate + -> YB Bitmap Table Scan on address a + -> Bitmap Index Scan on idx_address_problem + Index Cond: (site_id = 1) + Storage Index Filter: (recorded_at IS NULL) + -> Seq Scan on site s +(7 rows) + DROP TABLE site; DROP TABLE address; diff --git a/src/postgres/src/test/regress/sql/yb_select.sql b/src/postgres/src/test/regress/sql/yb_select.sql index cec7c227679d..c49cc8df2598 100644 --- a/src/postgres/src/test/regress/sql/yb_select.sql +++ b/src/postgres/src/test/regress/sql/yb_select.sql @@ -247,6 +247,21 @@ CREATE INDEX idx_address_problem ON address (site_id HASH) INCLUDE (recorded_at) explain (costs off) SELECT s.id, aa.addresses FROM site s +JOIN ( + SELECT + array_agg(json_build_object( + 'id', a.id, + 'site_id', a.site_id)) AS addresses + FROM address a + WHERE a.site_id = 1 + AND a.recorded_at IS NULL + ) aa ON true; + +-- try with bitmap scans +/*+ Set(yb_enable_bitmapscan true) Set(enable_indexscan false) Set(enable_seqscan false) */ +explain (costs off) +SELECT s.id, aa.addresses +FROM site s JOIN ( SELECT array_agg(json_build_object(