# LogicTest: 5node-dist

# Disable automatic stats.
statement ok
SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false

# Check that vectorize row count threshold is respected. The test relies on the
# fact that DistSQL and vectorized execution engines output execution stats in
# a different format.
statement ok
CREATE TABLE small (a INT PRIMARY KEY)

statement ok
SET vectorize_row_count_threshold = 1000

# There are no stats available, so this should run through the row execution
# engine.
query TTT
EXPLAIN SELECT count(*) from small
----
·          distributed  true
·          vectorized   false
group      ·            ·
 │         aggregate 0  count_rows()
 │         scalar       ·
 └── scan  ·            ·
·          table        small@primary
·          spans        ALL

statement ok
SET vectorize_row_count_threshold = 0

# This should run through the vectorized execution engine because we disabled
# the threshold.
query TTT
EXPLAIN SELECT count(*) from small
----
·          distributed  true
·          vectorized   true
group      ·            ·
 │         aggregate 0  count_rows()
 │         scalar       ·
 └── scan  ·            ·
·          table        small@primary
·          spans        ALL

statement ok
SET vectorize_row_count_threshold = 1000

statement ok
ALTER TABLE small INJECT STATISTICS '[
  {
    "columns": ["a"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 100,
    "distinct_count": 100
  }
]'

# This should run through the row execution engine.
query TTT
EXPLAIN SELECT count(*) from small
----
·          distributed  true
·          vectorized   false
group      ·            ·
 │         aggregate 0  count_rows()
 │         scalar       ·
 └── scan  ·            ·
·          table        small@primary
·          spans        ALL

statement ok
SET vectorize_row_count_threshold = 1

# This should run through the vectorized execution engine because we lowered
# the threshold.
query TTT
EXPLAIN SELECT count(*) from small
----
·          distributed  true
·          vectorized   true
group      ·            ·
 │         aggregate 0  count_rows()
 │         scalar       ·
 └── scan  ·            ·
·          table        small@primary
·          spans        ALL

statement ok
SET vectorize_row_count_threshold = 1000

statement ok
CREATE TABLE large (a INT PRIMARY KEY)

statement ok
ALTER TABLE large INJECT STATISTICS '[
  {
    "columns": ["a"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 100000,
    "distinct_count": 100000
  }
]'

# This should run through the vectorized execution engine.
query TTT
EXPLAIN SELECT count(*) from large
----
·          distributed  true
·          vectorized   true
group      ·            ·
 │         aggregate 0  count_rows()
 │         scalar       ·
 └── scan  ·            ·
·          table        large@primary
·          spans        ALL

statement ok
SET vectorize_row_count_threshold = 1000000

# This should run through the row execution engine because we increased the
# threshold.
query TTT
EXPLAIN SELECT count(*) from large
----
·          distributed  true
·          vectorized   false
group      ·            ·
 │         aggregate 0  count_rows()
 │         scalar       ·
 └── scan  ·            ·
·          table        large@primary
·          spans        ALL
