Skip to content

Commit 59c3510

Browse files
liushengsongoppenheimer01
authored andcommitted
Fix: fix ORCA bugs and tests
1 parent dc8dae9 commit 59c3510

File tree

118 files changed

+25710
-2542
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+25710
-2542
lines changed

.github/workflows/pg16-merge-validation.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ jobs:
423423
if ! su - gpadmin -c "
424424
cd ${SRC_DIR}
425425
export LD_LIBRARY_PATH=${INSTALL_PREFIX}/lib:${LD_LIBRARY_PATH}
426-
CFLAGS='-O0 -g3' ./configure \
426+
CFLAGS='-g3' ./configure \
427427
--prefix=${INSTALL_PREFIX} \
428428
--with-pgport=5432 \
429429
--enable-cassert \

src/Makefile

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@ include Makefile.mock
1616
# In PostgreSQL, libpqwalreceiver is linked into a separate shared library.
1717
# In GPDB, it's linked as a normal object, check src/backend/replication/Makefile.
1818
# Remove backend/replication/libpqwalreceiver from SUBDIRS here.
19-
#SUBDIRS = \
20-
# common \
21-
# port \
22-
# timezone \
23-
# backend \
24-
# backend/utils/mb/conversion_procs \
25-
# backend/snowball \
26-
# include \
27-
# interfaces \
28-
# backend/replication/pgoutput \
29-
# fe_utils \
30-
# bin
31-
# pl \
32-
# makefiles \
33-
# test/regress \
34-
# test/isolation \
35-
# test/perl
3619

3720
SUBDIRS = \
3821
common \
@@ -47,7 +30,10 @@ SUBDIRS = \
4730
fe_utils \
4831
bin \
4932
pl \
50-
test/regress
33+
makefiles \
34+
test/regress \
35+
test/isolation \
36+
test/perl
5137

5238
ifeq ($(with_llvm), yes)
5339
SUBDIRS += backend/jit/llvm

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/executor/nodeSplitMerge.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ MergeTupleTableSlot(TupleTableSlot *slot, SplitMerge *plannode, SplitMergeState
146146
/* Compute segment ID for the new row */
147147
int32 target_seg;
148148

149-
target_seg = evalHashKey(node, newslot->tts_values, newslot->tts_isnull);
149+
if (node->cdbhash)
150+
target_seg = evalHashKey(node, newslot->tts_values, newslot->tts_isnull);
151+
else
152+
target_seg = cdbhashrandomseg(plannode->numHashSegments);
150153

151154
slot->tts_values[node->segid_attno - 1] = Int32GetDatum(target_seg);
152155
slot->tts_isnull[node->segid_attno - 1] = false;

src/backend/gpopt/gpdbwrappers.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "catalog/pg_collation.h"
3535
extern "C" {
3636
#include "access/amapi.h"
37+
#include "commands/defrem.h"
3738
#include "access/external.h"
3839
#include "access/genam.h"
3940
#include "catalog/pg_inherits.h"
@@ -1960,8 +1961,8 @@ gpdb::GetMVNDistinct(Oid stat_oid)
19601961
{
19611962
GP_WRAP_START;
19621963
{
1963-
/* CBDB_16_MERGE: xxx: do we need ihn = true in any case? */
1964-
return statext_ndistinct_load(stat_oid, false);
1964+
bool inh = has_subclass(StatisticsGetRelation(stat_oid, false));
1965+
return statext_ndistinct_load(stat_oid, inh);
19651966
}
19661967
GP_WRAP_END;
19671968
}
@@ -1971,7 +1972,8 @@ gpdb::GetMVDependencies(Oid stat_oid)
19711972
{
19721973
GP_WRAP_START;
19731974
{
1974-
return statext_dependencies_load(stat_oid, false, true);
1975+
bool inh = has_subclass(StatisticsGetRelation(stat_oid, false));
1976+
return statext_dependencies_load(stat_oid, inh, true);
19751977
}
19761978
GP_WRAP_END;
19771979
}
@@ -2818,11 +2820,12 @@ gpdb::TestexprIsHashable(Node *testexpr, List *param_ids)
28182820
}
28192821

28202822
RTEPermissionInfo *
2821-
gpdb::GetRTEPermissionInfo(List *rteperminfos,
2822-
const RangeTblEntry *rte)
2823+
gpdb::GetRTEPermissionInfo(List *rteperminfos, const RangeTblEntry *rte)
28232824
{
28242825
GP_WRAP_START;
28252826
{
2827+
// Cast away const: upstream getRTEPermissionInfo() only reads
2828+
// rte->perminfoindex and rte->relid but its signature lacks const.
28262829
return getRTEPermissionInfo(rteperminfos, (RangeTblEntry *) rte);
28272830
}
28282831
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: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,24 @@ CTranslatorDXLToPlStmt::TranslateDXLTblScan(
659659

660660
// The postgres_fdw wrapper does not support row level security. So
661661
// passing only the query_quals while creating the foreign scan node.
662+
//
663+
// BuildForeignScan internally calls build_simple_rel which looks up
664+
// RTEPermissionInfo via root->parse->rteperminfos. The RTE here was
665+
// newly created by ORCA with its own perminfoindex numbering, which
666+
// may not match m_orig_query->rteperminfos (e.g. after the rewriter
667+
// expands external-table ON SELECT rules into subqueries the outer
668+
// query's rteperminfos shrinks). Temporarily swap in ORCA's own
669+
// perminfos list so the indices are consistent.
670+
Query *orig_query = m_dxl_to_plstmt_context->m_orig_query;
671+
List *saved_perminfos = orig_query->rteperminfos;
672+
orig_query->rteperminfos =
673+
m_dxl_to_plstmt_context->GetPermInfosList();
674+
662675
ForeignScan *foreign_scan =
663676
gpdb::CreateForeignScan(oidRel, index, query_quals, targetlist,
664-
m_dxl_to_plstmt_context->m_orig_query, rte);
677+
orig_query, rte);
678+
679+
orig_query->rteperminfos = saved_perminfos;
665680
foreign_scan->scan.scanrelid = index;
666681
plan = &(foreign_scan->scan.plan);
667682
plan_return = (Plan *) foreign_scan;
@@ -4611,9 +4626,15 @@ CTranslatorDXLToPlStmt::TranslateDXLDynForeignScan(
46114626
RelationGetDescr(childRel),
46124627
index, qual, targetlist);
46134628

4629+
// Same perminfos swap as in the non-dynamic foreign scan path above.
4630+
Query *orig_query = m_dxl_to_plstmt_context->m_orig_query;
4631+
List *saved_perminfos = orig_query->rteperminfos;
4632+
orig_query->rteperminfos =
4633+
m_dxl_to_plstmt_context->GetPermInfosList();
4634+
46144635
ForeignScan *foreign_scan_first_part =
46154636
gpdb::CreateForeignScan(oid_first_child, index, qual, targetlist,
4616-
m_dxl_to_plstmt_context->m_orig_query, rte);
4637+
orig_query, rte);
46174638

46184639
// Set the plan fields to the first partition. We still want the plan type to be
46194640
// a dynamic foreign scan
@@ -4645,11 +4666,14 @@ CTranslatorDXLToPlStmt::TranslateDXLDynForeignScan(
46454666

46464667
ForeignScan *foreign_scan =
46474668
gpdb::CreateForeignScan(rte->relid, index, qual, targetlist,
4648-
m_dxl_to_plstmt_context->m_orig_query, rte);
4669+
orig_query, rte);
46494670

46504671
dyn_foreign_scan->fdw_private_list = gpdb::LAppend(
46514672
dyn_foreign_scan->fdw_private_list, foreign_scan->fdw_private);
46524673
}
4674+
4675+
orig_query->rteperminfos = saved_perminfos;
4676+
46534677
// convert qual and targetlist back to root relation. This is used by the
46544678
// executor node to remap to the children
46554679
gpdb::RelationWrapper prevRel = gpdb::GetRelation(rte->relid);
@@ -5336,7 +5360,7 @@ CTranslatorDXLToPlStmt::ProcessDXLTblDescr(
53365360
rte->eref = alias;
53375361
rte->alias = alias;
53385362

5339-
m_dxl_to_plstmt_context->AddPerfmInfo(pi);
5363+
m_dxl_to_plstmt_context->AddPermInfo(pi);
53405364

53415365
// set up rte <> perm info link.
53425366
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);

src/test/isolation2/expected/concurrent_drop_truncate_tablespace.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,4 @@ SELECT gp_inject_fault('drop_tablespace_after_acquire_lock', 'reset', dbid) FROM
131131
DROP
132132
-- fail
133133
2<: <... completed>
134-
ERROR: tablespace "concurrent_tblspace" does not exist (seg0 127.0.1.1:7002 pid=1865353)
134+
ERROR: could not create directory "pg_tblspc/XXXX": No such file or directory

0 commit comments

Comments
 (0)