Files
greptimedb/tests/cases/distributed/optimizer/partition_pruning_cast.result
T
discord9 4135e65a76 fix(query): keep requested sort when filtering on a lossy cast (#10026)
fix(query): pin DataFusion cast constraint propagation fix

Update the pinned GreptimeTeam/datafusion revision from e3221b8 to
cc55344 (the 55.1 fork carrying apache/datafusion#25531 plus a stronger
regression test).

CastExpr::propagate_constraints used to cast a filter's output interval
straight back to the input type. For CAST(k AS BIGINT) = 1 the singleton
output [1, 1] was inverted to [1.0, 1.0] although k = 1.5 matches too, so
FilterExec advertised k as constant, the equivalence classes dropped the
ordering required by ORDER BY k, and rows returned in scan order.

The sqlness case keeps every #9600 assertion and the explicit ORDER BY,
and adds ascending and descending checks for the partitioned table, the
control table and a table whose matching rows are stored in neither
ascending nor descending k order.

Cargo.lock only rewrites the 35 datafusion source lines.

Signed-off-by: discord9 <discord9@163.com>
2026-10-28 10:40:43 +00:00

250 lines
8.5 KiB
Plaintext

CREATE TABLE partition_pruning_cast_partitioned (
ts TIMESTAMP TIME INDEX,
k FLOAT PRIMARY KEY,
v BIGINT,
)
PARTITION ON COLUMNS (k) (
k < 1.25,
k >= 1.25
);
Affected Rows: 0
CREATE TABLE partition_pruning_cast_control (
ts TIMESTAMP TIME INDEX,
k FLOAT PRIMARY KEY,
v BIGINT,
);
Affected Rows: 0
INSERT INTO partition_pruning_cast_partitioned VALUES
(1000, 1.0, 10),
(1000, 1.5, 15),
(1000, 2.5, 25);
Affected Rows: 3
INSERT INTO partition_pruning_cast_control VALUES
(1000, 1.0, 10),
(1000, 1.5, 15),
(1000, 2.5, 25);
Affected Rows: 3
SELECT k, v, CAST(k AS BIGINT) AS cast_k
FROM partition_pruning_cast_partitioned ORDER BY k;
+-----+----+--------+
| k | v | cast_k |
+-----+----+--------+
| 1.0 | 10 | 1 |
| 1.5 | 15 | 1 |
| 2.5 | 25 | 2 |
+-----+----+--------+
SELECT k, v, CAST(k AS BIGINT) AS cast_k
FROM partition_pruning_cast_control ORDER BY k;
+-----+----+--------+
| k | v | cast_k |
+-----+----+--------+
| 1.0 | 10 | 1 |
| 1.5 | 15 | 1 |
| 2.5 | 25 | 2 |
+-----+----+--------+
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (partitioning.*) REDACTED
EXPLAIN SELECT k, v, CAST(k AS BIGINT) AS cast_k
FROM partition_pruning_cast_partitioned
WHERE CAST(k AS BIGINT) = 1;
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| plan_type | plan |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| logical_plan | MergeScan [is_placeholder=false, remote_input=[ |
| | Projection: partition_pruning_cast_partitioned.k, partition_pruning_cast_partitioned.v, CAST(partition_pruning_cast_partitioned.k AS Int64) AS cast_k |
| | Filter: CAST(partition_pruning_cast_partitioned.k AS Int64) = Int64(1) |
| | TableScan: partition_pruning_cast_partitioned, partial_filters=[CAST(partition_pruning_cast_partitioned.k AS Int64) = Int64(1)] |
| | ]] |
| physical_plan | CooperativeExec |
| | MergeScanExec: REDACTED
| | |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
SELECT ts, k, v, CAST(k AS BIGINT) AS cast_k
FROM partition_pruning_cast_partitioned
WHERE CAST(k AS BIGINT) = 1
ORDER BY k;
+---------------------+-----+----+--------+
| ts | k | v | cast_k |
+---------------------+-----+----+--------+
| 1970-01-01T00:00:01 | 1.0 | 10 | 1 |
| 1970-01-01T00:00:01 | 1.5 | 15 | 1 |
+---------------------+-----+----+--------+
SELECT ts, k, v, CAST(k AS BIGINT) AS cast_k
FROM partition_pruning_cast_control
WHERE CAST(k AS BIGINT) = 1
ORDER BY k;
+---------------------+-----+----+--------+
| ts | k | v | cast_k |
+---------------------+-----+----+--------+
| 1970-01-01T00:00:01 | 1.0 | 10 | 1 |
| 1970-01-01T00:00:01 | 1.5 | 15 | 1 |
+---------------------+-----+----+--------+
-- The same rows must also come back in descending order: a filter on a lossy
-- cast must not turn `k` into a constant and drop the requested sort.
SELECT ts, k, v, CAST(k AS BIGINT) AS cast_k
FROM partition_pruning_cast_partitioned
WHERE CAST(k AS BIGINT) = 1
ORDER BY k DESC;
+---------------------+-----+----+--------+
| ts | k | v | cast_k |
+---------------------+-----+----+--------+
| 1970-01-01T00:00:01 | 1.5 | 15 | 1 |
| 1970-01-01T00:00:01 | 1.0 | 10 | 1 |
+---------------------+-----+----+--------+
SELECT ts, k, v, CAST(k AS BIGINT) AS cast_k
FROM partition_pruning_cast_control
WHERE CAST(k AS BIGINT) = 1
ORDER BY k DESC;
+---------------------+-----+----+--------+
| ts | k | v | cast_k |
+---------------------+-----+----+--------+
| 1970-01-01T00:00:01 | 1.5 | 15 | 1 |
| 1970-01-01T00:00:01 | 1.0 | 10 | 1 |
+---------------------+-----+----+--------+
-- A table whose matching rows are stored in neither ascending nor descending
-- order, so a dropped sort cannot accidentally satisfy the ASC assertion.
CREATE TABLE partition_pruning_cast_unsorted (
ts TIMESTAMP TIME INDEX,
k FLOAT PRIMARY KEY,
v BIGINT,
);
Affected Rows: 0
INSERT INTO partition_pruning_cast_unsorted VALUES
(1000, 1.5, 15),
(1000, 2.5, 25),
(1000, 1.0, 10),
(1000, 1.25, 12);
Affected Rows: 4
SELECT ts, k, v, CAST(k AS BIGINT) AS cast_k
FROM partition_pruning_cast_unsorted
WHERE CAST(k AS BIGINT) = 1
ORDER BY k;
+---------------------+------+----+--------+
| ts | k | v | cast_k |
+---------------------+------+----+--------+
| 1970-01-01T00:00:01 | 1.0 | 10 | 1 |
| 1970-01-01T00:00:01 | 1.25 | 12 | 1 |
| 1970-01-01T00:00:01 | 1.5 | 15 | 1 |
+---------------------+------+----+--------+
SELECT ts, k, v, CAST(k AS BIGINT) AS cast_k
FROM partition_pruning_cast_unsorted
WHERE CAST(k AS BIGINT) = 1
ORDER BY k DESC;
+---------------------+------+----+--------+
| ts | k | v | cast_k |
+---------------------+------+----+--------+
| 1970-01-01T00:00:01 | 1.5 | 15 | 1 |
| 1970-01-01T00:00:01 | 1.25 | 12 | 1 |
| 1970-01-01T00:00:01 | 1.0 | 10 | 1 |
+---------------------+------+----+--------+
DROP TABLE partition_pruning_cast_unsorted;
Affected Rows: 0
-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (elapsed_compute.*) REDACTED
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (metrics.*) REDACTED
-- SQLNESS REPLACE (partitioning.*) REDACTED
-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED
-- SQLNESS REPLACE \\?"(dyn_filters|time_range|file_summary|max_part_range)\\?":.* "$1":REDACTED
EXPLAIN ANALYZE SELECT k, v
FROM partition_pruning_cast_partitioned
WHERE k >= 1.25::FLOAT AND k < 2.5::FLOAT;
+-+-+-+
| stage | node | plan_|
+-+-+-+
| 0_| 0_|_CooperativeExec REDACTED
|_|_|_MergeScanExec: REDACTED
|_|_|_|
| 1_| 0_|_CooperativeExec REDACTED
|_|_|_SeqScan: region=REDACTED, "partition_count":{"count":1, "mem_ranges":1, "files":0, "file_ranges":0}, "projection": ["k", "v"], "filters": ["k >= Float32(1.25)", "k < Float32(2.5)", "k >= Float32(1.25)"], "time_range":REDACTED
|_|_|_|
|_|_| Total rows: 1_|
+-+-+-+
-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (elapsed_compute.*) REDACTED
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (metrics.*) REDACTED
-- SQLNESS REPLACE (partitioning.*) REDACTED
-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED
-- SQLNESS REPLACE \\?"(dyn_filters|time_range|file_summary|max_part_range)\\?":.* "$1":REDACTED
EXPLAIN ANALYZE SELECT k, v
FROM partition_pruning_cast_partitioned
WHERE k >= 1.25::FLOAT AND k < 2.5::FLOAT AND CAST(v AS INT) IS NOT NULL;
+-+-+-+
| stage | node | plan_|
+-+-+-+
| 0_| 0_|_CooperativeExec REDACTED
|_|_|_MergeScanExec: REDACTED
|_|_|_|
| 1_| 0_|_FilterExec: CAST(v@1 AS Int32) IS NOT NULL REDACTED
|_|_|_CooperativeExec REDACTED
|_|_|_SeqScan: region=REDACTED, "partition_count":{"count":1, "mem_ranges":1, "files":0, "file_ranges":0}, "projection": ["k", "v"], "filters": ["k >= Float32(1.25)", "k < Float32(2.5)", "CAST(v AS Int32) IS NOT NULL", "k >= Float32(1.25)"], "time_range":REDACTED
|_|_|_|
|_|_| Total rows: 1_|
+-+-+-+
SELECT k, v FROM partition_pruning_cast_partitioned
WHERE k >= 1.25::FLOAT AND k < 2.5::FLOAT AND CAST(v AS INT) IS NOT NULL;
+-----+----+
| k | v |
+-----+----+
| 1.5 | 15 |
+-----+----+
SELECT k FROM partition_pruning_cast_partitioned
WHERE k = 1.0 OR CAST(k AS BIGINT) = 1 ORDER BY k;
+-----+
| k |
+-----+
| 1.0 |
| 1.5 |
+-----+
DROP TABLE partition_pruning_cast_partitioned;
Affected Rows: 0
DROP TABLE partition_pruning_cast_control;
Affected Rows: 0