Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lwtdev opened this issue Mar 26, 2019 · 12 comments
Assignees

Comments

@lwtdev
Copy link
Contributor

lwtdev commented Mar 26, 2019

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 lwtdev changed the title Sharding-jdbc get different shardingValue between version 3.1.0 and version 2.0.3 Sharding-jdbc get different shardingValue between version 3.1.0 and version 2.0.3 when use a string type sharding column. Mar 26, 2019
@codefairy08
Copy link
Contributor

duplicate #1754

@lwtdev
Copy link
Contributor Author

lwtdev commented Mar 27, 2019

@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?

@terrymanu
Copy link
Member

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

@lwtdev
Copy link
Contributor Author

lwtdev commented Mar 27, 2019

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

Thank you so much for your patience~

@lwtdev lwtdev closed this as completed Mar 27, 2019
@lwtdev lwtdev reopened this Mar 29, 2019
@lwtdev
Copy link
Contributor Author

lwtdev commented Mar 29, 2019

@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)。

@codefairy08
Copy link
Contributor

@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

@lwtdev
Copy link
Contributor Author

lwtdev commented Apr 1, 2019

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}

@codefairy08
Copy link
Contributor

thanks , i will try it again using your code

@codefairy08
Copy link
Contributor

@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

@lwtdev
Copy link
Contributor Author

lwtdev commented Apr 2, 2019

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 .

@codefairy08
Copy link
Contributor

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

@lwtdev
Copy link
Contributor Author

lwtdev commented Apr 3, 2019

thanks so much for your reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants