2
2
use std:: collections:: { BTreeSet , HashSet } ;
3
3
4
4
use crate :: {
5
- backend:: { databases:: databases, Cluster } ,
5
+ backend:: { databases:: databases, Cluster , ShardingSchema } ,
6
6
frontend:: {
7
7
router:: { parser:: OrderBy , round_robin, sharding:: shard_str, CopyRow } ,
8
8
Buffer ,
@@ -110,8 +110,10 @@ impl QueryParser {
110
110
}
111
111
}
112
112
113
+ let sharding_schema = cluster. sharding_schema ( ) ;
114
+
113
115
// Hardcoded shard from a comment.
114
- let shard = super :: comment:: shard ( query, cluster . shards ( ) . len ( ) ) . map_err ( Error :: PgQuery ) ?;
116
+ let shard = super :: comment:: shard ( query, & sharding_schema ) . map_err ( Error :: PgQuery ) ?;
115
117
116
118
// Cluster is read only or write only, traffic split isn't needed,
117
119
// so don't parse the query further.
@@ -144,11 +146,11 @@ impl QueryParser {
144
146
round_robin:: next ( ) % cluster. shards ( ) . len ( ) ,
145
147
) ) ) ) ;
146
148
} else {
147
- Self :: select ( stmt, cluster , params)
149
+ Self :: select ( stmt, & sharding_schema , params)
148
150
}
149
151
}
150
152
Some ( NodeEnum :: CopyStmt ( ref stmt) ) => Self :: copy ( stmt, cluster) ,
151
- Some ( NodeEnum :: InsertStmt ( ref stmt) ) => Self :: insert ( stmt, cluster , & params) ,
153
+ Some ( NodeEnum :: InsertStmt ( ref stmt) ) => Self :: insert ( stmt, & sharding_schema , & params) ,
152
154
Some ( NodeEnum :: UpdateStmt ( ref stmt) ) => Self :: update ( stmt) ,
153
155
Some ( NodeEnum :: DeleteStmt ( ref stmt) ) => Self :: delete ( stmt) ,
154
156
Some ( NodeEnum :: TransactionStmt ( ref stmt) ) => match stmt. kind ( ) {
@@ -193,11 +195,10 @@ impl QueryParser {
193
195
194
196
fn select (
195
197
stmt : & SelectStmt ,
196
- cluster : & Cluster ,
198
+ sharding_schema : & ShardingSchema ,
197
199
params : Option < Bind > ,
198
200
) -> Result < Command , Error > {
199
201
let order_by = Self :: select_sort ( & stmt. sort_clause ) ;
200
- let sharded_tables = cluster. sharded_tables ( ) ;
201
202
let mut shards = HashSet :: new ( ) ;
202
203
let table_name = stmt
203
204
. from_clause
@@ -215,13 +216,13 @@ impl QueryParser {
215
216
. flatten ( ) ;
216
217
if let Some ( where_clause) = WhereClause :: new ( table_name, & stmt. where_clause ) {
217
218
// Complexity: O(number of sharded tables * number of columns in the query)
218
- for table in sharded_tables {
219
+ for table in sharding_schema . tables . tables ( ) {
219
220
let table_name = table. name . as_deref ( ) ;
220
221
let keys = where_clause. keys ( table_name, & table. column ) ;
221
222
for key in keys {
222
223
match key {
223
224
Key :: Constant ( value) => {
224
- if let Some ( shard) = shard_str ( & value, cluster . shards ( ) . len ( ) ) {
225
+ if let Some ( shard) = shard_str ( & value, & sharding_schema ) {
225
226
shards. insert ( shard) ;
226
227
}
227
228
}
@@ -231,8 +232,7 @@ impl QueryParser {
231
232
if let Some ( param) = params. parameter ( param) ? {
232
233
// TODO: Handle binary encoding.
233
234
if let Some ( text) = param. text ( ) {
234
- if let Some ( shard) = shard_str ( text, cluster. shards ( ) . len ( ) )
235
- {
235
+ if let Some ( shard) = shard_str ( text, & sharding_schema) {
236
236
shards. insert ( shard) ;
237
237
}
238
238
}
@@ -310,7 +310,7 @@ impl QueryParser {
310
310
311
311
fn insert (
312
312
stmt : & InsertStmt ,
313
- cluster : & Cluster ,
313
+ sharding_schema : & ShardingSchema ,
314
314
params : & Option < Bind > ,
315
315
) -> Result < Command , Error > {
316
316
let insert = Insert :: new ( stmt) ;
@@ -320,17 +320,15 @@ impl QueryParser {
320
320
. map ( |column| column. name )
321
321
. collect :: < Vec < _ > > ( ) ;
322
322
let table = insert. table ( ) . unwrap ( ) . name ;
323
- let num_shards = cluster. shards ( ) . len ( ) ;
324
-
325
- let sharding_column = cluster. sharded_column ( table, & columns) ;
323
+ let sharding_column = sharding_schema. tables . sharded_column ( table, & columns) ;
326
324
let mut shards = BTreeSet :: new ( ) ;
327
325
if let Some ( column) = sharding_column {
328
326
for tuple in insert. tuples ( ) {
329
327
if let Some ( value) = tuple. get ( column) {
330
328
shards. insert ( if let Some ( bind) = params {
331
- value. shard_placeholder ( bind, num_shards )
329
+ value. shard_placeholder ( bind, & sharding_schema )
332
330
} else {
333
- value. shard ( num_shards )
331
+ value. shard ( & sharding_schema )
334
332
} ) ;
335
333
}
336
334
}
0 commit comments