Skip to content

Commit de6ca9c

Browse files
authored
Tokenize and build FTS query string for postgres and sqlite (#3933)
1 parent 0da29e6 commit de6ca9c

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

common/persistence/visibility/store/sql/query_converter_postgresql.go

+17
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,23 @@ func (c *pgQueryConverter) convertTextComparisonExpr(
156156
if !isSupportedTextOperator(expr.Operator) {
157157
return nil, query.NewConverterError("invalid query")
158158
}
159+
valueExpr, ok := expr.Right.(*unsafeSQLString)
160+
if !ok {
161+
return nil, query.NewConverterError(
162+
"%s: unexpected value type (expected string, got %s)",
163+
query.InvalidExpressionErrMessage,
164+
sqlparser.String(expr.Right),
165+
)
166+
}
167+
tokens := tokenizeTextQueryString(valueExpr.Val)
168+
if len(tokens) == 0 {
169+
return nil, query.NewConverterError(
170+
"%s: unexpected value for Text type search attribute (no tokens found in %s)",
171+
query.InvalidExpressionErrMessage,
172+
sqlparser.String(expr.Right),
173+
)
174+
}
175+
valueExpr.Val = strings.Join(tokens, " | ")
159176
var newExpr sqlparser.Expr = &sqlparser.ComparisonExpr{
160177
Operator: ftsMatchOp,
161178
Left: expr.Left,

common/persistence/visibility/store/sql/query_converter_sqlite.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -179,22 +179,23 @@ func (c *sqliteQueryConverter) convertTextComparisonExpr(
179179
}
180180
colNameStr := sqlparser.String(colName)
181181

182-
value, err := query.ParseSqlValue(sqlparser.String(expr.Right))
183-
if err != nil {
184-
return nil, err
182+
valueExpr, ok := expr.Right.(*unsafeSQLString)
183+
if !ok {
184+
return nil, query.NewConverterError(
185+
"%s: unexpected value type (expected string, got %s)",
186+
query.InvalidExpressionErrMessage,
187+
sqlparser.String(expr.Right),
188+
)
185189
}
186-
187-
var ftsQuery string
188-
switch v := value.(type) {
189-
case string:
190-
ftsQuery = fmt.Sprintf(`%s:(%s)`, colNameStr, v)
191-
default:
190+
tokens := tokenizeTextQueryString(valueExpr.Val)
191+
if len(tokens) == 0 {
192192
return nil, query.NewConverterError(
193-
"%s: unexpected value type %T",
193+
"%s: unexpected value for Text type search attribute (no tokens found in %s)",
194194
query.InvalidExpressionErrMessage,
195-
expr,
195+
sqlparser.String(expr.Right),
196196
)
197197
}
198+
ftsQuery := fmt.Sprintf("%s:(%s)", colNameStr, strings.Join(tokens, " OR "))
198199

199200
var oper string
200201
switch expr.Operator {

common/persistence/visibility/store/sql/query_converter_util.go

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package sql
2626

2727
import (
28+
"strings"
2829
"time"
2930

3031
"github.com/xwb1989/sqlparser"
@@ -94,3 +95,16 @@ func getMaxDatetimeValue() time.Time {
9495
t, _ := time.Parse(time.RFC3339, "9999-12-31T23:59:59Z")
9596
return t
9697
}
98+
99+
// Simple tokenizer by spaces. It's a temporary solution as it doesn't cover tokenizer used by
100+
// PostgreSQL or SQLite.
101+
func tokenizeTextQueryString(s string) []string {
102+
tokens := strings.Split(s, " ")
103+
nonEmptyTokens := make([]string, 0, len(tokens))
104+
for _, token := range tokens {
105+
if token != "" {
106+
nonEmptyTokens = append(nonEmptyTokens, token)
107+
}
108+
}
109+
return nonEmptyTokens
110+
}

0 commit comments

Comments
 (0)