github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/sharding_select_test.go (about)

     1  // Copyright 2021 ecodeclub
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package eorm
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/ecodeclub/eorm/internal/datasource/masterslave/slaves/roundrobin"
    24  
    25  	"github.com/ecodeclub/eorm/internal/datasource/masterslave"
    26  	"github.com/ecodeclub/eorm/internal/datasource/masterslave/slaves"
    27  
    28  	"github.com/ecodeclub/eorm/internal/datasource/shardingsource"
    29  
    30  	"github.com/ecodeclub/eorm/internal/datasource/cluster"
    31  
    32  	"github.com/DATA-DOG/go-sqlmock"
    33  	"github.com/ecodeclub/eorm/internal/datasource"
    34  	"github.com/ecodeclub/eorm/internal/errs"
    35  	"github.com/ecodeclub/eorm/internal/sharding"
    36  	"github.com/ecodeclub/eorm/internal/sharding/hash"
    37  	"github.com/ecodeclub/eorm/internal/test"
    38  
    39  	"github.com/ecodeclub/eorm/internal/model"
    40  	"github.com/stretchr/testify/assert"
    41  	"github.com/stretchr/testify/require"
    42  )
    43  
    44  func TestShardingSelector_shadow_Build(t *testing.T) {
    45  	r := model.NewMetaRegistry()
    46  	_, err := r.Register(&Order{},
    47  		model.WithTableShardingAlgorithm(&hash.ShadowHash{
    48  			Hash: &hash.Hash{
    49  				ShardingKey:  "UserId",
    50  				DBPattern:    &hash.Pattern{Name: "order_db_%d", Base: 2},
    51  				TablePattern: &hash.Pattern{Name: "order_tab_%d", Base: 3},
    52  				DsPattern:    &hash.Pattern{Name: "0.db.cluster.company.com:3306", NotSharding: true},
    53  			},
    54  			Prefix: "shadow_",
    55  		}))
    56  	require.NoError(t, err)
    57  	m := map[string]*masterslave.MasterSlavesDB{
    58  		"order_db_0": MasterSlavesMemoryDB(),
    59  		"order_db_1": MasterSlavesMemoryDB(),
    60  		"order_db_2": MasterSlavesMemoryDB(),
    61  	}
    62  	clusterDB := cluster.NewClusterDB(m)
    63  	ds := map[string]datasource.DataSource{
    64  		"0.db.cluster.company.com:3306": clusterDB,
    65  	}
    66  	shardingDB, err := OpenDS("sqlite3",
    67  		shardingsource.NewShardingDataSource(ds), DBWithMetaRegistry(r))
    68  	require.NoError(t, err)
    69  
    70  	testCases := []struct {
    71  		name    string
    72  		builder sharding.QueryBuilder
    73  		qs      []sharding.Query
    74  		wantErr error
    75  	}{
    76  		{
    77  			name: "only eq",
    78  			builder: func() sharding.QueryBuilder {
    79  				s := NewShardingSelector[Order](shardingDB).Where(C("UserId").EQ(123))
    80  				return s
    81  			}(),
    82  			qs: []sharding.Query{
    83  				{
    84  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE `user_id`=?;",
    85  					Args:       []any{123},
    86  					DB:         "shadow_order_db_1",
    87  					Datasource: "0.db.cluster.company.com:3306",
    88  				},
    89  			},
    90  		},
    91  		{
    92  			name: "only eq broadcast",
    93  			builder: func() sharding.QueryBuilder {
    94  				s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123))
    95  				return s
    96  			}(),
    97  			qs: []sharding.Query{
    98  				{
    99  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE `order_id`=?;",
   100  					Args:       []any{123},
   101  					DB:         "shadow_order_db_0",
   102  					Datasource: "0.db.cluster.company.com:3306",
   103  				},
   104  				{
   105  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_0`.`shadow_order_tab_1` WHERE `order_id`=?;",
   106  					Args:       []any{123},
   107  					DB:         "shadow_order_db_0",
   108  					Datasource: "0.db.cluster.company.com:3306",
   109  				},
   110  				{
   111  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_0`.`shadow_order_tab_2` WHERE `order_id`=?;",
   112  					Args:       []any{123},
   113  					DB:         "shadow_order_db_0",
   114  					Datasource: "0.db.cluster.company.com:3306",
   115  				},
   116  				{
   117  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE `order_id`=?;",
   118  					Args:       []any{123},
   119  					DB:         "shadow_order_db_1",
   120  					Datasource: "0.db.cluster.company.com:3306",
   121  				},
   122  				{
   123  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_1`.`shadow_order_tab_1` WHERE `order_id`=?;",
   124  					Args:       []any{123},
   125  					DB:         "shadow_order_db_1",
   126  					Datasource: "0.db.cluster.company.com:3306",
   127  				},
   128  				{
   129  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `shadow_order_db_1`.`shadow_order_tab_2` WHERE `order_id`=?;",
   130  					Args:       []any{123},
   131  					DB:         "shadow_order_db_1",
   132  					Datasource: "0.db.cluster.company.com:3306",
   133  				},
   134  			},
   135  		},
   136  		{
   137  			name: "columns",
   138  			builder: func() sharding.QueryBuilder {
   139  				s := NewShardingSelector[Order](shardingDB).
   140  					Select(Columns("Content", "OrderId")).Where(C("UserId").EQ(123))
   141  				return s
   142  			}(),
   143  			qs: []sharding.Query{
   144  				{
   145  					SQL:        "SELECT `content`,`order_id` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE `user_id`=?;",
   146  					Args:       []any{123},
   147  					DB:         "shadow_order_db_1",
   148  					Datasource: "0.db.cluster.company.com:3306",
   149  				},
   150  			},
   151  		},
   152  		{
   153  			name: "invalid columns",
   154  			builder: func() sharding.QueryBuilder {
   155  				s := NewShardingSelector[Order](shardingDB).
   156  					Select(C("Invalid")).Where(C("UserId").EQ(123))
   157  				return s
   158  			}(),
   159  			wantErr: errs.NewInvalidFieldError("Invalid"),
   160  		},
   161  		{
   162  			name: "order by",
   163  			builder: func() sharding.QueryBuilder {
   164  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
   165  					Where(C("UserId").EQ(123)).OrderBy(ASC("UserId"), DESC("OrderId"))
   166  				return s
   167  			}(),
   168  			qs: []sharding.Query{
   169  				{
   170  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE `user_id`=? ORDER BY `user_id` ASC,`order_id` DESC;",
   171  					Args:       []any{123},
   172  					DB:         "shadow_order_db_1",
   173  					Datasource: "0.db.cluster.company.com:3306",
   174  				},
   175  			},
   176  		},
   177  		{
   178  			name: "group by",
   179  			builder: func() sharding.QueryBuilder {
   180  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
   181  					Where(C("UserId").EQ(123)).GroupBy("UserId", "OrderId")
   182  				return s
   183  			}(),
   184  			qs: []sharding.Query{
   185  				{
   186  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE `user_id`=? GROUP BY `user_id`,`order_id`;",
   187  					Args:       []any{123},
   188  					DB:         "shadow_order_db_1",
   189  					Datasource: "0.db.cluster.company.com:3306",
   190  				},
   191  			},
   192  		},
   193  		{
   194  			name: "having",
   195  			builder: func() sharding.QueryBuilder {
   196  				s := NewShardingSelector[Order](shardingDB).
   197  					Select(C("OrderId"), C("Content")).
   198  					Where(C("UserId").EQ(123)).GroupBy("OrderId").Having(C("OrderId").EQ(int64(18)))
   199  				return s
   200  			}(),
   201  			qs: []sharding.Query{
   202  				{
   203  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE `user_id`=? GROUP BY `order_id` HAVING `order_id`=?;",
   204  					Args:       []any{123, int64(18)},
   205  					DB:         "shadow_order_db_1",
   206  					Datasource: "0.db.cluster.company.com:3306",
   207  				},
   208  			},
   209  		},
   210  		{
   211  			name: "where and left",
   212  			builder: func() sharding.QueryBuilder {
   213  				s := NewShardingSelector[Order](shardingDB).
   214  					Select(C("OrderId"), C("Content")).
   215  					Where(C("OrderId").EQ(int64(12)).And(C("UserId").EQ(123)))
   216  				return s
   217  			}(),
   218  			qs: []sharding.Query{
   219  				{
   220  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE (`order_id`=?) AND (`user_id`=?);",
   221  					Args:       []any{int64(12), 123},
   222  					DB:         "shadow_order_db_1",
   223  					Datasource: "0.db.cluster.company.com:3306",
   224  				},
   225  			},
   226  		},
   227  		{
   228  			name: "where and right",
   229  			builder: func() sharding.QueryBuilder {
   230  				s := NewShardingSelector[Order](shardingDB).
   231  					Select(C("OrderId"), C("Content")).
   232  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))))
   233  				return s
   234  			}(),
   235  			qs: []sharding.Query{
   236  				{
   237  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE (`user_id`=?) AND (`order_id`=?);",
   238  					Args:       []any{123, int64(12)},
   239  					DB:         "shadow_order_db_1",
   240  					Datasource: "0.db.cluster.company.com:3306",
   241  				},
   242  			},
   243  		},
   244  		{
   245  			name: "where or",
   246  			builder: func() sharding.QueryBuilder {
   247  				s := NewShardingSelector[Order](shardingDB).
   248  					Select(C("OrderId"), C("Content")).
   249  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)))
   250  				return s
   251  			}(),
   252  			qs: []sharding.Query{
   253  				{
   254  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE (`user_id`=?) OR (`user_id`=?);",
   255  					Args:       []any{123, 234},
   256  					DB:         "shadow_order_db_1",
   257  					Datasource: "0.db.cluster.company.com:3306",
   258  				},
   259  				{
   260  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE (`user_id`=?) OR (`user_id`=?);",
   261  					Args:       []any{123, 234},
   262  					DB:         "shadow_order_db_0",
   263  					Datasource: "0.db.cluster.company.com:3306",
   264  				},
   265  			},
   266  		},
   267  		{
   268  			name: "where or left broadcast",
   269  			builder: func() sharding.QueryBuilder {
   270  				s := NewShardingSelector[Order](shardingDB).
   271  					Select(C("OrderId"), C("Content")).
   272  					Where(C("OrderId").EQ(int64(12)).Or(C("UserId").EQ(123)))
   273  				return s
   274  			}(),
   275  			qs: []sharding.Query{
   276  				{
   277  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE (`order_id`=?) OR (`user_id`=?);",
   278  					Args:       []any{int64(12), 123},
   279  					DB:         "shadow_order_db_0",
   280  					Datasource: "0.db.cluster.company.com:3306",
   281  				},
   282  				{
   283  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_1` WHERE (`order_id`=?) OR (`user_id`=?);",
   284  					Args:       []any{int64(12), 123},
   285  					DB:         "shadow_order_db_0",
   286  					Datasource: "0.db.cluster.company.com:3306",
   287  				},
   288  				{
   289  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_2` WHERE (`order_id`=?) OR (`user_id`=?);",
   290  					Args:       []any{int64(12), 123},
   291  					DB:         "shadow_order_db_0",
   292  					Datasource: "0.db.cluster.company.com:3306",
   293  				},
   294  				{
   295  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE (`order_id`=?) OR (`user_id`=?);",
   296  					Args:       []any{int64(12), 123},
   297  					DB:         "shadow_order_db_1",
   298  					Datasource: "0.db.cluster.company.com:3306",
   299  				},
   300  				{
   301  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_1` WHERE (`order_id`=?) OR (`user_id`=?);",
   302  					Args:       []any{int64(12), 123},
   303  					DB:         "shadow_order_db_1",
   304  					Datasource: "0.db.cluster.company.com:3306",
   305  				},
   306  				{
   307  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_2` WHERE (`order_id`=?) OR (`user_id`=?);",
   308  					Args:       []any{int64(12), 123},
   309  					DB:         "shadow_order_db_1",
   310  					Datasource: "0.db.cluster.company.com:3306",
   311  				},
   312  			},
   313  		},
   314  		{
   315  			name: "where or right broadcast",
   316  			builder: func() sharding.QueryBuilder {
   317  				s := NewShardingSelector[Order](shardingDB).
   318  					Select(C("OrderId"), C("Content")).
   319  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))))
   320  				return s
   321  			}(),
   322  			qs: []sharding.Query{
   323  				{
   324  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE (`user_id`=?) OR (`order_id`=?);",
   325  					Args:       []any{123, int64(12)},
   326  					DB:         "shadow_order_db_0",
   327  					Datasource: "0.db.cluster.company.com:3306",
   328  				},
   329  				{
   330  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_1` WHERE (`user_id`=?) OR (`order_id`=?);",
   331  					Args:       []any{123, int64(12)},
   332  					DB:         "shadow_order_db_0",
   333  					Datasource: "0.db.cluster.company.com:3306",
   334  				},
   335  				{
   336  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_2` WHERE (`user_id`=?) OR (`order_id`=?);",
   337  					Args:       []any{123, int64(12)},
   338  					DB:         "shadow_order_db_0",
   339  					Datasource: "0.db.cluster.company.com:3306",
   340  				},
   341  				{
   342  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE (`user_id`=?) OR (`order_id`=?);",
   343  					Args:       []any{123, int64(12)},
   344  					DB:         "shadow_order_db_1",
   345  					Datasource: "0.db.cluster.company.com:3306",
   346  				},
   347  				{
   348  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_1` WHERE (`user_id`=?) OR (`order_id`=?);",
   349  					Args:       []any{123, int64(12)},
   350  					DB:         "shadow_order_db_1",
   351  					Datasource: "0.db.cluster.company.com:3306",
   352  				},
   353  				{
   354  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_2` WHERE (`user_id`=?) OR (`order_id`=?);",
   355  					Args:       []any{123, int64(12)},
   356  					DB:         "shadow_order_db_1",
   357  					Datasource: "0.db.cluster.company.com:3306",
   358  				},
   359  			},
   360  		},
   361  		{
   362  			name: "where and-or",
   363  			builder: func() sharding.QueryBuilder {
   364  				s := NewShardingSelector[Order](shardingDB).
   365  					Select(C("OrderId"), C("Content")).
   366  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).Or(C("UserId").EQ(234)))
   367  				return s
   368  			}(),
   369  			qs: []sharding.Query{
   370  				{
   371  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
   372  					Args:       []any{123, int64(12), 234},
   373  					DB:         "shadow_order_db_1",
   374  					Datasource: "0.db.cluster.company.com:3306",
   375  				},
   376  				{
   377  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
   378  					Args:       []any{123, int64(12), 234},
   379  					DB:         "shadow_order_db_0",
   380  					Datasource: "0.db.cluster.company.com:3306",
   381  				},
   382  			},
   383  		},
   384  		{
   385  			name: "where and-or broadcast",
   386  			builder: func() sharding.QueryBuilder {
   387  				s := NewShardingSelector[Order](shardingDB).
   388  					Select(C("OrderId"), C("Content")).
   389  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12)).
   390  						Or(C("UserId").EQ(234))))
   391  				return s
   392  			}(),
   393  			qs: []sharding.Query{
   394  				{
   395  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE (`user_id`=?) AND ((`order_id`=?) OR (`user_id`=?));",
   396  					Args:       []any{123, int64(12), 234},
   397  					DB:         "shadow_order_db_1",
   398  					Datasource: "0.db.cluster.company.com:3306",
   399  				},
   400  			},
   401  		},
   402  		{
   403  			name: "where or-and all",
   404  			builder: func() sharding.QueryBuilder {
   405  				s := NewShardingSelector[Order](shardingDB).
   406  					Select(C("OrderId"), C("Content")).
   407  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(181).And(C("UserId").EQ(234))))
   408  				return s
   409  			}(),
   410  			qs: []sharding.Query{
   411  				{
   412  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));",
   413  					Args:       []any{123, 181, 234},
   414  					DB:         "shadow_order_db_1",
   415  					Datasource: "0.db.cluster.company.com:3306",
   416  				},
   417  			},
   418  		},
   419  		{
   420  			name: "where or-and",
   421  			builder: func() sharding.QueryBuilder {
   422  				s := NewShardingSelector[Order](shardingDB).
   423  					Select(C("OrderId"), C("Content")).
   424  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)).
   425  						And(C("OrderId").EQ(int64(24))))
   426  				return s
   427  			}(),
   428  			qs: []sharding.Query{
   429  				{
   430  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
   431  					Args:       []any{123, 234, int64(24)},
   432  					DB:         "shadow_order_db_1",
   433  					Datasource: "0.db.cluster.company.com:3306",
   434  				},
   435  				{
   436  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
   437  					Args:       []any{123, 234, int64(24)},
   438  					DB:         "shadow_order_db_0",
   439  					Datasource: "0.db.cluster.company.com:3306",
   440  				},
   441  			},
   442  		},
   443  		{
   444  			name: "where or-and broadcast",
   445  			builder: func() sharding.QueryBuilder {
   446  				s := NewShardingSelector[Order](shardingDB).
   447  					Select(C("OrderId"), C("Content")).
   448  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
   449  						And(C("UserId").EQ(234)))
   450  				return s
   451  			}(),
   452  			qs: []sharding.Query{
   453  				{
   454  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) AND (`user_id`=?);",
   455  					Args:       []any{123, int64(12), 234},
   456  					DB:         "shadow_order_db_0",
   457  					Datasource: "0.db.cluster.company.com:3306",
   458  				},
   459  			},
   460  		},
   461  		{
   462  			name: "where or-or",
   463  			builder: func() sharding.QueryBuilder {
   464  				s := NewShardingSelector[Order](shardingDB).
   465  					Select(C("OrderId"), C("Content")).
   466  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(253)).
   467  						Or(C("UserId").EQ(234)))
   468  				return s
   469  			}(),
   470  			qs: []sharding.Query{
   471  				{
   472  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
   473  					Args:       []any{123, 253, 234},
   474  					DB:         "shadow_order_db_1",
   475  					Datasource: "0.db.cluster.company.com:3306",
   476  				},
   477  				{
   478  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_1` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
   479  					Args:       []any{123, 253, 234},
   480  					DB:         "shadow_order_db_1",
   481  					Datasource: "0.db.cluster.company.com:3306",
   482  				},
   483  				{
   484  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
   485  					Args:       []any{123, 253, 234},
   486  					DB:         "shadow_order_db_0",
   487  					Datasource: "0.db.cluster.company.com:3306",
   488  				},
   489  			},
   490  		},
   491  		{
   492  			name: "where or-or broadcast",
   493  			builder: func() sharding.QueryBuilder {
   494  				s := NewShardingSelector[Order](shardingDB).
   495  					Select(C("OrderId"), C("Content")).
   496  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
   497  						Or(C("UserId").EQ(234)))
   498  				return s
   499  			}(),
   500  			qs: []sharding.Query{
   501  				{
   502  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
   503  					Args:       []any{123, int64(12), 234},
   504  					DB:         "shadow_order_db_0",
   505  					Datasource: "0.db.cluster.company.com:3306",
   506  				},
   507  				{
   508  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_1` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
   509  					Args:       []any{123, int64(12), 234},
   510  					DB:         "shadow_order_db_0",
   511  					Datasource: "0.db.cluster.company.com:3306",
   512  				},
   513  				{
   514  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_2` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
   515  					Args:       []any{123, int64(12), 234},
   516  					DB:         "shadow_order_db_0",
   517  					Datasource: "0.db.cluster.company.com:3306",
   518  				},
   519  				{
   520  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
   521  					Args:       []any{123, int64(12), 234},
   522  					DB:         "shadow_order_db_1",
   523  					Datasource: "0.db.cluster.company.com:3306",
   524  				},
   525  				{
   526  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_1` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
   527  					Args:       []any{123, int64(12), 234},
   528  					DB:         "shadow_order_db_1",
   529  					Datasource: "0.db.cluster.company.com:3306",
   530  				},
   531  				{
   532  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_2` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
   533  					Args:       []any{123, int64(12), 234},
   534  					DB:         "shadow_order_db_1",
   535  					Datasource: "0.db.cluster.company.com:3306",
   536  				},
   537  			},
   538  		},
   539  		{
   540  			name: "where and-and",
   541  			builder: func() sharding.QueryBuilder {
   542  				s := NewShardingSelector[Order](shardingDB).
   543  					Select(C("OrderId"), C("Content")).
   544  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
   545  						And(C("OrderId").EQ(int64(23))))
   546  				return s
   547  			}(),
   548  			qs: []sharding.Query{
   549  				{
   550  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) AND (`order_id`=?);",
   551  					Args:       []any{123, int64(12), int64(23)},
   552  					DB:         "shadow_order_db_1",
   553  					Datasource: "0.db.cluster.company.com:3306",
   554  				},
   555  			},
   556  		},
   557  		{
   558  			name: "where and-or-and",
   559  			builder: func() sharding.QueryBuilder {
   560  				s := NewShardingSelector[Order](shardingDB).
   561  					Select(C("OrderId"), C("Content")).
   562  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
   563  						Or(C("UserId").EQ(234).And(C("OrderId").EQ(int64(18)))))
   564  				return s
   565  			}(),
   566  			qs: []sharding.Query{
   567  				{
   568  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_1`.`shadow_order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
   569  					Args:       []any{123, int64(12), 234, int64(18)},
   570  					DB:         "shadow_order_db_1",
   571  					Datasource: "0.db.cluster.company.com:3306",
   572  				},
   573  				{
   574  					SQL:        "SELECT `order_id`,`content` FROM `shadow_order_db_0`.`shadow_order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
   575  					Args:       []any{123, int64(12), 234, int64(18)},
   576  					DB:         "shadow_order_db_0",
   577  					Datasource: "0.db.cluster.company.com:3306",
   578  				},
   579  			},
   580  		},
   581  		{
   582  			name: "and empty",
   583  			builder: func() sharding.QueryBuilder {
   584  				s := NewShardingSelector[Order](shardingDB).
   585  					Select(C("OrderId"), C("Content")).
   586  					Where(C("UserId").EQ(123).And(C("UserId").EQ(124)))
   587  				return s
   588  			}(),
   589  			qs: []sharding.Query{},
   590  		},
   591  	}
   592  
   593  	for _, tc := range testCases {
   594  		c := tc
   595  		t.Run(c.name, func(t *testing.T) {
   596  			ctx := hash.CtxWithDBKey(context.Background())
   597  			qs, err := c.builder.Build(hash.CtxWithTableKey(ctx))
   598  			assert.Equal(t, c.wantErr, err)
   599  			if err != nil {
   600  				return
   601  			}
   602  			assert.ElementsMatch(t, c.qs, qs)
   603  		})
   604  	}
   605  }
   606  
   607  func TestShardingSelector_onlyDataSource_Build(t *testing.T) {
   608  	r := model.NewMetaRegistry()
   609  	dsBase := 2
   610  	dbPattern, tablePattern, dsPattern := "order_db", "order_tab", "%d.db.cluster.company.com:3306"
   611  	_, err := r.Register(&Order{},
   612  		model.WithTableShardingAlgorithm(&hash.Hash{
   613  			ShardingKey:  "UserId",
   614  			DBPattern:    &hash.Pattern{Name: dbPattern, NotSharding: true},
   615  			TablePattern: &hash.Pattern{Name: tablePattern, NotSharding: true},
   616  			DsPattern:    &hash.Pattern{Name: dsPattern, Base: dsBase},
   617  		}))
   618  	require.NoError(t, err)
   619  	m := map[string]*masterslave.MasterSlavesDB{
   620  		"order_db": MasterSlavesMemoryDB(),
   621  	}
   622  	clusterDB := cluster.NewClusterDB(m)
   623  	ds := map[string]datasource.DataSource{
   624  		"0.db.cluster.company.com:3306": clusterDB,
   625  		"1.db.cluster.company.com:3306": clusterDB,
   626  	}
   627  	shardingDB, err := OpenDS("sqlite3",
   628  		shardingsource.NewShardingDataSource(ds), DBWithMetaRegistry(r))
   629  	require.NoError(t, err)
   630  
   631  	testCases := []struct {
   632  		name    string
   633  		builder sharding.QueryBuilder
   634  		qs      []sharding.Query
   635  		wantErr error
   636  	}{
   637  		{
   638  			name: "only eq",
   639  			builder: func() sharding.QueryBuilder {
   640  				s := NewShardingSelector[Order](shardingDB).Where(C("UserId").EQ(123))
   641  				return s
   642  			}(),
   643  			qs: []sharding.Query{
   644  				{
   645  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab` WHERE `user_id`=?;",
   646  					Args:       []any{123},
   647  					DB:         "order_db",
   648  					Datasource: "1.db.cluster.company.com:3306",
   649  				},
   650  			},
   651  		},
   652  		{
   653  			name: "only eq broadcast",
   654  			builder: func() sharding.QueryBuilder {
   655  				s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123))
   656  				return s
   657  			}(),
   658  			qs: []sharding.Query{
   659  				{
   660  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab` WHERE `order_id`=?;",
   661  					Args:       []any{123},
   662  					DB:         "order_db",
   663  					Datasource: "0.db.cluster.company.com:3306",
   664  				},
   665  				{
   666  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab` WHERE `order_id`=?;",
   667  					Args:       []any{123},
   668  					DB:         "order_db",
   669  					Datasource: "1.db.cluster.company.com:3306",
   670  				},
   671  			},
   672  		},
   673  		{
   674  			name: "columns",
   675  			builder: func() sharding.QueryBuilder {
   676  				s := NewShardingSelector[Order](shardingDB).
   677  					Select(Columns("Content", "OrderId")).Where(C("UserId").EQ(123))
   678  				return s
   679  			}(),
   680  			qs: []sharding.Query{
   681  				{
   682  					SQL:        "SELECT `content`,`order_id` FROM `order_db`.`order_tab` WHERE `user_id`=?;",
   683  					Args:       []any{123},
   684  					DB:         "order_db",
   685  					Datasource: "1.db.cluster.company.com:3306",
   686  				},
   687  			},
   688  		},
   689  		{
   690  			name: "invalid columns",
   691  			builder: func() sharding.QueryBuilder {
   692  				s := NewShardingSelector[Order](shardingDB).
   693  					Select(C("Invalid")).Where(C("UserId").EQ(123))
   694  				return s
   695  			}(),
   696  			wantErr: errs.NewInvalidFieldError("Invalid"),
   697  		},
   698  		{
   699  			name: "order by",
   700  			builder: func() sharding.QueryBuilder {
   701  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
   702  					Where(C("UserId").EQ(123)).OrderBy(ASC("UserId"), DESC("OrderId"))
   703  				return s
   704  			}(),
   705  			qs: []sharding.Query{
   706  				{
   707  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE `user_id`=? ORDER BY `user_id` ASC,`order_id` DESC;",
   708  					Args:       []any{123},
   709  					DB:         "order_db",
   710  					Datasource: "1.db.cluster.company.com:3306",
   711  				},
   712  			},
   713  		},
   714  		{
   715  			name: "group by",
   716  			builder: func() sharding.QueryBuilder {
   717  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
   718  					Where(C("UserId").EQ(123)).GroupBy("UserId", "OrderId")
   719  				return s
   720  			}(),
   721  			qs: []sharding.Query{
   722  				{
   723  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE `user_id`=? GROUP BY `user_id`,`order_id`;",
   724  					Args:       []any{123},
   725  					DB:         "order_db",
   726  					Datasource: "1.db.cluster.company.com:3306",
   727  				},
   728  			},
   729  		},
   730  		{
   731  			name: "having",
   732  			builder: func() sharding.QueryBuilder {
   733  				s := NewShardingSelector[Order](shardingDB).
   734  					Select(C("OrderId"), C("Content")).
   735  					Where(C("UserId").EQ(123)).GroupBy("OrderId").Having(C("OrderId").EQ(int64(18)))
   736  				return s
   737  			}(),
   738  			qs: []sharding.Query{
   739  				{
   740  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE `user_id`=? GROUP BY `order_id` HAVING `order_id`=?;",
   741  					Args:       []any{123, int64(18)},
   742  					DB:         "order_db",
   743  					Datasource: "1.db.cluster.company.com:3306",
   744  				},
   745  			},
   746  		},
   747  		{
   748  			name: "where and left",
   749  			builder: func() sharding.QueryBuilder {
   750  				s := NewShardingSelector[Order](shardingDB).
   751  					Select(C("OrderId"), C("Content")).
   752  					Where(C("OrderId").EQ(int64(12)).And(C("UserId").EQ(123)))
   753  				return s
   754  			}(),
   755  			qs: []sharding.Query{
   756  				{
   757  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`order_id`=?) AND (`user_id`=?);",
   758  					Args:       []any{int64(12), 123},
   759  					DB:         "order_db",
   760  					Datasource: "1.db.cluster.company.com:3306",
   761  				},
   762  			},
   763  		},
   764  		{
   765  			name: "where and right",
   766  			builder: func() sharding.QueryBuilder {
   767  				s := NewShardingSelector[Order](shardingDB).
   768  					Select(C("OrderId"), C("Content")).
   769  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))))
   770  				return s
   771  			}(),
   772  			qs: []sharding.Query{
   773  				{
   774  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) AND (`order_id`=?);",
   775  					Args:       []any{123, int64(12)},
   776  					DB:         "order_db",
   777  					Datasource: "1.db.cluster.company.com:3306",
   778  				},
   779  			},
   780  		},
   781  		{
   782  			name: "where or",
   783  			builder: func() sharding.QueryBuilder {
   784  				s := NewShardingSelector[Order](shardingDB).
   785  					Select(C("OrderId"), C("Content")).
   786  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)))
   787  				return s
   788  			}(),
   789  			qs: []sharding.Query{
   790  				{
   791  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) OR (`user_id`=?);",
   792  					Args:       []any{123, 234},
   793  					DB:         "order_db",
   794  					Datasource: "0.db.cluster.company.com:3306",
   795  				},
   796  				{
   797  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) OR (`user_id`=?);",
   798  					Args:       []any{123, 234},
   799  					DB:         "order_db",
   800  					Datasource: "1.db.cluster.company.com:3306",
   801  				},
   802  			},
   803  		},
   804  		{
   805  			name: "where or left broadcast",
   806  			builder: func() sharding.QueryBuilder {
   807  				s := NewShardingSelector[Order](shardingDB).
   808  					Select(C("OrderId"), C("Content")).
   809  					Where(C("OrderId").EQ(int64(12)).Or(C("UserId").EQ(123)))
   810  				return s
   811  			}(),
   812  			qs: []sharding.Query{
   813  				{
   814  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`order_id`=?) OR (`user_id`=?);",
   815  					Args:       []any{int64(12), 123},
   816  					DB:         "order_db",
   817  					Datasource: "0.db.cluster.company.com:3306",
   818  				},
   819  				{
   820  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`order_id`=?) OR (`user_id`=?);",
   821  					Args:       []any{int64(12), 123},
   822  					DB:         "order_db",
   823  					Datasource: "1.db.cluster.company.com:3306",
   824  				},
   825  			},
   826  		},
   827  		{
   828  			name: "where or right broadcast",
   829  			builder: func() sharding.QueryBuilder {
   830  				s := NewShardingSelector[Order](shardingDB).
   831  					Select(C("OrderId"), C("Content")).
   832  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))))
   833  				return s
   834  			}(),
   835  			qs: []sharding.Query{
   836  				{
   837  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) OR (`order_id`=?);",
   838  					Args:       []any{123, int64(12)},
   839  					DB:         "order_db",
   840  					Datasource: "0.db.cluster.company.com:3306",
   841  				},
   842  				{
   843  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) OR (`order_id`=?);",
   844  					Args:       []any{123, int64(12)},
   845  					DB:         "order_db",
   846  					Datasource: "1.db.cluster.company.com:3306",
   847  				},
   848  			},
   849  		},
   850  		{
   851  			name: "where and-or",
   852  			builder: func() sharding.QueryBuilder {
   853  				s := NewShardingSelector[Order](shardingDB).
   854  					Select(C("OrderId"), C("Content")).
   855  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).Or(C("UserId").EQ(234)))
   856  				return s
   857  			}(),
   858  			qs: []sharding.Query{
   859  				{
   860  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
   861  					Args:       []any{123, int64(12), 234},
   862  					DB:         "order_db",
   863  					Datasource: "0.db.cluster.company.com:3306",
   864  				},
   865  				{
   866  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
   867  					Args:       []any{123, int64(12), 234},
   868  					DB:         "order_db",
   869  					Datasource: "1.db.cluster.company.com:3306",
   870  				},
   871  			},
   872  		},
   873  		{
   874  			name: "where and-or broadcast",
   875  			builder: func() sharding.QueryBuilder {
   876  				s := NewShardingSelector[Order](shardingDB).
   877  					Select(C("OrderId"), C("Content")).
   878  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12)).
   879  						Or(C("UserId").EQ(234))))
   880  				return s
   881  			}(),
   882  			qs: []sharding.Query{
   883  				{
   884  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) AND ((`order_id`=?) OR (`user_id`=?));",
   885  					Args:       []any{123, int64(12), 234},
   886  					DB:         "order_db",
   887  					Datasource: "1.db.cluster.company.com:3306",
   888  				},
   889  			},
   890  		},
   891  		{
   892  			name: "where or-and all",
   893  			builder: func() sharding.QueryBuilder {
   894  				s := NewShardingSelector[Order](shardingDB).
   895  					Select(C("OrderId"), C("Content")).
   896  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(181).And(C("UserId").EQ(234))))
   897  				return s
   898  			}(),
   899  			qs: []sharding.Query{
   900  				{
   901  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));",
   902  					Args:       []any{123, 181, 234},
   903  					DB:         "order_db",
   904  					Datasource: "1.db.cluster.company.com:3306",
   905  				},
   906  			},
   907  		},
   908  		{
   909  			name: "where or-and",
   910  			builder: func() sharding.QueryBuilder {
   911  				s := NewShardingSelector[Order](shardingDB).
   912  					Select(C("OrderId"), C("Content")).
   913  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)).
   914  						And(C("OrderId").EQ(int64(24))))
   915  				return s
   916  			}(),
   917  			qs: []sharding.Query{
   918  				{
   919  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
   920  					Args:       []any{123, 234, int64(24)},
   921  					DB:         "order_db",
   922  					Datasource: "0.db.cluster.company.com:3306",
   923  				},
   924  				{
   925  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
   926  					Args:       []any{123, 234, int64(24)},
   927  					DB:         "order_db",
   928  					Datasource: "1.db.cluster.company.com:3306",
   929  				},
   930  			},
   931  		},
   932  		{
   933  			name: "where or-and broadcast",
   934  			builder: func() sharding.QueryBuilder {
   935  				s := NewShardingSelector[Order](shardingDB).
   936  					Select(C("OrderId"), C("Content")).
   937  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
   938  						And(C("UserId").EQ(234)))
   939  				return s
   940  			}(),
   941  			qs: []sharding.Query{
   942  				{
   943  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) OR (`order_id`=?)) AND (`user_id`=?);",
   944  					Args:       []any{123, int64(12), 234},
   945  					DB:         "order_db",
   946  					Datasource: "0.db.cluster.company.com:3306",
   947  				},
   948  			},
   949  		},
   950  		{
   951  			name: "where or-or",
   952  			builder: func() sharding.QueryBuilder {
   953  				s := NewShardingSelector[Order](shardingDB).
   954  					Select(C("OrderId"), C("Content")).
   955  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(253)).
   956  						Or(C("UserId").EQ(234)))
   957  				return s
   958  			}(),
   959  			qs: []sharding.Query{
   960  				{
   961  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
   962  					Args:       []any{123, 253, 234},
   963  					DB:         "order_db",
   964  					Datasource: "0.db.cluster.company.com:3306",
   965  				},
   966  				{
   967  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
   968  					Args:       []any{123, 253, 234},
   969  					DB:         "order_db",
   970  					Datasource: "1.db.cluster.company.com:3306",
   971  				},
   972  			},
   973  		},
   974  		{
   975  			name: "where or-or broadcast",
   976  			builder: func() sharding.QueryBuilder {
   977  				s := NewShardingSelector[Order](shardingDB).
   978  					Select(C("OrderId"), C("Content")).
   979  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
   980  						Or(C("UserId").EQ(234)))
   981  				return s
   982  			}(),
   983  			qs: []sharding.Query{
   984  				{
   985  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
   986  					Args:       []any{123, int64(12), 234},
   987  					DB:         "order_db",
   988  					Datasource: "0.db.cluster.company.com:3306",
   989  				},
   990  				{
   991  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
   992  					Args:       []any{123, int64(12), 234},
   993  					DB:         "order_db",
   994  					Datasource: "1.db.cluster.company.com:3306",
   995  				},
   996  			},
   997  		},
   998  		{
   999  			name: "where and-and",
  1000  			builder: func() sharding.QueryBuilder {
  1001  				s := NewShardingSelector[Order](shardingDB).
  1002  					Select(C("OrderId"), C("Content")).
  1003  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
  1004  						And(C("OrderId").EQ(int64(23))))
  1005  				return s
  1006  			}(),
  1007  			qs: []sharding.Query{
  1008  				{
  1009  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) AND (`order_id`=?)) AND (`order_id`=?);",
  1010  					Args:       []any{123, int64(12), int64(23)},
  1011  					DB:         "order_db",
  1012  					Datasource: "1.db.cluster.company.com:3306",
  1013  				},
  1014  			},
  1015  		},
  1016  		{
  1017  			name: "where and-or-and",
  1018  			builder: func() sharding.QueryBuilder {
  1019  				s := NewShardingSelector[Order](shardingDB).
  1020  					Select(C("OrderId"), C("Content")).
  1021  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
  1022  						Or(C("UserId").EQ(234).And(C("OrderId").EQ(int64(18)))))
  1023  				return s
  1024  			}(),
  1025  			qs: []sharding.Query{
  1026  				{
  1027  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
  1028  					Args:       []any{123, int64(12), 234, int64(18)},
  1029  					DB:         "order_db",
  1030  					Datasource: "0.db.cluster.company.com:3306",
  1031  				},
  1032  				{
  1033  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
  1034  					Args:       []any{123, int64(12), 234, int64(18)},
  1035  					DB:         "order_db",
  1036  					Datasource: "1.db.cluster.company.com:3306",
  1037  				},
  1038  			},
  1039  		},
  1040  		{
  1041  			name: "where lt",
  1042  			builder: func() sharding.QueryBuilder {
  1043  				s := NewShardingSelector[Order](shardingDB).
  1044  					Select(C("OrderId"), C("Content")).Where(C("UserId").LT(1))
  1045  				return s
  1046  			}(),
  1047  			qs: func() []sharding.Query {
  1048  				var res []sharding.Query
  1049  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`<?;"
  1050  				for b := 0; b < dsBase; b++ {
  1051  					dsName := fmt.Sprintf(dsPattern, b)
  1052  					res = append(res, sharding.Query{
  1053  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1054  						Args:       []any{1},
  1055  						DB:         dbPattern,
  1056  						Datasource: dsName,
  1057  					})
  1058  				}
  1059  				return res
  1060  			}(),
  1061  		},
  1062  		{
  1063  			name: "where lt eq",
  1064  			builder: func() sharding.QueryBuilder {
  1065  				s := NewShardingSelector[Order](shardingDB).
  1066  					Select(C("OrderId"), C("Content")).Where(C("UserId").LTEQ(1))
  1067  				return s
  1068  			}(),
  1069  			qs: func() []sharding.Query {
  1070  				var res []sharding.Query
  1071  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`<=?;"
  1072  				for b := 0; b < dsBase; b++ {
  1073  					dsName := fmt.Sprintf(dsPattern, b)
  1074  					res = append(res, sharding.Query{
  1075  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1076  						Args:       []any{1},
  1077  						DB:         dbPattern,
  1078  						Datasource: dsName,
  1079  					})
  1080  				}
  1081  				return res
  1082  			}(),
  1083  		},
  1084  		{
  1085  			name: "where eq and lt",
  1086  			builder: func() sharding.QueryBuilder {
  1087  				s := NewShardingSelector[Order](shardingDB).
  1088  					Select(C("OrderId"), C("Content")).Where(C("UserId").EQ(12).And(C("UserId").LT(133)))
  1089  				return s
  1090  			}(),
  1091  			qs: []sharding.Query{
  1092  				{
  1093  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) AND (`user_id`<?);",
  1094  					Args:       []any{12, 133},
  1095  					DB:         "order_db",
  1096  					Datasource: "0.db.cluster.company.com:3306",
  1097  				},
  1098  			},
  1099  		},
  1100  		{
  1101  			name: "where gt",
  1102  			builder: func() sharding.QueryBuilder {
  1103  				s := NewShardingSelector[Order](shardingDB).
  1104  					Select(C("OrderId"), C("Content")).Where(C("UserId").GT(1))
  1105  				return s
  1106  			}(),
  1107  			qs: func() []sharding.Query {
  1108  				var res []sharding.Query
  1109  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`>?;"
  1110  				for b := 0; b < dsBase; b++ {
  1111  					dsName := fmt.Sprintf(dsPattern, b)
  1112  					res = append(res, sharding.Query{
  1113  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1114  						Args:       []any{1},
  1115  						DB:         dbPattern,
  1116  						Datasource: dsName,
  1117  					})
  1118  				}
  1119  				return res
  1120  			}(),
  1121  		},
  1122  		{
  1123  			name: "where gt eq",
  1124  			builder: func() sharding.QueryBuilder {
  1125  				s := NewShardingSelector[Order](shardingDB).
  1126  					Select(C("OrderId"), C("Content")).Where(C("UserId").GTEQ(1))
  1127  				return s
  1128  			}(),
  1129  			qs: func() []sharding.Query {
  1130  				var res []sharding.Query
  1131  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`>=?;"
  1132  				for b := 0; b < dsBase; b++ {
  1133  					dsName := fmt.Sprintf(dsPattern, b)
  1134  					res = append(res, sharding.Query{
  1135  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1136  						Args:       []any{1},
  1137  						DB:         dbPattern,
  1138  						Datasource: dsName,
  1139  					})
  1140  				}
  1141  				return res
  1142  			}(),
  1143  		},
  1144  		{
  1145  			name: "where eq and gt",
  1146  			builder: func() sharding.QueryBuilder {
  1147  				s := NewShardingSelector[Order](shardingDB).
  1148  					Select(C("OrderId"), C("Content")).Where(C("UserId").EQ(12).And(C("UserId").GT(133)))
  1149  				return s
  1150  			}(),
  1151  			qs: []sharding.Query{
  1152  				{
  1153  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id`=?) AND (`user_id`>?);",
  1154  					Args:       []any{12, 133},
  1155  					DB:         "order_db",
  1156  					Datasource: "0.db.cluster.company.com:3306",
  1157  				},
  1158  			},
  1159  		},
  1160  		{
  1161  			name: "where eq and lt or gt",
  1162  			builder: func() sharding.QueryBuilder {
  1163  				s := NewShardingSelector[Order](shardingDB).
  1164  					Select(C("OrderId"), C("Content")).
  1165  					Where(C("UserId").EQ(12).
  1166  						And(C("UserId").LT(133)).Or(C("UserId").GT(234)))
  1167  				return s
  1168  			}(),
  1169  			qs: func() []sharding.Query {
  1170  				var res []sharding.Query
  1171  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE ((`user_id`=?) AND (`user_id`<?)) OR (`user_id`>?);"
  1172  				for b := 0; b < dsBase; b++ {
  1173  					dsName := fmt.Sprintf(dsPattern, b)
  1174  					res = append(res, sharding.Query{
  1175  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1176  						Args:       []any{12, 133, 234},
  1177  						DB:         dbPattern,
  1178  						Datasource: dsName,
  1179  					})
  1180  				}
  1181  				return res
  1182  			}(),
  1183  		},
  1184  		{
  1185  			name: "where in",
  1186  			builder: func() sharding.QueryBuilder {
  1187  				s := NewShardingSelector[Order](shardingDB).
  1188  					Select(C("OrderId"), C("Content")).
  1189  					Where(C("UserId").In(12, 35, 101))
  1190  				return s
  1191  			}(),
  1192  			qs: []sharding.Query{
  1193  				{
  1194  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE `user_id` IN (?,?,?);",
  1195  					Args:       []any{12, 35, 101},
  1196  					DB:         "order_db",
  1197  					Datasource: "0.db.cluster.company.com:3306",
  1198  				},
  1199  				{
  1200  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE `user_id` IN (?,?,?);",
  1201  					Args:       []any{12, 35, 101},
  1202  					DB:         "order_db",
  1203  					Datasource: "1.db.cluster.company.com:3306",
  1204  				},
  1205  			},
  1206  		},
  1207  		{
  1208  			name: "where in and eq",
  1209  			builder: func() sharding.QueryBuilder {
  1210  				s := NewShardingSelector[Order](shardingDB).
  1211  					Select(C("OrderId"), C("Content")).
  1212  					Where(C("UserId").In(12, 35, 101).And(C("UserId").EQ(234)))
  1213  				return s
  1214  			}(),
  1215  			qs: []sharding.Query{
  1216  				{
  1217  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id` IN (?,?,?)) AND (`user_id`=?);",
  1218  					Args:       []any{12, 35, 101, 234},
  1219  					DB:         "order_db",
  1220  					Datasource: "0.db.cluster.company.com:3306",
  1221  				},
  1222  			},
  1223  		},
  1224  		{
  1225  			name: "where not in",
  1226  			builder: func() sharding.QueryBuilder {
  1227  				s := NewShardingSelector[Order](shardingDB).
  1228  					Select(C("OrderId"), C("Content")).
  1229  					Where(C("UserId").NotIn(12, 35, 101))
  1230  				return s
  1231  			}(),
  1232  			qs: func() []sharding.Query {
  1233  				var res []sharding.Query
  1234  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id` NOT IN (?,?,?);"
  1235  				for b := 0; b < dsBase; b++ {
  1236  					dsName := fmt.Sprintf(dsPattern, b)
  1237  					res = append(res, sharding.Query{
  1238  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1239  						Args:       []any{12, 35, 101},
  1240  						DB:         dbPattern,
  1241  						Datasource: dsName,
  1242  					})
  1243  				}
  1244  				return res
  1245  			}(),
  1246  		},
  1247  		{
  1248  			name: "where not in and eq",
  1249  			builder: func() sharding.QueryBuilder {
  1250  				s := NewShardingSelector[Order](shardingDB).
  1251  					Select(C("OrderId"), C("Content")).
  1252  					Where(C("UserId").NotIn(12, 35, 101).And(C("UserId").EQ(234)))
  1253  				return s
  1254  			}(),
  1255  			qs: []sharding.Query{
  1256  				{
  1257  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab` WHERE (`user_id` NOT IN (?,?,?)) AND (`user_id`=?);",
  1258  					Args:       []any{12, 35, 101, 234},
  1259  					DB:         "order_db",
  1260  					Datasource: "0.db.cluster.company.com:3306",
  1261  				},
  1262  			},
  1263  		},
  1264  		{
  1265  			name: "where not in or eq",
  1266  			builder: func() sharding.QueryBuilder {
  1267  				s := NewShardingSelector[Order](shardingDB).
  1268  					Select(C("OrderId"), C("Content")).
  1269  					Where(C("UserId").NotIn(12, 35, 101).Or(C("UserId").EQ(531)))
  1270  				return s
  1271  			}(),
  1272  			qs: func() []sharding.Query {
  1273  				var res []sharding.Query
  1274  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` NOT IN (?,?,?)) OR (`user_id`=?);"
  1275  				for b := 0; b < dsBase; b++ {
  1276  					dsName := fmt.Sprintf(dsPattern, b)
  1277  					res = append(res, sharding.Query{
  1278  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1279  						Args:       []any{12, 35, 101, 531},
  1280  						DB:         dbPattern,
  1281  						Datasource: dsName,
  1282  					})
  1283  				}
  1284  				return res
  1285  			}(),
  1286  		},
  1287  		{
  1288  			name: "where not in or gt",
  1289  			builder: func() sharding.QueryBuilder {
  1290  				s := NewShardingSelector[Order](shardingDB).
  1291  					Select(C("OrderId"), C("Content")).
  1292  					Where(C("UserId").NotIn(12, 35, 101).Or(C("UserId").GT(531)))
  1293  				return s
  1294  			}(),
  1295  			qs: func() []sharding.Query {
  1296  				var res []sharding.Query
  1297  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` NOT IN (?,?,?)) OR (`user_id`>?);"
  1298  				for b := 0; b < dsBase; b++ {
  1299  					dsName := fmt.Sprintf(dsPattern, b)
  1300  					res = append(res, sharding.Query{
  1301  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1302  						Args:       []any{12, 35, 101, 531},
  1303  						DB:         dbPattern,
  1304  						Datasource: dsName,
  1305  					})
  1306  				}
  1307  				return res
  1308  			}(),
  1309  		},
  1310  		{
  1311  			name: "where not gt",
  1312  			builder: func() sharding.QueryBuilder {
  1313  				s := NewShardingSelector[Order](shardingDB).
  1314  					Select(C("OrderId"), C("Content")).
  1315  					Where(Not(C("UserId").GT(101)))
  1316  				return s
  1317  			}(),
  1318  			qs: func() []sharding.Query {
  1319  				var res []sharding.Query
  1320  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`>?);"
  1321  				for b := 0; b < dsBase; b++ {
  1322  					dsName := fmt.Sprintf(dsPattern, b)
  1323  					res = append(res, sharding.Query{
  1324  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1325  						Args:       []any{101},
  1326  						DB:         dbPattern,
  1327  						Datasource: dsName,
  1328  					})
  1329  				}
  1330  				return res
  1331  			}(),
  1332  		},
  1333  		{
  1334  			name: "where not lt",
  1335  			builder: func() sharding.QueryBuilder {
  1336  				s := NewShardingSelector[Order](shardingDB).
  1337  					Select(C("OrderId"), C("Content")).
  1338  					Where(Not(C("UserId").LT(101)))
  1339  				return s
  1340  			}(),
  1341  			qs: func() []sharding.Query {
  1342  				var res []sharding.Query
  1343  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`<?);"
  1344  				for b := 0; b < dsBase; b++ {
  1345  					dsName := fmt.Sprintf(dsPattern, b)
  1346  					res = append(res, sharding.Query{
  1347  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1348  						Args:       []any{101},
  1349  						DB:         dbPattern,
  1350  						Datasource: dsName,
  1351  					})
  1352  				}
  1353  				return res
  1354  			}(),
  1355  		},
  1356  		{
  1357  			name: "where not (gt and lt)",
  1358  			builder: func() sharding.QueryBuilder {
  1359  				s := NewShardingSelector[Order](shardingDB).
  1360  					Select(C("OrderId"), C("Content")).
  1361  					Where(Not(C("UserId").GT(12).And(C("UserId").LT(531))))
  1362  				return s
  1363  			}(),
  1364  			qs: func() []sharding.Query {
  1365  				var res []sharding.Query
  1366  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`>?) AND (`user_id`<?));"
  1367  				for b := 0; b < dsBase; b++ {
  1368  					dsName := fmt.Sprintf(dsPattern, b)
  1369  					res = append(res, sharding.Query{
  1370  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1371  						Args:       []any{12, 531},
  1372  						DB:         dbPattern,
  1373  						Datasource: dsName,
  1374  					})
  1375  				}
  1376  				return res
  1377  			}(),
  1378  		},
  1379  		{
  1380  			name: "where not (gt eq and lt eq)",
  1381  			builder: func() sharding.QueryBuilder {
  1382  				s := NewShardingSelector[Order](shardingDB).
  1383  					Select(C("OrderId"), C("Content")).
  1384  					Where(Not(C("UserId").GTEQ(12).And(C("UserId").LTEQ(531))))
  1385  				return s
  1386  			}(),
  1387  			qs: func() []sharding.Query {
  1388  				var res []sharding.Query
  1389  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`>=?) AND (`user_id`<=?));"
  1390  				for b := 0; b < dsBase; b++ {
  1391  					dsName := fmt.Sprintf(dsPattern, b)
  1392  					res = append(res, sharding.Query{
  1393  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1394  						Args:       []any{12, 531},
  1395  						DB:         dbPattern,
  1396  						Datasource: dsName,
  1397  					})
  1398  				}
  1399  				return res
  1400  			}(),
  1401  		},
  1402  		{
  1403  			name: "where not (in or gt)",
  1404  			builder: func() sharding.QueryBuilder {
  1405  				s := NewShardingSelector[Order](shardingDB).
  1406  					Select(C("OrderId"), C("Content")).
  1407  					Where(Not(C("UserId").In(12, 35, 101).Or(C("UserId").GT(531))))
  1408  				return s
  1409  			}(),
  1410  			qs: func() []sharding.Query {
  1411  				var res []sharding.Query
  1412  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id` IN (?,?,?)) OR (`user_id`>?));"
  1413  				for b := 0; b < dsBase; b++ {
  1414  					dsName := fmt.Sprintf(dsPattern, b)
  1415  					res = append(res, sharding.Query{
  1416  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1417  						Args:       []any{12, 35, 101, 531},
  1418  						DB:         dbPattern,
  1419  						Datasource: dsName,
  1420  					})
  1421  				}
  1422  				return res
  1423  			}(),
  1424  		},
  1425  		{
  1426  			name: "where not (in or eq)",
  1427  			builder: func() sharding.QueryBuilder {
  1428  				s := NewShardingSelector[Order](shardingDB).
  1429  					Select(C("OrderId"), C("Content")).
  1430  					Where(Not(C("UserId").In(12, 35, 101).Or(C("UserId").EQ(531))))
  1431  				return s
  1432  			}(),
  1433  			qs: func() []sharding.Query {
  1434  				var res []sharding.Query
  1435  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id` IN (?,?,?)) OR (`user_id`=?));"
  1436  				for b := 0; b < dsBase; b++ {
  1437  					dsName := fmt.Sprintf(dsPattern, b)
  1438  					res = append(res, sharding.Query{
  1439  						SQL:        fmt.Sprintf(sql, dbPattern, tablePattern),
  1440  						Args:       []any{12, 35, 101, 531},
  1441  						DB:         dbPattern,
  1442  						Datasource: dsName,
  1443  					})
  1444  				}
  1445  				return res
  1446  			}(),
  1447  		},
  1448  		{
  1449  			name: "and empty",
  1450  			builder: func() sharding.QueryBuilder {
  1451  				s := NewShardingSelector[Order](shardingDB).
  1452  					Select(C("OrderId"), C("Content")).
  1453  					Where(C("UserId").EQ(123).And(C("UserId").EQ(124)))
  1454  				return s
  1455  			}(),
  1456  			qs: []sharding.Query{},
  1457  		},
  1458  	}
  1459  
  1460  	for _, tc := range testCases {
  1461  		c := tc
  1462  		t.Run(c.name, func(t *testing.T) {
  1463  			qs, err := c.builder.Build(context.Background())
  1464  			assert.Equal(t, c.wantErr, err)
  1465  			if err != nil {
  1466  				return
  1467  			}
  1468  			assert.ElementsMatch(t, c.qs, qs)
  1469  		})
  1470  	}
  1471  }
  1472  
  1473  func TestShardingSelector_onlyTable_Build(t *testing.T) {
  1474  	r := model.NewMetaRegistry()
  1475  	tableBase := 3
  1476  	dbPattern, tablePattern, dsPattern := "order_db", "order_tab_%d", "0.db.cluster.company.com:3306"
  1477  	_, err := r.Register(&Order{},
  1478  		model.WithTableShardingAlgorithm(&hash.Hash{
  1479  			ShardingKey:  "UserId",
  1480  			DBPattern:    &hash.Pattern{Name: dbPattern, NotSharding: true},
  1481  			TablePattern: &hash.Pattern{Name: tablePattern, Base: tableBase},
  1482  			DsPattern:    &hash.Pattern{Name: dsPattern, NotSharding: true},
  1483  		}))
  1484  	require.NoError(t, err)
  1485  	m := map[string]*masterslave.MasterSlavesDB{
  1486  		"order_db_0": MasterSlavesMemoryDB(),
  1487  		"order_db_1": MasterSlavesMemoryDB(),
  1488  		"order_db_2": MasterSlavesMemoryDB(),
  1489  	}
  1490  	clusterDB := cluster.NewClusterDB(m)
  1491  	ds := map[string]datasource.DataSource{
  1492  		"0.db.cluster.company.com:3306": clusterDB,
  1493  	}
  1494  	shardingDB, err := OpenDS("sqlite3",
  1495  		shardingsource.NewShardingDataSource(ds), DBWithMetaRegistry(r))
  1496  	require.NoError(t, err)
  1497  
  1498  	testCases := []struct {
  1499  		name    string
  1500  		builder sharding.QueryBuilder
  1501  		qs      []sharding.Query
  1502  		wantErr error
  1503  	}{
  1504  		{
  1505  			name: "only eq",
  1506  			builder: func() sharding.QueryBuilder {
  1507  				s := NewShardingSelector[Order](shardingDB).Where(C("UserId").EQ(123))
  1508  				return s
  1509  			}(),
  1510  			qs: []sharding.Query{
  1511  				{
  1512  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab_0` WHERE `user_id`=?;",
  1513  					Args:       []any{123},
  1514  					DB:         "order_db",
  1515  					Datasource: "0.db.cluster.company.com:3306",
  1516  				},
  1517  			},
  1518  		},
  1519  		{
  1520  			name: "only eq broadcast",
  1521  			builder: func() sharding.QueryBuilder {
  1522  				s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123))
  1523  				return s
  1524  			}(),
  1525  			qs: []sharding.Query{
  1526  				{
  1527  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab_0` WHERE `order_id`=?;",
  1528  					Args:       []any{123},
  1529  					DB:         "order_db",
  1530  					Datasource: "0.db.cluster.company.com:3306",
  1531  				},
  1532  				{
  1533  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab_1` WHERE `order_id`=?;",
  1534  					Args:       []any{123},
  1535  					DB:         "order_db",
  1536  					Datasource: "0.db.cluster.company.com:3306",
  1537  				},
  1538  				{
  1539  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db`.`order_tab_2` WHERE `order_id`=?;",
  1540  					Args:       []any{123},
  1541  					DB:         "order_db",
  1542  					Datasource: "0.db.cluster.company.com:3306",
  1543  				},
  1544  			},
  1545  		},
  1546  		{
  1547  			name: "columns",
  1548  			builder: func() sharding.QueryBuilder {
  1549  				s := NewShardingSelector[Order](shardingDB).
  1550  					Select(Columns("Content", "OrderId")).Where(C("UserId").EQ(123))
  1551  				return s
  1552  			}(),
  1553  			qs: []sharding.Query{
  1554  				{
  1555  					SQL:        "SELECT `content`,`order_id` FROM `order_db`.`order_tab_0` WHERE `user_id`=?;",
  1556  					Args:       []any{123},
  1557  					DB:         "order_db",
  1558  					Datasource: "0.db.cluster.company.com:3306",
  1559  				},
  1560  			},
  1561  		},
  1562  		{
  1563  			name: "invalid columns",
  1564  			builder: func() sharding.QueryBuilder {
  1565  				s := NewShardingSelector[Order](shardingDB).
  1566  					Select(C("Invalid")).Where(C("UserId").EQ(123))
  1567  				return s
  1568  			}(),
  1569  			wantErr: errs.NewInvalidFieldError("Invalid"),
  1570  		},
  1571  		{
  1572  			name: "order by",
  1573  			builder: func() sharding.QueryBuilder {
  1574  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
  1575  					Where(C("UserId").EQ(123)).OrderBy(ASC("UserId"), DESC("OrderId"))
  1576  				return s
  1577  			}(),
  1578  			qs: []sharding.Query{
  1579  				{
  1580  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE `user_id`=? ORDER BY `user_id` ASC,`order_id` DESC;",
  1581  					Args:       []any{123},
  1582  					DB:         "order_db",
  1583  					Datasource: "0.db.cluster.company.com:3306",
  1584  				},
  1585  			},
  1586  		},
  1587  		{
  1588  			name: "group by",
  1589  			builder: func() sharding.QueryBuilder {
  1590  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
  1591  					Where(C("UserId").EQ(123)).GroupBy("UserId", "OrderId")
  1592  				return s
  1593  			}(),
  1594  			qs: []sharding.Query{
  1595  				{
  1596  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE `user_id`=? GROUP BY `user_id`,`order_id`;",
  1597  					Args:       []any{123},
  1598  					DB:         "order_db",
  1599  					Datasource: "0.db.cluster.company.com:3306",
  1600  				},
  1601  			},
  1602  		},
  1603  		{
  1604  			name: "having",
  1605  			builder: func() sharding.QueryBuilder {
  1606  				s := NewShardingSelector[Order](shardingDB).
  1607  					Select(C("OrderId"), C("Content")).
  1608  					Where(C("UserId").EQ(123)).GroupBy("OrderId").Having(C("OrderId").EQ(int64(18)))
  1609  				return s
  1610  			}(),
  1611  			qs: []sharding.Query{
  1612  				{
  1613  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE `user_id`=? GROUP BY `order_id` HAVING `order_id`=?;",
  1614  					Args:       []any{123, int64(18)},
  1615  					DB:         "order_db",
  1616  					Datasource: "0.db.cluster.company.com:3306",
  1617  				},
  1618  			},
  1619  		},
  1620  		{
  1621  			name: "where and left",
  1622  			builder: func() sharding.QueryBuilder {
  1623  				s := NewShardingSelector[Order](shardingDB).
  1624  					Select(C("OrderId"), C("Content")).
  1625  					Where(C("OrderId").EQ(int64(12)).And(C("UserId").EQ(123)))
  1626  				return s
  1627  			}(),
  1628  			qs: []sharding.Query{
  1629  				{
  1630  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`order_id`=?) AND (`user_id`=?);",
  1631  					Args:       []any{int64(12), 123},
  1632  					DB:         "order_db",
  1633  					Datasource: "0.db.cluster.company.com:3306",
  1634  				},
  1635  			},
  1636  		},
  1637  		{
  1638  			name: "where and right",
  1639  			builder: func() sharding.QueryBuilder {
  1640  				s := NewShardingSelector[Order](shardingDB).
  1641  					Select(C("OrderId"), C("Content")).
  1642  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))))
  1643  				return s
  1644  			}(),
  1645  			qs: []sharding.Query{
  1646  				{
  1647  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id`=?) AND (`order_id`=?);",
  1648  					Args:       []any{123, int64(12)},
  1649  					DB:         "order_db",
  1650  					Datasource: "0.db.cluster.company.com:3306",
  1651  				},
  1652  			},
  1653  		},
  1654  		{
  1655  			name: "where or",
  1656  			builder: func() sharding.QueryBuilder {
  1657  				s := NewShardingSelector[Order](shardingDB).
  1658  					Select(C("OrderId"), C("Content")).
  1659  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)))
  1660  				return s
  1661  			}(),
  1662  			qs: []sharding.Query{
  1663  				{
  1664  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id`=?) OR (`user_id`=?);",
  1665  					Args:       []any{123, 234},
  1666  					DB:         "order_db",
  1667  					Datasource: "0.db.cluster.company.com:3306",
  1668  				},
  1669  			},
  1670  		},
  1671  		{
  1672  			name: "where or left broadcast",
  1673  			builder: func() sharding.QueryBuilder {
  1674  				s := NewShardingSelector[Order](shardingDB).
  1675  					Select(C("OrderId"), C("Content")).
  1676  					Where(C("OrderId").EQ(int64(12)).Or(C("UserId").EQ(123)))
  1677  				return s
  1678  			}(),
  1679  			qs: []sharding.Query{
  1680  				{
  1681  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`order_id`=?) OR (`user_id`=?);",
  1682  					Args:       []any{int64(12), 123},
  1683  					DB:         "order_db",
  1684  					Datasource: "0.db.cluster.company.com:3306",
  1685  				},
  1686  				{
  1687  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_1` WHERE (`order_id`=?) OR (`user_id`=?);",
  1688  					Args:       []any{int64(12), 123},
  1689  					DB:         "order_db",
  1690  					Datasource: "0.db.cluster.company.com:3306",
  1691  				},
  1692  				{
  1693  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_2` WHERE (`order_id`=?) OR (`user_id`=?);",
  1694  					Args:       []any{int64(12), 123},
  1695  					DB:         "order_db",
  1696  					Datasource: "0.db.cluster.company.com:3306",
  1697  				},
  1698  			},
  1699  		},
  1700  		{
  1701  			name: "where or right broadcast",
  1702  			builder: func() sharding.QueryBuilder {
  1703  				s := NewShardingSelector[Order](shardingDB).
  1704  					Select(C("OrderId"), C("Content")).
  1705  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))))
  1706  				return s
  1707  			}(),
  1708  			qs: []sharding.Query{
  1709  				{
  1710  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id`=?) OR (`order_id`=?);",
  1711  					Args:       []any{123, int64(12)},
  1712  					DB:         "order_db",
  1713  					Datasource: "0.db.cluster.company.com:3306",
  1714  				},
  1715  				{
  1716  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_1` WHERE (`user_id`=?) OR (`order_id`=?);",
  1717  					Args:       []any{123, int64(12)},
  1718  					DB:         "order_db",
  1719  					Datasource: "0.db.cluster.company.com:3306",
  1720  				},
  1721  				{
  1722  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_2` WHERE (`user_id`=?) OR (`order_id`=?);",
  1723  					Args:       []any{123, int64(12)},
  1724  					DB:         "order_db",
  1725  					Datasource: "0.db.cluster.company.com:3306",
  1726  				},
  1727  			},
  1728  		},
  1729  		{
  1730  			name: "where and-or",
  1731  			builder: func() sharding.QueryBuilder {
  1732  				s := NewShardingSelector[Order](shardingDB).
  1733  					Select(C("OrderId"), C("Content")).
  1734  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).Or(C("UserId").EQ(234)))
  1735  				return s
  1736  			}(),
  1737  			qs: []sharding.Query{
  1738  				{
  1739  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
  1740  					Args:       []any{123, int64(12), 234},
  1741  					DB:         "order_db",
  1742  					Datasource: "0.db.cluster.company.com:3306",
  1743  				},
  1744  			},
  1745  		},
  1746  		{
  1747  			name: "where and-or broadcast",
  1748  			builder: func() sharding.QueryBuilder {
  1749  				s := NewShardingSelector[Order](shardingDB).
  1750  					Select(C("OrderId"), C("Content")).
  1751  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12)).
  1752  						Or(C("UserId").EQ(234))))
  1753  				return s
  1754  			}(),
  1755  			qs: []sharding.Query{
  1756  				{
  1757  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id`=?) AND ((`order_id`=?) OR (`user_id`=?));",
  1758  					Args:       []any{123, int64(12), 234},
  1759  					DB:         "order_db",
  1760  					Datasource: "0.db.cluster.company.com:3306",
  1761  				},
  1762  			},
  1763  		},
  1764  		{
  1765  			name: "where or-and all",
  1766  			builder: func() sharding.QueryBuilder {
  1767  				s := NewShardingSelector[Order](shardingDB).
  1768  					Select(C("OrderId"), C("Content")).
  1769  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(181).And(C("UserId").EQ(234))))
  1770  				return s
  1771  			}(),
  1772  			qs: []sharding.Query{
  1773  				{
  1774  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));",
  1775  					Args:       []any{123, 181, 234},
  1776  					DB:         "order_db",
  1777  					Datasource: "0.db.cluster.company.com:3306",
  1778  				},
  1779  			},
  1780  		},
  1781  		{
  1782  			name: "where or-and all",
  1783  			builder: func() sharding.QueryBuilder {
  1784  				s := NewShardingSelector[Order](shardingDB).
  1785  					Select(C("OrderId"), C("Content")).
  1786  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(181).And(C("UserId").EQ(234))))
  1787  				return s
  1788  			}(),
  1789  			qs: []sharding.Query{
  1790  				{
  1791  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));",
  1792  					Args:       []any{123, 181, 234},
  1793  					DB:         "order_db",
  1794  					Datasource: "0.db.cluster.company.com:3306",
  1795  				},
  1796  			},
  1797  		},
  1798  		{
  1799  			name: "where or-and",
  1800  			builder: func() sharding.QueryBuilder {
  1801  				s := NewShardingSelector[Order](shardingDB).
  1802  					Select(C("OrderId"), C("Content")).
  1803  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)).
  1804  						And(C("OrderId").EQ(int64(24))))
  1805  				return s
  1806  			}(),
  1807  			qs: []sharding.Query{
  1808  				{
  1809  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
  1810  					Args:       []any{123, 234, int64(24)},
  1811  					DB:         "order_db",
  1812  					Datasource: "0.db.cluster.company.com:3306",
  1813  				},
  1814  			},
  1815  		},
  1816  		{
  1817  			name: "where or-and broadcast",
  1818  			builder: func() sharding.QueryBuilder {
  1819  				s := NewShardingSelector[Order](shardingDB).
  1820  					Select(C("OrderId"), C("Content")).
  1821  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
  1822  						And(C("UserId").EQ(234)))
  1823  				return s
  1824  			}(),
  1825  			qs: []sharding.Query{
  1826  				{
  1827  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) AND (`user_id`=?);",
  1828  					Args:       []any{123, int64(12), 234},
  1829  					DB:         "order_db",
  1830  					Datasource: "0.db.cluster.company.com:3306",
  1831  				},
  1832  			},
  1833  		},
  1834  		{
  1835  			name: "where or-or",
  1836  			builder: func() sharding.QueryBuilder {
  1837  				s := NewShardingSelector[Order](shardingDB).
  1838  					Select(C("OrderId"), C("Content")).
  1839  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(253)).
  1840  						Or(C("UserId").EQ(234)))
  1841  				return s
  1842  			}(),
  1843  			qs: []sharding.Query{
  1844  				{
  1845  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
  1846  					Args:       []any{123, 253, 234},
  1847  					DB:         "order_db",
  1848  					Datasource: "0.db.cluster.company.com:3306",
  1849  				},
  1850  				{
  1851  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_1` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
  1852  					Args:       []any{123, 253, 234},
  1853  					DB:         "order_db",
  1854  					Datasource: "0.db.cluster.company.com:3306",
  1855  				},
  1856  			},
  1857  		},
  1858  		{
  1859  			name: "where or-or broadcast",
  1860  			builder: func() sharding.QueryBuilder {
  1861  				s := NewShardingSelector[Order](shardingDB).
  1862  					Select(C("OrderId"), C("Content")).
  1863  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
  1864  						Or(C("UserId").EQ(234)))
  1865  				return s
  1866  			}(),
  1867  			qs: []sharding.Query{
  1868  				{
  1869  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  1870  					Args:       []any{123, int64(12), 234},
  1871  					DB:         "order_db",
  1872  					Datasource: "0.db.cluster.company.com:3306",
  1873  				},
  1874  				{
  1875  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_1` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  1876  					Args:       []any{123, int64(12), 234},
  1877  					DB:         "order_db",
  1878  					Datasource: "0.db.cluster.company.com:3306",
  1879  				},
  1880  				{
  1881  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_2` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  1882  					Args:       []any{123, int64(12), 234},
  1883  					DB:         "order_db",
  1884  					Datasource: "0.db.cluster.company.com:3306",
  1885  				},
  1886  			},
  1887  		},
  1888  		{
  1889  			name: "where and-and",
  1890  			builder: func() sharding.QueryBuilder {
  1891  				s := NewShardingSelector[Order](shardingDB).
  1892  					Select(C("OrderId"), C("Content")).
  1893  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
  1894  						And(C("OrderId").EQ(int64(23))))
  1895  				return s
  1896  			}(),
  1897  			qs: []sharding.Query{
  1898  				{
  1899  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) AND (`order_id`=?);",
  1900  					Args:       []any{123, int64(12), int64(23)},
  1901  					DB:         "order_db",
  1902  					Datasource: "0.db.cluster.company.com:3306",
  1903  				},
  1904  			},
  1905  		},
  1906  		{
  1907  			name: "where and-or-and",
  1908  			builder: func() sharding.QueryBuilder {
  1909  				s := NewShardingSelector[Order](shardingDB).
  1910  					Select(C("OrderId"), C("Content")).
  1911  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
  1912  						Or(C("UserId").EQ(234).And(C("OrderId").EQ(int64(18)))))
  1913  				return s
  1914  			}(),
  1915  			qs: []sharding.Query{
  1916  				{
  1917  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
  1918  					Args:       []any{123, int64(12), 234, int64(18)},
  1919  					DB:         "order_db",
  1920  					Datasource: "0.db.cluster.company.com:3306",
  1921  				},
  1922  			},
  1923  		},
  1924  		{
  1925  			name: "where lt",
  1926  			builder: func() sharding.QueryBuilder {
  1927  				s := NewShardingSelector[Order](shardingDB).
  1928  					Select(C("OrderId"), C("Content")).Where(C("UserId").LT(1))
  1929  				return s
  1930  			}(),
  1931  			qs: func() []sharding.Query {
  1932  				var res []sharding.Query
  1933  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`<?;"
  1934  				for b := 0; b < tableBase; b++ {
  1935  					tableName := fmt.Sprintf(tablePattern, b)
  1936  					res = append(res, sharding.Query{
  1937  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  1938  						Args:       []any{1},
  1939  						DB:         dbPattern,
  1940  						Datasource: dsPattern,
  1941  					})
  1942  				}
  1943  				return res
  1944  			}(),
  1945  		},
  1946  		{
  1947  			name: "where lt eq",
  1948  			builder: func() sharding.QueryBuilder {
  1949  				s := NewShardingSelector[Order](shardingDB).
  1950  					Select(C("OrderId"), C("Content")).Where(C("UserId").LTEQ(1))
  1951  				return s
  1952  			}(),
  1953  			qs: func() []sharding.Query {
  1954  				var res []sharding.Query
  1955  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`<=?;"
  1956  				for b := 0; b < tableBase; b++ {
  1957  					tableName := fmt.Sprintf(tablePattern, b)
  1958  					res = append(res, sharding.Query{
  1959  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  1960  						Args:       []any{1},
  1961  						DB:         dbPattern,
  1962  						Datasource: dsPattern,
  1963  					})
  1964  				}
  1965  				return res
  1966  			}(),
  1967  		},
  1968  		{
  1969  			name: "where eq and lt",
  1970  			builder: func() sharding.QueryBuilder {
  1971  				s := NewShardingSelector[Order](shardingDB).
  1972  					Select(C("OrderId"), C("Content")).Where(C("UserId").EQ(12).And(C("UserId").LT(133)))
  1973  				return s
  1974  			}(),
  1975  			qs: []sharding.Query{
  1976  				{
  1977  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id`=?) AND (`user_id`<?);",
  1978  					Args:       []any{12, 133},
  1979  					DB:         "order_db",
  1980  					Datasource: "0.db.cluster.company.com:3306",
  1981  				},
  1982  			},
  1983  		},
  1984  		{
  1985  			name: "where gt",
  1986  			builder: func() sharding.QueryBuilder {
  1987  				s := NewShardingSelector[Order](shardingDB).
  1988  					Select(C("OrderId"), C("Content")).Where(C("UserId").GT(1))
  1989  				return s
  1990  			}(),
  1991  			qs: func() []sharding.Query {
  1992  				var res []sharding.Query
  1993  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`>?;"
  1994  				for b := 0; b < tableBase; b++ {
  1995  					tableName := fmt.Sprintf(tablePattern, b)
  1996  					res = append(res, sharding.Query{
  1997  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  1998  						Args:       []any{1},
  1999  						DB:         dbPattern,
  2000  						Datasource: dsPattern,
  2001  					})
  2002  				}
  2003  				return res
  2004  			}(),
  2005  		},
  2006  		{
  2007  			name: "where gt eq",
  2008  			builder: func() sharding.QueryBuilder {
  2009  				s := NewShardingSelector[Order](shardingDB).
  2010  					Select(C("OrderId"), C("Content")).Where(C("UserId").GTEQ(1))
  2011  				return s
  2012  			}(),
  2013  			qs: func() []sharding.Query {
  2014  				var res []sharding.Query
  2015  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`>=?;"
  2016  				for b := 0; b < tableBase; b++ {
  2017  					tableName := fmt.Sprintf(tablePattern, b)
  2018  					res = append(res, sharding.Query{
  2019  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2020  						Args:       []any{1},
  2021  						DB:         dbPattern,
  2022  						Datasource: dsPattern,
  2023  					})
  2024  				}
  2025  				return res
  2026  			}(),
  2027  		},
  2028  		{
  2029  			name: "where eq and gt",
  2030  			builder: func() sharding.QueryBuilder {
  2031  				s := NewShardingSelector[Order](shardingDB).
  2032  					Select(C("OrderId"), C("Content")).Where(C("UserId").EQ(12).And(C("UserId").GT(133)))
  2033  				return s
  2034  			}(),
  2035  			qs: []sharding.Query{
  2036  				{
  2037  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id`=?) AND (`user_id`>?);",
  2038  					Args:       []any{12, 133},
  2039  					DB:         "order_db",
  2040  					Datasource: "0.db.cluster.company.com:3306",
  2041  				},
  2042  			},
  2043  		},
  2044  		{
  2045  			name: "where eq and lt or gt",
  2046  			builder: func() sharding.QueryBuilder {
  2047  				s := NewShardingSelector[Order](shardingDB).
  2048  					Select(C("OrderId"), C("Content")).
  2049  					Where(C("UserId").EQ(12).
  2050  						And(C("UserId").LT(133)).Or(C("UserId").GT(234)))
  2051  				return s
  2052  			}(),
  2053  			qs: func() []sharding.Query {
  2054  				var res []sharding.Query
  2055  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE ((`user_id`=?) AND (`user_id`<?)) OR (`user_id`>?);"
  2056  				for b := 0; b < tableBase; b++ {
  2057  					tableName := fmt.Sprintf(tablePattern, b)
  2058  					res = append(res, sharding.Query{
  2059  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2060  						Args:       []any{12, 133, 234},
  2061  						DB:         dbPattern,
  2062  						Datasource: dsPattern,
  2063  					})
  2064  				}
  2065  				return res
  2066  			}(),
  2067  		},
  2068  		{
  2069  			name: "where in",
  2070  			builder: func() sharding.QueryBuilder {
  2071  				s := NewShardingSelector[Order](shardingDB).
  2072  					Select(C("OrderId"), C("Content")).
  2073  					Where(C("UserId").In(12, 35, 101))
  2074  				return s
  2075  			}(),
  2076  			qs: []sharding.Query{
  2077  				{
  2078  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_2` WHERE `user_id` IN (?,?,?);",
  2079  					Args:       []any{12, 35, 101},
  2080  					DB:         "order_db",
  2081  					Datasource: "0.db.cluster.company.com:3306",
  2082  				},
  2083  				{
  2084  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE `user_id` IN (?,?,?);",
  2085  					Args:       []any{12, 35, 101},
  2086  					DB:         "order_db",
  2087  					Datasource: "0.db.cluster.company.com:3306",
  2088  				},
  2089  			},
  2090  		},
  2091  		{
  2092  			name: "where in and eq",
  2093  			builder: func() sharding.QueryBuilder {
  2094  				s := NewShardingSelector[Order](shardingDB).
  2095  					Select(C("OrderId"), C("Content")).
  2096  					Where(C("UserId").In(12, 35, 101).And(C("UserId").EQ(234)))
  2097  				return s
  2098  			}(),
  2099  			qs: []sharding.Query{
  2100  				{
  2101  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id` IN (?,?,?)) AND (`user_id`=?);",
  2102  					Args:       []any{12, 35, 101, 234},
  2103  					DB:         "order_db",
  2104  					Datasource: "0.db.cluster.company.com:3306",
  2105  				},
  2106  			},
  2107  		},
  2108  		{
  2109  			name: "where not in",
  2110  			builder: func() sharding.QueryBuilder {
  2111  				s := NewShardingSelector[Order](shardingDB).
  2112  					Select(C("OrderId"), C("Content")).
  2113  					Where(C("UserId").NotIn(12, 35, 101))
  2114  				return s
  2115  			}(),
  2116  			qs: func() []sharding.Query {
  2117  				var res []sharding.Query
  2118  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id` NOT IN (?,?,?);"
  2119  				for b := 0; b < tableBase; b++ {
  2120  					tableName := fmt.Sprintf(tablePattern, b)
  2121  					res = append(res, sharding.Query{
  2122  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2123  						Args:       []any{12, 35, 101},
  2124  						DB:         dbPattern,
  2125  						Datasource: dsPattern,
  2126  					})
  2127  				}
  2128  				return res
  2129  			}(),
  2130  		},
  2131  		{
  2132  			name: "where not in and eq",
  2133  			builder: func() sharding.QueryBuilder {
  2134  				s := NewShardingSelector[Order](shardingDB).
  2135  					Select(C("OrderId"), C("Content")).
  2136  					Where(C("UserId").NotIn(12, 35, 101).And(C("UserId").EQ(234)))
  2137  				return s
  2138  			}(),
  2139  			qs: []sharding.Query{
  2140  				{
  2141  					SQL:        "SELECT `order_id`,`content` FROM `order_db`.`order_tab_0` WHERE (`user_id` NOT IN (?,?,?)) AND (`user_id`=?);",
  2142  					Args:       []any{12, 35, 101, 234},
  2143  					DB:         "order_db",
  2144  					Datasource: "0.db.cluster.company.com:3306",
  2145  				},
  2146  			},
  2147  		},
  2148  		{
  2149  			name: "where not in or eq",
  2150  			builder: func() sharding.QueryBuilder {
  2151  				s := NewShardingSelector[Order](shardingDB).
  2152  					Select(C("OrderId"), C("Content")).
  2153  					Where(C("UserId").NotIn(12, 35, 101).Or(C("UserId").EQ(531)))
  2154  				return s
  2155  			}(),
  2156  			qs: func() []sharding.Query {
  2157  				var res []sharding.Query
  2158  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` NOT IN (?,?,?)) OR (`user_id`=?);"
  2159  				for b := 0; b < tableBase; b++ {
  2160  					tableName := fmt.Sprintf(tablePattern, b)
  2161  					res = append(res, sharding.Query{
  2162  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2163  						Args:       []any{12, 35, 101, 531},
  2164  						DB:         dbPattern,
  2165  						Datasource: dsPattern,
  2166  					})
  2167  				}
  2168  				return res
  2169  			}(),
  2170  		},
  2171  		{
  2172  			name: "where not in or gt",
  2173  			builder: func() sharding.QueryBuilder {
  2174  				s := NewShardingSelector[Order](shardingDB).
  2175  					Select(C("OrderId"), C("Content")).
  2176  					Where(C("UserId").NotIn(12, 35, 101).Or(C("UserId").GT(531)))
  2177  				return s
  2178  			}(),
  2179  			qs: func() []sharding.Query {
  2180  				var res []sharding.Query
  2181  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` NOT IN (?,?,?)) OR (`user_id`>?);"
  2182  				for b := 0; b < tableBase; b++ {
  2183  					tableName := fmt.Sprintf(tablePattern, b)
  2184  					res = append(res, sharding.Query{
  2185  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2186  						Args:       []any{12, 35, 101, 531},
  2187  						DB:         dbPattern,
  2188  						Datasource: dsPattern,
  2189  					})
  2190  				}
  2191  				return res
  2192  			}(),
  2193  		},
  2194  		{
  2195  			name: "where not gt",
  2196  			builder: func() sharding.QueryBuilder {
  2197  				s := NewShardingSelector[Order](shardingDB).
  2198  					Select(C("OrderId"), C("Content")).
  2199  					Where(Not(C("UserId").GT(101)))
  2200  				return s
  2201  			}(),
  2202  			qs: func() []sharding.Query {
  2203  				var res []sharding.Query
  2204  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`>?);"
  2205  				for b := 0; b < tableBase; b++ {
  2206  					tableName := fmt.Sprintf(tablePattern, b)
  2207  					res = append(res, sharding.Query{
  2208  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2209  						Args:       []any{101},
  2210  						DB:         dbPattern,
  2211  						Datasource: dsPattern,
  2212  					})
  2213  				}
  2214  				return res
  2215  			}(),
  2216  		},
  2217  		{
  2218  			name: "where not lt",
  2219  			builder: func() sharding.QueryBuilder {
  2220  				s := NewShardingSelector[Order](shardingDB).
  2221  					Select(C("OrderId"), C("Content")).
  2222  					Where(Not(C("UserId").LT(101)))
  2223  				return s
  2224  			}(),
  2225  			qs: func() []sharding.Query {
  2226  				var res []sharding.Query
  2227  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`<?);"
  2228  				for b := 0; b < tableBase; b++ {
  2229  					tableName := fmt.Sprintf(tablePattern, b)
  2230  					res = append(res, sharding.Query{
  2231  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2232  						Args:       []any{101},
  2233  						DB:         dbPattern,
  2234  						Datasource: dsPattern,
  2235  					})
  2236  				}
  2237  				return res
  2238  			}(),
  2239  		},
  2240  		{
  2241  			name: "where not (gt and lt)",
  2242  			builder: func() sharding.QueryBuilder {
  2243  				s := NewShardingSelector[Order](shardingDB).
  2244  					Select(C("OrderId"), C("Content")).
  2245  					Where(Not(C("UserId").GT(12).And(C("UserId").LT(531))))
  2246  				return s
  2247  			}(),
  2248  			qs: func() []sharding.Query {
  2249  				var res []sharding.Query
  2250  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`>?) AND (`user_id`<?));"
  2251  				for b := 0; b < tableBase; b++ {
  2252  					tableName := fmt.Sprintf(tablePattern, b)
  2253  					res = append(res, sharding.Query{
  2254  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2255  						Args:       []any{12, 531},
  2256  						DB:         dbPattern,
  2257  						Datasource: dsPattern,
  2258  					})
  2259  				}
  2260  				return res
  2261  			}(),
  2262  		},
  2263  		{
  2264  			name: "where not (gt eq and lt eq)",
  2265  			builder: func() sharding.QueryBuilder {
  2266  				s := NewShardingSelector[Order](shardingDB).
  2267  					Select(C("OrderId"), C("Content")).
  2268  					Where(Not(C("UserId").GTEQ(12).And(C("UserId").LTEQ(531))))
  2269  				return s
  2270  			}(),
  2271  			qs: func() []sharding.Query {
  2272  				var res []sharding.Query
  2273  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`>=?) AND (`user_id`<=?));"
  2274  				for b := 0; b < tableBase; b++ {
  2275  					tableName := fmt.Sprintf(tablePattern, b)
  2276  					res = append(res, sharding.Query{
  2277  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2278  						Args:       []any{12, 531},
  2279  						DB:         dbPattern,
  2280  						Datasource: dsPattern,
  2281  					})
  2282  				}
  2283  				return res
  2284  			}(),
  2285  		},
  2286  		{
  2287  			name: "where not (in or gt)",
  2288  			builder: func() sharding.QueryBuilder {
  2289  				s := NewShardingSelector[Order](shardingDB).
  2290  					Select(C("OrderId"), C("Content")).
  2291  					Where(Not(C("UserId").In(12, 35, 101).Or(C("UserId").GT(531))))
  2292  				return s
  2293  			}(),
  2294  			qs: func() []sharding.Query {
  2295  				var res []sharding.Query
  2296  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id` IN (?,?,?)) OR (`user_id`>?));"
  2297  				for b := 0; b < tableBase; b++ {
  2298  					tableName := fmt.Sprintf(tablePattern, b)
  2299  					res = append(res, sharding.Query{
  2300  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2301  						Args:       []any{12, 35, 101, 531},
  2302  						DB:         dbPattern,
  2303  						Datasource: dsPattern,
  2304  					})
  2305  				}
  2306  				return res
  2307  			}(),
  2308  		},
  2309  		{
  2310  			name: "where not (in or eq)",
  2311  			builder: func() sharding.QueryBuilder {
  2312  				s := NewShardingSelector[Order](shardingDB).
  2313  					Select(C("OrderId"), C("Content")).
  2314  					Where(Not(C("UserId").In(12, 35, 101).Or(C("UserId").EQ(531))))
  2315  				return s
  2316  			}(),
  2317  			qs: func() []sharding.Query {
  2318  				var res []sharding.Query
  2319  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id` IN (?,?,?)) OR (`user_id`=?));"
  2320  				for b := 0; b < tableBase; b++ {
  2321  					tableName := fmt.Sprintf(tablePattern, b)
  2322  					res = append(res, sharding.Query{
  2323  						SQL:        fmt.Sprintf(sql, dbPattern, tableName),
  2324  						Args:       []any{12, 35, 101, 531},
  2325  						DB:         dbPattern,
  2326  						Datasource: dsPattern,
  2327  					})
  2328  				}
  2329  				return res
  2330  			}(),
  2331  		},
  2332  		{
  2333  			name: "and empty",
  2334  			builder: func() sharding.QueryBuilder {
  2335  				s := NewShardingSelector[Order](shardingDB).
  2336  					Select(C("OrderId"), C("Content")).
  2337  					Where(C("UserId").EQ(123).And(C("UserId").EQ(124)))
  2338  				return s
  2339  			}(),
  2340  			qs: []sharding.Query{},
  2341  		},
  2342  	}
  2343  
  2344  	for _, tc := range testCases {
  2345  		c := tc
  2346  		t.Run(c.name, func(t *testing.T) {
  2347  			qs, err := c.builder.Build(context.Background())
  2348  			assert.Equal(t, c.wantErr, err)
  2349  			if err != nil {
  2350  				return
  2351  			}
  2352  			assert.ElementsMatch(t, c.qs, qs)
  2353  		})
  2354  	}
  2355  }
  2356  
  2357  func TestShardingSelector_onlyDB_Build(t *testing.T) {
  2358  	r := model.NewMetaRegistry()
  2359  	dbBase := 2
  2360  	dbPattern, tablePattern, dsPattern := "order_db_%d", "order_tab", "0.db.cluster.company.com:3306"
  2361  	_, err := r.Register(&Order{},
  2362  		model.WithTableShardingAlgorithm(&hash.Hash{
  2363  			ShardingKey:  "UserId",
  2364  			DBPattern:    &hash.Pattern{Name: dbPattern, Base: dbBase},
  2365  			TablePattern: &hash.Pattern{Name: tablePattern, NotSharding: true},
  2366  			DsPattern:    &hash.Pattern{Name: dsPattern, NotSharding: true},
  2367  		}))
  2368  	require.NoError(t, err)
  2369  	m := map[string]*masterslave.MasterSlavesDB{
  2370  		"order_db_0": MasterSlavesMemoryDB(),
  2371  		"order_db_1": MasterSlavesMemoryDB(),
  2372  		"order_db_2": MasterSlavesMemoryDB(),
  2373  	}
  2374  	clusterDB := cluster.NewClusterDB(m)
  2375  	ds := map[string]datasource.DataSource{
  2376  		"0.db.cluster.company.com:3306": clusterDB,
  2377  	}
  2378  	shardingDB, err := OpenDS("sqlite3",
  2379  		shardingsource.NewShardingDataSource(ds), DBWithMetaRegistry(r))
  2380  	require.NoError(t, err)
  2381  
  2382  	testCases := []struct {
  2383  		name    string
  2384  		builder sharding.QueryBuilder
  2385  		qs      []sharding.Query
  2386  		wantErr error
  2387  	}{
  2388  		{
  2389  			name: "only eq",
  2390  			builder: func() sharding.QueryBuilder {
  2391  				s := NewShardingSelector[Order](shardingDB).Where(C("UserId").EQ(123))
  2392  				return s
  2393  			}(),
  2394  			qs: []sharding.Query{
  2395  				{
  2396  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab` WHERE `user_id`=?;",
  2397  					Args:       []any{123},
  2398  					DB:         "order_db_1",
  2399  					Datasource: "0.db.cluster.company.com:3306",
  2400  				},
  2401  			},
  2402  		},
  2403  		{
  2404  			name: "only eq broadcast",
  2405  			builder: func() sharding.QueryBuilder {
  2406  				s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123))
  2407  				return s
  2408  			}(),
  2409  			qs: []sharding.Query{
  2410  				{
  2411  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab` WHERE `order_id`=?;",
  2412  					Args:       []any{123},
  2413  					DB:         "order_db_0",
  2414  					Datasource: "0.db.cluster.company.com:3306",
  2415  				},
  2416  				{
  2417  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab` WHERE `order_id`=?;",
  2418  					Args:       []any{123},
  2419  					DB:         "order_db_1",
  2420  					Datasource: "0.db.cluster.company.com:3306",
  2421  				},
  2422  			},
  2423  		},
  2424  		{
  2425  			name: "columns",
  2426  			builder: func() sharding.QueryBuilder {
  2427  				s := NewShardingSelector[Order](shardingDB).
  2428  					Select(Columns("Content", "OrderId")).Where(C("UserId").EQ(123))
  2429  				return s
  2430  			}(),
  2431  			qs: []sharding.Query{
  2432  				{
  2433  					SQL:        "SELECT `content`,`order_id` FROM `order_db_1`.`order_tab` WHERE `user_id`=?;",
  2434  					Args:       []any{123},
  2435  					DB:         "order_db_1",
  2436  					Datasource: "0.db.cluster.company.com:3306",
  2437  				},
  2438  			},
  2439  		},
  2440  		{
  2441  			name: "invalid columns",
  2442  			builder: func() sharding.QueryBuilder {
  2443  				s := NewShardingSelector[Order](shardingDB).
  2444  					Select(C("Invalid")).Where(C("UserId").EQ(123))
  2445  				return s
  2446  			}(),
  2447  			wantErr: errs.NewInvalidFieldError("Invalid"),
  2448  		},
  2449  		{
  2450  			name: "order by",
  2451  			builder: func() sharding.QueryBuilder {
  2452  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
  2453  					Where(C("UserId").EQ(123)).OrderBy(ASC("UserId"), DESC("OrderId"))
  2454  				return s
  2455  			}(),
  2456  			qs: []sharding.Query{
  2457  				{
  2458  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE `user_id`=? ORDER BY `user_id` ASC,`order_id` DESC;",
  2459  					Args:       []any{123},
  2460  					DB:         "order_db_1",
  2461  					Datasource: "0.db.cluster.company.com:3306",
  2462  				},
  2463  			},
  2464  		},
  2465  		{
  2466  			name: "group by",
  2467  			builder: func() sharding.QueryBuilder {
  2468  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
  2469  					Where(C("UserId").EQ(123)).GroupBy("UserId", "OrderId")
  2470  				return s
  2471  			}(),
  2472  			qs: []sharding.Query{
  2473  				{
  2474  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE `user_id`=? GROUP BY `user_id`,`order_id`;",
  2475  					Args:       []any{123},
  2476  					DB:         "order_db_1",
  2477  					Datasource: "0.db.cluster.company.com:3306",
  2478  				},
  2479  			},
  2480  		},
  2481  		{
  2482  			name: "having",
  2483  			builder: func() sharding.QueryBuilder {
  2484  				s := NewShardingSelector[Order](shardingDB).
  2485  					Select(C("OrderId"), C("Content")).
  2486  					Where(C("UserId").EQ(123)).GroupBy("OrderId").Having(C("OrderId").EQ(int64(18)))
  2487  				return s
  2488  			}(),
  2489  			qs: []sharding.Query{
  2490  				{
  2491  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE `user_id`=? GROUP BY `order_id` HAVING `order_id`=?;",
  2492  					Args:       []any{123, int64(18)},
  2493  					DB:         "order_db_1",
  2494  					Datasource: "0.db.cluster.company.com:3306",
  2495  				},
  2496  			},
  2497  		},
  2498  		{
  2499  			name: "where and left",
  2500  			builder: func() sharding.QueryBuilder {
  2501  				s := NewShardingSelector[Order](shardingDB).
  2502  					Select(C("OrderId"), C("Content")).
  2503  					Where(C("OrderId").EQ(int64(12)).And(C("UserId").EQ(123)))
  2504  				return s
  2505  			}(),
  2506  			qs: []sharding.Query{
  2507  				{
  2508  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE (`order_id`=?) AND (`user_id`=?);",
  2509  					Args:       []any{int64(12), 123},
  2510  					DB:         "order_db_1",
  2511  					Datasource: "0.db.cluster.company.com:3306",
  2512  				},
  2513  			},
  2514  		},
  2515  		{
  2516  			name: "where and right",
  2517  			builder: func() sharding.QueryBuilder {
  2518  				s := NewShardingSelector[Order](shardingDB).
  2519  					Select(C("OrderId"), C("Content")).
  2520  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))))
  2521  				return s
  2522  			}(),
  2523  			qs: []sharding.Query{
  2524  				{
  2525  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE (`user_id`=?) AND (`order_id`=?);",
  2526  					Args:       []any{123, int64(12)},
  2527  					DB:         "order_db_1",
  2528  					Datasource: "0.db.cluster.company.com:3306",
  2529  				},
  2530  			},
  2531  		},
  2532  		{
  2533  			name: "where or",
  2534  			builder: func() sharding.QueryBuilder {
  2535  				s := NewShardingSelector[Order](shardingDB).
  2536  					Select(C("OrderId"), C("Content")).
  2537  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)))
  2538  				return s
  2539  			}(),
  2540  			qs: []sharding.Query{
  2541  				{
  2542  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE (`user_id`=?) OR (`user_id`=?);",
  2543  					Args:       []any{123, 234},
  2544  					DB:         "order_db_1",
  2545  					Datasource: "0.db.cluster.company.com:3306",
  2546  				},
  2547  				{
  2548  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE (`user_id`=?) OR (`user_id`=?);",
  2549  					Args:       []any{123, 234},
  2550  					DB:         "order_db_0",
  2551  					Datasource: "0.db.cluster.company.com:3306",
  2552  				},
  2553  			},
  2554  		},
  2555  		{
  2556  			name: "where or left broadcast",
  2557  			builder: func() sharding.QueryBuilder {
  2558  				s := NewShardingSelector[Order](shardingDB).
  2559  					Select(C("OrderId"), C("Content")).
  2560  					Where(C("OrderId").EQ(int64(12)).Or(C("UserId").EQ(123)))
  2561  				return s
  2562  			}(),
  2563  			qs: []sharding.Query{
  2564  				{
  2565  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE (`order_id`=?) OR (`user_id`=?);",
  2566  					Args:       []any{int64(12), 123},
  2567  					DB:         "order_db_0",
  2568  					Datasource: "0.db.cluster.company.com:3306",
  2569  				},
  2570  				{
  2571  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE (`order_id`=?) OR (`user_id`=?);",
  2572  					Args:       []any{int64(12), 123},
  2573  					DB:         "order_db_1",
  2574  					Datasource: "0.db.cluster.company.com:3306",
  2575  				},
  2576  			},
  2577  		},
  2578  		{
  2579  			name: "where or right broadcast",
  2580  			builder: func() sharding.QueryBuilder {
  2581  				s := NewShardingSelector[Order](shardingDB).
  2582  					Select(C("OrderId"), C("Content")).
  2583  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))))
  2584  				return s
  2585  			}(),
  2586  			qs: []sharding.Query{
  2587  				{
  2588  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE (`user_id`=?) OR (`order_id`=?);",
  2589  					Args:       []any{123, int64(12)},
  2590  					DB:         "order_db_0",
  2591  					Datasource: "0.db.cluster.company.com:3306",
  2592  				},
  2593  				{
  2594  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE (`user_id`=?) OR (`order_id`=?);",
  2595  					Args:       []any{123, int64(12)},
  2596  					DB:         "order_db_1",
  2597  					Datasource: "0.db.cluster.company.com:3306",
  2598  				},
  2599  			},
  2600  		},
  2601  		{
  2602  			name: "where and-or",
  2603  			builder: func() sharding.QueryBuilder {
  2604  				s := NewShardingSelector[Order](shardingDB).
  2605  					Select(C("OrderId"), C("Content")).
  2606  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).Or(C("UserId").EQ(234)))
  2607  				return s
  2608  			}(),
  2609  			qs: []sharding.Query{
  2610  				{
  2611  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
  2612  					Args:       []any{123, int64(12), 234},
  2613  					DB:         "order_db_1",
  2614  					Datasource: "0.db.cluster.company.com:3306",
  2615  				},
  2616  				{
  2617  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
  2618  					Args:       []any{123, int64(12), 234},
  2619  					DB:         "order_db_0",
  2620  					Datasource: "0.db.cluster.company.com:3306",
  2621  				},
  2622  			},
  2623  		},
  2624  		{
  2625  			name: "where and-or broadcast",
  2626  			builder: func() sharding.QueryBuilder {
  2627  				s := NewShardingSelector[Order](shardingDB).
  2628  					Select(C("OrderId"), C("Content")).
  2629  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12)).
  2630  						Or(C("UserId").EQ(234))))
  2631  				return s
  2632  			}(),
  2633  			qs: []sharding.Query{
  2634  				{
  2635  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE (`user_id`=?) AND ((`order_id`=?) OR (`user_id`=?));",
  2636  					Args:       []any{123, int64(12), 234},
  2637  					DB:         "order_db_1",
  2638  					Datasource: "0.db.cluster.company.com:3306",
  2639  				},
  2640  			},
  2641  		},
  2642  		{
  2643  			name: "where or-and all",
  2644  			builder: func() sharding.QueryBuilder {
  2645  				s := NewShardingSelector[Order](shardingDB).
  2646  					Select(C("OrderId"), C("Content")).
  2647  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(181).And(C("UserId").EQ(234))))
  2648  				return s
  2649  			}(),
  2650  			qs: []sharding.Query{
  2651  				{
  2652  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));",
  2653  					Args:       []any{123, 181, 234},
  2654  					DB:         "order_db_1",
  2655  					Datasource: "0.db.cluster.company.com:3306",
  2656  				},
  2657  			},
  2658  		},
  2659  		{
  2660  			name: "where or-and",
  2661  			builder: func() sharding.QueryBuilder {
  2662  				s := NewShardingSelector[Order](shardingDB).
  2663  					Select(C("OrderId"), C("Content")).
  2664  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)).
  2665  						And(C("OrderId").EQ(int64(24))))
  2666  				return s
  2667  			}(),
  2668  			qs: []sharding.Query{
  2669  				{
  2670  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
  2671  					Args:       []any{123, 234, int64(24)},
  2672  					DB:         "order_db_1",
  2673  					Datasource: "0.db.cluster.company.com:3306",
  2674  				},
  2675  				{
  2676  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
  2677  					Args:       []any{123, 234, int64(24)},
  2678  					DB:         "order_db_0",
  2679  					Datasource: "0.db.cluster.company.com:3306",
  2680  				},
  2681  			},
  2682  		},
  2683  		{
  2684  			name: "where or-and broadcast",
  2685  			builder: func() sharding.QueryBuilder {
  2686  				s := NewShardingSelector[Order](shardingDB).
  2687  					Select(C("OrderId"), C("Content")).
  2688  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
  2689  						And(C("UserId").EQ(234)))
  2690  				return s
  2691  			}(),
  2692  			qs: []sharding.Query{
  2693  				{
  2694  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE ((`user_id`=?) OR (`order_id`=?)) AND (`user_id`=?);",
  2695  					Args:       []any{123, int64(12), 234},
  2696  					DB:         "order_db_0",
  2697  					Datasource: "0.db.cluster.company.com:3306",
  2698  				},
  2699  			},
  2700  		},
  2701  		{
  2702  			name: "where or-or",
  2703  			builder: func() sharding.QueryBuilder {
  2704  				s := NewShardingSelector[Order](shardingDB).
  2705  					Select(C("OrderId"), C("Content")).
  2706  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(253)).
  2707  						Or(C("UserId").EQ(234)))
  2708  				return s
  2709  			}(),
  2710  			qs: []sharding.Query{
  2711  				{
  2712  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
  2713  					Args:       []any{123, 253, 234},
  2714  					DB:         "order_db_1",
  2715  					Datasource: "0.db.cluster.company.com:3306",
  2716  				},
  2717  				{
  2718  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
  2719  					Args:       []any{123, 253, 234},
  2720  					DB:         "order_db_0",
  2721  					Datasource: "0.db.cluster.company.com:3306",
  2722  				},
  2723  			},
  2724  		},
  2725  		{
  2726  			name: "where or-or broadcast",
  2727  			builder: func() sharding.QueryBuilder {
  2728  				s := NewShardingSelector[Order](shardingDB).
  2729  					Select(C("OrderId"), C("Content")).
  2730  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
  2731  						Or(C("UserId").EQ(234)))
  2732  				return s
  2733  			}(),
  2734  			qs: []sharding.Query{
  2735  				{
  2736  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  2737  					Args:       []any{123, int64(12), 234},
  2738  					DB:         "order_db_0",
  2739  					Datasource: "0.db.cluster.company.com:3306",
  2740  				},
  2741  				{
  2742  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  2743  					Args:       []any{123, int64(12), 234},
  2744  					DB:         "order_db_1",
  2745  					Datasource: "0.db.cluster.company.com:3306",
  2746  				},
  2747  			},
  2748  		},
  2749  		{
  2750  			name: "where and-and",
  2751  			builder: func() sharding.QueryBuilder {
  2752  				s := NewShardingSelector[Order](shardingDB).
  2753  					Select(C("OrderId"), C("Content")).
  2754  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
  2755  						And(C("OrderId").EQ(int64(23))))
  2756  				return s
  2757  			}(),
  2758  			qs: []sharding.Query{
  2759  				{
  2760  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE ((`user_id`=?) AND (`order_id`=?)) AND (`order_id`=?);",
  2761  					Args:       []any{123, int64(12), int64(23)},
  2762  					DB:         "order_db_1",
  2763  					Datasource: "0.db.cluster.company.com:3306",
  2764  				},
  2765  			},
  2766  		},
  2767  		{
  2768  			name: "where and-or-and",
  2769  			builder: func() sharding.QueryBuilder {
  2770  				s := NewShardingSelector[Order](shardingDB).
  2771  					Select(C("OrderId"), C("Content")).
  2772  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
  2773  						Or(C("UserId").EQ(234).And(C("OrderId").EQ(int64(18)))))
  2774  				return s
  2775  			}(),
  2776  			qs: []sharding.Query{
  2777  				{
  2778  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
  2779  					Args:       []any{123, int64(12), 234, int64(18)},
  2780  					DB:         "order_db_1",
  2781  					Datasource: "0.db.cluster.company.com:3306",
  2782  				},
  2783  				{
  2784  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
  2785  					Args:       []any{123, int64(12), 234, int64(18)},
  2786  					DB:         "order_db_0",
  2787  					Datasource: "0.db.cluster.company.com:3306",
  2788  				},
  2789  			},
  2790  		},
  2791  		{
  2792  			name: "where lt",
  2793  			builder: func() sharding.QueryBuilder {
  2794  				s := NewShardingSelector[Order](shardingDB).
  2795  					Select(C("OrderId"), C("Content")).Where(C("UserId").LT(1))
  2796  				return s
  2797  			}(),
  2798  			qs: func() []sharding.Query {
  2799  				var res []sharding.Query
  2800  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`<?;"
  2801  				for b := 0; b < dbBase; b++ {
  2802  					dbName := fmt.Sprintf(dbPattern, b)
  2803  					res = append(res, sharding.Query{
  2804  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  2805  						Args:       []any{1},
  2806  						DB:         dbName,
  2807  						Datasource: dsPattern,
  2808  					})
  2809  				}
  2810  				return res
  2811  			}(),
  2812  		},
  2813  		{
  2814  			name: "where lt eq",
  2815  			builder: func() sharding.QueryBuilder {
  2816  				s := NewShardingSelector[Order](shardingDB).
  2817  					Select(C("OrderId"), C("Content")).Where(C("UserId").LTEQ(1))
  2818  				return s
  2819  			}(),
  2820  			qs: func() []sharding.Query {
  2821  				var res []sharding.Query
  2822  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`<=?;"
  2823  				for b := 0; b < dbBase; b++ {
  2824  					dbName := fmt.Sprintf(dbPattern, b)
  2825  					res = append(res, sharding.Query{
  2826  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  2827  						Args:       []any{1},
  2828  						DB:         dbName,
  2829  						Datasource: dsPattern,
  2830  					})
  2831  				}
  2832  				return res
  2833  			}(),
  2834  		},
  2835  		{
  2836  			name: "where eq and lt",
  2837  			builder: func() sharding.QueryBuilder {
  2838  				s := NewShardingSelector[Order](shardingDB).
  2839  					Select(C("OrderId"), C("Content")).Where(C("UserId").EQ(12).And(C("UserId").LT(133)))
  2840  				return s
  2841  			}(),
  2842  			qs: []sharding.Query{
  2843  				{
  2844  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE (`user_id`=?) AND (`user_id`<?);",
  2845  					Args:       []any{12, 133},
  2846  					DB:         "order_db_0",
  2847  					Datasource: "0.db.cluster.company.com:3306",
  2848  				},
  2849  			},
  2850  		},
  2851  		{
  2852  			name: "where gt",
  2853  			builder: func() sharding.QueryBuilder {
  2854  				s := NewShardingSelector[Order](shardingDB).
  2855  					Select(C("OrderId"), C("Content")).Where(C("UserId").GT(1))
  2856  				return s
  2857  			}(),
  2858  			qs: func() []sharding.Query {
  2859  				var res []sharding.Query
  2860  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`>?;"
  2861  				for b := 0; b < dbBase; b++ {
  2862  					dbName := fmt.Sprintf(dbPattern, b)
  2863  					res = append(res, sharding.Query{
  2864  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  2865  						Args:       []any{1},
  2866  						DB:         dbName,
  2867  						Datasource: dsPattern,
  2868  					})
  2869  				}
  2870  				return res
  2871  			}(),
  2872  		},
  2873  		{
  2874  			name: "where gt eq",
  2875  			builder: func() sharding.QueryBuilder {
  2876  				s := NewShardingSelector[Order](shardingDB).
  2877  					Select(C("OrderId"), C("Content")).Where(C("UserId").GTEQ(1))
  2878  				return s
  2879  			}(),
  2880  			qs: func() []sharding.Query {
  2881  				var res []sharding.Query
  2882  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`>=?;"
  2883  				for b := 0; b < dbBase; b++ {
  2884  					dbName := fmt.Sprintf(dbPattern, b)
  2885  					res = append(res, sharding.Query{
  2886  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  2887  						Args:       []any{1},
  2888  						DB:         dbName,
  2889  						Datasource: dsPattern,
  2890  					})
  2891  				}
  2892  				return res
  2893  			}(),
  2894  		},
  2895  		{
  2896  			name: "where eq and gt",
  2897  			builder: func() sharding.QueryBuilder {
  2898  				s := NewShardingSelector[Order](shardingDB).
  2899  					Select(C("OrderId"), C("Content")).Where(C("UserId").EQ(12).And(C("UserId").GT(133)))
  2900  				return s
  2901  			}(),
  2902  			qs: []sharding.Query{
  2903  				{
  2904  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE (`user_id`=?) AND (`user_id`>?);",
  2905  					Args:       []any{12, 133},
  2906  					DB:         "order_db_0",
  2907  					Datasource: "0.db.cluster.company.com:3306",
  2908  				},
  2909  			},
  2910  		},
  2911  		{
  2912  			name: "where eq and lt or gt",
  2913  			builder: func() sharding.QueryBuilder {
  2914  				s := NewShardingSelector[Order](shardingDB).
  2915  					Select(C("OrderId"), C("Content")).
  2916  					Where(C("UserId").EQ(12).
  2917  						And(C("UserId").LT(133)).Or(C("UserId").GT(234)))
  2918  				return s
  2919  			}(),
  2920  			qs: func() []sharding.Query {
  2921  				var res []sharding.Query
  2922  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE ((`user_id`=?) AND (`user_id`<?)) OR (`user_id`>?);"
  2923  				for b := 0; b < dbBase; b++ {
  2924  					dbName := fmt.Sprintf(dbPattern, b)
  2925  					res = append(res, sharding.Query{
  2926  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  2927  						Args:       []any{12, 133, 234},
  2928  						DB:         dbName,
  2929  						Datasource: dsPattern,
  2930  					})
  2931  				}
  2932  				return res
  2933  			}(),
  2934  		},
  2935  		{
  2936  			name: "where in",
  2937  			builder: func() sharding.QueryBuilder {
  2938  				s := NewShardingSelector[Order](shardingDB).
  2939  					Select(C("OrderId"), C("Content")).
  2940  					Where(C("UserId").In(12, 35, 101))
  2941  				return s
  2942  			}(),
  2943  			qs: []sharding.Query{
  2944  				{
  2945  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE `user_id` IN (?,?,?);",
  2946  					Args:       []any{12, 35, 101},
  2947  					DB:         "order_db_0",
  2948  					Datasource: "0.db.cluster.company.com:3306",
  2949  				},
  2950  				{
  2951  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE `user_id` IN (?,?,?);",
  2952  					Args:       []any{12, 35, 101},
  2953  					DB:         "order_db_1",
  2954  					Datasource: "0.db.cluster.company.com:3306",
  2955  				},
  2956  			},
  2957  		},
  2958  		{
  2959  			name: "where in and eq",
  2960  			builder: func() sharding.QueryBuilder {
  2961  				s := NewShardingSelector[Order](shardingDB).
  2962  					Select(C("OrderId"), C("Content")).
  2963  					Where(C("UserId").In(12, 35, 101).And(C("UserId").EQ(234)))
  2964  				return s
  2965  			}(),
  2966  			qs: []sharding.Query{
  2967  				{
  2968  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE (`user_id` IN (?,?,?)) AND (`user_id`=?);",
  2969  					Args:       []any{12, 35, 101, 234},
  2970  					DB:         "order_db_0",
  2971  					Datasource: "0.db.cluster.company.com:3306",
  2972  				},
  2973  			},
  2974  		},
  2975  		{
  2976  			name: "where in or eq",
  2977  			builder: func() sharding.QueryBuilder {
  2978  				s := NewShardingSelector[Order](shardingDB).
  2979  					Select(C("OrderId"), C("Content")).
  2980  					Where(C("UserId").In(12, 35, 101).Or(C("UserId").EQ(531)))
  2981  				return s
  2982  			}(),
  2983  			qs: []sharding.Query{
  2984  				{
  2985  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE (`user_id` IN (?,?,?)) OR (`user_id`=?);",
  2986  					Args:       []any{12, 35, 101, 531},
  2987  					DB:         "order_db_0",
  2988  					Datasource: "0.db.cluster.company.com:3306",
  2989  				},
  2990  				{
  2991  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab` WHERE (`user_id` IN (?,?,?)) OR (`user_id`=?);",
  2992  					Args:       []any{12, 35, 101, 531},
  2993  					DB:         "order_db_1",
  2994  					Datasource: "0.db.cluster.company.com:3306",
  2995  				},
  2996  			},
  2997  		},
  2998  		{
  2999  			name: "where in or gt",
  3000  			builder: func() sharding.QueryBuilder {
  3001  				s := NewShardingSelector[Order](shardingDB).
  3002  					Select(C("OrderId"), C("Content")).
  3003  					Where(C("UserId").In(12, 35, 101).Or(C("UserId").GT(531)))
  3004  				return s
  3005  			}(),
  3006  			qs: func() []sharding.Query {
  3007  				var res []sharding.Query
  3008  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` IN (?,?,?)) OR (`user_id`>?);"
  3009  				for b := 0; b < dbBase; b++ {
  3010  					dbName := fmt.Sprintf(dbPattern, b)
  3011  					res = append(res, sharding.Query{
  3012  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  3013  						Args:       []any{12, 35, 101, 531},
  3014  						DB:         dbName,
  3015  						Datasource: dsPattern,
  3016  					})
  3017  				}
  3018  				return res
  3019  			}(),
  3020  		},
  3021  		{
  3022  			name: "where not in",
  3023  			builder: func() sharding.QueryBuilder {
  3024  				s := NewShardingSelector[Order](shardingDB).
  3025  					Select(C("OrderId"), C("Content")).
  3026  					Where(C("UserId").NotIn(12, 35, 101))
  3027  				return s
  3028  			}(),
  3029  			qs: func() []sharding.Query {
  3030  				var res []sharding.Query
  3031  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id` NOT IN (?,?,?);"
  3032  				for b := 0; b < dbBase; b++ {
  3033  					dbName := fmt.Sprintf(dbPattern, b)
  3034  					res = append(res, sharding.Query{
  3035  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  3036  						Args:       []any{12, 35, 101},
  3037  						DB:         dbName,
  3038  						Datasource: dsPattern,
  3039  					})
  3040  				}
  3041  				return res
  3042  			}(),
  3043  		},
  3044  		{
  3045  			name: "where not in and eq",
  3046  			builder: func() sharding.QueryBuilder {
  3047  				s := NewShardingSelector[Order](shardingDB).
  3048  					Select(C("OrderId"), C("Content")).
  3049  					Where(C("UserId").NotIn(12, 35, 101).And(C("UserId").EQ(234)))
  3050  				return s
  3051  			}(),
  3052  			qs: []sharding.Query{
  3053  				{
  3054  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab` WHERE (`user_id` NOT IN (?,?,?)) AND (`user_id`=?);",
  3055  					Args:       []any{12, 35, 101, 234},
  3056  					DB:         "order_db_0",
  3057  					Datasource: "0.db.cluster.company.com:3306",
  3058  				},
  3059  			},
  3060  		},
  3061  		{
  3062  			name: "where not in or eq",
  3063  			builder: func() sharding.QueryBuilder {
  3064  				s := NewShardingSelector[Order](shardingDB).
  3065  					Select(C("OrderId"), C("Content")).
  3066  					Where(C("UserId").NotIn(12, 35, 101).Or(C("UserId").EQ(531)))
  3067  				return s
  3068  			}(),
  3069  			qs: func() []sharding.Query {
  3070  				var res []sharding.Query
  3071  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` NOT IN (?,?,?)) OR (`user_id`=?);"
  3072  				for b := 0; b < dbBase; b++ {
  3073  					dbName := fmt.Sprintf(dbPattern, b)
  3074  					res = append(res, sharding.Query{
  3075  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  3076  						Args:       []any{12, 35, 101, 531},
  3077  						DB:         dbName,
  3078  						Datasource: dsPattern,
  3079  					})
  3080  				}
  3081  				return res
  3082  			}(),
  3083  		},
  3084  		{
  3085  			name: "where not in or gt",
  3086  			builder: func() sharding.QueryBuilder {
  3087  				s := NewShardingSelector[Order](shardingDB).
  3088  					Select(C("OrderId"), C("Content")).
  3089  					Where(C("UserId").NotIn(12, 35, 101).Or(C("UserId").GT(531)))
  3090  				return s
  3091  			}(),
  3092  			qs: func() []sharding.Query {
  3093  				var res []sharding.Query
  3094  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` NOT IN (?,?,?)) OR (`user_id`>?);"
  3095  				for b := 0; b < dbBase; b++ {
  3096  					dbName := fmt.Sprintf(dbPattern, b)
  3097  					res = append(res, sharding.Query{
  3098  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  3099  						Args:       []any{12, 35, 101, 531},
  3100  						DB:         dbName,
  3101  						Datasource: dsPattern,
  3102  					})
  3103  				}
  3104  				return res
  3105  			}(),
  3106  		},
  3107  		{
  3108  			name: "where not gt",
  3109  			builder: func() sharding.QueryBuilder {
  3110  				s := NewShardingSelector[Order](shardingDB).
  3111  					Select(C("OrderId"), C("Content")).
  3112  					Where(Not(C("UserId").GT(101)))
  3113  				return s
  3114  			}(),
  3115  			qs: func() []sharding.Query {
  3116  				var res []sharding.Query
  3117  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`>?);"
  3118  				for b := 0; b < dbBase; b++ {
  3119  					dbName := fmt.Sprintf(dbPattern, b)
  3120  					res = append(res, sharding.Query{
  3121  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  3122  						Args:       []any{101},
  3123  						DB:         dbName,
  3124  						Datasource: dsPattern,
  3125  					})
  3126  				}
  3127  				return res
  3128  			}(),
  3129  		},
  3130  		{
  3131  			name: "where not lt",
  3132  			builder: func() sharding.QueryBuilder {
  3133  				s := NewShardingSelector[Order](shardingDB).
  3134  					Select(C("OrderId"), C("Content")).
  3135  					Where(Not(C("UserId").LT(101)))
  3136  				return s
  3137  			}(),
  3138  			qs: func() []sharding.Query {
  3139  				var res []sharding.Query
  3140  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`<?);"
  3141  				for b := 0; b < dbBase; b++ {
  3142  					dbName := fmt.Sprintf(dbPattern, b)
  3143  					res = append(res, sharding.Query{
  3144  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  3145  						Args:       []any{101},
  3146  						DB:         dbName,
  3147  						Datasource: dsPattern,
  3148  					})
  3149  				}
  3150  				return res
  3151  			}(),
  3152  		},
  3153  		{
  3154  			name: "where not (gt and lt)",
  3155  			builder: func() sharding.QueryBuilder {
  3156  				s := NewShardingSelector[Order](shardingDB).
  3157  					Select(C("OrderId"), C("Content")).
  3158  					Where(Not(C("UserId").GT(12).And(C("UserId").LT(531))))
  3159  				return s
  3160  			}(),
  3161  			qs: func() []sharding.Query {
  3162  				var res []sharding.Query
  3163  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`>?) AND (`user_id`<?));"
  3164  				for b := 0; b < dbBase; b++ {
  3165  					dbName := fmt.Sprintf(dbPattern, b)
  3166  					res = append(res, sharding.Query{
  3167  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  3168  						Args:       []any{12, 531},
  3169  						DB:         dbName,
  3170  						Datasource: dsPattern,
  3171  					})
  3172  				}
  3173  				return res
  3174  			}(),
  3175  		},
  3176  		{
  3177  			name: "where not (gt eq and lt eq)",
  3178  			builder: func() sharding.QueryBuilder {
  3179  				s := NewShardingSelector[Order](shardingDB).
  3180  					Select(C("OrderId"), C("Content")).
  3181  					Where(Not(C("UserId").GTEQ(12).And(C("UserId").LTEQ(531))))
  3182  				return s
  3183  			}(),
  3184  			qs: func() []sharding.Query {
  3185  				var res []sharding.Query
  3186  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`>=?) AND (`user_id`<=?));"
  3187  				for b := 0; b < dbBase; b++ {
  3188  					dbName := fmt.Sprintf(dbPattern, b)
  3189  					res = append(res, sharding.Query{
  3190  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  3191  						Args:       []any{12, 531},
  3192  						DB:         dbName,
  3193  						Datasource: dsPattern,
  3194  					})
  3195  				}
  3196  				return res
  3197  			}(),
  3198  		},
  3199  		{
  3200  			name: "where not (in or gt)",
  3201  			builder: func() sharding.QueryBuilder {
  3202  				s := NewShardingSelector[Order](shardingDB).
  3203  					Select(C("OrderId"), C("Content")).
  3204  					Where(Not(C("UserId").In(12, 35, 101).Or(C("UserId").GT(531))))
  3205  				return s
  3206  			}(),
  3207  			qs: func() []sharding.Query {
  3208  				var res []sharding.Query
  3209  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id` IN (?,?,?)) OR (`user_id`>?));"
  3210  				for b := 0; b < dbBase; b++ {
  3211  					dbName := fmt.Sprintf(dbPattern, b)
  3212  					res = append(res, sharding.Query{
  3213  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  3214  						Args:       []any{12, 35, 101, 531},
  3215  						DB:         dbName,
  3216  						Datasource: dsPattern,
  3217  					})
  3218  				}
  3219  				return res
  3220  			}(),
  3221  		},
  3222  		{
  3223  			name: "where not (in or eq)",
  3224  			builder: func() sharding.QueryBuilder {
  3225  				s := NewShardingSelector[Order](shardingDB).
  3226  					Select(C("OrderId"), C("Content")).
  3227  					Where(Not(C("UserId").In(12, 35, 101).Or(C("UserId").EQ(531))))
  3228  				return s
  3229  			}(),
  3230  			qs: func() []sharding.Query {
  3231  				var res []sharding.Query
  3232  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id` IN (?,?,?)) OR (`user_id`=?));"
  3233  				for b := 0; b < dbBase; b++ {
  3234  					dbName := fmt.Sprintf(dbPattern, b)
  3235  					res = append(res, sharding.Query{
  3236  						SQL:        fmt.Sprintf(sql, dbName, tablePattern),
  3237  						Args:       []any{12, 35, 101, 531},
  3238  						DB:         dbName,
  3239  						Datasource: dsPattern,
  3240  					})
  3241  				}
  3242  				return res
  3243  			}(),
  3244  		},
  3245  		{
  3246  			name: "and empty",
  3247  			builder: func() sharding.QueryBuilder {
  3248  				s := NewShardingSelector[Order](shardingDB).
  3249  					Select(C("OrderId"), C("Content")).
  3250  					Where(C("UserId").EQ(123).And(C("UserId").EQ(124)))
  3251  				return s
  3252  			}(),
  3253  			qs: []sharding.Query{},
  3254  		},
  3255  	}
  3256  
  3257  	for _, tc := range testCases {
  3258  		c := tc
  3259  		t.Run(c.name, func(t *testing.T) {
  3260  			qs, err := c.builder.Build(context.Background())
  3261  			assert.Equal(t, c.wantErr, err)
  3262  			if err != nil {
  3263  				return
  3264  			}
  3265  			assert.ElementsMatch(t, c.qs, qs)
  3266  		})
  3267  	}
  3268  }
  3269  
  3270  func TestShardingSelector_all_Build(t *testing.T) {
  3271  	r := model.NewMetaRegistry()
  3272  	dbBase, tableBase, dsBase := 2, 3, 2
  3273  	dbPattern, tablePattern, dsPattern := "order_db_%d", "order_tab_%d", "%d.db.cluster.company.com:3306"
  3274  	_, err := r.Register(&Order{},
  3275  		model.WithTableShardingAlgorithm(&hash.Hash{
  3276  			ShardingKey:  "UserId",
  3277  			DBPattern:    &hash.Pattern{Name: dbPattern, Base: dbBase},
  3278  			TablePattern: &hash.Pattern{Name: tablePattern, Base: tableBase},
  3279  			DsPattern:    &hash.Pattern{Name: dsPattern, Base: dsBase},
  3280  		}))
  3281  	require.NoError(t, err)
  3282  	m := map[string]*masterslave.MasterSlavesDB{
  3283  		"order_db_0": MasterSlavesMemoryDB(),
  3284  		"order_db_1": MasterSlavesMemoryDB(),
  3285  		"order_db_2": MasterSlavesMemoryDB(),
  3286  	}
  3287  	clusterDB := cluster.NewClusterDB(m)
  3288  	ds := map[string]datasource.DataSource{
  3289  		"0.db.cluster.company.com:3306": clusterDB,
  3290  		"1.db.cluster.company.com:3306": clusterDB,
  3291  	}
  3292  	shardingDB, err := OpenDS("sqlite3",
  3293  		shardingsource.NewShardingDataSource(ds), DBWithMetaRegistry(r))
  3294  	require.NoError(t, err)
  3295  
  3296  	testCases := []struct {
  3297  		name    string
  3298  		builder sharding.QueryBuilder
  3299  		qs      []sharding.Query
  3300  		wantErr error
  3301  	}{
  3302  		{
  3303  			name: "only eq",
  3304  			builder: func() sharding.QueryBuilder {
  3305  				s := NewShardingSelector[Order](shardingDB).Where(C("UserId").EQ(123))
  3306  				return s
  3307  			}(),
  3308  			qs: []sharding.Query{
  3309  				{
  3310  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_0` WHERE `user_id`=?;",
  3311  					Args:       []any{123},
  3312  					DB:         "order_db_1",
  3313  					Datasource: "1.db.cluster.company.com:3306",
  3314  				},
  3315  			},
  3316  		},
  3317  		{
  3318  			name: "only eq broadcast",
  3319  			builder: func() sharding.QueryBuilder {
  3320  				s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123))
  3321  				return s
  3322  			}(),
  3323  			qs: []sharding.Query{
  3324  				{
  3325  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_0` WHERE `order_id`=?;",
  3326  					Args:       []any{123},
  3327  					DB:         "order_db_0",
  3328  					Datasource: "0.db.cluster.company.com:3306",
  3329  				},
  3330  				{
  3331  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_1` WHERE `order_id`=?;",
  3332  					Args:       []any{123},
  3333  					DB:         "order_db_0",
  3334  					Datasource: "0.db.cluster.company.com:3306",
  3335  				},
  3336  				{
  3337  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_2` WHERE `order_id`=?;",
  3338  					Args:       []any{123},
  3339  					DB:         "order_db_0",
  3340  					Datasource: "0.db.cluster.company.com:3306",
  3341  				},
  3342  				{
  3343  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_0` WHERE `order_id`=?;",
  3344  					Args:       []any{123},
  3345  					DB:         "order_db_1",
  3346  					Datasource: "0.db.cluster.company.com:3306",
  3347  				},
  3348  				{
  3349  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_1` WHERE `order_id`=?;",
  3350  					Args:       []any{123},
  3351  					DB:         "order_db_1",
  3352  					Datasource: "0.db.cluster.company.com:3306",
  3353  				},
  3354  				{
  3355  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_2` WHERE `order_id`=?;",
  3356  					Args:       []any{123},
  3357  					DB:         "order_db_1",
  3358  					Datasource: "0.db.cluster.company.com:3306",
  3359  				},
  3360  				{
  3361  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_0` WHERE `order_id`=?;",
  3362  					Args:       []any{123},
  3363  					DB:         "order_db_0",
  3364  					Datasource: "1.db.cluster.company.com:3306",
  3365  				},
  3366  				{
  3367  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_1` WHERE `order_id`=?;",
  3368  					Args:       []any{123},
  3369  					DB:         "order_db_0",
  3370  					Datasource: "1.db.cluster.company.com:3306",
  3371  				},
  3372  				{
  3373  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_2` WHERE `order_id`=?;",
  3374  					Args:       []any{123},
  3375  					DB:         "order_db_0",
  3376  					Datasource: "1.db.cluster.company.com:3306",
  3377  				},
  3378  				{
  3379  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_0` WHERE `order_id`=?;",
  3380  					Args:       []any{123},
  3381  					DB:         "order_db_1",
  3382  					Datasource: "1.db.cluster.company.com:3306",
  3383  				},
  3384  				{
  3385  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_1` WHERE `order_id`=?;",
  3386  					Args:       []any{123},
  3387  					DB:         "order_db_1",
  3388  					Datasource: "1.db.cluster.company.com:3306",
  3389  				},
  3390  				{
  3391  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_2` WHERE `order_id`=?;",
  3392  					Args:       []any{123},
  3393  					DB:         "order_db_1",
  3394  					Datasource: "1.db.cluster.company.com:3306",
  3395  				},
  3396  			},
  3397  		},
  3398  		{
  3399  			name: "columns",
  3400  			builder: func() sharding.QueryBuilder {
  3401  				s := NewShardingSelector[Order](shardingDB).
  3402  					Select(Columns("Content", "OrderId")).Where(C("UserId").EQ(123))
  3403  				return s
  3404  			}(),
  3405  			qs: []sharding.Query{
  3406  				{
  3407  					SQL:        "SELECT `content`,`order_id` FROM `order_db_1`.`order_tab_0` WHERE `user_id`=?;",
  3408  					Args:       []any{123},
  3409  					DB:         "order_db_1",
  3410  					Datasource: "1.db.cluster.company.com:3306",
  3411  				},
  3412  			},
  3413  		},
  3414  		{
  3415  			name: "invalid columns",
  3416  			builder: func() sharding.QueryBuilder {
  3417  				s := NewShardingSelector[Order](shardingDB).
  3418  					Select(C("Invalid")).Where(C("UserId").EQ(123))
  3419  				return s
  3420  			}(),
  3421  			wantErr: errs.NewInvalidFieldError("Invalid"),
  3422  		},
  3423  		{
  3424  			name: "order by",
  3425  			builder: func() sharding.QueryBuilder {
  3426  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
  3427  					Where(C("UserId").EQ(123)).OrderBy(ASC("UserId"), DESC("OrderId"))
  3428  				return s
  3429  			}(),
  3430  			qs: []sharding.Query{
  3431  				{
  3432  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE `user_id`=? ORDER BY `user_id` ASC,`order_id` DESC;",
  3433  					Args:       []any{123},
  3434  					DB:         "order_db_1",
  3435  					Datasource: "1.db.cluster.company.com:3306",
  3436  				},
  3437  			},
  3438  		},
  3439  		{
  3440  			name: "group by",
  3441  			builder: func() sharding.QueryBuilder {
  3442  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
  3443  					Where(C("UserId").EQ(123)).GroupBy("UserId", "OrderId")
  3444  				return s
  3445  			}(),
  3446  			qs: []sharding.Query{
  3447  				{
  3448  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE `user_id`=? GROUP BY `user_id`,`order_id`;",
  3449  					Args:       []any{123},
  3450  					DB:         "order_db_1",
  3451  					Datasource: "1.db.cluster.company.com:3306",
  3452  				},
  3453  			},
  3454  		},
  3455  		{
  3456  			name: "having",
  3457  			builder: func() sharding.QueryBuilder {
  3458  				s := NewShardingSelector[Order](shardingDB).
  3459  					Select(C("OrderId"), C("Content")).
  3460  					Where(C("UserId").EQ(123)).GroupBy("OrderId").Having(C("OrderId").EQ(int64(18)))
  3461  				return s
  3462  			}(),
  3463  			qs: []sharding.Query{
  3464  				{
  3465  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE `user_id`=? GROUP BY `order_id` HAVING `order_id`=?;",
  3466  					Args:       []any{123, int64(18)},
  3467  					DB:         "order_db_1",
  3468  					Datasource: "1.db.cluster.company.com:3306",
  3469  				},
  3470  			},
  3471  		},
  3472  		{
  3473  			name: "where and left",
  3474  			builder: func() sharding.QueryBuilder {
  3475  				s := NewShardingSelector[Order](shardingDB).
  3476  					Select(C("OrderId"), C("Content")).
  3477  					Where(C("OrderId").EQ(int64(12)).And(C("UserId").EQ(123)))
  3478  				return s
  3479  			}(),
  3480  			qs: []sharding.Query{
  3481  				{
  3482  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`order_id`=?) AND (`user_id`=?);",
  3483  					Args:       []any{int64(12), 123},
  3484  					DB:         "order_db_1",
  3485  					Datasource: "1.db.cluster.company.com:3306",
  3486  				},
  3487  			},
  3488  		},
  3489  		{
  3490  			name: "where and right",
  3491  			builder: func() sharding.QueryBuilder {
  3492  				s := NewShardingSelector[Order](shardingDB).
  3493  					Select(C("OrderId"), C("Content")).
  3494  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))))
  3495  				return s
  3496  			}(),
  3497  			qs: []sharding.Query{
  3498  				{
  3499  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) AND (`order_id`=?);",
  3500  					Args:       []any{123, int64(12)},
  3501  					DB:         "order_db_1",
  3502  					Datasource: "1.db.cluster.company.com:3306",
  3503  				},
  3504  			},
  3505  		},
  3506  		{
  3507  			name: "where or",
  3508  			builder: func() sharding.QueryBuilder {
  3509  				s := NewShardingSelector[Order](shardingDB).
  3510  					Select(C("OrderId"), C("Content")).
  3511  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)))
  3512  				return s
  3513  			}(),
  3514  			qs: []sharding.Query{
  3515  				{
  3516  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) OR (`user_id`=?);",
  3517  					Args:       []any{123, 234},
  3518  					DB:         "order_db_1",
  3519  					Datasource: "1.db.cluster.company.com:3306",
  3520  				},
  3521  				{
  3522  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id`=?) OR (`user_id`=?);",
  3523  					Args:       []any{123, 234},
  3524  					DB:         "order_db_0",
  3525  					Datasource: "0.db.cluster.company.com:3306",
  3526  				},
  3527  			},
  3528  		},
  3529  		{
  3530  			name: "where or left broadcast",
  3531  			builder: func() sharding.QueryBuilder {
  3532  				s := NewShardingSelector[Order](shardingDB).
  3533  					Select(C("OrderId"), C("Content")).
  3534  					Where(C("OrderId").EQ(int64(12)).Or(C("UserId").EQ(123)))
  3535  				return s
  3536  			}(),
  3537  			qs: []sharding.Query{
  3538  				{
  3539  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`order_id`=?) OR (`user_id`=?);",
  3540  					Args:       []any{int64(12), 123},
  3541  					DB:         "order_db_0",
  3542  					Datasource: "0.db.cluster.company.com:3306",
  3543  				},
  3544  				{
  3545  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE (`order_id`=?) OR (`user_id`=?);",
  3546  					Args:       []any{int64(12), 123},
  3547  					DB:         "order_db_0",
  3548  					Datasource: "0.db.cluster.company.com:3306",
  3549  				},
  3550  				{
  3551  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE (`order_id`=?) OR (`user_id`=?);",
  3552  					Args:       []any{int64(12), 123},
  3553  					DB:         "order_db_0",
  3554  					Datasource: "0.db.cluster.company.com:3306",
  3555  				},
  3556  				{
  3557  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`order_id`=?) OR (`user_id`=?);",
  3558  					Args:       []any{int64(12), 123},
  3559  					DB:         "order_db_1",
  3560  					Datasource: "0.db.cluster.company.com:3306",
  3561  				},
  3562  				{
  3563  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE (`order_id`=?) OR (`user_id`=?);",
  3564  					Args:       []any{int64(12), 123},
  3565  					DB:         "order_db_1",
  3566  					Datasource: "0.db.cluster.company.com:3306",
  3567  				},
  3568  				{
  3569  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`order_id`=?) OR (`user_id`=?);",
  3570  					Args:       []any{int64(12), 123},
  3571  					DB:         "order_db_1",
  3572  					Datasource: "0.db.cluster.company.com:3306",
  3573  				},
  3574  				{
  3575  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`order_id`=?) OR (`user_id`=?);",
  3576  					Args:       []any{int64(12), 123},
  3577  					DB:         "order_db_0",
  3578  					Datasource: "1.db.cluster.company.com:3306",
  3579  				},
  3580  				{
  3581  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE (`order_id`=?) OR (`user_id`=?);",
  3582  					Args:       []any{int64(12), 123},
  3583  					DB:         "order_db_0",
  3584  					Datasource: "1.db.cluster.company.com:3306",
  3585  				},
  3586  				{
  3587  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE (`order_id`=?) OR (`user_id`=?);",
  3588  					Args:       []any{int64(12), 123},
  3589  					DB:         "order_db_0",
  3590  					Datasource: "1.db.cluster.company.com:3306",
  3591  				},
  3592  				{
  3593  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`order_id`=?) OR (`user_id`=?);",
  3594  					Args:       []any{int64(12), 123},
  3595  					DB:         "order_db_1",
  3596  					Datasource: "1.db.cluster.company.com:3306",
  3597  				},
  3598  				{
  3599  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE (`order_id`=?) OR (`user_id`=?);",
  3600  					Args:       []any{int64(12), 123},
  3601  					DB:         "order_db_1",
  3602  					Datasource: "1.db.cluster.company.com:3306",
  3603  				},
  3604  				{
  3605  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`order_id`=?) OR (`user_id`=?);",
  3606  					Args:       []any{int64(12), 123},
  3607  					DB:         "order_db_1",
  3608  					Datasource: "1.db.cluster.company.com:3306",
  3609  				},
  3610  			},
  3611  		},
  3612  		{
  3613  			name: "where or right broadcast",
  3614  			builder: func() sharding.QueryBuilder {
  3615  				s := NewShardingSelector[Order](shardingDB).
  3616  					Select(C("OrderId"), C("Content")).
  3617  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))))
  3618  				return s
  3619  			}(),
  3620  			qs: []sharding.Query{
  3621  				{
  3622  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id`=?) OR (`order_id`=?);",
  3623  					Args:       []any{123, int64(12)},
  3624  					DB:         "order_db_0",
  3625  					Datasource: "0.db.cluster.company.com:3306",
  3626  				},
  3627  				{
  3628  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE (`user_id`=?) OR (`order_id`=?);",
  3629  					Args:       []any{123, int64(12)},
  3630  					DB:         "order_db_0",
  3631  					Datasource: "0.db.cluster.company.com:3306",
  3632  				},
  3633  				{
  3634  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE (`user_id`=?) OR (`order_id`=?);",
  3635  					Args:       []any{123, int64(12)},
  3636  					DB:         "order_db_0",
  3637  					Datasource: "0.db.cluster.company.com:3306",
  3638  				},
  3639  				{
  3640  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) OR (`order_id`=?);",
  3641  					Args:       []any{123, int64(12)},
  3642  					DB:         "order_db_1",
  3643  					Datasource: "0.db.cluster.company.com:3306",
  3644  				},
  3645  				{
  3646  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE (`user_id`=?) OR (`order_id`=?);",
  3647  					Args:       []any{123, int64(12)},
  3648  					DB:         "order_db_1",
  3649  					Datasource: "0.db.cluster.company.com:3306",
  3650  				},
  3651  				{
  3652  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`user_id`=?) OR (`order_id`=?);",
  3653  					Args:       []any{123, int64(12)},
  3654  					DB:         "order_db_1",
  3655  					Datasource: "0.db.cluster.company.com:3306",
  3656  				},
  3657  				{
  3658  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id`=?) OR (`order_id`=?);",
  3659  					Args:       []any{123, int64(12)},
  3660  					DB:         "order_db_0",
  3661  					Datasource: "1.db.cluster.company.com:3306",
  3662  				},
  3663  				{
  3664  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE (`user_id`=?) OR (`order_id`=?);",
  3665  					Args:       []any{123, int64(12)},
  3666  					DB:         "order_db_0",
  3667  					Datasource: "1.db.cluster.company.com:3306",
  3668  				},
  3669  				{
  3670  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE (`user_id`=?) OR (`order_id`=?);",
  3671  					Args:       []any{123, int64(12)},
  3672  					DB:         "order_db_0",
  3673  					Datasource: "1.db.cluster.company.com:3306",
  3674  				},
  3675  				{
  3676  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) OR (`order_id`=?);",
  3677  					Args:       []any{123, int64(12)},
  3678  					DB:         "order_db_1",
  3679  					Datasource: "1.db.cluster.company.com:3306",
  3680  				},
  3681  				{
  3682  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE (`user_id`=?) OR (`order_id`=?);",
  3683  					Args:       []any{123, int64(12)},
  3684  					DB:         "order_db_1",
  3685  					Datasource: "1.db.cluster.company.com:3306",
  3686  				},
  3687  				{
  3688  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`user_id`=?) OR (`order_id`=?);",
  3689  					Args:       []any{123, int64(12)},
  3690  					DB:         "order_db_1",
  3691  					Datasource: "1.db.cluster.company.com:3306",
  3692  				},
  3693  			},
  3694  		},
  3695  		{
  3696  			name: "where and-or",
  3697  			builder: func() sharding.QueryBuilder {
  3698  				s := NewShardingSelector[Order](shardingDB).
  3699  					Select(C("OrderId"), C("Content")).
  3700  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).Or(C("UserId").EQ(234)))
  3701  				return s
  3702  			}(),
  3703  			qs: []sharding.Query{
  3704  				{
  3705  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
  3706  					Args:       []any{123, int64(12), 234},
  3707  					DB:         "order_db_1",
  3708  					Datasource: "1.db.cluster.company.com:3306",
  3709  				},
  3710  				{
  3711  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
  3712  					Args:       []any{123, int64(12), 234},
  3713  					DB:         "order_db_0",
  3714  					Datasource: "0.db.cluster.company.com:3306",
  3715  				},
  3716  			},
  3717  		},
  3718  		{
  3719  			name: "where and-or broadcast",
  3720  			builder: func() sharding.QueryBuilder {
  3721  				s := NewShardingSelector[Order](shardingDB).
  3722  					Select(C("OrderId"), C("Content")).
  3723  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12)).
  3724  						Or(C("UserId").EQ(234))))
  3725  				return s
  3726  			}(),
  3727  			qs: []sharding.Query{
  3728  				{
  3729  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) AND ((`order_id`=?) OR (`user_id`=?));",
  3730  					Args:       []any{123, int64(12), 234},
  3731  					DB:         "order_db_1",
  3732  					Datasource: "1.db.cluster.company.com:3306",
  3733  				},
  3734  			},
  3735  		},
  3736  		{
  3737  			name: "where or-and all",
  3738  			builder: func() sharding.QueryBuilder {
  3739  				s := NewShardingSelector[Order](shardingDB).
  3740  					Select(C("OrderId"), C("Content")).
  3741  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(181).And(C("UserId").EQ(234))))
  3742  				return s
  3743  			}(),
  3744  			qs: []sharding.Query{
  3745  				{
  3746  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));",
  3747  					Args:       []any{123, 181, 234},
  3748  					DB:         "order_db_1",
  3749  					Datasource: "1.db.cluster.company.com:3306",
  3750  				},
  3751  			},
  3752  		},
  3753  		{
  3754  			name: "where or-and",
  3755  			builder: func() sharding.QueryBuilder {
  3756  				s := NewShardingSelector[Order](shardingDB).
  3757  					Select(C("OrderId"), C("Content")).
  3758  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)).
  3759  						And(C("OrderId").EQ(int64(24))))
  3760  				return s
  3761  			}(),
  3762  			qs: []sharding.Query{
  3763  				{
  3764  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
  3765  					Args:       []any{123, 234, int64(24)},
  3766  					DB:         "order_db_1",
  3767  					Datasource: "1.db.cluster.company.com:3306",
  3768  				},
  3769  				{
  3770  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
  3771  					Args:       []any{123, 234, int64(24)},
  3772  					DB:         "order_db_0",
  3773  					Datasource: "0.db.cluster.company.com:3306",
  3774  				},
  3775  			},
  3776  		},
  3777  		{
  3778  			name: "where or-and broadcast",
  3779  			builder: func() sharding.QueryBuilder {
  3780  				s := NewShardingSelector[Order](shardingDB).
  3781  					Select(C("OrderId"), C("Content")).
  3782  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
  3783  						And(C("UserId").EQ(234)))
  3784  				return s
  3785  			}(),
  3786  			qs: []sharding.Query{
  3787  				{
  3788  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) AND (`user_id`=?);",
  3789  					Args:       []any{123, int64(12), 234},
  3790  					DB:         "order_db_0",
  3791  					Datasource: "0.db.cluster.company.com:3306",
  3792  				},
  3793  			},
  3794  		},
  3795  		{
  3796  			name: "where or-or",
  3797  			builder: func() sharding.QueryBuilder {
  3798  				s := NewShardingSelector[Order](shardingDB).
  3799  					Select(C("OrderId"), C("Content")).
  3800  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(253)).
  3801  						Or(C("UserId").EQ(234)))
  3802  				return s
  3803  			}(),
  3804  			qs: []sharding.Query{
  3805  				{
  3806  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
  3807  					Args:       []any{123, 253, 234},
  3808  					DB:         "order_db_1",
  3809  					Datasource: "1.db.cluster.company.com:3306",
  3810  				},
  3811  				{
  3812  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
  3813  					Args:       []any{123, 253, 234},
  3814  					DB:         "order_db_1",
  3815  					Datasource: "1.db.cluster.company.com:3306",
  3816  				},
  3817  				{
  3818  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
  3819  					Args:       []any{123, 253, 234},
  3820  					DB:         "order_db_0",
  3821  					Datasource: "0.db.cluster.company.com:3306",
  3822  				},
  3823  			},
  3824  		},
  3825  		{
  3826  			name: "where or-or broadcast",
  3827  			builder: func() sharding.QueryBuilder {
  3828  				s := NewShardingSelector[Order](shardingDB).
  3829  					Select(C("OrderId"), C("Content")).
  3830  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
  3831  						Or(C("UserId").EQ(234)))
  3832  				return s
  3833  			}(),
  3834  			qs: []sharding.Query{
  3835  				{
  3836  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3837  					Args:       []any{123, int64(12), 234},
  3838  					DB:         "order_db_0",
  3839  					Datasource: "0.db.cluster.company.com:3306",
  3840  				},
  3841  				{
  3842  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3843  					Args:       []any{123, int64(12), 234},
  3844  					DB:         "order_db_0",
  3845  					Datasource: "0.db.cluster.company.com:3306",
  3846  				},
  3847  				{
  3848  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3849  					Args:       []any{123, int64(12), 234},
  3850  					DB:         "order_db_0",
  3851  					Datasource: "0.db.cluster.company.com:3306",
  3852  				},
  3853  				{
  3854  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3855  					Args:       []any{123, int64(12), 234},
  3856  					DB:         "order_db_1",
  3857  					Datasource: "0.db.cluster.company.com:3306",
  3858  				},
  3859  				{
  3860  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3861  					Args:       []any{123, int64(12), 234},
  3862  					DB:         "order_db_1",
  3863  					Datasource: "0.db.cluster.company.com:3306",
  3864  				},
  3865  				{
  3866  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3867  					Args:       []any{123, int64(12), 234},
  3868  					DB:         "order_db_1",
  3869  					Datasource: "0.db.cluster.company.com:3306",
  3870  				},
  3871  				{
  3872  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3873  					Args:       []any{123, int64(12), 234},
  3874  					DB:         "order_db_0",
  3875  					Datasource: "1.db.cluster.company.com:3306",
  3876  				},
  3877  				{
  3878  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3879  					Args:       []any{123, int64(12), 234},
  3880  					DB:         "order_db_0",
  3881  					Datasource: "1.db.cluster.company.com:3306",
  3882  				},
  3883  				{
  3884  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3885  					Args:       []any{123, int64(12), 234},
  3886  					DB:         "order_db_0",
  3887  					Datasource: "1.db.cluster.company.com:3306",
  3888  				},
  3889  				{
  3890  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3891  					Args:       []any{123, int64(12), 234},
  3892  					DB:         "order_db_1",
  3893  					Datasource: "1.db.cluster.company.com:3306",
  3894  				},
  3895  				{
  3896  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3897  					Args:       []any{123, int64(12), 234},
  3898  					DB:         "order_db_1",
  3899  					Datasource: "1.db.cluster.company.com:3306",
  3900  				},
  3901  				{
  3902  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  3903  					Args:       []any{123, int64(12), 234},
  3904  					DB:         "order_db_1",
  3905  					Datasource: "1.db.cluster.company.com:3306",
  3906  				},
  3907  			},
  3908  		},
  3909  		{
  3910  			name: "where and-and",
  3911  			builder: func() sharding.QueryBuilder {
  3912  				s := NewShardingSelector[Order](shardingDB).
  3913  					Select(C("OrderId"), C("Content")).
  3914  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
  3915  						And(C("OrderId").EQ(int64(23))))
  3916  				return s
  3917  			}(),
  3918  			qs: []sharding.Query{
  3919  				{
  3920  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) AND (`order_id`=?);",
  3921  					Args:       []any{123, int64(12), int64(23)},
  3922  					DB:         "order_db_1",
  3923  					Datasource: "1.db.cluster.company.com:3306",
  3924  				},
  3925  			},
  3926  		},
  3927  		{
  3928  			name: "where and-or-and",
  3929  			builder: func() sharding.QueryBuilder {
  3930  				s := NewShardingSelector[Order](shardingDB).
  3931  					Select(C("OrderId"), C("Content")).
  3932  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
  3933  						Or(C("UserId").EQ(234).And(C("OrderId").EQ(int64(18)))))
  3934  				return s
  3935  			}(),
  3936  			qs: []sharding.Query{
  3937  				{
  3938  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
  3939  					Args:       []any{123, int64(12), 234, int64(18)},
  3940  					DB:         "order_db_1",
  3941  					Datasource: "1.db.cluster.company.com:3306",
  3942  				},
  3943  				{
  3944  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
  3945  					Args:       []any{123, int64(12), 234, int64(18)},
  3946  					DB:         "order_db_0",
  3947  					Datasource: "0.db.cluster.company.com:3306",
  3948  				},
  3949  			},
  3950  		},
  3951  		{
  3952  			name: "where lt",
  3953  			builder: func() sharding.QueryBuilder {
  3954  				s := NewShardingSelector[Order](shardingDB).
  3955  					Select(C("OrderId"), C("Content")).Where(C("UserId").LT(1))
  3956  				return s
  3957  			}(),
  3958  			qs: func() []sharding.Query {
  3959  				var res []sharding.Query
  3960  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`<?;"
  3961  				for a := 0; a < dsBase; a++ {
  3962  					dsName := fmt.Sprintf(dsPattern, a)
  3963  					for b := 0; b < dbBase; b++ {
  3964  						dbName := fmt.Sprintf(dbPattern, b)
  3965  						for c := 0; c < tableBase; c++ {
  3966  							tableName := fmt.Sprintf(tablePattern, c)
  3967  							res = append(res, sharding.Query{
  3968  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  3969  								Args:       []any{1},
  3970  								DB:         dbName,
  3971  								Datasource: dsName,
  3972  							})
  3973  						}
  3974  					}
  3975  				}
  3976  				return res
  3977  			}(),
  3978  		},
  3979  		{
  3980  			name: "where lt eq",
  3981  			builder: func() sharding.QueryBuilder {
  3982  				s := NewShardingSelector[Order](shardingDB).
  3983  					Select(C("OrderId"), C("Content")).Where(C("UserId").LTEQ(1))
  3984  				return s
  3985  			}(),
  3986  			qs: func() []sharding.Query {
  3987  				var res []sharding.Query
  3988  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`<=?;"
  3989  				for a := 0; a < dsBase; a++ {
  3990  					dsName := fmt.Sprintf(dsPattern, a)
  3991  					for b := 0; b < dbBase; b++ {
  3992  						dbName := fmt.Sprintf(dbPattern, b)
  3993  						for c := 0; c < tableBase; c++ {
  3994  							tableName := fmt.Sprintf(tablePattern, c)
  3995  							res = append(res, sharding.Query{
  3996  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  3997  								Args:       []any{1},
  3998  								DB:         dbName,
  3999  								Datasource: dsName,
  4000  							})
  4001  						}
  4002  					}
  4003  				}
  4004  				return res
  4005  			}(),
  4006  		},
  4007  		{
  4008  			name: "where eq and lt",
  4009  			builder: func() sharding.QueryBuilder {
  4010  				s := NewShardingSelector[Order](shardingDB).
  4011  					Select(C("OrderId"), C("Content")).Where(C("UserId").EQ(12).And(C("UserId").LT(133)))
  4012  				return s
  4013  			}(),
  4014  			qs: []sharding.Query{
  4015  				{
  4016  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id`=?) AND (`user_id`<?);",
  4017  					Args:       []any{12, 133},
  4018  					DB:         "order_db_0",
  4019  					Datasource: "0.db.cluster.company.com:3306",
  4020  				},
  4021  			},
  4022  		},
  4023  		{
  4024  			name: "where gt",
  4025  			builder: func() sharding.QueryBuilder {
  4026  				s := NewShardingSelector[Order](shardingDB).
  4027  					Select(C("OrderId"), C("Content")).Where(C("UserId").GT(1))
  4028  				return s
  4029  			}(),
  4030  			qs: func() []sharding.Query {
  4031  				var res []sharding.Query
  4032  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`>?;"
  4033  				for a := 0; a < dsBase; a++ {
  4034  					dsName := fmt.Sprintf(dsPattern, a)
  4035  					for b := 0; b < dbBase; b++ {
  4036  						dbName := fmt.Sprintf(dbPattern, b)
  4037  						for c := 0; c < tableBase; c++ {
  4038  							tableName := fmt.Sprintf(tablePattern, c)
  4039  							res = append(res, sharding.Query{
  4040  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4041  								Args:       []any{1},
  4042  								DB:         dbName,
  4043  								Datasource: dsName,
  4044  							})
  4045  						}
  4046  					}
  4047  				}
  4048  				return res
  4049  			}(),
  4050  		},
  4051  		{
  4052  			name: "where gt eq",
  4053  			builder: func() sharding.QueryBuilder {
  4054  				s := NewShardingSelector[Order](shardingDB).
  4055  					Select(C("OrderId"), C("Content")).Where(C("UserId").GTEQ(1))
  4056  				return s
  4057  			}(),
  4058  			qs: func() []sharding.Query {
  4059  				var res []sharding.Query
  4060  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`>=?;"
  4061  				for a := 0; a < dsBase; a++ {
  4062  					dsName := fmt.Sprintf(dsPattern, a)
  4063  					for b := 0; b < dbBase; b++ {
  4064  						dbName := fmt.Sprintf(dbPattern, b)
  4065  						for c := 0; c < tableBase; c++ {
  4066  							tableName := fmt.Sprintf(tablePattern, c)
  4067  							res = append(res, sharding.Query{
  4068  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4069  								Args:       []any{1},
  4070  								DB:         dbName,
  4071  								Datasource: dsName,
  4072  							})
  4073  						}
  4074  					}
  4075  				}
  4076  				return res
  4077  			}(),
  4078  		},
  4079  		{
  4080  			name: "where eq and gt",
  4081  			builder: func() sharding.QueryBuilder {
  4082  				s := NewShardingSelector[Order](shardingDB).
  4083  					Select(C("OrderId"), C("Content")).Where(C("UserId").EQ(12).And(C("UserId").GT(133)))
  4084  				return s
  4085  			}(),
  4086  			qs: []sharding.Query{
  4087  				{
  4088  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id`=?) AND (`user_id`>?);",
  4089  					Args:       []any{12, 133},
  4090  					DB:         "order_db_0",
  4091  					Datasource: "0.db.cluster.company.com:3306",
  4092  				},
  4093  			},
  4094  		},
  4095  		{
  4096  			name: "where eq and lt or gt",
  4097  			builder: func() sharding.QueryBuilder {
  4098  				s := NewShardingSelector[Order](shardingDB).
  4099  					Select(C("OrderId"), C("Content")).
  4100  					Where(C("UserId").EQ(12).
  4101  						And(C("UserId").LT(133)).Or(C("UserId").GT(234)))
  4102  				return s
  4103  			}(),
  4104  			qs: func() []sharding.Query {
  4105  				var res []sharding.Query
  4106  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE ((`user_id`=?) AND (`user_id`<?)) OR (`user_id`>?);"
  4107  				for a := 0; a < dsBase; a++ {
  4108  					dsName := fmt.Sprintf(dsPattern, a)
  4109  					for b := 0; b < dbBase; b++ {
  4110  						dbName := fmt.Sprintf(dbPattern, b)
  4111  						for c := 0; c < tableBase; c++ {
  4112  							tableName := fmt.Sprintf(tablePattern, c)
  4113  							res = append(res, sharding.Query{
  4114  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4115  								Args:       []any{12, 133, 234},
  4116  								DB:         dbName,
  4117  								Datasource: dsName,
  4118  							})
  4119  						}
  4120  					}
  4121  				}
  4122  				return res
  4123  			}(),
  4124  		},
  4125  		{
  4126  			name: "where in",
  4127  			builder: func() sharding.QueryBuilder {
  4128  				s := NewShardingSelector[Order](shardingDB).
  4129  					Select(C("OrderId"), C("Content")).
  4130  					Where(C("UserId").In(12, 35, 101))
  4131  				return s
  4132  			}(),
  4133  			qs: []sharding.Query{
  4134  				{
  4135  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE `user_id` IN (?,?,?);",
  4136  					Args:       []any{12, 35, 101},
  4137  					DB:         "order_db_0",
  4138  					Datasource: "0.db.cluster.company.com:3306",
  4139  				},
  4140  				{
  4141  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE `user_id` IN (?,?,?);",
  4142  					Args:       []any{12, 35, 101},
  4143  					DB:         "order_db_1",
  4144  					Datasource: "1.db.cluster.company.com:3306",
  4145  				},
  4146  			},
  4147  		},
  4148  		{
  4149  			name: "where in and eq",
  4150  			builder: func() sharding.QueryBuilder {
  4151  				s := NewShardingSelector[Order](shardingDB).
  4152  					Select(C("OrderId"), C("Content")).
  4153  					Where(C("UserId").In(12, 35, 101).And(C("UserId").EQ(234)))
  4154  				return s
  4155  			}(),
  4156  			qs: []sharding.Query{
  4157  				{
  4158  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id` IN (?,?,?)) AND (`user_id`=?);",
  4159  					Args:       []any{12, 35, 101, 234},
  4160  					DB:         "order_db_0",
  4161  					Datasource: "0.db.cluster.company.com:3306",
  4162  				},
  4163  			},
  4164  		},
  4165  		{
  4166  			name: "where in or eq",
  4167  			builder: func() sharding.QueryBuilder {
  4168  				s := NewShardingSelector[Order](shardingDB).
  4169  					Select(C("OrderId"), C("Content")).
  4170  					Where(C("UserId").In(12, 35, 101).Or(C("UserId").EQ(531)))
  4171  				return s
  4172  			}(),
  4173  			qs: []sharding.Query{
  4174  				{
  4175  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id` IN (?,?,?)) OR (`user_id`=?);",
  4176  					Args:       []any{12, 35, 101, 531},
  4177  					DB:         "order_db_0",
  4178  					Datasource: "0.db.cluster.company.com:3306",
  4179  				},
  4180  				{
  4181  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id` IN (?,?,?)) OR (`user_id`=?);",
  4182  					Args:       []any{12, 35, 101, 531},
  4183  					DB:         "order_db_1",
  4184  					Datasource: "1.db.cluster.company.com:3306",
  4185  				},
  4186  				{
  4187  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`user_id` IN (?,?,?)) OR (`user_id`=?);",
  4188  					Args:       []any{12, 35, 101, 531},
  4189  					DB:         "order_db_1",
  4190  					Datasource: "1.db.cluster.company.com:3306",
  4191  				},
  4192  			},
  4193  		},
  4194  		{
  4195  			name: "where in or gt",
  4196  			builder: func() sharding.QueryBuilder {
  4197  				s := NewShardingSelector[Order](shardingDB).
  4198  					Select(C("OrderId"), C("Content")).
  4199  					Where(C("UserId").In(12, 35, 101).Or(C("UserId").GT(531)))
  4200  				return s
  4201  			}(),
  4202  			qs: func() []sharding.Query {
  4203  				var res []sharding.Query
  4204  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` IN (?,?,?)) OR (`user_id`>?);"
  4205  				for a := 0; a < dsBase; a++ {
  4206  					dsName := fmt.Sprintf(dsPattern, a)
  4207  					for b := 0; b < dbBase; b++ {
  4208  						dbName := fmt.Sprintf(dbPattern, b)
  4209  						for c := 0; c < tableBase; c++ {
  4210  							tableName := fmt.Sprintf(tablePattern, c)
  4211  							res = append(res, sharding.Query{
  4212  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4213  								Args:       []any{12, 35, 101, 531},
  4214  								DB:         dbName,
  4215  								Datasource: dsName,
  4216  							})
  4217  						}
  4218  					}
  4219  				}
  4220  				return res
  4221  			}(),
  4222  		},
  4223  		{
  4224  			name: "where not in",
  4225  			builder: func() sharding.QueryBuilder {
  4226  				s := NewShardingSelector[Order](shardingDB).
  4227  					Select(C("OrderId"), C("Content")).
  4228  					Where(C("UserId").NotIn(12, 35, 101))
  4229  				return s
  4230  			}(),
  4231  			qs: func() []sharding.Query {
  4232  				var res []sharding.Query
  4233  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id` NOT IN (?,?,?);"
  4234  				for a := 0; a < dsBase; a++ {
  4235  					dsName := fmt.Sprintf(dsPattern, a)
  4236  					for b := 0; b < dbBase; b++ {
  4237  						dbName := fmt.Sprintf(dbPattern, b)
  4238  						for c := 0; c < tableBase; c++ {
  4239  							tableName := fmt.Sprintf(tablePattern, c)
  4240  							res = append(res, sharding.Query{
  4241  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4242  								Args:       []any{12, 35, 101},
  4243  								DB:         dbName,
  4244  								Datasource: dsName,
  4245  							})
  4246  						}
  4247  					}
  4248  				}
  4249  				return res
  4250  			}(),
  4251  		},
  4252  		{
  4253  			name: "where not in and eq",
  4254  			builder: func() sharding.QueryBuilder {
  4255  				s := NewShardingSelector[Order](shardingDB).
  4256  					Select(C("OrderId"), C("Content")).
  4257  					Where(C("UserId").NotIn(12, 35, 101).And(C("UserId").EQ(234)))
  4258  				return s
  4259  			}(),
  4260  			qs: []sharding.Query{
  4261  				{
  4262  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id` NOT IN (?,?,?)) AND (`user_id`=?);",
  4263  					Args:       []any{12, 35, 101, 234},
  4264  					DB:         "order_db_0",
  4265  					Datasource: "0.db.cluster.company.com:3306",
  4266  				},
  4267  			},
  4268  		},
  4269  		{
  4270  			name: "where not in or eq",
  4271  			builder: func() sharding.QueryBuilder {
  4272  				s := NewShardingSelector[Order](shardingDB).
  4273  					Select(C("OrderId"), C("Content")).
  4274  					Where(C("UserId").NotIn(12, 35, 101).Or(C("UserId").EQ(531)))
  4275  				return s
  4276  			}(),
  4277  			qs: func() []sharding.Query {
  4278  				var res []sharding.Query
  4279  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` NOT IN (?,?,?)) OR (`user_id`=?);"
  4280  				for a := 0; a < dsBase; a++ {
  4281  					dsName := fmt.Sprintf(dsPattern, a)
  4282  					for b := 0; b < dbBase; b++ {
  4283  						dbName := fmt.Sprintf(dbPattern, b)
  4284  						for c := 0; c < tableBase; c++ {
  4285  							tableName := fmt.Sprintf(tablePattern, c)
  4286  							res = append(res, sharding.Query{
  4287  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4288  								Args:       []any{12, 35, 101, 531},
  4289  								DB:         dbName,
  4290  								Datasource: dsName,
  4291  							})
  4292  						}
  4293  					}
  4294  				}
  4295  				return res
  4296  			}(),
  4297  		},
  4298  		{
  4299  			name: "where not in or gt",
  4300  			builder: func() sharding.QueryBuilder {
  4301  				s := NewShardingSelector[Order](shardingDB).
  4302  					Select(C("OrderId"), C("Content")).
  4303  					Where(C("UserId").NotIn(12, 35, 101).Or(C("UserId").GT(531)))
  4304  				return s
  4305  			}(),
  4306  			qs: func() []sharding.Query {
  4307  				var res []sharding.Query
  4308  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` NOT IN (?,?,?)) OR (`user_id`>?);"
  4309  				for a := 0; a < dsBase; a++ {
  4310  					dsName := fmt.Sprintf(dsPattern, a)
  4311  					for b := 0; b < dbBase; b++ {
  4312  						dbName := fmt.Sprintf(dbPattern, b)
  4313  						for c := 0; c < tableBase; c++ {
  4314  							tableName := fmt.Sprintf(tablePattern, c)
  4315  							res = append(res, sharding.Query{
  4316  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4317  								Args:       []any{12, 35, 101, 531},
  4318  								DB:         dbName,
  4319  								Datasource: dsName,
  4320  							})
  4321  						}
  4322  					}
  4323  				}
  4324  				return res
  4325  			}(),
  4326  		},
  4327  		{
  4328  			name: "where not gt",
  4329  			builder: func() sharding.QueryBuilder {
  4330  				s := NewShardingSelector[Order](shardingDB).
  4331  					Select(C("OrderId"), C("Content")).
  4332  					Where(Not(C("UserId").GT(101)))
  4333  				return s
  4334  			}(),
  4335  			qs: func() []sharding.Query {
  4336  				var res []sharding.Query
  4337  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`>?);"
  4338  				for a := 0; a < dsBase; a++ {
  4339  					dsName := fmt.Sprintf(dsPattern, a)
  4340  					for b := 0; b < dbBase; b++ {
  4341  						dbName := fmt.Sprintf(dbPattern, b)
  4342  						for c := 0; c < tableBase; c++ {
  4343  							tableName := fmt.Sprintf(tablePattern, c)
  4344  							res = append(res, sharding.Query{
  4345  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4346  								Args:       []any{101},
  4347  								DB:         dbName,
  4348  								Datasource: dsName,
  4349  							})
  4350  						}
  4351  					}
  4352  				}
  4353  				return res
  4354  			}(),
  4355  		},
  4356  		{
  4357  			name: "where not lt",
  4358  			builder: func() sharding.QueryBuilder {
  4359  				s := NewShardingSelector[Order](shardingDB).
  4360  					Select(C("OrderId"), C("Content")).
  4361  					Where(Not(C("UserId").LT(101)))
  4362  				return s
  4363  			}(),
  4364  			qs: func() []sharding.Query {
  4365  				var res []sharding.Query
  4366  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`<?);"
  4367  				for a := 0; a < dsBase; a++ {
  4368  					dsName := fmt.Sprintf(dsPattern, a)
  4369  					for b := 0; b < dbBase; b++ {
  4370  						dbName := fmt.Sprintf(dbPattern, b)
  4371  						for c := 0; c < tableBase; c++ {
  4372  							tableName := fmt.Sprintf(tablePattern, c)
  4373  							res = append(res, sharding.Query{
  4374  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4375  								Args:       []any{101},
  4376  								DB:         dbName,
  4377  								Datasource: dsName,
  4378  							})
  4379  						}
  4380  					}
  4381  				}
  4382  				return res
  4383  			}(),
  4384  		},
  4385  		{
  4386  			name: "where not (gt and lt)",
  4387  			builder: func() sharding.QueryBuilder {
  4388  				s := NewShardingSelector[Order](shardingDB).
  4389  					Select(C("OrderId"), C("Content")).
  4390  					Where(Not(C("UserId").GT(12).And(C("UserId").LT(531))))
  4391  				return s
  4392  			}(),
  4393  			qs: func() []sharding.Query {
  4394  				var res []sharding.Query
  4395  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`>?) AND (`user_id`<?));"
  4396  				for a := 0; a < dsBase; a++ {
  4397  					dsName := fmt.Sprintf(dsPattern, a)
  4398  					for b := 0; b < dbBase; b++ {
  4399  						dbName := fmt.Sprintf(dbPattern, b)
  4400  						for c := 0; c < tableBase; c++ {
  4401  							tableName := fmt.Sprintf(tablePattern, c)
  4402  							res = append(res, sharding.Query{
  4403  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4404  								Args:       []any{12, 531},
  4405  								DB:         dbName,
  4406  								Datasource: dsName,
  4407  							})
  4408  						}
  4409  					}
  4410  				}
  4411  				return res
  4412  			}(),
  4413  		},
  4414  		{
  4415  			name: "where not (gt eq and lt eq)",
  4416  			builder: func() sharding.QueryBuilder {
  4417  				s := NewShardingSelector[Order](shardingDB).
  4418  					Select(C("OrderId"), C("Content")).
  4419  					Where(Not(C("UserId").GTEQ(12).And(C("UserId").LTEQ(531))))
  4420  				return s
  4421  			}(),
  4422  			qs: func() []sharding.Query {
  4423  				var res []sharding.Query
  4424  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`>=?) AND (`user_id`<=?));"
  4425  				for a := 0; a < dsBase; a++ {
  4426  					dsName := fmt.Sprintf(dsPattern, a)
  4427  					for b := 0; b < dbBase; b++ {
  4428  						dbName := fmt.Sprintf(dbPattern, b)
  4429  						for c := 0; c < tableBase; c++ {
  4430  							tableName := fmt.Sprintf(tablePattern, c)
  4431  							res = append(res, sharding.Query{
  4432  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4433  								Args:       []any{12, 531},
  4434  								DB:         dbName,
  4435  								Datasource: dsName,
  4436  							})
  4437  						}
  4438  					}
  4439  				}
  4440  				return res
  4441  			}(),
  4442  		},
  4443  		{
  4444  			name: "where not (in or gt)",
  4445  			builder: func() sharding.QueryBuilder {
  4446  				s := NewShardingSelector[Order](shardingDB).
  4447  					Select(C("OrderId"), C("Content")).
  4448  					Where(Not(C("UserId").In(12, 35, 101).Or(C("UserId").GT(531))))
  4449  				return s
  4450  			}(),
  4451  			qs: func() []sharding.Query {
  4452  				var res []sharding.Query
  4453  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id` IN (?,?,?)) OR (`user_id`>?));"
  4454  				for a := 0; a < dsBase; a++ {
  4455  					dsName := fmt.Sprintf(dsPattern, a)
  4456  					for b := 0; b < dbBase; b++ {
  4457  						dbName := fmt.Sprintf(dbPattern, b)
  4458  						for c := 0; c < tableBase; c++ {
  4459  							tableName := fmt.Sprintf(tablePattern, c)
  4460  							res = append(res, sharding.Query{
  4461  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4462  								Args:       []any{12, 35, 101, 531},
  4463  								DB:         dbName,
  4464  								Datasource: dsName,
  4465  							})
  4466  						}
  4467  					}
  4468  				}
  4469  				return res
  4470  			}(),
  4471  		},
  4472  		{
  4473  			name: "where not (in or eq)",
  4474  			builder: func() sharding.QueryBuilder {
  4475  				s := NewShardingSelector[Order](shardingDB).
  4476  					Select(C("OrderId"), C("Content")).
  4477  					Where(Not(C("UserId").In(12, 35, 101).Or(C("UserId").EQ(531))))
  4478  				return s
  4479  			}(),
  4480  			qs: func() []sharding.Query {
  4481  				var res []sharding.Query
  4482  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id` IN (?,?,?)) OR (`user_id`=?));"
  4483  				for a := 0; a < dsBase; a++ {
  4484  					dsName := fmt.Sprintf(dsPattern, a)
  4485  					for b := 0; b < dbBase; b++ {
  4486  						dbName := fmt.Sprintf(dbPattern, b)
  4487  						for c := 0; c < tableBase; c++ {
  4488  							tableName := fmt.Sprintf(tablePattern, c)
  4489  							res = append(res, sharding.Query{
  4490  								SQL:        fmt.Sprintf(sql, dbName, tableName),
  4491  								Args:       []any{12, 35, 101, 531},
  4492  								DB:         dbName,
  4493  								Datasource: dsName,
  4494  							})
  4495  						}
  4496  					}
  4497  				}
  4498  				return res
  4499  			}(),
  4500  		},
  4501  		{
  4502  			name: "and empty",
  4503  			builder: func() sharding.QueryBuilder {
  4504  				s := NewShardingSelector[Order](shardingDB).
  4505  					Select(C("OrderId"), C("Content")).
  4506  					Where(C("UserId").EQ(123).And(C("UserId").EQ(124)))
  4507  				return s
  4508  			}(),
  4509  			qs: []sharding.Query{},
  4510  		},
  4511  	}
  4512  
  4513  	for _, tc := range testCases {
  4514  		c := tc
  4515  		t.Run(c.name, func(t *testing.T) {
  4516  			qs, err := c.builder.Build(context.Background())
  4517  			assert.Equal(t, c.wantErr, err)
  4518  			if err != nil {
  4519  				return
  4520  			}
  4521  			assert.ElementsMatch(t, c.qs, qs)
  4522  		})
  4523  	}
  4524  }
  4525  
  4526  func TestShardingSelector_Build(t *testing.T) {
  4527  	r := model.NewMetaRegistry()
  4528  	dbBase, tableBase := 2, 3
  4529  	dbPattern, tablePattern, dsPattern := "order_db_%d", "order_tab_%d", "0.db.cluster.company.com:3306"
  4530  	_, err := r.Register(&Order{},
  4531  		model.WithTableShardingAlgorithm(&hash.Hash{
  4532  			ShardingKey:  "UserId",
  4533  			DBPattern:    &hash.Pattern{Name: dbPattern, Base: dbBase},
  4534  			TablePattern: &hash.Pattern{Name: tablePattern, Base: tableBase},
  4535  			DsPattern:    &hash.Pattern{Name: dsPattern, NotSharding: true},
  4536  		}))
  4537  	require.NoError(t, err)
  4538  
  4539  	m := map[string]*masterslave.MasterSlavesDB{
  4540  		"order_db_0": MasterSlavesMemoryDB(),
  4541  		"order_db_1": MasterSlavesMemoryDB(),
  4542  		"order_db_2": MasterSlavesMemoryDB(),
  4543  	}
  4544  	clusterDB := cluster.NewClusterDB(m)
  4545  	ds := map[string]datasource.DataSource{
  4546  		"0.db.cluster.company.com:3306": clusterDB,
  4547  	}
  4548  	shardingDB, err := OpenDS("sqlite3",
  4549  		shardingsource.NewShardingDataSource(ds), DBWithMetaRegistry(r))
  4550  	require.NoError(t, err)
  4551  
  4552  	testCases := []struct {
  4553  		name    string
  4554  		builder sharding.QueryBuilder
  4555  		qs      []sharding.Query
  4556  		wantErr error
  4557  	}{
  4558  		{
  4559  			name: "only eq",
  4560  			builder: func() sharding.QueryBuilder {
  4561  				s := NewShardingSelector[Order](shardingDB).Where(C("UserId").EQ(123))
  4562  				return s
  4563  			}(),
  4564  			qs: []sharding.Query{
  4565  				{
  4566  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_0` WHERE `user_id`=?;",
  4567  					Args:       []any{123},
  4568  					DB:         "order_db_1",
  4569  					Datasource: "0.db.cluster.company.com:3306",
  4570  				},
  4571  			},
  4572  		},
  4573  		{
  4574  			name: "only eq broadcast",
  4575  			builder: func() sharding.QueryBuilder {
  4576  				s := NewShardingSelector[Order](shardingDB).Where(C("OrderId").EQ(123))
  4577  				return s
  4578  			}(),
  4579  			qs: []sharding.Query{
  4580  				{
  4581  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_0` WHERE `order_id`=?;",
  4582  					Args:       []any{123},
  4583  					DB:         "order_db_0",
  4584  					Datasource: "0.db.cluster.company.com:3306",
  4585  				},
  4586  				{
  4587  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_1` WHERE `order_id`=?;",
  4588  					Args:       []any{123},
  4589  					DB:         "order_db_0",
  4590  					Datasource: "0.db.cluster.company.com:3306",
  4591  				},
  4592  				{
  4593  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_0`.`order_tab_2` WHERE `order_id`=?;",
  4594  					Args:       []any{123},
  4595  					DB:         "order_db_0",
  4596  					Datasource: "0.db.cluster.company.com:3306",
  4597  				},
  4598  				{
  4599  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_0` WHERE `order_id`=?;",
  4600  					Args:       []any{123},
  4601  					DB:         "order_db_1",
  4602  					Datasource: "0.db.cluster.company.com:3306",
  4603  				},
  4604  				{
  4605  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_1` WHERE `order_id`=?;",
  4606  					Args:       []any{123},
  4607  					DB:         "order_db_1",
  4608  					Datasource: "0.db.cluster.company.com:3306",
  4609  				},
  4610  				{
  4611  					SQL:        "SELECT `user_id`,`order_id`,`content`,`account` FROM `order_db_1`.`order_tab_2` WHERE `order_id`=?;",
  4612  					Args:       []any{123},
  4613  					DB:         "order_db_1",
  4614  					Datasource: "0.db.cluster.company.com:3306",
  4615  				},
  4616  			},
  4617  		},
  4618  		{
  4619  			name: "columns",
  4620  			builder: func() sharding.QueryBuilder {
  4621  				s := NewShardingSelector[Order](shardingDB).
  4622  					Select(Columns("Content", "OrderId")).Where(C("UserId").EQ(123))
  4623  				return s
  4624  			}(),
  4625  			qs: []sharding.Query{
  4626  				{
  4627  					SQL:        "SELECT `content`,`order_id` FROM `order_db_1`.`order_tab_0` WHERE `user_id`=?;",
  4628  					Args:       []any{123},
  4629  					DB:         "order_db_1",
  4630  					Datasource: "0.db.cluster.company.com:3306",
  4631  				},
  4632  			},
  4633  		},
  4634  		{
  4635  			name: "invalid columns",
  4636  			builder: func() sharding.QueryBuilder {
  4637  				s := NewShardingSelector[Order](shardingDB).
  4638  					Select(C("Invalid")).Where(C("UserId").EQ(123))
  4639  				return s
  4640  			}(),
  4641  			wantErr: errs.NewInvalidFieldError("Invalid"),
  4642  		},
  4643  		{
  4644  			name: "order by",
  4645  			builder: func() sharding.QueryBuilder {
  4646  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
  4647  					Where(C("UserId").EQ(123)).OrderBy(ASC("UserId"), DESC("OrderId"))
  4648  				return s
  4649  			}(),
  4650  			qs: []sharding.Query{
  4651  				{
  4652  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE `user_id`=? ORDER BY `user_id` ASC,`order_id` DESC;",
  4653  					Args:       []any{123},
  4654  					DB:         "order_db_1",
  4655  					Datasource: "0.db.cluster.company.com:3306",
  4656  				},
  4657  			},
  4658  		},
  4659  		{
  4660  			name: "group by",
  4661  			builder: func() sharding.QueryBuilder {
  4662  				s := NewShardingSelector[Order](shardingDB).Select(C("OrderId"), C("Content")).
  4663  					Where(C("UserId").EQ(123)).GroupBy("UserId", "OrderId")
  4664  				return s
  4665  			}(),
  4666  			qs: []sharding.Query{
  4667  				{
  4668  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE `user_id`=? GROUP BY `user_id`,`order_id`;",
  4669  					Args:       []any{123},
  4670  					DB:         "order_db_1",
  4671  					Datasource: "0.db.cluster.company.com:3306",
  4672  				},
  4673  			},
  4674  		},
  4675  		{
  4676  			name: "having",
  4677  			builder: func() sharding.QueryBuilder {
  4678  				s := NewShardingSelector[Order](shardingDB).
  4679  					Select(C("OrderId"), C("Content")).
  4680  					Where(C("UserId").EQ(123)).GroupBy("OrderId").Having(C("OrderId").EQ(int64(18)))
  4681  				return s
  4682  			}(),
  4683  			qs: []sharding.Query{
  4684  				{
  4685  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE `user_id`=? GROUP BY `order_id` HAVING `order_id`=?;",
  4686  					Args:       []any{123, int64(18)},
  4687  					DB:         "order_db_1",
  4688  					Datasource: "0.db.cluster.company.com:3306",
  4689  				},
  4690  			},
  4691  		},
  4692  		{
  4693  			name: "where and left",
  4694  			builder: func() sharding.QueryBuilder {
  4695  				s := NewShardingSelector[Order](shardingDB).
  4696  					Select(C("OrderId"), C("Content")).
  4697  					Where(C("OrderId").EQ(int64(12)).And(C("UserId").EQ(123)))
  4698  				return s
  4699  			}(),
  4700  			qs: []sharding.Query{
  4701  				{
  4702  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`order_id`=?) AND (`user_id`=?);",
  4703  					Args:       []any{int64(12), 123},
  4704  					DB:         "order_db_1",
  4705  					Datasource: "0.db.cluster.company.com:3306",
  4706  				},
  4707  			},
  4708  		},
  4709  		{
  4710  			name: "where and right",
  4711  			builder: func() sharding.QueryBuilder {
  4712  				s := NewShardingSelector[Order](shardingDB).
  4713  					Select(C("OrderId"), C("Content")).
  4714  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))))
  4715  				return s
  4716  			}(),
  4717  			qs: []sharding.Query{
  4718  				{
  4719  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) AND (`order_id`=?);",
  4720  					Args:       []any{123, int64(12)},
  4721  					DB:         "order_db_1",
  4722  					Datasource: "0.db.cluster.company.com:3306",
  4723  				},
  4724  			},
  4725  		},
  4726  		{
  4727  			name: "where or",
  4728  			builder: func() sharding.QueryBuilder {
  4729  				s := NewShardingSelector[Order](shardingDB).
  4730  					Select(C("OrderId"), C("Content")).
  4731  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)))
  4732  				return s
  4733  			}(),
  4734  			qs: []sharding.Query{
  4735  				{
  4736  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) OR (`user_id`=?);",
  4737  					Args:       []any{123, 234},
  4738  					DB:         "order_db_1",
  4739  					Datasource: "0.db.cluster.company.com:3306",
  4740  				},
  4741  				{
  4742  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id`=?) OR (`user_id`=?);",
  4743  					Args:       []any{123, 234},
  4744  					DB:         "order_db_0",
  4745  					Datasource: "0.db.cluster.company.com:3306",
  4746  				},
  4747  			},
  4748  		},
  4749  		{
  4750  			name: "where or left broadcast",
  4751  			builder: func() sharding.QueryBuilder {
  4752  				s := NewShardingSelector[Order](shardingDB).
  4753  					Select(C("OrderId"), C("Content")).
  4754  					Where(C("OrderId").EQ(int64(12)).Or(C("UserId").EQ(123)))
  4755  				return s
  4756  			}(),
  4757  			qs: []sharding.Query{
  4758  				{
  4759  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`order_id`=?) OR (`user_id`=?);",
  4760  					Args:       []any{int64(12), 123},
  4761  					DB:         "order_db_0",
  4762  					Datasource: "0.db.cluster.company.com:3306",
  4763  				},
  4764  				{
  4765  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE (`order_id`=?) OR (`user_id`=?);",
  4766  					Args:       []any{int64(12), 123},
  4767  					DB:         "order_db_0",
  4768  					Datasource: "0.db.cluster.company.com:3306",
  4769  				},
  4770  				{
  4771  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE (`order_id`=?) OR (`user_id`=?);",
  4772  					Args:       []any{int64(12), 123},
  4773  					DB:         "order_db_0",
  4774  					Datasource: "0.db.cluster.company.com:3306",
  4775  				},
  4776  				{
  4777  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`order_id`=?) OR (`user_id`=?);",
  4778  					Args:       []any{int64(12), 123},
  4779  					DB:         "order_db_1",
  4780  					Datasource: "0.db.cluster.company.com:3306",
  4781  				},
  4782  				{
  4783  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE (`order_id`=?) OR (`user_id`=?);",
  4784  					Args:       []any{int64(12), 123},
  4785  					DB:         "order_db_1",
  4786  					Datasource: "0.db.cluster.company.com:3306",
  4787  				},
  4788  				{
  4789  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`order_id`=?) OR (`user_id`=?);",
  4790  					Args:       []any{int64(12), 123},
  4791  					DB:         "order_db_1",
  4792  					Datasource: "0.db.cluster.company.com:3306",
  4793  				},
  4794  			},
  4795  		},
  4796  		{
  4797  			name: "where or right broadcast",
  4798  			builder: func() sharding.QueryBuilder {
  4799  				s := NewShardingSelector[Order](shardingDB).
  4800  					Select(C("OrderId"), C("Content")).
  4801  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))))
  4802  				return s
  4803  			}(),
  4804  			qs: []sharding.Query{
  4805  				{
  4806  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id`=?) OR (`order_id`=?);",
  4807  					Args:       []any{123, int64(12)},
  4808  					DB:         "order_db_0",
  4809  					Datasource: "0.db.cluster.company.com:3306",
  4810  				},
  4811  				{
  4812  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE (`user_id`=?) OR (`order_id`=?);",
  4813  					Args:       []any{123, int64(12)},
  4814  					DB:         "order_db_0",
  4815  					Datasource: "0.db.cluster.company.com:3306",
  4816  				},
  4817  				{
  4818  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE (`user_id`=?) OR (`order_id`=?);",
  4819  					Args:       []any{123, int64(12)},
  4820  					DB:         "order_db_0",
  4821  					Datasource: "0.db.cluster.company.com:3306",
  4822  				},
  4823  				{
  4824  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) OR (`order_id`=?);",
  4825  					Args:       []any{123, int64(12)},
  4826  					DB:         "order_db_1",
  4827  					Datasource: "0.db.cluster.company.com:3306",
  4828  				},
  4829  				{
  4830  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE (`user_id`=?) OR (`order_id`=?);",
  4831  					Args:       []any{123, int64(12)},
  4832  					DB:         "order_db_1",
  4833  					Datasource: "0.db.cluster.company.com:3306",
  4834  				},
  4835  				{
  4836  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`user_id`=?) OR (`order_id`=?);",
  4837  					Args:       []any{123, int64(12)},
  4838  					DB:         "order_db_1",
  4839  					Datasource: "0.db.cluster.company.com:3306",
  4840  				},
  4841  			},
  4842  		},
  4843  		{
  4844  			name: "where and-or all",
  4845  			builder: func() sharding.QueryBuilder {
  4846  				s := NewShardingSelector[Order](shardingDB).
  4847  					Select(C("OrderId"), C("Content")).
  4848  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).Or(C("UserId").EQ(234)))
  4849  				return s
  4850  			}(),
  4851  			qs: []sharding.Query{
  4852  				{
  4853  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
  4854  					Args:       []any{123, int64(12), 234},
  4855  					DB:         "order_db_1",
  4856  					Datasource: "0.db.cluster.company.com:3306",
  4857  				},
  4858  				{
  4859  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR (`user_id`=?);",
  4860  					Args:       []any{123, int64(12), 234},
  4861  					DB:         "order_db_0",
  4862  					Datasource: "0.db.cluster.company.com:3306",
  4863  				},
  4864  			},
  4865  		},
  4866  		{
  4867  			name: "where and-or",
  4868  			builder: func() sharding.QueryBuilder {
  4869  				s := NewShardingSelector[Order](shardingDB).
  4870  					Select(C("OrderId"), C("Content")).
  4871  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12)).
  4872  						Or(C("UserId").EQ(234))))
  4873  				return s
  4874  			}(),
  4875  			qs: []sharding.Query{
  4876  				{
  4877  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) AND ((`order_id`=?) OR (`user_id`=?));",
  4878  					Args:       []any{123, int64(12), 234},
  4879  					DB:         "order_db_1",
  4880  					Datasource: "0.db.cluster.company.com:3306",
  4881  				},
  4882  			},
  4883  		},
  4884  		{
  4885  			name: "where or-and all",
  4886  			builder: func() sharding.QueryBuilder {
  4887  				s := NewShardingSelector[Order](shardingDB).
  4888  					Select(C("OrderId"), C("Content")).
  4889  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(181).And(C("UserId").EQ(234))))
  4890  				return s
  4891  			}(),
  4892  			qs: []sharding.Query{
  4893  				{
  4894  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id`=?) OR ((`user_id`=?) AND (`user_id`=?));",
  4895  					Args:       []any{123, 181, 234},
  4896  					DB:         "order_db_1",
  4897  					Datasource: "0.db.cluster.company.com:3306",
  4898  				},
  4899  			},
  4900  		},
  4901  		{
  4902  			name: "where or-and",
  4903  			builder: func() sharding.QueryBuilder {
  4904  				s := NewShardingSelector[Order](shardingDB).
  4905  					Select(C("OrderId"), C("Content")).
  4906  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(234)).
  4907  						And(C("OrderId").EQ(int64(24))))
  4908  				return s
  4909  			}(),
  4910  			qs: []sharding.Query{
  4911  				{
  4912  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
  4913  					Args:       []any{123, 234, int64(24)},
  4914  					DB:         "order_db_1",
  4915  					Datasource: "0.db.cluster.company.com:3306",
  4916  				},
  4917  				{
  4918  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) AND (`order_id`=?);",
  4919  					Args:       []any{123, 234, int64(24)},
  4920  					DB:         "order_db_0",
  4921  					Datasource: "0.db.cluster.company.com:3306",
  4922  				},
  4923  			},
  4924  		},
  4925  		{
  4926  			name: "where or-and broadcast",
  4927  			builder: func() sharding.QueryBuilder {
  4928  				s := NewShardingSelector[Order](shardingDB).
  4929  					Select(C("OrderId"), C("Content")).
  4930  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
  4931  						And(C("UserId").EQ(234)))
  4932  				return s
  4933  			}(),
  4934  			qs: []sharding.Query{
  4935  				{
  4936  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) AND (`user_id`=?);",
  4937  					Args:       []any{123, int64(12), 234},
  4938  					DB:         "order_db_0",
  4939  					Datasource: "0.db.cluster.company.com:3306",
  4940  				},
  4941  			},
  4942  		},
  4943  		{
  4944  			name: "where or-or",
  4945  			builder: func() sharding.QueryBuilder {
  4946  				s := NewShardingSelector[Order](shardingDB).
  4947  					Select(C("OrderId"), C("Content")).
  4948  					Where(C("UserId").EQ(123).Or(C("UserId").EQ(253)).
  4949  						Or(C("UserId").EQ(234)))
  4950  				return s
  4951  			}(),
  4952  			qs: []sharding.Query{
  4953  				{
  4954  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
  4955  					Args:       []any{123, 253, 234},
  4956  					DB:         "order_db_1",
  4957  					Datasource: "0.db.cluster.company.com:3306",
  4958  				},
  4959  				{
  4960  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
  4961  					Args:       []any{123, 253, 234},
  4962  					DB:         "order_db_1",
  4963  					Datasource: "0.db.cluster.company.com:3306",
  4964  				},
  4965  				{
  4966  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) OR (`user_id`=?)) OR (`user_id`=?);",
  4967  					Args:       []any{123, 253, 234},
  4968  					DB:         "order_db_0",
  4969  					Datasource: "0.db.cluster.company.com:3306",
  4970  				},
  4971  			},
  4972  		},
  4973  		{
  4974  			name: "where or-or broadcast",
  4975  			builder: func() sharding.QueryBuilder {
  4976  				s := NewShardingSelector[Order](shardingDB).
  4977  					Select(C("OrderId"), C("Content")).
  4978  					Where(C("UserId").EQ(123).Or(C("OrderId").EQ(int64(12))).
  4979  						Or(C("UserId").EQ(234)))
  4980  				return s
  4981  			}(),
  4982  			qs: []sharding.Query{
  4983  				{
  4984  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  4985  					Args:       []any{123, int64(12), 234},
  4986  					DB:         "order_db_0",
  4987  					Datasource: "0.db.cluster.company.com:3306",
  4988  				},
  4989  				{
  4990  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_1` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  4991  					Args:       []any{123, int64(12), 234},
  4992  					DB:         "order_db_0",
  4993  					Datasource: "0.db.cluster.company.com:3306",
  4994  				},
  4995  				{
  4996  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_2` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  4997  					Args:       []any{123, int64(12), 234},
  4998  					DB:         "order_db_0",
  4999  					Datasource: "0.db.cluster.company.com:3306",
  5000  				},
  5001  				{
  5002  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  5003  					Args:       []any{123, int64(12), 234},
  5004  					DB:         "order_db_1",
  5005  					Datasource: "0.db.cluster.company.com:3306",
  5006  				},
  5007  				{
  5008  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_1` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  5009  					Args:       []any{123, int64(12), 234},
  5010  					DB:         "order_db_1",
  5011  					Datasource: "0.db.cluster.company.com:3306",
  5012  				},
  5013  				{
  5014  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE ((`user_id`=?) OR (`order_id`=?)) OR (`user_id`=?);",
  5015  					Args:       []any{123, int64(12), 234},
  5016  					DB:         "order_db_1",
  5017  					Datasource: "0.db.cluster.company.com:3306",
  5018  				},
  5019  			},
  5020  		},
  5021  		{
  5022  			name: "where and-and",
  5023  			builder: func() sharding.QueryBuilder {
  5024  				s := NewShardingSelector[Order](shardingDB).
  5025  					Select(C("OrderId"), C("Content")).
  5026  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
  5027  						And(C("OrderId").EQ(int64(23))))
  5028  				return s
  5029  			}(),
  5030  			qs: []sharding.Query{
  5031  				{
  5032  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) AND (`order_id`=?);",
  5033  					Args:       []any{123, int64(12), int64(23)},
  5034  					DB:         "order_db_1",
  5035  					Datasource: "0.db.cluster.company.com:3306",
  5036  				},
  5037  			},
  5038  		},
  5039  		{
  5040  			name: "where and-or-and",
  5041  			builder: func() sharding.QueryBuilder {
  5042  				s := NewShardingSelector[Order](shardingDB).
  5043  					Select(C("OrderId"), C("Content")).
  5044  					Where(C("UserId").EQ(123).And(C("OrderId").EQ(int64(12))).
  5045  						Or(C("UserId").EQ(234).And(C("OrderId").EQ(int64(18)))))
  5046  				return s
  5047  			}(),
  5048  			qs: []sharding.Query{
  5049  				{
  5050  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
  5051  					Args:       []any{123, int64(12), 234, int64(18)},
  5052  					DB:         "order_db_1",
  5053  					Datasource: "0.db.cluster.company.com:3306",
  5054  				},
  5055  				{
  5056  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE ((`user_id`=?) AND (`order_id`=?)) OR ((`user_id`=?) AND (`order_id`=?));",
  5057  					Args:       []any{123, int64(12), 234, int64(18)},
  5058  					DB:         "order_db_0",
  5059  					Datasource: "0.db.cluster.company.com:3306",
  5060  				},
  5061  			},
  5062  		},
  5063  		{
  5064  			name: "where lt",
  5065  			builder: func() sharding.QueryBuilder {
  5066  				s := NewShardingSelector[Order](shardingDB).
  5067  					Select(C("OrderId"), C("Content")).Where(C("UserId").LT(1))
  5068  				return s
  5069  			}(),
  5070  			qs: func() []sharding.Query {
  5071  				var res []sharding.Query
  5072  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`<?;"
  5073  				for i := 0; i < dbBase; i++ {
  5074  					dbName := fmt.Sprintf(dbPattern, i)
  5075  					for j := 0; j < tableBase; j++ {
  5076  						tableName := fmt.Sprintf(tablePattern, j)
  5077  						res = append(res, sharding.Query{
  5078  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5079  							Args:       []any{1},
  5080  							DB:         dbName,
  5081  							Datasource: dsPattern,
  5082  						})
  5083  					}
  5084  				}
  5085  				return res
  5086  			}(),
  5087  		},
  5088  		{
  5089  			name: "where lt eq",
  5090  			builder: func() sharding.QueryBuilder {
  5091  				s := NewShardingSelector[Order](shardingDB).
  5092  					Select(C("OrderId"), C("Content")).Where(C("UserId").LTEQ(1))
  5093  				return s
  5094  			}(),
  5095  			qs: func() []sharding.Query {
  5096  				var res []sharding.Query
  5097  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`<=?;"
  5098  				for i := 0; i < dbBase; i++ {
  5099  					dbName := fmt.Sprintf(dbPattern, i)
  5100  					for j := 0; j < tableBase; j++ {
  5101  						tableName := fmt.Sprintf(tablePattern, j)
  5102  						res = append(res, sharding.Query{
  5103  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5104  							Args:       []any{1},
  5105  							DB:         dbName,
  5106  							Datasource: dsPattern,
  5107  						})
  5108  					}
  5109  				}
  5110  				return res
  5111  			}(),
  5112  		},
  5113  		{
  5114  			name: "where gt",
  5115  			builder: func() sharding.QueryBuilder {
  5116  				s := NewShardingSelector[Order](shardingDB).
  5117  					Select(C("OrderId"), C("Content")).Where(C("UserId").GT(1))
  5118  				return s
  5119  			}(),
  5120  			qs: func() []sharding.Query {
  5121  				var res []sharding.Query
  5122  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`>?;"
  5123  				for i := 0; i < dbBase; i++ {
  5124  					dbName := fmt.Sprintf(dbPattern, i)
  5125  					for j := 0; j < tableBase; j++ {
  5126  						tableName := fmt.Sprintf(tablePattern, j)
  5127  						res = append(res, sharding.Query{
  5128  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5129  							Args:       []any{1},
  5130  							DB:         dbName,
  5131  							Datasource: dsPattern,
  5132  						})
  5133  					}
  5134  				}
  5135  				return res
  5136  			}(),
  5137  		},
  5138  		{
  5139  			name: "where gt eq",
  5140  			builder: func() sharding.QueryBuilder {
  5141  				s := NewShardingSelector[Order](shardingDB).
  5142  					Select(C("OrderId"), C("Content")).Where(C("UserId").GTEQ(1))
  5143  				return s
  5144  			}(),
  5145  			qs: func() []sharding.Query {
  5146  				var res []sharding.Query
  5147  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id`>=?;"
  5148  				for i := 0; i < dbBase; i++ {
  5149  					dbName := fmt.Sprintf(dbPattern, i)
  5150  					for j := 0; j < tableBase; j++ {
  5151  						tableName := fmt.Sprintf(tablePattern, j)
  5152  						res = append(res, sharding.Query{
  5153  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5154  							Args:       []any{1},
  5155  							DB:         dbName,
  5156  							Datasource: dsPattern,
  5157  						})
  5158  					}
  5159  				}
  5160  				return res
  5161  			}(),
  5162  		},
  5163  		{
  5164  			name: "where eq and lt or gt",
  5165  			builder: func() sharding.QueryBuilder {
  5166  				s := NewShardingSelector[Order](shardingDB).
  5167  					Select(C("OrderId"), C("Content")).
  5168  					Where(C("UserId").EQ(12).
  5169  						And(C("UserId").LT(133)).Or(C("UserId").GT(234)))
  5170  				return s
  5171  			}(),
  5172  			qs: func() []sharding.Query {
  5173  				var res []sharding.Query
  5174  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE ((`user_id`=?) AND (`user_id`<?)) OR (`user_id`>?);"
  5175  				for i := 0; i < dbBase; i++ {
  5176  					dbName := fmt.Sprintf(dbPattern, i)
  5177  					for j := 0; j < tableBase; j++ {
  5178  						tableName := fmt.Sprintf(tablePattern, j)
  5179  						res = append(res, sharding.Query{
  5180  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5181  							Args:       []any{12, 133, 234},
  5182  							DB:         dbName,
  5183  							Datasource: dsPattern,
  5184  						})
  5185  					}
  5186  				}
  5187  				return res
  5188  			}(),
  5189  		},
  5190  		{
  5191  			name: "where in",
  5192  			builder: func() sharding.QueryBuilder {
  5193  				s := NewShardingSelector[Order](shardingDB).
  5194  					Select(C("OrderId"), C("Content")).
  5195  					Where(C("UserId").In(12, 35, 101))
  5196  				return s
  5197  			}(),
  5198  			qs: []sharding.Query{
  5199  				{
  5200  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE `user_id` IN (?,?,?);",
  5201  					Args:       []any{12, 35, 101},
  5202  					DB:         "order_db_0",
  5203  					Datasource: "0.db.cluster.company.com:3306",
  5204  				},
  5205  				{
  5206  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE `user_id` IN (?,?,?);",
  5207  					Args:       []any{12, 35, 101},
  5208  					DB:         "order_db_1",
  5209  					Datasource: "0.db.cluster.company.com:3306",
  5210  				},
  5211  			},
  5212  		},
  5213  		{
  5214  			name: "where in and eq",
  5215  			builder: func() sharding.QueryBuilder {
  5216  				s := NewShardingSelector[Order](shardingDB).
  5217  					Select(C("OrderId"), C("Content")).
  5218  					Where(C("UserId").In(12, 35, 101).And(C("UserId").EQ(234)))
  5219  				return s
  5220  			}(),
  5221  			qs: []sharding.Query{
  5222  				{
  5223  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id` IN (?,?,?)) AND (`user_id`=?);",
  5224  					Args:       []any{12, 35, 101, 234},
  5225  					DB:         "order_db_0",
  5226  					Datasource: "0.db.cluster.company.com:3306",
  5227  				},
  5228  			},
  5229  		},
  5230  		{
  5231  			name: "where in or eq",
  5232  			builder: func() sharding.QueryBuilder {
  5233  				s := NewShardingSelector[Order](shardingDB).
  5234  					Select(C("OrderId"), C("Content")).
  5235  					Where(C("UserId").In(12, 35, 101).Or(C("UserId").EQ(531)))
  5236  				return s
  5237  			}(),
  5238  			qs: []sharding.Query{
  5239  				{
  5240  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id` IN (?,?,?)) OR (`user_id`=?);",
  5241  					Args:       []any{12, 35, 101, 531},
  5242  					DB:         "order_db_0",
  5243  					Datasource: "0.db.cluster.company.com:3306",
  5244  				},
  5245  				{
  5246  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_0` WHERE (`user_id` IN (?,?,?)) OR (`user_id`=?);",
  5247  					Args:       []any{12, 35, 101, 531},
  5248  					DB:         "order_db_1",
  5249  					Datasource: "0.db.cluster.company.com:3306",
  5250  				},
  5251  				{
  5252  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE (`user_id` IN (?,?,?)) OR (`user_id`=?);",
  5253  					Args:       []any{12, 35, 101, 531},
  5254  					DB:         "order_db_1",
  5255  					Datasource: "0.db.cluster.company.com:3306",
  5256  				},
  5257  			},
  5258  		},
  5259  		{
  5260  			name: "where in or gt",
  5261  			builder: func() sharding.QueryBuilder {
  5262  				s := NewShardingSelector[Order](shardingDB).
  5263  					Select(C("OrderId"), C("Content")).
  5264  					Where(C("UserId").In(12, 35, 101).Or(C("UserId").GT(531)))
  5265  				return s
  5266  			}(),
  5267  			qs: func() []sharding.Query {
  5268  				var res []sharding.Query
  5269  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` IN (?,?,?)) OR (`user_id`>?);"
  5270  				for i := 0; i < dbBase; i++ {
  5271  					dbName := fmt.Sprintf(dbPattern, i)
  5272  					for j := 0; j < tableBase; j++ {
  5273  						tableName := fmt.Sprintf(tablePattern, j)
  5274  						res = append(res, sharding.Query{
  5275  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5276  							Args:       []any{12, 35, 101, 531},
  5277  							DB:         dbName,
  5278  							Datasource: dsPattern,
  5279  						})
  5280  					}
  5281  				}
  5282  				return res
  5283  			}(),
  5284  		},
  5285  		{
  5286  			name: "where not in",
  5287  			builder: func() sharding.QueryBuilder {
  5288  				s := NewShardingSelector[Order](shardingDB).
  5289  					Select(C("OrderId"), C("Content")).
  5290  					Where(C("UserId").NotIn(12, 35, 101))
  5291  				return s
  5292  			}(),
  5293  			qs: func() []sharding.Query {
  5294  				var res []sharding.Query
  5295  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE `user_id` NOT IN (?,?,?);"
  5296  				for i := 0; i < dbBase; i++ {
  5297  					dbName := fmt.Sprintf(dbPattern, i)
  5298  					for j := 0; j < tableBase; j++ {
  5299  						tableName := fmt.Sprintf(tablePattern, j)
  5300  						res = append(res, sharding.Query{
  5301  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5302  							Args:       []any{12, 35, 101},
  5303  							DB:         dbName,
  5304  							Datasource: dsPattern,
  5305  						})
  5306  					}
  5307  				}
  5308  				return res
  5309  			}(),
  5310  		},
  5311  		{
  5312  			name: "where not in and eq",
  5313  			builder: func() sharding.QueryBuilder {
  5314  				s := NewShardingSelector[Order](shardingDB).
  5315  					Select(C("OrderId"), C("Content")).
  5316  					Where(C("UserId").NotIn(12, 35, 101).And(C("UserId").EQ(234)))
  5317  				return s
  5318  			}(),
  5319  			qs: []sharding.Query{
  5320  				{
  5321  					SQL:        "SELECT `order_id`,`content` FROM `order_db_0`.`order_tab_0` WHERE (`user_id` NOT IN (?,?,?)) AND (`user_id`=?);",
  5322  					Args:       []any{12, 35, 101, 234},
  5323  					DB:         "order_db_0",
  5324  					Datasource: "0.db.cluster.company.com:3306",
  5325  				},
  5326  			},
  5327  		},
  5328  		{
  5329  			name: "where not in or eq",
  5330  			builder: func() sharding.QueryBuilder {
  5331  				s := NewShardingSelector[Order](shardingDB).
  5332  					Select(C("OrderId"), C("Content")).
  5333  					Where(C("UserId").NotIn(12, 35, 101).Or(C("UserId").EQ(531)))
  5334  				return s
  5335  			}(),
  5336  			qs: func() []sharding.Query {
  5337  				var res []sharding.Query
  5338  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` NOT IN (?,?,?)) OR (`user_id`=?);"
  5339  				for i := 0; i < dbBase; i++ {
  5340  					dbName := fmt.Sprintf(dbPattern, i)
  5341  					for j := 0; j < tableBase; j++ {
  5342  						tableName := fmt.Sprintf(tablePattern, j)
  5343  						res = append(res, sharding.Query{
  5344  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5345  							Args:       []any{12, 35, 101, 531},
  5346  							DB:         dbName,
  5347  							Datasource: dsPattern,
  5348  						})
  5349  					}
  5350  				}
  5351  				return res
  5352  			}(),
  5353  		},
  5354  		{
  5355  			name: "where not in or gt",
  5356  			builder: func() sharding.QueryBuilder {
  5357  				s := NewShardingSelector[Order](shardingDB).
  5358  					Select(C("OrderId"), C("Content")).
  5359  					Where(C("UserId").NotIn(12, 35, 101).Or(C("UserId").GT(531)))
  5360  				return s
  5361  			}(),
  5362  			qs: func() []sharding.Query {
  5363  				var res []sharding.Query
  5364  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id` NOT IN (?,?,?)) OR (`user_id`>?);"
  5365  				for i := 0; i < dbBase; i++ {
  5366  					dbName := fmt.Sprintf(dbPattern, i)
  5367  					for j := 0; j < tableBase; j++ {
  5368  						tableName := fmt.Sprintf(tablePattern, j)
  5369  						res = append(res, sharding.Query{
  5370  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5371  							Args:       []any{12, 35, 101, 531},
  5372  							DB:         dbName,
  5373  							Datasource: dsPattern,
  5374  						})
  5375  					}
  5376  				}
  5377  				return res
  5378  			}(),
  5379  		},
  5380  		{
  5381  			name: "where not gt",
  5382  			builder: func() sharding.QueryBuilder {
  5383  				s := NewShardingSelector[Order](shardingDB).
  5384  					Select(C("OrderId"), C("Content")).
  5385  					Where(Not(C("UserId").GT(101)))
  5386  				return s
  5387  			}(),
  5388  			qs: func() []sharding.Query {
  5389  				var res []sharding.Query
  5390  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`>?);"
  5391  				for i := 0; i < dbBase; i++ {
  5392  					dbName := fmt.Sprintf(dbPattern, i)
  5393  					for j := 0; j < tableBase; j++ {
  5394  						tableName := fmt.Sprintf(tablePattern, j)
  5395  						res = append(res, sharding.Query{
  5396  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5397  							Args:       []any{101},
  5398  							DB:         dbName,
  5399  							Datasource: dsPattern,
  5400  						})
  5401  					}
  5402  				}
  5403  				return res
  5404  			}(),
  5405  		},
  5406  		{
  5407  			name: "where not lt",
  5408  			builder: func() sharding.QueryBuilder {
  5409  				s := NewShardingSelector[Order](shardingDB).
  5410  					Select(C("OrderId"), C("Content")).
  5411  					Where(Not(C("UserId").LT(101)))
  5412  				return s
  5413  			}(),
  5414  			qs: func() []sharding.Query {
  5415  				var res []sharding.Query
  5416  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`<?);"
  5417  				for i := 0; i < dbBase; i++ {
  5418  					dbName := fmt.Sprintf(dbPattern, i)
  5419  					for j := 0; j < tableBase; j++ {
  5420  						tableName := fmt.Sprintf(tablePattern, j)
  5421  						res = append(res, sharding.Query{
  5422  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5423  							Args:       []any{101},
  5424  							DB:         dbName,
  5425  							Datasource: dsPattern,
  5426  						})
  5427  					}
  5428  				}
  5429  				return res
  5430  			}(),
  5431  		},
  5432  		{
  5433  			name: "where not gt eq",
  5434  			builder: func() sharding.QueryBuilder {
  5435  				s := NewShardingSelector[Order](shardingDB).
  5436  					Select(C("OrderId"), C("Content")).
  5437  					Where(Not(C("UserId").GTEQ(101)))
  5438  				return s
  5439  			}(),
  5440  			qs: func() []sharding.Query {
  5441  				var res []sharding.Query
  5442  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`>=?);"
  5443  				for i := 0; i < dbBase; i++ {
  5444  					dbName := fmt.Sprintf(dbPattern, i)
  5445  					for j := 0; j < tableBase; j++ {
  5446  						tableName := fmt.Sprintf(tablePattern, j)
  5447  						res = append(res, sharding.Query{
  5448  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5449  							Args:       []any{101},
  5450  							DB:         dbName,
  5451  							Datasource: dsPattern,
  5452  						})
  5453  					}
  5454  				}
  5455  				return res
  5456  			}(),
  5457  		},
  5458  		{
  5459  			name: "where not lt eq",
  5460  			builder: func() sharding.QueryBuilder {
  5461  				s := NewShardingSelector[Order](shardingDB).
  5462  					Select(C("OrderId"), C("Content")).
  5463  					Where(Not(C("UserId").LTEQ(101)))
  5464  				return s
  5465  			}(),
  5466  			qs: func() []sharding.Query {
  5467  				var res []sharding.Query
  5468  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`<=?);"
  5469  				for i := 0; i < dbBase; i++ {
  5470  					dbName := fmt.Sprintf(dbPattern, i)
  5471  					for j := 0; j < tableBase; j++ {
  5472  						tableName := fmt.Sprintf(tablePattern, j)
  5473  						res = append(res, sharding.Query{
  5474  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5475  							Args:       []any{101},
  5476  							DB:         dbName,
  5477  							Datasource: dsPattern,
  5478  						})
  5479  					}
  5480  				}
  5481  				return res
  5482  			}(),
  5483  		},
  5484  		{
  5485  			name: "where not eq",
  5486  			builder: func() sharding.QueryBuilder {
  5487  				s := NewShardingSelector[Order](shardingDB).
  5488  					Select(C("OrderId"), C("Content")).
  5489  					Where(Not(C("UserId").EQ(101)))
  5490  				return s
  5491  			}(),
  5492  			qs: func() []sharding.Query {
  5493  				var res []sharding.Query
  5494  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT (`user_id`=?);"
  5495  				for i := 0; i < dbBase; i++ {
  5496  					dbName := fmt.Sprintf(dbPattern, i)
  5497  					for j := 0; j < tableBase; j++ {
  5498  						tableName := fmt.Sprintf(tablePattern, j)
  5499  						res = append(res, sharding.Query{
  5500  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5501  							Args:       []any{101},
  5502  							DB:         dbName,
  5503  							Datasource: dsPattern,
  5504  						})
  5505  					}
  5506  				}
  5507  				return res
  5508  			}(),
  5509  		},
  5510  		{
  5511  			name: "where not neq",
  5512  			builder: func() sharding.QueryBuilder {
  5513  				s := NewShardingSelector[Order](shardingDB).
  5514  					Select(C("OrderId"), C("Content")).
  5515  					Where(Not(C("UserId").NEQ(101)))
  5516  				return s
  5517  			}(),
  5518  			qs: []sharding.Query{
  5519  				{
  5520  					SQL:        "SELECT `order_id`,`content` FROM `order_db_1`.`order_tab_2` WHERE NOT (`user_id`!=?);",
  5521  					Args:       []any{101},
  5522  					DB:         "order_db_1",
  5523  					Datasource: "0.db.cluster.company.com:3306",
  5524  				},
  5525  			},
  5526  		},
  5527  		{
  5528  			name: "where not (gt and lt)",
  5529  			builder: func() sharding.QueryBuilder {
  5530  				s := NewShardingSelector[Order](shardingDB).
  5531  					Select(C("OrderId"), C("Content")).
  5532  					Where(Not(C("UserId").GT(12).And(C("UserId").LT(531))))
  5533  				return s
  5534  			}(),
  5535  			qs: func() []sharding.Query {
  5536  				var res []sharding.Query
  5537  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`>?) AND (`user_id`<?));"
  5538  				for i := 0; i < dbBase; i++ {
  5539  					dbName := fmt.Sprintf(dbPattern, i)
  5540  					for j := 0; j < tableBase; j++ {
  5541  						tableName := fmt.Sprintf(tablePattern, j)
  5542  						res = append(res, sharding.Query{
  5543  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5544  							Args:       []any{12, 531},
  5545  							DB:         dbName,
  5546  							Datasource: dsPattern,
  5547  						})
  5548  					}
  5549  				}
  5550  				return res
  5551  			}(),
  5552  		},
  5553  		{
  5554  			name: "where not (gt eq and lt eq)",
  5555  			builder: func() sharding.QueryBuilder {
  5556  				s := NewShardingSelector[Order](shardingDB).
  5557  					Select(C("OrderId"), C("Content")).
  5558  					Where(Not(C("UserId").GTEQ(12).And(C("UserId").LTEQ(531))))
  5559  				return s
  5560  			}(),
  5561  			qs: func() []sharding.Query {
  5562  				var res []sharding.Query
  5563  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`>=?) AND (`user_id`<=?));"
  5564  				for i := 0; i < dbBase; i++ {
  5565  					dbName := fmt.Sprintf(dbPattern, i)
  5566  					for j := 0; j < tableBase; j++ {
  5567  						tableName := fmt.Sprintf(tablePattern, j)
  5568  						res = append(res, sharding.Query{
  5569  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5570  							Args:       []any{12, 531},
  5571  							DB:         dbName,
  5572  							Datasource: dsPattern,
  5573  						})
  5574  					}
  5575  				}
  5576  				return res
  5577  			}(),
  5578  		},
  5579  		{
  5580  			name: "where not (in or gt)",
  5581  			builder: func() sharding.QueryBuilder {
  5582  				s := NewShardingSelector[Order](shardingDB).
  5583  					Select(C("OrderId"), C("Content")).
  5584  					Where(Not(C("UserId").In(12, 35, 101).Or(C("UserId").GT(531))))
  5585  				return s
  5586  			}(),
  5587  			qs: func() []sharding.Query {
  5588  				var res []sharding.Query
  5589  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id` IN (?,?,?)) OR (`user_id`>?));"
  5590  				for i := 0; i < dbBase; i++ {
  5591  					dbName := fmt.Sprintf(dbPattern, i)
  5592  					for j := 0; j < tableBase; j++ {
  5593  						tableName := fmt.Sprintf(tablePattern, j)
  5594  						res = append(res, sharding.Query{
  5595  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5596  							Args:       []any{12, 35, 101, 531},
  5597  							DB:         dbName,
  5598  							Datasource: dsPattern,
  5599  						})
  5600  					}
  5601  				}
  5602  				return res
  5603  			}(),
  5604  		},
  5605  		{
  5606  			name: "where not (in or eq)",
  5607  			builder: func() sharding.QueryBuilder {
  5608  				s := NewShardingSelector[Order](shardingDB).
  5609  					Select(C("OrderId"), C("Content")).
  5610  					Where(Not(C("UserId").In(12, 35, 101).Or(C("UserId").EQ(531))))
  5611  				return s
  5612  			}(),
  5613  			qs: func() []sharding.Query {
  5614  				var res []sharding.Query
  5615  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id` IN (?,?,?)) OR (`user_id`=?));"
  5616  				for i := 0; i < dbBase; i++ {
  5617  					dbName := fmt.Sprintf(dbPattern, i)
  5618  					for j := 0; j < tableBase; j++ {
  5619  						tableName := fmt.Sprintf(tablePattern, j)
  5620  						res = append(res, sharding.Query{
  5621  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5622  							Args:       []any{12, 35, 101, 531},
  5623  							DB:         dbName,
  5624  							Datasource: dsPattern,
  5625  						})
  5626  					}
  5627  				}
  5628  				return res
  5629  			}(),
  5630  		},
  5631  		{
  5632  			name: "where not (eq and eq)",
  5633  			builder: func() sharding.QueryBuilder {
  5634  				s := NewShardingSelector[Order](shardingDB).
  5635  					Select(C("OrderId"), C("Content")).
  5636  					Where(Not(C("UserId").EQ(12).And(C("UserId").EQ(531))))
  5637  				return s
  5638  			}(),
  5639  			qs: func() []sharding.Query {
  5640  				var res []sharding.Query
  5641  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`=?) AND (`user_id`=?));"
  5642  				for i := 0; i < dbBase; i++ {
  5643  					dbName := fmt.Sprintf(dbPattern, i)
  5644  					for j := 0; j < tableBase; j++ {
  5645  						tableName := fmt.Sprintf(tablePattern, j)
  5646  						res = append(res, sharding.Query{
  5647  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5648  							Args:       []any{12, 531},
  5649  							DB:         dbName,
  5650  							Datasource: dsPattern,
  5651  						})
  5652  					}
  5653  				}
  5654  				return res
  5655  			}(),
  5656  		},
  5657  		{
  5658  			name: "where not (eq and eq not sharding key)",
  5659  			builder: func() sharding.QueryBuilder {
  5660  				s := NewShardingSelector[Order](shardingDB).
  5661  					Select(C("OrderId"), C("Content")).
  5662  					Where(Not(C("UserId").EQ(12).And(C("OrderId").EQ(111))))
  5663  				return s
  5664  			}(),
  5665  			qs: func() []sharding.Query {
  5666  				var res []sharding.Query
  5667  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE NOT ((`user_id`=?) AND (`order_id`=?));"
  5668  				for i := 0; i < dbBase; i++ {
  5669  					dbName := fmt.Sprintf(dbPattern, i)
  5670  					for j := 0; j < tableBase; j++ {
  5671  						tableName := fmt.Sprintf(tablePattern, j)
  5672  						res = append(res, sharding.Query{
  5673  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5674  							Args:       []any{12, 111},
  5675  							DB:         dbName,
  5676  							Datasource: dsPattern,
  5677  						})
  5678  					}
  5679  				}
  5680  				return res
  5681  			}(),
  5682  		},
  5683  		{
  5684  			name: "where between",
  5685  			builder: func() sharding.QueryBuilder {
  5686  				s := NewShardingSelector[Order](shardingDB).
  5687  					Select(C("OrderId"), C("Content")).
  5688  					Where(C("UserId").GTEQ(12).And(C("UserId").LTEQ(531)))
  5689  				return s
  5690  			}(),
  5691  			qs: func() []sharding.Query {
  5692  				var res []sharding.Query
  5693  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s` WHERE (`user_id`>=?) AND (`user_id`<=?);"
  5694  				for i := 0; i < dbBase; i++ {
  5695  					dbName := fmt.Sprintf(dbPattern, i)
  5696  					for j := 0; j < tableBase; j++ {
  5697  						tableName := fmt.Sprintf(tablePattern, j)
  5698  						res = append(res, sharding.Query{
  5699  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5700  							Args:       []any{12, 531},
  5701  							DB:         dbName,
  5702  							Datasource: dsPattern,
  5703  						})
  5704  					}
  5705  				}
  5706  				return res
  5707  			}(),
  5708  		},
  5709  		{
  5710  			name: "not where",
  5711  			builder: func() sharding.QueryBuilder {
  5712  				s := NewShardingSelector[Order](shardingDB).
  5713  					Select(C("OrderId"), C("Content"))
  5714  				return s
  5715  			}(),
  5716  			qs: func() []sharding.Query {
  5717  				var res []sharding.Query
  5718  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s`;"
  5719  				for i := 0; i < dbBase; i++ {
  5720  					dbName := fmt.Sprintf(dbPattern, i)
  5721  					for j := 0; j < tableBase; j++ {
  5722  						tableName := fmt.Sprintf(tablePattern, j)
  5723  						res = append(res, sharding.Query{
  5724  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5725  							DB:         dbName,
  5726  							Datasource: dsPattern,
  5727  						})
  5728  					}
  5729  				}
  5730  				return res
  5731  			}(),
  5732  		},
  5733  		{
  5734  			name: "select from",
  5735  			builder: func() sharding.QueryBuilder {
  5736  				s := NewShardingSelector[Order](shardingDB).
  5737  					Select(C("OrderId"), C("Content")).From(&Order{})
  5738  				return s
  5739  			}(),
  5740  			qs: func() []sharding.Query {
  5741  				var res []sharding.Query
  5742  				sql := "SELECT `order_id`,`content` FROM `%s`.`%s`;"
  5743  				for i := 0; i < dbBase; i++ {
  5744  					dbName := fmt.Sprintf(dbPattern, i)
  5745  					for j := 0; j < tableBase; j++ {
  5746  						tableName := fmt.Sprintf(tablePattern, j)
  5747  						res = append(res, sharding.Query{
  5748  							SQL:        fmt.Sprintf(sql, dbName, tableName),
  5749  							DB:         dbName,
  5750  							Datasource: dsPattern,
  5751  						})
  5752  					}
  5753  				}
  5754  				return res
  5755  			}(),
  5756  		},
  5757  		{
  5758  			name: "and empty",
  5759  			builder: func() sharding.QueryBuilder {
  5760  				s := NewShardingSelector[Order](shardingDB).
  5761  					Select(C("OrderId"), C("Content")).
  5762  					Where(C("UserId").EQ(123).And(C("UserId").EQ(124)))
  5763  				return s
  5764  			}(),
  5765  			qs: []sharding.Query{},
  5766  		},
  5767  	}
  5768  
  5769  	for _, tc := range testCases {
  5770  		c := tc
  5771  		t.Run(c.name, func(t *testing.T) {
  5772  			qs, err := c.builder.Build(context.Background())
  5773  			assert.Equal(t, c.wantErr, err)
  5774  			if err != nil {
  5775  				return
  5776  			}
  5777  			assert.ElementsMatch(t, c.qs, qs)
  5778  		})
  5779  	}
  5780  }
  5781  
  5782  func TestShardingSelector_Build_Error(t *testing.T) {
  5783  	r := model.NewMetaRegistry()
  5784  	dbBase, tableBase := 2, 3
  5785  	dbPattern, tablePattern, dsPattern := "order_db_%d", "order_tab_%d", "0.db.cluster.company.com:3306"
  5786  	_, err := r.Register(&Order{},
  5787  		model.WithTableShardingAlgorithm(&hash.Hash{
  5788  			ShardingKey:  "UserId",
  5789  			DBPattern:    &hash.Pattern{Name: dbPattern, Base: dbBase},
  5790  			TablePattern: &hash.Pattern{Name: tablePattern, Base: tableBase},
  5791  			DsPattern:    &hash.Pattern{Name: dsPattern, NotSharding: true},
  5792  		}))
  5793  	require.NoError(t, err)
  5794  
  5795  	m := map[string]*masterslave.MasterSlavesDB{
  5796  		"order_db_0": MasterSlavesMemoryDB(),
  5797  		"order_db_1": MasterSlavesMemoryDB(),
  5798  		"order_db_2": MasterSlavesMemoryDB(),
  5799  	}
  5800  	clusterDB := cluster.NewClusterDB(m)
  5801  	ds := map[string]datasource.DataSource{
  5802  		"0.db.cluster.company.com:3306": clusterDB,
  5803  	}
  5804  	shardingDB, err := OpenDS("sqlite3",
  5805  		shardingsource.NewShardingDataSource(ds), DBWithMetaRegistry(r))
  5806  	require.NoError(t, err)
  5807  
  5808  	testCases := []struct {
  5809  		name    string
  5810  		builder sharding.QueryBuilder
  5811  		qs      []sharding.Query
  5812  		wantErr error
  5813  	}{
  5814  		{
  5815  			name: "not and left too complex operator",
  5816  			builder: func() sharding.QueryBuilder {
  5817  				s := NewShardingSelector[Order](shardingDB).Where(Not(C("Content").
  5818  					Like("%kfc").And(C("OrderId").EQ(101))))
  5819  				return s
  5820  			}(),
  5821  			wantErr: errs.NewUnsupportedOperatorError(opLike.Text),
  5822  		},
  5823  		{
  5824  			name: "not or left too complex operator",
  5825  			builder: func() sharding.QueryBuilder {
  5826  				s := NewShardingSelector[Order](shardingDB).Where(Not(C("Content").
  5827  					Like("%kfc").Or(C("OrderId").EQ(101))))
  5828  				return s
  5829  			}(),
  5830  			wantErr: errs.NewUnsupportedOperatorError(opLike.Text),
  5831  		},
  5832  		{
  5833  			name: "not and right too complex operator",
  5834  			builder: func() sharding.QueryBuilder {
  5835  				s := NewShardingSelector[Order](shardingDB).Where(Not(C("OrderId").
  5836  					EQ(101).And(C("Content").Like("%kfc"))))
  5837  				return s
  5838  			}(),
  5839  			wantErr: errs.NewUnsupportedOperatorError(opLike.Text),
  5840  		},
  5841  		{
  5842  			name: "not or right too complex operator",
  5843  			builder: func() sharding.QueryBuilder {
  5844  				s := NewShardingSelector[Order](shardingDB).Where(Not(C("OrderId").
  5845  					EQ(101).Or(C("Content").Like("%kfc"))))
  5846  				return s
  5847  			}(),
  5848  			wantErr: errs.NewUnsupportedOperatorError(opLike.Text),
  5849  		},
  5850  		{
  5851  			name: "invalid field err",
  5852  			builder: func() sharding.QueryBuilder {
  5853  				s := NewShardingSelector[Order](shardingDB).Select(C("ccc"))
  5854  				return s
  5855  			}(),
  5856  			wantErr: errs.NewInvalidFieldError("ccc"),
  5857  		},
  5858  		{
  5859  			name: "group by invalid field err",
  5860  			builder: func() sharding.QueryBuilder {
  5861  				s := NewShardingSelector[Order](shardingDB).Select(C("UserId")).GroupBy("ccc")
  5862  				return s
  5863  			}(),
  5864  			wantErr: errs.NewInvalidFieldError("ccc"),
  5865  		},
  5866  		{
  5867  			name: "order by invalid field err",
  5868  			builder: func() sharding.QueryBuilder {
  5869  				s := NewShardingSelector[Order](shardingDB).Select(C("UserId")).OrderBy(ASC("ccc"))
  5870  				return s
  5871  			}(),
  5872  			wantErr: errs.NewInvalidFieldError("ccc"),
  5873  		},
  5874  		{
  5875  			name: "pointer only err",
  5876  			builder: func() sharding.QueryBuilder {
  5877  				s := NewShardingSelector[int64](shardingDB)
  5878  				return s
  5879  			}(),
  5880  			wantErr: errs.ErrPointerOnly,
  5881  		},
  5882  		{
  5883  			name: "too complex operator",
  5884  			builder: func() sharding.QueryBuilder {
  5885  				s := NewShardingSelector[Order](shardingDB).Where(C("Content").Like("%kfc"))
  5886  				return s
  5887  			}(),
  5888  			wantErr: errs.NewUnsupportedOperatorError(opLike.Text),
  5889  		},
  5890  		{
  5891  			name: "too complex expr",
  5892  			builder: func() sharding.QueryBuilder {
  5893  				s := NewShardingSelector[Order](shardingDB).Where(Avg("UserId").EQ(1))
  5894  				return s
  5895  			}(),
  5896  			wantErr: errs.ErrUnsupportedTooComplexQuery,
  5897  		},
  5898  		{
  5899  			name: "miss sharding key err",
  5900  			builder: func() sharding.QueryBuilder {
  5901  				reg := model.NewMetaRegistry()
  5902  				meta, err := reg.Register(&Order{},
  5903  					model.WithTableShardingAlgorithm(&hash.Hash{}))
  5904  				require.NoError(t, err)
  5905  				require.NotNil(t, meta.ShardingAlgorithm)
  5906  				db, err := OpenDS("sqlite3",
  5907  					shardingsource.NewShardingDataSource(map[string]datasource.DataSource{
  5908  						"0.db.cluster.company.com:3306": MasterSlavesMemoryDB(),
  5909  					}),
  5910  					DBWithMetaRegistry(reg))
  5911  				require.NoError(t, err)
  5912  				s := NewShardingSelector[Order](db).Where(C("UserId").EQ(123))
  5913  				return s
  5914  			}(),
  5915  			wantErr: errs.ErrMissingShardingKey,
  5916  		},
  5917  	}
  5918  
  5919  	for _, tc := range testCases {
  5920  		c := tc
  5921  		t.Run(c.name, func(t *testing.T) {
  5922  			_, err = c.builder.Build(context.Background())
  5923  			assert.Equal(t, c.wantErr, err)
  5924  		})
  5925  	}
  5926  
  5927  }
  5928  
  5929  func TestShardingSelector_Get(t *testing.T) {
  5930  	r := model.NewMetaRegistry()
  5931  	_, err := r.Register(&test.OrderDetail{},
  5932  		model.WithTableShardingAlgorithm(&hash.Hash{
  5933  			ShardingKey:  "OrderId",
  5934  			DBPattern:    &hash.Pattern{Name: "order_detail_db_%d", Base: 2},
  5935  			TablePattern: &hash.Pattern{Name: "order_detail_tab_%d", Base: 3},
  5936  			DsPattern:    &hash.Pattern{Name: "0.db.slave.company.com:3306", NotSharding: true},
  5937  		}))
  5938  	require.NoError(t, err)
  5939  
  5940  	mockDB, mock, err := sqlmock.New(
  5941  		sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
  5942  	if err != nil {
  5943  		t.Fatal(err)
  5944  	}
  5945  	defer func() { _ = mockDB.Close() }()
  5946  
  5947  	rbSlaves, err := roundrobin.NewSlaves(mockDB)
  5948  	require.NoError(t, err)
  5949  	masterSlaveDB := masterslave.NewMasterSlavesDB(
  5950  		mockDB, masterslave.MasterSlavesWithSlaves(newMockSlaveNameGet(rbSlaves)))
  5951  	require.NoError(t, err)
  5952  
  5953  	m := map[string]datasource.DataSource{
  5954  		"0.db.slave.company.com:3306": masterSlaveDB,
  5955  	}
  5956  	shardingDB, err := OpenDS("mysql",
  5957  		shardingsource.NewShardingDataSource(m), DBWithMetaRegistry(r))
  5958  	require.NoError(t, err)
  5959  
  5960  	testCases := []struct {
  5961  		name      string
  5962  		s         *ShardingSelector[test.OrderDetail]
  5963  		mockOrder func(mock sqlmock.Sqlmock)
  5964  		wantErr   error
  5965  		wantRes   *test.OrderDetail
  5966  	}{
  5967  		{
  5968  			name: "invalid field err",
  5969  			s: func() *ShardingSelector[test.OrderDetail] {
  5970  				b := NewShardingSelector[test.OrderDetail](shardingDB).Select(C("ccc"))
  5971  				return b
  5972  			}(),
  5973  			mockOrder: func(mock sqlmock.Sqlmock) {},
  5974  			wantErr:   errs.NewInvalidFieldError("ccc"),
  5975  		},
  5976  		{
  5977  			name: "not gen sharding query",
  5978  			s: func() *ShardingSelector[test.OrderDetail] {
  5979  				b := NewShardingSelector[test.OrderDetail](shardingDB).
  5980  					Where(C("OrderId").EQ(12).And(C("OrderId").EQ(14)))
  5981  				return b
  5982  			}(),
  5983  			mockOrder: func(mock sqlmock.Sqlmock) {},
  5984  			wantErr:   errs.ErrNotGenShardingQuery,
  5985  		},
  5986  		{
  5987  			name: "no rows err",
  5988  			s: func() *ShardingSelector[test.OrderDetail] {
  5989  				b := NewShardingSelector[test.OrderDetail](shardingDB).Select(C("UsingCol1")).
  5990  					Where(C("OrderId").EQ(123))
  5991  				return b
  5992  			}(),
  5993  			mockOrder: func(mock sqlmock.Sqlmock) {
  5994  				rows := mock.NewRows([]string{"order_id", "item_id", "using_col1", "using_col2"})
  5995  				mock.ExpectQuery("SELECT `using_col1` FROM `order_detail_db_1`.`order_detail_tab_0` WHERE `order_id`=? LIMIT ?;").
  5996  					WithArgs(123, 1).WillReturnRows(rows)
  5997  			},
  5998  			wantErr: ErrNoRows,
  5999  		},
  6000  		{
  6001  			name: "query err",
  6002  			s: func() *ShardingSelector[test.OrderDetail] {
  6003  				b := NewShardingSelector[test.OrderDetail](shardingDB).Select(C("UsingCol1")).
  6004  					Where(C("OrderId").EQ(123))
  6005  				return b
  6006  			}(),
  6007  			mockOrder: func(mock sqlmock.Sqlmock) {
  6008  				mock.ExpectQuery("SELECT `using_col1` FROM `order_detail_db_1`.`order_detail_tab_0` WHERE `order_id`=? LIMIT ?;").
  6009  					WithArgs(123, 1).WillReturnError(errors.New("query exception"))
  6010  			},
  6011  			wantErr: errors.New("query exception"),
  6012  		},
  6013  		{
  6014  			name: "multi row err",
  6015  			s: func() *ShardingSelector[test.OrderDetail] {
  6016  				b := NewShardingSelector[test.OrderDetail](shardingDB).
  6017  					Where(C("OrderId").EQ(123))
  6018  				return b
  6019  			}(),
  6020  			mockOrder: func(mock sqlmock.Sqlmock) {
  6021  				rows := mock.NewRows([]string{"order_id", "item_id", "using_col1", "using_col2", "using_col3"}).
  6022  					AddRow(123, 10, "LeBron", "James", "de")
  6023  				mock.ExpectQuery("SELECT `order_id`,`item_id`,`using_col1`,`using_col2` FROM `order_detail_db_1`.`order_detail_tab_0` WHERE `order_id`=? LIMIT ?;").
  6024  					WithArgs(123, 1).WillReturnRows(rows)
  6025  			},
  6026  			wantErr: errs.ErrTooManyColumns,
  6027  		},
  6028  		{
  6029  			name: "only result one query",
  6030  			s: func() *ShardingSelector[test.OrderDetail] {
  6031  				b := NewShardingSelector[test.OrderDetail](shardingDB).
  6032  					Where(C("OrderId").EQ(123).Or(C("ItemId").EQ(12)))
  6033  				return b
  6034  			}(),
  6035  			mockOrder: func(mock sqlmock.Sqlmock) {},
  6036  			wantErr:   errs.ErrOnlyResultOneQuery,
  6037  		},
  6038  		{
  6039  			name: "found tab 1",
  6040  			s: func() *ShardingSelector[test.OrderDetail] {
  6041  				b := NewShardingSelector[test.OrderDetail](shardingDB).
  6042  					Where(C("OrderId").EQ(123))
  6043  				return b
  6044  			}(),
  6045  			mockOrder: func(mock sqlmock.Sqlmock) {
  6046  				rows := mock.NewRows([]string{"order_id", "item_id", "using_col1", "using_col2"}).
  6047  					AddRow(123, 10, "LeBron", "James")
  6048  				mock.ExpectQuery("SELECT `order_id`,`item_id`,`using_col1`,`using_col2` FROM `order_detail_db_1`.`order_detail_tab_0` WHERE `order_id`=? LIMIT ?;").
  6049  					WithArgs(123, 1).
  6050  					WillReturnRows(rows)
  6051  			},
  6052  			wantRes: &test.OrderDetail{OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"},
  6053  		},
  6054  		{
  6055  			name: "found tab 2",
  6056  			s: func() *ShardingSelector[test.OrderDetail] {
  6057  				b := NewShardingSelector[test.OrderDetail](shardingDB).
  6058  					Where(C("OrderId").EQ(234))
  6059  				return b
  6060  			}(),
  6061  			mockOrder: func(mock sqlmock.Sqlmock) {
  6062  				rows := mock.NewRows([]string{"order_id", "item_id", "using_col1", "using_col2"}).
  6063  					AddRow(234, 12, "Kevin", "Durant")
  6064  				mock.ExpectQuery("SELECT `order_id`,`item_id`,`using_col1`,`using_col2` FROM `order_detail_db_0`.`order_detail_tab_0` WHERE `order_id`=? LIMIT ?;").
  6065  					WithArgs(234, 1).
  6066  					WillReturnRows(rows)
  6067  			},
  6068  			wantRes: &test.OrderDetail{OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"},
  6069  		},
  6070  		{
  6071  			name: "found tab and",
  6072  			s: func() *ShardingSelector[test.OrderDetail] {
  6073  				b := NewShardingSelector[test.OrderDetail](shardingDB).
  6074  					Where(C("OrderId").EQ(234).And(C("ItemId").EQ(12)))
  6075  				return b
  6076  			}(),
  6077  			mockOrder: func(mock sqlmock.Sqlmock) {
  6078  				rows := mock.NewRows([]string{"order_id", "item_id", "using_col1", "using_col2"}).
  6079  					AddRow(234, 12, "Kevin", "Durant")
  6080  				mock.ExpectQuery("SELECT `order_id`,`item_id`,`using_col1`,`using_col2` FROM `order_detail_db_0`.`order_detail_tab_0` WHERE (`order_id`=?) AND (`item_id`=?) LIMIT ?;").
  6081  					WithArgs(234, 12, 1).
  6082  					WillReturnRows(rows)
  6083  			},
  6084  			wantRes: &test.OrderDetail{OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"},
  6085  		},
  6086  	}
  6087  
  6088  	for _, tc := range testCases {
  6089  		t.Run(tc.name, func(t *testing.T) {
  6090  			tc.mockOrder(mock)
  6091  			res, err := tc.s.Get(context.Background())
  6092  			assert.Equal(t, tc.wantErr, err)
  6093  			if err != nil {
  6094  				return
  6095  			}
  6096  			assert.Equal(t, tc.wantRes, res)
  6097  		})
  6098  	}
  6099  }
  6100  
  6101  func TestShardingSelector_GetMulti(t *testing.T) {
  6102  	r := model.NewMetaRegistry()
  6103  	_, err := r.Register(&test.OrderDetail{},
  6104  		model.WithTableShardingAlgorithm(&hash.Hash{
  6105  			ShardingKey:  "OrderId",
  6106  			DBPattern:    &hash.Pattern{Name: "order_detail_db_%d", Base: 2},
  6107  			TablePattern: &hash.Pattern{Name: "order_detail_tab_%d", Base: 3},
  6108  			DsPattern:    &hash.Pattern{Name: "0.db.cluster.company.com:3306", NotSharding: true},
  6109  		}))
  6110  	require.NoError(t, err)
  6111  
  6112  	mockDB, mock, err := sqlmock.New(
  6113  		sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
  6114  	if err != nil {
  6115  		t.Fatal(err)
  6116  	}
  6117  	defer func() { _ = mockDB.Close() }()
  6118  
  6119  	rbSlaves, err := roundrobin.NewSlaves(mockDB)
  6120  	require.NoError(t, err)
  6121  	masterSlaveDB := masterslave.NewMasterSlavesDB(
  6122  		mockDB, masterslave.MasterSlavesWithSlaves(newMockSlaveNameGet(rbSlaves)))
  6123  	require.NoError(t, err)
  6124  
  6125  	mockDB2, mock2, err := sqlmock.New(
  6126  		sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
  6127  	if err != nil {
  6128  		t.Fatal(err)
  6129  	}
  6130  	defer func() { _ = mockDB2.Close() }()
  6131  
  6132  	rbSlaves2, err := roundrobin.NewSlaves(mockDB2)
  6133  	require.NoError(t, err)
  6134  	masterSlaveDB2 := masterslave.NewMasterSlavesDB(
  6135  		mockDB2, masterslave.MasterSlavesWithSlaves(newMockSlaveNameGet(rbSlaves2)))
  6136  	require.NoError(t, err)
  6137  
  6138  	clusterDB := cluster.NewClusterDB(map[string]*masterslave.MasterSlavesDB{
  6139  		"order_detail_db_0": masterSlaveDB,
  6140  		"order_detail_db_1": masterSlaveDB2,
  6141  	})
  6142  	ds := map[string]datasource.DataSource{
  6143  		"0.db.cluster.company.com:3306": clusterDB,
  6144  	}
  6145  	shardingDB, err := OpenDS("mysql",
  6146  		shardingsource.NewShardingDataSource(ds), DBWithMetaRegistry(r))
  6147  	require.NoError(t, err)
  6148  
  6149  	testCases := []struct {
  6150  		name      string
  6151  		s         *ShardingSelector[test.OrderDetail]
  6152  		mockOrder func(mock1, mock2 sqlmock.Sqlmock)
  6153  		wantErr   error
  6154  		wantRes   []*test.OrderDetail
  6155  	}{
  6156  		{
  6157  			name: "invalid field err",
  6158  			s: func() *ShardingSelector[test.OrderDetail] {
  6159  				b := NewShardingSelector[test.OrderDetail](shardingDB).Select(C("ccc"))
  6160  				return b
  6161  			}(),
  6162  			mockOrder: func(mock1, mock2 sqlmock.Sqlmock) {},
  6163  			wantErr:   errs.NewInvalidFieldError("ccc"),
  6164  		},
  6165  		{
  6166  			name: "multi row err",
  6167  			s: func() *ShardingSelector[test.OrderDetail] {
  6168  				b := NewShardingSelector[test.OrderDetail](shardingDB).
  6169  					Where(C("OrderId").EQ(123))
  6170  				return b
  6171  			}(),
  6172  			mockOrder: func(mock1, mock2 sqlmock.Sqlmock) {
  6173  				rows := mock2.NewRows([]string{"order_id", "item_id", "using_col1", "using_col2", "using_col3"}).
  6174  					AddRow(123, 10, "LeBron", "James", "de")
  6175  				mock2.ExpectQuery("SELECT `order_id`,`item_id`,`using_col1`,`using_col2` FROM `order_detail_db_1`.`order_detail_tab_0` WHERE `order_id`=?;").
  6176  					WithArgs(123).WillReturnRows(rows)
  6177  			},
  6178  			wantErr: errs.ErrTooManyColumns,
  6179  		},
  6180  		{
  6181  			name: "found tab or",
  6182  			s: func() *ShardingSelector[test.OrderDetail] {
  6183  				b := NewShardingSelector[test.OrderDetail](shardingDB).
  6184  					Where(C("OrderId").EQ(123).Or(C("OrderId").EQ(234)))
  6185  				return b
  6186  			}(),
  6187  			mockOrder: func(mock1, mock2 sqlmock.Sqlmock) {
  6188  				rows1 := mock1.NewRows([]string{"order_id", "item_id", "using_col1", "using_col2"})
  6189  				rows1.AddRow(234, 12, "Kevin", "Durant")
  6190  				mock1.ExpectQuery("SELECT `order_id`,`item_id`,`using_col1`,`using_col2` FROM `order_detail_db_0`.`order_detail_tab_0` WHERE (`order_id`=?) OR (`order_id`=?);").
  6191  					WithArgs(123, 234).WillReturnRows(rows1)
  6192  				rows2 := mock2.NewRows([]string{"order_id", "item_id", "using_col1", "using_col2"})
  6193  				rows2.AddRow(123, 10, "LeBron", "James")
  6194  				mock2.ExpectQuery("SELECT `order_id`,`item_id`,`using_col1`,`using_col2` FROM `order_detail_db_1`.`order_detail_tab_0` WHERE (`order_id`=?) OR (`order_id`=?);").
  6195  					WithArgs(123, 234).WillReturnRows(rows2)
  6196  			},
  6197  			wantRes: []*test.OrderDetail{
  6198  				{OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"},
  6199  				{OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"},
  6200  			},
  6201  		},
  6202  		{
  6203  			name: "err merge rows diff",
  6204  			s: func() *ShardingSelector[test.OrderDetail] {
  6205  				b := NewShardingSelector[test.OrderDetail](shardingDB).
  6206  					Where(C("OrderId").EQ(123).Or(C("OrderId").EQ(234)))
  6207  				return b
  6208  			}(),
  6209  			mockOrder: func(mock1, mock2 sqlmock.Sqlmock) {
  6210  				rows1 := mock1.NewRows([]string{"order_id", "ite_id", "using_col1", "using_col2"})
  6211  				rows1.AddRow(234, 12, "Kevin", "Durant")
  6212  				mock1.ExpectQuery("SELECT `order_id`,`item_id`,`using_col1`,`using_col2` FROM `order_detail_db_0`.`order_detail_tab_0` WHERE (`order_id`=?) OR (`order_id`=?);").
  6213  					WithArgs(123, 234).WillReturnRows(rows1)
  6214  				rows2 := mock2.NewRows([]string{"order_id", "item_id", "using_col1", "using_col2"})
  6215  				rows2.AddRow(123, 10, "LeBron", "James")
  6216  				mock2.ExpectQuery("SELECT `order_id`,`item_id`,`using_col1`,`using_col2` FROM `order_detail_db_1`.`order_detail_tab_0` WHERE (`order_id`=?) OR (`order_id`=?);").
  6217  					WithArgs(123, 234).WillReturnRows(rows2)
  6218  			},
  6219  			wantErr: errors.New("merger: sql.Rows列表中的字段不同"),
  6220  		},
  6221  	}
  6222  
  6223  	for _, tc := range testCases {
  6224  		t.Run(tc.name, func(t *testing.T) {
  6225  			tc.mockOrder(mock, mock2)
  6226  			res, err := tc.s.GetMulti(context.Background())
  6227  			assert.Equal(t, tc.wantErr, err)
  6228  			if err != nil {
  6229  				return
  6230  			}
  6231  			assert.ElementsMatch(t, tc.wantRes, res)
  6232  		})
  6233  	}
  6234  }
  6235  
  6236  type Order struct {
  6237  	UserId  int
  6238  	OrderId int64
  6239  	Content string
  6240  	Account float64
  6241  }
  6242  
  6243  type testSlaves struct {
  6244  	slaves.Slaves
  6245  }
  6246  
  6247  func newMockSlaveNameGet(s slaves.Slaves) *testSlaves {
  6248  	return &testSlaves{
  6249  		Slaves: s,
  6250  	}
  6251  }
  6252  
  6253  func (s *testSlaves) Next(ctx context.Context) (slaves.Slave, error) {
  6254  	slave, err := s.Slaves.Next(ctx)
  6255  	if err != nil {
  6256  		return slave, err
  6257  	}
  6258  	return slave, err
  6259  }