Skip to content

Commit 6bf82a5

Browse files
committed
ORCA ignores empty or unsupported ext stats
STATS_EXT_DEPENDENCIES can be empty after analyzed. This is because when the degree of all combinations is 0, PG will not record the dependencies data in pg_statistic_ext_data. The current changes ensure that ORCA will not fail to generate a plan due to failure to read dependencies data, and also ignore unsupported ext stats type(STATS_EXT_EXPRESSIONS).
1 parent a171250 commit 6bf82a5

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

src/backend/gpopt/gpdbwrappers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,7 @@ gpdb::GetMVDependencies(Oid stat_oid)
19291929
{
19301930
GP_WRAP_START;
19311931
{
1932-
return statext_dependencies_load(stat_oid);
1932+
return statext_dependencies_load(stat_oid, true);
19331933
}
19341934
GP_WRAP_END;
19351935
}

src/backend/gpopt/translate/CTranslatorRelcacheToDXL.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -345,21 +345,24 @@ CTranslatorRelcacheToDXL::RetrieveExtStats(CMemoryPool *mp, IMDId *mdid)
345345
if (list_member_int(kinds, STATS_EXT_DEPENDENCIES))
346346
{
347347
MVDependencies *dependencies = gpdb::GetMVDependencies(stat_oid);
348+
// dependencies can be null after analyzed.
349+
if (dependencies) {
348350

349-
for (ULONG i = 0; i < dependencies->ndeps; i++)
350-
{
351-
MVDependency *dep = dependencies->deps[i];
352-
353-
// Note: MVDependency->attributes's last index is the dependent "to"
354-
// column.
355-
IntPtrArray *from_attnos = GPOS_NEW(mp) IntPtrArray(mp);
356-
for (INT j = 0; j < dep->nattributes - 1; j++)
351+
for (ULONG i = 0; i < dependencies->ndeps; i++)
357352
{
358-
from_attnos->Append(GPOS_NEW(mp) INT(dep->attributes[j]));
353+
MVDependency *dep = dependencies->deps[i];
354+
355+
// Note: MVDependency->attributes's last index is the dependent "to"
356+
// column.
357+
IntPtrArray *from_attnos = GPOS_NEW(mp) IntPtrArray(mp);
358+
for (INT j = 0; j < dep->nattributes - 1; j++)
359+
{
360+
from_attnos->Append(GPOS_NEW(mp) INT(dep->attributes[j]));
361+
}
362+
deps->Append(GPOS_NEW(mp) CMDDependency(
363+
mp, dep->degree, from_attnos,
364+
dep->attributes[dep->nattributes - 1]));
359365
}
360-
deps->Append(GPOS_NEW(mp) CMDDependency(
361-
mp, dep->degree, from_attnos,
362-
dep->attributes[dep->nattributes - 1]));
363366
}
364367
}
365368

@@ -442,12 +445,22 @@ CTranslatorRelcacheToDXL::RetrieveExtStatsInfo(CMemoryPool *mp, IMDId *mdid)
442445
statkind = CMDExtStatsInfo::EstatMCV;
443446
break;
444447
}
448+
case STATS_EXT_EXPRESSIONS:
449+
{
450+
statkind = CMDExtStatsInfo::EstatExpr;
451+
break;
452+
}
445453
default:
446454
{
447455
GPOS_ASSERT(false && "Unknown extended stat type");
448456
}
449457
}
450458

459+
// CBDB_MERGE_FIXME: support expr ext stats in the feature
460+
if (statkind == CMDExtStatsInfo::EstatExpr) {
461+
continue;
462+
}
463+
451464
const CWStringConst *statname = GPOS_NEW(mp)
452465
CWStringConst(CDXLUtils::CreateDynamicStringFromCharArray(
453466
mp, gpdb::GetExtStatsName(info->statOid))

src/backend/gporca/libnaucrates/include/naucrates/md/CMDExtStatsInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CMDExtStatsInfo : public CRefCount
4242
EstatDependencies,
4343
EstatNDistinct,
4444
EstatMCV,
45+
EstatExpr,
4546
EstatSentinel
4647
};
4748

src/backend/statistics/dependencies.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ dependency_is_fully_matched(MVDependency *dependency, Bitmapset *attnums)
619619
* Load the functional dependencies for the indicated pg_statistic_ext tuple
620620
*/
621621
MVDependencies *
622-
statext_dependencies_load(Oid mvoid)
622+
statext_dependencies_load(Oid mvoid, bool allow_null)
623623
{
624624
MVDependencies *result;
625625
bool isnull;
@@ -633,9 +633,17 @@ statext_dependencies_load(Oid mvoid)
633633
deps = SysCacheGetAttr(STATEXTDATASTXOID, htup,
634634
Anum_pg_statistic_ext_data_stxddependencies, &isnull);
635635
if (isnull)
636-
elog(ERROR,
637-
"requested statistics kind \"%c\" is not yet built for statistics object %u",
638-
STATS_EXT_DEPENDENCIES, mvoid);
636+
{
637+
if (!allow_null)
638+
{
639+
elog(ERROR,
640+
"requested statistics kind \"%c\" is not yet built for statistics object %u",
641+
STATS_EXT_DEPENDENCIES, mvoid);
642+
}
643+
644+
ReleaseSysCache(htup);
645+
return NULL;
646+
}
639647

640648
result = statext_dependencies_deserialize(DatumGetByteaPP(deps));
641649

@@ -1656,7 +1664,7 @@ dependencies_clauselist_selectivity(PlannerInfo *root,
16561664
if (nmatched + nexprs < 2)
16571665
continue;
16581666

1659-
deps = statext_dependencies_load(stat->statOid);
1667+
deps = statext_dependencies_load(stat->statOid, false);
16601668

16611669
/*
16621670
* The expressions may be represented by different attnums in the

src/include/statistics/statistics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ typedef struct MCVList
9595
} MCVList;
9696

9797
extern MVNDistinct *statext_ndistinct_load(Oid mvoid);
98-
extern MVDependencies *statext_dependencies_load(Oid mvoid);
98+
extern MVDependencies *statext_dependencies_load(Oid mvoid, bool allow_null);
9999
extern MCVList *statext_mcv_load(Oid mvoid);
100100

101101
extern void BuildRelationExtStatistics(Relation onerel, double totalrows,

0 commit comments

Comments
 (0)