Skip to content

Commit

Permalink
[BACKPORT pg15-cherrypicks][#23596] YSQL: Fix setrefs for Bitmap Inde…
Browse files Browse the repository at this point in the history
…x Scan

Summary:
Original commit: 3bf9301 / D37630

* setrefs.c
  * set_plan_refs: case T_YbBitmapIndexScan
    * upstream PG's 41efb8340877e8ffd0023bb6b2ef22ffd1ca014d added a new
      parameter num_exec to fix_upper_expr
    * 3bf9301 / D37630 puts existing fix_upper_expr calls under an if statement and adds a new fix_scan_list call
    * Solution: add the new parameter to fix_upper_expr and fix_scan_list, keep the logical flow of D37630

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: jason, tfoucher

Reviewed By: jason

Subscribers: yql

Differential Revision: https://phorge.dev.yugabyte.com/D37945
  • Loading branch information
timothy-e committed Sep 11, 2024
1 parent d181dbf commit c9371fd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/postgres/src/backend/optimizer/plan/setrefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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:
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 @@ -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;
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 c9371fd

Please sign in to comment.