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

Add join function #4075

Merged
merged 3 commits into from
Feb 5, 2024
Merged

Add join function #4075

merged 3 commits into from
Feb 5, 2024

Conversation

oeyh
Copy link
Collaborator

@oeyh oeyh commented Feb 2, 2024

Description

Adds a join function that takes a source key to a list or a map where values are all of list type, and joins list to string with the given delimiter (optional).

It can be used in processors that support value_expression such as add_entries:

  processor:
    - add_entries:
        entries:
          - key: result
            value_expression: join(/source)

With input {"source": [1, 2, 3]}, it will get you {"result": "1,2,3"}
With input

{"source": {"key1": [1, 2, 3], "key2": ["a", "b", "c"]}}

, it will get you

{"result": {"key1": "1,2,3", "key2": "a,b,c"}}

Issues Resolved

Contributes to #3965

Check List

  • New functionality includes testing.
  • New functionality has a documentation issue. Please link to it in this PR.
    • New functionality has javadoc added
  • Commits are signed with a real name per the DCO

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Hai Yan <oeyh@amazon.com>
Copy link
Member

@dlvenable dlvenable left a comment

Choose a reason for hiding this comment

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

This is a good change. Thanks!

I wonder if we should change the join arguments to make it more extensible in the future.

If we put the delimiter first, then we could potentially have a varargs function in the future.

join(',', request_headers, response_headers)

@@ -291,6 +292,31 @@ void testCoerceTerminalNodeWithUnknownFunction() {
assertThrows(RuntimeException.class, () -> objectUnderTest.coercePrimaryTerminalNode(terminalNode, null));
}

@Test
void testCoerceTerminalNodeFunctionTypeWithCommaInArgumentsThrowsException() {
Copy link
Member

Choose a reason for hiding this comment

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

...WithComma...

Perhaps rename this to:

...WithUnescapedComma...

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.apache.commons.lang3.RandomStringUtils;

class GenericExpressionEvaluator_StringIT {
class GenericExpressionEvaluator_MultiTypeIT {
Copy link
Member

Choose a reason for hiding this comment

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

This test was explicitly named _StringIT. This may be an indication that we should have a different IT for _MultiTypeIT. Would it duplicate a lot of code?

Thoughts?

Copy link
Collaborator Author

@oeyh oeyh Feb 2, 2024

Choose a reason for hiding this comment

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

Would it duplicate a lot of code?

That's the reason why I renamed the test class and having new tests for Map here as well. They are using the same GenericExpressionEvaluator, the only difference is the type casting after evaluation. My thought is that we can use this MultiTypeIT class to hold all future types returned by functions, Map, List, etc.

Signed-off-by: Hai Yan <oeyh@amazon.com>
Signed-off-by: Hai Yan <oeyh@amazon.com>
sourceKey = argStrings.get(1);
} else {
delimiter = ",";
sourceKey = argStrings.get(0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this mean, we allow join(/aaa, /bbb, /ccc, /ddd) and it takes just "/aaa" and ignores rest? If yes, that seems confusing that we silently ignore other parameters.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently it only supports one or two parameters. There's a check on number of arguments at the beginning, so join(/aaa, /bbb, /ccc, /ddd) won't pass that.

Copy link
Member

Choose a reason for hiding this comment

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

So we don't currently support join(/aaa, /bbb, /ccc, /ddd), but the method could be extended to support join(/aaa, /bbb, /ccc, /ddd). We would just need to update the code for it.

@oeyh oeyh merged commit 5df1621 into opensearch-project:main Feb 5, 2024
46 of 47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants