Column-oriented GPU-accelerated Database Management System
CoGaDB
|
00001 #pragma once 00002 00003 #include <core/column.hpp> 00004 #include <iostream> 00005 00006 #include <utility> 00007 #include <functional> 00008 #include <algorithm> 00009 00010 namespace CoGaDB{ 00011 00012 template <class T> 00013 struct min_obj : std::binary_function <T,T,T> { 00014 T operator() (const T& x, const T& y) const 00015 { 00016 return std::min(x,y); 00017 } 00018 }; 00019 00020 template <class T> 00021 struct max_obj : std::binary_function <T,T,T> { 00022 T operator() (const T& x, const T& y) const 00023 { 00024 return std::max(x,y); 00025 } 00026 }; 00027 00028 template <typename T, typename U, typename BinaryOperator> 00029 std::pair<ColumnPtr,ColumnPtr> reduce_by_keys(shared_pointer_namespace::shared_ptr<ColumnBaseTyped<U> > keys, shared_pointer_namespace::shared_ptr<ColumnBaseTyped<T> > values, BinaryOperator binary_op){ 00030 00031 assert(keys->size()==values->size()); 00032 00033 Column<U>* new_keys = new Column<U>(keys->getName(),keys->getType()); 00034 Column<T>* aggregated_values = new Column<T>(values->getName(),values->getType()); 00035 00036 //if there is nothign to do, return valid but empty colums 00037 if(keys->size()==0){ 00038 return std::pair<ColumnPtr,ColumnPtr>(ColumnPtr(new_keys), ColumnPtr(aggregated_values)); 00039 } 00040 00041 T val=0; 00042 for(unsigned int i=0;i<values->size();i++){ 00043 //unsigned int i=0; 00044 //while(i<values->size()) 00045 00046 if(i==0){ 00047 val=(*values)[i]; 00048 continue; 00049 } 00050 00051 00052 if( (*keys)[i-1]==(*keys)[i] ){ 00053 //std::cout << i << "SUM up values: " << val << "+" << (*values)[i] << "=" << binary_op(val,(*values)[i]) << std::endl; 00054 val = binary_op(val,(*values)[i]); 00055 }else{ 00056 //std::cout << "found new key: " << (*keys)[i] << " old: " << (*keys)[i-1] << " computed value: " << val << std::endl; 00057 T tmp_key=(*keys)[i-1]; 00058 new_keys->insert(tmp_key); 00059 aggregated_values->insert(val); 00060 val=(*values)[i]; 00061 } 00062 00063 00064 } 00065 //write result of last line 00066 T tmp_key=(*keys)[keys->size()-1]; 00067 new_keys->insert(tmp_key); 00068 aggregated_values->insert(val); 00069 00070 return std::pair<ColumnPtr,ColumnPtr>(ColumnPtr(new_keys), ColumnPtr(aggregated_values)); 00071 //return std::pair<ColumnPtr,ColumnPtr>(ColumnPtr(), ColumnPtr()); 00072 } 00073 00074 template <typename T, typename U> 00075 std::pair<ColumnPtr,ColumnPtr> reduce_by_keys(shared_pointer_namespace::shared_ptr<ColumnBaseTyped<U> > keys, shared_pointer_namespace::shared_ptr<ColumnBaseTyped<T> > values, AggregationMethod agg_meth){ 00076 std::pair<ColumnPtr,ColumnPtr> result; 00077 if(agg_meth==SUM){ 00078 result = reduce_by_keys(keys, values, std::plus<T>()); 00079 }else if(agg_meth==MIN){ 00080 result = reduce_by_keys(keys, values, min_obj<T>()); 00081 }else if(agg_meth==MAX){ 00082 result = reduce_by_keys(keys, values, max_obj<T>()); 00083 }else{ 00084 std::cerr << "FATAL Error! Unknown Aggregation Method!" << agg_meth << std::endl; 00085 00086 } 00087 00088 return result; 00089 } 00090 00091 00092 00093 00094 }; //end namespace CogaDB 00095 00096