#include <iostream>
#include "supersonic/supersonic.h"
using std::cout;
using std::endl;
using supersonic::TupleSchema;
using supersonic::Attribute;
using supersonic::INT32;
using supersonic::NOT_NULLABLE;
using supersonic::View;
using supersonic::AggregationSpecification;
using supersonic::SUM;
using supersonic::Operation;
using supersonic::ScalarAggregate;
using supersonic::ScanView;
using supersonic::ResultView;
using supersonic::SucceedOrDie;
using supersonic::Cursor;
Cursor* SingleColumnSums(int32 *data, size_t row_number) {
TupleSchema schema;
schema.add_attribute(Attribute("data", INT32, NOT_NULLABLE));
View input_view(schema);
input_view.set_row_count(row_number);
input_view.mutable_column(0)->Reset(data, NULL);
scoped_ptr<AggregationSpecification> aggregator(new AggregationSpecification);
aggregator->AddAggregation(SUM, "data", "data_sums");
scoped_ptr<Operation> aggregation(ScalarAggregate(aggregator.release(), ScanView(input_view)));
scoped_ptr<Cursor> bound_aggregation(SucceedOrDie(aggregation->CreateCursor()));
return bound_aggregation.release();
}
int main(void) {
const unsigned size = 8;
int32 a[size] = { 1, 2, 3, 1, 2, 3, 1, 2};
scoped_ptr<Cursor> result_cursor(SingleColumnSums(a, size));
ResultView result(result_cursor->Next(-1));
if(!result.has_data()) {
cout << "Failed to do the sum operation!" << endl;
return -1;
}
View result_view(result.view());
cout << "Result column number : " << result_view.column_count() << endl;
cout << "Result row number : " << result_view.row_count() << endl;
const int32* keys = result_view.column(0).typed_data<INT32>();
cout << "Sum result : " << keys[0] << endl;
return 0;
}
在 2013年4月12日星期五UTC+8下午7时41分35秒,ptab写道: