pingcap / tidb

TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
https://pingcap.com
Apache License 2.0
37.28k stars 5.84k forks source link

Incorrect result when using HashJoin on partitioned tables #19450

Closed zyguan closed 4 years ago

zyguan commented 4 years ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

drop table if exists t1, t2;
create table t1  (c_int int, c_str varchar(40), c_decimal decimal(12, 6), primary key (c_int));
create table t2  (c_int int, c_str varchar(40), c_decimal decimal(12, 6), primary key (c_int)) partition by hash (c_int) partitions 4 ;
insert into t1 values (1, 'romantic robinson', 4.436), (2, 'stoic chaplygin', 9.826), (3, 'vibrant shamir', 6.300), (4, 'hungry wilson', 4.900), (5, 'naughty swartz', 9.524);
insert into t2 select * from t1 ;
select * from t1 where c_decimal in (select c_decimal from t2 where t1.c_int = t2.c_int or t1.c_int = t2.c_int and t1.c_str > t2.c_str);

2. What did you expect to see? (Required)

+-------+-------------------+-----------+
| c_int | c_str             | c_decimal |
+-------+-------------------+-----------+
|     1 | romantic robinson |  4.436000 |
|     2 | stoic chaplygin   |  9.826000 |
|     3 | vibrant shamir    |  6.300000 |
|     4 | hungry wilson     |  4.900000 |
|     5 | naughty swartz    |  9.524000 |
+-------+-------------------+-----------+

3. What did you see instead (Required)

+-------+---------------+-----------+
| c_int | c_str         | c_decimal |
+-------+---------------+-----------+
|     4 | hungry wilson |  4.900000 |
+-------+---------------+-----------+

4. What is your TiDB version? (Required)

release-4.0 @ 61f1150e45f929fac9b4b82fc475840efd6b43a1

XuHuaiyu commented 4 years ago

This can be reproduced on TiKV. If we test this on mocktikv, we'll get an index out of range error:

[2020/08/26 15:46:20.698 +08:00] [ERROR] [conn.go:730] ["command dispatched failed"] [conn=1] [connInfo="id:1, addr:127.0.0.1:51468 status:10, collation:utf8_general_ci, user:root"] [command=Query] [status="inTxn:0, autocommit:1"] [sql="select * from t1 where c_decimal in (select c_decimal from t2 where t1.c_int = t2.c_int or t1.c_int = t2.c_int and t1.c_str > t2.c_str)"] [txn_mode=OPTIMISTIC] [err="runtime error: index out of range [2] with length 2\ngithub.com/pingcap/tidb/store/tikv.(*copIteratorWorker).handleTask.func1\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/tikv/coprocessor.go:698\nruntime.gopanic\n\t/Users/xuhuaiyu/.gvm/gos/go1.13/src/runtime/panic.go:679\nruntime.goPanicIndex\n\t/Users/xuhuaiyu/.gvm/gos/go1.13/src/runtime/panic.go:75\ngithub.com/pingcap/tidb/util/rowcodec.(*BytesDecoder).decodeToBytesInternal\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/util/rowcodec/decoder.go:391\ngithub.com/pingcap/tidb/util/rowcodec.(*BytesDecoder).DecodeToBytes\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/util/rowcodec/decoder.go:423\ngithub.com/pingcap/tidb/store/mockstore/mocktikv.getRowData\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/mockstore/mocktikv/executor.go:675\ngithub.com/pingcap/tidb/store/mockstore/mocktikv.(*tableScanExec).getRowFromRange\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/mockstore/mocktikv/executor.go:244\ngithub.com/pingcap/tidb/store/mockstore/mocktikv.(*tableScanExec).Next\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/mockstore/mocktikv/executor.go:166\ngithub.com/pingcap/tidb/store/mockstore/mocktikv.(*selectionExec).Next\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/mockstore/mocktikv/executor.go:481\ngithub.com/pingcap/tidb/store/mockstore/mocktikv.(*rpcHandler).handleCopDAGRequest\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/mockstore/mocktikv/cop_handler_dag.go:73\ngithub.com/pingcap/tidb/store/mockstore/mocktikv.(*RPCClient).SendRequest\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/mockstore/mocktikv/rpc.go:1031\ngithub.com/pingcap/tidb/store/tikv.reqCollapse.SendRequest\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/tikv/client_collapse.go:49\ngithub.com/pingcap/tidb/store/tikv.(*RegionRequestSender).sendReqToRegion\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/tikv/region_request.go:358\ngithub.com/pingcap/tidb/store/tikv.(*RegionRequestSender).SendReqCtx\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/tikv/region_request.go:251\ngithub.com/pingcap/tidb/store/tikv.(*clientHelper).SendReqCtx\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/tikv/coprocessor.go:879\ngithub.com/pingcap/tidb/store/tikv.(*copIteratorWorker).handleTaskOnce\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/tikv/coprocessor.go:778\ngithub.com/pingcap/tidb/store/tikv.(*copIteratorWorker).handleTask\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/tikv/coprocessor.go:708\ngithub.com/pingcap/tidb/store/tikv.(*copIteratorWorker).run\n\t/Users/xuhuaiyu/Development/GOPATH/src/github.com/pingcap/tidb/store/tikv/coprocessor.go:499\nruntime.goexit\n\t/Users/xuhuaiyu/.gvm/gos/go1.13/src/runtime/asm_amd64.s:1357"]
lzmhhh123 commented 4 years ago

19514 only fix the master branch. I will file another pull request for release-4.0.

tiancaiamao commented 4 years ago

It should be close after 4.0 is fixed