Skip to content

Commit 7bc9b3f

Browse files
author
liushengsong
committed
Fix ORCA permission checking crash with RTEPermissionInfo
1. orca.c: transformGroupedWindows() incorrectly created an RTEPermissionInfo with relid=0 for a subquery RTE. Per PG16 commit a61b1f7, only RTE_RELATION entries need RTEPermissionInfo. This caused Assert(OidIsValid(perminfo->relid)) failure when ORCA translated ROLLUP + window function queries. 2. matview.c: replace_rte_with_delta() converted RTE_RELATION to RTE_SUBQUERY but forgot to clear perminfoindex, causing Assert(rte->rtekind == RTE_RELATION || ...) failure during IVM incremental maintenance with ORCA enabled. Also fix subquery not inheriting rteperminfos, and correct AddPerfmInfo/Perfission typos.
1 parent 8a4fa20 commit 7bc9b3f

6 files changed

Lines changed: 17 additions & 16 deletions

File tree

src/backend/commands/matview.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,6 +2340,7 @@ get_prestate_rte(RangeTblEntry *rte, MV_TriggerTable *table,
23402340
rte->relkind = 0;
23412341
rte->rellockmode = 0;
23422342
rte->tablesample = NULL;
2343+
rte->perminfoindex = 0; /* subquery RTE does not need permission check */
23432344
rte->inh = false; /* must not be set for a subquery */
23442345

23452346
return rte;
@@ -2403,6 +2404,7 @@ replace_rte_with_delta(RangeTblEntry *rte, MV_TriggerTable *table, bool is_new,
24032404
rte->relkind = 0;
24042405
rte->rellockmode = 0;
24052406
rte->tablesample = NULL;
2407+
rte->perminfoindex = 0; /* subquery RTE does not need permission check */
24062408
rte->inh = false; /* must not be set for a subquery */
24072409

24082410
return rte;

src/backend/gpopt/gpdbwrappers.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,11 +2818,12 @@ gpdb::TestexprIsHashable(Node *testexpr, List *param_ids)
28182818
}
28192819

28202820
RTEPermissionInfo *
2821-
gpdb::GetRTEPermissionInfo(List *rteperminfos,
2822-
const RangeTblEntry *rte)
2821+
gpdb::GetRTEPermissionInfo(List *rteperminfos, const RangeTblEntry *rte)
28232822
{
28242823
GP_WRAP_START;
28252824
{
2825+
// Cast away const: upstream getRTEPermissionInfo() only reads
2826+
// rte->perminfoindex and rte->relid but its signature lacks const.
28262827
return getRTEPermissionInfo(rteperminfos, (RangeTblEntry *) rte);
28272828
}
28282829
GP_WRAP_END;

src/backend/gpopt/translate/CContextDXLToPlStmt.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,16 +594,16 @@ CContextDXLToPlStmt::GetRTEIndexByAssignedQueryId(
594594

595595
//---------------------------------------------------------------------------
596596
// @function:
597-
// CContextDXLToPlStmt::AddPerfmInfo
597+
// CContextDXLToPlStmt::AddPermInfo
598598
//
599599
// @doc:
600-
// Add a Perfission Info list entry
600+
// Add a Permission Info list entry
601601
//
602602
//---------------------------------------------------------------------------
603603
void
604-
CContextDXLToPlStmt::AddPerfmInfo(RTEPermissionInfo *pi)
604+
CContextDXLToPlStmt::AddPermInfo(RTEPermissionInfo *pi)
605605
{
606-
// add rte to rtable entries list
606+
// add permission info to list
607607
m_perminfo_list = gpdb::LAppend(m_perminfo_list, pi);
608608
}
609609

src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5336,7 +5336,7 @@ CTranslatorDXLToPlStmt::ProcessDXLTblDescr(
53365336
rte->eref = alias;
53375337
rte->alias = alias;
53385338

5339-
m_dxl_to_plstmt_context->AddPerfmInfo(pi);
5339+
m_dxl_to_plstmt_context->AddPermInfo(pi);
53405340

53415341
// set up rte <> perm info link.
53425342
rte->perminfoindex = gpdb::ListLength(

src/backend/optimizer/plan/orca.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,6 @@ transformGroupedWindows(Node *node, void *context)
518518

519519
Query *subq;
520520
RangeTblEntry *rte;
521-
RTEPermissionInfo *perminfo;
522521
RangeTblRef *ref;
523522
Alias *alias;
524523
bool hadSubLinks = qry->hasSubLinks;
@@ -545,6 +544,7 @@ transformGroupedWindows(Node *node, void *context)
545544

546545
/* Core of subquery input table expression: */
547546
subq->rtable = qry->rtable; /* before windowing */
547+
subq->rteperminfos = qry->rteperminfos; /* before windowing */
548548
subq->jointree = qry->jointree; /* before windowing */
549549
subq->targetList = NIL; /* fill in later */
550550

@@ -578,11 +578,9 @@ transformGroupedWindows(Node *node, void *context)
578578
rte->eref = NULL; /* fill in later */
579579
rte->inFromCl = true;
580580

581-
perminfo = makeNode(RTEPermissionInfo);
582-
perminfo->requiredPerms = ACL_SELECT;
583-
584581
/*
585-
* Default? rte->inh = 0; rte->checkAsUser = 0;
582+
* Subquery RTEs do not need RTEPermissionInfo. Permission checks
583+
* are performed on the base tables within the subquery itself.
586584
*/
587585

588586
/*
@@ -605,7 +603,7 @@ transformGroupedWindows(Node *node, void *context)
605603

606604
/* Core of outer query input table expression: */
607605
qry->rtable = list_make1(rte);
608-
qry->rteperminfos = list_make1(perminfo);
606+
qry->rteperminfos = NIL;
609607
qry->jointree = (FromExpr *) makeNode(FromExpr);
610608
qry->jointree->fromlist = list_make1(ref);
611609
qry->jointree->quals = NULL;

src/include/gpopt/translate/CContextDXLToPlStmt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class CContextDXLToPlStmt
109109
// list of all rtable entries
110110
List *m_rtable_entries_list;
111111

112-
// list of all rtable entries
112+
// list of all RTEPermissionInfo entries
113113
List *m_perminfo_list;
114114

115115
// list of all subplan entries
@@ -249,8 +249,8 @@ class CContextDXLToPlStmt
249249
Index GetRTEIndexByAssignedQueryId(ULONG assigned_query_id_for_target_rel,
250250
BOOL *is_rte_exists);
251251

252-
// add a perm info.
253-
void AddPerfmInfo(RTEPermissionInfo *pi);
252+
// add a permission info entry
253+
void AddPermInfo(RTEPermissionInfo *pi);
254254

255255
// get perm info from m_perminfo_list by given index
256256
RTEPermissionInfo *GetPermInfoByIndex(Index index);

0 commit comments

Comments
 (0)