# This file tests that the OptTester flag perturb-cost works. It's not possible
# to include tests with the opt directive (other than for trivial scalar
# queries), since by construction those tests will produce a random query plan
# and we cannot predict the output in advance. For example,
#   `SELECT * FROM a JOIN b ON a.x=b.x`
# may produce one of at least 6 different plans.

# For this reason, the perturb-cost flag is only intended for debugging at this
# time, by using the "-rewrite=true" flag.

exec-ddl
CREATE TABLE a (x INT PRIMARY KEY, y INT)
----
TABLE a
 ├── x int not null
 ├── y int
 └── INDEX primary
      └── x int not null

exec-ddl
CREATE TABLE b (x INT PRIMARY KEY)
----
TABLE b
 ├── x int not null
 └── INDEX primary
      └── x int not null

norm perturb-cost=(0.5)
SELECT * FROM a JOIN b ON a.x=b.x ORDER BY a.y
----
sort
 ├── columns: x:1(int!null) y:2(int) x:3(int!null)
 ├── stats: [rows=1000, distinct(1)=1000, null(1)=0, distinct(3)=1000, null(3)=0]
 ├── cost: 2319.37569
 ├── key: (3)
 ├── fd: (1)-->(2), (1)==(3), (3)==(1)
 ├── ordering: +2
 └── inner-join
      ├── columns: a.x:1(int!null) y:2(int) b.x:3(int!null)
      ├── stats: [rows=1000, distinct(1)=1000, null(1)=0, distinct(3)=1000, null(3)=0]
      ├── cost: 2100.05
      ├── key: (3)
      ├── fd: (1)-->(2), (1)==(3), (3)==(1)
      ├── scan a
      │    ├── columns: a.x:1(int!null) y:2(int)
      │    ├── stats: [rows=1000, distinct(1)=1000, null(1)=0]
      │    ├── cost: 1040.02
      │    ├── key: (1)
      │    └── fd: (1)-->(2)
      ├── scan b
      │    ├── columns: b.x:3(int!null)
      │    ├── stats: [rows=1000, distinct(3)=1000, null(3)=0]
      │    ├── cost: 1020.02
      │    └── key: (3)
      └── filters
           └── a.x = b.x [type=bool, outer=(1,3), constraints=(/1: (/NULL - ]; /3: (/NULL - ]), fd=(1)==(3), (3)==(1)]

opt perturb-cost=(0.9)
SELECT 1
----
values
 ├── columns: "?column?":1(int)
 ├── cardinality: [1 - 1]
 ├── stats: [rows=1]
 ├── cost: 0.02
 ├── key: ()
 ├── fd: ()-->(1)
 └── (1,) [type=tuple{int}]

opt perturb-cost=(2.5)
SELECT 1
----
values
 ├── columns: "?column?":1(int)
 ├── cardinality: [1 - 1]
 ├── stats: [rows=1]
 ├── cost: 0.02
 ├── key: ()
 ├── fd: ()-->(1)
 └── (1,) [type=tuple{int}]
