AggComputer.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/exec/AggComputer.cpp#11 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 //
00008 // This program is free software; you can redistribute it and/or modify it
00009 // under the terms of the GNU General Public License as published by the Free
00010 // Software Foundation; either version 2 of the License, or (at your option)
00011 // any later version approved by The Eigenbase Project.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #include "fennel/common/CommonPreamble.h"
00024 #include "fennel/common/FennelResource.h"
00025 #include "fennel/exec/AggComputerImpl.h"
00026 #include "fennel/tuple/TupleDescriptor.h"
00027 #include "fennel/tuple/StandardTypeDescriptor.h"
00028 #include "fennel/common/FennelExcn.h"
00029 
00030 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/exec/AggComputer.cpp#11 $");
00031 
00032 using namespace std;
00033 
00034 AggComputer *AggComputer::newAggComputer(
00035     AggFunction aggFunction,
00036     TupleAttributeDescriptor const *pAttrDesc)
00037 {
00038     switch (aggFunction) {
00039     case AGG_FUNC_COUNT:
00040         if (pAttrDesc) {
00041             return new CountNullableAggComputer();
00042         } else {
00043             return new CountStarAggComputer();
00044         }
00045     case AGG_FUNC_SUM:
00046         assert(pAttrDesc);
00047         // TODO jvs 6-Oct-2005:  gotta use some of that template
00048         // metaprogramming mumbo jumbo to get rid of this
00049         switch (pAttrDesc->pTypeDescriptor->getOrdinal()) {
00050         case STANDARD_TYPE_INT_8:
00051             return new SumAggComputer<int8_t>();
00052         case STANDARD_TYPE_UINT_8:
00053             return new SumAggComputer<uint8_t>();
00054         case STANDARD_TYPE_INT_16:
00055             return new SumAggComputer<int16_t>();
00056         case STANDARD_TYPE_UINT_16:
00057             return new SumAggComputer<uint16_t>();
00058         case STANDARD_TYPE_INT_32:
00059             return new SumAggComputer<int32_t>();
00060         case STANDARD_TYPE_UINT_32:
00061             return new SumAggComputer<uint32_t>();
00062         case STANDARD_TYPE_INT_64:
00063             return new SumAggComputer<int64_t>();
00064         case STANDARD_TYPE_UINT_64:
00065             return new SumAggComputer<uint64_t>();
00066         case STANDARD_TYPE_REAL:
00067             return new SumAggComputer<float>();
00068         case STANDARD_TYPE_DOUBLE:
00069             return new SumAggComputer<double>();
00070         }
00071     case AGG_FUNC_MIN:
00072     case AGG_FUNC_MAX:
00073     case AGG_FUNC_SINGLE_VALUE:
00074         assert(pAttrDesc);
00075         return new ExtremeAggComputer(aggFunction, *pAttrDesc);
00076     }
00077     permAssert(false);
00078 }
00079 
00080 AggComputer::AggComputer()
00081 {
00082     iInputAttr = -1;
00083 }
00084 
00085 void AggComputer::setInputAttrIndex(uint iInputAttrInit)
00086 {
00087     iInputAttr = iInputAttrInit;
00088 }
00089 
00090 AggComputer::~AggComputer()
00091 {
00092 }
00093 
00094 inline uint64_t &CountAggComputer::interpretDatum(TupleDatum &datum)
00095 {
00096     assert(datum.cbData == sizeof(uint64_t));
00097     assert(datum.pData);
00098     return *reinterpret_cast<uint64_t *>(const_cast<PBuffer>(datum.pData));
00099 }
00100 
00101 inline void CountAggComputer::clearAccumulatorImpl(TupleDatum &accumulatorDatum)
00102 {
00103     uint64_t &count = interpretDatum(accumulatorDatum);
00104     count = 0;
00105 }
00106 
00107 inline void CountAggComputer::initAccumulatorImpl(TupleDatum &accumulatorDatum)
00108 {
00109     uint64_t &count = interpretDatum(accumulatorDatum);
00110     count = 1;
00111 }
00112 
00113 void CountAggComputer::updateAccumulatorImpl(
00114     TupleDatum &accumulatorDatum)
00115 {
00116     uint64_t &count = interpretDatum(accumulatorDatum);
00117     ++count;
00118 }
00119 
00120 void CountAggComputer::computeOutput(
00121     TupleDatum &outputDatum,
00122     TupleDatum const &accumulatorDatum)
00123 {
00124     // Set output to alias accumulator value directly.
00125     outputDatum = accumulatorDatum;
00126 }
00127 
00128 void CountStarAggComputer::clearAccumulator(TupleDatum &accumulatorDatum)
00129 {
00130     assert(iInputAttr == -1);
00131     clearAccumulatorImpl(accumulatorDatum);
00132 }
00133 
00134 void CountStarAggComputer::updateAccumulator(
00135     TupleDatum &accumulatorDatum,
00136     TupleData const &)
00137 {
00138     updateAccumulatorImpl(accumulatorDatum);
00139 }
00140 
00141 void CountStarAggComputer::initAccumulator(
00142     TupleDatum &accumulatorDatum,
00143     TupleData const &)
00144 {
00145     assert(iInputAttr == -1);
00146     initAccumulatorImpl(accumulatorDatum);
00147 }
00148 
00149 void CountStarAggComputer::initAccumulator(
00150     TupleDatum &accumulatorDatumSrc,
00151     TupleDatum &accumulatorDatumDest)
00152 {
00153     accumulatorDatumDest.memCopyFrom(accumulatorDatumSrc);
00154 }
00155 
00156 void CountStarAggComputer::updateAccumulator(
00157     TupleDatum &accumulatorDatumSrc,
00158     TupleDatum &accumulatorDatumDest,
00159     TupleData const &)
00160 {
00161     updateAccumulatorImpl(accumulatorDatumSrc);
00162     /*
00163      * For count, accumulatorDatumSrc can accomodate the updated value so
00164      * there is no need to use memCopyFrom.
00165      */
00166     accumulatorDatumDest.copyFrom(accumulatorDatumSrc);
00167 }
00168 
00169 void CountNullableAggComputer::clearAccumulator(TupleDatum &accumulatorDatum)
00170 {
00171     clearAccumulatorImpl(accumulatorDatum);
00172 }
00173 
00174 void CountNullableAggComputer::updateAccumulator(
00175     TupleDatum &accumulatorDatum,
00176     TupleData const &inputTuple)
00177 {
00178     assert(iInputAttr != -1);
00179     TupleDatum const &inputDatum = inputTuple[iInputAttr];
00180     if (inputDatum.pData) {
00181         updateAccumulatorImpl(accumulatorDatum);
00182     }
00183 }
00184 
00185 void CountNullableAggComputer::initAccumulator(
00186     TupleDatum &accumulatorDatum,
00187     TupleData const &inputTuple)
00188 {
00189     assert(iInputAttr != -1);
00190     TupleDatum const &inputDatum = inputTuple[iInputAttr];
00191     if (inputDatum.pData) {
00192         initAccumulatorImpl(accumulatorDatum);
00193     } else {
00194         clearAccumulatorImpl(accumulatorDatum);
00195     }
00196 }
00197 
00198 void CountNullableAggComputer::initAccumulator(
00199     TupleDatum &accumulatorDatumSrc,
00200     TupleDatum &accumulatorDatumDest)
00201 {
00202     accumulatorDatumDest.memCopyFrom(accumulatorDatumSrc);
00203 }
00204 
00205 void CountNullableAggComputer::updateAccumulator(
00206     TupleDatum &accumulatorDatumSrc,
00207     TupleDatum &accumulatorDatumDest,
00208     TupleData const &inputTuple)
00209 {
00210     assert(iInputAttr != -1);
00211     TupleDatum const &inputDatum = inputTuple[iInputAttr];
00212     if (inputDatum.pData) {
00213         updateAccumulatorImpl(accumulatorDatumSrc);
00214     }
00215     /*
00216      * For count, accumulatorDatumSrc can accomodate the updated value so
00217      * there is no need to use memCopyFrom.
00218      */
00219     accumulatorDatumDest.copyFrom(accumulatorDatumSrc);
00220 }
00221 
00222 ExtremeAggComputer::ExtremeAggComputer(
00223     AggFunction aggFunctionInit,
00224     TupleAttributeDescriptor const &attrDesc)
00225 {
00226     aggFunction = aggFunctionInit;
00227     pTypeDescriptor = attrDesc.pTypeDescriptor;
00228 }
00229 
00230 void ExtremeAggComputer::clearAccumulator(TupleDatum &accumulatorDatum)
00231 {
00232     isResultNull = true;
00233 }
00234 
00235 inline void ExtremeAggComputer::copyInputToAccumulator(
00236     TupleDatum &accumulatorDatum,
00237     TupleDatum const &inputDatum)
00238 {
00239     // Use the utility function to copy from inputDatum's buffer to
00240     // accumulatorDatum.
00241     accumulatorDatum.memCopyFrom(inputDatum);
00242 }
00243 
00244 void ExtremeAggComputer::updateAccumulator(
00245     TupleDatum &accumulatorDatum,
00246     TupleData const &inputTuple)
00247 {
00248     assert(iInputAttr != -1);
00249     TupleDatum const &inputDatum = inputTuple[iInputAttr];
00250     if (!inputDatum.pData) {
00251         // SQL2003 Part 2 Section 10.9 General Rule 4.a
00252         // TODO jvs 6-Oct-2005:  we're supposed to queue a warning
00253         // for null value eliminated in set function
00254         return;
00255     }
00256     if (isResultNull) {
00257         isResultNull = false;
00258         // first non-null input:  use it
00259         copyInputToAccumulator(accumulatorDatum, inputDatum);
00260         return;
00261     } else if (aggFunction == AGG_FUNC_SINGLE_VALUE) {
00262         throw FennelExcn(
00263             FennelResource::instance().scalarQueryReturnedMultipleRows());
00264     }
00265 
00266     // c = (input - accumulator)
00267     int c = pTypeDescriptor->compareValues(
00268         inputDatum.pData,
00269         inputDatum.cbData,
00270         accumulatorDatum.pData,
00271         accumulatorDatum.cbData);
00272     if (aggFunction == AGG_FUNC_MIN) {
00273         // invert comparison for MIN
00274         c = -c;
00275     }
00276     if (c <= 0) {
00277         // for MAX, input has to be greater than accumulator for accumulator
00278         // to be updated
00279         return;
00280     }
00281     copyInputToAccumulator(accumulatorDatum, inputDatum);
00282 }
00283 
00284 void ExtremeAggComputer::computeOutput(
00285     TupleDatum &outputDatum,
00286     TupleDatum const &accumulatorDatum)
00287 {
00288     // Set output to alias accumulator value directly.
00289     outputDatum = accumulatorDatum;
00290     if (isResultNull) {
00291         outputDatum.pData = NULL;
00292     }
00293 }
00294 
00295 void ExtremeAggComputer::initAccumulator(
00296     TupleDatum &accumulatorDatumDest,
00297     TupleData const &inputTuple)
00298 {
00299     accumulatorDatumDest.memCopyFrom(inputTuple[iInputAttr]);
00300     isResultNull = false;
00301 }
00302 
00303 void ExtremeAggComputer::initAccumulator(
00304     TupleDatum &accumulatorDatumSrc,
00305     TupleDatum &accumulatorDatumDest)
00306 {
00307     accumulatorDatumDest.memCopyFrom(accumulatorDatumSrc);
00308     isResultNull = false;
00309 }
00310 
00311 void ExtremeAggComputer::updateAccumulator(
00312     TupleDatum &accumulatorDatumSrc,
00313     TupleDatum &accumulatorDatumDest,
00314     TupleData const &inputTuple)
00315 {
00316     if (aggFunction == AGG_FUNC_SINGLE_VALUE) {
00317         throw FennelExcn(
00318             FennelResource::instance().scalarQueryReturnedMultipleRows());
00319     }
00320 
00321     TupleDatum const &inputDatum = inputTuple[iInputAttr];
00322 
00323     if (!accumulatorDatumSrc.pData) {
00324         accumulatorDatumDest.memCopyFrom(inputDatum);
00325     } else if (inputDatum.pData) {
00326         // c = (input - accumulator)
00327         int c = pTypeDescriptor->compareValues(
00328             inputDatum.pData,
00329             inputDatum.cbData,
00330             accumulatorDatumSrc.pData,
00331             accumulatorDatumSrc.cbData);
00332         if (aggFunction == AGG_FUNC_MIN) {
00333             // invert comparison for MIN
00334             c = -c;
00335         }
00336         if (c <= 0) {
00337             // for MAX, input has to be greater than accumulator for accumulator
00338             // to be updated
00339             accumulatorDatumDest.memCopyFrom(accumulatorDatumSrc);
00340         } else {
00341             accumulatorDatumDest.memCopyFrom(inputDatum);
00342         }
00343     } else {
00344         accumulatorDatumDest.memCopyFrom(accumulatorDatumSrc);
00345     }
00346 }
00347 
00348 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/exec/AggComputer.cpp#11 $");
00349 
00350 // End AggComputer.cpp

Generated on Mon Jun 22 04:00:18 2009 for Fennel by  doxygen 1.5.1