shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

OrConditionFiller#getTableName error

Open sofay opened this issue 7 years ago • 8 comments

Bug Report

For English only, other languages will not accept.

Before report a bug, make sure you have:

Please pay attention on issues you submitted, because we maybe need more details. If no response more than 7 days and we cannot reproduce it on current information, we will close it.

Please answer these questions before submitting your issue. Thanks!

Which version of ShardingSphere did you use?

4.0.0-RC2

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

Sharding-JDBC

Expected behavior

sql like:

select id customerId, entity_card_num entityCardNum, mobile, NAME
        from customer where company_id = ? and id not in (
        SELECT distinct customer_id
        FROM customer_account_change_record cacr inner join customer c on cacr.company_id = c.company_id and c.id =
        cacr.customer_id and cacr.company_id = ?
        WHERE cacr.created between ? and ?
        and cacr.company_id = ?
        and cacr.card_type = 1
        ) and enabled_status = 1
        order by created desc
            limit ?,?

ConditionSegment(name:created, owner:cacr, operator:BETWEEN)

Actual behavior

getTableName will return 'customer' not 'customer_account_change_record'

Reason analyze (If you can)

private String getTableName(final ShardingTableMetaData shardingTableMetaData, final ShardingRule shardingRule, final Tables tables, final ConditionSegment conditionSegment) {
        Collection<String> shardingLogicTableNames = shardingRule.getShardingLogicTableNames(tables.getTableNames());
        if (tables.isSingleTable() || tables.isSameTable() || 1 == shardingLogicTableNames.size() || shardingRule.isAllBindingTables(shardingLogicTableNames)) {
            return tables.getSingleTableName();
        }
        if (conditionSegment.getColumn().getOwner().isPresent()) {
            Optional<Table> table = tables.find(conditionSegment.getColumn().getOwner().get());
            return table.isPresent() ? table.get().getName() : "";
        } else {
            return getTableNameFromMetaData(shardingTableMetaData, tables, conditionSegment.getColumn().getName());
        }
    }

match the if condition 1 == shardingLogicTableNames.size() then direct return the first table name

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

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

sofay avatar Apr 19 '19 01:04 sofay

if tables have more than one, why 1 == shardingLogicTableNames.size() return the first table name?

sofay avatar Apr 19 '19 01:04 sofay

“1 == shardingLogicTableNames.size() return the first table name": the outer query just has one table, the column just belong to this table, ignore subquery condition use its table, it cause parsing wrong table name please show your sharding rule thanks

codefairy08 avatar Apr 22 '19 02:04 codefairy08

please show your sharding rule sharding rule is

customer_account_change_record:
    tableStrategy:
            standard:
              shardingColumn: created
              precise-algorithm-class-name: xxx.TimeSharding$PreciseTimeSharding
              range-algorithm-class-name: xxx.TimeSharding$RangeTimeSharding

sofay avatar Apr 22 '19 02:04 sofay

customer is binding table with customer_account_change_record?or customer is not sharding table?

codefairy08 avatar Apr 22 '19 02:04 codefairy08

customer is binding table with customer_account_change_record?

no. customer is single table, and customer_account_change_record is multi talbe

sofay avatar Apr 22 '19 03:04 sofay

customer is in default data source ? customer_account_change_record is in sharding da.ta source? sharding column is in subquery condition,sharding condition does not fill into SelectStatement

codefairy08 avatar Apr 22 '19 10:04 codefairy08

customer and customer_account_change_record all use shardingDataSource.

sofay avatar Apr 22 '19 11:04 sofay

The SQL has subquery in where condition, it does not always route in one sharding datasource, the SQL is not supported now

codefairy08 avatar Apr 23 '19 06:04 codefairy08