Skip to content

Commit 304bc98

Browse files
authored
Fix MySQL visibility index for full text search (#3921)
1 parent 158a70e commit 304bc98

File tree

3 files changed

+13
-34
lines changed

3 files changed

+13
-34
lines changed

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

+7-28
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ type (
5656
JSONDoc2 sqlparser.Expr
5757
}
5858

59-
mysqlQueryConverter struct {
60-
namespaceID namespace.ID
61-
}
59+
mysqlQueryConverter struct{}
6260
)
6361

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

7068
var _ pluginQueryConverter = (*mysqlQueryConverter)(nil)
7169

72-
const (
73-
mysqlCustomSearchAttributesTableName = "custom_search_attributes"
74-
)
75-
7670
func (node *castExpr) Format(buf *sqlparser.TrackedBuffer) {
7771
buf.Myprintf("cast(%v as %v)", node.Value, node.Type)
7872
}
@@ -91,7 +85,7 @@ func newMySQLQueryConverter(
9185
saMapper searchattribute.Mapper,
9286
) *QueryConverter {
9387
return newQueryConverterInternal(
94-
&mysqlQueryConverter{namespaceID: request.NamespaceID},
88+
&mysqlQueryConverter{},
9589
request,
9690
saTypeMap,
9791
saMapper,
@@ -158,7 +152,7 @@ func (c *mysqlQueryConverter) convertToJsonOverlapsExpr(
158152
return &jsonOverlapsExpr{
159153
JSONDoc1: expr.Left,
160154
JSONDoc2: &castExpr{
161-
Value: sqlparser.NewStrVal(jsonValue),
155+
Value: newUnsafeSQLString(string(jsonValue)),
162156
Type: convertTypeJSON,
163157
},
164158
}, nil
@@ -170,27 +164,12 @@ func (c *mysqlQueryConverter) convertTextComparisonExpr(
170164
if !isSupportedTextOperator(expr.Operator) {
171165
return nil, query.NewConverterError("invalid query")
172166
}
173-
valueExpr, ok := expr.Right.(*unsafeSQLString)
174-
if !ok {
175-
return nil, query.NewConverterError("invalid query")
176-
}
177-
valueExpr.Val = fmt.Sprintf("%s %s", c.namespaceID, valueExpr.Val)
178167
// build the following expression:
179-
// `match (custom_search_attributes.namespace_id, {expr.Left}) against ({expr.Right} in natural language mode)`
168+
// `match ({expr.Left}) against ({expr.Right} in natural language mode)`
180169
var newExpr sqlparser.Expr = &sqlparser.MatchExpr{
181-
Columns: []sqlparser.SelectExpr{
182-
&sqlparser.AliasedExpr{
183-
Expr: &sqlparser.ColName{
184-
Name: sqlparser.NewColIdent(searchattribute.GetSqlDbColName(searchattribute.NamespaceID)),
185-
Qualifier: sqlparser.TableName{
186-
Name: sqlparser.NewTableIdent(mysqlCustomSearchAttributesTableName),
187-
},
188-
},
189-
},
190-
&sqlparser.AliasedExpr{Expr: expr.Left},
191-
},
192-
Expr: expr.Right,
193-
Option: sqlparser.NaturalLanguageModeStr,
170+
Columns: []sqlparser.SelectExpr{&sqlparser.AliasedExpr{Expr: expr.Left}},
171+
Expr: expr.Right,
172+
Option: sqlparser.NaturalLanguageModeStr,
194173
}
195174
if expr.Operator == sqlparser.NotEqualStr {
196175
newExpr = &sqlparser.NotExpr{Expr: newExpr}

schema/mysql/v8/visibility/schema.sql

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ CREATE INDEX by_keyword_07 ON custom_search_attributes (namespace_id, Key
135135
CREATE INDEX by_keyword_08 ON custom_search_attributes (namespace_id, Keyword08);
136136
CREATE INDEX by_keyword_09 ON custom_search_attributes (namespace_id, Keyword09);
137137
CREATE INDEX by_keyword_10 ON custom_search_attributes (namespace_id, Keyword10);
138-
CREATE FULLTEXT INDEX by_text_01 ON custom_search_attributes (namespace_id, Text01);
139-
CREATE FULLTEXT INDEX by_text_02 ON custom_search_attributes (namespace_id, Text02);
140-
CREATE FULLTEXT INDEX by_text_03 ON custom_search_attributes (namespace_id, Text03);
138+
CREATE FULLTEXT INDEX by_text_01 ON custom_search_attributes (Text01);
139+
CREATE FULLTEXT INDEX by_text_02 ON custom_search_attributes (Text02);
140+
CREATE FULLTEXT INDEX by_text_03 ON custom_search_attributes (Text03);
141141
CREATE INDEX by_keyword_list_01 ON custom_search_attributes (namespace_id, (CAST(KeywordList01 AS CHAR(255) ARRAY)));
142142
CREATE INDEX by_keyword_list_02 ON custom_search_attributes (namespace_id, (CAST(KeywordList02 AS CHAR(255) ARRAY)));
143143
CREATE INDEX by_keyword_list_03 ON custom_search_attributes (namespace_id, (CAST(KeywordList03 AS CHAR(255) ARRAY)));

schema/mysql/v8/visibility/versioned/v1.2/advanced_visibility.sql

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ CREATE INDEX by_keyword_07 ON custom_search_attributes (namespace_id, Key
121121
CREATE INDEX by_keyword_08 ON custom_search_attributes (namespace_id, Keyword08);
122122
CREATE INDEX by_keyword_09 ON custom_search_attributes (namespace_id, Keyword09);
123123
CREATE INDEX by_keyword_10 ON custom_search_attributes (namespace_id, Keyword10);
124-
CREATE FULLTEXT INDEX by_text_01 ON custom_search_attributes (namespace_id, Text01);
125-
CREATE FULLTEXT INDEX by_text_02 ON custom_search_attributes (namespace_id, Text02);
126-
CREATE FULLTEXT INDEX by_text_03 ON custom_search_attributes (namespace_id, Text03);
124+
CREATE FULLTEXT INDEX by_text_01 ON custom_search_attributes (Text01);
125+
CREATE FULLTEXT INDEX by_text_02 ON custom_search_attributes (Text02);
126+
CREATE FULLTEXT INDEX by_text_03 ON custom_search_attributes (Text03);
127127
CREATE INDEX by_keyword_list_01 ON custom_search_attributes (namespace_id, (CAST(KeywordList01 AS CHAR(255) ARRAY)));
128128
CREATE INDEX by_keyword_list_02 ON custom_search_attributes (namespace_id, (CAST(KeywordList02 AS CHAR(255) ARRAY)));
129129
CREATE INDEX by_keyword_list_03 ON custom_search_attributes (namespace_id, (CAST(KeywordList03 AS CHAR(255) ARRAY)));

0 commit comments

Comments
 (0)