You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafodion.apache.org by sa...@apache.org on 2016/10/11 21:02:58 UTC
[1/7] incubator-trafodion git commit: [TRAFODION-2259] TopN sort
changes. This includes first of changes related to sort implementation in
executor. cqd gen_sort_topn_size 'N' forces sort to use topn. Subsequent
changes in compiler will be able to push d
Repository: incubator-trafodion
Updated Branches:
refs/heads/master a9f34a6a1 -> 75048b0f3
[TRAFODION-2259] TopN sort changes.
This includes first of changes related to sort implementation in executor.
cqd gen_sort_topn_size 'N' forces sort to use topn.
Subsequent changes in compiler will be able to push down topn to sort.
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/61cc7b5b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/61cc7b5b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/61cc7b5b
Branch: refs/heads/master
Commit: 61cc7b5b26d760839e6515b3e7cc94d8ed5ed879
Parents: b8dc6da
Author: Prashant Vasudev <pr...@esgyn.com>
Authored: Tue Oct 4 05:45:43 2016 +0000
Committer: Prashant Vasudev <pr...@esgyn.com>
Committed: Tue Oct 4 05:45:43 2016 +0000
----------------------------------------------------------------------
core/sql/comexe/ComTdbSort.h | 9 +-
core/sql/executor/ex_sort.cpp | 1 +
core/sql/generator/GenRelMisc.cpp | 3 +
core/sql/nskgmake/sort/Makefile | 3 +-
core/sql/sort/Qsort.h | 5 +-
core/sql/sort/Record.cpp | 14 +-
core/sql/sort/Record.h | 8 +
core/sql/sort/SortUtil.cpp | 22 +-
core/sql/sort/SortUtilCfg.cpp | 42 ----
core/sql/sort/SortUtilCfg.h | 13 +-
core/sql/sort/Topn.cpp | 337 +++++++++++++++++++++++++++++++
core/sql/sort/Topn.h | 89 ++++++++
core/sql/sqlcomp/DefaultConstants.h | 1 +
core/sql/sqlcomp/nadefaults.cpp | 1 +
14 files changed, 490 insertions(+), 58 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/comexe/ComTdbSort.h
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbSort.h b/core/sql/comexe/ComTdbSort.h
index 5ddc950..93a1a81 100644
--- a/core/sql/comexe/ComTdbSort.h
+++ b/core/sql/comexe/ComTdbSort.h
@@ -204,8 +204,9 @@ protected:
Float32 bmoCitizenshipFactor_; // 68-71
Int32 pMemoryContingencyMB_; // 72-75
UInt16 sortGrowthPercent_; // 76-77
-
- char fillersComTdbSort_[18]; // 78-95
+ char filler2_[2]; // 78-79
+ ULng32 topNSize_; // 80-83
+ char fillersComTdbSort_[12]; // 84-95
public:
@@ -326,6 +327,10 @@ public:
{ pMemoryContingencyMB_ = mCMB;}
Int32 getMemoryContingencyMB(void)
{ return pMemoryContingencyMB_; }
+ void setTopNSize(UInt32 size)
+ { topNSize_ = size; }
+ ULng32 getTopNSize(void)
+ { return topNSize_; }
void setSortMemEstInMbPerCpu(Float32 s) {sortMemEstInMbPerCpu_=s;}
Float32 getSortMemEstInMbPerCpu() {return sortMemEstInMbPerCpu_;}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/executor/ex_sort.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_sort.cpp b/core/sql/executor/ex_sort.cpp
index c200737..e655abd 100644
--- a/core/sql/executor/ex_sort.cpp
+++ b/core/sql/executor/ex_sort.cpp
@@ -242,6 +242,7 @@ ExSortTcb::ExSortTcb(const ExSortTdb & sort_tdb,
sortCfg_->setIntermediateScratchCleanup(st->sortOptions_->intermediateScratchCleanup());
sortCfg_->setResizeCifRecord(st->sortOptions_->resizeCifRecord());
sortCfg_->setConsiderBufferDefrag(st->sortOptions_->considerBufferDefrag());
+ sortCfg_->setTopNSize(st->getTopNSize());
switch(st->getOverFlowMode())
{
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/generator/GenRelMisc.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenRelMisc.cpp b/core/sql/generator/GenRelMisc.cpp
index d37a8e6..a12186f 100644
--- a/core/sql/generator/GenRelMisc.cpp
+++ b/core/sql/generator/GenRelMisc.cpp
@@ -3224,6 +3224,9 @@ short Sort::generateTdb(Generator * generator,
sort_tdb->setSortFromTop(sortFromTop());
sort_tdb->setOverflowMode(generator->getOverflowMode());
+
+ sort_tdb->setTopNSize((ULng32)getDefault(GEN_SORT_TOPN_SIZE));
+
if (generator->getUserSidetreeInsert())
sort_tdb->setUserSidetreeInsert(TRUE);
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/nskgmake/sort/Makefile
----------------------------------------------------------------------
diff --git a/core/sql/nskgmake/sort/Makefile b/core/sql/nskgmake/sort/Makefile
index 9aa1504..b0b7401 100755
--- a/core/sql/nskgmake/sort/Makefile
+++ b/core/sql/nskgmake/sort/Makefile
@@ -36,7 +36,8 @@ CPPSRC := CommonUtil.cpp \
SortUtilCfg.cpp \
Statistics.cpp \
TourTree.cpp \
- TreeNode.cpp
+ TreeNode.cpp \
+ Topn.cpp
CPPSRC += vers_libsort.cpp
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/sort/Qsort.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/Qsort.h b/core/sql/sort/Qsort.h
index 00aeb3f..af1c7e6 100644
--- a/core/sql/sort/Qsort.h
+++ b/core/sql/sort/Qsort.h
@@ -112,10 +112,7 @@ class ExBMOStats;
// to be used for quicksort.
//----------------------------------------------------------------------
-struct RecKeyBuffer {
- char* key_;
- Record* rec_;
-};
+
void heapSort(RecKeyBuffer keysToSort[], Int32 runsize);
void siftDown(RecKeyBuffer keysToSort[], Int32 root, Int32 bottom);
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/sort/Record.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/Record.cpp b/core/sql/sort/Record.cpp
index 4184d69..2437575 100644
--- a/core/sql/sort/Record.cpp
+++ b/core/sql/sort/Record.cpp
@@ -55,11 +55,23 @@ Record::Record(ULng32 size, NABoolean doNotallocRec, CollHeap* heap)
}
}
+Record::Record(void *rec, ULng32 reclen, void* tupp, CollHeap* heap, SortError* sorterror)
+{
+ recSize_ = reclen;
+ sortError_ = sorterror;
+ heap_ = heap;
+ tupp_ = tupp;
+ allocatedRec_ = FALSE_L;
+ rec_ =(char *) rec;
+}
+
+
//----------------------------------------------------------------------
// Record Destructor.
//----------------------------------------------------------------------
Record::~Record(void)
-{
+{
+
if (allocatedRec_ && rec_ != NULL) {
NADELETEBASIC(rec_, heap_);
rec_ = NULL;
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/sort/Record.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/Record.h b/core/sql/sort/Record.h
index 4b66f63..8928d45 100644
--- a/core/sql/sort/Record.h
+++ b/core/sql/sort/Record.h
@@ -47,11 +47,19 @@
#include "NABasicObject.h"
#include "SortError.h"
+class Record;
+
+struct RecKeyBuffer {
+ char* key_;
+ Record* rec_;
+ };
+
class Record {
public :
Record();
Record(ULng32 size, NABoolean doNotallocRec, CollHeap* heap);
+ Record(void *rec, ULng32 reclen, void* tupp, CollHeap* heap, SortError* sorterror);
~Record(void);
void initialize(ULng32 recsize, NABoolean doNotallocRec,
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/sort/SortUtil.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtil.cpp b/core/sql/sort/SortUtil.cpp
index 64fb793..433e815 100644
--- a/core/sql/sort/SortUtil.cpp
+++ b/core/sql/sort/SortUtil.cpp
@@ -45,6 +45,7 @@
#include "ex_sort.h"
#include "SortUtil.h"
#include "Qsort.h"
+#include "Topn.h"
#include "ComCextdecs.h"
#include "logmxevent.h"
#include "ExStats.h"
@@ -158,7 +159,10 @@ NABoolean SortUtil::sortInitialize(SortUtilConfig& config)
//---------------------------------------------------------------
doCleanUp();
- sortAlgo_ =
+ //if topNSize_ is set, then use TopN.
+ if(!config.topNSize_)
+ {
+ sortAlgo_ =
new (config.heapAddr_) Qsort(config.runSize_,
config.maxMem_,
config.recSize_,
@@ -170,6 +174,22 @@ NABoolean SortUtil::sortInitialize(SortUtilConfig& config)
&sortError_,
explainNodeId_,
this);
+ }
+ else
+ {
+ sortAlgo_ =
+ new (config.heapAddr_) TopN(config.topNSize_,
+ config.maxMem_,
+ config.recSize_,
+ config.sortType_.doNotAllocRec_,
+ config.keySize_,
+ scratch_,
+ TRUE,
+ config.heapAddr_,
+ &sortError_,
+ explainNodeId_,
+ this);
+ }
if (sortAlgo_ == NULL)
{
sortError_.setErrorInfo( EScrNoMemory //sort error
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/sort/SortUtilCfg.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtilCfg.cpp b/core/sql/sort/SortUtilCfg.cpp
index 81ed8ab..25bca77 100644
--- a/core/sql/sort/SortUtilCfg.cpp
+++ b/core/sql/sort/SortUtilCfg.cpp
@@ -225,48 +225,6 @@ NABoolean SortUtilConfig::setKeyInfo(ULng32 keysize)
return SORT_SUCCESS;
}
-
-
-//----------------------------------------------------------------------
-// Name : setTemps
-//
-// Parameters : ...
-//
-// Description :
-//
-// Return Value :
-// SORT_SUCCESS if everything goes on well.
-// SORT_FAILURE if any error encounterd.
-//
-//----------------------------------------------------------------------
-NABoolean SortUtilConfig::setTemps(ULng32 runsize=100L,
- ULng32 mergeorder=10L)
-{
- runSize_ = runsize;
- mergeOrder_ = mergeorder;
-
- return SORT_SUCCESS;
-}
-
-
-//----------------------------------------------------------------------
-// Name : getTemps
-//
-// Parameters : ...
-//
-// Description :
-//
-// Return Value :
-// SORT_SUCCESS if everything goes on well.
-// SORT_FAILURE if any error encounterd.
-//
-//----------------------------------------------------------------------
-NABoolean SortUtilConfig::getTemps() const
-{
- return SORT_SUCCESS;
-}
-
-
void SortUtilConfig::setUseBuffered(NABoolean torf)
{
useBufferedWrites_ = torf;
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/sort/SortUtilCfg.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtilCfg.h b/core/sql/sort/SortUtilCfg.h
index 3a0e946..be7ad9a 100644
--- a/core/sql/sort/SortUtilCfg.h
+++ b/core/sql/sort/SortUtilCfg.h
@@ -87,11 +87,7 @@ public:
ULng32 getRecSize() const;
NABoolean setKeyInfo(ULng32 keysize);
-
-
- NABoolean setTemps(ULng32 runsize, ULng32 mergesize);
- NABoolean getTemps() const;
-
+
void setUseBuffered(NABoolean torf);
NABoolean getUseBuffered() ;
@@ -152,7 +148,10 @@ public:
{
return numEsps_;
}
-
+ void setTopNSize(ULng32 size)
+ {
+ topNSize_ = size;
+ }
void setEventHandler(ExSubtask *eh)
{
ioEventHandler_ = eh;
@@ -319,7 +318,7 @@ private:
ULng32 mergeOrder_; // Need to modify this to do automatically.
ULng32 minMem_; // Minimum sort heap memory
ULng32 maxMem_; // Maximum sort heap memory
- ULng32 initialRunSize_; // unused :to be set by the executor
+ ULng32 topNSize_; // TopN size set by the executor
ULng32 runSizeIncr_; // unused :how much to increment the run size by.
ULng32 maxNumBuffers_; // Max buffer space as set by the compiler
unsigned short scratchThreshold_; // percent of disk usage after which a disk will be discarded for use
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/sort/Topn.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/Topn.cpp b/core/sql/sort/Topn.cpp
new file mode 100644
index 0000000..417a795
--- /dev/null
+++ b/core/sql/sort/Topn.cpp
@@ -0,0 +1,337 @@
+/**********************************************************************
+// @@@ START COPYRIGHT @@@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// @@@ END COPYRIGHT @@@
+**********************************************************************/
+/* -*-C++-*-
+******************************************************************************
+*
+* File: TopN.cpp
+*
+* Description: This file contains the implementation of all member functions
+* of the class TopN.
+*
+* 1. Sort would initially maintain Top N array of elements to being with.
+* 2. Read records into TopN array.
+* 3. Once TopN array is full, heapify the array into max heap. Top node in the heap is always the highest node.
+* 4. Subsequent record read either gets discarded( if greater than top node) or replace top node( if lesser then top node) . if replaced top node, re-balance the heap.
+* 5. Repeat steps 4 until last record is read.
+* 6. sort the final heap using heap sort.
+*******************************************************************************/
+
+#include <iostream>
+#include <fstream>
+
+#ifndef DEBUG
+#undef NDEBUG
+#define NDEBUG
+#endif
+#include "ex_stdh.h"
+#include "Topn.h"
+#include "ScratchSpace.h"
+#include "logmxevent.h"
+#include "SortUtil.h"
+#include "ex_ex.h"
+#include "ExStats.h"
+
+//------------------------------------------------------------------------
+// Class Constructor.
+//------------------------------------------------------------------------
+TopN::TopN(ULng32 runsize, ULng32 sortmaxmem, ULng32 recsize,
+ NABoolean doNotallocRec, ULng32 keysize,
+ SortScratchSpace* scratch, NABoolean iterSort,
+ CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil):
+ SortAlgo(runsize, recsize, doNotallocRec, keysize, scratch, explainNodeId),
+ loopIndex_(0), heap_(heap), sortError_(sorterror),
+ sortUtil_(sortutil)
+{
+ //runsize is TopN size. Fixed.
+ allocRunSize_ = runsize;
+
+ //Actual run size after all elements read.
+ runSize_ = 0;
+
+ isHeapified_ = FALSE;
+
+ //allocateMemory failureIsFatal is defaulted to TRUE means allocation failure results in
+ //longjump to jump handler defined in ex_sort.cpp. Only applicable on NSK.
+ topNKeys_ = (RecKeyBuffer *)heap_->allocateMemory(sizeof(RecKeyBuffer) * allocRunSize_);
+
+ // Below asserts useful in debug mode. Also asserts if longjmp did not happen.
+ //ex_assert(rootRecord_!= NULL, "Sort: Initial rootRecord_ allocation failed");
+ ex_assert(topNKeys_ != NULL, "Sort: Initial topNKeys_ allocation failed");
+
+ recNum_ = 0;
+ ExOperStats *stat = sortUtil_->config()->getCallingTcb()->getStatsEntry();
+ if (stat)
+ bmoStats_ = stat->castToExBMOStats();
+ else
+ bmoStats_ = NULL;
+ if (bmoStats_)
+ bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
+}
+
+
+//------------------------------------------------------------------------
+// Class Destructor: Delete all the heap space pointed by pointers in Qsort
+//------------------------------------------------------------------------
+TopN::~TopN(void)
+{
+
+ for (int i = 0; i < runSize_; i++)
+ topNKeys_[i].rec_->releaseTupp();
+
+
+ if (topNKeys_ != NULL) {
+ NADELETEBASIC(topNKeys_, heap_);
+ topNKeys_ = NULL;
+ }
+ if (bmoStats_)
+ bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
+
+}
+
+Lng32 TopN::sortSend(void *rec, ULng32 len, void* tupp)
+{
+ //if heap not built means, TopN array has more slots
+ //available to fill.
+ if(! isHeapified_)
+ {
+ ex_assert(loopIndex_ >= 0, "TopN::sortSend: loopIndex_ is < 0");
+ ex_assert(loopIndex_ < allocRunSize_, "TopN::sortSend: loopIndex_ > allocRunSize_");
+ ex_assert(rec != NULL, "TopN::sortSend: rec is NULL");
+
+ Record * insertRec = new Record(rec, len, tupp, heap_, sortError_);
+
+ topNKeys_[loopIndex_].key_ = insertRec->extractKey(keySize_, sortUtil_->config()->numberOfBytesForRecordSize());
+ topNKeys_[loopIndex_].rec_ = insertRec;
+ if (++loopIndex_ == allocRunSize_)
+ {
+ //Reaching here means, we have filled up the array.
+ //Now heapify the array to start accepting/eliminating new elements from now on.
+
+ //Note lookIndex_ contains the current number of filled elements.
+
+ buildHeap();
+ }
+ return SORT_SUCCESS;
+ }
+
+ //Reaching here means, heap is already build.
+ //Check if the new rec belongs to this heap by comparing the
+ //new rec key with the root node of the heap ( root node is always the greatest).
+ insertRec(rec, len, tupp);
+ return SORT_SUCCESS;
+ }
+
+
+void TopN::buildHeap()
+{
+ if(!isHeapified_)
+ {
+ //loopIndex_ is now <= TopN
+ runSize_ = loopIndex_;
+
+ satisfyHeap();
+
+ isHeapified_ = TRUE;
+ }
+}
+
+//Satisfy Heap makes sure the heap is balanced maxHeap.
+//Note this does not mean heap is sorted. It just makes sure
+//the higher level nodes are greater than lower level nodes.
+//Topmost node or root will be the highest.
+void TopN::satisfyHeap()
+{
+ for (int i = (runSize_/2 ); i >= 0; i--)
+ siftDown(topNKeys_, i, runSize_-1);
+}
+
+
+void TopN::insertRec(void *rec, ULng32 len, void* tupp)
+{
+ ex_assert(isHeapified_, "TopN::insertRec: not heapified");
+
+ int root = 0; //Always, root node is the zero'th element in array.
+
+ Record * insertRec = new Record(rec, len, tupp, heap_, sortError_);
+ insertRecKey_.key_ = insertRec->extractKey(keySize_, sortUtil_->config()->numberOfBytesForRecordSize());
+ insertRecKey_.rec_ = insertRec;
+
+ if (compare(topNKeys_[root].key_ ,insertRecKey_.key_) == KEY1_IS_GREATER)
+ {
+ swap( &topNKeys_[root],&insertRecKey_);
+
+
+ //After swap, satisfy or rebalance the heap.
+ satisfyHeap();
+
+ }
+
+ //Once swapped, or not swapped, delete the unneeded record.
+ //This step is very important to discard the unwanted record.
+ //Note releaseTupp() also deletes the tupp allocated in sql
+ //buffer. This makes space for new records read in.
+ insertRecKey_.rec_->releaseTupp();
+ delete insertRecKey_.rec_;
+
+}
+
+Lng32 TopN::sortSendEnd()
+{
+ Lng32 retcode = SORT_SUCCESS;
+ ex_assert(loopIndex_ >= 0, "TopN::sortSendEnd: loopIndex_ is < 0");
+
+ buildHeap();
+ sortHeap();
+
+ return retcode;
+}
+
+void TopN::sortHeap()
+{
+ for (int i = runSize_-1; i >= 1; i--)
+ {
+ swap(&topNKeys_[0],&topNKeys_[i]);
+ siftDown(topNKeys_, 0, i-1);
+ }
+}
+
+Lng32 TopN::sortReceive(void *rec, ULng32& len)
+{
+ return SORT_FAILURE;
+}
+
+Lng32 TopN::sortReceive(void*& rec, ULng32& len, void*& tupp)
+{
+ //---------------------------------------------------------------
+ // We use Qsort to receive records only in case of internal sort
+ // for merging.
+ //---------------------------------------------------------------
+ if (recNum_ < runSize_) {
+ topNKeys_[recNum_].rec_->getRecordTupp(rec, recSize_, tupp);
+ len = recSize_;
+ recNum_++;
+ }
+ else {
+ len = 0;
+ }
+
+ return SORT_SUCCESS;
+}
+/*
+//----------------------------------------------------------------------
+// Name : heapSort
+//
+// Parameters : ...
+//
+// Description : This member function implements the heap sort
+// Return Value :
+// SORT_SUCCESS if everything goes on well.
+// SORT_FAILURE if any error encounterd.
+//
+// NOTE: For this implementation no extra buffer is required. It is done all
+// in place. This is the algorithm used if SORT_ITERATIVE_ALGO is set.
+// requirement. If the performance of heapSort is not as good we may switch
+// back to quickSort or iterativeQuickSort.
+
+//----------------------------------------------------------------------
+void TopN::heapSort(RecKeyBuffer keysToSort[], Int64 runsize)
+{
+ Int64 i;
+
+ for (i = (runsize/2 ); i >= 0; i--)
+ siftDown(keysToSort, i, runsize-1);
+
+ for (i = runsize-1; i >= 1; i--)
+ {
+
+ swap(&keysToSort[0],&keysToSort[i]);
+ siftDown(keysToSort, 0, i-1);
+ }
+}
+*/
+
+void TopN::siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom)
+{
+ Int64 done, maxChild;
+
+ done = 0;
+ while ((root*2 <= bottom) && (!done))
+ {
+ if (root*2 == bottom)
+ maxChild = root * 2;
+ else if (compare(keysToSort[root * 2].key_ ,
+ keysToSort[root * 2 + 1].key_) >= KEY1_IS_GREATER)
+ maxChild = root * 2;
+ else
+ maxChild = root * 2 + 1;
+
+ if (compare(keysToSort[root].key_ ,keysToSort[maxChild].key_) <=KEY1_IS_SMALLER)
+ {
+
+ swap( &keysToSort[root],&keysToSort[maxChild]);
+ root = maxChild;
+
+ }
+ else
+ done = 1;
+ }
+}
+
+//----------------------------------------------------------------------
+// Name : swap
+//
+// Parameters : ..
+//
+// Description : Swaps two elements from the QuickSort workspace. May
+// consider making this inline rather than a seperate
+// procedure call for performance reasons.
+//
+// Recompareturn Value :
+// SORT_SUCCESS if everything goes on well.
+// SORT_FAILURE if any error encounterd.
+//
+//----------------------------------------------------------------------
+
+NABoolean TopN::swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo)
+{
+ char* tempKey;
+ Record* tempRec;
+
+
+ tempKey = recKeyOne->key_;
+ tempRec = recKeyOne->rec_;
+
+
+ recKeyOne->key_ = recKeyTwo->key_;
+ recKeyOne->rec_ = recKeyTwo->rec_;
+
+
+ recKeyTwo->key_ = tempKey;
+ recKeyTwo->rec_ = tempRec;
+ return SORT_SUCCESS;
+}
+
+UInt32 TopN::getOverheadPerRecord(void)
+{
+ return (sizeof(RecKeyBuffer) + sizeof(Record));
+}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/sort/Topn.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/Topn.h b/core/sql/sort/Topn.h
new file mode 100644
index 0000000..a683536
--- /dev/null
+++ b/core/sql/sort/Topn.h
@@ -0,0 +1,89 @@
+/**********************************************************************
+// @@@ START COPYRIGHT @@@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// @@@ END COPYRIGHT @@@
+**********************************************************************/
+#ifndef TOPN_H
+#define TOPN_H
+
+/* -*-C++-*-
+******************************************************************************
+*
+* File: Topn.h
+*
+*
+******************************************************************************
+*/
+
+#include "SortAlgo.h"
+#include "Record.h"
+#include "Const.h"
+#include "NABasicObject.h"
+#include "SortError.h"
+
+
+class SortUtil;
+class ExBMOStats;
+
+
+class TopN : public SortAlgo { //SortAlgo inherits from NABasicObject
+
+public:
+
+ TopN(ULng32 recmax,ULng32 sortmaxmem, ULng32 recsize, NABoolean doNotallocRec,
+ ULng32 keysize, SortScratchSpace* scratch,NABoolean iterQuickSort,
+ CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil);
+ ~TopN(void);
+
+ Lng32 sortSend(void* rec, ULng32 len, void* tupp);
+
+ Lng32 sortClientOutOfMem(void){ return 0;}
+
+ Lng32 sortSendEnd();
+
+ Lng32 sortReceive(void* rec, ULng32& len);
+ Lng32 sortReceive(void*& rec, ULng32& len, void*& tupp);
+ UInt32 getOverheadPerRecord(void);
+ Lng32 generateInterRuns(){ return 0;}
+
+
+private:
+ void buildHeap();
+ void satisfyHeap();
+ void insertRec(void *rec, ULng32 len, void* tupp);
+ void sortHeap();
+ void siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom);
+ NABoolean swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo);
+
+ ULng32 loopIndex_;
+ ULng32 recNum_;
+ ULng32 allocRunSize_;
+ NABoolean isHeapified_;
+ RecKeyBuffer insertRecKey_;
+ RecKeyBuffer* topNKeys_;
+ SortError* sortError_;
+ CollHeap* heap_;
+ SortUtil* sortUtil_;
+ ExBMOStats *bmoStats_;
+};
+
+#endif
+
+
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/sqlcomp/DefaultConstants.h
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/DefaultConstants.h b/core/sql/sqlcomp/DefaultConstants.h
index a6270c9..69078f9 100644
--- a/core/sql/sqlcomp/DefaultConstants.h
+++ b/core/sql/sqlcomp/DefaultConstants.h
@@ -1676,6 +1676,7 @@ enum DefaultConstants
DO_RUNTIME_SPACE_OPTIMIZATION,
GEN_SORT_MAX_NUM_BUFFERS,
+ GEN_SORT_TOPN_SIZE,
SORT_ALGO, // Sort algorithm choice
// Not used anymore. OVERRIDE_SYSKEY takes its place.
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/61cc7b5b/core/sql/sqlcomp/nadefaults.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/nadefaults.cpp b/core/sql/sqlcomp/nadefaults.cpp
index d001ce6..149cca2 100644
--- a/core/sql/sqlcomp/nadefaults.cpp
+++ b/core/sql/sqlcomp/nadefaults.cpp
@@ -1629,6 +1629,7 @@ SDDkwd__(EXE_DIAGNOSTIC_EVENTS, "OFF"),
DDui1__(GEN_SORT_NUM_BUFFERS, "4"),
DDui1__(GEN_SORT_SIZE_DOWN, "2"),
DDui1__(GEN_SORT_SIZE_UP, "1024"),
+ DDui___(GEN_SORT_TOPN_SIZE, "0"),
DDui1__(GEN_SPLB_BUFFER_SIZE, "2"),
DDui1__(GEN_SPLB_NUM_BUFFERS, "1"),
DDui1__(GEN_SPLB_SIZE_DOWN, "2"),
[6/7] incubator-trafodion git commit: Merge remote branch
'prashanth-vasudev/TopN' into TopN
Posted by sa...@apache.org.
Merge remote branch 'prashanth-vasudev/TopN' into TopN
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/97052926
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/97052926
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/97052926
Branch: refs/heads/master
Commit: 97052926a15a40929e148653296f67e7a302a0f5
Parents: f95f736 6e93325
Author: Prashant Vasudev <pr...@esgyn.com>
Authored: Mon Oct 10 20:44:27 2016 +0000
Committer: Prashant Vasudev <pr...@esgyn.com>
Committed: Mon Oct 10 20:44:27 2016 +0000
----------------------------------------------------------------------
----------------------------------------------------------------------
[7/7] incubator-trafodion git commit: Merge remote branch
'origin/pr/743/head' into merge_743
Posted by sa...@apache.org.
Merge remote branch 'origin/pr/743/head' into merge_743
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/75048b0f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/75048b0f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/75048b0f
Branch: refs/heads/master
Commit: 75048b0f34c0191ea091a8308bd618257eabf739
Parents: a9f34a6 9705292
Author: Sandhya Sundaresan <sa...@apache.org>
Authored: Tue Oct 11 21:02:11 2016 +0000
Committer: Sandhya Sundaresan <sa...@apache.org>
Committed: Tue Oct 11 21:02:11 2016 +0000
----------------------------------------------------------------------
core/sql/comexe/ComTdbSort.h | 10 +-
core/sql/executor/ex_sort.cpp | 16 +-
core/sql/generator/GenRelMisc.cpp | 3 +-
core/sql/nskgmake/sort/Makefile | 3 +-
core/sql/regress/compGeneral/DIFF042.KNOWN | 36 +--
core/sql/sort/Qsort.h | 5 +-
core/sql/sort/Record.cpp | 14 +-
core/sql/sort/Record.h | 8 +
core/sql/sort/SortTopN.cpp | 318 ++++++++++++++++++++++++
core/sql/sort/SortTopN.h | 89 +++++++
core/sql/sort/SortUtil.cpp | 78 +++---
core/sql/sort/SortUtil.h | 2 +-
core/sql/sort/SortUtilCfg.cpp | 43 +---
core/sql/sort/SortUtilCfg.h | 11 +-
core/sql/sqlcomp/DefaultConstants.h | 1 +
core/sql/sqlcomp/nadefaults.cpp | 1 +
16 files changed, 526 insertions(+), 112 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/75048b0f/core/sql/sqlcomp/nadefaults.cpp
----------------------------------------------------------------------
[4/7] incubator-trafodion git commit: [TRAFODION-2259] changes from
feedback, drive TopN sort from GETN
Posted by sa...@apache.org.
[TRAFODION-2259] changes from feedback, drive TopN sort from GETN
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/08081bd7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/08081bd7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/08081bd7
Branch: refs/heads/master
Commit: 08081bd70956bfe5d8edbd35dd87ec75d3de70e5
Parents: 3cd201a
Author: Prashant Vasudev <pr...@esgyn.com>
Authored: Thu Oct 6 17:23:52 2016 +0000
Committer: Prashant Vasudev <pr...@esgyn.com>
Committed: Mon Oct 10 17:49:35 2016 +0000
----------------------------------------------------------------------
core/sql/comexe/ComTdbSort.h | 17 +-
core/sql/executor/ex_sort.cpp | 17 +-
core/sql/generator/GenRelMisc.cpp | 4 +-
core/sql/nskgmake/sort/Makefile | 2 +-
core/sql/sort/Record.cpp | 12 +-
core/sql/sort/SortTopN.cpp | 318 +++++++++++++++++++++++++++++
core/sql/sort/SortTopN.h | 89 ++++++++
core/sql/sort/SortUtil.cpp | 84 ++++----
core/sql/sort/SortUtil.h | 2 +-
core/sql/sort/SortUtilCfg.cpp | 1 +
core/sql/sort/SortUtilCfg.h | 8 +-
core/sql/sort/Topn.cpp | 337 -------------------------------
core/sql/sort/Topn.h | 89 --------
core/sql/sqlcomp/DefaultConstants.h | 2 +-
core/sql/sqlcomp/nadefaults.cpp | 2 +-
15 files changed, 483 insertions(+), 501 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/comexe/ComTdbSort.h
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbSort.h b/core/sql/comexe/ComTdbSort.h
index 93a1a81..f73cb6b 100644
--- a/core/sql/comexe/ComTdbSort.h
+++ b/core/sql/comexe/ComTdbSort.h
@@ -168,7 +168,8 @@ protected:
COLLECT_NF_ERRORS_ = 0x0001,
PREFIX_ORDERED_INPUT = 0x0002,
SORT_FROM_TOP = 0x0004,
- USER_SIDETREE_INSERT = 0x0008
+ USER_SIDETREE_INSERT = 0x0008,
+ SORT_TOPN_ENABLE = 0x0010
};
SortOptionsPtr sortOptions_; // 00-07
@@ -204,9 +205,7 @@ protected:
Float32 bmoCitizenshipFactor_; // 68-71
Int32 pMemoryContingencyMB_; // 72-75
UInt16 sortGrowthPercent_; // 76-77
- char filler2_[2]; // 78-79
- ULng32 topNSize_; // 80-83
- char fillersComTdbSort_[12]; // 84-95
+ char fillersComTdbSort_[18]; // 78-95
public:
@@ -307,6 +306,10 @@ public:
NABoolean sortFromTop() { return (flags_ & SORT_FROM_TOP) != 0;}
void setSortFromTop(NABoolean v)
{(v ? flags_ |= SORT_FROM_TOP : flags_ &= ~SORT_FROM_TOP);}
+
+ NABoolean topNSort() { return (flags_ & SORT_TOPN_ENABLE) != 0;}
+ void setTopNSort(NABoolean v)
+ {(v ? flags_ |= SORT_TOPN_ENABLE : flags_ &= ~SORT_TOPN_ENABLE);}
NABoolean userSidetreeInsert()
{
@@ -327,11 +330,7 @@ public:
{ pMemoryContingencyMB_ = mCMB;}
Int32 getMemoryContingencyMB(void)
{ return pMemoryContingencyMB_; }
- void setTopNSize(UInt32 size)
- { topNSize_ = size; }
- ULng32 getTopNSize(void)
- { return topNSize_; }
-
+
void setSortMemEstInMbPerCpu(Float32 s) {sortMemEstInMbPerCpu_=s;}
Float32 getSortMemEstInMbPerCpu() {return sortMemEstInMbPerCpu_;}
Float32 sortGrowthPercent() {return Float32(sortGrowthPercent_/100.0);}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/executor/ex_sort.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_sort.cpp b/core/sql/executor/ex_sort.cpp
index e655abd..82a8fbc 100644
--- a/core/sql/executor/ex_sort.cpp
+++ b/core/sql/executor/ex_sort.cpp
@@ -242,8 +242,8 @@ ExSortTcb::ExSortTcb(const ExSortTdb & sort_tdb,
sortCfg_->setIntermediateScratchCleanup(st->sortOptions_->intermediateScratchCleanup());
sortCfg_->setResizeCifRecord(st->sortOptions_->resizeCifRecord());
sortCfg_->setConsiderBufferDefrag(st->sortOptions_->considerBufferDefrag());
- sortCfg_->setTopNSize(st->getTopNSize());
-
+ sortCfg_->setTopNSort(st->topNSort());
+
switch(st->getOverFlowMode())
{
case SQLCLI_OFM_SSD_TYPE:
@@ -570,6 +570,7 @@ short ExSortTcb::workUp()
{
Lng32 rc = 0;
short workRC = 0;
+ ULng32 topNCount = 0;
// if no parent request, return
if (qparent_.down->isEmpty())
@@ -611,9 +612,13 @@ short ExSortTcb::workUp()
sortDiag_ = NULL; // reset
// LCOV_EXCL_STOP
}
-
- if (sortUtil_->sortInitialize(*sortCfg_) != SORT_SUCCESS)
- {
+
+ if((request == ex_queue::GET_N) &&
+ (pentry_down->downState.requestValue > 0))
+ topNCount = (ULng32)pentry_down->downState.requestValue;
+
+ if (sortUtil_->sortInitialize(*sortCfg_, topNCount) != SORT_SUCCESS)
+ {
// LCOV_EXCL_START
createSortDiags();
pstate.step_ = ExSortTcb::SORT_ERROR;
@@ -1927,7 +1932,7 @@ short ExSortFromTopTcb::work()
// LCOV_EXCL_STOP
}
- if (sortUtil_->sortInitialize(*sortCfg_) != SORT_SUCCESS)
+ if (sortUtil_->sortInitialize(*sortCfg_, 0) != SORT_SUCCESS)
{
// LCOV_EXCL_START
createSortDiags();
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/generator/GenRelMisc.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenRelMisc.cpp b/core/sql/generator/GenRelMisc.cpp
index 4db4711..3d69151 100644
--- a/core/sql/generator/GenRelMisc.cpp
+++ b/core/sql/generator/GenRelMisc.cpp
@@ -3184,10 +3184,8 @@ short Sort::generateTdb(Generator * generator,
sort_tdb->setSortFromTop(sortFromTop());
sort_tdb->setOverflowMode(generator->getOverflowMode());
+ sort_tdb->setTopNSort(CmpCommon::getDefault(GEN_SORT_TOPN) == DF_ON);
- sort_tdb->setTopNSize((ULng32)getDefault(GEN_SORT_TOPN_SIZE));
-
-
if (generator->getUserSidetreeInsert())
sort_tdb->setUserSidetreeInsert(TRUE);
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/nskgmake/sort/Makefile
----------------------------------------------------------------------
diff --git a/core/sql/nskgmake/sort/Makefile b/core/sql/nskgmake/sort/Makefile
index b0b7401..64db734 100755
--- a/core/sql/nskgmake/sort/Makefile
+++ b/core/sql/nskgmake/sort/Makefile
@@ -37,7 +37,7 @@ CPPSRC := CommonUtil.cpp \
Statistics.cpp \
TourTree.cpp \
TreeNode.cpp \
- Topn.cpp
+ SortTopN.cpp
CPPSRC += vers_libsort.cpp
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sort/Record.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/Record.cpp b/core/sql/sort/Record.cpp
index 2437575..cf4bf07 100644
--- a/core/sql/sort/Record.cpp
+++ b/core/sql/sort/Record.cpp
@@ -57,12 +57,12 @@ Record::Record(ULng32 size, NABoolean doNotallocRec, CollHeap* heap)
Record::Record(void *rec, ULng32 reclen, void* tupp, CollHeap* heap, SortError* sorterror)
{
- recSize_ = reclen;
- sortError_ = sorterror;
- heap_ = heap;
- tupp_ = tupp;
- allocatedRec_ = FALSE_L;
- rec_ =(char *) rec;
+ recSize_ = reclen;
+ sortError_ = sorterror;
+ heap_ = heap;
+ tupp_ = tupp;
+ allocatedRec_ = FALSE_L;
+ rec_ =(char *) rec;
}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sort/SortTopN.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortTopN.cpp b/core/sql/sort/SortTopN.cpp
new file mode 100644
index 0000000..2e6351b
--- /dev/null
+++ b/core/sql/sort/SortTopN.cpp
@@ -0,0 +1,318 @@
+/**********************************************************************
+// @@@ START COPYRIGHT @@@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// @@@ END COPYRIGHT @@@
+**********************************************************************/
+/* -*-C++-*-
+******************************************************************************
+*
+* File: SortTopN.cpp
+*
+* Description: This file contains the implementation of all member functions
+* of the class TopN.
+*
+* 1. Sort would initially maintain Top N array of elements to being with.
+* 2. Read records into TopN array.
+* 3. Once TopN array is full, heapify the array into max heap. Top node in
+* the heap is always the highest node.
+* 4. Subsequent record read either gets discarded( if greater than top node)
+* or replace top node( if lesser then top node) . if replaced top node,
+* re-balance the heap.
+* 5. Repeat steps 4 until last record is read.
+* 6. sort the final heap using heap sort.
+*******************************************************************************/
+
+#include <iostream>
+#include <fstream>
+
+#ifndef DEBUG
+#undef NDEBUG
+#define NDEBUG
+#endif
+#include "ex_stdh.h"
+#include "SortTopN.h"
+#include "ScratchSpace.h"
+#include "logmxevent.h"
+#include "SortUtil.h"
+#include "ex_ex.h"
+#include "ExStats.h"
+
+//------------------------------------------------------------------------
+// Class Constructor.
+//------------------------------------------------------------------------
+SortTopN::SortTopN(ULng32 runsize, ULng32 sortmaxmem, ULng32 recsize,
+ NABoolean doNotallocRec, ULng32 keysize,
+ SortScratchSpace* scratch, NABoolean iterSort,
+ CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil):
+ SortAlgo(runsize, recsize, doNotallocRec, keysize, scratch, explainNodeId),
+ loopIndex_(0), heap_(heap), sortError_(sorterror),
+ sortUtil_(sortutil)
+{
+ //runsize is TopN size. Fixed.
+ allocRunSize_ = runsize;
+
+ //Actual run size after all elements read.
+ runSize_ = 0;
+
+ isHeapified_ = FALSE;
+
+ topNKeys_ = (RecKeyBuffer *)heap_->allocateMemory(sizeof(RecKeyBuffer) * allocRunSize_);
+
+ // Below asserts useful in debug mode.
+ ex_assert(topNKeys_ != NULL, "Sort: Initial topNKeys_ allocation failed");
+
+ recNum_ = 0;
+ ExOperStats *stat = sortUtil_->config()->getCallingTcb()->getStatsEntry();
+ if (stat)
+ bmoStats_ = stat->castToExBMOStats();
+ else
+ bmoStats_ = NULL;
+ if (bmoStats_)
+ bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
+}
+
+
+SortTopN::~SortTopN(void)
+{
+ if (topNKeys_ != NULL)
+ {
+ for (int i = 0; i < runSize_; i++)
+ topNKeys_[i].rec_->releaseTupp();
+
+ NADELETEBASIC(topNKeys_, heap_);
+ topNKeys_ = NULL;
+ }
+
+ if (bmoStats_)
+ bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
+}
+
+Lng32 SortTopN::sortSend(void *rec, ULng32 len, void* tupp)
+{
+ //if heap not built means, TopN array has more slots
+ //available to fill.
+ if(! isHeapified_)
+ {
+ ex_assert(loopIndex_ >= 0, "TopN::sortSend: loopIndex_ is < 0");
+ ex_assert(loopIndex_ < allocRunSize_, "TopN::sortSend: loopIndex_ > allocRunSize_");
+ ex_assert(rec != NULL, "TopN::sortSend: rec is NULL");
+
+ Record * newRec = new (heap_)Record(rec, len, tupp, heap_, sortError_);
+
+ topNKeys_[loopIndex_].key_ = newRec->extractKey(keySize_,
+ sortUtil_->config()->numberOfBytesForRecordSize());
+ topNKeys_[loopIndex_].rec_ = newRec;
+ if (++loopIndex_ == allocRunSize_)
+ {
+ //Reaching here means, we have filled up the array.
+ //Now heapify the array to start accepting/eliminating new elements from now on.
+
+ //Note lookIndex_ contains the current number of filled elements.
+ buildHeap();
+ }
+ return SORT_SUCCESS;
+ }
+
+ //Reaching here means, heap is already built.
+ //Check if the new rec belongs to this heap by comparing the
+ //new rec key with the root node of the heap ( root node is always the greatest).
+ insertRec(rec, len, tupp);
+ return SORT_SUCCESS;
+ }
+
+
+void SortTopN::buildHeap()
+{
+ if(!isHeapified_)
+ {
+ //loopIndex_ is now <= TopN
+ runSize_ = loopIndex_;
+
+ satisfyHeap();
+
+ isHeapified_ = TRUE;
+ }
+}
+
+//Satisfy Heap makes sure the heap is balanced maxHeap.
+//Note this does not mean heap is sorted. It just makes sure
+//the higher level nodes are greater than lower level nodes.
+//Topmost node or root will be the highest.
+void SortTopN::satisfyHeap()
+{
+ for (int i = (runSize_/2 ); i >= 0; i--)
+ siftDown(topNKeys_, i, runSize_-1);
+}
+
+
+void SortTopN::insertRec(void *rec, ULng32 len, void* tupp)
+{
+ ex_assert(isHeapified_, "TopN::insertRec: not heapified");
+
+ int root = 0; //Always, root node is the zero'th element in array.
+
+ Record * newrec = new (heap_)Record(rec, len, tupp, heap_, sortError_);
+
+ insertRecKey_.key_ = newrec->extractKey(keySize_,
+ sortUtil_->config()->numberOfBytesForRecordSize());
+ insertRecKey_.rec_ = newrec;
+
+ if (compare(topNKeys_[root].key_ ,insertRecKey_.key_) == KEY1_IS_GREATER)
+ {
+ swap( &topNKeys_[root],&insertRecKey_);
+
+
+ //After swap, satisfy or rebalance the heap.
+ satisfyHeap();
+
+ }
+
+ //Once swapped, or not swapped, delete the unneeded record.
+ //This step is very important to discard the unwanted record.
+ //Note releaseTupp() also deletes the tupp allocated in sql
+ //buffer. This makes space for new records read in.
+ insertRecKey_.rec_->releaseTupp();
+ NADELETEBASIC(insertRecKey_.rec_, heap_);
+
+}
+
+Lng32 SortTopN::sortSendEnd()
+{
+ Lng32 retcode = SORT_SUCCESS;
+ ex_assert(loopIndex_ >= 0, "TopN::sortSendEnd: loopIndex_ is < 0");
+
+ buildHeap();
+ sortHeap();
+
+ return retcode;
+}
+
+//----------------------------------------------------------------------
+// Name : sortHeap
+//
+//
+// Description : The heap is already balanced. This step sorts the heap.
+//----------------------------------------------------------------------
+void SortTopN::sortHeap()
+{
+ for (int i = runSize_-1; i >= 1; i--)
+ {
+ swap(&topNKeys_[0],&topNKeys_[i]);
+ siftDown(topNKeys_, 0, i-1);
+ }
+}
+
+Lng32 SortTopN::sortReceive(void *rec, ULng32& len)
+{
+ //This method applicable to overflow records only
+ return SORT_FAILURE;
+}
+
+Lng32 SortTopN::sortReceive(void*& rec, ULng32& len, void*& tupp)
+{
+ if (recNum_ < runSize_)
+ {
+ topNKeys_[recNum_].rec_->getRecordTupp(rec, recSize_, tupp);
+ len = recSize_;
+ recNum_++;
+ }
+ else
+ {
+ len = 0;
+ }
+ return SORT_SUCCESS;
+}
+
+
+//----------------------------------------------------------------------
+// Name : siftDown
+//
+// Parameters : ..
+//
+// Description : Given the root node,rebalances the heap.Child nodes are less
+// value than parent nodes. Top most node or root contains
+// highest value.
+//
+//
+//----------------------------------------------------------------------
+void SortTopN::siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom)
+{
+ Int64 done, maxChild;
+
+ done = 0;
+ while ((root*2 <= bottom) && (!done))
+ {
+ if (root*2 == bottom)
+ maxChild = root * 2;
+ else if (compare(keysToSort[root * 2].key_ ,
+ keysToSort[root * 2 + 1].key_) >= KEY1_IS_GREATER)
+ maxChild = root * 2;
+ else
+ maxChild = root * 2 + 1;
+
+ if (compare(keysToSort[root].key_ ,keysToSort[maxChild].key_) <=KEY1_IS_SMALLER)
+ {
+ swap( &keysToSort[root],&keysToSort[maxChild]);
+ root = maxChild;
+
+ }
+ else
+ done = 1;
+ }
+}
+
+//----------------------------------------------------------------------
+// Name : swap
+//
+// Parameters : ..
+//
+// Description : Swaps two elements from the QuickSort workspace. May
+// consider making this inline rather than a seperate
+// procedure call for performance reasons.
+//
+// Recompareturn Value :
+// SORT_SUCCESS if everything goes on well.
+// SORT_FAILURE if any error encounterd.
+//
+//----------------------------------------------------------------------
+
+NABoolean SortTopN::swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo)
+{
+ char* tempKey;
+ Record* tempRec;
+
+
+ tempKey = recKeyOne->key_;
+ tempRec = recKeyOne->rec_;
+
+
+ recKeyOne->key_ = recKeyTwo->key_;
+ recKeyOne->rec_ = recKeyTwo->rec_;
+
+
+ recKeyTwo->key_ = tempKey;
+ recKeyTwo->rec_ = tempRec;
+ return SORT_SUCCESS;
+}
+
+UInt32 SortTopN::getOverheadPerRecord(void)
+{
+ return (sizeof(RecKeyBuffer) + sizeof(Record));
+}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sort/SortTopN.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortTopN.h b/core/sql/sort/SortTopN.h
new file mode 100644
index 0000000..cd7e4f9
--- /dev/null
+++ b/core/sql/sort/SortTopN.h
@@ -0,0 +1,89 @@
+/**********************************************************************
+// @@@ START COPYRIGHT @@@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// @@@ END COPYRIGHT @@@
+**********************************************************************/
+#ifndef SORTTOPN_H
+#define SORTTOPN_H
+
+/* -*-C++-*-
+******************************************************************************
+*
+* File: SortTopN.h
+*
+*
+******************************************************************************
+*/
+
+#include "SortAlgo.h"
+#include "Record.h"
+#include "Const.h"
+#include "NABasicObject.h"
+#include "SortError.h"
+
+
+class SortUtil;
+class ExBMOStats;
+
+
+class SortTopN : public SortAlgo { //SortAlgo inherits from NABasicObject
+
+public:
+
+ SortTopN(ULng32 recmax,ULng32 sortmaxmem, ULng32 recsize, NABoolean doNotallocRec,
+ ULng32 keysize, SortScratchSpace* scratch,NABoolean iterQuickSort,
+ CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil);
+ ~SortTopN(void);
+
+ Lng32 sortSend(void* rec, ULng32 len, void* tupp);
+
+ Lng32 sortClientOutOfMem(void){ return 0;}
+
+ Lng32 sortSendEnd();
+
+ Lng32 sortReceive(void* rec, ULng32& len);
+ Lng32 sortReceive(void*& rec, ULng32& len, void*& tupp);
+ UInt32 getOverheadPerRecord(void);
+ Lng32 generateInterRuns(){ return 0;}
+
+
+private:
+ void buildHeap();
+ void satisfyHeap();
+ void insertRec(void *rec, ULng32 len, void* tupp);
+ void sortHeap();
+ void siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom);
+ NABoolean swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo);
+
+ ULng32 loopIndex_;
+ ULng32 recNum_;
+ ULng32 allocRunSize_;
+ NABoolean isHeapified_;
+ RecKeyBuffer insertRecKey_;
+ RecKeyBuffer* topNKeys_;
+ SortError* sortError_;
+ CollHeap* heap_;
+ SortUtil* sortUtil_;
+ ExBMOStats *bmoStats_;
+};
+
+#endif
+
+
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sort/SortUtil.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtil.cpp b/core/sql/sort/SortUtil.cpp
index 433e815..4cb11a8 100644
--- a/core/sql/sort/SortUtil.cpp
+++ b/core/sql/sort/SortUtil.cpp
@@ -45,7 +45,7 @@
#include "ex_sort.h"
#include "SortUtil.h"
#include "Qsort.h"
-#include "Topn.h"
+#include "SortTopN.h"
#include "ComCextdecs.h"
#include "logmxevent.h"
#include "ExStats.h"
@@ -101,17 +101,17 @@ NABoolean SortUtil::scratchInitialize(void)
&sortError_,
explainNodeId_,
config_->scratchIOBlockSize_,
- config_->logInfoEvent_,
+ config_->logInfoEvent_,
config_->scratchMgmtOption_
- );
+ );
if (scratch_ == NULL)
{
sortError_.setErrorInfo( EScrNoMemory //sort error
- ,NULL //syserr: the actual FS error
- ,NULL //syserrdetail
- ,"SortUtil::scratchInitialize" //methodname
- );
+ ,NULL //syserr: the actual FS error
+ ,NULL //syserrdetail
+ ,"SortUtil::scratchInitialize" //methodname
+ );
return SORT_FAILURE;
}
@@ -149,7 +149,7 @@ NABoolean SortUtil::scratchInitialize(void)
// SORT_FAILURE if any error encounterd.
//
//----------------------------------------------------------------------
-NABoolean SortUtil::sortInitialize(SortUtilConfig& config)
+NABoolean SortUtil::sortInitialize(SortUtilConfig& config, ULng32 topNSize)
{
//---------------------------------------------------------------
@@ -160,46 +160,46 @@ NABoolean SortUtil::sortInitialize(SortUtilConfig& config)
doCleanUp();
//if topNSize_ is set, then use TopN.
- if(!config.topNSize_)
+ if(config.topNSort_ && topNSize)
{
sortAlgo_ =
- new (config.heapAddr_) Qsort(config.runSize_,
- config.maxMem_,
- config.recSize_,
- config.sortType_.doNotAllocRec_,
- config.keySize_,
- scratch_,
- TRUE,
- config.heapAddr_,
- &sortError_,
- explainNodeId_,
- this);
+ new (config.heapAddr_) SortTopN(topNSize,
+ config.maxMem_,
+ config.recSize_,
+ config.sortType_.doNotAllocRec_,
+ config.keySize_,
+ scratch_,
+ TRUE,
+ config.heapAddr_,
+ &sortError_,
+ explainNodeId_,
+ this);
+
}
else
{
- sortAlgo_ =
- new (config.heapAddr_) TopN(config.topNSize_,
- config.maxMem_,
- config.recSize_,
- config.sortType_.doNotAllocRec_,
- config.keySize_,
- scratch_,
- TRUE,
- config.heapAddr_,
- &sortError_,
- explainNodeId_,
- this);
+ sortAlgo_ =
+ new (config.heapAddr_) Qsort(config.runSize_,
+ config.maxMem_,
+ config.recSize_,
+ config.sortType_.doNotAllocRec_,
+ config.keySize_,
+ scratch_,
+ TRUE,
+ config.heapAddr_,
+ &sortError_,
+ explainNodeId_,
+ this);
+ }
+ if (sortAlgo_ == NULL)
+ {
+ sortError_.setErrorInfo(EScrNoMemory //sort error
+ ,NULL //syserr: the actual FS error
+ ,NULL //syserrdetail
+ ,"SortUtil::sortInitialize" //methodname
+ );
+ return SORT_FAILURE;
}
- if (sortAlgo_ == NULL)
- {
- sortError_.setErrorInfo( EScrNoMemory //sort error
- ,NULL //syserr: the actual FS error
- ,NULL //syserrdetail
- ,"SortUtil::sortInitialize" //methodname
- );
- return SORT_FAILURE;
- }
-
//The maximum memory that sort can consume is governed by three parameters.
//(config.maxNumBuffers_ * sorttdb.maxbufferSize) + config..maxMem_.
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sort/SortUtil.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtil.h b/core/sql/sort/SortUtil.h
index 6eeb490..82b86f7 100644
--- a/core/sql/sort/SortUtil.h
+++ b/core/sql/sort/SortUtil.h
@@ -62,7 +62,7 @@ public:
SortUtil(Lng32 explainNodeId);
~SortUtil();
- NABoolean sortInitialize(SortUtilConfig& config);
+ NABoolean sortInitialize(SortUtilConfig& config, ULng32 topNSize);
NABoolean sortEnd(void);
Lng32 sortSend(void* record, ULng32 len, void* tupp);
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sort/SortUtilCfg.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtilCfg.cpp b/core/sql/sort/SortUtilCfg.cpp
index 25bca77..591b645 100644
--- a/core/sql/sort/SortUtilCfg.cpp
+++ b/core/sql/sort/SortUtilCfg.cpp
@@ -89,6 +89,7 @@ SortUtilConfig::SortUtilConfig(CollHeap* heap)
sortMemEstInMbPerCpu_ = 0;
bmoMaxMemThresholdMB_ = 0;
intermediateScratchCleanup_ = TRUE;
+ topNSort_ = FALSE;
}
SortUtilConfig::~SortUtilConfig(void)
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sort/SortUtilCfg.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtilCfg.h b/core/sql/sort/SortUtilCfg.h
index be7ad9a..1bc8716 100644
--- a/core/sql/sort/SortUtilCfg.h
+++ b/core/sql/sort/SortUtilCfg.h
@@ -148,10 +148,6 @@ public:
{
return numEsps_;
}
- void setTopNSize(ULng32 size)
- {
- topNSize_ = size;
- }
void setEventHandler(ExSubtask *eh)
{
ioEventHandler_ = eh;
@@ -280,6 +276,8 @@ public:
void setIntermediateScratchCleanup(NABoolean v)
{ intermediateScratchCleanup_ = v;}
NABoolean intermediateScratchCleanup(){return intermediateScratchCleanup_;}
+ void setTopNSort(NABoolean v)
+ { topNSort_ = v; }
friend class SortUtil;
@@ -318,7 +316,7 @@ private:
ULng32 mergeOrder_; // Need to modify this to do automatically.
ULng32 minMem_; // Minimum sort heap memory
ULng32 maxMem_; // Maximum sort heap memory
- ULng32 topNSize_; // TopN size set by the executor
+ NABoolean topNSort_; // TopN sorting enable/disable
ULng32 runSizeIncr_; // unused :how much to increment the run size by.
ULng32 maxNumBuffers_; // Max buffer space as set by the compiler
unsigned short scratchThreshold_; // percent of disk usage after which a disk will be discarded for use
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sort/Topn.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/Topn.cpp b/core/sql/sort/Topn.cpp
deleted file mode 100644
index 417a795..0000000
--- a/core/sql/sort/Topn.cpp
+++ /dev/null
@@ -1,337 +0,0 @@
-/**********************************************************************
-// @@@ START COPYRIGHT @@@
-//
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-//
-// @@@ END COPYRIGHT @@@
-**********************************************************************/
-/* -*-C++-*-
-******************************************************************************
-*
-* File: TopN.cpp
-*
-* Description: This file contains the implementation of all member functions
-* of the class TopN.
-*
-* 1. Sort would initially maintain Top N array of elements to being with.
-* 2. Read records into TopN array.
-* 3. Once TopN array is full, heapify the array into max heap. Top node in the heap is always the highest node.
-* 4. Subsequent record read either gets discarded( if greater than top node) or replace top node( if lesser then top node) . if replaced top node, re-balance the heap.
-* 5. Repeat steps 4 until last record is read.
-* 6. sort the final heap using heap sort.
-*******************************************************************************/
-
-#include <iostream>
-#include <fstream>
-
-#ifndef DEBUG
-#undef NDEBUG
-#define NDEBUG
-#endif
-#include "ex_stdh.h"
-#include "Topn.h"
-#include "ScratchSpace.h"
-#include "logmxevent.h"
-#include "SortUtil.h"
-#include "ex_ex.h"
-#include "ExStats.h"
-
-//------------------------------------------------------------------------
-// Class Constructor.
-//------------------------------------------------------------------------
-TopN::TopN(ULng32 runsize, ULng32 sortmaxmem, ULng32 recsize,
- NABoolean doNotallocRec, ULng32 keysize,
- SortScratchSpace* scratch, NABoolean iterSort,
- CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil):
- SortAlgo(runsize, recsize, doNotallocRec, keysize, scratch, explainNodeId),
- loopIndex_(0), heap_(heap), sortError_(sorterror),
- sortUtil_(sortutil)
-{
- //runsize is TopN size. Fixed.
- allocRunSize_ = runsize;
-
- //Actual run size after all elements read.
- runSize_ = 0;
-
- isHeapified_ = FALSE;
-
- //allocateMemory failureIsFatal is defaulted to TRUE means allocation failure results in
- //longjump to jump handler defined in ex_sort.cpp. Only applicable on NSK.
- topNKeys_ = (RecKeyBuffer *)heap_->allocateMemory(sizeof(RecKeyBuffer) * allocRunSize_);
-
- // Below asserts useful in debug mode. Also asserts if longjmp did not happen.
- //ex_assert(rootRecord_!= NULL, "Sort: Initial rootRecord_ allocation failed");
- ex_assert(topNKeys_ != NULL, "Sort: Initial topNKeys_ allocation failed");
-
- recNum_ = 0;
- ExOperStats *stat = sortUtil_->config()->getCallingTcb()->getStatsEntry();
- if (stat)
- bmoStats_ = stat->castToExBMOStats();
- else
- bmoStats_ = NULL;
- if (bmoStats_)
- bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
-}
-
-
-//------------------------------------------------------------------------
-// Class Destructor: Delete all the heap space pointed by pointers in Qsort
-//------------------------------------------------------------------------
-TopN::~TopN(void)
-{
-
- for (int i = 0; i < runSize_; i++)
- topNKeys_[i].rec_->releaseTupp();
-
-
- if (topNKeys_ != NULL) {
- NADELETEBASIC(topNKeys_, heap_);
- topNKeys_ = NULL;
- }
- if (bmoStats_)
- bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
-
-}
-
-Lng32 TopN::sortSend(void *rec, ULng32 len, void* tupp)
-{
- //if heap not built means, TopN array has more slots
- //available to fill.
- if(! isHeapified_)
- {
- ex_assert(loopIndex_ >= 0, "TopN::sortSend: loopIndex_ is < 0");
- ex_assert(loopIndex_ < allocRunSize_, "TopN::sortSend: loopIndex_ > allocRunSize_");
- ex_assert(rec != NULL, "TopN::sortSend: rec is NULL");
-
- Record * insertRec = new Record(rec, len, tupp, heap_, sortError_);
-
- topNKeys_[loopIndex_].key_ = insertRec->extractKey(keySize_, sortUtil_->config()->numberOfBytesForRecordSize());
- topNKeys_[loopIndex_].rec_ = insertRec;
- if (++loopIndex_ == allocRunSize_)
- {
- //Reaching here means, we have filled up the array.
- //Now heapify the array to start accepting/eliminating new elements from now on.
-
- //Note lookIndex_ contains the current number of filled elements.
-
- buildHeap();
- }
- return SORT_SUCCESS;
- }
-
- //Reaching here means, heap is already build.
- //Check if the new rec belongs to this heap by comparing the
- //new rec key with the root node of the heap ( root node is always the greatest).
- insertRec(rec, len, tupp);
- return SORT_SUCCESS;
- }
-
-
-void TopN::buildHeap()
-{
- if(!isHeapified_)
- {
- //loopIndex_ is now <= TopN
- runSize_ = loopIndex_;
-
- satisfyHeap();
-
- isHeapified_ = TRUE;
- }
-}
-
-//Satisfy Heap makes sure the heap is balanced maxHeap.
-//Note this does not mean heap is sorted. It just makes sure
-//the higher level nodes are greater than lower level nodes.
-//Topmost node or root will be the highest.
-void TopN::satisfyHeap()
-{
- for (int i = (runSize_/2 ); i >= 0; i--)
- siftDown(topNKeys_, i, runSize_-1);
-}
-
-
-void TopN::insertRec(void *rec, ULng32 len, void* tupp)
-{
- ex_assert(isHeapified_, "TopN::insertRec: not heapified");
-
- int root = 0; //Always, root node is the zero'th element in array.
-
- Record * insertRec = new Record(rec, len, tupp, heap_, sortError_);
- insertRecKey_.key_ = insertRec->extractKey(keySize_, sortUtil_->config()->numberOfBytesForRecordSize());
- insertRecKey_.rec_ = insertRec;
-
- if (compare(topNKeys_[root].key_ ,insertRecKey_.key_) == KEY1_IS_GREATER)
- {
- swap( &topNKeys_[root],&insertRecKey_);
-
-
- //After swap, satisfy or rebalance the heap.
- satisfyHeap();
-
- }
-
- //Once swapped, or not swapped, delete the unneeded record.
- //This step is very important to discard the unwanted record.
- //Note releaseTupp() also deletes the tupp allocated in sql
- //buffer. This makes space for new records read in.
- insertRecKey_.rec_->releaseTupp();
- delete insertRecKey_.rec_;
-
-}
-
-Lng32 TopN::sortSendEnd()
-{
- Lng32 retcode = SORT_SUCCESS;
- ex_assert(loopIndex_ >= 0, "TopN::sortSendEnd: loopIndex_ is < 0");
-
- buildHeap();
- sortHeap();
-
- return retcode;
-}
-
-void TopN::sortHeap()
-{
- for (int i = runSize_-1; i >= 1; i--)
- {
- swap(&topNKeys_[0],&topNKeys_[i]);
- siftDown(topNKeys_, 0, i-1);
- }
-}
-
-Lng32 TopN::sortReceive(void *rec, ULng32& len)
-{
- return SORT_FAILURE;
-}
-
-Lng32 TopN::sortReceive(void*& rec, ULng32& len, void*& tupp)
-{
- //---------------------------------------------------------------
- // We use Qsort to receive records only in case of internal sort
- // for merging.
- //---------------------------------------------------------------
- if (recNum_ < runSize_) {
- topNKeys_[recNum_].rec_->getRecordTupp(rec, recSize_, tupp);
- len = recSize_;
- recNum_++;
- }
- else {
- len = 0;
- }
-
- return SORT_SUCCESS;
-}
-/*
-//----------------------------------------------------------------------
-// Name : heapSort
-//
-// Parameters : ...
-//
-// Description : This member function implements the heap sort
-// Return Value :
-// SORT_SUCCESS if everything goes on well.
-// SORT_FAILURE if any error encounterd.
-//
-// NOTE: For this implementation no extra buffer is required. It is done all
-// in place. This is the algorithm used if SORT_ITERATIVE_ALGO is set.
-// requirement. If the performance of heapSort is not as good we may switch
-// back to quickSort or iterativeQuickSort.
-
-//----------------------------------------------------------------------
-void TopN::heapSort(RecKeyBuffer keysToSort[], Int64 runsize)
-{
- Int64 i;
-
- for (i = (runsize/2 ); i >= 0; i--)
- siftDown(keysToSort, i, runsize-1);
-
- for (i = runsize-1; i >= 1; i--)
- {
-
- swap(&keysToSort[0],&keysToSort[i]);
- siftDown(keysToSort, 0, i-1);
- }
-}
-*/
-
-void TopN::siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom)
-{
- Int64 done, maxChild;
-
- done = 0;
- while ((root*2 <= bottom) && (!done))
- {
- if (root*2 == bottom)
- maxChild = root * 2;
- else if (compare(keysToSort[root * 2].key_ ,
- keysToSort[root * 2 + 1].key_) >= KEY1_IS_GREATER)
- maxChild = root * 2;
- else
- maxChild = root * 2 + 1;
-
- if (compare(keysToSort[root].key_ ,keysToSort[maxChild].key_) <=KEY1_IS_SMALLER)
- {
-
- swap( &keysToSort[root],&keysToSort[maxChild]);
- root = maxChild;
-
- }
- else
- done = 1;
- }
-}
-
-//----------------------------------------------------------------------
-// Name : swap
-//
-// Parameters : ..
-//
-// Description : Swaps two elements from the QuickSort workspace. May
-// consider making this inline rather than a seperate
-// procedure call for performance reasons.
-//
-// Recompareturn Value :
-// SORT_SUCCESS if everything goes on well.
-// SORT_FAILURE if any error encounterd.
-//
-//----------------------------------------------------------------------
-
-NABoolean TopN::swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo)
-{
- char* tempKey;
- Record* tempRec;
-
-
- tempKey = recKeyOne->key_;
- tempRec = recKeyOne->rec_;
-
-
- recKeyOne->key_ = recKeyTwo->key_;
- recKeyOne->rec_ = recKeyTwo->rec_;
-
-
- recKeyTwo->key_ = tempKey;
- recKeyTwo->rec_ = tempRec;
- return SORT_SUCCESS;
-}
-
-UInt32 TopN::getOverheadPerRecord(void)
-{
- return (sizeof(RecKeyBuffer) + sizeof(Record));
-}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sort/Topn.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/Topn.h b/core/sql/sort/Topn.h
deleted file mode 100644
index a683536..0000000
--- a/core/sql/sort/Topn.h
+++ /dev/null
@@ -1,89 +0,0 @@
-/**********************************************************************
-// @@@ START COPYRIGHT @@@
-//
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-//
-// @@@ END COPYRIGHT @@@
-**********************************************************************/
-#ifndef TOPN_H
-#define TOPN_H
-
-/* -*-C++-*-
-******************************************************************************
-*
-* File: Topn.h
-*
-*
-******************************************************************************
-*/
-
-#include "SortAlgo.h"
-#include "Record.h"
-#include "Const.h"
-#include "NABasicObject.h"
-#include "SortError.h"
-
-
-class SortUtil;
-class ExBMOStats;
-
-
-class TopN : public SortAlgo { //SortAlgo inherits from NABasicObject
-
-public:
-
- TopN(ULng32 recmax,ULng32 sortmaxmem, ULng32 recsize, NABoolean doNotallocRec,
- ULng32 keysize, SortScratchSpace* scratch,NABoolean iterQuickSort,
- CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil);
- ~TopN(void);
-
- Lng32 sortSend(void* rec, ULng32 len, void* tupp);
-
- Lng32 sortClientOutOfMem(void){ return 0;}
-
- Lng32 sortSendEnd();
-
- Lng32 sortReceive(void* rec, ULng32& len);
- Lng32 sortReceive(void*& rec, ULng32& len, void*& tupp);
- UInt32 getOverheadPerRecord(void);
- Lng32 generateInterRuns(){ return 0;}
-
-
-private:
- void buildHeap();
- void satisfyHeap();
- void insertRec(void *rec, ULng32 len, void* tupp);
- void sortHeap();
- void siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom);
- NABoolean swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo);
-
- ULng32 loopIndex_;
- ULng32 recNum_;
- ULng32 allocRunSize_;
- NABoolean isHeapified_;
- RecKeyBuffer insertRecKey_;
- RecKeyBuffer* topNKeys_;
- SortError* sortError_;
- CollHeap* heap_;
- SortUtil* sortUtil_;
- ExBMOStats *bmoStats_;
-};
-
-#endif
-
-
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sqlcomp/DefaultConstants.h
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/DefaultConstants.h b/core/sql/sqlcomp/DefaultConstants.h
index df0b27c..1201a26 100644
--- a/core/sql/sqlcomp/DefaultConstants.h
+++ b/core/sql/sqlcomp/DefaultConstants.h
@@ -1675,7 +1675,7 @@ enum DefaultConstants
DO_RUNTIME_SPACE_OPTIMIZATION,
GEN_SORT_MAX_NUM_BUFFERS,
- GEN_SORT_TOPN_SIZE,
+ GEN_SORT_TOPN,
SORT_ALGO, // Sort algorithm choice
// Not used anymore. OVERRIDE_SYSKEY takes its place.
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/08081bd7/core/sql/sqlcomp/nadefaults.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/nadefaults.cpp b/core/sql/sqlcomp/nadefaults.cpp
index c03d05f..9e6c3eb 100644
--- a/core/sql/sqlcomp/nadefaults.cpp
+++ b/core/sql/sqlcomp/nadefaults.cpp
@@ -1629,7 +1629,7 @@ SDDkwd__(EXE_DIAGNOSTIC_EVENTS, "OFF"),
DDui1__(GEN_SORT_NUM_BUFFERS, "4"),
DDui1__(GEN_SORT_SIZE_DOWN, "2"),
DDui1__(GEN_SORT_SIZE_UP, "1024"),
- DDui___(GEN_SORT_TOPN_SIZE, "0"),
+ DDkwd__(GEN_SORT_TOPN, "ON"),
DDui1__(GEN_SPLB_BUFFER_SIZE, "2"),
DDui1__(GEN_SPLB_NUM_BUFFERS, "1"),
DDui1__(GEN_SPLB_SIZE_DOWN, "2"),
[2/7] incubator-trafodion git commit: [TRAFODION-2259] changes from
feedback, drive TopN sort from GETN
Posted by sa...@apache.org.
[TRAFODION-2259] changes from feedback, drive TopN sort from GETN
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/6e933252
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/6e933252
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/6e933252
Branch: refs/heads/master
Commit: 6e933252c3cb9012b911f47347694bc6ac59298d
Parents: 61cc7b5
Author: Prashant Vasudev <pr...@esgyn.com>
Authored: Thu Oct 6 17:23:52 2016 +0000
Committer: Prashant Vasudev <pr...@esgyn.com>
Committed: Thu Oct 6 17:23:52 2016 +0000
----------------------------------------------------------------------
core/sql/comexe/ComTdbSort.h | 17 +-
core/sql/executor/ex_sort.cpp | 17 +-
core/sql/generator/GenRelMisc.cpp | 4 +-
core/sql/nskgmake/sort/Makefile | 2 +-
core/sql/sort/Record.cpp | 12 +-
core/sql/sort/SortTopN.cpp | 318 +++++++++++++++++++++++++++++
core/sql/sort/SortTopN.h | 89 ++++++++
core/sql/sort/SortUtil.cpp | 84 ++++----
core/sql/sort/SortUtil.h | 2 +-
core/sql/sort/SortUtilCfg.cpp | 1 +
core/sql/sort/SortUtilCfg.h | 8 +-
core/sql/sort/Topn.cpp | 337 -------------------------------
core/sql/sort/Topn.h | 89 --------
core/sql/sqlcomp/DefaultConstants.h | 2 +-
core/sql/sqlcomp/nadefaults.cpp | 2 +-
15 files changed, 483 insertions(+), 501 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/comexe/ComTdbSort.h
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbSort.h b/core/sql/comexe/ComTdbSort.h
index 93a1a81..f73cb6b 100644
--- a/core/sql/comexe/ComTdbSort.h
+++ b/core/sql/comexe/ComTdbSort.h
@@ -168,7 +168,8 @@ protected:
COLLECT_NF_ERRORS_ = 0x0001,
PREFIX_ORDERED_INPUT = 0x0002,
SORT_FROM_TOP = 0x0004,
- USER_SIDETREE_INSERT = 0x0008
+ USER_SIDETREE_INSERT = 0x0008,
+ SORT_TOPN_ENABLE = 0x0010
};
SortOptionsPtr sortOptions_; // 00-07
@@ -204,9 +205,7 @@ protected:
Float32 bmoCitizenshipFactor_; // 68-71
Int32 pMemoryContingencyMB_; // 72-75
UInt16 sortGrowthPercent_; // 76-77
- char filler2_[2]; // 78-79
- ULng32 topNSize_; // 80-83
- char fillersComTdbSort_[12]; // 84-95
+ char fillersComTdbSort_[18]; // 78-95
public:
@@ -307,6 +306,10 @@ public:
NABoolean sortFromTop() { return (flags_ & SORT_FROM_TOP) != 0;}
void setSortFromTop(NABoolean v)
{(v ? flags_ |= SORT_FROM_TOP : flags_ &= ~SORT_FROM_TOP);}
+
+ NABoolean topNSort() { return (flags_ & SORT_TOPN_ENABLE) != 0;}
+ void setTopNSort(NABoolean v)
+ {(v ? flags_ |= SORT_TOPN_ENABLE : flags_ &= ~SORT_TOPN_ENABLE);}
NABoolean userSidetreeInsert()
{
@@ -327,11 +330,7 @@ public:
{ pMemoryContingencyMB_ = mCMB;}
Int32 getMemoryContingencyMB(void)
{ return pMemoryContingencyMB_; }
- void setTopNSize(UInt32 size)
- { topNSize_ = size; }
- ULng32 getTopNSize(void)
- { return topNSize_; }
-
+
void setSortMemEstInMbPerCpu(Float32 s) {sortMemEstInMbPerCpu_=s;}
Float32 getSortMemEstInMbPerCpu() {return sortMemEstInMbPerCpu_;}
Float32 sortGrowthPercent() {return Float32(sortGrowthPercent_/100.0);}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/executor/ex_sort.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_sort.cpp b/core/sql/executor/ex_sort.cpp
index e655abd..82a8fbc 100644
--- a/core/sql/executor/ex_sort.cpp
+++ b/core/sql/executor/ex_sort.cpp
@@ -242,8 +242,8 @@ ExSortTcb::ExSortTcb(const ExSortTdb & sort_tdb,
sortCfg_->setIntermediateScratchCleanup(st->sortOptions_->intermediateScratchCleanup());
sortCfg_->setResizeCifRecord(st->sortOptions_->resizeCifRecord());
sortCfg_->setConsiderBufferDefrag(st->sortOptions_->considerBufferDefrag());
- sortCfg_->setTopNSize(st->getTopNSize());
-
+ sortCfg_->setTopNSort(st->topNSort());
+
switch(st->getOverFlowMode())
{
case SQLCLI_OFM_SSD_TYPE:
@@ -570,6 +570,7 @@ short ExSortTcb::workUp()
{
Lng32 rc = 0;
short workRC = 0;
+ ULng32 topNCount = 0;
// if no parent request, return
if (qparent_.down->isEmpty())
@@ -611,9 +612,13 @@ short ExSortTcb::workUp()
sortDiag_ = NULL; // reset
// LCOV_EXCL_STOP
}
-
- if (sortUtil_->sortInitialize(*sortCfg_) != SORT_SUCCESS)
- {
+
+ if((request == ex_queue::GET_N) &&
+ (pentry_down->downState.requestValue > 0))
+ topNCount = (ULng32)pentry_down->downState.requestValue;
+
+ if (sortUtil_->sortInitialize(*sortCfg_, topNCount) != SORT_SUCCESS)
+ {
// LCOV_EXCL_START
createSortDiags();
pstate.step_ = ExSortTcb::SORT_ERROR;
@@ -1927,7 +1932,7 @@ short ExSortFromTopTcb::work()
// LCOV_EXCL_STOP
}
- if (sortUtil_->sortInitialize(*sortCfg_) != SORT_SUCCESS)
+ if (sortUtil_->sortInitialize(*sortCfg_, 0) != SORT_SUCCESS)
{
// LCOV_EXCL_START
createSortDiags();
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/generator/GenRelMisc.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenRelMisc.cpp b/core/sql/generator/GenRelMisc.cpp
index a12186f..e887bae 100644
--- a/core/sql/generator/GenRelMisc.cpp
+++ b/core/sql/generator/GenRelMisc.cpp
@@ -3224,10 +3224,8 @@ short Sort::generateTdb(Generator * generator,
sort_tdb->setSortFromTop(sortFromTop());
sort_tdb->setOverflowMode(generator->getOverflowMode());
+ sort_tdb->setTopNSort(CmpCommon::getDefault(GEN_SORT_TOPN) == DF_ON);
- sort_tdb->setTopNSize((ULng32)getDefault(GEN_SORT_TOPN_SIZE));
-
-
if (generator->getUserSidetreeInsert())
sort_tdb->setUserSidetreeInsert(TRUE);
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/nskgmake/sort/Makefile
----------------------------------------------------------------------
diff --git a/core/sql/nskgmake/sort/Makefile b/core/sql/nskgmake/sort/Makefile
index b0b7401..64db734 100755
--- a/core/sql/nskgmake/sort/Makefile
+++ b/core/sql/nskgmake/sort/Makefile
@@ -37,7 +37,7 @@ CPPSRC := CommonUtil.cpp \
Statistics.cpp \
TourTree.cpp \
TreeNode.cpp \
- Topn.cpp
+ SortTopN.cpp
CPPSRC += vers_libsort.cpp
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sort/Record.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/Record.cpp b/core/sql/sort/Record.cpp
index 2437575..cf4bf07 100644
--- a/core/sql/sort/Record.cpp
+++ b/core/sql/sort/Record.cpp
@@ -57,12 +57,12 @@ Record::Record(ULng32 size, NABoolean doNotallocRec, CollHeap* heap)
Record::Record(void *rec, ULng32 reclen, void* tupp, CollHeap* heap, SortError* sorterror)
{
- recSize_ = reclen;
- sortError_ = sorterror;
- heap_ = heap;
- tupp_ = tupp;
- allocatedRec_ = FALSE_L;
- rec_ =(char *) rec;
+ recSize_ = reclen;
+ sortError_ = sorterror;
+ heap_ = heap;
+ tupp_ = tupp;
+ allocatedRec_ = FALSE_L;
+ rec_ =(char *) rec;
}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sort/SortTopN.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortTopN.cpp b/core/sql/sort/SortTopN.cpp
new file mode 100644
index 0000000..2e6351b
--- /dev/null
+++ b/core/sql/sort/SortTopN.cpp
@@ -0,0 +1,318 @@
+/**********************************************************************
+// @@@ START COPYRIGHT @@@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// @@@ END COPYRIGHT @@@
+**********************************************************************/
+/* -*-C++-*-
+******************************************************************************
+*
+* File: SortTopN.cpp
+*
+* Description: This file contains the implementation of all member functions
+* of the class TopN.
+*
+* 1. Sort would initially maintain Top N array of elements to being with.
+* 2. Read records into TopN array.
+* 3. Once TopN array is full, heapify the array into max heap. Top node in
+* the heap is always the highest node.
+* 4. Subsequent record read either gets discarded( if greater than top node)
+* or replace top node( if lesser then top node) . if replaced top node,
+* re-balance the heap.
+* 5. Repeat steps 4 until last record is read.
+* 6. sort the final heap using heap sort.
+*******************************************************************************/
+
+#include <iostream>
+#include <fstream>
+
+#ifndef DEBUG
+#undef NDEBUG
+#define NDEBUG
+#endif
+#include "ex_stdh.h"
+#include "SortTopN.h"
+#include "ScratchSpace.h"
+#include "logmxevent.h"
+#include "SortUtil.h"
+#include "ex_ex.h"
+#include "ExStats.h"
+
+//------------------------------------------------------------------------
+// Class Constructor.
+//------------------------------------------------------------------------
+SortTopN::SortTopN(ULng32 runsize, ULng32 sortmaxmem, ULng32 recsize,
+ NABoolean doNotallocRec, ULng32 keysize,
+ SortScratchSpace* scratch, NABoolean iterSort,
+ CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil):
+ SortAlgo(runsize, recsize, doNotallocRec, keysize, scratch, explainNodeId),
+ loopIndex_(0), heap_(heap), sortError_(sorterror),
+ sortUtil_(sortutil)
+{
+ //runsize is TopN size. Fixed.
+ allocRunSize_ = runsize;
+
+ //Actual run size after all elements read.
+ runSize_ = 0;
+
+ isHeapified_ = FALSE;
+
+ topNKeys_ = (RecKeyBuffer *)heap_->allocateMemory(sizeof(RecKeyBuffer) * allocRunSize_);
+
+ // Below asserts useful in debug mode.
+ ex_assert(topNKeys_ != NULL, "Sort: Initial topNKeys_ allocation failed");
+
+ recNum_ = 0;
+ ExOperStats *stat = sortUtil_->config()->getCallingTcb()->getStatsEntry();
+ if (stat)
+ bmoStats_ = stat->castToExBMOStats();
+ else
+ bmoStats_ = NULL;
+ if (bmoStats_)
+ bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
+}
+
+
+SortTopN::~SortTopN(void)
+{
+ if (topNKeys_ != NULL)
+ {
+ for (int i = 0; i < runSize_; i++)
+ topNKeys_[i].rec_->releaseTupp();
+
+ NADELETEBASIC(topNKeys_, heap_);
+ topNKeys_ = NULL;
+ }
+
+ if (bmoStats_)
+ bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
+}
+
+Lng32 SortTopN::sortSend(void *rec, ULng32 len, void* tupp)
+{
+ //if heap not built means, TopN array has more slots
+ //available to fill.
+ if(! isHeapified_)
+ {
+ ex_assert(loopIndex_ >= 0, "TopN::sortSend: loopIndex_ is < 0");
+ ex_assert(loopIndex_ < allocRunSize_, "TopN::sortSend: loopIndex_ > allocRunSize_");
+ ex_assert(rec != NULL, "TopN::sortSend: rec is NULL");
+
+ Record * newRec = new (heap_)Record(rec, len, tupp, heap_, sortError_);
+
+ topNKeys_[loopIndex_].key_ = newRec->extractKey(keySize_,
+ sortUtil_->config()->numberOfBytesForRecordSize());
+ topNKeys_[loopIndex_].rec_ = newRec;
+ if (++loopIndex_ == allocRunSize_)
+ {
+ //Reaching here means, we have filled up the array.
+ //Now heapify the array to start accepting/eliminating new elements from now on.
+
+ //Note lookIndex_ contains the current number of filled elements.
+ buildHeap();
+ }
+ return SORT_SUCCESS;
+ }
+
+ //Reaching here means, heap is already built.
+ //Check if the new rec belongs to this heap by comparing the
+ //new rec key with the root node of the heap ( root node is always the greatest).
+ insertRec(rec, len, tupp);
+ return SORT_SUCCESS;
+ }
+
+
+void SortTopN::buildHeap()
+{
+ if(!isHeapified_)
+ {
+ //loopIndex_ is now <= TopN
+ runSize_ = loopIndex_;
+
+ satisfyHeap();
+
+ isHeapified_ = TRUE;
+ }
+}
+
+//Satisfy Heap makes sure the heap is balanced maxHeap.
+//Note this does not mean heap is sorted. It just makes sure
+//the higher level nodes are greater than lower level nodes.
+//Topmost node or root will be the highest.
+void SortTopN::satisfyHeap()
+{
+ for (int i = (runSize_/2 ); i >= 0; i--)
+ siftDown(topNKeys_, i, runSize_-1);
+}
+
+
+void SortTopN::insertRec(void *rec, ULng32 len, void* tupp)
+{
+ ex_assert(isHeapified_, "TopN::insertRec: not heapified");
+
+ int root = 0; //Always, root node is the zero'th element in array.
+
+ Record * newrec = new (heap_)Record(rec, len, tupp, heap_, sortError_);
+
+ insertRecKey_.key_ = newrec->extractKey(keySize_,
+ sortUtil_->config()->numberOfBytesForRecordSize());
+ insertRecKey_.rec_ = newrec;
+
+ if (compare(topNKeys_[root].key_ ,insertRecKey_.key_) == KEY1_IS_GREATER)
+ {
+ swap( &topNKeys_[root],&insertRecKey_);
+
+
+ //After swap, satisfy or rebalance the heap.
+ satisfyHeap();
+
+ }
+
+ //Once swapped, or not swapped, delete the unneeded record.
+ //This step is very important to discard the unwanted record.
+ //Note releaseTupp() also deletes the tupp allocated in sql
+ //buffer. This makes space for new records read in.
+ insertRecKey_.rec_->releaseTupp();
+ NADELETEBASIC(insertRecKey_.rec_, heap_);
+
+}
+
+Lng32 SortTopN::sortSendEnd()
+{
+ Lng32 retcode = SORT_SUCCESS;
+ ex_assert(loopIndex_ >= 0, "TopN::sortSendEnd: loopIndex_ is < 0");
+
+ buildHeap();
+ sortHeap();
+
+ return retcode;
+}
+
+//----------------------------------------------------------------------
+// Name : sortHeap
+//
+//
+// Description : The heap is already balanced. This step sorts the heap.
+//----------------------------------------------------------------------
+void SortTopN::sortHeap()
+{
+ for (int i = runSize_-1; i >= 1; i--)
+ {
+ swap(&topNKeys_[0],&topNKeys_[i]);
+ siftDown(topNKeys_, 0, i-1);
+ }
+}
+
+Lng32 SortTopN::sortReceive(void *rec, ULng32& len)
+{
+ //This method applicable to overflow records only
+ return SORT_FAILURE;
+}
+
+Lng32 SortTopN::sortReceive(void*& rec, ULng32& len, void*& tupp)
+{
+ if (recNum_ < runSize_)
+ {
+ topNKeys_[recNum_].rec_->getRecordTupp(rec, recSize_, tupp);
+ len = recSize_;
+ recNum_++;
+ }
+ else
+ {
+ len = 0;
+ }
+ return SORT_SUCCESS;
+}
+
+
+//----------------------------------------------------------------------
+// Name : siftDown
+//
+// Parameters : ..
+//
+// Description : Given the root node,rebalances the heap.Child nodes are less
+// value than parent nodes. Top most node or root contains
+// highest value.
+//
+//
+//----------------------------------------------------------------------
+void SortTopN::siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom)
+{
+ Int64 done, maxChild;
+
+ done = 0;
+ while ((root*2 <= bottom) && (!done))
+ {
+ if (root*2 == bottom)
+ maxChild = root * 2;
+ else if (compare(keysToSort[root * 2].key_ ,
+ keysToSort[root * 2 + 1].key_) >= KEY1_IS_GREATER)
+ maxChild = root * 2;
+ else
+ maxChild = root * 2 + 1;
+
+ if (compare(keysToSort[root].key_ ,keysToSort[maxChild].key_) <=KEY1_IS_SMALLER)
+ {
+ swap( &keysToSort[root],&keysToSort[maxChild]);
+ root = maxChild;
+
+ }
+ else
+ done = 1;
+ }
+}
+
+//----------------------------------------------------------------------
+// Name : swap
+//
+// Parameters : ..
+//
+// Description : Swaps two elements from the QuickSort workspace. May
+// consider making this inline rather than a seperate
+// procedure call for performance reasons.
+//
+// Recompareturn Value :
+// SORT_SUCCESS if everything goes on well.
+// SORT_FAILURE if any error encounterd.
+//
+//----------------------------------------------------------------------
+
+NABoolean SortTopN::swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo)
+{
+ char* tempKey;
+ Record* tempRec;
+
+
+ tempKey = recKeyOne->key_;
+ tempRec = recKeyOne->rec_;
+
+
+ recKeyOne->key_ = recKeyTwo->key_;
+ recKeyOne->rec_ = recKeyTwo->rec_;
+
+
+ recKeyTwo->key_ = tempKey;
+ recKeyTwo->rec_ = tempRec;
+ return SORT_SUCCESS;
+}
+
+UInt32 SortTopN::getOverheadPerRecord(void)
+{
+ return (sizeof(RecKeyBuffer) + sizeof(Record));
+}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sort/SortTopN.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortTopN.h b/core/sql/sort/SortTopN.h
new file mode 100644
index 0000000..cd7e4f9
--- /dev/null
+++ b/core/sql/sort/SortTopN.h
@@ -0,0 +1,89 @@
+/**********************************************************************
+// @@@ START COPYRIGHT @@@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// @@@ END COPYRIGHT @@@
+**********************************************************************/
+#ifndef SORTTOPN_H
+#define SORTTOPN_H
+
+/* -*-C++-*-
+******************************************************************************
+*
+* File: SortTopN.h
+*
+*
+******************************************************************************
+*/
+
+#include "SortAlgo.h"
+#include "Record.h"
+#include "Const.h"
+#include "NABasicObject.h"
+#include "SortError.h"
+
+
+class SortUtil;
+class ExBMOStats;
+
+
+class SortTopN : public SortAlgo { //SortAlgo inherits from NABasicObject
+
+public:
+
+ SortTopN(ULng32 recmax,ULng32 sortmaxmem, ULng32 recsize, NABoolean doNotallocRec,
+ ULng32 keysize, SortScratchSpace* scratch,NABoolean iterQuickSort,
+ CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil);
+ ~SortTopN(void);
+
+ Lng32 sortSend(void* rec, ULng32 len, void* tupp);
+
+ Lng32 sortClientOutOfMem(void){ return 0;}
+
+ Lng32 sortSendEnd();
+
+ Lng32 sortReceive(void* rec, ULng32& len);
+ Lng32 sortReceive(void*& rec, ULng32& len, void*& tupp);
+ UInt32 getOverheadPerRecord(void);
+ Lng32 generateInterRuns(){ return 0;}
+
+
+private:
+ void buildHeap();
+ void satisfyHeap();
+ void insertRec(void *rec, ULng32 len, void* tupp);
+ void sortHeap();
+ void siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom);
+ NABoolean swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo);
+
+ ULng32 loopIndex_;
+ ULng32 recNum_;
+ ULng32 allocRunSize_;
+ NABoolean isHeapified_;
+ RecKeyBuffer insertRecKey_;
+ RecKeyBuffer* topNKeys_;
+ SortError* sortError_;
+ CollHeap* heap_;
+ SortUtil* sortUtil_;
+ ExBMOStats *bmoStats_;
+};
+
+#endif
+
+
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sort/SortUtil.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtil.cpp b/core/sql/sort/SortUtil.cpp
index 433e815..4cb11a8 100644
--- a/core/sql/sort/SortUtil.cpp
+++ b/core/sql/sort/SortUtil.cpp
@@ -45,7 +45,7 @@
#include "ex_sort.h"
#include "SortUtil.h"
#include "Qsort.h"
-#include "Topn.h"
+#include "SortTopN.h"
#include "ComCextdecs.h"
#include "logmxevent.h"
#include "ExStats.h"
@@ -101,17 +101,17 @@ NABoolean SortUtil::scratchInitialize(void)
&sortError_,
explainNodeId_,
config_->scratchIOBlockSize_,
- config_->logInfoEvent_,
+ config_->logInfoEvent_,
config_->scratchMgmtOption_
- );
+ );
if (scratch_ == NULL)
{
sortError_.setErrorInfo( EScrNoMemory //sort error
- ,NULL //syserr: the actual FS error
- ,NULL //syserrdetail
- ,"SortUtil::scratchInitialize" //methodname
- );
+ ,NULL //syserr: the actual FS error
+ ,NULL //syserrdetail
+ ,"SortUtil::scratchInitialize" //methodname
+ );
return SORT_FAILURE;
}
@@ -149,7 +149,7 @@ NABoolean SortUtil::scratchInitialize(void)
// SORT_FAILURE if any error encounterd.
//
//----------------------------------------------------------------------
-NABoolean SortUtil::sortInitialize(SortUtilConfig& config)
+NABoolean SortUtil::sortInitialize(SortUtilConfig& config, ULng32 topNSize)
{
//---------------------------------------------------------------
@@ -160,46 +160,46 @@ NABoolean SortUtil::sortInitialize(SortUtilConfig& config)
doCleanUp();
//if topNSize_ is set, then use TopN.
- if(!config.topNSize_)
+ if(config.topNSort_ && topNSize)
{
sortAlgo_ =
- new (config.heapAddr_) Qsort(config.runSize_,
- config.maxMem_,
- config.recSize_,
- config.sortType_.doNotAllocRec_,
- config.keySize_,
- scratch_,
- TRUE,
- config.heapAddr_,
- &sortError_,
- explainNodeId_,
- this);
+ new (config.heapAddr_) SortTopN(topNSize,
+ config.maxMem_,
+ config.recSize_,
+ config.sortType_.doNotAllocRec_,
+ config.keySize_,
+ scratch_,
+ TRUE,
+ config.heapAddr_,
+ &sortError_,
+ explainNodeId_,
+ this);
+
}
else
{
- sortAlgo_ =
- new (config.heapAddr_) TopN(config.topNSize_,
- config.maxMem_,
- config.recSize_,
- config.sortType_.doNotAllocRec_,
- config.keySize_,
- scratch_,
- TRUE,
- config.heapAddr_,
- &sortError_,
- explainNodeId_,
- this);
+ sortAlgo_ =
+ new (config.heapAddr_) Qsort(config.runSize_,
+ config.maxMem_,
+ config.recSize_,
+ config.sortType_.doNotAllocRec_,
+ config.keySize_,
+ scratch_,
+ TRUE,
+ config.heapAddr_,
+ &sortError_,
+ explainNodeId_,
+ this);
+ }
+ if (sortAlgo_ == NULL)
+ {
+ sortError_.setErrorInfo(EScrNoMemory //sort error
+ ,NULL //syserr: the actual FS error
+ ,NULL //syserrdetail
+ ,"SortUtil::sortInitialize" //methodname
+ );
+ return SORT_FAILURE;
}
- if (sortAlgo_ == NULL)
- {
- sortError_.setErrorInfo( EScrNoMemory //sort error
- ,NULL //syserr: the actual FS error
- ,NULL //syserrdetail
- ,"SortUtil::sortInitialize" //methodname
- );
- return SORT_FAILURE;
- }
-
//The maximum memory that sort can consume is governed by three parameters.
//(config.maxNumBuffers_ * sorttdb.maxbufferSize) + config..maxMem_.
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sort/SortUtil.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtil.h b/core/sql/sort/SortUtil.h
index 6eeb490..82b86f7 100644
--- a/core/sql/sort/SortUtil.h
+++ b/core/sql/sort/SortUtil.h
@@ -62,7 +62,7 @@ public:
SortUtil(Lng32 explainNodeId);
~SortUtil();
- NABoolean sortInitialize(SortUtilConfig& config);
+ NABoolean sortInitialize(SortUtilConfig& config, ULng32 topNSize);
NABoolean sortEnd(void);
Lng32 sortSend(void* record, ULng32 len, void* tupp);
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sort/SortUtilCfg.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtilCfg.cpp b/core/sql/sort/SortUtilCfg.cpp
index 25bca77..591b645 100644
--- a/core/sql/sort/SortUtilCfg.cpp
+++ b/core/sql/sort/SortUtilCfg.cpp
@@ -89,6 +89,7 @@ SortUtilConfig::SortUtilConfig(CollHeap* heap)
sortMemEstInMbPerCpu_ = 0;
bmoMaxMemThresholdMB_ = 0;
intermediateScratchCleanup_ = TRUE;
+ topNSort_ = FALSE;
}
SortUtilConfig::~SortUtilConfig(void)
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sort/SortUtilCfg.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtilCfg.h b/core/sql/sort/SortUtilCfg.h
index be7ad9a..1bc8716 100644
--- a/core/sql/sort/SortUtilCfg.h
+++ b/core/sql/sort/SortUtilCfg.h
@@ -148,10 +148,6 @@ public:
{
return numEsps_;
}
- void setTopNSize(ULng32 size)
- {
- topNSize_ = size;
- }
void setEventHandler(ExSubtask *eh)
{
ioEventHandler_ = eh;
@@ -280,6 +276,8 @@ public:
void setIntermediateScratchCleanup(NABoolean v)
{ intermediateScratchCleanup_ = v;}
NABoolean intermediateScratchCleanup(){return intermediateScratchCleanup_;}
+ void setTopNSort(NABoolean v)
+ { topNSort_ = v; }
friend class SortUtil;
@@ -318,7 +316,7 @@ private:
ULng32 mergeOrder_; // Need to modify this to do automatically.
ULng32 minMem_; // Minimum sort heap memory
ULng32 maxMem_; // Maximum sort heap memory
- ULng32 topNSize_; // TopN size set by the executor
+ NABoolean topNSort_; // TopN sorting enable/disable
ULng32 runSizeIncr_; // unused :how much to increment the run size by.
ULng32 maxNumBuffers_; // Max buffer space as set by the compiler
unsigned short scratchThreshold_; // percent of disk usage after which a disk will be discarded for use
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sort/Topn.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/Topn.cpp b/core/sql/sort/Topn.cpp
deleted file mode 100644
index 417a795..0000000
--- a/core/sql/sort/Topn.cpp
+++ /dev/null
@@ -1,337 +0,0 @@
-/**********************************************************************
-// @@@ START COPYRIGHT @@@
-//
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-//
-// @@@ END COPYRIGHT @@@
-**********************************************************************/
-/* -*-C++-*-
-******************************************************************************
-*
-* File: TopN.cpp
-*
-* Description: This file contains the implementation of all member functions
-* of the class TopN.
-*
-* 1. Sort would initially maintain Top N array of elements to being with.
-* 2. Read records into TopN array.
-* 3. Once TopN array is full, heapify the array into max heap. Top node in the heap is always the highest node.
-* 4. Subsequent record read either gets discarded( if greater than top node) or replace top node( if lesser then top node) . if replaced top node, re-balance the heap.
-* 5. Repeat steps 4 until last record is read.
-* 6. sort the final heap using heap sort.
-*******************************************************************************/
-
-#include <iostream>
-#include <fstream>
-
-#ifndef DEBUG
-#undef NDEBUG
-#define NDEBUG
-#endif
-#include "ex_stdh.h"
-#include "Topn.h"
-#include "ScratchSpace.h"
-#include "logmxevent.h"
-#include "SortUtil.h"
-#include "ex_ex.h"
-#include "ExStats.h"
-
-//------------------------------------------------------------------------
-// Class Constructor.
-//------------------------------------------------------------------------
-TopN::TopN(ULng32 runsize, ULng32 sortmaxmem, ULng32 recsize,
- NABoolean doNotallocRec, ULng32 keysize,
- SortScratchSpace* scratch, NABoolean iterSort,
- CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil):
- SortAlgo(runsize, recsize, doNotallocRec, keysize, scratch, explainNodeId),
- loopIndex_(0), heap_(heap), sortError_(sorterror),
- sortUtil_(sortutil)
-{
- //runsize is TopN size. Fixed.
- allocRunSize_ = runsize;
-
- //Actual run size after all elements read.
- runSize_ = 0;
-
- isHeapified_ = FALSE;
-
- //allocateMemory failureIsFatal is defaulted to TRUE means allocation failure results in
- //longjump to jump handler defined in ex_sort.cpp. Only applicable on NSK.
- topNKeys_ = (RecKeyBuffer *)heap_->allocateMemory(sizeof(RecKeyBuffer) * allocRunSize_);
-
- // Below asserts useful in debug mode. Also asserts if longjmp did not happen.
- //ex_assert(rootRecord_!= NULL, "Sort: Initial rootRecord_ allocation failed");
- ex_assert(topNKeys_ != NULL, "Sort: Initial topNKeys_ allocation failed");
-
- recNum_ = 0;
- ExOperStats *stat = sortUtil_->config()->getCallingTcb()->getStatsEntry();
- if (stat)
- bmoStats_ = stat->castToExBMOStats();
- else
- bmoStats_ = NULL;
- if (bmoStats_)
- bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
-}
-
-
-//------------------------------------------------------------------------
-// Class Destructor: Delete all the heap space pointed by pointers in Qsort
-//------------------------------------------------------------------------
-TopN::~TopN(void)
-{
-
- for (int i = 0; i < runSize_; i++)
- topNKeys_[i].rec_->releaseTupp();
-
-
- if (topNKeys_ != NULL) {
- NADELETEBASIC(topNKeys_, heap_);
- topNKeys_ = NULL;
- }
- if (bmoStats_)
- bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
-
-}
-
-Lng32 TopN::sortSend(void *rec, ULng32 len, void* tupp)
-{
- //if heap not built means, TopN array has more slots
- //available to fill.
- if(! isHeapified_)
- {
- ex_assert(loopIndex_ >= 0, "TopN::sortSend: loopIndex_ is < 0");
- ex_assert(loopIndex_ < allocRunSize_, "TopN::sortSend: loopIndex_ > allocRunSize_");
- ex_assert(rec != NULL, "TopN::sortSend: rec is NULL");
-
- Record * insertRec = new Record(rec, len, tupp, heap_, sortError_);
-
- topNKeys_[loopIndex_].key_ = insertRec->extractKey(keySize_, sortUtil_->config()->numberOfBytesForRecordSize());
- topNKeys_[loopIndex_].rec_ = insertRec;
- if (++loopIndex_ == allocRunSize_)
- {
- //Reaching here means, we have filled up the array.
- //Now heapify the array to start accepting/eliminating new elements from now on.
-
- //Note lookIndex_ contains the current number of filled elements.
-
- buildHeap();
- }
- return SORT_SUCCESS;
- }
-
- //Reaching here means, heap is already build.
- //Check if the new rec belongs to this heap by comparing the
- //new rec key with the root node of the heap ( root node is always the greatest).
- insertRec(rec, len, tupp);
- return SORT_SUCCESS;
- }
-
-
-void TopN::buildHeap()
-{
- if(!isHeapified_)
- {
- //loopIndex_ is now <= TopN
- runSize_ = loopIndex_;
-
- satisfyHeap();
-
- isHeapified_ = TRUE;
- }
-}
-
-//Satisfy Heap makes sure the heap is balanced maxHeap.
-//Note this does not mean heap is sorted. It just makes sure
-//the higher level nodes are greater than lower level nodes.
-//Topmost node or root will be the highest.
-void TopN::satisfyHeap()
-{
- for (int i = (runSize_/2 ); i >= 0; i--)
- siftDown(topNKeys_, i, runSize_-1);
-}
-
-
-void TopN::insertRec(void *rec, ULng32 len, void* tupp)
-{
- ex_assert(isHeapified_, "TopN::insertRec: not heapified");
-
- int root = 0; //Always, root node is the zero'th element in array.
-
- Record * insertRec = new Record(rec, len, tupp, heap_, sortError_);
- insertRecKey_.key_ = insertRec->extractKey(keySize_, sortUtil_->config()->numberOfBytesForRecordSize());
- insertRecKey_.rec_ = insertRec;
-
- if (compare(topNKeys_[root].key_ ,insertRecKey_.key_) == KEY1_IS_GREATER)
- {
- swap( &topNKeys_[root],&insertRecKey_);
-
-
- //After swap, satisfy or rebalance the heap.
- satisfyHeap();
-
- }
-
- //Once swapped, or not swapped, delete the unneeded record.
- //This step is very important to discard the unwanted record.
- //Note releaseTupp() also deletes the tupp allocated in sql
- //buffer. This makes space for new records read in.
- insertRecKey_.rec_->releaseTupp();
- delete insertRecKey_.rec_;
-
-}
-
-Lng32 TopN::sortSendEnd()
-{
- Lng32 retcode = SORT_SUCCESS;
- ex_assert(loopIndex_ >= 0, "TopN::sortSendEnd: loopIndex_ is < 0");
-
- buildHeap();
- sortHeap();
-
- return retcode;
-}
-
-void TopN::sortHeap()
-{
- for (int i = runSize_-1; i >= 1; i--)
- {
- swap(&topNKeys_[0],&topNKeys_[i]);
- siftDown(topNKeys_, 0, i-1);
- }
-}
-
-Lng32 TopN::sortReceive(void *rec, ULng32& len)
-{
- return SORT_FAILURE;
-}
-
-Lng32 TopN::sortReceive(void*& rec, ULng32& len, void*& tupp)
-{
- //---------------------------------------------------------------
- // We use Qsort to receive records only in case of internal sort
- // for merging.
- //---------------------------------------------------------------
- if (recNum_ < runSize_) {
- topNKeys_[recNum_].rec_->getRecordTupp(rec, recSize_, tupp);
- len = recSize_;
- recNum_++;
- }
- else {
- len = 0;
- }
-
- return SORT_SUCCESS;
-}
-/*
-//----------------------------------------------------------------------
-// Name : heapSort
-//
-// Parameters : ...
-//
-// Description : This member function implements the heap sort
-// Return Value :
-// SORT_SUCCESS if everything goes on well.
-// SORT_FAILURE if any error encounterd.
-//
-// NOTE: For this implementation no extra buffer is required. It is done all
-// in place. This is the algorithm used if SORT_ITERATIVE_ALGO is set.
-// requirement. If the performance of heapSort is not as good we may switch
-// back to quickSort or iterativeQuickSort.
-
-//----------------------------------------------------------------------
-void TopN::heapSort(RecKeyBuffer keysToSort[], Int64 runsize)
-{
- Int64 i;
-
- for (i = (runsize/2 ); i >= 0; i--)
- siftDown(keysToSort, i, runsize-1);
-
- for (i = runsize-1; i >= 1; i--)
- {
-
- swap(&keysToSort[0],&keysToSort[i]);
- siftDown(keysToSort, 0, i-1);
- }
-}
-*/
-
-void TopN::siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom)
-{
- Int64 done, maxChild;
-
- done = 0;
- while ((root*2 <= bottom) && (!done))
- {
- if (root*2 == bottom)
- maxChild = root * 2;
- else if (compare(keysToSort[root * 2].key_ ,
- keysToSort[root * 2 + 1].key_) >= KEY1_IS_GREATER)
- maxChild = root * 2;
- else
- maxChild = root * 2 + 1;
-
- if (compare(keysToSort[root].key_ ,keysToSort[maxChild].key_) <=KEY1_IS_SMALLER)
- {
-
- swap( &keysToSort[root],&keysToSort[maxChild]);
- root = maxChild;
-
- }
- else
- done = 1;
- }
-}
-
-//----------------------------------------------------------------------
-// Name : swap
-//
-// Parameters : ..
-//
-// Description : Swaps two elements from the QuickSort workspace. May
-// consider making this inline rather than a seperate
-// procedure call for performance reasons.
-//
-// Recompareturn Value :
-// SORT_SUCCESS if everything goes on well.
-// SORT_FAILURE if any error encounterd.
-//
-//----------------------------------------------------------------------
-
-NABoolean TopN::swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo)
-{
- char* tempKey;
- Record* tempRec;
-
-
- tempKey = recKeyOne->key_;
- tempRec = recKeyOne->rec_;
-
-
- recKeyOne->key_ = recKeyTwo->key_;
- recKeyOne->rec_ = recKeyTwo->rec_;
-
-
- recKeyTwo->key_ = tempKey;
- recKeyTwo->rec_ = tempRec;
- return SORT_SUCCESS;
-}
-
-UInt32 TopN::getOverheadPerRecord(void)
-{
- return (sizeof(RecKeyBuffer) + sizeof(Record));
-}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sort/Topn.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/Topn.h b/core/sql/sort/Topn.h
deleted file mode 100644
index a683536..0000000
--- a/core/sql/sort/Topn.h
+++ /dev/null
@@ -1,89 +0,0 @@
-/**********************************************************************
-// @@@ START COPYRIGHT @@@
-//
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-//
-// @@@ END COPYRIGHT @@@
-**********************************************************************/
-#ifndef TOPN_H
-#define TOPN_H
-
-/* -*-C++-*-
-******************************************************************************
-*
-* File: Topn.h
-*
-*
-******************************************************************************
-*/
-
-#include "SortAlgo.h"
-#include "Record.h"
-#include "Const.h"
-#include "NABasicObject.h"
-#include "SortError.h"
-
-
-class SortUtil;
-class ExBMOStats;
-
-
-class TopN : public SortAlgo { //SortAlgo inherits from NABasicObject
-
-public:
-
- TopN(ULng32 recmax,ULng32 sortmaxmem, ULng32 recsize, NABoolean doNotallocRec,
- ULng32 keysize, SortScratchSpace* scratch,NABoolean iterQuickSort,
- CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil);
- ~TopN(void);
-
- Lng32 sortSend(void* rec, ULng32 len, void* tupp);
-
- Lng32 sortClientOutOfMem(void){ return 0;}
-
- Lng32 sortSendEnd();
-
- Lng32 sortReceive(void* rec, ULng32& len);
- Lng32 sortReceive(void*& rec, ULng32& len, void*& tupp);
- UInt32 getOverheadPerRecord(void);
- Lng32 generateInterRuns(){ return 0;}
-
-
-private:
- void buildHeap();
- void satisfyHeap();
- void insertRec(void *rec, ULng32 len, void* tupp);
- void sortHeap();
- void siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom);
- NABoolean swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo);
-
- ULng32 loopIndex_;
- ULng32 recNum_;
- ULng32 allocRunSize_;
- NABoolean isHeapified_;
- RecKeyBuffer insertRecKey_;
- RecKeyBuffer* topNKeys_;
- SortError* sortError_;
- CollHeap* heap_;
- SortUtil* sortUtil_;
- ExBMOStats *bmoStats_;
-};
-
-#endif
-
-
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sqlcomp/DefaultConstants.h
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/DefaultConstants.h b/core/sql/sqlcomp/DefaultConstants.h
index 69078f9..340b653 100644
--- a/core/sql/sqlcomp/DefaultConstants.h
+++ b/core/sql/sqlcomp/DefaultConstants.h
@@ -1676,7 +1676,7 @@ enum DefaultConstants
DO_RUNTIME_SPACE_OPTIMIZATION,
GEN_SORT_MAX_NUM_BUFFERS,
- GEN_SORT_TOPN_SIZE,
+ GEN_SORT_TOPN,
SORT_ALGO, // Sort algorithm choice
// Not used anymore. OVERRIDE_SYSKEY takes its place.
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/6e933252/core/sql/sqlcomp/nadefaults.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/nadefaults.cpp b/core/sql/sqlcomp/nadefaults.cpp
index 149cca2..3fcc8f9 100644
--- a/core/sql/sqlcomp/nadefaults.cpp
+++ b/core/sql/sqlcomp/nadefaults.cpp
@@ -1629,7 +1629,7 @@ SDDkwd__(EXE_DIAGNOSTIC_EVENTS, "OFF"),
DDui1__(GEN_SORT_NUM_BUFFERS, "4"),
DDui1__(GEN_SORT_SIZE_DOWN, "2"),
DDui1__(GEN_SORT_SIZE_UP, "1024"),
- DDui___(GEN_SORT_TOPN_SIZE, "0"),
+ DDkwd__(GEN_SORT_TOPN, "ON"),
DDui1__(GEN_SPLB_BUFFER_SIZE, "2"),
DDui1__(GEN_SPLB_NUM_BUFFERS, "1"),
DDui1__(GEN_SPLB_SIZE_DOWN, "2"),
[3/7] incubator-trafodion git commit: [TRAFODION-2259] TopN sort
changes. This includes first of changes related to sort implementation in
executor. cqd gen_sort_topn_size 'N' forces sort to use topn. Subsequent
changes in compiler will be able to push d
Posted by sa...@apache.org.
[TRAFODION-2259] TopN sort changes.
This includes first of changes related to sort implementation in executor.
cqd gen_sort_topn_size 'N' forces sort to use topn.
Subsequent changes in compiler will be able to push down topn to sort.
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/3cd201a6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/3cd201a6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/3cd201a6
Branch: refs/heads/master
Commit: 3cd201a6eb98ec412e33af44f106b69b215e2dcf
Parents: 12f602c
Author: Prashant Vasudev <pr...@esgyn.com>
Authored: Tue Oct 4 05:45:43 2016 +0000
Committer: Prashant Vasudev <pr...@esgyn.com>
Committed: Mon Oct 10 17:49:34 2016 +0000
----------------------------------------------------------------------
core/sql/comexe/ComTdbSort.h | 9 +-
core/sql/executor/ex_sort.cpp | 1 +
core/sql/generator/GenRelMisc.cpp | 3 +
core/sql/nskgmake/sort/Makefile | 3 +-
core/sql/sort/Qsort.h | 5 +-
core/sql/sort/Record.cpp | 14 +-
core/sql/sort/Record.h | 8 +
core/sql/sort/SortUtil.cpp | 22 +-
core/sql/sort/SortUtilCfg.cpp | 42 ----
core/sql/sort/SortUtilCfg.h | 13 +-
core/sql/sort/Topn.cpp | 337 +++++++++++++++++++++++++++++++
core/sql/sort/Topn.h | 89 ++++++++
core/sql/sqlcomp/DefaultConstants.h | 1 +
core/sql/sqlcomp/nadefaults.cpp | 1 +
14 files changed, 490 insertions(+), 58 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/comexe/ComTdbSort.h
----------------------------------------------------------------------
diff --git a/core/sql/comexe/ComTdbSort.h b/core/sql/comexe/ComTdbSort.h
index 5ddc950..93a1a81 100644
--- a/core/sql/comexe/ComTdbSort.h
+++ b/core/sql/comexe/ComTdbSort.h
@@ -204,8 +204,9 @@ protected:
Float32 bmoCitizenshipFactor_; // 68-71
Int32 pMemoryContingencyMB_; // 72-75
UInt16 sortGrowthPercent_; // 76-77
-
- char fillersComTdbSort_[18]; // 78-95
+ char filler2_[2]; // 78-79
+ ULng32 topNSize_; // 80-83
+ char fillersComTdbSort_[12]; // 84-95
public:
@@ -326,6 +327,10 @@ public:
{ pMemoryContingencyMB_ = mCMB;}
Int32 getMemoryContingencyMB(void)
{ return pMemoryContingencyMB_; }
+ void setTopNSize(UInt32 size)
+ { topNSize_ = size; }
+ ULng32 getTopNSize(void)
+ { return topNSize_; }
void setSortMemEstInMbPerCpu(Float32 s) {sortMemEstInMbPerCpu_=s;}
Float32 getSortMemEstInMbPerCpu() {return sortMemEstInMbPerCpu_;}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/executor/ex_sort.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/ex_sort.cpp b/core/sql/executor/ex_sort.cpp
index c200737..e655abd 100644
--- a/core/sql/executor/ex_sort.cpp
+++ b/core/sql/executor/ex_sort.cpp
@@ -242,6 +242,7 @@ ExSortTcb::ExSortTcb(const ExSortTdb & sort_tdb,
sortCfg_->setIntermediateScratchCleanup(st->sortOptions_->intermediateScratchCleanup());
sortCfg_->setResizeCifRecord(st->sortOptions_->resizeCifRecord());
sortCfg_->setConsiderBufferDefrag(st->sortOptions_->considerBufferDefrag());
+ sortCfg_->setTopNSize(st->getTopNSize());
switch(st->getOverFlowMode())
{
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/generator/GenRelMisc.cpp
----------------------------------------------------------------------
diff --git a/core/sql/generator/GenRelMisc.cpp b/core/sql/generator/GenRelMisc.cpp
index 6b4b074..4db4711 100644
--- a/core/sql/generator/GenRelMisc.cpp
+++ b/core/sql/generator/GenRelMisc.cpp
@@ -3184,6 +3184,9 @@ short Sort::generateTdb(Generator * generator,
sort_tdb->setSortFromTop(sortFromTop());
sort_tdb->setOverflowMode(generator->getOverflowMode());
+
+ sort_tdb->setTopNSize((ULng32)getDefault(GEN_SORT_TOPN_SIZE));
+
if (generator->getUserSidetreeInsert())
sort_tdb->setUserSidetreeInsert(TRUE);
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/nskgmake/sort/Makefile
----------------------------------------------------------------------
diff --git a/core/sql/nskgmake/sort/Makefile b/core/sql/nskgmake/sort/Makefile
index 9aa1504..b0b7401 100755
--- a/core/sql/nskgmake/sort/Makefile
+++ b/core/sql/nskgmake/sort/Makefile
@@ -36,7 +36,8 @@ CPPSRC := CommonUtil.cpp \
SortUtilCfg.cpp \
Statistics.cpp \
TourTree.cpp \
- TreeNode.cpp
+ TreeNode.cpp \
+ Topn.cpp
CPPSRC += vers_libsort.cpp
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/sort/Qsort.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/Qsort.h b/core/sql/sort/Qsort.h
index 00aeb3f..af1c7e6 100644
--- a/core/sql/sort/Qsort.h
+++ b/core/sql/sort/Qsort.h
@@ -112,10 +112,7 @@ class ExBMOStats;
// to be used for quicksort.
//----------------------------------------------------------------------
-struct RecKeyBuffer {
- char* key_;
- Record* rec_;
-};
+
void heapSort(RecKeyBuffer keysToSort[], Int32 runsize);
void siftDown(RecKeyBuffer keysToSort[], Int32 root, Int32 bottom);
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/sort/Record.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/Record.cpp b/core/sql/sort/Record.cpp
index 4184d69..2437575 100644
--- a/core/sql/sort/Record.cpp
+++ b/core/sql/sort/Record.cpp
@@ -55,11 +55,23 @@ Record::Record(ULng32 size, NABoolean doNotallocRec, CollHeap* heap)
}
}
+Record::Record(void *rec, ULng32 reclen, void* tupp, CollHeap* heap, SortError* sorterror)
+{
+ recSize_ = reclen;
+ sortError_ = sorterror;
+ heap_ = heap;
+ tupp_ = tupp;
+ allocatedRec_ = FALSE_L;
+ rec_ =(char *) rec;
+}
+
+
//----------------------------------------------------------------------
// Record Destructor.
//----------------------------------------------------------------------
Record::~Record(void)
-{
+{
+
if (allocatedRec_ && rec_ != NULL) {
NADELETEBASIC(rec_, heap_);
rec_ = NULL;
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/sort/Record.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/Record.h b/core/sql/sort/Record.h
index 4b66f63..8928d45 100644
--- a/core/sql/sort/Record.h
+++ b/core/sql/sort/Record.h
@@ -47,11 +47,19 @@
#include "NABasicObject.h"
#include "SortError.h"
+class Record;
+
+struct RecKeyBuffer {
+ char* key_;
+ Record* rec_;
+ };
+
class Record {
public :
Record();
Record(ULng32 size, NABoolean doNotallocRec, CollHeap* heap);
+ Record(void *rec, ULng32 reclen, void* tupp, CollHeap* heap, SortError* sorterror);
~Record(void);
void initialize(ULng32 recsize, NABoolean doNotallocRec,
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/sort/SortUtil.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtil.cpp b/core/sql/sort/SortUtil.cpp
index 64fb793..433e815 100644
--- a/core/sql/sort/SortUtil.cpp
+++ b/core/sql/sort/SortUtil.cpp
@@ -45,6 +45,7 @@
#include "ex_sort.h"
#include "SortUtil.h"
#include "Qsort.h"
+#include "Topn.h"
#include "ComCextdecs.h"
#include "logmxevent.h"
#include "ExStats.h"
@@ -158,7 +159,10 @@ NABoolean SortUtil::sortInitialize(SortUtilConfig& config)
//---------------------------------------------------------------
doCleanUp();
- sortAlgo_ =
+ //if topNSize_ is set, then use TopN.
+ if(!config.topNSize_)
+ {
+ sortAlgo_ =
new (config.heapAddr_) Qsort(config.runSize_,
config.maxMem_,
config.recSize_,
@@ -170,6 +174,22 @@ NABoolean SortUtil::sortInitialize(SortUtilConfig& config)
&sortError_,
explainNodeId_,
this);
+ }
+ else
+ {
+ sortAlgo_ =
+ new (config.heapAddr_) TopN(config.topNSize_,
+ config.maxMem_,
+ config.recSize_,
+ config.sortType_.doNotAllocRec_,
+ config.keySize_,
+ scratch_,
+ TRUE,
+ config.heapAddr_,
+ &sortError_,
+ explainNodeId_,
+ this);
+ }
if (sortAlgo_ == NULL)
{
sortError_.setErrorInfo( EScrNoMemory //sort error
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/sort/SortUtilCfg.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtilCfg.cpp b/core/sql/sort/SortUtilCfg.cpp
index 81ed8ab..25bca77 100644
--- a/core/sql/sort/SortUtilCfg.cpp
+++ b/core/sql/sort/SortUtilCfg.cpp
@@ -225,48 +225,6 @@ NABoolean SortUtilConfig::setKeyInfo(ULng32 keysize)
return SORT_SUCCESS;
}
-
-
-//----------------------------------------------------------------------
-// Name : setTemps
-//
-// Parameters : ...
-//
-// Description :
-//
-// Return Value :
-// SORT_SUCCESS if everything goes on well.
-// SORT_FAILURE if any error encounterd.
-//
-//----------------------------------------------------------------------
-NABoolean SortUtilConfig::setTemps(ULng32 runsize=100L,
- ULng32 mergeorder=10L)
-{
- runSize_ = runsize;
- mergeOrder_ = mergeorder;
-
- return SORT_SUCCESS;
-}
-
-
-//----------------------------------------------------------------------
-// Name : getTemps
-//
-// Parameters : ...
-//
-// Description :
-//
-// Return Value :
-// SORT_SUCCESS if everything goes on well.
-// SORT_FAILURE if any error encounterd.
-//
-//----------------------------------------------------------------------
-NABoolean SortUtilConfig::getTemps() const
-{
- return SORT_SUCCESS;
-}
-
-
void SortUtilConfig::setUseBuffered(NABoolean torf)
{
useBufferedWrites_ = torf;
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/sort/SortUtilCfg.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/SortUtilCfg.h b/core/sql/sort/SortUtilCfg.h
index 3a0e946..be7ad9a 100644
--- a/core/sql/sort/SortUtilCfg.h
+++ b/core/sql/sort/SortUtilCfg.h
@@ -87,11 +87,7 @@ public:
ULng32 getRecSize() const;
NABoolean setKeyInfo(ULng32 keysize);
-
-
- NABoolean setTemps(ULng32 runsize, ULng32 mergesize);
- NABoolean getTemps() const;
-
+
void setUseBuffered(NABoolean torf);
NABoolean getUseBuffered() ;
@@ -152,7 +148,10 @@ public:
{
return numEsps_;
}
-
+ void setTopNSize(ULng32 size)
+ {
+ topNSize_ = size;
+ }
void setEventHandler(ExSubtask *eh)
{
ioEventHandler_ = eh;
@@ -319,7 +318,7 @@ private:
ULng32 mergeOrder_; // Need to modify this to do automatically.
ULng32 minMem_; // Minimum sort heap memory
ULng32 maxMem_; // Maximum sort heap memory
- ULng32 initialRunSize_; // unused :to be set by the executor
+ ULng32 topNSize_; // TopN size set by the executor
ULng32 runSizeIncr_; // unused :how much to increment the run size by.
ULng32 maxNumBuffers_; // Max buffer space as set by the compiler
unsigned short scratchThreshold_; // percent of disk usage after which a disk will be discarded for use
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/sort/Topn.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sort/Topn.cpp b/core/sql/sort/Topn.cpp
new file mode 100644
index 0000000..417a795
--- /dev/null
+++ b/core/sql/sort/Topn.cpp
@@ -0,0 +1,337 @@
+/**********************************************************************
+// @@@ START COPYRIGHT @@@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// @@@ END COPYRIGHT @@@
+**********************************************************************/
+/* -*-C++-*-
+******************************************************************************
+*
+* File: TopN.cpp
+*
+* Description: This file contains the implementation of all member functions
+* of the class TopN.
+*
+* 1. Sort would initially maintain Top N array of elements to being with.
+* 2. Read records into TopN array.
+* 3. Once TopN array is full, heapify the array into max heap. Top node in the heap is always the highest node.
+* 4. Subsequent record read either gets discarded( if greater than top node) or replace top node( if lesser then top node) . if replaced top node, re-balance the heap.
+* 5. Repeat steps 4 until last record is read.
+* 6. sort the final heap using heap sort.
+*******************************************************************************/
+
+#include <iostream>
+#include <fstream>
+
+#ifndef DEBUG
+#undef NDEBUG
+#define NDEBUG
+#endif
+#include "ex_stdh.h"
+#include "Topn.h"
+#include "ScratchSpace.h"
+#include "logmxevent.h"
+#include "SortUtil.h"
+#include "ex_ex.h"
+#include "ExStats.h"
+
+//------------------------------------------------------------------------
+// Class Constructor.
+//------------------------------------------------------------------------
+TopN::TopN(ULng32 runsize, ULng32 sortmaxmem, ULng32 recsize,
+ NABoolean doNotallocRec, ULng32 keysize,
+ SortScratchSpace* scratch, NABoolean iterSort,
+ CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil):
+ SortAlgo(runsize, recsize, doNotallocRec, keysize, scratch, explainNodeId),
+ loopIndex_(0), heap_(heap), sortError_(sorterror),
+ sortUtil_(sortutil)
+{
+ //runsize is TopN size. Fixed.
+ allocRunSize_ = runsize;
+
+ //Actual run size after all elements read.
+ runSize_ = 0;
+
+ isHeapified_ = FALSE;
+
+ //allocateMemory failureIsFatal is defaulted to TRUE means allocation failure results in
+ //longjump to jump handler defined in ex_sort.cpp. Only applicable on NSK.
+ topNKeys_ = (RecKeyBuffer *)heap_->allocateMemory(sizeof(RecKeyBuffer) * allocRunSize_);
+
+ // Below asserts useful in debug mode. Also asserts if longjmp did not happen.
+ //ex_assert(rootRecord_!= NULL, "Sort: Initial rootRecord_ allocation failed");
+ ex_assert(topNKeys_ != NULL, "Sort: Initial topNKeys_ allocation failed");
+
+ recNum_ = 0;
+ ExOperStats *stat = sortUtil_->config()->getCallingTcb()->getStatsEntry();
+ if (stat)
+ bmoStats_ = stat->castToExBMOStats();
+ else
+ bmoStats_ = NULL;
+ if (bmoStats_)
+ bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
+}
+
+
+//------------------------------------------------------------------------
+// Class Destructor: Delete all the heap space pointed by pointers in Qsort
+//------------------------------------------------------------------------
+TopN::~TopN(void)
+{
+
+ for (int i = 0; i < runSize_; i++)
+ topNKeys_[i].rec_->releaseTupp();
+
+
+ if (topNKeys_ != NULL) {
+ NADELETEBASIC(topNKeys_, heap_);
+ topNKeys_ = NULL;
+ }
+ if (bmoStats_)
+ bmoStats_->updateBMOHeapUsage((NAHeap *)heap_);
+
+}
+
+Lng32 TopN::sortSend(void *rec, ULng32 len, void* tupp)
+{
+ //if heap not built means, TopN array has more slots
+ //available to fill.
+ if(! isHeapified_)
+ {
+ ex_assert(loopIndex_ >= 0, "TopN::sortSend: loopIndex_ is < 0");
+ ex_assert(loopIndex_ < allocRunSize_, "TopN::sortSend: loopIndex_ > allocRunSize_");
+ ex_assert(rec != NULL, "TopN::sortSend: rec is NULL");
+
+ Record * insertRec = new Record(rec, len, tupp, heap_, sortError_);
+
+ topNKeys_[loopIndex_].key_ = insertRec->extractKey(keySize_, sortUtil_->config()->numberOfBytesForRecordSize());
+ topNKeys_[loopIndex_].rec_ = insertRec;
+ if (++loopIndex_ == allocRunSize_)
+ {
+ //Reaching here means, we have filled up the array.
+ //Now heapify the array to start accepting/eliminating new elements from now on.
+
+ //Note lookIndex_ contains the current number of filled elements.
+
+ buildHeap();
+ }
+ return SORT_SUCCESS;
+ }
+
+ //Reaching here means, heap is already build.
+ //Check if the new rec belongs to this heap by comparing the
+ //new rec key with the root node of the heap ( root node is always the greatest).
+ insertRec(rec, len, tupp);
+ return SORT_SUCCESS;
+ }
+
+
+void TopN::buildHeap()
+{
+ if(!isHeapified_)
+ {
+ //loopIndex_ is now <= TopN
+ runSize_ = loopIndex_;
+
+ satisfyHeap();
+
+ isHeapified_ = TRUE;
+ }
+}
+
+//Satisfy Heap makes sure the heap is balanced maxHeap.
+//Note this does not mean heap is sorted. It just makes sure
+//the higher level nodes are greater than lower level nodes.
+//Topmost node or root will be the highest.
+void TopN::satisfyHeap()
+{
+ for (int i = (runSize_/2 ); i >= 0; i--)
+ siftDown(topNKeys_, i, runSize_-1);
+}
+
+
+void TopN::insertRec(void *rec, ULng32 len, void* tupp)
+{
+ ex_assert(isHeapified_, "TopN::insertRec: not heapified");
+
+ int root = 0; //Always, root node is the zero'th element in array.
+
+ Record * insertRec = new Record(rec, len, tupp, heap_, sortError_);
+ insertRecKey_.key_ = insertRec->extractKey(keySize_, sortUtil_->config()->numberOfBytesForRecordSize());
+ insertRecKey_.rec_ = insertRec;
+
+ if (compare(topNKeys_[root].key_ ,insertRecKey_.key_) == KEY1_IS_GREATER)
+ {
+ swap( &topNKeys_[root],&insertRecKey_);
+
+
+ //After swap, satisfy or rebalance the heap.
+ satisfyHeap();
+
+ }
+
+ //Once swapped, or not swapped, delete the unneeded record.
+ //This step is very important to discard the unwanted record.
+ //Note releaseTupp() also deletes the tupp allocated in sql
+ //buffer. This makes space for new records read in.
+ insertRecKey_.rec_->releaseTupp();
+ delete insertRecKey_.rec_;
+
+}
+
+Lng32 TopN::sortSendEnd()
+{
+ Lng32 retcode = SORT_SUCCESS;
+ ex_assert(loopIndex_ >= 0, "TopN::sortSendEnd: loopIndex_ is < 0");
+
+ buildHeap();
+ sortHeap();
+
+ return retcode;
+}
+
+void TopN::sortHeap()
+{
+ for (int i = runSize_-1; i >= 1; i--)
+ {
+ swap(&topNKeys_[0],&topNKeys_[i]);
+ siftDown(topNKeys_, 0, i-1);
+ }
+}
+
+Lng32 TopN::sortReceive(void *rec, ULng32& len)
+{
+ return SORT_FAILURE;
+}
+
+Lng32 TopN::sortReceive(void*& rec, ULng32& len, void*& tupp)
+{
+ //---------------------------------------------------------------
+ // We use Qsort to receive records only in case of internal sort
+ // for merging.
+ //---------------------------------------------------------------
+ if (recNum_ < runSize_) {
+ topNKeys_[recNum_].rec_->getRecordTupp(rec, recSize_, tupp);
+ len = recSize_;
+ recNum_++;
+ }
+ else {
+ len = 0;
+ }
+
+ return SORT_SUCCESS;
+}
+/*
+//----------------------------------------------------------------------
+// Name : heapSort
+//
+// Parameters : ...
+//
+// Description : This member function implements the heap sort
+// Return Value :
+// SORT_SUCCESS if everything goes on well.
+// SORT_FAILURE if any error encounterd.
+//
+// NOTE: For this implementation no extra buffer is required. It is done all
+// in place. This is the algorithm used if SORT_ITERATIVE_ALGO is set.
+// requirement. If the performance of heapSort is not as good we may switch
+// back to quickSort or iterativeQuickSort.
+
+//----------------------------------------------------------------------
+void TopN::heapSort(RecKeyBuffer keysToSort[], Int64 runsize)
+{
+ Int64 i;
+
+ for (i = (runsize/2 ); i >= 0; i--)
+ siftDown(keysToSort, i, runsize-1);
+
+ for (i = runsize-1; i >= 1; i--)
+ {
+
+ swap(&keysToSort[0],&keysToSort[i]);
+ siftDown(keysToSort, 0, i-1);
+ }
+}
+*/
+
+void TopN::siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom)
+{
+ Int64 done, maxChild;
+
+ done = 0;
+ while ((root*2 <= bottom) && (!done))
+ {
+ if (root*2 == bottom)
+ maxChild = root * 2;
+ else if (compare(keysToSort[root * 2].key_ ,
+ keysToSort[root * 2 + 1].key_) >= KEY1_IS_GREATER)
+ maxChild = root * 2;
+ else
+ maxChild = root * 2 + 1;
+
+ if (compare(keysToSort[root].key_ ,keysToSort[maxChild].key_) <=KEY1_IS_SMALLER)
+ {
+
+ swap( &keysToSort[root],&keysToSort[maxChild]);
+ root = maxChild;
+
+ }
+ else
+ done = 1;
+ }
+}
+
+//----------------------------------------------------------------------
+// Name : swap
+//
+// Parameters : ..
+//
+// Description : Swaps two elements from the QuickSort workspace. May
+// consider making this inline rather than a seperate
+// procedure call for performance reasons.
+//
+// Recompareturn Value :
+// SORT_SUCCESS if everything goes on well.
+// SORT_FAILURE if any error encounterd.
+//
+//----------------------------------------------------------------------
+
+NABoolean TopN::swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo)
+{
+ char* tempKey;
+ Record* tempRec;
+
+
+ tempKey = recKeyOne->key_;
+ tempRec = recKeyOne->rec_;
+
+
+ recKeyOne->key_ = recKeyTwo->key_;
+ recKeyOne->rec_ = recKeyTwo->rec_;
+
+
+ recKeyTwo->key_ = tempKey;
+ recKeyTwo->rec_ = tempRec;
+ return SORT_SUCCESS;
+}
+
+UInt32 TopN::getOverheadPerRecord(void)
+{
+ return (sizeof(RecKeyBuffer) + sizeof(Record));
+}
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/sort/Topn.h
----------------------------------------------------------------------
diff --git a/core/sql/sort/Topn.h b/core/sql/sort/Topn.h
new file mode 100644
index 0000000..a683536
--- /dev/null
+++ b/core/sql/sort/Topn.h
@@ -0,0 +1,89 @@
+/**********************************************************************
+// @@@ START COPYRIGHT @@@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+// @@@ END COPYRIGHT @@@
+**********************************************************************/
+#ifndef TOPN_H
+#define TOPN_H
+
+/* -*-C++-*-
+******************************************************************************
+*
+* File: Topn.h
+*
+*
+******************************************************************************
+*/
+
+#include "SortAlgo.h"
+#include "Record.h"
+#include "Const.h"
+#include "NABasicObject.h"
+#include "SortError.h"
+
+
+class SortUtil;
+class ExBMOStats;
+
+
+class TopN : public SortAlgo { //SortAlgo inherits from NABasicObject
+
+public:
+
+ TopN(ULng32 recmax,ULng32 sortmaxmem, ULng32 recsize, NABoolean doNotallocRec,
+ ULng32 keysize, SortScratchSpace* scratch,NABoolean iterQuickSort,
+ CollHeap* heap, SortError* sorterror, Lng32 explainNodeId, SortUtil* sortutil);
+ ~TopN(void);
+
+ Lng32 sortSend(void* rec, ULng32 len, void* tupp);
+
+ Lng32 sortClientOutOfMem(void){ return 0;}
+
+ Lng32 sortSendEnd();
+
+ Lng32 sortReceive(void* rec, ULng32& len);
+ Lng32 sortReceive(void*& rec, ULng32& len, void*& tupp);
+ UInt32 getOverheadPerRecord(void);
+ Lng32 generateInterRuns(){ return 0;}
+
+
+private:
+ void buildHeap();
+ void satisfyHeap();
+ void insertRec(void *rec, ULng32 len, void* tupp);
+ void sortHeap();
+ void siftDown(RecKeyBuffer keysToSort[], Int64 root, Int64 bottom);
+ NABoolean swap(RecKeyBuffer* recKeyOne, RecKeyBuffer* recKeyTwo);
+
+ ULng32 loopIndex_;
+ ULng32 recNum_;
+ ULng32 allocRunSize_;
+ NABoolean isHeapified_;
+ RecKeyBuffer insertRecKey_;
+ RecKeyBuffer* topNKeys_;
+ SortError* sortError_;
+ CollHeap* heap_;
+ SortUtil* sortUtil_;
+ ExBMOStats *bmoStats_;
+};
+
+#endif
+
+
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/sqlcomp/DefaultConstants.h
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/DefaultConstants.h b/core/sql/sqlcomp/DefaultConstants.h
index be53281..df0b27c 100644
--- a/core/sql/sqlcomp/DefaultConstants.h
+++ b/core/sql/sqlcomp/DefaultConstants.h
@@ -1675,6 +1675,7 @@ enum DefaultConstants
DO_RUNTIME_SPACE_OPTIMIZATION,
GEN_SORT_MAX_NUM_BUFFERS,
+ GEN_SORT_TOPN_SIZE,
SORT_ALGO, // Sort algorithm choice
// Not used anymore. OVERRIDE_SYSKEY takes its place.
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/3cd201a6/core/sql/sqlcomp/nadefaults.cpp
----------------------------------------------------------------------
diff --git a/core/sql/sqlcomp/nadefaults.cpp b/core/sql/sqlcomp/nadefaults.cpp
index 7b65e0d..c03d05f 100644
--- a/core/sql/sqlcomp/nadefaults.cpp
+++ b/core/sql/sqlcomp/nadefaults.cpp
@@ -1629,6 +1629,7 @@ SDDkwd__(EXE_DIAGNOSTIC_EVENTS, "OFF"),
DDui1__(GEN_SORT_NUM_BUFFERS, "4"),
DDui1__(GEN_SORT_SIZE_DOWN, "2"),
DDui1__(GEN_SORT_SIZE_UP, "1024"),
+ DDui___(GEN_SORT_TOPN_SIZE, "0"),
DDui1__(GEN_SPLB_BUFFER_SIZE, "2"),
DDui1__(GEN_SPLB_NUM_BUFFERS, "1"),
DDui1__(GEN_SPLB_SIZE_DOWN, "2"),
[5/7] incubator-trafodion git commit: Update DIFF042.KNOWN file. Line
numbers mismatch.
Posted by sa...@apache.org.
Update DIFF042.KNOWN file. Line numbers mismatch.
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/f95f7364
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/f95f7364
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/f95f7364
Branch: refs/heads/master
Commit: f95f736479b4d7f610001699371f631df705048a
Parents: 08081bd
Author: Prashant Vasudev <pr...@esgyn.com>
Authored: Mon Oct 10 20:05:29 2016 +0000
Committer: Prashant Vasudev <pr...@esgyn.com>
Committed: Mon Oct 10 20:05:29 2016 +0000
----------------------------------------------------------------------
core/sql/regress/compGeneral/DIFF042.KNOWN | 36 ++++++++++++-------------
1 file changed, 18 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/f95f7364/core/sql/regress/compGeneral/DIFF042.KNOWN
----------------------------------------------------------------------
diff --git a/core/sql/regress/compGeneral/DIFF042.KNOWN b/core/sql/regress/compGeneral/DIFF042.KNOWN
index e73fb2d..088a99d 100644
--- a/core/sql/regress/compGeneral/DIFF042.KNOWN
+++ b/core/sql/regress/compGeneral/DIFF042.KNOWN
@@ -1,60 +1,60 @@
-1863c1863
+1865c1865
< 22 0 22
---
> 32 0 32
-1875c1875
+1877c1877
< 22 0 22
---
> 32 0 32
-1970a1971,1975
+1972a1973,1977
> 0 1
> 0 1
> 0 1
> 0 1
> 0 1
-1980a1986,1990
+1982a1988,1992
> 1 1
> 1 1
> 1 1
> 1 1
> 1 1
-1990c2000
+1992c2002
< 9 4
---
> 13 4
-1992c2002
+1994c2004
< --- 24 row(s) selected.
---
> --- 34 row(s) selected.
-2063a2074,2078
+2065a2076,2080
> 0 1
> 0 1
> 0 1
> 0 1
> 0 1
-2085a2101,2105
+2087a2103,2107
> 1 1
> 1 1
> 1 1
> 1 1
> 1 1
-2099c2119
+2101c2121
< 9 4
---
> 13 4
-2101c2121
+2103c2123
< --- 99 row(s) selected.
---
> --- 109 row(s) selected.
-2119c2139
+2121c2141
< 22 22 5 211
---
> 32 32 5 211
-2131c2151
+2133c2153
< 22 22 5 211
---
> 32 32 5 211
-2221a2242,2249
+2223a2244,2251
> 227 0 1 0
> 227 1 1 0
> 233 0 1 0
@@ -63,14 +63,14 @@
> 270 1 1 0
> 282 0 1 0
> 282 1 1 0
-2224a2253,2254
+2226a2255,2256
> 336 0 1 14
> 336 1 1 14
-2233c2263
+2235c2265
< --- 24 row(s) selected.
---
> --- 34 row(s) selected.
-2315a2346,2353
+2317a2348,2355
> 227 0 1 0
> 227 1 1 0
> 233 0 1 0
@@ -79,10 +79,10 @@
> 270 1 1 0
> 282 0 1 0
> 282 1 1 0
-2318a2357,2358
+2320a2359,2360
> 336 0 1 14
> 336 1 1 14
-2327c2367
+2329c2369
< --- 84 row(s) selected.
---
> --- 94 row(s) selected.