[
https://issues.apache.org/jira/browse/TRAFODION-2208?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15492894#comment-15492894
]
ASF GitHub Bot commented on TRAFODION-2208:
-------------------------------------------
Github user traflm commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/703#discussion_r78926114
--- Diff: core/sql/exp/exp_function.cpp ---
@@ -7724,5 +7753,354 @@ Lng32 ExFunctionExtractColumns::unpack (void *
base, void * reallocator)
if (extractColList_.unpack(base)) return -1;
return unpackClause(base, reallocator);
}
+
+//helper function, convert a string into IPV4 , if valid, it can support
leading and padding space
+static Lng32 string2ipv4(char *srcData, Lng32 slen, unsigned int
*inet_addr)
+{
+ Int16 i = 0, j = 0 , p=0, leadingspace=0;
+ char buf[16];
+ Int16 dot=0;
+
+ if(slen < MIN_IPV4_STRING_LEN )
+ return 0;
+
+ unsigned char *ipv4_bytes= (unsigned char *)inet_addr;
+
+ if(srcData[0] == ' ')
+ {
+ leadingspace++;
+ for(i=1; i< slen; i++)
+ {
+ if(srcData[i] == ' ') leadingspace++;
+ else break;
+ }
+ }
+
+
+ for(i=leadingspace , j = 0; i < slen ; i++)
+ {
+ if(srcData[i] == '.')
+ {
+ buf[j]=0;
+ p = str_atoi(buf, j);
+ if( p < 0 || p > 255 || j == 0)
+ {
+ return 0;
+ }
+ else
+ {
+ if(ipv4_bytes)
+ ipv4_bytes[dot] = (unsigned char)p;
+ }
+ j = 0;
+ dot++;
+ if(dot > 3) return 0;
+ }
+ else if(srcData[i] == ' ')
+ {
+ break; //space is terminator
+ }
+ else
+ {
+ if(isdigit(srcData[i]) == 0)
+ {
+ return 0;
+ }
+ else
+ buf[j] = srcData[i];
+ j++;
+ }
+ }
+ Int16 stoppos=i;
+
+ // the last part
+ buf[j]=0; //null terminator
+
+ for(i = 0; i < j; i ++) //check for invalid character
+ {
+ if(isdigit(buf[i]) == 0)
+ {
+ return 0;
+ }
+ }
+ p = str_atoi(buf, j);
+ if( p < 0 || p > 255 || j == 0) // check for invalid number
+ {
+ return 0;
+ }
+ else
+ {
+ if(ipv4_bytes)
+ ipv4_bytes[dot] = (unsigned char)p;
+ }
+
+ //if terminated by space
+ if( stoppos < slen -1)
+ {
+ for(j = stoppos ; j < slen; j++)
+ {
+ if(srcData[j] != ' ') return 0;
+ }
+ }
+
+ if(dot != 3)
+ return 0;
+ else
+ return 1;
+
+}
+
+ex_expr::exp_return_type ExFunctionInetAton::eval(char * op_data[],
+ CollHeap *heap,
+ ComDiagsArea
**diags)
+{
+ char * srcData = op_data[1];
+ char * resultData = op_data[0];
+
+ Attributes *resultAttr = getOperand(0);
+ Attributes *srcAttr = getOperand(1);
+
+ Lng32 slen = srcAttr->getLength(op_data[-MAX_OPERANDS+1]);
+ Lng32 rlen = resultAttr->getLength();
+
+ unsigned int addr;
+ int ret=string2ipv4(srcData, slen, &addr);
+ if(ret)
+ {
+ *(unsigned int *)op_data[0]=addr;
+ return ex_expr::EXPR_OK;
+ }
+ else
+ {
+ ExRaiseSqlError(heap, diags, EXE_INVALID_CHARACTER);
+ *(*diags) << DgString0("IP format") << DgString1("INET_ATON
FUNCTION");
+ return ex_expr::EXPR_ERROR;
+ }
+}
+
+
+ex_expr::exp_return_type ExFunctionInetNtoa::eval(char * op_data[],
+ CollHeap *heap,
+ ComDiagsArea
**diags)
+{
+ char buf[16]; //big enough
+ unsigned long addr = *(unsigned long*)op_data[1];
+ char * resultData = op_data[0];
+ Attributes *resultAttr = getOperand(0);
+ const unsigned char *ipv4_bytes= (const unsigned char *) &addr;
+
+ if( ipv4_bytes[0] > 255 ||
+ ipv4_bytes[1] > 255 ||
+ ipv4_bytes[2] > 255 ||
+ ipv4_bytes[3] > 255 ||
+ addr > 4294967295 )
+ {
+ ExRaiseSqlError(heap, diags, EXE_INVALID_CHARACTER);
--- End diff --
Yes, let me check if there is a proper one, or add a new error message here.
> function support: log2,IS_IPV4,IS_IPV6 etc
> ------------------------------------------
>
> Key: TRAFODION-2208
> URL: https://issues.apache.org/jira/browse/TRAFODION-2208
> Project: Apache Trafodion
> Issue Type: Sub-task
> Reporter: liu ming
> Assignee: liu ming
>
> add built-in function
> IS_IPV4
> IS_IPV6
> LOG2
> Also try to enhance the least to accept multiple input arguments which is
> required in both Oracle/MySQL.
> This will be an example about how to add new built-in functions, so start
> with some very simple functions.
> One of the important goal is to guide the following developing works.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)