Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/ottl]: Add Sort converter #34283

Merged
merged 27 commits into from
Sep 5, 2024

Conversation

kaisecheng
Copy link
Contributor

@kaisecheng kaisecheng commented Jul 28, 2024

Description:

Add Sort function to sort array to ascending order or descending order

Sort(target, Optional[order])

The Sort Converter preserves the data type of the original elements while sorting.
The behavior varies based on the types of elements in the target slice:

Element Types Sorting Behavior Return Value
Integers Sorts as integers Sorted array of integers
Doubles Sorts as doubles Sorted array of doubles
Integers and doubles Converts all to doubles, then sorts Sorted array of integers and doubles
Strings Sorts as strings Sorted array of strings
Booleans Converts all to strings, then sorts Sorted array of booleans
Mix of integers, doubles, booleans, and strings Converts all to strings, then sorts Sorted array of mixed types
Any other types N/A Returns an error

Examples:

  • Sort(attributes["device.tags"])
  • Sort(attributes["device.tags"], "desc")

Link to tracking Issue:

#34200

Testing:

  • Unit tests
  • E2E tests

Documentation:

readme for Sort function

Copy link
Contributor

@evan-bradley evan-bradley left a comment

Choose a reason for hiding this comment

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

Thanks for the thorough PR.

pkg/ottl/ottlfuncs/func_sort.go Outdated Show resolved Hide resolved
pkg/ottl/ottlfuncs/func_sort.go Show resolved Hide resolved
pkg/ottl/ottlfuncs/func_sort.go Outdated Show resolved Hide resolved
pkg/ottl/ottlfuncs/func_sort.go Outdated Show resolved Hide resolved

`target` is a `pcommon.Slice` type field. `order` is a string that must be one of `asc` or `desc`. The default `order` is `asc`.

If elements in `target` are
Copy link
Contributor

Choose a reason for hiding this comment

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

What are your thoughts on permanently converting the types of values in heterogeneous arrays vs. just converting the types for comparison? I think it would be fairly straightforward to only convert the types within the sort function and leave the actual type in the array. I can see arguments for both sides here, but want to make sure we're considering our options.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see changing the types permanently and sorting a list as two distinct intentions. Both are reasonable actions in data processing. Here, we provide better flexibility to users. If they want to change the types permanently, they can simply overwrite the attributes by using set(). Otherwise, they will still have the original array on their hands

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense, thanks. If we see changing the types and sorting as separate actions, then shouldn't we keep the original types of the array elements when sorting? Right now we do:

[true, 1, 2.3, "str"] --> ["1", "2.3", "str", "true"]

When I think it would be possible to do:

[true, 1, 2.3, "str"] --> [1, 2.3, "str", true]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see your point. Yes, preserving the types may be more intuitive. I thought keeping the sort result as a string to make it clear that the comparison is done as a string. It sounds good to maintain the original type.

kaisecheng and others added 7 commits July 29, 2024 23:01
Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
- remove redundant order check
- return error for unsupported types
- change visibility of type constraint
Copy link
Contributor

@evan-bradley evan-bradley left a comment

Choose a reason for hiding this comment

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

Thanks for your continued efforts @kaisecheng. This mostly looks good to me.

pkg/ottl/ottlfuncs/func_sort.go Outdated Show resolved Hide resolved
pkg/ottl/ottlfuncs/README.md Outdated Show resolved Hide resolved
pkg/ottl/ottlfuncs/README.md Outdated Show resolved Hide resolved
pkg/ottl/ottlfuncs/func_sort.go Outdated Show resolved Hide resolved
kaisecheng and others added 6 commits August 2, 2024 15:54
Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
@kaisecheng
Copy link
Contributor Author

@evan-bradley Many thanks for reviews and suggestions. I believe this is ready to merge now.

@evan-bradley
Copy link
Contributor

@TylerHelmuth Please take a look.

@@ -619,6 +619,24 @@ func Test_e2e_converters(t *testing.T) {
tCtx.GetLogRecord().Attributes().PutStr("test", "d74ff0ee8da3b9806b18c877dbf29bbde50b5bd8e4dad7a3a725000feb82e8f1")
},
},
{
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test for a mixed-type slice?

Copy link
Member

Choose a reason for hiding this comment

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

This is a pretty complex function, I'd like to see more e2e tests that cover the different type scenarios it supports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added more e2e test covering unit types and mixed types

pkg/ottl/ottlfuncs/func_sort.go Show resolved Hide resolved
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Aug 23, 2024
@kaisecheng
Copy link
Contributor Author

@evan-bradley @TylerHelmuth Do you have any additional thoughts or feedback that I can address?

@github-actions github-actions bot removed the Stale label Aug 24, 2024
@evan-bradley
Copy link
Contributor

This still looks good to me. @TylerHelmuth please take a look when you are able.

@TylerHelmuth TylerHelmuth merged commit e0e6489 into open-telemetry:main Sep 5, 2024
154 checks passed
@github-actions github-actions bot added this to the next release milestone Sep 5, 2024
f7o pushed a commit to f7o/opentelemetry-collector-contrib that referenced this pull request Sep 12, 2024
**Description:** <Describe what has changed.>

Add `Sort` function to sort array to ascending order or descending order

`Sort(target, Optional[order])`

The Sort Converter preserves the data type of the original elements
while sorting.
The behavior varies based on the types of elements in the target slice:

| Element Types | Sorting Behavior                    | Return Value |
|---------------|-------------------------------------|--------------|
| Integers | Sorts as integers | Sorted array of integers |
| Doubles | Sorts as doubles | Sorted array of doubles |
| Integers and doubles | Converts all to doubles, then sorts | Sorted
array of integers and doubles |
| Strings | Sorts as strings | Sorted array of strings |
| Booleans | Converts all to strings, then sorts | Sorted array of
booleans |
| Mix of integers, doubles, booleans, and strings | Converts all to
strings, then sorts | Sorted array of mixed types |
| Any other types | N/A | Returns an error |


Examples:

- `Sort(attributes["device.tags"])`
- `Sort(attributes["device.tags"], "desc")`

**Link to tracking Issue:** <Issue number if applicable>


open-telemetry#34200

**Testing:** <Describe what testing was performed and which tests were
added.>

- Unit tests
- E2E tests

**Documentation:** <Describe the documentation added.>

readme for Sort function

---------

Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants