Skip to content

Commit 457d6b9

Browse files
committed
Update search attribute defs package and add CHASM visibility manager interface
1 parent f6a4b9a commit 457d6b9

File tree

71 files changed

+1062
-960
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1062
-960
lines changed

chasm/test_visibility.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package chasm
2+
3+
import enumspb "go.temporal.io/api/enums/v1"
4+
5+
// NewVisibilitySearchAttributesMapper creates a new VisibilitySearchAttributesMapper.
6+
// For testing only.
7+
func NewVisibilitySearchAttributesMapper(
8+
fieldToAlias map[string]string,
9+
saTypeMap map[string]enumspb.IndexedValueType,
10+
) *VisibilitySearchAttributesMapper {
11+
aliasToField := make(map[string]string, len(fieldToAlias))
12+
for field, alias := range fieldToAlias {
13+
aliasToField[alias] = field
14+
}
15+
return &VisibilitySearchAttributesMapper{
16+
aliasToField: aliasToField,
17+
fieldToAlias: fieldToAlias,
18+
saTypeMap: saTypeMap,
19+
}
20+
}

chasm/visibility.go

Lines changed: 86 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package chasm
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
commonpb "go.temporal.io/api/common/v1"
89
enumspb "go.temporal.io/api/enums/v1"
@@ -39,23 +40,6 @@ type VisibilitySearchAttributesMapper struct {
3940
saTypeMap map[string]enumspb.IndexedValueType
4041
}
4142

42-
// NewVisibilitySearchAttributesMapper creates a new VisibilitySearchAttributesMapper for testing.
43-
// fieldToAlias maps field names to their aliases (e.g., "ChasmField1" -> "chasm_alias1").
44-
func NewVisibilitySearchAttributesMapper(
45-
fieldToAlias map[string]string,
46-
saTypeMap map[string]enumspb.IndexedValueType,
47-
) *VisibilitySearchAttributesMapper {
48-
aliasToField := make(map[string]string, len(fieldToAlias))
49-
for field, alias := range fieldToAlias {
50-
aliasToField[alias] = field
51-
}
52-
return &VisibilitySearchAttributesMapper{
53-
aliasToField: aliasToField,
54-
fieldToAlias: fieldToAlias,
55-
saTypeMap: saTypeMap,
56-
}
57-
}
58-
5943
// Alias returns the alias for a given field.
6044
func (v *VisibilitySearchAttributesMapper) Alias(field string) (string, error) {
6145
if v == nil {
@@ -89,29 +73,104 @@ func (v *VisibilitySearchAttributesMapper) SATypeMap() map[string]enumspb.Indexe
8973
}
9074

9175
// SearchAttributeMap wraps search attribute values with type-safe access.
92-
type SearchAttributeMap struct {
76+
type SearchAttributesMap struct {
9377
values map[string]VisibilityValue
9478
}
9579

9680
// NewSearchAttributeMap creates a new SearchAttributeMap from raw values.
97-
func NewSearchAttributeMap(values map[string]VisibilityValue) SearchAttributeMap {
98-
return SearchAttributeMap{values: values}
81+
func NewSearchAttributesMap(values map[string]VisibilityValue) SearchAttributesMap {
82+
return SearchAttributesMap{values: values}
83+
}
84+
85+
// GetBool returns the boolean value for a given SearchAttributeBool. If not found or map is nil, second parameter is false.
86+
func (m SearchAttributesMap) GetBool(sa SearchAttributeBool) (bool, bool) {
87+
if m.values == nil {
88+
return false, false
89+
}
90+
91+
alias := sa.definition().alias
92+
boolValue, ok := m.values[alias].(VisibilityValueBool)
93+
if !ok {
94+
return false, false
95+
}
96+
97+
return bool(boolValue), true
98+
}
99+
100+
// GetInt returns the int value for a given SearchAttributeInt. If not found or map is nil, second parameter is false.
101+
func (m SearchAttributesMap) GetInt(sa SearchAttributeInt) (int, bool) {
102+
103+
if m.values == nil {
104+
return 0, false
105+
}
106+
107+
alias := sa.definition().alias
108+
intValue, ok := m.values[alias].(VisibilityValueInt)
109+
if !ok {
110+
return 0, false
111+
}
112+
113+
return int(intValue), true
114+
}
115+
116+
// GetDouble returns the double value for a given SearchAttributeDouble. If not found or map is nil, second parameter is false.
117+
func (m SearchAttributesMap) GetDouble(sa SearchAttributeDouble) (float64, bool) {
118+
if m.values == nil {
119+
return 0, false
120+
}
121+
122+
alias := sa.definition().alias
123+
doubleValue, ok := m.values[alias].(VisibilityValueFloat64)
124+
if !ok {
125+
return 0, false
126+
}
127+
128+
return float64(doubleValue), true
129+
}
130+
131+
// GetString returns the string value for a given SearchAttributeKeyword. If not found or map is nil, second parameter is false.
132+
func (m SearchAttributesMap) GetString(sa SearchAttributeKeyword) (string, bool) {
133+
if m.values == nil {
134+
return "", false
135+
}
136+
137+
alias := sa.definition().alias
138+
stringValue, ok := m.values[alias].(VisibilityValueString)
139+
if !ok {
140+
return "", false
141+
}
142+
143+
return string(stringValue), true
144+
}
145+
146+
// GetTime returns the time value for a given SearchAttributeDateTime. If not found or map is nil, second parameter is false.
147+
func (m SearchAttributesMap) GetTime(sa SearchAttributeDateTime) (time.Time, bool) {
148+
if m.values == nil {
149+
return time.Time{}, false
150+
}
151+
152+
alias := sa.definition().alias
153+
timeValue, ok := m.values[alias].(VisibilityValueTime)
154+
if !ok {
155+
return time.Time{}, false
156+
}
157+
158+
return time.Time(timeValue), true
99159
}
100160

101-
// Value returns the value for a given search attribute, using its alias.
102-
func (m SearchAttributeMap) Value(sa SearchAttribute) (VisibilityValue, error) {
161+
// GetStringList returns the string list value for a given SearchAttributeKeywordList. If not found or map is nil, second parameter is false.
162+
func (m SearchAttributesMap) GetStringList(sa SearchAttributeKeywordList) ([]string, bool) {
103163
if m.values == nil {
104-
return nil, serviceerror.NewInvalidArgument("search attribute map is nil")
164+
return nil, false
105165
}
106166

107167
alias := sa.definition().alias
108-
value, ok := m.values[alias]
168+
keywordListValue, ok := m.values[alias].(VisibilityValueStringSlice)
109169
if !ok {
110-
return nil, serviceerror.NewInvalidArgument(
111-
fmt.Sprintf("search attribute %q not found in map", alias))
170+
return nil, false
112171
}
113172

114-
return value, nil
173+
return []string(keywordListValue), true
115174
}
116175

117176
type Visibility struct {

common/persistence/sql/sqlplugin/mysql/query_converter.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"go.temporal.io/server/common/namespace"
1111
"go.temporal.io/server/common/persistence/sql/sqlplugin"
1212
"go.temporal.io/server/common/persistence/visibility/store/query"
13-
"go.temporal.io/server/common/searchattribute"
13+
"go.temporal.io/server/common/searchattribute/defs"
1414
)
1515

1616
var maxDatetime = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)
@@ -143,7 +143,7 @@ func (c *queryConverter) BuildSelectStmt(
143143
whereClauses,
144144
fmt.Sprintf(
145145
"%s = '%s'",
146-
searchattribute.GetSqlDbColName(searchattribute.NamespaceID),
146+
defs.GetSqlDbColName(defs.NamespaceID),
147147
namespaceID,
148148
),
149149
)
@@ -160,10 +160,10 @@ func (c *queryConverter) BuildSelectStmt(
160160
fmt.Sprintf(
161161
"((%s = ? AND %s = ? AND %s > ?) OR (%s = ? AND %s < ?) OR %s < ?)",
162162
sqlparser.String(c.GetCoalesceCloseTimeExpr()),
163-
searchattribute.GetSqlDbColName(searchattribute.StartTime),
164-
searchattribute.GetSqlDbColName(searchattribute.RunID),
163+
defs.GetSqlDbColName(defs.StartTime),
164+
defs.GetSqlDbColName(defs.RunID),
165165
sqlparser.String(c.GetCoalesceCloseTimeExpr()),
166-
searchattribute.GetSqlDbColName(searchattribute.StartTime),
166+
defs.GetSqlDbColName(defs.StartTime),
167167
sqlparser.String(c.GetCoalesceCloseTimeExpr()),
168168
),
169169
)
@@ -186,12 +186,12 @@ func (c *queryConverter) BuildSelectStmt(
186186
stmt := fmt.Sprintf(
187187
`SELECT %s FROM executions_visibility ev LEFT JOIN custom_search_attributes USING (%s, %s) WHERE %s ORDER BY %s DESC, %s DESC, %s LIMIT ?`,
188188
strings.Join(dbFields, ", "),
189-
searchattribute.GetSqlDbColName(searchattribute.NamespaceID),
190-
searchattribute.GetSqlDbColName(searchattribute.RunID),
189+
defs.GetSqlDbColName(defs.NamespaceID),
190+
defs.GetSqlDbColName(defs.RunID),
191191
strings.Join(whereClauses, " AND "),
192192
sqlparser.String(c.GetCoalesceCloseTimeExpr()),
193-
searchattribute.GetSqlDbColName(searchattribute.StartTime),
194-
searchattribute.GetSqlDbColName(searchattribute.RunID),
193+
defs.GetSqlDbColName(defs.StartTime),
194+
defs.GetSqlDbColName(defs.RunID),
195195
)
196196
queryArgs = append(queryArgs, pageSize)
197197

@@ -209,7 +209,7 @@ func (c *queryConverter) BuildCountStmt(
209209
whereClauses,
210210
fmt.Sprintf(
211211
"%s = '%s'",
212-
searchattribute.GetSqlDbColName(searchattribute.NamespaceID),
212+
defs.GetSqlDbColName(defs.NamespaceID),
213213
namespaceID,
214214
),
215215
)
@@ -222,7 +222,7 @@ func (c *queryConverter) BuildCountStmt(
222222

223223
groupBy := make([]string, 0, len(queryParams.GroupBy)+1)
224224
for _, field := range queryParams.GroupBy {
225-
groupBy = append(groupBy, searchattribute.GetSqlDbColName(field.FieldName))
225+
groupBy = append(groupBy, defs.GetSqlDbColName(field.FieldName))
226226
}
227227

228228
groupByClause := ""
@@ -233,8 +233,8 @@ func (c *queryConverter) BuildCountStmt(
233233
return fmt.Sprintf(
234234
`SELECT %s FROM executions_visibility ev LEFT JOIN custom_search_attributes USING (%s, %s) WHERE %s%s`,
235235
strings.Join(append(groupBy, "COUNT(*)"), ", "),
236-
searchattribute.GetSqlDbColName(searchattribute.NamespaceID),
237-
searchattribute.GetSqlDbColName(searchattribute.RunID),
236+
defs.GetSqlDbColName(defs.NamespaceID),
237+
defs.GetSqlDbColName(defs.RunID),
238238
strings.Join(whereClauses, " AND "),
239239
groupByClause,
240240
), nil

common/persistence/sql/sqlplugin/mysql/query_converter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"go.temporal.io/server/common/namespace"
1313
"go.temporal.io/server/common/persistence/sql/sqlplugin"
1414
"go.temporal.io/server/common/persistence/visibility/store/query"
15-
"go.temporal.io/server/common/searchattribute"
15+
"go.temporal.io/server/common/searchattribute/defs"
1616
)
1717

1818
func TestQueryConverter_GetCoalesceCloseTimeExpr(t *testing.T) {
@@ -297,7 +297,7 @@ func TestQueryConverter_BuildCountStmt(t *testing.T) {
297297
Right: query.NewUnsafeSQLString("foo"),
298298
},
299299
groupBy: []*query.SAColumn{
300-
query.NewSAColumn(searchattribute.ExecutionStatus, searchattribute.ExecutionStatus, enumspb.INDEXED_VALUE_TYPE_KEYWORD),
300+
query.NewSAColumn(defs.ExecutionStatus, defs.ExecutionStatus, enumspb.INDEXED_VALUE_TYPE_KEYWORD),
301301
},
302302
stmt: fmt.Sprintf(
303303
"SELECT status, COUNT(*) FROM executions_visibility ev LEFT JOIN custom_search_attributes USING (namespace_id, run_id) WHERE namespace_id = '%s' AND Keyword01 = 'foo' GROUP BY status",

common/persistence/sql/sqlplugin/postgresql/query_converter.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"go.temporal.io/server/common/namespace"
1010
"go.temporal.io/server/common/persistence/sql/sqlplugin"
1111
"go.temporal.io/server/common/persistence/visibility/store/query"
12-
"go.temporal.io/server/common/searchattribute"
12+
"go.temporal.io/server/common/searchattribute/defs"
1313
)
1414

1515
var maxDatetime = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)
@@ -139,7 +139,7 @@ func (c *queryConverter) BuildSelectStmt(
139139
whereClauses,
140140
fmt.Sprintf(
141141
"%s = '%s'",
142-
searchattribute.GetSqlDbColName(searchattribute.NamespaceID),
142+
defs.GetSqlDbColName(defs.NamespaceID),
143143
namespaceID,
144144
),
145145
)
@@ -156,10 +156,10 @@ func (c *queryConverter) BuildSelectStmt(
156156
fmt.Sprintf(
157157
"((%s = ? AND %s = ? AND %s > ?) OR (%s = ? AND %s < ?) OR %s < ?)",
158158
sqlparser.String(c.GetCoalesceCloseTimeExpr()),
159-
searchattribute.GetSqlDbColName(searchattribute.StartTime),
160-
searchattribute.GetSqlDbColName(searchattribute.RunID),
159+
defs.GetSqlDbColName(defs.StartTime),
160+
defs.GetSqlDbColName(defs.RunID),
161161
sqlparser.String(c.GetCoalesceCloseTimeExpr()),
162-
searchattribute.GetSqlDbColName(searchattribute.StartTime),
162+
defs.GetSqlDbColName(defs.StartTime),
163163
sqlparser.String(c.GetCoalesceCloseTimeExpr()),
164164
),
165165
)
@@ -179,8 +179,8 @@ func (c *queryConverter) BuildSelectStmt(
179179
strings.Join(sqlplugin.DbFields, ", "),
180180
strings.Join(whereClauses, " AND "),
181181
sqlparser.String(c.GetCoalesceCloseTimeExpr()),
182-
searchattribute.GetSqlDbColName(searchattribute.StartTime),
183-
searchattribute.GetSqlDbColName(searchattribute.RunID),
182+
defs.GetSqlDbColName(defs.StartTime),
183+
defs.GetSqlDbColName(defs.RunID),
184184
)
185185
queryArgs = append(queryArgs, pageSize)
186186

@@ -198,7 +198,7 @@ func (c *queryConverter) BuildCountStmt(
198198
whereClauses,
199199
fmt.Sprintf(
200200
"%s = '%s'",
201-
searchattribute.GetSqlDbColName(searchattribute.NamespaceID),
201+
defs.GetSqlDbColName(defs.NamespaceID),
202202
namespaceID,
203203
),
204204
)
@@ -211,7 +211,7 @@ func (c *queryConverter) BuildCountStmt(
211211

212212
groupBy := make([]string, 0, len(queryParams.GroupBy)+1)
213213
for _, field := range queryParams.GroupBy {
214-
groupBy = append(groupBy, searchattribute.GetSqlDbColName(field.FieldName))
214+
groupBy = append(groupBy, defs.GetSqlDbColName(field.FieldName))
215215
}
216216

217217
groupByClause := ""

common/persistence/sql/sqlplugin/postgresql/query_converter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"go.temporal.io/server/common/namespace"
1313
"go.temporal.io/server/common/persistence/sql/sqlplugin"
1414
"go.temporal.io/server/common/persistence/visibility/store/query"
15-
"go.temporal.io/server/common/searchattribute"
15+
"go.temporal.io/server/common/searchattribute/defs"
1616
)
1717

1818
func TestQueryConverter_GetCoalesceCloseTimeExpr(t *testing.T) {
@@ -314,7 +314,7 @@ func TestQueryConverter_BuildCountStmt(t *testing.T) {
314314
Right: query.NewUnsafeSQLString("foo"),
315315
},
316316
groupBy: []*query.SAColumn{
317-
query.NewSAColumn(searchattribute.ExecutionStatus, searchattribute.ExecutionStatus, enumspb.INDEXED_VALUE_TYPE_KEYWORD),
317+
query.NewSAColumn(defs.ExecutionStatus, defs.ExecutionStatus, enumspb.INDEXED_VALUE_TYPE_KEYWORD),
318318
},
319319
stmt: fmt.Sprintf(
320320
"SELECT status, COUNT(*) FROM executions_visibility WHERE namespace_id = '%s' AND Keyword01 = 'foo' GROUP BY status",

0 commit comments

Comments
 (0)