[ 
https://issues.apache.org/jira/browse/PIG-2443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13174409#comment-13174409
 ] 

Prashant Kommireddi commented on PIG-2443:
------------------------------------------

I agree with you on that, it would be nice to have the UDF return the integer 
value in case input is an Integer, and a default otherwise. May be we can visit 
that at a later time.

As per your feedback, here is what I am going to do. Let me know your thoughts 
on this

1. IsNumeric works for floating points
2. Override IsInt for Long, Double, Float

{code}
public class IsNumeric extends EvalFunc<Boolean> {

    @Override
    public Boolean exec(Tuple input) throws IOException {
        if (input == null || input.size() == 0) return false;
        try {
            String str = (String)input.get(0);
            if (str == null || str.length() == 0) return false;

            if (str.startsWith("-")) str = str.substring(1);

            return str.matches("\\d+(\\.\\d+)?");

        } catch (ClassCastException e) {
            warn(e.getMessage(), PigWarning.UDF_WARNING_1);
            return false;
        }
    }
}
{code}
                
> [Piggybank] Add UDFs to check if a String is an Integer And if a String is 
> Numeric
> ----------------------------------------------------------------------------------
>
>                 Key: PIG-2443
>                 URL: https://issues.apache.org/jira/browse/PIG-2443
>             Project: Pig
>          Issue Type: New Feature
>          Components: piggybank
>            Reporter: Prashant Kommireddi
>            Assignee: Prashant Kommireddi
>         Attachments: isIntNumeric.patch, isIntNumeric.patch
>
>
> UDF that could be used to check if a String is numeric (or an Integer). 
> Several tools such as Splunk, AbInitio have this UDF built-in and companies 
> making an effort to move to Hadoop/Pig could use this.
> Use Case:
> In raw logs there are certain filters/conditions applied based on whether a 
> particular field/value is numeric or not. For eg, SPLIT A INTO CATEGORY1 IF 
> IsInt($0), CATEGORY2 IF !IsInt($0);

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to