Skip to content

Commit

Permalink
executor: fix select wrong partition for hash partition table (pingca…
Browse files Browse the repository at this point in the history
  • Loading branch information
Defined2014 authored Jan 4, 2024
1 parent cdc7572 commit ebe334f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ func (s *partitionProcessor) getUsedHashPartitions(ctx sessionctx.Context,
rangeScalar = 0
} else {
rangeScalar = uint64(posHigh - posLow)
offset = mathutil.Abs(posLow % int64(numPartitions))
offset = posLow % int64(numPartitions)
}
}

// if range is less than the number of partitions, there will be unused partitions we can prune out.
if rangeScalar < uint64(numPartitions) && !highIsNull && !lowIsNull {
var i int64
for i = 0; i <= int64(rangeScalar); i++ {
idx := (offset + i) % int64(numPartitions)
idx := mathutil.Abs(offset+i) % int64(numPartitions)
if len(partitionNames) > 0 && !s.findByName(partitionNames, pi.Definitions[idx].Name.L) {
continue
}
Expand Down
34 changes: 34 additions & 0 deletions tests/integrationtest/r/executor/partition/issues.result
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,37 @@ SELECT * FROM `t` WHERE `t`.`col_51` BETWEEN -9223372036854775808 AND 9223372036
col_51
-9223372036854775808
9223372036854775807
drop table if exists t;
CREATE TABLE `t` (
`col_29` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
PARTITION BY HASH (`col_29`) PARTITIONS 7;
INSERT INTO `t` VALUES (-1), (11), (-128), (39), (-46), (38), (-102), (-99), (-87), (-127), (-89), (43), (108), (59), (0), (24), (101), (37), (-103), (90), (-95), (-44), (123), (124), (-123), (-52), (-49), (-98), (-104), (-68), (2), (-24), (67), (89), (1), (-65), (36), (-109), (41), (5), (98), (-63), (-14), (127), (-6), (121), (14), (-122);
analyze table t;
explain select * from t where col_29 between -2 and -1;
id estRows task access object operator info
TableReader_7 1.00 root partition:p1,p2 data:Selection_6
└─Selection_6 1.00 cop[tikv] ge(executor__partition__issues.t.col_29, -2), le(executor__partition__issues.t.col_29, -1)
└─TableFullScan_5 48.00 cop[tikv] table:t keep order:false
select * from t where col_29 between -2 and -1;
col_29
-1
explain select * from t where col_29 between -2 and 0;
id estRows task access object operator info
TableReader_7 2.00 root partition:p0,p1,p2 data:Selection_6
└─Selection_6 2.00 cop[tikv] ge(executor__partition__issues.t.col_29, -2), le(executor__partition__issues.t.col_29, 0)
└─TableFullScan_5 48.00 cop[tikv] table:t keep order:false
select * from t where col_29 between -2 and 0;
col_29
-1
0
explain select * from t where col_29 between -2 and 1;
id estRows task access object operator info
TableReader_7 3.00 root partition:p0,p1,p2 data:Selection_6
└─Selection_6 3.00 cop[tikv] ge(executor__partition__issues.t.col_29, -2), le(executor__partition__issues.t.col_29, 1)
└─TableFullScan_5 48.00 cop[tikv] table:t keep order:false
select * from t where col_29 between -2 and 1;
col_29
-1
0
1
23 changes: 23 additions & 0 deletions tests/integrationtest/t/executor/partition/issues.test
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,26 @@ analyze table t;
desc SELECT * FROM `t` WHERE `t`.`col_51` BETWEEN -9223372036854775808 AND 9223372036854775807;
--sorted_result
SELECT * FROM `t` WHERE `t`.`col_51` BETWEEN -9223372036854775808 AND 9223372036854775807;


# TestIssue50044
drop table if exists t;
CREATE TABLE `t` (
`col_29` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
PARTITION BY HASH (`col_29`) PARTITIONS 7;
INSERT INTO `t` VALUES (-1), (11), (-128), (39), (-46), (38), (-102), (-99), (-87), (-127), (-89), (43), (108), (59), (0), (24), (101), (37), (-103), (90), (-95), (-44), (123), (124), (-123), (-52), (-49), (-98), (-104), (-68), (2), (-24), (67), (89), (1), (-65), (36), (-109), (41), (5), (98), (-63), (-14), (127), (-6), (121), (14), (-122);
analyze table t;

explain select * from t where col_29 between -2 and -1;
--sorted_result
select * from t where col_29 between -2 and -1;

explain select * from t where col_29 between -2 and 0;
--sorted_result
select * from t where col_29 between -2 and 0;

explain select * from t where col_29 between -2 and 1;
--sorted_result
select * from t where col_29 between -2 and 1;

0 comments on commit ebe334f

Please sign in to comment.