@@ -116,13 +116,17 @@ impl<'a> Insert<'a> {
116116 if let Some ( key) = key {
117117 if let Some ( bind) = bind {
118118 if let Ok ( Some ( param) ) = bind. parameter ( key. position ) {
119- // Arrays not supported as sharding keys at the moment.
120- let value = ShardingValue :: from_param ( & param, key. table . data_type ) ?;
121- let ctx = ContextBuilder :: new ( key. table )
122- . value ( value)
123- . shards ( schema. shards )
124- . build ( ) ?;
125- return Ok ( InsertRouting :: Routed ( ctx. apply ( ) ?) ) ;
119+ if param. is_null ( ) {
120+ return Ok ( InsertRouting :: Routed ( Shard :: All ) ) ;
121+ } else {
122+ // Arrays not supported as sharding keys at the moment.
123+ let value = ShardingValue :: from_param ( & param, key. table . data_type ) ?;
124+ let ctx = ContextBuilder :: new ( key. table )
125+ . value ( value)
126+ . shards ( schema. shards )
127+ . build ( ) ?;
128+ return Ok ( InsertRouting :: Routed ( ctx. apply ( ) ?) ) ;
129+ }
126130 }
127131 }
128132
@@ -845,4 +849,34 @@ mod test {
845849 _ => panic ! ( "not an insert" ) ,
846850 }
847851 }
852+
853+ #[ test]
854+ fn test_null_sharding_key_routes_to_all ( ) {
855+ let query = parse ( "INSERT INTO sharded (id, value) VALUES ($1, 'test')" ) . unwrap ( ) ;
856+ let select = query. protobuf . stmts . first ( ) . unwrap ( ) . stmt . as_ref ( ) . unwrap ( ) ;
857+ let schema = ShardingSchema {
858+ shards : 3 ,
859+ tables : ShardedTables :: new (
860+ vec ! [ ShardedTable {
861+ name: Some ( "sharded" . into( ) ) ,
862+ column: "id" . into( ) ,
863+ ..Default :: default ( )
864+ } ] ,
865+ vec ! [ ] ,
866+ ) ,
867+ ..Default :: default ( )
868+ } ;
869+
870+ match & select. node {
871+ Some ( NodeEnum :: InsertStmt ( stmt) ) => {
872+ let insert = Insert :: new ( stmt) ;
873+ let bind = Bind :: new_params ( "" , & [ Parameter :: new_null ( ) ] ) ;
874+ let routing = insert
875+ . shard ( & schema, Some ( & bind) , false , RewriteMode :: Error )
876+ . unwrap ( ) ;
877+ assert ! ( matches!( routing. shard( ) , Shard :: All ) ) ;
878+ }
879+ _ => panic ! ( "not an insert" ) ,
880+ }
881+ }
848882}
0 commit comments