manual work to find places where we could use new macros

feature/foreach_index
Nils Dijk 2021-11-03 18:29:56 +01:00
parent 9bc7ed13f0
commit bb2e5f0e13
No known key found for this signature in database
GPG Key ID: CA1177EF9434F241
14 changed files with 47 additions and 92 deletions

View File

@ -222,12 +222,12 @@ AppendStatTypes(StringInfo buf, CreateStatsStmt *stmt)
Value *statType = NULL; Value *statType = NULL;
foreach_ptr(statType, stmt->stat_types) foreach_ptr(statType, stmt->stat_types)
{ {
appendStringInfoString(buf, strVal(statType)); if (!foreach_first(statType))
if (statType != llast(stmt->stat_types))
{ {
appendStringInfoString(buf, ", "); appendStringInfoString(buf, ", ");
} }
appendStringInfoString(buf, strVal(statType));
} }
appendStringInfoString(buf, ")"); appendStringInfoString(buf, ")");
@ -250,14 +250,13 @@ AppendColumnNames(StringInfo buf, CreateStatsStmt *stmt)
"only simple column references are allowed in CREATE STATISTICS"))); "only simple column references are allowed in CREATE STATISTICS")));
} }
const char *columnName = quote_identifier(column->name); if (!foreach_first(column))
appendStringInfoString(buf, columnName);
if (column != llast(stmt->exprs))
{ {
appendStringInfoString(buf, ", "); appendStringInfoString(buf, ", ");
} }
const char *columnName = quote_identifier(column->name);
appendStringInfoString(buf, columnName);
} }
} }

View File

@ -637,7 +637,6 @@ QueryStringForFragmentsTransfer(NodeToNodeFragmentsTransfer *fragmentsTransfer)
{ {
StringInfo queryString = makeStringInfo(); StringInfo queryString = makeStringInfo();
StringInfo fragmentNamesArrayString = makeStringInfo(); StringInfo fragmentNamesArrayString = makeStringInfo();
int fragmentCount = 0;
NodePair *nodePair = &fragmentsTransfer->nodes; NodePair *nodePair = &fragmentsTransfer->nodes;
WorkerNode *sourceNode = LookupNodeByNodeIdOrError(nodePair->sourceNodeId); WorkerNode *sourceNode = LookupNodeByNodeIdOrError(nodePair->sourceNodeId);
@ -648,15 +647,13 @@ QueryStringForFragmentsTransfer(NodeToNodeFragmentsTransfer *fragmentsTransfer)
{ {
const char *fragmentName = fragment->resultId; const char *fragmentName = fragment->resultId;
if (fragmentCount > 0) if (!foreach_first(fragment))
{ {
appendStringInfoString(fragmentNamesArrayString, ","); appendStringInfoString(fragmentNamesArrayString, ",");
} }
appendStringInfoString(fragmentNamesArrayString, appendStringInfoString(fragmentNamesArrayString,
quote_literal_cstr(fragmentName)); quote_literal_cstr(fragmentName));
fragmentCount++;
} }
appendStringInfoString(fragmentNamesArrayString, "]::text[]"); appendStringInfoString(fragmentNamesArrayString, "]::text[]");

View File

@ -580,7 +580,6 @@ static int
PartitionColumnIndexFromColumnList(Oid relationId, List *columnNameList) PartitionColumnIndexFromColumnList(Oid relationId, List *columnNameList)
{ {
Var *partitionColumn = PartitionColumn(relationId, 0); Var *partitionColumn = PartitionColumn(relationId, 0);
int partitionColumnIndex = 0;
const char *columnName = NULL; const char *columnName = NULL;
foreach_ptr(columnName, columnNameList) foreach_ptr(columnName, columnNameList)
@ -590,10 +589,8 @@ PartitionColumnIndexFromColumnList(Oid relationId, List *columnNameList)
/* check whether this is the partition column */ /* check whether this is the partition column */
if (partitionColumn != NULL && attrNumber == partitionColumn->varattno) if (partitionColumn != NULL && attrNumber == partitionColumn->varattno)
{ {
return partitionColumnIndex; return foreach_index(columnName);
} }
partitionColumnIndex++;
} }
return -1; return -1;
@ -727,15 +724,12 @@ static int
PartitionColumnIndex(List *insertTargetList, Var *partitionColumn) PartitionColumnIndex(List *insertTargetList, Var *partitionColumn)
{ {
TargetEntry *insertTargetEntry = NULL; TargetEntry *insertTargetEntry = NULL;
int targetEntryIndex = 0;
foreach_ptr(insertTargetEntry, insertTargetList) foreach_ptr(insertTargetEntry, insertTargetList)
{ {
if (insertTargetEntry->resno == partitionColumn->varattno) if (insertTargetEntry->resno == partitionColumn->varattno)
{ {
return targetEntryIndex; return foreach_index(insertTargetEntry);
} }
targetEntryIndex++;
} }
return -1; return -1;
@ -803,11 +797,10 @@ static void
WrapTaskListForProjection(List *taskList, List *projectedTargetEntries) WrapTaskListForProjection(List *taskList, List *projectedTargetEntries)
{ {
StringInfo projectedColumnsString = makeStringInfo(); StringInfo projectedColumnsString = makeStringInfo();
int entryIndex = 0;
TargetEntry *targetEntry = NULL; TargetEntry *targetEntry = NULL;
foreach_ptr(targetEntry, projectedTargetEntries) foreach_ptr(targetEntry, projectedTargetEntries)
{ {
if (entryIndex != 0) if (!foreach_first(targetEntry))
{ {
appendStringInfoChar(projectedColumnsString, ','); appendStringInfoChar(projectedColumnsString, ',');
} }
@ -815,8 +808,6 @@ WrapTaskListForProjection(List *taskList, List *projectedTargetEntries)
char *columnName = targetEntry->resname; char *columnName = targetEntry->resname;
Assert(columnName != NULL); Assert(columnName != NULL);
appendStringInfoString(projectedColumnsString, quote_identifier(columnName)); appendStringInfoString(projectedColumnsString, quote_identifier(columnName));
entryIndex++;
} }
Task *task = NULL; Task *task = NULL;

View File

@ -460,8 +460,6 @@ SortTupleStore(CitusScanState *scanState)
Oid *collations = (Oid *) palloc(numberOfSortKeys * sizeof(Oid)); Oid *collations = (Oid *) palloc(numberOfSortKeys * sizeof(Oid));
bool *nullsFirst = (bool *) palloc(numberOfSortKeys * sizeof(bool)); bool *nullsFirst = (bool *) palloc(numberOfSortKeys * sizeof(bool));
int sortKeyIndex = 0;
/* /*
* Iterate on the returning target list and generate the necessary information * Iterate on the returning target list and generate the necessary information
* for sorting the tuples. * for sorting the tuples.
@ -477,12 +475,11 @@ SortTupleStore(CitusScanState *scanState)
&sortop, NULL, NULL, &sortop, NULL, NULL,
NULL); NULL);
sortColIdx[sortKeyIndex] = sortKeyIndex + 1; int index = foreach_index(returningEntry);
sortOperators[sortKeyIndex] = sortop; sortColIdx[index] = index + 1;
collations[sortKeyIndex] = exprCollation((Node *) returningEntry->expr); sortOperators[index] = sortop;
nullsFirst[sortKeyIndex] = false; collations[index] = exprCollation((Node *) returningEntry->expr);
nullsFirst[index] = false;
sortKeyIndex++;
} }
Tuplesortstate *tuplesortstate = Tuplesortstate *tuplesortstate =

View File

@ -1477,7 +1477,6 @@ BuildCachedShardList(CitusTableCacheEntry *cacheEntry)
{ {
Relation distShardRelation = table_open(DistShardRelationId(), AccessShareLock); Relation distShardRelation = table_open(DistShardRelationId(), AccessShareLock);
TupleDesc distShardTupleDesc = RelationGetDescr(distShardRelation); TupleDesc distShardTupleDesc = RelationGetDescr(distShardRelation);
int arrayIndex = 0;
shardIntervalArray = MemoryContextAllocZero(MetadataCacheMemoryContext, shardIntervalArray = MemoryContextAllocZero(MetadataCacheMemoryContext,
shardIntervalArrayLength * shardIntervalArrayLength *
@ -1501,13 +1500,12 @@ BuildCachedShardList(CitusTableCacheEntry *cacheEntry)
intervalTypeMod); intervalTypeMod);
MemoryContext oldContext = MemoryContextSwitchTo(MetadataCacheMemoryContext); MemoryContext oldContext = MemoryContextSwitchTo(MetadataCacheMemoryContext);
shardIntervalArray[arrayIndex] = CopyShardInterval(shardInterval); shardIntervalArray[foreach_index(shardTuple)] =
CopyShardInterval(shardInterval);
MemoryContextSwitchTo(oldContext); MemoryContextSwitchTo(oldContext);
heap_freetuple(shardTuple); heap_freetuple(shardTuple);
arrayIndex++;
} }
table_close(distShardRelation, AccessShareLock); table_close(distShardRelation, AccessShareLock);
@ -1603,7 +1601,6 @@ BuildCachedShardList(CitusTableCacheEntry *cacheEntry)
{ {
ShardInterval *shardInterval = sortedShardIntervalArray[shardIndex]; ShardInterval *shardInterval = sortedShardIntervalArray[shardIndex];
int64 shardId = shardInterval->shardId; int64 shardId = shardInterval->shardId;
int placementOffset = 0;
/* /*
* Enable quick lookups of this shard ID by adding it to ShardIdCacheHash * Enable quick lookups of this shard ID by adding it to ShardIdCacheHash
@ -1633,8 +1630,7 @@ BuildCachedShardList(CitusTableCacheEntry *cacheEntry)
GroupShardPlacement *srcPlacement = NULL; GroupShardPlacement *srcPlacement = NULL;
foreach_ptr(srcPlacement, placementList) foreach_ptr(srcPlacement, placementList)
{ {
placementArray[placementOffset] = *srcPlacement; placementArray[foreach_index(srcPlacement)] = *srcPlacement;
placementOffset++;
} }
MemoryContextSwitchTo(oldContext); MemoryContextSwitchTo(oldContext);

View File

@ -990,6 +990,11 @@ ShardListInsertCommand(List *shardIntervalList)
appendStringInfo(maxHashToken, "NULL"); appendStringInfo(maxHashToken, "NULL");
} }
if (!foreach_first(shardInterval))
{
appendStringInfo(insertShardCommand, ", ");
}
appendStringInfo(insertShardCommand, appendStringInfo(insertShardCommand,
"(%s::regclass, %ld, '%c'::\"char\", %s, %s)", "(%s::regclass, %ld, '%c'::\"char\", %s, %s)",
quote_literal_cstr(qualifiedRelationName), quote_literal_cstr(qualifiedRelationName),
@ -997,11 +1002,6 @@ ShardListInsertCommand(List *shardIntervalList)
shardInterval->storageType, shardInterval->storageType,
minHashToken->data, minHashToken->data,
maxHashToken->data); maxHashToken->data);
if (llast(shardIntervalList) != shardInterval)
{
appendStringInfo(insertShardCommand, ", ");
}
} }
appendStringInfo(insertShardCommand, ") "); appendStringInfo(insertShardCommand, ") ");

View File

@ -280,7 +280,6 @@ CheckRebalanceStateInvariants(const RebalanceState *state)
{ {
NodeFillState *fillState = NULL; NodeFillState *fillState = NULL;
NodeFillState *prevFillState = NULL; NodeFillState *prevFillState = NULL;
int fillStateIndex = 0;
int fillStateLength = list_length(state->fillStateListAsc); int fillStateLength = list_length(state->fillStateListAsc);
Assert(state != NULL); Assert(state != NULL);
@ -300,8 +299,8 @@ CheckRebalanceStateInvariants(const RebalanceState *state)
} }
/* Check that fillStateListDesc is the reversed version of fillStateListAsc */ /* Check that fillStateListDesc is the reversed version of fillStateListAsc */
Assert(list_nth(state->fillStateListDesc, fillStateLength - fillStateIndex - 1) == Assert(list_nth(state->fillStateListDesc,
fillState); fillStateLength - foreach_index(fillState) - 1) == fillState);
foreach_ptr(shardCost, fillState->shardCostListDesc) foreach_ptr(shardCost, fillState->shardCostListDesc)
@ -333,7 +332,6 @@ CheckRebalanceStateInvariants(const RebalanceState *state)
1000); 1000);
prevFillState = fillState; prevFillState = fillState;
fillStateIndex++;
} }
} }

View File

@ -62,7 +62,6 @@ Datum
load_shard_id_array(PG_FUNCTION_ARGS) load_shard_id_array(PG_FUNCTION_ARGS)
{ {
Oid distributedTableId = PG_GETARG_OID(0); Oid distributedTableId = PG_GETARG_OID(0);
int shardIdIndex = 0;
Oid shardIdTypeId = INT8OID; Oid shardIdTypeId = INT8OID;
List *shardList = LoadShardIntervalList(distributedTableId); List *shardList = LoadShardIntervalList(distributedTableId);
@ -75,8 +74,7 @@ load_shard_id_array(PG_FUNCTION_ARGS)
{ {
Datum shardIdDatum = Int64GetDatum(shardInterval->shardId); Datum shardIdDatum = Int64GetDatum(shardInterval->shardId);
shardIdDatumArray[shardIdIndex] = shardIdDatum; shardIdDatumArray[foreach_index(shardInterval)] = shardIdDatum;
shardIdIndex++;
} }
ArrayType *shardIdArrayType = DatumArrayToArrayType(shardIdDatumArray, shardIdCount, ArrayType *shardIdArrayType = DatumArrayToArrayType(shardIdDatumArray, shardIdCount,
@ -122,7 +120,6 @@ load_shard_placement_array(PG_FUNCTION_ARGS)
int64 shardId = PG_GETARG_INT64(0); int64 shardId = PG_GETARG_INT64(0);
bool onlyActive = PG_GETARG_BOOL(1); bool onlyActive = PG_GETARG_BOOL(1);
List *placementList = NIL; List *placementList = NIL;
int placementIndex = 0;
Oid placementTypeId = TEXTOID; Oid placementTypeId = TEXTOID;
StringInfo placementInfo = makeStringInfo(); StringInfo placementInfo = makeStringInfo();
@ -146,8 +143,8 @@ load_shard_placement_array(PG_FUNCTION_ARGS)
appendStringInfo(placementInfo, "%s:%d", placement->nodeName, appendStringInfo(placementInfo, "%s:%d", placement->nodeName,
placement->nodePort); placement->nodePort);
placementDatumArray[placementIndex] = CStringGetTextDatum(placementInfo->data); placementDatumArray[foreach_index(placement)] =
placementIndex++; CStringGetTextDatum(placementInfo->data);
resetStringInfo(placementInfo); resetStringInfo(placementInfo);
} }

View File

@ -44,7 +44,6 @@ master_metadata_snapshot(PG_FUNCTION_ARGS)
List *dropSnapshotCommands = MetadataDropCommands(); List *dropSnapshotCommands = MetadataDropCommands();
List *createSnapshotCommands = MetadataCreateCommands(); List *createSnapshotCommands = MetadataCreateCommands();
List *snapshotCommandList = NIL; List *snapshotCommandList = NIL;
int snapshotCommandIndex = 0;
Oid ddlCommandTypeId = TEXTOID; Oid ddlCommandTypeId = TEXTOID;
snapshotCommandList = list_concat(snapshotCommandList, dropSnapshotCommands); snapshotCommandList = list_concat(snapshotCommandList, dropSnapshotCommands);
@ -58,8 +57,8 @@ master_metadata_snapshot(PG_FUNCTION_ARGS)
{ {
Datum metadataSnapshotCommandDatum = CStringGetTextDatum(metadataSnapshotCommand); Datum metadataSnapshotCommandDatum = CStringGetTextDatum(metadataSnapshotCommand);
snapshotCommandDatumArray[snapshotCommandIndex] = metadataSnapshotCommandDatum; snapshotCommandDatumArray[foreach_index(metadataSnapshotCommand)] =
snapshotCommandIndex++; metadataSnapshotCommandDatum;
} }
ArrayType *snapshotCommandArrayType = DatumArrayToArrayType(snapshotCommandDatumArray, ArrayType *snapshotCommandArrayType = DatumArrayToArrayType(snapshotCommandDatumArray,

View File

@ -207,7 +207,6 @@ MakeTextPartitionExpression(Oid distributedTableId, text *value)
static ArrayType * static ArrayType *
PrunedShardIdsForTable(Oid distributedTableId, List *whereClauseList) PrunedShardIdsForTable(Oid distributedTableId, List *whereClauseList)
{ {
int shardIdIndex = 0;
Oid shardIdTypeId = INT8OID; Oid shardIdTypeId = INT8OID;
Index tableId = 1; Index tableId = 1;
@ -222,8 +221,7 @@ PrunedShardIdsForTable(Oid distributedTableId, List *whereClauseList)
{ {
Datum shardIdDatum = Int64GetDatum(shardInterval->shardId); Datum shardIdDatum = Int64GetDatum(shardInterval->shardId);
shardIdDatumArray[shardIdIndex] = shardIdDatum; shardIdDatumArray[foreach_index(shardInterval)] = shardIdDatum;
shardIdIndex++;
} }
ArrayType *shardIdArrayType = DatumArrayToArrayType(shardIdDatumArray, shardIdCount, ArrayType *shardIdArrayType = DatumArrayToArrayType(shardIdDatumArray, shardIdCount,

View File

@ -671,7 +671,7 @@ WaitsForToString(List *waitsFor)
TransactionNode *waitingNode = NULL; TransactionNode *waitingNode = NULL;
foreach_ptr(waitingNode, waitsFor) foreach_ptr(waitingNode, waitsFor)
{ {
if (transactionIdStr->len != 0) if (!foreach_first(waitingNode))
{ {
appendStringInfoString(transactionIdStr, ","); appendStringInfoString(transactionIdStr, ",");
} }

View File

@ -182,7 +182,6 @@ get_colocated_shard_array(PG_FUNCTION_ARGS)
int colocatedShardCount = list_length(colocatedShardList); int colocatedShardCount = list_length(colocatedShardList);
Datum *colocatedShardsDatumArray = palloc0(colocatedShardCount * sizeof(Datum)); Datum *colocatedShardsDatumArray = palloc0(colocatedShardCount * sizeof(Datum));
Oid arrayTypeId = OIDOID; Oid arrayTypeId = OIDOID;
int colocatedShardIndex = 0;
ShardInterval *colocatedShardInterval = NULL; ShardInterval *colocatedShardInterval = NULL;
foreach_ptr(colocatedShardInterval, colocatedShardList) foreach_ptr(colocatedShardInterval, colocatedShardList)
@ -191,8 +190,8 @@ get_colocated_shard_array(PG_FUNCTION_ARGS)
Datum colocatedShardDatum = Int64GetDatum(colocatedShardId); Datum colocatedShardDatum = Int64GetDatum(colocatedShardId);
colocatedShardsDatumArray[colocatedShardIndex] = colocatedShardDatum; colocatedShardsDatumArray[foreach_index(colocatedShardInterval)] =
colocatedShardIndex++; colocatedShardDatum;
} }
ArrayType *colocatedShardsArrayType = DatumArrayToArrayType(colocatedShardsDatumArray, ArrayType *colocatedShardsArrayType = DatumArrayToArrayType(colocatedShardsDatumArray,

View File

@ -43,9 +43,7 @@ SortList(List *pointerList, int (*comparisonFunction)(const void *, const void *
void *pointer = NULL; void *pointer = NULL;
foreach_ptr(pointer, pointerList) foreach_ptr(pointer, pointerList)
{ {
array[arrayIndex] = pointer; array[foreach_index(pointer)] = pointer;
arrayIndex++;
} }
/* sort the array of pointers using the comparison function */ /* sort the array of pointers using the comparison function */
@ -77,13 +75,11 @@ PointerArrayFromList(List *pointerList)
{ {
int pointerCount = list_length(pointerList); int pointerCount = list_length(pointerList);
void **pointerArray = (void **) palloc0(pointerCount * sizeof(void *)); void **pointerArray = (void **) palloc0(pointerCount * sizeof(void *));
int pointerIndex = 0;
void *pointer = NULL; void *pointer = NULL;
foreach_ptr(pointer, pointerList) foreach_ptr(pointer, pointerList)
{ {
pointerArray[pointerIndex] = pointer; pointerArray[foreach_index(pointer)] = pointer;
pointerIndex += 1;
} }
return pointerArray; return pointerArray;
@ -188,15 +184,13 @@ StringJoin(List *stringList, char delimiter)
StringInfo joinedString = makeStringInfo(); StringInfo joinedString = makeStringInfo();
const char *command = NULL; const char *command = NULL;
int curIndex = 0;
foreach_ptr(command, stringList) foreach_ptr(command, stringList)
{ {
if (curIndex > 0) if (!foreach_first(command))
{ {
appendStringInfoChar(joinedString, delimiter); appendStringInfoChar(joinedString, delimiter);
} }
appendStringInfoString(joinedString, command); appendStringInfoString(joinedString, command);
curIndex++;
} }
return joinedString->data; return joinedString->data;
@ -212,14 +206,12 @@ List *
ListTake(List *pointerList, int size) ListTake(List *pointerList, int size)
{ {
List *result = NIL; List *result = NIL;
int listIndex = 0;
void *pointer = NULL; void *pointer = NULL;
foreach_ptr(pointer, pointerList) foreach_ptr(pointer, pointerList)
{ {
result = lappend(result, pointer); result = lappend(result, pointer);
listIndex++; if (list_length(result) >= size)
if (listIndex >= size)
{ {
break; break;
} }

View File

@ -216,8 +216,6 @@ static void
LockShardListResourcesOnFirstWorker(LOCKMODE lockmode, List *shardIntervalList) LockShardListResourcesOnFirstWorker(LOCKMODE lockmode, List *shardIntervalList)
{ {
StringInfo lockCommand = makeStringInfo(); StringInfo lockCommand = makeStringInfo();
int processedShardIntervalCount = 0;
int totalShardIntervalCount = list_length(shardIntervalList);
WorkerNode *firstWorkerNode = GetFirstPrimaryWorkerNode(); WorkerNode *firstWorkerNode = GetFirstPrimaryWorkerNode();
int connectionFlags = 0; int connectionFlags = 0;
const char *superuser = CurrentUserName(); const char *superuser = CurrentUserName();
@ -227,15 +225,13 @@ LockShardListResourcesOnFirstWorker(LOCKMODE lockmode, List *shardIntervalList)
ShardInterval *shardInterval = NULL; ShardInterval *shardInterval = NULL;
foreach_ptr(shardInterval, shardIntervalList) foreach_ptr(shardInterval, shardIntervalList)
{ {
int64 shardId = shardInterval->shardId; if (!foreach_first(shardInterval))
appendStringInfo(lockCommand, "%lu", shardId);
processedShardIntervalCount++;
if (processedShardIntervalCount != totalShardIntervalCount)
{ {
appendStringInfo(lockCommand, ", "); appendStringInfo(lockCommand, ", ");
} }
int64 shardId = shardInterval->shardId;
appendStringInfo(lockCommand, "%lu", shardId);
} }
appendStringInfo(lockCommand, "])"); appendStringInfo(lockCommand, "])");
@ -303,8 +299,6 @@ void
LockShardListMetadataOnWorkers(LOCKMODE lockmode, List *shardIntervalList) LockShardListMetadataOnWorkers(LOCKMODE lockmode, List *shardIntervalList)
{ {
StringInfo lockCommand = makeStringInfo(); StringInfo lockCommand = makeStringInfo();
int processedShardIntervalCount = 0;
int totalShardIntervalCount = list_length(shardIntervalList);
if (list_length(shardIntervalList) == 0) if (list_length(shardIntervalList) == 0)
{ {
@ -316,15 +310,13 @@ LockShardListMetadataOnWorkers(LOCKMODE lockmode, List *shardIntervalList)
ShardInterval *shardInterval = NULL; ShardInterval *shardInterval = NULL;
foreach_ptr(shardInterval, shardIntervalList) foreach_ptr(shardInterval, shardIntervalList)
{ {
int64 shardId = shardInterval->shardId; if (!foreach_first(shardInterval))
appendStringInfo(lockCommand, "%lu", shardId);
processedShardIntervalCount++;
if (processedShardIntervalCount != totalShardIntervalCount)
{ {
appendStringInfo(lockCommand, ", "); appendStringInfo(lockCommand, ", ");
} }
int64 shardId = shardInterval->shardId;
appendStringInfo(lockCommand, "%lu", shardId);
} }
appendStringInfo(lockCommand, "])"); appendStringInfo(lockCommand, "])");