Skip to content

Commit

Permalink
sql: fix index column direction lookup bugs
Browse files Browse the repository at this point in the history
This patch fixes a couple of instances in which we look up an index's
column directions using a wrong ordinal when the index has implicit
columns due to it being sharded or partitioned.

Fixes #58945.

Release note (bug fix): The indoption column in pg_catalog.index is now
populated correctly.
  • Loading branch information
Marius Posta committed Jan 14, 2021
1 parent b22e903 commit 91220b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
19 changes: 19 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -3031,3 +3031,22 @@ SELECT tablename FROM pg_catalog.pg_tables

statement ok
SET DATABASE = test;

subtest 58945

statement ok
SET experimental_enable_hash_sharded_indexes = true

statement ok
CREATE TABLE t_hash (
a INT,
INDEX t_hash_a_idx (a DESC) USING HASH WITH BUCKET_COUNT=8
);

query T colnames
SELECT indoption
FROM pg_catalog.pg_index
WHERE indexrelid IN (SELECT crdb_oid FROM pg_catalog.pg_indexes WHERE indexname = 't_hash_a_idx')
----
indoption
1
20 changes: 9 additions & 11 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -1453,22 +1453,17 @@ https://www.postgresql.org/docs/9.5/catalog-pg-index.html`,
table.GetIndexMutationCapabilities(index.GetID())
isReady := isMutation && isWriteOnly

colIDs := make([]descpb.ColumnID, 0, index.NumColumns())
for i := index.IndexDesc().ExplicitColumnStartIdx(); i < index.NumColumns(); i++ {
colIDs = append(colIDs, index.GetColumnID(i))
}
indkey, err := colIDArrayToVector(colIDs)
if err != nil {
return err
}
// Get the collations for all of the columns. To do this we require
// the type of the column.
// Also fill in indoption for each column to indicate if the index
// is ASC/DESC and if nulls appear first/last.
collationOids := tree.NewDArray(types.Oid)
indoption := tree.NewDArray(types.Int)

for i, columnID := range colIDs {
colIDs := make([]descpb.ColumnID, 0, index.NumColumns())
for i := index.IndexDesc().ExplicitColumnStartIdx(); i < index.NumColumns(); i++ {
columnID := index.GetColumnID(i)
colIDs = append(colIDs, columnID)
col, err := table.FindColumnByID(columnID)
if err != nil {
return err
Expand All @@ -1488,6 +1483,10 @@ https://www.postgresql.org/docs/9.5/catalog-pg-index.html`,
return err
}
}
indkey, err := colIDArrayToVector(colIDs)
if err != nil {
return err
}
collationOidVector := tree.NewDOidVectorFromDArray(collationOids)
indoptionIntVector := tree.NewDIntVectorFromDArray(indoption)
// TODO(bram): #27763 indclass still needs to be populated but it
Expand Down Expand Up @@ -1562,7 +1561,6 @@ func indexDefFromDescriptor(
tableLookup tableLookupFn,
) (string, error) {
colNames := index.ColumnNames[index.ExplicitColumnStartIdx():]

indexDef := tree.CreateIndex{
Name: tree.Name(index.Name),
Table: tree.MakeTableName(tree.Name(db.GetName()), tree.Name(table.GetName())),
Expand All @@ -1576,7 +1574,7 @@ func indexDefFromDescriptor(
Column: tree.Name(name),
Direction: tree.Ascending,
}
if index.ColumnDirections[i] == descpb.IndexDescriptor_DESC {
if index.ColumnDirections[index.ExplicitColumnStartIdx()+i] == descpb.IndexDescriptor_DESC {
elem.Direction = tree.Descending
}
indexDef.Columns[i] = elem
Expand Down

0 comments on commit 91220b7

Please sign in to comment.