Skip to content

Commit

Permalink
Fix MySQL visibility index for full text search (temporalio#3921)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigozhou authored Feb 9, 2023
1 parent 158a70e commit 304bc98
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
35 changes: 7 additions & 28 deletions common/persistence/visibility/store/sql/query_converter_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ type (
JSONDoc2 sqlparser.Expr
}

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

var convertTypeJSON = &sqlparser.ConvertType{Type: "json"}
Expand All @@ -69,10 +67,6 @@ 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 @@ -91,7 +85,7 @@ func newMySQLQueryConverter(
saMapper searchattribute.Mapper,
) *QueryConverter {
return newQueryConverterInternal(
&mysqlQueryConverter{namespaceID: request.NamespaceID},
&mysqlQueryConverter{},
request,
saTypeMap,
saMapper,
Expand Down Expand Up @@ -158,7 +152,7 @@ func (c *mysqlQueryConverter) convertToJsonOverlapsExpr(
return &jsonOverlapsExpr{
JSONDoc1: expr.Left,
JSONDoc2: &castExpr{
Value: sqlparser.NewStrVal(jsonValue),
Value: newUnsafeSQLString(string(jsonValue)),
Type: convertTypeJSON,
},
}, nil
Expand All @@ -170,27 +164,12 @@ 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)`
// `match ({expr.Left}) against ({expr.Right} in natural language mode)`
var newExpr sqlparser.Expr = &sqlparser.MatchExpr{
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,
Columns: []sqlparser.SelectExpr{&sqlparser.AliasedExpr{Expr: expr.Left}},
Expr: expr.Right,
Option: sqlparser.NaturalLanguageModeStr,
}
if expr.Operator == sqlparser.NotEqualStr {
newExpr = &sqlparser.NotExpr{Expr: newExpr}
Expand Down
6 changes: 3 additions & 3 deletions schema/mysql/v8/visibility/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ CREATE INDEX by_keyword_07 ON custom_search_attributes (namespace_id, Key
CREATE INDEX by_keyword_08 ON custom_search_attributes (namespace_id, Keyword08);
CREATE INDEX by_keyword_09 ON custom_search_attributes (namespace_id, Keyword09);
CREATE INDEX by_keyword_10 ON custom_search_attributes (namespace_id, Keyword10);
CREATE FULLTEXT INDEX by_text_01 ON custom_search_attributes (namespace_id, Text01);
CREATE FULLTEXT INDEX by_text_02 ON custom_search_attributes (namespace_id, Text02);
CREATE FULLTEXT INDEX by_text_03 ON custom_search_attributes (namespace_id, Text03);
CREATE FULLTEXT INDEX by_text_01 ON custom_search_attributes (Text01);
CREATE FULLTEXT INDEX by_text_02 ON custom_search_attributes (Text02);
CREATE FULLTEXT INDEX by_text_03 ON custom_search_attributes (Text03);
CREATE INDEX by_keyword_list_01 ON custom_search_attributes (namespace_id, (CAST(KeywordList01 AS CHAR(255) ARRAY)));
CREATE INDEX by_keyword_list_02 ON custom_search_attributes (namespace_id, (CAST(KeywordList02 AS CHAR(255) ARRAY)));
CREATE INDEX by_keyword_list_03 ON custom_search_attributes (namespace_id, (CAST(KeywordList03 AS CHAR(255) ARRAY)));
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ CREATE INDEX by_keyword_07 ON custom_search_attributes (namespace_id, Key
CREATE INDEX by_keyword_08 ON custom_search_attributes (namespace_id, Keyword08);
CREATE INDEX by_keyword_09 ON custom_search_attributes (namespace_id, Keyword09);
CREATE INDEX by_keyword_10 ON custom_search_attributes (namespace_id, Keyword10);
CREATE FULLTEXT INDEX by_text_01 ON custom_search_attributes (namespace_id, Text01);
CREATE FULLTEXT INDEX by_text_02 ON custom_search_attributes (namespace_id, Text02);
CREATE FULLTEXT INDEX by_text_03 ON custom_search_attributes (namespace_id, Text03);
CREATE FULLTEXT INDEX by_text_01 ON custom_search_attributes (Text01);
CREATE FULLTEXT INDEX by_text_02 ON custom_search_attributes (Text02);
CREATE FULLTEXT INDEX by_text_03 ON custom_search_attributes (Text03);
CREATE INDEX by_keyword_list_01 ON custom_search_attributes (namespace_id, (CAST(KeywordList01 AS CHAR(255) ARRAY)));
CREATE INDEX by_keyword_list_02 ON custom_search_attributes (namespace_id, (CAST(KeywordList02 AS CHAR(255) ARRAY)));
CREATE INDEX by_keyword_list_03 ON custom_search_attributes (namespace_id, (CAST(KeywordList03 AS CHAR(255) ARRAY)));

0 comments on commit 304bc98

Please sign in to comment.