You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafodion.apache.org by se...@apache.org on 2017/01/11 14:55:01 UTC

[3/6] incubator-trafodion git commit: [TRAFODION-2259] Add TopN stats counter and realign BMO stats.

[TRAFODION-2259] Add TopN stats counter and realign BMO stats.


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

Branch: refs/heads/master
Commit: 271a5d0e656dc467453381416c261dda5c3411b0
Parents: 548a193
Author: Prashant Vasudev <pr...@esgyn.com>
Authored: Mon Jan 9 21:07:18 2017 +0000
Committer: Prashant Vasudev <pr...@esgyn.com>
Committed: Mon Jan 9 21:07:18 2017 +0000

----------------------------------------------------------------------
 core/sql/cli/sqlcli.h                   |    3 +-
 core/sql/executor/ExExeUtilGetStats.cpp |  133 ++-
 core/sql/executor/ExStats.cpp           |   39 +-
 core/sql/executor/ExStats.h             |    9 +-
 core/sql/executor/ex_sort.cpp           |    6 +
 core/sql/regress/core/EXPECTEDRTS       | 1407 +++++++++++++-------------
 core/sql/regress/core/FILTERRTS         |    3 +
 core/sql/sort/CommonStructs.h           |    5 +
 8 files changed, 859 insertions(+), 746 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/271a5d0e/core/sql/cli/sqlcli.h
----------------------------------------------------------------------
diff --git a/core/sql/cli/sqlcli.h b/core/sql/cli/sqlcli.h
index fe13abd..342348e 100644
--- a/core/sql/cli/sqlcli.h
+++ b/core/sql/cli/sqlcli.h
@@ -1231,7 +1231,8 @@ enum SQLSTATS_ITEM_ID {
   SQLSTATS_REPL_TOTAL_UNCOMPRESS_BYTES = 241,
   SQLSTATS_REPL_CONCURRENCY = 242,
   SQLSTATS_REPL_COMPLETED_PARTNS = 243,
-  SQLSTATS_REPL_BLOCKS_READ = 244
+  SQLSTATS_REPL_BLOCKS_READ = 244,
+  SQLSTATS_TOPN = 245
 };
 
 typedef struct {

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/271a5d0e/core/sql/executor/ExExeUtilGetStats.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ExExeUtilGetStats.cpp b/core/sql/executor/ExExeUtilGetStats.cpp
index 4b6c698..76b605b 100644
--- a/core/sql/executor/ExExeUtilGetStats.cpp
+++ b/core/sql/executor/ExExeUtilGetStats.cpp
@@ -2351,8 +2351,8 @@ short ExExeUtilGetRTSStatisticsTcb::work()
           measStatsItems_[22].statsItem_id = SQLSTATS_SCRATCH_BUFFER_BLOCKS_WRITTEN;
           measStatsItems_[23].statsItem_id = SQLSTATS_SCRATCH_READ_COUNT;
           measStatsItems_[24].statsItem_id = SQLSTATS_SCRATCH_WRITE_COUNT;
-
-          // maxMeasStatsItems_ is set to  25
+          measStatsItems_[25].statsItem_id = SQLSTATS_TOPN;
+          // maxMeasStatsItems_ is set to  26
         }
         else
           initSqlStatsItems(measStatsItems_, maxMeasStatsItems_, TRUE);
@@ -2496,6 +2496,12 @@ short ExExeUtilGetRTSStatisticsTcb::work()
             str_sprintf(statsBuf_, "%25s%s", "Scr. Overflow Mode", 
               ExBMOStats::getScratchOverflowMode((Int16)measStatsItems_[i].int64_value));
             break;
+          case SQLSTATS_TOPN:
+            str_sprintf(Int64Val, "%Ld", measStatsItems_[i].int64_value);
+            intSize = str_len(Int64Val);
+            AddCommas(Int64Val,intSize); 
+            str_sprintf(statsBuf_, "%25s%s", "Sort TopN", Int64Val);
+            break;
           case SQLSTATS_SCRATCH_FILE_COUNT:
             str_sprintf(Int64Val, "%Ld", measStatsItems_[i].int64_value);
             intSize = str_len(Int64Val);
@@ -2546,7 +2552,7 @@ short ExExeUtilGetRTSStatisticsTcb::work()
       {
         if (rootOperStatsItems_ == NULL)
         {
-          maxRootOperStatsItems_ = 23;
+          maxRootOperStatsItems_ = 24;
           rootOperStatsItems_ = new (getGlobals()->getDefaultHeap()) 
                   SQLSTATS_ITEM[maxRootOperStatsItems_];
           initSqlStatsItems(rootOperStatsItems_, maxRootOperStatsItems_, FALSE);
@@ -2573,7 +2579,8 @@ short ExExeUtilGetRTSStatisticsTcb::work()
           rootOperStatsItems_[20].statsItem_id = SQLSTATS_SCRATCH_BUFFER_BLOCKS_WRITTEN;
           rootOperStatsItems_[21].statsItem_id = SQLSTATS_SCRATCH_READ_COUNT;
           rootOperStatsItems_[22].statsItem_id = SQLSTATS_SCRATCH_WRITE_COUNT;
-          // maxRootOperStatsItems_ is set to 23
+          rootOperStatsItems_[23].statsItem_id = SQLSTATS_TOPN;
+          // maxRootOperStatsItems_ is set to 24
         }
         else
           initSqlStatsItems(rootOperStatsItems_, maxRootOperStatsItems_, TRUE);
@@ -2690,6 +2697,15 @@ short ExExeUtilGetRTSStatisticsTcb::work()
             else
               continue;
             break;
+          case SQLSTATS_TOPN:
+            if (statsMergeType != SQLCLI_PROGRESS_STATS)
+            {
+              formatWInt64( rootOperStatsItems_[i], Int64Val);
+              str_sprintf(statsBuf_, "%25s%s", "Sort TopN", Int64Val);
+            }
+            else
+              continue;
+            break;
           case SQLSTATS_SCRATCH_FILE_COUNT:
             if (statsMergeType != SQLCLI_PROGRESS_STATS)
             {
@@ -3198,28 +3214,30 @@ short ExExeUtilGetRTSStatisticsTcb::work()
       {
         if (bmoStatsItems_ == NULL)
         {
-          maxBMOStatsItems_ = 15;
+          maxBMOStatsItems_ = 16;
           bmoStatsItems_ = new (getGlobals()->getDefaultHeap()) 
                   SQLSTATS_ITEM[maxBMOStatsItems_];
           initSqlStatsItems(bmoStatsItems_, maxBMOStatsItems_, FALSE);
           bmoStatsItems_[0].statsItem_id = SQLSTATS_TDB_ID;
           bmoStatsItems_[1].statsItem_id = SQLSTATS_TDB_NAME;
           bmoStatsItems_[2].statsItem_id = SQLSTATS_SCRATCH_OVERFLOW_MODE;
+          bmoStatsItems_[3].statsItem_id = SQLSTATS_TOPN;
           //bmoStatsItems_[3].statsItem_id = SQLSTATS_OVERFLOW_PHASE;
           //bmoStatsItems_[4].statsItem_id = SQLSTATS_OVEFLOW_PHASE_STARTTIME;
-          bmoStatsItems_[3].statsItem_id = SQLSTATS_OPER_CPU_TIME;
-          bmoStatsItems_[4].statsItem_id = SQLSTATS_BMO_HEAP_USED;
-          bmoStatsItems_[5].statsItem_id = SQLSTATS_BMO_HEAP_ALLOC;
-          bmoStatsItems_[6].statsItem_id = SQLSTATS_BMO_HEAP_WM;
-          bmoStatsItems_[7].statsItem_id = SQLSTATS_BMO_SPACE_BUFFER_SIZE;
-          bmoStatsItems_[8].statsItem_id = SQLSTATS_BMO_SPACE_BUFFER_COUNT;
-          bmoStatsItems_[9].statsItem_id = SQLSTATS_SCRATCH_FILE_COUNT;
-          bmoStatsItems_[10].statsItem_id = SQLSTATS_SCRATCH_BUFFER_BLOCK_SIZE;
-          bmoStatsItems_[11].statsItem_id = SQLSTATS_SCRATCH_BUFFER_BLOCKS_READ;
-          bmoStatsItems_[12].statsItem_id = SQLSTATS_SCRATCH_BUFFER_BLOCKS_WRITTEN;
-          bmoStatsItems_[13].statsItem_id = SQLSTATS_SCRATCH_READ_COUNT;
-          bmoStatsItems_[14].statsItem_id = SQLSTATS_SCRATCH_WRITE_COUNT;
-          // maxBMOStatsItems_ is set to 15
+          bmoStatsItems_[4].statsItem_id = SQLSTATS_OPER_CPU_TIME;
+          bmoStatsItems_[5].statsItem_id = SQLSTATS_SCRATCH_FILE_COUNT;
+          bmoStatsItems_[6].statsItem_id = SQLSTATS_BMO_HEAP_USED;
+          bmoStatsItems_[7].statsItem_id = SQLSTATS_BMO_HEAP_ALLOC;
+          bmoStatsItems_[8].statsItem_id = SQLSTATS_BMO_HEAP_WM;
+          bmoStatsItems_[9].statsItem_id = SQLSTATS_BMO_SPACE_BUFFER_SIZE;
+          bmoStatsItems_[10].statsItem_id = SQLSTATS_BMO_SPACE_BUFFER_COUNT;
+          bmoStatsItems_[11].statsItem_id = SQLSTATS_SCRATCH_BUFFER_BLOCK_SIZE;
+          bmoStatsItems_[12].statsItem_id = SQLSTATS_SCRATCH_BUFFER_BLOCKS_READ;
+          bmoStatsItems_[13].statsItem_id = SQLSTATS_SCRATCH_BUFFER_BLOCKS_WRITTEN;
+          bmoStatsItems_[14].statsItem_id = SQLSTATS_SCRATCH_READ_COUNT;
+          bmoStatsItems_[15].statsItem_id = SQLSTATS_SCRATCH_WRITE_COUNT;
+          
+          // maxBMOStatsItems_ is set to 16
           // TDB_NAME
           bmoStatsItems_[1].str_value = new (getGlobals()->getDefaultHeap())
                       char[MAX_TDB_NAME_LEN+1];
@@ -3255,11 +3273,20 @@ short ExExeUtilGetRTSStatisticsTcb::work()
         if ((qparent_.up->getSize() - qparent_.up->getLength()) < 4)
 	      return WORK_CALL_AGAIN;
         moveRowToUpQueue(" ");
-       str_sprintf(statsBuf_, "%5s %35s%5s%-19s%-15s%-15s%-15s%-17s%-17s%-12s%-45s %s",
-                "   Id", "TDB Name", "Mode", "CPU Time", 
-                "BMO Heap Used", "BMO Heap Total", "BMO Heap WM", "BMO Space BufSz","BMO Space BufCnt",
-                "File Count", "Scratch Buffer Block Size/Read/Written",
-                "Scratch IO Count Read/Written");
+              
+        str_sprintf(statsBuf_, "%-5s%-17s%-20s%-20s%-20s%-20s%",
+            "Id", "TDB Name", "Mode", "TopN", "CPU Time","File Count");
+        moveRowToUpQueue(statsBuf_);
+        
+       str_sprintf(statsBuf_, "%-22s%-20s%-20s%-20s%-20s%",
+                "BMO Heap Used", "BMO Heap Total", "BMO Heap WM",
+                "BMO Space BufSz","BMO Space BufCnt");
+       moveRowToUpQueue(statsBuf_);
+       
+       str_sprintf(statsBuf_, "%-22s%-20s%-20s%-20s%-20s%",
+                "ScrBlk Size",
+                "ScrBlk Read", "ScrBlk Written", "ScrIO Read", "ScrIO Written");
+  
         moveRowToUpQueue(statsBuf_);
         isBMOHeadingDisplayed_ = TRUE;
         step_ = FORMAT_AND_RETURN_BMO_STATS_;
@@ -3275,16 +3302,37 @@ short ExExeUtilGetRTSStatisticsTcb::work()
           switch (bmoStatsItems_[i].statsItem_id)
           {
           case SQLSTATS_TDB_ID:
-            str_sprintf(statsBuf_, "%5Ld", bmoStatsItems_[i].int64_value);
+            str_sprintf(statsBuf_, "%-5Ld", bmoStatsItems_[i].int64_value);
             break;
           case SQLSTATS_TDB_NAME:
             bmoStatsItems_[i].str_value[bmoStatsItems_[i].str_ret_len] = '\0';
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], " %35s", bmoStatsItems_[i].str_value);
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-17s", bmoStatsItems_[i].str_value);
             break;
           case SQLSTATS_SCRATCH_OVERFLOW_MODE:
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%5s", 
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", 
                  ExBMOStats::getScratchOverflowMode((Int16) bmoStatsItems_[i].int64_value));
             break;
+          case SQLSTATS_TOPN:
+            str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
+            intSize = str_len(Int64Val);
+            AddCommas(Int64Val,intSize); 
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", Int64Val);
+            break;
+          case SQLSTATS_OPER_CPU_TIME:
+            str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
+            intSize = str_len(Int64Val);
+            AddCommas(Int64Val,intSize); 
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", Int64Val);
+            break;
+          case SQLSTATS_SCRATCH_FILE_COUNT:
+            str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
+            intSize = str_len(Int64Val);
+            AddCommas(Int64Val,intSize); 
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", Int64Val);
+            if (moveRowToUpQueue(statsBuf_, strlen(statsBuf_), &rc) == -1)
+              return rc;
+            break;
+/*
           case SQLSTATS_OVERFLOW_PHASE:
             bmoStatsItems_[i].str_value[bmoStatsItems_[i].str_ret_len] = '\0';
             str_sprintf(&statsBuf_[strlen(statsBuf_)], "%12s", bmoStatsItems_[i].str_value);
@@ -3303,72 +3351,63 @@ short ExExeUtilGetRTSStatisticsTcb::work()
 			  timestamp[3], timestamp[4], timestamp[5]);
             }
             break;
+*/
           case SQLSTATS_BMO_HEAP_USED:
             str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
             intSize = str_len(Int64Val);
             AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-15s", Int64Val);
+            str_sprintf(statsBuf_, "%-22s", Int64Val);
             break;
           case SQLSTATS_BMO_HEAP_ALLOC:
           case SQLSTATS_BMO_HEAP_WM:
             str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
             intSize = str_len(Int64Val);
             AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-15s", Int64Val);
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", Int64Val);
             break;
           case SQLSTATS_BMO_SPACE_BUFFER_SIZE:
             str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
             intSize = str_len(Int64Val);
             AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-17s", Int64Val);
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", Int64Val);
             break;
           case SQLSTATS_BMO_SPACE_BUFFER_COUNT:
             str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
             intSize = str_len(Int64Val);
             AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-17s", Int64Val);
-            break;
-          case SQLSTATS_SCRATCH_FILE_COUNT:
-            str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
-            intSize = str_len(Int64Val);
-            AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-12s", Int64Val);
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", Int64Val);
+            if (moveRowToUpQueue(statsBuf_, strlen(statsBuf_), &rc) == -1)
+              return rc;
             break;
           case SQLSTATS_SCRATCH_BUFFER_BLOCK_SIZE:
             str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
             intSize = str_len(Int64Val);
             AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-10s", Int64Val);
+            str_sprintf(statsBuf_, "%-22s", Int64Val);
             break;
           case SQLSTATS_SCRATCH_BUFFER_BLOCKS_READ:
             str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
             intSize = str_len(Int64Val);
             AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-19s", Int64Val);
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", Int64Val);
             break;
           case SQLSTATS_SCRATCH_BUFFER_BLOCKS_WRITTEN:
             str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
             intSize = str_len(Int64Val);
             AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-19s", Int64Val);
-            break;
-          case SQLSTATS_OPER_CPU_TIME:
-            str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
-            intSize = str_len(Int64Val);
-            AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-19s", Int64Val);
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", Int64Val);
             break;
           case SQLSTATS_SCRATCH_READ_COUNT:
             str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
             intSize = str_len(Int64Val);
             AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-19s", Int64Val);
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", Int64Val);
             break;
           case SQLSTATS_SCRATCH_WRITE_COUNT:
             str_sprintf(Int64Val, "%Ld", bmoStatsItems_[i].int64_value);
             intSize = str_len(Int64Val);
             AddCommas(Int64Val,intSize); 
-            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-19s", Int64Val);
+            str_sprintf(&statsBuf_[strlen(statsBuf_)], "%-20s", Int64Val);
             if (moveRowToUpQueue(statsBuf_, strlen(statsBuf_), &rc) == -1)
               return rc;
             break;

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/271a5d0e/core/sql/executor/ExStats.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ExStats.cpp b/core/sql/executor/ExStats.cpp
index 43d2b50..6031198 100644
--- a/core/sql/executor/ExStats.cpp
+++ b/core/sql/executor/ExStats.cpp
@@ -1585,6 +1585,7 @@ void ExFragRootOperStats::init()
   scratchWriteCount_ = 0;
   scratchReadCount_ = 0;
   udrCpuTime_ = 0;
+  topN_ = -1;
   waitTime_ = 0;
   maxWaitTime_ = 0;
   diffCpuTime_ = 0;
@@ -1773,6 +1774,8 @@ void ExFragRootOperStats::merge(ExFragRootOperStats* other)
   scratchReadCount_ += other->scratchReadCount_;
   scratchWriteCount_ += other->scratchWriteCount_;
   udrCpuTime_ += other->udrCpuTime_;
+  if(topN_ == -1 && other->topN_ > 0)
+    topN_ = other->topN_;
   // Remember, don't merge or copy  executionCount_ !
   waitTime_ += other->waitTime_;
   if (other->maxWaitTime_ > maxWaitTime_)
@@ -1810,6 +1813,9 @@ void ExFragRootOperStats::merge(ExBMOStats *other)
 
   scratchReadCount_ += other->scratchReadCount_;
   scratchWriteCount_ += other->scratchWriteCount_;
+  if(topN_ == -1 && other->topN_ > 0)
+    topN_ = other->topN_;
+  
 #endif
 }
 
@@ -1945,7 +1951,7 @@ void ExFragRootOperStats::getVariableStatsInfo(char * dataBuffer,
 		"SpaceUsed: %u SpaceTotal: %u HeapUsed: %u HeapTotal: %u "
 		"Newprocess: %u NewprocessTime: %Ld reqMsgCnt: %Ld "
 		"regMsgBytes: %Ld replyMsgCnt: %Ld replyMsgBytes: %Ld "
-		"PMemUsed: %Ld scrOverFlowMode: %d "
+		"PMemUsed: %Ld scrOverFlowMode: %d sortMode: %d"
 		"scrFileCount: %d scrBufferBlockSize: %d scrBuffferRead: %Ld scrBufferWritten: %Ld "
 		"scrWriteCount:%Ld scrReadCount: %Ld udrCpuTime: %Ld "
 		"maxWaitTime: %Ld avgWaitTime: %Ld "
@@ -1973,6 +1979,7 @@ void ExFragRootOperStats::getVariableStatsInfo(char * dataBuffer,
 		scratchWriteCount_,
 		scratchReadCount_,
 		udrCpuTime_,
+		topN_,
 		maxWaitTime_,
 		getAvgWaitTime(),
 		(hdfsAccess() ? 1 : 0)
@@ -2053,6 +2060,9 @@ Lng32 ExFragRootOperStats::getStatsItem(SQLSTATS_ITEM* sqlStats_item)
     case SQLSTATS_SCRATCH_OVERFLOW_MODE:
       sqlStats_item->int64_value = scratchOverflowMode_;
       break;
+    case SQLSTATS_TOPN:
+      sqlStats_item->int64_value = topN_;
+      break;
     case SQLSTATS_SCRATCH_BUFFER_BLOCK_SIZE:
       sqlStats_item->int64_value = scratchBufferBlockSize_;
       break;
@@ -4820,6 +4830,7 @@ void ExMeasStats::init()
   scratchWriteCount_ = 0;
   scratchReadCount_ = 0;
   udrCpuTime_ = 0;
+  topN_ = -1;
 #endif
 }
 
@@ -4831,7 +4842,7 @@ void ExMeasStats::merge(ExFragRootOperStats* other)
   maxHeapUsage_  += other -> maxHeapUsage_; 
   maxHeapAlloc_  += other -> maxHeapAlloc_;
   if (scratchOverflowMode_ == -1)
-    scratchOverflowMode_ = other->scratchOverflowMode_; 
+    scratchOverflowMode_ = other->scratchOverflowMode_;
 #ifndef __EID
   cpuTime_          += other -> cpuTime_;
   newprocess_       += other -> newprocess_; 
@@ -4864,7 +4875,6 @@ void ExMeasStats::merge(ExBMOStats *other)
 #ifndef __EID
   scratchFileCount_ += other->scratchFileCount_;
   scratchOverflowMode_ = other->scratchOverflowMode_;
-  
   if (scratchBufferBlockSize_ == 0 &&
      other->scratchBufferBlockSize_ > 0)
      scratchBufferBlockSize_ = other->scratchBufferBlockSize_;
@@ -4876,6 +4886,9 @@ void ExMeasStats::merge(ExBMOStats *other)
 
   scratchReadCount_ += other->scratchReadCount_;
   scratchWriteCount_ += other->scratchWriteCount_;
+  if (topN_ == -1 && other->topN_ > 0)
+      topN_ = other->topN_;
+
 #endif
 }
 
@@ -4936,6 +4949,9 @@ void ExMeasStats::merge(ExMeasStats* other)
   scratchReadCount_ += other->scratchReadCount_;
   scratchWriteCount_ += other->scratchWriteCount_;
   udrCpuTime_ += other->udrCpuTime_;
+  if (topN_ == -1 && other->topN_ > 0)
+      topN_ = other->topN_;
+
 #endif
 }
 
@@ -5044,6 +5060,7 @@ void ExMeasStats::copyContents(ExMeasStats *other)
   scratchReadCount_ = other->scratchReadCount_;
   scratchWriteCount_ = other->scratchWriteCount_;
   udrCpuTime_ = other->udrCpuTime_;
+  topN_ = other->topN_;
 #endif
   }
 }
@@ -5108,7 +5125,7 @@ void ExMeasStats::getVariableStatsInfo(char * dataBuffer, char * datalen,
     "reqMsgCnt: %Ld reqMsgBytes: %Ld replyMsgCnt: %Ld "
     "replyMsgBytes: %Ld scrOverflowMode: %d "
     "scrFileCount: %d scrBufferBlockSize: %d scrBufferRead: %Ld scrBufferWritten: %Ld "
-    "scrWriteCount: %Ld scrReadCount: %Ld udrCpuTime: %Ld ",
+    "scrWriteCount: %Ld scrReadCount: %Ld udrCpuTime: %Ld topN: %Ld",
 	      statType(),
               getNewprocess(),
 	      getNewprocessTime(),
@@ -5131,7 +5148,8 @@ void ExMeasStats::getVariableStatsInfo(char * dataBuffer, char * datalen,
               scratchBufferBlockWritten_,
               scratchWriteCount_,
               scratchReadCount_,
-              udrCpuTime_
+              udrCpuTime_,
+              topN_
               );
   }
   buf += str_len(buf);
@@ -10663,6 +10681,7 @@ void ExBMOStats::init()
   scratchBufferBlockWritten_ = 0;
   scratchReadCount_ = 0;
   scratchWriteCount_ = 0;
+  topN_ = -1;
 }
 
 UInt32 ExBMOStats::packedLength()
@@ -10760,7 +10779,7 @@ void ExBMOStats::getVariableStatsInfo(char * dataBuffer,
        "statsRowType: %d explainTdbId: %d bmoHeapUsed: %d bmoHeapTotal: %d bmoHeapWM: %d "
        "bmoSpaceBufferSize: %d bmoSpaceBufferCount: %d OFPhase: %s OFPhaseStartTime: %Ld "
        "scrOverFlowMode: %d scrFileCount: %d scrBufferBlockSize: %d scrBuffferRead: %d scrBufferWritten: %d "
-       "scrWriteCount: %Ld scrReadCount: %Ld ",
+       "scrWriteCount: %Ld scrReadCount: %Ld topN: %Ld",
         statType(),
         getExplainNodeId(),
         bmoHeapUsage_,
@@ -10776,7 +10795,8 @@ void ExBMOStats::getVariableStatsInfo(char * dataBuffer,
         scratchBufferBlockRead_,
         scratchBufferBlockWritten_,
         scratchWriteCount_,
-        scratchReadCount_
+        scratchReadCount_,
+        topN_
        );
   buf += str_len(buf);
   *(short*)dataLen = (short) (buf - dataBuffer);
@@ -10801,6 +10821,8 @@ void ExBMOStats::merge(ExBMOStats* other)
   str_cpy_all(overflowPhase_, other->overflowPhase_, str_len(other->overflowPhase_)+1);
   if (other->scratchBufferBlockSize_ != -1)
     scratchBufferBlockSize_ = other->scratchBufferBlockSize_;
+  if (other->topN_ != -1)
+    topN_ = other->topN_;
   scratchOverflowMode_ = other->scratchOverflowMode_;
   scratchFileCount_ += other->scratchFileCount_;
   scratchBufferBlockRead_ += other->scratchBufferBlockRead_;
@@ -10851,6 +10873,9 @@ Lng32 ExBMOStats::getStatsItem(SQLSTATS_ITEM* sqlStats_item)
   case SQLSTATS_SCRATCH_OVERFLOW_MODE:
     sqlStats_item->int64_value = scratchOverflowMode_;
     break;
+  case SQLSTATS_TOPN:
+    sqlStats_item->int64_value = topN_;
+    break;
   case SQLSTATS_SCRATCH_FILE_COUNT:
     sqlStats_item->int64_value = scratchFileCount_;
     break;

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/271a5d0e/core/sql/executor/ExStats.h
----------------------------------------------------------------------
diff --git a/core/sql/executor/ExStats.h b/core/sql/executor/ExStats.h
index 6581ffc..00e6e5e 100644
--- a/core/sql/executor/ExStats.h
+++ b/core/sql/executor/ExStats.h
@@ -1211,6 +1211,10 @@ public:
   {
     scratchOverflowMode_ = overflowMode;
   }
+  inline void setTopN(Int32 size) 
+  { 
+    topN_ = size;
+  }
   inline Int64 getScratchReadCount(void) { return scratchReadCount_; }
   static const char *getScratchOverflowMode(Int16 overflowMode);
 private:
@@ -1228,7 +1232,8 @@ private:
   Int64 scratchReadCount_;
   Int64 scratchWriteCount_;
   Int16 scratchOverflowMode_;   // 0 - disk 1 - SSD
-  char bmoFiller_[6];          // For versioning with old dp2
+  Int16 topN_;                 // TOPN value
+  char bmoFiller_[4];          // For versioning with old dp2
 };
 
 
@@ -1487,6 +1492,7 @@ private:
   Int64 scratchReadCount_;
   Int64 scratchWriteCount_;
   Int64 udrCpuTime_;
+  Int64 topN_;
   // process id of this fragment instance (to correlate it with MEASURE data)
   // Also used by logic on runtimestats/CancelBroker.cpp
   SB_Phandle_Type phandle_;
@@ -2882,6 +2888,7 @@ private:
   Int64 scratchReadCount_;
   Int64 scratchWriteCount_;
   Int64 udrCpuTime_;
+  Int64 topN_;
 #endif
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/271a5d0e/core/sql/executor/ex_sort.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_sort.cpp b/core/sql/executor/ex_sort.cpp
index 3b2b15a..c304750 100644
--- a/core/sql/executor/ex_sort.cpp
+++ b/core/sql/executor/ex_sort.cpp
@@ -174,6 +174,12 @@ void ExSortTcb::setupPoolBuffers(ex_queue_entry *pentry_down)
     defragTd_ = sortPool_->addDefragTuppDescriptor(sortTdb().sortRecLen_);
   }
   
+  if(bmoStats_)
+  {
+    if(topNSortPool_)
+      bmoStats_->setTopN(pentry_down->downState.requestValue);
+  }
+  
 }
 
 //