Skip to content

Commit ad5ca42

Browse files
authored
planner: fix count(*) return wrong value for information_schema.tables (#57506)
close #56987
1 parent ea7ec59 commit ad5ca42

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

pkg/executor/infoschema_reader_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,15 @@ func TestTablesTable(t *testing.T) {
552552
testkit.Rows())
553553
tk.MustQuery(fmt.Sprintf("select table_schema, table_name, tidb_table_id from information_schema.tables where table_schema = 'db1' and table_name = 't1' and tidb_table_id in (%s,%s)", tableMetas[0].id, tableMetas[1].id)).Check(
554554
testkit.Rows(toString(tableMetas[0])))
555+
556+
selectTables, err := strconv.Atoi(tk.MustQuery("select count(*) from information_schema.tables where upper(table_name) = 'T1'").Rows()[0][0].(string))
557+
require.NoError(t, err)
558+
totalTables, err := strconv.Atoi(tk.MustQuery("select count(*) from information_schema.tables").Rows()[0][0].(string))
559+
require.NoError(t, err)
560+
remainTables, err := strconv.Atoi(tk.MustQuery("select count(*) from information_schema.tables where upper(table_name) != 'T1'").Rows()[0][0].(string))
561+
require.NoError(t, err)
562+
require.Equal(t, 2, selectTables)
563+
require.Equal(t, totalTables, remainTables+selectTables)
555564
}
556565

557566
func TestColumnTable(t *testing.T) {

pkg/planner/core/memtable_infoschema_extractor.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,17 @@ func (e *InfoSchemaBaseExtractor) filter(colName string, val string) bool {
260260
return true
261261
}
262262
}
263+
264+
toLower := false
265+
if e.extractLowerString != nil {
266+
toLower = e.extractLowerString[colName]
267+
}
268+
263269
predVals, ok := e.ColPredicates[colName]
264270
if ok && len(predVals) > 0 {
271+
if toLower {
272+
return !predVals.Exist(strings.ToLower(val))
273+
}
265274
fn, ok := e.pushedDownFuncs[colName]
266275
if ok {
267276
return !predVals.Exist(fn(val))

pkg/planner/core/memtable_predicate_extractor.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ import (
5151
type extractHelper struct {
5252
enableScalarPushDown bool
5353
pushedDownFuncs map[string]func(string) string
54+
55+
// Store whether the extracted strings for a specific column are converted to lower case
56+
extractLowerString map[string]bool
5457
}
5558

5659
func (extractHelper) extractColInConsExpr(ctx base.PlanContext, extractCols map[int64]*types.FieldName, expr *expression.ScalarFunction) (string, []types.Datum) {
@@ -342,6 +345,11 @@ func (helper *extractHelper) extractCol(
342345
break
343346
}
344347
}
348+
349+
if helper.extractLowerString == nil {
350+
helper.extractLowerString = make(map[string]bool)
351+
}
352+
helper.extractLowerString[extractColName] = valueToLower
345353
return
346354
}
347355

0 commit comments

Comments
 (0)