apache / shardingsphere

Distributed SQL transaction & query engine for data sharding, scaling, encryption, and more - on any database.
Apache License 2.0
19.76k stars 6.69k forks source link

[Discussion] Update `<T extends Comparable<?>>` to `<T>` in ShardingAlgorithm, KeyGenerateAlgorithm etc #24837

Open sandynz opened 1 year ago

sandynz commented 1 year ago

Feature Request

Is your feature request related to a problem?

Yes

MySQL VARBINARY column value is byte[] in Java, it doesn't implement Comparable interface. But some kernel interfaces limit generic type to sub-type of Comparable, it cause some feature doesn't work or limited. Related code example:

public interface StandardShardingAlgorithm<T extends Comparable<?>> extends ShardingAlgorithm {

public final class OrderByValue implements Comparable<OrderByValue> {

VARBINARY column could be compared and sorted in MYSQL, e.g.

mysql> show create table t_varbin;
+----------+-----------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                          |
+----------+-----------------------------------------------------------------------------------------------------------------------+
| t_varbin | CREATE TABLE `t_varbin` (
  `id` varbinary(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+----------+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.04 sec)

mysql> select * from t_varbin;
+-----+
| id  |
+-----+
| 1   |
| a12 |
| b34 |
+-----+
3 rows in set (0.00 sec)

mysql> select * from t_varbin where id>'a' order by id;
+-----+
| id  |
+-----+
| a12 |
| b34 |
+-----+
2 rows in set (0.12 sec)

Problem examples: 1, Insert failed on MySQL VARBINARY column when it is sharding column. Refer to issue #24210 for more details. There's partial fix #24285 for now. Related classes: StandardShardingAlgorithm, ComplexKeysShardingAlgorithm, HintShardingAlgorithm, and sub-classes.

2, Order by MySQL VARBINARY primary key failed. Refer to issue #24107 for more details. Related classes: OrderByValue

3, Insert failed on MySQL VARBINARY column when keyGenerateStrategy is configured. Refer to #24135 for more details. Related classes: GeneratedKeyContextEngine

4, Could not customize KeyGenerateAlgorithm for VARBINARY column. Since generateKey return Comparable. Related classes: KeyGenerateAlgorithm

Describe the feature you would like.

Could we update these interfaces and classes generic type definition, change <T extends Comparable<?>> to <T>, in later version, e.g. 5.4.0, 5.5.0 etc (current stable version is 5.3.1).

strongduanmu commented 1 year ago

@sandynz Thanks for your suggestion, I think it's time to support more data types. We will consider the solution and support it in the next version.