Skip to content

Commit

Permalink
planner: fix the case that projection prunes all columns
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Jul 18, 2024
1 parent ac92026 commit 8f075a6
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 21 deletions.
17 changes: 7 additions & 10 deletions pkg/planner/core/casetest/hint/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -799,16 +799,13 @@
"HashAgg 16000.00 root group by:Column#41, funcs:firstrow(Column#41)->Column#41",
"└─Union 1000000010000.00 root ",
" ├─HashJoin 1000000000000.00 root CARTESIAN inner join",
" │ ├─TableReader(Build) 10000.00 root data:TableFullScan",
" │ │ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
" │ └─Projection(Probe) 100000000.00 root 1->Column#55",
" │ └─HashJoin 100000000.00 root CARTESIAN inner join",
" │ ├─Projection(Build) 10000.00 root 1->Column#54",
" │ │ └─IndexReader 10000.00 root index:IndexFullScan",
" │ │ └─IndexFullScan 10000.00 cop[tikv] table:t3, index:idx_a(a) keep order:false, stats:pseudo",
" │ └─Projection(Probe) 10000.00 root 1->Column#53",
" │ └─IndexReader 10000.00 root index:IndexFullScan",
" │ └─IndexFullScan 10000.00 cop[tikv] table:t2, index:idx_a(a) keep order:false, stats:pseudo",
" │ ├─IndexReader(Build) 10000.00 root index:IndexFullScan",
" │ │ └─IndexFullScan 10000.00 cop[tikv] table:t3, index:idx_a(a) keep order:false, stats:pseudo",
" │ └─HashJoin(Probe) 100000000.00 root CARTESIAN inner join",
" │ ├─IndexReader(Build) 10000.00 root index:IndexFullScan",
" │ │ └─IndexFullScan 10000.00 cop[tikv] table:t2, index:idx_a(a) keep order:false, stats:pseudo",
" │ └─TableReader(Probe) 10000.00 root data:TableFullScan",
" │ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
" └─TableReader 10000.00 root data:TableFullScan",
" └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3455,11 +3455,12 @@
"└─TableReader 1.00 root MppVersion: 2, data:ExchangeSender",
" └─ExchangeSender 1.00 mpp[tiflash] ExchangeType: PassThrough",
" └─HashAgg 1.00 mpp[tiflash] funcs:count(1)->Column#20",
" └─HashJoin 12500.00 mpp[tiflash] inner join, equal:[eq(test.t.a, test.t.e)]",
" ├─ExchangeReceiver(Build) 10000.00 mpp[tiflash] ",
" │ └─ExchangeSender 10000.00 mpp[tiflash] ExchangeType: Broadcast, Compression: FAST",
" │ └─TableFullScan 10000.00 mpp[tiflash] table:t1 keep order:false, stats:pseudo",
" └─TableFullScan(Probe) 10000.00 mpp[tiflash] table:t2 keep order:false, stats:pseudo"
" └─Projection 12500.00 mpp[tiflash] test.t.a",
" └─HashJoin 12500.00 mpp[tiflash] inner join, equal:[eq(test.t.a, test.t.e)]",
" ├─ExchangeReceiver(Build) 10000.00 mpp[tiflash] ",
" │ └─ExchangeSender 10000.00 mpp[tiflash] ExchangeType: Broadcast, Compression: FAST",
" │ └─TableFullScan 10000.00 mpp[tiflash] table:t1 keep order:false, stats:pseudo",
" └─TableFullScan(Probe) 10000.00 mpp[tiflash] table:t2 keep order:false, stats:pseudo"
],
"Warning": null
},
Expand Down
4 changes: 4 additions & 0 deletions pkg/planner/core/logical_projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (p *LogicalProjection) PruneColumns(parentUsedCols []*expression.Column, op
if err != nil {
return nil, err
}
// If its columns are all pruned, we directly use its child. The child will output at least one column.
if p.Schema().Len() == 0 {
return p.Children()[0], nil
}
return p, nil
}

Expand Down
20 changes: 17 additions & 3 deletions pkg/planner/core/operator/logicalop/logical_schema_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package logicalop

import (
"math"

"github.com/pingcap/tidb/pkg/expression"
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace"
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace/logicaltrace"
Expand Down Expand Up @@ -70,11 +72,23 @@ func (s *LogicalSchemaProducer) SetSchemaAndNames(schema *expression.Schema, nam

// InlineProjection prunes unneeded columns inline an executor.
func (s *LogicalSchemaProducer) InlineProjection(parentUsedCols []*expression.Column, opt *optimizetrace.LogicalOptimizeOp) {
if len(parentUsedCols) == 0 {
return
}
prunedColumns := make([]*expression.Column, 0)
used := expression.GetUsedList(s.SCtx().GetExprCtx().GetEvalCtx(), parentUsedCols, s.Schema())
if len(parentUsedCols) == 0 {
minColLen := math.MaxInt
chosenPos := 0
for i, col := range s.schema.Columns {
flen := col.GetType(s.SCtx().GetExprCtx().GetEvalCtx()).GetFlen()
if flen < minColLen {
chosenPos = i
minColLen = flen
}
}
// It chould be always true.
if len(used) > 0 {
used[chosenPos] = true
}
}
for i := len(used) - 1; i >= 0; i-- {
if !used[i] {
prunedColumns = append(prunedColumns, s.Schema().Columns[i])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,8 @@ id estRows task access object operator info
HashJoin 10000.00 root CARTESIAN left outer semi join, other cond:eq(2, planner__core__casetest__integration.t.b)
├─TableReader(Build) 10000.00 root data:TableFullScan
│ └─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
└─Projection(Probe) 10000.00 root 1->Column#22
└─TableReader 10000.00 root data:TableFullScan
└─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
└─TableReader(Probe) 10000.00 root data:TableFullScan
└─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
select (2) in (select b from t) from (select t.a < (select t.a from t t1 limit 1) from t) t;
(2) in (select b from t)
1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,93 @@ drop view if exists v0;
CREATE VIEW v0(c0) AS SELECT t0.c0 FROM t0;
SELECT t0.c0 FROM v0, t0 LEFT JOIN t1 ON t0.c0 WHERE ((INET_ATON('5V')) IS NULL);
c0
CREATE TABLE `t31cdd702` (
`col_48` date NOT NULL DEFAULT '2003-04-01',
`col_49` time NOT NULL,
`col_50` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk COLLATE=gbk_chinese_ci;
INSERT INTO `t31cdd702` VALUES('1976-03-13','02:40:13',''),('2007-07-19','02:40:13',''),('2000-03-19','02:40:13',''),('1970-04-16','02:40:13',''),('2024-06-23','02:40:13',''),('2022-01-22','02:40:13',''),('2017-04-12','02:40:13',''),('1998-08-10','02:40:13',''),('2025-12-28','02:40:13',''),('2001-01-14','02:40:13',''),('1997-12-10','02:40:13',''),('2014-01-15','02:40:13',''),('1992-04-21','07:55:36','&v'),('2024-05-15','02:40:13',''),('1978-08-28','02:40:13',''),('2030-01-07','02:40:13',''),('1981-10-26','02:40:13',''),('1988-02-10','02:40:13',''),('2016-02-14','02:40:13',''),('1996-11-21','09:54:04','on6VRKYtaI'),('2022-08-03','02:40:13',''),('2034-10-05','02:40:13',''),('1988-11-12','02:18:22','m'),('2028-04-17','02:40:13',''),('1993-08-30','07:32:34','f@C*'),('2002-11-22','02:40:13',''),('2016-11-13','02:40:13',''),('2018-08-02','02:40:13',''),('1973-07-30','02:40:13',''),('2014-12-28','02:40:13',''),('1978-05-07','02:40:13',''),('1992-05-07','08:05:41','9'),('1979-05-23','04:08:58','brm'),('1996-08-01','23:10:58','IyG643!'),('2034-05-31','04:48:28',''),('1978-08-10','20:38:16','_$8reH*!MLE43'),('1986-08-13','23:20:51','eHr%WCBu');
CREATE TABLE `tl45f49bec` (
`col_21` time NOT NULL DEFAULT '19:03:14',
`col_22` text COLLATE gbk_bin NOT NULL,
`col_23` float NOT NULL,
PRIMARY KEY (`col_22`(2),`col_21`,`col_23`) /*T![clustered_index] CLUSTERED */,
UNIQUE KEY `idx_4` (`col_22`(1),`col_21`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk COLLATE=gbk_bin;
INSERT INTO `tl45f49bec` VALUES('05:26:53','',6436.3984),('05:33:06','',2418.3447),('05:56:03','!34',5327.29),('11:11:13','$uX7jK=*(prX#fm',8447.91),('05:35:17','*Jqx7z%a9~1Xw',7480.8096),('22:48:06','-',4563.9565),('03:48:30','1*t@',282.95325),('19:34:18','1~4i@f8X&exNs+CG0x',1238.2216),('13:22:26','3c9iE',1337.3021),('11:30:51','4xyKNd7+tKbh',130.22589),('22:35:40','56vCiz^%#hcS',8593.311),('07:56:40','5W0p%FB$cMOcC_-37k',7798.0645),('23:17:09','6A!03oVaCmLM',6335.1514),('04:29:52','7N',8081.281),('04:14:22','7rW*b',2108.5618),('02:11:20','87O8gvnLG5',5109.0845),('11:43:02','=0vROyDng',9798.294),('06:21:18','@+0~@GdUE%+hSJg*#',7182.4136),('03:08:56','B1y^-u_v+l',2024.7775),('11:36:31','E#o%-MWl',3556.0056),('17:40:46','E1!qy4Qvw6s',8514.763),('13:40:54','IwMfmh$lfz',2577.1978),('00:55:17','J&eq%cQP+cx',1946.7703),('23:26:11','JJ0',9597.079),('19:16:32','K0VO3g(_nx%HMX',3434.9307),('14:35:00','LEJ9!B',1137.5157),('01:26:40','Sfuqtm',5829.2686),('11:58:06','XpqXa^b*%b!&I4ZnS',5890.494),('21:06:51','^',6630.6665),('03:22:56','^a',9613.8545),('04:30:59','_bmnB!IeDpljq',6335.3916),('08:29:45','b)=RH&R',5911.286),('18:56:18','h+5l9',1037.6467),('22:44:14','sZuxMLWUU',5482.626),('03:51:42','x-7',9611.379);
SELECT 1 AS `r0` FROM (`t31cdd702`) JOIN `tl45f49bec` WHERE `tl45f49bec`.`col_22` BETWEEN 'LEJ9!B' AND 'TV#~!yw' LIMIT 24622829;
r0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
21 changes: 21 additions & 0 deletions tests/integrationtest/t/planner/core/issuetest/planner_issue.test
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,24 @@ drop view if exists v0;
CREATE VIEW v0(c0) AS SELECT t0.c0 FROM t0;

SELECT t0.c0 FROM v0, t0 LEFT JOIN t1 ON t0.c0 WHERE ((INET_ATON('5V')) IS NULL);

# https://github.com/pingcap/tidb/issues/54648
CREATE TABLE `t31cdd702` (
`col_48` date NOT NULL DEFAULT '2003-04-01',
`col_49` time NOT NULL,
`col_50` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk COLLATE=gbk_chinese_ci;

INSERT INTO `t31cdd702` VALUES('1976-03-13','02:40:13',''),('2007-07-19','02:40:13',''),('2000-03-19','02:40:13',''),('1970-04-16','02:40:13',''),('2024-06-23','02:40:13',''),('2022-01-22','02:40:13',''),('2017-04-12','02:40:13',''),('1998-08-10','02:40:13',''),('2025-12-28','02:40:13',''),('2001-01-14','02:40:13',''),('1997-12-10','02:40:13',''),('2014-01-15','02:40:13',''),('1992-04-21','07:55:36','&v'),('2024-05-15','02:40:13',''),('1978-08-28','02:40:13',''),('2030-01-07','02:40:13',''),('1981-10-26','02:40:13',''),('1988-02-10','02:40:13',''),('2016-02-14','02:40:13',''),('1996-11-21','09:54:04','on6VRKYtaI'),('2022-08-03','02:40:13',''),('2034-10-05','02:40:13',''),('1988-11-12','02:18:22','m'),('2028-04-17','02:40:13',''),('1993-08-30','07:32:34','f@C*'),('2002-11-22','02:40:13',''),('2016-11-13','02:40:13',''),('2018-08-02','02:40:13',''),('1973-07-30','02:40:13',''),('2014-12-28','02:40:13',''),('1978-05-07','02:40:13',''),('1992-05-07','08:05:41','9'),('1979-05-23','04:08:58','brm'),('1996-08-01','23:10:58','IyG643!'),('2034-05-31','04:48:28',''),('1978-08-10','20:38:16','_$8reH*!MLE43'),('1986-08-13','23:20:51','eHr%WCBu');

CREATE TABLE `tl45f49bec` (
`col_21` time NOT NULL DEFAULT '19:03:14',
`col_22` text COLLATE gbk_bin NOT NULL,
`col_23` float NOT NULL,
PRIMARY KEY (`col_22`(2),`col_21`,`col_23`) /*T![clustered_index] CLUSTERED */,
UNIQUE KEY `idx_4` (`col_22`(1),`col_21`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk COLLATE=gbk_bin;

INSERT INTO `tl45f49bec` VALUES('05:26:53','',6436.3984),('05:33:06','',2418.3447),('05:56:03','!34',5327.29),('11:11:13','$uX7jK=*(prX#fm',8447.91),('05:35:17','*Jqx7z%a9~1Xw',7480.8096),('22:48:06','-',4563.9565),('03:48:30','1*t@',282.95325),('19:34:18','1~4i@f8X&exNs+CG0x',1238.2216),('13:22:26','3c9iE',1337.3021),('11:30:51','4xyKNd7+tKbh',130.22589),('22:35:40','56vCiz^%#hcS',8593.311),('07:56:40','5W0p%FB$cMOcC_-37k',7798.0645),('23:17:09','6A!03oVaCmLM',6335.1514),('04:29:52','7N',8081.281),('04:14:22','7rW*b',2108.5618),('02:11:20','87O8gvnLG5',5109.0845),('11:43:02','=0vROyDng',9798.294),('06:21:18','@+0~@GdUE%+hSJg*#',7182.4136),('03:08:56','B1y^-u_v+l',2024.7775),('11:36:31','E#o%-MWl',3556.0056),('17:40:46','E1!qy4Qvw6s',8514.763),('13:40:54','IwMfmh$lfz',2577.1978),('00:55:17','J&eq%cQP+cx',1946.7703),('23:26:11','JJ0',9597.079),('19:16:32','K0VO3g(_nx%HMX',3434.9307),('14:35:00','LEJ9!B',1137.5157),('01:26:40','Sfuqtm',5829.2686),('11:58:06','XpqXa^b*%b!&I4ZnS',5890.494),('21:06:51','^',6630.6665),('03:22:56','^a',9613.8545),('04:30:59','_bmnB!IeDpljq',6335.3916),('08:29:45','b)=RH&R',5911.286),('18:56:18','h+5l9',1037.6467),('22:44:14','sZuxMLWUU',5482.626),('03:51:42','x-7',9611.379);

SELECT 1 AS `r0` FROM (`t31cdd702`) JOIN `tl45f49bec` WHERE `tl45f49bec`.`col_22` BETWEEN 'LEJ9!B' AND 'TV#~!yw' LIMIT 24622829;

0 comments on commit 8f075a6

Please sign in to comment.