# LogicTest: 5node

statement ok
CREATE TABLE t (k1 INT, k2 INT DEFAULT 999, v INT DEFAULT 999, w INT DEFAULT 999, PRIMARY KEY (k1, k2))

query error expected int in the first EXPERIMENTAL_RELOCATE data column; got unknown
ALTER TABLE t EXPERIMENTAL_RELOCATE LEASE SELECT NULL, NULL;

query error NULL value in relocation array for EXPERIMENTAL_RELOCATE
ALTER TABLE t EXPERIMENTAL_RELOCATE VALUES (ARRAY[NULL], NULL);

query TTTI colnames,rowsort
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM TABLE t WITH DETAILS]
ORDER BY 1
----
start_key           end_key       replicas  lease_holder
<before:/Table/72>  <after:/Max>  {1}       1

statement ok
ALTER TABLE t SPLIT AT VALUES (1), (10)

query TTTI colnames,rowsort
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM TABLE t WITH DETAILS]
ORDER BY 1
----
start_key           end_key       replicas  lease_holder
<before:/Table/72>  …/1/1         {1}       1
…/1/1               …/1/10        {1}       1
…/1/10              <after:/Max>  {1}       1

statement ok
ALTER TABLE t EXPERIMENTAL_RELOCATE VALUES (ARRAY[4], 1, 12)

statement ok
ALTER TABLE t EXPERIMENTAL_RELOCATE LEASE VALUES (4, 1, 12)

query TTTI colnames,rowsort
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM TABLE t WITH DETAILS]
ORDER BY 1
----
start_key           end_key       replicas  lease_holder
<before:/Table/72>  …/1/1         {1}       1
…/1/1               …/1/10        {4}       4
…/1/10              <after:/Max>  {1}       1

statement ok
ALTER TABLE t SPLIT AT VALUES (5,1), (5,2), (5,3)

query TTTI colnames,rowsort
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM TABLE t WITH DETAILS]
ORDER BY 1
----
start_key           end_key       replicas  lease_holder
<before:/Table/72>  …/1/1         {1}       1
…/1/1               …/1/5/1       {4}       4
…/1/10              <after:/Max>  {1}       1
…/1/5/1             …/1/5/2       {4}       4
…/1/5/2             …/1/5/3       {4}       4
…/1/5/3             …/1/10        {4}       4

statement ok
ALTER TABLE t EXPERIMENTAL_RELOCATE VALUES (ARRAY[1,2,3], 5, 1), (ARRAY[5,2,3], 5, 2), (ARRAY[4,2,1], 5, 3)

statement ok
ALTER TABLE t EXPERIMENTAL_RELOCATE VALUES (ARRAY[3,4], 4)

statement ok
ALTER TABLE t EXPERIMENTAL_RELOCATE LEASE VALUES (1, 5, 1), (5, 5, 2)

query TTTI colnames,rowsort
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM TABLE t WITH DETAILS]
ORDER BY 1
----
start_key           end_key       replicas  lease_holder
<before:/Table/72>  …/1/1         {1}       1
…/1/1               …/1/5/1       {3,4}     3
…/1/10              <after:/Max>  {1}       1
…/1/5/1             …/1/5/2       {1,2,3}   1
…/1/5/2             …/1/5/3       {2,3,5}   5
…/1/5/3             …/1/10        {1,2,4}   4

statement ok
CREATE INDEX idx ON t(v, w)

query TTTI colnames,rowsort
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM INDEX t@idx WITH DETAILS]
----
start_key     end_key       replicas  lease_holder
…/<IndexMin>  …/<IndexMax>  {1}       1

statement ok
ALTER INDEX t@idx SPLIT AT VALUES (100,1), (100,50)

query TTTI colnames,rowsort
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM INDEX t@idx WITH DETAILS]
----
start_key     end_key       replicas  lease_holder
…/<IndexMin>  …/100/1       {1}       1
…/100/1       …/100/50      {1}       1
…/100/50      …/<IndexMax>  {1}       1

statement ok
ALTER INDEX t@idx SPLIT AT VALUES (8), (9)

query TTTI colnames,rowsort
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM INDEX t@idx WITH DETAILS]
----
start_key     end_key       replicas  lease_holder
…/<IndexMin>  …/8           {1}       1
…/8           …/9           {1}       1
…/9           …/100/1       {1}       1
…/100/1       …/100/50      {1}       1
…/100/50      …/<IndexMax>  {1}       1

statement ok
ALTER INDEX t@idx EXPERIMENTAL_RELOCATE VALUES (ARRAY[5], 100, 10), (ARRAY[3], 100, 11)

query TTTI colnames,rowsort
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM INDEX t@idx WITH DETAILS]
----
start_key     end_key       replicas  lease_holder
…/<IndexMin>  …/8           {1}       1
…/8           …/9           {1}       1
…/9           …/100/1       {1}       1
…/100/1       …/100/50      {3}       3
…/100/50      …/<IndexMax>  {1}       1

# Create and drop things to produce interesting data for crdb_internal.ranges.

statement ok
CREATE DATABASE d

statement ok
CREATE TABLE d.a ()

statement ok
CREATE DATABASE e

statement ok
CREATE TABLE e.b (i INT)

statement ok
ALTER TABLE e.b SPLIT AT VALUES (0)

statement ok
CREATE TABLE d.c (i INT)

statement ok
DROP DATABASE e CASCADE

statement ok
CREATE INDEX ON d.c (i)

statement ok
ALTER TABLE d.c SPLIT AT VALUES (123)

statement ok
ALTER INDEX d.c@c_i_idx SPLIT AT VALUES (0)

# Hex encode start_key, end_key so that text is UTF-8 compatible.
# Non-UTF-8 characters cause git to treat this as a binary file and prevent diff from working.
query TTTTTI colnames,rowsort
SELECT encode(start_key, 'hex'), start_pretty, encode(end_key, 'hex'), end_pretty, replicas, crdb_internal.lease_holder(start_key) FROM crdb_internal.ranges_no_leases;
----
encode                  start_pretty             encode                  end_pretty               replicas  crdb_internal.lease_holder
·                       /Min                     04006c6976656e6573732d  /System/NodeLiveness     {1}       1
04006c6976656e6573732d  /System/NodeLiveness     04006c6976656e6573732e  /System/NodeLivenessMax  {1}       1
04006c6976656e6573732e  /System/NodeLivenessMax  04747364                /System/tsd              {1}       1
04747364                /System/tsd              04747365                /System/"tse"            {1}       1
04747365                /System/"tse"            88                      /Table/0                 {1}       1
88                      /Table/0                 8b                      /Table/3                 {1}       1
8b                      /Table/3                 8c                      /Table/4                 {1}       1
8c                      /Table/4                 8d                      /Table/5                 {1}       1
8d                      /Table/5                 8e                      /Table/6                 {1}       1
8e                      /Table/6                 8f                      /Table/7                 {1}       1
8f                      /Table/7                 90                      /Table/8                 {1}       1
90                      /Table/8                 91                      /Table/9                 {1}       1
91                      /Table/9                 93                      /Table/11                {1}       1
93                      /Table/11                94                      /Table/12                {1}       1
94                      /Table/12                95                      /Table/13                {1}       1
95                      /Table/13                96                      /Table/14                {1}       1
96                      /Table/14                97                      /Table/15                {1}       1
97                      /Table/15                98                      /Table/16                {1}       1
98                      /Table/16                99                      /Table/17                {1}       1
99                      /Table/17                9a                      /Table/18                {1}       1
9a                      /Table/18                9b                      /Table/19                {1}       1
9b                      /Table/19                9c                      /Table/20                {1}       1
9c                      /Table/20                9d                      /Table/21                {1}       1
9d                      /Table/21                9e                      /Table/22                {1}       1
9e                      /Table/22                9f                      /Table/23                {1}       1
9f                      /Table/23                a0                      /Table/24                {1}       1
a0                      /Table/24                a1                      /Table/25                {1}       1
a1                      /Table/25                a2                      /Table/26                {1}       1
a2                      /Table/26                a3                      /Table/27                {1}       1
a3                      /Table/27                a4                      /Table/28                {1}       1
a4                      /Table/28                a5                      /Table/29                {1}       1
a5                      /Table/29                a6                      /NamespaceTable/30       {1}       1
a6                      /NamespaceTable/30       a7                      /NamespaceTable/Max      {1}       1
a7                      /NamespaceTable/Max      a8                      /Table/32                {1}       1
a8                      /Table/32                a9                      /Table/33                {1}       1
a9                      /Table/33                aa                      /Table/34                {1}       1
aa                      /Table/34                ab                      /Table/35                {1}       1
ab                      /Table/35                ac                      /Table/36                {1}       1
ac                      /Table/36                ad                      /Table/37                {1}       1
ad                      /Table/37                ae                      /Table/38                {1}       1
ae                      /Table/38                af                      /Table/39                {1}       1
af                      /Table/39                b0                      /Table/40                {1}       1
b0                      /Table/40                b1                      /Table/41                {1}       1
b1                      /Table/41                b2                      /Table/42                {1}       1
b2                      /Table/42                b3                      /Table/43                {1}       1
b3                      /Table/43                b4                      /Table/44                {1}       1
b4                      /Table/44                b5                      /Table/45                {1}       1
b5                      /Table/45                b6                      /Table/46                {1}       1
b6                      /Table/46                b7                      /Table/47                {1}       1
b7                      /Table/47                b8                      /Table/48                {1}       1
b8                      /Table/48                ba                      /Table/50                {1}       1
ba                      /Table/50                bb                      /Table/51                {1}       1
bb                      /Table/51                bc                      /Table/52                {1}       1
bc                      /Table/52                bd                      /Table/53                {1}       1
bd                      /Table/53                be                      /Table/54                {1}       1
be                      /Table/54                bf                      /Table/55                {1}       1
bf                      /Table/55                c0                      /Table/56                {1}       1
c0                      /Table/56                c1                      /Table/57                {1}       1
c1                      /Table/57                c2                      /Table/58                {1}       1
c2                      /Table/58                c3                      /Table/59                {1}       1
c3                      /Table/59                c4                      /Table/60                {1}       1
c4                      /Table/60                c5                      /Table/61                {1}       1
c5                      /Table/61                c6                      /Table/62                {1}       1
c6                      /Table/62                c7                      /Table/63                {1}       1
c7                      /Table/63                c8                      /Table/64                {1}       1
c8                      /Table/64                c9                      /Table/65                {1}       1
c9                      /Table/65                ca                      /Table/66                {1}       1
ca                      /Table/66                cb                      /Table/67                {1}       1
cb                      /Table/67                cc                      /Table/68                {1}       1
cc                      /Table/68                cd                      /Table/69                {1}       1
cd                      /Table/69                ce                      /Table/70                {1}       1
ce                      /Table/70                cf                      /Table/71                {1}       1
cf                      /Table/71                d0                      /Table/72                {1}       1
d0                      /Table/72                f28989                  /Table/106/1/1           {1}       1
f28989                  /Table/106/1/1           f2898d89                /Table/106/1/5/1         {3,4}     3
f2898d89                /Table/106/1/5/1         f2898d8a                /Table/106/1/5/2         {1,2,3}   1
f2898d8a                /Table/106/1/5/2         f2898d8b                /Table/106/1/5/3         {2,3,5}   5
f2898d8b                /Table/106/1/5/3         f28992                  /Table/106/1/10          {1,2,4}   4
f28992                  /Table/106/1/10          f28a                    /Table/106/2             {1}       1
f28a                    /Table/106/2             f28a90                  /Table/106/2/8           {1}       1
f28a90                  /Table/106/2/8           f28a91                  /Table/106/2/9           {1}       1
f28a91                  /Table/106/2/9           f28aec89                /Table/106/2/100/1       {1}       1
f28aec89                /Table/106/2/100/1       f28aecba                /Table/106/2/100/50      {3}       3
f28aecba                /Table/106/2/100/50      f28b                    /Table/106/3             {1}       1
f28b                    /Table/106/3             f6708988                /Table/112/1/0           {1}       1
f6708988                /Table/112/1/0           f67189f67b              /Table/113/1/123         {1}       1
f67189f67b              /Table/113/1/123         f6718a                  /Table/113/2             {1}       1
f6718a                  /Table/113/2             f6718a88                /Table/113/2/0           {1}       1
f6718a88                /Table/113/2/0           f6718b                  /Table/113/3             {1}       1
f6718b                  /Table/113/3             ffff                    /Max                     {1}       1

query TTTTTI colnames,rowsort
SELECT encode(start_key, 'hex'), start_pretty, encode(end_key, 'hex'), end_pretty, replicas, lease_holder FROM crdb_internal.ranges
----
encode                  start_pretty             encode                  end_pretty               replicas  lease_holder
·                       /Min                     04006c6976656e6573732d  /System/NodeLiveness     {1}       1
04006c6976656e6573732d  /System/NodeLiveness     04006c6976656e6573732e  /System/NodeLivenessMax  {1}       1
04006c6976656e6573732e  /System/NodeLivenessMax  04747364                /System/tsd              {1}       1
04747364                /System/tsd              04747365                /System/"tse"            {1}       1
04747365                /System/"tse"            88                      /Table/0                 {1}       1
88                      /Table/0                 8b                      /Table/3                 {1}       1
8b                      /Table/3                 8c                      /Table/4                 {1}       1
8c                      /Table/4                 8d                      /Table/5                 {1}       1
8d                      /Table/5                 8e                      /Table/6                 {1}       1
8e                      /Table/6                 8f                      /Table/7                 {1}       1
8f                      /Table/7                 90                      /Table/8                 {1}       1
90                      /Table/8                 91                      /Table/9                 {1}       1
91                      /Table/9                 93                      /Table/11                {1}       1
93                      /Table/11                94                      /Table/12                {1}       1
94                      /Table/12                95                      /Table/13                {1}       1
95                      /Table/13                96                      /Table/14                {1}       1
96                      /Table/14                97                      /Table/15                {1}       1
97                      /Table/15                98                      /Table/16                {1}       1
98                      /Table/16                99                      /Table/17                {1}       1
99                      /Table/17                9a                      /Table/18                {1}       1
9a                      /Table/18                9b                      /Table/19                {1}       1
9b                      /Table/19                9c                      /Table/20                {1}       1
9c                      /Table/20                9d                      /Table/21                {1}       1
9d                      /Table/21                9e                      /Table/22                {1}       1
9e                      /Table/22                9f                      /Table/23                {1}       1
9f                      /Table/23                a0                      /Table/24                {1}       1
a0                      /Table/24                a1                      /Table/25                {1}       1
a1                      /Table/25                a2                      /Table/26                {1}       1
a2                      /Table/26                a3                      /Table/27                {1}       1
a3                      /Table/27                a4                      /Table/28                {1}       1
a4                      /Table/28                a5                      /Table/29                {1}       1
a5                      /Table/29                a6                      /NamespaceTable/30       {1}       1
a6                      /NamespaceTable/30       a7                      /NamespaceTable/Max      {1}       1
a7                      /NamespaceTable/Max      a8                      /Table/32                {1}       1
a8                      /Table/32                a9                      /Table/33                {1}       1
a9                      /Table/33                aa                      /Table/34                {1}       1
aa                      /Table/34                ab                      /Table/35                {1}       1
ab                      /Table/35                ac                      /Table/36                {1}       1
ac                      /Table/36                ad                      /Table/37                {1}       1
ad                      /Table/37                ae                      /Table/38                {1}       1
ae                      /Table/38                af                      /Table/39                {1}       1
af                      /Table/39                b0                      /Table/40                {1}       1
b0                      /Table/40                b1                      /Table/41                {1}       1
b1                      /Table/41                b2                      /Table/42                {1}       1
b2                      /Table/42                b3                      /Table/43                {1}       1
b3                      /Table/43                b4                      /Table/44                {1}       1
b4                      /Table/44                b5                      /Table/45                {1}       1
b5                      /Table/45                b6                      /Table/46                {1}       1
b6                      /Table/46                b7                      /Table/47                {1}       1
b7                      /Table/47                b8                      /Table/48                {1}       1
b8                      /Table/48                ba                      /Table/50                {1}       1
ba                      /Table/50                bb                      /Table/51                {1}       1
bb                      /Table/51                bc                      /Table/52                {1}       1
bc                      /Table/52                bd                      /Table/53                {1}       1
bd                      /Table/53                be                      /Table/54                {1}       1
be                      /Table/54                bf                      /Table/55                {1}       1
bf                      /Table/55                c0                      /Table/56                {1}       1
c0                      /Table/56                c1                      /Table/57                {1}       1
c1                      /Table/57                c2                      /Table/58                {1}       1
c2                      /Table/58                c3                      /Table/59                {1}       1
c3                      /Table/59                c4                      /Table/60                {1}       1
c4                      /Table/60                c5                      /Table/61                {1}       1
c5                      /Table/61                c6                      /Table/62                {1}       1
c6                      /Table/62                c7                      /Table/63                {1}       1
c7                      /Table/63                c8                      /Table/64                {1}       1
c8                      /Table/64                c9                      /Table/65                {1}       1
c9                      /Table/65                ca                      /Table/66                {1}       1
ca                      /Table/66                cb                      /Table/67                {1}       1
cb                      /Table/67                cc                      /Table/68                {1}       1
cc                      /Table/68                cd                      /Table/69                {1}       1
cd                      /Table/69                ce                      /Table/70                {1}       1
ce                      /Table/70                cf                      /Table/71                {1}       1
cf                      /Table/71                d0                      /Table/72                {1}       1
d0                      /Table/72                f28989                  /Table/106/1/1           {1}       1
f28989                  /Table/106/1/1           f2898d89                /Table/106/1/5/1         {3,4}     3
f2898d89                /Table/106/1/5/1         f2898d8a                /Table/106/1/5/2         {1,2,3}   1
f2898d8a                /Table/106/1/5/2         f2898d8b                /Table/106/1/5/3         {2,3,5}   5
f2898d8b                /Table/106/1/5/3         f28992                  /Table/106/1/10          {1,2,4}   4
f28992                  /Table/106/1/10          f28a                    /Table/106/2             {1}       1
f28a                    /Table/106/2             f28a90                  /Table/106/2/8           {1}       1
f28a90                  /Table/106/2/8           f28a91                  /Table/106/2/9           {1}       1
f28a91                  /Table/106/2/9           f28aec89                /Table/106/2/100/1       {1}       1
f28aec89                /Table/106/2/100/1       f28aecba                /Table/106/2/100/50      {3}       3
f28aecba                /Table/106/2/100/50      f28b                    /Table/106/3             {1}       1
f28b                    /Table/106/3             f6708988                /Table/112/1/0           {1}       1
f6708988                /Table/112/1/0           f67189f67b              /Table/113/1/123         {1}       1
f67189f67b              /Table/113/1/123         f6718a                  /Table/113/2             {1}       1
f6718a                  /Table/113/2             f6718a88                /Table/113/2/0           {1}       1
f6718a88                /Table/113/2/0           f6718b                  /Table/113/3             {1}       1
f6718b                  /Table/113/3             ffff                    /Max                     {1}       1

# Due to asynchronous splitting of ranges, we cannot guarantee the output
# of the show ranges from database command. The test below just ensures that
# the command gets parsed and evaluated correctly.

# Regression tests for #40450.
statement ok
CREATE DATABASE "show ranges"

statement ok
CREATE TABLE "show ranges".t (x INT PRIMARY KEY)

statement ok
SHOW RANGES FROM DATABASE "show ranges"

query TT
SELECT start_key, end_key FROM [SHOW RANGES FROM TABLE "show ranges".t]
----
<before:/Table/113/3>  <after:/Max>

query TT
SELECT start_key, end_key FROM [SHOW RANGES FROM INDEX "show ranges".t@t_pkey]
----
<before:/Table/113/3>  <after:/Max>

statement ok
CREATE DATABASE """"

statement ok
CREATE TABLE """".t (x INT PRIMARY KEY)

statement ok
SHOW RANGES FROM DATABASE """"

query TT
SELECT start_key, end_key FROM [SHOW RANGES FROM TABLE """".t]
----
<before:/Table/113/3>  <after:/Max>

query TT
SELECT start_key, end_key FROM [SHOW RANGES FROM INDEX """".t@t_pkey]
----
<before:/Table/113/3>  <after:/Max>

query T
SELECT feature_name FROM crdb_internal.feature_usage WHERE feature_name='sql.show.ranges' AND usage_count > 0
----
sql.show.ranges

subtest range_for_row

statement ok
CREATE TABLE simple_range_for_row(x INT PRIMARY KEY)

statement ok
ALTER TABLE simple_range_for_row SPLIT AT VALUES (1), (2)

# Before split
query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM TABLE simple_range_for_row FOR ROW (0)]
----
<before:/Table/113/3>  …/1

# Between splits
query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM TABLE simple_range_for_row FOR ROW (1)]
----
…/1  …/2

# After split
query TT retry
SELECT start_key, end_key FROM [SHOW RANGE FROM TABLE simple_range_for_row FOR ROW (2)]
----
…/2  <after:/Max>

statement ok
CREATE TABLE range_for_row(x INT, y INT, z INT, w INT, PRIMARY KEY (x, y), INDEX i (z, w))

statement ok
ALTER TABLE range_for_row SPLIT AT VALUES (1, 2), (1, 3)

statement ok
ALTER INDEX range_for_row@i SPLIT AT VALUES (3, 4), (3, 5)

query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM TABLE range_for_row FOR ROW (1, 2)]
----
…/1/2  …/1/3

query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM TABLE range_for_row FOR ROW (1, 3)]
----
…/1/3  …/3/4

query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM TABLE range_for_row FOR ROW (1, 1)]
----
<before:/Table/120/1/2>  …/1/2

query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM INDEX range_for_row@i FOR ROW (1, 2, 1, 2)]
----
…/1/3  …/3/4

query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM INDEX range_for_row@i FOR ROW (3, 4, 1, 2)]
----
…/3/4  …/3/5

query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM INDEX range_for_row@i FOR ROW (3, 5, 1, 2)]
----
…/3/5  <after:/Max>

statement ok
CREATE TABLE range_for_row_string(x STRING PRIMARY KEY)

statement ok
ALTER TABLE range_for_row_string SPLIT AT VALUES ('hello')

query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM TABLE range_for_row_string FOR ROW ('he')]
----
<before:/Table/121/2/3/5>  …/"hello"

statement ok
CREATE TABLE range_for_row_decimal(x DECIMAL PRIMARY KEY)

statement ok
ALTER TABLE range_for_row_decimal SPLIT AT VALUES (1), (2)

query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM TABLE range_for_row_decimal FOR ROW (1)]
----
…/1  …/2

statement ok
CREATE TABLE range_for_row_nulls(x INT PRIMARY KEY, y INT, INDEX i (y))

statement ok
ALTER INDEX range_for_row_nulls@i SPLIT AT VALUES (NULL)

query TT
SELECT start_key, end_key from [SHOW RANGE FROM INDEX range_for_row_nulls@i FOR ROW (NULL, 1)]
----
…/NULL  <after:/Max>

subtest end

# Regression for #42456
statement ok
CREATE TABLE t42456 (x int primary key);

statement ok
CREATE INDEX i1 on t42456 (x);

statement ok
CREATE INDEX i2 on t42456 (x);

statement ok
DROP INDEX t42456@i1;

statement ok
DROP INDEX t42456@i2;

statement ok
CREATE INDEX i3 on t42456 (x)

let $t42456_id
SELECT id FROM system.namespace WHERE name='t42456'

# This test depends on the index ID in the query and expectation for
# crdb_internal.encode_key.  Here we look up the index ID to assert
# that it is what we think it is.  If this test fails, then the
# expectation below likely needs to change as well.
query T
WITH indexes AS (
    SELECT json_array_elements(crdb_internal.pb_to_json('cockroach.sql.sqlbase.Descriptor', descriptor)->'table'->'indexes') as idx from system.descriptor WHERE id = $t42456_id
)
SELECT idx->'id' from indexes WHERE idx->>'name' = 'i3';
----
6

query T
SELECT crdb_internal.pretty_key(crdb_internal.encode_key($t42456_id, 6, (1, )), 0)
----
/125/6/1

# Regression test for #44326. SHOW RANGES on a virtual table should cause
# an error, not a panic.
query error SHOW RANGES may not be called on a virtual table
SHOW RANGES FROM TABLE crdb_internal.tables

query error SHOW RANGE FOR ROW may not be called on a virtual table
SHOW RANGE FROM TABLE crdb_internal.tables FOR ROW (0, 0)

# Test that SHOW RANGE FOR ROW returns a nice error message for hidden columns
# in a secondary index.
statement ok
CREATE TABLE err_msg (x INT, y INT, z INT, PRIMARY KEY (x, y), INDEX i (z))

statement error HINT: columns \[x y\] are implicitly part of index "i"'s key, include columns \[z x y\] in this order
SHOW RANGE FROM INDEX err_msg@i FOR ROW (1)

# Regression test for incorrectly handling an excessive number of values in
# SPLIT/UNSPLIT AT statements (#59011).
statement ok
CREATE TABLE t59011 (id UUID NOT NULL DEFAULT gen_random_uuid(), level INT8 NULL DEFAULT 0:::INT8, CONSTRAINT "primary" PRIMARY KEY (id ASC), INDEX i59011 (level ASC));

statement ok
INSERT INTO t59011(level) SELECT 2 FROM generate_series(1, 10);

statement error excessive number of values provided: expected 1, got 2
ALTER INDEX i59011 SPLIT AT VALUES (2, '6cf22b39-a1eb-43ee-8edf-0da8543c5c38'::UUID);

statement error excessive number of values provided: expected 1, got 2
ALTER INDEX i59011 UNSPLIT AT VALUES (2, '6cf22b39-a1eb-43ee-8edf-0da8543c5c38'::UUID);

# Regression for #63646

statement ok
CREATE TYPE e63646 AS ENUM ('a', 'b');

statement ok
CREATE TABLE t63646 (e e63646 PRIMARY KEY);

statement ok
INSERT INTO t63646 VALUES ('a'), ('b');

statement ok
ALTER TABLE t63646 SPLIT AT VALUES ('a'), ('b')

query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM TABLE t63646 FOR ROW ('a')]
----
…/"@"  …/"\x80"

query TT
SELECT start_key, end_key FROM [SHOW RANGE FROM TABLE t63646 FOR ROW ('b')]
----
…/"\x80"  <after:/Max>

# Test permissions for showing ranges with ZONECONFIG privilege

user root

statement ok
GRANT SELECT ON TABLE t to testuser

user testuser

statement error only users with the VIEWACTIVITY or VIEWACTIVITYREDACTED or ZONECONFIG privilege or the admin role can read crdb_internal.ranges_no_leases
SHOW RANGES FROM TABLE t

statement error only users with the VIEWACTIVITY or VIEWACTIVITYREDACTED or ZONECONFIG privilege or the admin role can read crdb_internal.ranges_no_leases
SHOW RANGES FROM INDEX t@idx

user root

statement ok
GRANT ZONECONFIG ON TABLE t TO testuser

user testuser

statement ok
SHOW RANGES FROM TABLE t

statement ok
SHOW RANGES FROM INDEX t@idx

# Test permissions for showing ranges with VIEWACTIVITY privilege

user root

# Revoke ZONECONFIG, user should not have access to the table.
statement ok
REVOKE ZONECONFIG ON TABLE t FROM testuser

user testuser

statement error only users with the VIEWACTIVITY or VIEWACTIVITYREDACTED or ZONECONFIG privilege or the admin role can read crdb_internal.ranges_no_leases
SHOW RANGES FROM TABLE t

statement error only users with the VIEWACTIVITY or VIEWACTIVITYREDACTED or ZONECONFIG privilege or the admin role can read crdb_internal.ranges_no_leases
SHOW RANGES FROM INDEX t@idx

user root

# Check user has access with VIEWACTIVITY
statement ok
ALTER ROLE testuser WITH VIEWACTIVITY

user testuser

statement ok
SHOW RANGES FROM TABLE t

statement ok
SHOW RANGES FROM INDEX t@idx

user root

# Remove VIEWACTIVITY
statement ok
ALTER ROLE testuser with NOVIEWACTIVITY

# Check user has access with VIEWACTIVITYREDACTED
statement ok
ALTER ROLE testuser with VIEWACTIVITYREDACTED

user testuser

statement ok
SHOW RANGES FROM TABLE t

statement ok
SHOW RANGES FROM INDEX t@idx

# Go back to root
user root

# This is a regression test for an issue in the vectorization of
# crdb_internal.range_stats whereby NULLs in the input were not
# properly handled.
statement ok
SELECT crdb_internal.range_stats(k)
  FROM (
          SELECT *
            FROM (
                     SELECT start_key AS k, random() AS r FROM crdb_internal.ranges_no_leases
                     UNION ALL SELECT NULL, random() FROM ROWS FROM (generate_series(1, 100))
                 )
        ORDER BY r DESC
       );

user root

subtest show_range_table_for_row_columns

statement ok
CREATE TABLE tbl_for_row(i INT PRIMARY KEY);

# Ignore non-deterministic range_id.
query TT_ITTTTT colnames
SHOW RANGE FROM TABLE tbl_for_row FOR ROW (0)
----
start_key                     end_key       range_id  lease_holder  lease_holder_locality  replicas  replica_localities      voting_replicas  non_voting_replicas
<before:/Table/130/1/"\x80">  <after:/Max>  _         1             region=test,dc=dc1     {1}       {"region=test,dc=dc1"}  {1}              {}

subtest end

subtest show_range_index_for_row_columns

# Ignore non-deterministic range_id.
statement ok
CREATE TABLE tbl_with_idx_for_row(i INT, INDEX idx (i));

query TT_ITTTTT colnames
SHOW RANGE FROM INDEX tbl_with_idx_for_row@idx FOR ROW (NULL, 0)
----
start_key                     end_key       range_id  lease_holder  lease_holder_locality  replicas  replica_localities      voting_replicas  non_voting_replicas
<before:/Table/130/1/"\x80">  <after:/Max>  _         1             region=test,dc=dc1     {1}       {"region=test,dc=dc1"}  {1}              {}

subtest end
