Skip to content

Commit

Permalink
[#23596] YSQL: Fix setrefs for Bitmap Index Scan
Browse files Browse the repository at this point in the history
Summary:
The same issue and fix as was present in #22533 (fixed by D37487 / 141703a) that was required for Index Scans was also required for Bitmap Index Scans.

I thought about pulling this common logic into a function that would handle both, but since the type of an `splan` is either an IndexScan or a YbBitmapIndexScan, and making it work for both seems like overcomplicating it.
Jira: DB-12514

Test Plan: ./yb_build.sh --java-test 'org.yb.pgsql.TestPgRegressMisc#testPgRegressMiscSerial'

Reviewers: amartsinchyk

Reviewed By: amartsinchyk

Subscribers: yql

Differential Revision: https://phorge.dev.yugabyte.com/D37630
  • Loading branch information
timothy-e committed Sep 10, 2024
1 parent 49f65a6 commit 3bf9301
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/postgres/src/backend/optimizer/plan/setrefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
splan->yb_rel_pushdown.quals =
fix_scan_list(root, splan->yb_rel_pushdown.quals, rtoffset);
/*
* 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.
*/
Expand Down Expand Up @@ -565,22 +565,32 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
fix_scan_list(root, splan->indexqual, rtoffset);
splan->indexqualorig =
fix_scan_list(root, splan->indexqualorig, rtoffset);

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);
splan->yb_idx_pushdown.colrefs = (List *)
fix_upper_expr(root,
(Node *) splan->yb_idx_pushdown.colrefs,
index_itlist,
INDEX_VAR,
rtoffset);
/*
* 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);
splan->yb_idx_pushdown.colrefs = (List *)
fix_upper_expr(root,
(Node *) splan->yb_idx_pushdown.colrefs,
index_itlist,
INDEX_VAR,
rtoffset);
splan->indextlist =
fix_scan_list(root, splan->indextlist, rtoffset);
pfree(index_itlist);
}
}
break;
case T_BitmapHeapScan:
Expand Down
25 changes: 25 additions & 0 deletions src/postgres/src/test/regress/expected/yb_select.out
Original file line number Diff line number Diff line change
Expand Up @@ -798,5 +798,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;
15 changes: 15 additions & 0 deletions src/postgres/src/test/regress/sql/yb_select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3bf9301

Please sign in to comment.