Refactor JoinRestrictionListExistsInContext to improve readability (#4249)

pull/4249/merge
Onur Tirtir 2020-10-16 12:24:56 +03:00 committed by GitHub
parent 212adfb26f
commit de6f2d3f42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 22 deletions

View File

@ -151,9 +151,8 @@ static bool RangeTableArrayContainsAnyRTEIdentities(RangeTblEntry **rangeTableEn
queryRteIdentities); queryRteIdentities);
static int RangeTableOffsetCompat(PlannerInfo *root, AppendRelInfo *appendRelInfo); static int RangeTableOffsetCompat(PlannerInfo *root, AppendRelInfo *appendRelInfo);
static Relids QueryRteIdentities(Query *queryTree); static Relids QueryRteIdentities(Query *queryTree);
static bool JoinRestrictionListExistsInContext(JoinRestriction *joinRestrictionInput, static bool ContextCoversJoinRestriction(JoinRestrictionContext *joinRestrictionContext,
JoinRestrictionContext * JoinRestriction *joinRestrictionInTest);
joinRestrictionContext);
/* /*
@ -2008,8 +2007,7 @@ RemoveDuplicateJoinRestrictions(JoinRestrictionContext *joinRestrictionContext)
{ {
JoinRestriction *joinRestriction = lfirst(joinRestrictionCell); JoinRestriction *joinRestriction = lfirst(joinRestrictionCell);
/* if we already have the same restrictions, skip */ if (ContextCoversJoinRestriction(filteredContext, joinRestriction))
if (JoinRestrictionListExistsInContext(joinRestriction, filteredContext))
{ {
continue; continue;
} }
@ -2023,25 +2021,19 @@ RemoveDuplicateJoinRestrictions(JoinRestrictionContext *joinRestrictionContext)
/* /*
* JoinRestrictionListExistsInContext returns true if the given joinRestrictionInput * ContextCoversJoinRestriction returns true if the given joinRestriction
* has an equivalent of in the given joinRestrictionContext. * has an equivalent of in the given joinRestrictionContext.
*/ */
static bool static bool
JoinRestrictionListExistsInContext(JoinRestriction *joinRestrictionInput, ContextCoversJoinRestriction(JoinRestrictionContext *joinRestrictionContext,
JoinRestrictionContext *joinRestrictionContext) JoinRestriction *joinRestrictionInTest)
{ {
List *joinRestrictionList = joinRestrictionContext->joinRestrictionList; JoinRestriction *joinRestrictionInContext = NULL;
List *inputJoinRestrictInfoList = joinRestrictionInput->joinRestrictInfoList; List *joinRestrictionInContextList = joinRestrictionContext->joinRestrictionList;
foreach_ptr(joinRestrictionInContext, joinRestrictionInContextList)
ListCell *joinRestrictionCell = NULL;
foreach(joinRestrictionCell, joinRestrictionList)
{ {
JoinRestriction *joinRestriction = lfirst(joinRestrictionCell);
List *joinRestrictInfoList = joinRestriction->joinRestrictInfoList;
/* obviously we shouldn't treat different join types as being the same */ /* obviously we shouldn't treat different join types as being the same */
if (joinRestriction->joinType != joinRestrictionInput->joinType) if (joinRestrictionInContext->joinType != joinRestrictionInTest->joinType)
{ {
continue; continue;
} }
@ -2050,14 +2042,14 @@ JoinRestrictionListExistsInContext(JoinRestriction *joinRestrictionInput,
* If we're dealing with different queries, we shouldn't treat their * If we're dealing with different queries, we shouldn't treat their
* restrictions as being the same. * restrictions as being the same.
*/ */
if (joinRestriction->plannerInfo != joinRestrictionInput->plannerInfo) if (joinRestrictionInContext->plannerInfo != joinRestrictionInTest->plannerInfo)
{ {
continue; continue;
} }
/* /*
* We check whether the restrictions in joinRestriction is a super set * We check whether the restrictions in joinRestrictionInTest is a subset
* of the restrictions in joinRestrictionInput in the sense that all the * of the restrictions in joinRestrictionInContext in the sense that all the
* restrictions in the latter already exists in the former. * restrictions in the latter already exists in the former.
* *
* Also, note that list_difference() returns a list that contains all the * Also, note that list_difference() returns a list that contains all the
@ -2065,7 +2057,11 @@ JoinRestrictionListExistsInContext(JoinRestriction *joinRestrictionInput,
* Finally, each element in these lists is a pointer to RestrictInfo * Finally, each element in these lists is a pointer to RestrictInfo
* structure, where equal() function is implemented for the struct. * structure, where equal() function is implemented for the struct.
*/ */
if (list_difference(joinRestrictInfoList, inputJoinRestrictInfoList) == NIL) List *joinRestrictInfoListInContext =
joinRestrictionInContext->joinRestrictInfoList;
List *joinRestrictInfoListInTest =
joinRestrictionInTest->joinRestrictInfoList;
if (LeftListIsSubset(joinRestrictInfoListInContext, joinRestrictInfoListInTest))
{ {
return true; return true;
} }

View File

@ -241,3 +241,15 @@ GenerateListFromElement(void *listElement, int listLength)
return list; return list;
} }
/*
* LeftListIsSubset returns true if lhs is subset of rhs list. Note that input
* lists' entries should implement equal() function.
*/
bool
LeftListIsSubset(const List *lhs, const List *rhs)
{
List *listDifference = list_difference(lhs, rhs);
return list_length(listDifference) == 0;
}

View File

@ -92,5 +92,6 @@ extern char * StringJoin(List *stringList, char delimiter);
extern List * ListTake(List *pointerList, int size); extern List * ListTake(List *pointerList, int size);
extern void * safe_list_nth(const List *list, int index); extern void * safe_list_nth(const List *list, int index);
extern List * GenerateListFromElement(void *listElement, int listLength); extern List * GenerateListFromElement(void *listElement, int listLength);
extern bool LeftListIsSubset(const List *lhs, const List *rhs);
#endif /* CITUS_LISTUTILS_H */ #endif /* CITUS_LISTUTILS_H */