Files
tidb/pkg/expression/fts_helper.go
2025-04-25 03:42:37 +00:00

76 lines
1.8 KiB
Go

// Copyright 2025 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
"github.com/pingcap/tidb/pkg/parser/ast"
)
// FTSInfo is an easy to use struct for interpreting a FullTextSearch expression.
type FTSInfo struct {
Query string
Column *Column
}
// ContainsFullTextSearchFn recursively checks whether the expression tree contains a
// possible FullTextSearch function.
func ContainsFullTextSearchFn(expr Expression) bool {
switch x := expr.(type) {
case *ScalarFunction:
if x.FuncName.L == ast.FTSMatchWord {
return true
}
for _, arg := range x.GetArgs() {
if ContainsFullTextSearchFn(arg) {
return true
}
}
}
return false
}
// InterpretFullTextSearchExpr try to interpret a FullText search expression.
// If interpret successfully, return a FTSInfo struct, otherwise return nil.
func InterpretFullTextSearchExpr(expr Expression) *FTSInfo {
x, ok := expr.(*ScalarFunction)
if !ok {
return nil
}
if x.FuncName.L != ast.FTSMatchWord {
return nil
}
if len(x.GetArgs()) != 2 {
return nil
}
argQuery := x.GetArgs()[0]
argColumn := x.GetArgs()[1]
query, ok := argQuery.(*Constant)
if !ok {
return nil
}
column, ok := argColumn.(*Column)
if !ok {
return nil
}
return &FTSInfo{
Query: query.Value.GetString(),
Column: column,
}
}