Skip to content

Commit 77a4487

Browse files
authored
Fix MySQL visibility indexes with close_time column (#3927)
1 parent 75c05a2 commit 77a4487

File tree

7 files changed

+74
-51
lines changed

7 files changed

+74
-51
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type (
5555
) (string, []any)
5656

5757
getDatetimeFormat() string
58+
59+
getCoalesceCloseTimeExpr() sqlparser.Expr
5860
}
5961

6062
QueryConverter struct {
@@ -347,7 +349,7 @@ func (c *QueryConverter) convertColName(
347349
}
348350
var newExpr sqlparser.Expr = newColName(searchattribute.GetSqlDbColName(saFieldName))
349351
if saAlias == searchattribute.CloseTime {
350-
newExpr = getCoalesceCloseTimeExpr(c.getDatetimeFormat())
352+
newExpr = c.getCoalesceCloseTimeExpr()
351353
}
352354
*exprRef = newExpr
353355
return saAlias, saFieldName, nil

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

+19-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ type (
5959
mysqlQueryConverter struct{}
6060
)
6161

62-
var convertTypeJSON = &sqlparser.ConvertType{Type: "json"}
62+
var (
63+
convertTypeDatetime = &sqlparser.ConvertType{Type: "datetime"}
64+
convertTypeJSON = &sqlparser.ConvertType{Type: "json"}
65+
)
6366

6467
var _ sqlparser.Expr = (*castExpr)(nil)
6568
var _ sqlparser.Expr = (*memberOfExpr)(nil)
@@ -96,6 +99,17 @@ func (c *mysqlQueryConverter) getDatetimeFormat() string {
9699
return "2006-01-02 15:04:05.999999"
97100
}
98101

102+
func (c *mysqlQueryConverter) getCoalesceCloseTimeExpr() sqlparser.Expr {
103+
return newFuncExpr(
104+
coalesceFuncName,
105+
newColName(searchattribute.GetSqlDbColName(searchattribute.CloseTime)),
106+
&castExpr{
107+
Value: newUnsafeSQLString(maxDatetimeValue.Format(c.getDatetimeFormat())),
108+
Type: convertTypeDatetime,
109+
},
110+
)
111+
}
112+
99113
func (c *mysqlQueryConverter) convertKeywordListComparisonExpr(
100114
expr *sqlparser.ComparisonExpr,
101115
) (sqlparser.Expr, error) {
@@ -201,12 +215,12 @@ func (c *mysqlQueryConverter) buildSelectStmt(
201215
whereClauses,
202216
fmt.Sprintf(
203217
"((%s = ? AND %s = ? AND %s > ?) OR (%s = ? AND %s < ?) OR %s < ?)",
204-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
218+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
205219
searchattribute.GetSqlDbColName(searchattribute.StartTime),
206220
searchattribute.GetSqlDbColName(searchattribute.RunID),
207-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
221+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
208222
searchattribute.GetSqlDbColName(searchattribute.StartTime),
209-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
223+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
210224
),
211225
)
212226
queryArgs = append(
@@ -234,7 +248,7 @@ func (c *mysqlQueryConverter) buildSelectStmt(
234248
searchattribute.GetSqlDbColName(searchattribute.NamespaceID),
235249
searchattribute.GetSqlDbColName(searchattribute.RunID),
236250
strings.Join(whereClauses, " AND "),
237-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
251+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
238252
searchattribute.GetSqlDbColName(searchattribute.StartTime),
239253
searchattribute.GetSqlDbColName(searchattribute.RunID),
240254
), queryArgs

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ func (c *pgQueryConverter) getDatetimeFormat() string {
8080
return "2006-01-02 15:04:05.999999"
8181
}
8282

83+
func (c *pgQueryConverter) getCoalesceCloseTimeExpr() sqlparser.Expr {
84+
return newFuncExpr(
85+
coalesceFuncName,
86+
newColName(searchattribute.GetSqlDbColName(searchattribute.CloseTime)),
87+
newUnsafeSQLString(maxDatetimeValue.Format(c.getDatetimeFormat())),
88+
)
89+
}
90+
8391
func (c *pgQueryConverter) convertKeywordListComparisonExpr(
8492
expr *sqlparser.ComparisonExpr,
8593
) (sqlparser.Expr, error) {
@@ -197,12 +205,12 @@ func (c *pgQueryConverter) buildSelectStmt(
197205
whereClauses,
198206
fmt.Sprintf(
199207
"((%s = ? AND %s = ? AND %s > ?) OR (%s = ? AND %s < ?) OR %s < ?)",
200-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
208+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
201209
searchattribute.GetSqlDbColName(searchattribute.StartTime),
202210
searchattribute.GetSqlDbColName(searchattribute.RunID),
203-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
211+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
204212
searchattribute.GetSqlDbColName(searchattribute.StartTime),
205-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
213+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
206214
),
207215
)
208216
queryArgs = append(
@@ -226,7 +234,7 @@ func (c *pgQueryConverter) buildSelectStmt(
226234
LIMIT ?`,
227235
strings.Join(sqlplugin.DbFields, ", "),
228236
strings.Join(whereClauses, " AND "),
229-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
237+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
230238
searchattribute.GetSqlDbColName(searchattribute.StartTime),
231239
searchattribute.GetSqlDbColName(searchattribute.RunID),
232240
), queryArgs

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ func (c *sqliteQueryConverter) getDatetimeFormat() string {
6464
return "2006-01-02 15:04:05.999999-07:00"
6565
}
6666

67+
func (c *sqliteQueryConverter) getCoalesceCloseTimeExpr() sqlparser.Expr {
68+
return newFuncExpr(
69+
coalesceFuncName,
70+
newColName(searchattribute.GetSqlDbColName(searchattribute.CloseTime)),
71+
newUnsafeSQLString(maxDatetimeValue.Format(c.getDatetimeFormat())),
72+
)
73+
}
74+
6775
//nolint:revive // cyclomatic complexity 17 (> 15)
6876
func (c *sqliteQueryConverter) convertKeywordListComparisonExpr(
6977
expr *sqlparser.ComparisonExpr,
@@ -233,12 +241,12 @@ func (c *sqliteQueryConverter) buildSelectStmt(
233241
whereClauses,
234242
fmt.Sprintf(
235243
"((%s = ? AND %s = ? AND %s > ?) OR (%s = ? AND %s < ?) OR %s < ?)",
236-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
244+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
237245
searchattribute.GetSqlDbColName(searchattribute.StartTime),
238246
searchattribute.GetSqlDbColName(searchattribute.RunID),
239-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
247+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
240248
searchattribute.GetSqlDbColName(searchattribute.StartTime),
241-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
249+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
242250
),
243251
)
244252
queryArgs = append(
@@ -262,7 +270,7 @@ func (c *sqliteQueryConverter) buildSelectStmt(
262270
LIMIT ?`,
263271
strings.Join(sqlplugin.DbFields, ", "),
264272
strings.Join(whereClauses, " AND "),
265-
sqlparser.String(getCoalesceCloseTimeExpr(c.getDatetimeFormat())),
273+
sqlparser.String(c.getCoalesceCloseTimeExpr()),
266274
searchattribute.GetSqlDbColName(searchattribute.StartTime),
267275
searchattribute.GetSqlDbColName(searchattribute.RunID),
268276
), queryArgs

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

-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"time"
2929

3030
"github.com/xwb1989/sqlparser"
31-
"go.temporal.io/server/common/searchattribute"
3231
)
3332

3433
type (
@@ -95,11 +94,3 @@ func getMaxDatetimeValue() time.Time {
9594
t, _ := time.Parse(time.RFC3339, "9999-12-31T23:59:59Z")
9695
return t
9796
}
98-
99-
func getCoalesceCloseTimeExpr(format string) sqlparser.Expr {
100-
return newFuncExpr(
101-
coalesceFuncName,
102-
newColName(searchattribute.GetSqlDbColName(searchattribute.CloseTime)),
103-
newUnsafeSQLString(maxDatetimeValue.Format(format)),
104-
)
105-
}

schema/mysql/v8/visibility/schema.sql

+14-14
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ CREATE TABLE executions_visibility (
4040
PRIMARY KEY (namespace_id, run_id)
4141
);
4242

43-
CREATE INDEX default_idx ON executions_visibility (namespace_id, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
44-
CREATE INDEX by_execution_time ON executions_visibility (namespace_id, execution_time, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
45-
CREATE INDEX by_workflow_id ON executions_visibility (namespace_id, workflow_id, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
46-
CREATE INDEX by_workflow_type ON executions_visibility (namespace_id, workflow_type_name, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
47-
CREATE INDEX by_status ON executions_visibility (namespace_id, status, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
48-
CREATE INDEX by_history_length ON executions_visibility (namespace_id, history_length, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
49-
CREATE INDEX by_task_queue ON executions_visibility (namespace_id, task_queue, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
43+
CREATE INDEX default_idx ON executions_visibility (namespace_id, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
44+
CREATE INDEX by_execution_time ON executions_visibility (namespace_id, execution_time, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
45+
CREATE INDEX by_workflow_id ON executions_visibility (namespace_id, workflow_id, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
46+
CREATE INDEX by_workflow_type ON executions_visibility (namespace_id, workflow_type_name, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
47+
CREATE INDEX by_status ON executions_visibility (namespace_id, status, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
48+
CREATE INDEX by_history_length ON executions_visibility (namespace_id, history_length, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
49+
CREATE INDEX by_task_queue ON executions_visibility (namespace_id, task_queue, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
5050

5151
-- Indexes for the predefined search attributes
52-
CREATE INDEX by_temporal_change_version ON executions_visibility (namespace_id, (CAST(TemporalChangeVersion AS CHAR(255) ARRAY)), (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
53-
CREATE INDEX by_binary_checksums ON executions_visibility (namespace_id, (CAST(BinaryChecksums AS CHAR(255) ARRAY)), (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
54-
CREATE INDEX by_batcher_user ON executions_visibility (namespace_id, BatcherUser, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
55-
CREATE INDEX by_temporal_scheduled_start_time ON executions_visibility (namespace_id, TemporalScheduledStartTime, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
56-
CREATE INDEX by_temporal_scheduled_by_id ON executions_visibility (namespace_id, TemporalScheduledById, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
57-
CREATE INDEX by_temporal_schedule_paused ON executions_visibility (namespace_id, TemporalSchedulePaused, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
58-
CREATE INDEX by_temporal_namespace_division ON executions_visibility (namespace_id, TemporalNamespaceDivision, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
52+
CREATE INDEX by_temporal_change_version ON executions_visibility (namespace_id, (CAST(TemporalChangeVersion AS CHAR(255) ARRAY)), (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
53+
CREATE INDEX by_binary_checksums ON executions_visibility (namespace_id, (CAST(BinaryChecksums AS CHAR(255) ARRAY)), (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
54+
CREATE INDEX by_batcher_user ON executions_visibility (namespace_id, BatcherUser, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
55+
CREATE INDEX by_temporal_scheduled_start_time ON executions_visibility (namespace_id, TemporalScheduledStartTime, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
56+
CREATE INDEX by_temporal_scheduled_by_id ON executions_visibility (namespace_id, TemporalScheduledById, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
57+
CREATE INDEX by_temporal_schedule_paused ON executions_visibility (namespace_id, TemporalSchedulePaused, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
58+
CREATE INDEX by_temporal_namespace_division ON executions_visibility (namespace_id, TemporalNamespaceDivision, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
5959

6060

6161
CREATE TABLE custom_search_attributes (

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ DROP INDEX by_status_by_close_time ON executions_visibility;
2525
DROP INDEX by_close_time_by_status ON executions_visibility;
2626

2727
-- Create new indexes
28-
CREATE INDEX default_idx ON executions_visibility (namespace_id, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
29-
CREATE INDEX by_execution_time ON executions_visibility (namespace_id, execution_time, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
30-
CREATE INDEX by_workflow_id ON executions_visibility (namespace_id, workflow_id, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
31-
CREATE INDEX by_workflow_type ON executions_visibility (namespace_id, workflow_type_name, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
32-
CREATE INDEX by_status ON executions_visibility (namespace_id, status, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
33-
CREATE INDEX by_history_length ON executions_visibility (namespace_id, history_length, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
34-
CREATE INDEX by_task_queue ON executions_visibility (namespace_id, task_queue, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
28+
CREATE INDEX default_idx ON executions_visibility (namespace_id, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
29+
CREATE INDEX by_execution_time ON executions_visibility (namespace_id, execution_time, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
30+
CREATE INDEX by_workflow_id ON executions_visibility (namespace_id, workflow_id, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
31+
CREATE INDEX by_workflow_type ON executions_visibility (namespace_id, workflow_type_name, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
32+
CREATE INDEX by_status ON executions_visibility (namespace_id, status, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
33+
CREATE INDEX by_history_length ON executions_visibility (namespace_id, history_length, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
34+
CREATE INDEX by_task_queue ON executions_visibility (namespace_id, task_queue, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
3535

3636
-- Indexes for the predefined search attributes
37-
CREATE INDEX by_temporal_change_version ON executions_visibility (namespace_id, (CAST(TemporalChangeVersion AS CHAR(255) ARRAY)), (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
38-
CREATE INDEX by_binary_checksums ON executions_visibility (namespace_id, (CAST(BinaryChecksums AS CHAR(255) ARRAY)), (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
39-
CREATE INDEX by_batcher_user ON executions_visibility (namespace_id, BatcherUser, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
40-
CREATE INDEX by_temporal_scheduled_start_time ON executions_visibility (namespace_id, TemporalScheduledStartTime, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
41-
CREATE INDEX by_temporal_scheduled_by_id ON executions_visibility (namespace_id, TemporalScheduledById, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
42-
CREATE INDEX by_temporal_schedule_paused ON executions_visibility (namespace_id, TemporalSchedulePaused, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
43-
CREATE INDEX by_temporal_namespace_division ON executions_visibility (namespace_id, TemporalNamespaceDivision, (COALESCE(close_time, '9999-12-31 23:59:59')) DESC, start_time DESC, run_id);
37+
CREATE INDEX by_temporal_change_version ON executions_visibility (namespace_id, (CAST(TemporalChangeVersion AS CHAR(255) ARRAY)), (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
38+
CREATE INDEX by_binary_checksums ON executions_visibility (namespace_id, (CAST(BinaryChecksums AS CHAR(255) ARRAY)), (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
39+
CREATE INDEX by_batcher_user ON executions_visibility (namespace_id, BatcherUser, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
40+
CREATE INDEX by_temporal_scheduled_start_time ON executions_visibility (namespace_id, TemporalScheduledStartTime, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
41+
CREATE INDEX by_temporal_scheduled_by_id ON executions_visibility (namespace_id, TemporalScheduledById, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
42+
CREATE INDEX by_temporal_schedule_paused ON executions_visibility (namespace_id, TemporalSchedulePaused, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
43+
CREATE INDEX by_temporal_namespace_division ON executions_visibility (namespace_id, TemporalNamespaceDivision, (COALESCE(close_time, CAST('9999-12-31 23:59:59' AS DATETIME))) DESC, start_time DESC, run_id);
4444

4545

4646
-- Custom search attributes

0 commit comments

Comments
 (0)