Handle some NULL issues that static analysis found (#4001)

Static analysis found some issues where we used the result from
ExtractResultRelationRTE, without checking that it wasn't NULL. It seems
like in all these cases it can never actually be NULL, since we have checked
before that it isn't a SELECT query. So, this PR is mostly to make static
analysis happy (and protect a bit against future changes of the code).
pull/4008/head^2
Jelte Fennema 2020-07-09 15:46:42 +02:00 committed by GitHub
parent 96adce77d6
commit 759e628dd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 5 deletions

View File

@ -74,7 +74,7 @@ RebuildQueryStrings(Job *workerJob)
query = copyObject(originalQuery); query = copyObject(originalQuery);
RangeTblEntry *copiedInsertRte = ExtractResultRelationRTE(query); RangeTblEntry *copiedInsertRte = ExtractResultRelationRTEOrError(query);
RangeTblEntry *copiedSubqueryRte = ExtractSelectRangeTableEntry(query); RangeTblEntry *copiedSubqueryRte = ExtractSelectRangeTableEntry(query);
Query *copiedSubquery = copiedSubqueryRte->subquery; Query *copiedSubquery = copiedSubqueryRte->subquery;

View File

@ -275,7 +275,7 @@ CreateDistributedInsertSelectPlan(Query *originalQuery,
uint32 taskIdIndex = 1; /* 0 is reserved for invalid taskId */ uint32 taskIdIndex = 1; /* 0 is reserved for invalid taskId */
uint64 jobId = INVALID_JOB_ID; uint64 jobId = INVALID_JOB_ID;
DistributedPlan *distributedPlan = CitusMakeNode(DistributedPlan); DistributedPlan *distributedPlan = CitusMakeNode(DistributedPlan);
RangeTblEntry *insertRte = ExtractResultRelationRTE(originalQuery); RangeTblEntry *insertRte = ExtractResultRelationRTEOrError(originalQuery);
RangeTblEntry *subqueryRte = ExtractSelectRangeTableEntry(originalQuery); RangeTblEntry *subqueryRte = ExtractSelectRangeTableEntry(originalQuery);
Oid targetRelationId = insertRte->relid; Oid targetRelationId = insertRte->relid;
CitusTableCacheEntry *targetCacheEntry = GetCitusTableCacheEntry(targetRelationId); CitusTableCacheEntry *targetCacheEntry = GetCitusTableCacheEntry(targetRelationId);
@ -649,7 +649,7 @@ RouterModifyTaskForShardInterval(Query *originalQuery,
DeferredErrorMessage **routerPlannerError) DeferredErrorMessage **routerPlannerError)
{ {
Query *copiedQuery = copyObject(originalQuery); Query *copiedQuery = copyObject(originalQuery);
RangeTblEntry *copiedInsertRte = ExtractResultRelationRTE(copiedQuery); RangeTblEntry *copiedInsertRte = ExtractResultRelationRTEOrError(copiedQuery);
RangeTblEntry *copiedSubqueryRte = ExtractSelectRangeTableEntry(copiedQuery); RangeTblEntry *copiedSubqueryRte = ExtractSelectRangeTableEntry(copiedQuery);
Query *copiedSubquery = (Query *) copiedSubqueryRte->subquery; Query *copiedSubquery = (Query *) copiedSubqueryRte->subquery;
@ -1344,7 +1344,7 @@ CreateNonPushableInsertSelectPlan(uint64 planId, Query *parse, ParamListInfo bou
Query *insertSelectQuery = copyObject(parse); Query *insertSelectQuery = copyObject(parse);
RangeTblEntry *selectRte = ExtractSelectRangeTableEntry(insertSelectQuery); RangeTblEntry *selectRte = ExtractSelectRangeTableEntry(insertSelectQuery);
RangeTblEntry *insertRte = ExtractResultRelationRTE(insertSelectQuery); RangeTblEntry *insertRte = ExtractResultRelationRTEOrError(insertSelectQuery);
Oid targetRelationId = insertRte->relid; Oid targetRelationId = insertRte->relid;
DistributedPlan *distributedPlan = CitusMakeNode(DistributedPlan); DistributedPlan *distributedPlan = CitusMakeNode(DistributedPlan);

View File

@ -508,7 +508,9 @@ ResultRelationOidForQuery(Query *query)
/* /*
* ExtractResultRelationRTE returns the table's resultRelation range table entry. * ExtractResultRelationRTE returns the table's resultRelation range table
* entry. This returns NULL when there's no resultRelation, such as in a SELECT
* query.
*/ */
RangeTblEntry * RangeTblEntry *
ExtractResultRelationRTE(Query *query) ExtractResultRelationRTE(Query *query)
@ -522,6 +524,28 @@ ExtractResultRelationRTE(Query *query)
} }
/*
* ExtractResultRelationRTEOrError returns the table's resultRelation range table
* entry and errors out if there's no result relation at all, e.g. like in a
* SELECT query.
*
* This is a separate function (instead of using missingOk), so static analysis
* reasons about NULL returns correctly.
*/
RangeTblEntry *
ExtractResultRelationRTEOrError(Query *query)
{
RangeTblEntry *relation = ExtractResultRelationRTE(query);
if (relation == NULL)
{
ereport(ERROR, (errmsg("no result relation could be found for the query"),
errhint("is this a SELECT query?")));
}
return relation;
}
/* /*
* IsTidColumn gets a node and returns true if the node is a Var type of TID. * IsTidColumn gets a node and returns true if the node is a Var type of TID.
*/ */

View File

@ -71,6 +71,7 @@ extern Oid ExtractFirstCitusTableId(Query *query);
extern RangeTblEntry * ExtractSelectRangeTableEntry(Query *query); extern RangeTblEntry * ExtractSelectRangeTableEntry(Query *query);
extern Oid ModifyQueryResultRelationId(Query *query); extern Oid ModifyQueryResultRelationId(Query *query);
extern RangeTblEntry * ExtractResultRelationRTE(Query *query); extern RangeTblEntry * ExtractResultRelationRTE(Query *query);
extern RangeTblEntry * ExtractResultRelationRTEOrError(Query *query);
extern RangeTblEntry * ExtractDistributedInsertValuesRTE(Query *query); extern RangeTblEntry * ExtractDistributedInsertValuesRTE(Query *query);
extern bool IsMultiRowInsert(Query *query); extern bool IsMultiRowInsert(Query *query);
extern void AddShardIntervalRestrictionToSelect(Query *subqery, extern void AddShardIntervalRestrictionToSelect(Query *subqery,