解决索引重复列名问题

This commit is contained in:
l00584793
2020-09-02 14:57:23 +08:00
committed by luozihao
parent 3c83bfa102
commit b36017fdb8
4 changed files with 114 additions and 2 deletions

View File

@ -222,6 +222,7 @@ static List* DividePartitionStartEndInterval(ParseState* pstate, Form_pg_attribu
Const* startVal, Const* endVal, Const* everyVal, Node* everyExpr, int* numPart, int maxNum);
static void TryReuseFilenode(Relation rel, CreateStmtContext *ctx, bool clonepart);
extern Node* makeAConst(Value* v, int location);
static bool IsElementExisted(List* indexElements, IndexElem* ielem);
/*
* transformCreateStmt -
@ -3120,7 +3121,7 @@ IndexStmt* transformIndexStmt(Oid relid, IndexStmt* stmt, const char* queryStrin
/* we have to fix its collations too */
assign_expr_collations(pstate, stmt->whereClause);
}
List* indexElements = NIL;
/* take care of any index expressions */
foreach (l, stmt->indexParams) {
IndexElem* ielem = (IndexElem*)lfirst(l);
@ -3144,8 +3145,15 @@ IndexStmt* transformIndexStmt(Oid relid, IndexStmt* stmt, const char* queryStrin
if (expression_returns_set(ielem->expr))
ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("index expression cannot return a set")));
}
if (IsElementExisted(indexElements, ielem)) {
ereport(ERROR, (errcode(ERRCODE_DUPLICATE_COLUMN), errmsg("duplicate column name")));
}
indexElements = lappend(indexElements, (IndexElem*)ielem);
}
list_free(indexElements);
/*
* Check that only the base rel is mentioned.
*/
@ -3177,6 +3185,18 @@ IndexStmt* transformIndexStmt(Oid relid, IndexStmt* stmt, const char* queryStrin
return stmt;
}
static bool IsElementExisted(List* indexElements, IndexElem* ielem)
{
ListCell* lc = NULL;
foreach (lc, indexElements) {
IndexElem* theElement = (IndexElem*)lfirst(lc);
if (equal(theElement, ielem)) {
return true;
}
}
return false;
}
/*
* transformRuleStmt -
* transform a CREATE RULE Statement. The action is a list of parse

View File

@ -0,0 +1,46 @@
--test duplicate keys can be created successfully
create table test_index(id int);
create index index_01 on test_index(id,id,id);
ERROR: duplicate column name
create index index_01 on test_index(id);
drop index if exists index_01;
create index index_01 on test_index using gin(to_tsvector('english', id),
to_tsvector('english', id));
ERROR: duplicate column name
create index index_01 on test_index using gin(to_tsvector('english', id));
drop index if exists index_01;
drop table if exists test_index;
create table test_index(id int) with (orientation=column);
create index index_01 on test_index using btree(id,id,id);
ERROR: duplicate column name
create index index_01 on test_index using btree(id);
drop index if exists index_01;
create index index_01 on test_index using psort(id,id,id);
ERROR: duplicate column name
create index index_01 on test_index using psort(id);
drop index if exists index_01;
create index index_01 on test_index using gin(to_tsvector('english', id),
to_tsvector('english', id),to_tsvector('english', id));
ERROR: duplicate column name
create index index_01 on test_index using gin(to_tsvector('english', id));
--test whether duplicate expression can be created successfully
drop table if exists test_index;
create table test_index(id int, id_1 int);
drop index if exists index_01;
NOTICE: index "index_01" does not exist, skipping
create index index_01 on test_index using btree(sin(id), sin(id));
ERROR: duplicate column name
create index index_01 on test_index using btree(sin(id), sin(id_1));
drop index if exists index_01;
create index index_01 on test_index using btree(sin(id), cos(id));
drop index if exists index_01;
create index index_01 on test_index using gin(to_tsvector('english', sin(id)),
to_tsvector('english', sin(id)));
ERROR: duplicate column name
drop index if exists index_01;
NOTICE: index "index_01" does not exist, skipping
create index index_01 on test_index using gin(to_tsvector('english', sin(id)),
to_tsvector('english', sin(id_1)));
drop index if exists index_01;
create index index_01 on test_index using gin(to_tsvector('english', sin(id)),
to_tsvector('english', cos(id)));

View File

@ -75,4 +75,5 @@ test: with
# run alter object to test pg_object
#test: pg_object_test
test: partition_foreign_key
test: partition_trigger
test: partition_trigger
test: duplicate_column_index

View File

@ -0,0 +1,45 @@
--test duplicate keys can be created successfully
create table test_index(id int);
create index index_01 on test_index(id,id,id);
create index index_01 on test_index(id);
drop index if exists index_01;
create index index_01 on test_index using gin(to_tsvector('english', id),
to_tsvector('english', id));
create index index_01 on test_index using gin(to_tsvector('english', id));
drop index if exists index_01;
drop table if exists test_index;
create table test_index(id int) with (orientation=column);
create index index_01 on test_index using btree(id,id,id);
create index index_01 on test_index using btree(id);
drop index if exists index_01;
create index index_01 on test_index using psort(id,id,id);
create index index_01 on test_index using psort(id);
drop index if exists index_01;
create index index_01 on test_index using gin(to_tsvector('english', id),
to_tsvector('english', id),to_tsvector('english', id));
create index index_01 on test_index using gin(to_tsvector('english', id));
--test whether duplicate expression can be created successfully
drop table if exists test_index;
create table test_index(id int, id_1 int);
drop index if exists index_01;
create index index_01 on test_index using btree(sin(id), sin(id));
create index index_01 on test_index using btree(sin(id), sin(id_1));
drop index if exists index_01;
create index index_01 on test_index using btree(sin(id), cos(id));
drop index if exists index_01;
create index index_01 on test_index using gin(to_tsvector('english', sin(id)),
to_tsvector('english', sin(id)));
drop index if exists index_01;
create index index_01 on test_index using gin(to_tsvector('english', sin(id)),
to_tsvector('english', sin(id_1)));
drop index if exists index_01;
create index index_01 on test_index using gin(to_tsvector('english', sin(id)),
to_tsvector('english', cos(id)));