Skip to content

feat: improve collision handling#9895

Closed
tushar-signoz wants to merge 6 commits intomainfrom
tvats-improve-collision-handling
Closed

feat: improve collision handling#9895
tushar-signoz wants to merge 6 commits intomainfrom
tvats-improve-collision-handling

Conversation

@tushar-signoz
Copy link
Copy Markdown
Contributor

@tushar-signoz tushar-signoz commented Dec 29, 2025

📄 Summary

This pull request introduces significant improvements to the field mapping and column expression logic in the telemetry logs package, making field resolution more robust and flexible. It adds new helper methods, enhances error handling, and expands test coverage to ensure correctness in various field resolution scenarios, especially when context or data type is missing or ambiguous.

Key changes include:

Field resolution and mapping enhancements

  • Added FieldFor, ColumnFor, and ColumnExpressionFor methods to the fieldMapper for more granular and user-friendly field-to-column resolution, including improved error messages and support for aliasing in SQL expressions. [1] [2]
  • Implemented populateMissingFieldContextAndDataType to infer missing field context and data type from available keys, improving the handling of ambiguous or incomplete field specifications.
  • Enhanced logic in ColumnExpressionFor to construct multiIf expressions for fields with multiple possible contexts or data types, and to suggest corrections for unrecognized field names.

Schema and constant updates

  • Added LogsV2ResourceColumn constant to support resource-level field resolution in log queries.

Query building improvements

  • Updated getKeySelectors in statement_builder.go to include selectors from SelectFields, ensuring all relevant field keys are extracted for query construction. [1] [2]

Testing improvements

  • Added comprehensive unit tests for the new field mapping and column expression logic, covering various scenarios including missing context, ambiguous fields, materialized columns, and error cases. [1] [2] [3] [4]

Metrics field mapper alignment

  • Updated the metrics field mapper to align its interface with the logs field mapper by accepting (but ignoring) the keys parameter in ColumnExpressionFor.

✅ Changes

  • Feature: Brief description
  • Bug fix: Brief description

🏷️ Required: Add Relevant Labels

⚠️ Manually add appropriate labels in the PR sidebar
Please select one or more labels (as applicable):

ex:

  • frontend
  • backend
  • devops
  • bug
  • enhancement
  • ui
  • test

👥 Reviewers

Tag the relevant teams for review:

  • frontend / backend / devops

🧪 How to Test

  1. ...
  2. ...
  3. ...

🔍 Related Issues

Closes #


📸 Screenshots / Screen Recording (if applicable / mandatory for UI related changes)


📋 Checklist

  • Dev Review
  • Test cases added (Unit/ Integration / E2E)
  • Manually tested the changes

👀 Notes for Reviewers


Note

Strengthens field resolution and SQL expression building, reducing ambiguity and improving query robustness.

  • Adds FieldFor, ColumnFor, ColumnExpressionFor to logs field_mapper with better errors and aliasing; builds multiIf across contexts/materialized fields; introduces populateMissingFieldContextAndDataType
  • Defines LogsV2ResourceColumn (resource) for resource-level lookups in logs
  • Includes SelectFields in getKeySelectors in statement_builder.go to ensure all referenced keys are fetched
  • Aligns metrics ColumnExpressionFor signature to accept keys (ignored), keeping API parity
  • Adds helper GetKeyTextFromFieldKey in telemetrytypes/field.go
  • Broad test additions for logs, metrics, and traces covering missing/ambiguous contexts, materialized fields, and errors

Written by Cursor Bugbot for commit f6d8ccf. This will update automatically on new commits. Configure here.

@tushar-signoz tushar-signoz self-assigned this Dec 29, 2025
Copilot AI review requested due to automatic review settings December 29, 2025 10:32
@github-actions github-actions Bot added enhancement New feature or request docs required labels Dec 29, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request enhances field mapping and column expression logic in the telemetry system to better handle field resolution when context or data type information is missing or ambiguous (collision scenarios). It introduces helper methods, improves error handling with suggestions for typos, and expands test coverage.

  • Adds populateMissingFieldContextAndDataType function to intelligently infer missing field context and data type from available keys
  • Enhances ColumnExpressionFor to build multiIf SQL expressions when fields could exist in multiple contexts or data types
  • Includes SelectFields in query key extraction to ensure all relevant fields are processed

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
pkg/types/telemetrytypes/field.go Adds GetKeyTextFromFieldKey utility function to convert field keys back to text format
pkg/telemetrytraces/stmt_builder_test.go Minor formatting fix (spacing adjustment)
pkg/telemetrytraces/field_mapper_test.go Adds test coverage for ColumnExpressionFor with various scenarios including missing context and materialized columns
pkg/telemetrymetrics/field_mapper_test.go Adds test coverage for ColumnExpressionFor including duplicate contexts and missing context fallback
pkg/telemetrymetrics/field_mapper.go Renames unused keys parameter to _ for clarity
pkg/telemetrylogs/statement_builder.go Updates getKeySelectors to include SelectFields and improves documentation
pkg/telemetrylogs/field_mapper_test.go Renames test functions for clarity and adds comprehensive tests for field resolution scenarios
pkg/telemetrylogs/field_mapper.go Implements core collision handling logic with populateMissingFieldContextAndDataType and enhanced ColumnExpressionFor
pkg/telemetrylogs/const.go Adds LogsV2ResourceColumn constant for resource-level field resolution

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/telemetrylogs/field_mapper_test.go Outdated
Comment thread pkg/telemetrylogs/field_mapper.go
Comment thread pkg/telemetrylogs/field_mapper.go
Comment thread pkg/telemetrylogs/field_mapper.go
Comment thread pkg/telemetrylogs/field_mapper.go
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

Comment thread pkg/telemetrylogs/field_mapper.go Outdated
Comment thread pkg/telemetrylogs/field_mapper.go Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 9 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/telemetrylogs/field_mapper.go
Comment thread pkg/telemetrylogs/field_mapper.go
Comment thread pkg/telemetrylogs/field_mapper.go Outdated
Comment thread pkg/telemetrylogs/field_mapper.go
Comment thread pkg/telemetrylogs/field_mapper.go
tushar-signoz and others added 2 commits December 29, 2025 17:38
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
} else if len(keysForField) == 1 {
// we have a single key for the field, use it
field.FieldContext = keysForField[0].FieldContext
field.FieldDataType = keysForField[0].FieldDataType
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User-specified field data type gets overwritten by key value

The populateMissingFieldContextAndDataType function's purpose (per its name and documentation) is to populate MISSING FieldContext and FieldDataType values. However, when there's exactly one key for a field, lines 325-326 unconditionally overwrite both values from the key, even if the user already specified one of them. For example, if a user explicitly specifies FieldDataType: String but the only key in the map has FieldDataType: Number, the user's explicit specification gets silently overwritten. This could cause queries to use incorrect data types when the user explicitly requests a specific type that differs from what exists in the keys map.

Fix in Cursor Fix in Web

}
}
if len(args) == 0 {
return "", errors.Wrapf(err, errors.TypeInvalidInput, errors.CodeInvalidInput, "field `%s` not found", field.Name)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-ErrColumnNotFound errors silently ignored producing invalid SQL

The ColumnExpressionFor function only checks for ErrColumnNotFound errors from FieldFor, but FieldFor can return several other error types (e.g., "only resource context fields are supported for json columns" at line 118). When such errors occur, colName is an empty string, the error check at line 191 evaluates to false, and the function silently returns invalid SQL like ASfieldname`` with an empty column expression. This can be triggered when populateMissingFieldContextAndDataType sets `FieldContext` to `Log` for the "resource" field (a JSON column), which then fails in `FieldFor` because JSON columns only support `Resource` context.

Fix in Cursor Fix in Web

// 3. if data type matches, consider this key
if (field.FieldContext == telemetrytypes.FieldContextUnspecified && field.FieldDataType == telemetrytypes.FieldDataTypeUnspecified) ||
key.FieldContext == field.FieldContext ||
key.FieldDataType == field.FieldDataType {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filtering logic uses OR instead of AND, matching wrong keys

The filtering condition in populateMissingFieldContextAndDataType uses OR logic instead of AND. When a user specifies only FieldContext (e.g., Attribute) but leaves FieldDataType as Unspecified, the condition key.FieldDataType == field.FieldDataType incorrectly matches any key that also has Unspecified data type, regardless of whether its context matches. If a key with wrong context but matching Unspecified data type is the only one that passes filtering, it becomes the "single match" at line 343 and overwrites the user's explicit context specification. The condition uses || but the intended behavior requires && logic to ensure keys match ALL specified criteria rather than just one.

Fix in Cursor Fix in Web

@tushar-signoz tushar-signoz marked this pull request as draft December 30, 2025 03:50
@srikanthccv
Copy link
Copy Markdown
Member

Superseded by #9927

@srikanthccv srikanthccv deleted the tvats-improve-collision-handling branch February 28, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs not required enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants