Skip to content

Commit

Permalink
Merge acef92f into 4583f31
Browse files Browse the repository at this point in the history
  • Loading branch information
onderkalaci committed Jan 9, 2015
2 parents 4583f31 + acef92f commit b4550e2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
5 changes: 5 additions & 0 deletions expected/queries.out
Expand Up @@ -13,6 +13,11 @@ SELECT master_create_distributed_table('articles', 'author_id');

(1 row)

-- test when a table is distributed but no shards created yet
SELECT count(*) from articles;
ERROR: could not find any shards for query
DETAIL: No shards exist for distributed table "articles".
HINT: Run master_create_worker_shards to create shards and try again.
\set VERBOSITY terse
SELECT master_create_worker_shards('articles', 2, 1);
WARNING: Connection failed to adeadhost:5432
Expand Down
33 changes: 27 additions & 6 deletions pg_shard.c
Expand Up @@ -226,7 +226,10 @@ PgShardPlanner(Query *query, int cursorOptions, ParamListInfo boundParams)

ErrorIfQueryNotSupported(distributedQuery);

/* compute the list of shards this query needs to access */
/*
* Compute the list of shards this query needs to access.
* Error out if there are no existing shards for the table.
*/
queryShardList = DistributedQueryShardList(distributedQuery);

/*
Expand Down Expand Up @@ -530,17 +533,35 @@ ExtractRangeTableEntryWalker(Node *node, List **rangeTableList)

/*
* DistributedQueryShardList prunes the shards for the table in the query based
* on the query's restriction qualifiers, and returns this list.
* on the query's restriction qualifiers, and returns this list. If the function
* cannot find any shards for the distributed table, it errors out. In other sense,
* the function errors out or returns a non-empty list.
*/
static List *
DistributedQueryShardList(Query *query)
{
Oid distributedTableId = ExtractFirstDistributedTableId(query);
List *restrictClauseList = NIL;
List *prunedShardList = NIL;

List *restrictClauseList = QueryRestrictList(query);
Oid distributedTableId = ExtractFirstDistributedTableId(query);
List *shardIntervalList = LookupShardIntervalList(distributedTableId);
List *prunedShardList = PruneShardList(distributedTableId, restrictClauseList,
shardIntervalList);

/* error out if no shards exists for the table */
if (shardIntervalList == NIL)
{
char *relationName = get_rel_name(distributedTableId);

ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("could not find any shards for query"),
errdetail("No shards exist for distributed table \"%s\".",
relationName),
errhint("Run master_create_worker_shards to create shards "
"and try again.")));
}

restrictClauseList = QueryRestrictList(query);
prunedShardList = PruneShardList(distributedTableId, restrictClauseList,
shardIntervalList);

return prunedShardList;
}
Expand Down
3 changes: 3 additions & 0 deletions sql/queries.sql
Expand Up @@ -11,6 +11,9 @@ CREATE TABLE articles (

SELECT master_create_distributed_table('articles', 'author_id');

-- test when a table is distributed but no shards created yet
SELECT count(*) from articles;

\set VERBOSITY terse
SELECT master_create_worker_shards('articles', 2, 1);
\set VERBOSITY default
Expand Down

0 comments on commit b4550e2

Please sign in to comment.