Merge pull request #1845 from sandhyasun/sandhya_merge_lob_fixes

[TRAFODION-3263] and other misc fixes for lob locking and refactoring 
diff --git a/core/sql/cli/Cli.cpp b/core/sql/cli/Cli.cpp
index b4f16d3..1c7fec2 100644
--- a/core/sql/cli/Cli.cpp
+++ b/core/sql/cli/Cli.cpp
@@ -6961,14 +6961,15 @@
 Lng32 SQLCLI_SwitchContext(
      /*IN*/ CliGlobals * cliGlobals,
      /*IN*/           SQLCTX_HANDLE   context_handle,
-     /*OUT OPTIONAL*/ SQLCTX_HANDLE * prev_context_handle)
+     /*OUT OPTIONAL*/ SQLCTX_HANDLE * prev_context_handle,
+     /*IN */    bool allowSwitchBackToDefault )
 {
   Lng32 retcode = SUCCESS;
 
   ContextCli   & currContext = *(cliGlobals->currContext());
   ComDiagsArea & diags       = currContext.diags();
 
-  if (context_handle == cliGlobals->getDefaultContext()->getContextHandle())
+  if (!allowSwitchBackToDefault && (context_handle == cliGlobals->getDefaultContext()->getContextHandle()))
   {
      diags << DgSqlCode(-CLI_DEFAULT_CONTEXT_NOT_ALLOWED);
      return SQLCLI_ReturnCode(&currContext,-CLI_DEFAULT_CONTEXT_NOT_ALLOWED);
@@ -8371,6 +8372,10 @@
   short schNameLen = 0;
   char schName[512];
   char logBuf[4096];
+  Int64 inputValues[5];
+  Lng32 inputValuesLen = 0;
+  Int64 rowsAffected = 0;
+
   lobDebugInfo("In LobCliInterface",0,__LINE__,lobTrace);
   if (inLobHandle)
     {
@@ -8405,7 +8410,7 @@
   lobDebugInfo(logBuf,0,__LINE__,lobTrace);
 
   char * query = new(currContext.exHeap()) char[4096];
-
+  char stmtName[200];
   if (outLobHandleLen)
     *outLobHandleLen = 0;
 
@@ -8444,10 +8449,9 @@
       {
 	strcpy(query, "set transaction autocommit on;");
 	cliRC = cliInterface->executeImmediate(query);
-
-	if (cliRC < 0)
-	    goto error_return;
-
+        if (cliRC < 0)
+          goto error_return;
+        
 	cliRC = 0;
       }
       break;
@@ -8554,17 +8558,21 @@
     case LOB_CLI_INSERT:
       {
 	// insert into lob descriptor handle table
-	str_sprintf(query, "select syskey from (insert into table(ghost table %s) values (%ld, 1, %ld)) x",
-		    lobDescHandleName, descPartnKey, (dataLen ? *dataLen : 0));
+        str_sprintf(query, "select syskey from (insert into table(ghost table %s) values (%ld, 1, %ld)) x",
+                    lobDescHandleName, descPartnKey, (dataLen ? *dataLen : 0));
+                
+		   
         lobDebugInfo(query,0,__LINE__,lobTrace);
 	// set parserflags to allow ghost table
 	currContext.setSqlParserFlags(0x1);
 	
-	Int64 descSyskey = 0;
+        Int64 descSyskey = 0;
 	Lng32 len = 0;
-	cliRC = cliInterface->executeImmediate(query,
+     
+        cliRC = cliInterface->executeImmediate(query,
 					       (char*)&descSyskey, &len, FALSE);
-
+        if (cliRC < 0)
+          goto error_return;
 	currContext.resetSqlParserFlags(0x1);
 
 	if (cliRC < 0)
@@ -9163,8 +9171,9 @@
       tempCliRC = cliInterface->executeImmediate(query);
       if (tempCliRC < 0)
 	cliRC = tempCliRC;
+    
     }
-
+ 
   NADELETEBASIC(query, currContext.exHeap());
 
   if (cliRC < 0)
diff --git a/core/sql/cli/Cli.h b/core/sql/cli/Cli.h
index c5aa690..52f90d5 100644
--- a/core/sql/cli/Cli.h
+++ b/core/sql/cli/Cli.h
@@ -607,7 +607,8 @@
 
    Lng32 SQLCLI_SwitchContext(/*IN*/           CliGlobals    * cliGlobals,
                              /*IN*/           SQLCTX_HANDLE   ctxt_handle,
-                             /*OUT OPTIONAL*/ SQLCTX_HANDLE * prev_ctxt_handle);
+                              /*OUT OPTIONAL*/ SQLCTX_HANDLE * prev_ctxt_handle,
+                              /*IN */ bool allowSwitchBackToDefault);
 
    Lng32 SQLCLI_Xact(/*IN*/                 CliGlobals * cliGlobals,
                     /*IN* (SQLTRANS_COMMAND) */  Lng32   command,
diff --git a/core/sql/cli/CliExtern.cpp b/core/sql/cli/CliExtern.cpp
index 7091d14..e504a07 100644
--- a/core/sql/cli/CliExtern.cpp
+++ b/core/sql/cli/CliExtern.cpp
@@ -5380,32 +5380,11 @@
 
 
 Lng32 SQL_EXEC_SwitchContext(/*IN*/           SQLCTX_HANDLE   ctxt_handle,
-                            /*OUT OPTIONAL*/ SQLCTX_HANDLE * prev_ctxt_handle)
-{
-   Lng32 retcode;
-
-   CLI_NONPRIV_PROLOGUE(retcode);
-
-   try
-   {
-      retcode =
-      SQLCLI_SwitchContext(GetCliGlobals(),
-			   ctxt_handle,
-			   prev_ctxt_handle);
-   }
-   catch(...)
-   {
-     retcode = -CLI_INTERNAL_ERROR;
-#if defined(_THROW_EXCEPTIONS)
-     if (cliWillThrow())
-       {
-         throw;
-       }
-#endif
-   }
-
-   RecordError(NULL, retcode);
-   return retcode;
+                             /*OUT OPTIONAL*/ SQLCTX_HANDLE * prev_ctxt_handle
+                             )
+{  
+  return  SQL_EXEC_SwitchContext_Internal(ctxt_handle,
+                                          prev_ctxt_handle, FALSE);
 }
 Lng32 SQL_EXEC_SWITCHCONTEXT(
 		/*IN*/ SQLCTX_HANDLE context_handle,
@@ -5742,6 +5721,36 @@
    return retcode;
 }
 
+Int32 SQL_EXEC_SwitchContext_Internal(/*IN*/ SQLCTX_HANDLE context_handle,
+                                /*OUT OPTIONAL*/ SQLCTX_HANDLE * prev_context_handle,
+                                /*IN*/ Int32 allowSwitchBackToDefault)
+{
+  Lng32 retcode;
+
+   CLI_NONPRIV_PROLOGUE(retcode);
+
+   try
+   {
+      retcode =
+      SQLCLI_SwitchContext(GetCliGlobals(),
+			   context_handle,
+			   prev_context_handle, allowSwitchBackToDefault);
+   }
+   catch(...)
+   {
+     retcode = -CLI_INTERNAL_ERROR;
+#if defined(_THROW_EXCEPTIONS)
+     if (cliWillThrow())
+       {
+         throw;
+       }
+#endif
+   }
+
+   RecordError(NULL, retcode);
+   return retcode;
+}
+
 Lng32 SQL_EXEC_AssignParserFlagsForExSqlComp_Internal(ULng32 flagbits)
 {
    Lng32 retcode;
diff --git a/core/sql/cli/Globals.cpp b/core/sql/cli/Globals.cpp
index 551fe3c..a3e9a75 100644
--- a/core/sql/cli/Globals.cpp
+++ b/core/sql/cli/Globals.cpp
@@ -619,6 +619,9 @@
         tsCurrentContextMap != NULL && 
            newContext == tsCurrentContextMap->context_)
      return 0;
+  if (newContext == defaultContext_)
+    if (tsCurrentContextMap)
+      tsCurrentContextMap->context_ = NULL;
   retcode = currContext()->getTransaction()->suspendTransaction();
   if (retcode != 0)
      return retcode;
diff --git a/core/sql/cli/SQLCLIdev.h b/core/sql/cli/SQLCLIdev.h
index 59646be..e7dee07 100644
--- a/core/sql/cli/SQLCLIdev.h
+++ b/core/sql/cli/SQLCLIdev.h
@@ -96,7 +96,12 @@
                                    // level.
 
 };
-
+//This internal call allows a caller to switch back to the default context.
+//In general this is not allow. But it's currently used in one place in 
+//UdrServer alone
+Int32 SQL_EXEC_SwitchContext_Internal(/*IN*/ Int32 context_handle,
+                                /*OUT OPTIONAL*/ Int32 *prev_context_handle,
+                                /*IN*/ Int32 allowSwitchBackToDefault);
 enum SQLATTRHOLDABLE_INTERNAL_TYPE
 {
   SQLCLIDEV_NONHOLDABLE = SQL_NONHOLDABLE,
diff --git a/core/sql/cli/sqlcli.h b/core/sql/cli/sqlcli.h
index 2bed109..9288493 100644
--- a/core/sql/cli/sqlcli.h
+++ b/core/sql/cli/sqlcli.h
@@ -1907,7 +1907,7 @@
 Int32 SQL_EXEC_SwitchContext(
 		/*IN*/ SQLCTX_HANDLE context_handle,
 		/*OUT OPTIONAL*/ SQLCTX_HANDLE * prev_context_handle);
-
+               
 Int32 SQL_EXEC_SWITCHCONTEXT(
 		/*IN*/ SQLCTX_HANDLE context_handle,
 		/*OUT OPTIONAL*/ SQLCTX_HANDLE * prev_context_handle);
diff --git a/core/sql/executor/HdfsClient_JNI.cpp b/core/sql/executor/HdfsClient_JNI.cpp
index 51bc30e..597e661 100644
--- a/core/sql/executor/HdfsClient_JNI.cpp
+++ b/core/sql/executor/HdfsClient_JNI.cpp
@@ -461,7 +461,7 @@
     JavaMethods_[JM_HDFS_WRITE      ].jm_name      = "hdfsWrite";
     JavaMethods_[JM_HDFS_WRITE      ].jm_signature = "([B)I";
     JavaMethods_[JM_HDFS_WRITE_IMMEDIATE].jm_name      = "hdfsWriteImmediate";
-    JavaMethods_[JM_HDFS_WRITE_IMMEDIATE].jm_signature = "([B)J";
+    JavaMethods_[JM_HDFS_WRITE_IMMEDIATE].jm_signature = "([BZ)J";
     JavaMethods_[JM_HDFS_READ       ].jm_name      = "hdfsRead";
     JavaMethods_[JM_HDFS_READ       ].jm_signature = "(JLjava/nio/ByteBuffer;)I";
     JavaMethods_[JM_HDFS_CLOSE      ].jm_name      = "hdfsClose";
@@ -692,7 +692,7 @@
   return bytesWritten; 
 }
 
-Int64 HdfsClient::hdfsWriteImmediate(const char* data, Int64 len, HDFS_Client_RetCode &hdfsClientRetcode, int maxChunkSize)
+Int64 HdfsClient::hdfsWriteImmediate(const char* data, Int64 len, HDFS_Client_RetCode &hdfsClientRetcode, int maxChunkSize, NABoolean doRetry)
 {
   QRLogger::log(CAT_SQL_HDFS, LL_DEBUG, "HdfsClient::hdfsWriteImmediate(%ld) called.", len);
 
@@ -706,6 +706,7 @@
   Int64 offset = 0;
   jlong writeOffset = -1;
   jlong chunkWriteOffset;
+  jboolean j_doRetry = doRetry;
   do 
   {
      if ((chunkLen > 0) && (lenRemain > chunkLen))
@@ -727,7 +728,7 @@
 
      tsRecentJMFromJNI = JavaMethods_[JM_HDFS_WRITE_IMMEDIATE].jm_full_name;
      // Java method returns the cumulative bytes written
-     chunkWriteOffset = jenv_->CallIntMethod(javaObj_, JavaMethods_[JM_HDFS_WRITE_IMMEDIATE].methodID, jbArray);
+     chunkWriteOffset = jenv_->CallIntMethod(javaObj_, JavaMethods_[JM_HDFS_WRITE_IMMEDIATE].methodID, jbArray,j_doRetry);
      if (writeOffset == -1)
         writeOffset = chunkWriteOffset;
 
diff --git a/core/sql/executor/HdfsClient_JNI.h b/core/sql/executor/HdfsClient_JNI.h
index 0791b25..f643937 100644
--- a/core/sql/executor/HdfsClient_JNI.h
+++ b/core/sql/executor/HdfsClient_JNI.h
@@ -190,7 +190,7 @@
   HDFS_Client_RetCode    hdfsOpen(const char* path, NABoolean compress);
   Int64                  hdfsSize(HDFS_Client_RetCode &hdfsClientRetcode);
   Int32                  hdfsWrite(const char* data, Int64 size, HDFS_Client_RetCode &hdfsClientRetcode, int maxChunkSize = 0);
-  Int64                  hdfsWriteImmediate(const char* data, Int64 size, HDFS_Client_RetCode &hdfsClientRetcode, int maxChunkSize = 0);
+  Int64                  hdfsWriteImmediate(const char* data, Int64 size, HDFS_Client_RetCode &hdfsClientRetcode, int maxChunkSize = 0, NABoolean doRetry = FALSE);
   Int32                  hdfsRead(Int64 offset, const char* data, Int64 size, HDFS_Client_RetCode &hdfsClientRetcode);
   HDFS_Client_RetCode    hdfsClose();
   HDFS_Client_RetCode    setHdfsFileInfo(JNIEnv *jenv, jint numFiles, jint fileNo, jboolean isDir, 
diff --git a/core/sql/exp/ExpLOB.cpp b/core/sql/exp/ExpLOB.cpp
index 84a8512..3aba911 100644
--- a/core/sql/exp/ExpLOB.cpp
+++ b/core/sql/exp/ExpLOB.cpp
@@ -706,9 +706,7 @@
                                                 CollHeap*h,
                                                 ComDiagsArea** diagsArea)
 {
-  Lng32 rc;
-
- 
+  Lng32 rc; 
   Lng32 handleLen = 0;
   char lobHandleBuf[LOB_HANDLE_LEN];
   Int64 descTS = NA_JulianTimestamp();
@@ -988,7 +986,7 @@
 					    CollHeap*h,
 					    ComDiagsArea** diagsArea)
 {
-
+  char timeBuf[1024] = "";
   ex_expr::exp_return_type err;
   Int32 retcode = 0;
   Int32 cliError = 0;
@@ -1130,6 +1128,8 @@
           return err;      
         }
     }
+ 
+ 
   err = insertDesc(op_data,inputAddr, chunkMemSize,lobHdfsOffset, result,h, diagsArea);
   if (err == ex_expr::EXPR_ERROR)  
     {
@@ -1189,9 +1189,9 @@
             }
           char * handle = op_data[0];
           handleLen = getOperand(0)->getLength();
-          LobsSubOper so = Lob_None;
+            LobsSubOper so = Lob_None;
           if (fromFile())
-            so = Lob_File;
+            so = Lob_Memory; //It's already been read into memory above
           else if (fromString()) {
             if (getOperand(1)->getVCIndicatorLength() > 0)
               lobLen = getOperand(1)->getLength(op_data[1]-getOperand(1)->getVCIndicatorLength());
@@ -1202,7 +1202,8 @@
           else if (fromBuffer())
             so= Lob_Buffer;
           else if (fromExternal())
-            so = Lob_External_File;
+          so = Lob_External_File;
+         
 
           retcode = ExpLOBInterfaceUpdateAppend
             (getExeGlobals()->getExLobGlobal(), 
@@ -1237,7 +1238,12 @@
             }
           inputSize -= chunkMemSize;
           if (fromFile())
-            sourceFileReadOffset +=chunkMemSize;       
+            {
+              sourceFileReadOffset +=chunkMemSize; 
+              getExeGlobals()->getExLobGlobal()->getHeap()->deallocateMemory(inputAddr);
+              
+            }
+                        
           inputAddr += chunkMemSize;
         }
     }
@@ -1418,6 +1424,12 @@
   char *inputAddr = NULL;
   Int64 lobLen = 0;
   char *lobData = NULL;
+  Int64 sourceFileSize = 0; // If the input is from a file get total file size
+  Int64 sourceFileReadOffset = 0; 
+  Int64 chunkMemSize = 0;
+  char *retBuf = 0;
+  Int64 retReadLen = 0;
+  Int32 cliError = 0;
   Lng32 sLobType;
   Int64 sUid;
   Lng32 sLobNum;
@@ -1426,7 +1438,31 @@
   Int16 sFlags;
   short sSchNameLen = 0;
   char sSchName[500];
+  char tgtLobNameBuf[LOB_NAME_LEN];
+  char *tgtLobName = NULL;
   lobLen = getOperand(1)->getLength();
+
+  char llid[LOB_LOCK_ID_SIZE];
+  if (lobLocking())
+    {
+      ExpLOBoper::genLobLockId(objectUID_,lobNum(),llid);;
+      NABoolean found = FALSE;
+      retcode = SQL_EXEC_CheckLobLock(llid, &found);
+      if (! retcode && !found) 
+        {    
+          retcode = SQL_EXEC_SetLobLock(llid);
+        }
+      else if (found)
+        {
+          Int32 lobError = LOB_DATA_FILE_LOCK_ERROR;
+          ExRaiseSqlError(h, diagsArea, 
+                          (ExeErrorCode)(8558), NULL,(Int32 *)&lobError, 
+                          NULL, NULL, (char*)"ExpLOBInterfaceInsert",
+                          getLobErrStr(LOB_DATA_FILE_LOCK_ERROR),NULL);
+          return ex_expr::EXPR_ERROR;
+        }
+    }
+
   if(fromString())
     {
       //If source is a varchar, find the actual length
@@ -1455,214 +1491,362 @@
   if (getOperand(2)->getNullFlag() &&
       nullValue_)
     {
-     
-     
-      char * handle = op_data[0];
+      if(fromFile())
+        {
+          
+          retcode = ExpLOBInterfaceGetFileSize(getExeGlobals()->getExLobGlobal(), 
+                                               lobData,//filename 
+                                               getLobHdfsServer(),
+                                               getLobHdfsPort(),
+                                               sourceFileSize);
+          if (retcode < 0)
+            {
+              if (lobLocking())
+                retcode = SQL_EXEC_ReleaseLobLock(llid);
+              Lng32 intParam1 = -retcode;
+              ExRaiseSqlError(h, diagsArea, 
+                              (ExeErrorCode)(8442), NULL, &intParam1, 
+                              &cliError, NULL, (char*)"ExpLOBInterfaceGetFileSize",
+                              getLobErrStr(intParam1), (char*)getSqlJniErrorStr());
+              return ex_expr::EXPR_ERROR;
+            }
+          inputSize = sourceFileSize;
+        }
+      chunkMemSize = MINOF(getLobMaxChunkMemSize(), inputSize);
+      lobHandle = op_data[0];
       handleLen = getOperand(0)->getLength();
-       ex_expr::exp_return_type err = insertData(handleLen, handle, inputAddr, inputSize, lobHdfsOffset, h, diagsArea);;
+
+      if (handleLen == 0)
+        {
+          ExRaiseSqlError(h, diagsArea, 
+                          (ExeErrorCode)(8443), NULL, NULL);
+          
+          return ex_expr::EXPR_ERROR;
+        }
+
+      if (fromFile())
+        {
+          //read a chunk of the file input data
+          retcode= ExpLOBInterfaceReadSourceFile(getExeGlobals()->getExLobGlobal(),
+                                                 lobData, 
+                                                 getLobHdfsServer(),
+                                                 getLobHdfsPort(),
+                                                 sourceFileReadOffset,
+                                                 chunkMemSize,
+                                                 retBuf,
+                                                 retReadLen);
+          if (retcode < 0)
+            {
+              if (lobLocking())
+                retcode = SQL_EXEC_ReleaseLobLock(llid);
+              Lng32 intParam1 = -retcode;
+              ExRaiseSqlError(h, diagsArea, 
+                              (ExeErrorCode)(8442), NULL, &intParam1, 
+                              &cliError, NULL, (char*)"ExpLOBInterfaceReadSourceFile",
+                              getLobErrStr(intParam1), (char*)getSqlJniErrorStr());
+              return ex_expr::EXPR_ERROR;
+            }
+
+         
+          inputAddr = retBuf;
+                                               
+        }
+
+      ex_expr::exp_return_type err = insertData(handleLen, lobHandle, inputAddr, chunkMemSize, lobHdfsOffset, h, diagsArea);;
+
       if (err == ex_expr::EXPR_ERROR)
 	return err;
-      err = insertDesc(op_data, inputAddr,inputSize, lobHdfsOffset, result,h, diagsArea);
+      err = insertDesc(op_data, inputAddr,chunkMemSize, lobHdfsOffset, result,h, diagsArea);
       if (err == ex_expr::EXPR_ERROR)
 	return err;
       return err;
 
+      inputSize -= chunkMemSize;
+      if (fromFile())
+        sourceFileReadOffset +=chunkMemSize;       
+      inputAddr += chunkMemSize;
+
+      if(inputSize > 0)
+        {
+          char * lobHandle = op_data[0];
+          handleLen = getOperand(0)->getLength();
+          extractFromLOBhandle(&sFlags, &sLobType, &sLobNum, &sUid,
+                               &sDescSyskey, &sDescTS, 
+                               &sSchNameLen, sSchName,
+                               lobHandle); 
+     
+          tgtLobName = ExpGetLOBname(sUid, sLobNum, tgtLobNameBuf, LOB_NAME_LEN);
+        }
+      while(inputSize > 0) // chunk rest of the input into lobMaxChunkMemSize chunks and append.
+        {
+          chunkMemSize = MINOF(getLobMaxChunkMemSize(), inputSize);
+          if(!fromEmpty())
+            {
+     
+              if (fromFile())
+                {
+                  //read a chunk of the file input data
+                  retcode= ExpLOBInterfaceReadSourceFile(getExeGlobals()->getExLobGlobal(),
+                                                         lobData, 
+                                                         getLobHdfsServer(),
+                                                         getLobHdfsPort(),
+                                                         sourceFileReadOffset,
+                                                         chunkMemSize,
+                                                         retBuf,
+                                                         retReadLen);
+                  if (retcode < 0)
+                    {
+                      if (lobLocking())
+                        retcode = SQL_EXEC_ReleaseLobLock(llid);
+                      Lng32 intParam1 = -retcode;
+                      ExRaiseSqlError(h, diagsArea, 
+                                      (ExeErrorCode)(8442), NULL, &intParam1, 
+                                      &cliError, NULL, (char*)"ExpLOBInterfaceReadSourceFile",
+                                      getLobErrStr(intParam1), (char*)getSqlJniErrorStr());
+                      return ex_expr::EXPR_ERROR;
+                    }
+                  inputAddr = retBuf;
+                                               
+                }
+              char * handle = op_data[0];
+              handleLen = getOperand(0)->getLength();
+              LobsSubOper so = Lob_None;
+              if (fromFile())
+                so = Lob_Memory; // It's already been read into memory above.
+              else if (fromString()) {
+                if (getOperand(1)->getVCIndicatorLength() > 0)
+                  lobLen = getOperand(1)->getLength(op_data[1]-getOperand(1)->getVCIndicatorLength());
+                so = Lob_Memory;
+              }
+              else if (fromLob())
+                so = Lob_Lob;
+              else if (fromBuffer())
+                so= Lob_Buffer;
+              else if (fromExternal())
+              so = Lob_External_File;
+                
+              retcode = ExpLOBInterfaceUpdateAppend
+                (getExeGlobals()->getExLobGlobal(), 
+                 (getTcb()->getStatsEntry() != NULL ? getTcb()->getStatsEntry()->castToExHdfsScanStats() : NULL),
+                 getLobHdfsServer(),
+                 getLobHdfsPort(),
+                 tgtLobName, 
+                 lobStorageLocation(),
+                 handleLen, handle,
+                 &outHandleLen_, outLobHandle_,
+                 lobHdfsOffset,
+                 -1,	 
+                 0,
+                 0,
+                 so,
+                 sDescSyskey,
+                 chunkMemSize, 
+                 inputAddr,
+                 NULL, 0, NULL,
+                 -1, 0,
+                 getLobMaxSize(), getLobMaxChunkMemSize(),getLobGCLimit());
+              if (retcode < 0)
+                {
+                  if (lobLocking())
+                    retcode = SQL_EXEC_ReleaseLobLock(llid);
+                  Lng32 intParam1 = -retcode;
+                  ExRaiseSqlError(h, diagsArea, 
+                                  (ExeErrorCode)(8442), NULL, &intParam1, 
+                                  &cliError, NULL, (char*)"ExpLOBInterfaceReadSourceFile",
+                                  getLobErrStr(intParam1), (char*)getSqlJniErrorStr());
+                  return ex_expr::EXPR_ERROR;
+                }
+              inputSize -= chunkMemSize;
+              if (fromFile())
+                {
+                  sourceFileReadOffset +=chunkMemSize;
+                  getExeGlobals()->getExLobGlobal()->getHeap()->deallocateMemory(inputAddr);
+                }     
+              inputAddr += chunkMemSize;
+            }
+        }
+
+
     }
   else
     {
       lobHandle = op_data[2];
 
       handleLen = getOperand(2)->getLength(op_data[-MAX_OPERANDS+2]);
-    }
+    
      
-  extractFromLOBhandle(&sFlags, &sLobType, &sLobNum, &sUid,
-		       &sDescSyskey, &sDescTS, 
-		       &sSchNameLen, sSchName,
-		       lobHandle); //op_data[2]);
-  if (sDescSyskey == -1) //updating empty lob
-    {
-      
-      char llid[LOB_LOCK_ID_SIZE];
-      if (lobLocking())
+      extractFromLOBhandle(&sFlags, &sLobType, &sLobNum, &sUid,
+                           &sDescSyskey, &sDescTS, 
+                           &sSchNameLen, sSchName,
+                           lobHandle); //op_data[2]);
+      if (sDescSyskey == -1) //updating empty lob
         {
-          ExpLOBoper::genLobLockId(objectUID_,lobNum(),llid);;
-          NABoolean found = FALSE;
-          retcode = SQL_EXEC_CheckLobLock(llid, &found);
-          if (! retcode && !found) 
-            {    
-              retcode = SQL_EXEC_SetLobLock(llid);
-            }
-          else if (found)
+      
+          char llid[LOB_LOCK_ID_SIZE];
+          if (lobLocking())
             {
-              ExRaiseSqlError(h, diagsArea, 
-                              (ExeErrorCode)(EXE_LOB_CONCURRENT_ACCESS_ERROR));
-              return ex_expr::EXPR_ERROR;
+              ExpLOBoper::genLobLockId(objectUID_,lobNum(),llid);;
+              NABoolean found = FALSE;
+              retcode = SQL_EXEC_CheckLobLock(llid, &found);
+              if (! retcode && !found) 
+                {    
+                  retcode = SQL_EXEC_SetLobLock(llid);
+                }
+              else if (found)
+                {
+                  ExRaiseSqlError(h, diagsArea, 
+                                  (ExeErrorCode)(EXE_LOB_CONCURRENT_ACCESS_ERROR));
+                  return ex_expr::EXPR_ERROR;
+                }
             }
-        }
      
-      char * handle = op_data[0];
-      handleLen = getOperand(0)->getLength();
-      ex_expr::exp_return_type err = insertData(handleLen, handle, inputAddr, inputSize, lobHdfsOffset, h, diagsArea);
-      if (err == ex_expr::EXPR_ERROR)
-	return err;
-      err = insertDesc(op_data,inputAddr,inputSize, lobHdfsOffset, result, h, diagsArea);
-      if (err == ex_expr::EXPR_ERROR)
-	return err;
-      if (lobLocking())
-        retcode = SQL_EXEC_ReleaseLobLock(llid);
-      return err;
-    }
+          char * handle = op_data[0];
+          handleLen = getOperand(0)->getLength();
+          ex_expr::exp_return_type err = insertData(handleLen, handle, inputAddr, inputSize, lobHdfsOffset, h, diagsArea);
+          if (err == ex_expr::EXPR_ERROR)
+            return err;
+          err = insertDesc(op_data,inputAddr,inputSize, lobHdfsOffset, result, h, diagsArea);
+          if (err == ex_expr::EXPR_ERROR)
+            return err;
+          if (lobLocking())
+            retcode = SQL_EXEC_ReleaseLobLock(llid);
+          return err;
+        }
     
   
-  // get the lob name where data need to be updated
-  char tgtLobNameBuf[LOB_NAME_LEN];
-  char * tgtLobName = ExpGetLOBname(sUid, sLobNum, tgtLobNameBuf, LOB_NAME_LEN);
+      // get the lob name where data need to be updated
+      char tgtLobNameBuf[LOB_NAME_LEN];
+      char * tgtLobName = ExpGetLOBname(sUid, sLobNum, tgtLobNameBuf, LOB_NAME_LEN);
 
-  if (tgtLobName == NULL)
-    return ex_expr::EXPR_ERROR;
+      if (tgtLobName == NULL)
+        return ex_expr::EXPR_ERROR;
 
-  char fromLobNameBuf[LOB_NAME_LEN];
-  char * fromLobName = NULL;
-  Int64 fromDescKey = 0;
-  Int64 fromDescTS = 0;
-  short fromSchNameLen = 0;
-  char  fromSchName[ComAnsiNamePart::MAX_IDENTIFIER_EXT_LEN+1];
-  // call function with the lobname and source value
-  // to update it in the LOB.
-  // Get back offset and len of the LOB.
-  //  Int64 offset = 0;
-   lobLen = getOperand(1)->getLength();
+      char fromLobNameBuf[LOB_NAME_LEN];
+      char * fromLobName = NULL;
+      Int64 fromDescKey = 0;
+      Int64 fromDescTS = 0;
+      short fromSchNameLen = 0;
+      char  fromSchName[ComAnsiNamePart::MAX_IDENTIFIER_EXT_LEN+1];
+      // call function with the lobname and source value
+      // to update it in the LOB.
+      // Get back offset and len of the LOB.
+      //  Int64 offset = 0;
+      lobLen = getOperand(1)->getLength();
  
-  LobsSubOper so = Lob_None;
-  if (fromFile())
-    so = Lob_File;
-  else if (fromString()) {
-      if (getOperand(1)->getVCIndicatorLength() > 0)
+      LobsSubOper so = Lob_None;
+      if (fromFile())
+        so = Lob_File;
+      else if (fromString()) {
+        if (getOperand(1)->getVCIndicatorLength() > 0)
           lobLen = getOperand(1)->getLength(op_data[1]-getOperand(1)->getVCIndicatorLength());
-    so = Lob_Memory;
-  }
-  else if (fromLob())
-    so = Lob_Lob;
-  else if (fromBuffer())
-    so= Lob_Buffer;
-  else if (fromExternal())
-    so = Lob_External_File;
+        so = Lob_Memory;
+      }
+      else if (fromLob())
+        so = Lob_Lob;
+      else if (fromBuffer())
+        so= Lob_Buffer;
+      else if (fromExternal())
+        so = Lob_External_File;
  
    
  
-  Int64 lobMaxSize = 0;
-  if (getLobSize() > 0)
-    {
-      lobMaxSize = MINOF(getLobSize(), getLobMaxSize());
-    }
-  else
-    lobMaxSize = getLobMaxSize();
-  Lng32 waitedOp = 0;
-  waitedOp = 1;
+      Int64 lobMaxSize = 0;
+      if (getLobSize() > 0)
+        {
+          lobMaxSize = MINOF(getLobSize(), getLobMaxSize());
+        }
+      else
+        lobMaxSize = getLobMaxSize();
+      Lng32 waitedOp = 0;
+      waitedOp = 1;
 
-  Lng32 cliError = 0;
+      Lng32 cliError = 0;
 
-  char * data = op_data[1];
-  if (fromBuffer())
-    {
-      memcpy(&lobLen, op_data[3],sizeof(Int64)); // user specified buffer length
-      Int64 userBufAddr = 0;
-      memcpy(&userBufAddr,op_data[1],sizeof(Int64));
-      data = (char *)userBufAddr;
-    }
+      char * data = op_data[1];
+      if (fromFile())
+        data[lobLen] = '\0';
+      if (fromBuffer())
+        {
+          memcpy(&lobLen, op_data[3],sizeof(Int64)); // user specified buffer length
+          Int64 userBufAddr = 0;
+          memcpy(&userBufAddr,op_data[1],sizeof(Int64));
+          data = (char *)userBufAddr;
+        }
 
-  if(fromEmpty())
-    {
-      lobLen = 0;
-      so = Lob_Memory;
-    }
+      if(fromEmpty())
+        {
+          lobLen = 0;
+          so = Lob_Memory;
+        }
 
-    char llid[LOB_LOCK_ID_SIZE];
-    if (lobLocking())
-      {
-        ExpLOBoper::genLobLockId(objectUID_,lobNum(),llid);;
-        NABoolean found = FALSE;
-        retcode = SQL_EXEC_CheckLobLock(llid, &found);
-        if (! retcode && !found) 
-          {    
-            retcode = SQL_EXEC_SetLobLock(llid);
-          }
-        else if (found)
-          {
-            Int32 lobError = LOB_DATA_FILE_LOCK_ERROR;
-            ExRaiseSqlError(h, diagsArea, 
-                            (ExeErrorCode)(8558), NULL,(Int32 *)&lobError, 
-                            NULL, NULL, (char*)"ExpLOBInterfaceInsert",
-                            getLobErrStr(LOB_DATA_FILE_LOCK_ERROR),NULL);
-            return ex_expr::EXPR_ERROR;
-          }
-      }
-   if (isAppend() && !fromEmpty())
-    {
-      rc = ExpLOBInterfaceUpdateAppend
-	(getExeGlobals()->getExLobGlobal(), 
-         (getTcb()->getStatsEntry() != NULL ? getTcb()->getStatsEntry()->castToExHdfsScanStats() : NULL),
-	 getLobHdfsServer(),
-	 getLobHdfsPort(),
-	 tgtLobName, 
-	 lobStorageLocation(),
-	 handleLen, lobHandle,
-	 &outHandleLen_, outLobHandle_,
-	 requestTag_,
-	 -1,
+     
+          
+      if (isAppend() && !fromEmpty())
+        {
+          rc = ExpLOBInterfaceUpdateAppend
+            (getExeGlobals()->getExLobGlobal(), 
+             (getTcb()->getStatsEntry() != NULL ? getTcb()->getStatsEntry()->castToExHdfsScanStats() : NULL),
+             getLobHdfsServer(),
+             getLobHdfsPort(),
+             tgtLobName, 
+             lobStorageLocation(),
+             handleLen, lobHandle,
+             &outHandleLen_, outLobHandle_,
+             requestTag_,
+             -1,
 	 
-	 (lobOperStatus == CHECK_STATUS_ ? 1 : 0),
-	 waitedOp,
-	 so,
-	 sDescSyskey,
-	 lobLen, 
-	 data,
-	 fromLobName, fromSchNameLen, fromSchName,
-	 fromDescKey, fromDescTS,
-	 lobMaxSize, getLobMaxChunkMemSize(),getLobGCLimit());
-    }
-  else
-    {
-      rc = ExpLOBInterfaceUpdate
-	(getExeGlobals()->getExLobGlobal(), 
-         (getTcb()->getStatsEntry() != NULL ? getTcb()->getStatsEntry()->castToExHdfsScanStats() : NULL),
-	 getLobHdfsServer(),
-	 getLobHdfsPort(),
-	 tgtLobName, 
-	 lobStorageLocation(),
-	 handleLen, lobHandle,
-	 &outHandleLen_, outLobHandle_,
-	 requestTag_,
-	 -1,
+             (lobOperStatus == CHECK_STATUS_ ? 1 : 0),
+             waitedOp,
+             so,
+             sDescSyskey,
+             lobLen, 
+             data,
+             fromLobName, fromSchNameLen, fromSchName,
+             fromDescKey, fromDescTS,
+             lobMaxSize, getLobMaxChunkMemSize(),getLobGCLimit());
+        }
+      else
+        {
+          rc = ExpLOBInterfaceUpdate
+            (getExeGlobals()->getExLobGlobal(), 
+             (getTcb()->getStatsEntry() != NULL ? getTcb()->getStatsEntry()->castToExHdfsScanStats() : NULL),
+             getLobHdfsServer(),
+             getLobHdfsPort(),
+             tgtLobName, 
+             lobStorageLocation(),
+             handleLen, lobHandle,
+             &outHandleLen_, outLobHandle_,
+             requestTag_,
+             -1,
 	 
-	 (lobOperStatus == CHECK_STATUS_ ? 1 : 0),
-	 waitedOp,
-	 so,
-	 sDescSyskey,
-	 lobLen, 
-	 data,
-	 fromLobName, fromSchNameLen, fromSchName,
-	 fromDescKey, fromDescTS,
-	 lobMaxSize, getLobMaxChunkMemSize(),getLobGCLimit());
+             (lobOperStatus == CHECK_STATUS_ ? 1 : 0),
+             waitedOp,
+             so,
+             sDescSyskey,
+             lobLen, 
+             data,
+             fromLobName, fromSchNameLen, fromSchName,
+             fromDescKey, fromDescTS,
+             lobMaxSize, getLobMaxChunkMemSize(),getLobGCLimit());
+        }
+      if (lobLocking())
+        retcode = SQL_EXEC_ReleaseLobLock(llid);
+      if (rc  < 0)
+        {
+          Lng32 intParam1 = -rc;
+          ExRaiseSqlError(h, diagsArea, 
+                          (ExeErrorCode)(8442), NULL, &intParam1, 
+                          &cliError, NULL, (char*)"ExpLOBInterfaceUpdate",
+                          getLobErrStr(intParam1), (char*)getSqlJniErrorStr());
+          return ex_expr::EXPR_ERROR;
+        }
     }
-   if (lobLocking())
-     retcode = SQL_EXEC_ReleaseLobLock(llid);
-   if (rc  < 0)
-    {
-      Lng32 intParam1 = -rc;
-      ExRaiseSqlError(h, diagsArea, 
-		      (ExeErrorCode)(8442), NULL, &intParam1, 
-		      &cliError, NULL, (char*)"ExpLOBInterfaceUpdate",
-		      getLobErrStr(intParam1), (char*)getSqlJniErrorStr());
-      return ex_expr::EXPR_ERROR;
-     }
-
-   // update lob handle with the returned values
-   str_cpy_all(result, lobHandle, handleLen);
+  // update lob handle with the returned values
+  str_cpy_all(result, lobHandle, handleLen);
  
-   getOperand(0)->setVarLength(handleLen, op_data[-MAX_OPERANDS]);
+  getOperand(0)->setVarLength(handleLen, op_data[-MAX_OPERANDS]);
 
-   return ex_expr::EXPR_OK;
+  return ex_expr::EXPR_OK;
 }
 
 
diff --git a/core/sql/exp/ExpLOBaccess.cpp b/core/sql/exp/ExpLOBaccess.cpp
index 7dc143e..84c7499 100644
--- a/core/sql/exp/ExpLOBaccess.cpp
+++ b/core/sql/exp/ExpLOBaccess.cpp
@@ -327,7 +327,7 @@
     //Int64 writeOffset;
 
     if (! useLibHdfs_ ) {
-       offset = hdfsClient_->hdfsWriteImmediate(data, size, hdfsClientRetcode); 
+      offset = hdfsClient_->hdfsWriteImmediate(data, size, hdfsClientRetcode,0,TRUE); 
        if (hdfsClientRetcode != HDFS_CLIENT_OK)
           return LOB_DATA_WRITE_ERROR;
        operLen = size;
@@ -1478,12 +1478,55 @@
           if (clierr < 0 || clierr == 100)       
             return LOB_DESC_UPDATE_ERROR;
         }
+     
+      chunkMemSize = MINOF(lobMaxChunkMemSize, inputSize);
+      if (so == Lob_File)
+        {
+          //read a chunk of the file input data
+          err = readSourceFile(source_filename,retBuf,chunkMemSize, sourceFileReadOffset);
+             
+          if (err != LOB_OPER_OK)              
+            return err;
+                
+          inputAddr = retBuf;
+        }
+      str_sprintf(logBuf,"Calling writeLobData.sourceLen:%ld, dataOffset:%ld",sourceLen,dataOffset);
+      lobDebugInfo(logBuf,0,__LINE__,lobTrace_);
+      if(sourceLen !=0)  
+        {     
+          err = writeLobData(inputAddr, chunkMemSize,so,dataOffset,operLen,lobMaxChunkMemSize);
+          str_sprintf(logBuf,"writeLobData returned. operLen:%ld",operLen);
+          lobDebugInfo(logBuf,0,__LINE__,lobTrace_);
+          if (err != LOB_OPER_OK)
+            {
+              lobDebugInfo("writeLobData Failed",0,__LINE__,lobTrace_); 
+              return err;
+            }
+        }
+      lobDebugInfo("Calling CLI LOB_CLI_UPDATE_UNIQUE",0,__LINE__,lobTrace_);
+      clierr = SQL_EXEC_LOBcliInterface(handleIn, 
+                                        handleInLen, 
+                                        blackBox, &blackBoxLen,
+                                        handleOut, &handleOutLen,
+                                        LOB_CLI_UPDATE_UNIQUE, LOB_CLI_ExecImmed,
+                                        &dataOffset, &chunkMemSize,
+                                        &outDescPartnKey, &outDescSyskey, 
+                                        0,
+                                        xnId,lobTrace_);
+    
+      if (clierr < 0 || clierr == 100)       
+        return LOB_DESC_UPDATE_ERROR;
+      inputSize -= chunkMemSize;
+      if (so ==Lob_File)
+        sourceFileReadOffset +=chunkMemSize;       
+      inputAddr += chunkMemSize;
+      //Insert rest of the chunks   
       while (inputSize >0)
         {
           chunkMemSize = MINOF(lobMaxChunkMemSize, inputSize);
           if (so == Lob_File)
             {
-               //read a chunk of the file input data
+              //read a chunk of the file input data
               err = readSourceFile(source_filename,retBuf,chunkMemSize, sourceFileReadOffset);
              
               if (err != LOB_OPER_OK)              
@@ -1491,38 +1534,35 @@
                 
               inputAddr = retBuf;
             }
-          str_sprintf(logBuf,"Calling writeLobData.sourceLen:%ld, dataOffset:%ld",sourceLen,dataOffset);
-          lobDebugInfo(logBuf,0,__LINE__,lobTrace_);
-          if(sourceLen !=0)  
-            {     
-              err = writeLobData(inputAddr, chunkMemSize,so,dataOffset,operLen,lobMaxChunkMemSize);
-              str_sprintf(logBuf,"writeLobData returned. operLen:%ld",operLen);
-              lobDebugInfo(logBuf,0,__LINE__,lobTrace_);
-              if (err != LOB_OPER_OK)
-                {
-                  lobDebugInfo("writeLobData Failed",0,__LINE__,lobTrace_); 
-                  return err;
-                }
+          str_sprintf(logBuf,"Calling writeLobData: inputAddr: %ld, InputSize%ld, tgtOffset:%ld",(long)inputAddr,sourceLen,dataOffset);
+          err = writeLobData(inputAddr, chunkMemSize,so,dataOffset,operLen,lobMaxChunkMemSize);
+          if (err != LOB_OPER_OK)
+            {
+              lobDebugInfo("writeLobData returned error",0,__LINE__,lobTrace_);
+              return err;
             }
-          lobDebugInfo("Calling CLI LOB_CLI_UPDATE_UNIQUE",0,__LINE__,lobTrace_);
-          clierr = SQL_EXEC_LOBcliInterface(handleIn, 
-                                            handleInLen, 
+          lobDebugInfo("Calling cli LOB_CLI_INSERT_APPEND",0,__LINE__,lobTrace_);
+          clierr = SQL_EXEC_LOBcliInterface(handleIn, handleInLen, 
                                             blackBox, &blackBoxLen,
                                             handleOut, &handleOutLen,
-                                            LOB_CLI_UPDATE_UNIQUE, LOB_CLI_ExecImmed,
+                                            LOB_CLI_INSERT_APPEND, LOB_CLI_ExecImmed,
                                             &dataOffset, &chunkMemSize,
                                             &outDescPartnKey, &outDescSyskey, 
                                             0,
                                             xnId,lobTrace_);
     
-          if (clierr < 0 || clierr == 100)       
-            return LOB_DESC_UPDATE_ERROR;
+    
+          if (clierr < 0 || clierr == 100) 
+            { // some error or EOD.
+              str_sprintf(logBuf,"cli LOB_CLI_INSERT_APPEND returned :%d", clierr);
+              lobDebugInfo(logBuf, 0,__LINE__,lobTrace_);
+              return LOB_DESC_APPEND_ERROR;
+            }
           inputSize -= chunkMemSize;
           if (so ==Lob_File)
             sourceFileReadOffset +=chunkMemSize;       
           inputAddr += chunkMemSize;
         }
-        
       return LOB_OPER_OK;
     }
 
diff --git a/core/sql/generator/GenPreCode.cpp b/core/sql/generator/GenPreCode.cpp
index 0296d1d..682717d 100644
--- a/core/sql/generator/GenPreCode.cpp
+++ b/core/sql/generator/GenPreCode.cpp
@@ -4339,7 +4339,23 @@
     generator->oltOptInfo()->setOltEidLeanOpt(FALSE);
     oltOptInfo().setOltEidLeanOpt(FALSE);
   }
+ if (updateCurrentOf())
+    generator->setAqrEnabled(FALSE);
 
+
+ if ((((NATable*)getTableDesc()->getNATable())->getTableType() == ExtendedQualName::GHOST_TABLE) &&
+
+     (Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL)) &&
+
+     (CmpCommon::getDefault(AUTO_QUERY_RETRY) == DF_SYSTEM) &&
+
+     (NOT generator->aqrEnabled()))
+
+   {
+
+     generator->setAqrEnabled(TRUE);
+
+   }
   // Accumulate the values that are provided as inputs by my parent
   // together with the values that are produced as outputs by my
   // children. Use these values for rewriting the VEG expressions.
@@ -4543,6 +4559,20 @@
   if (updateCurrentOf())
     generator->setAqrEnabled(FALSE);
 
+
+  if ((((NATable*)getTableDesc()->getNATable())->getTableType() == ExtendedQualName::GHOST_TABLE) &&
+
+      (Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL)) &&
+
+      (CmpCommon::getDefault(AUTO_QUERY_RETRY) == DF_SYSTEM) &&
+
+      (NOT generator->aqrEnabled()))
+
+    {
+
+      generator->setAqrEnabled(TRUE);
+
+    }
   if (getTableDesc()->getNATable()->hasLobColumn())
     {
       oltOptInfo().setOltOpt(FALSE);
diff --git a/core/sql/optimizer/ItemCache.cpp b/core/sql/optimizer/ItemCache.cpp
index b74447b..d0a70a2 100644
--- a/core/sql/optimizer/ItemCache.cpp
+++ b/core/sql/optimizer/ItemCache.cpp
@@ -1506,7 +1506,7 @@
     child(i)->generateCacheKey(cwa);
   }
  
-  if (getRoutineDesc()->getLocale() != 0 )
+  if (getRoutineDesc() && getRoutineDesc()->getLocale() != 0 )
   {
     cwa += ", LOCALE: ";
     char dFmt[20]; 
@@ -1582,3 +1582,66 @@
   return this;
 }
 
+
+
+
+
+// --------------------------------------------------------------
+// member functions for LOBoper operator
+// --------------------------------------------------------------
+NABoolean LOBoper::isCacheableExpr(CacheWA& cwa)
+{
+  if (NOT ItemExpr::isCacheableExpr(cwa))
+    return FALSE;
+
+  return TRUE;
+}
+
+void LOBoper::generateCacheKey(CacheWA& cwa) const
+{
+  ItemExpr::generateCacheKey(cwa);
+
+  char oper[20];
+  cwa += " operType: ";
+  cwa += str_itoa((int)getOperatorType(), oper);
+  cwa += " ";
+}
+
+
+void LOBinsert::generateCacheKey(CacheWA & cwa) const
+{
+  LOBoper::generateCacheKey(cwa);
+   char oper[40];
+  cwa += " fromObj: ";
+  cwa += str_itoa((int)getObj(), oper);
+  cwa += " isAppend: ";
+  cwa += append_? "1":"0";
+  cwa += " ";
+  
+}
+
+void LOBconvert::generateCacheKey(CacheWA & cwa) const
+{
+  LOBoper::generateCacheKey(cwa);
+   char oper[40];
+  cwa += " fromObj: ";
+  cwa += str_itoa((int)getObj(), oper);
+  cwa += " tgtSize: ";
+  cwa += str_itoa((int)getTgtSize(), oper);
+  cwa += " ";
+  
+}
+
+
+void LOBupdate::generateCacheKey(CacheWA & cwa) const
+{
+  LOBoper::generateCacheKey(cwa);
+   char oper[40];
+  cwa += " fromObj: ";
+  cwa += str_itoa((int)getObj(), oper);
+  cwa += " isAppend: ";
+  cwa += append_? "1":"0"; 
+  cwa += " ";
+  
+}
+
diff --git a/core/sql/optimizer/ItemFunc.h b/core/sql/optimizer/ItemFunc.h
index dc36eb6..3a86657 100644
--- a/core/sql/optimizer/ItemFunc.h
+++ b/core/sql/optimizer/ItemFunc.h
@@ -3009,8 +3009,10 @@
   
   // method to do precode generation
   virtual ItemExpr * preCodeGen(Generator*);
+  virtual NABoolean isCacheableExpr(CacheWA& cwa);
+  virtual void generateCacheKey(CacheWA& cwa) const;
 
-  ObjectType getObj() { return obj_; }
+  ObjectType getObj() const { return obj_; }
 
   short &lobNum() {return lobNum_; }
   LobsStorage &lobStorageType() { return lobStorageType_; }
@@ -3072,7 +3074,7 @@
 
   // returns TRUE so this could be evaluated in master.
   virtual NABoolean isAUserSuppliedInput() const { return TRUE; };
-
+  virtual void generateCacheKey(CacheWA& cwa) const;
   Int64 & insertedTableObjectUID() { return objectUID_; }
   
   NAString &insertedTableSchemaName() { return schName_; }
@@ -3175,6 +3177,7 @@
   virtual short codeGen(Generator*);
   // method to do precode generation
   virtual ItemExpr * preCodeGen(Generator*);
+  virtual void generateCacheKey(CacheWA& cwa) const ;
   // virtual Int32 getArity() const;
   NABoolean isAppend() { return append_; };
 
@@ -3220,8 +3223,8 @@
 
   // method to do code generation
   virtual short codeGen(Generator*);
-
-  Lng32 getTgtSize() { return tgtSize_; }
+  virtual void generateCacheKey(CacheWA& cwa) const;
+  Lng32 getTgtSize() const{ return tgtSize_; }
  private:
   Lng32 tgtSize_;
  
diff --git a/core/sql/optimizer/ObjectNames.h b/core/sql/optimizer/ObjectNames.h
index afa95fc..8e8c2ac 100644
--- a/core/sql/optimizer/ObjectNames.h
+++ b/core/sql/optimizer/ObjectNames.h
@@ -569,12 +569,14 @@
   NABoolean isCacheable() const
   { 
     return    (type_ == NORMAL_TABLE)
-           || (type_ == TRIGTEMP_TABLE)
-           || (type_ == EXCEPTION_TABLE)
-           //|| (type_ == IUD_LOG_TABLE)
-           //|| (type_ == RANGE_LOG_TABLE)
-      || (type_ == INDEX_TABLE)  
-      || ((type_ == GHOST_TABLE)&& qualName_.isLOBDesc())
+      || (type_ == TRIGTEMP_TABLE)
+      || (type_ == EXCEPTION_TABLE)
+      //|| (type_ == IUD_LOG_TABLE)
+      //|| (type_ == RANGE_LOG_TABLE)
+      || (type_ == INDEX_TABLE)
+      || (type_ == SCHEMA_TABLE)
+      || (type_ == SG_TABLE)
+      || ((type_ == GHOST_TABLE))
   ; }
   
   NABoolean hasPartnClause() const {return NOT partnClause_.isEmpty();}
diff --git a/core/sql/optimizer/RelCache.cpp b/core/sql/optimizer/RelCache.cpp
index 4fb9a0d..722b7da 100644
--- a/core/sql/optimizer/RelCache.cpp
+++ b/core/sql/optimizer/RelCache.cpp
@@ -31,6 +31,9 @@
 *
 ******************************************************************************
 */
+#define   SQLPARSERGLOBALS_FLAGS   // must precede all #include's
+#define   SQLPARSERGLOBALS_NADEFAULTS
+
 #include "AllRelExpr.h"
 #include "CacheWA.h"
 #include "CmpMain.h"
@@ -38,7 +41,7 @@
 #include "OptHints.h"
 #include "QRDescGenerator.h"
 #include "HDFSHook.h"
-
+#include "SqlParserGlobals.h"      // must be last #include
 // append an ascii-version of GenericUpdate into cachewa.qryText_
 void GenericUpdate::generateCacheKey(CacheWA& cwa) const
   // NB: This comment applies to all generateCacheKey methods. 
@@ -1078,7 +1081,11 @@
     return FALSE;
   }
   if (cwa.getPhase() == CmpMain::PARSE) {
-    if (compExprTree_ || compExpr_.entries() > 0) {
+    // it is unclear why select...insert is not being cached.
+    // For now, cache it only for internal queries. This is needed to
+    // improve compile time of internal lob queries.
+
+    if (! Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL) &&(compExprTree_ || compExpr_.entries() > 0)) {
       // insert-returning is not cacheable after parse
       return FALSE; 
     }
@@ -1086,7 +1093,7 @@
   else if (cwa.getPhase() >= CmpMain::BIND) {
     // make sure select list is cacheable
     if (compExprTree_) {
-      if (!compExprTree_->isCacheableExpr(cwa)) { 
+      if  (!compExprTree_->isCacheableExpr(cwa)) { 
         return FALSE; 
       }
     }
diff --git a/core/sql/optimizer/SynthType.cpp b/core/sql/optimizer/SynthType.cpp
index 64d59ab..bb31367 100644
--- a/core/sql/optimizer/SynthType.cpp
+++ b/core/sql/optimizer/SynthType.cpp
@@ -7129,6 +7129,7 @@
 
       Lng32 tgtSize = MINOF((Lng32)op1.getLobLength(), tgtSize_);
 
+
       NAType *result = new HEAP SQLVarChar(HEAP, tgtSize,
 					   typ1.supportsSQLnull());
       return result;
@@ -7160,6 +7161,7 @@
   SQLlob& op1 = (SQLlob&)vid1.getType();
   
   Lng32 tgtSize = MINOF((Lng32)op1.getLobLength(), tgtSize_);
+
   
   NAType *result = new HEAP SQLVarChar(HEAP, tgtSize,
 				       typ1.supportsSQLnull());
diff --git a/core/sql/regress/executor/EXPECTED130 b/core/sql/regress/executor/EXPECTED130
index a862063..57ce079 100644
--- a/core/sql/regress/executor/EXPECTED130
+++ b/core/sql/regress/executor/EXPECTED130
@@ -63,9 +63,9 @@
 C1           C2
 -----------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
-          1  LOBH0000000200010302322462619480041819302322462659477729318212415509160868978020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
-          2  LOBH0000000200010302322462619480041819302322462659658191518212415509163952312020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
-          3  LOBH0000000200010302322462619480041819302322462659685731618212415509164230282020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
+          1  LOBH0000000200010398925915096716996919398925915109826613118212427913592310259020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
+          2  LOBH0000000200010398925915096716996919398925915109990051018212427913596302251020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
+          3  LOBH0000000200010398925915096716996919398925915110000589018212427913596408099020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
 
 --- 3 row(s) selected.
 >>
@@ -685,7 +685,7 @@
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract lobtofile(LOB '/g" | sed "s/$/' , 'tlob130_txt1.txt');/g" >> t130_extract_command;
 >>
 >>obey t130_extract_command;
->>extract lobtofile(LOB 'LOBH0000000200010302322462619485420419302322462711610371618212415509682402230020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'tlob130_txt1.txt');
+>>extract lobtofile(LOB 'LOBH0000000200010398925915096719754919398925915135562869418212427913851004829020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'tlob130_txt1.txt');
 Success. Targetfile :tlob130_txt1.txt  Length : 19
 
 --- SQL operation complete.
@@ -701,7 +701,7 @@
 >>sh rm t130_extract_command;
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract lobtofile(LOB '/g" | sed "s/$/' , 'tlob130_deep.jpg');/g" >> t130_extract_command;
 >>obey t130_extract_command;
->>extract lobtofile(LOB 'LOBH0000000200010302322462619485651919302322462715900372918212415509725274892020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'tlob130_deep.jpg');
+>>extract lobtofile(LOB 'LOBH0000000200010398925915096719915619398925915136686288718212427913862324790020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'tlob130_deep.jpg');
 Success. Targetfile :tlob130_deep.jpg  Length : 159018
 
 --- SQL operation complete.
@@ -717,7 +717,7 @@
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract lobtofile(LOB '/g" | sed "s/$/' , 'tlob130_anoush.jpg');/g" >> t130_extract_command;
 >>
 >>obey t130_extract_command;
->>extract lobtofile(LOB 'LOBH0000000200010302322462619485651919302322462715900372918212415509725274892020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'tlob130_anoush.jpg');
+>>extract lobtofile(LOB 'LOBH0000000200010398925915096719915619398925915136686288718212427913862324790020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'tlob130_anoush.jpg');
 Success. Targetfile :tlob130_anoush.jpg  Length : 230150
 
 --- SQL operation complete.
@@ -799,6 +799,38 @@
                                                                                         
 
 --- 3 row(s) selected.
+>>update tlob130txt2 set c2=NULL where c1=3;
+
+--- 1 row(s) updated.
+>>select * from tlob130txt2;
+
+C1           C2
+-----------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+
+          1  LOBH0000000200010398925915096720669219398925915143384552018212427913929275257020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
+          2  LOBH0000000200010398925915096720669219398925915143509995318212427913931502076020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
+          3  ?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
+
+--- 3 row(s) selected.
+>>update tlob130txt2 set c2=filetolob('hdfs:///user/trafodion/lobs/lob_input_d1.txt', append) where c1 = 3;
+
+--- 1 row(s) updated.
+>>select lobtostring(c2, 200 ) from tlob130txt2;
+
+(EXPR)
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+
+Hey diddle diddle,
+                                                                                                                                                                                     
+The cat and the fiddle,
+                                                                                                                                                                                
+The little dog laughed,
+To see such sport,
+
+And the dish ran away with the spoon.
+                                                                                                                      
+
+--- 3 row(s) selected.
 >>
 >>-- should see wrong text in the last few lines
 >>update tlob130txt2 set c2=filetolob('hdfs:///user/trafodion/lobs/lob_input_e1.txt') where c1 =3 ;
@@ -838,7 +870,7 @@
 >>
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract lobtofile(LOB '/g" | sed "s/$/' , 'hdfs:\/\/\/user\/trafodion\/lobs\/tlob130_txt2.txt');/g" >> t130_extract_command;
 >>obey t130_extract_command;
->>extract lobtofile(LOB 'LOBH0000000200010302322462619486948319302322462724980286218212415509815960051020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'hdfs:///user/trafodion/lobs/tlob130_txt2.txt');
+>>extract lobtofile(LOB 'LOBH0000000200010398925915096720669219398925915143384552018212427913929275257020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'hdfs:///user/trafodion/lobs/tlob130_txt2.txt');
 Success. Targetfile :hdfs:///user/trafodion/lobs/tlob130_txt2.txt  Length : 19
 
 --- SQL operation complete.
@@ -854,7 +886,7 @@
 >>sh rm t130_extract_command;
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract lobtofile(LOB '/g" | sed "s/$/' , 'hdfs:\/\/\/user\/trafodion\/lobs\/tlob130_deep.jpg');/g" >> t130_extract_command;
 >>obey t130_extract_command;
->>extract lobtofile(LOB 'LOBH0000000200010302322462619487179919302322462728926701518212415509855552322020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'hdfs:///user/trafodion/lobs/tlob130_deep.jpg');
+>>extract lobtofile(LOB 'LOBH0000000200010398925915096720819419398925915144231512618212427913937737310020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'hdfs:///user/trafodion/lobs/tlob130_deep.jpg');
 Success. Targetfile :hdfs:///user/trafodion/lobs/tlob130_deep.jpg  Length : 159018
 
 --- SQL operation complete.
@@ -870,7 +902,7 @@
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract lobtofile(LOB '/g" | sed "s/$/' , 'hdfs:\/\/\/user\/trafodion\/lobs\/tlob130_anoush.jpg');/g" >> t130_extract_command;
 >>
 >>obey t130_extract_command;
->>extract lobtofile(LOB 'LOBH0000000200010302322462619485651919302322462715900372918212415509725274892020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'hdfs:///user/trafodion/lobs/tlob130_anoush.jpg');
+>>extract lobtofile(LOB 'LOBH0000000200010398925915096719915619398925915136686288718212427913862324790020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'hdfs:///user/trafodion/lobs/tlob130_anoush.jpg');
 Success. Targetfile :hdfs:///user/trafodion/lobs/tlob130_anoush.jpg  Length : 230150
 
 --- SQL operation complete.
@@ -889,7 +921,7 @@
 Input a filename to extract to : 
 Output File Name : lobc2out.jpg
 Extracting  lob handle for column c2...
-LOB handle for c2: LOBH0000000200010302322462619485651919302322462715900372918212415509725274892020"TRAFODION"."LOB130"
+LOB handle for c2: LOBH0000000200010398925915096719915619398925915136686288718212427913862324790020"TRAFODION"."LOB130"
 Extracting LOB data length for the above handle...
 LOB data length :230150
 Extracting lob data into file in chunks ...
@@ -963,7 +995,7 @@
 >>sh rm t130_extract_command;
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract lobtofile(LOB '/g" | sed "s/$/' , 'tlob130_deep2.jpg');/g" >> t130_extract_command;
 >>obey t130_extract_command;
->>extract lobtofile(LOB 'LOBH0000000200020302322462619489007919302322462742420561018212415509990504853020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'tlob130_deep2.jpg');
+>>extract lobtofile(LOB 'LOBH0000000200020398925915096721905119398925915153005398018212427914025446780020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'tlob130_deep2.jpg');
 Success. Targetfile :tlob130_deep2.jpg  Length : 159018
 
 --- SQL operation complete.
@@ -972,7 +1004,7 @@
 >>sh rm t130_extract_command;
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract lobtofile(LOB '/g" | sed "s/$/' , 'hdfs:\/\/\/user\/trafodion\/lobs\/tlob130_anoush2.jpg');/g" >> t130_extract_command;
 >>obey t130_extract_command;
->>extract lobtofile(LOB 'LOBH0000000200030302322462619489007919302322462742988318718212415509996094238020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'hdfs:///user/trafodion/lobs/tlob130_anoush2.jpg');
+>>extract lobtofile(LOB 'LOBH0000000200030398925915096721905119398925915153223853618212427914027706616020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'hdfs:///user/trafodion/lobs/tlob130_anoush2.jpg');
 Success. Targetfile :hdfs:///user/trafodion/lobs/tlob130_anoush2.jpg  Length : 230150
 
 --- SQL operation complete.
@@ -999,7 +1031,7 @@
 >>sh rm t130_extract_command;
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract lobtofile(LOB '/g" | sed "s/$/' , 'tlob130_anoush3.jpg',create,truncate);/g" >> t130_extract_command;
 >>obey t130_extract_command;
->>extract lobtofile(LOB 'LOBH0000000200030302322462619489549719302322462748547754518212415510051648710020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'tlob130_anoush3.jpg',create,truncate);
+>>extract lobtofile(LOB 'LOBH0000000200030398925915096722237619398925915156466167118212427914060068242020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ' , 'tlob130_anoush3.jpg',create,truncate);
 Success. Targetfile :tlob130_anoush3.jpg  Length : 230150
 
 --- SQL operation complete.
@@ -1114,12 +1146,12 @@
 
   ColumnName :  C2
   Lob Location :  /user/trafodion/lobs
-  LOB Data File:  LOBP_03023224626194919246_0001
+  LOB Data File:  LOBP_03989259150967233649_0001
   LOB EOD :  0
   LOB Used Len :  0
   ColumnName :  C3
   Lob Location :  /user/trafodion/lobs
-  LOB Data File:  LOBP_03023224626194919246_0002
+  LOB Data File:  LOBP_03989259150967233649_0002
   LOB EOD :  0
   LOB Used Len :  0
   ColumnName :  C4
@@ -1134,8 +1166,8 @@
 CATALOG_NAME                                                                                                                                                                                                                                                      SCHEMA_NAME                                                                                                                                                                                                                                                       OBJECT_NAME                                                                                                                                                                                                                                                       COLUMN_NAME                                                                                                                                                                                                                                                       LOB_LOCATION                                                                                                                                                                                                                                                      LOB_DATA_FILE                                                                                                                                                                                                                                                     LOB_DATA_FILE_SIZE_EOD  LOB_DATA_FILE_SIZE_USED
 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------  -----------------------
 
-TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT2                                                                                                                                                                                                                                                        C2                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03023224626194919246_0001                                                                                                                                                                                                                                                         0                        0
-TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT2                                                                                                                                                                                                                                                        C3                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03023224626194919246_0002                                                                                                                                                                                                                                                         0                        0
+TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT2                                                                                                                                                                                                                                                        C2                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03989259150967233649_0001                                                                                                                                                                                                                                                         0                        0
+TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT2                                                                                                                                                                                                                                                        C3                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03989259150967233649_0002                                                                                                                                                                                                                                                         0                        0
 TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT2                                                                                                                                                                                                                                                        C4                                                                                                                                                                                                                                                                External HDFS Location                                                                                                                                                                                                                                            External HDFS File                                                                                                                                                                                                                                                                     0                        0
 
 --- 3 row(s) selected.
@@ -1156,17 +1188,17 @@
 
   ColumnName :  C2
   Lob Location :  /user/trafodion/lobs
-  LOB Data File:  LOBP_03023224626194914515_0001
+  LOB Data File:  LOBP_03989259150967230440_0001
   LOB EOD :  15
   LOB Used Len :  15
   ColumnName :  C3
   Lob Location :  /user/trafodion/lobs
-  LOB Data File:  LOBP_03023224626194914515_0002
+  LOB Data File:  LOBP_03989259150967230440_0002
   LOB EOD :  15
   LOB Used Len :  15
   ColumnName :  C4
   Lob Location :  /user/trafodion/lobs
-  LOB Data File:  LOBP_03023224626194914515_0003
+  LOB Data File:  LOBP_03989259150967230440_0003
   LOB EOD :  45
   LOB Used Len :  45
 
@@ -1176,9 +1208,9 @@
 CATALOG_NAME                                                                                                                                                                                                                                                      SCHEMA_NAME                                                                                                                                                                                                                                                       OBJECT_NAME                                                                                                                                                                                                                                                       COLUMN_NAME                                                                                                                                                                                                                                                       LOB_LOCATION                                                                                                                                                                                                                                                      LOB_DATA_FILE                                                                                                                                                                                                                                                     LOB_DATA_FILE_SIZE_EOD  LOB_DATA_FILE_SIZE_USED
 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------  -----------------------
 
-TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C2                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03023224626194914515_0001                                                                                                                                                                                                                                                        15                       15
-TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C3                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03023224626194914515_0002                                                                                                                                                                                                                                                        15                       15
-TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C4                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03023224626194914515_0003                                                                                                                                                                                                                                                        45                       45
+TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C2                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03989259150967230440_0001                                                                                                                                                                                                                                                        15                       15
+TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C3                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03989259150967230440_0002                                                                                                                                                                                                                                                        15                       15
+TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C4                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03989259150967230440_0003                                                                                                                                                                                                                                                        45                       45
 
 --- 3 row(s) selected.
 >>delete from tlob130gt where c1=2;
@@ -1195,17 +1227,17 @@
 
   ColumnName :  C2
   Lob Location :  /user/trafodion/lobs
-  LOB Data File:  LOBP_03023224626194914515_0001
+  LOB Data File:  LOBP_03989259150967230440_0001
   LOB EOD :  30
   LOB Used Len :  25
   ColumnName :  C3
   Lob Location :  /user/trafodion/lobs
-  LOB Data File:  LOBP_03023224626194914515_0002
+  LOB Data File:  LOBP_03989259150967230440_0002
   LOB EOD :  31
   LOB Used Len :  26
   ColumnName :  C4
   Lob Location :  /user/trafodion/lobs
-  LOB Data File:  LOBP_03023224626194914515_0003
+  LOB Data File:  LOBP_03989259150967230440_0003
   LOB EOD :  71
   LOB Used Len :  56
 
@@ -1215,9 +1247,9 @@
 CATALOG_NAME                                                                                                                                                                                                                                                      SCHEMA_NAME                                                                                                                                                                                                                                                       OBJECT_NAME                                                                                                                                                                                                                                                       COLUMN_NAME                                                                                                                                                                                                                                                       LOB_LOCATION                                                                                                                                                                                                                                                      LOB_DATA_FILE                                                                                                                                                                                                                                                     LOB_DATA_FILE_SIZE_EOD  LOB_DATA_FILE_SIZE_USED
 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------  -----------------------
 
-TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C2                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03023224626194914515_0001                                                                                                                                                                                                                                                        30                       25
-TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C3                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03023224626194914515_0002                                                                                                                                                                                                                                                        31                       26
-TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C4                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03023224626194914515_0003                                                                                                                                                                                                                                                        71                       56
+TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C2                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03989259150967230440_0001                                                                                                                                                                                                                                                        30                       25
+TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C3                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03989259150967230440_0002                                                                                                                                                                                                                                                        31                       26
+TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130GT                                                                                                                                                                                                                                                         C4                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03989259150967230440_0003                                                                                                                                                                                                                                                        71                       56
 
 --- 3 row(s) selected.
 >>
@@ -1248,14 +1280,14 @@
 >>sh rm t130_extract_command;
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract name(LOB '/g" | sed "s/$/');/g" >> t130_extract_command;
 >>obey t130_extract_command;
->>extract name(LOB 'LOBH0000000200020302322462619492996719302322462796022099318212415510527586648020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ');
- LOB filename : /user/trafodion/lobs/LOBP_03023224626194929967_0002
+>>extract name(LOB 'LOBH0000000200020398925915096723884819398925915180791519818212427914304316031020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ');
+ LOB filename : /user/trafodion/lobs/LOBP_03989259150967238848_0002
 
 --- SQL operation complete.
 >>sh rm t130_extract_command;
 >>sh grep "^LOBH" TMP130 | sed "s/^/extract offset(LOB '/g" | sed "s/$/');/g" >> t130_extract_command;
 >>obey t130_extract_command;
->>extract offset(LOB 'LOBH0000000200020302322462619492996719302322462796022099318212415510527586648020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ');
+>>extract offset(LOB 'LOBH0000000200020398925915096723884819398925915180791519818212427914304316031020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ');
  LOB Offset : 43
 
 --- SQL operation complete.
@@ -1408,12 +1440,12 @@
 
   ColumnName :  C2
   Lob Location :  /user/trafodion/lobs
-  LOB Data File:  LOBP_03023224626194929967_0001
+  LOB Data File:  LOBP_03989259150967238848_0001
   LOB EOD :  70
   LOB Used Len :  42
   ColumnName :  C3
   Lob Location :  /user/trafodion/lobs
-  LOB Data File:  LOBP_03023224626194929967_0002
+  LOB Data File:  LOBP_03989259150967238848_0002
   LOB EOD :  125
   LOB Used Len :  68
   ColumnName :  C4
@@ -1428,8 +1460,8 @@
 CATALOG_NAME                                                                                                                                                                                                                                                      SCHEMA_NAME                                                                                                                                                                                                                                                       OBJECT_NAME                                                                                                                                                                                                                                                       COLUMN_NAME                                                                                                                                                                                                                                                       LOB_LOCATION                                                                                                                                                                                                                                                      LOB_DATA_FILE                                                                                                                                                                                                                                                     LOB_DATA_FILE_SIZE_EOD  LOB_DATA_FILE_SIZE_USED
 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  ----------------------  -----------------------
 
-TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130EXT                                                                                                                                                                                                                                                        C2                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03023224626194929967_0001                                                                                                                                                                                                                                                        70                       42
-TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130EXT                                                                                                                                                                                                                                                        C3                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03023224626194929967_0002                                                                                                                                                                                                                                                       125                       68
+TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130EXT                                                                                                                                                                                                                                                        C2                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03989259150967238848_0001                                                                                                                                                                                                                                                        70                       42
+TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130EXT                                                                                                                                                                                                                                                        C3                                                                                                                                                                                                                                                                /user/trafodion/lobs                                                                                                                                                                                                                                              LOBP_03989259150967238848_0002                                                                                                                                                                                                                                                       125                       68
 TRAFODION                                                                                                                                                                                                                                                         LOB130                                                                                                                                                                                                                                                            TLOB130EXT                                                                                                                                                                                                                                                        C4                                                                                                                                                                                                                                                                External HDFS Location                                                                                                                                                                                                                                            External HDFS File                                                                                                                                                                                                                                                                     0                        0
 
 --- 3 row(s) selected.
@@ -1446,7 +1478,7 @@
 Input a filename to extract to : 
 Output File Name : lobc4ext.txt
 Extracting  lob handle for column c4...
-LOB handle for c4: LOBH0000000800030302322462619492996719302322462798323448718212415510550602499020"TRAFODION"."LOB130"
+LOB handle for c4: LOBH0000000800030398925915096723884819398925915180923835018212427914305639837020"TRAFODION"."LOB130"
 Extracting LOB data length for the above handle...
 LOB data length :19
 Extracting lob data into file in chunks ...
@@ -1512,7 +1544,7 @@
 Input lob column name to get handle from :
 Column Name : c2
 Extracting  lob handle for column c2...
-LOB handle for c2: LOBH0000000200010302322462619496180919302322462812783026418212415510694095790020"TRAFODION"."LOB130"
+LOB handle for c2: LOBH0000000200010398925915096725384219398925915186962270218212427914365164689020"TRAFODION"."LOB130"
 >>select lobtostring(c2,20) from t130lob5;
 
 (EXPR)              
@@ -1529,7 +1561,7 @@
 Input lob column name to get handle from :
 Column Name : c2
 Extracting  lob handle for column c2...
-LOB handle for c2: LOBH0000000200010302322462619496180919302322462812783026418212415510694095790020"TRAFODION"."LOB130"
+LOB handle for c2: LOBH0000000200010398925915096725384219398925915186962270218212427914365164689020"TRAFODION"."LOB130"
 >>select lobtostring(c2,40) from t130lob5;
 
 (EXPR)                                  
@@ -1546,7 +1578,7 @@
 Input lob column name to get handle from :
 Column Name : c2
 Extracting  lob handle for column c2...
-LOB handle for c2: LOBH0000000200010302322462619496180919302322462812783026418212415510694095790020"TRAFODION"."LOB130"
+LOB handle for c2: LOBH0000000200010398925915096725384219398925915186962270218212427914365164689020"TRAFODION"."LOB130"
 >>select lobtostring(c2,20) from t130lob5;
 
 (EXPR)              
@@ -1563,7 +1595,7 @@
 Input lob column name to get handle from :
 Column Name : c2
 Extracting  lob handle for column c2...
-LOB handle for c2: LOBH0000000200010302322462619496180919302322462812783026418212415510694095790020"TRAFODION"."LOB130"
+LOB handle for c2: LOBH0000000200010398925915096725384219398925915186962270218212427914365164689020"TRAFODION"."LOB130"
 >>select lobtostring(c2,40) from t130lob5;
 
 (EXPR)                                  
@@ -1590,7 +1622,7 @@
 C2
 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
-LOBH0000000200010302322462619492996719302322462823895005518212415510806316467020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
+LOBH0000000200010398925915096723884819398925915195303539218212427914449438981020"TRAFODION"."LOB130"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
 
 --- 1 row(s) selected.
 >>-- following should return error since only external lobs will be allowed
@@ -1633,9 +1665,9 @@
 Tables in Schema TRAFODION.LOBSCH
 =================================
 
-LOBDescChunks__03023224626194981236_0001
-LOBDescHandle__03023224626194981236_0001
-LOBMD__03023224626194981236
+LOBDescChunks__03989259150967266691_0001
+LOBDescHandle__03989259150967266691_0001
+LOBMD__03989259150967266691
 SB_HISTOGRAMS
 SB_HISTOGRAM_INTERVALS
 SB_PERSISTENT_SAMPLES
diff --git a/core/sql/regress/executor/TEST130 b/core/sql/regress/executor/TEST130
index 4e82e00..20782c3 100755
--- a/core/sql/regress/executor/TEST130
+++ b/core/sql/regress/executor/TEST130
@@ -379,6 +379,10 @@
 --should update with full poem
 update tlob130txt2 set c2=filetolob('hdfs:///user/trafodion/lobs/lob_input_d1.txt', append) where c1 = 3;
 select lobtostring(c2, 200 ) from tlob130txt2;
+update tlob130txt2 set c2=NULL where c1=3;
+select * from tlob130txt2;
+update tlob130txt2 set c2=filetolob('hdfs:///user/trafodion/lobs/lob_input_d1.txt', append) where c1 = 3;
+select lobtostring(c2, 200 ) from tlob130txt2;
 
 -- should see wrong text in the last few lines
 update tlob130txt2 set c2=filetolob('hdfs:///user/trafodion/lobs/lob_input_e1.txt') where c1 =3 ;
diff --git a/core/sql/regress/seabase/EXPECTED025 b/core/sql/regress/seabase/EXPECTED025
index 677cafa..24c87f0 100644
--- a/core/sql/regress/seabase/EXPECTED025
+++ b/core/sql/regress/seabase/EXPECTED025
@@ -1526,7 +1526,7 @@
 --- SQL operation complete.
 >>
 >>--control query default def_num_smp_cpus '4';
->>control query shape esp_exchange(cut,4);
+>>control query shape esp_exchange(cut,2);
 
 --- SQL operation complete.
 >>
@@ -1595,7 +1595,7 @@
 --- SQL operation complete.
 >>
 >>--control query default def_num_smp_cpus '4';
->>control query shape esp_exchange (cut, 4);
+>>control query shape esp_exchange (cut, 2);
 
 --- SQL operation complete.
 >>
@@ -1618,8 +1618,8 @@
 ---- ---- ---- --------------------  --------  --------------------  ---------
 
 8    .    9    root                                                  1.00E+004
-7    .    8    esp_exchange                    1:4(hash2)            1.00E+004
-6    .    7    esp_exchange                    4(hash2):1            1.00E+004
+7    .    8    esp_exchange                    1:2(hash2)            1.00E+004
+6    .    7    esp_exchange                    2(hash2):1            1.00E+004
 4    5    6    tuple_flow                                            1.00E+004
 .    .    5    trafodion_load        h         T025T007              1.00E+000
 3    .    4    transpose                                             1.00E+004
@@ -1806,7 +1806,7 @@
 A                     B           C         
 --------------------  ----------  ----------
 
-                  10           1           1
+                  11           1           1
 
 --- 1 row(s) selected.
 >>
@@ -1822,7 +1822,7 @@
 A                     B           C         
 --------------------  ----------  ----------
 
-                  10           1           1
+                  11           1           1
 
 --- 1 row(s) selected.
 >>
@@ -1896,7 +1896,7 @@
 >>insert into T025T003 values  	
 +>(DEFAULT,-3,0,1,'dasf',2.1,'dfaf','dfa','dfa','d');
 
-*** ERROR[8101] The operation is prevented by check constraint TRAFODION.S025.T025T003_918519353_6685 on table TRAFODION.S025.T025T003.
+*** ERROR[8101] The operation is prevented by check constraint TRAFODION.S025.T025T003_573479767_9484 on table TRAFODION.S025.T025T003.
 
 --- 0 row(s) inserted.
 >>
@@ -1906,7 +1906,7 @@
 +>(DEFAULT,-2,1,1,'dasf',2.1,'dfaf','dfa','dfa','d'),	
 +>(DEFAULT,-1,2,1,'dasf',2.1,'dfaf','dfa','dfa','d');
 
-*** ERROR[8101] The operation is prevented by check constraint TRAFODION.S025.T025T003_918519353_6685 on table TRAFODION.S025.T025T003.
+*** ERROR[8101] The operation is prevented by check constraint TRAFODION.S025.T025T003_573479767_9484 on table TRAFODION.S025.T025T003.
 
 --- 0 row(s) inserted.
 >>
@@ -2628,7 +2628,7 @@
 >>invoke T025T00V2;
 
 -- Definition of Trafodion volatile table T025T00V2
--- Definition current  Sun Jan  7 23:18:49 2018
+-- Definition current  Tue Jul  2 18:37:56 2019
 
   (
     SURROGATE_KEY                    SMALLINT UNSIGNED GENERATED BY DEFAULT AS
@@ -2816,7 +2816,7 @@
 >>invoke t025t010;
 
 -- Definition of Trafodion table TRAFODION.S025.T025T010
--- Definition current  Sun Jan  7 23:20:51 2018
+-- Definition current  Tue Jul  2 18:40:58 2019
 
   (
     A                                LARGEINT GENERATED ALWAYS AS IDENTITY NOT
diff --git a/core/sql/regress/seabase/TEST025 b/core/sql/regress/seabase/TEST025
index 55c0d0f..ca9bcd1 100644
--- a/core/sql/regress/seabase/TEST025
+++ b/core/sql/regress/seabase/TEST025
@@ -1040,7 +1040,7 @@
 salt using 4 partitions;
 
 --control query default def_num_smp_cpus '4';
-control query shape esp_exchange(cut,4);
+control query shape esp_exchange(cut,2);
 
 prepare ins06 from
 upsert using load into T025T006(a, c)
@@ -1083,7 +1083,7 @@
 ;
 
 --control query default def_num_smp_cpus '4';
-control query shape esp_exchange (cut, 4);
+control query shape esp_exchange (cut, 2);
 
 prepare ins07 from
 upsert using load into t025t007 (b,c)
diff --git a/core/sql/src/main/java/org/trafodion/sql/HDFSClient.java b/core/sql/src/main/java/org/trafodion/sql/HDFSClient.java
index 4f06e7b..28d9187 100644
--- a/core/sql/src/main/java/org/trafodion/sql/HDFSClient.java
+++ b/core/sql/src/main/java/org/trafodion/sql/HDFSClient.java
@@ -493,24 +493,52 @@
           return -1;
     }
 
-    long hdfsWriteImmediate(byte[] buff) throws IOException
+    long hdfsWriteImmediate(byte[] buff, boolean doRetry) throws IOException
     {
+
       if (logger_.isDebugEnabled()) 
-         logger_.debug("HDFSClient.hdfsWriteClose() - started" );
-      FSDataOutputStream fsOut;
-      FileStatus filestatus;
-      long writeOffset; 
-      if (fs_.exists(filepath_)) {
-         filestatus = fs_.getFileStatus(filepath_);
-         fsOut = fs_.append(filepath_);
-         writeOffset = filestatus.getLen(); 
-      }
-      else {
-         fsOut = fs_.create(filepath_);
-         writeOffset = 0;
-      }
-      fsOut.write(buff);
-      fsOut.close();
+         logger_.debug("HDFSClient.hdfsWriteImmediate() - started" );
+   
+        FSDataOutputStream fsOut=null;
+        FileStatus filestatus = null;
+        long writeOffset=0;
+        long writeEndOffset =0;
+        int trycount = 0;
+        
+        while (trycount < 3)
+        {
+          try
+          { 
+            if (logger_.isDebugEnabled()) 
+                logger_.debug("HDFSClient.hdfsWriteImmediate() - started" );
+  
+            if (fs_.exists(filepath_)) {
+                filestatus = fs_.getFileStatus(filepath_);
+                fsOut = fs_.append(filepath_);
+                 writeOffset = filestatus.getLen(); 
+             }
+             else {
+                   fsOut = fs_.create(filepath_);
+                   writeOffset = 0;
+             }
+             fsOut.write(buff);
+             fsOut.hflush();
+             writeEndOffset = fsOut.getPos(); 
+             } finally {
+                if (fsOut != null )
+                   fsOut.close();
+           }
+           // Do a checksum to ensure writeOffset is really the right value.
+          if (doRetry && (buff.length != writeEndOffset-writeOffset))
+            {
+               trycount++;
+               if (trycount == 3)
+                 {
+                    throw new IOException("HDFSwrite did not succeed due to multiple concurrent writes ");    
+                 }
+             }
+           else trycount = 100; //if doRetry is false or writeOffset is ok
+        } //while
       return writeOffset;
     }
     
diff --git a/core/sql/udrserv/UdrResultSet.cpp b/core/sql/udrserv/UdrResultSet.cpp
index 78beebc..0f57901 100644
--- a/core/sql/udrserv/UdrResultSet.cpp
+++ b/core/sql/udrserv/UdrResultSet.cpp
@@ -299,7 +299,7 @@
   singleRowFetchEnabled_ = FALSE;
 
   if (prevContext != 0)
-    SQL_EXEC_SwitchContext(prevContext, NULL);
+    SQL_EXEC_SwitchContext_Internal(prevContext, NULL,TRUE);
 
   // Let's clear cli diags if there are any
   SQL_EXEC_ClearDiagnostics(NULL);
@@ -543,7 +543,7 @@
 
   SQLCTX_HANDLE tmpCtx;
   Int32 result = SQL_EXEC_SwitchContext((Lng32) getContextHandle(),
-                                      &tmpCtx);
+                                        &tmpCtx);
 
   if (result < 0)
   {
@@ -565,7 +565,7 @@
     return 0;
 
   SQLCTX_HANDLE tmpCtxHandle;
-  Int32 result = SQL_EXEC_SwitchContext(ctxHandle, &tmpCtxHandle);
+  Int32 result = SQL_EXEC_SwitchContext_Internal(ctxHandle, &tmpCtxHandle,TRUE);
 
   if (result < 0)
   {