trafodion-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lium...@apache.org
Subject [01/13] incubator-trafodion git commit: jira 2227
Date Tue, 06 Dec 2016 14:50:35 GMT
Repository: incubator-trafodion
Updated Branches:
  refs/heads/master 1430279d9 -> 52ae2089c


http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/8a49f900/core/sql/exp/exp_function.cpp
----------------------------------------------------------------------
diff --git a/core/sql/exp/exp_function.cpp b/core/sql/exp/exp_function.cpp
index 22fef99..124e154 100644
--- a/core/sql/exp/exp_function.cpp
+++ b/core/sql/exp/exp_function.cpp
@@ -71,6 +71,7 @@
 #include "NAUserId.h"
 #include "ComUser.h"
 #include "ExpSeqGen.h"
+#include "json.h"
 
 #undef DllImport
 #define DllImport __declspec ( dllimport )
@@ -124,6 +125,10 @@ extern char * exClauseGetText(OperatorTypeEnum ote);
 NA_EIDPROC
 void setVCLength(char * VCLen, Lng32 VCLenSize, ULng32 value);
 
+NA_EIDPROC
+static void ExRaiseJSONError(CollHeap* heap, ComDiagsArea** diagsArea, JsonReturnType type);
+
+
 //#define TOUPPER(c) (((c >= 'a') && (c <= 'z')) ? (c - 32) : c);
 //#define TOLOWER(c) (((c >= 'A') && (c <= 'Z')) ? (c + 32) : c);
 
@@ -181,6 +186,8 @@ ex_function_ansi_user::ex_function_ansi_user(){};
 ex_function_user::ex_function_user(){};
 ex_function_nullifzero::ex_function_nullifzero(){};
 ex_function_nvl::ex_function_nvl(){};
+ex_function_json_object_field_text::ex_function_json_object_field_text(){};
+
 ex_function_queryid_extract::ex_function_queryid_extract(){};
 ExFunctionUniqueId::ExFunctionUniqueId(){};
 ExFunctionRowNum::ExFunctionRowNum(){};
@@ -613,6 +620,13 @@ ex_function_nvl::ex_function_nvl(OperatorTypeEnum oper_type,
 {
 };
 
+ex_function_json_object_field_text::ex_function_json_object_field_text (OperatorTypeEnum
oper_type,
+				 Attributes ** attr, Space * space)
+     : ex_function_clause(oper_type, 3, attr, space)
+{
+};
+
+
 ex_function_queryid_extract::ex_function_queryid_extract(OperatorTypeEnum oper_type,
 							 Attributes ** attr, Space * space)
      : ex_function_clause(oper_type, 3, attr, space)
@@ -6480,6 +6494,54 @@ ex_expr::exp_return_type ex_function_nvl::eval(char *op_data[],
   return ex_expr::EXPR_OK;
 }
 
+ex_expr::exp_return_type ex_function_json_object_field_text::eval(char *op_data[],
+					       CollHeap *heap,
+					       ComDiagsArea** diagsArea)
+{
+    CharInfo::CharSet cs = ((SimpleType *)getOperand(1))->getCharSet();
+    // search for operand 1
+#pragma nowarn(1506)   // warning elimination 
+    Lng32 len1 = getOperand(1)->getLength(op_data[-MAX_OPERANDS+1]);
+#pragma warn(1506)  // warning elimination 
+    if ( cs == CharInfo::UTF8 )
+    {
+        Int32 prec1 = ((SimpleType *)getOperand(1))->getPrecision();
+        len1 = Attributes::trimFillerSpaces( op_data[1], prec1, len1, cs );
+    }
+
+    // in operand 2
+#pragma nowarn(1506)   // warning elimination 
+    Lng32 len2 = getOperand(2)->getLength(op_data[-MAX_OPERANDS+2]);
+#pragma warn(1506)  // warning elimination 
+    if ( cs == CharInfo::UTF8 )
+    {
+        Int32 prec2 = ((SimpleType *)getOperand(2))->getPrecision();
+        len2 = Attributes::trimFillerSpaces( op_data[2], prec2, len2, cs );
+    }
+    char *rltStr = NULL;
+    JsonReturnType ret = json_extract_path_text(&rltStr, op_data[1], 1, op_data[2]);
+    if (ret != JSON_OK)
+    {
+        ExRaiseJSONError(heap, diagsArea, ret);
+        return ex_expr::EXPR_ERROR;
+    }
+    if (rltStr != NULL)
+    {
+        Lng32 rltLen = str_len(rltStr)+1;
+        str_cpy_all(op_data[0], rltStr, rltLen);
+        free(rltStr);
+
+        // If result is a varchar, store the length of substring 
+        // in the varlen indicator.
+        if (getOperand(0)->getVCIndicatorLength() > 0)
+            getOperand(0)->setVarLength(rltLen, op_data[-MAX_OPERANDS]);
+    }
+    else
+        getOperand(0)->setVarLength(0, op_data[-MAX_OPERANDS]);
+
+    return ex_expr::EXPR_OK;
+}
+
 //
 // Clause used to clear header bytes for both disk formats
 // SQLMX_FORMAT and SQLMX_ALIGNED_FORMAT.  The number of bytes to clear
@@ -8235,5 +8297,49 @@ ex_expr::exp_return_type ExFunctionIsIP::eval(char * op_data[],
   }
 }
 
+// Parse json errors
+static void ExRaiseJSONError(CollHeap* heap, ComDiagsArea** diagsArea, JsonReturnType type)
+{
+    switch(type)
+    {
+    case JSON_INVALID_TOKEN:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_INVALID_TOKEN);
+        break;
+    case JSON_INVALID_VALUE:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_INVALID_VALUE);
+        break;
+    case JSON_INVALID_STRING:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_INVALID_STRING);
+        break;
+    case JSON_INVALID_ARRAY_START:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_INVALID_ARRAY_START);
+        break;
+    case JSON_INVALID_ARRAY_NEXT:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_INVALID_ARRAY_NEXT);
+        break;
+    case JSON_INVALID_OBJECT_START:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_INVALID_OBJECT_START);
+        break;
+    case JSON_INVALID_OBJECT_LABEL:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_INVALID_OBJECT_LABEL);
+        break;
+    case JSON_INVALID_OBJECT_NEXT:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_INVALID_OBJECT_NEXT);
+        break;
+    case JSON_INVALID_OBJECT_COMMA:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_INVALID_OBJECT_COMMA);
+        break;
+    case JSON_INVALID_END:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_INVALID_END);
+        break;
+    case JSON_END_PREMATURELY:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_END_PREMATURELY);
+        break;
+    default:
+        ExRaiseSqlError(heap, diagsArea, EXE_JSON_UNEXPECTED_ERROR);
+        break;
+    }
+}
+
 // LCOV_EXCL_STOP
 #pragma warn(1506)  // warning elimination 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/8a49f900/core/sql/exp/exp_function.h
----------------------------------------------------------------------
diff --git a/core/sql/exp/exp_function.h b/core/sql/exp/exp_function.h
index cfa5907..1b582ba 100644
--- a/core/sql/exp/exp_function.h
+++ b/core/sql/exp/exp_function.h
@@ -3757,6 +3757,28 @@ private:
   // ---------------------------------------------------------------------
 };
 
+class SQLEXP_LIB_FUNC  ex_function_json_object_field_text : public ex_function_clause {
+public:
+  NA_EIDPROC ex_function_json_object_field_text(OperatorTypeEnum oper_type,
+			     Attributes ** attr,
+			     Space * space);
+  NA_EIDPROC ex_function_json_object_field_text();
+
+  NA_EIDPROC ex_expr::exp_return_type eval(char *op_data[], CollHeap*, 
+					   ComDiagsArea** = 0);  
+  NA_EIDPROC Long pack(void *);
+
+  NA_EIDPROC virtual short getClassSize() { return (short)sizeof(*this); }
+
+  // This clause handles all NULL processing in the eval() method.
+  NA_EIDPROC Int32 isNullRelevant() const { return 0; };
+
+private:
+  char fillers_[64];
+  // ---------------------------------------------------------------------
+};
+
+
 class SQLEXP_LIB_FUNC ExFunctionExtractColumns : public ex_function_clause
 {
    public:

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/8a49f900/core/sql/generator/GenItemFunc.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenItemFunc.cpp b/core/sql/generator/GenItemFunc.cpp
index 77d9913..8d745b0 100644
--- a/core/sql/generator/GenItemFunc.cpp
+++ b/core/sql/generator/GenItemFunc.cpp
@@ -592,6 +592,13 @@ short BuiltinFunction::codeGen(Generator * generator)
 						     attr, space);
       }
       break;
+    case ITM_JSONOBJECTFIELDTEXT:
+    {
+	  function_clause =
+	    new(generator->getSpace()) ex_function_json_object_field_text(getOperatorType(),
+						     attr, space);
+    }
+    break;
       
     case ITM_QUERYID_EXTRACT:
       {

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/8a49f900/core/sql/optimizer/BindItemExpr.cpp
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/BindItemExpr.cpp b/core/sql/optimizer/BindItemExpr.cpp
index d145a18..2a1ff7f 100644
--- a/core/sql/optimizer/BindItemExpr.cpp
+++ b/core/sql/optimizer/BindItemExpr.cpp
@@ -3283,7 +3283,10 @@ ItemExpr *BuiltinFunction::bindNode(BindWA *bindWA)
 
 	break;
       }
-
+    case ITM_JSONOBJECTFIELDTEXT:
+    {
+        break;
+    }
     case ITM_QUERYID_EXTRACT:
       {
         // type cast any params

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/8a49f900/core/sql/optimizer/ItemExpr.cpp
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/ItemExpr.cpp b/core/sql/optimizer/ItemExpr.cpp
index 564f6e3..2462220 100644
--- a/core/sql/optimizer/ItemExpr.cpp
+++ b/core/sql/optimizer/ItemExpr.cpp
@@ -7432,6 +7432,11 @@ NABoolean BuiltinFunction::isCacheableExpr(CacheWA& cwa)
 	return FALSE;
       }
     break;
+    case ITM_JSONOBJECTFIELDTEXT:
+    {
+	    return FALSE;
+    }
+    break;
 
     default:
       {
@@ -7539,6 +7544,8 @@ const NAString BuiltinFunction::getText() const
       return "nullifzero";
     case ITM_NVL:
       return "nvl";
+    case ITM_JSONOBJECTFIELDTEXT:
+      return "json_object_field_text";
     case ITM_QUERYID_EXTRACT:
       return "queryid_extract";
     case ITM_UPPER:
@@ -7797,7 +7804,12 @@ ItemExpr * BuiltinFunction::copyTopNode(ItemExpr * derivedNode,
 						   outHeap, 2, child(0), child(1));
 	  }
 	break;
-
+    case ITM_JSONOBJECTFIELDTEXT:
+	{
+	    result = new (outHeap) BuiltinFunction(getOperatorType(),
+						   outHeap, 2, child(0), child(1));
+	}
+	break;
 	default:
 	  {
 	    ABORT("copyTopNode() can only be called for a derived class of BuiltinFunction");

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/8a49f900/core/sql/optimizer/SynthType.cpp
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/SynthType.cpp b/core/sql/optimizer/SynthType.cpp
index 8c78d85..35202b4 100644
--- a/core/sql/optimizer/SynthType.cpp
+++ b/core/sql/optimizer/SynthType.cpp
@@ -1204,6 +1204,30 @@ const NAType *BuiltinFunction::synthesizeType()
       }
     break;
 
+    case ITM_JSONOBJECTFIELDTEXT:
+    {
+        ValueId vid1 = child(0)->getValueId();
+        ValueId vid2 = child(1)->getValueId();
+
+        // untyped param operands are typed as CHAR
+        vid2.coerceType(NA_CHARACTER_TYPE);
+
+        const NAType &typ1 = vid1.getType();
+        const NAType &typ2 = vid2.getType();
+
+        if ((typ1.getTypeQualifier() != NA_CHARACTER_TYPE) ||
+            (typ2.getTypeQualifier() != NA_CHARACTER_TYPE))
+        {
+            // 4043 The operand of a $0~String0 function must be character.
+            *CmpCommon::diags() << DgSqlCode(-4043) << DgString0(getTextUpper());
+            return NULL;
+        }
+
+        retType = new HEAP
+        SQLVarChar(typ1.getNominalSize(), typ1.supportsSQLnull());
+    }
+    break;
+
     case ITM_QUERYID_EXTRACT:
       {
 	// type cast any params

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/8a49f900/core/sql/parser/ParKeyWords.cpp
----------------------------------------------------------------------
diff --git a/core/sql/parser/ParKeyWords.cpp b/core/sql/parser/ParKeyWords.cpp
index 0cb1aec..ec243c8 100644
--- a/core/sql/parser/ParKeyWords.cpp
+++ b/core/sql/parser/ParKeyWords.cpp
@@ -556,7 +556,8 @@ ParKeyWord ParKeyWords::keyWords_[] = {
   ParKeyWord("INVALIDATE", 	   TOK_INVALIDATE, NONRESTOKEN_),
   ParKeyWord("JAVA",               TOK_JAVA,        NONRESTOKEN_),
   ParKeyWord("JOIN",               TOK_JOIN,        SECOND_|ANS_|RESWORD_|MPWORD_),
-  ParKeyWord("JOURNAL",            TOK_JOURNAL,     NONRESTOKEN_),  
+  ParKeyWord("JOURNAL",            TOK_JOURNAL,     NONRESTOKEN_), 
+  ParKeyWord("JSON_OBJECT_FIELD_TEXT",  TOK_JSONOBJECTFIELDTEXT, NONRESTOKEN_),
   ParKeyWord("JULIANTIMESTAMP",    TOK_JULIANTIMESTAMP, NONRESTOKEN_),
   ParKeyWord("K",                  TOK_K,           NONRESTOKEN_),
   ParKeyWord("KEY",                TOK_KEY,         ANS_|RESWORD_|MPWORD_),

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/8a49f900/core/sql/parser/sqlparser.y
----------------------------------------------------------------------
diff --git a/core/sql/parser/sqlparser.y b/core/sql/parser/sqlparser.y
index 0987c1f..29d2bf8 100755
--- a/core/sql/parser/sqlparser.y
+++ b/core/sql/parser/sqlparser.y
@@ -773,6 +773,7 @@ static void enableMakeQuotedStringISO88591Mechanism()
 %token <tokval> TOK_IUDLOG	// MV REFRESH
 %token <tokval> TOK_JAVA                /* Tandem extension non-reserved word */
 %token <tokval> TOK_JOIN
+%token <tokval> TOK_JSONOBJECTFIELDTEXT
 %token <tokval> TOK_JULIANTIMESTAMP     /* Tandem extension */
 %token <tokval> TOK_LAG                 /* LAG OLAP Function */
 %token <tokval> TOK_LANGUAGE
@@ -9780,6 +9781,11 @@ misc_function :
 						    2, $3, $5);
 				}
 
+    | TOK_JSONOBJECTFIELDTEXT '(' value_expression ',' value_expression ')'
+    {
+        $$ = new (PARSERHEAP()) 
+        BuiltinFunction(ITM_JSONOBJECTFIELDTEXT, CmpCommon::statementHeap(), 2, $3, $5);
+    }
      | TOK_NULLIF '(' value_expression ',' value_expression ')'
               {
                 $$ = new (PARSERHEAP()) ZZZBinderFunction(ITM_NULLIF, $3, $5);
@@ -33735,7 +33741,6 @@ nonreserved_func_word:  TOK_ABS
                       | TOK_FLOOR
                       | TOK_FN
                       | TOK_GREATEST
-                      | TOK_GROUPING_ID
                       | TOK_HASHPARTFUNC
                       | TOK_HASH2PARTFUNC
                       | TOK_HBASE_TIMESTAMP
@@ -33748,6 +33753,7 @@ nonreserved_func_word:  TOK_ABS
                       | TOK_ISIPV4
                       | TOK_ISIPV6
                       | TOK_KEY_RANGE_COMPARE
+                      | TOK_JSONOBJECTFIELDTEXT
                       | TOK_JULIANTIMESTAMP
                       | TOK_LASTNOTNULL
                       | TOK_LAST_DAY


Mime
View raw message