Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions pkg/planner/core/memtable_predicate_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (helper *extractHelper) extractColBinaryOpConsExpr(
ctx base.PlanContext,
extractCols map[int64]*types.FieldName,
expr *expression.ScalarFunction,
) (string, []types.Datum) {
) (string, []types.Datum, bool) {
args := expr.GetArgs()
var col *expression.Column
var colIdx int
Expand All @@ -203,20 +203,20 @@ func (helper *extractHelper) extractColBinaryOpConsExpr(
col, colIdx = innerCol, innerColIdx
}
if col == nil {
return "", nil
return "", nil, false
}

name, found := extractCols[col.UniqueID]
if !found {
return "", nil
return "", nil, false
}

// The `lhs/rhs` of EQ expression must be a constant
// SELECT * FROM t1 WHERE c='rhs'
// SELECT * FROM t1 WHERE 'lhs'=c
constant, ok := args[1-colIdx].(*expression.Constant)
if !ok || constant.DeferredExpr != nil {
return "", nil
return "", nil, false
}
v := constant.Value
if constant.ParamMarker != nil {
Expand All @@ -225,10 +225,10 @@ func (helper *extractHelper) extractColBinaryOpConsExpr(
intest.AssertNoError(err, "fail to get param")
if err != nil {
logutil.BgLogger().Warn("fail to get param", zap.Error(err))
return "", nil
return "", nil, false
}
}
return name.ColName.L, []types.Datum{v}
return name.ColName.L, []types.Datum{v}, colIdx == 0
}

// extract the OR expression, e.g:
Expand All @@ -247,7 +247,8 @@ func (helper *extractHelper) extractColOrExpr(ctx base.PlanContext, extractCols
var extract = func(extractCols map[int64]*types.FieldName, fn *expression.ScalarFunction) (string, []types.Datum) {
switch helper.getStringFunctionName(fn) {
case ast.EQ:
return helper.extractColBinaryOpConsExpr(ctx, extractCols, fn)
colName, datums, _ := helper.extractColBinaryOpConsExpr(ctx, extractCols, fn)
return colName, datums
case ast.LogicOr:
return helper.extractColOrExpr(ctx, extractCols, fn)
case ast.In:
Expand Down Expand Up @@ -319,7 +320,7 @@ func (helper *extractHelper) extractCol(
switch helper.getStringFunctionName(fn) {
case ast.EQ:
helper.enableScalarPushDown = true
colName, datums = helper.extractColBinaryOpConsExpr(ctx, extractCols, fn)
colName, datums, _ = helper.extractColBinaryOpConsExpr(ctx, extractCols, fn)
if colName == extractColName {
helper.setColumnPushedDownFn(colName, extractCols, fn)
}
Expand Down Expand Up @@ -449,7 +450,7 @@ func (helper extractHelper) extractLikePattern(
var datums []types.Datum
switch fn.FuncName.L {
case ast.EQ, ast.Like, ast.Ilike, ast.Regexp, ast.RegexpLike:
colName, datums = helper.extractColBinaryOpConsExpr(ctx, extractCols, fn)
colName, datums, _ = helper.extractColBinaryOpConsExpr(ctx, extractCols, fn)
}
if colName != extractColName {
return false, ""
Expand Down Expand Up @@ -551,13 +552,27 @@ func (helper extractHelper) extractTimeRange(

var colName string
var datums []types.Datum
var colOnLeft bool
fnName := helper.getTimeFunctionName(fn)
switch fnName {
case ast.GT, ast.GE, ast.LT, ast.LE, ast.EQ:
colName, datums = helper.extractColBinaryOpConsExpr(ctx, extractCols, fn)
colName, datums, colOnLeft = helper.extractColBinaryOpConsExpr(ctx, extractCols, fn)
}

if colName == extractColName {
if !colOnLeft {
switch fnName {
case ast.GT:
fnName = ast.LT
case ast.GE:
fnName = ast.LE
case ast.LT:
fnName = ast.GT
case ast.LE:
fnName = ast.GE
}
}

timeType := types.NewFieldType(mysql.TypeDatetime)
timeType.SetDecimal(6)
timeDatum, err := datums[0].ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), timeType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,13 @@ func TestClusterLogTableExtractor(t *testing.T) {
startTime: timestamp(t, "2019-10-10 10:10:10"),
endTime: timestamp(t, "2019-10-11 10:10:10") - 1,
},
{
sql: "select * from information_schema.cluster_log where '2019-10-10 10:10:10' < time and '2019-10-11 10:10:10' > time",
nodeTypes: set.NewStringSet(),
instances: set.NewStringSet(),
startTime: timestamp(t, "2019-10-10 10:10:10") + 1,
endTime: timestamp(t, "2019-10-11 10:10:10") - 1,
},
{
sql: "select * from information_schema.cluster_log where time>='2019-10-12 10:10:10' and time<'2019-10-11 10:10:10'",
nodeTypes: set.NewStringSet(),
Expand Down Expand Up @@ -632,6 +639,12 @@ func TestMetricTableExtractor(t *testing.T) {
startTime: parseTime(t, "2019-10-10 10:10:10"),
endTime: parseTime(t, "2019-10-10 10:20:10"),
},
{
sql: "select * from metrics_schema.tidb_query_duration where '2019-10-10 10:10:10' < time and '2019-10-11 10:10:10' > time",
promQL: `histogram_quantile(0.9, sum(rate(tidb_server_handle_query_duration_seconds_bucket{}[60s])) by (le,sql_type,instance))`,
startTime: parseTime(t, "2019-10-10 10:10:10.001"),
endTime: parseTime(t, "2019-10-11 10:10:09.999"),
},
{
sql: "select * from metrics_schema.tidb_query_duration where time>='2019-10-10 10:10:10' and time<='2019-10-09 10:10:10'",
promQL: "",
Expand Down Expand Up @@ -1143,6 +1156,14 @@ func TestTiDBHotRegionsHistoryTableExtractor(t *testing.T) {
isLeaders: []bool{false, true},
hotRegionTypes: set.NewStringSet(plannercore.HotRegionTypeRead, plannercore.HotRegionTypeWrite),
},
{
sql: "select * from information_schema.tidb_hot_regions_history where '2019-10-10 10:10:10' < update_time and '2019-10-11 10:10:10' > update_time",
startTime: timestamp(t, "2019-10-10 10:10:10") + 1,
endTime: timestamp(t, "2019-10-11 10:10:10") - 1,
isLearners: []bool{false, true},
isLeaders: []bool{false, true},
hotRegionTypes: set.NewStringSet(plannercore.HotRegionTypeRead, plannercore.HotRegionTypeWrite),
},
{
sql: "select * from information_schema.tidb_hot_regions_history where update_time>='2019-10-12 10:10:10' and update_time<'2019-10-11 10:10:10'",
startTime: timestamp(t, "2019-10-12 10:10:10"),
Expand Down
Loading