Skip to content

Commit

Permalink
Merge #91762
Browse files Browse the repository at this point in the history
91762: sql: permit forward indexes on arrays r=jordanlewis a=jordanlewis

Closes #17154

This functionality has been unlocked since we added trigram indexes - the blocker before was the ability to support both forward and inverted indexes on a given type.

This commit simply permits forward indexes on arrays, where they were previously not supported at all.

Release note (sql change): enable forward indexes on arrays

Co-authored-by: Jordan Lewis <jordanthelewis@gmail.com>
  • Loading branch information
craig[bot] and jordanlewis committed Nov 18, 2022
2 parents e9f217f + 4dcc573 commit b0daa5f
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 127 deletions.
42 changes: 42 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning
Original file line number Diff line number Diff line change
Expand Up @@ -1184,3 +1184,45 @@ CREATE TABLE public.partition_by_nothing (
INDEX partition_by_nothing_b_idx (b ASC),
FAMILY fam_0_pk_a_b (pk, a, b)
)

# Verify that it's not possible to partition tables by array columns (#91766).

statement error unimplemented: partitioning by array column
CREATE TABLE partition_array (
pk INT[] PRIMARY KEY
) PARTITION BY LIST (pk) (PARTITION blah VALUES IN (ARRAY[1], ARRAY[2]))

statement error unimplemented: partitioning by array column
CREATE TABLE partition_array (
pk INT[] PRIMARY KEY
) PARTITION BY RANGE (pk) (PARTITION blah VALUES FROM (ARRAY[1]) TO (ARRAY[2]))

statement error unimplemented: partitioning by array column
CREATE TABLE partition_array (
a INT[],
INDEX (a) PARTITION BY LIST (a) (PARTITION blah VALUES IN (ARRAY[1], ARRAY[2]))
)

statement error unimplemented: partitioning by array column
CREATE TABLE partition_array (
a INT[],
INDEX (a) PARTITION BY RANGE (a) (PARTITION blah VALUES FROM (ARRAY[1]) TO (ARRAY[2]))
)

statement ok
CREATE TABLE partition_array (
pk INT[] PRIMARY KEY,
a INT[]
)

statement error unimplemented: partitioning by array column
ALTER TABLE partition_array PARTITION BY LIST (pk) (PARTITION blah VALUES IN (ARRAY[1], ARRAY[2]))

statement error unimplemented: partitioning by array column
ALTER TABLE partition_array PARTITION BY RANGE (pk) (PARTITION blah VALUES FROM (ARRAY[1]) TO (ARRAY[2]))

statement error unimplemented: partitioning by array column
CREATE INDEX ON partition_array (a) PARTITION BY LIST (a) (PARTITION blah VALUES IN (ARRAY[1], ARRAY[2]))

statement error unimplemented: partitioning by array column
CREATE INDEX ON partition_array (a) PARTITION BY RANGE (a) (PARTITION blah VALUES FROM (ARRAY[1]) TO (ARRAY[2]))
1 change: 1 addition & 0 deletions pkg/ccl/partitionccl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_library(
"//pkg/sql/sem/eval",
"//pkg/sql/sem/tree",
"//pkg/sql/sem/volatility",
"//pkg/sql/types",
"//pkg/util/encoding",
"//pkg/util/errorutil/unimplemented",
"@com_github_cockroachdb_errors//:errors",
Expand Down
5 changes: 5 additions & 0 deletions pkg/ccl/partitionccl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/volatility"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/errors"
Expand Down Expand Up @@ -203,6 +204,10 @@ func createPartitioningImpl(
"declared partition columns (%s) do not match first %d columns in index being partitioned (%s)",
partitioningString(), n, strings.Join(newIdxColumnNames[:n], ", "))
}
if col.GetType().Family() == types.ArrayFamily {
return partDesc, unimplemented.NewWithIssuef(91766, "partitioning by array column (%s) not supported",
col.GetName())
}
}

for _, l := range partBy.List {
Expand Down
6 changes: 4 additions & 2 deletions pkg/sql/catalog/colinfo/col_type_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func ColumnTypeIsIndexable(t *types.T) bool {
// using an inverted index.
func ColumnTypeIsInvertedIndexable(t *types.T) bool {
switch t.Family() {
case types.StringFamily:
case types.ArrayFamily, types.StringFamily:
return true
}
return ColumnTypeIsOnlyInvertedIndexable(t)
Expand All @@ -142,9 +142,11 @@ func ColumnTypeIsOnlyInvertedIndexable(t *types.T) bool {
if t.IsAmbiguous() || t.Family() == types.TupleFamily {
return false
}
if t.Family() == types.ArrayFamily {
t = t.ArrayContents()
}
switch t.Family() {
case types.JsonFamily:
case types.ArrayFamily:
case types.GeographyFamily:
case types.GeometryFamily:
default:
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -2668,13 +2668,13 @@ ALTER TABLE t81448 ADD COLUMN b INT PRIMARY KEY
statement ok
DROP TABLE t81448

subtest add_column_non_indexable_type
subtest add_unique_array_column

statement ok
CREATE TABLE t1_non_indexable (n INT8);
CREATE TABLE t1_unique_array (n INT8);

statement error pq: unimplemented: column x is of type char\[\] and thus is not indexable
ALTER TABLE t1_non_indexable ADD COLUMN x CHAR(256)[] UNIQUE;
statement ok
ALTER TABLE t1_unique_array ADD COLUMN x CHAR(256)[] UNIQUE;

subtest regression_89025

Expand Down
63 changes: 52 additions & 11 deletions pkg/sql/logictest/testdata/logic_test/array
Original file line number Diff line number Diff line change
Expand Up @@ -1373,8 +1373,8 @@ subtest array_indexes
statement ok
DROP TABLE IF EXISTS t

statement error column x is of type int\[\] and thus is not indexable
CREATE TABLE t (x INT[] PRIMARY KEY)
statement ok
CREATE TABLE t_indexed (x INT[] PRIMARY KEY)

statement ok
CREATE TABLE t (x INT[])
Expand Down Expand Up @@ -1455,20 +1455,18 @@ SELECT x FROM t WHERE x > ARRAY[NULL, NULL]:::INT[] ORDER BY x
{5}

# Test some operations on a descending index.
statement error column x is of type int\[\] and thus is not indexable
statement ok
CREATE INDEX i ON t(x DESC)

# Add "t@i" index annotation once #50659 is fixed.
query T
SELECT x FROM t WHERE x <= ARRAY[1] ORDER BY x DESC
SELECT x FROM t@i WHERE x <= ARRAY[1] ORDER BY x DESC
----
{1}
{NULL,NULL,NULL}
{NULL}

# Add "t@i" index annotation once #50659 is fixed.
query T
SELECT x FROM t WHERE x > ARRAY[1] ORDER BY x
SELECT x FROM t@i WHERE x > ARRAY[1] ORDER BY x
----
{1,NULL,10}
{1,4,5}
Expand All @@ -1478,10 +1476,10 @@ SELECT x FROM t WHERE x > ARRAY[1] ORDER BY x

# Ensure that we can order by the arrays without any indexes.
statement ok
DROP TABLE t;
DROP TABLE t

statement ok
CREATE TABLE t (x INT[]);
CREATE TABLE t (x INT[])

statement ok
INSERT INTO t VALUES
Expand Down Expand Up @@ -1518,9 +1516,36 @@ SELECT x FROM t ORDER BY x DESC
{NULL,NULL,NULL}
{NULL}

# Enable index creation once #50659 is fixed.
statement ok
--CREATE INDEX i ON t (x);
CREATE INDEX i ON t (x);

# Ensure selecting works fine on the index.

query T
SELECT x FROM t@i ORDER BY x
----
{NULL}
{NULL,NULL,NULL}
{1}
{1,NULL,10}
{1,4,5}
{1,4,6}
{4}
{5}

query T
SELECT x FROM t@i ORDER BY x DESC
----
{5}
{4}
{1,4,6}
{1,4,5}
{1,NULL,10}
{1}
{NULL,NULL,NULL}
{NULL}

statement ok
INSERT INTO t VALUES (NULL), (NULL)

# Test that NULL's are differentiated from {NULL}.
Expand All @@ -1537,6 +1562,22 @@ SELECT x FROM t WHERE x IS NOT NULL ORDER BY x
{4}
{5}

# Ensure that unique indexes on array columns work okay.
statement ok
CREATE TABLE unique_array (a INT[] UNIQUE, b INT[])

statement ok
INSERT INTO unique_array VALUES (ARRAY[1], ARRAY[2, 3])

statement error duplicate key value violates unique constraint
INSERT INTO unique_array VALUES (ARRAY[1], ARRAY[2, 3])

statement ok
INSERT INTO unique_array VALUES (ARRAY[2], ARRAY[2, 3])

statement error duplicate key value violates unique constraint
CREATE UNIQUE INDEX ON unique_array(b)

# Create an indexes on a bad type.
statement error pq: unimplemented: column x is of type geography\[\] and thus is not indexable
CREATE TABLE tbad (x GEOGRAPHY[] PRIMARY KEY)
Expand Down
57 changes: 56 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/distsql_stats
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ ORDER BY statistics_name, column_names::STRING
----
statistics_name column_names row_count distinct_count null_count has_histogram
arr_stats {rowid} 4 4 0 true
arr_stats_x {x} 4 3 1 false
arr_stats_x {x} 4 3 1 true

# Test that enum columns always have histograms collected for them.
statement ok
Expand Down Expand Up @@ -1900,3 +1900,58 @@ u_defaults {d,c}
u_defaults {d,c,a}
u_c_d_b {d,c,b}
u_defaults {d,c,b,a}

# Make sure that we can properly collect statistics on an array column that's
# both forward and inverted indexable.

statement ok
CREATE TABLE indexed_arr(a INT[]);
CREATE INDEX ON indexed_arr(a)

statement ok
INSERT INTO indexed_arr SELECT ARRAY[g] FROM generate_series(1,10000) g(g)

statement ok
ANALYZE indexed_arr

query TTIB
SELECT statistics_name, column_names, row_count, histogram_id IS NOT NULL AS has_histogram
FROM [SHOW STATISTICS FOR TABLE indexed_arr]
ORDER BY statistics_name, column_names::STRING
----
NULL {a} 10000 true
NULL {rowid} 10000 true

statement ok
CREATE INDEX ON indexed_arr USING GIN (a)

query T
SELECT * FROM indexed_arr WHERE a = ARRAY[100]
----
{100}

query T
SELECT * FROM indexed_arr WHERE a @> ARRAY[100]
----
{100}

statement ok
ANALYZE indexed_arr

query TTIB
SELECT statistics_name, column_names, row_count, histogram_id IS NOT NULL AS has_histogram
FROM [SHOW STATISTICS FOR TABLE indexed_arr]
ORDER BY statistics_name, column_names::STRING
----
NULL {a} 10000 true
NULL {rowid} 10000 true

query T
SELECT * FROM indexed_arr WHERE a = ARRAY[100]
----
{100}

query T
SELECT * FROM indexed_arr WHERE a @> ARRAY[100]
----
{100}
Loading

0 comments on commit b0daa5f

Please sign in to comment.