Skip to content

Commit

Permalink
Merge 20b17d6 into 036e3a8
Browse files Browse the repository at this point in the history
  • Loading branch information
onderkalaci committed Feb 6, 2015
2 parents 036e3a8 + 20b17d6 commit 47b1577
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 6 deletions.
38 changes: 38 additions & 0 deletions create_shards.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "create_shards.h"
#include "ddl_commands.h"
#include "distribution_metadata.h"
#include "prune_shard_list.h"

#include <ctype.h>
#include <limits.h>
Expand All @@ -29,8 +30,11 @@
#include <string.h>

#include "access/attnum.h"
#include "access/skey.h"
#include "catalog/namespace.h"
#include "catalog/pg_class.h"
#include "catalog/pg_am.h"
#include "commands/defrem.h"
#include "lib/stringinfo.h"
#include "nodes/pg_list.h"
#include "nodes/primnodes.h"
Expand Down Expand Up @@ -73,6 +77,11 @@ master_create_distributed_table(PG_FUNCTION_ARGS)
AttrNumber partitionColumnId = InvalidAttrNumber;
char *partitionColumnName = text_to_cstring(partitionColumnText);
char *tableName = text_to_cstring(tableNameText);
Var* partitionColumnVar = NULL;
Oid partitionColumnTypeId = InvalidOid;
Oid partitionColumnOpClassId = InvalidOid;
Oid lessEqualOperatorId = InvalidOid;
Oid greaterEqualOperatorId = InvalidOid;

/* verify target relation is either regular or foreign table */
relationKind = get_rel_relkind(distributedTableId);
Expand All @@ -97,6 +106,35 @@ master_create_distributed_table(PG_FUNCTION_ARGS)
ereport(ERROR, (errmsg("unsupported partition method: %c", partitionMethod)));
}

partitionColumnVar = ColumnNameToColumn(distributedTableId, partitionColumnName);
partitionColumnTypeId = partitionColumnVar->vartype;
partitionColumnOpClassId = GetDefaultOpClass(partitionColumnTypeId, BTREE_AM_OID);

/* if there is no operator class for the partition column, error out */
if (partitionColumnOpClassId == InvalidOid)
{
ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("cannot distribute relation: \"%s\"", tableName),
errdetail("Column \"%s\" is not eligible to be the distribution "
"column.", partitionColumnName),
errhint("Try another column as the distribution column.")));
}

lessEqualOperatorId = GetOperatorByType(partitionColumnTypeId, BTREE_AM_OID,
BTLessEqualStrategyNumber);
greaterEqualOperatorId = GetOperatorByType(partitionColumnTypeId, BTREE_AM_OID,
BTGreaterEqualStrategyNumber);

/* if partition column does not support <= and >= operators, error out*/
if (lessEqualOperatorId == InvalidOid || greaterEqualOperatorId == InvalidOid)
{
ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("cannot distribute relation: \"%s\"", tableName),
errdetail("Column \"%s\" does not support comparison operators.",
partitionColumnName),
errhint("Try another column as the distribution column.")));
}

/* insert row into the partition metadata table */
InsertPartitionRow(distributedTableId, partitionMethod, partitionColumnText);

Expand Down
3 changes: 1 addition & 2 deletions distribution_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ static List *ShardIntervalListCache = NIL;


/* local function forward declarations */
static Var * ColumnNameToColumn(Oid relationId, char *columnName);
static void LoadShardIntervalRow(int64 shardId, Oid *relationId,
char **minValue, char **maxValue);
static ShardPlacement * TupleToShardPlacement(HeapTuple heapTuple,
Expand Down Expand Up @@ -445,7 +444,7 @@ IsDistributedTable(Oid tableId)
* a Var that represents that column in that relation. This function throws an
* error if the column doesn't exist or is a system column.
*/
static Var *
Var *
ColumnNameToColumn(Oid relationId, char *columnName)
{
Var *partitionColumn = NULL;
Expand Down
1 change: 1 addition & 0 deletions distribution_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ extern List * LoadShardPlacementList(int64 shardId);
extern Var * PartitionColumn(Oid distributedTableId);
extern char PartitionType(Oid distributedTableId);
extern bool IsDistributedTable(Oid tableId);
extern Var * ColumnNameToColumn(Oid relationId, char *columnName);
extern void InsertPartitionRow(Oid distributedTableId, char partitionType,
text *partitionKeyText);
extern void InsertShardRow(Oid distributedTableId, uint64 shardId, char shardStorage,
Expand Down
8 changes: 7 additions & 1 deletion expected/create_shards.out.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ CREATE FUNCTION create_table_then_fail(cstring, integer)
-- ===================================================================
CREATE TABLE table_to_distribute (
name text,
id bigint PRIMARY KEY
id bigint PRIMARY KEY,
data json
);
-- use an index instead of table name
SELECT master_create_distributed_table('table_to_distribute_pkey', 'id');
Expand All @@ -26,6 +27,11 @@ ERROR: could not find column: bad_column
-- use unsupported partition type
SELECT master_create_distributed_table('table_to_distribute', 'name', 'r');
ERROR: unsupported partition method: r
-- use unsupported partition column
SELECT master_create_distributed_table('table_to_distribute', 'data');
ERROR: cannot distribute relation: "table_to_distribute"
DETAIL: Column "data" is not eligible to be the distribution column.
HINT: Try another column as the distribution column.
-- distribute table and inspect side effects
SELECT master_create_distributed_table('table_to_distribute', 'name');
master_create_distributed_table
Expand Down
3 changes: 1 addition & 2 deletions prune_shard_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ static List *OperatorIdCache = NIL;

/* local function forward declarations */
static Oid LookupOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber);
static Oid GetOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber);
static bool SimpleOpExpression(Expr *clause);
static Node * HashableClauseMutator(Node *originalNode, Var *partitionColumn);
static bool OpExpressionContainsColumn(OpExpr *operatorExpression, Var *partitionColumn);
Expand Down Expand Up @@ -305,7 +304,7 @@ LookupOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber)
* GetOperatorByType returns the operator oid for the given type, access
* method, and strategy number.
*/
static Oid
Oid
GetOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber)
{
/* Get default operator class from pg_opclass */
Expand Down
1 change: 1 addition & 0 deletions prune_shard_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ typedef struct OperatorIdCacheEntry
extern List * PruneShardList(Oid relationId, List *whereClauseList,
List *shardIntervalList);
extern OpExpr * MakeOpExpression(Var *variable, int16 strategyNumber);
extern Oid GetOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber);


#endif /* PG_SHARD_PRUNE_SHARD_LIST_H */
6 changes: 5 additions & 1 deletion sql/create_shards.sql.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ CREATE FUNCTION create_table_then_fail(cstring, integer)

CREATE TABLE table_to_distribute (
name text,
id bigint PRIMARY KEY
id bigint PRIMARY KEY,
data json
);

-- use an index instead of table name
Expand All @@ -30,6 +31,9 @@ SELECT master_create_distributed_table('table_to_distribute', 'bad_column');
-- use unsupported partition type
SELECT master_create_distributed_table('table_to_distribute', 'name', 'r');

-- use unsupported partition column
SELECT master_create_distributed_table('table_to_distribute', 'data');

-- distribute table and inspect side effects
SELECT master_create_distributed_table('table_to_distribute', 'name');
SELECT partition_method, key FROM pgs_distribution_metadata.partition
Expand Down

0 comments on commit 47b1577

Please sign in to comment.