@@ -471,6 +471,52 @@ func (s *testChunkSuite) TestChunkMemoryUsage(c *check.C) {
471
471
c .Assert (memUsage , check .Equals , int64 (expectedUsage ))
472
472
}
473
473
474
+ func (s * testChunkSuite ) TestSwapColumn (c * check.C ) {
475
+ fieldTypes := make ([]* types.FieldType , 0 , 2 )
476
+ fieldTypes = append (fieldTypes , & types.FieldType {Tp : mysql .TypeFloat })
477
+ fieldTypes = append (fieldTypes , & types.FieldType {Tp : mysql .TypeFloat })
478
+ fieldTypes = append (fieldTypes , & types.FieldType {Tp : mysql .TypeFloat })
479
+
480
+ // chk1: column1 refers to column0
481
+ chk1 := NewChunkWithCapacity (fieldTypes , 1 )
482
+ chk1 .AppendFloat64 (0 , 1 )
483
+ chk1 .MakeRef (0 , 1 )
484
+ chk1 .AppendFloat64 (2 , 3 )
485
+
486
+ // chk2: column1 refers to column0
487
+ chk2 := NewChunkWithCapacity (fieldTypes , 1 )
488
+ chk2 .AppendFloat64 (0 , 1 )
489
+ chk2 .MakeRef (0 , 1 )
490
+ chk2 .AppendFloat64 (2 , 3 )
491
+
492
+ c .Assert (chk1 .columns [0 ] == chk1 .columns [1 ], check .IsTrue )
493
+ c .Assert (chk2 .columns [0 ] == chk2 .columns [1 ], check .IsTrue )
494
+
495
+ checkRef := func () {
496
+ c .Assert (chk1 .columns [0 ] == chk1 .columns [1 ], check .IsTrue )
497
+ c .Assert (chk1 .columns [0 ] == chk2 .columns [0 ], check .IsFalse )
498
+ c .Assert (chk2 .columns [0 ] == chk2 .columns [1 ], check .IsTrue )
499
+ }
500
+
501
+ chk1 .SwapColumn (0 , chk2 , 0 )
502
+ checkRef ()
503
+
504
+ chk1 .SwapColumn (0 , chk2 , 1 )
505
+ checkRef ()
506
+
507
+ chk2 .SwapColumn (1 , chk2 , 0 )
508
+ checkRef ()
509
+
510
+ chk2 .SwapColumn (1 , chk2 , 1 )
511
+ checkRef ()
512
+
513
+ chk2 .SwapColumn (1 , chk2 , 2 )
514
+ checkRef ()
515
+
516
+ chk2 .SwapColumn (2 , chk2 , 0 )
517
+ checkRef ()
518
+ }
519
+
474
520
func BenchmarkAppendInt (b * testing.B ) {
475
521
b .ReportAllocs ()
476
522
chk := newChunk (8 )
0 commit comments