static rasqal_rowsource* rasqal_algebra_group_algebra_node_to_rowsource(rasqal_engine_algebra_data* execution_data, rasqal_algebra_node* node, rasqal_engine_error *error_p) { rasqal_query *query = execution_data->query; rasqal_rowsource *rs; rs = rasqal_algebra_node_to_rowsource(execution_data, node->node1, error_p); if(!rs || *error_p) return NULL; return rasqal_new_groupby_rowsource(query->world, query, rs, node->seq); }
int main(int argc, char *argv[]) { const char *program = rasqal_basename(argv[0]); rasqal_rowsource *rowsource = NULL; rasqal_world* world = NULL; rasqal_query* query = NULL; raptor_sequence* row_seq = NULL; raptor_sequence* expr_seq = NULL; int failures = 0; rasqal_variables_table* vt; rasqal_rowsource *input_rs = NULL; int vars_count; raptor_sequence* vars_seq = NULL; int test_id; world = rasqal_new_world(); if(!world || rasqal_world_open(world)) { fprintf(stderr, "%s: rasqal_world init failed\n", program); return(1); } query = rasqal_new_query(world, "sparql", NULL); vt = query->vars_table; for(test_id = 0; test_id < GROUP_TESTS_COUNT; test_id++) { int expected_rows_count = test_data[test_id].rows; int expected_vars_count = test_data[test_id].vars; const int* expected_group_ids = test_data[test_id].group_ids; int expected_ngroups = test_data[test_id].ngroups; raptor_sequence* seq = NULL; int count; int size; int i; int groups_counted; int last_group_id; vars_count = expected_vars_count; row_seq = rasqal_new_row_sequence(world, vt, test_data[test_id].data, vars_count, &vars_seq); if(row_seq) { input_rs = rasqal_new_rowsequence_rowsource(world, query, vt, row_seq, vars_seq); /* vars_seq and row_seq are now owned by input_rs */ vars_seq = row_seq = NULL; } if(!input_rs) { fprintf(stderr, "%s: failed to create rowsequence rowsource\n", program); failures++; goto tidy; } #ifdef HAVE_RAPTOR2_API expr_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression, (raptor_data_print_handler)rasqal_expression_print); #else expr_seq = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_expression, (raptor_sequence_print_handler*)rasqal_expression_print); #endif if(test_data[test_id].expr_vars[0] != NULL) { int vindex; const unsigned char* var_name; for(vindex = 0; (var_name = (const unsigned char*)test_data[test_id].expr_vars[vindex] ); vindex++) { rasqal_variable* v; rasqal_literal *l = NULL; rasqal_expression* e = NULL; v = rasqal_variables_table_get_by_name(vt, var_name); if(v) l = rasqal_new_variable_literal(world, v); if(l) e = rasqal_new_literal_expression(world, l); if(e) raptor_sequence_push(expr_seq, e); else { fprintf(stderr, "%s: failed to create variable %s\n", program, (const char*)var_name); failures++; goto tidy; } } } rowsource = rasqal_new_groupby_rowsource(world, query, input_rs, expr_seq); /* input_rs is now owned by rowsource */ input_rs = NULL; if(!rowsource) { fprintf(stderr, "%s: failed to create groupby rowsource\n", program); failures++; goto tidy; } seq = rasqal_rowsource_read_all_rows(rowsource); if(!seq) { fprintf(stderr, "%s: test %d rasqal_rowsource_read_all_rows() returned a NULL seq for a groupby rowsource\n", program, test_id); failures++; goto tidy; } count = raptor_sequence_size(seq); if(count != expected_rows_count) { fprintf(stderr, "%s: test %d rasqal_rowsource_read_all_rows() returned %d rows for a groupby rowsource, expected %d\n", program, test_id, count, expected_rows_count); failures++; goto tidy; } size = rasqal_rowsource_get_size(rowsource); if(size != expected_vars_count) { fprintf(stderr, "%s: test %d rasqal_rowsource_get_size() returned %d columns (variables) for a groupby rowsource, expected %d\n", program, test_id, size, expected_vars_count); failures++; goto tidy; } groups_counted = 0; last_group_id = -1; for(i = 0; i < count; i++) { rasqal_row* row = raptor_sequence_get_at(seq, i); if(row->group_id != last_group_id) { groups_counted++; last_group_id = row->group_id; } if(row->group_id != expected_group_ids[i]) { fprintf(stderr, "%s: test %d row #%d has group_id %d, expected %d\n", program, test_id, i, row->group_id, expected_group_ids[i]); failures++; goto tidy; } } if(groups_counted != expected_ngroups) { fprintf(stderr, "%s: test %d returnd %d groups, expected %d\n", program, test_id, groups_counted, expected_ngroups); failures++; goto tidy; } #ifdef RASQAL_DEBUG rasqal_rowsource_print_row_sequence(rowsource, seq, stderr); #endif raptor_free_sequence(seq); seq = NULL; rasqal_free_rowsource(rowsource); rowsource = NULL; if(expr_seq) raptor_free_sequence(expr_seq); expr_seq = NULL; } tidy: if(expr_seq) raptor_free_sequence(expr_seq); if(rowsource) rasqal_free_rowsource(rowsource); if(input_rs) rasqal_free_rowsource(input_rs); if(query) rasqal_free_query(query); if(world) rasqal_free_world(world); return failures; }