Skip to content

Commit

Permalink
Fix visibility full text search query in MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigozhou committed Feb 8, 2023
1 parent 793dd7e commit 030da3b
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions common/persistence/visibility/store/sql/query_converter_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ type (
JSONDoc2 sqlparser.Expr
}

mysqlQueryConverter struct{}
mysqlQueryConverter struct {
namespaceID namespace.ID
}
)

var convertTypeJSON = &sqlparser.ConvertType{Type: "json"}
Expand All @@ -67,6 +69,10 @@ var _ sqlparser.Expr = (*jsonOverlapsExpr)(nil)

var _ pluginQueryConverter = (*mysqlQueryConverter)(nil)

const (
mysqlCustomSearchAttributesTableName = "custom_search_attributes"
)

func (node *castExpr) Format(buf *sqlparser.TrackedBuffer) {
buf.Myprintf("cast(%v as %v)", node.Value, node.Type)
}
Expand All @@ -85,7 +91,7 @@ func newMySQLQueryConverter(
saMapper searchattribute.Mapper,
) *QueryConverter {
return newQueryConverterInternal(
&mysqlQueryConverter{},
&mysqlQueryConverter{namespaceID: request.NamespaceID},
request,
saTypeMap,
saMapper,
Expand Down Expand Up @@ -164,10 +170,27 @@ func (c *mysqlQueryConverter) convertTextComparisonExpr(
if !isSupportedTextOperator(expr.Operator) {
return nil, query.NewConverterError("invalid query")
}
valueExpr, ok := expr.Right.(*unsafeSQLString)
if !ok {
return nil, query.NewConverterError("invalid query")
}
valueExpr.Val = fmt.Sprintf("%s %s", c.namespaceID, valueExpr.Val)
// build the following expression:
// `match (custom_search_attributes.namespace_id, {expr.Left}) against ({expr.Right} in natural language mode)`
var newExpr sqlparser.Expr = &sqlparser.MatchExpr{
Columns: []sqlparser.SelectExpr{&sqlparser.AliasedExpr{Expr: expr.Left}},
Expr: expr.Right,
Option: sqlparser.NaturalLanguageModeStr,
Columns: []sqlparser.SelectExpr{
&sqlparser.AliasedExpr{
Expr: &sqlparser.ColName{
Name: sqlparser.NewColIdent(searchattribute.GetSqlDbColName(searchattribute.NamespaceID)),
Qualifier: sqlparser.TableName{
Name: sqlparser.NewTableIdent(mysqlCustomSearchAttributesTableName),
},
},
},
&sqlparser.AliasedExpr{Expr: expr.Left},
},
Expr: expr.Right,
Option: sqlparser.NaturalLanguageModeStr,
}
if expr.Operator == sqlparser.NotEqualStr {
newExpr = &sqlparser.NotExpr{Expr: newExpr}
Expand Down

0 comments on commit 030da3b

Please sign in to comment.