# LogicTest: 5node-dist-opt

# Ensure that cost-based-optimizer uses an index with zone constraints that most
# closely matches the gateway's locality. Use "retry" option, since it can take
# a bit of time for gossip to refresh the zone.

statement ok
CREATE TABLE t (
    k INT PRIMARY KEY,
    v STRING,
    INDEX secondary (k) STORING (v),
    INDEX tertiary (k) STORING (v)
);

# ------------------------------------------------------------------------------
# Put table in dc2 and secondary index in dc1 so that the gateway matches the
# secondary index rather the primary index.
# ------------------------------------------------------------------------------

statement ok
ALTER TABLE t CONFIGURE ZONE USING constraints='[+region=test,+dc=dc2]'

statement ok
ALTER INDEX t@secondary CONFIGURE ZONE USING constraints='[+region=test,+dc=dc1]'

query TTT retry
EXPLAIN SELECT * FROM t WHERE k=10
----
scan  ·      ·
·     table  t@secondary
·     spans  /10-/11

# ------------------------------------------------------------------------------
# Move secondary to dc3 and put tertiary in dc1 and ensure that gateway matches
# tertiary instead of secondary. Regression for #35546.
# ------------------------------------------------------------------------------

statement ok
ALTER INDEX t@secondary CONFIGURE ZONE USING constraints='[+region=test,+dc=dc3]'

statement ok
ALTER INDEX t@tertiary CONFIGURE ZONE USING constraints='[+region=test,+dc=dc1]'

query TTT retry
EXPLAIN SELECT * FROM t WHERE k=10
----
scan  ·      ·
·     table  t@tertiary
·     spans  /10-/11

# ------------------------------------------------------------------------------
# Swap secondary and tertiary localities and ensure invalidation occurs.
# Regression for #35546.
# ------------------------------------------------------------------------------

statement ok
ALTER INDEX t@secondary CONFIGURE ZONE USING constraints='[+region=test,+dc=dc1]'

statement ok
ALTER INDEX t@tertiary CONFIGURE ZONE USING constraints='[+region=test,+dc=dc3]'

query TTT retry
EXPLAIN SELECT * FROM t WHERE k=10
----
scan  ·      ·
·     table  t@secondary
·     spans  /10-/11

# ------------------------------------------------------------------------------
# Swap location of primary and secondary indexes and ensure that primary index
# is used instead.
# ------------------------------------------------------------------------------

statement ok
ALTER TABLE t CONFIGURE ZONE USING constraints='[+region=test,+dc=dc1]'

statement ok
ALTER INDEX t@secondary CONFIGURE ZONE USING constraints='[+region=test,+dc=dc2]'

query TTT retry
EXPLAIN SELECT * FROM t WHERE k=10
----
scan  ·      ·
·     table  t@primary
·     spans  /10-/10/#

# ------------------------------------------------------------------------------
# Use PREPARE to make sure that the prepared plan is invalidated when the
# secondary index's constraints change.
# ------------------------------------------------------------------------------

statement
PREPARE p AS SELECT tree, field, description FROM [EXPLAIN SELECT k, v FROM t WHERE k=10]

query TTT retry
EXECUTE p
----
scan  ·      ·
·     table  t@primary
·     spans  /10-/10/#

statement ok
ALTER TABLE t CONFIGURE ZONE USING constraints='[+region=test,+dc=dc2]'

statement ok
ALTER INDEX t@secondary CONFIGURE ZONE USING constraints='[+region=test,+dc=dc1]'

query TTT retry
EXECUTE p
----
scan  ·      ·
·     table  t@secondary
·     spans  /10-/11

statement ok
DEALLOCATE p

# ------------------------------------------------------------------------------
# Put table lease preference in dc2 and secondary index lease preference in dc1
# so that the gateway matches the secondary index rather the primary index.
# ------------------------------------------------------------------------------

statement ok
ALTER TABLE t CONFIGURE ZONE
USING constraints='[+region=test]', lease_preferences='[[+region=test,+dc=dc2]]'

statement ok
ALTER INDEX t@secondary CONFIGURE ZONE
USING constraints='[+region=test]', lease_preferences='[[+region=test,+dc=dc1]]'

query TTT retry
EXPLAIN SELECT * FROM t WHERE k=10
----
scan  ·      ·
·     table  t@secondary
·     spans  /10-/11

# ------------------------------------------------------------------------------
# Move secondary lease preference to dc3 and put tertiary lease preference in
# dc1 and ensure that gateway matches tertiary.
# ------------------------------------------------------------------------------

statement ok
ALTER INDEX t@secondary CONFIGURE ZONE
USING constraints='[+region=test]', lease_preferences='[[+region=test,+dc=dc3]]'

statement ok
ALTER INDEX t@tertiary CONFIGURE ZONE
USING constraints='[+region=test]', lease_preferences='[[+region=test,+dc=dc1]]'

query TTT retry
EXPLAIN SELECT * FROM t WHERE k=10
----
scan  ·      ·
·     table  t@tertiary
·     spans  /10-/11

# ------------------------------------------------------------------------------
# Ensure that an index constrained to a region is preferred over an index that
# merely has a lease preference in that region (since lease preferences can
# move, whereas constraints are fixed).
# ------------------------------------------------------------------------------

statement ok
ALTER TABLE t CONFIGURE ZONE
USING constraints='[+region=test]', lease_preferences='[[+region=test,+dc=dc1]]'

statement ok
ALTER INDEX t@secondary CONFIGURE ZONE
USING constraints='[+region=test,+dc=dc1]'

statement ok
ALTER INDEX t@tertiary CONFIGURE ZONE
USING constraints='[+region=test]', lease_preferences='[[+region=test,+dc=dc1]]'

query TTT retry
EXPLAIN SELECT * FROM t WHERE k=10
----
scan  ·      ·
·     table  t@secondary
·     spans  /10-/11

# ------------------------------------------------------------------------------
# Use PREPARE to make sure that the prepared plan is invalidated when the
# secondary index's lease preferences change.
# ------------------------------------------------------------------------------

statement ok
PREPARE p AS SELECT tree, field, description FROM [EXPLAIN SELECT k, v FROM t WHERE k=10]

query TTT retry
EXECUTE p
----
scan  ·      ·
·     table  t@secondary
·     spans  /10-/11

statement ok
ALTER INDEX t@secondary CONFIGURE ZONE
USING constraints='[+region=test]', lease_preferences='[[+region=test,+dc=dc2]]'

query TTT retry
EXECUTE p
----
scan  ·      ·
·     table  t@primary
·     spans  /10-/10/#

statement ok
DEALLOCATE p


# ------------------------------------------------------------------------------
# Regression for issue #36642. Optimizer picked wrong index when the index had
# constraints / lease preferences, but the table had no zone config.
# ------------------------------------------------------------------------------

statement ok
CREATE TABLE t36642 (
    k INT PRIMARY KEY,
    v STRING,
    INDEX secondary (k) STORING (v),
    INDEX tertiary (k) STORING (v)
);

statement ok
ALTER INDEX t36642@secondary CONFIGURE ZONE USING lease_preferences='[[+region=test,+dc=dc1]]'

query TTT retry
EXPLAIN SELECT * FROM t36642 WHERE k=10
----
scan  ·      ·
·     table  t36642@secondary
·     spans  /10-/11

statement ok
ALTER INDEX t36642@tertiary CONFIGURE ZONE USING lease_preferences='[[+region=test,+dc=dc1]]'

statement ok
ALTER INDEX t36642@secondary CONFIGURE ZONE USING lease_preferences='[[+region=test,+dc=dc2]]'

query TTT retry
EXPLAIN SELECT * FROM t36642 WHERE k=10
----
scan  ·      ·
·     table  t36642@tertiary
·     spans  /10-/11


# ------------------------------------------------------------------------------
# Regression for issue #36644. Allow matching constraints for leading locality
# tiers to be omitted.
# ------------------------------------------------------------------------------

statement ok
CREATE TABLE t36644 (
    k INT PRIMARY KEY,
    v STRING,
    INDEX secondary (k) STORING (v),
    INDEX tertiary (k) STORING (v)
);

statement ok
ALTER INDEX t36644@secondary
CONFIGURE ZONE USING constraints='[+region=test]', lease_preferences='[[+dc=dc1]]'

query TTT retry
EXPLAIN SELECT * FROM t36644 WHERE k=10
----
scan  ·      ·
·     table  t36644@secondary
·     spans  /10-/11

statement ok
ALTER INDEX t36644@secondary CONFIGURE ZONE USING lease_preferences='[[+dc=dc3]]'

statement ok
ALTER INDEX t36644@tertiary
CONFIGURE ZONE USING constraints='[+region=test]', lease_preferences='[[+dc=dc1]]'

query TTT retry
EXPLAIN SELECT * FROM t36644 WHERE k=10
----
scan  ·      ·
·     table  t36644@tertiary
·     spans  /10-/11

subtest regression_35756

statement ok
CREATE TABLE t35756 (x INT PRIMARY KEY)
  PARTITION BY LIST (x) (
    PARTITION x1 VALUES IN (1),
    PARTITION DEFAULT VALUES IN (DEFAULT)
  )

# When using partitions, if the partition has no zone config set, SHOW
# should display the default zone config and NOT display the "PARTITION"
# keyword.

query TT
SHOW ZONE CONFIGURATION FOR PARTITION x1 OF TABLE t35756
----
.default  ALTER RANGE default CONFIGURE ZONE USING
          range_min_bytes = 16777216,
          range_max_bytes = 67108864,
          gc.ttlseconds = 90000,
          num_replicas = 3,
          constraints = '[]',
          lease_preferences = '[]'

# regression for #38074
statement ok
CREATE TABLE t38074 (x INT, index i(x));

statement ok
ALTER INDEX t38074@i CONFIGURE ZONE USING gc.ttlseconds = 80000

statement ok
ALTER TABLE t38074 CONFIGURE ZONE USING gc.ttlseconds = 70000

# Ensure that the table-level zone configuration is no longer a placeholder.
query TT
SHOW ZONE CONFIGURATION FOR TABLE t38074
----
test.t38074 ALTER TABLE t38074 CONFIGURE ZONE USING
                               range_min_bytes = 16777216,
                               range_max_bytes = 67108864,
                               gc.ttlseconds = 70000,
                               num_replicas = 3,
                               constraints = '[]',
                               lease_preferences = '[]'

query TT
SHOW ZONE CONFIGURATION FOR INDEX t38074@i
----
test.t38074@i ALTER INDEX t38074@i CONFIGURE ZONE USING
                                   range_min_bytes = 16777216,
                                   range_max_bytes = 67108864,
                                   gc.ttlseconds = 80000,
                                   num_replicas = 3,
                                   constraints = '[]',
                                   lease_preferences = '[]'

# Test that index configurations don't infect partition configurations.
# Specifically we are testing that values written to infect@primary's
# zone configuration does not appear in partition p1 of infect@primary's zone config.
statement ok
CREATE TABLE infect (x INT PRIMARY KEY);
ALTER TABLE infect PARTITION BY LIST (x) ( PARTITION p1 VALUES IN (1));
ALTER INDEX infect@primary CONFIGURE ZONE USING num_replicas=5;
ALTER PARTITION p1 OF TABLE infect CONFIGURE ZONE USING constraints='[+dc=dc1]'

query T
SELECT config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.infect.p1'
----
ALTER PARTITION p1 OF INDEX test.public.infect@primary CONFIGURE ZONE USING
  constraints = '[+dc=dc1]'

# Test that copy from parent works as expected.
statement ok
CREATE TABLE copy_from_parent (x INT PRIMARY KEY);
ALTER TABLE copy_from_parent PARTITION BY LIST (x) ( PARTITION p1 VALUES IN (1))

statement ok
ALTER DATABASE test CONFIGURE ZONE USING num_replicas = 7

# Test that first inheriting from the parent database works correctly.
statement ok
ALTER TABLE copy_from_parent CONFIGURE ZONE USING num_replicas = COPY FROM PARENT

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.copy_from_parent'
----
test.copy_from_parent  ALTER TABLE test.public.copy_from_parent CONFIGURE ZONE USING
                  num_replicas = 7

# Test that resetting the field manually works correctly.
statement ok
ALTER TABLE copy_from_parent CONFIGURE ZONE USING num_replicas = 3

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.copy_from_parent'
----
test.copy_from_parent  ALTER TABLE test.public.copy_from_parent CONFIGURE ZONE USING
                  num_replicas = 3

# Test that trying to apply COPY FROM PARENT again picks up the parent's value.
statement ok
ALTER TABLE copy_from_parent CONFIGURE ZONE USING num_replicas = COPY FROM PARENT

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.copy_from_parent'
----
test.copy_from_parent  ALTER TABLE test.public.copy_from_parent CONFIGURE ZONE USING
                  num_replicas = 7

# Ensure that the table has different zone configurations than its parent in
# order to avoid accidentally copying the parent value.
statement ok
ALTER TABLE copy_from_parent CONFIGURE ZONE USING num_replicas = 6

# Test that the partition can inherit the table's configuration values.
statement ok
ALTER PARTITION p1 OF TABLE copy_from_parent CONFIGURE ZONE USING num_replicas = 3

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.copy_from_parent.p1'
----
test.copy_from_parent.p1  ALTER PARTITION p1 OF INDEX test.public.copy_from_parent@primary CONFIGURE ZONE USING
                              num_replicas = 3

statement ok
ALTER PARTITION p1 OF TABLE copy_from_parent CONFIGURE ZONE USING num_replicas = COPY FROM PARENT

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.copy_from_parent.p1'
----
test.copy_from_parent.p1  ALTER PARTITION p1 OF INDEX test.public.copy_from_parent@primary CONFIGURE ZONE USING
                              num_replicas = 6

statement ok
ALTER INDEX copy_from_parent@primary CONFIGURE ZONE USING num_replicas = 5

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.copy_from_parent@primary'
----
test.copy_from_parent@primary ALTER INDEX test.public.copy_from_parent@primary CONFIGURE ZONE USING
                              num_replicas = 5


# Test that an index can inherit from its parent.
statement ok
ALTER INDEX copy_from_parent@primary CONFIGURE ZONE USING num_replicas = COPY FROM PARENT

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.copy_from_parent@primary'
----
test.copy_from_parent@primary ALTER INDEX test.public.copy_from_parent@primary CONFIGURE ZONE USING
                              num_replicas = 6

# Test that a partition can inherit from its parent index configuration.

# First change the index's field value.
statement ok
ALTER INDEX copy_from_parent@primary CONFIGURE ZONE USING num_replicas = 9

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.copy_from_parent@primary'
----
test.copy_from_parent@primary ALTER INDEX test.public.copy_from_parent@primary CONFIGURE ZONE USING
                              num_replicas = 9

statement ok
ALTER PARTITION p1 OF TABLE copy_from_parent CONFIGURE ZONE USING num_replicas = COPY FROM PARENT

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.copy_from_parent.p1'
----
test.copy_from_parent.p1  ALTER PARTITION p1 OF INDEX test.public.copy_from_parent@primary CONFIGURE ZONE USING
                              num_replicas = 9

# check that copy from parent on a subzone doesn't accidentally modify the parent zone.
statement ok
CREATE TABLE parent_modify (x INT, INDEX idx (x));
ALTER TABLE parent_modify CONFIGURE ZONE USING gc.ttlseconds = 700;
ALTER INDEX parent_modify@idx CONFIGURE ZONE USING num_replicas = COPY FROM PARENT

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.parent_modify@idx'
----
test.parent_modify@idx  ALTER INDEX test.public.parent_modify@idx CONFIGURE ZONE USING
                        num_replicas = 7

query TT
SELECT zone_name, config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE zone_name = 'test.parent_modify'
----
test.parent_modify  ALTER TABLE test.public.parent_modify CONFIGURE ZONE USING
                    gc.ttlseconds = 700

# Regression test for #44231.
statement ok
CREATE TABLE t44231 (x INT, INDEX i (x));
ALTER INDEX t44231@i CONFIGURE ZONE USING num_replicas=COPY FROM PARENT;
ALTER INDEX t44231@i CONFIGURE ZONE USING num_replicas=COPY FROM PARENT;
ALTER INDEX t44231@i CONFIGURE ZONE USING num_replicas=COPY FROM PARENT


# ------------------------------------------------------------------------------
# Regression test for #36348; place this at the bottom of this file.
# ------------------------------------------------------------------------------

statement ok
DROP INDEX t@secondary
