diff --git a/src/errors.h b/src/errors.h index f837743ab52f8b0f43a42d1390371cda2f04c60c..dfa09992d2f2aece2272c164c5880cbb9fabd293 100644 --- a/src/errors.h +++ b/src/errors.h @@ -181,6 +181,12 @@ class DateTimeFormatError : public RMDBError { : RMDBError("Datetime literal " + val + " format error") {} }; +class AggregateParamFormatError : public RMDBError { + public: + AggregateParamFormatError(const std::string &type) + : RMDBError("Aggregate " + type + " param format error") {} +}; + class IncompatibleTypeError : public RMDBError { public: IncompatibleTypeError(const std::string &lhs, const std::string &rhs) diff --git a/src/execution/execution_manager.cpp b/src/execution/execution_manager.cpp index 6c6ccca1ed036c349efe79258f7ea60ef1935b26..eebcd4a501082b8360f0c560aa1dab543069c2e6 100644 --- a/src/execution/execution_manager.cpp +++ b/src/execution/execution_manager.cpp @@ -267,14 +267,18 @@ void QlManager::select_from(std::unique_ptr<AbstractExecutor> executorTreeRoot, switch(sel_cols[col_id].aggregate->aggregate_type){ case 0: // COUNT - if(tmp_item.size() <= col_id){tmp_item.push_back("1");} - else + if(col.type == TYPE_INT || col.type == TYPE_FLOAT || col.type == TYPE_STRING) { - if(col_str != "") + if(tmp_item.size() <= col_id){tmp_item.push_back("1");} + else { tmp_item[col_id] = std::to_string(atoi(tmp_item[col_id].c_str()) + 1); } } + else + { + throw AggregateParamFormatError("COUNT"); + } break; case 1: // MAX @@ -293,7 +297,10 @@ void QlManager::select_from(std::unique_ptr<AbstractExecutor> executorTreeRoot, { tmp_item[col_id] = tmp_item[col_id] > col_str ? tmp_item[col_id]:col_str; } - // 缺一个else,throw一个max傿•°ç±»åž‹é”™è¯¯ + else + { + throw AggregateParamFormatError("MAX"); + } } break; case 2: @@ -313,7 +320,10 @@ void QlManager::select_from(std::unique_ptr<AbstractExecutor> executorTreeRoot, { tmp_item[col_id] = tmp_item[col_id] < col_str ? tmp_item[col_id]:col_str; } - // 缺一个else,throw一个max傿•°ç±»åž‹é”™è¯¯ + else + { + throw AggregateParamFormatError("MIN"); + } } break; case 3: @@ -334,7 +344,10 @@ void QlManager::select_from(std::unique_ptr<AbstractExecutor> executorTreeRoot, tmp_item[col_id] = std::to_string(atof(tmp_item[col_id].c_str()) + atof(col_str.c_str())); } } - // 缺一个else,throw一个sum傿•°ç±»åž‹é”™è¯¯ + else + { + throw AggregateParamFormatError("SUM"); + } break; default: break; } @@ -359,6 +372,7 @@ void QlManager::select_from(std::unique_ptr<AbstractExecutor> executorTreeRoot, // 打å°èšé›†å‡½æ•°ç›¸å…³ if(has_aggregate) { + num_rec = 1; // print record into buffer rec_printer.print_record(tmp_item, context); // print record into file @@ -367,7 +381,6 @@ void QlManager::select_from(std::unique_ptr<AbstractExecutor> executorTreeRoot, outfile << " " << tmp_item[i] << " |"; } outfile << "\n"; - num_rec++; } outfile.close(); // Print footer into buffer