JIRA TRAFODION-2072 plus few more

-- long varchars (> 32K) are handled for traf (regular and aligned)
   and hive tables
-- showddl for hive tables shows traf and hive definitions
-- traf external tables can optionally take columns attrs that would
   overwrite corresponding hive col attrs
-- metadata is updated with info no whether col attrs were specified
-- 'repeat' function can optionally take a max length parameter
-- error is returned if an external table is created for non-hive
-- tests added to regress/seabase/TEST031 and hive/TEST009


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/b39f7524
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/b39f7524
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/b39f7524

Branch: refs/heads/master
Commit: b39f7524104659a2ca14cb09ddf6cab817491211
Parents: b02ce72
Author: Anoop Sharma <[email protected]>
Authored: Mon Jun 20 23:07:19 2016 +0000
Committer: Anoop Sharma <[email protected]>
Committed: Mon Jun 20 23:07:19 2016 +0000

----------------------------------------------------------------------
 core/sql/bin/SqlciErrors.txt             |    1 +
 core/sql/comexe/ComTdb.h                 |    3 +-
 core/sql/common/CharType.cpp             |    6 +-
 core/sql/common/CharType.h               |   10 +-
 core/sql/common/NAType.cpp               |   79 ++
 core/sql/common/NAType.h                 |    2 +
 core/sql/exp/ExpAlignedFormat.h          |   10 +-
 core/sql/exp/exp_tuple_desc.h            |    2 +-
 core/sql/generator/GenRelScan.cpp        |   59 +-
 core/sql/generator/Generator.cpp         |    3 +-
 core/sql/optimizer/BindRelExpr.cpp       |   25 +-
 core/sql/optimizer/BindWA.cpp            |    3 +-
 core/sql/optimizer/BindWA.h              |   37 +-
 core/sql/optimizer/ItemExpr.cpp          |    1 +
 core/sql/optimizer/ItemFunc.h            |   28 +-
 core/sql/optimizer/NAColumn.cpp          |   15 +
 core/sql/optimizer/NAColumn.h            |    3 +
 core/sql/optimizer/NAFileSet.h           |    2 +
 core/sql/optimizer/NATable.cpp           |  125 ++-
 core/sql/optimizer/NATable.h             |   25 +-
 core/sql/optimizer/RelExeUtil.cpp        |   11 +-
 core/sql/optimizer/SynthType.cpp         |   11 +-
 core/sql/parser/ElemDDLLike.cpp          |    4 +-
 core/sql/parser/ElemDDLLikeCreateTable.h |    6 +-
 core/sql/parser/StmtDDLCreate.cpp        |   43 +-
 core/sql/parser/sqlparser.y              |   18 +
 core/sql/regress/hive/EXPECTED005        |   12 +-
 core/sql/regress/hive/EXPECTED009        | 1131 +++++++++++++++++++++----
 core/sql/regress/hive/FILTER009          |   49 ++
 core/sql/regress/hive/TEST009            |   91 +-
 core/sql/regress/hive/TEST020            |    2 +-
 core/sql/regress/privs2/EXPECTED129      |   36 +-
 core/sql/regress/seabase/EXPECTED003     |    2 +-
 core/sql/regress/seabase/EXPECTED011     |   16 +-
 core/sql/regress/seabase/EXPECTED031     |  237 ++++++
 core/sql/regress/seabase/TEST011         |    2 +-
 core/sql/regress/seabase/TEST031         |   53 ++
 core/sql/sqlcat/desc.h                   |    3 +-
 core/sql/sqlcomp/CmpDescribe.cpp         |  300 +++++--
 core/sql/sqlcomp/CmpSeabaseDDL.h         |  124 +--
 core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp |   27 +-
 core/sql/sqlcomp/CmpSeabaseDDLmd.h       |    8 +
 core/sql/sqlcomp/CmpSeabaseDDLtable.cpp  |  135 ++-
 core/sql/sqlcomp/DefaultConstants.h      |    3 +
 core/sql/sqlcomp/nadefaults.cpp          |    6 +
 45 files changed, 2343 insertions(+), 426 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/bin/SqlciErrors.txt
----------------------------------------------------------------------
diff --git a/core/sql/bin/SqlciErrors.txt b/core/sql/bin/SqlciErrors.txt
index 0f19d9e..3242894 100644
--- a/core/sql/bin/SqlciErrors.txt
+++ b/core/sql/bin/SqlciErrors.txt
@@ -1554,6 +1554,7 @@ $1~String1 --------------------------------
 8434 ZZZZZ 99999 BEGINNER MAJOR DBADMIN Invalid target column for LOB 
function. The column needs to be blob/clob type. 
 8435 ZZZZZ 99999 UUUUUUUU UUUUU UUUUUUU --- unused ---
 8436 ZZZZZ 99999 BEGINNER MAJOR DBADMIN Mismatch detected between compiletime 
and runtime hive table definitions.
+8437 ZZZZZ 99999 BEGINNER MAJOR DBADMIN Mismatch detected between external 
table and underlying hive table definitions.
 8440 ZZZZZ 99999 BEGINNER MAJOR DBADMIN The size of the history buffer is too 
small to execute one or more of the OLAP Windowed Functions in the query. 
 8441 ZZZZZ 99999 BEGINNER MAJOR DBADMIN one or more of the OLAP Windowed 
Functions in the query may require overflow which is not supported yet.
 8442 ZZZZZ 99999 BEGINNER MAJOR DBADMIN Unable to access $0~string0 interface. 
Call to $1~string1 returned error $2~string2($0~int0). Error detail $1~int1.

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/comexe/ComTdb.h
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdb.h b/core/sql/comexe/ComTdb.h
index b5f36b4..96a55c4 100644
--- a/core/sql/comexe/ComTdb.h
+++ b/core/sql/comexe/ComTdb.h
@@ -882,7 +882,8 @@ class ComTdbVirtTableTableInfo  : public ComTdbVirtTableBase
   ComRowFormat rowFormat;
   const char * defaultColFam;
   const char * allColFams;
-  Int64 objectFlags; 
+  Int64 objectFlags;  // flags from OBJECTS table
+  Int64 tablesFlags;  // flags from TABLES table
 };
 
 class ComTdbVirtTableColumnInfo : public ComTdbVirtTableBase

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/common/CharType.cpp
----------------------------------------------------------------------
diff --git a/core/sql/common/CharType.cpp b/core/sql/common/CharType.cpp
index 29d57cc..0491a94 100644
--- a/core/sql/common/CharType.cpp
+++ b/core/sql/common/CharType.cpp
@@ -942,7 +942,8 @@ SQLVarChar::SQLVarChar(Lng32 maxLen,
               FALSE, allowSQLnull, isUpShifted, isCaseInsensitive,
               TRUE, cs, co, ce,
               encoding, vcIndLen),
-      clientDataType_(collHeap())  // Get heap from NABasicObject. Can't 
allocate on stack.
+      clientDataType_(collHeap()),  // Get heap from NABasicObject. Can't 
allocate on stack.
+      wasHiveString_(FALSE)
 {}
 #pragma warn(1506)  // warning elimination
 
@@ -961,7 +962,8 @@ SQLVarChar::SQLVarChar(const CharLenInfo & maxLenInfo,
               FALSE, allowSQLnull, isUpShifted, isCaseInsensitive,
               TRUE, cs, co, ce,
               encoding),
-      clientDataType_(collHeap())  // Get heap from NABasicObject. Can't 
allocate on stack.
+      clientDataType_(collHeap()),  // Get heap from NABasicObject. Can't 
allocate on stack.
+      wasHiveString_(FALSE)
 {}
 
 // -----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/common/CharType.h
----------------------------------------------------------------------
diff --git a/core/sql/common/CharType.h b/core/sql/common/CharType.h
index eefc953..877b377 100644
--- a/core/sql/common/CharType.h
+++ b/core/sql/common/CharType.h
@@ -517,7 +517,8 @@ public:
 //copy ctor
   SQLVarChar(const SQLVarChar& varChar, NAMemory * heap):
                                CharType(varChar,heap),
-                               
clientDataType_(varChar.getClientDataTypeName(),heap)
+                               
clientDataType_(varChar.getClientDataTypeName(),heap),
+                                wasHiveString_(varChar.wasHiveString())
                                {}
 virtual void minRepresentableValue(void*, Lng32*,
                                     NAString** stringLiteral = NULL,
@@ -554,9 +555,14 @@ NAString getClientDataTypeName() const
 
 void setClientDataType(NAString clientName) { clientDataType_ = clientName; }
 
+NABoolean wasHiveString() const {return wasHiveString_;}
+void setWasHiveString(NABoolean v) { wasHiveString_ = v;}
 private:
 
-NAString clientDataType_;
+  NAString clientDataType_;
+
+  // if original datatype was hive 'string' type
+  NABoolean wasHiveString_;
 
 }; // class SQLVarChar
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/common/NAType.cpp
----------------------------------------------------------------------
diff --git a/core/sql/common/NAType.cpp b/core/sql/common/NAType.cpp
index 3553888..3bc3b6e 100644
--- a/core/sql/common/NAType.cpp
+++ b/core/sql/common/NAType.cpp
@@ -40,6 +40,7 @@
 #include "NumericType.h"
 #include "CharType.h"
 #include "CmpCommon.h"     /* want to put NAType obj's on statement heap ... */
+#include "str.h"
 
 // extern declaration
 extern short
@@ -670,6 +671,84 @@ short NAType::convertTypeToText(char * text,          // 
OUTPUT
                                 displayCaseSpecific);
 }
 
+short NAType::getMyTypeAsHiveText(NAString * outputStr)  // output
+{
+  Lng32                      fs_datatype               = getFSDatatype();
+
+  switch (fs_datatype)
+    {
+    case REC_MIN_F_CHAR_H ... REC_MAX_F_CHAR_H:
+      *outputStr = "string";
+      break;
+
+    case REC_MIN_V_CHAR_H ... REC_MAX_V_CHAR_H:
+      {
+        SQLVarChar * ct = (SQLVarChar*)this;
+        if (ct->wasHiveString())
+          *outputStr = "string";
+        else
+          {
+            char buf[20];
+            Int32 size = getNominalSize() / ct->getBytesPerChar();
+            str_itoa(size, buf);
+            *outputStr = "varchar(";
+            *outputStr += buf;
+            *outputStr += ")";
+          }
+      }
+      break;
+
+    case REC_BIN8_SIGNED:
+    case REC_BIN8_UNSIGNED:
+      *outputStr = "tinyint";
+      break;
+
+    case REC_BIN16_SIGNED:
+    case REC_BIN16_UNSIGNED:
+      *outputStr = "smallint";
+      break;
+
+    case REC_BIN32_SIGNED:
+    case REC_BIN32_UNSIGNED:
+      *outputStr = "int";
+      break;
+
+    case REC_BIN64_SIGNED:
+      *outputStr = "bigint";
+      break;
+
+    case REC_FLOAT32:
+      *outputStr = "float";
+      break;
+
+    case REC_FLOAT64:
+      *outputStr = "double";
+      break;
+
+    case REC_DATETIME:
+      {
+        DatetimeIntervalCommonType & dtiCommonType =
+          (DatetimeIntervalCommonType &) *this;
+         
+        ComDateTimeStartEnd dtEndField = 
+          (ComDateTimeStartEnd)dtiCommonType.getEndField();
+
+        if ((rec_datetime_field)dtEndField == REC_DATE_SECOND)
+          *outputStr = "timestamp";
+        else 
+          *outputStr = "date";
+      }
+      break;
+
+    default:
+      *outputStr = "unknown";
+      break;
+
+    } // switch
+
+  return 0;
+}
+
 short NAType::getMyTypeAsText(NAString * outputStr,  // output
                              NABoolean addNullability)
 {

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/common/NAType.h
----------------------------------------------------------------------
diff --git a/core/sql/common/NAType.h b/core/sql/common/NAType.h
index ea4c64d..4bf09a0 100644
--- a/core/sql/common/NAType.h
+++ b/core/sql/common/NAType.h
@@ -563,6 +563,8 @@ public:
   short getMyTypeAsText(NAString * outputStr, 
                        NABoolean addNullability = TRUE); // output
 
+  short getMyTypeAsHiveText(NAString * outputStr);  // output
+
   // used for query caching
   Lng32 getSize() const;
   Lng32 hashKey() const;

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/exp/ExpAlignedFormat.h
----------------------------------------------------------------------
diff --git a/core/sql/exp/ExpAlignedFormat.h b/core/sql/exp/ExpAlignedFormat.h
index 2bc82cb..1a614b7 100644
--- a/core/sql/exp/ExpAlignedFormat.h
+++ b/core/sql/exp/ExpAlignedFormat.h
@@ -297,14 +297,14 @@ public:
          ( (bitmapOffset_ == 0) && (firstFixedOffset == hdrSz) ) )
       return 0;
     else
-      return ( *((UInt16 *)(((char *)this) + hdrSz)) );
+      return ( *((UInt32 *)(((char *)this) + hdrSz)) );
   }
 
   //
   // Get the offset at the specified VOA entry
   UInt32 getVoaEntry( UInt32                    voaOffset )
   {
-    return( *((UInt16 *)((char *)this + voaOffset)) );
+    return( *((UInt32 *)((char *)this + voaOffset)) );
   }
 
   //
@@ -312,7 +312,7 @@ public:
   void setVoaOffset( UInt32                     voaOffset,
                      UInt32                     voaEntryValue )
   {
-    *((UInt16 *)(((char *)this) + voaOffset)) = (UInt16)voaEntryValue;
+    *((UInt32 *)(((char *)this) + voaOffset)) = (UInt32)voaEntryValue;
   }
 
   static void incrVoaOffset( UInt32            &voaOffset )
@@ -402,7 +402,7 @@ public:
 
   static UInt32 getVarLength( char             *dataPtr )
   {
-    UInt16 len;
+    UInt32 len;
     str_cpy_all( (char *)&len, dataPtr, sizeof(len) );
     return len;
   }
@@ -452,7 +452,7 @@ public:
       UInt32 endVoa = firstFixedOffset - OFFSET_SIZE;
       while( endVoa >= hdrSz )
       {
-        if ( *(UInt16 *)(dataPtr + endVoa) > 0 )
+        if ( *(UInt32 *)(dataPtr + endVoa) > 0 )
         break;
 
         endVoa -= OFFSET_SIZE;

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/exp/exp_tuple_desc.h
----------------------------------------------------------------------
diff --git a/core/sql/exp/exp_tuple_desc.h b/core/sql/exp/exp_tuple_desc.h
index 8a39f72..aa4838c 100644
--- a/core/sql/exp/exp_tuple_desc.h
+++ b/core/sql/exp/exp_tuple_desc.h
@@ -825,7 +825,7 @@ public:
       else // indirect varchar, get offset from loop variable.
         tgtOffset = varOffset + nullIndLen;  // skip over the null indicator 
len
 
-      setVoaValue(tgt, voaEntryOffset, varOffset, (UInt16)vcIndLen); 
+      setVoaValue(tgt, voaEntryOffset, varOffset, (UInt32)vcIndLen); 
 
       varOffset += len + nullIndLen + vcIndLen; 
     }

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/generator/GenRelScan.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenRelScan.cpp 
b/core/sql/generator/GenRelScan.cpp
index d1cb243..0b152ff 100644
--- a/core/sql/generator/GenRelScan.cpp
+++ b/core/sql/generator/GenRelScan.cpp
@@ -836,8 +836,9 @@ short FileScan::codeGenForHive(Generator * generator)
   //   account for this when we generate the move expression
   //   by making sure that the output ValueIds created during
   //   binding refer to the outputs of the move expression
-
   ValueIdList origExprVids;
+
+  NABoolean longVC = FALSE;
   for (int ii = 0; ii < (int)hdfsVals.entries();ii++)
   {
     if (convertSkipList[ii] == 0)
@@ -874,15 +875,22 @@ short FileScan::codeGenForHive(Generator * generator)
     orcRowLen += sizeof(Lng32);
     orcRowLen += givenType.getDisplayLength();
 
+    if ((DFS2REC::isAnyVarChar(givenType.getFSDatatype())) &&
+        (givenType.getTotalSize() > 1024))
+      longVC = TRUE;
   } // for (ii = 0; ii < hdfsVals; ii++)
     
-
   UInt32 hiveScanMode = CmpCommon::getDefaultLong(HIVE_SCAN_SPECIAL_MODE);
   //enhance pCode to handle this mode in the future
   //this is for JIRA 1920
   if((hiveScanMode & 2 ) > 0)   //if HIVE_SCAN_SPECIAL_MODE is 2, disable pCode
     exp_gen->setPCodeMode(ex_expr::PCODE_NONE);
 
+  // use CIF if there are long varchars (> 1K length) and CIF has not
+  // been explicitly turned off.
+  if (longVC && (CmpCommon::getDefault(COMPRESSED_INTERNAL_FORMAT) != DF_OFF))
+    generator->setCompressedInternalFormat();
+ 
   // Add ascii columns to the MapTable. After this call the MapTable
   // has ascii values in the work ATP at index asciiTuppIndex.
   exp_gen->processValIdList(
@@ -1131,9 +1139,13 @@ short FileScan::codeGenForHive(Generator * generator)
   // Try to get enough buffer space to hold twice as many records
   // as the up queue.
   //
-  // This should be more sophisticate than this, and should maybe be done
-  // within the buffer class, but for now this will do.
-  // 
+  
+  UInt32 FiveM = 5*1024*1024;
+
+  // If returnedrowlen > 5M, then set upqueue entries to 2.
+  if (returnedRowlen > FiveM)
+    upqueuelength = 2;
+
   ULng32 cbuffersize = 
     SqlBufferNeededSize((upqueuelength * 2 / numBuffers),
                        returnedRowlen);
@@ -1141,6 +1153,15 @@ short FileScan::codeGenForHive(Generator * generator)
   //
   buffersize = buffersize > cbuffersize ? buffersize : cbuffersize;
 
+  // Cap the buffer size at 5M and adjust upqueue entries.
+  // Do this only if returnrowlen is not > 5M
+  if ((returnedRowlen <= FiveM) && (buffersize > FiveM))
+    {
+      buffersize = FiveM;
+
+      upqueuelength = ((buffersize / returnedRowlen) * numBuffers)/2;
+    }
+
   // default value is in K bytes
   Int64 hdfsBufSize = 0;
 
@@ -2402,6 +2423,7 @@ short HbaseAccess::codeGen(Generator * generator)
   ValueIdArray encodedKeyExprVidArr(getIndexDesc()->getIndexKey().entries());
   const CollIndex numColumns = columnList.entries();
 
+  NABoolean longVC = FALSE;
   for (CollIndex ii = 0; ii < numColumns; ii++)
     {
      ItemExpr * col_node = ((columnList[ii]).getValueDesc())->getItemExpr();
@@ -2458,8 +2480,17 @@ short HbaseAccess::codeGen(Generator * generator)
      HbaseAccess_updateHbaseInfoNode(hbTsVIDlist, nac->getColName(), ii);
      HbaseAccess_updateHbaseInfoNode(hbVersVIDlist, nac->getColName(), ii);
 
+     if ((DFS2REC::isAnyVarChar(givenType.getFSDatatype())) &&
+          (givenType.getTotalSize() > 1024))
+        longVC = TRUE;
+
     } // for (ii = 0; ii < numCols; ii++)
 
+  // use CIF if there are long varchars (> 1K length) and CIF has not
+  // been explicitly turned off.
+  if (longVC && (CmpCommon::getDefault(COMPRESSED_INTERNAL_FORMAT) != DF_OFF))
+    generator->setCompressedInternalFormat();
+
   ValueIdList encodedKeyExprVids(encodedKeyExprVidArr);
 
   ExpTupleDesc::TupleDataFormat asciiRowFormat = 
@@ -2804,13 +2835,29 @@ short HbaseAccess::codeGen(Generator * generator)
   // This should be more sophisticate than this, and should maybe be done
   // within the buffer class, but for now this will do.
   // 
+  UInt32 FiveM = 5*1024*1024;
+
+  // If returnedrowlen > 5M, then set upqueue entries to 2.
+  UInt32 bufRowlen = MAXOF(convertRowLen, 1000);
+  if (bufRowlen > FiveM)
+    upqueuelength = 2;
+
   ULng32 cbuffersize = 
     SqlBufferNeededSize((upqueuelength * 2 / numBuffers),
-                       1000); //returnedRowlen);
+                       bufRowlen); //returnedRowlen);
   // But use at least the default buffer size.
   //
   buffersize = buffersize > cbuffersize ? buffersize : cbuffersize;
 
+  // Cap the buffer size at 5M and adjust upqueue entries.
+  // Do this only if returnrowlen is not > 5M
+  if ((bufRowlen <= FiveM) && (buffersize > FiveM))
+    {
+      buffersize = FiveM;
+
+      upqueuelength = ((buffersize / bufRowlen) * numBuffers)/2;
+    }
+
   Int32 computedHBaseRowSizeFromMetaData = 
getTableDesc()->getNATable()->computeHBaseRowSizeFromMetaData();
   if (computedHBaseRowSizeFromMetaData * getEstRowsAccessed().getValue()  <
                 
getDefault(TRAF_TABLE_SNAPSHOT_SCAN_TABLE_SIZE_THRESHOLD)*1024*1024)

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/generator/Generator.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/Generator.cpp b/core/sql/generator/Generator.cpp
index b0d5f3b..c244c5a 100644
--- a/core/sql/generator/Generator.cpp
+++ b/core/sql/generator/Generator.cpp
@@ -1874,7 +1874,8 @@ desc_struct * Generator::createVirtualTableDesc(
       strcpy(table_desc->body.table_desc.all_col_fams, tableInfo->allColFams);
     }
 
-  table_desc->body.table_desc.tableFlags = (tableInfo ? tableInfo->objectFlags 
: 0);
+  table_desc->body.table_desc.objectFlags = (tableInfo ? 
tableInfo->objectFlags : 0);
+  table_desc->body.table_desc.tablesFlags = (tableInfo ? 
tableInfo->tablesFlags : 0);
 
   desc_struct * files_desc = readtabledef_allocate_desc(DESC_FILES_TYPE);
   //  files_desc->body.files_desc.audit = -1; // audited table

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/BindRelExpr.cpp
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/BindRelExpr.cpp 
b/core/sql/optimizer/BindRelExpr.cpp
index b79e8e1..0be1d61 100644
--- a/core/sql/optimizer/BindRelExpr.cpp
+++ b/core/sql/optimizer/BindRelExpr.cpp
@@ -1551,19 +1551,20 @@ NATable *BindWA::getNATable(CorrName& corrName,
       NATable *nativeNATable = bindWA->getSchemaDB()->getNATableDB()->
                                   get(externalCorrName, bindWA, 
inTableDescStruct);
   
-       // Compare column lists
-       // TBD - return what mismatches
-       if ( nativeNATable && !(table->getNAColumnArray() == 
nativeNATable->getNAColumnArray()))
-         {
-           *CmpCommon::diags() << DgSqlCode(-3078)
-                               << DgString0(adjustedName)
-                               << 
DgTableName(table->getTableName().getQualifiedNameAsAnsiString());
-           bindWA->setErrStatus();
-           nativeNATable->setRemoveFromCacheBNC(TRUE);
-           return NULL;
-         }
+      // Compare column lists
+      // TBD - return what mismatches
+      if ( nativeNATable && !(table->getNAColumnArray() == 
nativeNATable->getNAColumnArray()) &&
+           (NOT bindWA->externalTableDrop()))
+        {
+          *CmpCommon::diags() << DgSqlCode(-3078)
+                              << DgString0(adjustedName)
+                              << 
DgTableName(table->getTableName().getQualifiedNameAsAnsiString());
+          bindWA->setErrStatus();
+          nativeNATable->setRemoveFromCacheBNC(TRUE);
+          return NULL;
+        }
     }
-    
+  
   HostVar *proto = corrName.getPrototype();
   if (proto && proto->isPrototypeValid())
     corrName.getPrototype()->bindNode(bindWA);

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/BindWA.cpp
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/BindWA.cpp b/core/sql/optimizer/BindWA.cpp
index b81af8b..7202d11 100644
--- a/core/sql/optimizer/BindWA.cpp
+++ b/core/sql/optimizer/BindWA.cpp
@@ -134,7 +134,6 @@ BindWA::BindWA(SchemaDB *schemaDB, CmpContext* cmpContext, 
NABoolean inDDL, NABo
   //     , inRIMaint_(FALSE)
      , inViewWithCheckOption_(NULL)
      , viewCount_(0)
-     , allowExternalTables_(allowExtTables)
      , errFlag_(FALSE)
      , uniqueNum_(0)
      , uniqueIudNum_(0) //++Triggers,
@@ -187,6 +186,7 @@ BindWA::BindWA(SchemaDB *schemaDB, CmpContext* cmpContext, 
NABoolean inDDL, NABo
      , outerAggScope_(NULL)
      , hasCallStmts_(FALSE)
      , isTrafLoadPrep_(FALSE)
+     , flags_(0)
 {
   // get current default schema, using NAMETYPE NSK or ANSI rules
   defaultSchema_ = schemaDB_->getDefaultSchema(SchemaDB::APPLY_NAMETYPE_RULES);
@@ -196,6 +196,7 @@ BindWA::BindWA(SchemaDB *schemaDB, CmpContext* cmpContext, 
NABoolean inDDL, NABo
 
   hcui_ = new(cmpContext->statementHeap()) 
HbaseColUsageInfo(cmpContext->statementHeap());
 
+  setAllowExternalTables(allowExtTables);
 } // BindWA::BindWA()
 
 // ***********************************************************************

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/BindWA.h
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/BindWA.h b/core/sql/optimizer/BindWA.h
index 0094bc8..c88b631 100644
--- a/core/sql/optimizer/BindWA.h
+++ b/core/sql/optimizer/BindWA.h
@@ -1173,6 +1173,21 @@ private:
 // ***********************************************************************
 class BindWA : public NABasicObject
 {
+ enum Flags {
+   // --------------------------------------------------------------------
+   // Flag to indicate we are accessing an object which is defined in an
+   // external (native) hive or hbase.
+   // --------------------------------------------------------------------
+    ALLOW_EXT_TABLES          = 0x00000001,
+
+    // external table being dropped.
+    EXT_TABLE_DROP            = 0x00000002,
+
+    // return underlying hive table defn, if the table has an associated
+    // external table
+    RETURN_HIVE_TABLE_DEFN    = 0x00000004
+ };
+
 public:
 
   // --------------------------------------------------------------------
@@ -1483,8 +1498,20 @@ public:
 
   short &viewCount()                      { return viewCount_; }
 
-  NABoolean allowExternalTables() const { return allowExternalTables_; }
-  void setAllowExternalTables(NABoolean t) { allowExternalTables_ = t; }
+  NABoolean allowExternalTables() const 
+  { return (flags_ & ALLOW_EXT_TABLES) != 0; }
+  void setAllowExternalTables(NABoolean v) 
+  { v ? flags_ |= ALLOW_EXT_TABLES : flags_ &= ~ALLOW_EXT_TABLES; }
+
+  NABoolean externalTableDrop() const 
+  { return (flags_ & EXT_TABLE_DROP) != 0; }
+  void setExternalTableDrop(NABoolean v) 
+  { v ? flags_ |= EXT_TABLE_DROP : flags_ &= ~EXT_TABLE_DROP; }
+
+  NABoolean returnHiveTableDefn() const 
+  { return (flags_ & RETURN_HIVE_TABLE_DEFN) != 0; }
+  void setReturnHiveTableDefn(NABoolean v) 
+  { v ? flags_ |= RETURN_HIVE_TABLE_DEFN : flags_ &= ~RETURN_HIVE_TABLE_DEFN; }
 
   LIST(OptSqlTableOpenInfo *) &getStoiList()  { return stoiList_; }
   LIST(OptUdrOpenInfo *) &getUdrStoiList()  { return udrStoiList_; }
@@ -1819,11 +1846,7 @@ private:
   // --------------------------------------------------------------------
   short viewCount_;
 
-  // --------------------------------------------------------------------
-  // Flag to indicate we are accessing an object which is defined in an
-  // external (native) hive or hbase.
-  // --------------------------------------------------------------------
-  NABoolean allowExternalTables_;
+  ULng32 flags_;
 
   // points to a class used by RowSets code.
   HostArraysWA *hostArraysArea_;

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/ItemExpr.cpp
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/ItemExpr.cpp b/core/sql/optimizer/ItemExpr.cpp
index bdc9ea1..c18ce02 100644
--- a/core/sql/optimizer/ItemExpr.cpp
+++ b/core/sql/optimizer/ItemExpr.cpp
@@ -12928,6 +12928,7 @@ ItemExpr * Repeat::copyTopNode(ItemExpr *derivedNode, 
CollHeap* outHeap)
     result = derivedNode;
   
   ((Repeat *) result)->setMaxLength(getMaxLength());
+  ((Repeat *) result)->maxLengthWasExplicitlySet_ = maxLengthWasExplicitlySet_;
 
   return BuiltinFunction::copyTopNode(result, outHeap);
 }

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/ItemFunc.h
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/ItemFunc.h b/core/sql/optimizer/ItemFunc.h
index 0589fc7..a431461 100644
--- a/core/sql/optimizer/ItemFunc.h
+++ b/core/sql/optimizer/ItemFunc.h
@@ -3527,10 +3527,16 @@ public:
 class Repeat : public BuiltinFunction
 {
 public:
-  Repeat(ItemExpr *val1Ptr, ItemExpr *val2Ptr)
+  Repeat(ItemExpr *val1Ptr, ItemExpr *val2Ptr, Int32 maxLength = -1)
        : BuiltinFunction(ITM_REPEAT, CmpCommon::statementHeap(),
-                         2, val1Ptr, val2Ptr)
-  { allowsSQLnullArg() = FALSE; maxLength_ = -1; }
+                         2, val1Ptr, val2Ptr),
+         maxLength_(maxLength),
+         maxLengthWasExplicitlySet_(FALSE)
+  { 
+    allowsSQLnullArg() = FALSE; 
+    if (maxLength > 0)
+      maxLengthWasExplicitlySet_ = TRUE;
+  }
 
   // a virtual function for type propagating the node
   virtual const NAType * synthesizeType();
@@ -3550,14 +3556,18 @@ public:
 
  private:
 
- // max length of Repeat expression. Currently this is set only when Repeat
- // is used certain expansions of LPAD and RPAD. Initialized to the value -1,
- // which indicates that maxLength has not been computed.
- Int32 maxLength_;
+  // max length of Repeat expression. 
+  // If not passed in during constrtuctor, then it is set only when Repeat
+  // is used certain expansions of LPAD and RPAD. 
+  // It is initialized to the value -1, which indicates that maxLength 
+  // has not been computed or passed in.
+  Int32 maxLength_;
 
-  virtual NABoolean hasEquivalentProperties(ItemExpr * other) { return TRUE;}
+  // if max length was specified in REPEAT function 
+  // and passed in during constructor.
+  NABoolean maxLengthWasExplicitlySet_;
 
-  
+  virtual NABoolean hasEquivalentProperties(ItemExpr * other) { return TRUE;}
 
 }; // class Repeat
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/NAColumn.cpp
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/NAColumn.cpp b/core/sql/optimizer/NAColumn.cpp
index 54c53fd..fd74215 100644
--- a/core/sql/optimizer/NAColumn.cpp
+++ b/core/sql/optimizer/NAColumn.cpp
@@ -68,6 +68,12 @@ NABoolean NAColumn::operator==(const NAColumn& other) const
          (*getType() == *other.getType()));
 }
 
+
+NABoolean NAColumn::operator==(const NAString& otherColName) const
+{
+  return (getColName() == otherColName);
+}
+
 void NAColumn::deepDelete()
 {
   if(defaultValue_)
@@ -864,6 +870,15 @@ Int32 NAColumnArray::getColumnPosition(NAColumn& nc) const
   return -1;
 }
 
+Int32 NAColumnArray::getColumnPosition(NAString& nc) const
+{
+  for (CollIndex j = 0; j < entries(); j++) {
+    if ( (* at(j)) == nc )  // compare via NAColumn::operator==()
+      return j;
+  }
+  return -1;
+}
+
 NAString NAColumnArray::getColumnNamesAsString(char separator) const
 {
    return getColumnNamesAsString(separator, entries());

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/NAColumn.h
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/NAColumn.h b/core/sql/optimizer/NAColumn.h
index af3fb84..2c13c57 100644
--- a/core/sql/optimizer/NAColumn.h
+++ b/core/sql/optimizer/NAColumn.h
@@ -339,6 +339,7 @@ public:
   // Standard operators
   // ---------------------------------------------------------------------
   NABoolean operator==(const NAColumn& other) const;
+  NABoolean operator==(const NAString& other) const;
 
   // needed by priority_queue for printing column names in order
   // do NOT use this operator for any other purpose because it
@@ -641,6 +642,8 @@ public:
   //    -1 if the column is not found in the array
   Int32 getColumnPosition(NAColumn&) const;
 
+  Int32 getColumnPosition(NAString&) const;
+
   // get total storage size (aggregated over each element)
   Int32 getTotalStorageSize() const;
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/NAFileSet.h
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/NAFileSet.h b/core/sql/optimizer/NAFileSet.h
index f8ca7ed..ea4414e 100644
--- a/core/sql/optimizer/NAFileSet.h
+++ b/core/sql/optimizer/NAFileSet.h
@@ -80,6 +80,7 @@ enum FileOrganizationEnum
 // -----------------------------------------------------------------------
 class NAFileSet : public NABasicObject
 {
+  friend class NATable;
 public:
 
   // ---------------------------------------------------------------------
@@ -153,6 +154,7 @@ public:
                                               { return indexKeyColumns_; }
 
   const desc_struct * getKeysDesc() const { return keysDesc_; }
+  desc_struct * getKeysDesc() { return keysDesc_; }
 
   Lng32 getCountOfFiles() const                  { return countOfFiles_; }
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/NATable.cpp
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/NATable.cpp b/core/sql/optimizer/NATable.cpp
index 0d45133..827aaca 100644
--- a/core/sql/optimizer/NATable.cpp
+++ b/core/sql/optimizer/NATable.cpp
@@ -3575,13 +3575,16 @@ NAType* getSQColTypeForHive(const char* hiveType, 
NAMemory* heap)
       CharInfo::CharSet hiveCharsetEnum = 
CharInfo::getCharSetEnum(hiveCharset);
       Int32 maxNumChars = 0;
       Int32 storageLen = len;
-      return new (heap) SQLVarChar(CharLenInfo(maxNumChars, storageLen),
-                                   TRUE, // allow NULL
-                                   FALSE, // not upshifted
-                                   FALSE, // not case-insensitive
-                                   CharInfo::getCharSetEnum(hiveCharset),
-                                   CharInfo::DefaultCollation,
-                                   CharInfo::IMPLICIT);
+      SQLVarChar * nat = 
+        new (heap) SQLVarChar(CharLenInfo(maxNumChars, storageLen),
+                              TRUE, // allow NULL
+                              FALSE, // not upshifted
+                              FALSE, // not case-insensitive
+                              CharInfo::getCharSetEnum(hiveCharset),
+                              CharInfo::DefaultCollation,
+                              CharInfo::IMPLICIT);
+      nat->setWasHiveString(TRUE);
+      return nat;
     }
   
   if ( !strcmp(hiveType, "float"))
@@ -5218,10 +5221,17 @@ NATable::NATable(BindWA *bindWA,
       (!(corrName.isSeabaseMD() || corrName.isSpecialTable())))
      setupPrivInfo();
 
-  if ((table_desc->body.table_desc.tableFlags & 
SEABASE_OBJECT_IS_EXTERNAL_HIVE) != 0 ||
-      (table_desc->body.table_desc.tableFlags & 
SEABASE_OBJECT_IS_EXTERNAL_HBASE) != 0)
+  if ((table_desc->body.table_desc.objectFlags & 
SEABASE_OBJECT_IS_EXTERNAL_HIVE) != 0 ||
+      (table_desc->body.table_desc.objectFlags & 
SEABASE_OBJECT_IS_EXTERNAL_HBASE) != 0)
     setIsExternalTable(TRUE);
 
+  if (CmpSeabaseDDL::isMDflagsSet
+      (table_desc->body.table_desc.tablesFlags, MD_TABLES_HIVE_EXT_COL_ATTRS))
+    setHiveExtColAttrs(TRUE);
+  if (CmpSeabaseDDL::isMDflagsSet
+      (table_desc->body.table_desc.tablesFlags, MD_TABLES_HIVE_EXT_KEY_ATTRS))
+    setHiveExtKeyAttrs(TRUE);
+
   rcb_ = table_desc->body.table_desc.rcb;
   rcbLen_ = table_desc->body.table_desc.rcbLen;
   keyLength_ = table_desc->body.table_desc.keyLen;
@@ -7901,6 +7911,39 @@ NABoolean  NATable::getHbaseTableInfo(Int32& 
hbtIndexLevels, Int32& hbtBlockSize
   return TRUE;
 }
 
+// This method is called on a hive NATable.
+// If that table has a corresponding external table,
+// then this method moves the relevant attributes from 
+// NATable of external table (etTable) to this.
+// Currently, column and clustering key info is moved.
+short NATable::updateExtTableAttrs(NATable *etTable)
+{
+  NAFileSet *fileset = this->getClusteringIndex();
+  NAFileSet *etFileset = etTable->getClusteringIndex();
+
+  colcount_ = etTable->getColumnCount();
+  colArray_ = etTable->getNAColumnArray();
+  fileset->allColumns_ = etFileset->getAllColumns();
+  if (NOT etFileset->hasOnlySyskey()) // explicit key was specified
+    {
+      keyLength_ = etTable->getKeyLength();
+      recordLength_ = etTable->getRecordLength();
+      
+      fileset->keysDesc_ = etFileset->getKeysDesc();
+      fileset->indexKeyColumns_ = etFileset->getIndexKeyColumns();
+      fileset->keyLength_ = etFileset->getKeyLength();
+      fileset->encodedKeyLength_ = etFileset->getEncodedKeyLength();
+    }
+
+  /*
+  fileset->partitioningKeyColumns_ = etFileset->getPartitioningKeyColumns();
+  fileset->partFunc_ = etFileset->getPartitioningFunction();
+  fileset->countOfFiles_ = etFileset->getCountOfFiles();
+  */
+
+  return 0;
+}
+
 // get details of this NATable cache entry
 void NATableDB::getEntryDetails(
      Int32 ii,                      // (IN) : NATable cache iterator entry
@@ -8083,6 +8126,7 @@ NATable * NATableDB::get(CorrName& corrName, BindWA * 
bindWA,
     //otherwise it is NULL.
     NATable * tableInCache = table;
 
+    CmpSeabaseDDL cmpSBD((NAHeap *)CmpCommon::statementHeap());
     if ((corrName.isHbase() || corrName.isSeabase()) &&
        (!isSQUmdTable(corrName)) &&
        (!isSQUtiDisplayExplain(corrName)) &&
@@ -8091,9 +8135,6 @@ NATable * NATableDB::get(CorrName& corrName, BindWA * 
bindWA,
       // ------------------------------------------------------------------
       // Create an NATable object for a Trafodion/HBase table
       // ------------------------------------------------------------------
-      CmpSeabaseDDL cmpSBD((NAHeap *)CmpCommon::statementHeap());
-
-
       desc_struct *tableDesc = NULL;
 
       NABoolean isSeabase = FALSE;
@@ -8275,9 +8316,63 @@ NATable * NATableDB::get(CorrName& corrName, BindWA * 
bindWA,
          htbl = hiveMetaDB_->getTableDesc(schemaNameInt, tableNameInt);
 
        if ( htbl )
-        {
-          table = new (naTableHeap) NATable(bindWA, corrName, naTableHeap, 
htbl);
-        }
+         {
+           table = new (naTableHeap) NATable
+             (bindWA, corrName, naTableHeap, htbl);
+
+           // 'table' is the NATable for underlying hive table.
+           // That table may also have an associated external table.
+           // Skip processing the external table defn, if only the 
+           // underlying hive table is needed.
+           if (NOT bindWA->returnHiveTableDefn())
+             {
+               // if this hive/orc table has an associated external table, 
+               // get table desc for it.
+               NAString extName = ComConvertNativeNameToTrafName(
+                    corrName.getQualifiedNameObj().getCatalogName(),
+                    corrName.getQualifiedNameObj().getSchemaName(),
+                    corrName.getQualifiedNameObj().getObjectName());
+               
+               QualifiedName qn(extName, 3);
+               desc_struct *etDesc = cmpSBD.getSeabaseTableDesc(
+                    qn.getCatalogName(),
+                    qn.getSchemaName(),
+                    qn.getObjectName(),
+                    COM_BASE_TABLE_OBJECT);
+               
+               if (table && etDesc)
+                 {
+                   CorrName cn(qn);
+                   NATable * etTable = new (naTableHeap) NATable
+                     (bindWA, cn, naTableHeap, etDesc);
+                   
+                   // if ext and hive columns dont match, return error
+                   // unless it is a drop stmt.
+                   if ((table->getUserColumnCount() != 
etTable->getUserColumnCount()) &&
+                       (NOT bindWA->externalTableDrop()))
+                     {
+                       *CmpCommon::diags()
+                         << DgSqlCode(-8437);
+                       
+                       bindWA->setErrStatus();
+                       return NULL;
+                     }
+                   
+                   if (etTable->hiveExtColAttrs() || 
etTable->hiveExtKeyAttrs())
+                     {
+                       // attrs were explicitly specified for this external
+                       // table. Merge them with the hive table attrs.
+                       short rc = table->updateExtTableAttrs(etTable);
+                       if (rc)
+                         {
+                           bindWA->setErrStatus();
+                           return NULL;
+                         }
+                     }
+                   table->setHasHiveExtTable(TRUE);
+                 } // ext table 
+             } // allowExternalTables
+         } // htbl
        else 
          {
            if ((hiveMetaDB_->getErrCode() == 0)||

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/NATable.h
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/NATable.h b/core/sql/optimizer/NATable.h
index 10d1b69..7a87344 100644
--- a/core/sql/optimizer/NATable.h
+++ b/core/sql/optimizer/NATable.h
@@ -502,6 +502,10 @@ public:
   NABoolean isOfflinePartition(const NAString &partitionName) const
   { return !partitionName.isNull() && !containsPartition(partitionName); }
 
+  // move relevant attributes from etTable to this.
+  // Currently, column and key info is moved.
+  short updateExtTableAttrs(NATable *etTable);
+
   const Int64 &getCreateTime() const            { return createTime_; }
   const Int64 &getRedefTime() const             { return redefTime_; }
   const Int64 &getCacheTime() const             { return cacheTime_; }
@@ -716,7 +720,6 @@ public:
   NABoolean isExternalTable() const
   {  return (flags_ & IS_EXTERNAL_TABLE) != 0; }
 
-
   void setHasExternalTable( NABoolean value )
   {  value ? flags_ |= HAS_EXTERNAL_TABLE : flags_ &= ~HAS_EXTERNAL_TABLE; }
 
@@ -729,6 +732,21 @@ public:
   NABoolean isHistogramTable() const
   {  return (flags_ & IS_HISTOGRAM_TABLE) != 0; }
 
+  void setHasHiveExtTable( NABoolean value )
+  {  value ? flags_ |= HAS_HIVE_EXT_TABLE : flags_ &= ~HAS_HIVE_EXT_TABLE; }
+  NABoolean hasHiveExtTable() const
+  {  return (flags_ & HAS_HIVE_EXT_TABLE) != 0; }
+
+  void setHiveExtColAttrs( NABoolean value )
+  {  value ? flags_ |= HIVE_EXT_COL_ATTRS : flags_ &= ~HIVE_EXT_COL_ATTRS; }
+  NABoolean hiveExtColAttrs() const
+  {  return (flags_ & HIVE_EXT_COL_ATTRS) != 0; }
+
+  void setHiveExtKeyAttrs( NABoolean value )
+  {  value ? flags_ |= HIVE_EXT_KEY_ATTRS : flags_ &= ~HIVE_EXT_KEY_ATTRS; }
+  NABoolean hiveExtKeyAttrs() const
+  {  return (flags_ & HIVE_EXT_KEY_ATTRS) != 0; }
+ 
   const CheckConstraintList &getCheckConstraints() const
                                                 { return checkConstraints_; }
   const AbstractRIConstraintList &getUniqueConstraints() const
@@ -944,7 +962,10 @@ private:
     SERIALIZED_COLUMN         = 0x00040000,
     IS_EXTERNAL_TABLE         = 0x00080000,
     HAS_EXTERNAL_TABLE        = 0x00100000,
-    IS_HISTOGRAM_TABLE        = 0x00200000
+    IS_HISTOGRAM_TABLE        = 0x00200000,
+    HAS_HIVE_EXT_TABLE        = 0x00400000,
+    HIVE_EXT_COL_ATTRS        = 0x00800000,
+    HIVE_EXT_KEY_ATTRS        = 0x01000000,
   };
     
   UInt32 flags_;

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/RelExeUtil.cpp
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/RelExeUtil.cpp 
b/core/sql/optimizer/RelExeUtil.cpp
index 7ea701a..7c9e1b4 100644
--- a/core/sql/optimizer/RelExeUtil.cpp
+++ b/core/sql/optimizer/RelExeUtil.cpp
@@ -3989,13 +3989,16 @@ RelExpr * DDLExpr::bindNode(BindWA *bindWA)
           (createTableNode->isSetTable()))
       {
         // these options not supported in open source
-        *CmpCommon::diags() << DgSqlCode(-4222) << DgString0("DDL");
+        *CmpCommon::diags() << DgSqlCode(-4222) << 
DgString0("InMemory/Set/Multiset");
         bindWA->setErrStatus();
         return NULL;
       }
 
       // Hive tables can only be specified as external and must be created
       // with the FOR clause
+       if (createTableNode->isExternal())
+         qualObjName_.applyDefaults(bindWA->getDefaultSchema());
+
       if (qualObjName_.isHive()) 
       {
         if (createTableNode->isExternal())
@@ -4005,7 +4008,7 @@ RelExpr * DDLExpr::bindNode(BindWA *bindWA)
         }
         else
         {
-          *CmpCommon::diags() << DgSqlCode(-4222) << DgString0("DDL");
+          *CmpCommon::diags() << DgSqlCode(-3242) << DgString0("External 
tables supported on hive tables only.");
           bindWA->setErrStatus();
           return NULL;
         }
@@ -4414,7 +4417,7 @@ RelExpr * DDLExpr::bindNode(BindWA *bindWA)
   if (isHbase_ || externalTable)
     return boundExpr;
 
-  *CmpCommon::diags() << DgSqlCode(-4222) << DgString0("DDL");
+  *CmpCommon::diags() << DgSqlCode(-3242) << DgString0("DDL operations can 
only be done on trafodion or external tables.");
   bindWA->setErrStatus();
   return NULL;
 }
@@ -4506,7 +4509,7 @@ RelExpr * ExeUtilProcessVolatileTable::bindNode(BindWA 
*bindWA)
   if (NOT isHbase_)
     {
       // non-hbase tables not supported in open source
-      *CmpCommon::diags() << DgSqlCode(-4222) << DgString0("DDL");
+      *CmpCommon::diags() << DgSqlCode(-3242) << DgString0("Non-hbase tables 
not supported.");
       bindWA->setErrStatus();
       return NULL;
     }

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/optimizer/SynthType.cpp
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/SynthType.cpp b/core/sql/optimizer/SynthType.cpp
index 67940f7..6d91505 100644
--- a/core/sql/optimizer/SynthType.cpp
+++ b/core/sql/optimizer/SynthType.cpp
@@ -3571,7 +3571,16 @@ const NAType *Repeat::synthesizeType()
 
   // figure out the max length of result.
   NABoolean negate;
-  if ((child(1)->getOperatorType() == ITM_CONSTANT) &&
+  if (maxLengthWasExplicitlySet_)
+    {
+      // cap max len at traf_max_character_col_length
+      size_in_bytes = 
+        MINOF(CmpCommon::getDefaultNumeric(TRAF_MAX_CHARACTER_COL_LENGTH), 
+              getMaxLength());
+      size_in_chars = 
+        size_in_bytes / CharInfo::minBytesPerChar(ctyp1.getCharSet());
+    }
+  else if ((child(1)->getOperatorType() == ITM_CONSTANT) &&
       (child(1)->castToConstValue(negate)))
     {
       ConstValue * cv = child(1)->castToConstValue(negate);

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/parser/ElemDDLLike.cpp
----------------------------------------------------------------------
diff --git a/core/sql/parser/ElemDDLLike.cpp b/core/sql/parser/ElemDDLLike.cpp
index 0bd703e..0dec64f 100644
--- a/core/sql/parser/ElemDDLLike.cpp
+++ b/core/sql/parser/ElemDDLLike.cpp
@@ -120,11 +120,13 @@ ElemDDLLike::displayLabel1() const
 ElemDDLLikeCreateTable::ElemDDLLikeCreateTable(
      const CorrName & sourceTableName,
      ElemDDLNode * pLikeOptions,
+     NABoolean forExtTable,
      CollHeap * h)
 : ElemDDLLike(ELM_LIKE_CREATE_TABLE_ELEM,
               sourceTableName,
               pLikeOptions,
-              h)
+              h),
+  forExtTable_(forExtTable)
 {
   if (pLikeOptions NEQ NULL)
   {

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/parser/ElemDDLLikeCreateTable.h
----------------------------------------------------------------------
diff --git a/core/sql/parser/ElemDDLLikeCreateTable.h 
b/core/sql/parser/ElemDDLLikeCreateTable.h
index 209e501..5eb61ad 100644
--- a/core/sql/parser/ElemDDLLikeCreateTable.h
+++ b/core/sql/parser/ElemDDLLikeCreateTable.h
@@ -64,6 +64,7 @@ public:
   ElemDDLLikeCreateTable(const CorrName & sourceTableName
                          = CorrName("", PARSERHEAP()),
                          ElemDDLNode * pLikeOptions = NULL,
+                         NABoolean forExtTable = FALSE,
                          CollHeap * h=0);
 
   // copy ctor
@@ -96,12 +97,15 @@ public:
   inline const QualifiedName & getDDLLikeCreateTableNameAsQualifiedName() 
const;
   inline       QualifiedName & getDDLLikeCreateTableNameAsQualifiedName();
   
-
+  const NABoolean forExtTable() const { return forExtTable_; }
 
 private:
 
   ParDDLLikeOptsCreateTable likeOptions_;
 
+  // if true, this was created to handle the 'for' clause of an external table
+  NABoolean forExtTable_;
+
 }; // class ElemDDLLikeCreateTable
 
 #endif /* ELEMDDLLIKECREATETABLE_H */

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/parser/StmtDDLCreate.cpp
----------------------------------------------------------------------
diff --git a/core/sql/parser/StmtDDLCreate.cpp 
b/core/sql/parser/StmtDDLCreate.cpp
index 1ff6542..4303d7b 100644
--- a/core/sql/parser/StmtDDLCreate.cpp
+++ b/core/sql/parser/StmtDDLCreate.cpp
@@ -4576,8 +4576,6 @@ StmtDDLCreateTable::synthesize()
       if (pTableDefBody->castToElemDDLLikeCreateTable() NEQ NULL)
        {
          
-         // LIKE clause currently not supported.
-         
          if (isLikeClauseSpec_)
            {
              // Duplicate LIKE clauses.
@@ -4589,6 +4587,21 @@ StmtDDLCreateTable::synthesize()
            ->getDDLLikeNameAsCorrName();
          likeOptions_ = pTableDefBody->castToElemDDLLikeCreateTable()
            ->getLikeOptions();
+
+          if ((NOT isExternal()) &&
+              (pTableDefBody->castToElemDDLLikeCreateTable()->forExtTable()))
+            {
+              *SqlParser_Diags << DgSqlCode(-3242)
+                               << DgString0("'for' clause can only be 
specified when creating an 'external' table.");
+              return;
+            }
+          else if ((isExternal()) &&
+                   (NOT 
pTableDefBody->castToElemDDLLikeCreateTable()->forExtTable()))
+            {
+              *SqlParser_Diags << DgSqlCode(-3242)
+                               << DgString0("'like' clause cannot be specified 
when creating an external table.");
+              return;
+            }
        }
       else
        {
@@ -5007,7 +5020,31 @@ StmtDDLCreateTable_visitTableDefElement(ElemDDLNode * 
pCreateTableNode,
   StmtDDLCreateTable * pCreateTable =
     pCreateTableNode->castToStmtDDLCreateTable();
 
-  if (pElement->castToElemDDLConstraint() NEQ NULL)
+  if (pElement->castToElemDDLLikeCreateTable() NEQ NULL)
+    {
+      pCreateTable->likeSourceTableCorrName_ = 
+        pElement->castToElemDDLLikeCreateTable()
+        ->getDDLLikeNameAsCorrName();
+      pCreateTable->likeOptions_ = 
+        pElement->castToElemDDLLikeCreateTable()
+        ->getLikeOptions();
+
+      if ((NOT pCreateTable->isExternal()) &&
+          (pElement->castToElemDDLLikeCreateTable()->forExtTable()))
+        {
+          *SqlParser_Diags << DgSqlCode(-3242)
+                           << DgString0("'for' clause can only be specified 
when creating an 'external' table.");
+          return;
+        }
+      else if ((pCreateTable->isExternal()) &&
+               (NOT pElement->castToElemDDLLikeCreateTable()->forExtTable()))
+        {
+          *SqlParser_Diags << DgSqlCode(-3242)
+                           << DgString0("'like' clause cannot be specified 
when creating an external table.");
+          return;
+        }
+    }
+  else if (pElement->castToElemDDLConstraint() NEQ NULL)
   {
     //
     // table constraint definition

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/parser/sqlparser.y
----------------------------------------------------------------------
diff --git a/core/sql/parser/sqlparser.y b/core/sql/parser/sqlparser.y
index 2b9d145..318988b 100755
--- a/core/sql/parser/sqlparser.y
+++ b/core/sql/parser/sqlparser.y
@@ -8813,6 +8813,15 @@ string_function :
                    $$ = new (PARSERHEAP()) Repeat ($3, $5);
                  }
 
+     | TOK_REPEAT '(' value_expression ',' value_expression ',' 
NUMERIC_LITERAL_EXACT_NO_SCALE ')'
+                  {
+                    Int64 value = atoInt64($7->data()); 
+                    if (value == 0)
+                      value = -1; 
+                   $$ = new (PARSERHEAP()) Repeat 
+                      ($3, $5, (Int32)value);
+                 }
+
      | TOK_REPLACE '(' value_expression ',' value_expression ',' 
value_expression ')'
                   {
                    $$ = new (PARSERHEAP()) Replace ($3, $5, $7);
@@ -24964,6 +24973,13 @@ create_table_as_token: TOK_AS
 /* type pElemDDL */
 table_definition_body : table_element_list
                       | external_table_definition
+                      | table_element_list external_table_definition
+                        {
+                          $$ = new (PARSERHEAP())
+                            ElemDDLList(
+                                 $1 /*table_elements*/,
+                                 $2 /*table_element*/);
+                        }
 
 /* type pElemDDL */
 table_element_list : '(' table_elements ')'
@@ -25820,6 +25836,7 @@ like_definition : TOK_LIKE source_table 
optional_like_option_list
                                    ElemDDLLikeCreateTable(
                                         *$2 /*source_table*/,
                                          $3 /*optional_like_option_list*/,
+                                        FALSE,
                                         PARSERHEAP());
                                   delete $2 /*source_table*/;
                                 }
@@ -25831,6 +25848,7 @@ external_table_definition : TOK_FOR source_table
                                    ElemDDLLikeCreateTable(
                                         *$2 /*source_table*/,
                                         NULL,
+                                        TRUE, 
                                         PARSERHEAP());
                                   delete $2 /*source_table*/;
                                 }

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/b39f7524/core/sql/regress/hive/EXPECTED005
----------------------------------------------------------------------
diff --git a/core/sql/regress/hive/EXPECTED005 
b/core/sql/regress/hive/EXPECTED005
index f6bca8f..0ad92b1 100644
--- a/core/sql/regress/hive/EXPECTED005
+++ b/core/sql/regress/hive/EXPECTED005
@@ -432,10 +432,10 @@ ID           CHAPTER                    ENGLISH           
         TRANSLATOR
 >>
 >>select * from tbl_type;
 
-TINT    SM      I            BIG                   STR                        
F                D                          T                           DT      
    VC
-------  ------  -----------  --------------------  -------------------------  
---------------  -------------------------  --------------------------  
----------  ----------------------------------------
+TINT  SM      I            BIG                   STR                        F  
              D                          T                           DT         
 VC
+----  ------  -----------  --------------------  -------------------------  
---------------  -------------------------  --------------------------  
----------  ----------------------------------------
 
-   101     202          203                   204  two hundred                 
2.0000000E+002   2.00000000000000000E+002  2022-02-02 22:22:22.222222  
2022-02-02  varchar
+ 101     202          203                   204  two hundred                 
2.0000000E+002   2.00000000000000000E+002  2022-02-02 22:22:22.222222  
2022-02-02  varchar                                 
 
 --- 1 row(s) selected.
 >>insert into tbl_type_temp select * from tbl_type;
@@ -443,10 +443,10 @@ TINT    SM      I            BIG                   STR    
                    F
 --- 1 row(s) inserted.
 >>select * from tbl_type_temp;
 
-TINT    SM      I            BIG                   STR                        
F                D                          T                           DT      
    VC
-------  ------  -----------  --------------------  -------------------------  
---------------  -------------------------  --------------------------  
----------  ----------------------------------------
+TINT  SM      I            BIG                   STR                        F  
              D                          T                           DT         
 VC
+----  ------  -----------  --------------------  -------------------------  
---------------  -------------------------  --------------------------  
----------  ----------------------------------------
 
-   101     202          203                   204  two hundred                 
2.0000000E+002   2.00000000000000000E+002  2022-02-02 22:22:22.222222  
2022-02-02  varchar
+ 101     202          203                   204  two hundred                 
2.0000000E+002   2.00000000000000000E+002  2022-02-02 22:22:22.222222  
2022-02-02  varchar                                 
 
 --- 1 row(s) selected.
 >>

Reply via email to