Skip to content

Commit

Permalink
sql: enabling forward indexes and ORDERBY on JSONB columns
Browse files Browse the repository at this point in the history
Currently, cockroachdb#97928 outlines the scheme for JSONB encoding
and decoding for forward indexes. However, the PR doesn't
enable this feature to our users. This current PR aims
to allow forward indexes on JSONB columns. The presence
of a lexicographical ordering, as described in cockroachdb#97928,
shall now allow primary and secondary indexes on JSONB
columns along with the ability to use ORDER BY filter
in their queries.

Additionally, JSON values consist of decimal numbers
and containers, such as Arrays and Objects, which can
contain these decimal numbers. In order to preserve
the values after the decimal, JSONB columns are now
required to be composite in nature. This shall enable
such values to be stored in both the key and the value
side of a K/V pair in hopes of receiving the exact value.

Release note (sql change): This PR adds support for enabling
forward indexes and ordering on JSON values.

Epic: CRDB-24501
  • Loading branch information
Shivs11 committed Apr 6, 2023
1 parent 146b7ea commit dbdf486
Show file tree
Hide file tree
Showing 14 changed files with 621 additions and 123 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ build/Railroad.jar
pkg/roachprod/vm/aws/embedded.go
pkg/security/securitytest/embedded.go

pkg/sql/logictest/testdata/logic_test/__test
.idea/
# Temporary directories during gomock generate
**/gomock_reflect_*
4 changes: 2 additions & 2 deletions pkg/sql/catalog/bootstrap/testdata/testdata

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/sql/catalog/systemschema/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2674,6 +2674,7 @@ var (
Version: descpb.StrictIndexColumnIDGuaranteesVersion,
Type: descpb.IndexDescriptor_INVERTED,
InvertedColumnKinds: []catpb.InvertedIndexColumnKind{catpb.InvertedIndexColumnKind_DEFAULT},
CompositeColumnIDs: []descpb.ColumnID{13},
},
descpb.IndexDescriptor{
Name: "execution_count_idx",
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/catalog/systemschema_test/testdata/bootstrap

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions pkg/sql/catalog/tabledesc/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,23 @@ func TestIndexInterface(t *testing.T) {
errMsgFmt := "Unexpected %s result for index '%s'."

// Check index methods on features not tested here.
for _, idx := range indexes {
for pos, idx := range indexes {
require.False(t, idx.IsDisabled(),
errMsgFmt, "IsDisabled", idx.GetName())
require.False(t, idx.IsCreatedExplicitly(),
errMsgFmt, "IsCreatedExplicitly", idx.GetName())
require.False(t, idx.HasOldStoredColumns(),
errMsgFmt, "HasOldStoredColumns", idx.GetName())
require.Equalf(t, 0, idx.NumCompositeColumns(),
errMsgFmt, "NumCompositeColumns", idx.GetName())
if pos != 2 {
require.Equalf(t, 0, idx.NumCompositeColumns(),
errMsgFmt, "NumCompositeColumns", idx.GetName())
} else {
// The secondary index "s2" is an inverted index on the JSONB
// column, and we thus expect one composite column for this
// index and 0 for all the others.
require.Equalf(t, 1, idx.NumCompositeColumns(),
errMsgFmt, "NumCompositeColumns", idx.GetName())
}
}

// Check particular index features.
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/colencoding/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ go_library(
"//pkg/sql/rowenc/keyside",
"//pkg/sql/rowenc/valueside",
"//pkg/sql/sem/tree",
"//pkg/sql/sessiondatapb",
"//pkg/sql/types",
"//pkg/util/buildutil",
"//pkg/util/duration",
"//pkg/util/encoding",
"//pkg/util/intsets",
"//pkg/util/json",
"//pkg/util/uuid",
"@com_github_cockroachdb_apd_v3//:apd",
"@com_github_cockroachdb_errors//:errors",
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/logictest/testdata/logic_test/array
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,7 @@ SELECT x FROM t WHERE x < ARRAY[1, 4, 3] ORDER BY x
{1}
{1,NULL,10}


query T
SELECT x FROM t WHERE x > ARRAY [1, NULL] ORDER BY x DESC
----
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit dbdf486

Please sign in to comment.