# LogicTest: local

query IITTITTTII colnames
SELECT * FROM crdb_internal.partitions
----
table_id  index_id  parent_name  name  columns column_names list_value range_value zone_id subzone_id

statement ok
CREATE TABLE t1 (
  a INT,
  b INT,
  c INT,
  PRIMARY KEY (a, b, c),
  INDEX (a, b) PARTITION BY LIST (a, b) (
    PARTITION p00 VALUES IN ((0, 0))
  )
) PARTITION BY LIST (a) (
    PARTITION p12 VALUES IN (1, 2) PARTITION BY LIST (b) (
        PARTITION p12p3 VALUES IN (3) PARTITION BY LIST (c) (
            PARTITION p12p3p8 VALUES IN (8)
        ),
        PARTITION pd VALUES IN (DEFAULT)
    ),
    PARTITION p6 VALUES IN (6) PARTITION BY RANGE (b) (
        PARTITION p6p7 VALUES FROM (MINVALUE) TO (7),
        PARTITION p6p8 VALUES FROM (7) TO (8),
        PARTITION p6px VALUES FROM (8) TO (MAXVALUE)
    )
)

statement ok
CREATE table t2 (a STRING PRIMARY KEY) PARTITION BY LIST (a) (
  PARTITION pfoo VALUES IN ('foo')
)

# Since there are no zone configurations on any of these partitions, tables,
# or databases, these partitions inherit directly from the default config.
skipif backup-restore
query IITTITTTII
SELECT * FROM crdb_internal.partitions ORDER BY table_id, index_id, name
----
106  1  NULL   p12      1  a     (1), (2)   NULL               0  0
106  1  p12    p12p3    1  b     (3)        NULL               0  0
106  1  p12p3  p12p3p8  1  c     (8)        NULL               0  0
106  1  NULL   p6       1  a     (6)        NULL               0  0
106  1  p6     p6p7     1  b     NULL       (MINVALUE) TO (7)  0  0
106  1  p6     p6p8     1  b     NULL       (7) TO (8)         0  0
106  1  p6     p6px     1  b     NULL       (8) TO (MAXVALUE)  0  0
106  1  p12    pd       1  b     (DEFAULT)  NULL               0  0
106  2  NULL   p00      2  a, b  (0, 0)     NULL               0  0
107  1  NULL   pfoo     1  a     ('foo')    NULL               0  0

# Test crdb_internal.zones functions correctly on zoned indexes.
subtest privileged_zones_test

statement ok
CREATE DATABASE db2; ALTER DATABASE db2 CONFIGURE ZONE USING num_replicas = 3;
REVOKE CONNECT ON DATABASE db2 FROM public

statement ok
CREATE TABLE t3 (a INT PRIMARY KEY, b INT); CREATE INDEX myt3index ON t3 (b); ALTER INDEX myt3index CONFIGURE ZONE USING num_replicas = 5; ALTER TABLE t3 CONFIGURE ZONE USING num_replicas = 8

statement ok
CREATE TABLE t4 (a INT PRIMARY KEY, b INT); CREATE INDEX myt4index ON t4 (b); ALTER TABLE t4 CONFIGURE ZONE USING num_replicas = 7; ALTER INDEX myt4index CONFIGURE ZONE USING num_replicas = 5; GRANT ALL ON t4 TO testuser

user testuser

skipif backup-restore
query IT
SELECT zone_id, target FROM crdb_internal.zones ORDER BY 1
----
0    RANGE default
16   RANGE meta
17   RANGE system
18   RANGE timeseries
22   RANGE liveness
111  TABLE test.public.t4
111  INDEX test.public.t4@myt4index

query TT
SELECT * FROM [SHOW ALL ZONE CONFIGURATIONS] ORDER BY 1
----
INDEX test.public.t4@myt4index  ALTER INDEX test.public.t4@myt4index CONFIGURE ZONE USING
                                  num_replicas = 5
RANGE default                   ALTER RANGE default CONFIGURE ZONE USING
                                  range_min_bytes = 134217728,
                                  range_max_bytes = 536870912,
                                  gc.ttlseconds = 14400,
                                  num_replicas = 3,
                                  constraints = '[]',
                                  lease_preferences = '[]'
RANGE liveness                  ALTER RANGE liveness CONFIGURE ZONE USING
                                  range_min_bytes = 134217728,
                                  range_max_bytes = 536870912,
                                  gc.ttlseconds = 600,
                                  num_replicas = 5,
                                  constraints = '[]',
                                  lease_preferences = '[]'
RANGE meta                      ALTER RANGE meta CONFIGURE ZONE USING
                                  range_min_bytes = 134217728,
                                  range_max_bytes = 536870912,
                                  gc.ttlseconds = 3600,
                                  num_replicas = 5,
                                  constraints = '[]',
                                  lease_preferences = '[]'
RANGE system                    ALTER RANGE system CONFIGURE ZONE USING
                                  range_min_bytes = 134217728,
                                  range_max_bytes = 536870912,
                                  gc.ttlseconds = 14400,
                                  num_replicas = 5,
                                  constraints = '[]',
                                  lease_preferences = '[]'
RANGE timeseries                ALTER RANGE timeseries CONFIGURE ZONE USING
                                  gc.ttlseconds = 14400
TABLE test.public.t4            ALTER TABLE test.public.t4 CONFIGURE ZONE USING
                                  num_replicas = 7

query error pq: user testuser has no privileges on database db2
SHOW ZONE CONFIGURATION FOR DATABASE db2

query error pq: user testuser has no privileges on relation t2
SHOW ZONE CONFIGURATION FOR TABLE t2

query error pq: user testuser has no privileges on relation t3
SHOW ZONE CONFIGURATION FOR TABLE t3

query error pq: user testuser has no privileges on relation t3
SHOW ZONE CONFIGURATION FOR INDEX myt3index

query TT
SHOW ZONE CONFIGURATION FOR INDEX myt4index
----
INDEX test.public.t4@myt4index  ALTER INDEX test.public.t4@myt4index CONFIGURE ZONE USING
                                  range_min_bytes = 134217728,
                                  range_max_bytes = 536870912,
                                  gc.ttlseconds = 14400,
                                  num_replicas = 5,
                                  constraints = '[]',
                                  lease_preferences = '[]'

query TT
SHOW ZONE CONFIGURATION FOR TABLE t4
----
TABLE t4  ALTER TABLE t4 CONFIGURE ZONE USING
            range_min_bytes = 134217728,
            range_max_bytes = 536870912,
            gc.ttlseconds = 14400,
            num_replicas = 7,
            constraints = '[]',
            lease_preferences = '[]'

query TT
SHOW ZONE CONFIGURATION FOR RANGE default
----
RANGE default  ALTER RANGE default CONFIGURE ZONE USING
                 range_min_bytes = 134217728,
                 range_max_bytes = 536870912,
                 gc.ttlseconds = 14400,
                 num_replicas = 3,
                 constraints = '[]',
                 lease_preferences = '[]'

# This test checks that generator backed tables do not concurrently
# access transactions. It does this by scanning two virtual tables at a time.
# If the background generator functions were performing work when not allowed
# to, the background generators would conflict with an error.
statement ok
SELECT a.* FROM crdb_internal.partitions AS a JOIN crdb_internal.partitions AS b ON a.table_id = b.table_id

subtest replication-builtins

user root


query error pq: crdb_internal\.stream_ingestion_stats_json\(\): unimplemented
SELECT crdb_internal.stream_ingestion_stats_json(1);

query error pq: crdb_internal\.stream_ingestion_stats_pb\(\): unimplemented
SELECT crdb_internal.stream_ingestion_stats_pb(1);

subtest node_tenant_capabilities_cache

user testuser

statement error user testuser does not have VIEWCLUSTERMETADATA system privilege
SELECT * FROM crdb_internal.node_tenant_capabilities_cache

user root

statement ok
SELECT crdb_internal.create_tenant(5)

# Use retry because source data is eventually consistent.
query ITT colnames,retry,rowsort
SELECT * FROM crdb_internal.node_tenant_capabilities_cache WHERE capability_name = 'can_view_node_info'
----
tenant_id  capability_name     capability_value
1          can_view_node_info  true
5          can_view_node_info  false

statement ok
ALTER TENANT [5] GRANT CAPABILITY can_view_node_info

# Use retry because source data is eventually consistent.
query ITT colnames,retry,rowsort
SELECT * FROM crdb_internal.node_tenant_capabilities_cache WHERE capability_name = 'can_view_node_info'
----
tenant_id  capability_name  capability_value
1          can_view_node_info  true
5          can_view_node_info  true

subtest end
