trafodion-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ansha...@apache.org
Subject [1/4] incubator-trafodion git commit: [TRAFODION-2584] Add support to register hive objects in traf metadata
Date Tue, 18 Apr 2017 23:05:54 GMT
Repository: incubator-trafodion
Updated Branches:
  refs/heads/master 2f07a0666 -> f894c3f10


http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/CmpSeabaseDDLcleanup.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/CmpSeabaseDDLcleanup.cpp b/core/sql/sqlcomp/CmpSeabaseDDLcleanup.cpp
index 5640604..cd3ccb5 100644
--- a/core/sql/sqlcomp/CmpSeabaseDDLcleanup.cpp
+++ b/core/sql/sqlcomp/CmpSeabaseDDLcleanup.cpp
@@ -65,7 +65,8 @@ CmpSeabaseMDcleanup::CmpSeabaseMDcleanup(NAHeap *heap)
     numOrphanMetadataEntries_(0),
     numOrphanHbaseEntries_(0),
     numOrphanObjectsEntries_(0),
-    numOrphanViewsEntries_(0)
+    numOrphanViewsEntries_(0),
+    numInconsistentHiveEntries_(0)
 {};
 
 Int64 CmpSeabaseMDcleanup::getCleanupObjectUID(
@@ -1033,6 +1034,81 @@ short CmpSeabaseMDcleanup::cleanupUIDs(ExeCliInterface *cliInterface,
   return 0;
 }
 
+void CmpSeabaseMDcleanup::cleanupHiveObject(const StmtDDLCleanupObjects * stmtCleanupNode,
+                                             ExeCliInterface *cliInterface)
+{
+
+  Lng32 cliRC = 0;
+  char query[1000];
+  NABoolean errorSeen = FALSE;
+
+  // check if this table exists in hive metadata
+  NABoolean hiveObjExists = TRUE;
+  NAString objName(stmtCleanupNode->getTableNameAsQualifiedName()->getObjectName());
+  objName.toLower();
+  str_sprintf(query, "select * from (get %s in schema %s.%s, no header, match '%s') x(a)",
+              (stmtCleanupNode->getType() == StmtDDLCleanupObjects::HIVE_TABLE_)
+              ? "tables" : "views",
+               stmtCleanupNode->getTableNameAsQualifiedName()->getCatalogName().data(),
+              stmtCleanupNode->getTableNameAsQualifiedName()->getSchemaName().data(),    
+              objName.data());
+  cliRC = cliInterface->fetchRowsPrologue(query, TRUE/*no exec*/);
+  if (cliRC < 0)
+    {
+      cliInterface->retrieveSQLDiagnostics(CmpCommon::diags());
+      return;
+    }
+  
+  cliRC = cliInterface->clearExecFetchClose(NULL, 0);
+  if (cliRC < 0)
+    {
+      cliInterface->retrieveSQLDiagnostics(CmpCommon::diags());
+      return;
+    }
+  
+  if (cliRC == 100) // did not find the row
+    {
+      hiveObjExists = FALSE;
+    }
+    
+  // if underlying hive object doesn't exist, drop external table and unregister
+  // objects
+  if (NOT hiveObjExists)
+    {
+      // drop external table
+      if (stmtCleanupNode->getType() == StmtDDLCleanupObjects::HIVE_TABLE_)
+        {
+          str_sprintf(query, "drop external table if exists %s for %s;",
+                      objName.data(),
+                      stmtCleanupNode->getTableNameAsQualifiedName()->
+                      getQualifiedNameAsString().data());
+          cliRC = cliInterface->executeImmediate(query);
+          if (cliRC < 0)
+            {
+              if (processCleanupErrors(NULL, errorSeen))
+                return;
+            }          
+        }
+      
+      // unregister registered table or view
+      if (stmtCleanupNode->getType() == StmtDDLCleanupObjects::HIVE_TABLE_)
+        str_sprintf(query, "unregister hive table if exists %s cleanup;",
+                    stmtCleanupNode->getTableNameAsQualifiedName()->getQualifiedNameAsString().data());
+      else
+        str_sprintf(query, "unregister hive view if exists %s cleanup;",
+                    stmtCleanupNode->getTableNameAsQualifiedName()->getQualifiedNameAsString().data());
+      
+      cliRC = cliInterface->executeImmediate(query);
+      if (cliRC < 0)
+        {
+          if (processCleanupErrors(NULL, errorSeen))
+            return;
+        }
+    }
+
+  return;
+}
+
 short CmpSeabaseMDcleanup::addReturnDetailsEntry(
                                                  ExeCliInterface * cliInterface,
                                                  Queue* &list, const char *value, 
@@ -1474,6 +1550,90 @@ short CmpSeabaseMDcleanup::cleanupOrphanViewsEntries(ExeCliInterface *cliInterfa
   return 0;
 }
 
+// remove hive objects that are registered in traf metadata but the 
+// corresponding object is missing in hive database.
+short CmpSeabaseMDcleanup::cleanupInconsistentHiveEntries(
+     ExeCliInterface *cliInterface,
+     ExpHbaseInterface *ehi)
+{
+  Lng32 cliRC = 0;
+  char query[4000];
+  NABoolean errorSeen = FALSE;
+
+  // get all registered tables that do not have corresponding hive objects.
+  str_sprintf(query, "select trim(O.a), 'table' from "
+              "(select lower(trim(catalog_name) || '.' || trim(schema_name)"
+              " || '.' || trim(object_name)) from %s.\"%s\".%s "
+              "where object_type = '%s' and catalog_name = 'HIVE') O(a) left join "
+              "(select '%s' || '.' || trim(y) from "
+              "(get tables in catalog %s, no header) x(y)) G(b) "
+              "on O.a = G.b where G.b is null "
+              "  union all "
+              "select trim(O.a), 'view' from "
+              "(select lower(trim(catalog_name) || '.' || trim(schema_name)"
+              " || '.' || trim(object_name)) from %s.\"%s\".%s "
+              "where object_type = '%s' and catalog_name = 'HIVE') O(a) left join "
+              "(select '%s' || '.' || trim(y) from "
+              "(get views in catalog %s, no header) x(y)) G(b) "
+              "on O.a = G.b where G.b is null ;",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_OBJECTS,
+              COM_BASE_TABLE_OBJECT_LIT,
+              HIVE_SYSTEM_CATALOG_LC,
+              HIVE_SYSTEM_CATALOG_LC,
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_OBJECTS,
+              COM_VIEW_OBJECT_LIT,
+              HIVE_SYSTEM_CATALOG_LC,
+              HIVE_SYSTEM_CATALOG_LC);
+  Queue * orphanHiveObjs = NULL;
+  cliRC = cliInterface->fetchAllRows
+    (orphanHiveObjs, query, 0, FALSE, FALSE, TRUE);
+  if (cliRC < 0)
+    {
+      if (processCleanupErrors(cliInterface, errorSeen))
+        return -1;
+    }
+
+  numInconsistentHiveEntries_ = 0;
+  returnDetailsList_ = NULL;
+  addReturnDetailsEntry(cliInterface, returnDetailsList_, NULL, TRUE);
+
+  numInconsistentHiveEntries_ += orphanHiveObjs->numEntries();
+
+  orphanHiveObjs->position();
+  for (size_t i = 0; i < orphanHiveObjs->numEntries(); i++)
+    {
+      
+      OutputInfo * oi = (OutputInfo*)orphanHiveObjs->getCurr(); 
+       
+      if (addReturnDetailsEntry(cliInterface, returnDetailsList_, 
+                                oi->get(0), FALSE))
+        return -1;
+
+      if (NOT checkOnly_)
+        {
+          if (strcmp(oi->get(1), "table") == 0)
+            str_sprintf(query, "unregister hive table if exists %s cleanup;",
+                        oi->get(0));
+          else
+            str_sprintf(query, "unregister hive view if exists %s cleanup;",
+                        oi->get(0));
+            
+          cliRC = cliInterface->executeImmediate(query);
+          if (cliRC < 0)
+            {
+              cliInterface->retrieveSQLDiagnostics(CmpCommon::diags());
+              return -1;
+            }
+        }
+
+      orphanHiveObjs->advance();
+    }
+
+  numOrphanMetadataEntries_ = orphanHiveObjs->numEntries();
+
+  return 0;
+}
+
 void CmpSeabaseMDcleanup::populateBlackBox(ExeCliInterface *cliInterface,
                                            Queue *returnDetailsList,
                                            Int32 &blackBoxLen,
@@ -1716,7 +1876,7 @@ void CmpSeabaseMDcleanup::cleanupMetadataEntries(ExeCliInterface *cliInterface,
                   dws->setBlackBox(blackBox);
                    
                   dws->setMsg(buf);
-                  dws->setStep(DONE_CLEANUP);
+                  dws->setStep(HIVE_ENTRIES);
                   dws->setSubstep(0);
                   dws->setEndStep(TRUE);
                   
@@ -1726,6 +1886,49 @@ void CmpSeabaseMDcleanup::cleanupMetadataEntries(ExeCliInterface *cliInterface,
               } // switch
           } // VIEWS_ENTRIES
 
+        case HIVE_ENTRIES:
+          {
+            switch (dws->subStep())
+              {
+              case 0:
+                {
+                  dws->setMsg("  Start: Cleanup Inconsistent Hive Entries");
+                  dws->subStep()++;
+                  dws->setEndStep(FALSE);
+                  
+                  return;
+                }
+                break;
+                
+              case 1:
+                {
+
+                  if (cleanupInconsistentHiveEntries(cliInterface, ehi))
+                    return;
+                  
+                  str_sprintf(buf, "  End:   Cleanup Inconsistent Hive Entries (%d %s %s)",
+                              numInconsistentHiveEntries_,
+                              (numInconsistentHiveEntries_ == 1 ? "entry" : "entries"),
+                              (checkOnly_ ? "found" : "cleaned up"));
+                  
+                  Int32 blackBoxLen = 0;
+                  char * blackBox = NULL;
+                  populateBlackBox(cliInterface, returnDetailsList_, blackBoxLen, blackBox);
+
+                  dws->setBlackBoxLen(blackBoxLen);
+                  dws->setBlackBox(blackBox);
+                   
+                  dws->setMsg(buf);
+                  dws->setStep(DONE_CLEANUP);
+                  dws->setSubstep(0);
+                  dws->setEndStep(TRUE);
+                  
+                  return;
+                }
+                break;
+              } // switch
+          } // HIVE_ENTRIES
+
        case DONE_CLEANUP:
           {
             dws->setMsg("Metadata Cleanup: done");
@@ -1799,6 +2002,13 @@ void CmpSeabaseMDcleanup::cleanupObjects(StmtDDLCleanupObjects * stmtCleanupNode
       return cleanupSchemaObjects(&cliInterface);
     }
 
+  if (stmtCleanupNode &&
+      ((stmtCleanupNode->getType() == StmtDDLCleanupObjects::HIVE_TABLE_) ||
+       (stmtCleanupNode->getType() == StmtDDLCleanupObjects::HIVE_VIEW_)))
+    {
+      return cleanupHiveObject(stmtCleanupNode, &cliInterface);
+    }
+
   if (gatherDependentObjects(&cliInterface))
     {
       if (stopOnError_)

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/CmpSeabaseDDLcleanup.h
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/CmpSeabaseDDLcleanup.h b/core/sql/sqlcomp/CmpSeabaseDDLcleanup.h
index 6d10e19..4afe00e 100644
--- a/core/sql/sqlcomp/CmpSeabaseDDLcleanup.h
+++ b/core/sql/sqlcomp/CmpSeabaseDDLcleanup.h
@@ -114,6 +114,13 @@ class CmpSeabaseMDcleanup : public CmpSeabaseDDL
   short cleanupOrphanViewsEntries(ExeCliInterface *cliInterface,
                                   ExpHbaseInterface *ehi,
                                   CmpDDLwithStatusInfo *dws);
+
+  short cleanupInconsistentHiveEntries(
+       ExeCliInterface *cliInterface,
+       ExpHbaseInterface *ehi);
+
+  void cleanupHiveObject(const StmtDDLCleanupObjects * stmtCleanupNode,
+                         ExeCliInterface *cliInterface);
     
   void populateBlackBox(ExeCliInterface *cliInterface,
                         Queue *returnDetailsList,
@@ -136,6 +143,7 @@ class CmpSeabaseMDcleanup : public CmpSeabaseDDL
     HBASE_ENTRIES,
     INCONSISTENT_OBJECTS_ENTRIES,
     VIEWS_ENTRIES,
+    HIVE_ENTRIES,
     DONE_CLEANUP
   };
 
@@ -175,6 +183,7 @@ class CmpSeabaseMDcleanup : public CmpSeabaseDDL
   Lng32 numOrphanHbaseEntries_;
   Lng32 numOrphanObjectsEntries_;
   Lng32 numOrphanViewsEntries_;
+  Lng32 numInconsistentHiveEntries_;
 };
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp b/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp
index 13e3f9a..c1339f7 100644
--- a/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp
+++ b/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp
@@ -3694,7 +3694,8 @@ Int64 CmpSeabaseDDL::getObjectInfo(
                                    Int32 & schemaOwner,
                                    Int64 & objectFlags,
                                    bool reportErrorNow,
-                                   NABoolean checkForValidDef)
+                                   NABoolean checkForValidDef,
+                                   Int64 *createTime)
 {
   Lng32 retcode = 0;
   Lng32 cliRC = 0;
@@ -3713,7 +3714,7 @@ Int64 CmpSeabaseDDL::getObjectInfo(
     strcpy(cfvd, " and valid_def = 'Y' ");
 
   char buf[4000];
-  str_sprintf(buf, "select object_uid, object_owner, schema_owner, flags from %s.\"%s\".%s where catalog_name = '%s' and schema_name = '%s' and object_name = '%s'  and object_type = '%s' %s ",
+  str_sprintf(buf, "select object_uid, object_owner, schema_owner, flags, create_time from %s.\"%s\".%s where catalog_name = '%s' and schema_name = '%s' and object_name = '%s'  and object_type = '%s' %s ",
               getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_OBJECTS,
               catName, quotedSchName.data(), quotedObjName.data(),
               objectTypeLit, cfvd);
@@ -3759,6 +3760,11 @@ Int64 CmpSeabaseDDL::getObjectInfo(
   cliInterface->getPtrAndLen(4, ptr, len);
   objectFlags = *(Int64*)ptr;
 
+  // return create_time
+  cliInterface->getPtrAndLen(5, ptr, len);
+  if (createTime)
+    *createTime = *(Int64*)ptr;
+  
   cliInterface->fetchRowsEpilogue(NULL, TRUE);
 
   return objUID;
@@ -4085,7 +4091,7 @@ short CmpSeabaseDDL::getAllUsingViews(ExeCliInterface *cliInterface,
   str_sprintf(buf, "select '\"' || trim(o.catalog_name) || '\"' || '.' || '\"' || trim(o.schema_name) || '\"' || '.' || '\"' || trim(o.object_name) || '\"' "
     ", o.create_time from %s.\"%s\".%s O, "
     " (get all views on table \"%s\".\"%s\".\"%s\") x(a) "
-    " where trim(O.schema_name) || '.' || trim(O.object_name) = x.a "
+    " where trim(O.catalog_name) || '.' || trim(O.schema_name) || '.' || trim(O.object_name) = x.a "
     " order by 2",
               getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_OBJECTS,
               catName.data(), schName.data(), objName.data());
@@ -4555,7 +4561,7 @@ short CmpSeabaseDDL::updateSeabaseMDObjectsTable(
               objUID,
               createTime, 
               createTime,
-              validDef,
+              (validDef ? validDef : COM_YES_LIT),
               COM_NO_LIT,
               objOwnerID,
               schemaOwnerID,
@@ -4572,6 +4578,39 @@ short CmpSeabaseDDL::updateSeabaseMDObjectsTable(
     
 }
 
+short CmpSeabaseDDL::deleteFromSeabaseMDObjectsTable(
+     ExeCliInterface *cliInterface,
+     const char * catName,
+     const char * schName,
+     const char * objName,
+     const ComObjectType & objectType)
+{
+  Lng32 retcode = 0;
+  Lng32 cliRC = 0;
+
+  char buf[4000];
+
+  NAString quotedSchName;
+  ToQuotedString(quotedSchName, NAString(schName), FALSE);
+  NAString quotedObjName;
+  ToQuotedString(quotedObjName, NAString(objName), FALSE);
+  char objectTypeLit[3] = {0};
+  strncpy(objectTypeLit,PrivMgr::ObjectEnumToLit(objectType),2);
+
+  str_sprintf(buf, "delete from %s.\"%s\".%s where catalog_name = '%s' and schema_name = '%s' and object_name = '%s' and object_type = '%s' ",
+              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_OBJECTS,
+              catName, quotedSchName.data(), quotedObjName.data(), objectTypeLit);
+  cliRC = cliInterface->executeImmediate(buf);
+  if (cliRC < 0)
+    {
+      cliInterface->retrieveSQLDiagnostics(CmpCommon::diags());
+
+      return -1;
+    }
+
+  return 0;
+}
+  
 static short AssignColEntry(ExeCliInterface *cliInterface, Lng32 entry,
                             char * currRWRSptr, const char * srcPtr, 
                             Lng32 firstColOffset)
@@ -4636,7 +4675,8 @@ short CmpSeabaseDDL::updateSeabaseMDTable(
                                          const ComTdbVirtTableKeyInfo * keyInfo,
                                          Lng32 numIndexes,
                                          const ComTdbVirtTableIndexInfo * indexInfo,
-                                         Int64 &inUID)
+                                         Int64 &inUID,
+                                         NABoolean updPrivs)
 {
   NABoolean useRWRS = FALSE;
   if (CmpCommon::getDefault(TRAF_USE_RWRS_FOR_MD_INSERT) == DF_ON)
@@ -5019,9 +5059,9 @@ short CmpSeabaseDDL::updateSeabaseMDTable(
         }
     } // is an index
 
-
    // Grant owner privileges
-  if (isAuthorizationEnabled())
+  if ((isAuthorizationEnabled()) &&
+      (updPrivs))
     {
       NAString fullName (catName);
       fullName += ".";
@@ -5200,23 +5240,16 @@ short CmpSeabaseDDL::deleteFromSeabaseMDTable(
   Lng32 cliRC = 0;
 
   char buf[4000];
-
-  NAString quotedSchName;
-  ToQuotedString(quotedSchName, NAString(schName), FALSE);
-  NAString quotedObjName;
-  ToQuotedString(quotedObjName, NAString(objName), FALSE);
   char objectTypeLit[3] = {0};
   strncpy(objectTypeLit,PrivMgr::ObjectEnumToLit(objType),2);
-
   Int64 objUID = getObjectUID(cliInterface, catName, schName, objName, objectTypeLit);
 
   if (objUID < 0)
      return -1;
 
-  str_sprintf(buf, "delete from %s.\"%s\".%s where catalog_name = '%s' and schema_name = '%s' and object_name = '%s' and object_type = '%s' ",
-              getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_OBJECTS,
-              catName, quotedSchName.data(), quotedObjName.data(), objectTypeLit);
-  cliRC = cliInterface->executeImmediate(buf);
+  cliRC = deleteFromSeabaseMDObjectsTable(cliInterface,
+                                          catName, schName, objName,
+                                          objType);
   if (cliRC < 0)
     {
       cliInterface->retrieveSQLDiagnostics(CmpCommon::diags());
@@ -6893,7 +6926,7 @@ short CmpSeabaseDDL::dropSeabaseObject(ExpHbaseInterface * ehi,
   const NAString catalogNamePart = tableName.getCatalogNamePartAsAnsiString();
   const NAString schemaNamePart = tableName.getSchemaNamePartAsAnsiString(TRUE);
   const NAString objectNamePart = tableName.getObjectNamePartAsAnsiString(TRUE);
-  const NAString extTableName = tableName.getExternalName(TRUE);
+  NAString extTableName = tableName.getExternalName(TRUE);
   const NAString extNameForHbase = catalogNamePart + "." + schemaNamePart + "." + objectNamePart;
 
   ExeCliInterface cliInterface(STMTHEAP, NULL, NULL,
@@ -6902,29 +6935,72 @@ short CmpSeabaseDDL::dropSeabaseObject(ExpHbaseInterface * ehi,
   if (dropFromMD)
     {
       if (isAuthorizationEnabled())
-      {
-        // Revoke owner privileges for object
-        //   it would be more efficient to pass in the object owner and UID
-        //   than to do an extra I/O.
-        Int32 objOwnerID = 0;
-        Int32 schemaOwnerID = 0;
-        Int64 objectFlags = 0;
-        Int64 objUID = getObjectInfo(&cliInterface,
+        {
+          NABoolean isHive = FALSE;
+          NAString hiveNativeName;
+          Int32 objOwnerID = 0;
+          Int32 schemaOwnerID = 0;
+          Int64 objectFlags = 0;
+          Int64 objUID = -1;
+
+          // remove priv info if this hive table is not registered in traf
+          // metadata. Could happen for hive external tables created prior
+          // to hive registration support.
+          if ((ComIsTrafodionExternalSchemaName(schemaNamePart, &isHive)) &&
+              (isHive))
+            {
+              NAString quotedSchemaName;
+              
+              quotedSchemaName = '\"';
+              quotedSchemaName += schemaNamePart;
+              quotedSchemaName += '\"';
+
+              hiveNativeName = ComConvertTrafNameToNativeName(
+                   catalogNamePart, quotedSchemaName, objectNamePart);
+              
+              // see if this hive table has been registered in traf metadata
+              ComObjectName hiveTableName(hiveNativeName);
+              const NAString hiveCatName = hiveTableName.getCatalogNamePartAsAnsiString();
+              const NAString hiveSchName = hiveTableName.getSchemaNamePartAsAnsiString();
+              const NAString hiveObjName = hiveTableName.getObjectNamePartAsAnsiString();           
+              CorrName cn(hiveObjName, STMTHEAP, hiveSchName, hiveCatName);
+              BindWA bindWA(ActiveSchemaDB(),CmpCommon::context(),FALSE/*inDDL*/);
+              NATable *naTable = bindWA.getNATableInternal(cn);
+              if ((naTable) && (naTable->isHiveTable()))
+                {
+                  if (NOT naTable->isRegistered())
+                    {
+                      objUID = naTable->objectUid().get_value();
+                      extTableName = hiveTableName.getExternalName(TRUE);
+
+                      objOwnerID = naTable->getOwner();
+                      schemaOwnerID = naTable->getSchemaOwner();
+                    }
+                }
+            }
+          
+          
+          // Revoke owner privileges for object
+          //   it would be more efficient to pass in the object owner and UID
+          //   than to do an extra I/O.
+          if (objUID < 0)
+            {
+              objUID = getObjectInfo(&cliInterface,
                                      catalogNamePart.data(), schemaNamePart.data(),
                                      objectNamePart.data(), objType,
                                      objOwnerID,schemaOwnerID,objectFlags);
-
-        if (objUID < 0 || objOwnerID == 0)
-          { //TODO: Internal error?
-            return -1;
-          }
-
-        if (!deletePrivMgrInfo ( extTableName, objUID, objType )) 
-          {
-            return -1;
-          }
-      }
-
+            }
+          if (objUID < 0 || objOwnerID == 0)
+            { //TODO: Internal error?
+              return -1;
+            }
+          
+          if (!deletePrivMgrInfo ( extTableName, objUID, objType )) 
+            {
+              return -1;
+            }
+        }
+      
       if (deleteFromSeabaseMDTable(&cliInterface, 
                                    catalogNamePart, schemaNamePart, objectNamePart, objType ))
         return -1;
@@ -8379,14 +8455,20 @@ NABoolean CmpSeabaseDDL::insertPrivMgrInfo(const Int64 objUID,
   if (!PrivMgr::isSecurableObject(objectType))
     return TRUE;
 
-  // View privileges are handled differently than other objects.  For views,
+  // Traf view privileges are handled differently than other objects. For views,
   // the creator does not automatically get all privileges.  Therefore, view 
   // owner privileges are not granted through this mechanism - 
   // see gatherViewPrivileges for details on how owner privileges are 
-  // calculated and granted. Just return TRUE.
+  // calculated and granted.
+  // Hive views are created outside of trafodion. They are treated like base 
+  // table objects.
+  // Return TRUE if traf views.
   if (objectType == COM_VIEW_OBJECT)
-    return TRUE;
-
+    {
+      ComObjectName viewName(objName, COM_TABLE_NAME);
+      if (viewName.getCatalogNamePartAsAnsiString() == TRAFODION_SYSCAT_LIT)
+        return TRUE;
+    }
 
   // If authorization is not enabled, return TRUE, no grants are needed
   if (!isAuthorizationEnabled())
@@ -9345,6 +9427,13 @@ short CmpSeabaseDDL::executeSeabaseDDL(DDLExpr * ddlExpr, ExprNode * ddlNode,
           else
             unregisterSeabaseUser(registerUserParseNode);
         }
+      else if (ddlNode->getOperatorType() == DDL_REG_OR_UNREG_HIVE)
+        {
+         StmtDDLRegOrUnregHive *regOrUnregHiveParseNode =
+            ddlNode->castToStmtDDLNode()->castToStmtDDLRegOrUnregHive();
+         regOrUnregHiveObjects(
+              regOrUnregHiveParseNode, currCatName, currSchName);
+        }
       else if (ddlNode->getOperatorType() == DDL_CREATE_ROLE)
         {
          StmtDDLCreateRole *createRoleParseNode =

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/CmpSeabaseDDLincludes.h
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/CmpSeabaseDDLincludes.h b/core/sql/sqlcomp/CmpSeabaseDDLincludes.h
index 2743bdd..3b7b47a 100644
--- a/core/sql/sqlcomp/CmpSeabaseDDLincludes.h
+++ b/core/sql/sqlcomp/CmpSeabaseDDLincludes.h
@@ -68,6 +68,7 @@
 #include "StmtDDLRevokeComponentPrivilege.h"
 #include "StmtDDLRegisterComponent.h"
 #include "StmtDDLCleanupObjects.h"
+#include "StmtDDLRegOrUnregHive.h"
 
 #include "ElemDDLHbaseOptions.h"
 #include "ElemDDLParamDefArray.h"

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/CmpSeabaseDDLmd.h
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/CmpSeabaseDDLmd.h b/core/sql/sqlcomp/CmpSeabaseDDLmd.h
index 64434f4..da072a2 100644
--- a/core/sql/sqlcomp/CmpSeabaseDDLmd.h
+++ b/core/sql/sqlcomp/CmpSeabaseDDLmd.h
@@ -265,10 +265,14 @@ enum SeabaseObjectsFlags {
   // stored descriptor is disabled, should not be used
   MD_OBJECTS_DISABLE_STORED_DESC   = 0x0000000000000008,
 
-  // set if this object was created as an implicit external hive table.
+  // set if this object was created as an implicit external table.
   // This happens if an external table was created for internal usage
   // (currently for: privilege info, ustat, views).
-  SEABASE_OBJECT_IS_IMPLICIT_EXTERNAL_HIVE  = 0x0000000000000010
+  SEABASE_OBJECT_IS_IMPLICIT_EXTERNAL  = 0x0000000000000010,
+
+  // set if this hive object was internally registered in traf metadata.
+  // This can happen for upd stats, create views or grant privs.
+  MD_OBJECTS_HIVE_INTERNAL_REGISTER  = 0x0000000000000020
 
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp b/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp
index 0485f9b..565eb18 100644
--- a/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp
+++ b/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp
@@ -53,6 +53,8 @@
 #include "PrivMgrRoles.h"
 #include "PrivMgrComponentPrivileges.h"
 
+#include "RelExeUtil.h"
+
 #include "TrafDDLdesc.h"
 
 // defined in CmpDescribe.cpp
@@ -492,10 +494,10 @@ short CmpSeabaseDDL::createSeabaseTableExternal(
 
   const NAString extTgtTableName = tgtTableName.getExternalName(TRUE);
 
-  CorrName cnSrc(srcTableName.getObjectNamePart().getInternalName(),
-              STMTHEAP,
-              srcTableName.getSchemaNamePart().getInternalName(),
-              srcTableName.getCatalogNamePart().getInternalName());
+  const NAString srcCatNamePart = srcTableName.getCatalogNamePartAsAnsiString();
+  const NAString srcSchNamePart = srcTableName.getSchemaNamePartAsAnsiString(TRUE);
+  const NAString srcObjNamePart = srcTableName.getObjectNamePartAsAnsiString(TRUE);
+  CorrName cnSrc(srcObjNamePart, STMTHEAP, srcSchNamePart, srcCatNamePart);
 
   // build the structures needed to create the table
   // tableInfo contains data inserted into OBJECTS and TABLES
@@ -512,11 +514,13 @@ short CmpSeabaseDDL::createSeabaseTableExternal(
   if (isHive)
     {
       tableInfo->objectFlags = SEABASE_OBJECT_IS_EXTERNAL_HIVE;
-      if (createTableNode->isImplicitExternal())
-        tableInfo->objectFlags |= SEABASE_OBJECT_IS_IMPLICIT_EXTERNAL_HIVE;
     }
   else
-    tableInfo->objectFlags = SEABASE_OBJECT_IS_EXTERNAL_HBASE;
+    {
+      tableInfo->objectFlags = SEABASE_OBJECT_IS_EXTERNAL_HBASE;
+      if (createTableNode->isImplicitExternal())
+        tableInfo->objectFlags |= SEABASE_OBJECT_IS_IMPLICIT_EXTERNAL;
+    }
   tableInfo->tablesFlags = 0;
 
   if (isAuthorizationEnabled())
@@ -726,8 +730,34 @@ short CmpSeabaseDDL::createSeabaseTableExternal(
         }
     }
 
+  NABoolean registerHiveObject =
+    (cnSrc.isHive() &&
+     CmpCommon::getDefault(HIVE_NO_REGISTER_OBJECTS) == DF_OFF);
+
+  // if source table is a hive table and not already registered, register
+  // it in traf metadata
+  if (registerHiveObject)
+    {
+      char buf[2000];
+      str_sprintf(buf, "register internal hive table if not exists %s.%s.%s",
+                  srcCatNamePart.data(), 
+                  srcSchNamePart.data(), 
+                  srcObjNamePart.data());
+      
+      Lng32 cliRC = cliInterface.executeImmediate(buf);
+      if (cliRC < 0)
+        {
+          cliInterface.retrieveSQLDiagnostics(CmpCommon::diags());
+          return -1;
+        }
+    } // ishive
+
   Int64 objUID = -1;
   cliRC = 0;
+
+  // Update traf MD tables with info about this table.
+  // But do not insert priv info if object is to be registered. 
+  // That will happen during hive object registration.
   if (updateSeabaseMDTable(&cliInterface,
                            catalogNamePart, schemaNamePart, objectNamePart,
                            COM_BASE_TABLE_OBJECT,
@@ -738,7 +768,8 @@ short CmpSeabaseDDL::createSeabaseTableExternal(
                            0 /*numKeys*/,
                            NULL /*keyInfoArray*/,
                            0, NULL,
-                           objUID /*returns generated UID*/))
+                           objUID /*returns generated UID*/,
+                           (NOT registerHiveObject)))
     {
       *CmpCommon::diags()
         << DgSqlCode(-CAT_UNABLE_TO_CREATE_OBJECT)
@@ -3655,7 +3686,7 @@ short CmpSeabaseDDL::dropSeabaseTable2(
   if (CmpCommon::getDefault(TRAF_RELOAD_NATABLE_CACHE) == DF_OFF)
     ActiveSchemaDB()->getNATableDB()->useCache();
 
- // save the current parserflags setting
+  // save the current parserflags setting
   ULng32 savedParserFlags = Get_SqlParser_Flags (0xFFFFFFFF);
   Set_SqlParser_Flags(ALLOW_VOLATILE_SCHEMA_IN_TABLE_NAME);
 
@@ -3703,18 +3734,7 @@ short CmpSeabaseDDL::dropSeabaseTable2(
       return -1;
     }
 
-  Int64 objUID = getObjectUID(cliInterface,
-                              catalogNamePart.data(), schemaNamePart.data(), 
-                              objectNamePart.data(),
-                              COM_BASE_TABLE_OBJECT_LIT);
-  if (objUID < 0)
-    {
-
-      deallocEHI(ehi); 
-      processReturn();
-
-      return -1;
-    }
+  Int64 objUID = naTable->objectUid().castToInt64();
 
   // Make sure user has necessary privileges to perform drop
   if (!isDDLOperationAuthorized(SQLOperation::DROP_TABLE,
@@ -4373,6 +4393,17 @@ short CmpSeabaseDDL::dropSeabaseTable2(
      ComQiScope::REMOVE_FROM_ALL_USERS, COM_BASE_TABLE_OBJECT,
      dropTableNode->ddlXns(), FALSE);
   
+  // if hive external table, remove hive entry from NATable cache as well
+  if ((dropTableNode->isExternal()) &&
+      (dropTableNode->getTableNameAsQualifiedName().isHive()))
+    {
+      CorrName hcn(dropTableNode->getTableNameAsQualifiedName());
+      ActiveSchemaDB()->getNATableDB()->removeNATable
+        (hcn,
+         ComQiScope::REMOVE_FROM_ALL_USERS, COM_BASE_TABLE_OBJECT,
+         dropTableNode->ddlXns(), FALSE);
+    }
+
   for (Int32 i = 0; i < refList.entries(); i++)
     {
       AbstractRIConstraint *ariConstr = refList[i];
@@ -9478,18 +9509,6 @@ void CmpSeabaseDDL::seabaseGrantRevoke(
   ExeCliInterface cliInterface(STMTHEAP, NULL, NULL, 
   CmpCommon::context()->sqlSession()->getParentQid());
 
-  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;
-    }
 
   BindWA bindWA(ActiveSchemaDB(), CmpCommon::context(), FALSE/*inDDL*/);
   CorrName cn(tableName.getObjectNamePart().getInternalName(),
@@ -9537,6 +9556,8 @@ void CmpSeabaseDDL::seabaseGrantRevoke(
       objectOwnerID = (int32_t)naTable->getOwner();
       schemaOwnerID = naTable->getSchemaOwner();
       objectType = naTable->getObjectType();
+      if (naTable->isView())
+        objectType = COM_VIEW_OBJECT;
     }
 
   ElemDDLGranteeArray & pGranteeArray = 
@@ -9622,10 +9643,9 @@ void CmpSeabaseDDL::seabaseGrantRevoke(
       return;
     }
 
-  // for Hive tables, the table must have an external table defined
-  if (objectUID == 0 && naTable &&
-      (naTable->isHiveTable() &&
-      !naTable->hasExternalTable()))
+  // Hive tables must be registered in traf metadata
+  if (objectUID == 0 &&
+      naTable && naTable->isHiveTable())
     {
       // For native hive tables, grantor must be DB__ROOT or belong
       // to one of the admin roles:  DB__ROOTROLE, DB__HIVEROLE
@@ -9640,13 +9660,11 @@ void CmpSeabaseDDL::seabaseGrantRevoke(
           return;
         }
 
-      // create an external table for this hive table.
-      // if the trafodion schema containing hive table descriptions does
-      // not exists, the "create external" table command creates it.
+      // register this hive table in traf metadata
       char query[(ComMAX_ANSI_IDENTIFIER_EXTERNAL_LEN*4) + 100];
       snprintf(query, sizeof(query),
-               "create implicit external table \"%s\" for %s.\"%s\".\"%s\"",
-               objectNamePart.data(),
+               "register internal hive %s if not exists %s.\"%s\".\"%s\"",
+               (naTable->isView() ? "view" : "table"),
                catalogNamePart.data(),
                schemaNamePart.data(),
                objectNamePart.data());
@@ -9657,12 +9675,7 @@ void CmpSeabaseDDL::seabaseGrantRevoke(
           return;
         }
 
-      // remove NATable and reload it to include external table defn.
-      ActiveSchemaDB()->getNATableDB()->removeNATable
-        (cn,
-         ComQiScope::REMOVE_MINE_ONLY, COM_BASE_TABLE_OBJECT,
-         FALSE, FALSE);
-
+      // reload NATable to get registered objectUID
       naTable = bindWA.getNATable(cn);
       if (naTable == NULL)
         {
@@ -9674,15 +9687,16 @@ void CmpSeabaseDDL::seabaseGrantRevoke(
       objectOwnerID = (int32_t)naTable->getOwner();
       schemaOwnerID = naTable->getSchemaOwner();
       objectType = naTable->getObjectType();
+      if (naTable->isView())
+        objectType = COM_VIEW_OBJECT;
     }
 
-
   // for metadata tables, the objectUID is not initialized in the NATable
   // structure
   if (objectUID == 0)
     {
       ExeCliInterface cliInterface(STMTHEAP, NULL, NULL, 
-  CmpCommon::context()->sqlSession()->getParentQid());
+                                   CmpCommon::context()->sqlSession()->getParentQid());
       objectUID = getObjectInfo(&cliInterface,
                                catalogNamePart.data(), schemaNamePart.data(),
                                objectNamePart.data(), objectType,
@@ -10083,6 +10097,615 @@ void CmpSeabaseDDL::dropNativeHbaseTable(
   
 }
 
+short CmpSeabaseDDL::registerHiveTable
+(
+     const NAString &catalogNamePart,
+     const NAString &schemaNamePart,
+     const NAString &objectNamePart,
+     Int32 objOwnerId,
+     Int32 schemaOwnerId,
+     ExeCliInterface &cliInterface,
+     NABoolean isRegister,
+     NABoolean isInternal
+ )
+{
+  Lng32 retcode = 0;
+
+  ComObjectType objType = COM_BASE_TABLE_OBJECT;
+
+  Int64 flags = 0;
+  if (isRegister && isInternal)
+    flags = MD_OBJECTS_HIVE_INTERNAL_REGISTER;
+  
+  Int64 objUID = -1;
+  retcode =
+    updateSeabaseMDObjectsTable
+    (&cliInterface,
+     catalogNamePart.data(),
+     schemaNamePart.data(),
+     objectNamePart.data(),
+     objType,
+     NULL,
+     objOwnerId, schemaOwnerId,
+     flags, objUID);
+  if (retcode < 0)
+    return -1;
+
+  // Grant owner privileges
+  if (isAuthorizationEnabled())
+    {
+      NAString fullName (catalogNamePart);
+      fullName += ".";
+      fullName += schemaNamePart;
+      fullName += ".";
+      fullName += objectNamePart;
+      if (!insertPrivMgrInfo(objUID,
+                             fullName,
+                             objType,
+                             objOwnerId,
+                             schemaOwnerId,
+                             ComUser::getCurrentUser()))
+        {
+          *CmpCommon::diags()
+            << DgSqlCode(-CAT_UNABLE_TO_GRANT_PRIVILEGES)
+            << DgTableName(objectNamePart);
+          return -1;
+        }
+      
+    }
+ 
+  return 0;
+}
+
+short CmpSeabaseDDL::unregisterHiveTable
+(
+     const NAString &catalogNamePart,
+     const NAString &schemaNamePart,
+     const NAString &objectNamePart,
+     ExeCliInterface &cliInterface
+ )
+{
+  short retcode = 0;
+
+  ComObjectType objType = COM_BASE_TABLE_OBJECT;
+
+  Int64 objUID = getObjectUID(&cliInterface,
+                              catalogNamePart.data(), 
+                              schemaNamePart.data(), 
+                              objectNamePart.data(),
+                              comObjectTypeLit(objType));
+  
+  // Revoke owner privileges
+  if (isAuthorizationEnabled())
+    {
+      NAString fullName (catalogNamePart);
+      fullName += ".";
+      fullName += schemaNamePart;
+      fullName += ".";
+      fullName += objectNamePart;
+      if (!deletePrivMgrInfo(fullName,
+                             objUID,
+                             objType))
+        {
+          *CmpCommon::diags()
+            << DgSqlCode(-CAT_PRIVILEGE_NOT_REVOKED)
+            << DgTableName(objectNamePart);
+          return -1;
+        }
+    }
+  
+  // delete hist stats, if HIST tables exist
+  retcode = existsInSeabaseMDTable
+    (&cliInterface, 
+     HIVE_STATS_CATALOG, HIVE_STATS_SCHEMA_NO_QUOTES, HBASE_HIST_NAME,
+     COM_BASE_TABLE_OBJECT);
+  if (retcode < 0)
+    return -1;
+  
+  if (retcode == 1) // exists
+    {
+      if (dropSeabaseStats(&cliInterface,
+                           HIVE_STATS_CATALOG,
+                           HIVE_STATS_SCHEMA_NO_QUOTES,
+                           objUID))
+        {
+          return -1;
+        }
+    }
+  
+  // drop from metadata
+  retcode =
+    deleteFromSeabaseMDObjectsTable
+    (&cliInterface,
+     catalogNamePart.data(),
+     schemaNamePart.data(),
+     objectNamePart.data(),
+     objType
+     );
+  
+  return 0;
+}
+
+short CmpSeabaseDDL::registerHiveView
+(
+     const NAString &catalogNamePart,
+     const NAString &schemaNamePart,
+     const NAString &objectNamePart,
+     Int32 objOwnerId,
+     Int32 schemaOwnerId,
+     NATable *naTable,
+     ExeCliInterface &cliInterface,
+     NABoolean isInternal,
+     NABoolean cascade
+ )
+{
+  Lng32 retcode = 0;
+
+  Int64 flags = 0;
+  if (isInternal)
+    flags = MD_OBJECTS_HIVE_INTERNAL_REGISTER;
+
+  ComObjectType objType = COM_VIEW_OBJECT;
+
+  Int64 objUID = -1;
+  retcode =
+    updateSeabaseMDObjectsTable
+    (&cliInterface,
+     catalogNamePart.data(),
+     schemaNamePart.data(),
+     objectNamePart.data(),
+     objType,
+     NULL,
+     objOwnerId, schemaOwnerId,
+     flags, objUID);
+  if (retcode < 0)
+    return -1;
+
+  // Grant owner privileges
+  if (isAuthorizationEnabled())
+    {
+      NAString fullName (catalogNamePart);
+      fullName += ".";
+      fullName += schemaNamePart;
+      fullName += ".";
+      fullName += objectNamePart;
+      if (!insertPrivMgrInfo(objUID,
+                             fullName,
+                             objType,
+                             objOwnerId,
+                             schemaOwnerId,
+                             ComUser::getCurrentUser()))
+        {
+          *CmpCommon::diags()
+            << DgSqlCode(-CAT_UNABLE_TO_GRANT_PRIVILEGES)
+            << DgTableName(objectNamePart);
+          return -1;
+        }
+    }
+
+  // if cascade option is specified, find out all objects that are part
+  // of this view. Register them in traf metadata and update view usage
+  // metadata table.
+  if (cascade)
+    {
+      BindWA bindWA(ActiveSchemaDB(), CmpCommon::context(), FALSE/*inDDL*/);
+
+      // temporarily change the default schema to
+      // that of viewName. 
+      // This will make sure that all unqualified objects in view
+      // text are expanded in this schema. 
+      SchemaName s(schemaNamePart, catalogNamePart);
+      bindWA.setDefaultSchema(s);
+
+      Parser parser(bindWA.currentCmpContext());
+      ExprNode *viewTree = parser.parseDML(naTable->getViewText(),
+                                           naTable->getViewLen(),
+                                           naTable->getViewTextCharSet());
+      if (! viewTree)
+        {
+          return -1;
+        }
+
+      RelExpr *queryTree = 
+        viewTree->castToStatementExpr()->getQueryExpression();
+
+      StmtDDLCreateView *createViewNode = 
+        ((DDLExpr *)(queryTree->getChild(0)))->
+        getDDLNode()->castToStmtDDLNode()->castToStmtDDLCreateView();
+      
+      ExprNode * boundTree = createViewNode->bindNode(&bindWA);
+      if ((! boundTree) || (bindWA.errStatus()) ||
+          (CmpCommon::diags()->getNumber(DgSqlCode::ERROR_)))
+        {
+          return -1;
+        }
+      
+      if (updateViewUsage(createViewNode, objUID, &cliInterface))
+        {
+          return -1;
+        }
+    }
+
+  return 0;
+}
+
+short CmpSeabaseDDL::unregisterHiveView
+(
+     const NAString &catalogNamePart,
+     const NAString &schemaNamePart,
+     const NAString &objectNamePart,
+     NATable *naTable,
+     ExeCliInterface &cliInterface,
+     NABoolean cascade
+ )
+{
+  Lng32 retcode = 0;
+
+  Int64 objUID = getObjectUID(&cliInterface,
+                              catalogNamePart.data(), 
+                              schemaNamePart.data(), 
+                              objectNamePart.data(),
+                              comObjectTypeLit(COM_VIEW_OBJECT));
+
+  // Revoke owner privileges
+  if (isAuthorizationEnabled())
+    {
+      NAString fullName (catalogNamePart);
+      fullName += ".";
+      fullName += schemaNamePart;
+      fullName += ".";
+      fullName += objectNamePart;
+      if (!deletePrivMgrInfo(fullName,
+                             objUID,
+                             COM_VIEW_OBJECT))
+        {
+          *CmpCommon::diags()
+            << DgSqlCode(-CAT_PRIVILEGE_NOT_REVOKED)
+            << DgTableName(objectNamePart);
+          return -1;
+        }
+    }
+  
+  // delete hist stats, if HIST tables exist
+  retcode = existsInSeabaseMDTable
+    (&cliInterface, 
+     HIVE_STATS_CATALOG, HIVE_STATS_SCHEMA_NO_QUOTES, HBASE_HIST_NAME,
+     COM_BASE_TABLE_OBJECT);
+  if (retcode < 0)
+    return -1;
+  
+  if (retcode == 1) // exists
+    {
+      if (dropSeabaseStats(&cliInterface,
+                           HIVE_STATS_CATALOG,
+                           HIVE_STATS_SCHEMA_NO_QUOTES,
+                           objUID))
+        {
+          return -1;
+        }
+    }
+  
+  // drop from metadata
+  retcode =
+    //    deleteFromSeabaseMDObjectsTable
+    deleteFromSeabaseMDTable
+    (&cliInterface,
+     catalogNamePart.data(),
+     schemaNamePart.data(),
+     objectNamePart.data(),
+     COM_VIEW_OBJECT
+     );
+
+  if (retcode < 0)
+    return -1;
+  
+  // if cascade option is specified, find out all objects that are part
+  // of this view. Unregister them from traf metadata and update view usage
+  // metadata table.
+  if ((cascade) && (naTable))
+    {
+      BindWA bindWA(ActiveSchemaDB(), CmpCommon::context(), FALSE/*inDDL*/);
+      
+      // temporarily change the default schema to
+      // that of viewName. 
+      // This will make sure that all unqualified objects in view
+      // text are expanded in this schema. 
+      SchemaName s(schemaNamePart, catalogNamePart);
+      bindWA.setDefaultSchema(s);
+
+      Parser parser(bindWA.currentCmpContext());
+      ExprNode *viewTree = parser.parseDML(naTable->getViewText(),
+                                           naTable->getViewLen(),
+                                           naTable->getViewTextCharSet());
+      if (! viewTree)
+        {
+          return -1;
+        }
+
+      RelExpr *queryTree = 
+        viewTree->castToStatementExpr()->getQueryExpression();
+
+      StmtDDLCreateView *createViewNode = 
+        ((DDLExpr *)(queryTree->getChild(0)))->
+        getDDLNode()->castToStmtDDLNode()->castToStmtDDLCreateView();
+      
+      ExprNode * boundTree = createViewNode->bindNode(&bindWA);
+      if ((! boundTree) || (bindWA.errStatus()) ||
+          (CmpCommon::diags()->getNumber(DgSqlCode::ERROR_)))
+        {
+          return -1;
+        }
+      
+      if (unregisterHiveViewUsage(createViewNode, objUID, &cliInterface))
+        {
+          return -1;
+        }
+    }
+
+  return 0;
+}
+
+short CmpSeabaseDDL::unregisterHiveViewUsage(StmtDDLCreateView * createViewParseNode,
+                                             Int64 viewUID,
+                                             ExeCliInterface * cliInterface)
+{
+  const ParViewUsages &vu = createViewParseNode->getViewUsages();
+  const ParTableUsageList &vtul = vu.getViewTableUsageList();
+
+  char query[1000];
+  for (CollIndex i = 0; i < vtul.entries(); i++)
+    {
+      ComObjectName usedObjName(vtul[i].getQualifiedNameObj()
+				.getQualifiedNameAsAnsiString(),
+				vtul[i].getAnsiNameSpace());
+      
+      const NAString catalogNamePart = usedObjName.getCatalogNamePartAsAnsiString();
+      const NAString schemaNamePart = usedObjName.getSchemaNamePartAsAnsiString(TRUE);
+      const NAString objectNamePart = usedObjName.getObjectNamePartAsAnsiString(TRUE);
+      const NAString extUsedObjName = usedObjName.getExternalName(TRUE);
+
+      Int64 usedObjUID = -1;      
+      CorrName cn(objectNamePart,STMTHEAP, schemaNamePart,catalogNamePart);
+      
+      BindWA bindWA(ActiveSchemaDB(),CmpCommon::context(),FALSE/*inDDL*/);
+      
+      NATable *naTable = bindWA.getNATableInternal(cn);
+      if ((naTable == NULL) ||
+          (NOT naTable->isHiveTable()) ||
+          (NOT naTable->isRegistered()))
+        {
+          SEABASEDDL_INTERNAL_ERROR("NATable pointer in unregisterHiveViewUsage");
+          return -1; 
+        }
+      
+      // unregister this hive object from traf metadata, if not already 
+      str_sprintf(query, "unregister hive %s if exists %s.\"%s\".\"%s\" ",
+                  (naTable->isView() ? "view" : "table"),
+                  catalogNamePart.data(),
+                  schemaNamePart.data(),
+                  objectNamePart.data());
+      Lng32 cliRC = cliInterface->executeImmediate(query);
+      if (cliRC < 0)
+        {
+          cliInterface->retrieveSQLDiagnostics(CmpCommon::diags());
+          return -1;
+        }
+      
+      // save the current parserflags setting
+      ULng32 savedCliParserFlags;
+      SQL_EXEC_GetParserFlagsForExSqlComp_Internal(savedCliParserFlags_);
+      SQL_EXEC_SetParserFlagsForExSqlComp_Internal(INTERNAL_QUERY_FROM_EXEUTIL);
+      
+      usedObjUID = naTable->objectUid().get_value();
+      str_sprintf(query, "delete from %s.\"%s\".%s where using_view_uid = %Ld and used_object_uid = %Ld",
+		  getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_VIEWS_USAGE,
+		  viewUID,
+		  usedObjUID);
+      cliRC = cliInterface->executeImmediate(query);
+
+      SQL_EXEC_AssignParserFlagsForExSqlComp_Internal(savedCliParserFlags);
+ 
+      if (cliRC < 0)
+	{
+	  cliInterface->retrieveSQLDiagnostics(CmpCommon::diags());
+          
+	  return -1;
+	}
+      
+    } // for
+
+  return 0;
+} 
+
+void CmpSeabaseDDL::regOrUnregHiveObjects (
+     StmtDDLRegOrUnregHive * regOrUnregHiveNode,
+     NAString &currCatName, NAString &currSchName)
+{
+  Lng32 retcode = 0;
+
+  char errReason[400];
+  ExeCliInterface cliInterface(STMTHEAP, NULL, NULL, 
+                               CmpCommon::context()->sqlSession()->getParentQid());
+
+  NAString catalogNamePart = regOrUnregHiveNode->getObjNameAsQualifiedName().
+    getCatalogName();
+  NAString schemaNamePart = regOrUnregHiveNode->getObjNameAsQualifiedName().
+    getSchemaName();
+  NAString objectNamePart = regOrUnregHiveNode->getObjNameAsQualifiedName().
+    getObjectName();
+  ComObjectName tableName;
+  NAString tabName;
+  NAString extTableName;
+
+  // make sure that underlying hive object exists
+  BindWA bindWA(ActiveSchemaDB(), CmpCommon::context(), FALSE/*inDDL*/);
+  CorrName cn(objectNamePart, STMTHEAP, schemaNamePart, catalogNamePart);
+
+  NATable * naTable = bindWA.getNATable(cn);
+  if (((naTable == NULL) || (bindWA.errStatus())) &&
+      ((regOrUnregHiveNode->isRegister()) || // register
+       (NOT regOrUnregHiveNode->cleanup()))) // unreg and not cleanup
+    {
+      *CmpCommon::diags() << DgSqlCode(-3251)
+                          << (regOrUnregHiveNode->isRegister() ? DgString0("REGISTER") :
+                              DgString0("UNREGISTER"))
+                          << DgString1(" ");
+      
+      processReturn();
+      
+      return;
+    }
+  
+  // ignore errors for 'unregister cleanup'
+  CmpCommon::diags()->clear();
+
+  if (naTable)
+    {
+      if (regOrUnregHiveNode->isRegister() && 
+          (naTable->isRegistered())) // already registered
+        {
+          if (NOT regOrUnregHiveNode->existsOption())
+            {
+              str_sprintf(errReason, " Reason: %s has already been registered.",
+                          regOrUnregHiveNode->getObjNameAsQualifiedName().
+                          getQualifiedNameAsString().data());
+              *CmpCommon::diags() << DgSqlCode(-3251)
+                                  << DgString0("REGISTER")
+                                  << DgString1(errReason);
+            }
+          
+          processReturn();
+          
+          return;
+        }
+      else if ((NOT regOrUnregHiveNode->isRegister()) && // unregister
+               (NOT naTable->isRegistered())) // not registered
+        {
+          if (NOT regOrUnregHiveNode->existsOption())
+            {
+              str_sprintf(errReason, " Reason: %s has not been registered.",
+                          regOrUnregHiveNode->getObjNameAsQualifiedName().
+                          getQualifiedNameAsString().data());
+              *CmpCommon::diags() << DgSqlCode(-3251)
+                                  << DgString0("UNREGISTER")
+                                  << DgString1(errReason);
+            }
+          
+          processReturn();
+          
+          return;
+        }
+    }
+
+  // For native hive tables, grantor must be DB__ROOT or belong
+  // to one of the admin roles:  DB__ROOTROLE, DB__HIVEROLE
+  // In hive, you must be an admin, DB__ROOTROLE and DB__HIVEROLE
+  // is the equivalent of an admin.
+  if (!ComUser::isRootUserID() &&
+      !ComUser::currentUserHasRole(ROOT_ROLE_ID) &&
+      !ComUser::currentUserHasRole(HIVE_ROLE_ID)) 
+    {
+      *CmpCommon::diags() << DgSqlCode (-CAT_NOT_AUTHORIZED);
+      processReturn();
+      return;
+    }
+
+  Int32 objOwnerId = HIVE_ROLE_ID;
+  Int32 schemaOwnerId = HIVE_ROLE_ID;
+  Int64 objUID = -1;
+  Int64 flags = 0;
+  if ((regOrUnregHiveNode->isRegister()) &&
+      (regOrUnregHiveNode->objType() == COM_SHARED_SCHEMA_OBJECT))
+    {
+      retcode =
+        updateSeabaseMDObjectsTable
+        (&cliInterface,
+         catalogNamePart.data(),
+         schemaNamePart.data(),
+         "__SCHEMA__",
+         regOrUnregHiveNode->objType(),
+         NULL,
+         objOwnerId, schemaOwnerId,
+         flags, objUID);
+    }
+  else if (regOrUnregHiveNode->isRegister())
+    {
+      if (((regOrUnregHiveNode->objType() == COM_BASE_TABLE_OBJECT) &&
+           (naTable->isView())) ||
+          ((regOrUnregHiveNode->objType() == COM_VIEW_OBJECT) &&
+           (! naTable->isView())))
+        {
+          // underlying object is a view but registered object type specified
+          // in the register statement is a table, or
+          // underlying object is a table but registered object type specified
+          // in the register statement is a view
+          str_sprintf(errReason, " Reason: Mismatch between specified(%s) and underlying(%s) type for %s.",
+                      (regOrUnregHiveNode->objType() == COM_BASE_TABLE_OBJECT ? "TABLE" : "VIEW"),
+                      (naTable->isView() ? "VIEW" : "TABLE"),
+                      regOrUnregHiveNode->getObjNameAsQualifiedName().
+                      getQualifiedNameAsString().data());
+
+          *CmpCommon::diags() << DgSqlCode(-3251)
+                              << DgString0("REGISTER")
+                              << DgString1(errReason);
+          
+          processReturn();
+          
+          return;
+        }
+
+      if (regOrUnregHiveNode->objType() == COM_BASE_TABLE_OBJECT)
+        {
+          retcode = registerHiveTable(
+               catalogNamePart, schemaNamePart, objectNamePart,
+               objOwnerId, schemaOwnerId,
+               cliInterface, 
+               regOrUnregHiveNode->isRegister(), 
+               regOrUnregHiveNode->isInternal());
+        }
+      else // COM_VIEW_OBJECT
+        {
+          retcode = registerHiveView(
+               catalogNamePart, schemaNamePart, objectNamePart,
+               objOwnerId, schemaOwnerId,
+               naTable,
+               cliInterface, 
+               regOrUnregHiveNode->isInternal(),
+               regOrUnregHiveNode->cascade());
+        }
+
+      if (retcode < 0)
+        return;
+    }
+  else // unregister
+    {
+      if (regOrUnregHiveNode->objType() == COM_BASE_TABLE_OBJECT)
+        {
+          retcode = unregisterHiveTable(
+               catalogNamePart, schemaNamePart, objectNamePart,
+               cliInterface);
+        }
+      else // view
+        {
+          retcode = unregisterHiveView(
+               catalogNamePart, schemaNamePart, objectNamePart,
+               naTable,
+               cliInterface,
+               regOrUnregHiveNode->cascade());
+        }
+    } // unregister
+
+  if (retcode < 0)
+    return;
+
+  ActiveSchemaDB()->getNATableDB()->removeNATable
+    (cn,
+     ComQiScope::REMOVE_FROM_ALL_USERS, regOrUnregHiveNode->objType(),
+     FALSE, FALSE);
+  
+  return;
+}
+ 
 /////////////////////////////////////////////////////////////////////////
 // This method generates and returns tableInfo struct for internal special
 // tables (like metadata, histograms). These tables have hardcoded definitions
@@ -10866,13 +11489,18 @@ TrafDesc * CmpSeabaseDDL::getSeabaseUserTableDesc(const NAString &catName,
   // query for the existence check in order to ensure we get the proper error
   // reported.
   //
+  NABoolean checkForValidDef = TRUE;
+  if ((includeInvalidDefs) ||
+      (Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL)) ||
+      (objType == COM_INDEX_OBJECT))
+    checkForValidDef = FALSE;
+  
   if ( objType ) // Must have objType
   {
     objUID = getObjectInfo(&cliInterface,
                            catName.data(), schName.data(), objName.data(),
                            objType, objectOwner, schemaOwner,objectFlags, FALSE /*no error now */,
-                           (Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL) ? FALSE
-                            : (objType != COM_INDEX_OBJECT ? TRUE : FALSE)));
+                           checkForValidDef);
   }
 
   // If we didn't call getObjectInfo() above OR if it gave an error, then:
@@ -10881,8 +11509,7 @@ TrafDesc * CmpSeabaseDDL::getSeabaseUserTableDesc(const NAString &catName,
     cliRC = existsInSeabaseMDTable(&cliInterface, 
                                    catName.data(), schName.data(), objName.data(),
                                    COM_UNKNOWN_OBJECT,
-                                   (Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL) ? FALSE
-                                    : (objType != COM_INDEX_OBJECT ? TRUE : FALSE)),
+                                   checkForValidDef,
                                    TRUE, TRUE);
     if (cliRC < 0)
       {

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/CmpSeabaseDDLview.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/CmpSeabaseDDLview.cpp b/core/sql/sqlcomp/CmpSeabaseDDLview.cpp
index 31950d9..bc210f5 100644
--- a/core/sql/sqlcomp/CmpSeabaseDDLview.cpp
+++ b/core/sql/sqlcomp/CmpSeabaseDDLview.cpp
@@ -371,16 +371,16 @@ short CmpSeabaseDDL::updateViewUsage(StmtDDLCreateView * createViewParseNode,
             }
 
           if ((naTable->isHiveTable()) &&
-              (NOT naTable->getViewText()) &&
-              (NOT naTable->hasExternalTable()) &&
-              (CmpCommon::getDefault(HIVE_VIEWS_CREATE_EXTERNAL_TABLE) == DF_ON))
+              (CmpCommon::getDefault(HIVE_NO_REGISTER_OBJECTS) == DF_OFF))
+
             {
-              // create an external table for this hive table.
-              str_sprintf(query, "create implicit external table \"%s\" for %s.\"%s\".\"%s\"",
-                          objectNamePart.data(), 
+              // register this hive object in traf metadata, if not already 
+              str_sprintf(query, "register internal hive %s if not exists %s.\"%s\".\"%s\" %s",
+                          (naTable->isView() ? "view" : "table"),
                           catalogNamePart.data(),
                           schemaNamePart.data(),
-                          objectNamePart.data());
+                          objectNamePart.data(),
+                          (naTable->isView() ? "cascade" : " "));
               Lng32 cliRC = cliInterface->executeImmediate(query);
               if (cliRC < 0)
                 {
@@ -388,10 +388,12 @@ short CmpSeabaseDDL::updateViewUsage(StmtDDLCreateView * createViewParseNode,
                   return -1;
                 }
 
-              // remove NATable and reload it to include external table defn.
+              // remove NATable and reload it to include object uid of register
+              // operation.
               ActiveSchemaDB()->getNATableDB()->removeNATable
                 (cn,
-                 ComQiScope::REMOVE_MINE_ONLY, COM_BASE_TABLE_OBJECT,
+                 ComQiScope::REMOVE_MINE_ONLY, 
+                 (naTable->isView() ? COM_VIEW_OBJECT : COM_BASE_TABLE_OBJECT),
                  FALSE, FALSE);
               
               naTable = bindWA.getNATableInternal(cn);
@@ -400,17 +402,17 @@ short CmpSeabaseDDL::updateViewUsage(StmtDDLCreateView * createViewParseNode,
                   SEABASEDDL_INTERNAL_ERROR("Bad NATable pointer in updateViewUsage");
                   return -1; 
                 }
-            }
+            } // isHiveTable
 
           if ((naTable->isHiveTable()) &&
-              (naTable->hasExternalTable()) &&
               (naTable->objectUid().get_value() > 0))
             {
               usedObjUID = naTable->objectUid().get_value();
-              strcpy(objType, COM_BASE_TABLE_OBJECT_LIT);
+              strcpy(objType, 
+                     (naTable->isView() ? COM_VIEW_OBJECT_LIT : COM_BASE_TABLE_OBJECT_LIT));
             }
 
-          // do not put in view usage list if it doesn't have an external table
+          // do not put in view usage list if it is not registered in traf
           if (usedObjUID == -1)
             {
               if (! naTable->getViewText())
@@ -1651,7 +1653,9 @@ static bool checkAccessPrivileges(
 
       // If hive or ORC table and table does not have an external table,
       // skip priv checks.
-      if ((naTable->isHiveTable()) && !naTable->hasExternalTable()) 
+      if ((naTable->isHiveTable()) && 
+          ((NOT naTable->isRegistered()) ||
+           (!naTable->hasExternalTable())))
       {
         privs.setOwnerDefaultPrivs();
         pPrivInfo = &privs;

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/DefaultConstants.h
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/DefaultConstants.h b/core/sql/sqlcomp/DefaultConstants.h
index 47560b9..7cf7a97 100644
--- a/core/sql/sqlcomp/DefaultConstants.h
+++ b/core/sql/sqlcomp/DefaultConstants.h
@@ -3629,7 +3629,8 @@ enum DefaultConstants
   TRAF_TRANS_TYPE, 
 
   // max size in bytes of a char or varchar column in a trafodion table.
-  // Valid values are 0 through 10M (10485760).
+  // Valid values are 0 through MAX_CHAR_COL_LENGTH_IN_BYTES.
+  //     (defined in common/ComSmallDefs.h)
   TRAF_MAX_CHARACTER_COL_LENGTH,
 
   // In special cases, previous default value could be overridden. 
@@ -3745,11 +3746,6 @@ enum DefaultConstants
   // if ON, then trafodion views on hive objects are supported.
   HIVE_VIEWS,
 
-  // if ON, then external table is automatically created for all hive tables 
-  // referenced in the view definition.
-  // External table is needed to keep track of view usage and privileges.
-  HIVE_VIEWS_CREATE_EXTERNAL_TABLE,
-
   // Specify whic additional restriction check to apply
   //  0: no check
   //  1: apply majority of keys with predicates check
@@ -3902,6 +3898,17 @@ enum DefaultConstants
   TRANSLATE_ERROR_UNICODE_TO_UNICODE,
   INDEX_HINT_WARNINGS,
 
+  // Operations on hive objects also register it in traf OBJECTS metadata table,
+  // if not already registered. create external table, grant, upd stats, create
+  // views are the current operations that also register hive objects.
+  // 
+  // This default is used to simulate the 
+  // scenario prior to 'hive registration' change. At that time, hive objects
+  // were represented by an external table. With this default set, operations
+  // on hive could be created without registering them.
+  // This default is for internal testing usage only and not externalized.
+  HIVE_NO_REGISTER_OBJECTS,
+
   // 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/9c58649f/core/sql/sqlcomp/PrivMgr.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/PrivMgr.cpp b/core/sql/sqlcomp/PrivMgr.cpp
index 817c796..ed43b10 100644
--- a/core/sql/sqlcomp/PrivMgr.cpp
+++ b/core/sql/sqlcomp/PrivMgr.cpp
@@ -382,8 +382,10 @@ const char * PrivMgr::getSQLOperationName(SQLOperation operation)
       case SQLOperation::QUERY_ACTIVATE: return "QUERY_ACTIVATE";
       case SQLOperation::QUERY_CANCEL: return "QUERY_CANCEL";
       case SQLOperation::QUERY_SUSPEND: return "QUERY_SUSPEND";
+      case SQLOperation::REGISTER_HIVE_OBJECT: return "REGISTER_HIVE_OBJECT";
       case SQLOperation::REMAP_USER: return "REMAP_USER";
       case SQLOperation::SHOW: return "SHOW";
+      case SQLOperation::UNREGISTER_HIVE_OBJECT: return "UNREGISTER_HIVE_OBJECT";
       case SQLOperation::USE_ALTERNATE_SCHEMA: return "USE_ALTERNATE_SCHEMA";
       default:
          return "UNKNOWN";   
@@ -475,8 +477,10 @@ const char * PrivMgr::getSQLOperationCode(SQLOperation operation)
       case SQLOperation::QUERY_ACTIVATE: return "QA";
       case SQLOperation::QUERY_CANCEL: return "QC";
       case SQLOperation::QUERY_SUSPEND: return "QS";
+      case SQLOperation::REGISTER_HIVE_OBJECT: return "RH";
       case SQLOperation::REMAP_USER: return "RU";
       case SQLOperation::SHOW: return "SW";
+      case SQLOperation::UNREGISTER_HIVE_OBJECT: return "UH";
       case SQLOperation::USE_ALTERNATE_SCHEMA: return "UA";
       default:
          return "  ";   
@@ -571,8 +575,10 @@ const char * PrivMgr::getSQLOperationDescription(SQLOperation operation)
       case SQLOperation::QUERY_ACTIVATE: return "Allow grantee to activate queries";
       case SQLOperation::QUERY_CANCEL: return "Allow grantee to cancel queries";
       case SQLOperation::QUERY_SUSPEND: return "Allow grantee to suspend queries";
+      case SQLOperation::REGISTER_HIVE_OBJECT: return "Allow grantee to register hive object in traf metadata";
       case SQLOperation::REMAP_USER: return "Allow grantee to remap DB__ users to a different external username";
       case SQLOperation::SHOW: return "Allow grantee to view metadata information about objects";
+      case SQLOperation::UNREGISTER_HIVE_OBJECT: return "Allow grantee to unregister hive object from traf metadata";
       case SQLOperation::USE_ALTERNATE_SCHEMA: return "Allow grantee to use non-default schemas";
       default:
          return "";   
@@ -868,13 +874,7 @@ bool PrivMgr::isSQLManageOperation(SQLOperation operation)
 const char * PrivMgr::ObjectEnumToLit(ComObjectType objectType)
 
 {
-
-   for (size_t i = 0; i < occurs(objectTypeConversionTable); i++)
-      if (objectType == objectTypeConversionTable[i].enum_)
-         return objectTypeConversionTable[i].literal_;
-
-   return COM_UNKNOWN_OBJECT_LIT;  
-    
+  return comObjectTypeLit(objectType);
 }
 //********************* End of PrivMgr::ObjectEnumToLit ************************
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/PrivMgrCommands.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/PrivMgrCommands.cpp b/core/sql/sqlcomp/PrivMgrCommands.cpp
index 52a5afc..8b8a1d0 100644
--- a/core/sql/sqlcomp/PrivMgrCommands.cpp
+++ b/core/sql/sqlcomp/PrivMgrCommands.cpp
@@ -561,8 +561,11 @@ PrivStatus PrivMgrCommands::getPrivileges(
     return STATUS_GOOD;
   }
 
-  // if a hive table and does not have an external table, assume no privs
-  if (naTable->isHiveTable() && !naTable->hasExternalTable())
+  // if a hive table and does not have an external table and is not
+  // registered in traf metadata, assume no privs
+  if ((naTable->isHiveTable()) && 
+      (NOT naTable->isRegistered()) &&
+      (!naTable->hasExternalTable()))
   {
     PrivMgrDesc emptyDesc;
     userPrivs.initUserPrivs(emptyDesc);

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/PrivMgrDefs.h
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/PrivMgrDefs.h b/core/sql/sqlcomp/PrivMgrDefs.h
index 552f7f3..0c615c2 100644
--- a/core/sql/sqlcomp/PrivMgrDefs.h
+++ b/core/sql/sqlcomp/PrivMgrDefs.h
@@ -213,7 +213,9 @@ enum class SQLOperation {
    QUERY_CANCEL,
    QUERY_SUSPEND,
    REMAP_USER,
+   REGISTER_HIVE_OBJECT,
    SHOW,
+   UNREGISTER_HIVE_OBJECT,
    USE_ALTERNATE_SCHEMA,
    FIRST_OPERATION = ALTER,
    LAST_OPERATION = USE_ALTERNATE_SCHEMA,

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/PrivMgrDesc.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/PrivMgrDesc.cpp b/core/sql/sqlcomp/PrivMgrDesc.cpp
index 0bd4679..ec55129 100644
--- a/core/sql/sqlcomp/PrivMgrDesc.cpp
+++ b/core/sql/sqlcomp/PrivMgrDesc.cpp
@@ -257,6 +257,10 @@ void PrivMgrCoreDesc::setAllObjectGrantPrivilege(
       case COM_STORED_PROCEDURE_OBJECT:
          setAllUdrGrantPrivileges(wgo);
          break;
+      case COM_VIEW_OBJECT:
+        // will reach here for native hive views
+         setAllTableGrantPrivileges(wgo);
+         break;
       default:
          ; //TODO: internal error?
    }   

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/sqlcomp/nadefaults.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/nadefaults.cpp b/core/sql/sqlcomp/nadefaults.cpp
index 8008872..67f8f67 100644
--- a/core/sql/sqlcomp/nadefaults.cpp
+++ b/core/sql/sqlcomp/nadefaults.cpp
@@ -1966,6 +1966,7 @@ SDDkwd__(EXE_DIAGNOSTIC_EVENTS,		"OFF"),
   DDkwd__(HIVE_METADATA_JAVA_ACCESS,            "ON"),
   DDint__(HIVE_METADATA_REFRESH_INTERVAL,       "0"),
   DDflt0_(HIVE_MIN_BYTES_PER_ESP_PARTITION,     "67108864"),
+  DDkwd__(HIVE_NO_REGISTER_OBJECTS,             "OFF"),
   DDui___(HIVE_NUM_ESPS_PER_DATANODE,           "2"),
   DDpct__(HIVE_NUM_ESPS_ROUND_DEVIATION,        "34"),
   DDint__(HIVE_SCAN_SPECIAL_MODE,                "0"),
@@ -1975,7 +1976,6 @@ SDDkwd__(EXE_DIAGNOSTIC_EVENTS,		"OFF"),
   DDkwd__(HIVE_USE_FAKE_TABLE_DESC,             "OFF"),
   DDkwd__(HIVE_USE_HASH2_AS_PARTFUNCION,        "ON"),
   DDkwd__(HIVE_VIEWS,                           "ON"),
-  DDkwd__(HIVE_VIEWS_CREATE_EXTERNAL_TABLE,     "ON"),
 
  // -------------------------------------------------------------------------
 
@@ -5935,9 +5935,7 @@ enum DefaultConstants NADefaults::validateAndInsert(const char *attrName,
      }
      break;
 
-     // max char col length is 10M (10485760).
-     // max char col length is defined in common/ComSmallDefs.h
-     // Currently set to 16M
+     // max char col length is defined in common/ComSmallDefs.h.
      // In special cases, it could be overridden. Internal use only or
      // use only under trafodion supervision.
      case TRAF_MAX_CHARACTER_COL_LENGTH:

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/9c58649f/core/sql/ustat/hs_la.cpp
----------------------------------------------------------------------
diff --git a/core/sql/ustat/hs_la.cpp b/core/sql/ustat/hs_la.cpp
index d33bedd..e3f784c 100644
--- a/core/sql/ustat/hs_la.cpp
+++ b/core/sql/ustat/hs_la.cpp
@@ -1122,6 +1122,44 @@ Lng32 CreateExternalTable(const NAString& catName, const NAString& schName, cons
    return retcode;
 }
 
+static 
+Lng32 RegisterHiveTable(const NAString& catName, const NAString& schName, const NAString& nativeTableName)
+{
+   HSLogMan *LM = HSLogMan::Instance();
+   if (LM->LogNeeded())
+      {
+        snprintf(LM->msg, sizeof(LM->msg), "Registering hive table %s on demand.",
+                          nativeTableName.data());
+        LM->Log(LM->msg);
+      }
+
+   // do not have to worry about the catalog and schema for the new external table 
+   // here. These names will be determined by the processing logic. 
+   NAString ddl = "REGISTER INTERNAL HIVE TABLE IF NOT EXISTS ";
+   ddl.append(catName);
+   ddl.append(".");
+   ddl.append(schName);
+   ddl.append(".");
+   ddl.append(nativeTableName);
+   ddl.append(" IN TRAFODION METADATA");
+
+   // set INTERNAL_QUERY_FROM_EXEUTIL bit in Sql_ParserFlags.
+   // This is needed to process 'register internal' syntax
+   ULng32 flagToSet = INTERNAL_QUERY_FROM_EXEUTIL;
+   PushAndSetSqlParserFlags savedParserFlags(flagToSet);
+
+   Lng32 retcode = HSFuncExecDDL(ddl.data(), - UERR_INTERNAL_ERROR, NULL,
+                            "register hive table", NULL);
+
+   if (retcode < 0 && LM->LogNeeded())
+      {
+        snprintf(LM->msg, sizeof(LM->msg), "Registration of the hive table failed.");
+        LM->Log(LM->msg);
+      }
+
+   return retcode;
+}
+
 NABoolean HSTableDef::setObjectUID(NABoolean createExternalTable)
 {
   objectUID_ = naTbl_->objectUid().get_value();
@@ -1134,8 +1172,12 @@ NABoolean HSTableDef::setObjectUID(NABoolean createExternalTable)
     NAString catName = getCatName(EXTERNAL_FORMAT);
     NAString schName = getSchemaName(EXTERNAL_FORMAT);
     NAString objName = getObjectName(EXTERNAL_FORMAT);
-    Lng32 retcode = CreateExternalTable(catName, schName, objName);
-
+    Lng32 retcode = 0;
+    if (catName == HIVE_SYSTEM_CATALOG)
+      retcode = RegisterHiveTable(catName, schName, objName);
+    else
+      retcode = CreateExternalTable(catName, schName, objName);
+      
     if (retcode != 0)
       return FALSE;
 
@@ -1145,7 +1187,7 @@ NABoolean HSTableDef::setObjectUID(NABoolean createExternalTable)
 
     CorrName corrName(getObjectName(), STMTHEAP, getSchemaName(), getCatName());
 
-    if ( !naTbl_->fetchObjectUIDForNativeTable(corrName) )
+    if ( !naTbl_->fetchObjectUIDForNativeTable(corrName, naTbl_->isView()) )
       return FALSE;
 
     objectUID_ = naTbl_->objectUid().get_value();


Mime
View raw message