go-gorm / sharding

High performance table sharding plugin for Gorm.
MIT License
249 stars 47 forks source link

go-gorm/sharding plugin would fail when combining with hints.ForceIndex in MySQL #159

Open zhaojames0707 opened 2 months ago

zhaojames0707 commented 2 months ago

GORM Playground Link

https://github.com/go-gorm/playground/pull/726

Description

I create a logical table named message, and use two underlying sharding tables: message_01 and message_02. When using go-gorm/sharding plugin alone, everything works well, as in test case TestSharding. However, after I added Clauses(hints.ForceIndex("idx_content")) in the query, the sharding plugin would fail, as in test case TestShardingAndForceIndex. The error log shows Error 1146 (42S02): Table 'gorm.message' doesn't exist. After some digging up, I found the cause of the issue comes from this line: https://github.com/go-gorm/sharding/blob/1ccc1749c6a4c2f77b23ddd515bf7c33dd6e350b/sharding.go#L307 It seems sqlparser can't handle the FORCE INDEX syntax in SQL, causing the sharding plugin fails and falls back to original(not sharded) SQL. A relevant issue: https://github.com/go-gorm/sharding/issues/91

zhaojames0707 commented 4 weeks ago

@jinzhu