Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/backend/cdb/cdbpartition.c
Original file line number Diff line number Diff line change
Expand Up @@ -5118,7 +5118,7 @@ atpxPart_validate_spec(
RelationGetNamespace(rel)),
pstrdup(RelationGetRelationName(rel)), -1)),
false, true /* isPartitioned */,
&inheritOids, &old_constraints, &parentOidCount, NULL);
&inheritOids, &old_constraints, &parentOidCount);

pcxt->columns = schema;

Expand Down
15 changes: 4 additions & 11 deletions src/backend/commands/tablecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,16 +512,14 @@ DefineRelation(CreateStmt *stmt, char relkind, char relstorage, bool dispatch)

/*
* Look up inheritance ancestors and generate relation schema, including
* inherited attributes. Update the offsets of the distribution attributes
* in GpPolicy if necessary. Save result to stmt->tableElts and dispatch to
* QEs.
* inherited attributes. Save result to stmt->tableElts and dispatch to QEs.
*/
isPartitioned = stmt->partitionBy ? true : false;
if (Gp_role == GP_ROLE_DISPATCH || Gp_role == GP_ROLE_UTILITY)
stmt->tableElts = MergeAttributes(stmt->tableElts, stmt->inhRelations,
stmt->relation->istemp, isPartitioned,
&stmt->inhOids, &old_constraints,
&stmt->parentOidCount, stmt->policy);
&stmt->parentOidCount);

/*
* Create a relation descriptor from the relation schema and create the
Expand Down Expand Up @@ -1371,15 +1369,13 @@ truncate_check_rel(Relation rel)
* of ColumnDef's.) It is destructively changed.
* 'supers' is a list of names (as RangeVar nodes) of parent relations.
* 'istemp' is TRUE if we are creating a temp relation.
* 'GpPolicy *' is NULL if the distribution policy is not to be updated
* 'isPartitioned' is TRUE if we are creating a partitioned relation.
*
* Output arguments:
* 'supOids' receives a list of the OIDs of the parent relations.
* 'supconstr' receives a list of constraints belonging to the parents,
* updated as necessary to be valid for the child.
* 'supOidCount' is set to the number of parents that have OID columns.
* 'GpPolicy' is updated with the offsets of the distribution
* attributes in the new schema
*
* Return value:
* Completed schema list.
Expand Down Expand Up @@ -1425,7 +1421,7 @@ truncate_check_rel(Relation rel)
*/
List *
MergeAttributes(List *schema, List *supers, bool istemp, bool isPartitioned,
List **supOids, List **supconstr, int *supOidCount, GpPolicy *policy)
List **supOids, List **supconstr, int *supOidCount)
{
ListCell *entry;
List *inhSchema = NIL;
Expand Down Expand Up @@ -1598,9 +1594,6 @@ MergeAttributes(List *schema, List *supers, bool istemp, bool isPartitioned,
def->is_not_null |= attribute->attnotnull;
/* Default and other constraints are handled below */
newattno[parent_attno - 1] = exist_attno;

Assert(policy->nattrs >= 0 && "the number of distribution attributes is not negative");

}
else
{
Expand Down
167 changes: 69 additions & 98 deletions src/backend/parser/parse_utilcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ typedef struct
List *grants; /* GRANT items */
} CreateSchemaStmtContext;

/* Items for an ordered inherited table column list */
typedef struct InheritColumn
{
char *columnName;
Oid typeOid;
int position;
} InheritColumn;


static void transformColumnDefinition(ParseState *pstate,
CreateStmtContext *cxt,
ColumnDef *column);
Expand All @@ -107,6 +116,8 @@ static void setSchemaName(char *context_schema, char **stmt_schema_name);

static List *getLikeDistributionPolicy(InhRelation *e);
static bool co_explicitly_disabled(List *opts);
static InheritColumn *getColumnByName(List *list, char *columnName);
static List *mergeColumnByName(List *list, char *columnName, Oid typeOid);
static void transformDistributedBy(ParseState *pstate, CreateStmtContext *cxt,
List *distributedBy, GpPolicy **policyp,
List *likeDistributedBy,
Expand Down Expand Up @@ -1342,6 +1353,38 @@ transformCreateExternalStmt(CreateExternalStmt *stmt, const char *queryString)
}


static InheritColumn *
getColumnByName(List *list, char *columnName)
{
ListCell *inhColumn;

foreach(inhColumn, list)
{
InheritColumn *col = (InheritColumn *) lfirst(inhColumn);

if (strcmp(col->columnName, columnName) == 0)
return col;
}

return NULL;
}


static List *
mergeColumnByName(List *list, char *columnName, Oid typeOid)
{
if (getColumnByName(list, columnName) != NULL)
return list;

InheritColumn *col = (InheritColumn *)palloc(sizeof(InheritColumn));
col->columnName = columnName;
col->typeOid = typeOid;
col->position = list == NIL ? 1 : list->length + 1;

return lappend(list, col);
}


/****************stmt->policy*********************/
static void
transformDistributedBy(ParseState *pstate, CreateStmtContext *cxt,
Expand Down Expand Up @@ -1694,12 +1737,6 @@ transformDistributedBy(ParseState *pstate, CreateStmtContext *cxt,
if (!(distributedBy->length == 1 && linitial(distributedBy) == NULL))
{

typedef struct
{
char *columnName;
Oid typeOid;
} InheritColumn;

List *orderedColumns = NIL;


Expand Down Expand Up @@ -1727,73 +1764,30 @@ transformDistributedBy(ParseState *pstate, CreateStmtContext *cxt,
for (count = 0; count < rel->rd_att->natts; count++)
{
Form_pg_attribute inhattr = rel->rd_att->attrs[count];
char *inhname = NameStr(inhattr->attname);
bool found = false;

if (inhattr->attisdropped)
continue;

ListCell *item;

foreach(item, orderedColumns)
{
InheritColumn *col = (InheritColumn *) lfirst(item);

if (strcmp(col->columnName, inhname) == 0)
{
found = true;
break;
}
}
if (!found)
{
InheritColumn *col = (InheritColumn *) palloc(sizeof(InheritColumn));

col->columnName = inhname;
col->typeOid = inhattr->atttypid;
orderedColumns = lappend(orderedColumns, col);
elog(DEBUG1, "Add inherited column \"%s\" of type %d to a column list",
inhname, inhattr->atttypid);
}
orderedColumns = mergeColumnByName(orderedColumns,
NameStr(inhattr->attname),
inhattr->atttypid);
}
heap_close(rel, NoLock);
}
}

/*
* Add non-present columns from inheriting (child) table to a
* column list with unique columns from interited (parent) tables.
* column list with unique columns from inherited (parent) tables.
*/
ListCell *columns;

foreach(columns, cxt->columns)
{
ColumnDef *column = (ColumnDef *) lfirst(columns);
ListCell *inhColumn;
bool found = false;
Oid typeOid = typenameTypeId(NULL, column->typeName, NULL);

foreach(inhColumn, orderedColumns)
{
InheritColumn *col = (InheritColumn *) lfirst(inhColumn);

if (strcmp(col->columnName, column->colname) == 0)
{
found = true;
break;
}
}
if (!found)
{
int32 typmod;
Oid typeOid = typenameTypeId(NULL, column->typeName, &typmod);
InheritColumn *col = (InheritColumn *) palloc(sizeof(InheritColumn));

col->columnName = column->colname;
col->typeOid = typeOid;
orderedColumns = lappend(orderedColumns, col);
elog(DEBUG1, "Add column \"%s\" of type %d to a column list",
column->colname, typeOid);
}
orderedColumns = mergeColumnByName(orderedColumns, column->colname, typeOid);
}

/*
Expand All @@ -1802,62 +1796,39 @@ transformDistributedBy(ParseState *pstate, CreateStmtContext *cxt,
foreach(keys, distributedBy)
{
char *key = strVal(lfirst(keys));
bool found = false;
ListCell *item;
InheritColumn *col = getColumnByName(orderedColumns, key);

colindex = 0;
foreach(item, orderedColumns)
{
InheritColumn *col = (InheritColumn *) lfirst(item);

colindex++;
elog(DEBUG1, "Iterating column list: column \"%s\", number %d",
col->columnName, colindex);
if (strcmp(key, col->columnName) == 0)
{
/*
* To be a part of a distribution key, this type must
* be supported for hashing internally in Greenplum
* Database. We check if the base type is supported
* for hashing or if it is an array type (we support
* hashing on all array types).
*/
if (!isGreenplumDbHashable(col->typeOid))
{
ereport(ERROR,
(errcode(ERRCODE_GP_FEATURE_NOT_SUPPORTED),
errmsg("type \"%s\" can't be a part of a "
"distribution key",
format_type_be(col->typeOid))));
}
elog(DEBUG1, "Distribution key \"%s\" matched a column \"%s\" with number %d in a column list",
key, col->columnName, colindex);
found = true;

break;
}
}

/*
* In the ALTER TABLE case, don't complain about index keys
* not created in the command; they may well exist already.
* DefineIndex will complain about them if not, and will also
* take care of marking them NOT NULL.
*/
if (!found && !cxt->isalter)
if (col == NULL)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("column \"%s\" named in 'DISTRIBUTED BY' clause does not exist",
key)));

policy->attrs[policy->nattrs++] = colindex;
/*
* To be a part of a distribution key, this type must
* be supported for hashing internally in Greenplum
* Database. We check if the base type is supported
* for hashing or if it is an array type (we support
* hashing on all array types).
*/
if (!isGreenplumDbHashable(col->typeOid))
{
ereport(ERROR,
(errcode(ERRCODE_GP_FEATURE_NOT_SUPPORTED),
errmsg("type \"%s\" can't be a part of a "
"distribution key",
format_type_be(col->typeOid))));
}

policy->attrs[policy->nattrs++] = col->position;
}
}
}


*policyp = policy;


if (cxt && cxt->pkey) /* Primary key specified. Make sure
* distribution columns match */
{
Expand Down
2 changes: 1 addition & 1 deletion src/include/commands/tablecmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ extern Oid get_settable_tablespace_oid(char *tablespacename);

extern List *
MergeAttributes(List *schema, List *supers, bool istemp, bool isPartitioned,
List **supOids, List **supconstr, int *supOidCount, GpPolicy *policy);
List **supOids, List **supconstr, int *supOidCount);
extern List *make_dist_clause(Relation rel);

extern Oid transformFkeyCheckAttrs(Relation pkrel,
Expand Down