Skip to content

Commit

Permalink
planner: enhance rule partition pruning (#14544) (#14546)
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHuaiyu authored and zz-jason committed Jan 20, 2020
1 parent 2ddeb25 commit 8286441
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
15 changes: 15 additions & 0 deletions cmd/explaintest/r/partition_pruning.result
Original file line number Diff line number Diff line change
Expand Up @@ -4352,3 +4352,18 @@ Union_8 30000.00 root
│ └─TableScan_11 10000.00 cop table:t, partition:p1, range:[-inf,+inf], keep order:false, stats:pseudo
└─TableReader_14 10000.00 root data:TableScan_13
└─TableScan_13 10000.00 cop table:t, partition:p2, range:[-inf,+inf], keep order:false, stats:pseudo
drop table if exists t;
CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE COLUMNS(a) (
PARTITION p0 VALUES LESS THAN (1),
PARTITION p1 VALUES LESS THAN (10),
PARTITION p2 VALUES LESS THAN (100)
);
desc select * from t where a = 11 and b = 1 or a = 12 and b = 1;
id count task operator info
TableReader_8 8000.00 root data:Selection_7
└─Selection_7 8000.00 cop or(and(eq(test.t.a, 11), eq(test.t.b, 1)), and(eq(test.t.a, 12), eq(test.t.b, 1)))
└─TableScan_6 10000.00 cop table:t, partition:p2, range:[-inf,+inf], keep order:false, stats:pseudo
13 changes: 13 additions & 0 deletions cmd/explaintest/t/partition_pruning.test
Original file line number Diff line number Diff line change
Expand Up @@ -963,3 +963,16 @@ insert into t values(201);
explain select * from t;
rollback;
explain select * from t;

drop table if exists t;
CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE COLUMNS(a) (
PARTITION p0 VALUES LESS THAN (1),
PARTITION p1 VALUES LESS THAN (10),
PARTITION p2 VALUES LESS THAN (100)
);

desc select * from t where a = 11 and b = 1 or a = 12 and b = 1;
5 changes: 2 additions & 3 deletions planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,11 @@ func (s *partitionProcessor) canBePruned(sctx sessionctx.Context, partCol *expre
// TODO: Remove prune by calculating range. Current constraint propagate doesn't
// handle the null condition, while calculate range can prune something like:
// "select * from t where t is null"
accessConds := ranger.ExtractAccessConditionsForColumn(conds, partCol.UniqueID)
r, err := ranger.BuildColumnRange(accessConds, sctx.GetSessionVars().StmtCtx, partCol.RetType, types.UnspecifiedLength)
res, err := ranger.DetachCondAndBuildRangeForIndex(sctx, conds, []*expression.Column{partCol}, []int{types.UnspecifiedLength})
if err != nil {
return false, err
}
return len(r) == 0, nil
return len(res.Ranges) == 0, nil
}

// findByName checks whether object name exists in list.
Expand Down

0 comments on commit 8286441

Please sign in to comment.