Skip to content

Commit

Permalink
ORCA ignores empty or unsupported ext stats
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jiaqizho committed Jan 15, 2025
1 parent e7d8fdf commit 6a5f2f7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/backend/gpopt/gpdbwrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ gpdb::GetMVDependencies(Oid stat_oid)
{
GP_WRAP_START;
{
return statext_dependencies_load(stat_oid);
return statext_dependencies_load(stat_oid, true);
}
GP_WRAP_END;
}
Expand Down
37 changes: 25 additions & 12 deletions src/backend/gpopt/translate/CTranslatorRelcacheToDXL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,21 +345,24 @@ CTranslatorRelcacheToDXL::RetrieveExtStats(CMemoryPool *mp, IMDId *mdid)
if (list_member_int(kinds, STATS_EXT_DEPENDENCIES))
{
MVDependencies *dependencies = gpdb::GetMVDependencies(stat_oid);
// dependencies can be null after analyzed.
if (dependencies) {

for (ULONG i = 0; i < dependencies->ndeps; i++)
{
MVDependency *dep = dependencies->deps[i];

// Note: MVDependency->attributes's last index is the dependent "to"
// column.
IntPtrArray *from_attnos = GPOS_NEW(mp) IntPtrArray(mp);
for (INT j = 0; j < dep->nattributes - 1; j++)
for (ULONG i = 0; i < dependencies->ndeps; i++)
{
from_attnos->Append(GPOS_NEW(mp) INT(dep->attributes[j]));
MVDependency *dep = dependencies->deps[i];

// Note: MVDependency->attributes's last index is the dependent "to"
// column.
IntPtrArray *from_attnos = GPOS_NEW(mp) IntPtrArray(mp);
for (INT j = 0; j < dep->nattributes - 1; j++)
{
from_attnos->Append(GPOS_NEW(mp) INT(dep->attributes[j]));
}
deps->Append(GPOS_NEW(mp) CMDDependency(
mp, dep->degree, from_attnos,
dep->attributes[dep->nattributes - 1]));
}
deps->Append(GPOS_NEW(mp) CMDDependency(
mp, dep->degree, from_attnos,
dep->attributes[dep->nattributes - 1]));
}
}

Expand Down Expand Up @@ -442,12 +445,22 @@ CTranslatorRelcacheToDXL::RetrieveExtStatsInfo(CMemoryPool *mp, IMDId *mdid)
statkind = CMDExtStatsInfo::EstatMCV;
break;
}
case STATS_EXT_EXPRESSIONS:
{
statkind = CMDExtStatsInfo::EstatExpr;
break;
}
default:
{
GPOS_ASSERT(false && "Unknown extended stat type");
}
}

// CBDB_MERGE_FIXME: support expr ext stats in the feature
if (statkind == CMDExtStatsInfo::EstatExpr) {
continue;
}

const CWStringConst *statname = GPOS_NEW(mp)
CWStringConst(CDXLUtils::CreateDynamicStringFromCharArray(
mp, gpdb::GetExtStatsName(info->statOid))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class CMDExtStatsInfo : public CRefCount
EstatDependencies,
EstatNDistinct,
EstatMCV,
EstatExpr,
EstatSentinel
};

Expand Down
18 changes: 13 additions & 5 deletions src/backend/statistics/dependencies.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ dependency_is_fully_matched(MVDependency *dependency, Bitmapset *attnums)
* Load the functional dependencies for the indicated pg_statistic_ext tuple
*/
MVDependencies *
statext_dependencies_load(Oid mvoid)
statext_dependencies_load(Oid mvoid, bool allow_null)
{
MVDependencies *result;
bool isnull;
Expand All @@ -633,9 +633,17 @@ statext_dependencies_load(Oid mvoid)
deps = SysCacheGetAttr(STATEXTDATASTXOID, htup,
Anum_pg_statistic_ext_data_stxddependencies, &isnull);
if (isnull)
elog(ERROR,
"requested statistics kind \"%c\" is not yet built for statistics object %u",
STATS_EXT_DEPENDENCIES, mvoid);
{
if (!allow_null)
{
elog(ERROR,
"requested statistics kind \"%c\" is not yet built for statistics object %u",
STATS_EXT_DEPENDENCIES, mvoid);
}

ReleaseSysCache(htup);
return NULL;
}

result = statext_dependencies_deserialize(DatumGetByteaPP(deps));

Expand Down Expand Up @@ -1656,7 +1664,7 @@ dependencies_clauselist_selectivity(PlannerInfo *root,
if (nmatched + nexprs < 2)
continue;

deps = statext_dependencies_load(stat->statOid);
deps = statext_dependencies_load(stat->statOid, false);

/*
* The expressions may be represented by different attnums in the
Expand Down
2 changes: 1 addition & 1 deletion src/include/statistics/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ typedef struct MCVList
} MCVList;

extern MVNDistinct *statext_ndistinct_load(Oid mvoid);
extern MVDependencies *statext_dependencies_load(Oid mvoid);
extern MVDependencies *statext_dependencies_load(Oid mvoid, bool allow_null);
extern MCVList *statext_mcv_load(Oid mvoid);

extern void BuildRelationExtStatistics(Relation onerel, double totalrows,
Expand Down

0 comments on commit 6a5f2f7

Please sign in to comment.