Repository: incubator-trafodion Updated Branches: refs/heads/master 4418ed3dc -> 66827b053
[TRAFODION-2208] function support: log2,IS_IPV4,IS_IPV6 etc Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/ba17bd3b Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/ba17bd3b Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/ba17bd3b Branch: refs/heads/master Commit: ba17bd3b23aeea3271d254aee29b3d69feb1231f Parents: ea6a933 Author: Liu Ming <ovis_p...@sina.com> Authored: Wed Sep 14 10:27:27 2016 +0000 Committer: Liu Ming <ovis_p...@sina.com> Committed: Wed Sep 14 10:27:27 2016 +0000 ---------------------------------------------------------------------- core/sql/common/OperTypeEnum.h | 7 + core/sql/exp/ExpPackDefs.cpp | 14 ++ core/sql/exp/exp_clause.cpp | 21 ++ core/sql/exp/exp_clause.h | 19 +- core/sql/exp/exp_function.cpp | 378 +++++++++++++++++++++++++++++++ core/sql/exp/exp_function.h | 93 ++++++++ core/sql/exp/exp_math_func.cpp | 14 +- core/sql/generator/GenItemFunc.cpp | 27 +++ core/sql/optimizer/BindItemExpr.cpp | 5 + core/sql/optimizer/ItemExpr.cpp | 1 + core/sql/optimizer/SynthType.cpp | 75 ++++++ core/sql/parser/ParKeyWords.cpp | 5 + core/sql/parser/sqlparser.y | 40 +++- 13 files changed, 689 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/common/OperTypeEnum.h ---------------------------------------------------------------------- diff --git a/core/sql/common/OperTypeEnum.h b/core/sql/common/OperTypeEnum.h index 2315ba0..032bbb1 100644 --- a/core/sql/common/OperTypeEnum.h +++ b/core/sql/common/OperTypeEnum.h @@ -499,6 +499,7 @@ enum OperatorTypeEnum { ITM_BITNOT = 2234, ITM_BITEXTRACT = 2235, ITM_CONVERTTOBITS = 2236, + ITM_LOG2= 2237, // string functions ITM_TRUNC = 2250, @@ -774,6 +775,12 @@ enum OperatorTypeEnum { ITM_SET_TRANS_AUTOABORT_INTERVAL = 2605, ITM_SET_TRANS_MULTI_COMMIT = 2606, + // Item expressions for MySQL/Oracle/DB2/SQL-Server compatibility + ITM_ISIPV4 = 2630, + ITM_ISIPV6 = 2631, + ITM_INET_ATON = 2632, + ITM_INET_NTOA = 2633, + // Items for needed for Translating to UCS2 output strings ITM_DATEFMT = 2990, ITM_CURRNT_USER = 2991, http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/exp/ExpPackDefs.cpp ---------------------------------------------------------------------- diff --git a/core/sql/exp/ExpPackDefs.cpp b/core/sql/exp/ExpPackDefs.cpp index 42929f7..e319328 100644 --- a/core/sql/exp/ExpPackDefs.cpp +++ b/core/sql/exp/ExpPackDefs.cpp @@ -583,7 +583,21 @@ Long ExHeaderClause::pack(void * space) { return packClause(space, sizeof(ExHeaderClause)); } +NA_EIDPROC Long ExFunctionIsIP::pack(void * space) +{ + return packClause(space, sizeof(ExFunctionIsIP)); +} + +NA_EIDPROC Long ExFunctionInetAton::pack(void * space) +{ + return packClause(space, sizeof(ExFunctionInetAton)); +} + +NA_EIDPROC Long ExFunctionInetNtoa::pack(void * space) +{ + return packClause(space, sizeof(ExFunctionInetNtoa)); +} // ----------------------------------------------------------------------- // U N P A C K http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/exp/exp_clause.cpp ---------------------------------------------------------------------- diff --git a/core/sql/exp/exp_clause.cpp b/core/sql/exp/exp_clause.cpp index 5fd6490..3ac18c1 100644 --- a/core/sql/exp/exp_clause.cpp +++ b/core/sql/exp/exp_clause.cpp @@ -338,6 +338,16 @@ ex_clause::ex_clause(clause_type type, case ITM_CURR_TRANSID: setClassID(FUNC_CURR_TRANSID_ID); break; + case ITM_ISIPV4: + case ITM_ISIPV6: + setClassID(FUNC_ISIP_ID); + break; + case ITM_INET_ATON: + setClassID(FUNC_INETATON_ID); + break; + case ITM_INET_NTOA: + setClassID(FUNC_INETNTOA_ID); + break; case ITM_USER: case ITM_USERID: case ITM_AUTHTYPE: @@ -415,6 +425,7 @@ ex_clause::ex_clause(clause_type type, case ITM_FLOOR: case ITM_LOG: case ITM_LOG10: + case ITM_LOG2: case ITM_SIN: case ITM_SINH: case ITM_SQRT: @@ -973,6 +984,15 @@ NA_EIDPROC char *ex_clause::findVTblPtr(short classID) case ex_clause::FUNC_HBASE_VERSION: GetVTblPtr(vtblPtr, ExFunctionHbaseVersion); break; + case ex_clause::FUNC_ISIP_ID: + GetVTblPtr(vtblPtr, ExFunctionIsIP); + break; + case ex_clause::FUNC_INETATON_ID: + GetVTblPtr(vtblPtr, ExFunctionInetAton); + break; + case ex_clause::FUNC_INETNTOA_ID: + GetVTblPtr(vtblPtr, ExFunctionInetNtoa); + break; default: GetVTblPtr(vtblPtr, ex_clause); break; @@ -1218,6 +1238,7 @@ NA_EIDPROC const char * getOperTypeEnumAsString(Int16 /*OperatorTypeEnum*/ ote) case ITM_FLOOR: return "ITM_FLOOR"; case ITM_LOG: return "ITM_LOG"; case ITM_LOG10: return "ITM_LOG10"; + case ITM_LOG2: return "ITM_LOG2"; case ITM_MOD: return "ITM_MOD"; case ITM_POWER: return "ITM_POWER"; case ITM_ROUND: return "ITM_ROUND"; http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/exp/exp_clause.h ---------------------------------------------------------------------- diff --git a/core/sql/exp/exp_clause.h b/core/sql/exp/exp_clause.h index 83c7124..4fc36d2 100644 --- a/core/sql/exp/exp_clause.h +++ b/core/sql/exp/exp_clause.h @@ -190,16 +190,19 @@ public: LOB_FUNC_SUBSTR =105, FUNC_HIVEHASH_ID =106, FUNC_HIVEHASHCOMB_ID =107, - FUNC_UNIQUE_ID = 108, - FUNC_HBASE_COLUMN_LOOKUP = 109, - FUNC_HBASE_COLUMNS_DISPLAY = 110, + FUNC_UNIQUE_ID = 108, + FUNC_HBASE_COLUMN_LOOKUP = 109, + FUNC_HBASE_COLUMNS_DISPLAY = 110, FUNC_HBASE_COLUMN_CREATE = 111, - FUNC_CAST_TYPE = 112, - FUNC_SEQUENCE_VALUE = 113, - FUNC_PIVOT_GROUP = 114, - FUNC_ROWNUM = 115, + FUNC_CAST_TYPE = 112, + FUNC_SEQUENCE_VALUE = 113, + FUNC_PIVOT_GROUP = 114, + FUNC_ROWNUM = 115, FUNC_HBASE_TIMESTAMP = 116, - FUNC_HBASE_VERSION = 117 + FUNC_HBASE_VERSION = 117, + FUNC_ISIP_ID = 118, + FUNC_INETATON_ID = 119, + FUNC_INETNTOA_ID = 120 }; // max number of operands (including result) in a clause. http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/exp/exp_function.cpp ---------------------------------------------------------------------- diff --git a/core/sql/exp/exp_function.cpp b/core/sql/exp/exp_function.cpp index 93c560f..21d3351 100644 --- a/core/sql/exp/exp_function.cpp +++ b/core/sql/exp/exp_function.cpp @@ -45,6 +45,11 @@ #include <ctype.h> #include <string.h> #include <stdio.h> +/* +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +*/ #include "NLSConversion.h" #include "nawstring.h" @@ -202,6 +207,9 @@ ExFunctionPack::ExFunctionPack(){}; ExUnPackCol::ExUnPackCol(){}; ExFunctionRangeLookup::ExFunctionRangeLookup(){}; ExAuditImage::ExAuditImage(){}; +ExFunctionIsIP::ExFunctionIsIP(){}; +ExFunctionInetAton::ExFunctionInetAton(){}; +ExFunctionInetNtoa::ExFunctionInetNtoa(){}; ExFunctionAscii::ExFunctionAscii(OperatorTypeEnum oper_type, Attributes ** attr, Space * space) : ex_function_clause(oper_type, 2, attr, space) @@ -216,6 +224,27 @@ ExFunctionChar::ExFunctionChar(OperatorTypeEnum oper_type, }; +ExFunctionIsIP::ExFunctionIsIP(OperatorTypeEnum oper_type, + Attributes ** attr, Space * space) + : ex_function_clause(oper_type, 2, attr, space) +{ + +}; + +ExFunctionInetAton::ExFunctionInetAton(OperatorTypeEnum oper_type, + Attributes ** attr, Space * space) + : ex_function_clause(oper_type, 2, attr, space) +{ + +}; + +ExFunctionInetNtoa::ExFunctionInetNtoa(OperatorTypeEnum oper_type, + Attributes ** attr, Space * space) + : ex_function_clause(oper_type, 2, attr, space) +{ + +}; + ExFunctionConvertHex::ExFunctionConvertHex(OperatorTypeEnum oper_type, Attributes ** attr, Space * space) : ex_function_clause(oper_type, 2, attr, space) @@ -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); + *(*diags) << DgString0("IP value") << DgString1("INET_NTOA FUNCTION"); + return ex_expr::EXPR_ERROR; + } + + str_sprintf(buf, "%d.%d.%d.%d", + ipv4_bytes[0], ipv4_bytes[1], ipv4_bytes[2], ipv4_bytes[3]); + int slen = str_len(buf); + str_cpy_all(resultData, buf, slen); + getOperand(0)->setVarLength(slen, op_data[-MAX_OPERANDS]); + + return ex_expr::EXPR_OK; +} + +ex_expr::exp_return_type ExFunctionIsIP::eval(char * op_data[], + CollHeap *heap, + ComDiagsArea **diags) +{ + + char * resultData = op_data[0]; + char * srcData = op_data[1]; + Int16 i = 0, j = 0 , p=0; + Attributes *resultAttr = getOperand(0); + Attributes *srcAttr = getOperand(1); + + Lng32 slen = srcAttr->getLength(op_data[-MAX_OPERANDS+1]); + Lng32 rlen = resultAttr->getLength(); + + if(getOperType() == ITM_ISIPV4) + { + if(string2ipv4(srcData, slen, NULL) == 0) + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + else + { + *(Int16 *)op_data[0] = 1; + return ex_expr::EXPR_OK; + } + + } + else + { + Int16 gapcounter = 0 , portidx = 0;; + char portion[IPV6_PARTS_NUM][MAX_IPV6_STRING_LEN + 1]; + char trimdata[MAX_IPV6_STRING_LEN + 1]; + str_pad(trimdata,MAX_IPV6_STRING_LEN + 1, 0); + + if(slen < MIN_IPV6_STRING_LEN ) + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + + char *ptr= srcData; + + //cannot start with single : + if (*ptr == ':') + { + ++ptr; + + if (*ptr != ':') + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + } + + if (*ptr == ' ') + { + while(*ptr==' ') ptr++; + } + + char * start=ptr; + if(slen - (srcData - ptr) > MAX_IPV6_STRING_LEN ) // must be padding space + { + if( start[MAX_IPV6_STRING_LEN] != ' ') + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + else { + for(j = MAX_IPV6_STRING_LEN; j >=0; j--) + { + if(ptr[j] != ' ') //stop, j is the last non-space char + break; + } + str_cpy_all(trimdata,start, j); + start = trimdata; + } + } + + char ipv4[MAX_IPV6_STRING_LEN + 1]; + j = 0; + int ipv4idx = 0; + // try to split the string into portions delieted by ':' + // also check '::', call it gap, there is only up to 1 gap + // if there is a gap, portion number can be smaller than 8 + // without gap, portion number should be 8 + // each portion must be 16 bit integer in HEX format + // leading 0 can be omit + for(i = 0; i< slen; i++) + { + if(start[i] == ':') + { + portion[portidx][j] = 0; //set the terminator + + if(start[i+1] == ':') + { + if(j != 0) //some characters are already saved into current portion + portidx++; + gapcounter++; + j = 0; //reset temp buffer pointer + i++; + continue; + } + else + { + //new portion start + if( j == 0 ) + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + portidx++; + j=0; + continue; + } + } + else if( start[i] == '.') //ipv4 mixed format + { + if( ipv4idx > 0 ) + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + + str_cpy_all(ipv4, portion[portidx],str_len(portion[portidx])); + if(strlen(start+i) < MAX_IPV4_STRING_LEN) //15 is the maximum IPV4 string length + str_cat((const char*)ipv4, start+i, ipv4); + else + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + + if(string2ipv4(ipv4, strlen(ipv4), NULL) == 0) + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + else + { + ipv4idx = 2; //ipv4 use 2 portions, 32 bits + break; // ipv4 string must be the last portion + } + } + + portion[portidx][j] = start[i]; + j++; + + } + + if(gapcounter > 1 || portidx > IPV6_PARTS_NUM - 1) + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + else if(gapcounter ==0 && portidx+ipv4idx < IPV6_PARTS_NUM - 1) + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + + //check each IPV6 portion + for(i =0; i < portidx ; i++) + { + int len = strlen(portion[i]); + if( len > 4) //IPV4 portion can be longer than 4 chars + { + if(ipv4idx == 0 || ((ipv4idx == 2) && ( i != portidx -1) ) ) // no IPV4 portion, or this is not the IPV4 portion + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + } + for(j = 0; j < len; j++) + { + if( (portion[i][j] >= 'A' && portion[i][j] <= 'F') || + (portion[i][j] >= 'a' && portion[i][j] <= 'f') || + (portion[i][j] >= '0' && portion[i][j] <= '9') + ) //good + continue; + else + { + *(Int16 *)op_data[0] = 0; + return ex_expr::EXPR_OK; + } + } + } + //everything is good, this is IPV6 + *(Int16 *)op_data[0] = 1; + return ex_expr::EXPR_OK; + } +} + // LCOV_EXCL_STOP #pragma warn(1506) // warning elimination http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/exp/exp_function.h ---------------------------------------------------------------------- diff --git a/core/sql/exp/exp_function.h b/core/sql/exp/exp_function.h index 589975e..918182b 100644 --- a/core/sql/exp/exp_function.h +++ b/core/sql/exp/exp_function.h @@ -69,6 +69,11 @@ class ex_function_clause; ) #define EXP_SCRAMBLE 0x35 +#define MAX_IPV4_STRING_LEN 15 +#define MAX_IPV6_STRING_LEN 8 * 4 + 7 +#define MIN_IPV4_STRING_LEN 7 +#define MIN_IPV6_STRING_LEN 2 +#define IPV6_PARTS_NUM 8 SQLEXP_LIB_FUNC NA_EIDPROC UInt32 FastHash(char *data, Int32 length); @@ -851,6 +856,94 @@ public: // --------------------------------------------------------------------- }; +class SQLEXP_LIB_FUNC ExFunctionIsIP : public ex_function_clause { +public: + NA_EIDPROC ExFunctionIsIP(OperatorTypeEnum oper_type, + Attributes ** attr, + Space * space); + NA_EIDPROC ExFunctionIsIP(); + + NA_EIDPROC ex_expr::exp_return_type eval(char *op_data[], CollHeap*, + ComDiagsArea** = 0); + NA_EIDPROC Long pack(void *); + + // --------------------------------------------------------------------- + // Redefinition of methods inherited from NAVersionedObject. + // --------------------------------------------------------------------- + NA_EIDPROC virtual unsigned char getClassVersionID() + { + return 1; + } + + NA_EIDPROC virtual void populateImageVersionIDArray() + { + setImageVersionID(2,getClassVersionID()); + ex_function_clause::populateImageVersionIDArray(); + } + + NA_EIDPROC virtual short getClassSize() { return (short)sizeof(*this); } + // --------------------------------------------------------------------- +}; + +class SQLEXP_LIB_FUNC ExFunctionInetAton: public ex_function_clause { +public: + NA_EIDPROC ExFunctionInetAton(OperatorTypeEnum oper_type, + Attributes ** attr, + Space * space); + NA_EIDPROC ExFunctionInetAton(); + + NA_EIDPROC ex_expr::exp_return_type eval(char *op_data[], CollHeap*, + ComDiagsArea** = 0); + NA_EIDPROC Long pack(void *); + + // --------------------------------------------------------------------- + // Redefinition of methods inherited from NAVersionedObject. + // --------------------------------------------------------------------- + NA_EIDPROC virtual unsigned char getClassVersionID() + { + return 1; + } + + NA_EIDPROC virtual void populateImageVersionIDArray() + { + setImageVersionID(2,getClassVersionID()); + ex_function_clause::populateImageVersionIDArray(); + } + + NA_EIDPROC virtual short getClassSize() { return (short)sizeof(*this); } + // --------------------------------------------------------------------- +}; + + +class SQLEXP_LIB_FUNC ExFunctionInetNtoa: public ex_function_clause { +public: + NA_EIDPROC ExFunctionInetNtoa(OperatorTypeEnum oper_type, + Attributes ** attr, + Space * space); + NA_EIDPROC ExFunctionInetNtoa(); + + NA_EIDPROC ex_expr::exp_return_type eval(char *op_data[], CollHeap*, + ComDiagsArea** = 0); + NA_EIDPROC Long pack(void *); + + // --------------------------------------------------------------------- + // Redefinition of methods inherited from NAVersionedObject. + // --------------------------------------------------------------------- + NA_EIDPROC virtual unsigned char getClassVersionID() + { + return 1; + } + + NA_EIDPROC virtual void populateImageVersionIDArray() + { + setImageVersionID(2,getClassVersionID()); + ex_function_clause::populateImageVersionIDArray(); + } + + NA_EIDPROC virtual short getClassSize() { return (short)sizeof(*this); } + // --------------------------------------------------------------------- +}; + class SQLEXP_LIB_FUNC ExFunctionTokenStr : public ex_function_clause { public: NA_EIDPROC ExFunctionTokenStr(OperatorTypeEnum oper_type, http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/exp/exp_math_func.cpp ---------------------------------------------------------------------- diff --git a/core/sql/exp/exp_math_func.cpp b/core/sql/exp/exp_math_func.cpp index e249df0..0ed9251 100644 --- a/core/sql/exp/exp_math_func.cpp +++ b/core/sql/exp/exp_math_func.cpp @@ -50,6 +50,7 @@ #define MathFloor(op, err) floor(op) #define MathLog(op, err) log(op) #define MathLog10(op, err) log10(op) +#define MathLog2(op, err) log2(op) #define MathModf(op, err) modf(op) #define MathPow(op1, op2, err) pow(op1, op2) //extern double MathPow(double Base, double Power, short &err); @@ -348,7 +349,18 @@ ex_expr::exp_return_type ExFunctionMath::eval(char *op_data[], return ex_expr::EXPR_ERROR; } - *(double *)op_data[0] = MathLog10(*(double *)op_data[1], err); + *(double *)op_data[0] = MathLog2(*(double *)op_data[1], err); + break; + + case ITM_LOG2: + if (*(double *)op_data[1] <= 0) + { + ExRaiseSqlError(heap, diagsArea, EXE_BAD_ARG_TO_MATH_FUNC); + **diagsArea << DgString0("LOG2"); + return ex_expr::EXPR_ERROR; + } + + *(double *)op_data[0] = MathLog2(*(double *)op_data[1], err); break; case ITM_SIN: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/generator/GenItemFunc.cpp ---------------------------------------------------------------------- diff --git a/core/sql/generator/GenItemFunc.cpp b/core/sql/generator/GenItemFunc.cpp index 3b115a2..1fb49d5 100644 --- a/core/sql/generator/GenItemFunc.cpp +++ b/core/sql/generator/GenItemFunc.cpp @@ -218,6 +218,33 @@ short BuiltinFunction::codeGen(Generator * generator) break; + case ITM_ISIPV4: + case ITM_ISIPV6: + { + function_clause = + new(generator->getSpace()) ExFunctionIsIP(getOperatorType(), + attr, space); + } + + break; + case ITM_INET_ATON: + { + function_clause = + new(generator->getSpace()) ExFunctionInetAton(getOperatorType(), + attr, space); + } + + break; + + case ITM_INET_NTOA: + { + function_clause = + new(generator->getSpace()) ExFunctionInetNtoa(getOperatorType(), + attr, space); + } + + break; + // -- MV case ITM_CONCAT: { http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/optimizer/BindItemExpr.cpp ---------------------------------------------------------------------- diff --git a/core/sql/optimizer/BindItemExpr.cpp b/core/sql/optimizer/BindItemExpr.cpp index 9b49da2..c7a7a30 100644 --- a/core/sql/optimizer/BindItemExpr.cpp +++ b/core/sql/optimizer/BindItemExpr.cpp @@ -3161,6 +3161,11 @@ ItemExpr *BuiltinFunction::bindNode(BindWA *bindWA) ItemExpr * ie = NULL; switch (getOperatorType()) { + case ITM_ISIPV4: + case ITM_ISIPV6: + { + break; + } case ITM_NULLIFZERO: { // binder has already verified that child is numeric http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/optimizer/ItemExpr.cpp ---------------------------------------------------------------------- diff --git a/core/sql/optimizer/ItemExpr.cpp b/core/sql/optimizer/ItemExpr.cpp index 9966a95..b72ac31 100644 --- a/core/sql/optimizer/ItemExpr.cpp +++ b/core/sql/optimizer/ItemExpr.cpp @@ -12926,6 +12926,7 @@ const NAString MathFunc::getText() const case ITM_FLOOR: return "floor"; case ITM_LOG: return "log"; case ITM_LOG10: return "log10"; + case ITM_LOG2: return "log2"; case ITM_PI: return "pi"; case ITM_POWER: return "power"; case ITM_RADIANS: return "radians"; http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/optimizer/SynthType.cpp ---------------------------------------------------------------------- diff --git a/core/sql/optimizer/SynthType.cpp b/core/sql/optimizer/SynthType.cpp index 2d48c50..e4ee01e 100644 --- a/core/sql/optimizer/SynthType.cpp +++ b/core/sql/optimizer/SynthType.cpp @@ -1010,7 +1010,81 @@ const NAType *BuiltinFunction::synthesizeType() SQLChar(maxLength, typ1.supportsSQLnull()); } break; + + case ITM_ISIPV4: + case ITM_ISIPV6: + { + // type cast any params + ValueId vid1 = child(0)->getValueId(); + SQLChar c1(ComSqlId::MAX_QUERY_ID_LEN); + vid1.coerceType(c1, NA_CHARACTER_TYPE); + //input type must be string + const NAType &typ1 = child(0)->getValueId().getType(); + + if (typ1.getTypeQualifier() != NA_CHARACTER_TYPE) + { + *CmpCommon::diags() << DgSqlCode(-4045) << DgString0("IS_IP"); + return NULL; + } + retType = new HEAP + SQLSmall(TRUE, FALSE); + if (typ1.supportsSQLnull()) + { + retType->setNullable(TRUE); + } + } + break; + case ITM_INET_ATON: + { + // type cast any params + ValueId vid1 = child(0)->getValueId(); + SQLChar c1(ComSqlId::MAX_QUERY_ID_LEN); + vid1.coerceType(c1, NA_CHARACTER_TYPE); + + //input type must be string + const NAType &typ1 = child(0)->getValueId().getType(); + + if (typ1.getTypeQualifier() != NA_CHARACTER_TYPE) + { + *CmpCommon::diags() << DgSqlCode(-4045) << DgString0("INET_ATON"); + return NULL; + } + retType = new HEAP + SQLInt(FALSE, FALSE); + if (typ1.supportsSQLnull()) + { + retType->setNullable(TRUE); + } + } + break; + case ITM_INET_NTOA: + { + // type cast any params + ValueId vid = child(0)->getValueId(); + vid.coerceType(NA_NUMERIC_TYPE); + + const NAType &typ1 = child(0)->getValueId().getType(); + if (typ1.getTypeQualifier() != NA_NUMERIC_TYPE) + { + *CmpCommon::diags() << DgSqlCode(-4045) << DgString0("INET_NTOA"); + return NULL; + } + const NumericType &ntyp1 = (NumericType &) typ1; + if (NOT ntyp1.isExact() || ntyp1.getScale() != 0) + { + *CmpCommon::diags() << DgSqlCode(-4046) << DgString0("INET_NTOA"); + return NULL; + } + retType = new HEAP + SQLVarChar(15, FALSE); + SQLInt(FALSE, FALSE); + if (typ1.supportsSQLnull()) + { + retType->setNullable(TRUE); + } + } + break; case ITM_NULLIFZERO: { // type cast any params @@ -3399,6 +3473,7 @@ const NAType *MathFunc::synthesizeType() case ITM_FLOOR: case ITM_LOG: case ITM_LOG10: + case ITM_LOG2: case ITM_PI: case ITM_POWER: case ITM_RADIANS: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/parser/ParKeyWords.cpp ---------------------------------------------------------------------- diff --git a/core/sql/parser/ParKeyWords.cpp b/core/sql/parser/ParKeyWords.cpp index 29f8dba..52478be 100644 --- a/core/sql/parser/ParKeyWords.cpp +++ b/core/sql/parser/ParKeyWords.cpp @@ -512,6 +512,8 @@ ParKeyWord ParKeyWords::keyWords_[] = { ParKeyWord("INDICATOR_DATA", TOK_INDICATOR_DATA, NONRESTOKEN_), ParKeyWord("INDICATOR_POINTER", TOK_INDICATOR_POINTER, NONRESTOKEN_), ParKeyWord("INDICATOR_TYPE", TOK_INDICATOR_TYPE, NONRESTOKEN_), + ParKeyWord("INET_ATON", TOK_INET_ATON, NONRESTOKEN_), + ParKeyWord("INET_NTOA", TOK_INET_NTOA, NONRESTOKEN_), ParKeyWord("INITIAL", TOK_INITIAL, NONRESTOKEN_), ParKeyWord("INITIALIZATION", TOK_INITIALIZATION, FLAGSNONE_), ParKeyWord("INITIALIZE", TOK_INITIALIZE, FIRST_|NONRESTOKEN_), @@ -543,6 +545,8 @@ ParKeyWord ParKeyWords::keyWords_[] = { ParKeyWord("ISNULL", TOK_ISNULL, NONRESTOKEN_), ParKeyWord("ISOLATE", TOK_ISOLATE, NONRESTOKEN_), ParKeyWord("ISOLATION", TOK_ISOLATION, ANS_|RESWORD_), + ParKeyWord("IS_IPV4", TOK_ISIPV4, NONRESTOKEN_), + ParKeyWord("IS_IPV6", TOK_ISIPV6, NONRESTOKEN_), ParKeyWord("ITERATE", TOK_ITERATE, COMPAQ_|RESWORD_), ParKeyWord("IUDLOG", TOK_IUDLOG, FLAGSNONE_), ParKeyWord("IUD_LOG_TABLE", TOK_IUD_LOG_TABLE, FLAGSNONE_), @@ -604,6 +608,7 @@ ParKeyWord ParKeyWords::keyWords_[] = { ParKeyWord("LOCKONREFRESH", TOK_LOCKONREFRESH, FLAGSNONE_), ParKeyWord("LOG", TOK_LOG, NONRESTOKEN_), ParKeyWord("LOG10", TOK_LOG10, NONRESTOKEN_), + ParKeyWord("LOG2", TOK_LOG2, NONRESTOKEN_), ParKeyWord("LOGGABLE", TOK_LOGGABLE, FLAGSNONE_), ParKeyWord("LOGON", TOK_LOGON, NONRESTOKEN_), ParKeyWord("LONG", TOK_LONG, NONRESTOKEN_), http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/ba17bd3b/core/sql/parser/sqlparser.y ---------------------------------------------------------------------- diff --git a/core/sql/parser/sqlparser.y b/core/sql/parser/sqlparser.y index 7804a73..57c1055 100755 --- a/core/sql/parser/sqlparser.y +++ b/core/sql/parser/sqlparser.y @@ -398,6 +398,8 @@ static void enableMakeQuotedStringISO88591Mechanism() %token <tokval> TOK_NAME /* ANSI SQL non-reserved word */ %token <tokval> TOK_UNNAMED /* ANSI SQL non-reserved word */ %token <tokval> TOK_INDICATOR_TYPE /* Tandem extension non-reserved word */ +%token <tokval> TOK_INET_ATON /* MySQL extension non-reserved word */ +%token <tokval> TOK_INET_NTOA /* MySQL extension non-reserved word */ %token <tokval> TOK_VARIABLE_POINTER /* Tandem extension non-reserved word */ %token <tokval> TOK_INDICATOR_POINTER /* Tandem extension non-reserved word */ %token <tokval> TOK_RETURNED_LENGTH /* ANSI SQL non-reserved word */ @@ -801,6 +803,7 @@ static void enableMakeQuotedStringISO88591Mechanism() %token <tokval> TOK_IUD_LOG_TABLE %token <tokval> TOK_RANGE_LOG_TABLE //++ MV %token <tokval> TOK_LOG10 +%token <tokval> TOK_LOG2 %token <tokval> TOK_LONGWVARCHAR %token <tokval> TOK_LOW_VALUE /* Tandem extension non-reserved word */ @@ -1290,6 +1293,8 @@ static void enableMakeQuotedStringISO88591Mechanism() %token <tokval> TOK_INSERTLOG // MV %token <tokval> TOK_INVALID %token <tokval> TOK_INVALIDATE +%token <tokval> TOK_ISIPV4 +%token <tokval> TOK_ISIPV6 %token <tokval> TOK_ISLACK /* Tandem extension */ %token <tokval> TOK_K /* Tandem extension */ %token <tokval> TOK_KEY @@ -9510,6 +9515,7 @@ math_func_1_operand : | TOK_FLOOR {$$ = ITM_FLOOR;} | TOK_LOG {$$ = ITM_LOG;} | TOK_LOG10 {$$ = ITM_LOG10;} + | TOK_LOG2 {$$ = ITM_LOG2;} | TOK_RADIANS {$$ = ITM_RADIANS;} | TOK_SIN {$$ = ITM_SIN;} | TOK_SINH {$$ = ITM_SINH;} @@ -9575,7 +9581,34 @@ misc_function : CmpCommon::statementHeap(), 1, $3); } - + | TOK_ISIPV4 '(' value_expression ')' + { + $$ = new (PARSERHEAP()) + BuiltinFunction(ITM_ISIPV4, + CmpCommon::statementHeap(), + 1, $3); + } + | TOK_ISIPV6 '(' value_expression ')' + { + $$ = new (PARSERHEAP()) + BuiltinFunction(ITM_ISIPV6, + CmpCommon::statementHeap(), + 1, $3); + } + | TOK_INET_ATON '(' value_expression ')' + { + $$ = new (PARSERHEAP()) + BuiltinFunction(ITM_INET_ATON, + CmpCommon::statementHeap(), + 1, $3); + } + | TOK_INET_NTOA '(' value_expression ')' + { + $$ = new (PARSERHEAP()) + BuiltinFunction(ITM_INET_NTOA, + CmpCommon::statementHeap(), + 1, $3); + } | TOK_ISNULL '(' value_expression ',' value_expression ')' { $$ = new (PARSERHEAP()) @@ -33619,8 +33652,12 @@ nonreserved_func_word: TOK_ABS | TOK_HBASE_TIMESTAMP | TOK_HBASE_VERSION | TOK_HIVEMD + | TOK_INET_ATON + | TOK_INET_NTOA | TOK_INITIAL | TOK_INSTR + | TOK_ISIPV4 + | TOK_ISIPV6 | TOK_KEY_RANGE_COMPARE | TOK_JULIANTIMESTAMP | TOK_LASTNOTNULL @@ -33631,6 +33668,7 @@ nonreserved_func_word: TOK_ABS | TOK_LOCATE | TOK_LOG | TOK_LOG10 + | TOK_LOG2 | TOK_LPAD | TOK_LTRIM | TOK_MAVG