How to impelment aggregation without group by projector ?

91 views
Skip to first unread message

Zhichao Liang

unread,
Apr 8, 2013, 3:28:40 AM4/8/13
to supersonic-...@googlegroups.com
Hi,

  I just wanna implement an aggregation on a specific column without group by projector, such as "SELECT SUM(attribute_name) FROM table_name", however, it's necessary for the API GroupAggregate to have a group by projector argument. Can anybody show me an example that impelments an aggregation without group by projector ? Thanks!

Best Regards!

Zhichao

ptab

unread,
Apr 12, 2013, 7:41:35 AM4/12/13
to supersonic-...@googlegroups.com

Zhichao Liang

unread,
May 2, 2013, 10:54:57 PM5/2/13
to supersonic-...@googlegroups.com
Yeah, ScalarAggregate is just what i need, thank you Piotr! In case some other guys encounter the same problem, i list an example of using ScalarAggregate as follows:

#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写道:
Reply all
Reply to author
Forward
0 new messages