8
8
"github.com/pg-sharding/spqr/pkg/config"
9
9
"github.com/pg-sharding/spqr/pkg/models/hashfunction"
10
10
"github.com/pg-sharding/spqr/pkg/models/kr"
11
- "github.com/pg-sharding/spqr/pkg/models/shrule"
12
11
"github.com/pg-sharding/spqr/pkg/session"
13
12
"github.com/pg-sharding/spqr/pkg/spqrlog"
14
13
"github.com/pg-sharding/spqr/qdb"
@@ -64,39 +63,20 @@ type RoutingMetadataContext struct {
64
63
// INSERT INTO x (...) SELECT 7
65
64
TargetList []lyx.Node
66
65
67
- rls []* shrule.ShardingRule
68
- krs []* kr.KeyRange
69
66
distribution string
70
67
71
68
params [][]byte
72
69
paramsFormatCodes []int16
73
70
// TODO: include client ops and metadata here
74
71
}
75
72
76
- func (m * RoutingMetadataContext ) CheckColumnRls (colname string ) bool {
77
- for i := range m .rls {
78
- for _ , c := range m .rls [i ].Entries () {
79
- if c .Column == colname {
80
- return true
81
- }
82
- }
83
- }
84
- return false
85
- }
86
-
87
- func NewRoutingMetadataContext (
88
- krs []* kr.KeyRange ,
89
- rls []* shrule.ShardingRule ,
90
- ds string ,
91
- params [][]byte , paramsFormatCodes []int16 ) * RoutingMetadataContext {
73
+ func NewRoutingMetadataContext (ds string , params [][]byte , paramsFormatCodes []int16 ) * RoutingMetadataContext {
92
74
93
75
meta := & RoutingMetadataContext {
94
76
rels : map [RelationFQN ][]string {},
95
77
tableAliases : map [string ]RelationFQN {},
96
78
exprs : map [RelationFQN ]map [string ]string {},
97
79
unparsed_columns : map [string ]struct {}{},
98
- krs : krs ,
99
- rls : rls ,
100
80
distribution : ds ,
101
81
params : params ,
102
82
}
@@ -171,19 +151,19 @@ func (qr *ProxyQrouter) DeparseExprShardingEntries(expr lyx.Node, meta *RoutingM
171
151
}
172
152
173
153
// TODO : unit tests
174
- func (qr * ProxyQrouter ) DeparseKeyWithRangesInternal (ctx context.Context , key string , meta * RoutingMetadataContext ) (* routingstate.DataShardRoute , error ) {
154
+ func (qr * ProxyQrouter ) DeparseKeyWithRangesInternal (_ context.Context , key string , krs [] * kr. KeyRange ) (* routingstate.DataShardRoute , error ) {
175
155
spqrlog .Zero .Debug ().
176
156
Str ("key" , key ).
177
157
Msg ("checking key" )
178
158
179
159
spqrlog .Zero .Debug ().
180
160
Str ("key" , key ).
181
- Int ("key-ranges-count" , len (meta . krs )).
161
+ Int ("key-ranges-count" , len (krs )).
182
162
Msg ("checking key with key ranges" )
183
163
184
164
var matched_krkey * kr.KeyRange = nil
185
165
186
- for _ , krkey := range meta . krs {
166
+ for _ , krkey := range krs {
187
167
if kr .CmpRangesLessEqual (krkey .LowerBound , []byte (key )) &&
188
168
(matched_krkey == nil || kr .CmpRangesLessEqual (matched_krkey .LowerBound , krkey .LowerBound )) {
189
169
matched_krkey = krkey
@@ -205,7 +185,7 @@ func (qr *ProxyQrouter) DeparseKeyWithRangesInternal(ctx context.Context, key st
205
185
}
206
186
207
187
// TODO : unit tests
208
- func (qr * ProxyQrouter ) RouteKeyWithRanges (ctx context.Context , expr lyx.Node , meta * RoutingMetadataContext , hf hashfunction.HashFunctionType ) (* routingstate.DataShardRoute , error ) {
188
+ func (qr * ProxyQrouter ) RouteKeyWithRanges (ctx context.Context , expr lyx.Node , meta * RoutingMetadataContext , krs [] * kr. KeyRange , hf hashfunction.HashFunctionType ) (* routingstate.DataShardRoute , error ) {
209
189
switch e := expr .(type ) {
210
190
case * lyx.ParamRef :
211
191
if e .Number > len (meta .params ) {
@@ -233,15 +213,15 @@ func (qr *ProxyQrouter) RouteKeyWithRanges(ctx context.Context, expr lyx.Node, m
233
213
}
234
214
spqrlog .Zero .Debug ().Str ("key" , string (routeParam )).Str ("hashed key" , string (hashedKey )).Msg ("applying hash function on key" )
235
215
236
- return qr .DeparseKeyWithRangesInternal (ctx , string (hashedKey ), meta )
216
+ return qr .DeparseKeyWithRangesInternal (ctx , string (hashedKey ), krs )
237
217
case * lyx.AExprSConst :
238
218
hashedKey , err := hashfunction .ApplyHashFunction ([]byte (e .Value ), hf )
239
219
if err != nil {
240
220
return nil , err
241
221
}
242
222
243
223
spqrlog .Zero .Debug ().Str ("key" , e .Value ).Str ("hashed key" , string (hashedKey )).Msg ("applying hash function on key" )
244
- return qr .DeparseKeyWithRangesInternal (ctx , string (hashedKey ), meta )
224
+ return qr .DeparseKeyWithRangesInternal (ctx , string (hashedKey ), krs )
245
225
case * lyx.AExprIConst :
246
226
val := fmt .Sprintf ("%d" , e .Value )
247
227
hashedKey , err := hashfunction .ApplyHashFunction ([]byte (val ), hf )
@@ -250,7 +230,7 @@ func (qr *ProxyQrouter) RouteKeyWithRanges(ctx context.Context, expr lyx.Node, m
250
230
}
251
231
252
232
spqrlog .Zero .Debug ().Int ("key" , e .Value ).Str ("hashed key" , string (hashedKey )).Msg ("applying hash function on key" )
253
- return qr .DeparseKeyWithRangesInternal (ctx , string (hashedKey ), meta )
233
+ return qr .DeparseKeyWithRangesInternal (ctx , string (hashedKey ), krs )
254
234
default :
255
235
return nil , ComplexQuery
256
236
}
@@ -746,17 +726,8 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
746
726
}
747
727
748
728
/* TODO: delay this until step 2. */
749
- krs , err := qr .mgr .ListKeyRanges (ctx , queryDistribution )
750
- if err != nil {
751
- return nil , err
752
- }
753
729
754
- rls , err := qr .mgr .ListShardingRules (ctx , queryDistribution )
755
- if err != nil {
756
- return nil , err
757
- }
758
-
759
- meta := NewRoutingMetadataContext (krs , rls , queryDistribution , sph .BindParams (), sph .BindParamFormatCodes ())
730
+ meta := NewRoutingMetadataContext (queryDistribution , sph .BindParams (), sph .BindParamFormatCodes ())
760
731
761
732
tsa := config .TargetSessionAttrsAny
762
733
@@ -931,9 +902,16 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
931
902
var route_err error
932
903
for rfqn , cols := range meta .rels {
933
904
934
- /*
935
- *
936
- */
905
+ // TODO: check by whole RFQN
906
+ ds , err := qr .mgr .GetRelationDistribution (ctx , rfqn .RelationName )
907
+ if err != nil {
908
+ return nil , err
909
+ }
910
+
911
+ krs , err := qr .mgr .ListKeyRanges (ctx , ds .Id )
912
+ if err != nil {
913
+ return nil , err
914
+ }
937
915
938
916
if rule , err := MatchShardingRule (ctx , rfqn .RelationName , cols , qr .mgr .QDB ()); err != nil {
939
917
for _ , col := range cols {
@@ -953,7 +931,7 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
953
931
continue
954
932
}
955
933
956
- currroute , err := qr .DeparseKeyWithRangesInternal (ctx , string (hashedKey ), meta )
934
+ currroute , err := qr .DeparseKeyWithRangesInternal (ctx , string (hashedKey ), krs )
957
935
if err != nil {
958
936
route_err = err
959
937
spqrlog .Zero .Debug ().Err (route_err ).Msg ("temporarily skip the route error" )
@@ -996,6 +974,15 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
996
974
}
997
975
}
998
976
977
+ ds , err := qr .mgr .GetRelationDistribution (ctx , meta .InsertStmtRel )
978
+ if err != nil {
979
+ return nil , err
980
+ }
981
+ krs , err := qr .mgr .ListKeyRanges (ctx , ds .Id )
982
+ if err != nil {
983
+ return nil , err
984
+ }
985
+
999
986
hf , err := hashfunction .HashFunctionByName (rule .Entries [0 ].HashFunction )
1000
987
if err != nil {
1001
988
/* failed to resolve hash function */
@@ -1005,7 +992,7 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
1005
992
meta .offsets = offsets
1006
993
routed := false
1007
994
if len (meta .offsets ) != 0 && len (meta .TargetList ) > meta .offsets [0 ] {
1008
- currroute , err := qr .RouteKeyWithRanges (ctx , meta .TargetList [meta .offsets [0 ]], meta , hf )
995
+ currroute , err := qr .RouteKeyWithRanges (ctx , meta .TargetList [meta .offsets [0 ]], meta , krs , hf )
1009
996
if err == nil {
1010
997
/* else failed, ignore */
1011
998
spqrlog .Zero .Debug ().
@@ -1023,7 +1010,7 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, stmt lyx.Node, sph s
1023
1010
if len (meta .offsets ) != 0 && len (meta .ValuesLists ) > meta .offsets [0 ] && ! routed && meta .ValuesLists != nil {
1024
1011
// only first value from value list
1025
1012
1026
- currroute , err := qr .RouteKeyWithRanges (ctx , meta .ValuesLists [meta .offsets [0 ]], meta , hf )
1013
+ currroute , err := qr .RouteKeyWithRanges (ctx , meta .ValuesLists [meta .offsets [0 ]], meta , krs , hf )
1027
1014
if err == nil { /* else failed, ignore */
1028
1015
spqrlog .Zero .Debug ().
1029
1016
Interface ("current-route" , currroute ).
0 commit comments