trafodion-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject [09/15] incubator-trafodion git commit: Ensured that CQD NUM_ESP_FRAGMENTS can take value upto 8. Removed the environment variable concept to set the number of esp fragments. Removed the CQDs ESP_NUM_FRAGMENTS_WITH_QUOTAS and ESP_MULTI_FRAGMENT_QUOTAS. U
Date Wed, 13 Sep 2017 23:23:27 GMT
Ensured that CQD NUM_ESP_FRAGMENTS can take value upto 8.
Removed the environment variable concept to set the number of esp
fragments. Removed the CQDs ESP_NUM_FRAGMENTS_WITH_QUOTAS and
ESP_MULTI_FRAGMENT_QUOTAS. Use the corresponding CQD
ESP_NUM_FRAGMENTS and ESP_MUTLI_FRAGMENT instead
(cherry picked from commit 72534e89633f3d4b8a61ab26d471aaa8b7f3e12a)

Conflicts:

	core/sql/executor/ex_frag_rt.cpp
	core/sql/executor/ex_frag_rt.h
	core/sql/generator/GenRelMisc.cpp


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

Branch: refs/heads/master
Commit: f538c2f9d45872a3d5a081032a849563b9d053be
Parents: 175402b
Author: selvaganesang <selva.govindarajan@esgyn.com>
Authored: Fri Sep 8 05:20:05 2017 +0000
Committer: selvaganesang <selva.govindarajan@esgyn.com>
Committed: Fri Sep 8 05:20:05 2017 +0000

----------------------------------------------------------------------
 core/sql/cli/Globals.h                  |  2 +-
 core/sql/cli/Statement.cpp              |  8 +--
 core/sql/cli/sqlcli.h                   |  5 +-
 core/sql/comexe/ComTdb.h                |  1 +
 core/sql/comexe/ComTdbHashGrby.h        | 20 ++++--
 core/sql/comexe/ComTdbHashj.cpp         |  6 +-
 core/sql/comexe/ComTdbHashj.h           | 18 ++++--
 core/sql/comexe/ComTdbSort.cpp          |  9 +--
 core/sql/comexe/ComTdbSort.h            | 27 ++++++--
 core/sql/comexe/ComTdbStats.cpp         | 10 ++-
 core/sql/comexe/ComTdbStats.h           |  7 ++-
 core/sql/common/ComSmallDefs.h          |  7 +++
 core/sql/common/NAMemory.cpp            |  1 +
 core/sql/common/NAMemory.h              |  3 +-
 core/sql/executor/Allocator.cpp         |  4 +-
 core/sql/executor/ExExeUtilGetStats.cpp |  2 +-
 core/sql/executor/ExStats.cpp           | 20 ++++--
 core/sql/executor/ExStats.h             |  1 +
 core/sql/executor/cluster.cpp           | 25 ++++----
 core/sql/executor/cluster.h             |  4 +-
 core/sql/executor/ex_exe_stmt_globals.h | 28 +++++++--
 core/sql/executor/ex_frag_rt.cpp        | 80 +++++++-----------------
 core/sql/executor/ex_frag_rt.h          |  8 ++-
 core/sql/executor/ex_hashj.cpp          |  4 +-
 core/sql/executor/ex_mj.cpp             |  2 +-
 core/sql/executor/ex_sort.cpp           |  5 +-
 core/sql/executor/ex_split_bottom.cpp   |  2 +-
 core/sql/generator/GenExplain.cpp       | 16 ++---
 core/sql/generator/GenPreCode.cpp       | 13 +++-
 core/sql/generator/GenProbeCache.cpp    | 25 +++-----
 core/sql/generator/GenRelEnforcer.cpp   | 11 +---
 core/sql/generator/GenRelGrby.cpp       | 58 ++++++++----------
 core/sql/generator/GenRelJoin.cpp       | 92 ++++++++++++++--------------
 core/sql/generator/GenRelMisc.cpp       | 81 ++++++++++++------------
 core/sql/generator/GenRelSequence.cpp   | 43 +++++++------
 core/sql/generator/Generator.cpp        | 27 +++-----
 core/sql/generator/Generator.h          | 41 +++++++------
 core/sql/optimizer/RelEnforcer.h        |  5 ++
 core/sql/optimizer/RelExpr.cpp          |  5 +-
 core/sql/optimizer/RelExpr.h            |  3 +-
 core/sql/sort/SortUtil.cpp              | 16 ++---
 core/sql/sort/SortUtilCfg.cpp           |  2 +-
 core/sql/sort/SortUtilCfg.h             |  6 +-
 core/sql/sqlcomp/DefaultConstants.h     |  7 +--
 core/sql/sqlcomp/DefaultValidator.h     |  8 +++
 core/sql/sqlcomp/nadefaults.cpp         | 11 ++--
 46 files changed, 412 insertions(+), 367 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/cli/Globals.h
----------------------------------------------------------------------
diff --git a/core/sql/cli/Globals.h b/core/sql/cli/Globals.h
index 864f891..fb98334 100644
--- a/core/sql/cli/Globals.h
+++ b/core/sql/cli/Globals.h
@@ -424,7 +424,7 @@ inline
   void resetMemoryQuota();
   ULng32 unusedMemoryQuota();
   void yieldMemoryQuota(ULng32 size);
-
+  NABoolean isEspProcess() { return espProcess_; }
 private:
   enum {
     DEFAULT_CONTEXT_HANDLE = 2000

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/cli/Statement.cpp
----------------------------------------------------------------------
diff --git a/core/sql/cli/Statement.cpp b/core/sql/cli/Statement.cpp
index 6084153..3c5eeda 100644
--- a/core/sql/cli/Statement.cpp
+++ b/core/sql/cli/Statement.cpp
@@ -2906,12 +2906,8 @@ RETCODE Statement::execute(CliGlobals * cliGlobals, Descriptor * input_desc,
                   }
               }
             // In case of master, the unused memory quota needs to be reset
-            // with every statement execution. Hence, when there are multiple
-            // statements active in the master, the unused memory quota management
-            // may not work as expected
-            // Currently only one active statement can be assigned to an ESP
-            // So, esp unused memory quota management should work correctly. 
-            context_->resetMemoryQuota();
+            // with every statement execution. 
+            statementGlobals_->resetMemoryQuota();
 	    /* execute it */
             if( root_tdb )
             {            

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/cli/sqlcli.h
----------------------------------------------------------------------
diff --git a/core/sql/cli/sqlcli.h b/core/sql/cli/sqlcli.h
index a27d942..3a28e54 100644
--- a/core/sql/cli/sqlcli.h
+++ b/core/sql/cli/sqlcli.h
@@ -662,7 +662,9 @@ typedef struct {
   double overflowSize;
   short queryType;
   short subqueryType;
-  char filler[28];
+  float bmoMemLimitPerNode;
+  float estBmoMemPerNode;
+  char filler[20];
 } SQL_QUERY_COMPILER_STATS_INFO;
 
 /* struct to hold statistics to be returned. */
@@ -1174,6 +1176,7 @@ enum SQLSTATS_ITEM_ID {
   SQLSTATS_SCRATCH_READ_COUNT = 162,
   SQLSTATS_SCRATCH_WRITE_COUNT = 163,
   SQLSTATS_SCRATCH_IO_MAX_TIME = 164,
+  SQLSTATS_BMO_EST_MEMORY = 165,
   SQLSTATS_BMO_PHASE = 166,
   SQLSTATS_INTERIM_ROW_COUNT = 167,
 /* SQLSTATS_ITEM_ID for UDR_BASE_STATS */

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/comexe/ComTdb.h
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdb.h b/core/sql/comexe/ComTdb.h
index 4a7b754..fdae66a 100644
--- a/core/sql/comexe/ComTdb.h
+++ b/core/sql/comexe/ComTdb.h
@@ -634,6 +634,7 @@ public:
   ex_cri_desc *getCriDescUp() { return criDescUp_; };
   void setCriDescUp(ex_cri_desc *cri) { criDescUp_ = cri; };
 
+  virtual Float32 getEstimatedMemoryUsage() { return 0; }
   enum Type
   {
     TABLE_INFO,

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/comexe/ComTdbHashGrby.h
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbHashGrby.h b/core/sql/comexe/ComTdbHashGrby.h
index f31f70e..4e0a9ea 100644
--- a/core/sql/comexe/ComTdbHashGrby.h
+++ b/core/sql/comexe/ComTdbHashGrby.h
@@ -88,11 +88,13 @@ protected:
   UInt32         numInBatch_;                    // 176-179
   UInt16         forceOverflowEvery_;            // 180-181
   UInt16         hgbGrowthPercent_;              // 182-183
-  Float32        hgbMemEstInMbPerCpu_;           // 184-187
+  Float32        hgbMemEstInKBPerNode_;           // 184-187
   Int16          scratchIOVectorSize_;           // 188-189
   UInt16         bmoMinMemBeforePressureCheck_;  // 190-191
   UInt16         bmoMaxMemThresholdMB_;          // 192-193
-  char           fillersComTdbHashGrby_[6];      // 194-199
+  char           fillersComTdbHashGrby_[2];      // 194-195
+  Float32        estMemoryUsage_;                // 196-199
+  Float32        bmoQuotaRatio_;
  
 public:
 
@@ -311,9 +313,9 @@ public:
 
   void setPartialGrbyMemoryMB(UInt16 v) { partialGrbyMemoryMB_ = v; }
 
-  void setHgbMemEstInMbPerCpu(Float32 s) {hgbMemEstInMbPerCpu_=s;}
+  void setHgbMemEstInKBPerNode(Float32 s) {hgbMemEstInKBPerNode_=s;}
 
-  Float32 getHgbMemEstInMbPerCpu() {return hgbMemEstInMbPerCpu_;}
+  Float32 getHgbMemEstInKBPerNode() {return hgbMemEstInKBPerNode_;}
 
   Float32 hgbGrowthPercent() {return Float32(hgbGrowthPercent_/100.0);}
 
@@ -333,6 +335,16 @@ public:
   UInt16  getBMOMaxMemThresholdMB()
   { return bmoMaxMemThresholdMB_; }
 
+  void setEstimatedMemoryUsage(Float32 estMemory)
+    { estMemoryUsage_ = estMemory; }
+  virtual Float32 getEstimatedMemoryUsage(void)
+    { return estMemoryUsage_;}
+
+  void setBmoQuotaRatio(Float32 bmoQuotaRatio)
+    { bmoQuotaRatio_ = bmoQuotaRatio; }
+  virtual Float32 getBmoQuotaRatio(void)
+    { return bmoQuotaRatio_;}
+
   NABoolean isNonBMOPartialGroupBy() { return (isPartialGroup_ == TRUE); }
 
 };

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/comexe/ComTdbHashj.cpp
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbHashj.cpp b/core/sql/comexe/ComTdbHashj.cpp
index 56f5cab..2c8e2b8 100644
--- a/core/sql/comexe/ComTdbHashj.cpp
+++ b/core/sql/comexe/ComTdbHashj.cpp
@@ -199,7 +199,7 @@ ComTdbHashj::ComTdbHashj(ComTdb * leftChildTdb,
     checkInnerNullExpr_(checkInnerNullExpr),
     checkOuterNullExpr_(checkOuterNullExpr),
     afterJoinPred5_(afterJoinPred5),
-    hjMemEstInMbPerCpu_(0),
+    hjMemEstInKBPerNode_(0),
     bmoCitizenshipFactor_(0),
     pMemoryContingencyMB_(0),
     bmoMinMemBeforePressureCheck_(0),
@@ -379,8 +379,8 @@ void ComTdbHashj::displayContents(Space * space,ULng32 flag)
 		  memUsagePercent_,pressureThreshold_);
       space->allocateAndCopyToAlignedSpace(buf, str_len(buf), sizeof(short));
 
-      str_sprintf(buf,"hjMemEstInMbPerCpu = %f, estimateErrorPenalty = %d ",
-		  hjMemEstInMbPerCpu_, hjGrowthPercent_);
+      str_sprintf(buf,"hjMemEstInKbPerNode = %f, estimateErrorPenalty = %d ",
+		  hjMemEstInKBPerNode_, hjGrowthPercent_);
       space->allocateAndCopyToAlignedSpace(buf, str_len(buf), sizeof(short));
       
       str_sprintf(buf,"bmoCitizenshipFactor = %f, PhyMemoryContingencyMB = %d ",

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/comexe/ComTdbHashj.h
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbHashj.h b/core/sql/comexe/ComTdbHashj.h
index a32aa05..f382acb 100644
--- a/core/sql/comexe/ComTdbHashj.h
+++ b/core/sql/comexe/ComTdbHashj.h
@@ -478,7 +478,7 @@ private:
   ExExprPtr    checkInnerNullExpr_;                 // 296-303
   ExExprPtr    checkOuterNullExpr_;                 // 304-311
   ExExprPtr    afterJoinPred5_;                     // 312-319
-  Float32      hjMemEstInMbPerCpu_;                 // 320-323
+  Float32      hjMemEstInKBPerNode_;                 // 320-323
   Float32      bmoCitizenshipFactor_;               // 324-327
   Int32        pMemoryContingencyMB_;               // 328-331
   Int16        scratchIOVectorSize_;                // 332-333
@@ -506,7 +506,8 @@ private:
   // additional tuple for the min max values.
   ExCriDescPtr leftDownCriDesc_;                    // 352-369
   UInt16       hjFlags2_;                           // 370-371
-  char         fillersComTdbHashj_[4];              // 372-375
+  Float32      estMemoryUsage_;                     // 372-375
+  Float32        bmoQuotaRatio_;
   
 
 protected:
@@ -533,8 +534,8 @@ protected:
   Int32 getMemoryContingencyMB(void)
     { return pMemoryContingencyMB_; }
 
-  void    setHjMemEstInMbPerCpu(Float32 s) {hjMemEstInMbPerCpu_=s;}
-  Float32 getHjMemEstInMbPerCpu() {return hjMemEstInMbPerCpu_;}
+  void    setHjMemEstInKBPerNode(Float32 s) {hjMemEstInKBPerNode_=s;}
+  Float32 getHjMemEstInKBPerNode() {return hjMemEstInKBPerNode_;}
   Float32 hjGrowthPercent() {return Float32(hjGrowthPercent_/100.0);}
   void  setBmoMinMemBeforePressureCheck(UInt16 m)
   { bmoMinMemBeforePressureCheck_ = m ; }
@@ -544,6 +545,15 @@ protected:
   { bmoMaxMemThresholdMB_ = m ; }
   UInt16  getBMOMaxMemThresholdMB()
   { return bmoMaxMemThresholdMB_; }
+  void setEstimatedMemoryUsage(Float32 estMemory)
+    { estMemoryUsage_ = estMemory; }
+  virtual Float32 getEstimatedMemoryUsage(void)
+    { return estMemoryUsage_;}
+
+  void setBmoQuotaRatio(Float32 bmoQuotaRatio)
+    { bmoQuotaRatio_ = bmoQuotaRatio; }
+  virtual Float32 getBmoQuotaRatio(void)
+    { return bmoQuotaRatio_;}
 };
 
 inline ComTdb * ComTdbHashj::getLeftChildTdb() {

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/comexe/ComTdbSort.cpp
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbSort.cpp b/core/sql/comexe/ComTdbSort.cpp
index 154cad2..4ca4e45 100644
--- a/core/sql/comexe/ComTdbSort.cpp
+++ b/core/sql/comexe/ComTdbSort.cpp
@@ -90,10 +90,11 @@ ComTdbSort::ComTdbSort(ex_expr * sort_key_expr,
 #pragma warn(1506)  // warning elimination 
   sortOptions_(sort_options),
   flags_(0),
-  sortMemEstInMbPerCpu_(0),
+  sortMemEstInKBPerNode_(0),
   sortGrowthPercent_(sortGrowthPercent),
   bmoCitizenshipFactor_(0),
-  pMemoryContingencyMB_(0)
+  pMemoryContingencyMB_(0),
+  topNThreshold_(-1) 
 {
 
 }
@@ -151,8 +152,8 @@ void ComTdbSort::displayContents(Space * space,ULng32 flag)
 	    str_sprintf(buf,"tuppIndex_ = %d", tuppIndex_);
             space->allocateAndCopyToAlignedSpace(buf, str_len(buf), sizeof(short));
 
-            str_sprintf(buf,"sortMemEstInMbPerCpu = %f, estimateErrorPenalty = %d ",
-                sortMemEstInMbPerCpu_, sortGrowthPercent_);
+            str_sprintf(buf,"sortMemEstInKBPerNode_ = %f, estimateErrorPenalty = %d ",
+                sortMemEstInKBPerNode_, sortGrowthPercent_);
             space->allocateAndCopyToAlignedSpace(buf, str_len(buf), sizeof(short));
 
             str_sprintf(buf,"bmoCitizenshipFactor = %f, PhyMemoryContingencyMB = %d ",

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/comexe/ComTdbSort.h
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbSort.h b/core/sql/comexe/ComTdbSort.h
index 15a64c3..28e772e 100644
--- a/core/sql/comexe/ComTdbSort.h
+++ b/core/sql/comexe/ComTdbSort.h
@@ -201,11 +201,15 @@ protected:
   Int32 sortPartialKeyLen_;                             // 56-59
 
   UInt32 minimalSortRecs_;                              // 60-63
-  Float32 sortMemEstInMbPerCpu_;                        // 64-67
+  Float32 sortMemEstInKBPerNode_;                        // 64-67
   Float32 bmoCitizenshipFactor_;                        // 68-71
   Int32  pMemoryContingencyMB_;                        // 72-75
   UInt16 sortGrowthPercent_;                            // 76-77
-  char fillersComTdbSort_[18];                          // 78-95
+  char filler_1[2];                                     // 78-79
+  Int32 topNThreshold_;                                 // 80-83
+  Float32 estMemoryUsage_;                             // 84-87
+  Float32 bmoQuotaRatio_;                              // 88-92
+  char fillersComTdbSort_[4];                          // 93-96
 
 public:
 
@@ -331,10 +335,25 @@ public:
   Int32 getMemoryContingencyMB(void)
     { return pMemoryContingencyMB_; }
   
-  void setSortMemEstInMbPerCpu(Float32 s) {sortMemEstInMbPerCpu_=s;}
-  Float32 getSortMemEstInMbPerCpu() {return sortMemEstInMbPerCpu_;}
+  void setSortMemEstInKBPerNode(Float32 s) {sortMemEstInKBPerNode_=s;}
+  Float32 getSortMemEstInKBPerNode() {return sortMemEstInKBPerNode_;}
   Float32 sortGrowthPercent() {return Float32(sortGrowthPercent_/100.0);}
 
+  void setTopNThreshold(Int32 limit)
+    {  topNThreshold_ = limit;} 
+  Int32 getTopNThreshold(void)
+    { return topNThreshold_; }
+
+  void setEstimatedMemoryUsage(Float32 estMemory)
+    { estMemoryUsage_ = estMemory; }
+  virtual Float32 getEstimatedMemoryUsage(void)
+    { return estMemoryUsage_;}
+
+  void setBmoQuotaRatio(Float32 bmoQuotaRatio)
+    { bmoQuotaRatio_ = bmoQuotaRatio; }
+  virtual Float32 getBmoQuotaRatio(void)
+    { return bmoQuotaRatio_;}
+
   // ---------------------------------------------------------------------
   // Used by the internal SHOWPLAN command to get attributes of a TDB.
   // ---------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/comexe/ComTdbStats.cpp
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbStats.cpp b/core/sql/comexe/ComTdbStats.cpp
index 27db4c1..90c0c4c 100644
--- a/core/sql/comexe/ComTdbStats.cpp
+++ b/core/sql/comexe/ComTdbStats.cpp
@@ -212,7 +212,9 @@ CompilerStatsInfo::CompilerStatsInfo()
        ofMode_((UInt16)ComTdb::OFM_DISK),
        collectStatsType_((UInt16)ComTdb::NO_STATS),
        queryType_((Int16)ComTdbRoot::SQL_UNKNOWN),
-       subqueryType_((Int16)ComTdbRoot::SQL_STMT_NA)
+       subqueryType_((Int16)ComTdbRoot::SQL_STMT_NA),
+       bmoMemLimitPerNode_(0),
+       estBmoMemPerNode_(0)
 {
 
 }
@@ -250,7 +252,8 @@ CompilerStatsInfo& CompilerStatsInfo::operator=(CompilerStatsInfo&csi)
   ofMode_ = csi.ofMode_;
   queryType_ = csi.queryType_;
   subqueryType_ = csi.subqueryType_;
-
+  bmoMemLimitPerNode_ = csi.bmoMemLimitPerNode_;
+  estBmoMemPerNode_ = csi.estBmoMemPerNode_;
   return *this;
 }
 
@@ -323,7 +326,8 @@ void CompilerStatsInfo :: translateToExternalFormat(SQL_QUERY_COMPILER_STATS_INF
     = queryType();
   query_comp_stats_info->subqueryType
     = subqueryType();
- 
+  query_comp_stats_info->bmoMemLimitPerNode = bmoMemLimitPerNode(); 
+  query_comp_stats_info->estBmoMemPerNode = estBmoMemPerNode(); 
 
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/comexe/ComTdbStats.h
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbStats.h b/core/sql/comexe/ComTdbStats.h
index 272cf65..ce9cdbe 100644
--- a/core/sql/comexe/ComTdbStats.h
+++ b/core/sql/comexe/ComTdbStats.h
@@ -268,7 +268,6 @@ private:
 
   NABasicPtr compileInfo_;                          // 88 - 95
   Int32 compileInfoLen_;                            // 96 - 99
-
   char filler_[42];                                 // 100 - 141
 };
 
@@ -310,6 +309,8 @@ public:
   UInt16 &ofMode() { return ofMode_; }
   Int16 &queryType() { return queryType_; }
   Int16 &subqueryType() { return subqueryType_; }
+  double &bmoMemLimitPerNode() { return bmoMemLimitPerNode_; }
+  double &estBmoMemPerNode() { return estBmoMemPerNode_; }
 
   NABoolean mandatoryCrossProduct() { return (flags_ & MANDATORY_CROSS_PRODUCT) != 0; }
   void setMandatoryCrossProduct(NABoolean v)      
@@ -370,7 +371,9 @@ private:
   UInt16 ofMode_;
   Int16 queryType_;
   Int16 subqueryType_;
-  char   filler_[48];
+  double bmoMemLimitPerNode_;
+  double estBmoMemPerNode_;
+  char   filler_[32];
 };
 
 // the enum values assigned to the enums must be the same as the

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/common/ComSmallDefs.h
----------------------------------------------------------------------
diff --git a/core/sql/common/ComSmallDefs.h b/core/sql/common/ComSmallDefs.h
index 67a9af2..ab07e6a 100644
--- a/core/sql/common/ComSmallDefs.h
+++ b/core/sql/common/ComSmallDefs.h
@@ -1946,6 +1946,13 @@ enum ComQiScope
 #define MAX_CHAR_COL_LENGTH_IN_BYTES 16777216
 #define MAX_CHAR_COL_LENGTH_IN_BYTES_STR "16777216"
 
+enum BMOQuotaRatio 
+ {
+    NO_RATIO = -1,
+    MIN_QUOTA = 0,
+    MAX_QUOTA = 1 
+ };
+
 //
 // Definition of class ComUID
 //

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/common/NAMemory.cpp
----------------------------------------------------------------------
diff --git a/core/sql/common/NAMemory.cpp b/core/sql/common/NAMemory.cpp
index 3dd5afd..ce78db9 100644
--- a/core/sql/common/NAMemory.cpp
+++ b/core/sql/common/NAMemory.cpp
@@ -2489,6 +2489,7 @@ NABoolean NAMemory::getUsage(size_t * lastBlockSize, size_t * freeSize, size_t *
   return crowded;
 }
 
+
 // ---------------------------------------------------------------------------
 // NASegGlobals methods
 // ---------------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/common/NAMemory.h
----------------------------------------------------------------------
diff --git a/core/sql/common/NAMemory.h b/core/sql/common/NAMemory.h
index 609e936..a6f04c5 100644
--- a/core/sql/common/NAMemory.h
+++ b/core/sql/common/NAMemory.h
@@ -393,8 +393,9 @@ public:
   inline NASegGlobals * getSegGlobals() { return segGlobals_; }
   char *getName() {  return name_; }
   NAMemoryType getType() {  return type_; }
+
   NABoolean getUsage(size_t* lastSegSize, size_t* freeSize, size_t* totalSize);
-  // for debugging
+
   NABoolean containsAddress(void *addr)
         { return NABlock::blockHolding(firstBlk_, addr) != NULL; }
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/Allocator.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/Allocator.cpp b/core/sql/executor/Allocator.cpp
index 8870488..c9a451f 100644
--- a/core/sql/executor/Allocator.cpp
+++ b/core/sql/executor/Allocator.cpp
@@ -117,7 +117,7 @@ namespace ExOverflow
 #endif
       if (yieldMB)
       {
-        GetCliGlobals()->yieldMemoryQuota(yieldMB);
+        exeGlobals_->yieldMemoryQuota(yieldMB);
       }
     }
 
@@ -184,7 +184,7 @@ namespace ExOverflow
       ByteCount quotaIncrease = (ByteCount) (totalBytes - quota_);
 #endif
       UInt32 quotaIncreaseMB = (quotaIncrease + ONE_MEGABYTE - 1)/ ONE_MEGABYTE;
-      if (GetCliGlobals()->grabMemoryQuotaIfAvailable(quotaIncreaseMB))
+      if (exeGlobals_->grabMemoryQuotaIfAvailable(quotaIncreaseMB))
       {
         quota_ += (quotaIncreaseMB * ONE_MEGABYTE);
       }

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/ExExeUtilGetStats.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ExExeUtilGetStats.cpp b/core/sql/executor/ExExeUtilGetStats.cpp
index 1dd0d44..81d94e8 100644
--- a/core/sql/executor/ExExeUtilGetStats.cpp
+++ b/core/sql/executor/ExExeUtilGetStats.cpp
@@ -3095,7 +3095,7 @@ short ExExeUtilGetRTSStatisticsTcb::work()
           bmoStatsItems_[17].statsItem_id = SQLSTATS_SCRATCH_IO_TIME;
           bmoStatsItems_[18].statsItem_id = SQLSTATS_SCRATCH_IO_MAX_TIME;
                   
-          // maxBMOStatsItems_ is set to 19 
+          // maxBMOStatsItems_ is set to 18 
           // TDB_NAME
           bmoStatsItems_[1].str_value = new (getGlobals()->getDefaultHeap())
                       char[MAX_TDB_NAME_LEN+1];

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/ExStats.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ExStats.cpp b/core/sql/executor/ExStats.cpp
index a2d20f6..5231714 100644
--- a/core/sql/executor/ExStats.cpp
+++ b/core/sql/executor/ExStats.cpp
@@ -10420,6 +10420,7 @@ ExBMOStats::ExBMOStats(NAMemory *heap, StatType statType)
   spaceBufferSize_ = -1;
   scratchIOSize_ = -1;
   scratchOverflowMode_ = -1;
+  estMemoryUsage_ = 0; 
 }
 
 ExBMOStats::ExBMOStats(NAMemory *heap, StatType statType,
@@ -10430,10 +10431,14 @@ ExBMOStats::ExBMOStats(NAMemory *heap, StatType statType,
   init(FALSE);
   spaceBufferSize_ = -1;
   scratchIOSize_ = -1;
-  if (tdb != NULL)
+  if (tdb != NULL) {
     scratchOverflowMode_ = ((ComTdb *)tdb)->getOverFlowMode();
-  else
+    estMemoryUsage_ = ((ComTdb *)tdb)->getEstimatedMemoryUsage();
+  }
+  else {
     scratchOverflowMode_ = -1;
+    estMemoryUsage_ = 0;
+  }
 }
 
 ExBMOStats::ExBMOStats(NAMemory *heap, 
@@ -10445,10 +10450,14 @@ ExBMOStats::ExBMOStats(NAMemory *heap,
   init(FALSE);
   spaceBufferSize_ = -1;
   scratchIOSize_ = -1;
-  if (tdb != NULL)
+  if (tdb != NULL) {
     scratchOverflowMode_ = ((ComTdb *)tdb)->getOverFlowMode();
-  else
+    estMemoryUsage_ = ((ComTdb *)tdb)->getEstimatedMemoryUsage();
+  }
+  else {
     scratchOverflowMode_ = -1;
+    estMemoryUsage_ = 0;
+  }
 }
 
 void ExBMOStats::init(NABoolean resetDop)
@@ -10693,6 +10702,9 @@ Lng32 ExBMOStats::getStatsItem(SQLSTATS_ITEM* sqlStats_item)
   case SQLSTATS_SCRATCH_IO_MAX_TIME:
     sqlStats_item->int64_value = scratchIOMaxTime_;
     break;
+  case SQLSTATS_BMO_EST_MEMORY:
+    sqlStats_item->double_value = estMemoryUsage_;
+    break;
   case SQLSTATS_INTERIM_ROW_COUNT:
     sqlStats_item->int64_value = interimRowCount_;
     break;

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/ExStats.h
----------------------------------------------------------------------
diff --git a/core/sql/executor/ExStats.h b/core/sql/executor/ExStats.h
index 5b3b1a8..7b4da48 100644
--- a/core/sql/executor/ExStats.h
+++ b/core/sql/executor/ExStats.h
@@ -1061,6 +1061,7 @@ private:
   Int16 scratchOverflowMode_;   // 0 - disk 1 - SSD
   Int32 topN_;                 // TOPN value
   Int64 interimRowCount_;
+  Float32 estMemoryUsage_;
   Int16 phase_;
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/cluster.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/cluster.cpp b/core/sql/executor/cluster.cpp
index 327c59c..0875a32 100644
--- a/core/sql/executor/cluster.cpp
+++ b/core/sql/executor/cluster.cpp
@@ -308,7 +308,7 @@ ClusterDB::ClusterDB(HashOperator hashOperator,
 		     Float32 bmoCitizenshipFactor,
 		     Int32  pMemoryContingencyMB, 
 		     Float32 estimateErrorPenalty,
-		     Float32 hashMemEstInMbPerCpu,
+		     Float32 hashMemEstInKBPerNode,
 		     ULng32 initialHashTableSize,
 		     ExOperStats * hashOperStats
 		     )
@@ -364,7 +364,7 @@ ClusterDB::ClusterDB(HashOperator hashOperator,
     bmoCitizenshipFactor_(bmoCitizenshipFactor),
     pMemoryContingencyMB_(pMemoryContingencyMB), 
     estimateErrorPenalty_(estimateErrorPenalty),
-    hashMemEstInMbPerCpu_(hashMemEstInMbPerCpu),
+    hashMemEstInKBPerNode_(hashMemEstInKBPerNode),
 
     totalPhase3TimeNoHL_(0),
     maxPhase3Time_(0),
@@ -476,7 +476,7 @@ void ClusterDB::yieldAllMemoryQuota()
 {
   if ( memoryQuotaMB_ == 0 || memoryQuotaMB_ <= minMemoryQuotaMB_ ) return; 
 
-  GetCliGlobals()->yieldMemoryQuota( memoryQuotaMB_ - minMemoryQuotaMB_ );
+  stmtGlobals_->yieldMemoryQuota( memoryQuotaMB_ - minMemoryQuotaMB_ );
 
   if ( doLog_ ) { // LOG -- to show that memory was yielded
     char msg[256];
@@ -508,7 +508,7 @@ void ClusterDB::YieldQuota(UInt32 memNeeded)
   // if there is no memory to yield - then return 
   if ( memToYieldMB <= 1 ) return; // 1 MB - to avoid thrashing
 
-  GetCliGlobals()->yieldMemoryQuota( memToYieldMB );  // Now yield 
+  stmtGlobals_->yieldMemoryQuota( memToYieldMB );  // Now yield 
 
   if ( doLog_ ) { // LOG -- to show that memory was yielded
     char msg[256], msg1[64];
@@ -520,7 +520,7 @@ void ClusterDB::YieldQuota(UInt32 memNeeded)
 	    hashOperator_ == HASH_GROUP_BY ? "HGB" : 
 	        hashOperator_ == SEQUENCE_OLAP ? "OLAP" : "HJ", 
 	    memToYieldMB, id, msg1, memNeededMB,
-	    GetCliGlobals()->unusedMemoryQuota());
+	    stmtGlobals_->unusedMemoryQuota());
 
     // log an EMS event and continue
     SQLMXLoggingArea::logExecRtInfo(NULL, 0, msg, explainNodeId_);
@@ -601,7 +601,7 @@ void ClusterDB::yieldUnusedMemoryQuota(Cluster * theOFList,
   // if there is no memory to yield - then return 
   if ( memToYieldMB <= 1 ) return; // 1 MB - to avoid thrashing
 
-  GetCliGlobals()->yieldMemoryQuota( memToYieldMB );  // Now yield 
+  stmtGlobals_->yieldMemoryQuota( memToYieldMB );  // Now yield 
 
   if ( doLog_ ) { // LOG -- to show that memory was yielded
     char msg[256], msg1[64];
@@ -612,7 +612,7 @@ void ClusterDB::yieldUnusedMemoryQuota(Cluster * theOFList,
 		extraBuffers == 1 ? "HJ" : "HGB", memToYieldMB, 
 		0,
                 msg1, memNeededMB,
-		GetCliGlobals()->unusedMemoryQuota());
+		stmtGlobals_->unusedMemoryQuota());
 
     // log an EMS event and continue
     SQLMXLoggingArea::logExecRtInfo(NULL, 0, msg, explainNodeId_);
@@ -679,7 +679,7 @@ NABoolean ClusterDB::enoughMemory(ULng32 reqSize, NABoolean checkCompilerHints)
     }
 
     // Try to increase the memory quota (from the global "pool") to meet need
-    if ( GetCliGlobals()->grabMemoryQuotaIfAvailable(memNeededMB) ) {
+    if ( stmtGlobals_->grabMemoryQuotaIfAvailable(memNeededMB) ) {
 
       memoryQuotaMB_ += memNeededMB ;  // got it
 
@@ -693,7 +693,7 @@ NABoolean ClusterDB::enoughMemory(ULng32 reqSize, NABoolean checkCompilerHints)
 		    "GRABBED %u MB (%u). Memory used %u, now allowed %u MB, request size %u, unused pool %u",
 		    memNeededMB, 0,
                     memoryUsed_, 
-		    memoryQuotaMB_, reqSize,GetCliGlobals()->unusedMemoryQuota() );
+		    memoryQuotaMB_, reqSize,stmtGlobals_->unusedMemoryQuota() );
 	// log an EMS event and continue
 	SQLMXLoggingArea::logExecRtInfo(NULL, 0, msg, explainNodeId_);
       }
@@ -714,6 +714,7 @@ NABoolean ClusterDB::enoughMemory(ULng32 reqSize, NABoolean checkCompilerHints)
     }
   }
 
+/*
   // Check if we are running out of address space or swap space.
   // getUsage() would return TRUE if and only if memory gets crowded (i.e. we 
   // failed at least once to allocate a desired flat segment size, and the 
@@ -736,7 +737,7 @@ NABoolean ClusterDB::enoughMemory(ULng32 reqSize, NABoolean checkCompilerHints)
       return FALSE;
     }
   }
-
+*/
 
   if (memMonitor_ && memoryUsed_ >= minMemBeforePressureCheck_ ) {
 
@@ -863,7 +864,7 @@ NABoolean ClusterDB::enoughMemory(ULng32 reqSize, NABoolean checkCompilerHints)
       // do the following check if HJ still in phase 1.
       if ( checkCompilerHints )
 	{
-	  Float32 E = hashMemEstInMbPerCpu_ ; //expected memory consumption
+	  Float32 E = hashMemEstInKBPerNode_ / 1024 ; //expected memory consumption
 	  
 #ifdef FUTURE_WORK
 	  //check extreme case first. Expected cannot be more than
@@ -898,7 +899,7 @@ NABoolean ClusterDB::enoughMemory(ULng32 reqSize, NABoolean checkCompilerHints)
 	  if ( C > E ) // consumed memory exceeded the expected -- adjust E
 	    {
 	      E = C * ( 1 + estimateErrorPenalty ) ;
-	      hashMemEstInMbPerCpu_ = E ;
+	      hashMemEstInKBPerNode_ = E * 1024;
 	    }
 	  
 	  Float32 m = E - C;  //delta memory required to avoid overflow.

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/cluster.h
----------------------------------------------------------------------
diff --git a/core/sql/executor/cluster.h b/core/sql/executor/cluster.h
index 7e7fa74..e3b7b1b 100644
--- a/core/sql/executor/cluster.h
+++ b/core/sql/executor/cluster.h
@@ -604,7 +604,7 @@ public:
 	     Float32 bmoCitizenshipFactor = 0,
 	     Int32  pMemoryContingencyMB = 0, 
 	     Float32 estimateErrorPenalty = 0,
-	     Float32 hashMemEstInMbPerCpu = 0,
+	     Float32 hashMemEstInKBPerNode = 0,
 	     ULng32 initialHashTableSize = 0, // default not resizable
 	     ExOperStats * hashOperStats = NULL
 );
@@ -749,7 +749,7 @@ private:
   Float32 bmoCitizenshipFactor_;
   Int32  pMemoryContingencyMB_; 
   Float32 estimateErrorPenalty_;
-  Float32 hashMemEstInMbPerCpu_;
+  Float32 hashMemEstInKBPerNode_;
 
   Int64 totalPhase3TimeNoHL_;
   Int64 maxPhase3Time_;

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/ex_exe_stmt_globals.h
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_exe_stmt_globals.h b/core/sql/executor/ex_exe_stmt_globals.h
index 5815eb7..c007b73 100644
--- a/core/sql/executor/ex_exe_stmt_globals.h
+++ b/core/sql/executor/ex_exe_stmt_globals.h
@@ -307,18 +307,38 @@ public:
 
   inline NABoolean grabMemoryQuotaIfAvailable(ULng32 size)
   { 
+    CliGlobals *cli_globals = GetCliGlobals();
+    if (cli_globals->isEspProcess())
+       return cli_globals->grabMemoryQuotaIfAvailable(size);
     if ( unusedBMOsMemoryQuota_ < size ) return FALSE;
     unusedBMOsMemoryQuota_ -= size ;
     return TRUE;
   }
 
-  inline void resetMemoryQuota() { unusedBMOsMemoryQuota_ = 0 ; }
+  inline void resetMemoryQuota() 
+  {
+    CliGlobals *cli_globals = GetCliGlobals();
+    if (cli_globals->isEspProcess())
+       return cli_globals->resetMemoryQuota();
+    unusedBMOsMemoryQuota_ = 0 ; 
+  }
 
-  inline ULng32 unusedMemoryQuota() { return unusedBMOsMemoryQuota_; }
+  inline ULng32 unusedMemoryQuota() 
+  { 
+    CliGlobals *cli_globals = GetCliGlobals();
+    if (cli_globals->isEspProcess())
+       return cli_globals->unusedMemoryQuota();
+    return unusedBMOsMemoryQuota_;
+  }
 
   inline void yieldMemoryQuota(ULng32 size) 
-  { unusedBMOsMemoryQuota_ += size; }
-
+  { 
+    CliGlobals *cli_globals = GetCliGlobals();
+    if (cli_globals->isEspProcess())
+       return cli_globals->yieldMemoryQuota(size);
+    unusedBMOsMemoryQuota_ += size; 
+  }
+  
   // getStreamTimeout: return TRUE (FALSE) if the stream-timeout was set (was
   // not set). If set, the timeoutValue parameter would return that value
   virtual NABoolean getStreamTimeout( Lng32 & timeoutValue );

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/ex_frag_rt.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_frag_rt.cpp b/core/sql/executor/ex_frag_rt.cpp
index d8b7fa2..bdcea36 100644
--- a/core/sql/executor/ex_frag_rt.cpp
+++ b/core/sql/executor/ex_frag_rt.cpp
@@ -241,51 +241,12 @@ void ExRtFragTable::setInactiveState()
   numRootRequests_--;
 }
 
-#define MAX_NUM_FRAGMENTS_PER_ESP 6
-// multi fragment esp - begin
-static NABoolean sv_esp_multi_fragment = TRUE;
-static UInt8 sv_esp_num_fragments = MAX_NUM_FRAGMENTS_PER_ESP;
-static char *sv_envvar_esp_multi_fragment = 0;
-static char *sv_envvar_esp_num_fragments = 0;
-
-static
-void
-getAssignedEspEnvVar()
-{
-  static bool lv_checked = false;
-
-  if (lv_checked) {
-    return;
-  }
-
-  sv_envvar_esp_multi_fragment = getenv("ESP_MULTI_FRAGMENTS");
-  sv_envvar_esp_num_fragments = getenv("ESP_NUM_FRAGMENTS");
-  lv_checked = true;
-
-  Int32 lv_i;
-  if (sv_envvar_esp_multi_fragment) {
-    lv_i = atoi(sv_envvar_esp_multi_fragment);
-    if (lv_i == 0)
-      sv_esp_multi_fragment = FALSE;
-    else
-      sv_esp_multi_fragment = TRUE;
-  }
-  sv_esp_num_fragments = 6;
-  if (sv_envvar_esp_num_fragments) {
-    lv_i = atoi(sv_envvar_esp_num_fragments);
-    if (lv_i > 0 && lv_i <= MAX_NUM_FRAGMENTS_PER_ESP) {
-      sv_esp_num_fragments = (UInt8)lv_i;
-    }
-  }
-  return;
-}
-// multi fragment esp - end
-
 void ExRtFragTable::assignEsps(NABoolean /*checkResourceGovernor*/,
 			       UInt32 &numOfTotalEspsUsed,
 			       UInt32 &numOfEspsStarted
                                )
 {
+  Int16 esp_multi_fragment, esp_num_fragments;
   Int32 entryNumber, numEntries, launchesStarted, launchesCompleted;
 
   if (state_ == NO_ESPS_USED)
@@ -299,10 +260,7 @@ void ExRtFragTable::assignEsps(NABoolean /*checkResourceGovernor*/,
   ComDiagsArea *diags = glob_->getDiagsArea();
 
   LIST(ExEspDbEntry *) alreadyAssignedEsps(heap);
-  // multi fragment esp - begin
-  getAssignedEspEnvVar(); // sets the static variable: sv_esp_multi_fragment
   // Note: alreadyAssignedEsps is cleared after assigning ESPs for all the instances of a fragment
-  // multi fragment esp - end
 
   // $$$$ check with the resource governor
 
@@ -319,10 +277,8 @@ void ExRtFragTable::assignEsps(NABoolean /*checkResourceGovernor*/,
   Lng32 idleTimeout = getEspIdleTimeout();
   Lng32 assignTimeWindow = currentContext->getSessionDefaults()->getEspAssignTimeWindow();
 
-  if (!sv_envvar_esp_multi_fragment)
-    sv_esp_multi_fragment = fragDir_->espMultiFragments();
-  if (!sv_envvar_esp_num_fragments)
-    sv_esp_num_fragments = fragDir_->espNumFragments();
+  esp_multi_fragment = fragDir_->espMultiFragments();
+  esp_num_fragments = fragDir_->espNumFragments();
 
  // de-coupling ESP with database uid if set
   const char *esp_with_uid = getenv("ESP_WITH_USERID");
@@ -409,7 +365,9 @@ void ExRtFragTable::assignEsps(NABoolean /*checkResourceGovernor*/,
                     idleTimeout,
                     assignTimeWindow,
 		    &assignEspArrays.creatingEspEntry_[entryNumber],
-                    soloFragment
+                    soloFragment, 
+                    esp_multi_fragment,
+                    esp_num_fragments 
 		    );
 
                 break; // Retry is done in IpcGuardianServer::launchNSKLiteProcess
@@ -467,7 +425,7 @@ void ExRtFragTable::assignEsps(NABoolean /*checkResourceGovernor*/,
                 }
 	    }
 	  // for multi fragment esp - begin
-	  if (sv_esp_multi_fragment) {
+	  if (esp_multi_fragment) {
 	    alreadyAssignedEsps.clear();
 	  }
 	  // for multi fragment esp - end
@@ -501,7 +459,9 @@ void ExRtFragTable::assignEsps(NABoolean /*checkResourceGovernor*/,
 		    idleTimeout,
                     assignTimeWindow,
 		    &assignEspArrays.creatingEspEntry_[entryNumber],
-                    soloFragment);
+                    soloFragment,
+                    esp_multi_fragment,
+                    esp_num_fragments);
 	      if (assignEspArrays.creatingEspEntry_[entryNumber] && assignEspArrays.creatingEspEntry_[entryNumber]->isReady())
               {
                 if (glob_->getIpcEnvironment()->getNumOpensInProgress() >= FS_MAX_CONCUR_NOWAIT_OPENS)
@@ -568,7 +528,7 @@ void ExRtFragTable::assignEsps(NABoolean /*checkResourceGovernor*/,
           while (glob_->getIpcEnvironment()->getNumOpensInProgress() > 0)
             glob_->getIpcEnvironment()->getAllConnections()->waitOnAll(IpcInfiniteTimeout);
 	  // multi fragment esp - begin
-	  if (sv_esp_multi_fragment)
+	  if (esp_multi_fragment)
           {
 	    alreadyAssignedEsps.clear();
 	  }
@@ -1522,7 +1482,7 @@ void ExRtFragTable::releaseEsps(NABoolean closeAllOpens)
 		// multi fragment esp - begin 
 		if (fragInst->usedEsp_) {
 		  if ((releasedEsps.insert(fragInst->usedEsp_) == FALSE) && 
-		      (sv_esp_multi_fragment)) {
+		      fragDir_->espMultiFragments()) {
 		    // decrement the usageCount_ of fragInst->usedEsp_
 		    glob_->getEspManager( )->releaseEsp(fragInst->usedEsp_, glob_->verifyESP(), fragInst->usedEsp_->inUse());
 		  }
@@ -2807,7 +2767,9 @@ ExEspDbEntry *ExEspManager::shareEsp(
      Lng32 idleTimeout,
      Lng32 assignTimeWindow,
      IpcGuardianServer **creatingEsp,
-     NABoolean soloFragment)
+     NABoolean soloFragment,
+     Int16 esp_multi_fragment,
+     Int16 esp_num_fragments)
 {
   Int32 nowaitDepth;
   IpcServer *server;
@@ -2828,7 +2790,7 @@ ExEspDbEntry *ExEspManager::shareEsp(
 
     // look up the cache for esp to share
     NABoolean espServerError = FALSE;
-    result = getEspFromCache(alreadyAssignedEsps, statementHeap, statement, clusterName, cpuNum, memoryQuota, user_id, verifyESP, espLevel, idleTimeout, assignTimeWindow, nowaitDepth, espServerError, soloFragment);
+    result = getEspFromCache(alreadyAssignedEsps, statementHeap, statement, clusterName, cpuNum, memoryQuota, user_id, verifyESP, espLevel, idleTimeout, assignTimeWindow, nowaitDepth, espServerError, soloFragment, esp_multi_fragment, esp_num_fragments);
     if (espServerError == TRUE)
       // found error from ESP already assigned to prev segment
       {
@@ -2857,7 +2819,7 @@ ExEspDbEntry *ExEspManager::shareEsp(
       // remote segment not available. look up cache for esp on local segment.
       ptrToClusterName = cliGlobals_->myNodeName();
       NABoolean espServerError = FALSE;
-      result = getEspFromCache(alreadyAssignedEsps, statementHeap, statement, ptrToClusterName, cpuNum, memoryQuota, user_id, verifyESP, espLevel, idleTimeout, assignTimeWindow, nowaitDepth, espServerError, soloFragment);
+      result = getEspFromCache(alreadyAssignedEsps, statementHeap, statement, ptrToClusterName, cpuNum, memoryQuota, user_id, verifyESP, espLevel, idleTimeout, assignTimeWindow, nowaitDepth, espServerError, soloFragment, esp_multi_fragment, esp_num_fragments);
       if (espServerError == TRUE)
         // found error from ESP already assigned to prev segment
         {
@@ -3000,7 +2962,9 @@ ExEspDbEntry *ExEspManager::getEspFromCache(LIST(ExEspDbEntry *) &alreadyAssigne
                                             Lng32 assignTimeWindow,
 					    Int32 nowaitDepth,
                                             NABoolean &espServerError,
-                                            NABoolean soloFragment)
+                                            NABoolean soloFragment,
+                                            Int16 esp_multi_fragment,
+                                            Int16 esp_num_fragments)
 {
   ExEspDbEntry *result = NULL;
   LIST(ExEspDbEntry *) badEsps(statementHeap);
@@ -3076,7 +3040,7 @@ ExEspDbEntry *ExEspManager::getEspFromCache(LIST(ExEspDbEntry *) &alreadyAssigne
 
       ExEspDbEntry *e = espList->usedEntry(i);
 
-      if ((e->inUse_) && (e->soloFragment_ || soloFragment || !(sv_esp_multi_fragment) || e->statement_ != statement))
+      if ((e->inUse_) && (e->soloFragment_ || soloFragment || !(esp_multi_fragment) || e->statement_ != statement))
 	continue;
 
       // don't reuse a broken ESP
@@ -3160,7 +3124,7 @@ ExEspDbEntry *ExEspManager::getEspFromCache(LIST(ExEspDbEntry *) &alreadyAssigne
 
       // we have found a free esp for reuse
       if ((2 * e->usageCount_ + 1 <= nowaitDepth) &&
-          (e->usageCount_ < sv_esp_num_fragments))
+          (e->usageCount_ < esp_num_fragments))
       {
         e->usageCount_++; // multi fragment esp
         e->statement_ = statement;

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/ex_frag_rt.h
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_frag_rt.h b/core/sql/executor/ex_frag_rt.h
index 9dc6ee5..00400ac 100644
--- a/core/sql/executor/ex_frag_rt.h
+++ b/core/sql/executor/ex_frag_rt.h
@@ -527,7 +527,9 @@ class ExEspManager
 	 Lng32 idleTimeout,
 	 Lng32 assignTimeWindow,
 	 IpcGuardianServer **creatingEsp,
-	 NABoolean soloFragment);
+	 NABoolean soloFragment,
+	 Int16 esp_multi_fragment,
+         Int16 esp_num_fragments);
 
     void releaseEsp(ExEspDbEntry *esp, NABoolean verifyEsp, NABoolean badEsp);
 
@@ -564,7 +566,9 @@ class ExEspManager
                                   Lng32 assignTimeWindow,
 				  Int32 nowaitDepth,
                                   NABoolean &espServerError,
-                                  NABoolean soloFragment);
+                                  NABoolean soloFragment,
+	                          Int16 esp_multi_fragment,
+                                  Int16 esp_num_fragments);
   IpcCpuNum getRoundRobinCPU();
 
   // ESP state tracing >>

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/ex_hashj.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_hashj.cpp b/core/sql/executor/ex_hashj.cpp
index b029975..bc032c4 100644
--- a/core/sql/executor/ex_hashj.cpp
+++ b/core/sql/executor/ex_hashj.cpp
@@ -1403,7 +1403,7 @@ NABoolean ex_hashj_tcb::allocateClusters() {
 
   ULng32 minB4Chk = hashJoinTdb().getBmoMinMemBeforePressureCheck() * ONE_MEG;
   // estimate memory needed in phase 1 (not incl. hash tables)
-  Float32 memEstInMbPerCpu = (Float32)(innerTableSize / ONE_MEG) ;
+  Float32 memEstInKBPerNode = (Float32)(innerTableSize / 1024) ;
 
   // Only set cross product optimizations on when there is no
   // right search expression 
@@ -1450,7 +1450,7 @@ NABoolean ex_hashj_tcb::allocateClusters() {
 				    hashJoinTdb().getMemoryContingencyMB(),
 				    // to estimate the error penalty
 				    hashJoinTdb().hjGrowthPercent(),
-				    memEstInMbPerCpu, // estimate mem needed
+				    memEstInKBPerNode, // estimate mem needed
 
 				    0,    // Hash-Table not resizable
 				    getStatsEntry()

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/ex_mj.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_mj.cpp b/core/sql/executor/ex_mj.cpp
index d22ebcc..55a589b 100644
--- a/core/sql/executor/ex_mj.cpp
+++ b/core/sql/executor/ex_mj.cpp
@@ -206,7 +206,7 @@ ex_mj_tcb::ex_mj_tcb(const ex_mj_tdb & mj_tdb,
 
       if (assumedMaxMB < quotaMB)
       {
-        GetCliGlobals()->yieldMemoryQuota(quotaMB - assumedMaxMB);
+        stmtGlobals->yieldMemoryQuota(quotaMB - assumedMaxMB);
         quotaMB = assumedMaxMB;
       }
     }

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/ex_sort.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_sort.cpp b/core/sql/executor/ex_sort.cpp
index d54a2a3..a70b9a2 100644
--- a/core/sql/executor/ex_sort.cpp
+++ b/core/sql/executor/ex_sort.cpp
@@ -178,7 +178,8 @@ void ExSortTcb::setupPoolBuffers(ex_queue_entry *pentry_down)
   //sortPool_.
   if((pentry_down->downState.request == ex_queue::GET_N) &&
      (pentry_down->downState.requestValue > 0) &&
-     (sortTdb().topNSortEnabled()))
+     (sortTdb().topNSortEnabled()) &&
+     (pentry_down->downState.requestValue <= sortTdb().getTopNThreshold()))
   {
     topNSortPool_ = new(sortSpace_)
                     ExSimpleSQLBuffer(pentry_down->downState.requestValue + 1,
@@ -392,7 +393,7 @@ ExSortTcb::ExSortTcb(const ExSortTdb & sort_tdb,
   sortCfg_->setScratchIOVectorSize(st->sortOptions_->scratchIOVectorSize());
   sortCfg_->setBmoCitizenshipFactor(st->getBmoCitizenshipFactor());
   sortCfg_->setMemoryContingencyMB(st->getMemoryContingencyMB());
-  sortCfg_->setSortMemEstInMbPerCpu(st->getSortMemEstInMbPerCpu());
+  sortCfg_->setSortMemEstInKBPerNode(st->getSortMemEstInKBPerNode());
   sortCfg_->setEstimateErrorPenalty(st->sortGrowthPercent());
   sortCfg_->setBmoMaxMemThresholdMB(st->sortOptions_->bmoMaxMemThresholdMB());
   sortCfg_->setIntermediateScratchCleanup(st->sortOptions_->intermediateScratchCleanup());

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/executor/ex_split_bottom.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_split_bottom.cpp b/core/sql/executor/ex_split_bottom.cpp
index 5d7faf8..c1a2360 100644
--- a/core/sql/executor/ex_split_bottom.cpp
+++ b/core/sql/executor/ex_split_bottom.cpp
@@ -735,7 +735,7 @@ ExWorkProcRetcode ex_split_bottom_tcb::work()
             
             setWorkState(WORK_ON_REQUEST);
             glob_->clearAllActivatedSendTopTcbs();
-            GetCliGlobals()->resetMemoryQuota();
+            glob_->resetMemoryQuota();
             glob_->incExecutionCount();
 
           } // if (okToPush)

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/generator/GenExplain.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenExplain.cpp b/core/sql/generator/GenExplain.cpp
index b26a123..25635d4 100644
--- a/core/sql/generator/GenExplain.cpp
+++ b/core/sql/generator/GenExplain.cpp
@@ -334,9 +334,9 @@ RelExpr::addExplainInfo(ComTdb * tdb,
             break;
 
          double BMOsMemory = 
-              generator->getTotalBMOsMemoryPerCPU().value() / (1024 * 1024);
+              generator->getTotalBMOsMemoryPerNode().value() / (1024 * 1024);
          double nBMOsTotalMemory = 
-              (generator->getTotalNBMOsMemoryPerCPU()).value() / (1024 * 1024);
+              (generator->getTotalNBMOsMemoryPerNode()).value() / (1024 * 1024);
          snprintf(buf, 120, "est_memory_per_node: %.2f(Limit), %.2f(BMOs), %.2f(nBMOs) MB ", 
                                    mlimit, BMOsMemory, nBMOsTotalMemory); 
          fragdescr += buf;
@@ -470,14 +470,8 @@ RelExpr::addExplainInfo(ComTdb * tdb,
               explainTuple->setDescription(buf);
            }
         }
-     }
-  } else {
-     if ( generator->getOperEstimatedMemory() > 0 ) {
-       sprintf(buf, "est_memory_per_cpu: %d KB ", 
-          generator->getOperEstimatedMemory());
-       explainTuple->setDescription(buf);
-     }
-  }
+    }
+  } 
 
   //calls virtual subclass-specific function
   addSpecificExplainInfo(explainTuple, tdb, generator);
@@ -1426,7 +1420,7 @@ RelRoot::addSpecificExplainInfo(ExplainTupleMaster *explainTuple,
   ComTdbRoot *rootTdb = (ComTdbRoot *)tdb;
 
   NADefaults &defs = ActiveSchemaDB()->getDefaults();
-  Lng32 mlimit = defs.getAsLong(BMO_MEMORY_LIMIT_PER_NODE);
+  ULng32 mlimit = defs.getAsLong(BMO_MEMORY_LIMIT_PER_NODE);
 
   if (mlimit == 0 && rootTdb->getQueryCostInfo())
   {

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/generator/GenPreCode.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenPreCode.cpp b/core/sql/generator/GenPreCode.cpp
index 5d59a3b..eed8d9d 100644
--- a/core/sql/generator/GenPreCode.cpp
+++ b/core/sql/generator/GenPreCode.cpp
@@ -2157,7 +2157,7 @@ RelExpr * RelRoot::preCodeGen(Generator * generator,
       // total per node
       double m = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) * (1024*1024);
 
-      generator->setBMOsMemoryLimitPerCPU(m);
+      generator->setBMOsMemoryLimitPerNode(m);
 
     }
 
@@ -6660,6 +6660,7 @@ RelExpr * Sort::preCodeGen(Generator * generator,
 				       numUnblockedHalloweenScansBefore);
 	}
     }
+  topNRows_ = generator->getTopNRows();
   return this;
 
 } // Sort::preCodeGen()
@@ -6729,7 +6730,7 @@ RelExpr *ProbeCache::preCodeGen(Generator * generator,
   */
 
   if ((ActiveSchemaDB()->getDefaults()).getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0)
-    generator->incrNBMOsMemoryPerCPU(getEstimatedRunTimeMemoryUsage(TRUE));
+    generator->incrNBMOsMemoryPerNode(getEstimatedRunTimeMemoryUsage(TRUE));
 
   markAsPreCodeGenned();
   return this;
@@ -7234,7 +7235,7 @@ RelExpr * Exchange::preCodeGen(Generator * generator,
     } // isEspExchange() && !eliminateThisExchange
   
   if ((ActiveSchemaDB()->getDefaults()).getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0)
-    generator->incrNBMOsMemoryPerCPU(getEstimatedRunTimeMemoryUsage(TRUE));
+    generator->incrNBMOsMemoryPerNode(getEstimatedRunTimeMemoryUsage(TRUE));
   
   return result;
   
@@ -10818,6 +10819,12 @@ RelExpr * FirstN::preCodeGen(Generator * generator,
   if (nodeIsPreCodeGenned())
     return this;
 
+
+  if (getFirstNRows() > 0)
+     generator->setTopNRows(getFirstNRows());
+  else
+     generator->setTopNRows(ActiveSchemaDB()->getDefaults().getAsULong(GEN_SORT_TOPN_THRESHOLD));
+
   if (! RelExpr::preCodeGen(generator,externalInputs,pulledNewInputs))
     return NULL;
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/generator/GenProbeCache.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenProbeCache.cpp b/core/sql/generator/GenProbeCache.cpp
index 63035e9..7e0b150 100644
--- a/core/sql/generator/GenProbeCache.cpp
+++ b/core/sql/generator/GenProbeCache.cpp
@@ -364,14 +364,14 @@ short ProbeCache::codeGen(Generator *generator)
       // that there is room enough for one row.
     }
 
-   double  memoryLimitPerCpu =
+   double  memoryLimitPerInstance =
       ActiveSchemaDB()->getDefaults().getAsLong(EXE_MEMORY_FOR_PROBE_CACHE_IN_MB) * 1024 * 1024;
    double estimatedMemory;
    
    if (numInnerTuples_ > 0) {
       estimatedMemory = numInnerTuples_ * innerRecLength;
-      if (estimatedMemory > memoryLimitPerCpu) {
-          numInnerTuples_ = memoryLimitPerCpu / innerRecLength;
+      if (estimatedMemory > memoryLimitPerInstance) {
+          numInnerTuples_ = memoryLimitPerInstance / innerRecLength;
           queue_index pUpSize_calc;
  
           pUpSize_calc = numInnerTuples_ ;
@@ -430,17 +430,10 @@ short ProbeCache::codeGen(Generator *generator)
 
   double probeCacheMemEst = getEstimatedRunTimeMemoryUsage(probeCacheTdb);
   generator->addToTotalEstimatedMemory(probeCacheMemEst);
-
+  Lng32 pcMemEstInKBPerNode = getEstimatedRunTimeMemoryUsage(TRUE).value() / 1024;
   if(!generator->explainDisabled()) {
-     Lng32 pcMemEstInKBPerCPU = (Lng32) (probeCacheMemEst / 1024) ;
-     pcMemEstInKBPerCPU = pcMemEstInKBPerCPU/
-      (MAXOF(generator->compilerStatsInfo().dop(),1));
-    generator->setOperEstimatedMemory(pcMemEstInKBPerCPU );
-
     generator->setExplainTuple(
        addExplainInfo(probeCacheTdb, childExplainTuple, 0, generator));
-
-    generator->setOperEstimatedMemory(0);
   }
 
   generator->setCriDesc(returned_desc, Generator::UP);
@@ -472,7 +465,6 @@ CostScalar ProbeCache::getEstimatedRunTimeMemoryUsage(NABoolean perNode, Lng32 *
 
   // totalMemory is perNode at this point of time.
   double totalMemory = cacheSize + outputBufferSize;
-
   Lng32 numOfStreams = 1;
   const PhysicalProperty* const phyProp = getPhysicalProperty();
   if (phyProp)
@@ -481,6 +473,10 @@ CostScalar ProbeCache::getEstimatedRunTimeMemoryUsage(NABoolean perNode, Lng32 *
      numOfStreams = partFunc->getCountOfPartitions();
      if (numOfStreams <= 0)
         numOfStreams = 1;
+     double  memoryLimitPerInstance =
+          ActiveSchemaDB()->getDefaults().getAsLong(EXE_MEMORY_FOR_PROBE_CACHE_IN_MB) * 1024 * 1024;
+     if (totalMemory > memoryLimitPerInstance)
+        totalMemory = memoryLimitPerInstance;          
      totalMemory *= numOfStreams;
   }
   if (numStreams != NULL)
@@ -489,11 +485,6 @@ CostScalar ProbeCache::getEstimatedRunTimeMemoryUsage(NABoolean perNode, Lng32 *
      totalMemory /= MINOF(MAXOF(((NAClusterInfoLinux*)gpClusterInfo)->getTotalNumberOfCPUs(), 1), numOfStreams);
   else
      totalMemory /= numOfStreams;
-
-  double  memoryLimitPerCpu =
-      ActiveSchemaDB()->getDefaults().getAsLong(EXE_MEMORY_FOR_PROBE_CACHE_IN_MB) * 1024 * 1024;
-  if (totalMemory > memoryLimitPerCpu)
-     totalMemory = memoryLimitPerCpu;          
   return totalMemory;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/generator/GenRelEnforcer.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenRelEnforcer.cpp b/core/sql/generator/GenRelEnforcer.cpp
index 52ff9ed..d6b746d 100644
--- a/core/sql/generator/GenRelEnforcer.cpp
+++ b/core/sql/generator/GenRelEnforcer.cpp
@@ -1036,16 +1036,14 @@ short Exchange::codeGenForESP(Generator * generator)
 
     if(!generator->explainDisabled())
     {
-      Lng32 sbMemEstInKBPerCPU = (Lng32) ((totalMemoryST + totalMemorySB) / 1024) ;
-      sbMemEstInKBPerCPU = sbMemEstInKBPerCPU/
+      Lng32 sbMemEstInKBPerNode = (Lng32) ((totalMemoryST + totalMemorySB) / 1024) ;
+      sbMemEstInKBPerNode = sbMemEstInKBPerNode/
         (MAXOF(generator->compilerStatsInfo().dop(),1));
-      generator->setOperEstimatedMemory(sbMemEstInKBPerCPU);
        
       generator->setExplainTuple(
 	  addExplainInfo(splitBottom, childExplainTuple, 0, generator));
       sendBottom->setExplainNodeId(generator->getExplainNodeId());
 
-      generator->setOperEstimatedMemory(0);
     }
     
     // ExplainTuple *sendBotExplain = 
@@ -1393,11 +1391,6 @@ CostScalar Exchange::getEstimatedRunTimeMemoryUsage(NABoolean perNode, Lng32 *nu
     // split top. 
   }
 
-
-  const PhysicalProperty* const phyProp = getPhysicalProperty();
-  if (phyProp != NULL) {
-     memoryRequired = numTopEsps * memoryRequired;
-  }
   if (numStreams != NULL)
      *numStreams = numTopEsps;
   if ( perNode == TRUE ) 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/generator/GenRelGrby.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenRelGrby.cpp b/core/sql/generator/GenRelGrby.cpp
index 9f750b0..1af3079 100644
--- a/core/sql/generator/GenRelGrby.cpp
+++ b/core/sql/generator/GenRelGrby.cpp
@@ -1477,6 +1477,8 @@ short HashGroupBy::codeGen(Generator * generator) {
 
   double memQuota = 0;
   Lng32 numStreams;
+  double memQuotaRatio;
+  double bmoMemoryUsagePerNode = getEstimatedRunTimeMemoryUsage(TRUE, &numStreams).value();
 
   if(isPartialGroupBy) {
     // The Quota system does not apply to Partial GroupBy
@@ -1507,38 +1509,35 @@ short HashGroupBy::codeGen(Generator * generator) {
       // Apply quota system if either one the following two is true:
       //   1. the memory limit feature is turned off and more than one BMOs
       //   2. the memory limit feature is turned on
-      NABoolean mlimitPerCPU = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0;
+      NABoolean mlimitPerNode = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0;
 
-      if ( mlimitPerCPU || numBMOsInFrag > 1 ||
+      if ( mlimitPerNode || numBMOsInFrag > 1 ||
            (numBMOsInFrag == 1 && CmpCommon::getDefault(EXE_SINGLE_BMO_QUOTA) == DF_ON)) {
-        double bmoMemoryUsage = getEstimatedRunTimeMemoryUsage(TRUE, &numStreams).value();
         memQuota =
            computeMemoryQuota(generator->getEspLevel() == 0,
-                              mlimitPerCPU,
-                              generator->getBMOsMemoryLimitPerCPU().value(),
-                              //generator->getTotalNumBMOsPerCPU(),
+                              mlimitPerNode,
+                              generator->getBMOsMemoryLimitPerNode().value(),
                               generator->getTotalNumBMOs(),
-                              generator->getTotalBMOsMemoryPerCPU().value(),
+                              generator->getTotalBMOsMemoryPerNode().value(),
                               numBMOsInFrag, 
-                              bmoMemoryUsage,
-                              numStreams
+                              bmoMemoryUsagePerNode,
+                              numStreams,
+                              memQuotaRatio
                              );
-
-        Lng32 hjGyMemoryLowbound = defs.getAsLong(BMO_MEMORY_LIMIT_LOWER_BOUND_HASHGROUPBY);
-        Lng32 memoryUpperbound = defs.getAsLong(BMO_MEMORY_LIMIT_UPPER_BOUND);
-
-        if ( memQuota < hjGyMemoryLowbound )
-           memQuota = hjGyMemoryLowbound;
-        else if (memQuota >  memoryUpperbound)
-           memQuota = memoryUpperbound;
-
-        hashGrbyTdb->setMemoryQuotaMB( UInt16(memQuota) );
       }
+      Lng32 hjGyMemoryLowbound = defs.getAsLong(BMO_MEMORY_LIMIT_LOWER_BOUND_HASHGROUPBY);
+      Lng32 memoryUpperbound = defs.getAsLong(BMO_MEMORY_LIMIT_UPPER_BOUND);
+      if ( memQuota < hjGyMemoryLowbound ) {
+          memQuota = hjGyMemoryLowbound;
+          memQuotaRatio = BMOQuotaRatio::MIN_QUOTA;
+      }
+      else if (memQuota >  memoryUpperbound)
+          memQuota = memoryUpperbound;
+
+      hashGrbyTdb->setMemoryQuotaMB( UInt16(memQuota) );
+      hashGrbyTdb->setBmoQuotaRatio(memQuotaRatio);
     }
 
-    generator->addToTotalOverflowMemory(
-          getEstimatedRunTimeOverflowSize(memQuota)
-                                    );
   }
 
   generator->addToTotalOverflowMemory(
@@ -1551,17 +1550,12 @@ short HashGroupBy::codeGen(Generator * generator) {
 			  getAsULong(EXE_TEST_HASH_FORCE_OVERFLOW_EVERY));
 
   double hashGBMemEst = getEstimatedRunTimeMemoryUsage(hashGrbyTdb);
+  hashGrbyTdb->setEstimatedMemoryUsage(hashGBMemEst / 1024);
   generator->addToTotalEstimatedMemory(hashGBMemEst);
 
   if ( generator->getRightSideOfFlow() ) 
     hashGrbyTdb->setPossibleMultipleCalls(TRUE);
 
-  Lng32 hgbMemEstInKBPerCPU = (Lng32)(hashGBMemEst / 1024) ;
-  hgbMemEstInKBPerCPU = hgbMemEstInKBPerCPU/
-    (MAXOF(generator->compilerStatsInfo().dop(),1));
-  hashGrbyTdb->setHgbMemEstInMbPerCpu
-    ( Float32(MAXOF(hgbMemEstInKBPerCPU/1024,1)) );
-
   // For now use variable size records whenever Aligned format is
   // used.
   if (resizeCifRecord) {//tupleFormat == ExpTupleDesc::SQLMX_ALIGNED_FORMAT) {
@@ -1573,17 +1567,13 @@ short HashGroupBy::codeGen(Generator * generator) {
   }
 
   hashGrbyTdb->setCIFON((tupleFormat == ExpTupleDesc::SQLMX_ALIGNED_FORMAT));
-  if(!generator->explainDisabled()) {
-    generator->setOperEstimatedMemory(hgbMemEstInKBPerCPU);
-
+  hashGrbyTdb->setHgbMemEstInKBPerNode(bmoMemoryUsagePerNode / 1024 );
+  if (!generator->explainDisabled()) {
     generator->setExplainTuple(
        addExplainInfo(hashGrbyTdb, childExplainTuple, 0, generator));
 
-    generator->setOperEstimatedMemory(0);
   }
 
-
-
   // set the new up cri desc.
   generator->setCriDesc(returnedDesc, Generator::UP);
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/generator/GenRelJoin.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenRelJoin.cpp b/core/sql/generator/GenRelJoin.cpp
index 5c3617b..b8cde84 100644
--- a/core/sql/generator/GenRelJoin.cpp
+++ b/core/sql/generator/GenRelJoin.cpp
@@ -1784,7 +1784,9 @@ short HashJoin::codeGen(Generator * generator) {
   UInt16 numBMOsInFrag = (UInt16)generator->getFragmentDir()->getNumBMOs();
         
   double memQuota = 0;
+  double memQuotaRatio;
   Lng32 numStreams;
+  double bmoMemoryUsagePerNode = getEstimatedRunTimeMemoryUsage(TRUE, &numStreams).value();
   if (mmu != 0) {
     memQuota = mmu;
     hashj_tdb->setMemoryQuotaMB(mmu);
@@ -1792,32 +1794,34 @@ short HashJoin::codeGen(Generator * generator) {
     // Apply quota system if either one the following two is true:
     //   1. the memory limit feature is turned off and more than one BMOs 
     //   2. the memory limit feature is turned on
-    NABoolean mlimitPerCPU = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0;
+    NABoolean mlimitPerNode = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0;
     
-    if ( mlimitPerCPU || numBMOsInFrag > 1 ||
+    if ( mlimitPerNode || numBMOsInFrag > 1 ||
          (numBMOsInFrag == 1 && CmpCommon::getDefault(EXE_SINGLE_BMO_QUOTA) == DF_ON)) {
-        double bmoMemoryUsage = getEstimatedRunTimeMemoryUsage(TRUE, &numStreams).value();
         memQuota = 
            computeMemoryQuota(generator->getEspLevel() == 0,
-                              mlimitPerCPU,
-                              generator->getBMOsMemoryLimitPerCPU().value(),
+                              mlimitPerNode,
+                              generator->getBMOsMemoryLimitPerNode().value(),
                               generator->getTotalNumBMOs(),
-                              generator->getTotalBMOsMemoryPerCPU().value(),
+                              generator->getTotalBMOsMemoryPerNode().value(),
                               numBMOsInFrag, 
-                              bmoMemoryUsage,
-                              numStreams
+                              bmoMemoryUsagePerNode,
+                              numStreams,
+                              memQuotaRatio
                              );
-                                  
-        Lng32 hjMemoryLowbound = defs.getAsLong(BMO_MEMORY_LIMIT_LOWER_BOUND_HASHJOIN);
-        Lng32 memoryUpperbound = defs.getAsLong(BMO_MEMORY_LIMIT_UPPER_BOUND);
-
-        if ( memQuota < hjMemoryLowbound )
-           memQuota = hjMemoryLowbound;
-        else if (memQuota >  memoryUpperbound)
-           memQuota = memoryUpperbound;
-           
-        hashj_tdb->setMemoryQuotaMB( UInt16(memQuota) );
+    }                                
+    Lng32 hjMemoryLowbound = defs.getAsLong(BMO_MEMORY_LIMIT_LOWER_BOUND_HASHJOIN);
+    Lng32 memoryUpperbound = defs.getAsLong(BMO_MEMORY_LIMIT_UPPER_BOUND);
+
+    if ( memQuota < hjMemoryLowbound ) {
+       memQuota = hjMemoryLowbound;
+       memQuotaRatio = BMOQuotaRatio::MIN_QUOTA;
     }
+    else if (memQuota >  memoryUpperbound)
+       memQuota = memoryUpperbound;
+       memQuotaRatio = BMOQuotaRatio::MIN_QUOTA;
+    hashj_tdb->setMemoryQuotaMB( UInt16(memQuota) );
+    hashj_tdb->setBmoQuotaRatio(memQuotaRatio);
   }
 
   if (beforeJoinPredOnOuterOnly())
@@ -1828,6 +1832,7 @@ short HashJoin::codeGen(Generator * generator) {
                                      );
 
   double hjMemEst = getEstimatedRunTimeMemoryUsage(hashj_tdb);
+  hashj_tdb->setEstimatedMemoryUsage(hjMemEst / 1024);
   generator->addToTotalEstimatedMemory(hjMemEst);
 
   if ( generator->getRightSideOfFlow() )
@@ -1845,12 +1850,6 @@ short HashJoin::codeGen(Generator * generator) {
   defs.getFloat(BMO_CITIZENSHIP_FACTOR, bmoCtzFactor);
   hashj_tdb->setBmoCitizenshipFactor((Float32)bmoCtzFactor);
 
-  Lng32 hjMemEstInKBPerCPU = (Lng32)(hjMemEst / 1024) ;
-  hjMemEstInKBPerCPU = hjMemEstInKBPerCPU/
-    (MAXOF(generator->compilerStatsInfo().dop(),1));
-  hashj_tdb->setHjMemEstInMbPerCpu
-    ( Float32(MAXOF(hjMemEstInKBPerCPU/1024,1)) );
-
 
   // For now, use variable for all CIF rows based on resizeCifRecord
   if(resizeCifRecord){ //tupleFormat == ExpTupleDesc::SQLMX_ALIGNED_FORMAT) {
@@ -1861,14 +1860,12 @@ short HashJoin::codeGen(Generator * generator) {
     }
   }
 
-
-  if(!generator->explainDisabled()) {
-    generator->setOperEstimatedMemory(hjMemEstInKBPerCPU);
+  hashj_tdb->setHjMemEstInKBPerNode(bmoMemoryUsagePerNode / 1024);
+  if (!generator->explainDisabled()) {
 
     generator->setExplainTuple(
        addExplainInfo(hashj_tdb, leftExplainTuple, rightExplainTuple, generator));
 
-    generator->setOperEstimatedMemory(0);
   }
 
   hashj_tdb->setReturnRightOrdered( returnRightOrdered() );
@@ -3076,42 +3073,43 @@ short MergeJoin::codeGen(Generator * generator)
   double BMOsMemoryLimit = 0;
   UInt16 quotaMB = 0;
   Lng32 numStreams;
+  double memQuotaRatio;
+  double bmoMemoryUsage = getEstimatedRunTimeMemoryUsage(TRUE, &numStreams).value();
 
+  NADefaults &defs = ActiveSchemaDB()->getDefaults();
   if ( CmpCommon::getDefaultLong(MJ_BMO_QUOTA_PERCENT) != 0) 
   {
     // Apply quota system if either one the following two is true:
     //   1. the memory limit feature is turned off and more than one BMOs
     //   2. the memory limit feature is turned on
-    NADefaults &defs = ActiveSchemaDB()->getDefaults();
-    NABoolean mlimitPerCPU = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0;
+    NABoolean mlimitPerNode = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0;
   
-    if ( mlimitPerCPU || numBMOsInFrag > 1 ||
+    if ( mlimitPerNode || numBMOsInFrag > 1 ||
          (numBMOsInFrag == 1 && CmpCommon::getDefault(EXE_SINGLE_BMO_QUOTA) == DF_ON)) {
   
-      double bmoMemoryUsage = getEstimatedRunTimeMemoryUsage(TRUE, &numStreams).value();
       quotaMB = (UInt16)
           computeMemoryQuota(generator->getEspLevel() == 0,
-                             mlimitPerCPU,
-                             generator->getBMOsMemoryLimitPerCPU().value(),
-                             // generator->getTotalNumBMOsPerCPU(),
+                             mlimitPerNode,
+                             generator->getBMOsMemoryLimitPerNode().value(),
                              generator->getTotalNumBMOs(),
-                             generator->getTotalBMOsMemoryPerCPU().value(),
+                             generator->getTotalBMOsMemoryPerNode().value(),
                              numBMOsInFrag, 
                              bmoMemoryUsage,
-                             numStreams
+                             numStreams,
+                             memQuotaRatio
                              );
+    }
+    Lng32 mjMemoryLowbound = defs.getAsLong(EXE_MEMORY_LIMIT_LOWER_BOUND_MERGEJOIN);
+    Lng32 memoryUpperbound = defs.getAsLong(BMO_MEMORY_LIMIT_UPPER_BOUND);
 
-       Lng32 mjMemoryLowbound = defs.getAsLong(EXE_MEMORY_LIMIT_LOWER_BOUND_MERGEJOIN);
-       Lng32 memoryUpperbound = defs.getAsLong(BMO_MEMORY_LIMIT_UPPER_BOUND);
-
-       if ( quotaMB < mjMemoryLowbound )
-           quotaMB = (UInt16)mjMemoryLowbound;
-        else if (quotaMB >  memoryUpperbound)
-           quotaMB = memoryUpperbound;
+    if ( quotaMB < mjMemoryLowbound ) {
+       quotaMB = (UInt16)mjMemoryLowbound;
+       memQuotaRatio = BMOQuotaRatio::MIN_QUOTA;
     }
+    else if (quotaMB >  memoryUpperbound)
+      quotaMB = memoryUpperbound;
   } else {
-    Lng32 memoryMB = getExeMemoryAvailable(generator->getEspLevel() == 0);
-    quotaMB =  (UInt16)( (numBMOsInFrag > 1) ? (memoryMB/numBMOsInFrag) : 0 ) ;
+    Lng32 quotaMB = defs.getAsLong(EXE_MEMORY_LIMIT_LOWER_BOUND_MERGEJOIN);
   }
 
 
@@ -3166,7 +3164,7 @@ short MergeJoin::codeGen(Generator * generator)
   }
   mj_tdb->setOverflowMode(generator->getOverflowMode());
 
-  if(!generator->explainDisabled()) {
+  if (!generator->explainDisabled()) {
     generator->setExplainTuple(
          addExplainInfo(mj_tdb, leftExplainTuple, rightExplainTuple, generator));
   }

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/generator/GenRelMisc.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenRelMisc.cpp b/core/sql/generator/GenRelMisc.cpp
index 8a47d39..799d64b 100644
--- a/core/sql/generator/GenRelMisc.cpp
+++ b/core/sql/generator/GenRelMisc.cpp
@@ -1908,15 +1908,13 @@ short RelRoot::codeGen(Generator * generator)
   // fragments of the plan that are executed locally or are downloaded
   // to DP2 or to ESPs) from the generator's copy <compFragDir> and attach
   // it to the root_tdb
-  NABoolean fragmentQuotas = CmpCommon::getDefault(ESP_MULTI_FRAGMENT_QUOTAS) == DF_ON;
+  NABoolean fragmentQuotas = CmpCommon::getDefault(ESP_MULTI_FRAGMENTS) == DF_ON;
   ExFragDir *exFragDir =
 #pragma nowarn(1506)   // warning elimination
     new(space) ExFragDir(compFragDir->entries(),space,
                          CmpCommon::getDefault(ESP_MULTI_FRAGMENTS) == DF_ON, 
                          fragmentQuotas, 
                          (UInt16)CmpCommon::getDefaultLong(ESP_MULTI_FRAGMENT_QUOTA_VM),
-                         fragmentQuotas ?
-                           (UInt8)CmpCommon::getDefaultLong(ESP_NUM_FRAGMENTS_WITH_QUOTAS) :
                            (UInt8)CmpCommon::getDefaultLong(ESP_NUM_FRAGMENTS));
 #pragma warn(1506)  // warning elimination
 
@@ -2794,7 +2792,7 @@ short RelRoot::codeGen(Generator * generator)
     root_tdb->setHiveAccess(TRUE);
 
   root_tdb->setBmoMemoryLimitPerNode(ActiveSchemaDB()->getDefaults().getAsDouble(BMO_MEMORY_LIMIT_PER_NODE));
-  root_tdb->setEstBmoMemoryPerNode(generator->getTotalBMOsMemoryPerCPU().value());
+  root_tdb->setEstBmoMemoryPerNode(generator->getTotalBMOsMemoryPerNode().value());
 
   Int32 numSikEntries = securityKeySet_.entries();
   if (numSikEntries > 0)
@@ -2913,9 +2911,9 @@ short RelRoot::codeGen(Generator * generator)
 
       // now set the values of the previously allocated directory entry
 
-      NABoolean mlimitPerCPU = CmpCommon::getDefaultLong(BMO_MEMORY_LIMIT_PER_NODE) > 0;
+      NABoolean mlimitPerNode = CmpCommon::getDefaultLong(BMO_MEMORY_LIMIT_PER_NODE) > 0;
       UInt16 BMOsMemoryUsage = 0;
-      if (mlimitPerCPU == TRUE)
+      if (mlimitPerNode == TRUE)
         BMOsMemoryUsage = (UInt16)compFragDir->getBMOsMemoryUsage(i);
       else if (compFragDir->getNumBMOs(i) > 1 ||
                (compFragDir->getNumBMOs(i) == 1 && CmpCommon::getDefault(EXE_SINGLE_BMO_QUOTA) == DF_ON))
@@ -2956,6 +2954,8 @@ short RelRoot::codeGen(Generator * generator)
   compilerStatsInfo->bmo() = generator->getTotalNumBMOs();
   compilerStatsInfo->queryType() = (Int16)root_tdb->getQueryType();
   compilerStatsInfo->subqueryType() = (Int16)root_tdb->getSubqueryType();
+  compilerStatsInfo->bmoMemLimitPerNode() = root_tdb->getBmoMemoryLimitPerNode();
+  compilerStatsInfo->estBmoMemPerNode() = root_tdb->getEstBmoMemoryPerNode();
 
   NADELETEBASIC(partInputDataDescs, generator->wHeap());
   NADELETEBASIC(nodeMap, generator->wHeap());
@@ -3102,13 +3102,14 @@ short Sort::generateTdb(Generator * generator,
   sort_options->setConsiderBufferDefrag(considerBufferDefrag);
 
   short memoryQuotaMB = 0;
+  double memoryQuotaRatio;
+  Lng32 numStreams;
+  double bmoMemoryUsagePerNode = getEstimatedRunTimeMemoryUsage(TRUE, &numStreams).value();
 
-  if(CmpCommon::getDefault(SORT_MEMORY_QUOTA_SYSTEM) != DF_OFF)
+  if (CmpCommon::getDefault(SORT_MEMORY_QUOTA_SYSTEM) != DF_OFF)
   {
-
     // The CQD EXE_MEM_LIMIT_PER_BMO_IN_MB has precedence over the mem quota sys
     memoryQuotaMB = (UInt16)defs.getAsDouble(EXE_MEM_LIMIT_PER_BMO_IN_MB);
-    Lng32 numStreams;
 
     if (memoryQuotaMB > 0) {
      sort_options->memoryQuotaMB() = memoryQuotaMB;
@@ -3120,34 +3121,35 @@ short Sort::generateTdb(Generator * generator,
       //   1. the memory limit feature is turned off and more than one BMOs
       //   2. the memory limit feature is turned on
       
-      NABoolean mlimitPerCPU = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0;
+      NABoolean mlimitPerNode = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0;
   
-      if ( mlimitPerCPU || numBMOsInFrag > 1 ) {
+      if ( mlimitPerNode || numBMOsInFrag > 1 ||
+         (numBMOsInFrag == 1 && CmpCommon::getDefault(EXE_SINGLE_BMO_QUOTA) == DF_ON)) {
   
-          double bmoMemoryUsage = getEstimatedRunTimeMemoryUsage(TRUE, &numStreams).value();
           memoryQuotaMB = (short)
              computeMemoryQuota(generator->getEspLevel() == 0,
-                                mlimitPerCPU,
-                                generator->getBMOsMemoryLimitPerCPU().value(),
-                                //generator->getTotalNumBMOsPerCPU(),
+                                mlimitPerNode,
+                                generator->getBMOsMemoryLimitPerNode().value(),
                                 generator->getTotalNumBMOs(),
-                                generator->getTotalBMOsMemoryPerCPU().value(),
+                                generator->getTotalBMOsMemoryPerNode().value(),
                                 numBMOsInFrag, 
-                                bmoMemoryUsage,
-                                numStreams
+                                bmoMemoryUsagePerNode,
+                                numStreams,
+                                memoryQuotaRatio
                                );
   
-                  
-          Lng32 sortMemoryLowbound = defs.getAsLong(BMO_MEMORY_LIMIT_LOWER_BOUND_SORT);
-          Lng32 memoryUpperbound = defs.getAsLong(BMO_MEMORY_LIMIT_UPPER_BOUND);
+      }            
+      Lng32 sortMemoryLowbound = defs.getAsLong(BMO_MEMORY_LIMIT_LOWER_BOUND_SORT);
+      Lng32 memoryUpperbound = defs.getAsLong(BMO_MEMORY_LIMIT_UPPER_BOUND);
   
-          if ( memoryQuotaMB < sortMemoryLowbound )
-             memoryQuotaMB = (short)sortMemoryLowbound;
-          else if (memoryQuotaMB >  memoryUpperbound)
-             memoryQuotaMB = memoryUpperbound;
+      if ( memoryQuotaMB < sortMemoryLowbound ) {
+         memoryQuotaMB = (short)sortMemoryLowbound;
+         memoryQuotaRatio = BMOQuotaRatio::MIN_QUOTA;
       }
+      else if (memoryQuotaMB >  memoryUpperbound)
+         memoryQuotaMB = memoryUpperbound;
     }
-   }
+  }
 
    //BMO settings. By Default set this value to max available 
    //irrespective of quota is enabled or disabled. Sort at run time
@@ -3203,6 +3205,7 @@ short Sort::generateTdb(Generator * generator,
   sort_tdb->setSortFromTop(sortFromTop());
   sort_tdb->setOverflowMode(generator->getOverflowMode());
   sort_tdb->setTopNSortEnabled(CmpCommon::getDefault(GEN_SORT_TOPN) == DF_ON);
+  sort_tdb->setBmoQuotaRatio(memoryQuotaRatio);
   
   if (generator->getUserSidetreeInsert())
     sort_tdb->setUserSidetreeInsert(TRUE);
@@ -3215,6 +3218,7 @@ short Sort::generateTdb(Generator * generator,
   generator->initTdbFields(sort_tdb);
 
   double sortMemEst = getEstimatedRunTimeMemoryUsage(sort_tdb);
+  sort_tdb->setEstimatedMemoryUsage(sortMemEst / 1024);
   generator->addToTotalEstimatedMemory(sortMemEst);
 
   generator->addToTotalOverflowMemory(
@@ -3230,21 +3234,12 @@ short Sort::generateTdb(Generator * generator,
   float bmoCtzFactor;
   defs.getFloat(BMO_CITIZENSHIP_FACTOR, bmoCtzFactor);
   sort_tdb->setBmoCitizenshipFactor((Float32)bmoCtzFactor);
-
- //if(!generator->explainDisabled()) {
-  Lng32 sortMemEstInKBPerCPU = (Lng32)(sortMemEst / 1024) ;
-  sortMemEstInKBPerCPU = sortMemEstInKBPerCPU/
-    (MAXOF(generator->compilerStatsInfo().dop(),1));
-  sort_tdb->setSortMemEstInMbPerCpu
-    ( Float32(MAXOF(sortMemEstInKBPerCPU/1024,1)) );
-
-  if(!generator->explainDisabled()) {
-    generator->setOperEstimatedMemory(sortMemEstInKBPerCPU );
-
+  sort_tdb->setSortMemEstInKBPerNode(bmoMemoryUsagePerNode /1024);
+  if (sortNRows())
+     sort_tdb->setTopNThreshold(defs.getAsLong(GEN_SORT_TOPN_THRESHOLD));
+  if (!generator->explainDisabled()) {
     generator->setExplainTuple(
        addExplainInfo(sort_tdb, childExplainTuple, 0, generator));
-
-    generator->setOperEstimatedMemory(0);
   }
 
   // set the new up cri desc.
@@ -3936,7 +3931,13 @@ CostScalar Sort::getEstimatedRunTimeMemoryUsage(NABoolean perNode, Lng32 *numStr
   GroupAttributes * childGroupAttr = child(0).getGroupAttr();
   Lng32 childRecordSize = 
       childGroupAttr->getCharacteristicOutputs().getRowLength();
-  CostScalar totalMemory = getEstRowsUsed() * childRecordSize;
+  CostScalar rowsUsed;
+  if (sortNRows() && (topNRows_ > 0)
+            && (topNRows_ <= getDefault(GEN_SORT_TOPN_THRESHOLD)))
+     rowsUsed = topNRows_; 
+  else
+     rowsUsed = getEstRowsUsed();
+  CostScalar totalMemory = rowsUsed * childRecordSize;
 
   Lng32 numOfStreams = 1;
   const PhysicalProperty* const phyProp = getPhysicalProperty();

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/generator/GenRelSequence.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenRelSequence.cpp b/core/sql/generator/GenRelSequence.cpp
index 4522344..b00aa95 100644
--- a/core/sql/generator/GenRelSequence.cpp
+++ b/core/sql/generator/GenRelSequence.cpp
@@ -781,7 +781,7 @@ RelExpr * PhysSequence::preCodeGen(Generator * generator,
       generator->incrBMOsMemory(getEstimatedRunTimeMemoryUsage(TRUE));
   }
   else
-    generator->incrNBMOsMemoryPerCPU(getEstimatedRunTimeMemoryUsage(TRUE));
+    generator->incrNBMOsMemoryPerNode(getEstimatedRunTimeMemoryUsage(TRUE));
 
   markAsPreCodeGenned();
 
@@ -1088,18 +1088,11 @@ PhysSequence::codeGen(Generator *generator)
   generator->addToTotalEstimatedMemory(sequenceMemEst);
 
   if(!generator->explainDisabled()) {
-    Lng32 seqMemEstInKBPerCPU = (Lng32)(sequenceMemEst / 1024) ;
-    seqMemEstInKBPerCPU = seqMemEstInKBPerCPU/
-      (MAXOF(generator->compilerStatsInfo().dop(),1));
-    generator->setOperEstimatedMemory(seqMemEstInKBPerCPU);
-
     generator->
       setExplainTuple(addExplainInfo(sequenceTdb,
                                      childExplainTuple,
                                      0,
                                      generator));
-
-    generator->setOperEstimatedMemory(0);
   }
 
   sequenceTdb->setScratchIOVectorSize((Int16)getDefault(SCRATCH_IO_VECTOR_SIZE_HASH));
@@ -1121,31 +1114,41 @@ PhysSequence::codeGen(Generator *generator)
   UInt16 mmu = (UInt16)(defs.getAsDouble(EXE_MEM_LIMIT_PER_BMO_IN_MB));
   UInt16 numBMOsInFrag = (UInt16)generator->getFragmentDir()->getNumBMOs();
   Lng32 numStreams;
+  double bmoMemoryUsagePerNode = getEstimatedRunTimeMemoryUsage(TRUE, &numStreams).value();
+  double memQuota = 0;
+  double memQuotaRatio;
   if (mmu != 0)
     sequenceTdb->setMemoryQuotaMB(mmu);
   else {
     // Apply quota system if either one the following two is true:
     //   1. the memory limit feature is turned off and more than one BMOs 
     //   2. the memory limit feature is turned on
-    NABoolean mlimitPerCPU = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0;
-
-    if ( mlimitPerCPU || numBMOsInFrag > 1 ) {
+    NABoolean mlimitPerNode = defs.getAsDouble(BMO_MEMORY_LIMIT_PER_NODE) > 0;
 
-        double bmoMemoryUsage = getEstimatedRunTimeMemoryUsage(TRUE, &numStreams).value();
+    if ( mlimitPerNode || numBMOsInFrag > 1 ) {
         double memQuota = 
            computeMemoryQuota(generator->getEspLevel() == 0,
-                              mlimitPerCPU,
-                              generator->getBMOsMemoryLimitPerCPU().value(),
-                              //generator->getTotalNumBMOsPerCPU(),
+                              mlimitPerNode,
+                              generator->getBMOsMemoryLimitPerNode().value(),
                               generator->getTotalNumBMOs(),
-                              generator->getTotalBMOsMemoryPerCPU().value(),
+                              generator->getTotalBMOsMemoryPerNode().value(),
                               numBMOsInFrag, 
-                              bmoMemoryUsage,
-                              numStreams
+                              bmoMemoryUsagePerNode,
+                              numStreams,
+                              memQuotaRatio
                              );
-                                  
-        sequenceTdb->setMemoryQuotaMB( UInt16(memQuota) );
     }
+    Lng32 seqMemoryLowbound = defs.getAsLong(EXE_MEMORY_LIMIT_LOWER_BOUND_SEQUENCE);
+    Lng32 memoryUpperbound = defs.getAsLong(BMO_MEMORY_LIMIT_UPPER_BOUND);
+
+    if ( memQuota < seqMemoryLowbound ) {
+       memQuota = seqMemoryLowbound;
+       memQuotaRatio = BMOQuotaRatio::MIN_QUOTA;
+    }
+    else if (memQuota >  memoryUpperbound)
+       memQuota = memoryUpperbound;
+           
+    sequenceTdb->setMemoryQuotaMB( UInt16(memQuota) );
   }
 
   generator->setCriDesc(givenCriDesc, Generator::DOWN);

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/generator/Generator.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/Generator.cpp b/core/sql/generator/Generator.cpp
index 8e72b60..3f061b8 100644
--- a/core/sql/generator/Generator.cpp
+++ b/core/sql/generator/Generator.cpp
@@ -163,23 +163,23 @@ Generator::Generator(CmpContext* currentCmpContext) :
   tempSpace_ = NULL;
 
   numBMOs_ = 0;
-  totalNumBMOsPerCPU_ = 0;
+  totalNumBMOsPerNode_ = 0;
 
   BMOsMemoryPerFrag_ = 0;
-  totalBMOsMemoryPerCPU_ = 0;
+  totalBMOsMemoryPerNode_ = 0;
 
-  nBMOsMemoryPerCPU_ = 0;
+  nBMOsMemoryPerNode_ = 0;
 
-  BMOsMemoryLimitPerCPU_ = 0;
+  BMOsMemoryLimitPerNode_ = 0;
 
-  totalNumBMOsPerCPU_ = 0;
+  totalNumBMOsPerNode_ = 0;
 
   BMOsMemoryPerFrag_ = 0;
-  totalBMOsMemoryPerCPU_ = 0;
+  totalBMOsMemoryPerNode_ = 0;
 
-  nBMOsMemoryPerCPU_ = 0;
+  nBMOsMemoryPerNode_ = 0;
 
-  BMOsMemoryLimitPerCPU_ = 0;
+  BMOsMemoryLimitPerNode_ = 0;
   
   totalNumBMOs_ = 0;
 
@@ -288,6 +288,7 @@ Generator::Generator(CmpContext* currentCmpContext) :
   //
   computeStats_ = FALSE;
   explainInRms_ = TRUE;
+  topNRows_ = 0;
 }
 
 void Generator::initTdbFields(ComTdb *tdb)
@@ -1041,16 +1042,6 @@ Generator::remapESPAllocationAS()
       
     NABoolean cycleSegs = (layersInCycle > 0);
 
-    // Use CQD ESP_NUM_FRAGMENTS_WITH_QUOTAS when the multi-ESP is on. That is
-    // we will shift the layers within a SQ node subset <n> times before we
-    // advance to next SQ node subset. Here <n> is the value of the cqd
-    // ESP_NUM_FRAGMENTS_WITH_QUOTAS. When the layer (or fragment) contains BMOs, then, 
-    // the layer is counted twice.
-    // 
-    if ( CmpCommon::getDefault(ESP_MULTI_FRAGMENT_QUOTAS) ==  DF_ON ) 
-      layersInCycle = 
-          (ActiveSchemaDB()->getDefaults()).getAsLong(ESP_NUM_FRAGMENTS_WITH_QUOTAS);
-      
     // if shiftESPs TRUE, then shift node map within each segment.
     //
     NABoolean shiftESPs =

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/generator/Generator.h
----------------------------------------------------------------------
diff --git a/core/sql/generator/Generator.h b/core/sql/generator/Generator.h
index 6764ac5..737feac 100644
--- a/core/sql/generator/Generator.h
+++ b/core/sql/generator/Generator.h
@@ -426,15 +426,15 @@ class Generator : public NABasicObject
 
   // temporary value holder (during pre code gen) for #BMOs in this fragment
   unsigned short numBMOs_;
-  unsigned short totalNumBMOsPerCPU_; // accumulated # of BMO, per CPU
+  unsigned short totalNumBMOsPerNode_; // accumulated # of BMO, per Node
 
   CostScalar BMOsMemoryPerFrag_; // accumulated BMO memory, per fragment 
-  CostScalar totalBMOsMemoryPerCPU_; // accumulated BMO memory, per CPU
+  CostScalar totalBMOsMemoryPerNode_; // accumulated BMO memory, per Node
 
-  CostScalar nBMOsMemoryPerCPU_; // accumulated nBMO memory, per CPU
+  CostScalar nBMOsMemoryPerNode_; // accumulated nBMO memory, per Node
 
-  // BMO memory limit per CPU
-  CostScalar BMOsMemoryLimitPerCPU_; 
+  // BMO memory limit per Node
+  CostScalar BMOsMemoryLimitPerNode_; 
 
   // Total number of BMOs in the query
   unsigned short totalNumBMOs_;
@@ -561,7 +561,7 @@ private:
 
   // estimated memory for an individual operator. Used by Explain
   // set to 0 after Explain has been called so that next operator
-  // can used this field. In KB and on a per CPU basis.
+  // can used this field. In KB and on a per Node basis.
   Lng32 operEstimatedMemory_ ;
 
   Int16 maxCpuUsage_ ;
@@ -604,6 +604,8 @@ private:
   char NExLogPathNam_[1024] ; // Only 1 needed, so we put it in Generator object
 
   LIST(CifAvgVarCharSizeCache) avgVarCharSizeList_;
+
+  UInt32 topNRows_;
   //LIST(double) avgVarCharSizeValList_;
   void addCifAvgVarCharSizeToCache( ValueId vid, double size)
   {
@@ -1462,7 +1464,7 @@ public:
   inline CostScalar getBMOsMemory() { return BMOsMemoryPerFrag_; }
 
   inline void incrBMOsMemory(CostScalar x) 
-     { incrBMOsMemoryPerFrag(x); totalBMOsMemoryPerCPU_ += x; }
+     { incrBMOsMemoryPerFrag(x); totalBMOsMemoryPerNode_ += x; }
 
   inline void incrBMOsMemoryPerFrag(CostScalar x) 
      { BMOsMemoryPerFrag_ += x;  }
@@ -1473,11 +1475,11 @@ public:
     BMOsMemoryPerFrag_ = newVal;
     return retVal;
   }
-  inline CostScalar getTotalBMOsMemoryPerCPU() 
-                 { return totalBMOsMemoryPerCPU_; }
+  inline CostScalar getTotalBMOsMemoryPerNode() 
+                 { return totalBMOsMemoryPerNode_; }
 
   inline void incrNumBMOs() 
-     {  incrNumBMOsPerFrag(1);  totalNumBMOsPerCPU_++; totalNumBMOs_++;}
+     {  incrNumBMOsPerFrag(1);  totalNumBMOsPerNode_++; totalNumBMOs_++;}
 
   inline void incrNumBMOsPerFrag(UInt32 x) { numBMOs_ += x; }
 
@@ -1487,15 +1489,15 @@ public:
     numBMOs_ = newVal;
     return retVal;
   }
-  inline unsigned short getTotalNumBMOsPerCPU() { return totalNumBMOsPerCPU_; }
+  inline unsigned short getTotalNumBMOsPerNode() { return totalNumBMOsPerNode_; }
   
-  inline CostScalar getTotalNBMOsMemoryPerCPU() { return nBMOsMemoryPerCPU_; }
-  inline void incrNBMOsMemoryPerCPU(CostScalar x) { nBMOsMemoryPerCPU_ += x; }
+  inline CostScalar getTotalNBMOsMemoryPerNode() { return nBMOsMemoryPerNode_; }
+  inline void incrNBMOsMemoryPerNode(CostScalar x) { nBMOsMemoryPerNode_ += x; }
 
-  inline void setBMOsMemoryLimitPerCPU(CostScalar x) 
-            { BMOsMemoryLimitPerCPU_ = x; }
+  inline void setBMOsMemoryLimitPerNode(CostScalar x) 
+            { BMOsMemoryLimitPerNode_ = x; }
 
-  inline CostScalar getBMOsMemoryLimitPerCPU() { return BMOsMemoryLimitPerCPU_; }
+  inline CostScalar getBMOsMemoryLimitPerNode() { return BMOsMemoryLimitPerNode_; }
 
   inline unsigned short getTotalNumBMOs() { return totalNumBMOs_; }
 
@@ -1608,9 +1610,6 @@ public:
   inline void addToTotalOverflowMemory(double val)
         {totalOverflowMemory_ += val;}
 
-  inline Lng32 getOperEstimatedMemory(){return operEstimatedMemory_;}
-  inline void setOperEstimatedMemory(Lng32 val){operEstimatedMemory_ = val;}
-
   inline ComTdb::OverflowModeType getOverflowMode() {return overflowMode_; }
 
   // Each of these two mutators return the old value
@@ -1691,6 +1690,10 @@ public:
     }
     return snapshotScanTmpLocation_;
   }
+  inline void setTopNRows(ULng32 topNRows) 
+     { topNRows_ = topNRows; }
+  inline ULng32 getTopNRows() { return topNRows_; }
+        
 }; // class Generator
 #pragma warn(1506)   // warning elimination
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f538c2f9/core/sql/optimizer/RelEnforcer.h
----------------------------------------------------------------------
diff --git a/core/sql/optimizer/RelEnforcer.h b/core/sql/optimizer/RelEnforcer.h
index 8fc705c..e6ebc12 100644
--- a/core/sql/optimizer/RelEnforcer.h
+++ b/core/sql/optimizer/RelEnforcer.h
@@ -107,6 +107,7 @@ public:
 	      CollHeap *oHeap = CmpCommon::statementHeap()) :
        RelExpr(REL_SORT,child,NULL,oHeap),
        sortNRows_(FALSE), 
+       topNRows_(0),
        collectNFErrors_(FALSE),
        forcedHalloweenProtection_(FALSE),
        checkAccessToSelfRefTable_(FALSE)
@@ -117,6 +118,7 @@ public:
        RelExpr(REL_SORT,child, NULL, oHeap),
        sortKey_(sortKey),
        sortNRows_(FALSE), 
+       topNRows_(0),
        collectNFErrors_(FALSE),
        forcedHalloweenProtection_(FALSE),
        checkAccessToSelfRefTable_(FALSE)
@@ -126,6 +128,7 @@ public:
        RelExpr(REL_SORT,child),
        arrangedCols_(arrangedCols),
        sortNRows_(FALSE), 
+       topNRows_(0),
     forcedHalloweenProtection_(FALSE),
     checkAccessToSelfRefTable_(FALSE),
     collectNFErrors_(FALSE)
@@ -234,6 +237,8 @@ protected:
 
   NABoolean collectNFErrors_;
 
+  ULng32 topNRows_;
+
   short generateTdb(Generator * generator,
                     ComTdb * child_tdb,
                     ex_expr * sortKeyExpr,



Mime
View raw message