|
| 1 | +// Copyright 2023 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package util |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/pingcap/tidb/pkg/expression" |
| 19 | + "github.com/pingcap/tidb/pkg/parser/ast" |
| 20 | +) |
| 21 | + |
| 22 | +// ValidCompareConstantPredicate checks if the predicate is an expression like [column '>'|'>='|'<'|'<='|'=' constant]. |
| 23 | +// return param1: return true, if the predicate is a compare constant predicate. |
| 24 | +// return param2: return the column side of predicate. |
| 25 | +func ValidCompareConstantPredicate(ctx expression.EvalContext, candidatePredicate expression.Expression) bool { |
| 26 | + scalarFunction, ok := candidatePredicate.(*expression.ScalarFunction) |
| 27 | + if !ok { |
| 28 | + return false |
| 29 | + } |
| 30 | + if scalarFunction.FuncName.L != ast.GT && scalarFunction.FuncName.L != ast.GE && |
| 31 | + scalarFunction.FuncName.L != ast.LT && scalarFunction.FuncName.L != ast.LE && |
| 32 | + scalarFunction.FuncName.L != ast.EQ { |
| 33 | + return false |
| 34 | + } |
| 35 | + column, _ := expression.ValidCompareConstantPredicateHelper(ctx, scalarFunction, true) |
| 36 | + if column == nil { |
| 37 | + column, _ = expression.ValidCompareConstantPredicateHelper(ctx, scalarFunction, false) |
| 38 | + } |
| 39 | + if column == nil { |
| 40 | + return false |
| 41 | + } |
| 42 | + return true |
| 43 | +} |
0 commit comments