mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-12-04 00:15:29 +00:00
perf(query): retain hash pruning when partition bounds cannot normalize Use scan-local iceberg_hash equalities to exclude routes even when another partition dimension prevents constraint normalization. Preserve NULL and unmatched-write destinations, and cover the fallback in a mixed timestamp/hash SQLNESS case. Signed-off-by: Lei, HUANG <ratuthomm@gmail.com>
123 lines
4.7 KiB
SQL
123 lines
4.7 KiB
SQL
-- The SQL forms of the partition hash: measure a value, derive breakpoints, and
|
|
-- render a rule list that can be pasted into `PARTITION ON COLUMNS`.
|
|
|
|
SELECT iceberg_hash('iceberg'), iceberg_hash(34), iceberg_hash(NULL);
|
|
|
|
SELECT iceberg_hash_bounds(2), iceberg_hash_bounds(4);
|
|
|
|
SELECT iceberg_hash_breakpoints('iceberg', 'a', 'iceberg');
|
|
|
|
-- The column name is quoted, so a name that is not a plain lowercase identifier
|
|
-- can be pasted as-is.
|
|
SELECT iceberg_hash_ranges_sql('host-name', 4);
|
|
|
|
-- A column name in another Arrow string representation renders the same rules.
|
|
SELECT iceberg_hash_ranges_sql(arrow_cast('host-name', 'Utf8View'), 4);
|
|
|
|
-- The rules below are the exact output of
|
|
-- `iceberg_hash_ranges_sql('host-name', 4)` pasted into a table definition.
|
|
CREATE TABLE hash_function_rules (
|
|
"host-name" STRING,
|
|
ts TIMESTAMP TIME INDEX,
|
|
v INT,
|
|
PRIMARY KEY ("host-name")
|
|
)
|
|
PARTITION ON COLUMNS ("host-name") (
|
|
iceberg_hash("host-name") < -1073741824,
|
|
iceberg_hash("host-name") >= -1073741824 AND iceberg_hash("host-name") < 0,
|
|
iceberg_hash("host-name") >= 0 AND iceberg_hash("host-name") < 1073741824,
|
|
iceberg_hash("host-name") >= 1073741824
|
|
);
|
|
|
|
SHOW CREATE TABLE hash_function_rules;
|
|
|
|
SELECT partition_name, partition_description
|
|
FROM information_schema.partitions
|
|
WHERE table_name = 'hash_function_rules'
|
|
ORDER BY partition_name;
|
|
|
|
-- One value per bucket, plus NULL, which the first rule catches.
|
|
INSERT INTO hash_function_rules VALUES
|
|
('b', 1, 1),
|
|
('delta', 2, 2),
|
|
('a', 3, 3),
|
|
('iceberg', 4, 4),
|
|
(NULL, 5, 5);
|
|
|
|
-- The hashes the rendered predicates compare against, computed by the function
|
|
-- the rules evaluate: b < -1073741824, -1073741824 <= delta < 0,
|
|
-- 0 <= a < 1073741824, iceberg >= 1073741824, NULL hashes to NULL.
|
|
SELECT "host-name", iceberg_hash("host-name") FROM hash_function_rules ORDER BY ts;
|
|
|
|
-- Row counts per rendered predicate, plus the NULL hash no comparison matches ...
|
|
SELECT
|
|
sum(CASE WHEN iceberg_hash("host-name") < -1073741824 THEN 1 ELSE 0 END) AS p0,
|
|
sum(CASE WHEN iceberg_hash("host-name") >= -1073741824 AND iceberg_hash("host-name") < 0 THEN 1 ELSE 0 END) AS p1,
|
|
sum(CASE WHEN iceberg_hash("host-name") >= 0 AND iceberg_hash("host-name") < 1073741824 THEN 1 ELSE 0 END) AS p2,
|
|
sum(CASE WHEN iceberg_hash("host-name") >= 1073741824 THEN 1 ELSE 0 END) AS p3,
|
|
sum(CASE WHEN iceberg_hash("host-name") IS NULL THEN 1 ELSE 0 END) AS nulls
|
|
FROM hash_function_rules;
|
|
|
|
-- ... must match the rows each region actually stored: region 0 holds p0 plus
|
|
-- the NULL rows, and every other region holds exactly its bucket.
|
|
-- SQLNESS SLEEP 3s
|
|
SELECT region_number, region_rows
|
|
FROM information_schema.region_statistics
|
|
WHERE table_id IN (
|
|
SELECT table_id FROM information_schema.tables
|
|
WHERE table_name = 'hash_function_rules' AND table_schema = 'public'
|
|
)
|
|
ORDER BY region_number;
|
|
|
|
-- 'b' and the omitted-key route share one region. The SQL predicate still rejects NULL.
|
|
-- SQLNESS REPLACE (?s)^.*?(MergeScanExec:\speers=\[[^\]]*\]).*$ $1
|
|
-- SQLNESS REPLACE \d+\(\d+,\s+(\d+)\) r$1
|
|
EXPLAIN SELECT "host-name", v FROM hash_function_rules WHERE 'b' = "host-name";
|
|
|
|
SELECT "host-name", v FROM hash_function_rules WHERE 'b' = "host-name";
|
|
|
|
DROP TABLE hash_function_rules;
|
|
|
|
-- The same hash bucket is needed on each side of the timestamp boundary.
|
|
CREATE TABLE hash_equality_time_ranges (
|
|
host STRING,
|
|
ts TIMESTAMP TIME INDEX,
|
|
PRIMARY KEY (host)
|
|
)
|
|
PARTITION ON COLUMNS (ts, host) (
|
|
ts < '2024-01-02 00:00:00' AND iceberg_hash(host) < 0,
|
|
ts < '2024-01-02 00:00:00' AND iceberg_hash(host) >= 0,
|
|
ts >= '2024-01-02 00:00:00' AND iceberg_hash(host) < 0,
|
|
ts >= '2024-01-02 00:00:00' AND iceberg_hash(host) >= 0
|
|
);
|
|
|
|
INSERT INTO hash_equality_time_ranges VALUES
|
|
('b', '2024-01-01 00:00:00'), ('b', '2024-01-02 00:00:00'), ('a', '2024-01-02 00:00:00');
|
|
|
|
-- SQLNESS REPLACE (?s)^.*?(MergeScanExec:\speers=\[[^\]]*\]).*$ $1
|
|
-- SQLNESS REPLACE \d+\(\d+,\s+\d+\) REGION
|
|
EXPLAIN SELECT ts FROM hash_equality_time_ranges WHERE host = 'b';
|
|
|
|
SELECT ts FROM hash_equality_time_ranges WHERE host = 'b' ORDER BY ts;
|
|
|
|
DROP TABLE hash_equality_time_ranges;
|
|
|
|
-- Check visibility after DROP DEFAULT for a write that omitted host. The NULL route is
|
|
-- region 1 and 'a' routes to region 2; no input needs the region 0 fallback here.
|
|
CREATE TABLE hash_equality_default (
|
|
host STRING DEFAULT 'a',
|
|
ts TIMESTAMP TIME INDEX,
|
|
PRIMARY KEY (host)
|
|
)
|
|
PARTITION ON COLUMNS (host) (
|
|
iceberg_hash(host) >= 1073741824,
|
|
iceberg_hash(host) < 0,
|
|
iceberg_hash(host) >= 0 AND iceberg_hash(host) < 1073741824
|
|
);
|
|
|
|
INSERT INTO hash_equality_default (ts) VALUES (1);
|
|
ALTER TABLE hash_equality_default MODIFY COLUMN host DROP DEFAULT;
|
|
SELECT host FROM hash_equality_default WHERE host = 'a';
|
|
|
|
DROP TABLE hash_equality_default;
|