# LogicTest: 5node

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

query TTITI colnames
SHOW TESTING_RANGES FROM TABLE t
----
Start Key  End Key  Range ID  Replicas  Lease Holder
NULL       NULL     1         {1}       1

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

query TTITI colnames
SHOW TESTING_RANGES FROM TABLE t
----
Start Key  End Key  Range ID  Replicas  Lease Holder
NULL       /1       1         {1}       1
/1         /10      2         {1}       1
/10        NULL     3         {1}       1

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

query TTITI colnames
SHOW TESTING_RANGES FROM TABLE t
----
Start Key  End Key   Range ID Replicas  Lease Holder
NULL       /1        1        {1}       1
/1         /10       2        {4}       4
/10        NULL      3        {1}       1

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

query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM TABLE t]
----
Start Key  End Key  Replicas  Lease Holder
NULL       /1       {1}       1
/1         /5/1     {4}       4
/5/1       /5/2     {4}       4
/5/2       /5/3     {4}       4
/5/3       /10      {4}       4
/10        NULL     {1}       1

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

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

query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM TABLE t]
----
Start Key  End Key  Replicas  Lease Holder
NULL       /1       {1}       1
/1         /5/1     {3,4}     3
/5/1       /5/2     {1,2,3}   1
/5/2       /5/3     {2,3,5}   5
/5/3       /10      {1,2,4}   4
/10        NULL     {1}       1

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

query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM INDEX t@idx]
----
Start Key  End Key  Replicas  Lease Holder
NULL       NULL     {1}       1

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

query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM INDEX t@idx]
----
Start Key  End Key  Replicas  Lease Holder
NULL       /100/1   {1}       1
/100/1     /100/50  {1}       1
/100/50    NULL     {1}       1

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

query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM INDEX t@idx]
----
Start Key  End Key  Replicas  Lease Holder
NULL       /8       {1}       1
/8         /9       {1}       1
/9         /100/1   {1}       1
/100/1     /100/50  {1}       1
/100/50    NULL     {1}       1

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

query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM INDEX t@idx]
----
Start Key  End Key  Replicas  Lease Holder
NULL       /8       {1}       1
/8         /9       {1}       1
/9         /100/1   {1}       1
/100/1     /100/50  {3}       3
/100/50    NULL     {1}       1

# Verify limits and orderings are propagated correctly to the select.
query TITTTTT colnames
EXPLAIN (METADATA) ALTER TABLE t SPLIT AT SELECT k1,k2 FROM t ORDER BY k1 LIMIT 3
----
Tree                 Level  Type    Field  Description  Columns                           Ordering
split                0      split   ·      ·            (key, pretty)                     ·
 └── limit           1      limit   ·      ·            (k1, k2)                          k1!=NULL; k2!=NULL; key(k1,k2); +k1
      └── render     2      render  ·      ·            (k1, k2)                          k1!=NULL; k2!=NULL; key(k1,k2); +k1
           └── scan  3      scan    ·      ·            (k1, k2, v[omitted], w[omitted])  k1!=NULL; k2!=NULL; key(k1,k2); +k1
·                    3      ·       table  t@primary    ·                                 ·
·                    3      ·       spans  ALL          ·                                 ·
·                    3      ·       limit  3            ·                                 ·

# -- Tests with interleaved tables --

statement ok
CREATE TABLE t0 (
  k1 INT, k2 INT, k3 INT, v INT, PRIMARY KEY (k1, k2, k3)
) INTERLEAVE IN PARENT t(k1, k2)

# We expect the splits for t0 to be the same as the splits for t.
query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM TABLE t0]
----
Start Key  End Key  Replicas  Lease Holder
NULL       /1       {1}       1
/1         /5/1     {3,4}     3
/5/1       /5/2     {1,2,3}   1
/5/2       /5/3     {2,3,5}   5
/5/3       /10      {1,2,4}   4
/10        NULL     {1}       1

statement ok
ALTER TABLE t0 SPLIT AT VALUES (7, 8, 9)

query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM TABLE t0]
----
Start Key      End Key        Replicas  Lease Holder
NULL           /1             {1}       1
/1             /5/1           {3,4}     3
/5/1           /5/2           {1,2,3}   1
/5/2           /5/3           {2,3,5}   5
/5/3           /7/8/#/52/1/9  {1,2,4}   4
/7/8/#/52/1/9  /10            {1,2,4}   4
/10            NULL           {1}       1

statement ok
ALTER TABLE t0 SPLIT AT VALUES (11)

query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM TABLE t0]
----
Start Key      End Key        Replicas  Lease Holder
NULL           /1             {1}       1
/1             /5/1           {3,4}     3
/5/1           /5/2           {1,2,3}   1
/5/2           /5/3           {2,3,5}   5
/5/3           /7/8/#/52/1/9  {1,2,4}   4
/7/8/#/52/1/9  /10            {1,2,4}   4
/10            /11            {1}       1
/11            NULL           {1}       1

query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM TABLE t]
----
Start Key          End Key            Replicas  Lease Holder
NULL               /1                 {1}       1
/1                 /5/1               {3,4}     3
/5/1               /5/2               {1,2,3}   1
/5/2               /5/3               {2,3,5}   5
/5/3               /7/8/#/52/1/9      {1,2,4}   4
/7/8/#/52/1/9      /10                {1,2,4}   4
/10                /11                {1}       1
/11                NULL               {1}       1


statement ok
CREATE TABLE t1 (k INT PRIMARY KEY, v1 INT, v2 INT, v3 INT)

statement ok
CREATE INDEX idx on t1(v1,v2,v3) INTERLEAVE IN PARENT t(v1,v2)

# We expect the splits for the index to be the same as the splits for t.
query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM INDEX t1@idx]
----
Start Key      End Key        Replicas  Lease Holder
NULL           /1             {1}       1
/1             /5/1           {3,4}     3
/5/1           /5/2           {1,2,3}   1
/5/2           /5/3           {2,3,5}   5
/5/3           /7/8/#/52/1/9  {1,2,4}   4
/7/8/#/52/1/9  /10            {1,2,4}   4
/10            /11            {1}       1
/11            NULL           {1}       1

statement ok
ALTER INDEX t1@idx SPLIT AT VALUES (15,16)

query TTTI colnames
SELECT "Start Key", "End Key", "Replicas", "Lease Holder" FROM [SHOW TESTING_RANGES FROM INDEX t1@idx]
----
Start Key      End Key        Replicas  Lease Holder
NULL           /1             {1}       1
/1             /5/1           {3,4}     3
/5/1           /5/2           {1,2,3}   1
/5/2           /5/3           {2,3,5}   5
/5/3           /7/8/#/52/1/9  {1,2,4}   4
/7/8/#/52/1/9  /10            {1,2,4}   4
/10            /11            {1}       1
/11            /15/16/#/53/2  {1}       1
/15/16/#/53/2  NULL           {1}       1

statement error too many columns in SPLIT AT data
ALTER TABLE t SPLIT AT VALUES (1, 2, 3)

statement error could not parse "foo" as type int
ALTER TABLE t SPLIT AT VALUES ('foo')

statement error too many columns in TESTING_RELOCATE data
ALTER TABLE t TESTING_RELOCATE VALUES (ARRAY[1], 1, 2, 3)

statement error could not parse "foo" as type int
ALTER TABLE t TESTING_RELOCATE VALUES (ARRAY[4], 'foo')

statement error TESTING_RELOCATE data column 1 \(relocation array\) must be of type int\[\], not type string
ALTER TABLE t TESTING_RELOCATE VALUES ('foo', 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)

query ITTTTTTTTI colnames
SELECT * FROM crdb_internal.ranges
----
range_id  start_key                          start_pretty              end_key                            end_pretty                database  table  index    replicas  lease_holder
                            1         ·                                  /Min                      [187 137 137]                      /Table/51/1/1             ·         ·      ·        {1}       1
                            2         [187 137 137]                      /Table/51/1/1             [187 137 141 137]                  /Table/51/1/5/1           test      t      ·        {4,3}     3
                            11        [187 137 141 137]                  /Table/51/1/5/1           [187 137 141 138]                  /Table/51/1/5/2           test      t      ·        {3,1,2}   1
                            12        [187 137 141 138]                  /Table/51/1/5/2           [187 137 141 139]                  /Table/51/1/5/3           test      t      ·        {3,5,2}   5
                            13        [187 137 141 139]                  /Table/51/1/5/3           [187 137 143 144 254 188 137 145]  /Table/51/1/7/8/#/52/1/9  test      t      ·        {4,1,2}   4
                            14        [187 137 143 144 254 188 137 145]  /Table/51/1/7/8/#/52/1/9  [187 137 146]                      /Table/51/1/10            test      t      ·        {4,1,2}   4
                            3         [187 137 146]                      /Table/51/1/10            [187 137 147]                      /Table/51/1/11            test      t      ·        {1}       1
                            8         [187 137 147]                      /Table/51/1/11            [187 137 151 152 254 189 138]      /Table/51/1/15/16/#/53/2  test      t      ·        {1}       1
                            9         [187 137 151 152 254 189 138]      /Table/51/1/15/16/#/53/2  [187 138 144]                      /Table/51/2/8             test      t      ·        {1}       1
                            6         [187 138 144]                      /Table/51/2/8             [187 138 145]                      /Table/51/2/9             test      t      idx      {1}       1
                            7         [187 138 145]                      /Table/51/2/9             [187 138 236 137]                  /Table/51/2/100/1         test      t      idx      {1}       1
                            4         [187 138 236 137]                  /Table/51/2/100/1         [187 138 236 186]                  /Table/51/2/100/50        test      t      idx      {3}       3
                            5         [187 138 236 186]                  /Table/51/2/100/50        [193 137 136]                      /Table/57/1/0             test      t      idx      {1}       1
                            10        [193 137 136]                      /Table/57/1/0             [194 137 246 123]                  /Table/58/1/123           ·         b      ·        {1}       1
                            21        [194 137 246 123]                  /Table/58/1/123           [194 138 136]                      /Table/58/2/0             d         c      ·        {1}       1
                            22        [194 138 136]                      /Table/58/2/0             [255 255]                          /Max                      d         c      c_i_idx  {1}       1
