Skip to content

Commit

Permalink
sql: enable indexing and ordering on arrays of orderable and indexabl…
Browse files Browse the repository at this point in the history
…e types

Fixes #17154.
Fixes #35707.

This PR enables arrays to be ordered and indexed by
introducing an ordered key encoding for arrays.
Once this exists, the rest of the SQL infrastructure
is ready to handle indexing and ordering on arrays.

To encode an array of elements `ARRAY[a, b]`,
we create the following encoding.

Let `AM` = a marker byte for arrays and let `AT` be a terminator byte.

`enc(ARRAY[a, b]) = [AM, enc(a), enc(b), AT]`

The key is that the terminator is less than the element marker.
This allows for the "prefix matching" style comparison that
arrays support.

Release note (sql change): This PR adds support for indexing
and ordering of arrays of indexable and orderable inner types.
  • Loading branch information
rohany committed May 6, 2020
1 parent b6dae14 commit 60cb535
Show file tree
Hide file tree
Showing 21 changed files with 993 additions and 182 deletions.
4 changes: 2 additions & 2 deletions pkg/sql/execinfra/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ import (
//
// ATTENTION: When updating these fields, add to version_history.txt explaining
// what changed.
const Version execinfrapb.DistSQLVersion = 28
const Version execinfrapb.DistSQLVersion = 29

// MinAcceptedVersion is the oldest version that the server is
// compatible with; see above.
const MinAcceptedVersion execinfrapb.DistSQLVersion = 27
const MinAcceptedVersion execinfrapb.DistSQLVersion = 29

// SettingWorkMemBytes is a cluster setting that determines the maximum amount
// of RAM that a processor can use.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/flowinfra/stream_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (se *StreamEncoder) AddRow(row sqlbase.EncDatumRow) error {
if !ok {
enc = PreferredEncoding
}
sType := se.infos[i].Type.Family()
sType := se.infos[i].Type
if enc != sqlbase.DatumEncoding_VALUE &&
(sqlbase.HasCompositeKeyEncoding(sType) || sqlbase.MustBeValueEncoded(sType)) {
// Force VALUE encoding for composite types (key encodings may lose data).
Expand Down
19 changes: 0 additions & 19 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -798,25 +798,6 @@ decomputed_column CREATE TABLE decomputed_column (
statement ok
CREATE TABLE b26483()

statement error unimplemented: column c is of type int\[\] and thus is not indexable
ALTER TABLE b26483 ADD COLUMN c INT[] UNIQUE

# As above, but performed in a transaction
statement ok
BEGIN

statement ok
CREATE TABLE b26483_tx()

statement ok
ALTER TABLE b26483_tx ADD COLUMN c INT[]

statement error unimplemented: column c is of type int\[\] and thus is not indexable
CREATE INDEX on b26483_tx (c)

statement ok
ROLLBACK

# Verify that auditing can be enabled by root, and cannot be disabled by non-root.

statement ok
Expand Down
Loading

0 comments on commit 60cb535

Please sign in to comment.