Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MySQL visibility index for full text search #3921

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, now I understand why you added it here. Yes, apparently it won't work for FTS fields.

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)));