Fennel: /home/pub/open/dev/fennel/lucidera/test/LbmSortedAggExecStreamTest.cpp Source File (original) (raw)

00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 #include "fennel/common/CommonPreamble.h" 00023 #include "fennel/lucidera/test/LbmExecStreamTestBase.h" 00024 #include "fennel/lucidera/bitmap/LbmByteSegment.h" 00025 #include "fennel/lucidera/bitmap/LbmSortedAggExecStream.h" 00026 #include <stdarg.h> 00027 00028 #include <boost/test/test_tools.hpp> 00029 00030 using namespace fennel; 00031 00035 class LbmSortedAggExecStreamTest : public LbmExecStreamTestBase 00036 { 00037 protected: 00058 void testSortedAgg( 00059 uint nRows, 00060 uint nKeys, 00061 std::vector const &repeatSeqValues); 00062 00063 public: 00064 explicit LbmSortedAggExecStreamTest() 00065 { 00066 FENNEL_UNIT_TEST_CASE(LbmSortedAggExecStreamTest, testScanFullKey); 00067 FENNEL_UNIT_TEST_CASE(LbmSortedAggExecStreamTest, testScanPartKey); 00068 } 00069 00070 void testScanFullKey(); 00071 void testScanPartKey(); 00072 }; 00073 00077 uint getValueCount(uint nRows, uint interval, uint value) 00078 { 00079 uint nCopies = nRows / interval; 00080 if (value < nRows % interval) { 00081 nCopies++; 00082 } 00083 return nCopies; 00084 } 00085 00094 class SortedAggExecStreamGenerator : public MockProducerExecStreamGenerator 00095 { 00096 protected: 00097 uint nRows; 00098 uint nKeys; 00099 std::vector keyRepeats; 00100 uint interval; 00101 boost::shared_array sortedToUnsortedMap; 00102 int current; 00103 boost::shared_array currentRow; 00104 00105 public: 00106 SortedAggExecStreamGenerator( 00107 uint nRows, uint nKeys, std::vector repeatSeqValues) 00108 { 00109 this->nRows = nRows; 00110 this->nKeys = nKeys; 00111 for (uint i = 0; i < nKeys; i++) { 00112 keyRepeats.push_back(repeatSeqValues[i]); 00113 } 00114 interval = LbmExecStreamTestBase::getTupleInterval(keyRepeats); 00115 00116 sortedToUnsortedMap.reset(new uint[interval]); 00117 for (uint i = 0; i < interval; i++) { 00118 uint value = 0; 00119 uint scale = 1; 00120
00121
00122 for (int j = nKeys - 1; j >= 0; j--) { 00123 uint key = i % keyRepeats[j]; 00124 value += key * scale; 00125 scale *= keyRepeats[j]; 00126 } 00127 sortedToUnsortedMap[value] = i; 00128 } 00129 current = -1; 00130 currentRow.reset(new uint[nKeys + 1]); 00131 } 00132 00133 virtual int64_t generateValue(uint iRow, uint iCol) 00134 { 00135 assert (iRow < interval); 00136 assert (iCol < nKeys + 1); 00137 00138 if (iRow != current) { 00139 current = iRow; 00140 uint unsorted = sortedToUnsortedMap[current]; 00141 for (uint i = 0; i < nKeys; i++) { 00142 currentRow[i] = unsorted % keyRepeats[i]; 00143 } 00144 currentRow[nKeys] = getValueCount(nRows, interval, unsorted); 00145 } 00146 return currentRow[iCol]; 00147 } 00148 }; 00149 00150 void LbmSortedAggExecStreamTest::testScanFullKey() 00151 { 00152 std::vector repeatSeqValues; 00153 repeatSeqValues.push_back(1); 00154 repeatSeqValues.push_back(5); 00155 repeatSeqValues.push_back(9); 00156 00157 testSortedAgg(1000, 3, repeatSeqValues); 00158 } 00159 00160 void LbmSortedAggExecStreamTest::testScanPartKey() 00161 { 00162 std::vector repeatSeqValues; 00163 repeatSeqValues.push_back(13); 00164 repeatSeqValues.push_back(5); 00165 repeatSeqValues.push_back(9); 00166 00167 testSortedAgg(1000, 2, repeatSeqValues); 00168 } 00169 00170 void LbmSortedAggExecStreamTest::testSortedAgg( 00171 uint nRows, 00172 uint nKeys, 00173 std::vector const &repeatSeqValues) 00174 { 00175 initKeyBitmap(nRows, repeatSeqValues); 00176 00177
00178 ValuesExecStreamParams valuesParams; 00179 valuesParams.outputTupleDesc = keyBitmapTupleDesc; 00180 valuesParams.pTupleBuffer = keyBitmapBuf; 00181 valuesParams.bufSize = keyBitmapBufSize; 00182 00183 ExecStreamEmbryo valuesStreamEmbryo; 00184 valuesStreamEmbryo.init(new ValuesExecStream(), valuesParams); 00185 valuesStreamEmbryo.getStream()->setName("ValuesExecStream"); 00186 00187
00188 std::vector transformEmbryos; 00189 00190 ExternalSortExecStreamParams sortParams; 00191 ExecStreamEmbryo sortEmbryo; 00192 initSorterExecStream(sortParams, sortEmbryo, keyBitmapTupleDesc, nKeys); 00193 transformEmbryos.push_back(sortEmbryo); 00194 00195 ExecStreamEmbryo aggEmbryo; 00196 LbmSortedAggExecStreamParams aggParams; 00197 aggParams.groupByKeyCount = nKeys; 00198 AggInvocation countStar; 00199 countStar.aggFunction = AGG_FUNC_COUNT; 00200 countStar.iInputAttr = -1; 00201 aggParams.aggInvocations.push_back(countStar); 00202 00203 TupleProjection keyProj; 00204 for (int i = 0; i < nKeys; i++) { 00205 keyProj.push_back(i); 00206 } 00207 TupleDescriptor aggDesc; 00208 aggDesc.projectFrom(keyBitmapTupleDesc, keyProj); 00209 aggDesc.push_back(attrDesc_int64); 00210 aggParams.outputTupleDesc = aggDesc; 00211 00212 aggEmbryo.init(new LbmSortedAggExecStream(), aggParams); 00213 aggEmbryo.getStream()->setName("SortedAgg"); 00214 transformEmbryos.push_back(aggEmbryo); 00215 00216 SharedExecStream pOutputStream = prepareTransformGraph( 00217 valuesStreamEmbryo, transformEmbryos); 00218 SortedAggExecStreamGenerator verifier(nRows, nKeys, repeatSeqValues); 00219 verifyOutput( 00220 *pOutputStream, 00221 getTupleInterval(repeatSeqValues, nKeys), 00222 verifier); 00223 } 00224 00225 FENNEL_UNIT_TEST_SUITE(LbmSortedAggExecStreamTest); 00226 00227