@@ -847,59 +847,63 @@ func TestTabletTargeting(t *testing.T) {
847847	qr  :=  utils .Exec (t , conn , "show vitess_tablets" )
848848	require .Greater (t , len (qr .Rows ), 0 , "no tablets found" )
849849
850- 	// Find PRIMARY tablets for both shards (-80 and 80-) 
851- 	var  primaryShard80Minus  string   // We'll target this shard 
852- 	var  primaryShard80Plus  string    // We'll verify this shard gets no writes 
850+ 	// Find PRIMARY and REPLICA  tablets for both shards (-80 and 80-) 
851+ 	var  primaryShard80Minus  string 
852+ 	var  primaryShard80Plus  string 
853853	for  _ , row  :=  range  qr .Rows  {
854854		shard  :=  row [2 ].ToString ()
855855		tabletType  :=  row [3 ].ToString ()
856- 		if  tabletType  ==  "PRIMARY"  {
857- 			switch  shard  {
858- 			case  "-80" :
859- 				primaryShard80Minus  =  row [0 ].ToString ()
860- 			case  "80-" :
861- 				primaryShard80Plus  =  row [0 ].ToString ()
862- 			}
856+ 		switch  {
857+ 		case  tabletType  ==  "PRIMARY"  &&  shard  ==  "-80" :
858+ 			primaryShard80Minus  =  row [0 ].ToString ()
859+ 		case  tabletType  ==  "PRIMARY"  &&  shard  ==  "80-" :
860+ 			primaryShard80Plus  =  row [0 ].ToString ()
863861		}
864862	}
865863	require .NotEmpty (t , primaryShard80Minus , "no PRIMARY tablet found for -80 shard" )
866864	require .NotEmpty (t , primaryShard80Plus , "no PRIMARY tablet found for 80- shard" )
867865
868- 	// Test: Target a specific tablet in -80 shard and insert rows  
869- 	// id1=1 and id1=2 both  hash to -80  shard - this validates normal behavior  
870- 	utils .Exec (t , conn , fmt .Sprintf ("USE ks:-80@%s" , primaryShard80Minus ))
866+ 	// Test 1: Shard targeting bypasses vindex resolution  
867+ 	// Insert data that would normally  hash to 80-  shard, but goes to -80 because of shard targeting  
868+ 	utils .Exec (t , conn , fmt .Sprintf ("USE ` ks:-80@%s` " , primaryShard80Minus ))
871869	utils .Exec (t , conn , "insert into t1(id1, id2) values(1, 100), (2, 200)" )
872- 	utils .AssertMatches (t , conn , "select id1 from t1 where id1 in (1, 2) order by id1" , "[[INT64(1)] [INT64(2)]]" )
873- 
874- 	// Test: Insert data that would normally hash to 80- shard, but goes to -80 because of shard targeting 
875870	// id1=4 hashes to 80-, but we're targeting -80 shard explicitly 
876- 	utils .Exec (t , conn , fmt .Sprintf ("USE ks:-80@%s" , primaryShard80Minus ))
877871	utils .Exec (t , conn , "insert into t1(id1, id2) values(4, 400)" )
878872
879873	// Verify the data went to -80 shard (not where vindex would have put it) 
880- 	utils .Exec (t , conn , "USE ks:-80" )
881- 	utils .AssertMatches (t , conn , "select id1 from t1 where id1=4 " , "[[INT64(4)]]" )
874+ 	utils .Exec (t , conn , "USE ` ks:-80` " )
875+ 	utils .AssertMatches (t , conn , "select id1 from t1 where id1 in (1, 2, 4) order by id1 " , "[[INT64(1)] [INT64(2)]  [INT64(4)]]" )
882876
883- 	// Verify the data did NOT go to 80- shard (where vindex says it  should be) 
884- 	utils .Exec (t , conn , "USE ks:80-" )
877+ 	// Verify the data did NOT go to 80- shard (where vindex says id1=4  should be) 
878+ 	utils .Exec (t , conn , "USE ` ks:80-` " )
885879	utils .AssertIsEmpty (t , conn , "select id1 from t1 where id1=4" )
886880
887- 	// Test: Transaction with tablet-specific routing 
888- 	utils .Exec (t , conn , fmt .Sprintf ("USE ks:-80@%s" , primaryShard80Minus ))
881+ 	// Test 2 : Transaction with tablet-specific routing maintains sticky connection  
882+ 	utils .Exec (t , conn , fmt .Sprintf ("USE ` ks:-80@%s` " , primaryShard80Minus ))
889883	utils .Exec (t , conn , "begin" )
890884	utils .Exec (t , conn , "insert into t1(id1, id2) values(10, 300)" )
885+ 	// Subsequent queries in transaction should go to same tablet 
886+ 	utils .AssertMatches (t , conn , "select id1 from t1 where id1=10" , "[[INT64(10)]]" )
891887	utils .Exec (t , conn , "commit" )
892888	utils .AssertMatches (t , conn , "select id1 from t1 where id1=10" , "[[INT64(10)]]" )
893889
894- 	// Test: Rollback with tablet-specific routing 
895- 	utils .Exec (t , conn , fmt .Sprintf ("USE ks:-80@%s" , primaryShard80Minus ))
890+ 	// Test 5 : Rollback with tablet-specific routing 
891+ 	utils .Exec (t , conn , fmt .Sprintf ("USE ` ks:-80@%s` " , primaryShard80Minus ))
896892	utils .Exec (t , conn , "begin" )
897893	utils .Exec (t , conn , "insert into t1(id1, id2) values(20, 500)" )
898894	utils .Exec (t , conn , "rollback" )
899- 	// 20 should not exist 
900895	utils .AssertIsEmpty (t , conn , "select id1 from t1 where id1=20" )
901896
902- 	// Test: Clear tablet targeting returns to normal routing 
897+ 	// Test 3: Invalid tablet alias should fail 
898+ 	_ , err  =  conn .ExecuteFetch ("USE `ks:-80@nonexistent-tablet`" , 1 , false )
899+ 	require .Error (t , err , "invalid tablet alias should fail" )
900+ 
901+ 	// Test 7: Tablet targeting without shard specification 
902+ 	utils .Exec (t , conn , fmt .Sprintf ("USE `ks@%s`" , primaryShard80Minus ))
903+ 	// Should be able to query data from the tablet's shard 
904+ 	utils .AssertMatches (t , conn , "select id1 from t1 where id1 in (1, 2, 4, 10) order by id1" , "[[INT64(1)] [INT64(2)] [INT64(4)] [INT64(10)]]" )
905+ 
906+ 	// Test 5: Clear tablet targeting returns to normal routing 
903907	utils .Exec (t , conn , "USE ks" )
904908	utils .AssertMatches (t , conn , "select id1 from t1 where id1 in (1, 2, 4, 10) order by id1" , "[[INT64(1)] [INT64(2)] [INT64(4)] [INT64(10)]]" )
905909
0 commit comments