diff --git a/pkg/sql/create_index.go b/pkg/sql/create_index.go index 0e78c89cf977..2c6cffeff45f 100644 --- a/pkg/sql/create_index.go +++ b/pkg/sql/create_index.go @@ -326,6 +326,7 @@ func checkIndexColumns( version clusterversion.ClusterVersion, ) error { for i, colDef := range columns { + lastCol := i == len(columns)-1 col, err := catalog.MustFindColumnByTreeName(desc, colDef.Column) if err != nil { return errors.Wrapf(err, "finding column %d", i) @@ -342,7 +343,7 @@ func checkIndexColumns( } // Checking if JSON Columns can be forward indexed for a given cluster version. - if col.GetType().Family() == types.JsonFamily && !inverted && !version.IsActive(clusterversion.V23_2) { + if col.GetType().Family() == types.JsonFamily && (!inverted || !lastCol) && !version.IsActive(clusterversion.V23_2) { return errors.WithHint( pgerror.Newf( pgcode.InvalidTableDefinition, diff --git a/pkg/sql/logictest/testdata/logic_test/inverted_index_multi_column b/pkg/sql/logictest/testdata/logic_test/inverted_index_multi_column index b398ca90b69d..689409d7d73b 100644 --- a/pkg/sql/logictest/testdata/logic_test/inverted_index_multi_column +++ b/pkg/sql/logictest/testdata/logic_test/inverted_index_multi_column @@ -323,6 +323,7 @@ SELECT * FROM backfill_d@idx WHERE i IN (1, 2, 3, 4) AND s = 'bar' AND j @> '7': # Test selecting, inserting, updating, and deleting on a table with a # multi-column JSON inverted index. +skipif config local-mixed-22.2-23.1 statement ok CREATE TABLE d ( id INT PRIMARY KEY, @@ -331,6 +332,17 @@ CREATE TABLE d ( INVERTED INDEX idx (foo, bar) ); +# On 22.2 these inverted indexes could not be created, so skip this +# test by making a normal index +onlyif config local-mixed-22.2-23.1 +statement ok +CREATE TABLE d ( + id INT PRIMARY KEY, + foo JSONB, + bar JSONB, + INDEX idx (id) +); + # Testing inserting statement ok INSERT into d VALUES @@ -394,15 +406,18 @@ INSERT into d VALUES (8, '"backfilling"', '[[0], [1], [2], []]') +skipif config local-mixed-22.2-23.1 statement ok CREATE INVERTED INDEX idx on d (foo, bar) +skipif config local-mixed-22.2-23.1 query ITT SELECT id, foo, bar FROM d@idx where foo = '"backfilling"' AND bar->2 @> '2' ORDER BY id ---- 6 "backfilling" [[0], [1], 2, 3] 8 "backfilling" [[0], [1], [2], []] +skipif config local-mixed-22.2-23.1 query ITT SELECT id, foo, bar FROM d@idx where foo = '"foo"' AND bar->0 = '7' ORDER BY id ---- diff --git a/pkg/upgrade/upgrades/json_forward_indexes_test.go b/pkg/upgrade/upgrades/json_forward_indexes_test.go index eb2171a1a06f..a3ceddc965fe 100644 --- a/pkg/upgrade/upgrades/json_forward_indexes_test.go +++ b/pkg/upgrade/upgrades/json_forward_indexes_test.go @@ -132,6 +132,14 @@ func TestJSONForwardingIndexes(t *testing.T) { )`) require.Error(t, err) + // Creating a table with an inverted index with a JSON column should result in an error. + _, err = db.Exec(`CREATE TABLE test.tbl_json_secondary_unique( + s GEOGRAPHY, + j JSONB, + INVERTED INDEX tbl_json_secondary_uidx (j, s) +)`) + require.Error(t, err) + // Setting a cluster version that supports forward indexes on JSON // columns and expecting success when creating forward indexes. _, err = tc.Conns[0].ExecContext(ctx, `SET CLUSTER SETTING version = $1`, @@ -158,4 +166,13 @@ func TestJSONForwardingIndexes(t *testing.T) { key JSONB PRIMARY KEY )`) require.NoError(t, err) + + // Creating a table with an inverted index with a JSON column should not result + // in an error once inverted indexes are supported. + _, err = db.Exec(`CREATE TABLE test.tbl_json_secondary_unique( + s GEOGRAPHY, + j JSONB, + INVERTED INDEX tbl_json_secondary_uidx (j, s) +)`) + require.NoError(t, err) }