Skip to content

Commit afea85a

Browse files
committed
fix: added more unit tests
1 parent 2da5e53 commit afea85a

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

pkg/querybuilder/collision.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ func AdjustKey(key *telemetrytypes.TelemetryFieldKey, keys map[string][]*telemet
198198
if !key.Equal(matchingKey) {
199199
actions = append(actions, fmt.Sprintf("Adjusting key %s to %s", key, matchingKey))
200200
}
201+
key.Name = matchingKey.Name
201202
key.FieldContext = matchingKey.FieldContext
202203
key.FieldDataType = matchingKey.FieldDataType
203204
key.JSONDataType = matchingKey.JSONDataType
@@ -274,10 +275,6 @@ func AdjustKeysForAliasExpressions[T any](query *qbtypes.QueryBuilderQuery[T], r
274275
For example, if user is using `body.count` as an alias for aggregation and
275276
Uses it in orderBy, upstream code will convert it to just `count` with fieldContext as Body
276277
But we need to adjust it back to `body.count` with fieldContext as unspecified
277-
278-
NOTE: One ambiguity here is that if user specified alias `body.count` is also a valid field then we're not sure
279-
what user meant. Here we take a call that we chose alias over fieldName because if user specifically wants to order by
280-
that field, a different alias can be chosen.
281278
*/
282279
actions := []string{}
283280
if requestType != qbtypes.RequestTypeRaw && requestType != qbtypes.RequestTypeRawStream {

pkg/querybuilder/collision_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func TestAdjustKey(t *testing.T) {
299299
},
300300
expectedKey: telemetrytypes.TelemetryFieldKey{
301301
Name: "trace_id",
302-
FieldContext: telemetrytypes.FieldContextLog, // Use intrinsic field context
302+
FieldContext: telemetrytypes.FieldContextLog, // Use intrinsic field context
303303
FieldDataType: telemetrytypes.FieldDataTypeString, // Use intrinsic field data type
304304
},
305305
expectedActions: []string{
@@ -364,14 +364,43 @@ func TestAdjustKey(t *testing.T) {
364364
expectedKey: telemetrytypes.TelemetryFieldKey{
365365
Name: "custom_field",
366366
FieldContext: telemetrytypes.FieldContextAttribute, // Use attribute field context
367-
FieldDataType: telemetrytypes.FieldDataTypeString, // Use attribute field data type
367+
FieldDataType: telemetrytypes.FieldDataTypeString, // Use attribute field data type
368368
Materialized: true,
369369
},
370370
expectedActions: []string{
371371
"Adjusting key name=custom_field to name=custom_field,context=attribute,datatype=string,materialized=true",
372372
},
373373
description: "Single matching attribute key should use its properties",
374374
},
375+
{
376+
name: "non-intrinsic field with attribute prefix as matching key",
377+
key: telemetrytypes.TelemetryFieldKey{
378+
Name: "custom_field",
379+
FieldContext: telemetrytypes.FieldContextLog,
380+
FieldDataType: telemetrytypes.FieldDataTypeUnspecified,
381+
},
382+
keys: map[string][]*telemetrytypes.TelemetryFieldKey{
383+
"log.custom_field": {
384+
{
385+
Name: "log.custom_field",
386+
FieldContext: telemetrytypes.FieldContextAttribute,
387+
FieldDataType: telemetrytypes.FieldDataTypeString,
388+
Materialized: true,
389+
},
390+
},
391+
},
392+
intrinsicOrCalculatedField: nil,
393+
expectedKey: telemetrytypes.TelemetryFieldKey{
394+
Name: "log.custom_field",
395+
FieldContext: telemetrytypes.FieldContextAttribute, // Use attribute field context
396+
FieldDataType: telemetrytypes.FieldDataTypeString, // Use attribute field data type
397+
Materialized: true,
398+
},
399+
expectedActions: []string{
400+
"Adjusting key name=custom_field,context=log to name=log.custom_field,context=attribute,datatype=string,materialized=true",
401+
},
402+
description: "Single matching attribute key should use its properties",
403+
},
375404
{
376405
name: "non-intrinsic field with no matching attribute keys",
377406
key: telemetrytypes.TelemetryFieldKey{

0 commit comments

Comments
 (0)