Summary:
Commit dad03e3cc65c059a2576e5ae2b927aab86f8961b attempted to resolve metadata inconsistencies following an upgrade for indexes involving renamed columns. However, the fix was incomplete and did not handle indexes with hash column groups correctly.
This revision addresses two main issues:
- Incomplete grammar support for aliases in hash column groups:
The previous implementation added alias support (`yb_opt_alias`) only for `index_elem_options`, which applies to individual
`index_elem`s. However, hash column groups (`yb_index_expr_list_hash_elems`) are defined as a list of `a_expr`, which currently
don’t allow aliases.
This is resolved by introducing new grammar rules:
- `yb_hash_index_expr_list` – a wrapper around a list of `yb_hash_index_expr_with_alias`
- `yb_hash_index_expr_with_alias` – wraps an `a_expr` with an optional alias (using `AS ColId`)
As before, the shift/reduce conflict for (col) HASH is resolved by giving higher precedence to col as an expression list (instead of a
single column expression). Since this precedence has to be made explicit, we cannot reuse yb_opt_alias (as it can also be empty,
which causes ambiguity).
Now, aliases are supported for hash column groups: `CREATE INDEX idx ON tbl ((a AS a_old, b AS b_old) HASH)`
- Incorrect alias placement in pg_get_indexdef_worker:
The function incorrectly placed the closing parenthesis (and the `HASH` keyword) before emitting the alias of the last column in a
hash group.
Example: `CREATE INDEX NONCONCURRENTLY idx ON tbl USING "lsm" (("a" AS "a_old", "b") HASH AS "b_old", "c" DESC AS "c_old")`
This has been corrected so the alias is emitted before closing the hash column group.
Other changes:
- Only emit column alias in `pg_get_indexdef_worker` when the column is renamed (the index attribute name differs from the corresponding relation attribute name).
Jira: DB-17218
Test Plan:
Tested manually using the following patch and a custom 2024.2.4 build (that includes this fix) for the upgrade-from version:
```
diff --git a/src/yb/integration-tests/upgrade-tests/ysql_major_upgrade-test.cc b/src/yb/integration-tests/upgrade-tests/ysql_major_upgrade-test.cc
index 097c37c16f..e29b4e5c14 100644
--- a/src/yb/integration-tests/upgrade-tests/ysql_major_upgrade-test.cc
+++ b/src/yb/integration-tests/upgrade-tests/ysql_major_upgrade-test.cc
@@ -1750,6 +1750,7 @@ TEST_F(YsqlMajorUpgradeTest, IndexWithRenamedColumn) {
"ALTER TABLE t RENAME COLUMN c TO c_new",
"CREATE INDEX i3 ON t (a_new, c_new)",
"CREATE INDEX i4 ON t ((a_new+c_new), c_new)",
+ "CREATE INDEX i5 ON t (((a_new+c_new), a_new) HASH, c_new)",
"ALTER TABLE t RENAME COLUMN a_new TO a_new2",
"ALTER TABLE t RENAME COLUMN c_new TO c_new2"}));
@@ -1763,6 +1764,7 @@ TEST_F(YsqlMajorUpgradeTest, IndexWithRenamedColumn) {
"ALTER TABLE t_part RENAME COLUMN c TO c_new",
"CREATE INDEX i3_part ON t_part (a_new, c_new)",
"CREATE INDEX i4_part ON t_part ((a_new+c_new), c_new)",
+ "CREATE INDEX i5_part ON t_part (((a_new+c_new), a_new) HASH, c_new)",
"ALTER TABLE t_part RENAME COLUMN a_new TO a_new2",
"ALTER TABLE t_part RENAME COLUMN c_new TO c_new2"}));
```
Reviewers: telgersma, jason, steve.varnau, #db-approvers
Reviewed By: telgersma, #db-approvers
Subscribers: svc_phabricator, yql
Differential Revision: https://phorge.dev.yugabyte.com/D44807