From ba93d32c8afb9a7a2fa2c55109f0e94161d663ce Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Thu, 27 Apr 2017 16:03:05 -0700 Subject: [PATCH] Fix: Make FindShardIntervalIndex robust against 0 shards. --- .../distributed/utils/shardinterval_utils.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/backend/distributed/utils/shardinterval_utils.c b/src/backend/distributed/utils/shardinterval_utils.c index 8a63375e8..5133da261 100644 --- a/src/backend/distributed/utils/shardinterval_utils.c +++ b/src/backend/distributed/utils/shardinterval_utils.c @@ -247,11 +247,12 @@ FindShardInterval(Datum partitionColumnValue, DistTableCacheEntry *cacheEntry) * the searched value. Note that the searched value must be the hashed value * of the original value if the distribution method is hash. * - * Note that, if the searched value can not be found for hash partitioned tables, - * we error out. This should only happen if something is terribly wrong, either - * metadata tables are corrupted or we have a bug somewhere. Such as a hash - * function which returns a value not in the range of [INT32_MIN, INT32_MAX] can - * fire this. + * Note that, if the searched value can not be found for hash partitioned + * tables, we error out (unless there are no shards, in which case + * INVALID_SHARD_INDEX is returned). This should only happen if something is + * terribly wrong, either metadata tables are corrupted or we have a bug + * somewhere. Such as a hash function which returns a value not in the range + * of [INT32_MIN, INT32_MAX] can fire this. */ static int FindShardIntervalIndex(Datum searchedValue, DistTableCacheEntry *cacheEntry) @@ -264,6 +265,11 @@ FindShardIntervalIndex(Datum searchedValue, DistTableCacheEntry *cacheEntry) !cacheEntry->hasUniformHashDistribution); int shardIndex = INVALID_SHARD_INDEX; + if (shardCount == 0) + { + return INVALID_SHARD_INDEX; + } + if (partitionMethod == DISTRIBUTE_BY_HASH) { if (useBinarySearch)