shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

Must have sharding column with subquery error occurred when use “exists”.

Open lwtdev opened this issue 7 years ago • 12 comments

Must have sharding column with subquery error occurred when use “exists”.

When execute sql as follow :

select * from ts_order td where exists (select 1 from ts_order_address tda where tda.order_id = 1);

Get error : Must have sharding column with subquery.

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

When I use version 3.0.1 get result without error :

[main] INFO ShardingSphere-SQL - Actual SQL: ds_0 ::: select * from ts_order_0000 td where exists (select 1 from ts_order_address_0000 tda where tda.order_id = 1);
[main] INFO ShardingSphere-SQL - Actual SQL: ds_0 ::: select * from ts_order_0001 td where exists (select 1 from ts_order_address_0001 tda where tda.order_id = 1);
[main] INFO ShardingSphere-SQL - Actual SQL: ds_1 ::: select * from ts_order_0002 td where exists (select 1 from ts_order_address_0002 tda where tda.order_id = 1);
[main] INFO ShardingSphere-SQL - Actual SQL: ds_1 ::: select * from ts_order_0003 td where exists (select 1 from ts_order_address_0003 tda where tda.order_id = 1);
------------------------------------------------------------
id                  
1                   
------------------------------------------------------------       

Actual behavior

Exception in thread "main" java.lang.IllegalStateException: Must have sharding column with subquery.

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 static void main(String[] args) throws Exception {
                queryTest("sharding-config-subquery-test2.yaml",
                "select * from ts_order td where exists (select 1 from ts_order_address tda where tda.order_id = 1);");
    }

    private static void queryTest(String conifgResourcePath, String querySql) throws Exception {
        String shardingConfig = loadFromResource(conifgResourcePath);
        DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(shardingConfig.getBytes("utf-8"));
        Connection connection = dataSource.getConnection();
        Statement st = connection.createStatement();
        ResultSet rs = st.executeQuery(querySql);
        showData(rs);
        connection.close();
    }

    private static String loadFromResource(String path) throws Exception{
        BufferedReader reader = new BufferedReader(new InputStreamReader(Class.class
                .getResourceAsStream("/" + path), "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());
        }
        return sb.toString();
    }

    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-subquery-test2.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())}

  bindingTables:
    - ts_order, ts_order_address
props:
  sql.show: true

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

lwtdev avatar Apr 24 '19 13:04 lwtdev

If use the case run another sql :

select * from ts_order td where exists (select 1 from ts_order_address tda where tda.id = 1);

The result is the opposite between version 4.0.0-RC1 and 3.0.1. In version 3.0.1 , get error :

Exception in thread "main" java.lang.IllegalStateException: Must have sharding column with subquery.

In version 4.0.0-RC, get result without error:

[main] INFO ShardingSphere-SQL - Actual SQL: ds_0 ::: select * from ts_order_0001 td where exists (select 1 from ts_order_address_0001 tda where tda.id = 1);
------------------------------------------------------------
id                  
1                   
------------------------------------------------------------
Get Data rows 1

lwtdev avatar Apr 24 '19 13:04 lwtdev

@AlbertTao select statement with subquery just only support routing single sharding datasource

codefairy08 avatar Apr 26 '19 01:04 codefairy08

@codefairy08 ts_order_address (tda) sharding column is 'order_id' not 'id'. But in version 4.0.0-RC1 , subquery with sharding column cause error, subquery without sharding column get result without error .

And its performance is different from the version 3.0.1.

lwtdev avatar Apr 26 '19 02:04 lwtdev

@AlbertTao Sharding column is in subquery, outer select may route all sharding datasource, It is very dangerous under the big data volume。 What are the performance differences between the two versions?

codefairy08 avatar Apr 26 '19 07:04 codefairy08

@codefairy08 please run test case in version 4.0.0-RC1 and version 3.0.1 with this two sql:

select * from ts_order td where exists (select 1 from ts_order_address tda where tda.order_id = 1);
select * from ts_order td where exists (select 1 from ts_order_address tda where tda.id = 1);

In my first comment, I have described the differences between the two versions when use first sql. And the second comment has the differences description about the second sql.

lwtdev avatar Apr 27 '19 04:04 lwtdev

@AlbertTao I know that the execution results of the two versions are different. you said “And its performance is different from the version 3.0.1.” I thought the performance is different.

codefairy08 avatar Apr 28 '19 06:04 codefairy08

@codefairy08 sorry, I just choose wrong word. I mean the result is different.

lwtdev avatar Apr 28 '19 06:04 lwtdev

@codefairy08 So, why the result was different between version 3.0.1 and version 4.0.0-RC1, And which version was right ?

lwtdev avatar Apr 29 '19 02:04 lwtdev

@AlbertTao the Select with subquery is executed successfully with sharding condition which is routed one sharding node。This restriction was added to after 3.0.1 version。 The reason is: 1 there are more sharding tables in subquery,the execution result may be wrong。 2 there is a lot of data in the table, the execution time will be very long.

codefairy08 avatar Apr 29 '19 08:04 codefairy08

@codefairy08 thanks so much for you replay.

lwtdev avatar Apr 30 '19 02:04 lwtdev

@codefairy08 what is the maven address of version 3.0.1? Thank you this??? 图片 but not right

tanhuang2016 avatar Sep 26 '19 03:09 tanhuang2016

Seems you guys do not consider this scenario: "table join itself", which also throws this "must have" exception. e.g. we optimize limit sql by using "select a.* from table a join(select id from table limit n, m) r using(id). I think this is a bug.

runjia1987 avatar Dec 23 '19 09:12 runjia1987