shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

Table doesn't exist when sharding value in different datasource with in expression

Open lwtdev opened this issue 7 years ago • 5 comments

Table doesn't exist when sharding value in different data source with in expression

Which version of ShardingSphere did you use?

4.0.0-RC1

Which project did you use? Sharding-JDBC or Sharding-Proxy?

Sharding-JDBC

Expected behavior

21:31:45.941 [main] INFO ShardingSphere-SQL - Actual SQL: ds_0 ::: select * from ts_order_0001 where id = 1 or id = 3 21:31:45.941 [main] INFO ShardingSphere-SQL - Actual SQL: ds_1 ::: select * from ts_order_0003 where id = 1 or id = 3

Actual behavior

21:08:30.000 [main] INFO ShardingSphere-SQL - Actual SQL: ds_0 ::: select * from ts_order_0001 where id in (1, 3) 21:08:30.000 [main] INFO ShardingSphere-SQL - Actual SQL: ds_0 ::: select * from ts_order_0003 where id in (1, 3) 21:08:30.000 [main] INFO ShardingSphere-SQL - Actual SQL: ds_1 ::: select * from ts_order_0001 where id in (1, 3) 21:08:30.000 [main] INFO ShardingSphere-SQL - Actual SQL: ds_1 ::: select * from ts_order_0003 where id in (1, 3) Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test_d_0.ts_order_0003' doesn't exist

Reason analyze (If you can)

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

dependency

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

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

sql

create database test_d_0;
create database test_d_1;
CREATE TABLE test_d_0.`ts_order_0000` (`id` BIGINT NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`));
CREATE TABLE test_d_0.`ts_order_0001` (`id` BIGINT NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`));
CREATE TABLE test_d_1.`ts_order_0002` (`id` BIGINT NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`));
CREATE TABLE test_d_1.`ts_order_0003` (`id` BIGINT NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`));

Java Code

        BufferedReader reader = new BufferedReader(new InputStreamReader(ShardingDataSourceTest.class
                        .getResourceAsStream("/sharding-config-example-1.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 * from ts_order where id in (1, 3)");

sharding rule sharding-config-example-1.yaml

dataSources: 
  ds_0: !!com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test_d_0
    username: root
    password: root135
  ds_1: !!com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test_d_1
    username: root
    password: root135

shardingRule:
  tables:
    ts_order:
      actualDataNodes: ds_0.ts_order_0000,ds_0.ts_order_0001,ds_1.ts_order_0002,ds_1.ts_order_0003
      databaseStrategy:
        inline:
          shardingColumn: id
          algorithmExpression: ds_${new BigDecimal(id).abs().divideAndRemainder(4)[1].longValue().intdiv(2)}
      tableStrategy:
        inline:
          shardingColumn: id
          algorithmExpression: ts_order_${String.format("%04d",new BigDecimal(id).abs().divideAndRemainder(4)[1].longValue())}

props:
  sql.show: true

Example codes for reproduce this issue (such as a github link).

lwtdev avatar Apr 11 '19 13:04 lwtdev

Hi @AlbertTao, thanks for your feedback. I think it may be a bug of DQL route. We will discuss how to fix it.

KomachiSion avatar Apr 12 '19 08:04 KomachiSion

Previous fixes may cause performance issues when actual nodes with large number. So we will consider new solution for this issue.

KomachiSion avatar Jul 02 '19 07:07 KomachiSion

hello , I have the same problems, when will you solve this issue, and publish a release version ? I guess the problem may happen at the TableRule class about actualDataNodes property.

I have an 8 shard databases and 64 shard columns.

both db and table's shard column is:

user_id

both db's ShardAlgorithm and table's ShardAlgorithm is MOD % n .

the actualDataNodes string just like this:

"ds_${0..8}.tb_${0..63}"

Expect actualDataNodes just like this:

ds_0-> tb_0, tb_8, tb_16, tb_24, tb_32, tb_40, tb_48, tb_56
ds_1-> tb_1, tb_9, tb_17, tb_25, tb_33, tb_41, tb_49, tb_57
ds_2-> tb_2, tb_10, tb_18, tb_26, tb_34, tb_42, tb_50, tb_58
ds_3-> tb_3, tb_11, tb_19, tb_27, tb_35, tb_43, tb_51, tb_59
ds_4-> tb_4, tb_12, tb_20, tb_28, tb_36, tb_44, tb_52, tb_60
ds_5-> tb_5, tb_13, tb_21, tb_29, tb_37, tb_45, tb_53, tb_61
ds_6-> tb_6, tb_14, tb_22, tb_30, tb_38, tb_46, tb_54, tb_62
ds_7-> tb_7, tb_15, tb_23, tb_31, tb_39, tb_47, tb_55, tb_63

Here is a code for reference only

private Map<String, List<DataNode>> convertDataNodes(List<DataNode> dataNodes) {
        LinkedHashMap<String, List<DataNode>> result = new LinkedHashMap<>();
        for (DataNode dataNode : dataNodes) {
            List<DataNode> nodes = result.get(dataNode.getDataSourceName());
            if (nodes == null) {
                nodes = new ArrayList<>();
                result.put(dataNode.getDataSourceName(), nodes);
            }
            nodes.add(dataNode);
        }
        final int size = result.size();
        int i = 0;
        for (Map.Entry<String, List<DataNode>> e : result.entrySet()) {
            final List<DataNode> tables = e.getValue();
            final List<DataNode> list   = new ArrayList<>(size);
            // notice here
            for (int j = i; j < tables.size(); j += size) {
                list.add(tables.get(j));
            }
            e.setValue(list);
            i++;
        }
        return result;
    }

xiaoma20082008 avatar Aug 08 '19 11:08 xiaoma20082008

mark

Ah39 avatar Sep 02 '19 07:09 Ah39

In this case, we could rewrite the sql where x in as where x= or x= to avoid the defects without version upgrade.

kevinleeex avatar Nov 22 '20 07:11 kevinleeex