exec-ddl
CREATE TABLE abc (
    a INT PRIMARY KEY,
    b INT,
    c STRING,
    UNIQUE INDEX bc1 (b, c),
    UNIQUE INDEX bc2 (b, c)
)
----

exec-ddl
CREATE TABLE xy (
    x INT PRIMARY KEY,
    y INT,
    INDEX y1 (y),
    INDEX y2 (y)
)
----

# --------------------------------------------------
# Single constraints.
# --------------------------------------------------

exec-ddl
ALTER TABLE abc CONFIGURE ZONE USING constraints='[+region=central]'
----

exec-ddl
ALTER INDEX abc@bc1 CONFIGURE ZONE USING constraints='[+region=east]'
----

exec-ddl
ALTER INDEX abc@bc2 CONFIGURE ZONE USING constraints='[+region=west]'
----

# With locality in central, use primary index.
opt format=show-all locality=(region=central)
SELECT * FROM abc
----
scan t.public.abc
 ├── columns: a:1(int!null) b:2(int) c:3(string)
 ├── stats: [rows=1000]
 ├── cost: 1084.62
 ├── key: (1)
 ├── fd: (1)-->(2,3), (2,3)~~>(1)
 ├── distribution: central
 ├── prune: (1-3)
 └── interesting orderings: (+1) (+2,+3,+1)

# With locality in central, still use bc1 index when the filter is selective.
opt format=show-all locality=(region=central)
SELECT * FROM abc WHERE b=10
----
distribute
 ├── columns: a:1(int!null) b:2(int!null) c:3(string)
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 425.24
 ├── key: (1)
 ├── fd: ()-->(2), (1)-->(3), (2,3)~~>(1)
 ├── distribution: central
 ├── input distribution: east
 ├── prune: (1,3)
 ├── interesting orderings: (+1 opt(2)) (+3,+1 opt(2))
 └── scan t.public.abc@bc1
      ├── columns: t.public.abc.a:1(int!null) t.public.abc.b:2(int!null) t.public.abc.c:3(string)
      ├── constraint: /2/3: [/10 - /10]
      ├── stats: [rows=10, distinct(2)=1, null(2)=0]
      ├── cost: 225.22
      ├── key: (1)
      ├── fd: ()-->(2), (1)-->(3), (2,3)~~>(1)
      ├── prune: (1,3)
      └── interesting orderings: (+1 opt(2)) (+3,+1 opt(2))

# With locality in east, use bc1 index.
opt format=show-all locality=(region=east)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.5200001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: east
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# With locality in west, use bc2 index.
opt format=show-all locality=(region=west)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc2
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.5200001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: west
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# No locality, so use bc1, since it's first.
opt format=show-all
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 224.52
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# Locality doesn't match any constraints, so use bc1, since it's first.
opt format=show-all locality=(region=central)
SELECT b, c FROM abc WHERE b=10
----
distribute
 ├── columns: b:2(int!null) c:3(string)
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 425.04
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: central
 ├── input distribution: east
 ├── prune: (3)
 ├── interesting orderings: (+3 opt(2))
 └── scan t.public.abc@bc1
      ├── columns: t.public.abc.b:2(int!null) t.public.abc.c:3(string)
      ├── constraint: /2/3: [/10 - /10]
      ├── stats: [rows=10, distinct(2)=1, null(2)=0]
      ├── cost: 225.02
      ├── lax-key: (3)
      ├── fd: ()-->(2)
      ├── prune: (3)
      └── interesting orderings: (+3 opt(2))

# --------------------------------------------------
# Multiple constraints.
# --------------------------------------------------

exec-ddl
ALTER TABLE abc CONFIGURE ZONE USING constraints='[+region=us,+dc=central,+rack=1]'
----

exec-ddl
ALTER INDEX abc@bc1 CONFIGURE ZONE USING constraints='[+region=us,+dc=east,+rack=1]'
----

# Do not specify region constraint.
exec-ddl
ALTER INDEX abc@bc2 CONFIGURE ZONE USING constraints='[+dc=west]'
----

# With locality in us + central, use primary index.
opt format=show-all locality=(region=us,dc=central)
SELECT * FROM abc
----
scan t.public.abc
 ├── columns: a:1(int!null) b:2(int) c:3(string)
 ├── stats: [rows=1000]
 ├── cost: 1084.62
 ├── key: (1)
 ├── fd: (1)-->(2,3), (2,3)~~>(1)
 ├── distribution: us
 ├── prune: (1-3)
 └── interesting orderings: (+1) (+2,+3,+1)

# With locality in us + central, still use bc1 index if filter is selective.
opt format=show-all locality=(region=us,dc=central)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.7700001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# With locality in us + east, use bc1 index.
opt format=show-all locality=(region=us,dc=east)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.5200001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# With locality in us + west, use bc2 index, even though region does not match
# any constraint on the index.
opt format=show-all locality=(region=us,dc=west)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.7700001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# --------------------------------------------------
# Multiple replica constraints.
# --------------------------------------------------

exec-ddl
ALTER INDEX abc@bc1 CONFIGURE ZONE USING constraints='{"+region=us,+dc=east":2, "+region=us,+dc=west":1}'
----

exec-ddl
ALTER INDEX abc@bc2 CONFIGURE ZONE USING constraints='[+dc=east]'
----

# With locality in us, use bc1 index, since only one tier matches in case of
# both indexes.
opt format=show-all locality=(region=us)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.5200001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# With locality in us + east, use bc2 index (use lowest match count when
# replicas have different numbers of matches).
opt format=show-all locality=(region=us,dc=east)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.7700001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# --------------------------------------------------
# Complex constraints.
# --------------------------------------------------

exec-ddl
ALTER INDEX abc@bc1 CONFIGURE ZONE USING constraints='[+region=us,-region=eu,+region=ap]'
----

exec-ddl
ALTER INDEX abc@bc2 CONFIGURE ZONE USING constraints='[+region=eu,+region=us,+dc=east]'
----

# With locality in us, use bc1, since it's first in order.
opt format=show-all locality=(region=us)
SELECT b, c FROM abc WHERE b=10
----
distribute
 ├── columns: b:2(int!null) c:3(string)
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 424.54
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── input distribution: ap,us
 ├── prune: (3)
 ├── interesting orderings: (+3 opt(2))
 └── scan t.public.abc@bc1
      ├── columns: t.public.abc.b:2(int!null) t.public.abc.c:3(string)
      ├── constraint: /2/3: [/10 - /10]
      ├── stats: [rows=10, distinct(2)=1, null(2)=0]
      ├── cost: 224.52
      ├── lax-key: (3)
      ├── fd: ()-->(2)
      ├── prune: (3)
      └── interesting orderings: (+3 opt(2))

# With locality in eu, use bc2, since it's prohibited with bc1.
opt format=show-all locality=(region=eu)
SELECT b, c FROM abc WHERE b=10
----
distribute
 ├── columns: b:2(int!null) c:3(string)
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 424.54
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: eu
 ├── input distribution: eu,us
 ├── prune: (3)
 ├── interesting orderings: (+3 opt(2))
 └── scan t.public.abc@bc2
      ├── columns: t.public.abc.b:2(int!null) t.public.abc.c:3(string)
      ├── constraint: /2/3: [/10 - /10]
      ├── stats: [rows=10, distinct(2)=1, null(2)=0]
      ├── cost: 224.52
      ├── lax-key: (3)
      ├── fd: ()-->(2)
      ├── prune: (3)
      └── interesting orderings: (+3 opt(2))

# With locality in us + east, use bc2, since it matches both tiers, even though
# "us" match is after "eu" in list.
opt format=show-all locality=(region=us,dc=east)
SELECT b, c FROM abc WHERE b=10
----
distribute
 ├── columns: b:2(int!null) c:3(string)
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 424.54
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── input distribution: eu,us
 ├── prune: (3)
 ├── interesting orderings: (+3 opt(2))
 └── scan t.public.abc@bc2
      ├── columns: t.public.abc.b:2(int!null) t.public.abc.c:3(string)
      ├── constraint: /2/3: [/10 - /10]
      ├── stats: [rows=10, distinct(2)=1, null(2)=0]
      ├── cost: 224.52
      ├── lax-key: (3)
      ├── fd: ()-->(2)
      ├── prune: (3)
      └── interesting orderings: (+3 opt(2))

# With locality in ap + east, use bc1, since ap is not in list of regions for
# bc2, even though dc=east matches.
opt format=show-all locality=(region=ap,dc=east)
SELECT b, c FROM abc WHERE b=10
----
distribute
 ├── columns: b:2(int!null) c:3(string)
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 424.79
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: ap
 ├── input distribution: ap,us
 ├── prune: (3)
 ├── interesting orderings: (+3 opt(2))
 └── scan t.public.abc@bc1
      ├── columns: t.public.abc.b:2(int!null) t.public.abc.c:3(string)
      ├── constraint: /2/3: [/10 - /10]
      ├── stats: [rows=10, distinct(2)=1, null(2)=0]
      ├── cost: 224.77
      ├── lax-key: (3)
      ├── fd: ()-->(2)
      ├── prune: (3)
      └── interesting orderings: (+3 opt(2))

exec-ddl
ALTER INDEX abc@bc1 CONFIGURE ZONE USING constraints='[-region=eu,+dc=east]'
----

exec-ddl
ALTER INDEX abc@bc2 CONFIGURE ZONE USING constraints='[+dc=east]'
----

# With locality in us + east, use bc1, since it's first in order.
opt format=show-all locality=(region=us,dc=east)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 28.5200001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# With locality in eu + east, use bc2, since eu is prohibited for bc1.
opt format=show-all locality=(region=eu,dc=east)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc2
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 28.5200001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: eu
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# --------------------------------------------------
# Lookup join.
# --------------------------------------------------

exec-ddl
ALTER INDEX abc@bc1 CONFIGURE ZONE USING constraints='[+region=us,+dc=east]'
----

exec-ddl
ALTER INDEX abc@bc2 CONFIGURE ZONE USING constraints='[+region=us,+dc=west]'
----

exec-ddl
ALTER INDEX xy@y1 CONFIGURE ZONE USING constraints='[+region=us,+dc=east]'
----

exec-ddl
ALTER INDEX xy@y2 CONFIGURE ZONE USING constraints='[+region=us,+dc=west]'
----

# Ensure that both indexes involved in the lookup join are selected from the
# "west" data center.
opt format=show-all locality=(region=us,dc=west)
SELECT * FROM abc INNER LOOKUP JOIN xy ON b=y WHERE b=1
----
inner-join (lookup t.public.xy@y2)
 ├── columns: a:1(int!null) b:2(int!null) c:3(string) x:6(int!null) y:7(int!null)
 ├── flags: force lookup join (into right side)
 ├── key columns: [2] = [7]
 ├── stats: [rows=100, distinct(2)=1, null(2)=0, distinct(7)=1, null(7)=0]
 ├── cost: 427.600004
 ├── key: (1,6)
 ├── fd: ()-->(2,7), (1)-->(3), (2,3)~~>(1), (2)==(7), (7)==(2)
 ├── distribution: us
 ├── prune: (1,3,6)
 ├── interesting orderings: (+1 opt(2)) (+3,+1 opt(2)) (+6 opt(7))
 ├── scan t.public.abc@bc2
 │    ├── columns: t.public.abc.a:1(int!null) t.public.abc.b:2(int!null) t.public.abc.c:3(string)
 │    ├── constraint: /2/3: [/1 - /1]
 │    ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 │    ├── cost: 24.6200001
 │    ├── key: (1)
 │    ├── fd: ()-->(2), (1)-->(3), (2,3)~~>(1)
 │    ├── distribution: us
 │    ├── prune: (1,3)
 │    └── interesting orderings: (+1 opt(2)) (+3,+1 opt(2))
 └── filters
      └── eq [type=bool, outer=(7), constraints=(/7: [/1 - /1]; tight), fd=()-->(7)]
           ├── variable: t.public.xy.y:7 [type=int]
           └── const: 1 [type=int]

# Switch the data center for the target lookup join index.

exec-ddl
ALTER INDEX xy@y1 CONFIGURE ZONE USING constraints='[+region=us,+dc=west]'
----

exec-ddl
ALTER INDEX xy@y2 CONFIGURE ZONE USING constraints='[+region=us,+dc=east]'
----

# Should use other index now.
opt format=show-all locality=(region=us,dc=west)
SELECT * FROM abc INNER LOOKUP JOIN xy ON b=y WHERE b=1
----
inner-join (lookup t.public.xy@y1)
 ├── columns: a:1(int!null) b:2(int!null) c:3(string) x:6(int!null) y:7(int!null)
 ├── flags: force lookup join (into right side)
 ├── key columns: [2] = [7]
 ├── stats: [rows=100, distinct(2)=1, null(2)=0, distinct(7)=1, null(7)=0]
 ├── cost: 427.600004
 ├── key: (1,6)
 ├── fd: ()-->(2,7), (1)-->(3), (2,3)~~>(1), (2)==(7), (7)==(2)
 ├── distribution: us
 ├── prune: (1,3,6)
 ├── interesting orderings: (+1 opt(2)) (+3,+1 opt(2)) (+6 opt(7))
 ├── scan t.public.abc@bc2
 │    ├── columns: t.public.abc.a:1(int!null) t.public.abc.b:2(int!null) t.public.abc.c:3(string)
 │    ├── constraint: /2/3: [/1 - /1]
 │    ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 │    ├── cost: 24.6200001
 │    ├── key: (1)
 │    ├── fd: ()-->(2), (1)-->(3), (2,3)~~>(1)
 │    ├── distribution: us
 │    ├── prune: (1,3)
 │    └── interesting orderings: (+1 opt(2)) (+3,+1 opt(2))
 └── filters
      └── eq [type=bool, outer=(7), constraints=(/7: [/1 - /1]; tight), fd=()-->(7)]
           ├── variable: t.public.xy.y:7 [type=int]
           └── const: 1 [type=int]

# --------------------------------------------------
# Lease preferences - single constraint.
# --------------------------------------------------

exec-ddl
ALTER TABLE abc CONFIGURE ZONE USING lease_preferences='[[+region=central]]'
----

exec-ddl
ALTER INDEX abc@bc1 CONFIGURE ZONE USING lease_preferences='[[+region=east]]'
----

exec-ddl
ALTER INDEX abc@bc2 CONFIGURE ZONE USING lease_preferences='[[+region=west]]'
----

# With locality in us + central, use primary index.
opt format=show-all locality=(region=central)
SELECT * FROM abc
----
scan t.public.abc
 ├── columns: a:1(int!null) b:2(int) c:3(string)
 ├── stats: [rows=1000]
 ├── cost: 1125.02
 ├── key: (1)
 ├── fd: (1)-->(2,3), (2,3)~~>(1)
 ├── distribution: central
 ├── prune: (1-3)
 └── interesting orderings: (+1) (+2,+3,+1)

# With locality in us + central, still use bc1 index if filter is selective.
opt format=show-all locality=(region=central)
SELECT b, c FROM abc WHERE b=10
----
distribute
 ├── columns: b:2(int!null) c:3(string)
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 425.04
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: central
 ├── input distribution: east
 ├── prune: (3)
 ├── interesting orderings: (+3 opt(2))
 └── scan t.public.abc@bc1
      ├── columns: t.public.abc.b:2(int!null) t.public.abc.c:3(string)
      ├── constraint: /2/3: [/10 - /10]
      ├── stats: [rows=10, distinct(2)=1, null(2)=0]
      ├── cost: 225.02
      ├── lax-key: (3)
      ├── fd: ()-->(2)
      ├── prune: (3)
      └── interesting orderings: (+3 opt(2))

# With locality in east, use bc1 index.
opt format=show-all locality=(region=east)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.8533334
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: east
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# With locality in west, use bc2 index.
opt format=show-all locality=(region=west)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc2
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.8533334
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: west
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# --------------------------------------------------
# Lease preferences - multiple constraints.
# --------------------------------------------------

exec-ddl
ALTER TABLE abc CONFIGURE ZONE USING lease_preferences='[[+region=us,+dc=central,+rack=1]]'
----

exec-ddl
ALTER INDEX abc@bc1 CONFIGURE ZONE USING lease_preferences='[[+region=us,+dc=east,+rack=1]]'
----

exec-ddl
ALTER INDEX abc@bc2 CONFIGURE ZONE USING lease_preferences='[[+region=us,+dc=west,+rack=1]]'
----

# With locality in us + central, use primary index.
opt format=show-all locality=(region=us,dc=central)
SELECT * FROM abc
----
scan t.public.abc
 ├── columns: a:1(int!null) b:2(int) c:3(string)
 ├── stats: [rows=1000]
 ├── cost: 1125.02
 ├── key: (1)
 ├── fd: (1)-->(2,3), (2,3)~~>(1)
 ├── distribution: us
 ├── prune: (1-3)
 └── interesting orderings: (+1) (+2,+3,+1)

# With locality in us + central, still use bc1 index if filter is selective.
opt format=show-all locality=(region=us,dc=central)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.9366668
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# With locality in us + east, use bc1 index.
opt format=show-all locality=(region=us,dc=east)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.8533334
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# With locality in us + west, use bc2 index.
opt format=show-all locality=(region=us,dc=west)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc2
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.8533334
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# --------------------------------------------------
# Zone constraint + leaseholder preference.
# --------------------------------------------------

exec-ddl
ALTER TABLE abc CONFIGURE ZONE
USING constraints='[+region=us]', lease_preferences='[[+region=us,+dc=central]]'
----

exec-ddl
ALTER INDEX abc@bc1 CONFIGURE ZONE
USING constraints='[+region=us]', lease_preferences='[[+region=us,+dc=east]]'
----

exec-ddl
ALTER INDEX abc@bc2 CONFIGURE ZONE
USING constraints='[+region=us]', lease_preferences='[[+region=us,+dc=west]]'
----

# With locality in us + central, use primary index.
opt format=show-all locality=(region=us,dc=central)
SELECT * FROM abc
----
scan t.public.abc
 ├── columns: a:1(int!null) b:2(int) c:3(string)
 ├── stats: [rows=1000]
 ├── cost: 1104.82
 ├── key: (1)
 ├── fd: (1)-->(2,3), (2,3)~~>(1)
 ├── distribution: us
 ├── prune: (1-3)
 └── interesting orderings: (+1) (+2,+3,+1)

# With locality in us + central, still use bc1 index if filter is selective.
opt format=show-all locality=(region=us,dc=central)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.7700001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# With locality in us + east, use bc1 index.
opt format=show-all locality=(region=us,dc=east)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc1
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.6866668
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# With locality in us + west, use bc2 index.
opt format=show-all locality=(region=us,dc=west)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc2
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.6866668
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

exec-ddl
ALTER TABLE abc CONFIGURE ZONE USING constraints='[+region=us]'
----

exec-ddl
ALTER INDEX abc@bc1 CONFIGURE ZONE
USING constraints='[+region=us]', lease_preferences='[[+region=us,+dc=east]]'
----

exec-ddl
ALTER INDEX abc@bc2 CONFIGURE ZONE
USING constraints='[+region=us,+dc=east]'
----

# With locality in the east, prefer the index with the constraints over the
# index with just the lease preferences.
opt format=show-all locality=(region=us,dc=east)
SELECT b, c FROM abc WHERE b=10
----
scan t.public.abc@bc2
 ├── columns: b:2(int!null) c:3(string)
 ├── constraint: /2/3: [/10 - /10]
 ├── stats: [rows=10, distinct(2)=1, null(2)=0]
 ├── cost: 24.5200001
 ├── lax-key: (3)
 ├── fd: ()-->(2)
 ├── distribution: us
 ├── prune: (3)
 └── interesting orderings: (+3 opt(2))

# --------------------------------------------------
# Partition zones.
# --------------------------------------------------

exec-ddl
CREATE TABLE abc_part (
    r STRING NOT NULL CHECK (r IN ('east', 'west')),
    a INT PRIMARY KEY,
    b INT,
    c STRING,
    UNIQUE WITHOUT INDEX (b, c),
    UNIQUE INDEX bc_idx (r, b, c) PARTITION BY LIST (r) (
      PARTITION east VALUES IN (('east')),
      PARTITION west VALUES IN (('west'))
    ),
    INDEX b_idx (r, b) PARTITION BY LIST (r) (
      PARTITION east VALUES IN (('east')),
      PARTITION west VALUES IN (('west'))
    )
)
----

exec-ddl
ALTER PARTITION "east" OF INDEX abc_part@bc_idx CONFIGURE ZONE USING
  num_voters = 5,
  voter_constraints = '{+region=east: 2}',
  lease_preferences = '[[+region=east]]'
----

exec-ddl
ALTER PARTITION "west" OF INDEX abc_part@bc_idx CONFIGURE ZONE USING
  num_voters = 5,
  voter_constraints = '{+region=west: 2}',
  lease_preferences = '[[+region=west]]';
----

# We should prefer the locality optimized search here.
opt format=show-all locality=(region=east,dc=a)
SELECT * FROM abc_part WHERE b = 1 AND c = 'foo'
----
locality-optimized-search
 ├── columns: r:1(string!null) a:2(int!null) b:3(int!null) c:4(string!null)
 ├── left columns: t.public.abc_part.r:7(string) t.public.abc_part.a:8(int) t.public.abc_part.b:9(int) t.public.abc_part.c:10(string)
 ├── right columns: t.public.abc_part.r:13(string) t.public.abc_part.a:14(int) t.public.abc_part.b:15(int) t.public.abc_part.c:16(string)
 ├── cardinality: [0 - 1]
 ├── stats: [rows=0.91, distinct(3)=0.91, null(3)=0, distinct(4)=0.91, null(4)=0, distinct(3,4)=0.91, null(3,4)=0]
 ├── cost: 5.6885176
 ├── key: ()
 ├── fd: ()-->(1-4)
 ├── distribution: east
 ├── prune: (1,2)
 ├── scan t.public.abc_part@bc_idx
 │    ├── columns: t.public.abc_part.r:7(string!null) t.public.abc_part.a:8(int!null) t.public.abc_part.b:9(int!null) t.public.abc_part.c:10(string!null)
 │    ├── constraint: /7/9/10: [/'east'/1/'foo' - /'east'/1/'foo']
 │    ├── cardinality: [0 - 1]
 │    ├── stats: [rows=0.9001, distinct(7)=0.9001, null(7)=0, distinct(9)=0.9001, null(9)=0, distinct(10)=0.9001, null(10)=0, distinct(7,9,10)=0.9001, null(7,9,10)=0]
 │    ├── cost: 5.154016
 │    ├── key: ()
 │    ├── fd: ()-->(7-10)
 │    └── prune: (7-10)
 └── scan t.public.abc_part@bc_idx
      ├── columns: t.public.abc_part.r:13(string!null) t.public.abc_part.a:14(int!null) t.public.abc_part.b:15(int!null) t.public.abc_part.c:16(string!null)
      ├── constraint: /13/15/16: [/'west'/1/'foo' - /'west'/1/'foo']
      ├── cardinality: [0 - 1]
      ├── stats: [rows=0.9001, distinct(13)=0.9001, null(13)=0, distinct(15)=0.9001, null(15)=0, distinct(16)=0.9001, null(16)=0, distinct(13,15,16)=0.9001, null(13,15,16)=0]
      ├── cost: 5.154016
      ├── key: ()
      ├── fd: ()-->(13-16)
      └── prune: (13-16)

# We should prefer locality optimized anti join (a pair of nested anti joins).
opt locality=(region=east,dc=a)
SELECT * FROM abc_part AS a1 WHERE NOT EXISTS (
  SELECT * FROM abc_part AS a2 WHERE a1.a = a2.b
) AND b = 1 AND c = 'foo'
----
anti-join (lookup abc_part@bc_idx [as=a2])
 ├── columns: r:1!null a:2!null b:3!null c:4!null
 ├── lookup expression
 │    └── filters
 │         ├── a2.r:7 = 'west' [outer=(7), constraints=(/7: [/'west' - /'west']; tight), fd=()-->(7)]
 │         └── a1.a:2 = a2.b:9 [outer=(2,9), constraints=(/2: (/NULL - ]; /9: (/NULL - ]), fd=(2)==(9), (9)==(2)]
 ├── cardinality: [0 - 1]
 ├── stats: [rows=1e-10]
 ├── cost: 20.3643993
 ├── key: ()
 ├── fd: ()-->(1-4)
 ├── distribution: east
 ├── anti-join (lookup abc_part@bc_idx [as=a2])
 │    ├── columns: a1.r:1!null a1.a:2!null a1.b:3!null a1.c:4!null
 │    ├── lookup expression
 │    │    └── filters
 │    │         ├── a2.r:7 = 'east' [outer=(7), constraints=(/7: [/'east' - /'east']; tight), fd=()-->(7)]
 │    │         └── a1.a:2 = a2.b:9 [outer=(2,9), constraints=(/2: (/NULL - ]; /9: (/NULL - ]), fd=(2)==(9), (9)==(2)]
 │    ├── cardinality: [0 - 1]
 │    ├── stats: [rows=0.9009, distinct(2)=0.9009, null(2)=0]
 │    ├── cost: 13.0632823
 │    ├── key: ()
 │    ├── fd: ()-->(1-4)
 │    ├── distribution: east
 │    ├── locality-optimized-search
 │    │    ├── columns: a1.r:1!null a1.a:2!null a1.b:3!null a1.c:4!null
 │    │    ├── left columns: a1.r:14 a1.a:15 a1.b:16 a1.c:17
 │    │    ├── right columns: a1.r:20 a1.a:21 a1.b:22 a1.c:23
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── stats: [rows=0.91, distinct(2)=0.91, null(2)=0, distinct(3)=0.91, null(3)=0, distinct(4)=0.91, null(4)=0, distinct(3,4)=0.91, null(3,4)=0]
 │    │    ├── cost: 5.6885176
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(1-4)
 │    │    ├── distribution: east
 │    │    ├── scan abc_part@bc_idx [as=a1]
 │    │    │    ├── columns: a1.r:14!null a1.a:15!null a1.b:16!null a1.c:17!null
 │    │    │    ├── constraint: /14/16/17: [/'east'/1/'foo' - /'east'/1/'foo']
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── stats: [rows=0.9001, distinct(14)=0.9001, null(14)=0, distinct(16)=0.9001, null(16)=0, distinct(17)=0.9001, null(17)=0, distinct(14,16,17)=0.9001, null(14,16,17)=0]
 │    │    │    ├── cost: 5.154016
 │    │    │    ├── key: ()
 │    │    │    └── fd: ()-->(14-17)
 │    │    └── scan abc_part@bc_idx [as=a1]
 │    │         ├── columns: a1.r:20!null a1.a:21!null a1.b:22!null a1.c:23!null
 │    │         ├── constraint: /20/22/23: [/'west'/1/'foo' - /'west'/1/'foo']
 │    │         ├── cardinality: [0 - 1]
 │    │         ├── stats: [rows=0.9001, distinct(20)=0.9001, null(20)=0, distinct(22)=0.9001, null(22)=0, distinct(23)=0.9001, null(23)=0, distinct(20,22,23)=0.9001, null(20,22,23)=0]
 │    │         ├── cost: 5.154016
 │    │         ├── key: ()
 │    │         └── fd: ()-->(20-23)
 │    └── filters (true)
 └── filters (true)
