Skip to content

Commit

Permalink
Merge #35915
Browse files Browse the repository at this point in the history
35915: sql: do not create stats on inverted index columns r=rytaft a=rytaft

This commit fixes an issue in which the default columns for
`CREATE STATISTICS` included inverted index columns. Since we
cannot create statistics on JSON columns, running automatic
statistics on a table with an inverted index resulted in the
error "unable to encode table key: *tree.DJSON". This commit
fixes the issue by skipping over inverted indexes when
determining the default columns for `CREATE STATISTICS`.

Fixes #35764

Release note (bug fix): Fixed an error that occurred when
creating statistics on tables with an inverted index.

Co-authored-by: Rebecca Taft <[email protected]>
  • Loading branch information
craig[bot] and rytaft committed Mar 19, 2019
2 parents f95d456 + 1b49d1e commit 5c524c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/create_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ func createStatsDefaultColumns(

// Add columns for each secondary index.
for i := range desc.Indexes {
if desc.Indexes[i].Type == sqlbase.IndexDescriptor_INVERTED {
// We don't yet support stats on inverted indexes.
continue
}
idxCol := desc.Indexes[i].ColumnIDs[0]
if !requestedCols.Contains(int(idxCol)) {
columns = append(
Expand Down
29 changes: 29 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/distsql_stats
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,35 @@ FROM [SHOW STATISTICS FOR TABLE groups] ORDER BY statistics_name, column_names::
statistics_name column_names
s {rowid}

# Regression test for #35212.
statement ok
CREATE TABLE users (
profile_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
last_updated TIMESTAMP DEFAULT now(),
user_profile JSONB,
INVERTED INDEX user_details (user_profile)
)

statement ok
INSERT INTO users (user_profile) VALUES
('{"first_name": "Lola", "last_name": "Dog", "location": "NYC", "online" : true, "friends" : 547}'),
('{"first_name": "Ernie", "status": "Looking for treats", "location" : "Brooklyn"}'),
('{"first_name": "Carl", "last_name": "Kimball", "location": "NYC", "breed": "Boston Terrier"}'
)

# Ensure that trying to create statistics with default columns does not fail
# when there is an inverted index.
statement ok
CREATE STATISTICS s FROM users

query TTI colnames
SELECT statistics_name, column_names, row_count
FROM [SHOW STATISTICS FOR TABLE users] ORDER BY statistics_name, column_names::STRING
----
statistics_name column_names row_count
s {last_updated} 3
s {profile_id} 3

# Arrays are supported.
statement ok
CREATE TABLE arr (x INT[])
Expand Down

0 comments on commit 5c524c4

Please sign in to comment.