Repository: incubator-trafodion
Updated Branches:
  refs/heads/master fabfc80d7 -> b2fe5fd7a


http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/7b99938c/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp 
b/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp
index 8f53cc9..2d97501 100644
--- a/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp
+++ b/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp
@@ -2799,6 +2799,67 @@ short CmpSeabaseDDL::getTypeInfo(const NAType * naType,
   return 0;
 }
 
+short CmpSeabaseDDL::getNAColumnFromColDef
+(ElemDDLColDef * colNode,
+ NAColumn* &naCol)
+{
+  NAString colFamily;
+  NAString colName;
+  Lng32 datatype, length, precision, scale, dt_start, dt_end;
+  Lng32 nullable, upshifted;
+  ComColumnClass colClass;
+  ComColumnDefaultClass defaultClass;
+  NAString charset, defVal;
+  NAString heading;
+  ULng32 hbaseColFlags;
+  Int64 colFlags;
+  LobsStorage lobStorage;
+  NABoolean alignedFormat = FALSE;
+  if (getColInfo(colNode,
+                 colFamily,
+                 colName,
+                 alignedFormat,
+                 datatype, length, precision, scale, dt_start, dt_end, 
+                 upshifted, nullable,
+                 charset, colClass, defaultClass, defVal, heading, lobStorage, 
+                 hbaseColFlags, colFlags))
+    {
+     *CmpCommon::diags() << DgSqlCode(-2004);
+     return -1;
+    }
+
+  NAType * naType = colNode->getColumnDataType();
+  if (! naType)
+    {
+      *CmpCommon::diags() << DgSqlCode(-2004);
+      return -1;
+    }
+
+  char * defV = NULL;
+  if ((defaultClass != COM_NO_DEFAULT) &&
+      (! defVal.isNull()))
+    {
+      char * data = (char*) defVal.data();
+      Lng32 len = defVal.length();
+      defV = new(STMTHEAP) char[len + 2];
+      str_cpy_all((char*)defV, data, len);
+      char * c = (char*)defV;
+      c[len] = 0;
+      c[len+1] = 0;
+    }
+
+  naCol = new(STMTHEAP) NAColumn(colNode->getColumnName().data(),
+                                 -1, // position
+                                 naType, NULL, NULL,
+                                 USER_COLUMN, //colClass, 
+                                 defaultClass,
+                                 defV);
+
+  naCol->setHbaseColFlags(hbaseColFlags);
+
+  return 0;
+}
+
 short CmpSeabaseDDL::getColInfo(ElemDDLColDef * colNode, 
                                 NAString &colFamily,
                                 NAString &colName,
@@ -7801,6 +7862,95 @@ void CmpSeabaseDDL::updateVersion()
 
 }
 
+// this method truncates an hbase table by dropping it and then recreating
+// it. Options that were used for the original hbase table are stored in
+// traf metadata and are passed in to hbase during table create.
+// When hbase truncate api is available (HBAse 1.0 and later), then this
+// method will call it instead of drop/recreate.
+short CmpSeabaseDDL::truncateHbaseTable(const NAString &catalogNamePart, 
+                                        const NAString &schemaNamePart, 
+                                        const NAString &objectNamePart,
+                                        NATable * naTable,
+                                        ExpHbaseInterface * ehi)
+{
+  Lng32 retcode = 0;
+
+  const NAString extNameForHbase = 
+    catalogNamePart + "." + schemaNamePart + "." + objectNamePart;
+
+  HbaseStr hbaseTable;
+  hbaseTable.val = (char*)extNameForHbase.data();
+  hbaseTable.len = extNameForHbase.length();
+
+  // drop this table from hbase
+  retcode = dropHbaseTable(ehi, &hbaseTable, FALSE);
+  if (retcode)
+    {
+      deallocEHI(ehi); 
+      
+      processReturn();
+      
+      return -1;
+    }
+
+  // and recreate it.
+  NAFileSet * naf = naTable->getClusteringIndex();
+
+  NAList<HbaseCreateOption*> * hbaseCreateOptions = 
+    naTable->hbaseCreateOptions();
+  Lng32 numSaltPartns = naf->numSaltPartns();
+  Lng32 numSaltSplits = numSaltPartns - 1;
+  Lng32 numSplits = 0;
+  const Lng32 numKeys = naf->getIndexKeyColumns().entries();
+  Lng32 keyLength = naf->getKeyLength();
+  char ** encodedKeysBuffer = NULL;
+
+  const desc_struct * tableDesc = naTable->getTableDesc();
+  desc_struct * colDescs = tableDesc->body.table_desc.columns_desc; 
+  desc_struct * keyDescs = (desc_struct*)naf->getKeysDesc();
+
+  if (createEncodedKeysBuffer(encodedKeysBuffer/*out*/,
+                              numSplits/*out*/,
+                              colDescs, keyDescs,
+                              numSaltPartns,
+                              numSaltSplits,
+                              NULL,
+                              numKeys, 
+                              keyLength,
+                              FALSE))
+    {
+      deallocEHI(ehi); 
+
+      processReturn();
+      
+      return -1;
+    }
+  
+  std::vector<NAString> userColFamVec;
+  std::vector<NAString> trafColFamVec;
+  NAString outColFam;
+  for (int i = 0; i < naTable->allColFams().entries(); i++)
+    {
+      processColFamily(naTable->allColFams()[i], outColFam,
+                       &userColFamVec, &trafColFamVec);
+    } // for
+  
+  retcode = createHbaseTable(ehi, &hbaseTable, trafColFamVec,
+                             hbaseCreateOptions,
+                             numSplits, keyLength, 
+                             encodedKeysBuffer);
+  if (retcode == -1)
+    {
+      deallocEHI(ehi); 
+
+      processReturn();
+
+      return -1;
+    }
+
+  return 0;
+}
+
 void CmpSeabaseDDL::purgedataHbaseTable(DDLExpr * ddlExpr,
                                        NAString &currCatName, NAString 
&currSchName)
 {
@@ -7966,14 +8116,9 @@ void CmpSeabaseDDL::purgedataHbaseTable(DDLExpr * 
ddlExpr,
     }
                                  
   NABoolean asyncDrop = (CmpCommon::getDefault(HBASE_ASYNC_DROP_TABLE) == 
DF_ON);
-
-  HbaseStr hbaseTable;
-  hbaseTable.val = (char*)extNameForHbase.data();
-  hbaseTable.len = extNameForHbase.length();
-
-  // drop this table from hbase
-  retcode = dropHbaseTable(ehi, &hbaseTable, FALSE);
-  if (retcode)
+  
+  if (truncateHbaseTable(catalogNamePart, schemaNamePart, objectNamePart,
+                         naTable, ehi))
     {
       deallocEHI(ehi); 
       
@@ -7982,60 +8127,6 @@ void CmpSeabaseDDL::purgedataHbaseTable(DDLExpr * 
ddlExpr,
       return;
     }
 
-  // and recreate it.
-  NAFileSet * naf = naTable->getClusteringIndex();
-
-  NAList<HbaseCreateOption*> * hbaseCreateOptions = 
naTable->hbaseCreateOptions();
-  Lng32 numSaltPartns = naf->numSaltPartns();
-  Lng32 numSaltSplits = numSaltPartns - 1;
-  Lng32 numSplits = 0;
-  Lng32 numKeys = naf->getIndexKeyColumns().entries();
-  Lng32 keyLength = naf->getKeyLength();
-  char ** encodedKeysBuffer = NULL;
-
-  const desc_struct * tableDesc = naTable->getTableDesc();
-  desc_struct * colDescs = tableDesc->body.table_desc.columns_desc; 
-  desc_struct * keyDescs = (desc_struct*)naf->getKeysDesc();
-
-  if (createEncodedKeysBuffer(encodedKeysBuffer/*out*/,
-                              numSplits/*out*/,
-                              colDescs, keyDescs,
-                              numSaltPartns,
-                              numSaltSplits,
-                              NULL,
-                              numKeys, 
-                              keyLength,
-                              FALSE))
-    {
-      deallocEHI(ehi); 
-
-      processReturn();
-      
-      return;
-    }
-  
-  std::vector<NAString> userColFamVec;
-  std::vector<NAString> trafColFamVec;
-  NAString outColFam;
-  for (int i = 0; i < naTable->allColFams().entries(); i++)
-    {
-      processColFamily(naTable->allColFams()[i], outColFam,
-                       &userColFamVec, &trafColFamVec);
-    } // for
-  
-  retcode = createHbaseTable(ehi, &hbaseTable, trafColFamVec,
-                             hbaseCreateOptions,
-                             numSplits, keyLength, 
-                             encodedKeysBuffer);
-  if (retcode == -1)
-    {
-      deallocEHI(ehi); 
-
-      processReturn();
-
-      return;
-    }
-
   if (naTable->hasSecondaryIndexes()) // user indexes
     {
       const NAFileSetList &indexList = naTable->getIndexList();
@@ -8190,6 +8281,8 @@ short CmpSeabaseDDL::executeSeabaseDDL(DDLExpr * ddlExpr, 
ExprNode * ddlNode,
         (ddlNode->getOperatorType() == DDL_POPULATE_INDEX) ||
         (ddlNode->getOperatorType() == DDL_CREATE_TABLE) ||
         (ddlNode->getOperatorType() == DDL_ALTER_TABLE_DROP_COLUMN) ||
+        (ddlNode->getOperatorType() == DDL_ALTER_TABLE_ALTER_COLUMN_DATATYPE) 
||
+
         (ddlNode->getOperatorType() == DDL_DROP_TABLE))))
     {
       // transaction will be started and commited in called methods.

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/7b99938c/core/sql/sqlcomp/CmpSeabaseDDLschema.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/CmpSeabaseDDLschema.cpp 
b/core/sql/sqlcomp/CmpSeabaseDDLschema.cpp
index 0e0bf64..5b3c8c3 100644
--- a/core/sql/sqlcomp/CmpSeabaseDDLschema.cpp
+++ b/core/sql/sqlcomp/CmpSeabaseDDLschema.cpp
@@ -100,6 +100,8 @@ static bool transferObjectPrivs(
 // *                                                                           
*
 // *   0: Schema was added                                                     
 *
 // *  -1: Schema was not added.  A CLI error is put into the diags area.       
*
+// *   1: Schema already exists and ignoreIfExists is specified.               
*
+// *      No error is added to the diags area.                                 
*
 // *                                                                           
*
 // 
*****************************************************************************
 int CmpSeabaseDDL::addSchemaObject(
@@ -136,7 +138,7 @@ Lng32 retcode = 
existsInSeabaseMDTable(&cliInterface,catalogName,schemaNamePart,
    if (retcode == 1 ) // already exists
    {
       if (ignoreIfExists)
-        return 0;
+        return 1;
       else
         *CmpCommon::diags() << DgSqlCode(-CAT_SCHEMA_ALREADY_EXISTS)
                             << 
DgSchemaName(schemaName.getExternalName().data());

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/7b99938c/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp 
b/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp
index 7a0498d..9ebf15e 100644
--- a/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp
+++ b/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp
@@ -66,11 +66,34 @@ extern short CmpDescribeSeabaseTable (
                              NABoolean withoutDivisioning = FALSE,
                              NABoolean noTrailingSemi = FALSE,
 
-                             // used to add or remove column definition from 
col list.
-                             // valid for 'createLike' mode. Used for 'alter 
add/drop col'.
+                             // used to add,rem,alter column definition from 
col list.
+                             // valid for 'createLike' mode. 
+                             // Used for 'alter add/drop/alter col'.
                              char * colName = NULL,
-                             NABoolean isAdd = FALSE,
-                             const NAColumn * nacol = NULL);
+                             short ada = 0, // 0,add. 1,drop. 2,alter
+                             const NAColumn * nacol = NULL,
+                             const NAType * natype = NULL);
+
+// type:  1, invoke. 2, showddl. 3, create_like
+extern short cmpDisplayColumn(const NAColumn *nac,
+                              char * inColName,
+                              const NAType *inNAT,
+                              short displayType,
+                              Space *inSpace,
+                              char * buf,
+                              Lng32 &ii,
+                              NABoolean namesOnly,
+                              NABoolean &identityCol,
+                              NABoolean isExternalTable,
+                              NABoolean isAlignedRowFormat);
+
+extern short cmpDisplayPrimaryKey(const NAColumnArray & naColArr,
+                                  Lng32 numKeys,
+                                  NABoolean displaySystemCols,
+                                  Space &space, char * buf, 
+                                  NABoolean displayCompact,
+                                  NABoolean displayAscDesc,
+                                  NABoolean displayParens);
                              
 static bool checkSpecifiedPrivs(
    ElemDDLPrivActArray & privActsArray,  
@@ -232,8 +255,6 @@ desc_struct * 
CmpSeabaseDDL::convertVirtTableKeyInfoArrayToDescStructs(
   return first_key_desc;
 }
 
-
-
 void CmpSeabaseDDL::createSeabaseTableLike(
                                            StmtDDLCreateTable * 
createTableNode,
                                            NAString &currCatName, NAString 
&currSchName)
@@ -286,16 +307,19 @@ void CmpSeabaseDDL::createSeabaseTableLike(
         }
 
       keyClause += ")";
+    }
+
+  // Check for other common options that are currently not supported
+  // with CREATE TABLE LIKE. Those could all be passed into
+  // CmpDescribeSeabaseTable as strings if we wanted to support them.
 
-      // NOTE: This is not currently supported
+  if (NOT keyClause.isNull())
+    {
       *CmpCommon::diags() << DgSqlCode(-3111)
                           << DgString0("PRIMARY KEY/STORE BY");
       return;
     }
 
-  // Check for other common options that are currently not supported
-  // with CREATE TABLE LIKE. Those could all be passed into
-  // CmpDescribeSeabaseTable as strings if we wanted to support them.
   if (createTableNode->isPartitionSpecified() ||
       createTableNode->isPartitionBySpecified())
     {
@@ -4053,154 +4077,26 @@ void CmpSeabaseDDL::alterSeabaseTableHBaseOptions(
   return;
 }
 
-/////////////////////////////////////////////////////////////////////
-// currTab:          table on which column is being added to or dropped from
-// newTempTab:  temporary table with new definition
-// currTempTab:  name of table that currTab that will be renamed to 
-//
-// Steps:
-//   create newTempTab based on currTab and added/dropped column
-//   insert data into newTempTab from currTab
-//   rename currTab to currTempTab
-//   rename newTempTab to currTab
-//   drop currTempTab
-//
-/////////////////////////////////////////////////////////////////////
-short CmpSeabaseDDL::alignedFormatTableAddDropColumn
-(
- Int64 objUID,
- NABoolean isAdd,
- const NAString &catalogNamePart,
- const NAString &schemaNamePart,
- const NAString &objectNamePart,
- char * colName, const NAColumn * nacol)
+short CmpSeabaseDDL::createSeabaseTableLike2(
+     CorrName &cn,
+     const NAString &likeTableName,
+     NABoolean withPartns,
+     NABoolean withoutSalt,
+     NABoolean withoutDivision)
 {
-  Lng32 cliRC = 0;
   Lng32 retcode = 0;
 
-  ExeCliInterface cliInterface(STMTHEAP, NULL, NULL, 
-                               
CmpCommon::context()->sqlSession()->getParentQid());
-
-  NABoolean xnWasStartedHere = FALSE;
-  Queue * usingViewsQueue = NULL;
-  cliRC = getUsingViews(&cliInterface, objUID, usingViewsQueue);
-  if (cliRC < 0)
-    {
-      processReturn();
-      
-      return -1;
-    }
-
-  if (usingViewsQueue->numEntries() > 0)
-    {
-      if (beginXnIfNotInProgress(&cliInterface, xnWasStartedHere))
-        return -1;
-    }
-
-  NAList<NAString> viewNameList(STMTHEAP, usingViewsQueue->numEntries());
-  NAList<NAString> viewDefnList(STMTHEAP, usingViewsQueue->numEntries());
-
-  // create temp table based on the current table DDL and added/dropped column.
-  // add/drop col name is colName.
-  // Added col defn is contained in nacol.
-  NAString newTempTab; 
-  ComDeriveRandomInternalName ( ComGetNameInterfaceCharSet(),
-                                objectNamePart, newTempTab, STMTHEAP);
-
-  char newTempTabStr[1000];
-  str_sprintf(newTempTabStr, "%s.\"%s\".%s", 
-              catalogNamePart.data(), schemaNamePart.data(), 
newTempTab.data());
-
-  CorrName newTempTabCN(newTempTab,
-                        STMTHEAP, schemaNamePart, catalogNamePart);
-
-  // current table and curr temp table
-  char currTabStr[1000];
-  str_sprintf(currTabStr, "%s.\"%s\".%s", 
-              catalogNamePart.data(), schemaNamePart.data(), 
objectNamePart.data());
-
-  CorrName currTabCN(objectNamePart, STMTHEAP, 
-                     schemaNamePart, catalogNamePart);
-
-  NAString currTempTab; 
-  ComDeriveRandomInternalName ( ComGetNameInterfaceCharSet(),
-                                objectNamePart, currTempTab, STMTHEAP);
-  
-  char currTempTabStr[1000];
-  str_sprintf(currTempTabStr, "%s.\"%s\".%s", 
-              catalogNamePart.data(), schemaNamePart.data(), 
currTempTab.data());
-
-
-  // create DDL for newTempTab  
   char * buf = NULL;
   ULng32 buflen = 0;
-  retcode = CmpDescribeSeabaseTable(currTabCN, 3/*createlike*/, buf, buflen, 
-                                    STMTHEAP,
+  retcode = CmpDescribeSeabaseTable(cn, 3/*createlike*/, buf, buflen, STMTHEAP,
                                     NULL,
-                                    FALSE, FALSE, FALSE,
-                                    TRUE,
-                                    colName, isAdd, nacol);
+                                    withPartns, withoutSalt, withoutDivision,
+                                    TRUE);
   if (retcode)
     return -1;
 
-  // find out any views on this table.
-  // save their definition and drop them.
-  // they will be recreated before return.
-  usingViewsQueue->position();
-  for (int idx = 0; idx < usingViewsQueue->numEntries(); idx++)
-    {
-      OutputInfo * vi = (OutputInfo*)usingViewsQueue->getNext(); 
-      char * viewName = vi->get(0);
-      
-      viewNameList.insert(viewName);
-
-      ComObjectName viewCO(viewName, COM_TABLE_NAME);
-
-      const NAString catName = viewCO.getCatalogNamePartAsAnsiString();
-      const NAString schName = viewCO.getSchemaNamePartAsAnsiString(TRUE);
-      const NAString objName = viewCO.getObjectNamePartAsAnsiString(TRUE);
-
-      Int64 viewUID = getObjectUID(&cliInterface,
-                                   catName.data(), schName.data(), 
objName.data(), 
-                                   COM_VIEW_OBJECT_LIT);
-      if (viewUID < 0 )
-        {
-          endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1);
-          
-          return -1;
-        }
-
-      NAString viewText;
-      if (getTextFromMD(&cliInterface, viewUID, COM_VIEW_TEXT, 0, viewText))
-        {
-          endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1);
-          
-          return -1;
-        }
-
-      viewDefnList.insert(viewText);
-
-      if (dropOneTableorView(cliInterface,viewName,COM_VIEW_OBJECT,false))
-        {
-          endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1);
-  
-          processReturn();
-          
-          return -1;
-        }
-    }
-
-  endXnIfStartedHere(&cliInterface, xnWasStartedHere, 0);
-  
-  BindWA bindWA(ActiveSchemaDB(), CmpCommon::context(), FALSE);
-  NATable * naTable = NULL;
-
-  NAString colNames;
-
-  char queryBuf[1000];
-
   NAString query = "create table ";
-  query += newTempTabStr;
+  query += likeTableName;
   query += " ";
 
   NABoolean done = FALSE;
@@ -4218,157 +4114,108 @@ short CmpSeabaseDDL::alignedFormatTableAddDropColumn
         done = TRUE;
     }
 
-  cliRC = cliInterface.executeImmediate((char*)query.data());
-  if (cliRC < 0)
-    {
-      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
-      goto label_error;
-    }
-
-  //  naTable = bindWA.getNATable((isAdd ? currTabCN : newTempTabCN));
-  naTable = bindWA.getNATable(newTempTabCN);
-  if (! naTable)
-    {
-      goto label_error;
-    }
+  query += ";";
 
-  // update metadata to change column type to 'A'(added)
-  if (isAdd)
-    {
-      str_sprintf(queryBuf, "update %s.\"%s\".%s set column_class = 'A' where 
object_uid = %Ld and column_name = '%s' ",
-                  getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
-                  naTable->objectUid().castToInt64(), colName);
-      
-      cliRC = cliInterface.executeImmediate(queryBuf);
-      if (cliRC < 0)
-        {
-          cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
-          goto label_error;
-        }
-      
-    }
+  // send any user CQDs down 
+  Lng32 retCode = sendAllControls(FALSE, FALSE, TRUE);
 
-  // insert data from current table into new temp table.
-  //  query = "upsert using load into ";
-  query = "insert into ";
-  query += newTempTabStr;
-  query += " ";
+  ExeCliInterface cliInterface(STMTHEAP, NULL, NULL, 
+  CmpCommon::context()->sqlSession()->getParentQid());
 
-  for (Lng32 i = 0; i < naTable->getNAColumnArray().entries(); i++)
+  Lng32 cliRC = 0;
+  cliRC = cliInterface.executeImmediate((char*)query.data());
+  if (cliRC < 0)
     {
-      const NAColumn *nac = naTable->getNAColumnArray()[i];
-
-      if (nac->isSystemColumn())
-        continue;
- 
-      if ((isAdd) && (colName == nac->getColName()))
-        continue;
-
-      colNames += nac->getColName();
-
-      colNames += ",";
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      return -1;
     }
 
-  // remove last comma
-  colNames = colNames.strip(NAString::trailing, ',');
-
-  query += "(" + colNames + ")";
+  return 0;
+}
 
-  query += " select ";
-  query += colNames;
+short CmpSeabaseDDL::cloneHbaseTable(
+     const NAString &srcTable, const NAString &clonedTable,
+     ExpHbaseInterface * inEHI)
+{
+  HbaseStr hbaseTable;
+  hbaseTable.val = (char*)srcTable.data();
+  hbaseTable.len = srcTable.length();
 
-  query += " from ";
-  query += currTabStr;
+  HbaseStr clonedHbaseTable;
+  clonedHbaseTable.val = (char*)clonedTable.data();
+  clonedHbaseTable.len = clonedTable.length();
 
-  query += ";";
+  ExpHbaseInterface * ehi = (inEHI ? inEHI : allocEHI());
 
-  cliRC = cliInterface.executeImmediate(query.data());
-  if (cliRC < 0)
+  // copy hbaseTable as clonedHbaseTable
+  if (ehi->copy(hbaseTable, clonedHbaseTable, TRUE))
     {
-      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      if (! inEHI)
+        deallocEHI(ehi); 
       
       processReturn();
-
-      goto label_error;
-    }
-  
-  // rename current table to temp
-  str_sprintf(queryBuf, "alter table \"%s\".\"%s\".\"%s\" rename to \"%s\" ",
-              catalogNamePart.data(), schemaNamePart.data(), 
objectNamePart.data(),
-              currTempTab.data());
-  
-  cliRC = cliInterface.executeImmediate(queryBuf);
-  if (cliRC < 0)
-    {
-      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
       
-      processReturn();
-
-      goto label_error;
+      return -1;
     }
 
-  // rename new temp table to current table
-  str_sprintf(queryBuf, "alter table \"%s\".\"%s\".\"%s\" rename to \"%s\" ",
-              catalogNamePart.data(), schemaNamePart.data(), newTempTab.data(),
-              objectNamePart.data());
-  
-  cliRC = cliInterface.executeImmediate(queryBuf);
-  if (cliRC < 0)
-    {
-      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
-      
-      processReturn();
+  if (! inEHI)
+    deallocEHI(ehi); 
+ 
+  return 0;
+}
 
-      goto label_error1;
-    }
+short CmpSeabaseDDL::cloneSeabaseTable(
+     CorrName &cn,
+     const NATable * naTable,
+     const NAString &clonedTableName,
+     ExpHbaseInterface * inEHI,
+     ExeCliInterface * cliInterface)
+{
+  Lng32 cliRC = 0;
+  Lng32 retcode = 0;
 
-  // drop curr temp table
-  str_sprintf(queryBuf, "drop table \"%s\".\"%s\".\"%s\" ",
-              catalogNamePart.data(), schemaNamePart.data(), 
currTempTab.data(),
-              objectNamePart.data());
-  
-  cliRC = cliInterface.executeImmediate(queryBuf);
+  retcode = createSeabaseTableLike2(cn, clonedTableName);
+  if (retcode)
+    return -1;
+
+  ComObjectName tableName(clonedTableName, COM_TABLE_NAME);
+  const NAString catalogNamePart = tableName.getCatalogNamePartAsAnsiString();
+  const NAString schemaNamePart = 
tableName.getSchemaNamePartAsAnsiString(TRUE);
+  const NAString objectNamePart = 
tableName.getObjectNamePartAsAnsiString(TRUE);
+  Int64 clonedTableUID = 
+    getObjectUID
+    (cliInterface,
+     catalogNamePart.data(), schemaNamePart.data(), objectNamePart.data(),
+     COM_BASE_TABLE_OBJECT_LIT);
+ 
+  char buf[2000];
+  str_sprintf(buf, "merge into %s.\"%s\".%s using (select column_name, 
column_class from %s.\"%s\".%s where object_uid = %Ld) x on (object_uid = %Ld 
and column_name = x.column_name) when matched then update set column_class = 
x.column_class;",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+              naTable->objectUid().castToInt64(),
+              clonedTableUID);
+  cliRC = cliInterface->executeImmediate(buf);
   if (cliRC < 0)
     {
-      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
-      
+      cliInterface->retrieveSQLDiagnostics(CmpCommon::diags());
       processReturn();
 
-      goto label_error0;
+      return -1;
     }
+ 
+  const NAString extNameForHbase = 
+    cn.getQualifiedNameObj().getCatalogName() + "." + 
+    cn.getQualifiedNameObj().getSchemaName() + "." + 
+    cn.getQualifiedNameObj().getObjectName();
 
-   if (recreateViews(cliInterface, viewNameList, viewDefnList))
+  if (cloneHbaseTable(extNameForHbase, clonedTableName, inEHI))
     {
+      processReturn();
+      
       return -1;
     }
-
-  return 0;
-
- label_error1:
-  // rename current temp table to current
-  str_sprintf(queryBuf, "alter table \"%s\".\"%s\".\"%s\" rename to \"%s\" ",
-              catalogNamePart.data(), schemaNamePart.data(), 
currTempTab.data(),
-              objectNamePart.data());
   
-  cliRC = cliInterface.executeImmediate(queryBuf);
-
- label_error:
-  cleanupObjectAfterError(cliInterface,
-                          catalogNamePart, schemaNamePart, newTempTab,
-                          COM_BASE_TABLE_OBJECT);
-
-  recreateViews(cliInterface, viewNameList, viewDefnList);
-
-  return -1;
-
- label_error0:
-  cleanupObjectAfterError(cliInterface,
-                          catalogNamePart, schemaNamePart, currTempTab,
-                          COM_BASE_TABLE_OBJECT);
-
-  recreateViews(cliInterface, viewNameList, viewDefnList);
-
-  return -1;
+  return 0;
 }
 
 short CmpSeabaseDDL::recreateViews(ExeCliInterface &cliInterface,
@@ -4626,11 +4473,97 @@ void CmpSeabaseDDL::alterSeabaseTableAddColumn(
     }
 
   Int64 objUID = naTable->objectUid().castToInt64();
+  Int32 newColNum = naTable->getColumnCount();
+  for (Int32 cc = nacolArr.entries()-1; cc >= 0; cc--)
+    {
+      const NAColumn *nac = nacolArr[cc];
+
+      if ((NOT naTable->isSQLMXAlignedTable()) &&
+          (nac->isComputedColumn()))
+        {
+          str_sprintf(query, "update %s.\"%s\".%s set column_number = 
column_number + 1 where object_uid = %Ld and column_number = %d",
+                      getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+                      objUID,
+                      nac->getPosition());
+          
+          cliRC = cliInterface.executeImmediate(query);
+          if (cliRC < 0)
+            {
+              cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+              
+              return;
+            }
+
+          str_sprintf(query, "update %s.\"%s\".%s set column_number = 
column_number + 1 where object_uid = %Ld and column_number = %d",
+                      getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_KEYS,
+                      objUID,
+                      nac->getPosition());
+          
+          cliRC = cliInterface.executeImmediate(query);
+          if (cliRC < 0)
+            {
+              cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+              
+              return;
+            }
+
+          str_sprintf(query, "update %s.\"%s\".%s set sub_id = sub_id + 1 
where text_uid = %Ld and text_type = %d and sub_id = %d",
+                      getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_TEXT,
+                      objUID,
+                      COM_COMPUTED_COL_TEXT,
+                      nac->getPosition());
+          
+          cliRC = cliInterface.executeImmediate(query);
+          if (cliRC < 0)
+            {
+              cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+              
+              return;
+            }
+
+          // keys for indexes refer to base table column number.
+          // modify it so they now refer to new column numbers.
+          if (naTable->hasSecondaryIndexes())
+            {
+              const NAFileSetList &naFsList = naTable->getIndexList();
+              
+              for (Lng32 i = 0; i < naFsList.entries(); i++)
+                {
+                  const NAFileSet * naFS = naFsList[i];
+                  
+                  // skip clustering index
+                  if (naFS->getKeytag() == 0)
+                    continue;
+                  
+                  const QualifiedName &indexName = naFS->getFileSetName();
+                  
+                  str_sprintf(query, "update %s.\"%s\".%s set column_number = 
column_number + 1  where column_number = %d and object_uid = (select object_uid 
from %s.\"%s\".%s where catalog_name = '%s' and schema_name = '%s' and 
object_name = '%s' and object_type = 'IX') ",
+                              getSystemCatalog(), SEABASE_MD_SCHEMA, 
SEABASE_KEYS,
+                              nac->getPosition(),
+                              getSystemCatalog(), SEABASE_MD_SCHEMA, 
SEABASE_OBJECTS,
+                              indexName.getCatalogName().data(),
+                              indexName.getSchemaName().data(),
+                              indexName.getObjectName().data());
+                  cliRC = cliInterface.executeImmediate(query);
+                  if (cliRC < 0)
+                    {
+                      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+                      
+                      goto label_return;
+                    }
+                  
+                } // for
+            } // secondary indexes present
+          
+          newColNum--;
+        }
+    }
+
   str_sprintf(query, "insert into %s.\"%s\".%s values (%Ld, '%s', %d, '%s', 
%d, '%s', %d, %d, %d, %d, %d, '%s', %d, %d, '%s', %d, '%s', '%s', '%s', '%u', 
'%s', '%s', %Ld )",
               getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
               objUID,
               col_name,
-              naTable->getColumnCount(), 
+              newColNum, //naTable->getColumnCount(), 
               COM_ADDED_USER_COLUMN_LIT,
               datatype,
               getAnsiTypeStrFromFSType(datatype),
@@ -4756,36 +4689,144 @@ void CmpSeabaseDDL::alterSeabaseTableAddColumn(
   return;
 }
 
-void CmpSeabaseDDL::alterSeabaseTableDropColumn(
-                                                StmtDDLAlterTableDropColumn * 
alterDropColNode,
-                                                NAString &currCatName, 
NAString &currSchName)
+short CmpSeabaseDDL::updateMDforDropCol(ExeCliInterface &cliInterface,
+                                        const NATable * naTable,
+                                        Lng32 dropColNum)
 {
   Lng32 cliRC = 0;
-  Lng32 retcode = 0;
-
-  const NAString &tabName = alterDropColNode->getTableName();
-
-  ComObjectName tableName(tabName, COM_TABLE_NAME);
-  ComAnsiNamePart currCatAnsiName(currCatName);
-  ComAnsiNamePart currSchAnsiName(currSchName);
-  tableName.applyDefaults(currCatAnsiName, currSchAnsiName);
-
-  const NAString catalogNamePart = tableName.getCatalogNamePartAsAnsiString();
-  const NAString schemaNamePart = 
tableName.getSchemaNamePartAsAnsiString(TRUE);
-  const NAString objectNamePart = 
tableName.getObjectNamePartAsAnsiString(TRUE);
-  const NAString extTableName = tableName.getExternalName(TRUE);
-  const NAString extNameForHbase = catalogNamePart + "." + schemaNamePart + 
"." + objectNamePart;
 
-  ExeCliInterface cliInterface(STMTHEAP, NULL, NULL, 
-  CmpCommon::context()->sqlSession()->getParentQid());
+  Int64 objUID = naTable->objectUid().castToInt64();
 
-  if ((isSeabaseReservedSchema(tableName)) &&
-      (!Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL)))
+  char buf[4000];
+  str_sprintf(buf, "delete from %s.\"%s\".%s where object_uid = %Ld and 
column_number = %d",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+              objUID,
+              dropColNum);
+  
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
     {
-      *CmpCommon::diags() << 
DgSqlCode(-CAT_CANNOT_ALTER_DEFINITION_METADATA_SCHEMA);
-      processReturn();
-      return;
-    }
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      return -1;
+    }
+  
+  str_sprintf(buf, "update %s.\"%s\".%s set column_number = column_number - 1 
where object_uid = %Ld and column_number >= %d",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+              objUID,
+              dropColNum);
+  
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      return -1;
+    }
+  
+  str_sprintf(buf, "update %s.\"%s\".%s set column_number = column_number - 1 
where object_uid = %Ld and column_number >= %d",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_KEYS,
+              objUID,
+              dropColNum);
+  
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      return -1;
+    }
+  
+  str_sprintf(buf, "update %s.\"%s\".%s set sub_id = sub_id - 1 where text_uid 
= %Ld and text_type = %d and sub_id > %d",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_TEXT,
+              objUID,
+              COM_COMPUTED_COL_TEXT,
+              dropColNum);
+  
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      return -1;
+    }
+  
+  // keys for pkey constraint refer to base table column number.
+  // modify it so they now refer to new column numbers.
+  str_sprintf(buf, "update %s.\"%s\".%s K set column_number = column_number - 
1  where K.column_number >= %d and K.object_uid = (select C.constraint_uid from 
%s.\"%s\".%s C where C.table_uid = %Ld and C.constraint_type = 'P')",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_KEYS,
+              dropColNum,
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_TABLE_CONSTRAINTS,
+              objUID);
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      return -1;
+    }
+  
+  // keys for indexes refer to base table column number.
+  // modify it so they now refer to new column numbers.
+  if (naTable->hasSecondaryIndexes())
+    {
+      const NAFileSetList &naFsList = naTable->getIndexList();
+      
+      for (Lng32 i = 0; i < naFsList.entries(); i++)
+        {
+          const NAFileSet * naFS = naFsList[i];
+          
+          // skip clustering index
+          if (naFS->getKeytag() == 0)
+            continue;
+          
+          const QualifiedName &indexName = naFS->getFileSetName();
+          
+          str_sprintf(buf, "update %s.\"%s\".%s set column_number = 
column_number - 1  where column_number >=  %d and object_uid = (select 
object_uid from %s.\"%s\".%s where catalog_name = '%s' and schema_name = '%s' 
and object_name = '%s' and object_type = 'IX') ",
+                      getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_KEYS,
+                      dropColNum,
+                      getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_OBJECTS,
+                      indexName.getCatalogName().data(),
+                      indexName.getSchemaName().data(),
+                      indexName.getObjectName().data());
+          cliRC = cliInterface.executeImmediate(buf);
+          if (cliRC < 0)
+            {
+              cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+              return -1;
+            }
+          
+        } // for
+    } // secondary indexes present
+
+  return 0;
+}
+
+void CmpSeabaseDDL::alterSeabaseTableDropColumn(
+                                                StmtDDLAlterTableDropColumn * 
alterDropColNode,
+                                                NAString &currCatName, 
NAString &currSchName)
+{
+  Lng32 cliRC = 0;
+  Lng32 retcode = 0;
+
+  const NAString &tabName = alterDropColNode->getTableName();
+
+  ComObjectName tableName(tabName, COM_TABLE_NAME);
+  ComAnsiNamePart currCatAnsiName(currCatName);
+  ComAnsiNamePart currSchAnsiName(currSchName);
+  tableName.applyDefaults(currCatAnsiName, currSchAnsiName);
+
+  const NAString catalogNamePart = tableName.getCatalogNamePartAsAnsiString();
+  const NAString schemaNamePart = 
tableName.getSchemaNamePartAsAnsiString(TRUE);
+  const NAString objectNamePart = 
tableName.getObjectNamePartAsAnsiString(TRUE);
+  const NAString extTableName = tableName.getExternalName(TRUE);
+  const NAString extNameForHbase = catalogNamePart + "." + schemaNamePart + 
"." + objectNamePart;
+
+  ExeCliInterface cliInterface(STMTHEAP, NULL, NULL, 
+  CmpCommon::context()->sqlSession()->getParentQid());
+
+  if ((isSeabaseReservedSchema(tableName)) &&
+      (!Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL)))
+    {
+      *CmpCommon::diags() << 
DgSqlCode(-CAT_CANNOT_ALTER_DEFINITION_METADATA_SCHEMA);
+      processReturn();
+      return;
+    }
 
   ExpHbaseInterface * ehi = allocEHI();
   if (ehi == NULL)
@@ -4816,7 +4857,7 @@ void CmpSeabaseDDL::alterSeabaseTableDropColumn(
               tableName.getSchemaNamePart().getInternalName(),
               tableName.getCatalogNamePart().getInternalName());
 
-  NATable *naTable = bindWA.getNATable(cn); 
+  const NATable *naTable = bindWA.getNATable(cn); 
   if (naTable == NULL || bindWA.errStatus())
     {
       *CmpCommon::diags()
@@ -4933,10 +4974,12 @@ void CmpSeabaseDDL::alterSeabaseTableDropColumn(
   char *col = NULL;
   if (naTable->isSQLMXAlignedTable())
     {
-      if (alignedFormatTableAddDropColumn(naTable->objectUid().castToInt64(),
-                                          FALSE, // drop col
-                                          catalogNamePart, schemaNamePart, 
objectNamePart,
-                                          (char*)colName.data(), NULL))
+      if (alignedFormatTableDropColumn
+          (
+           catalogNamePart, schemaNamePart, objectNamePart,
+           naTable, 
+           alterDropColNode->getColName(),
+           NULL))
         {
           processReturn();
           return;
@@ -4946,80 +4989,12 @@ void CmpSeabaseDDL::alterSeabaseTableDropColumn(
     {
       if (beginXnIfNotInProgress(&cliInterface, xnWasStartedHere))
         return;
-      
-      char buf[4000];
-      str_sprintf(buf, "delete from %s.\"%s\".%s where object_uid = %Ld and 
column_number = %d",
-                  getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
-                  objUID,
-                  colNumber);
-      
-      cliRC = cliInterface.executeImmediate(buf);
-      if (cliRC < 0)
-        {
-          cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
-          goto label_return;
-        }
-      
-      str_sprintf(buf, "update %s.\"%s\".%s set column_number = column_number 
- 1 where object_uid = %Ld and column_number >= %d",
-                  getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
-                  objUID,
-                  colNumber);
-      
-      cliRC = cliInterface.executeImmediate(buf);
-      if (cliRC < 0)
-        {
-          cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
-          
-          goto label_return;
-        }
-      
-      str_sprintf(buf, "update %s.\"%s\".%s set column_number = column_number 
- 1 where object_uid = %Ld and column_number >= %d",
-                  getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_KEYS,
-                  objUID,
-                  colNumber);
-      
-      cliRC = cliInterface.executeImmediate(buf);
-      if (cliRC < 0)
+
+      if (updateMDforDropCol(cliInterface, naTable, colNumber))
         {
-          cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
-          
           goto label_return;
         }
 
-      // keys for indexes refer to base table column number.
-      // modify it so they now refer to new column numbers.
-      if (naTable->hasSecondaryIndexes())
-        {
-          const NAFileSetList &naFsList = naTable->getIndexList();
-          
-          for (Lng32 i = 0; i < naFsList.entries(); i++)
-            {
-              naFS = naFsList[i];
-              
-              // skip clustering index
-              if (naFS->getKeytag() == 0)
-                continue;
-              
-              const QualifiedName &indexName = naFS->getFileSetName();
-
-              str_sprintf(buf, "update %s.\"%s\".%s set column_number = 
column_number - 1  where column_number >=  %d and object_uid = (select 
object_uid from %s.\"%s\".%s where catalog_name = '%s' and schema_name = '%s' 
and object_name = '%s' and object_type = 'IX') ",
-                          getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_KEYS,
-                          colNumber,
-                          getSystemCatalog(), SEABASE_MD_SCHEMA, 
SEABASE_OBJECTS,
-                          indexName.getCatalogName().data(),
-                          indexName.getSchemaName().data(),
-                          indexName.getObjectName().data());
-              cliRC = cliInterface.executeImmediate(buf);
-              if (cliRC < 0)
-                {
-                  cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
-                  
-                  goto label_return;
-                }
-
-            } // for
-        } // secondary indexes present
-      
       // remove column from all rows of the base table
       HbaseStr hbaseTable;
       hbaseTable.val = (char*)extNameForHbase.data();
@@ -5261,166 +5236,627 @@ void 
CmpSeabaseDDL::alterSeabaseTableAlterIdentityColumn(
         }
     }
 
-  //  CorrName cn(objectNamePart, STMTHEAP, schemaNamePart, catalogNamePart);
   ActiveSchemaDB()->getNATableDB()->removeNATable(cn,
     NATableDB::REMOVE_FROM_ALL_USERS, COM_BASE_TABLE_OBJECT);
 
   return;
 }
 
-void CmpSeabaseDDL::alterSeabaseTableAlterColumnDatatype(
-     StmtDDLAlterTableAlterColumnDatatype * alterColNode,
-     NAString &currCatName, NAString &currSchName)
+///////////////////////////////////////////////////////////////////////
+//
+// An aligned table constains all columns in one hbase cell.
+// To drop a column, we need to read each row, create a
+// new row with the removed column and insert into the original table.
+//
+// Steps to drop a column from an aligned table:
+//
+// -- make a copy of the source aligned table using hbase copy
+// -- truncate the source table
+// -- Update metadata and remove the dropped column.
+// -- bulk load data from copied table into the source table
+// -- drop the copied temp table
+//
+// If an error happens after the source table has been truncated, then
+// it will be restored from the copied table.
+//
+///////////////////////////////////////////////////////////////////////
+short CmpSeabaseDDL::alignedFormatTableDropColumn
+(
+ const NAString &catalogNamePart,
+ const NAString &schemaNamePart,
+ const NAString &objectNamePart,
+ const NATable * naTable,
+ const NAString &altColName,
+ ElemDDLColDef *pColDef)
 {
   Lng32 cliRC = 0;
-  Lng32 retcode = 0;
 
-  const NAString &tabName = alterColNode->getTableName();
+  const NAFileSet * naf = naTable->getClusteringIndex();
+  
+  CorrName cn(objectNamePart, STMTHEAP, schemaNamePart, catalogNamePart);
 
-  ComObjectName tableName(tabName, COM_TABLE_NAME);
-  ComAnsiNamePart currCatAnsiName(currCatName);
-  ComAnsiNamePart currSchAnsiName(currSchName);
-  tableName.applyDefaults(currCatAnsiName, currSchAnsiName);
+  ComUID comUID;
+  comUID.make_UID();
+  Int64 objUID = comUID.get_value();
+  
+  char objUIDbuf[100];
 
-  const NAString catalogNamePart = tableName.getCatalogNamePartAsAnsiString();
-  const NAString schemaNamePart = 
tableName.getSchemaNamePartAsAnsiString(TRUE);
-  const NAString objectNamePart = 
tableName.getObjectNamePartAsAnsiString(TRUE);
-  const NAString extTableName = tableName.getExternalName(TRUE);
-  const NAString extNameForHbase = catalogNamePart + "." + schemaNamePart + 
"." + objectNamePart;
+  NAString tempTable(naTable->getTableName().getQualifiedNameAsAnsiString());
+  tempTable += "_";
+  tempTable += str_ltoa(objUID, objUIDbuf);
 
-  ExeCliInterface cliInterface(STMTHEAP, NULL, NULL, 
-  CmpCommon::context()->sqlSession()->getParentQid());
+  ExpHbaseInterface * ehi = allocEHI();
+  ExeCliInterface cliInterface
+    (STMTHEAP, NULL, NULL, 
+     CmpCommon::context()->sqlSession()->getParentQid());
 
-  if ((isSeabaseReservedSchema(tableName)) &&
-      (!Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL)))
+  Int64 tableUID = naTable->objectUid().castToInt64();
+  const NAColumnArray &naColArr = naTable->getNAColumnArray();
+  const NAColumn * altNaCol = naColArr.getColumn(altColName);
+  Lng32 altColNum = altNaCol->getPosition();
+
+  NAString tgtCols;
+  NAString srcCols;
+
+ NABoolean xnWasStartedHere = FALSE;
+
+  char buf[4000];
+
+  if (cloneSeabaseTable(cn, naTable, tempTable, ehi, &cliInterface))
     {
-      *CmpCommon::diags() << 
DgSqlCode(-CAT_CANNOT_ALTER_DEFINITION_METADATA_SCHEMA);
-      processReturn();
-      return;
+      cliRC = -1;
+      goto label_drop;
     }
-
-  ExpHbaseInterface * ehi = allocEHI();
-  if (ehi == NULL)
+  
+  if (truncateHbaseTable(catalogNamePart, schemaNamePart, objectNamePart,
+                         (NATable*)naTable, ehi))
     {
-      processReturn();
-      
-      return;
+      cliRC = -1;
+      goto label_restore;
     }
 
-  retcode = existsInSeabaseMDTable(&cliInterface, 
-                                   catalogNamePart, schemaNamePart, 
objectNamePart,
-                                   COM_BASE_TABLE_OBJECT,
-                                   
(Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL) 
-                                    ? FALSE : TRUE),
-                                   TRUE, TRUE);
-  if (retcode < 0)
+  if (beginXnIfNotInProgress(&cliInterface, xnWasStartedHere))
     {
-      processReturn();
-
-      return;
+      cliRC = -1;
+      goto label_restore;
     }
 
-  ActiveSchemaDB()->getNATableDB()->useCache();
-
-  BindWA bindWA(ActiveSchemaDB(), CmpCommon::context(), FALSE/*inDDL*/);
-  CorrName cn(tableName.getObjectNamePart().getInternalName(),
-              STMTHEAP,
-              tableName.getSchemaNamePart().getInternalName(),
-              tableName.getCatalogNamePart().getInternalName());
+  if (updateMDforDropCol(cliInterface, naTable, altColNum))
+    {
+      cliRC = -1;
+      goto label_restore;
+    }
+  
+  ActiveSchemaDB()->getNATableDB()->removeNATable
+    (cn,
+     NATableDB::REMOVE_FROM_ALL_USERS, 
+     COM_BASE_TABLE_OBJECT);
 
-  NATable *naTable = bindWA.getNATable(cn); 
-  if (naTable == NULL || bindWA.errStatus())
+  for (Int32 c = 0; c < naColArr.entries(); c++)
     {
-      *CmpCommon::diags()
-        << DgSqlCode(-4082)
-        << DgTableName(cn.getExposedNameAsAnsiString());
-    
-      processReturn();
+      const NAColumn * nac = naColArr[c];
+      if (nac->getColName() == altColName)
+        continue;
 
-      return;
-    }
+      if (nac->isComputedColumn())
+        continue;
 
-  // Make sure user has the privilege to perform the alter column
-  if (!isDDLOperationAuthorized(SQLOperation::ALTER_TABLE,
-                                naTable->getOwner(),naTable->getSchemaOwner()))
-  {
-     *CmpCommon::diags() << DgSqlCode(-CAT_NOT_AUTHORIZED);
+      if (nac->isSystemColumn())
+        continue;
 
-     processReturn ();
+      tgtCols += nac->getColName();
+      tgtCols += ",";
+    } // for
 
-     return;
-  }
+  tgtCols = tgtCols.strip(NAString::trailing, ',');
 
-  // return an error if trying to alter a column from a volatile table
-  if (naTable->isVolatileTable())
+  str_sprintf(buf, "upsert using load into %s(%s) select %s from %s",
+              naTable->getTableName().getQualifiedNameAsAnsiString().data(),
+              tgtCols.data(),
+              tgtCols.data(),
+              tempTable.data());
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
     {
-      *CmpCommon::diags() << 
DgSqlCode(-CAT_REGULAR_OPERATION_ON_VOLATILE_OBJECT);
-     
-      processReturn ();
-
-      return;
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      goto label_restore;
     }
 
-  const NAColumnArray &nacolArr = naTable->getNAColumnArray();
-  const NAString &colName = alterColNode->getColumnName();
+  endXnIfStartedHere(&cliInterface, xnWasStartedHere, 0);
 
-  const NAColumn * nacol = nacolArr.getColumn(colName);
-  if (! nacol)
+  str_sprintf(buf, "drop table %s", tempTable.data());
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
     {
-      // column doesnt exist. Error.
-      *CmpCommon::diags() << DgSqlCode(-CAT_COLUMN_DOES_NOT_EXIST_ERROR)
-                          << DgColumnName(colName);
-
-      processReturn();
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      goto label_restore;
+    }
+  
+  deallocEHI(ehi); 
+  
+  return 0;
 
-      return;
+ label_restore:
+  endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1);
+
+  cloneHbaseTable(tempTable, 
+                  naTable->getTableName().getQualifiedNameAsAnsiString(),
+                  ehi);
+ 
+ label_drop:  
+  str_sprintf(buf, "drop table %s", tempTable.data());
+  Lng32 cliRC2 = cliInterface.executeImmediate(buf);
+  
+  deallocEHI(ehi); 
+  
+  return (cliRC < 0 ? -1 : 0);  
+}
+
+///////////////////////////////////////////////////////////////////////
+//
+// An aligned table constains all columns in one hbase cell.
+// To alter a column, we need to read each row, create a
+// new row with the altered column and insert into the original table.
+//
+// Steps to alter a column from an aligned table:
+//
+// -- make a copy of the source aligned table using hbase copy
+// -- truncate the source table
+// -- Update metadata column definition with the new definition
+// -- bulk load data from copied table into the source table
+// -- drop the copied temp table
+//
+// If an error happens after the source table has been truncated, then
+// it will be restored from the copied table.
+//
+///////////////////////////////////////////////////////////////////////
+short CmpSeabaseDDL::alignedFormatTableAlterColumn
+(
+ const NAString &catalogNamePart,
+ const NAString &schemaNamePart,
+ const NAString &objectNamePart,
+ const NATable * naTable,
+ const NAString &altColName,
+ ElemDDLColDef *pColDef)
+{
+  Lng32 cliRC = 0;
+
+  const NAFileSet * naf = naTable->getClusteringIndex();
+  
+  CorrName cn(objectNamePart, STMTHEAP, schemaNamePart, catalogNamePart);
+
+  ComUID comUID;
+  comUID.make_UID();
+  Int64 objUID = comUID.get_value();
+  
+  char objUIDbuf[100];
+
+  NAString tempTable(naTable->getTableName().getQualifiedNameAsAnsiString());
+  tempTable += "_";
+  tempTable += str_ltoa(objUID, objUIDbuf);
+
+  ExpHbaseInterface * ehi = allocEHI();
+  ExeCliInterface cliInterface
+    (STMTHEAP, NULL, NULL, 
+     CmpCommon::context()->sqlSession()->getParentQid());
+
+  Int64 tableUID = naTable->objectUid().castToInt64();
+  const NAColumnArray &naColArr = naTable->getNAColumnArray();
+  const NAColumn * altNaCol = naColArr.getColumn(altColName);
+  Lng32 altColNum = altNaCol->getPosition();
+
+  char buf[4000];
+  NAString colFamily;
+  NAString colName;
+  Lng32 datatype, length, precision, scale, dt_start, dt_end, 
+    nullable, upshifted;
+  ComColumnClass colClass;
+  ComColumnDefaultClass defaultClass;
+  NAString charset, defVal;
+  NAString heading;
+  ULng32 hbaseColFlags;
+  Int64 colFlags;
+  LobsStorage lobStorage;
+  NAString quotedDefVal;
+
+  NABoolean xnWasStartedHere = FALSE;
+
+  if (cloneSeabaseTable(cn, naTable, tempTable, ehi, &cliInterface))
+    {
+      cliRC = -1;
+      goto label_drop;
+    }
+  
+  if (truncateHbaseTable(catalogNamePart, schemaNamePart, objectNamePart,
+                         (NATable*)naTable, ehi))
+    {
+      cliRC = -1;
+      goto label_restore;
     }
 
-  const NAType * currType = nacol->getType();
-  NAType * newType = alterColNode->getType();
 
-  // Column that can be altered must meet these conditions:
-  //   -- old and new column datatype must be VARCHAR
-  //   -- new col length must be greater than or equal to old length
-  //   -- old and new character sets must be the same
-  NABoolean canAlter = FALSE;
-  if ((DFS2REC::isSQLVarChar(currType->getFSDatatype())) &&
-      (DFS2REC::isSQLVarChar(newType->getFSDatatype())) &&
-      (currType->getFSDatatype() == newType->getFSDatatype()) &&
-      (currType->getNominalSize() <= newType->getNominalSize()) &&
-      (((CharType*)currType)->getCharSet() == 
((CharType*)newType)->getCharSet()))
-    canAlter = TRUE;
+  if (beginXnIfNotInProgress(&cliInterface, xnWasStartedHere))
+    goto label_restore;
 
-  if (NOT canAlter)
+  if (getColInfo(pColDef,
+                 colFamily,
+                 colName, 
+                 naTable->isSQLMXAlignedTable(),
+                 datatype, length, precision, scale, dt_start, dt_end, 
+                 upshifted, nullable,
+                 charset, colClass, defaultClass, defVal, heading, lobStorage,
+                 hbaseColFlags, colFlags))
     {
+      cliRC = -1;
+      processReturn();
+      
+      goto label_restore;
+    }
+  
+  if (NOT defVal.isNull())
+    {
+      ToQuotedString(quotedDefVal, defVal, FALSE);
+    }
+  
+  str_sprintf(buf, "update %s.\"%s\".%s set (column_class, fs_data_type, 
sql_data_type, column_size, column_precision, column_scale, 
datetime_start_field, datetime_end_field, is_upshifted, nullable, 
character_set, default_class, default_value) = ('%s', %d, '%s', %d, %d, %d, %d, 
%d, '%s', %d, '%s', %d, '%s') where object_uid = %Ld and column_number = %d",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+              COM_ALTERED_USER_COLUMN_LIT,
+              datatype,
+              getAnsiTypeStrFromFSType(datatype),
+              length,
+              precision,
+              scale,
+              dt_start,
+              dt_end,
+              (upshifted ? "Y" : "N"),
+              nullable,
+              (char*)charset.data(),
+              (Lng32)defaultClass,
+              (quotedDefVal.isNull() ? "" : quotedDefVal.data()),
+              tableUID,
+              altColNum);
+  
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      goto label_restore;
+    }
+  
+  ActiveSchemaDB()->getNATableDB()->removeNATable
+    (cn,
+     NATableDB::REMOVE_FROM_ALL_USERS, 
+     COM_BASE_TABLE_OBJECT);
+  
+  str_sprintf(buf, "upsert using load into %s select * from %s",
+              naTable->getTableName().getQualifiedNameAsAnsiString().data(),
+              tempTable.data());
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+
       NAString reason;
-      if (NOT ((DFS2REC::isSQLVarChar(currType->getFSDatatype())) &&
-               (DFS2REC::isSQLVarChar(newType->getFSDatatype()))))
-        reason = "Old and New datatypes must be VARCHAR.";
-      else if (currType->getFSDatatype() != newType->getFSDatatype())
-        reason = "Old and New datatypes must be the same.";
-      else if (((CharType*)currType)->getCharSet() != 
((CharType*)newType)->getCharSet())
-        reason = "Old and New character sets must be the same.";
-      else if (currType->getNominalSize() > newType->getNominalSize())
-        reason = "New length must be greater than or equal to old length.";
+      reason = "Old data could not be updated using the altered column 
definition.";
+      
+      // column cannot be altered
+      *CmpCommon::diags() << DgSqlCode(-1404)
+                          << DgColumnName(altColName)
+                          << DgString0(reason);
 
-      // key column cannot be altered
+      goto label_restore;
+    }
+
+  deallocEHI(ehi); 
+  
+  endXnIfStartedHere(&cliInterface, xnWasStartedHere, 0);
+  
+  return 0;
+
+ label_restore:
+  cloneHbaseTable(tempTable, 
+                  naTable->getTableName().getQualifiedNameAsAnsiString(),
+                  ehi);
+ 
+ label_drop:  
+  str_sprintf(buf, "drop table %s", tempTable.data());
+  Lng32 cliRC2 = cliInterface.executeImmediate(buf);
+  
+  deallocEHI(ehi); 
+  
+  endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1);
+
+  return (cliRC < 0 ? -1 : 0);
+}
+
+short CmpSeabaseDDL::alterColumnAttr(
+     const NAString &catalogNamePart, const NAString &schemaNamePart,
+     const NAString &objectNamePart,
+     const NATable * naTable, const NAColumn * naCol, NAType * newType,
+     StmtDDLAlterTableAlterColumnDatatype * alterColNode)
+{
+  if (naTable->isSQLMXAlignedTable())
+    {
+      ElemDDLColDef *pColDef = 
+        alterColNode->getColToAlter()->castToElemDDLColDef();
+
+      if (alignedFormatTableAlterColumn
+          (
+           catalogNamePart, schemaNamePart, objectNamePart,
+           naTable,
+           naCol->getColName(), 
+           pColDef))
+        {
+          processReturn();
+          return -1;
+        }
+
+      return 0;
+    }
+
+  CorrName cn(objectNamePart, STMTHEAP, schemaNamePart,catalogNamePart);
+
+  Lng32 cliRC = 0;
+  Lng32 retcode = 0;
+
+  ComUID comUID;
+  comUID.make_UID();
+  Int64 objUID = comUID.get_value();
+  
+  char objUIDbuf[100];
+
+  NAString tempCol(naCol->getColName());
+  tempCol += "_";
+  tempCol += str_ltoa(objUID, objUIDbuf);
+
+  char dispBuf[1000];
+  Lng32 ii = 0;
+  NABoolean identityCol;
+  ElemDDLColDef *pColDef = 
alterColNode->getColToAlter()->castToElemDDLColDef();
+  NAColumn *nac = NULL;
+  if (getNAColumnFromColDef(pColDef, nac))
+    return -1;
+
+  dispBuf[0] = 0;
+  if (cmpDisplayColumn(nac, (char*)tempCol.data(), newType, 3, NULL, dispBuf, 
+                       ii, FALSE, identityCol, 
+                       FALSE, FALSE))
+    return -1;
+  
+  ExeCliInterface cliInterface
+    (STMTHEAP, NULL, NULL, 
+     CmpCommon::context()->sqlSession()->getParentQid());
+
+  Int64 tableUID = naTable->objectUid().castToInt64();
+  const NAColumnArray &nacolArr = naTable->getNAColumnArray();
+  const NAString &altColName = naCol->getColName();
+  const NAColumn * altNaCol = nacolArr.getColumn(altColName);
+  Lng32 altColNum = altNaCol->getPosition();
+
+  char buf[4000];
+  str_sprintf(buf, "alter table %s add column %s",
+              naTable->getTableName().getQualifiedNameAsAnsiString().data(), 
+              dispBuf);
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+
+      processReturn();
+      return -1;
+    }
+
+  str_sprintf(buf, "update %s set %s = %s",
+              naTable->getTableName().getQualifiedNameAsAnsiString().data(), 
+              tempCol.data(),
+              naCol->getColName().data());
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+
+      goto label_error1;
+    }
+   
+  str_sprintf(buf, "delete from %s.\"%s\".%s where object_uid = %Ld and 
column_number = %d",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+              tableUID,
+              altColNum);
+  
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      goto label_error1;
+    }
+  
+  str_sprintf(buf, "insert into %s.\"%s\".%s select object_uid, '%s', %d, 
'%s', fs_data_type, sql_data_type, column_size, column_precision, column_scale, 
datetime_start_field, datetime_end_field, is_upshifted, column_flags, nullable, 
character_set, default_class, default_value, column_heading, '%s', '%s', 
direction, is_optional, flags from %s.\"%s\".%s where object_uid = %Ld and 
column_number = (select column_number from %s.\"%s\".%s where object_uid = %Ld 
and column_name = '%s')",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+              naCol->getColName().data(),              
+              altColNum,
+              COM_ALTERED_USER_COLUMN_LIT,
+              altNaCol->getHbaseColFam().data(),
+              altNaCol->getHbaseColQual().data(),
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+              tableUID,
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+              tableUID,
+              tempCol.data());
+  
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      goto label_error1;
+    }
+
+  ActiveSchemaDB()->getNATableDB()->removeNATable
+    (cn,
+     NATableDB::REMOVE_FROM_ALL_USERS, 
+     COM_BASE_TABLE_OBJECT);
+  str_sprintf(buf, "update %s set %s = %s",
+              naTable->getTableName().getQualifiedNameAsAnsiString().data(), 
+              naCol->getColName().data(),
+              tempCol.data());
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+
+      NAString reason;
+      reason = "Old data could not be updated into the new column definition.";
+      
+      // column cannot be altered
       *CmpCommon::diags() << DgSqlCode(-1404)
-                          << DgColumnName(colName)
+                          << DgColumnName(naCol->getColName())
                           << DgString0(reason);
 
       processReturn();
+      goto label_error1;
+    }
+   
+  str_sprintf(buf, "alter table %s drop column %s",
+              naTable->getTableName().getQualifiedNameAsAnsiString().data(), 
+              tempCol.data());
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
       
+      processReturn();
+      return -1;
+    }
+
+  return 0;
+
+ label_error1:
+  str_sprintf(buf, "alter table %s drop column %s",
+              naTable->getTableName().getQualifiedNameAsAnsiString().data(), 
+              tempCol.data());
+  cliRC = cliInterface.executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      processReturn();
+      return -1;
+    } 
+ 
+  return -1;
+}
+
+void CmpSeabaseDDL::alterSeabaseTableAlterColumnDatatype(
+     StmtDDLAlterTableAlterColumnDatatype * alterColNode,
+     NAString &currCatName, NAString &currSchName)
+{
+  Lng32 cliRC = 0;
+  Lng32 retcode = 0;
+
+  const NAString &tabName = alterColNode->getTableName();
+
+  ComObjectName tableName(tabName, COM_TABLE_NAME);
+  ComAnsiNamePart currCatAnsiName(currCatName);
+  ComAnsiNamePart currSchAnsiName(currSchName);
+  tableName.applyDefaults(currCatAnsiName, currSchAnsiName);
+
+  const NAString catalogNamePart = tableName.getCatalogNamePartAsAnsiString();
+  const NAString schemaNamePart = 
tableName.getSchemaNamePartAsAnsiString(TRUE);
+  const NAString objectNamePart = 
tableName.getObjectNamePartAsAnsiString(TRUE);
+  const NAString extTableName = tableName.getExternalName(TRUE);
+  const NAString extNameForHbase = catalogNamePart + "." + schemaNamePart + 
"." + objectNamePart;
+
+  ExeCliInterface cliInterface(STMTHEAP, NULL, NULL, 
+  CmpCommon::context()->sqlSession()->getParentQid());
+
+  if ((isSeabaseReservedSchema(tableName)) &&
+      (!Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL)))
+    {
+      *CmpCommon::diags() << 
DgSqlCode(-CAT_CANNOT_ALTER_DEFINITION_METADATA_SCHEMA);
+      processReturn();
       return;
     }
- // If column is a LOB column , error
+
+  retcode = existsInSeabaseMDTable(&cliInterface, 
+                                   catalogNamePart, schemaNamePart, 
objectNamePart,
+                                   COM_BASE_TABLE_OBJECT,
+                                   
(Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL) 
+                                    ? FALSE : TRUE),
+                                   TRUE, TRUE);
+  if (retcode < 0)
+    {
+      processReturn();
+
+      return;
+    }
+
+  ActiveSchemaDB()->getNATableDB()->useCache();
+
+  BindWA bindWA(ActiveSchemaDB(), CmpCommon::context(), FALSE/*inDDL*/);
+  CorrName cn(tableName.getObjectNamePart().getInternalName(),
+              STMTHEAP,
+              tableName.getSchemaNamePart().getInternalName(),
+              tableName.getCatalogNamePart().getInternalName());
+
+  NATable *naTable = bindWA.getNATable(cn); 
+  if (naTable == NULL || bindWA.errStatus())
+    {
+      *CmpCommon::diags()
+        << DgSqlCode(-4082)
+        << DgTableName(cn.getExposedNameAsAnsiString());
+    
+      processReturn();
+
+      return;
+    }
+
+  // Make sure user has the privilege to perform the alter column
+  if (!isDDLOperationAuthorized(SQLOperation::ALTER_TABLE,
+                                naTable->getOwner(),naTable->getSchemaOwner()))
+  {
+     *CmpCommon::diags() << DgSqlCode(-CAT_NOT_AUTHORIZED);
+
+     processReturn ();
+
+     return;
+  }
+
+  // return an error if trying to alter a column from a volatile table
+  if (naTable->isVolatileTable())
+    {
+      *CmpCommon::diags() << 
DgSqlCode(-CAT_REGULAR_OPERATION_ON_VOLATILE_OBJECT);
+     
+      processReturn ();
+
+      return;
+    }
+
+  ElemDDLColDef *pColDef = 
alterColNode->getColToAlter()->castToElemDDLColDef();
+  
+  const NAColumnArray &nacolArr = naTable->getNAColumnArray();
+  const NAString &colName = pColDef->getColumnName();
+
+  const NAColumn * nacol = nacolArr.getColumn(colName);
+  if (! nacol)
+    {
+      // column doesnt exist. Error.
+      *CmpCommon::diags() << DgSqlCode(-CAT_COLUMN_DOES_NOT_EXIST_ERROR)
+                          << DgColumnName(colName);
+
+      processReturn();
+
+      return;
+    }
+
+  const NAType * currType = nacol->getType();
+  NAType * newType = pColDef->getColumnDataType();
+
+  // If column is a LOB column , error
   if ((currType->getFSDatatype() == REC_BLOB) || (currType->getFSDatatype() == 
REC_CLOB))
-     {
+    {
       *CmpCommon::diags() << DgSqlCode(-CAT_LOB_COLUMN_ALTER)
-                              << DgColumnName(colName);
+                          << DgColumnName(colName);
       processReturn();
       return;
      }
+
   const NAFileSet * naFS = naTable->getClusteringIndex();
   const NAColumnArray &naKeyColArr = naFS->getIndexKeyColumns();
   if (naKeyColArr.getColumn(colName))
@@ -5461,32 +5897,104 @@ void 
CmpSeabaseDDL::alterSeabaseTableAlterColumnDatatype(
         } // for
     } // secondary indexes present
 
-  Int64 objUID = naTable->objectUid().castToInt64();
+  if ((NOT currType->isCompatible(*newType)) &&
+      (NOT ((currType->getTypeQualifier() == NA_CHARACTER_TYPE) &&
+            (newType->getTypeQualifier() == NA_CHARACTER_TYPE))))
+    {
+      NAString reason = "Old and New datatypes must be compatible.";
 
-  Lng32 colNumber = nacol->getPosition();
-  char *col = NULL;
+      // column cannot be altered
+      *CmpCommon::diags() << DgSqlCode(-1404)
+                          << DgColumnName(colName)
+                          << DgString0(reason);
+      
+      processReturn();
+      
+      return;
+    }
 
-  char buf[4000];
-  str_sprintf(buf, "update %s.\"%s\".%s set column_size = %d where object_uid 
= %Ld and column_number = %d",
-              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
-              newType->getNominalSize(),
-              objUID,
-              colNumber);
-  
-  cliRC = cliInterface.executeImmediate(buf);
-  if (cliRC < 0)
+  // Column that can be altered by updating metadata only
+  // must meet these conditions:
+  //   -- old and new column datatype must be VARCHAR
+  //   -- old and new datatype must have the same nullable attr
+  //   -- new col length must be greater than or equal to old length
+  //   -- old and new character sets must be the same
+  NABoolean mdAlterOnly = FALSE;
+  if ((DFS2REC::isSQLVarChar(currType->getFSDatatype())) &&
+      (DFS2REC::isSQLVarChar(newType->getFSDatatype())) &&
+      (currType->getFSDatatype() == newType->getFSDatatype()) &&
+      (currType->supportsSQLnull() == newType->supportsSQLnull()) &&
+      (currType->getNominalSize() <= newType->getNominalSize()) &&
+      (((CharType*)currType)->getCharSet() == 
((CharType*)newType)->getCharSet()))
+    mdAlterOnly = TRUE;
+
+  if ((NOT mdAlterOnly) &&
+      (CmpCommon::getDefault(TRAF_ALTER_COL_ATTRS) == DF_OFF))
     {
-      cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+      NAString reason;
+      if (NOT ((DFS2REC::isSQLVarChar(currType->getFSDatatype())) &&
+               (DFS2REC::isSQLVarChar(newType->getFSDatatype()))))
+        reason = "Old and New datatypes must be VARCHAR.";
+      else if (currType->getFSDatatype() != newType->getFSDatatype())
+        reason = "Old and New datatypes must be the same.";
+      else if (((CharType*)currType)->getCharSet() != 
((CharType*)newType)->getCharSet())
+        reason = "Old and New character sets must be the same.";
+      else if (currType->getNominalSize() > newType->getNominalSize())
+        reason = "New length must be greater than or equal to old length.";
+      else if (currType->supportsSQLnull() != newType->supportsSQLnull())
+        reason = "Old and New nullability must be the same.";
+
+      // column cannot be altered
+      *CmpCommon::diags() << DgSqlCode(-1404)
+                          << DgColumnName(colName)
+                          << DgString0(reason);
 
       processReturn();
+      
       return;
     }
+
+  if (mdAlterOnly)
+    {
+      Int64 objUID = naTable->objectUid().castToInt64();
+      
+      Lng32 colNumber = nacol->getPosition();
+      
+      char buf[4000];
+      str_sprintf(buf, "update %s.\"%s\".%s set column_size = %d, column_class 
= '%s' where object_uid = %Ld and column_number = %d",
+                  getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_COLUMNS,
+                  newType->getNominalSize(),
+                  COM_ALTERED_USER_COLUMN_LIT,
+                  objUID,
+                  colNumber);
+      
+      cliRC = cliInterface.executeImmediate(buf);
+      if (cliRC < 0)
+        {
+          cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+          
+          processReturn();
+          return;
+        }
+    }
+  else
+    {
+      if (alterColumnAttr(catalogNamePart, schemaNamePart, objectNamePart,
+                          naTable, nacol, newType, alterColNode))
+        return;
+    }
   
-  deallocEHI(ehi); 
-  heap_->deallocateMemory(col);
-  
-  ActiveSchemaDB()->getNATableDB()->removeNATable(cn,
-                                                  
NATableDB::REMOVE_FROM_ALL_USERS, COM_BASE_TABLE_OBJECT);
+  cliRC = updateObjectRedefTime(&cliInterface,
+                                catalogNamePart, schemaNamePart, 
objectNamePart,
+                                COM_BASE_TABLE_OBJECT_LIT);
+  if (cliRC < 0)
+    {
+      return;
+    }
+
+  ActiveSchemaDB()->getNATableDB()->removeNATable
+    (cn,
+     NATableDB::REMOVE_FROM_ALL_USERS, COM_BASE_TABLE_OBJECT);
   
   processReturn();
   
@@ -8204,6 +8712,8 @@ Lng32 CmpSeabaseDDL::getSeabaseColumnInfo(ExeCliInterface 
*cliInterface,
         colInfo.columnClass = COM_SYSTEM_COLUMN;
       else if (strcmp(colClass,COM_ADDED_USER_COLUMN_LIT) == 0)
         colInfo.columnClass = COM_ADDED_USER_COLUMN;
+      else if (strcmp(colClass,COM_ALTERED_USER_COLUMN_LIT) == 0)
+        colInfo.columnClass = COM_ALTERED_USER_COLUMN;
       else if (strcmp(colClass,COM_MV_SYSTEM_ADDED_COLUMN_LIT) == 0)
         colInfo.columnClass = COM_MV_SYSTEM_ADDED_COLUMN;
       else

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/7b99938c/core/sql/sqlcomp/DefaultConstants.h
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/DefaultConstants.h 
b/core/sql/sqlcomp/DefaultConstants.h
index d4acd8d..3a14da0 100644
--- a/core/sql/sqlcomp/DefaultConstants.h
+++ b/core/sql/sqlcomp/DefaultConstants.h
@@ -3796,6 +3796,10 @@ enum DefaultConstants
   // real charset in the HIVE table
   HIVE_FILE_CHARSET,
 
+  // By default only alter of varchar col length is supported.
+  // If this cqd is on, then other alters (name, datatype) are also supported.
+  TRAF_ALTER_COL_ATTRS,
+
   // This enum constant must be the LAST one in the list; it's a count,
   // not an Attribute (it's not IN DefaultDefaults; it's the SIZE of it)!
   __NUM_DEFAULT_ATTRIBUTES

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/7b99938c/core/sql/sqlcomp/nadefaults.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/nadefaults.cpp b/core/sql/sqlcomp/nadefaults.cpp
index b556100..ff75603 100644
--- a/core/sql/sqlcomp/nadefaults.cpp
+++ b/core/sql/sqlcomp/nadefaults.cpp
@@ -3296,9 +3296,9 @@ XDDkwd__(SUBQUERY_UNNESTING,                      "ON"),
   DDint__(TEST_PASS_TWO_ASSERT_TASK_NUMBER,    "-1"),
 
  XDDintN2(TIMEOUT,                             "6000"),
-
-  DDflt0_(TMUDF_CARDINALITY_FACTOR, "1"),
-  DDflt0_(TMUDF_LEAF_CARDINALITY, "1"),
+ 
+ DDflt0_(TMUDF_CARDINALITY_FACTOR, "1"),
+ DDflt0_(TMUDF_LEAF_CARDINALITY, "1"),
 
   DDkwd__(TOTAL_RESOURCE_COSTING,               "ON"),
 
@@ -3310,6 +3310,8 @@ XDDkwd__(SUBQUERY_UNNESTING,                      "ON"),
 
  DDkwd__(TRAF_ALLOW_SELF_REF_CONSTR,                 "ON"),   
 
+ DDkwd__(TRAF_ALTER_COL_ATTRS,                 "ON"),   
+
  DDkwd__(TRAF_BLOB_AS_VARCHAR,                 "ON"), //set to OFF to enable 
Lobs support  
 
  DDkwd__(TRAF_BOOTSTRAP_MD_MODE,                            "OFF"),   

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/7b99938c/core/sql/src/main/java/org/trafodion/sql/HBaseClient.java
----------------------------------------------------------------------
diff --git a/core/sql/src/main/java/org/trafodion/sql/HBaseClient.java 
b/core/sql/src/main/java/org/trafodion/sql/HBaseClient.java
index 61f8081..01e15a3 100644
--- a/core/sql/src/main/java/org/trafodion/sql/HBaseClient.java
+++ b/core/sql/src/main/java/org/trafodion/sql/HBaseClient.java
@@ -794,12 +794,12 @@ public class HBaseClient {
             return regionInfo;
     }
 
-    public boolean copy(String currTblName, String oldTblName)
+    public boolean copy(String srcTblName, String tgtTblName, boolean force)
        throws MasterNotRunningException, IOException, 
SnapshotCreationException, InterruptedException {
-            if (logger.isDebugEnabled()) logger.debug("HBaseClient.copy(" + 
currTblName + oldTblName + ") called.");
+            if (logger.isDebugEnabled()) logger.debug("HBaseClient.copy(" + 
srcTblName + tgtTblName + ") called.");
             HBaseAdmin admin = new HBaseAdmin(config);
            
-           String snapshotName = currTblName + "_SNAPSHOT";
+           String snapshotName = srcTblName + "_SNAPSHOT";
            
            List<SnapshotDescription> l = new ArrayList<SnapshotDescription>(); 
            //      l = admin.listSnapshots(snapshotName);
@@ -807,28 +807,25 @@ public class HBaseClient {
            if (! l.isEmpty())
                {
                    for (SnapshotDescription sd : l) {
-                       //                      System.out.println("here 1");
-                       //                      
System.out.println(snapshotName);
-                       //                      
System.out.println(sd.getName());
                        if (sd.getName().compareTo(snapshotName) == 0)
                            {
-                               //                              
System.out.println("here 2");
-                               //                          
admin.enableTable(snapshotName);
-                               //                              
System.out.println("here 3");
                                admin.deleteSnapshot(snapshotName);
-                               //                              
System.out.println("here 4");
                            }
                    }
                }
-           //      System.out.println(snapshotName);
-           if (! admin.isTableDisabled(currTblName))
-               admin.disableTable(currTblName);
-           //      System.out.println("here 5");
-           admin.snapshot(snapshotName, currTblName);
-           admin.cloneSnapshot(snapshotName, oldTblName);
+
+            if ((force == true) &&
+                (admin.tableExists(tgtTblName))) {
+                admin.disableTable(tgtTblName);
+                admin.deleteTable(tgtTblName);
+            }
+                
+           if (! admin.isTableDisabled(srcTblName))
+               admin.disableTable(srcTblName);
+           admin.snapshot(snapshotName, srcTblName);
+           admin.cloneSnapshot(snapshotName, tgtTblName);
            admin.deleteSnapshot(snapshotName);
-           //      System.out.println("here 6");
-           admin.enableTable(currTblName);
+           admin.enableTable(srcTblName);
             admin.close();
             return true;
     }

Reply via email to