shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

Join condition not considered for query rewrite

Open JamesRTaylor opened this issue 1 year ago • 0 comments

Bug Report

Which version of ShardingSphere did you use?

5.5.1-SNAPSHOT

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

Testing through unit tests

Expected behavior

The join condition should help determine how the logical query is rewritten.

Actual behavior

Only the name of the column referenced seems to have an impact on how the logical query is rewritten. The join condition doesn't impact it at all. For example, if an inequality join condition is used, the join would need to be run across all shards, while with an equality join, you'd be able to run it against a single shard.

Reason analyze (If you can)

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

Add the following to select.xml and run ShardingSQLRewriterIT:

<rewrite-assertion id="select_with_inequality_join">
    <input sql="SELECT * FROM t_account o, t_account_detail i WHERE o.account_id>i.account_id AND o.account_id = ?" parameters="100" />
    <output sql="SELECT * FROM t_account_0 o, t_account_detail_0 i WHERE o.account_id > i.account_id AND o.account_id = ?" parameters="100" />
    <output sql="SELECT * FROM t_account_0 o, t_account_detail_1 i WHERE o.account_id>i.account_id AND o.account_id = ?" parameters="100" />
</rewrite-assertion>

ShardingSphere only produces the first output query, which is correct if the join condition is an equality join, but not if it's an inequality join. Note that if there's no correlation between the two tables being joined, ShardingSphere produces the expected output queries. For example, this is correct:

<rewrite-assertion id="select_with_unconstrained_user">
    <input sql="SELECT * FROM t_account o, t_user u WHERE o.account_id=u.id AND o.account_id = ?" parameters="100" />
    <output sql="SELECT * FROM t_account_0 o, t_user_0 u WHERE o.account_id=u.id AND o.account_id = ?" parameters="100" />
    <output sql="SELECT * FROM t_account_0 o, t_user_1 u WHERE o.account_id=u.id AND o.account_id = ?" parameters="100" />
</rewrite-assertion>

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

JamesRTaylor avatar Jun 11 '24 23:06 JamesRTaylor