shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

Left join get more data then expected

Open lwtdev opened this issue 7 years ago • 6 comments

Left Join Get more data then expected

When execute sql as follow :

select td.*, tda.* from ts_order td left join ts_order_address tda on td.id = tda.order_id where td.id = 1

Get more data then expected.

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

------------------------------------------------------------
id                  id                  order_id            address             
1                   1                   1                   china               

Actual behavior

------------------------------------------------------------
id                  id                  order_id            address             
1                   1                   1                   china               
1                   null                null                null  

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

-- prepare schemas
create database test_d_0;
create database test_d_1;

-- prepare tables
-- prepare table ts_order
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`));
-- prepare table ts_order_address
CREATE TABLE `test_d_0`.`ts_order_address_0000` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `order_id` BIGINT NOT NULL, `address` VARCHAR(255) NULL, PRIMARY KEY (`id`));
CREATE TABLE `test_d_0`.`ts_order_address_0001` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `order_id` BIGINT NOT NULL, `address` VARCHAR(255) NULL, PRIMARY KEY (`id`));
CREATE TABLE `test_d_1`.`ts_order_address_0002` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `order_id` BIGINT NOT NULL, `address` VARCHAR(255) NULL, PRIMARY KEY (`id`));
CREATE TABLE `test_d_1`.`ts_order_address_0003` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `order_id` BIGINT NOT NULL, `address` VARCHAR(255) NULL, PRIMARY KEY (`id`));

-- prepare init data
INSERT INTO `test_d_0`.`ts_order_0001` (`id`) VALUES ('1');
INSERT INTO `test_d_0`.`ts_order_address_0001` (`id`, `order_id`, `address`) VALUES ('1', '1', 'china');

Java Code

// ignore package and import

public class LeftJoinTest {
    public static void main(String[] args) throws IOException, SQLException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(LeftJoinTest.class
                        .getResourceAsStream("/sharding-config-left-join-test.yaml"), "utf-8"));
        StringBuffer sb = new StringBuffer();
        CharBuffer charBuffer = CharBuffer.allocate(1024);
        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();
        String sql = "select td.*, tda.* from ts_order td left join ts_order_address tda on td.id = tda.order_id where td.id = 1";
        ResultSet rs = st.executeQuery(sql);
        showData(rs);

        st.close();
        connection.close();
    }

    private static void showData(ResultSet rs) throws SQLException {
        System.out.println("------------------------------------------------------------");
        int colCount = rs.getMetaData().getColumnCount();
        for (int i = 0; i < colCount; i++) {
            System.out.print(String.format("%-20s", rs.getMetaData().getColumnName(i + 1)));
        }
        System.out.println();
        int dataCount = 0;
        while (rs.next()) {
            dataCount++;
            for (int i = 0; i < colCount; i++) {
                System.out.print(String.format("%-20s", rs.getObject(i + 1) + " "));
            }
            System.out.println();
        }
        System.out.println("------------------------------------------------------------");
        System.out.println("Get data rows : " + dataCount);
    }
}

Sharding Rule sharding-config-left-join-test.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_000${0..1},ds_1.ts_order_000${2..3}
      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())}
    ts_order_address:
      actualDataNodes: ds_0.ts_order_address_000${0..1},ds_1.ts_order_address_000${2..3}
      databaseStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: ds_${new BigDecimal(order_id).abs().divideAndRemainder(4)[1].longValue().intdiv(2)}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: ts_order_address_${String.format("%04d",new BigDecimal(order_id).abs().divideAndRemainder(4)[1].longValue())}

props:
  sql.show: true

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

lwtdev avatar Apr 21 '19 14:04 lwtdev

Actually, I am a little confuse whether this is a bug or not.

But I tried mycat, then get only one row data in this test case. And, suppose we can select one more time from the result(two row data), we will just get one row data.

So I report it as a bug finally.

lwtdev avatar Apr 21 '19 14:04 lwtdev

The description is very clear

codefairy08 avatar Apr 23 '19 02:04 codefairy08

The sql has two sharding tables ts_order and ts_order_address, but only has one sharding column,ts_order_address table has no sharding condition,it cause route all datasource and all tables. configure ts_order and ts_order_address to binding table,like this image

codefairy08 avatar Apr 23 '19 05:04 codefairy08

Thanks so much for your reply. This config resolve my problem ! J just found the config in document

  bindingTables: # 绑定表,也就是实际上哪些配置的sharidng表规则需要实际生效的列表,配置为yaml列表,并且允许单个条目中以逗号切割,所配置表必须已经配置为逻辑表
    - sharding_t1
    - sharding_t2,sharding_t3

But after read this document, I still don't know which scene to use, what are the side effects of this configuration that need attention?

lwtdev avatar Apr 23 '19 09:04 lwtdev

binding table means two table has same sharding strategy, two join tables can be binding table. if logic table ts_order has one row which id is 1 in the table ts_order_0001 , logic table ts_order_address also has one row which order_id is 1 in the ts_order_address_0001 , binding table is correct in his situation, logic table ts_order_address has more than one row which order_id is 1 in the ts_order_address_0001 and ts_order_address_0002, binding table is not correct in his situation. the route all sharding data source is correct

codefairy08 avatar Apr 23 '19 10:04 codefairy08

I am so grateful for your help.

lwtdev avatar Apr 23 '19 13:04 lwtdev