Open wengsy150943 opened 5 months ago
We explain the execution plan. It seems that TiDB will cast blob into other types before right shift, which applies incorrect truncation.
explain select * from t2,t3 where (t2.a >> 8) = t3.a;
+------------------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+------------------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------+
| HashJoin_12 | 1.00 | root | | inner join, equal:[eq(Column#5, Column#6)] |
| ├─Projection_17(Build) | 1.00 | root | | test.t3.a, cast(test.t3.a, double BINARY)->Column#6 |
| │ └─TableReader_19 | 1.00 | root | | data:TableFullScan_18 |
| │ └─TableFullScan_18 | 1.00 | cop[tikv] | table:t3 | keep order:false, stats:pseudo |
| └─Projection_14(Probe) | 1.00 | root | | test.t2.a, cast(rightshift(cast(test.t2.a, bigint(65535) BINARY), 8), double UNSIGNED BINARY)->Column#5 |
| └─TableReader_16 | 1.00 | root | | data:TableFullScan_15 |
| └─TableFullScan_15 | 1.00 | cop[tikv] | table:t2 | keep order:false, stats:pseudo |
+------------------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------+
MySQL's right shift supports two types: bigint
and binary
[1]. So no casting is applied to t2.a >> 8
and it evaluates to a binary
as well. Whereas TiDB so far only supports bigint
for right shift [2], this is why an implicit cast to bigint
is applied to t2.a
and this cast evaluates to 0
(with warning saying truncation). Then t2.a >> 8
still evaluates to a bigint
0
. Then it is compared with binary
(t3.a
), causing both sides being casted to double
, and the comparison is true
for 0
and 0
.
[1] https://dev.mysql.com/doc/refman/8.4/en/bit-functions.html#operator_right-shift [2] https://github.com/pingcap/tidb/blob/29fc940ae4ea01482088994e14777c9765f913f7/pkg/expression/builtin_op.go#L420
Bug Report
Please answer these questions before submitting your issue. Thanks!
1. Minimal reproduce step (Required)
init db
Then running:
2. What did you expect to see? (Required)
MySQL8.0.33 shows empty set for each query.
3. What did you see instead (Required)
TiDB 8.1 shows one row with warning.
4. What is your TiDB version? (Required)