From 91220b741027a8bc166f12e4df3254091b7a6b88 Mon Sep 17 00:00:00 2001 From: Marius Posta Date: Thu, 14 Jan 2021 09:02:11 -0500 Subject: [PATCH] sql: fix index column direction lookup bugs 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. --- .../logictest/testdata/logic_test/pg_catalog | 19 ++++++++++++++++++ pkg/sql/pg_catalog.go | 20 +++++++++---------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/pkg/sql/logictest/testdata/logic_test/pg_catalog b/pkg/sql/logictest/testdata/logic_test/pg_catalog index 68c482602885..d30c97c56e66 100644 --- a/pkg/sql/logictest/testdata/logic_test/pg_catalog +++ b/pkg/sql/logictest/testdata/logic_test/pg_catalog @@ -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 diff --git a/pkg/sql/pg_catalog.go b/pkg/sql/pg_catalog.go index da73cf43a2d0..89ea15aecacf 100644 --- a/pkg/sql/pg_catalog.go +++ b/pkg/sql/pg_catalog.go @@ -1453,14 +1453,6 @@ 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 @@ -1468,7 +1460,10 @@ https://www.postgresql.org/docs/9.5/catalog-pg-index.html`, 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 @@ -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 @@ -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())), @@ -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