Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
[TRAFODION-2259] TopN sort changes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Prashant Vasudev committed Oct 10, 2016
1 parent 12f602c commit 3cd201a
Show file tree
Hide file tree
Showing 14 changed files with 490 additions and 58 deletions.
9 changes: 7 additions & 2 deletions core/sql/comexe/ComTdbSort.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ class ComTdbSort : public ComTdb
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:

Expand Down Expand Up @@ -326,6 +327,10 @@ class ComTdbSort : public ComTdb
{ 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_;}
Expand Down
1 change: 1 addition & 0 deletions core/sql/executor/ex_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
3 changes: 3 additions & 0 deletions core/sql/generator/GenRelMisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion core/sql/nskgmake/sort/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ CPPSRC := CommonUtil.cpp \
SortUtilCfg.cpp \
Statistics.cpp \
TourTree.cpp \
TreeNode.cpp
TreeNode.cpp \
Topn.cpp

CPPSRC += vers_libsort.cpp

Expand Down
5 changes: 1 addition & 4 deletions core/sql/sort/Qsort.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 13 additions & 1 deletion core/sql/sort/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions core/sql/sort/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 21 additions & 1 deletion core/sql/sort/SortUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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_,
Expand All @@ -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
Expand Down
42 changes: 0 additions & 42 deletions core/sql/sort/SortUtilCfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 6 additions & 7 deletions core/sql/sort/SortUtilCfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ class SortUtilConfig : public NABasicObject {
ULng32 getRecSize() const;

NABoolean setKeyInfo(ULng32 keysize);


NABoolean setTemps(ULng32 runsize, ULng32 mergesize);
NABoolean getTemps() const;


void setUseBuffered(NABoolean torf);
NABoolean getUseBuffered() ;

Expand Down Expand Up @@ -152,7 +148,10 @@ class SortUtilConfig : public NABasicObject {
{
return numEsps_;
}

void setTopNSize(ULng32 size)
{
topNSize_ = size;
}
void setEventHandler(ExSubtask *eh)
{
ioEventHandler_ = eh;
Expand Down Expand Up @@ -319,7 +318,7 @@ class SortUtilConfig : public NABasicObject {
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
Expand Down

0 comments on commit 3cd201a

Please sign in to comment.