shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

Sharding-jdbc get different shardingValue between version 3.1.0 and version 2.0.3 when use a string type sharding column.

Open lwtdev opened this issue 7 years ago • 12 comments

Use a string sharding column , suppose the sharding column value is "abcd"; When use sharding-jdbc 3.1.0 , found sharding value pass to ShardingStrategy.doSharding() was a integer value : -1 . But use version 2.0.3 , the sharding value is original value : "abcd"。

lwtdev avatar Mar 26 '19 09:03 lwtdev

duplicate #1754

codefairy08 avatar Mar 27 '19 02:03 codefairy08

@terrymanu I found the fix code, merge to version 3.1.0.1 . When will this version be released, does it has a certain time?

lwtdev avatar Mar 27 '19 05:03 lwtdev

Please subscribe mail list, you can find the scheduler of release

terrymanu avatar Mar 27 '19 06:03 terrymanu

Please subscribe mail list, you can find the scheduler of release

Thank you so much for your patience~

lwtdev avatar Mar 27 '19 08:03 lwtdev

@terrymanu @codefairy08 I just try with version 4.0.0.M1-SNAPSHOT , also get different with version 2.0.3. version 2.0.3 get value "abcd" (4 chars), but in version 4.0.0.M1-SNAPSHOT get value "'abcd'" (6chars)。

lwtdev avatar Mar 29 '19 14:03 lwtdev

@AlbertTao Please describe in detail, tell me where you get value "'abcd'" ? two column value in condition are correct.the results are shown below。 image image

codefairy08 avatar Apr 01 '19 08:04 codefairy08

Result in InlineShardingStrategy.doSharding() image

pom.xml dependency

        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-core</artifactId>
            <version>4.0.0.M1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>

Java code

        BufferedReader reader = new BufferedReader(new InputStreamReader(OrderTest.class
                .getResourceAsStream("/sharding-config-order-test.yaml"), "utf-8"));
        StringBuffer sb = new StringBuffer();
        CharBuffer charBuffer = CharBuffer.allocate(32);
        for (int count = reader.read(charBuffer); count > 0; count = reader.read(charBuffer)) {
            sb.append(charBuffer.flip());
        }

        DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(sb.toString().getBytes("utf-8"));
        Connection connection = dataSource.getConnection();
        Statement st = connection.createStatement();
        ResultSet rs = st.executeQuery("select ts_order.* FROM ts_order where order_id='abcd'");

sharding-config-order-test.yaml config

dataSources:
  ds_0: !!com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test_d_1
    username: demo4
    password: demo135
    filters: stat
    initialSize: 2
    maxActive: 80
    maxWait: 5000
    testOnBorrow: true
    testOnReturn: true
    testWhileIdle: true
  ds_1: !!com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test_d_2
    username: demo4
    password: demo135
    filters: stat
    initialSize: 2
    maxActive: 80
    maxWait: 5000
    testOnBorrow: true
    testOnReturn: true
    testWhileIdle: true

shardingRule:
  tables:
    ts_order:
      actualDataNodes: ds_${0..1}.ts_order_0${1..2}
      databaseStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: ds_${order_id.hashCode() % 2}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: ts_order_0${order_id.hashCode() % 2 + 1}

lwtdev avatar Apr 01 '19 10:04 lwtdev

thanks , i will try it again using your code

codefairy08 avatar Apr 01 '19 10:04 codefairy08

@AlbertTao this issue was fixed at 4.0.0-RC1, i use your code testing in 4.0.0-RC1 it is ok, but in 4.0.0.M1-SNAPSHOT has a wrong value , please have a try in 4.0.0-RC1. image

codefairy08 avatar Apr 02 '19 02:04 codefairy08

I had test this code in 4.0.0-RC1, and get the right value !

But I am not sure whether this problem was fix by the same code as #1754. I just found the fix code also apply to version 4.0.0.M1-SNAPSHOT。

#1754 fix code image

in version 4.0.0.M1-SNAPSHOT

@RequiredArgsConstructor
@Getter
@Setter
public final class CommonExpressionSegment implements ExpressionSegment {
    
    private final int startIndex;
    
    private final int stopIndex;
    
    private int index = -1;
    
    private Number value;
    
    private boolean text;
}

This code had not be changed in version 4.0.0.RC1 .

lwtdev avatar Apr 02 '19 09:04 lwtdev

please view code in 4.0.0.RC1, this code fixed it . image

codefairy08 avatar Apr 02 '19 10:04 codefairy08

thanks so much for your reply.

lwtdev avatar Apr 03 '19 08:04 lwtdev