Files
greptimedb/tests/cases/standalone/common/aggregate/append_count_nulls.sql
T
dennis zhuang 5cfdc30a69 fix(table): stop claiming exact null and distinct counts for append scans (#9593)
RegionScanExec reported null_count = 0 and distinct_count = total_rows for
every column of an append-only table. DataFusion answers count(col) and
count(DISTINCT col) from these statistics, so both returned the row count.
Keep the exact row count and an exact zero null count for non-nullable
columns, so count(*) is still answered from statistics.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
2026-10-15 03:45:48 +00:00

68 lines
1.8 KiB
SQL

-- count(col) and count(DISTINCT col) on append-only tables must not be answered
-- from row-count statistics, while count(*) still is.
CREATE TABLE n1 (ts TIMESTAMP TIME INDEX, v BIGINT, s STRING) WITH (append_mode='true');
INSERT INTO n1 VALUES (1000, 1, 'a'), (2000, NULL, 'a'), (3000, NULL, NULL), (4000, 4, 'b');
SELECT count(*), count(v), count(s), count(DISTINCT s) FROM n1;
SELECT count(v) FROM n1;
-- SQLNESS REPLACE (metrics.*) REDACTED
-- SQLNESS REPLACE (RoundRobinBatch.*) REDACTED
-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED
EXPLAIN ANALYZE SELECT count(*) FROM n1;
ADMIN FLUSH_TABLE('n1');
SELECT count(*), count(v), count(s), count(DISTINCT s) FROM n1;
-- SQLNESS REPLACE (metrics.*) REDACTED
-- SQLNESS REPLACE (RoundRobinBatch.*) REDACTED
-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED
EXPLAIN ANALYZE SELECT count(*) FROM n1;
DROP TABLE n1;
CREATE TABLE n2 (
host STRING,
ts TIMESTAMP TIME INDEX,
v BIGINT,
s STRING,
PRIMARY KEY (host)
)
PARTITION ON COLUMNS (host) (
host < 'm',
host >= 'm'
)
WITH (append_mode='true');
INSERT INTO n2 VALUES
('a', 1000, 1, 'a'),
('b', 2000, NULL, 'a'),
('x', 3000, NULL, NULL),
('y', 4000, 4, 'b');
-- count is pushed down to each region as a state aggregate.
SELECT count(v), count(s) FROM n2;
ADMIN FLUSH_TABLE('n2');
SELECT count(v), count(s) FROM n2;
-- SQLNESS REPLACE (metrics.*) REDACTED
-- SQLNESS REPLACE (RoundRobinBatch.*) REDACTED
-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED
EXPLAIN ANALYZE SELECT count(*) FROM n2;
DROP TABLE n2;