SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
feat: improved keys adjustment logic (#9927)
Tushar Vatsf348f47951e1
diff --git a/pkg/querybuilder/collision.go b/pkg/querybuilder/collision.gonew file mode 100644index 0000000000..5d6dc450a3--- /dev/null+++ b/pkg/querybuilder/collision.go@@ -0,0 +1,308 @@+package querybuilder++import (+ "fmt"++ qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"+)++// AdjustDuplicateKeys adjusts duplicate keys in the query by removing specific context and data type+// if the same key appears with different contexts or data types across SelectFields, GroupBy, and OrderBy.+// This ensures that each key is unique and generic enough to cover all its usages in the query.+func AdjustDuplicateKeys[T any](query *qbtypes.QueryBuilderQuery[T]) []string {++ // Create a map to track unique keys across SelectFields, GroupBy, and OrderBy+ globalUniqueKeysMap := map[string]telemetrytypes.TelemetryFieldKey{}++ // for recording modifications+ actions := []string{}++ // SelectFields+ for _, key := range query.SelectFields {+ deduplicateKeys(key, globalUniqueKeysMap, &actions)+ }++ // GroupBy+ for _, key := range query.GroupBy {+ deduplicateKeys(key.TelemetryFieldKey, globalUniqueKeysMap, &actions)+ }++ // OrderBy+ for _, key := range query.Order {+ deduplicateKeys(key.Key.TelemetryFieldKey, globalUniqueKeysMap, &actions)+ }++ // Reconstruct SelectFields slice++ newSelectFields := make([]telemetrytypes.TelemetryFieldKey, 0, len(query.SelectFields))++ seen := map[string]bool{}+ for _, key := range query.SelectFields {+ if !seen[key.Name] {+ newSelectFields = append(newSelectFields, globalUniqueKeysMap[key.Name])+ seen[key.Name] = true+ } else {+ actions = append(actions, fmt.Sprintf("Skipped duplicate SelectField key %s", key))+ }+ }+ query.SelectFields = newSelectFields++ // Reconstruct GroupBy slice+ newGroupBy := make([]qbtypes.GroupByKey, 0, len(query.GroupBy))+ seen = map[string]bool{}+ for _, key := range query.GroupBy {+ if !seen[key.Name] {+ newGroupBy = append(newGroupBy, qbtypes.GroupByKey{TelemetryFieldKey: globalUniqueKeysMap[key.Name]})+ seen[key.Name] = true+ } else {+ actions = append(actions, fmt.Sprintf("Skipped duplicate GroupBy key %s", key))+ }+ }+ query.GroupBy = newGroupBy++ // Reconstruct OrderBy slice+ // NOTE: 1 Edge case here is that if there are two order by on same key with different directions,+ // we will only keep one of them (the first one encountered). This is acceptable as such queries+ // don't make much sense.+ newOrderBy := make([]qbtypes.OrderBy, 0, len(query.Order))+ seen = map[string]bool{}+ for _, key := range query.Order {+ if !seen[key.Key.Name] {+ newOrderBy = append(newOrderBy, qbtypes.OrderBy{Key: qbtypes.OrderByKey{TelemetryFieldKey: globalUniqueKeysMap[key.Key.Name]}, Direction: key.Direction})+ seen[key.Key.Name] = true+ } else {+ actions = append(actions, fmt.Sprintf("Skipped duplicate OrderBy key %s", key.Key))+ }+ }+ query.Order = newOrderBy++ return actions+}++func deduplicateKeys(key telemetrytypes.TelemetryFieldKey, keysMap map[string]telemetrytypes.TelemetryFieldKey, actions *[]string) {+ if existingKey, ok := keysMap[key.Name]; !ok {+ keysMap[key.Name] = key+ } else {+ if existingKey.FieldContext != key.FieldContext && existingKey.FieldContext != telemetrytypes.FieldContextUnspecified {+ // remove field context in the map to make it generic+ *actions = append(*actions, fmt.Sprintf("Removed field context from %s for duplicate key %s", existingKey, key))+ existingKey.FieldContext = telemetrytypes.FieldContextUnspecified+ }+ if existingKey.FieldDataType != key.FieldDataType && existingKey.FieldDataType != telemetrytypes.FieldDataTypeUnspecified {+ // remove field data type in the map to make it generic+ *actions = append(*actions, fmt.Sprintf("Removed field data type from %s for duplicate key %s", existingKey, key))+ existingKey.FieldDataType = telemetrytypes.FieldDataTypeUnspecified+ }+ // Update the map with the modified key+ keysMap[key.Name] = existingKey+ }+}++func AdjustKey(key *telemetrytypes.TelemetryFieldKey, keys map[string][]*telemetrytypes.TelemetryFieldKey, intrinsicOrCalculatedField *telemetrytypes.TelemetryFieldKey) []string {++ // for recording modifications+ actions := []string{}++ if intrinsicOrCalculatedField != nil {+ /*+ Check if it also matches with any of the metadata keys++ For example, lets consider trace_id exists in attributes and is also an intrinsic field++ Now if user is using trace_id, we don't know if they mean intrinsic field or attribute.trace_id+ So we cannot take a call here (we'll leave this upto query builder to decide).++ However, if user is using attribute.trace_id, we can safely assume they mean attribute field+ and not intrinsic field.+ Similarly, if user is using trace_id with a field context or data type that doesn't match+ the intrinsic field, and there is no matching key in the metadata with the same name,+ we can safely assume they mean the intrinsic field and override the context and data type.++ */++ // Check if there is any matching key in the metadata with the same name and it is not the same intrinsic/calculated field+ match := false+ for _, mapKey := range keys[key.Name] {+ // Either field context is unspecified or matches+ // and+ // Either field data type is unspecified or matches+ if (key.FieldContext == telemetrytypes.FieldContextUnspecified || mapKey.FieldContext == key.FieldContext) &&+ (key.FieldDataType == telemetrytypes.FieldDataTypeUnspecified || mapKey.FieldDataType == key.FieldDataType) &&+ !mapKey.Equal(intrinsicOrCalculatedField) {+ match = true+ break+ }+ }++ // NOTE: if a user is highly opinionated and use attribute.duration_nano:string+ // It will be defaulted to intrinsic field duration_nano as the actual attribute might be attribute.duration_nano:number++ // We don't have a match, then it doesn't exist in attribute or resource attribute+ // use the intrinsic/calculated field+ if !match {+ // This is the case where user is using an intrinsic/calculated field+ // with a context or data type that may or may not match the intrinsic/calculated field+ // and there is no matching key in the metadata with the same name+ // So we can safely override the context and data type++ actions = append(actions, fmt.Sprintf("Overriding key: %s to %s", key, intrinsicOrCalculatedField))+ key.FieldContext = intrinsicOrCalculatedField.FieldContext+ key.FieldDataType = intrinsicOrCalculatedField.FieldDataType+ key.JSONDataType = intrinsicOrCalculatedField.JSONDataType+ key.Indexes = intrinsicOrCalculatedField.Indexes+ key.Materialized = intrinsicOrCalculatedField.Materialized++ return actions++ }+ }++ // This means that the key provided by the user cannot be overridden to a single field because of ambiguity+ // So we need to look into metadata keys to find the best match++ // check if all the keys for the given field with matching context and data type+ matchingKeys := []*telemetrytypes.TelemetryFieldKey{}+ for _, metadataKey := range keys[key.Name] {+ // Only consider keys that match the context and data type (if specified)+ if (key.FieldContext == telemetrytypes.FieldContextUnspecified || key.FieldContext == metadataKey.FieldContext) &&+ (key.FieldDataType == telemetrytypes.FieldDataTypeUnspecified || key.FieldDataType == metadataKey.FieldDataType) {+ matchingKeys = append(matchingKeys, metadataKey)+ }+ }++ // Also consider if context is actually part of the key name+ contextPrefixedMatchingKeys := []*telemetrytypes.TelemetryFieldKey{}+ if key.FieldContext != telemetrytypes.FieldContextUnspecified {+ for _, metadataKey := range keys[key.FieldContext.StringValue()+"."+key.Name] {+ // Since we prefixed the context in the name, we only need to match data type+ if key.FieldDataType == telemetrytypes.FieldDataTypeUnspecified || key.FieldDataType == metadataKey.FieldDataType {+ contextPrefixedMatchingKeys = append(contextPrefixedMatchingKeys, metadataKey)+ }+ }+ }++ if len(matchingKeys)+len(contextPrefixedMatchingKeys) == 0 {+ // we do not have any matching keys, most likely user made a mistake, let downstream query builder handle it+ // Set materialized to false explicitly to avoid QB looking for materialized column+ key.Materialized = false+ } else if len(matchingKeys)+len(contextPrefixedMatchingKeys) == 1 {+ // only one matching key, use it+ var matchingKey *telemetrytypes.TelemetryFieldKey+ if len(matchingKeys) == 1 {+ matchingKey = matchingKeys[0]+ } else {+ matchingKey = contextPrefixedMatchingKeys[0]+ }++ if !key.Equal(matchingKey) {+ actions = append(actions, fmt.Sprintf("Adjusting key %s to %s", key, matchingKey))+ }+ key.Name = matchingKey.Name+ key.FieldContext = matchingKey.FieldContext+ key.FieldDataType = matchingKey.FieldDataType+ key.JSONDataType = matchingKey.JSONDataType+ key.Indexes = matchingKey.Indexes+ key.Materialized = matchingKey.Materialized++ return actions+ } else {+ // multiple matching keys, set materialized only if all the keys are materialized+ // TODO: This could all be redundant if it is not, it should be.+ // Downstream query builder should handle multiple matching keys with their own metadata+ // and not rely on this function to do so.+ materialized := true+ indexes := []telemetrytypes.JSONDataTypeIndex{}+ fieldContextsSeen := map[telemetrytypes.FieldContext]bool{}+ dataTypesSeen := map[telemetrytypes.FieldDataType]bool{}+ jsonTypesSeen := map[string]*telemetrytypes.JSONDataType{}+ for _, matchingKey := range matchingKeys {+ materialized = materialized && matchingKey.Materialized+ fieldContextsSeen[matchingKey.FieldContext] = true+ dataTypesSeen[matchingKey.FieldDataType] = true+ if matchingKey.JSONDataType != nil {+ jsonTypesSeen[matchingKey.JSONDataType.StringValue()] = matchingKey.JSONDataType+ }+ indexes = append(indexes, matchingKey.Indexes...)+ }+ for _, matchingKey := range contextPrefixedMatchingKeys {+ materialized = materialized && matchingKey.Materialized+ fieldContextsSeen[matchingKey.FieldContext] = true+ dataTypesSeen[matchingKey.FieldDataType] = true+ if matchingKey.JSONDataType != nil {+ jsonTypesSeen[matchingKey.JSONDataType.StringValue()] = matchingKey.JSONDataType+ }+ indexes = append(indexes, matchingKey.Indexes...)+ }+ key.Materialized = materialized+ if len(indexes) > 0 {+ key.Indexes = indexes+ }++ if len(fieldContextsSeen) == 1 && key.FieldContext == telemetrytypes.FieldContextUnspecified {+ // all matching keys have same field context, use it+ for context := range fieldContextsSeen {+ actions = append(actions, fmt.Sprintf("Adjusting key %s to have field context %s", key, context.StringValue()))+ key.FieldContext = context+ break+ }+ }++ if len(dataTypesSeen) == 1 && key.FieldDataType == telemetrytypes.FieldDataTypeUnspecified {+ // all matching keys have same data type, use it+ for dt := range dataTypesSeen {+ actions = append(actions, fmt.Sprintf("Adjusting key %s to have data type %s", key, dt.StringValue()))+ key.FieldDataType = dt+ break+ }+ }++ if len(jsonTypesSeen) == 1 && key.JSONDataType == nil {+ // all matching keys have same JSON data type, use it+ for _, jt := range jsonTypesSeen {+ actions = append(actions, fmt.Sprintf("Adjusting key %s to have JSON data type %s", key, jt.StringValue()))+ key.JSONDataType = jt+ break+ }+ }+ }++ return actions+}++func AdjustKeysForAliasExpressions[T any](query *qbtypes.QueryBuilderQuery[T], requestType qbtypes.RequestType) []string {+ /*+ For example, if user is using `body.count` as an alias for aggregation and+ Uses it in orderBy, upstream code will convert it to just `count` with fieldContext as Body+ But we need to adjust it back to `body.count` with fieldContext as unspecified+ */+ actions := []string{}+ if requestType != qbtypes.RequestTypeRaw && requestType != qbtypes.RequestTypeRawStream {+ aliasExpressions := map[string]bool{}+ for _, agg := range query.Aggregations {+ switch v := any(agg).(type) {+ case qbtypes.LogAggregation:+ if v.Alias != "" {+ aliasExpressions[v.Alias] = true+ }+ case qbtypes.TraceAggregation:+ if v.Alias != "" {+ aliasExpressions[v.Alias] = true+ }+ default:+ continue+ }+ }+ if len(aliasExpressions) > 0 {+ for idx := range query.Order {+ contextPrefixedKeyName := fmt.Sprintf("%s.%s", query.Order[idx].Key.FieldContext.StringValue(), query.Order[idx].Key.Name)+ if aliasExpressions[contextPrefixedKeyName] {+ actions = append(actions, fmt.Sprintf("Adjusting OrderBy key %s to %s", query.Order[idx].Key, contextPrefixedKeyName))+ query.Order[idx].Key.FieldContext = telemetrytypes.FieldContextUnspecified+ query.Order[idx].Key.Name = contextPrefixedKeyName+ }+ }+ }+ }+ return actions+}diff --git a/pkg/querybuilder/collision_test.go b/pkg/querybuilder/collision_test.gonew file mode 100644index 0000000000..f59d20ee4b--- /dev/null+++ b/pkg/querybuilder/collision_test.go@@ -0,0 +1,610 @@+package querybuilder++import (+ "testing"++ qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"+ "github.com/stretchr/testify/assert"+)++func TestAdjustDuplicateKeys(t *testing.T) {+ tests := []struct {+ name string+ query qbtypes.QueryBuilderQuery[any]+ expectedQuery qbtypes.QueryBuilderQuery[any]+ expectedActions []string+ description string+ }{+ {+ name: "no duplicates - should remain unchanged",+ query: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "field1", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString},+ {Name: "field2", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeNumber},+ },+ GroupBy: []qbtypes.GroupByKey{+ {TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "field3", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString}},+ },+ Order: []qbtypes.OrderBy{+ {Key: qbtypes.OrderByKey{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "field4", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeNumber}}},+ },+ },+ expectedQuery: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "field1", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString},+ {Name: "field2", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeNumber},+ },+ GroupBy: []qbtypes.GroupByKey{+ {TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "field3", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString}},+ },+ Order: []qbtypes.OrderBy{+ {Key: qbtypes.OrderByKey{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "field4", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeNumber}}},+ },+ },+ expectedActions: []string{},+ description: "No duplicate keys - fields should remain unchanged",+ },+ {+ name: "duplicate in SelectFields with different context",+ query: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "duration", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeNumber},+ {Name: "duration", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeNumber},+ },+ },+ expectedQuery: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "duration", FieldContext: telemetrytypes.FieldContextUnspecified, FieldDataType: telemetrytypes.FieldDataTypeNumber},+ },+ GroupBy: []qbtypes.GroupByKey{},+ Order: []qbtypes.OrderBy{},+ },+ expectedActions: []string{+ "Removed field context from name=duration,context=attribute,datatype=number for duplicate key name=duration,context=resource,datatype=number",+ "Skipped duplicate SelectField key name=duration,context=resource,datatype=number",+ },+ description: "Duplicate key with different context should be merged with unspecified context",+ },+ {+ name: "duplicate in SelectFields with different data type",+ query: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "value", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString},+ {Name: "value", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeNumber},+ },+ },+ expectedQuery: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "value", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeUnspecified},+ },+ GroupBy: []qbtypes.GroupByKey{},+ Order: []qbtypes.OrderBy{},+ },+ expectedActions: []string{+ "Removed field data type from name=value,context=attribute,datatype=string for duplicate key name=value,context=attribute,datatype=number",+ "Skipped duplicate SelectField key name=value,context=attribute,datatype=number",+ },+ description: "Duplicate key with different data type should be merged with unspecified data type",+ },+ {+ name: "duplicate in SelectFields with different context and data type",+ query: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "field", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString},+ {Name: "field", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeNumber},+ },+ },+ expectedQuery: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "field", FieldContext: telemetrytypes.FieldContextUnspecified, FieldDataType: telemetrytypes.FieldDataTypeUnspecified},+ },+ GroupBy: []qbtypes.GroupByKey{},+ Order: []qbtypes.OrderBy{},+ },+ expectedActions: []string{+ "Removed field context from name=field,context=attribute,datatype=string for duplicate key name=field,context=resource,datatype=number",+ "Removed field data type from name=field,datatype=string for duplicate key name=field,context=resource,datatype=number",+ "Skipped duplicate SelectField key name=field,context=resource,datatype=number",+ },+ description: "Duplicate key with different context and data type should be merged with both unspecified",+ },+ {+ name: "duplicate across SelectFields and GroupBy",+ query: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "service", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString},+ },+ GroupBy: []qbtypes.GroupByKey{+ {TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "service", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeString}},+ },+ },+ expectedQuery: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "service", FieldContext: telemetrytypes.FieldContextUnspecified, FieldDataType: telemetrytypes.FieldDataTypeString},+ },+ GroupBy: []qbtypes.GroupByKey{+ {TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "service", FieldContext: telemetrytypes.FieldContextUnspecified, FieldDataType: telemetrytypes.FieldDataTypeString}},+ },+ Order: []qbtypes.OrderBy{},+ },+ expectedActions: []string{+ "Removed field context from name=service,context=attribute,datatype=string for duplicate key name=service,context=resource,datatype=string",+ },+ description: "Duplicate across SelectFields and GroupBy with different context should be merged",+ },+ {+ name: "duplicate across SelectFields, GroupBy, and OrderBy",+ query: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "timestamp", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeNumber},+ },+ GroupBy: []qbtypes.GroupByKey{+ {TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "timestamp", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeNumber}},+ },+ Order: []qbtypes.OrderBy{+ {Key: qbtypes.OrderByKey{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "timestamp", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString}}},+ },+ },+ expectedQuery: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "timestamp", FieldContext: telemetrytypes.FieldContextUnspecified, FieldDataType: telemetrytypes.FieldDataTypeUnspecified},+ },+ GroupBy: []qbtypes.GroupByKey{+ {TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "timestamp", FieldContext: telemetrytypes.FieldContextUnspecified, FieldDataType: telemetrytypes.FieldDataTypeUnspecified}},+ },+ Order: []qbtypes.OrderBy{+ {Key: qbtypes.OrderByKey{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "timestamp", FieldContext: telemetrytypes.FieldContextUnspecified, FieldDataType: telemetrytypes.FieldDataTypeUnspecified}}},+ },+ },+ expectedActions: []string{+ "Removed field context from name=timestamp,context=attribute,datatype=number for duplicate key name=timestamp,context=resource,datatype=number",+ "Removed field data type from name=timestamp,datatype=number for duplicate key name=timestamp,context=attribute,datatype=string",+ },+ description: "Duplicate across all three sections with different contexts and data types should be fully merged",+ },+ {+ name: "multiple duplicates in OrderBy - keeps first occurrence",+ query: qbtypes.QueryBuilderQuery[any]{+ Order: []qbtypes.OrderBy{+ {+ Key: qbtypes.OrderByKey{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "field", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString}},+ Direction: qbtypes.OrderDirectionAsc,+ },+ {+ Key: qbtypes.OrderByKey{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "field", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeString}},+ Direction: qbtypes.OrderDirectionDesc,+ },+ },+ },+ expectedQuery: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{},+ GroupBy: []qbtypes.GroupByKey{},+ Order: []qbtypes.OrderBy{+ {+ Key: qbtypes.OrderByKey{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "field", FieldContext: telemetrytypes.FieldContextUnspecified, FieldDataType: telemetrytypes.FieldDataTypeString}},+ Direction: qbtypes.OrderDirectionAsc,+ },+ },+ },+ expectedActions: []string{+ "Removed field context from name=field,context=attribute,datatype=string for duplicate key name=field,context=resource,datatype=string",+ "Skipped duplicate OrderBy key name=field,context=resource,datatype=string",+ },+ description: "Multiple OrderBy on same key keeps first occurrence and merges contexts",+ },+ {+ name: "three duplicate entries in SelectFields",+ query: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "status", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString},+ {Name: "status", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeString},+ {Name: "status", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeNumber},+ },+ },+ expectedQuery: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {Name: "status", FieldContext: telemetrytypes.FieldContextUnspecified, FieldDataType: telemetrytypes.FieldDataTypeUnspecified},+ },+ GroupBy: []qbtypes.GroupByKey{},+ Order: []qbtypes.OrderBy{},+ },+ expectedActions: []string{+ "Removed field context from name=status,context=attribute,datatype=string for duplicate key name=status,context=resource,datatype=string",+ "Removed field data type from name=status,datatype=string for duplicate key name=status,context=attribute,datatype=number",+ "Skipped duplicate SelectField key name=status,context=resource,datatype=string",+ "Skipped duplicate SelectField key name=status,context=attribute,datatype=number",+ },+ description: "Three duplicate entries with various differences should be fully merged",+ },+ {+ name: "duplicate entries in GroupBy",+ query: qbtypes.QueryBuilderQuery[any]{+ GroupBy: []qbtypes.GroupByKey{+ {TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "status", FieldContext: telemetrytypes.FieldContextAttribute, FieldDataType: telemetrytypes.FieldDataTypeString}},+ {TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "status", FieldContext: telemetrytypes.FieldContextResource, FieldDataType: telemetrytypes.FieldDataTypeNumber}},+ },+ },+ expectedQuery: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{},+ GroupBy: []qbtypes.GroupByKey{+ {TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{Name: "status", FieldContext: telemetrytypes.FieldContextUnspecified, FieldDataType: telemetrytypes.FieldDataTypeUnspecified}},+ },+ Order: []qbtypes.OrderBy{},+ },+ expectedActions: []string{+ "Removed field context from name=status,context=attribute,datatype=string for duplicate key name=status,context=resource,datatype=number",+ "Removed field data type from name=status,datatype=string for duplicate key name=status,context=resource,datatype=number",+ "Skipped duplicate GroupBy key name=status,context=resource,datatype=number",+ },+ description: "Duplicate entries in GroupBy with different context should be merged",+ },+ {+ name: "empty query",+ query: qbtypes.QueryBuilderQuery[any]{},+ expectedQuery: qbtypes.QueryBuilderQuery[any]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{},+ GroupBy: []qbtypes.GroupByKey{},+ Order: []qbtypes.OrderBy{},+ },+ expectedActions: []string{},+ description: "Empty query should result in empty slices",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Make a copy to avoid modifying the original+ query := tt.query+ actions := AdjustDuplicateKeys(&query)++ assert.Equal(t, tt.expectedQuery.SelectFields, query.SelectFields, "SelectFields mismatch: %s", tt.description)+ assert.Equal(t, tt.expectedQuery.GroupBy, query.GroupBy, "GroupBy mismatch: %s", tt.description)+ assert.Equal(t, tt.expectedQuery.Order, query.Order, "Order mismatch: %s", tt.description)+ assert.Equal(t, tt.expectedActions, actions, "Actions mismatch: %s", tt.description)+ })+ }+}++func TestAdjustKey(t *testing.T) {+ tests := []struct {+ name string+ key telemetrytypes.TelemetryFieldKey+ keys map[string][]*telemetrytypes.TelemetryFieldKey+ intrinsicOrCalculatedField *telemetrytypes.TelemetryFieldKey+ expectedKey telemetrytypes.TelemetryFieldKey+ expectedActions []string+ description string+ }{+ {+ name: "intrinsic field with no matching attribute/resource key",+ key: telemetrytypes.TelemetryFieldKey{+ Name: "trace_id", // User provided key "trace_id" with no context or data type+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keys: map[string][]*telemetrytypes.TelemetryFieldKey{+ "trace_id": { // This is the intrinsic field itself in the keys map+ &telemetrytypes.TelemetryFieldKey{+ Name: "trace_id",+ FieldContext: telemetrytypes.FieldContextLog,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ },+ },+ intrinsicOrCalculatedField: &telemetrytypes.TelemetryFieldKey{+ Name: "trace_id",+ FieldContext: telemetrytypes.FieldContextLog,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "trace_id",+ FieldContext: telemetrytypes.FieldContextLog, // Use intrinsic field context+ FieldDataType: telemetrytypes.FieldDataTypeString, // Use intrinsic field data type+ },+ expectedActions: []string{+ "Overriding key: name=trace_id to name=trace_id,context=log,datatype=string",+ },+ description: "Intrinsic field with no attribute.resource key match should use intrinsic field properties",+ },+ {+ name: "intrinsic field with matching attribute/resource key",+ key: telemetrytypes.TelemetryFieldKey{+ Name: "trace_id", // User provided key "trace_id" with no context or data type+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keys: map[string][]*telemetrytypes.TelemetryFieldKey{+ "trace_id": {+ {+ Name: "trace_id", // This is an attribute key matching the intrinsic field name+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ {+ Name: "trace_id", // This is the intrinsic field itself in the keys map+ FieldContext: telemetrytypes.FieldContextLog,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ },+ },+ intrinsicOrCalculatedField: &telemetrytypes.TelemetryFieldKey{+ Name: "trace_id",+ FieldContext: telemetrytypes.FieldContextLog,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "trace_id",+ FieldContext: telemetrytypes.FieldContextUnspecified, // This is left unspecified due to ambiguity+ FieldDataType: telemetrytypes.FieldDataTypeString, // This is set to string as both have same type+ Materialized: false,+ },+ expectedActions: []string{"Adjusting key name=trace_id to have data type string"},+ description: "Intrinsic field with attribute key match should set data type to string since both in both intrinsic and attribute have same type (ambiguous case)",+ },+ {+ name: "non-intrinsic field with single matching attribute key",+ key: telemetrytypes.TelemetryFieldKey{+ Name: "custom_field",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keys: map[string][]*telemetrytypes.TelemetryFieldKey{+ "custom_field": {+ {+ Name: "custom_field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ },+ },+ intrinsicOrCalculatedField: nil,+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "custom_field",+ FieldContext: telemetrytypes.FieldContextAttribute, // Use attribute field context+ FieldDataType: telemetrytypes.FieldDataTypeString, // Use attribute field data type+ Materialized: true,+ },+ expectedActions: []string{+ "Adjusting key name=custom_field to name=custom_field,context=attribute,datatype=string,materialized=true",+ },+ description: "Single matching attribute key should use its properties",+ },+ {+ name: "non-intrinsic field with attribute prefix as matching key",+ key: telemetrytypes.TelemetryFieldKey{+ Name: "custom_field",+ FieldContext: telemetrytypes.FieldContextLog,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keys: map[string][]*telemetrytypes.TelemetryFieldKey{+ "log.custom_field": {+ {+ Name: "log.custom_field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ },+ },+ intrinsicOrCalculatedField: nil,+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "log.custom_field",+ FieldContext: telemetrytypes.FieldContextAttribute, // Use attribute field context+ FieldDataType: telemetrytypes.FieldDataTypeString, // Use attribute field data type+ Materialized: true,+ },+ expectedActions: []string{+ "Adjusting key name=custom_field,context=log to name=log.custom_field,context=attribute,datatype=string,materialized=true",+ },+ description: "Single matching attribute key should use its properties",+ },+ {+ name: "non-intrinsic field with no matching attribute keys",+ key: telemetrytypes.TelemetryFieldKey{+ Name: "unknown_field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ keys: map[string][]*telemetrytypes.TelemetryFieldKey{},+ intrinsicOrCalculatedField: nil,+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "unknown_field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ expectedActions: []string{},+ description: "No matching attribute keys should set materialized to false",+ },+ {+ name: "multiple matching keys with different contexts",+ key: telemetrytypes.TelemetryFieldKey{+ Name: "field", // User provided key "field" with no context and string data type+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ keys: map[string][]*telemetrytypes.TelemetryFieldKey{+ "field": {+ {+ Name: "field", // Attribute context+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ {+ Name: "field", // Resource context+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ },+ },+ intrinsicOrCalculatedField: nil,+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "field",+ FieldContext: telemetrytypes.FieldContextUnspecified, // Too ambiguous to set context+ FieldDataType: telemetrytypes.FieldDataTypeString, // Both have same data type+ Materialized: true,+ },+ expectedActions: []string{},+ description: "Multiple matching keys with different contexts should keep context unspecified but data type specified",+ },+ {+ name: "multiple matching keys with different data types",+ key: telemetrytypes.TelemetryFieldKey{+ Name: "field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keys: map[string][]*telemetrytypes.TelemetryFieldKey{+ "field": {+ {+ Name: "field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ {+ Name: "field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ Materialized: true,+ },+ },+ },+ intrinsicOrCalculatedField: nil,+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ Materialized: true,+ },+ expectedActions: []string{},+ description: "Multiple matching keys with different data types should keep data type unspecified but context specified",+ },+ {+ name: "specific context filters matching keys",+ key: telemetrytypes.TelemetryFieldKey{+ Name: "field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keys: map[string][]*telemetrytypes.TelemetryFieldKey{+ "field": {+ {+ Name: "field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ {+ Name: "field",+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ },+ intrinsicOrCalculatedField: nil,+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ expectedActions: []string{+ "Adjusting key name=field,context=attribute to name=field,context=attribute,datatype=string,materialized=true",+ },+ description: "Specific context should filter to matching keys only",+ },+ {+ name: "specific data type filters matching keys",+ key: telemetrytypes.TelemetryFieldKey{+ Name: "field",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ keys: map[string][]*telemetrytypes.TelemetryFieldKey{+ "field": {+ {+ Name: "field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ {+ Name: "field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ Materialized: false,+ },+ },+ },+ intrinsicOrCalculatedField: nil,+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ expectedActions: []string{+ "Adjusting key name=field,datatype=string to name=field,context=attribute,datatype=string,materialized=true",+ },+ description: "Specific data type should filter to matching keys only",+ },+ {+ name: "intrinsic field with explicit different context matches metadata",+ key: telemetrytypes.TelemetryFieldKey{+ Name: "duration",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keys: map[string][]*telemetrytypes.TelemetryFieldKey{+ "duration": {+ {+ Name: "duration", // This is an attribute key matching the intrinsic field name+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ Materialized: false,+ },+ {+ Name: "duration", // This is the intrinsic field itself in the keys map+ FieldContext: telemetrytypes.FieldContextSpan,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ Materialized: true,+ },+ },+ },+ intrinsicOrCalculatedField: &telemetrytypes.TelemetryFieldKey{+ Name: "duration",+ FieldContext: telemetrytypes.FieldContextSpan,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ Materialized: true,+ },+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "duration",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ Materialized: false,+ },+ expectedActions: []string{"Adjusting key name=duration,context=attribute to name=duration,context=attribute,datatype=number"},+ description: "User explicitly specified attribute.duration, should prefer metadata over intrinsic",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ key := tt.key+ actions := AdjustKey(&key, tt.keys, tt.intrinsicOrCalculatedField)++ assert.Equal(t, tt.expectedKey.Name, key.Name, "Name mismatch: %s", tt.description)+ assert.Equal(t, tt.expectedKey.FieldContext, key.FieldContext, "FieldContext mismatch: %s", tt.description)+ assert.Equal(t, tt.expectedKey.FieldDataType, key.FieldDataType, "FieldDataType mismatch: %s", tt.description)+ assert.Equal(t, tt.expectedKey.Materialized, key.Materialized, "Materialized mismatch: %s", tt.description)+ assert.Equal(t, tt.expectedActions, actions, "Actions mismatch: %s", tt.description)+ })+ }+}diff --git a/pkg/telemetrylogs/json_stmt_builder_test.go b/pkg/telemetrylogs/json_stmt_builder_test.goindex 89a3644877..df9b387bc7 100644--- a/pkg/telemetrylogs/json_stmt_builder_test.go+++ b/pkg/telemetrylogs/json_stmt_builder_test.go@@ -45,7 +45,7 @@ func TestStmtBuilderTimeSeriesBodyGroupByJSON(t *testing.T) {GroupBy: []qbtypes.GroupByKey{{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{- Name: "body.user.age",+ Name: "user.age",},},},@@ -70,7 +70,7 @@ func TestStmtBuilderTimeSeriesBodyGroupByJSON(t *testing.T) {GroupBy: []qbtypes.GroupByKey{{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{- Name: "body.education[].awards[].type",+ Name: "education[].awards[].type",},},},@@ -126,12 +126,12 @@ func TestStmtBuilderTimeSeriesBodyGroupByPromoted(t *testing.T) {GroupBy: []qbtypes.GroupByKey{{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{- Name: "body.user.age",+ Name: "user.age",},},{TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{- Name: "body.user.name",+ Name: "user.name",},},},@@ -240,7 +240,7 @@ func TestStatementBuilderListQueryBody(t *testing.T) {expected: qbtypes.Statement{Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_logs.distributed_logs_v2_resource WHERE true AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT timestamp, id, trace_id, span_id, trace_flags, severity_text, severity_number, scope_name, scope_version, body, body_json, body_json_promoted, attributes_string, attributes_number, attributes_bool, resources_string, scope_string FROM signoz_logs.distributed_logs_v2 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND ((arrayExists(`body_json.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))')) OR arrayExists(toFloat64(x) -> toFloat64(x) = ?, dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))'))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))'))) OR (arrayExists(`body_json.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), arrayMap(x->dynamicElement(x, 'Float64'), arrayFilter(x->(dynamicType(x) = 'Float64'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)')))) OR arrayExists(toFloat64(x) -> toFloat64(x) = ?, arrayMap(x->dynamicElement(x, 'Float64'), arrayFilter(x->(dynamicType(x) = 'Float64'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)'))))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')))) AND timestamp >= ? AND ts_bucket_start >= ? AND timestamp < ? AND ts_bucket_start <= ? LIMIT ?",Args: []any{uint64(1747945619), uint64(1747983448), "%1.65%", 1.65, "%1.65%", 1.65, "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), 10},- Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64 name=education[].parameters,context=body,datatype=[]dynamic]."},+ Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64,jsondatatype=Array(Nullable(Float64)) name=education[].parameters,context=body,datatype=[]dynamic,jsondatatype=Array(Dynamic)]."},},expectedErr: nil,},@@ -269,7 +269,7 @@ func TestStatementBuilderListQueryBody(t *testing.T) {expected: qbtypes.Statement{Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_logs.distributed_logs_v2_resource WHERE true AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT timestamp, id, trace_id, span_id, trace_flags, severity_text, severity_number, scope_name, scope_version, body, body_json, body_json_promoted, attributes_string, attributes_number, attributes_bool, resources_string, scope_string FROM signoz_logs.distributed_logs_v2 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND ((arrayExists(`body_json.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))')) OR arrayExists(x -> x = ?, dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))'))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))'))) OR (arrayExists(`body_json.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), arrayMap(x->dynamicElement(x, 'Bool'), arrayFilter(x->(dynamicType(x) = 'Bool'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)')))) OR arrayExists(x -> x = ?, arrayMap(x->dynamicElement(x, 'Bool'), arrayFilter(x->(dynamicType(x) = 'Bool'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)'))))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')))) AND timestamp >= ? AND ts_bucket_start >= ? AND timestamp < ? AND ts_bucket_start <= ? LIMIT ?",Args: []any{uint64(1747945619), uint64(1747983448), "%true%", true, "%true%", true, "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), 10},- Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64 name=education[].parameters,context=body,datatype=[]dynamic]."},+ Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64,jsondatatype=Array(Nullable(Float64)) name=education[].parameters,context=body,datatype=[]dynamic,jsondatatype=Array(Dynamic)]."},},expectedErr: nil,},@@ -284,7 +284,7 @@ func TestStatementBuilderListQueryBody(t *testing.T) {expected: qbtypes.Statement{Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_logs.distributed_logs_v2_resource WHERE true AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT timestamp, id, trace_id, span_id, trace_flags, severity_text, severity_number, scope_name, scope_version, body, body_json, body_json_promoted, attributes_string, attributes_number, attributes_bool, resources_string, scope_string FROM signoz_logs.distributed_logs_v2 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND ((arrayExists(`body_json.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))')) OR arrayExists(toString(x) -> toString(x) = ?, dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))'))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))'))) OR (arrayExists(`body_json.education`-> (arrayExists(x -> LOWER(x) LIKE LOWER(?), arrayMap(x->dynamicElement(x, 'String'), arrayFilter(x->(dynamicType(x) = 'String'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)')))) OR arrayExists(x -> x = ?, arrayMap(x->dynamicElement(x, 'String'), arrayFilter(x->(dynamicType(x) = 'String'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)'))))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')))) AND timestamp >= ? AND ts_bucket_start >= ? AND timestamp < ? AND ts_bucket_start <= ? LIMIT ?",Args: []any{uint64(1747945619), uint64(1747983448), "%passed%", "passed", "%passed%", "passed", "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), 10},- Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64 name=education[].parameters,context=body,datatype=[]dynamic]."},+ Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64,jsondatatype=Array(Nullable(Float64)) name=education[].parameters,context=body,datatype=[]dynamic,jsondatatype=Array(Dynamic)]."},},expectedErr: nil,},@@ -313,7 +313,7 @@ func TestStatementBuilderListQueryBody(t *testing.T) {expected: qbtypes.Statement{Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_logs.distributed_logs_v2_resource WHERE true AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT timestamp, id, trace_id, span_id, trace_flags, severity_text, severity_number, scope_name, scope_version, body, body_json, body_json_promoted, attributes_string, attributes_number, attributes_bool, resources_string, scope_string FROM signoz_logs.distributed_logs_v2 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND ((arrayExists(`body_json.interests`-> arrayExists(`body_json.interests[].entities`-> arrayExists(`body_json.interests[].entities[].reviews`-> arrayExists(`body_json.interests[].entities[].reviews[].entries`-> arrayExists(`body_json.interests[].entities[].reviews[].entries[].metadata`-> arrayExists(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`.`ratings`, 'Array(Nullable(Int64))')) OR arrayExists(toFloat64(x) -> toFloat64(x) = ?, dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`.`ratings`, 'Array(Nullable(Int64))'))), dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata`.`positions`, 'Array(JSON(max_dynamic_types=0, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities[].reviews[].entries`.`metadata`, 'Array(JSON(max_dynamic_types=1, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities[].reviews`.`entries`, 'Array(JSON(max_dynamic_types=2, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities`.`reviews`, 'Array(JSON(max_dynamic_types=4, max_dynamic_paths=0))')), dynamicElement(`body_json.interests`.`entities`, 'Array(JSON(max_dynamic_types=8, max_dynamic_paths=0))')), dynamicElement(body_json.`interests`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))'))) OR (arrayExists(`body_json.interests`-> arrayExists(`body_json.interests[].entities`-> arrayExists(`body_json.interests[].entities[].reviews`-> arrayExists(`body_json.interests[].entities[].reviews[].entries`-> arrayExists(`body_json.interests[].entities[].reviews[].entries[].metadata`-> arrayExists(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`-> (arrayExists(x -> LOWER(x) LIKE LOWER(?), dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`.`ratings`, 'Array(Nullable(String))')) OR arrayExists(toFloat64OrNull(x) -> toFloat64OrNull(x) = ?, dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`.`ratings`, 'Array(Nullable(String))'))), dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata`.`positions`, 'Array(JSON(max_dynamic_types=0, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities[].reviews[].entries`.`metadata`, 'Array(JSON(max_dynamic_types=1, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities[].reviews`.`entries`, 'Array(JSON(max_dynamic_types=2, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities`.`reviews`, 'Array(JSON(max_dynamic_types=4, max_dynamic_paths=0))')), dynamicElement(`body_json.interests`.`entities`, 'Array(JSON(max_dynamic_types=8, max_dynamic_paths=0))')), dynamicElement(body_json.`interests`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')))) AND timestamp >= ? AND ts_bucket_start >= ? AND timestamp < ? AND ts_bucket_start <= ? LIMIT ?",Args: []any{uint64(1747945619), uint64(1747983448), "%4%", float64(4), "%4%", float64(4), "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), 10},- Warnings: []string{"Key `interests[].entities[].reviews[].entries[].metadata[].positions[].ratings` is ambiguous, found 2 different combinations of field context / data type: [name=interests[].entities[].reviews[].entries[].metadata[].positions[].ratings,context=body,datatype=[]int64 name=interests[].entities[].reviews[].entries[].metadata[].positions[].ratings,context=body,datatype=[]string]."},+ Warnings: []string{"Key `interests[].entities[].reviews[].entries[].metadata[].positions[].ratings` is ambiguous, found 2 different combinations of field context / data type: [name=interests[].entities[].reviews[].entries[].metadata[].positions[].ratings,context=body,datatype=[]int64,jsondatatype=Array(Nullable(Int64)) name=interests[].entities[].reviews[].entries[].metadata[].positions[].ratings,context=body,datatype=[]string,jsondatatype=Array(Nullable(String))]."},},expectedErr: nil,},@@ -328,7 +328,7 @@ func TestStatementBuilderListQueryBody(t *testing.T) {expected: qbtypes.Statement{Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_logs.distributed_logs_v2_resource WHERE true AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT timestamp, id, trace_id, span_id, trace_flags, severity_text, severity_number, scope_name, scope_version, body, body_json, body_json_promoted, attributes_string, attributes_number, attributes_bool, resources_string, scope_string FROM signoz_logs.distributed_logs_v2 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND ((arrayExists(`body_json.interests`-> arrayExists(`body_json.interests[].entities`-> arrayExists(`body_json.interests[].entities[].reviews`-> arrayExists(`body_json.interests[].entities[].reviews[].entries`-> arrayExists(`body_json.interests[].entities[].reviews[].entries[].metadata`-> arrayExists(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`.`ratings`, 'Array(Nullable(Int64))')) OR arrayExists(toString(x) -> toString(x) = ?, dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`.`ratings`, 'Array(Nullable(Int64))'))), dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata`.`positions`, 'Array(JSON(max_dynamic_types=0, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities[].reviews[].entries`.`metadata`, 'Array(JSON(max_dynamic_types=1, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities[].reviews`.`entries`, 'Array(JSON(max_dynamic_types=2, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities`.`reviews`, 'Array(JSON(max_dynamic_types=4, max_dynamic_paths=0))')), dynamicElement(`body_json.interests`.`entities`, 'Array(JSON(max_dynamic_types=8, max_dynamic_paths=0))')), dynamicElement(body_json.`interests`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))'))) OR (arrayExists(`body_json.interests`-> arrayExists(`body_json.interests[].entities`-> arrayExists(`body_json.interests[].entities[].reviews`-> arrayExists(`body_json.interests[].entities[].reviews[].entries`-> arrayExists(`body_json.interests[].entities[].reviews[].entries[].metadata`-> arrayExists(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`-> (arrayExists(x -> LOWER(x) LIKE LOWER(?), dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`.`ratings`, 'Array(Nullable(String))')) OR arrayExists(x -> x = ?, dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata[].positions`.`ratings`, 'Array(Nullable(String))'))), dynamicElement(`body_json.interests[].entities[].reviews[].entries[].metadata`.`positions`, 'Array(JSON(max_dynamic_types=0, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities[].reviews[].entries`.`metadata`, 'Array(JSON(max_dynamic_types=1, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities[].reviews`.`entries`, 'Array(JSON(max_dynamic_types=2, max_dynamic_paths=0))')), dynamicElement(`body_json.interests[].entities`.`reviews`, 'Array(JSON(max_dynamic_types=4, max_dynamic_paths=0))')), dynamicElement(`body_json.interests`.`entities`, 'Array(JSON(max_dynamic_types=8, max_dynamic_paths=0))')), dynamicElement(body_json.`interests`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')))) AND timestamp >= ? AND ts_bucket_start >= ? AND timestamp < ? AND ts_bucket_start <= ? LIMIT ?",Args: []any{uint64(1747945619), uint64(1747983448), "%Good%", "Good", "%Good%", "Good", "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), 10},- Warnings: []string{"Key `interests[].entities[].reviews[].entries[].metadata[].positions[].ratings` is ambiguous, found 2 different combinations of field context / data type: [name=interests[].entities[].reviews[].entries[].metadata[].positions[].ratings,context=body,datatype=[]int64 name=interests[].entities[].reviews[].entries[].metadata[].positions[].ratings,context=body,datatype=[]string]."},+ Warnings: []string{"Key `interests[].entities[].reviews[].entries[].metadata[].positions[].ratings` is ambiguous, found 2 different combinations of field context / data type: [name=interests[].entities[].reviews[].entries[].metadata[].positions[].ratings,context=body,datatype=[]int64,jsondatatype=Array(Nullable(Int64)) name=interests[].entities[].reviews[].entries[].metadata[].positions[].ratings,context=body,datatype=[]string,jsondatatype=Array(Nullable(String))]."},},expectedErr: nil,},@@ -357,7 +357,7 @@ func TestStatementBuilderListQueryBody(t *testing.T) {expected: qbtypes.Statement{Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_logs.distributed_logs_v2_resource WHERE true AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT timestamp, id, trace_id, span_id, trace_flags, severity_text, severity_number, scope_name, scope_version, body, body_json, body_json_promoted, attributes_string, attributes_number, attributes_bool, resources_string, scope_string FROM signoz_logs.distributed_logs_v2 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND ((LOWER(toString(dynamicElement(body_json.`user.age`, 'Int64'))) LIKE LOWER(?)) OR (LOWER(dynamicElement(body_json.`user.age`, 'String')) LIKE LOWER(?))) AND timestamp >= ? AND ts_bucket_start >= ? AND timestamp < ? AND ts_bucket_start <= ? LIMIT ?",Args: []any{uint64(1747945619), uint64(1747983448), "%25%", "%25%", "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), 10},- Warnings: []string{"Key `user.age` is ambiguous, found 2 different combinations of field context / data type: [name=user.age,context=body,datatype=int64 name=user.age,context=body,datatype=string]."},+ Warnings: []string{"Key `user.age` is ambiguous, found 2 different combinations of field context / data type: [name=user.age,context=body,datatype=int64,jsondatatype=Int64 name=user.age,context=body,datatype=string,jsondatatype=String]."},},expectedErr: nil,},@@ -475,7 +475,7 @@ func TestStatementBuilderListQueryBodyPromoted(t *testing.T) {expected: qbtypes.Statement{Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_logs.distributed_logs_v2_resource WHERE true AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT timestamp, id, trace_id, span_id, trace_flags, severity_text, severity_number, scope_name, scope_version, body, body_json, body_json_promoted, attributes_string, attributes_number, attributes_bool, resources_string, scope_string FROM signoz_logs.distributed_logs_v2 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND ((arrayExists(`body_json.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))')) OR arrayExists(toFloat64(x) -> toFloat64(x) = ?, dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))'))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')) OR arrayExists(`body_json_promoted.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Nullable(Float64))')) OR arrayExists(toFloat64(x) -> toFloat64(x) = ?, dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Nullable(Float64))'))), dynamicElement(body_json_promoted.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=256))'))) OR (arrayExists(`body_json.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), arrayMap(x->dynamicElement(x, 'Float64'), arrayFilter(x->(dynamicType(x) = 'Float64'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)')))) OR arrayExists(toFloat64(x) -> toFloat64(x) = ?, arrayMap(x->dynamicElement(x, 'Float64'), arrayFilter(x->(dynamicType(x) = 'Float64'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)'))))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')) OR arrayExists(`body_json_promoted.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), arrayMap(x->dynamicElement(x, 'Float64'), arrayFilter(x->(dynamicType(x) = 'Float64'), dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Dynamic)')))) OR arrayExists(toFloat64(x) -> toFloat64(x) = ?, arrayMap(x->dynamicElement(x, 'Float64'), arrayFilter(x->(dynamicType(x) = 'Float64'), dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Dynamic)'))))), dynamicElement(body_json_promoted.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=256))')))) AND timestamp >= ? AND ts_bucket_start >= ? AND timestamp < ? AND ts_bucket_start <= ? LIMIT ?",Args: []any{uint64(1747945619), uint64(1747983448), "%1.65%", 1.65, "%1.65%", 1.65, "%1.65%", 1.65, "%1.65%", 1.65, "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), 10},- Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64 name=education[].parameters,context=body,datatype=[]dynamic]."},+ Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64,materialized=true,jsondatatype=Array(Nullable(Float64)) name=education[].parameters,context=body,datatype=[]dynamic,materialized=true,jsondatatype=Array(Dynamic)]."},},expectedErr: nil,},@@ -504,7 +504,7 @@ func TestStatementBuilderListQueryBodyPromoted(t *testing.T) {expected: qbtypes.Statement{Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_logs.distributed_logs_v2_resource WHERE true AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT timestamp, id, trace_id, span_id, trace_flags, severity_text, severity_number, scope_name, scope_version, body, body_json, body_json_promoted, attributes_string, attributes_number, attributes_bool, resources_string, scope_string FROM signoz_logs.distributed_logs_v2 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND ((arrayExists(`body_json.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))')) OR arrayExists(x -> x = ?, dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))'))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')) OR arrayExists(`body_json_promoted.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Nullable(Float64))')) OR arrayExists(x -> x = ?, dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Nullable(Float64))'))), dynamicElement(body_json_promoted.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=256))'))) OR (arrayExists(`body_json.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), arrayMap(x->dynamicElement(x, 'Bool'), arrayFilter(x->(dynamicType(x) = 'Bool'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)')))) OR arrayExists(x -> x = ?, arrayMap(x->dynamicElement(x, 'Bool'), arrayFilter(x->(dynamicType(x) = 'Bool'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)'))))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')) OR arrayExists(`body_json_promoted.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), arrayMap(x->dynamicElement(x, 'Bool'), arrayFilter(x->(dynamicType(x) = 'Bool'), dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Dynamic)')))) OR arrayExists(x -> x = ?, arrayMap(x->dynamicElement(x, 'Bool'), arrayFilter(x->(dynamicType(x) = 'Bool'), dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Dynamic)'))))), dynamicElement(body_json_promoted.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=256))')))) AND timestamp >= ? AND ts_bucket_start >= ? AND timestamp < ? AND ts_bucket_start <= ? LIMIT ?",Args: []any{uint64(1747945619), uint64(1747983448), "%true%", true, "%true%", true, "%true%", true, "%true%", true, "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), 10},- Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64 name=education[].parameters,context=body,datatype=[]dynamic]."},+ Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64,materialized=true,jsondatatype=Array(Nullable(Float64)) name=education[].parameters,context=body,datatype=[]dynamic,materialized=true,jsondatatype=Array(Dynamic)]."},},expectedErr: nil,},@@ -519,7 +519,7 @@ func TestStatementBuilderListQueryBodyPromoted(t *testing.T) {expected: qbtypes.Statement{Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_logs.distributed_logs_v2_resource WHERE true AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT timestamp, id, trace_id, span_id, trace_flags, severity_text, severity_number, scope_name, scope_version, body, body_json, body_json_promoted, attributes_string, attributes_number, attributes_bool, resources_string, scope_string FROM signoz_logs.distributed_logs_v2 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND ((arrayExists(`body_json.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))')) OR arrayExists(toString(x) -> toString(x) = ?, dynamicElement(`body_json.education`.`parameters`, 'Array(Nullable(Float64))'))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')) OR arrayExists(`body_json_promoted.education`-> (arrayExists(toString(x) -> LOWER(toString(x)) LIKE LOWER(?), dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Nullable(Float64))')) OR arrayExists(toString(x) -> toString(x) = ?, dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Nullable(Float64))'))), dynamicElement(body_json_promoted.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=256))'))) OR (arrayExists(`body_json.education`-> (arrayExists(x -> LOWER(x) LIKE LOWER(?), arrayMap(x->dynamicElement(x, 'String'), arrayFilter(x->(dynamicType(x) = 'String'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)')))) OR arrayExists(x -> x = ?, arrayMap(x->dynamicElement(x, 'String'), arrayFilter(x->(dynamicType(x) = 'String'), dynamicElement(`body_json.education`.`parameters`, 'Array(Dynamic)'))))), dynamicElement(body_json.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=0))')) OR arrayExists(`body_json_promoted.education`-> (arrayExists(x -> LOWER(x) LIKE LOWER(?), arrayMap(x->dynamicElement(x, 'String'), arrayFilter(x->(dynamicType(x) = 'String'), dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Dynamic)')))) OR arrayExists(x -> x = ?, arrayMap(x->dynamicElement(x, 'String'), arrayFilter(x->(dynamicType(x) = 'String'), dynamicElement(`body_json_promoted.education`.`parameters`, 'Array(Dynamic)'))))), dynamicElement(body_json_promoted.`education`, 'Array(JSON(max_dynamic_types=16, max_dynamic_paths=256))')))) AND timestamp >= ? AND ts_bucket_start >= ? AND timestamp < ? AND ts_bucket_start <= ? LIMIT ?",Args: []any{uint64(1747945619), uint64(1747983448), "%passed%", "passed", "%passed%", "passed", "%passed%", "passed", "%passed%", "passed", "1747947419000000000", uint64(1747945619), "1747983448000000000", uint64(1747983448), 10},- Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64 name=education[].parameters,context=body,datatype=[]dynamic]."},+ Warnings: []string{"Key `education[].parameters` is ambiguous, found 2 different combinations of field context / data type: [name=education[].parameters,context=body,datatype=[]float64,materialized=true,jsondatatype=Array(Nullable(Float64)) name=education[].parameters,context=body,datatype=[]dynamic,materialized=true,jsondatatype=Array(Dynamic)]."},},expectedErr: nil,},diff --git a/pkg/telemetrylogs/statement_builder.go b/pkg/telemetrylogs/statement_builder.goindex e262dd67cf..33d50c8c9f 100644--- a/pkg/telemetrylogs/statement_builder.go+++ b/pkg/telemetrylogs/statement_builder.go@@ -71,7 +71,7 @@ func (b *logQueryStatementBuilder) Build(return nil, err}- b.adjustKeys(ctx, keys, query)+ query = b.adjustKeys(ctx, keys, query, requestType)// Create SQL builderq := sqlbuilder.NewSelectBuilder()@@ -104,8 +104,22 @@ func getKeySelectors(query qbtypes.QueryBuilderQuery[qbtypes.LogAggregation]) []for idx := range query.GroupBy {groupBy := query.GroupBy[idx]- selectors := querybuilder.QueryStringToKeysSelectors(groupBy.TelemetryFieldKey.Name)- keySelectors = append(keySelectors, selectors...)+ keySelectors = append(keySelectors, &telemetrytypes.FieldKeySelector{+ Name: groupBy.Name,+ Signal: telemetrytypes.SignalLogs,+ FieldContext: groupBy.FieldContext,+ FieldDataType: groupBy.FieldDataType,+ })+ }++ for idx := range query.SelectFields {+ selectField := query.SelectFields[idx]+ keySelectors = append(keySelectors, &telemetrytypes.FieldKeySelector{+ Name: selectField.Name,+ Signal: telemetrytypes.SignalLogs,+ FieldContext: selectField.FieldContext,+ FieldDataType: selectField.FieldDataType,+ })}for idx := range query.Order {@@ -125,88 +139,48 @@ func getKeySelectors(query qbtypes.QueryBuilderQuery[qbtypes.LogAggregation]) []return keySelectors}-func (b *logQueryStatementBuilder) adjustKeys(ctx context.Context, keys map[string][]*telemetrytypes.TelemetryFieldKey, query qbtypes.QueryBuilderQuery[qbtypes.LogAggregation]) {- // for group by / order by, if there is a key- // that exactly matches the name of intrinsic field but has- // a field context or data type that doesn't match the field context or data type of the- // intrinsic field,- // and there is no additional key present in the data with the incoming key match,- // then override the given context with- // intrinsic field context and data type- // Why does that happen? Because we have a lot of dashboards created by users and shared over web- // that has incorrect context or data type populated so we fix it- // note: this override happens only when there is no match; if there is a match,- // we can't make decision on behalf of users so we let it use unmodified-- // example: {"key": "severity_text","type": "tag","dataType": "string"}- // This is sent as "tag", when it's not, this was earlier managed with- // `isColumn`, which we don't have in v5 (because it's not a user concern whether it's mat col or not)- // Such requests as-is look for attributes, the following code exists to handle them- checkMatch := func(k *telemetrytypes.TelemetryFieldKey) {- var overallMatch bool-- findMatch := func(staticKeys map[string]telemetrytypes.TelemetryFieldKey) bool {- // for a given key `k`, iterate over the metadata keys `keys`- // and see if there is any exact match- match := false- for _, mapKey := range keys[k.Name] {- if mapKey.FieldContext == k.FieldContext && mapKey.FieldDataType == k.FieldDataType {- match = true- }- }- // we don't have exact match, then it's doesn't exist in attribute or resource attribute- // use the intrinsic/calculated field- if !match {- b.logger.InfoContext(ctx, "overriding the field context and data type", "key", k.Name)- k.FieldContext = staticKeys[k.Name].FieldContext- k.FieldDataType = staticKeys[k.Name].FieldDataType- }- return match- }+func (b *logQueryStatementBuilder) adjustKeys(ctx context.Context, keys map[string][]*telemetrytypes.TelemetryFieldKey, query qbtypes.QueryBuilderQuery[qbtypes.LogAggregation], requestType qbtypes.RequestType) qbtypes.QueryBuilderQuery[qbtypes.LogAggregation] {+ /*+ Adjust keys for alias expressions in aggregations+ */+ actions := querybuilder.AdjustKeysForAliasExpressions(&query, requestType)- if _, ok := IntrinsicFields[k.Name]; ok {- overallMatch = overallMatch || findMatch(IntrinsicFields)- }+ /*+ Check if user is using multiple contexts or data types for same field name+ Idea is to use a super set of keys that can satisfy all the usages- if strings.HasPrefix(k.Name, telemetrytypes.BodyJSONStringSearchPrefix) {- k.Name = strings.TrimPrefix(k.Name, telemetrytypes.BodyJSONStringSearchPrefix)- fieldKeys, found := keys[k.Name]- if found && len(fieldKeys) > 0 {- k.FieldContext = fieldKeys[0].FieldContext- k.FieldDataType = fieldKeys[0].FieldDataType- // only attach the JSON data type if there is only one key for the field so incase there are multiple keys.- // it's handled by the fallback expr logic- if len(fieldKeys) == 1 {- k.JSONDataType = fieldKeys[0].JSONDataType- k.Materialized = fieldKeys[0].Materialized- }- k.Indexes = fieldKeys[0].Indexes-- overallMatch = true // because we found a match- } else {- b.logger.InfoContext(ctx, "overriding the field context and data type", "key", k.Name)- k.FieldContext = telemetrytypes.FieldContextBody- k.FieldDataType = telemetrytypes.FieldDataTypeString- k.JSONDataType = &telemetrytypes.String- }- }+ For example, lets consider model_id exists in both attributes and resources+ And user is trying to use `attribute.model_id` and `model_id`.- if !overallMatch {- // check if all the key for the given field have been materialized, if so- // set the key to materialized- materilized := true- for _, key := range keys[k.Name] {- materilized = materilized && key.Materialized- }- k.Materialized = materilized- }- }+ In this case, we'll remove the context from `attribute.model_id`+ and make it just `model_id` and remove the duplicate entry.+ Same goes with data types.+ Consider user is using http.status_code:number and http.status_code+ In this case, we'll remove the data type from http.status_code:number+ and make it just http.status_code and remove the duplicate entry.+ */++ actions = append(actions, querybuilder.AdjustDuplicateKeys(&query)...)++ /*+ Now adjust each key to have correct context and data type+ Here we try to make intelligent guesses which work for all users (not just majority)+ Reason for doing this is to not create an unexpected behavior for users+ */+ for idx := range query.SelectFields {+ actions = append(actions, b.adjustKey(&query.SelectFields[idx], keys)...)+ }for idx := range query.GroupBy {- checkMatch(&query.GroupBy[idx].TelemetryFieldKey)+ actions = append(actions, b.adjustKey(&query.GroupBy[idx].TelemetryFieldKey, keys)...)}for idx := range query.Order {- checkMatch(&query.Order[idx].Key.TelemetryFieldKey)+ actions = append(actions, b.adjustKey(&query.Order[idx].Key.TelemetryFieldKey, keys)...)+ }++ for _, action := range actions {+ // TODO: change to debug level once we are confident about the behavior+ b.logger.InfoContext(ctx, "key adjustment action", "action", action)}keys["id"] = []*telemetrytypes.TelemetryFieldKey{@@ -217,6 +191,21 @@ func (b *logQueryStatementBuilder) adjustKeys(ctx context.Context, keys map[striFieldDataType: telemetrytypes.FieldDataTypeString,},}++ return query+}++func (b *logQueryStatementBuilder) adjustKey(key *telemetrytypes.TelemetryFieldKey, keys map[string][]*telemetrytypes.TelemetryFieldKey) []string {++ // First check if it matches with any intrinsic fields+ var intrinsicOrCalculatedField telemetrytypes.TelemetryFieldKey+ if _, ok := IntrinsicFields[key.Name]; ok {+ intrinsicOrCalculatedField = IntrinsicFields[key.Name]+ return querybuilder.AdjustKey(key, keys, &intrinsicOrCalculatedField)+ }++ return querybuilder.AdjustKey(key, keys, nil)+}// buildListQuery builds a query for list panel typediff --git a/pkg/telemetrylogs/stmt_builder_test.go b/pkg/telemetrylogs/stmt_builder_test.goindex ca489d9e28..69415e18da 100644--- a/pkg/telemetrylogs/stmt_builder_test.go+++ b/pkg/telemetrylogs/stmt_builder_test.go@@ -633,3 +633,224 @@ func TestStatementBuilderListQueryServiceCollision(t *testing.T) {})}}++func TestAdjustKey(t *testing.T) {+ cases := []struct {+ name string+ inputKey telemetrytypes.TelemetryFieldKey+ keysMap map[string][]*telemetrytypes.TelemetryFieldKey+ expectedKey telemetrytypes.TelemetryFieldKey+ }{+ {+ name: "intrinsic field with no other key match - use intrinsic",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "severity_text",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: IntrinsicFields["severity_text"],+ },+ {+ name: "intrinsic field with other key match - no override",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "body",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: map[string][]*telemetrytypes.TelemetryFieldKey{+ "body": {+ {+ Name: "body",+ FieldContext: telemetrytypes.FieldContextBody,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ {+ Name: "body",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ },+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "body",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ {+ name: "json field with no context specified",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "severity_number",+ FieldContext: telemetrytypes.FieldContextBody,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "severity_number",+ FieldContext: telemetrytypes.FieldContextBody,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ },+ },+ {+ name: "single matching key in metadata",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: *buildCompleteFieldKeyMap()["service.name"][0],+ },+ {+ name: "single matching key with incorrect context specified - no override",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ {+ name: "single matching key with no context specified - override",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: *buildCompleteFieldKeyMap()["service.name"][0],+ },+ {+ name: "multiple matching keys - all materialized",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "multi.mat.key",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "multi.mat.key",+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ },+ {+ name: "multiple matching keys - mixed materialization",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "mixed.materialization.key",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "mixed.materialization.key",+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ {+ name: "multiple matching keys with context specified",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "mixed.materialization.key",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: *buildCompleteFieldKeyMap()["mixed.materialization.key"][0],+ },+ {+ name: "no matching keys - unknown field",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "unknown.field",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "unknown.field",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ Materialized: false,+ },+ },+ {+ name: "no matching keys with context filter",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "unknown.field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "unknown.field",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ Materialized: false,+ },+ },+ {+ name: "materialized field",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "mat.key",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: *buildCompleteFieldKeyMap()["mat.key"][0],+ },+ {+ name: "non-materialized field",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "user.id",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: *buildCompleteFieldKeyMap()["user.id"][0],+ },+ }++ fm := NewFieldMapper()+ mockMetadataStore := telemetrytypestest.NewMockMetadataStore()+ mockMetadataStore.KeysMap = buildCompleteFieldKeyMapCollision()+ cb := NewConditionBuilder(fm, mockMetadataStore)++ aggExprRewriter := querybuilder.NewAggExprRewriter(instrumentationtest.New().ToProviderSettings(), nil, fm, cb, nil)++ resourceFilterStmtBuilder := resourceFilterStmtBuilder()++ statementBuilder := NewLogQueryStatementBuilder(+ instrumentationtest.New().ToProviderSettings(),+ mockMetadataStore,+ fm,+ cb,+ resourceFilterStmtBuilder,+ aggExprRewriter,+ DefaultFullTextColumn,+ GetBodyJSONKey,+ )++ for _, c := range cases {+ t.Run(c.name, func(t *testing.T) {+ // Create a copy of the input key to avoid modifying the original+ key := c.inputKey++ // Call adjustKey+ statementBuilder.adjustKey(&key, c.keysMap)++ // Verify the key was adjusted as expected+ require.Equal(t, c.expectedKey.Name, key.Name, "key name should match")+ require.Equal(t, c.expectedKey.FieldContext, key.FieldContext, "field context should match")+ require.Equal(t, c.expectedKey.FieldDataType, key.FieldDataType, "field data type should match")+ require.Equal(t, c.expectedKey.Materialized, key.Materialized, "materialized should match")+ require.Equal(t, c.expectedKey.JSONDataType, key.JSONDataType, "json data type should match")+ require.Equal(t, c.expectedKey.Indexes, key.Indexes, "json exists should match")+ })+ }+}diff --git a/pkg/telemetrylogs/test_data.go b/pkg/telemetrylogs/test_data.goindex da4045f299..4b208e5b63 100644--- a/pkg/telemetrylogs/test_data.go+++ b/pkg/telemetrylogs/test_data.go@@ -322,6 +322,18 @@ func buildCompleteFieldKeyMap() map[string][]*telemetrytypes.TelemetryFieldKey {FieldDataType: telemetrytypes.FieldDataTypeString,},},+ "severity_number": {+ {+ Name: "severity_number",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ },+ {+ Name: "severity_number",+ FieldContext: telemetrytypes.FieldContextBody,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ },+ },"created_at": {{Name: "created_at",@@ -890,6 +902,42 @@ func buildCompleteFieldKeyMap() map[string][]*telemetrytypes.TelemetryFieldKey {FieldDataType: telemetrytypes.FieldDataTypeString,},},+ "mixed.materialization.key": {+ {+ Name: "mixed.materialization.key",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ {+ Name: "mixed.materialization.key",+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ "multi.mat.key": {+ {+ Name: "multi.mat.key",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ {+ Name: "multi.mat.key",+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ },+ "mat.key": {+ {+ Name: "mat.key",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ },}for _, keys := range keysMap {@@ -942,6 +990,14 @@ func buildCompleteFieldKeyMapCollision() map[string][]*telemetrytypes.TelemetryFFieldDataType: telemetrytypes.FieldDataTypeString,},},+ "materialized.key.name": {+ {+ Name: "materialized.key.name",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ },}for _, keys := range keysMap {diff --git a/pkg/telemetrytraces/statement_builder.go b/pkg/telemetrytraces/statement_builder.goindex 9a6082b358..77134f547e 100644--- a/pkg/telemetrytraces/statement_builder.go+++ b/pkg/telemetrytraces/statement_builder.go@@ -74,7 +74,7 @@ func (b *traceQueryStatementBuilder) Build(return nil, err}- b.adjustKeys(ctx, keys, query)+ query = b.adjustKeys(ctx, keys, query, requestType)// Check if filter contains trace_id(s) and optimize time range if neededif query.Filter != nil && query.Filter.Expression != "" && b.telemetryStore != nil {@@ -126,23 +126,29 @@ func getKeySelectors(query qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation])for idx := range query.GroupBy {groupBy := query.GroupBy[idx]- selectors := querybuilder.QueryStringToKeysSelectors(groupBy.TelemetryFieldKey.Name)- keySelectors = append(keySelectors, selectors...)+ keySelectors = append(keySelectors, &telemetrytypes.FieldKeySelector{+ Name: groupBy.Name,+ Signal: telemetrytypes.SignalTraces,+ FieldContext: groupBy.FieldContext,+ FieldDataType: groupBy.FieldDataType,+ })}for idx := range query.SelectFields {keySelectors = append(keySelectors, &telemetrytypes.FieldKeySelector{- Name: query.SelectFields[idx].Name,- Signal: telemetrytypes.SignalTraces,- FieldContext: query.SelectFields[idx].FieldContext,+ Name: query.SelectFields[idx].Name,+ Signal: telemetrytypes.SignalTraces,+ FieldContext: query.SelectFields[idx].FieldContext,+ FieldDataType: query.SelectFields[idx].FieldDataType,})}for idx := range query.Order {keySelectors = append(keySelectors, &telemetrytypes.FieldKeySelector{- Name: query.Order[idx].Key.Name,- Signal: telemetrytypes.SignalTraces,- FieldContext: query.Order[idx].Key.FieldContext,+ Name: query.Order[idx].Key.Name,+ Signal: telemetrytypes.SignalTraces,+ FieldContext: query.Order[idx].Key.FieldContext,+ FieldDataType: query.Order[idx].Key.FieldDataType,})}@@ -154,77 +160,47 @@ func getKeySelectors(query qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation])return keySelectors}-func (b *traceQueryStatementBuilder) adjustKeys(ctx context.Context, keys map[string][]*telemetrytypes.TelemetryFieldKey, query qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]) {- // for group by / order by / selected fields, if there is a key- // that exactly matches the name of intrinsic / calculated field but has- // a field context or data type that doesn't match the field context or data type of the- // intrinsic field,- // and there is no additional key present in the data with the incoming key match,- // then override the given context with- // intrinsic / calculated field context and data type- // Why does that happen? Because we have a lot of assets created by users and shared over web- // that has incorrect context or data type populated so we fix it- // note: this override happens only when there is no match; if there is a match,- // we can't make decision on behalf of users so we let it use unmodified-- // example: {"key": "httpRoute","type": "tag","dataType": "string"}- // This is sent as "tag", when it's not, this was earlier managed with- // `isColumn`, which we don't have in v5 (because it's not a user concern whether it's mat col or not)- // Such requests as-is look for attributes, the following code exists to handle them- checkMatch := func(k *telemetrytypes.TelemetryFieldKey) {- var overallMatch bool-- findMatch := func(staticKeys map[string]telemetrytypes.TelemetryFieldKey) bool {- // for a given key `k`, iterate over the metadata keys `keys`- // and see if there is any exact match- match := false- for _, mapKey := range keys[k.Name] {- if mapKey.FieldContext == k.FieldContext && mapKey.FieldDataType == k.FieldDataType {- match = true- }- }- // we don't have exact match, then it's doesn't exist in attribute or resource attribute- // use the intrinsic/calculated field- if !match {- b.logger.InfoContext(ctx, "overriding the field context and data type", "key", k.Name)- k.FieldContext = staticKeys[k.Name].FieldContext- k.FieldDataType = staticKeys[k.Name].FieldDataType- }- return match- }+func (b *traceQueryStatementBuilder) adjustKeys(ctx context.Context, keys map[string][]*telemetrytypes.TelemetryFieldKey, query qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation], requestType qbtypes.RequestType) qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation] {- if _, ok := IntrinsicFields[k.Name]; ok {- overallMatch = overallMatch || findMatch(IntrinsicFields)- }- if _, ok := CalculatedFields[k.Name]; ok {- overallMatch = overallMatch || findMatch(CalculatedFields)- }- if _, ok := IntrinsicFieldsDeprecated[k.Name]; ok {- overallMatch = overallMatch || findMatch(IntrinsicFieldsDeprecated)- }- if _, ok := CalculatedFieldsDeprecated[k.Name]; ok {- overallMatch = overallMatch || findMatch(CalculatedFieldsDeprecated)- }+ // Adjust keys for alias expressions in aggregations+ actions := querybuilder.AdjustKeysForAliasExpressions(&query, requestType)- if !overallMatch {- // check if all the key for the given field have been materialized, if so- // set the key to materialized- materilized := true- for _, key := range keys[k.Name] {- materilized = materilized && key.Materialized- }- k.Materialized = materilized- }- }+ /*+ Check if user is using multiple contexts or data types for same field name+ Idea is to use a super set of keys that can satisfy all the usages+ For example, lets consider model_id exists in both attributes and resources+ And user is trying to use `attribute.model_id` and `model_id`.++ In this case, we'll remove the context from `attribute.model_id`+ and make it just `model_id` and remove the duplicate entry.++ Same goes with data types.+ Consider user is using http.status_code:number and http.status_code+ In this case, we'll remove the data type from http.status_code:number+ and make it just http.status_code and remove the duplicate entry.+ */++ actions = append(actions, querybuilder.AdjustDuplicateKeys(&query)...)++ /*+ Now adjust each key to have correct context and data type+ Here we try to make intelligent guesses which work for all users (not just majority)+ Reason for doing this is to not create an unexpected behavior for users+ */+ for idx := range query.SelectFields {+ actions = append(actions, b.adjustKey(&query.SelectFields[idx], keys)...)+ }for idx := range query.GroupBy {- checkMatch(&query.GroupBy[idx].TelemetryFieldKey)+ actions = append(actions, b.adjustKey(&query.GroupBy[idx].TelemetryFieldKey, keys)...)}for idx := range query.Order {- checkMatch(&query.Order[idx].Key.TelemetryFieldKey)+ actions = append(actions, b.adjustKey(&query.Order[idx].Key.TelemetryFieldKey, keys)...)}- for idx := range query.SelectFields {- checkMatch(&query.SelectFields[idx])++ for _, action := range actions {+ // TODO: change to debug level once we are confident about the behavior+ b.logger.InfoContext(ctx, "key adjustment action", "action", action)}// add deprecated fields only during statement building@@ -246,6 +222,42 @@ func (b *traceQueryStatementBuilder) adjustKeys(ctx context.Context, keys map[stkeys[fieldKeyName] = append(keys[fieldKeyName], &fieldKey)}}++ return query+}++func (b *traceQueryStatementBuilder) adjustKey(key *telemetrytypes.TelemetryFieldKey, keys map[string][]*telemetrytypes.TelemetryFieldKey) []string {++ // for recording actions taken+ actions := []string{}+ /*+ Check if this key is an intrinsic or calculated field++ For example: trace_id (intrinsic), response_status_code (calculated).+ */+ var isIntrinsicOrCalculatedField bool+ var intrinsicOrCalculatedField telemetrytypes.TelemetryFieldKey+ if _, ok := IntrinsicFields[key.Name]; ok {+ isIntrinsicOrCalculatedField = true+ intrinsicOrCalculatedField = IntrinsicFields[key.Name]+ } else if _, ok := CalculatedFields[key.Name]; ok {+ isIntrinsicOrCalculatedField = true+ intrinsicOrCalculatedField = CalculatedFields[key.Name]+ } else if _, ok := IntrinsicFieldsDeprecated[key.Name]; ok {+ isIntrinsicOrCalculatedField = true+ intrinsicOrCalculatedField = IntrinsicFieldsDeprecated[key.Name]+ } else if _, ok := CalculatedFieldsDeprecated[key.Name]; ok {+ isIntrinsicOrCalculatedField = true+ intrinsicOrCalculatedField = CalculatedFieldsDeprecated[key.Name]+ }++ if isIntrinsicOrCalculatedField {+ actions = append(actions, querybuilder.AdjustKey(key, keys, &intrinsicOrCalculatedField)...)+ } else {+ actions = append(actions, querybuilder.AdjustKey(key, keys, nil)...)+ }++ return actions}// buildListQuery builds a query for list panel typediff --git a/pkg/telemetrytraces/stmt_builder_test.go b/pkg/telemetrytraces/stmt_builder_test.goindex a61a6172b5..1331981c49 100644--- a/pkg/telemetrytraces/stmt_builder_test.go+++ b/pkg/telemetrytraces/stmt_builder_test.go@@ -5,6 +5,7 @@ import ("testing""time"+ "github.com/SigNoz/signoz/pkg/errors""github.com/SigNoz/signoz/pkg/instrumentation/instrumentationtest""github.com/SigNoz/signoz/pkg/querybuilder""github.com/SigNoz/signoz/pkg/querybuilder/resourcefilter"@@ -201,6 +202,7 @@ func TestStatementBuilder(t *testing.T) {Aggregations: []qbtypes.TraceAggregation{{Expression: "sum(metric.max_count)",+ Alias: "metric.max_count",},},Filter: &qbtypes.Filter{@@ -214,9 +216,20 @@ func TestStatementBuilder(t *testing.T) {},},},+ Order: []qbtypes.OrderBy{+ {+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "max_count",+ FieldContext: telemetrytypes.FieldContextMetric,+ },+ },+ Direction: qbtypes.OrderDirectionDesc,+ },+ },},expected: qbtypes.Statement{- Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_traces.distributed_traces_v3_resource WHERE (simpleJSONExtractString(labels, 'service.name') = ? AND labels LIKE ? AND labels LIKE ?) AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?), __limit_cte AS (SELECT toString(multiIf(multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, mapContains(resources_string, 'service.name'), resources_string['service.name'], NULL) IS NOT NULL, multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, mapContains(resources_string, 'service.name'), resources_string['service.name'], NULL), NULL)) AS `service.name`, sum(multiIf(mapContains(attributes_number, 'metric.max_count') = ?, toFloat64(attributes_number['metric.max_count']), NULL)) AS __result_0 FROM signoz_traces.distributed_signoz_index_v3 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND true AND timestamp >= ? AND timestamp < ? AND ts_bucket_start >= ? AND ts_bucket_start <= ? GROUP BY `service.name` ORDER BY __result_0 DESC LIMIT ?) SELECT toStartOfInterval(timestamp, INTERVAL 30 SECOND) AS ts, toString(multiIf(multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, mapContains(resources_string, 'service.name'), resources_string['service.name'], NULL) IS NOT NULL, multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, mapContains(resources_string, 'service.name'), resources_string['service.name'], NULL), NULL)) AS `service.name`, sum(multiIf(mapContains(attributes_number, 'metric.max_count') = ?, toFloat64(attributes_number['metric.max_count']), NULL)) AS __result_0 FROM signoz_traces.distributed_signoz_index_v3 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND true AND timestamp >= ? AND timestamp < ? AND ts_bucket_start >= ? AND ts_bucket_start <= ? AND (`service.name`) GLOBAL IN (SELECT `service.name` FROM __limit_cte) GROUP BY ts, `service.name`",+ Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_traces.distributed_traces_v3_resource WHERE (simpleJSONExtractString(labels, 'service.name') = ? AND labels LIKE ? AND labels LIKE ?) AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?), __limit_cte AS (SELECT toString(multiIf(multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, mapContains(resources_string, 'service.name'), resources_string['service.name'], NULL) IS NOT NULL, multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, mapContains(resources_string, 'service.name'), resources_string['service.name'], NULL), NULL)) AS `service.name`, sum(multiIf(mapContains(attributes_number, 'metric.max_count') = ?, toFloat64(attributes_number['metric.max_count']), NULL)) AS __result_0 FROM signoz_traces.distributed_signoz_index_v3 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND true AND timestamp >= ? AND timestamp < ? AND ts_bucket_start >= ? AND ts_bucket_start <= ? GROUP BY `service.name` ORDER BY __result_0 desc LIMIT ?) SELECT toStartOfInterval(timestamp, INTERVAL 30 SECOND) AS ts, toString(multiIf(multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, mapContains(resources_string, 'service.name'), resources_string['service.name'], NULL) IS NOT NULL, multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, mapContains(resources_string, 'service.name'), resources_string['service.name'], NULL), NULL)) AS `service.name`, sum(multiIf(mapContains(attributes_number, 'metric.max_count') = ?, toFloat64(attributes_number['metric.max_count']), NULL)) AS __result_0 FROM signoz_traces.distributed_signoz_index_v3 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND true AND timestamp >= ? AND timestamp < ? AND ts_bucket_start >= ? AND ts_bucket_start <= ? AND (`service.name`) GLOBAL IN (SELECT `service.name` FROM __limit_cte) GROUP BY ts, `service.name` ORDER BY ts desc",Args: []any{"redis-manual", "%service.name%", "%service.name\":\"redis-manual%", uint64(1747945619), uint64(1747983448), true, "1747947419000000000", "1747983448000000000", uint64(1747945619), uint64(1747983448), 10, true, "1747947419000000000", "1747983448000000000", uint64(1747945619), uint64(1747983448)},},expectedErr: nil,@@ -519,6 +532,134 @@ func TestStatementBuilderListQuery(t *testing.T) {},expectedErr: nil,},+ {+ name: "List query with legacy fields with mixed materialization field",+ requestType: qbtypes.RequestTypeRaw,+ query: qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]{+ Signal: telemetrytypes.SignalTraces,+ StepInterval: qbtypes.Step{Duration: 30 * time.Second},+ Filter: &qbtypes.Filter{+ Expression: "service.name = 'redis-manual'",+ },+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {+ Name: "name",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ {+ Name: "serviceName",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ {+ Name: "durationNano",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ },+ {+ Name: "httpMethod",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ {+ // "mixed.materialization.key" exists in both attribute and resource,+ // attribute being materialized and resource being non-materialized.+ Name: "mixed.materialization.key",+ },+ },+ Limit: 10,+ },+ expected: qbtypes.Statement{+ Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_traces.distributed_traces_v3_resource WHERE (simpleJSONExtractString(labels, 'service.name') = ? AND labels LIKE ? AND labels LIKE ?) AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT name AS `name`, resource_string_service$$name AS `serviceName`, duration_nano AS `durationNano`, http_method AS `httpMethod`, multiIf(toString(`attribute_string_mixed$$materialization$$key`) != '', toString(`attribute_string_mixed$$materialization$$key`), toString(multiIf(resource.`mixed.materialization.key` IS NOT NULL, resource.`mixed.materialization.key`::String, mapContains(resources_string, 'mixed.materialization.key'), resources_string['mixed.materialization.key'], NULL)) != '', toString(multiIf(resource.`mixed.materialization.key` IS NOT NULL, resource.`mixed.materialization.key`::String, mapContains(resources_string, 'mixed.materialization.key'), resources_string['mixed.materialization.key'], NULL)), NULL) AS `mixed.materialization.key`, timestamp AS `timestamp`, span_id AS `span_id`, trace_id AS `trace_id` FROM signoz_traces.distributed_signoz_index_v3 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND true AND timestamp >= ? AND timestamp < ? AND ts_bucket_start >= ? AND ts_bucket_start <= ? LIMIT ?",+ Args: []any{"redis-manual", "%service.name%", "%service.name\":\"redis-manual%", uint64(1747945619), uint64(1747983448), "1747947419000000000", "1747983448000000000", uint64(1747945619), uint64(1747983448), 10},+ },+ expectedErr: nil,+ },+ {+ name: "List query with legacy fields with mixed materialization field with context provided",+ requestType: qbtypes.RequestTypeRaw,+ query: qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]{+ Signal: telemetrytypes.SignalTraces,+ StepInterval: qbtypes.Step{Duration: 30 * time.Second},+ Filter: &qbtypes.Filter{+ Expression: "service.name = 'redis-manual'",+ },+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {+ Name: "name",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ {+ Name: "serviceName",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ {+ Name: "durationNano",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ },+ {+ Name: "httpMethod",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ {+ // "mixed.materialization.key" exists in both attribute and resource,+ // attribute being materialized and resource being non-materialized.+ Name: "mixed.materialization.key",+ FieldContext: telemetrytypes.FieldContextAttribute,+ },+ },+ Limit: 10,+ },+ expected: qbtypes.Statement{+ Query: "WITH __resource_filter AS (SELECT fingerprint FROM signoz_traces.distributed_traces_v3_resource WHERE (simpleJSONExtractString(labels, 'service.name') = ? AND labels LIKE ? AND labels LIKE ?) AND seen_at_ts_bucket_start >= ? AND seen_at_ts_bucket_start <= ?) SELECT name AS `name`, resource_string_service$$name AS `serviceName`, duration_nano AS `durationNano`, http_method AS `httpMethod`, `attribute_string_mixed$$materialization$$key` AS `mixed.materialization.key`, timestamp AS `timestamp`, span_id AS `span_id`, trace_id AS `trace_id` FROM signoz_traces.distributed_signoz_index_v3 WHERE resource_fingerprint GLOBAL IN (SELECT fingerprint FROM __resource_filter) AND true AND timestamp >= ? AND timestamp < ? AND ts_bucket_start >= ? AND ts_bucket_start <= ? LIMIT ?",+ Args: []any{"redis-manual", "%service.name%", "%service.name\":\"redis-manual%", uint64(1747945619), uint64(1747983448), "1747947419000000000", "1747983448000000000", uint64(1747945619), uint64(1747983448), 10},+ },+ expectedErr: nil,+ },+ {+ name: "List query with legacy fields with field that doesn't exist",+ requestType: qbtypes.RequestTypeRaw,+ query: qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]{+ Signal: telemetrytypes.SignalTraces,+ StepInterval: qbtypes.Step{Duration: 30 * time.Second},+ Filter: &qbtypes.Filter{+ Expression: "service.name = 'redis-manual'",+ },+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {+ Name: "name",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ {+ Name: "serviceName",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ {+ Name: "durationNano",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ },+ {+ Name: "httpMethod",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+ {+ Name: "non-existent.key",+ },+ },+ Limit: 10,+ },+ expected: qbtypes.Statement{},+ expectedErr: errors.NewInvalidInputf(errors.CodeInvalidInput, "field not found"),+ },}fm := NewFieldMapper()@@ -660,7 +801,7 @@ func TestStatementBuilderTraceQuery(t *testing.T) {expectedErr: nil,},{- name: "list query with deprecated filter field",+ name: "list query with deprecated filter field",requestType: qbtypes.RequestTypeTrace,query: qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]{Signal: telemetrytypes.SignalTraces,@@ -712,3 +853,546 @@ func TestStatementBuilderTraceQuery(t *testing.T) {})}}++func TestAdjustKey(t *testing.T) {+ cases := []struct {+ name string+ inputKey telemetrytypes.TelemetryFieldKey+ keysMap map[string][]*telemetrytypes.TelemetryFieldKey+ expectedKey telemetrytypes.TelemetryFieldKey+ }{+ {+ name: "intrinsic field with no metadata match - override",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "trace_id",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: IntrinsicFields["trace_id"],+ },+ {+ name: "intrinsic field with metadata match with incorrect context - override",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "duration_nano",+ FieldContext: telemetrytypes.FieldContextBody, // incorrect context+ FieldDataType: telemetrytypes.FieldDataTypeInt64,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "duration_nano",+ FieldContext: telemetrytypes.FieldContextSpan, // should be corrected+ FieldDataType: telemetrytypes.FieldDataTypeNumber, // modified+ },+ },+ {+ name: "intrinsic field with metadata match with correct context - no override",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "duration_nano",+ FieldContext: telemetrytypes.FieldContextSpan, // correct context+ FieldDataType: telemetrytypes.FieldDataTypeInt64,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "duration_nano",+ FieldContext: telemetrytypes.FieldContextSpan, // should be corrected+ FieldDataType: telemetrytypes.FieldDataTypeInt64, // not modified+ },+ },+ {+ name: "single matching key in metadata - override",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: *buildCompleteFieldKeyMap()["service.name"][0],+ },+ {+ name: "single matching key with context specified - override",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "cart.items_count",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: *buildCompleteFieldKeyMap()["cart.items_count"][0],+ },+ {+ name: "multiple matching keys - all materialized",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "multi.mat.key",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: map[string][]*telemetrytypes.TelemetryFieldKey{+ "multi.mat.key": {+ {+ Name: "multi.mat.key",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ {+ Name: "multi.mat.key",+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeNumber,+ Materialized: true,+ },+ },+ },+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "multi.mat.key",+ Materialized: true,+ },+ },+ {+ name: "multiple matching keys - mixed materialization",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "mixed.materialization.key",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "mixed.materialization.key",+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ {+ name: "multiple matching keys with context specified",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "mixed.materialization.key",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "mixed.materialization.key",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ },+ {+ name: "no matching keys - unknown field",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "unknown.field",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "unknown.field",+ Materialized: false,+ },+ },+ {+ name: "no matching keys with context filter",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ Materialized: false,+ },+ },+ {+ name: "materialized field",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "cart.items_count",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "cart.items_count",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeFloat64,+ Materialized: true,+ },+ },+ {+ name: "non-materialized field",+ inputKey: telemetrytypes.TelemetryFieldKey{+ Name: "user.id",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedKey: telemetrytypes.TelemetryFieldKey{+ Name: "user.id",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ }++ fm := NewFieldMapper()+ cb := NewConditionBuilder(fm)+ mockMetadataStore := telemetrytypestest.NewMockMetadataStore()+ aggExprRewriter := querybuilder.NewAggExprRewriter(instrumentationtest.New().ToProviderSettings(), nil, fm, cb, nil)+ resourceFilterStmtBuilder := resourceFilterStmtBuilder()++ statementBuilder := NewTraceQueryStatementBuilder(+ instrumentationtest.New().ToProviderSettings(),+ mockMetadataStore,+ fm,+ cb,+ resourceFilterStmtBuilder,+ aggExprRewriter,+ nil,+ )++ for _, c := range cases {+ t.Run(c.name, func(t *testing.T) {+ // Create a copy of the input key to avoid modifying the original+ key := c.inputKey++ // Call adjustKey+ statementBuilder.adjustKey(&key, c.keysMap)++ // Verify the key was adjusted as expected+ require.Equal(t, c.expectedKey.Name, key.Name, "key name should match")+ require.Equal(t, c.expectedKey.FieldContext, key.FieldContext, "field context should match")+ require.Equal(t, c.expectedKey.FieldDataType, key.FieldDataType, "field data type should match")+ require.Equal(t, c.expectedKey.Materialized, key.Materialized, "materialized should match")+ })+ }+}++func TestAdjustKeys(t *testing.T) {+ cases := []struct {+ name string+ query qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]+ keysMap map[string][]*telemetrytypes.TelemetryFieldKey+ expectedSelectFields []telemetrytypes.TelemetryFieldKey+ expectedGroupBy []qbtypes.GroupByKey+ expectedOrder []qbtypes.OrderBy+ expectDeprecatedFieldsAdd bool+ }{+ {+ name: "adjust select fields",+ query: qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ {+ Name: "cart.items_count",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedSelectFields: []telemetrytypes.TelemetryFieldKey{+ {+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ {+ Name: "cart.items_count",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeFloat64,+ Materialized: true,+ },+ },+ expectDeprecatedFieldsAdd: true,+ },+ {+ name: "adjust group by fields",+ query: qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]{+ GroupBy: []qbtypes.GroupByKey{+ {+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ {+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "user.id",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ },+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedGroupBy: []qbtypes.GroupByKey{+ {+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ {+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "user.id",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ },+ expectDeprecatedFieldsAdd: true,+ },+ {+ name: "adjust order by fields",+ query: qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]{+ Order: []qbtypes.OrderBy{+ {+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ Direction: qbtypes.OrderDirectionAsc,+ },+ {+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "cart.items_count",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ Direction: qbtypes.OrderDirectionDesc,+ },+ },+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedOrder: []qbtypes.OrderBy{+ {+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ Direction: qbtypes.OrderDirectionAsc,+ },+ {+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "cart.items_count",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeFloat64,+ Materialized: true,+ },+ },+ Direction: qbtypes.OrderDirectionDesc,+ },+ },+ expectDeprecatedFieldsAdd: true,+ },+ {+ name: "adjust all field types together",+ query: qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]{+ SelectFields: []telemetrytypes.TelemetryFieldKey{+ {+ Name: "trace_id",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ GroupBy: []qbtypes.GroupByKey{+ {+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ },+ Order: []qbtypes.OrderBy{+ {+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "user.id",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ FieldDataType: telemetrytypes.FieldDataTypeUnspecified,+ },+ },+ Direction: qbtypes.OrderDirectionDesc,+ },+ },+ },+ keysMap: buildCompleteFieldKeyMap(),+ expectedSelectFields: []telemetrytypes.TelemetryFieldKey{+ {+ Name: "trace_id",+ FieldContext: telemetrytypes.FieldContextSpan,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ expectedGroupBy: []qbtypes.GroupByKey{+ {+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ },+ expectedOrder: []qbtypes.OrderBy{+ {+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "user.id",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },+ Direction: qbtypes.OrderDirectionDesc,+ },+ },+ expectDeprecatedFieldsAdd: true,+ },+ {+ name: "adjust keys for alias expressions in aggregations - order by",+ query: qbtypes.QueryBuilderQuery[qbtypes.TraceAggregation]{+ Aggregations: []qbtypes.TraceAggregation{+ {+ Expression: "sum(span.duration)",+ Alias: "span.duration",+ },+ },+ Order: []qbtypes.OrderBy{+ {+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "duration",+ FieldContext: telemetrytypes.FieldContextSpan,+ },+ },+ Direction: qbtypes.OrderDirectionDesc,+ },+ },+ },+ keysMap: buildCompleteFieldKeyMap(),+ // After alias adjustment, name becomes "span.duration" with FieldContextUnspecified+ // "span.duration" is not in keysMap, so context stays unspecified+ expectedOrder: []qbtypes.OrderBy{+ {+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "span.duration",+ FieldContext: telemetrytypes.FieldContextUnspecified,+ },+ },+ Direction: qbtypes.OrderDirectionDesc,+ },+ },+ expectDeprecatedFieldsAdd: true,+ },+ }++ fm := NewFieldMapper()+ cb := NewConditionBuilder(fm)+ mockMetadataStore := telemetrytypestest.NewMockMetadataStore()+ aggExprRewriter := querybuilder.NewAggExprRewriter(instrumentationtest.New().ToProviderSettings(), nil, fm, cb, nil)+ resourceFilterStmtBuilder := resourceFilterStmtBuilder()++ statementBuilder := NewTraceQueryStatementBuilder(+ instrumentationtest.New().ToProviderSettings(),+ mockMetadataStore,+ fm,+ cb,+ resourceFilterStmtBuilder,+ aggExprRewriter,+ nil,+ )++ for _, c := range cases {+ t.Run(c.name, func(t *testing.T) {+ // Create a deep copy of the keys map to avoid modifying the original+ keysMapCopy := make(map[string][]*telemetrytypes.TelemetryFieldKey)+ for k, v := range c.keysMap {+ keysMapCopy[k] = make([]*telemetrytypes.TelemetryFieldKey, len(v))+ copy(keysMapCopy[k], v)+ }++ // Call adjustKeys+ c.query = statementBuilder.adjustKeys(context.Background(), keysMapCopy, c.query, qbtypes.RequestTypeScalar)++ // Verify select fields were adjusted+ if c.expectedSelectFields != nil {+ require.Len(t, c.query.SelectFields, len(c.expectedSelectFields))+ for i, expected := range c.expectedSelectFields {+ require.Equal(t, expected.Name, c.query.SelectFields[i].Name, "select field %d name should match", i)+ require.Equal(t, expected.FieldContext, c.query.SelectFields[i].FieldContext, "select field %d context should match", i)+ require.Equal(t, expected.FieldDataType, c.query.SelectFields[i].FieldDataType, "select field %d data type should match", i)+ require.Equal(t, expected.Materialized, c.query.SelectFields[i].Materialized, "select field %d materialized should match", i)+ }+ }++ // Verify group by fields were adjusted+ if c.expectedGroupBy != nil {+ require.Len(t, c.query.GroupBy, len(c.expectedGroupBy))+ for i, expected := range c.expectedGroupBy {+ require.Equal(t, expected.TelemetryFieldKey.Name, c.query.GroupBy[i].TelemetryFieldKey.Name, "group by field %d name should match", i)+ require.Equal(t, expected.TelemetryFieldKey.FieldContext, c.query.GroupBy[i].TelemetryFieldKey.FieldContext, "group by field %d context should match", i)+ require.Equal(t, expected.TelemetryFieldKey.FieldDataType, c.query.GroupBy[i].TelemetryFieldKey.FieldDataType, "group by field %d data type should match", i)+ require.Equal(t, expected.TelemetryFieldKey.Materialized, c.query.GroupBy[i].TelemetryFieldKey.Materialized, "group by field %d materialized should match", i)+ }+ }++ // Verify order by fields were adjusted+ if c.expectedOrder != nil {+ require.Len(t, c.query.Order, len(c.expectedOrder))+ for i, expected := range c.expectedOrder {+ require.Equal(t, expected.Key.TelemetryFieldKey.Name, c.query.Order[i].Key.TelemetryFieldKey.Name, "order field %d name should match", i)+ require.Equal(t, expected.Key.TelemetryFieldKey.FieldContext, c.query.Order[i].Key.TelemetryFieldKey.FieldContext, "order field %d context should match", i)+ require.Equal(t, expected.Key.TelemetryFieldKey.FieldDataType, c.query.Order[i].Key.TelemetryFieldKey.FieldDataType, "order field %d data type should match", i)+ require.Equal(t, expected.Key.TelemetryFieldKey.Materialized, c.query.Order[i].Key.TelemetryFieldKey.Materialized, "order field %d materialized should match", i)+ require.Equal(t, expected.Direction, c.query.Order[i].Direction, "order field %d direction should match", i)+ }+ }++ // Verify deprecated fields were added to the keys map+ if c.expectDeprecatedFieldsAdd {+ // Check that at least some deprecated fields were added+ foundDeprecatedField := false+ for fieldName := range IntrinsicFieldsDeprecated {+ if _, ok := keysMapCopy[fieldName]; ok {+ foundDeprecatedField = true+ break+ }+ }+ if !foundDeprecatedField {+ for fieldName := range CalculatedFieldsDeprecated {+ if _, ok := keysMapCopy[fieldName]; ok {+ foundDeprecatedField = true+ break+ }+ }+ }+ require.True(t, foundDeprecatedField, "at least one deprecated field should be added to keys map")+ }+ })+ }+}diff --git a/pkg/telemetrytraces/test_data.go b/pkg/telemetrytraces/test_data.goindex 2c3059e539..209b3c4d18 100644--- a/pkg/telemetrytraces/test_data.go+++ b/pkg/telemetrytraces/test_data.go@@ -62,6 +62,11 @@ func buildCompleteFieldKeyMap() map[string][]*telemetrytypes.TelemetryFieldKey {FieldContext: telemetrytypes.FieldContextSpan,FieldDataType: telemetrytypes.FieldDataTypeInt64,},+ {+ Name: "duration_nano",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeInt64,+ },},"http.method": {{@@ -85,6 +90,20 @@ func buildCompleteFieldKeyMap() map[string][]*telemetrytypes.TelemetryFieldKey {Materialized: true,},},+ "mixed.materialization.key": {+ {+ Name: "mixed.materialization.key",+ FieldContext: telemetrytypes.FieldContextAttribute,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: true,+ },+ {+ Name: "mixed.materialization.key",+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ Materialized: false,+ },+ },"isentrypoint": {{Name: "isentrypoint",diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/validation.go b/pkg/types/querybuildertypes/querybuildertypesv5/validation.goindex d305147d1a..c2fae40db3 100644--- a/pkg/types/querybuildertypes/querybuildertypesv5/validation.go+++ b/pkg/types/querybuildertypes/querybuildertypesv5/validation.go@@ -370,7 +370,12 @@ func (q *QueryBuilderQuery[T]) validateOrderByForAggregation() error {for i, order := range q.Order {orderKey := order.Key.Name- if !validOrderKeys[orderKey] {+ // Also check the context-prefixed key name for alias matching+ // This handles cases where user specifies alias like "span.count_" and+ // order by comes as FieldContext=span, Name=count_+ contextPrefixedKey := fmt.Sprintf("%s.%s", order.Key.FieldContext.StringValue(), order.Key.Name)++ if !validOrderKeys[orderKey] && !validOrderKeys[contextPrefixedKey] {orderId := fmt.Sprintf("order by clause #%d", i+1)if q.Name != "" {orderId = fmt.Sprintf("order by clause #%d in query '%s'", i+1, q.Name)diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go b/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.goindex 37e6f1c006..38f39dfec3 100644--- a/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go+++ b/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go@@ -332,3 +332,184 @@ func TestQueryRangeRequest_ValidateAllQueriesNotDisabled(t *testing.T) {})}}++func TestQueryRangeRequest_ValidateOrderByForAggregation(t *testing.T) {+ tests := []struct {+ name string+ query QueryBuilderQuery[TraceAggregation]+ wantErr bool+ errMsg string+ }{+ {+ name: "order by with context-prefixed alias should pass",+ query: QueryBuilderQuery[TraceAggregation]{+ Name: "A",+ Signal: telemetrytypes.SignalTraces,+ Aggregations: []TraceAggregation{+ {+ Expression: "count()",+ Alias: "span.count_",+ },+ },+ Order: []OrderBy{+ {+ Direction: OrderDirectionDesc,+ Key: OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "count_",+ FieldContext: telemetrytypes.FieldContextSpan,+ },+ },+ },+ },+ },+ wantErr: false,+ },+ {+ name: "order by with alias directly should pass",+ query: QueryBuilderQuery[TraceAggregation]{+ Name: "A",+ Signal: telemetrytypes.SignalTraces,+ Aggregations: []TraceAggregation{+ {+ Expression: "count()",+ Alias: "my_count",+ },+ },+ Order: []OrderBy{+ {+ Direction: OrderDirectionDesc,+ Key: OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "my_count",+ },+ },+ },+ },+ },+ wantErr: false,+ },+ {+ name: "order by with expression should pass",+ query: QueryBuilderQuery[TraceAggregation]{+ Name: "A",+ Signal: telemetrytypes.SignalTraces,+ Aggregations: []TraceAggregation{+ {+ Expression: "count()",+ },+ },+ Order: []OrderBy{+ {+ Direction: OrderDirectionDesc,+ Key: OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "count()",+ },+ },+ },+ },+ },+ wantErr: false,+ },+ {+ name: "order by with invalid key should fail",+ query: QueryBuilderQuery[TraceAggregation]{+ Name: "A",+ Signal: telemetrytypes.SignalTraces,+ Aggregations: []TraceAggregation{+ {+ Expression: "count()",+ Alias: "my_count",+ },+ },+ Order: []OrderBy{+ {+ Direction: OrderDirectionDesc,+ Key: OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "invalid_key",+ },+ },+ },+ },+ },+ wantErr: true,+ errMsg: "invalid order by key",+ },+ {+ name: "order by with group by key should pass",+ query: QueryBuilderQuery[TraceAggregation]{+ Name: "A",+ Signal: telemetrytypes.SignalTraces,+ Aggregations: []TraceAggregation{+ {+ Expression: "count()",+ },+ },+ GroupBy: []GroupByKey{+ {+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ },+ },+ },+ Order: []OrderBy{+ {+ Direction: OrderDirectionAsc,+ Key: OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "service.name",+ },+ },+ },+ },+ },+ wantErr: false,+ },+ {+ name: "order by with resource context-prefixed alias should pass",+ query: QueryBuilderQuery[TraceAggregation]{+ Name: "A",+ Signal: telemetrytypes.SignalTraces,+ Aggregations: []TraceAggregation{+ {+ Expression: "count()",+ Alias: "resource.count_",+ },+ },+ Order: []OrderBy{+ {+ Direction: OrderDirectionDesc,+ Key: OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: "count_",+ FieldContext: telemetrytypes.FieldContextResource,+ },+ },+ },+ },+ },+ wantErr: false,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ err := tt.query.Validate(RequestTypeTimeSeries)+ if tt.wantErr {+ if err == nil {+ t.Errorf("validateOrderByForAggregation() expected error but got none")+ return+ }+ if tt.errMsg != "" && !contains(err.Error(), tt.errMsg) {+ t.Errorf("validateOrderByForAggregation() error = %v, want to contain %v", err.Error(), tt.errMsg)+ }+ } else {+ if err != nil {+ t.Errorf("validateOrderByForAggregation() unexpected error = %v", err)+ }+ }+ })+ }+}\ No newline at end of filediff --git a/pkg/types/telemetrytypes/field.go b/pkg/types/telemetrytypes/field.goindex 6150753f0b..f45209533d 100644--- a/pkg/types/telemetrytypes/field.go+++ b/pkg/types/telemetrytypes/field.go@@ -48,6 +48,22 @@ func (f TelemetryFieldKey) String() string {if f.FieldDataType != FieldDataTypeUnspecified {sb.WriteString(fmt.Sprintf(",datatype=%s", f.FieldDataType.StringValue()))}+ if f.Materialized {+ sb.WriteString(",materialized=true")+ }+ if f.JSONDataType != nil {+ sb.WriteString(fmt.Sprintf(",jsondatatype=%s", f.JSONDataType.StringValue()))+ }+ if len(f.Indexes) > 0 {+ sb.WriteString(",indexes=[")+ for i, index := range f.Indexes {+ if i > 0 {+ sb.WriteString("; ")+ }+ sb.WriteString(fmt.Sprintf("{type=%s, columnExpr=%s, indexExpr=%s}", index.Type.StringValue(), index.ColumnExpression, index.IndexExpression))+ }+ sb.WriteString("]")+ }return sb.String()}@@ -55,6 +71,12 @@ func (f TelemetryFieldKey) Text() string {return TelemetryFieldKeyToText(&f)}+func (f *TelemetryFieldKey) Equal(key *TelemetryFieldKey) bool {+ return f.Name == key.Name &&+ f.FieldContext == key.FieldContext &&+ f.FieldDataType == key.FieldDataType+}+// Normalize parses and normalizes a TelemetryFieldKey by extracting// the field context and data type from the field name if they are not already specified.// This function modifies the key in place.diff --git a/tests/integration/src/querier/04_traces.py b/tests/integration/src/querier/04_traces.pyindex d6b34f6ed4..eb48e499ae 100644--- a/tests/integration/src/querier/04_traces.py+++ b/tests/integration/src/querier/04_traces.py@@ -270,7 +270,6 @@ def test_traces_list()assert_identical_query_response(response, response_with_inline_context)-assert response.status_code == HTTPStatus.OKassert response.json()["status"] == "success"@@ -518,9 +517,7 @@ def test_traces_list(# Case 6a: count by span.count_pytest.param({"name": "span.count_"}, "count_", HTTPStatus.OK),# Case 6b: count by span.count_ with alias span.count_- pytest.param(- {"name": "span.count_"}, "span.count_", HTTPStatus.BAD_REQUEST- ), # THIS SHOULD BE OK BUT FAILS DUE TO LIMITATION IN CURRENT IMPLEMENTATION+ pytest.param({"name": "span.count_"}, "span.count_", HTTPStatus.OK),# Case 7a: count by span.count_ and context specified in the key [BAD REQUEST]pytest.param({"name": "span.count_", "fieldContext": "span"},