-
Notifications
You must be signed in to change notification settings - Fork 263
SubList function #5535
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
base: main
Are you sure you want to change the base?
SubList function #5535
Conversation
...xpression/src/main/java/org/opensearch/dataprepper/expression/SubListExpressionFunction.java
Show resolved
Hide resolved
...xpression/src/main/java/org/opensearch/dataprepper/expression/SubListExpressionFunction.java
Outdated
Show resolved
Hide resolved
...xpression/src/main/java/org/opensearch/dataprepper/expression/SubListExpressionFunction.java
Outdated
Show resolved
Hide resolved
List sourceList = (List)value; | ||
int startIndex = (Integer)args.get(1), endIndex = (Integer)args.get(2); | ||
|
||
if (startIndex < 0 || startIndex >= sourceList.size()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be
if (startIndex < 0 || startIndex > sourceList.size())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are using 0 based index and startIndex is inclusive so startIndex equals to sourceList.size() causes index out of bounds for subList function, that's why I used >=
For endIndex I used > since it's exclusive
...xpression/src/main/java/org/opensearch/dataprepper/expression/SubListExpressionFunction.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making this contribution. I left a couple comments.
Also the build is failing due to lack of test coverage, You also need to sign your commit. You can do git commit --amend and modify the message to include the signature. For example, this is my signature
|
cdded93
to
9aca2dc
Compare
Added test cases for 100% coverage. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add one test case where the key is nested in the event object for example, if the event object look like
{
"level1": {
"level2": [ 1, 2, 3, 4, 5]
}
}
and
assertThat(subListExpressionFunction.evaluate(List.of("/level1/level2", 1, 3), testEvent, testFunction), equalTo(List.of(2, 3)));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added please check #testWithValidArgumentsCase3
throw new RuntimeException("subList() end index should be between 0 and list length or -1 for list length (exclusive)"); | ||
} | ||
if (startIndex > endIndex) { | ||
throw new RuntimeException("subList() start index should be less or equal to end index"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Less than or equal to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed
if (!(args.get(0) instanceof String)) { | ||
throw new IllegalArgumentException("subList() takes 1st argument as string type"); | ||
} | ||
if (!(args.get(1) instanceof Integer) || !(args.get(2) instanceof Integer)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you run data prepper end to end to test this function? I pulled your PR and it looks like Integers can't be passed as arguments to the functions due to the grammar. I think we will need these to be strings for now and we can throw illegal argument exception if we can't parse the string to int.
- add_entries:
entries:
- key: "result"
value_expression: 'subList(/my_list, "0", "1")'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accepting integers as string now, please check the message is fine or not "subList() takes 2nd and 3rd arguments as integers"
Thanks! You still need to amend the commit message with the DCO though |
9aca2dc
to
0f5d36a
Compare
Signed now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks this looks good! Will you creating a PR to the documentation website for this (https://github.com/opensearch-project/documentation-website/blob/main/_data-prepper/pipelines/functions.md)?
Thanks! |
I tested again with the changes and am still getting exceptions when passing the 2nd and 3rd argument. Looking into what needs to change for that to work |
@charan2628 I have gotten it working with this format
else {
try {
argList.add(Integer.parseInt(trimmedArg));
continue;
} catch (final Exception e) {
}
throw new RuntimeException("Unsupported type passed as function argument");
}
@dlvenable Please weigh in on these changes, especially the best way to modify for number 2 if there is an alternative |
Does not work yet after testing, recommendations to fix added in comment
@graytaylor0 sublist should support both integers and strings for 2nd and 3rd arguments?
|
0f5d36a
to
868ef1e
Compare
int startIndex, endIndex; | ||
try { | ||
startIndex = args.get(1) instanceof Integer ? (Integer) args.get(1) : Integer.parseInt((String)args.get(1)); | ||
endIndex = args.get(2) instanceof Integer ? (Integer) args.get(2) : Integer.parseInt((String)args.get(2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@graytaylor0 Done the changes to include both integer and string.
We could just require Integer here with the change I had shared previously. But if you were able to test this end to end with both string and integers then that works too |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing all the changes. Were you able to run some tests end to end before we merge?
8ddce87
to
0fde339
Compare
1
@graytaylor0 Done changes to remove double quotes if we receive string as arguments I name the new function as subList, in the original issue it was sublist, I will change it if needs to be changed |
@graytaylor0 Tried with example mentioned in the issue, replacing the original list with sublist
input.json
Submitted PR for documentation, please review opensearch-project/documentation-website#9718 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all of this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this contribution!
@charan2628 , Thank you for this great contribution! The unit tests are failing. You can more easily run all the relevant tests locally using this command:
This will allow you to quickly test these changes without building everything. Below are the failing tests. Some appear to be based on restrictions for other functions which are not allowed to take in numbers.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need tests to pass.
Working on it |
@dlvenable functions are now accepting Integer arguments, can I remove test cases checking for invalid arguments which are of type integer? |
* SubList function opensearch-project#5529 Signed-off-by: Sai charan raj Gudala <[email protected]>
0fde339
to
320840a
Compare
@dlvenable Since functions now accepting Integer arguments I removed those scenarios as part of invalid argument case in GenericExpressionEvaluator_ConditionalIT and added new valid case where subList function accepting integer argument. All tests are now passing. |
Description
Support extracting subLists from a list
Issues Resolved
Resolves #5529
Check List
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.