Skip to content

Commit

Permalink
[#12186] YSQL: Disable hash partitioned index syntax for colocated in…
Browse files Browse the repository at this point in the history
…dexes and tablegroup indexes

Summary:
This diff explicitly disables the syntax to create a hash partitioned
colocated index/tablegroup index.

Consider two examples below:
```
CREATE TABLEGROUP tbl_group;
CREATE TABLE tbl(h1 int, h2 int, r1 int, r2 int) TABLEGROUP tbl_group;
CREATE INDEX idx ON tbl(h1 hash);
CREATE INDEX idx2 on tbl((h1, h2) hash);
\d tbl
                Table "public.tbl"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 h1     | integer |           |          |
 h2     | integer |           |          |
 r1     | integer |           |          |
 r2     | integer |           |          |
Indexes:
    "idx" lsm (h1 HASH), tablegroup "tbl_group"
    "idx2" lsm ((h1, h2) HASH), tablegroup "tbl_group"
Tablegroup: "tbl_group"
```
```
CREATE DATABASE colocation_test colocated = true;
\c colocation_test
CREATE TABLE tbl (h1 int, h2 int, r1 int, r2 int);
CREATE INDEX idx on tbl (h1 hash);
CREATE INDEX idx2 on tbl ((h1, h2) hash);
\d tbl
                Table "public.tbl"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 h1     | integer |           |          |
 h2     | integer |           |          |
 r1     | integer |           |          |
 r2     | integer |           |          |
Indexes:
    "idx" lsm (h1 HASH)
    "idx2" lsm ((h1, h2) HASH)

\c yugabyte
DROP DATABASE colocation_test;

```
The PG table descriptions of all colocated/tablegroup indexes created
in these two examples are hash-partitioned.
However, in reality, all colocated/tablegroup indexes created
in these two examples are range-partitioned.
The reason is we allow specifying hash columns for colocated/tablegroup indexes,
and on CatalogManager side, we change all hash columns to range columns in
`CreateTableRequestPB` in `CreateTable()`.
On Postgre's side, `index_create()` inserts an index tuple
with the specified HASH index option into `pg_index`.

Test Plan:
ybd --java-test org.yb.pgsql.TestPgRegressTablegroup
ybd --java-test org.yb.pgsql.TestPgRegressColocation

Reviewers: mihnea, tverona, alex

Reviewed By: alex

Subscribers: bogdan, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D16702
  • Loading branch information
yifanguan committed Jul 18, 2022
1 parent 171e5f6 commit 2ad388f
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public void tablesInColocatedDb() throws Exception {
new Row("nopk_nc", false, null, null)));

runInvalidQuery(stmt, "ALTER TABLE nopk_c ADD PRIMARY KEY (id HASH)",
"cannot colocate hash partitioned table");
"cannot colocate hash partitioned index");

// This doesn't really accomplish much though, since colocated property is invisible to SQL
// - we can't check whether a re-created table keeps/gains/loses it.
Expand Down
9 changes: 9 additions & 0 deletions src/postgres/src/backend/commands/indexcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,15 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("hash column not allowed after an ASC/DESC column")));
else if (tablegroupId != InvalidOid)
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("cannot create a hash partitioned"
" index in a TABLEGROUP")));
else if (is_colocated)
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("cannot colocate hash partitioned index")));
break;
default:
ereport(ERROR,
Expand Down
43 changes: 43 additions & 0 deletions src/postgres/src/test/regress/expected/yb_feature_colocation.out
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,49 @@ CREATE TABLE tab_range_nonkey_noco3 (a INT, b INT, PRIMARY KEY (a ASC)) WITH (co
CREATE INDEX idx_range4 ON tab_range_nonkey_noco3 (a);
CREATE TABLE tab_range_nonkey5 (a INT, b INT, PRIMARY KEY (a ASC));
CREATE INDEX idx_range5 ON tab_range_nonkey5 (a);
CREATE TABLE tbl (r1 INT, r2 INT, v1 INT, v2 INT,
PRIMARY KEY (r1, r2));
CREATE INDEX idx_hash1 on tbl (r1 HASH);
ERROR: cannot colocate hash partitioned index
CREATE INDEX idx_hash2 on tbl ((r1, r2) HASH);
ERROR: cannot colocate hash partitioned index
CREATE INDEX idx_hash3 on tbl (r1 HASH, r2 ASC);
ERROR: cannot colocate hash partitioned index
CREATE UNIQUE INDEX unique_idx_hash1 on tbl (r1 HASH);
ERROR: cannot colocate hash partitioned index
CREATE UNIQUE INDEX unique_idx_hash2 on tbl ((r1, r2) HASH);
ERROR: cannot colocate hash partitioned index
CREATE UNIQUE INDEX unique_idx_hash3 on tbl (r1 HASH, r2 ASC);
ERROR: cannot colocate hash partitioned index
\d tbl
Table "public.tbl"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
r1 | integer | | not null |
r2 | integer | | not null |
v1 | integer | | |
v2 | integer | | |
Indexes:
"tbl_pkey" PRIMARY KEY, lsm (r1 ASC, r2 ASC)

-- Make sure nothing bad happens to UNIQUE constraints after disabling HASH columns
-- for colocated indexes
CREATE TABLE tbl2 (r1 int PRIMARY KEY, r2 int, v1 int, v2 int, UNIQUE(v1));
ALTER TABLE tbl2 ADD CONSTRAINT unique_v2_tbl2 UNIQUE(v2);
\d tbl2
Table "public.tbl2"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
r1 | integer | | not null |
r2 | integer | | |
v1 | integer | | |
v2 | integer | | |
Indexes:
"tbl2_pkey" PRIMARY KEY, lsm (r1 ASC)
"tbl2_v1_key" UNIQUE CONSTRAINT, lsm (v1 ASC)
"unique_v2_tbl2" UNIQUE CONSTRAINT, lsm (v2 ASC)

DROP TABLE tbl, tbl2;
-- colocated table with unique index
CREATE TABLE tab_nonkey2 (a INT) WITH (colocated = true);
CREATE UNIQUE INDEX idx_range6 ON tab_nonkey2 (a);
Expand Down
51 changes: 51 additions & 0 deletions src/postgres/src/test/regress/expected/yb_tablegroup.out
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,57 @@ ERROR: Cannot use TABLEGROUP with SPLIT.
CREATE INDEX ON tgroup_test1(col1) SPLIT AT VALUES((10), (20), (30));
ERROR: Cannot use TABLEGROUP with SPLIT.
--
-- Test CREATE INDEX with hash columns
--
CREATE TABLEGROUP tgroup_hash_index_test;
CREATE TABLE tbl (r1 int, r2 int, v1 int, v2 int,
PRIMARY KEY (r1, r2)) TABLEGROUP tgroup_hash_index_test;
CREATE INDEX idx_tbl ON tbl (r1 hash);
ERROR: cannot create a hash partitioned index in a TABLEGROUP
CREATE INDEX idx2_tbl ON tbl ((r1, r2) hash);
ERROR: cannot create a hash partitioned index in a TABLEGROUP
CREATE INDEX idx3_tbl ON tbl (r1 hash, r2 asc);
ERROR: cannot create a hash partitioned index in a TABLEGROUP
CREATE UNIQUE INDEX unique_idx_tbl ON tbl (r1 hash);
ERROR: cannot create a hash partitioned index in a TABLEGROUP
CREATE UNIQUE INDEX unique_idx2_tbl ON tbl ((r1, r2) hash);
ERROR: cannot create a hash partitioned index in a TABLEGROUP
CREATE UNIQUE INDEX unique_idx3_tbl ON tbl (r1 hash, r2 asc);
ERROR: cannot create a hash partitioned index in a TABLEGROUP
\d tbl
Table "public.tbl"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
r1 | integer | | not null |
r2 | integer | | not null |
v1 | integer | | |
v2 | integer | | |
Indexes:
"tbl_pkey" PRIMARY KEY, lsm (r1 ASC, r2 ASC)
Tablegroup: "tgroup_hash_index_test"

-- Make sure nothing bad happens to UNIQUE constraints after disabling HASH columns
-- for tablegroup indexes
CREATE TABLE tbl2 (r1 int PRIMARY KEY, r2 int, v1 int, v2 int, UNIQUE(v1))
TABLEGROUP tgroup_hash_index_test;
ALTER TABLE tbl2 ADD CONSTRAINT unique_v2_tbl2 UNIQUE(v2);
\d tbl2
Table "public.tbl2"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
r1 | integer | | not null |
r2 | integer | | |
v1 | integer | | |
v2 | integer | | |
Indexes:
"tbl2_pkey" PRIMARY KEY, lsm (r1 ASC)
"tbl2_v1_key" UNIQUE CONSTRAINT, lsm (v1 ASC), tablegroup "tgroup_hash_index_test"
"unique_v2_tbl2" UNIQUE CONSTRAINT, lsm (v2 ASC), tablegroup "tgroup_hash_index_test"
Tablegroup: "tgroup_hash_index_test"

DROP TABLE tbl, tbl2;
DROP TABLEGROUP tgroup_hash_index_test;
--
-- Test describes
--
CREATE TABLE tgroup_test4 (col1 int, col2 int) TABLEGROUP tgroup2;
Expand Down
16 changes: 16 additions & 0 deletions src/postgres/src/test/regress/sql/yb_feature_colocation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ CREATE TABLE tab_range_nonkey_noco3 (a INT, b INT, PRIMARY KEY (a ASC)) WITH (co
CREATE INDEX idx_range4 ON tab_range_nonkey_noco3 (a);
CREATE TABLE tab_range_nonkey5 (a INT, b INT, PRIMARY KEY (a ASC));
CREATE INDEX idx_range5 ON tab_range_nonkey5 (a);
CREATE TABLE tbl (r1 INT, r2 INT, v1 INT, v2 INT,
PRIMARY KEY (r1, r2));
CREATE INDEX idx_hash1 on tbl (r1 HASH);
CREATE INDEX idx_hash2 on tbl ((r1, r2) HASH);
CREATE INDEX idx_hash3 on tbl (r1 HASH, r2 ASC);
CREATE UNIQUE INDEX unique_idx_hash1 on tbl (r1 HASH);
CREATE UNIQUE INDEX unique_idx_hash2 on tbl ((r1, r2) HASH);
CREATE UNIQUE INDEX unique_idx_hash3 on tbl (r1 HASH, r2 ASC);
\d tbl
-- Make sure nothing bad happens to UNIQUE constraints after disabling HASH columns
-- for colocated indexes
CREATE TABLE tbl2 (r1 int PRIMARY KEY, r2 int, v1 int, v2 int, UNIQUE(v1));
ALTER TABLE tbl2 ADD CONSTRAINT unique_v2_tbl2 UNIQUE(v2);
\d tbl2

DROP TABLE tbl, tbl2;

-- colocated table with unique index
CREATE TABLE tab_nonkey2 (a INT) WITH (colocated = true);
Expand Down
24 changes: 24 additions & 0 deletions src/postgres/src/test/regress/sql/yb_tablegroup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,30 @@ CREATE TABLE tgroup_split (col1 int PRIMARY KEY) SPLIT INTO 3 TABLETS TABLEGROUP
CREATE TABLE tgroup_split (col1 int, col2 text) SPLIT INTO 3 TABLETS TABLEGROUP tgroup1;
CREATE INDEX ON tgroup_test1(col1) SPLIT AT VALUES((10), (20), (30));

--
-- Test CREATE INDEX with hash columns
--
CREATE TABLEGROUP tgroup_hash_index_test;
CREATE TABLE tbl (r1 int, r2 int, v1 int, v2 int,
PRIMARY KEY (r1, r2)) TABLEGROUP tgroup_hash_index_test;
CREATE INDEX idx_tbl ON tbl (r1 hash);
CREATE INDEX idx2_tbl ON tbl ((r1, r2) hash);
CREATE INDEX idx3_tbl ON tbl (r1 hash, r2 asc);
CREATE UNIQUE INDEX unique_idx_tbl ON tbl (r1 hash);
CREATE UNIQUE INDEX unique_idx2_tbl ON tbl ((r1, r2) hash);
CREATE UNIQUE INDEX unique_idx3_tbl ON tbl (r1 hash, r2 asc);
\d tbl

-- Make sure nothing bad happens to UNIQUE constraints after disabling HASH columns
-- for tablegroup indexes
CREATE TABLE tbl2 (r1 int PRIMARY KEY, r2 int, v1 int, v2 int, UNIQUE(v1))
TABLEGROUP tgroup_hash_index_test;
ALTER TABLE tbl2 ADD CONSTRAINT unique_v2_tbl2 UNIQUE(v2);
\d tbl2

DROP TABLE tbl, tbl2;
DROP TABLEGROUP tgroup_hash_index_test;

--
-- Test describes
--
Expand Down

0 comments on commit 2ad388f

Please sign in to comment.