Merge pull request #1853 from DaveBirdsall/Trafodion3324

[TRAFODION-3324] Prevent core when histogram exists on non-existent column
diff --git a/core/sql/bin/SqlciErrors.txt b/core/sql/bin/SqlciErrors.txt
index 8034bf1..e66074a 100644
--- a/core/sql/bin/SqlciErrors.txt
+++ b/core/sql/bin/SqlciErrors.txt
@@ -1924,7 +1924,7 @@
 9232 ZZZZZ 99999 BEGINNER MAJOR DBADMIN Incremental UPDATE STATISTICS: cannot proceed because of the on-going IUS transaction originated at $0~String0. 
 9233 ZZZZZ 99999 BEGINNER MAJOR DBADMIN No operation was done since the table consists only of LOB columns. 
 9234 ZZZZZ 99999 BEGINNER MINOR LOGONLY Incremental UPDATE STATISTICS: a new high frequency value is detected for column $0~string0. A regular UPDATE STATISTICS is performed instead.
-9235 ZZZZZ 99999 BEGINNER MINOR LOGONLY --- unused ---
+9235 ZZZZZ 99999 BEGINNER MINOR LOGONLY Histogram for non-existent column found on table $0~string0. Consider dropping and recreating histograms for this table.
 9236 ZZZZZ 99999 BEGINNER MINOR LOGONLY Incremental UPDATE STATISTICS: the histogram for column $0~string0 is empty. A regular UPDATE STATISTICS is performed instead.
 9237 ZZZZZ 99999 BEGINNER MINOR LOGONLY Incremental UPDATE STATISTICS: WHERE clause of an IUS statement cannot contain $0~String0.
 9238 ZZZZZ 99999 BEGINNER INFRM DBADMIN Histograms were updated successfully, but partition statistics could not be updated from file labels.
diff --git a/core/sql/ustat/hs_const.h b/core/sql/ustat/hs_const.h
index 876ac3e..1a8b4ab 100644
--- a/core/sql/ustat/hs_const.h
+++ b/core/sql/ustat/hs_const.h
@@ -160,6 +160,7 @@
                         UERR_IUS_IN_PROGRESS                 = 9232,
                         UERR_ALL_LOB_COLUMNS                 = 9233,
                         UERR_IUS_INSERT_NONMFV_OVERFLOW      = 9234,
+                        UERR_WARNING_NONEXISTENT_COLUMN      = 9235,
                         UERR_IUS_NO_EXISTING_STATS           = 9236,
                         UERR_IUS_WHERE_CLAUSE                = 9237,
                         UERR_WARNING_FILESTATS_FAILED        = 9238,
diff --git a/core/sql/ustat/hs_read.cpp b/core/sql/ustat/hs_read.cpp
index 80def3d..70642fc 100644
--- a/core/sql/ustat/hs_read.cpp
+++ b/core/sql/ustat/hs_read.cpp
@@ -1469,6 +1469,7 @@
   NABoolean updateReadTime = FALSE, continueAfterReadTimeCheck = FALSE;
   HSLogMan *LM = HSLogMan::Instance();
   HSTranMan *TM = HSTranMan::Instance();  // Do not reset, this is not an entry point.
+  NABoolean nonExistentColumnWarningIssued = FALSE;
 
   // These are local variables, not in HSColStats class, as their state is
   // really local to this loop.  They are distinct from the booleans in
@@ -1534,6 +1535,23 @@
       tableColNum_ += offset; // Align columns. See comment above.
       colmap[i] = tableColNum_;
 
+      if (tableColNum_ >= cs.colArray().entries())
+        {
+          // We have encountered a histogram for a column that does not exist.
+          // This can happen if a Hive table has histograms, and then is dropped
+          // and recreated with fewer columns outside of Trafodion. It could
+          // also happen if someone foolishly, maliciously or otherwise 
+          // manually modifies the SB_HISTOGRAMS.COLUMN_NUMBER column. Ignore
+          // this histogram, but also raise a warning.
+          ComDiagsArea *ptrDiags = CmpCommon::diags();
+          if (ptrDiags && !nonExistentColumnWarningIssued)
+            {
+              nonExistentColumnWarningIssued = TRUE;  // just issue the warning once
+              *ptrDiags << DgSqlCode(UERR_WARNING_NONEXISTENT_COLUMN)
+                 << DgString0(tabDef->getObjectFullName().data());
+            }
+          continue;
+        }
       const NAColumn *nacol = cs.colArray()[tableColNum_];
       if (preFetch || nacol->needHistogram())
         needHistogram = TRUE;
diff --git a/docs/messages_guide/src/asciidoc/_chapters/update_stats_msgs.adoc b/docs/messages_guide/src/asciidoc/_chapters/update_stats_msgs.adoc
index 71b74c7..ebcff35 100644
--- a/docs/messages_guide/src/asciidoc/_chapters/update_stats_msgs.adoc
+++ b/docs/messages_guide/src/asciidoc/_chapters/update_stats_msgs.adoc
@@ -419,6 +419,24 @@
 is necessary.
 
 <<<
+[[SQL-9235]]
+== SQL 9235
+
+```
+Histogram for non-existent column found on table <table-name>. Consider dropping and recreating histograms for this table.
+```
+
+*Cause:* A SQL statement was compiled that referenced the given table name. When the compiler
+read the histograms for that table, it encountered one for a non-existent column. This can happen,
+for example, if a Hive table is recreated with fewer columns outside of Trafodion. In this case,
+Trafodion does not know in advance that the histograms are now stale.
+
+*Effect:* This is a warning only. {project-name} attempts to continue compiling the query.
+
+*Recovery:* To remove this warning, perform UPDATE STATISTICS ... CLEAR on the given table. This
+will remove all histograms on the table. If histograms are desired, then perform UPDATE STATISTICS
+to create the desired histograms.
+
 [[SQL-9240]]
 == SQL 9240