#ifndef _CEGOAGGREGATION_H_INCLUDED_
#define _CEGOAGGREGATION_H_INCLUDED_
///////////////////////////////////////////////////////////////////////////////
//                                                         
// CegoAggregation.h
// -----------------
// Cego aggregation structure class definition
//                                                         
// Design and Implementation by Bjoern Lemke               
//                                                         
// (C)opyright 2000-2025 Bjoern Lemke  
//
// INTERFACE MODULE
//
// Class: CegoAggregation
// 
// Description: Query aggregation utility class
//
// Status: CLEAN
//
///////////////////////////////////////////////////////////////////////////////

// LFC INCLUDES
#include <lfcbase/Chain.h>

// CEGO INCLUDES
#include "CegoFieldValue.h"
#include "CegoProcBlock.h"
#include "CegoProcFetch.h"
#include "CegoFunction.h"

class CegoExpr;

class CegoAggregation {
    
public:
    
    enum AggType { MIN, MAX, AVG, SUM, COUNT };
    
    CegoAggregation(bool isDistinct);
    CegoAggregation(char* buf);
    CegoAggregation(AggType type, CegoExpr* pExpr, bool isDistinct = false);
    ~CegoAggregation();

    void setQueryManager(CegoQueryManager* pTabMng);
    void setTabSetId(unsigned tabSetId);
    
    void cleanUp();

    
    CegoFieldValue& getFieldValue();
    void setFieldValue(const CegoFieldValue& fv);
    
    const AggType& getType() const;
    CegoExpr* getExpr() const;

    bool isDistinct() const;

    
    
    void setAggregationId(unsigned aid);
    const unsigned getAggregationId() const;

    void clearAttrCache();
    
    CegoAggregation* clone(bool isAttrRef = false);

    CegoAggregation& operator = ( const CegoAggregation& ag);

    ListT<CegoAttrDesc*> getAttrRefList() const;

    void encode(char *buf, CegoProcBlock* pBlock);
    void decode(char *buf);
    unsigned getEncodingLength(CegoProcBlock* pBlock = 0) const;
    
    Chain getId(CegoProcBlock *pBlock) const;
    Chain toChain(int defTabSetId) const;
    Chain dbFormat(CegoDatabaseFormater *pForm) const;
    
    friend ostream& operator << (ostream& s, const CegoAggregation& ag);
      
private:

    CegoFieldValue _fv;
    
    unsigned _aid;
    AggType _type;    
    CegoExpr* _pExpr;
    bool _isDistinct;
};
#endif
