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 feature: listKeys Scalar Transformation Function #13581

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Navya-Kumar
Copy link

add listKeys() scalar transformation function and unit tests under new mapfunctions class. takes in JSON map input and outputs string array of all keys in map.

@@ -0,0 +1,67 @@
package org.apache.pinot.common.function.scalar;
Copy link
Contributor

Choose a reason for hiding this comment

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

Header is missing

Copy link
Author

Choose a reason for hiding this comment

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

Addressed with Update!

import java.util.Map;

public class MapFunctions {
private MapFunctions() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please apply the Pinot Style and reformat the changes

Copy link
Author

Choose a reason for hiding this comment

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

Attempted with new diff, please ping if it still isn't resolved.

Comment on lines 97 to 98
// CHECKSTYLE:OFF
// @formatter:off
Copy link
Contributor

Choose a reason for hiding this comment

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

(minor) No need to turn off them, same for testNull()

Copy link
Author

Choose a reason for hiding this comment

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

Addressed

public void testRegMapSameTypes()
throws JsonProcessingException {

// CHECKSTYLE:OFF
Copy link
Contributor

Choose a reason for hiding this comment

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

(minor) No need to turn off CHECKSTYLE, same for other places

Copy link
Author

Choose a reason for hiding this comment

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

Addressed

* @param jsonString
* @return converted Java Map from JSON String
*/
public static Map stringToMap(String jsonString) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able to use JsonUtils.jsonNodeToMap()

Copy link
Author

Choose a reason for hiding this comment

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

Redid implementation with a LinkedHashMap, is this function still usable? I further noticed that jsonUtils' jsonNodeToMap takes in a JSON Object whereas this new implementation is string based, do you have any thoughts on to still use jsonNodeToMap() or maybe add in a function there that takes string-based input?

Copy link
Contributor

Choose a reason for hiding this comment

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

Any specific reason why you need a LinkedHashMap?
JsonUtils.stringToObject() can be used in order to deserialize to a custom object


//add map keys to an object list
List<Object> mapList = new ArrayList<>(convertedMap.size());
mapList.addAll(convertedMap.keySet());
Copy link
Contributor

Choose a reason for hiding this comment

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

is this convertedMap.keySet() order guaranteed? if we ingest data with the udf, will the output consistent from the all partitions?

Copy link
Author

Choose a reason for hiding this comment

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

No, addressed with LinkedHashMap change in new commit

Copy link
Contributor

@Jackie-Jiang Jackie-Jiang 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 contribution! Can you update the PR description about the semantic of the new added functions?

import java.util.Map;
import org.apache.pinot.spi.annotations.ScalarFunction;

public class MapFunctions {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please apply Pinot Style and reformat the changes

public class MapFunctions {
private MapFunctions() { }

public static final ObjectMapper oM = new ObjectMapper();
Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able to use JsonUtils for JSON ser-de

@codecov-commenter
Copy link

codecov-commenter commented Aug 16, 2024

Codecov Report

Attention: Patch coverage is 81.96721% with 11 lines in your changes missing coverage. Please review.

Project coverage is 61.96%. Comparing base (59551e4) to head (1c3c605).
Report is 895 commits behind head on master.

Files Patch % Lines
...che/pinot/common/function/scalar/MapFunctions.java 81.96% 9 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #13581      +/-   ##
============================================
+ Coverage     61.75%   61.96%   +0.21%     
+ Complexity      207      198       -9     
============================================
  Files          2436     2554     +118     
  Lines        133233   140532    +7299     
  Branches      20636    21857    +1221     
============================================
+ Hits          82274    87083    +4809     
- Misses        44911    46824    +1913     
- Partials       6048     6625     +577     
Flag Coverage Δ
custom-integration1 <0.01% <0.00%> (-0.01%) ⬇️
integration <0.01% <0.00%> (-0.01%) ⬇️
integration1 <0.01% <0.00%> (-0.01%) ⬇️
integration2 0.00% <0.00%> (ø)
java-11 61.93% <81.96%> (+0.22%) ⬆️
java-21 61.84% <81.96%> (+0.22%) ⬆️
skip-bytebuffers-false 61.95% <81.96%> (+0.20%) ⬆️
skip-bytebuffers-true 61.82% <81.96%> (+34.09%) ⬆️
temurin 61.96% <81.96%> (+0.21%) ⬆️
unittests 61.96% <81.96%> (+0.21%) ⬆️
unittests1 46.46% <81.96%> (-0.43%) ⬇️
unittests2 27.72% <0.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@yashmayya yashmayya left a comment

Choose a reason for hiding this comment

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

Looks like there are some Spotless violations in the build, you can fix them by running mvn spotless:apply locally.

Comment on lines +67 to +69
public static String[] listMapEntries(String jsonString) {
// handle empty string case
if (jsonString == null) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you want this function to be able to take null values as input, you'll need to add nullableParameters = true to the @ScalarFunction annotation. By default, scalar functions are assumed to be null "intolerant", so if any argument to the function is null, the value null will be returned without even invoking the function (see FunctionInvoker). Same goes for the other scalar functions added here.

* @param key
* @return corresponding value for given key from JSON map in data
*/
public static Object getValueFromKey(Object object, Object key) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This shouldn't be a public method in a non-testing class if it's only being used in unit tests.

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.

5 participants