feat: include order by to commutativity rule set (#4753)

* feat: include order by to commutativity rule set

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* tune sqlness replace interceptor

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

---------

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
Ruihang Xia
2024-09-23 16:35:06 +08:00
committed by GitHub
parent 0f99218386
commit 2feddca1cb
4 changed files with 224 additions and 19 deletions

View File

@@ -223,3 +223,102 @@ DROP TABLE test;
Affected Rows: 0
-- ORDER BY for partition table
CREATE TABLE IF NOT EXISTS `t` (
`tag` STRING NULL,
`ts` TIMESTAMP(3) NOT NULL,
`num` BIGINT NULL,
TIME INDEX (`ts`),
PRIMARY KEY (`tag`)
)
PARTITION ON COLUMNS (`tag`) (
tag <= 'z',
tag > 'z'
);
Affected Rows: 0
INSERT INTO t (tag, ts, num) VALUES
('abc', 0, 1),
('abc', 3000, 2),
('abc', 6000, 3),
('abc', 9000, 4),
('abc', 12000, 5),
('zzz', 3000, 6),
('zzz', 6000, 7),
('zzz', 9000, 8),
('zzz', 0, 9),
('zzz', 3000, 10);
Affected Rows: 10
select * from t where num > 3 order by ts desc limit 2;
+-----+---------------------+-----+
| tag | ts | num |
+-----+---------------------+-----+
| abc | 1970-01-01T00:00:12 | 5 |
| abc | 1970-01-01T00:00:09 | 4 |
+-----+---------------------+-----+
select tag from t where num > 6 order by ts desc limit 2;
+-----+---------------------+
| tag | ts |
+-----+---------------------+
| zzz | 1970-01-01T00:00:09 |
| zzz | 1970-01-01T00:00:06 |
+-----+---------------------+
select tag from t where num > 6 order by ts;
+-----+---------------------+
| tag | ts |
+-----+---------------------+
| zzz | 1970-01-01T00:00:00 |
| zzz | 1970-01-01T00:00:03 |
| zzz | 1970-01-01T00:00:06 |
| zzz | 1970-01-01T00:00:09 |
+-----+---------------------+
-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (metrics.*) REDACTED
-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED
-- SQLNESS REPLACE (RoundRobinBatch.*) REDACTED
explain analyze select tag from t where num > 6 order by ts desc limit 2;
+-+-+-+
| stage | node | plan_|
+-+-+-+
| 0_| 0_|_GlobalLimitExec: skip=0, fetch=2 REDACTED
|_|_|_SortPreservingMergeExec: [ts@1 DESC] REDACTED
|_|_|_SortExec: TopK(fetch=2), expr=[ts@1 DESC], preserve_partitioning=[true] REDACTED
|_|_|_MergeScanExec: REDACTED
|_|_|_|
| 1_| 0_|_GlobalLimitExec: skip=0, fetch=2 REDACTED
|_|_|_SortPreservingMergeExec: [ts@1 DESC] REDACTED
|_|_|_SortExec: TopK(fetch=2), expr=[ts@1 DESC], preserve_partitioning=[true] REDACTED
|_|_|_ProjectionExec: expr=[tag@0 as tag, ts@1 as ts] REDACTED
|_|_|_CoalesceBatchesExec: target_batch_size=8192 REDACTED
|_|_|_FilterExec: num@2 > 6 REDACTED
|_|_|_RepartitionExec: partitioning=REDACTED
|_|_|_SeqScan: region=REDACTED, partition_count=1 (1 memtable ranges, 0 file 0 ranges) REDACTED
|_|_|_|
| 1_| 1_|_GlobalLimitExec: skip=0, fetch=2 REDACTED
|_|_|_SortPreservingMergeExec: [ts@1 DESC] REDACTED
|_|_|_SortExec: TopK(fetch=2), expr=[ts@1 DESC], preserve_partitioning=[true] REDACTED
|_|_|_ProjectionExec: expr=[tag@0 as tag, ts@1 as ts] REDACTED
|_|_|_CoalesceBatchesExec: target_batch_size=8192 REDACTED
|_|_|_FilterExec: num@2 > 6 REDACTED
|_|_|_RepartitionExec: partitioning=REDACTED
|_|_|_SeqScan: region=REDACTED, partition_count=1 (1 memtable ranges, 0 file 0 ranges) REDACTED
|_|_|_|
|_|_| Total rows: 2_|
+-+-+-+
drop table t;
Affected Rows: 0

View File

@@ -56,3 +56,44 @@ SELECT a-10 AS k FROM test UNION SELECT a-10 AS l FROM test ORDER BY a-10;
SELECT a-10 AS k FROM test UNION SELECT a-11 AS l FROM test ORDER BY a-11;
DROP TABLE test;
-- ORDER BY for partition table
CREATE TABLE IF NOT EXISTS `t` (
`tag` STRING NULL,
`ts` TIMESTAMP(3) NOT NULL,
`num` BIGINT NULL,
TIME INDEX (`ts`),
PRIMARY KEY (`tag`)
)
PARTITION ON COLUMNS (`tag`) (
tag <= 'z',
tag > 'z'
);
INSERT INTO t (tag, ts, num) VALUES
('abc', 0, 1),
('abc', 3000, 2),
('abc', 6000, 3),
('abc', 9000, 4),
('abc', 12000, 5),
('zzz', 3000, 6),
('zzz', 6000, 7),
('zzz', 9000, 8),
('zzz', 0, 9),
('zzz', 3000, 10);
select * from t where num > 3 order by ts desc limit 2;
select tag from t where num > 6 order by ts desc limit 2;
select tag from t where num > 6 order by ts;
-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (metrics.*) REDACTED
-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED
-- SQLNESS REPLACE (RoundRobinBatch.*) REDACTED
explain analyze select tag from t where num > 6 order by ts desc limit 2;
drop table t;