@@ -627,3 +627,88 @@ func TestReferencedTableSchemaWithForeignKey(t *testing.T) {
627
627
WHERE table_name = 't2' AND table_schema = 'test2';` ).Check (testkit .Rows (
628
628
"id id t1 test2 test" ))
629
629
}
630
+
631
+ func TestInfoSchemaConditionWorks (t * testing.T ) {
632
+ // this test creates table in different schema with different index name, and check
633
+ // the condition in the following columns whether work as expected.
634
+ //
635
+ // - "table_schema"
636
+ // - "constraint_schema"
637
+ // - "table_name"
638
+ // - "constraint_name"
639
+ // - "partition_name"
640
+ // - "schema_name"
641
+ // - "index_name"
642
+ store := testkit .CreateMockStore (t )
643
+ tk := testkit .NewTestKit (t , store )
644
+ for db := 0 ; db < 2 ; db ++ {
645
+ for table := 0 ; table < 2 ; table ++ {
646
+ tk .MustExec (fmt .Sprintf ("create database if not exists db%d;" , db ))
647
+ tk .MustExec (fmt .Sprintf (`create table db%d.table%d (id int primary key, data0 varchar(255), data1 varchar(255))
648
+ partition by range (id) (
649
+ partition p0 values less than (10),
650
+ partition p1 values less than (20)
651
+ );` , db , table ))
652
+ for index := 0 ; index < 2 ; index ++ {
653
+ tk .MustExec (fmt .Sprintf ("create index idx%d on db%d.table%d (data%d);" , index , db , table , index ))
654
+ }
655
+ }
656
+ }
657
+
658
+ testColumns := map [string ]string {
659
+ "table_schema" : "db" ,
660
+ "constraint_schema" : "db" ,
661
+ "table_name" : "table" ,
662
+ "constraint_name" : "idx" ,
663
+ "partition_name" : "p" ,
664
+ "schema_name" : "db" ,
665
+ "index_name" : "idx" ,
666
+ }
667
+ testTables := []string {}
668
+ for _ , row := range tk .MustQuery ("show tables in information_schema" ).Rows () {
669
+ tableName := row [0 ].(string )
670
+ // exclude some tables which cannot run without TiKV.
671
+ if strings .HasPrefix (tableName , "CLUSTER_" ) ||
672
+ strings .HasPrefix (tableName , "INSPECTION_" ) ||
673
+ strings .HasPrefix (tableName , "METRICS_" ) ||
674
+ strings .HasPrefix (tableName , "TIFLASH_" ) ||
675
+ strings .HasPrefix (tableName , "TIKV_" ) ||
676
+ strings .HasPrefix (tableName , "USER_" ) ||
677
+ tableName == "TABLE_STORAGE_STATS" ||
678
+ strings .Contains (tableName , "REGION" ) {
679
+ continue
680
+ }
681
+ testTables = append (testTables , row [0 ].(string ))
682
+ }
683
+ for _ , table := range testTables {
684
+ rs , err := tk .Exec (fmt .Sprintf ("select * from information_schema.%s" , table ))
685
+ require .NoError (t , err )
686
+ cols := rs .Fields ()
687
+
688
+ chk := rs .NewChunk (nil )
689
+ rowCount := 0
690
+ for {
691
+ err := rs .Next (context .Background (), chk )
692
+ require .NoError (t , err )
693
+ if chk .NumRows () == 0 {
694
+ break
695
+ }
696
+ rowCount += chk .NumRows ()
697
+ }
698
+ if rowCount == 0 {
699
+ // TODO: find a way to test the table without any rows by adding some rows to them.
700
+ continue
701
+ }
702
+ for i := 0 ; i < len (cols ); i ++ {
703
+ colName := cols [i ].Column .Name .L
704
+ if valPrefix , ok := testColumns [colName ]; ok {
705
+ for j := 0 ; j < 2 ; j ++ {
706
+ rows := tk .MustQuery (fmt .Sprintf ("select * from information_schema.%s where %s = '%s%d';" ,
707
+ table , colName , valPrefix , j )).Rows ()
708
+ rowCountWithCondition := len (rows )
709
+ require .Less (t , rowCountWithCondition , rowCount , "%s has no effect on %s" , colName , table )
710
+ }
711
+ }
712
+ }
713
+ }
714
+ }
0 commit comments