Skip to content

Commit

Permalink
sql/logictest: require sort option or ORDER BY
Browse files Browse the repository at this point in the history
A logic test using the `query` directive now fails if at least one of
the following is not true:

  - The query returns zero or one row.
  - The query returns multiple rows that are all identical.
  - The directive has a sort option (`rowsort`, `valuesort`,
    `partialsort`, or `nosort`) or the `kvtrace` option.
  - The query has an `ORDER BY` clause.
  - The query has `EXPLAIN` or `SHOW [KV] TRACE` in it.

This change will help prevent tests that flake due to a
non-deterministic order of the results. This is one of the most common
causes of flakes in logic tests.

Note that the check for `ORDER BY` is a simple regex search, so it will
not prevent all possible flakes due to ordering. As one example, an
`ORDER BY` in a subquery will satisfy the check, even though it does not
guarantee the ordering of the result of the main query:

    SELECT * FROM (SELECT * FROM t ORDER BY a)

Release note: None
  • Loading branch information
mgartner committed Jun 20, 2023
1 parent 4e6dc2e commit d93923b
Show file tree
Hide file tree
Showing 185 changed files with 1,542 additions and 1,450 deletions.
13 changes: 8 additions & 5 deletions pkg/ccl/logictestccl/testdata/logic_test/cluster_locks_tenant
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ user_name query phase
testuser SELECT * FROM t executing

# looking at each transaction separately, validate the expected results in the lock table
query TTTTTTBB colnames,retry
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn1'
query TTTTTTBB colnames,rowsort,retry
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended
FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn1'
----
database_name schema_name table_name lock_key_pretty lock_strength durability granted contended
test public t /Table/106/1/"b"/0 Exclusive Replicated true true
Expand All @@ -87,16 +88,18 @@ test public t /Table/106/1/"s"/0 Exclusive Replic
test public t /Table/106/1/"t"/0 Exclusive Replicated true false

query TTTTTTBB colnames
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn2'
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended
FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn2'
----
database_name schema_name table_name lock_key_pretty lock_strength durability granted contended
test public t /Table/106/1/"b"/0 None Replicated false true

# check that we can't see keys, potentially revealing PII, with VIEWACTIVITYREDACTED
user testuser2

query TTTTTTBB colnames
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn1'
query TTTTTTBB colnames,rowsort
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended
FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn1'
----
database_name schema_name table_name lock_key_pretty lock_strength durability granted contended
test public t · Exclusive Replicated true true
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/logictestccl/testdata/logic_test/crdb_internal
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ statement ok
ALTER TENANT [5] GRANT CAPABILITY can_admin_split

# Use retry because source data is eventually consistent.
query ITT colnames,retry
query ITT colnames,retry,rowsort
SELECT * FROM crdb_internal.node_tenant_capabilities_cache WHERE capability_name = 'can_admin_split'
----
tenant_id capability_name capability_value
Expand Down
10 changes: 5 additions & 5 deletions pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CREATE TABLE crdb_internal.t (x INT)
query error database "crdb_internal" does not exist
DROP DATABASE crdb_internal

query TTTTIT
query TTTTIT rowsort
SHOW TABLES FROM crdb_internal
----
crdb_internal active_range_feeds table admin NULL NULL
Expand Down Expand Up @@ -169,7 +169,7 @@ table_id parent_id name database_name version format_version
statement ok
CREATE TABLE testdb." ""\'" (i int)

query T
query T rowsort
SELECT NAME from crdb_internal.tables WHERE DATABASE_NAME = 'testdb'
----
foo
Expand Down Expand Up @@ -399,7 +399,7 @@ select regexp_replace(regexp_replace(crdb_internal.node_executable_version()::st
----
23.1

query ITTT colnames
query ITTT colnames,rowsort
select node_id, component, field, regexp_replace(regexp_replace(value, '^\d+$', '<port>'), e':\\d+', ':<port>') as value from crdb_internal.node_runtime_info
----
node_id component field value
Expand Down Expand Up @@ -597,7 +597,7 @@ CREATE TYPE enum1 AS ENUM ('hello', 'hi');
CREATE TYPE enum2 AS ENUM ()

query ITTITTT
SELECT * FROM crdb_internal.create_type_statements
SELECT * FROM crdb_internal.create_type_statements ORDER BY descriptor_id
----
104 test public 113 enum1 CREATE TYPE public.enum1 AS ENUM ('hello', 'hi') {hello,hi}
104 test public 115 enum2 CREATE TYPE public.enum2 AS ENUM () {}
Expand Down Expand Up @@ -625,7 +625,7 @@ BEGIN; SELECT * FROM t_53504; COMMIT;
statement ok
SELECT * FROM t_53504

query ITTTI colnames
query ITTTI colnames,rowsort
SELECT node_id, application_name, key, statement_ids, count FROM crdb_internal.node_transaction_statistics where application_name = 'test_txn_statistics'
----
node_id application_name key statement_ids count
Expand Down
22 changes: 11 additions & 11 deletions pkg/ccl/logictestccl/testdata/logic_test/distsql_partitioning
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ALTER TABLE t1 PARTITION BY LIST (x) (
)

query TTTTTTTTT colnames
SHOW PARTITIONS FROM DATABASE test
SELECT * FROM [SHOW PARTITIONS FROM DATABASE test] ORDER BY partition_name
----
database_name table_name partition_name parent_partition column_names index_name partition_value zone_config full_zone_config
test t1 p1 NULL x t1@t1_pkey (1) NULL range_min_bytes = 134217728,
Expand Down Expand Up @@ -41,7 +41,7 @@ ALTER PARTITION p2 OF TABLE t1 CONFIGURE ZONE USING constraints='[+dc=dc2]';
ALTER PARTITION p3 OF TABLE t1 CONFIGURE ZONE USING constraints='[+dc=dc3]'

query TTTTTTTTT colnames
SHOW PARTITIONS FROM DATABASE test
SELECT * FROM [SHOW PARTITIONS FROM DATABASE test] ORDER BY partition_name
----
database_name table_name partition_name parent_partition column_names index_name partition_value zone_config full_zone_config
test t1 p1 NULL x t1@t1_pkey (1) constraints = '[+dc=dc1]' range_min_bytes = 134217728,
Expand All @@ -64,7 +64,7 @@ test t1 p3 NULL x t1@t1
lease_preferences = '[]'

query TTTTTTTTT
SHOW PARTITIONS FROM TABLE t1
SELECT * FROM [SHOW PARTITIONS FROM TABLE t1] ORDER BY partition_name
----
test t1 p1 NULL x t1@t1_pkey (1) constraints = '[+dc=dc1]' range_min_bytes = 134217728,
range_max_bytes = 536870912,
Expand All @@ -86,7 +86,7 @@ test t1 p3 NULL x t1@t1_pkey (3) constraints = '[+dc=dc3]' range_min_byt
lease_preferences = '[]'

query TTTTTTTTT
SHOW PARTITIONS FROM INDEX t1@t1_pkey
SELECT * FROM [SHOW PARTITIONS FROM INDEX t1@t1_pkey] ORDER BY partition_name
----
test t1 p1 NULL x t1@t1_pkey (1) constraints = '[+dc=dc1]' range_min_bytes = 134217728,
range_max_bytes = 536870912,
Expand Down Expand Up @@ -121,7 +121,7 @@ ALTER PARTITION p1 OF TABLE t2 CONFIGURE ZONE USING constraints='[+dc=dc1]';
ALTER PARTITION p2 OF TABLE t2 CONFIGURE ZONE USING constraints='[+dc=dc2]'

query TTTTTTTTT
SHOW PARTITIONS FROM DATABASE test
SELECT * FROM [SHOW PARTITIONS FROM DATABASE test] ORDER BY table_name, partition_name
----
test t1 p1 NULL x t1@t1_pkey (1) constraints = '[+dc=dc1]' range_min_bytes = 134217728,
range_max_bytes = 536870912,
Expand Down Expand Up @@ -155,7 +155,7 @@ test t2 p2 NULL x t2@t2_pkey (2) TO (3) constraints = '[+dc=dc2]' range_
lease_preferences = '[]'

query TTTTTTTTT
SHOW PARTITIONS FROM TABLE t2
SELECT * FROM [SHOW PARTITIONS FROM TABLE t2] ORDER BY partition_name
----
test t2 p1 NULL x t2@t2_pkey (1) TO (2) constraints = '[+dc=dc1]' range_min_bytes = 134217728,
range_max_bytes = 536870912,
Expand All @@ -171,7 +171,7 @@ test t2 p2 NULL x t2@t2_pkey (2) TO (3) constraints = '[+dc=dc2]' range_
lease_preferences = '[]'

query TTTTTTTTT
SHOW PARTITIONS FROM INDEX t2@t2_pkey
SELECT * FROM [SHOW PARTITIONS FROM INDEX t2@t2_pkey] ORDER BY partition_name
----
test t2 p1 NULL x t2@t2_pkey (1) TO (2) constraints = '[+dc=dc1]' range_min_bytes = 134217728,
range_max_bytes = 536870912,
Expand Down Expand Up @@ -208,7 +208,7 @@ ALTER PARTITION p3 OF INDEX t3@sec CONFIGURE ZONE USING constraints='[+dc=dc3]';
ALTER PARTITION p4 OF INDEX t3@sec CONFIGURE ZONE USING constraints='[+dc=dc4]'

query TTTTTTTTT
SHOW PARTITIONS FROM TABLE t3
SELECT * FROM [SHOW PARTITIONS FROM TABLE t3] ORDER BY partition_name
----
test t3 p1 NULL x t3@t3_pkey (1) constraints = '[+dc=dc1]' range_min_bytes = 134217728,
range_max_bytes = 536870912,
Expand Down Expand Up @@ -236,7 +236,7 @@ test t3 p4 NULL y t3@sec (4) constraints = '[+dc=dc4]' range_min_byt
lease_preferences = '[]'

query TTTTTTTTT
SHOW PARTITIONS FROM INDEX t3@*
SELECT * FROM [SHOW PARTITIONS FROM INDEX t3@*] ORDER BY partition_name
----
test t3 p1 NULL x t3@t3_pkey (1) constraints = '[+dc=dc1]' range_min_bytes = 134217728,
range_max_bytes = 536870912,
Expand Down Expand Up @@ -264,7 +264,7 @@ test t3 p4 NULL y t3@sec (4) constraints = '[+dc=dc4]' range_min_byt
lease_preferences = '[]'

query TTTTTTTTT
SHOW PARTITIONS FROM INDEX t3@sec
SELECT * FROM [SHOW PARTITIONS FROM INDEX t3@sec] ORDER BY partition_name
----
test t3 p3 NULL y t3@sec (3) constraints = '[+dc=dc3]' range_min_bytes = 134217728,
range_max_bytes = 536870912,
Expand Down Expand Up @@ -301,7 +301,7 @@ ALTER PARTITION p2 OF TABLE t4 CONFIGURE ZONE USING constraints='[+dc=dc4]';
ALTER PARTITION p2_a OF TABLE t4 CONFIGURE ZONE USING constraints='[+dc=dc5]'

query TTTTTTTTT
SHOW PARTITIONS FROM TABLE t4
SELECT * FROM [SHOW PARTITIONS FROM TABLE t4] ORDER BY partition_name
----
test t4 p1 NULL x t4@t4_pkey (1) constraints = '[+dc=dc1]' range_min_bytes = 134217728,
range_max_bytes = 536870912,
Expand Down
38 changes: 19 additions & 19 deletions pkg/ccl/logictestccl/testdata/logic_test/multi_region
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# knob-opt: sync-event-log
# LogicTest: multiregion-9node-3region-3azs multiregion-9node-3region-3azs-tenant multiregion-9node-3region-3azs-no-los

query TTTTT colnames
query TTTTT colnames,rowsort
SHOW REGIONS
----
region zones database_names primary_region_of secondary_region_of
ap-southeast-2 {ap-az1,ap-az2,ap-az3} {} {} {}
ca-central-1 {ca-az1,ca-az2,ca-az3} {} {} {}
us-east-1 {us-az1,us-az2,us-az3} {} {} {}

query TT colnames
query TT colnames,rowsort
SHOW REGIONS FROM CLUSTER
----
region zones
Expand Down Expand Up @@ -166,7 +166,7 @@ CREATE DATABASE invalid_region_db PRIMARY REGION "region_no_exists" REGION "regi
statement ok
CREATE DATABASE multi_region_test_survive_zone_failure_db PRIMARY REGION "us-east-1" REGIONS "ap-southeast-2", "ca-central-1", "us-east-1" SURVIVE ZONE FAILURE

query TTTTTT colnames
query TTTTTT colnames,rowsort
SHOW DATABASES
----
database_name owner primary_region secondary_region regions survival_goal
Expand All @@ -179,7 +179,7 @@ region_test_db root ap-southeast-2 ·
system node NULL NULL {} NULL
test root NULL NULL {} NULL

query TTTT colnames
query TTTT colnames,rowsort
SHOW REGIONS FROM ALL DATABASES
----
database_name regions primary_region secondary_region
Expand All @@ -195,15 +195,15 @@ test {}
statement ok
USE multi_region_test_db

query TTBBT colnames
query TTBBT colnames,rowsort
SHOW REGIONS FROM DATABASE
----
database region primary secondary zones
multi_region_test_db ca-central-1 true false {ca-az1,ca-az2,ca-az3}
multi_region_test_db ap-southeast-2 false false {ap-az1,ap-az2,ap-az3}
multi_region_test_db us-east-1 false false {us-az1,us-az2,us-az3}

query TTTTT colnames
query TTTTT colnames,rowsort
SHOW REGIONS
----
region zones database_names primary_region_of secondary_region_of
Expand Down Expand Up @@ -509,7 +509,7 @@ CREATE INDEX bad_idx ON global_table(a) PARTITION BY LIST (a) (
)

query TTTTIT colnames
SHOW TABLES
SELECT * FROM [SHOW TABLES] ORDER BY table_name
----
schema_name table_name type owner estimated_row_count locality
public global_table table root 0 GLOBAL
Expand Down Expand Up @@ -569,7 +569,7 @@ public crdb_internal_region {ca-central-1} root
statement ok
ALTER DATABASE alter_test_db ADD REGION "ap-southeast-2"

query TTBBT colnames
query TTBBT colnames,rowsort
show regions from database alter_test_db
----
database region primary secondary zones
Expand Down Expand Up @@ -600,7 +600,7 @@ query T noticetrace
ALTER DATABASE alter_test_db ADD REGION IF NOT EXISTS "us-east-1"
----

query TTBBT colnames
query TTBBT colnames,rowsort
show regions from database alter_test_db
----
database region primary secondary zones
Expand Down Expand Up @@ -645,7 +645,7 @@ ALTER DATABASE new_db ADD REGION "us-west-1"
statement error pq: database has no regions to drop
ALTER DATABASE new_db DROP REGION "us-west-1"

query TTBBT colnames
query TTBBT colnames,rowsort
show regions from database alter_test_db
----
database region primary secondary zones
Expand Down Expand Up @@ -750,7 +750,7 @@ SHOW ENUMS FROM alter_primary_region_db.public
schema name values owner
public crdb_internal_region {ap-southeast-2,ca-central-1} root

query TTBBT colnames
query TTBBT colnames,rowsort
show regions from database alter_primary_region_db
----
database region primary secondary zones
Expand Down Expand Up @@ -779,7 +779,7 @@ SHOW ENUMS FROM alter_primary_region_db.public
schema name values owner
public crdb_internal_region {ap-southeast-2,ca-central-1} root

query TTBBT colnames
query TTBBT colnames,rowsort
show regions from database alter_primary_region_db
----
database region primary secondary zones
Expand Down Expand Up @@ -854,7 +854,7 @@ TABLE t_global ALTER TABLE t_global CONFIGURE ZONE USING
voter_constraints = '[+region=ca-central-1]',
lease_preferences = '[[+region=ca-central-1]]'

query TTTTTT colnames
query TTTTTT colnames,rowsort
SHOW DATABASES
----
database_name owner primary_region secondary_region regions survival_goal
Expand Down Expand Up @@ -887,7 +887,7 @@ DATABASE alter_survive_db ALTER DATABASE alter_survive_db CONFIGURE ZONE USING
statement ok
alter database alter_survive_db survive region failure

query TTTTTT colnames
query TTTTTT colnames,rowsort
SHOW DATABASES
----
database_name owner primary_region secondary_region regions survival_goal
Expand Down Expand Up @@ -947,7 +947,7 @@ TABLE t_global ALTER TABLE t_global CONFIGURE ZONE USING
statement ok
alter database alter_survive_db survive zone failure

query TTTTTT colnames
query TTTTTT colnames,rowsort
SHOW DATABASES
----
database_name owner primary_region secondary_region regions survival_goal
Expand Down Expand Up @@ -1098,7 +1098,7 @@ NOTICE: region "us-east-1" is not defined on the database; skipping
statement error pgcode 42704 region "non-existent-region" has not been added to the database
ALTER DATABASE drop_region_db DROP REGION "non-existent-region"

query TTBBT colnames
query TTBBT colnames,rowsort
SHOW REGIONS FROM DATABASE drop_region_db
----
database region primary secondary zones
Expand Down Expand Up @@ -1182,7 +1182,7 @@ ALTER DATABASE start_off_non_multi_region DROP REGION "ca-central-1"
statement ok
CREATE DATABASE txn_database_drop_regions PRIMARY REGION "ca-central-1" REGIONS "ap-southeast-2", "us-east-1"

query TTBBT colnames
query TTBBT colnames,rowsort
SHOW REGIONS FROM DATABASE txn_database_drop_regions
----
database region primary secondary zones
Expand Down Expand Up @@ -1541,7 +1541,7 @@ CREATE TABLE public.t_as (
CONSTRAINT t_as_pkey PRIMARY KEY (rowid ASC)
) LOCALITY REGIONAL BY ROW AS crdb_region

query TI colnames
query TI colnames,rowsort
SELECT crdb_region, i FROM t_as
----
crdb_region i
Expand All @@ -1553,7 +1553,7 @@ statement ok
INSERT INTO t_as VALUES (4)

# Validate that hidden column works
query TI colnames
query TI colnames,rowsort
SELECT crdb_region, i FROM t_as
----
crdb_region i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ SELECT status, error FROM [SHOW JOB WHEN COMPLETE $job_id]
----
succeeded ·

query TI
query TI rowsort
SELECT crdb_region, * FROM t
----
ap-southeast-2 1
Expand Down Expand Up @@ -79,7 +79,7 @@ SELECT status, error FROM [SHOW JOB WHEN COMPLETE $job_id]
----
succeeded ·

query I
query I rowsort
SELECT * FROM t
----
1
Expand Down
Loading

0 comments on commit d93923b

Please sign in to comment.