From 508dbc1be4fa99f758b5aba0477baaf20c5760f0 Mon Sep 17 00:00:00 2001 From: heroyf <13788978852@163.com> Date: Sun, 26 Mar 2023 17:53:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=BA=93=E5=88=86=E8=A1=A8=EF=BC=9A?= =?UTF-8?q?=E5=88=86=E7=89=87=E7=BB=93=E6=9E=9C=E4=B8=8D=E7=AC=A6=E9=A2=84?= =?UTF-8?q?=E6=9C=9F=E4=BF=AE=E5=A4=8D=20(#174)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .CHANGELOG.md | 1 + go.mod | 4 +- go.sum | 8 +- internal/sharding/hash/hash.go | 2 +- internal/sharding/hash/shadow_hash.go | 2 +- sharding_select.go | 54 +--- sharding_select_test.go | 402 ++++++++++++++++---------- 7 files changed, 274 insertions(+), 199 deletions(-) diff --git a/.CHANGELOG.md b/.CHANGELOG.md index 01ff2f2..7c0342e 100644 --- a/.CHANGELOG.md +++ b/.CHANGELOG.md @@ -17,6 +17,7 @@ - [eorm: 增强的 ShardingAlgorithm 设计与实现](https://github.com/ecodeclub/eorm/pull/161) - [eorm: 分库分表: Merger排序实现](https://github.com/ecodeclub/eorm/pull/166) - [eorm: BasicTypeValue重命名](https://github.com/ecodeclub/eorm/pull/177) +- [eorm: 分库分表: hash、shadow_hash算法不符合预期](https://github.com/ecodeclub/eorm/pull/174) ## v0.0.1: - [Init Project](https://github.com/ecodeclub/eorm/pull/1) diff --git a/go.mod b/go.mod index 85e2c17..d229c4a 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.18 require ( github.com/DATA-DOG/go-sqlmock v1.5.0 github.com/go-sql-driver/mysql v1.6.0 - github.com/gotomicro/ekit v0.0.0-20220612043755-81d8a8fb714a - github.com/mattn/go-sqlite3 v1.14.13 + github.com/gotomicro/ekit v0.0.6 + github.com/mattn/go-sqlite3 v1.14.15 github.com/stretchr/testify v1.7.1 github.com/valyala/bytebufferpool v1.0.0 go.uber.org/multierr v1.9.0 diff --git a/go.sum b/go.sum index 5e10e16..e9eb1d2 100644 --- a/go.sum +++ b/go.sum @@ -5,10 +5,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/gotomicro/ekit v0.0.0-20220612043755-81d8a8fb714a h1:Zpr8+lnnuUu3VpLZzde0ScX0FJlCZRBQhuVUhMIyh70= -github.com/gotomicro/ekit v0.0.0-20220612043755-81d8a8fb714a/go.mod h1:knWKyV4PLI/HhpXjdOCkH3v3w6RvqDiNvq2is/kjSyY= -github.com/mattn/go-sqlite3 v1.14.13 h1:1tj15ngiFfcZzii7yd82foL+ks+ouQcj8j/TPq3fk1I= -github.com/mattn/go-sqlite3 v1.14.13/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/gotomicro/ekit v0.0.6 h1:Tw3vcx8hltUzFmK7zkp6/5OGlE+ceuq6wha7KxBfpaA= +github.com/gotomicro/ekit v0.0.6/go.mod h1:LpstTheKiI/j532rejAlTwPRemwFQXhyqdH6lpzr4wk= +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/internal/sharding/hash/hash.go b/internal/sharding/hash/hash.go index 0d97fd8..72069da 100644 --- a/internal/sharding/hash/hash.go +++ b/internal/sharding/hash/hash.go @@ -122,7 +122,7 @@ func (h *Hash) Sharding(ctx context.Context, req sharding.Request) (sharding.Res } skVal, ok := req.SkValues[h.ShardingKey] if !ok { - return sharding.EmptyResult, nil + return sharding.Result{Dsts: h.Broadcast(ctx)}, nil } dbName := h.DBPattern.Name if !h.DBPattern.NotSharding && strings.Contains(dbName, "%d") { diff --git a/internal/sharding/hash/shadow_hash.go b/internal/sharding/hash/shadow_hash.go index 4030f45..2fffce0 100644 --- a/internal/sharding/hash/shadow_hash.go +++ b/internal/sharding/hash/shadow_hash.go @@ -50,7 +50,7 @@ func (h *ShadowHash) Sharding(ctx context.Context, req sharding.Request) (shardi } skVal, ok := req.SkValues[h.ShardingKey] if !ok { - return sharding.EmptyResult, nil + return sharding.Result{Dsts: h.Broadcast(ctx)}, nil } dbName := h.DBPattern.Name if !h.DBPattern.NotSharding && strings.Contains(dbName, "%d") { diff --git a/sharding_select.go b/sharding_select.go index fb90ca4..5eb7a5d 100644 --- a/sharding_select.go +++ b/sharding_select.go @@ -17,9 +17,10 @@ package eorm import ( "context" "database/sql" - "fmt" "sync" + "github.com/gotomicro/ekit/slice" + "github.com/ecodeclub/eorm/internal/sharding" "github.com/ecodeclub/eorm/internal/errs" @@ -165,36 +166,20 @@ func (s *ShardingSelector[T]) findDstByPredicate(ctx context.Context, pre Predic if err != nil { return sharding.EmptyResult, err } - if len(left.Dsts) == 0 { - return s.findDstByPredicate(ctx, pre.right.(Predicate)) - } right, err := s.findDstByPredicate(ctx, pre.right.(Predicate)) if err != nil { return sharding.EmptyResult, err } - if len(right.Dsts) == 0 { - return left, nil - } return s.mergeAnd(left, right), nil case opOr: left, err := s.findDstByPredicate(ctx, pre.left.(Predicate)) if err != nil { return sharding.EmptyResult, err } - if len(left.Dsts) == 0 { - return sharding.Result{ - Dsts: s.meta.ShardingAlgorithm.Broadcast(ctx), - }, nil - } right, err := s.findDstByPredicate(ctx, pre.right.(Predicate)) if err != nil { return sharding.EmptyResult, err } - if len(right.Dsts) == 0 { - return sharding.Result{ - Dsts: s.meta.ShardingAlgorithm.Broadcast(ctx), - }, nil - } return s.mergeOR(left, right), nil case opEQ: col, isCol := pre.left.(Column) @@ -209,38 +194,19 @@ func (s *ShardingSelector[T]) findDstByPredicate(ctx context.Context, pre Predic } } +// mergeAnd 两个分片结果的交集 func (*ShardingSelector[T]) mergeAnd(left, right sharding.Result) sharding.Result { - dsts := make([]sharding.Dst, 0, len(left.Dsts)+len(right.Dsts)) - for _, r := range right.Dsts { - exist := false - for _, l := range left.Dsts { - if r.Equals(l) { - exist = true - } - } - if exist { - dsts = append(dsts, r) - } - } + dsts := slice.IntersectSetFunc[sharding.Dst](left.Dsts, right.Dsts, func(src, dst sharding.Dst) bool { + return src.Equals(dst) + }) return sharding.Result{Dsts: dsts} } +// mergeAnd 两个分片结果的并集 func (*ShardingSelector[T]) mergeOR(left, right sharding.Result) sharding.Result { - dsts := make([]sharding.Dst, 0, len(left.Dsts)+len(right.Dsts)) - m := make(map[string]bool, 8) - for _, r := range right.Dsts { - for _, l := range left.Dsts { - if r.NotEquals(l) { - tbl := fmt.Sprintf("%s_%s_%s", l.Name, l.DB, l.Table) - if _, ok := m[tbl]; ok { - continue - } - dsts = append(dsts, l) - m[tbl] = true - } - } - dsts = append(dsts, r) - } + dsts := slice.UnionSetFunc[sharding.Dst](left.Dsts, right.Dsts, func(src, dst sharding.Dst) bool { + return src.Equals(dst) + }) return sharding.Result{Dsts: dsts} } diff --git a/sharding_select_test.go b/sharding_select_test.go index 96d39d5..b90cc04 100644 --- a/sharding_select_test.go +++ b/sharding_select_test.go @@ -80,6 +80,51 @@ func TestShardingSelector_shadow_Build(t *testing.T) { }, }, }, + { + name: "only eq broadcast", + builder: func() sharding.QueryBuilder { + s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123)) + return s + }(), + qs: []*sharding.Query{ + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE `order_id`=?;", + Args: []any{123}, + DB: "shadow_order_db_0", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_0`.`shadow_order_tab_1` WHERE `order_id`=?;", + Args: []any{123}, + DB: "shadow_order_db_0", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_0`.`shadow_order_tab_2` WHERE `order_id`=?;", + Args: []any{123}, + DB: "shadow_order_db_0", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE `order_id`=?;", + Args: []any{123}, + DB: "shadow_order_db_1", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_1`.`shadow_order_tab_1` WHERE `order_id`=?;", + Args: []any{123}, + DB: "shadow_order_db_1", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_1`.`shadow_order_tab_2` WHERE `order_id`=?;", + Args: []any{123}, + DB: "shadow_order_db_1", + Datasource: "0.db.cluster.company.com:3306", + }, + }, + }, { name: "columns", builder: func() sharding.QueryBuilder { @@ -355,42 +400,12 @@ func TestShardingSelector_shadow_Build(t *testing.T) { return s }(), qs: []*sharding.Query{ - { - SQL: "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "shadow_order_db_0", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_1` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "shadow_order_db_0", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_2` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "shadow_order_db_0", - Datasource: "0.db.cluster.company.com:3306", - }, { SQL: "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", Args: []any{123, 181, 234}, DB: "shadow_order_db_1", Datasource: "0.db.cluster.company.com:3306", }, - { - SQL: "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_1` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "shadow_order_db_1", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_2` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "shadow_order_db_1", - Datasource: "0.db.cluster.company.com:3306", - }, }, }, { @@ -624,6 +639,27 @@ func TestShardingSelector_onlyDataSource_Build(t *testing.T) { }, }, }, + { + name: "only eq broadcast", + builder: func() sharding.QueryBuilder { + s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123)) + return s + }(), + qs: []*sharding.Query{ + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db", + Datasource: "1.db.cluster.company.com:3306", + }, + }, + }, { name: "columns", builder: func() sharding.QueryBuilder { @@ -851,12 +887,6 @@ func TestShardingSelector_onlyDataSource_Build(t *testing.T) { return s }(), qs: []*sharding.Query{ - { - SQL: "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db", - Datasource: "0.db.cluster.company.com:3306", - }, { SQL: "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", Args: []any{123, 181, 234}, @@ -1066,6 +1096,33 @@ func TestShardingSelector_onlyTable_Build(t *testing.T) { }, }, }, + { + name: "only eq broadcast", + builder: func() sharding.QueryBuilder { + s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123)) + return s + }(), + qs: []*sharding.Query{ + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab_0` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab_1` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab_2` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db", + Datasource: "0.db.cluster.company.com:3306", + }, + }, + }, { name: "columns", builder: func() sharding.QueryBuilder { @@ -1299,14 +1356,19 @@ func TestShardingSelector_onlyTable_Build(t *testing.T) { DB: "order_db", Datasource: "0.db.cluster.company.com:3306", }, + }, + }, + { + name: "where or-and all", + builder: func() sharding.QueryBuilder { + s := NewShardingSelector[Order](shardingDB). + Select(C("OrderId"), C("Content")). + Where(C("UserId").EQ(123).Or(C("UserId").EQ(181).And(C("UserId").EQ(234)))) + return s + }(), + qs: []*sharding.Query{ { - SQL: "SELECT `order_id`,`content` FROM `order_db`.`order_tab_1` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db`.`order_tab_2` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", + SQL: "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", Args: []any{123, 181, 234}, DB: "order_db", Datasource: "0.db.cluster.company.com:3306", @@ -1508,6 +1570,28 @@ func TestShardingSelector_onlyDB_Build(t *testing.T) { }, }, }, + { + name: "only eq broadcast", + builder: func() sharding.QueryBuilder { + s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123)) + return s + }(), + qs: []*sharding.Query{ + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_0", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_1", + Datasource: "0.db.cluster.company.com:3306", + }, + }, + }, + { name: "columns", builder: func() sharding.QueryBuilder { @@ -1735,12 +1819,6 @@ func TestShardingSelector_onlyDB_Build(t *testing.T) { return s }(), qs: []*sharding.Query{ - { - SQL: "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_0", - Datasource: "0.db.cluster.company.com:3306", - }, { SQL: "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", Args: []any{123, 181, 234}, @@ -1951,6 +2029,87 @@ func TestShardingSelector_all_Build(t *testing.T) { }, }, }, + { + name: "only eq broadcast", + builder: func() sharding.QueryBuilder { + s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123)) + return s + }(), + qs: []*sharding.Query{ + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_0` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_0", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_1` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_0", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_2` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_0", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_0` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_1", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_1` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_1", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_2` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_1", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_0` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_0", + Datasource: "1.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_1` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_0", + Datasource: "1.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_2` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_0", + Datasource: "1.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_0` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_1", + Datasource: "1.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_1` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_1", + Datasource: "1.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_2` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_1", + Datasource: "1.db.cluster.company.com:3306", + }, + }, + }, { name: "columns", builder: func() sharding.QueryBuilder { @@ -2298,78 +2457,12 @@ func TestShardingSelector_all_Build(t *testing.T) { return s }(), qs: []*sharding.Query{ - { - SQL: "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_0", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_0", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_0", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_1", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_1", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_1", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_0", - Datasource: "1.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_0", - Datasource: "1.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_0", - Datasource: "1.db.cluster.company.com:3306", - }, { SQL: "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", Args: []any{123, 181, 234}, DB: "order_db_1", Datasource: "1.db.cluster.company.com:3306", }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_1", - Datasource: "1.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_1", - Datasource: "1.db.cluster.company.com:3306", - }, }, }, { @@ -2675,6 +2768,51 @@ func TestShardingSelector_Build(t *testing.T) { }, }, }, + { + name: "only eq broadcast", + builder: func() sharding.QueryBuilder { + s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123)) + return s + }(), + qs: []*sharding.Query{ + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_0` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_0", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_1` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_0", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_2` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_0", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_0` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_1", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_1` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_1", + Datasource: "0.db.cluster.company.com:3306", + }, + { + SQL: "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_2` WHERE `order_id`=?;", + Args: []any{123}, + DB: "order_db_1", + Datasource: "0.db.cluster.company.com:3306", + }, + }, + }, { name: "columns", builder: func() sharding.QueryBuilder { @@ -2950,42 +3088,12 @@ func TestShardingSelector_Build(t *testing.T) { return s }(), qs: []*sharding.Query{ - { - SQL: "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_0", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_0", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_0", - Datasource: "0.db.cluster.company.com:3306", - }, { SQL: "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", Args: []any{123, 181, 234}, DB: "order_db_1", Datasource: "0.db.cluster.company.com:3306", }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_1", - Datasource: "0.db.cluster.company.com:3306", - }, - { - SQL: "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));", - Args: []any{123, 181, 234}, - DB: "order_db_1", - Datasource: "0.db.cluster.company.com:3306", - }, }, }, { @@ -3175,7 +3283,7 @@ func TestShardingSelector_Build(t *testing.T) { } } -func TestSardingSelector_Get(t *testing.T) { +func TestShardingSelector_Get(t *testing.T) { r := model.NewMetaRegistry() _, err := r.Register(&test.OrderDetail{}, model.WithTableShardingAlgorithm(&hash.Hash{ @@ -3217,7 +3325,7 @@ func TestSardingSelector_Get(t *testing.T) { name: "not gen sharding query", s: func() *ShardingSelector[test.OrderDetail] { builder := NewShardingSelector[test.OrderDetail](shardingDB). - Where(C("ItemId").EQ(12)) + Where(C("OrderId").EQ(12).And(C("OrderId").EQ(14))) return builder }(), mockOrder: func(mock sqlmock.Sqlmock) {},