/* * rasqal_expression_evaluate_uri_constructor: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_URI and RASQAL_EXPR_IRI (string) * expressions. * * Return value: A #rasqal_literal URI value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_uri_constructor(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; raptor_uri* dt_uri; const unsigned char *s; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; s = rasqal_literal_as_string_flags(l1, eval_context->flags, error_p); if(*error_p) goto failed; dt_uri = raptor_new_uri_relative_to_base(world->raptor_world_ptr, eval_context->base_uri, s); rasqal_free_literal(l1); l1 = NULL; if(!dt_uri) goto failed; /* after this dt_uri is owned by the result literal */ return rasqal_new_uri_literal(world, dt_uri); failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
static int rasqal_builtin_agg_expression_execute_step(void* user_data, raptor_sequence* literals) { rasqal_builtin_agg_expression_execute* b; rasqal_literal* l; int i; b = (rasqal_builtin_agg_expression_execute*)user_data; if(b->error) return 1; if(b->expr->op == RASQAL_EXPR_COUNT) { /* COUNT(*) : counts every row (does not care about literals) */ if(b->expr->arg1->op == RASQAL_EXPR_VARSTAR) b->count++; /* COUNT(expr list) : counts rows with non-empty sequence of literals */ else if(raptor_sequence_size(literals) > 0) b->count++; return 0; } /* Other aggregate functions count every row */ b->count++; for(i = 0; (l = (rasqal_literal*)raptor_sequence_get_at(literals, i)); i++) { rasqal_literal* result = NULL; if(b->expr->op == RASQAL_EXPR_SAMPLE) { /* Sample chooses the first literal it sees */ if(!b->l) b->l = rasqal_new_literal_from_literal(l); break; } if(b->expr->op == RASQAL_EXPR_GROUP_CONCAT) { const unsigned char* str; int error = 0; str = (const unsigned char*)rasqal_literal_as_string_flags(l, 0, &error); if(!error) { if(raptor_stringbuffer_length(b->sb)) raptor_stringbuffer_append_counted_string(b->sb, b->separator, 1, 1); raptor_stringbuffer_append_string(b->sb, str, 1); } continue; } if(!b->l) result = rasqal_new_literal_from_literal(l); else { if(b->expr->op == RASQAL_EXPR_SUM || b->expr->op == RASQAL_EXPR_AVG) { result = rasqal_literal_add(b->l, l, &b->error); } else if(b->expr->op == RASQAL_EXPR_MIN) { int cmp = rasqal_literal_compare(b->l, l, 0, &b->error); if(cmp <= 0) result = rasqal_new_literal_from_literal(b->l); else result = rasqal_new_literal_from_literal(l); } else if(b->expr->op == RASQAL_EXPR_MAX) { int cmp = rasqal_literal_compare(b->l, l, 0, &b->error); if(cmp >= 0) result = rasqal_new_literal_from_literal(b->l); else result = rasqal_new_literal_from_literal(l); } else { RASQAL_FATAL2("Builtin aggregation operation %d is not implemented", b->expr->op); } rasqal_free_literal(b->l); if(!result) b->error = 1; } b->l = result; #if RASQAL_DEBUG > 1 RASQAL_DEBUG3("Aggregation step result %s (error=%d)\n", (result ? (const char*)rasqal_literal_as_string(result) : "(NULL)"), b->error); #endif if(b->error) break; } return b->error; }
/* * rasqal_expression_evaluate_strdt: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_STRDT(expr) expression. * * Return value: A #rasqal_literal string value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_strdt(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal *l1 = NULL; rasqal_literal *l2 = NULL; const unsigned char* s = NULL; unsigned char* new_s = NULL; size_t len; raptor_uri* dt_uri = NULL; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; if(l1->language || l1->datatype) { /* not a simple literal so return NULL success */ rasqal_free_literal(l1); return NULL; } s = rasqal_literal_as_counted_string(l1, &len, eval_context->flags, error_p); if(*error_p) goto failed; l2 = rasqal_expression_evaluate2(e->arg2, eval_context, error_p); if(*error_p || !l2) goto failed; dt_uri = rasqal_literal_as_uri(l2); if(dt_uri) { dt_uri = raptor_uri_copy(dt_uri); } else { const unsigned char *uri_string; uri_string = rasqal_literal_as_string_flags(l2, eval_context->flags, error_p); if(*error_p) goto failed; dt_uri = raptor_new_uri(world->raptor_world_ptr, uri_string); if(!dt_uri) goto failed; } new_s =(unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy(new_s, s, len + 1); rasqal_free_literal(l1); rasqal_free_literal(l2); /* after this new_s and dt_uri become owned by result */ return rasqal_new_string_literal(world, new_s, /* language */ NULL, dt_uri, /* qname */ NULL); failed: if(error_p) *error_p = 1; if(new_s) RASQAL_FREE(cstring, new_s); if(l1) rasqal_free_literal(l1); if(l2) rasqal_free_literal(l2); return NULL; }
int main(int argc, char **argv) { const char *program=rasqal_basename(argv[0]); const char *query_language_name=QUERY_LANGUAGE; raptor_uri *base_uri; unsigned char *uri_string; int test_i; limit_test* test; int tests_failed_count=0; int single_shot= -1; int query_i; rasqal_world *world; world=rasqal_new_world(); if(!world || rasqal_world_open(world)) { fprintf(stderr, "%s: rasqal_world init failed\n", program); return(1); } if(argc < 2 || argc > 3) { fprintf(stderr, "USAGE: %s data-filename [test number]\n", program); return(1); } if(argv[2]) single_shot=atoi(argv[2]); uri_string=raptor_uri_filename_to_uri_string(""); base_uri = raptor_new_uri(world->raptor_world_ptr, uri_string); raptor_free_memory(uri_string); for(query_i=0; query_i < NQUERIES; query_i++) { const char *query_format = limit_queries[query_i]; int dynamic_limits = (strstr(query_format, "%s %s") != NULL); for(test_i=(single_shot >=0 ? single_shot : 0); (test=&limit_offset_tests[test_i]) && (test->expected_count >=0); test_i++) { int result_i; rasqal_query *query = NULL; rasqal_query_results *results = NULL; const char* expected_results; int test_ok=1; unsigned char *data_string; unsigned char *query_string; size_t qs_len; #define LIM_OFF_BUF_SIZE 20 data_string=raptor_uri_filename_to_uri_string(argv[1]); qs_len = strlen((const char*)data_string) + strlen(query_format) + (2 * LIM_OFF_BUF_SIZE); query_string = RASQAL_MALLOC(unsigned char*, qs_len + 1); if(dynamic_limits) { char lim[LIM_OFF_BUF_SIZE]; char off[LIM_OFF_BUF_SIZE]; if(test->limit >= 0) snprintf(lim, LIM_OFF_BUF_SIZE, "LIMIT %d", test->limit); else *lim = '\0'; if(test->offset >= 0) snprintf(off, LIM_OFF_BUF_SIZE, "OFFSET %d", test->offset); else *off = '\0'; IGNORE_FORMAT_NONLITERAL_START snprintf((char*)query_string, qs_len, query_format, data_string, lim, off); IGNORE_FORMAT_NONLITERAL_END } else { IGNORE_FORMAT_NONLITERAL_START snprintf((char*)query_string, qs_len, query_format, data_string); IGNORE_FORMAT_NONLITERAL_END } raptor_free_memory(data_string); query = rasqal_new_query(world, query_language_name, NULL); if(!query) { fprintf(stderr, "%s: creating query in language %s FAILED\n", program, query_language_name); return(1); } #if defined(RASQAL_DEBUG) && RASQAL_DEBUG > 1 fprintf(stderr, "%s: preparing query %d test %d - %s\n", program, query_i, test_i, query_string); #endif if(rasqal_query_prepare(query, query_string, base_uri)) { fprintf(stderr, "%s: query %d test %d prepare '%s' FAILED\n", program, query_i, test_i, query_string); return(1); } if(!dynamic_limits) { rasqal_query_set_limit(query, test->limit); rasqal_query_set_offset(query, test->offset); } #if defined(RASQAL_DEBUG) && RASQAL_DEBUG > 1 fprintf(stderr, "%s: executing query %d test %d\n", program, query_i, test_i); #endif results=rasqal_query_execute(query); if(!results) { fprintf(stderr, "%s: query %d test %d FAILED\n", program, query_i, test_i); return(1); } /* check results */ expected_results=test->expected_results[query_i]; result_i=0; tests_failed_count=0; while(results && !rasqal_query_results_finished(results)) { rasqal_literal *value; const char* str; char expected_str[2]={0,0}; int failed=1; if(result_i < test->expected_count) expected_str[0]=expected_results[result_i]; value=rasqal_query_results_get_binding_value(results, 0); if(value) { int error=0; str=(const char*)rasqal_literal_as_string_flags(value, 0, &error); if(!error && str) { size_t len=strlen(str); if(len == 1 && str[0] == expected_str[0]) failed=0; } } if(failed) { fprintf(stderr, "%s: query %d test %d result %d FAILED returning value '%s' expected value '%s'\n", program, query_i, test_i, result_i, (str ? str: "NULL"), expected_str); test_ok=0; } rasqal_query_results_next(results); result_i++; } if(results) rasqal_free_query_results(results); if(!test_ok) { fprintf(stderr, "%s: query %d test %d limit %d offset %d FAILED\n", program, query_i, test_i, test->limit, test->offset); } else { if(result_i != test->expected_count) { fprintf(stderr, "%s: query %d test %d limit %d offset %d FAILED returned %d results, expected %d\n", program, query_i, test_i, test->limit, test->offset, result_i, test->expected_count); test_ok=0; } } if(!test_ok) { tests_failed_count++; } else { #if defined(RASQAL_DEBUG) && RASQAL_DEBUG > 1 fprintf(stderr, "%s: query %d test %d OK\n", program, query_i, test_i); #endif } rasqal_free_query(query); RASQAL_FREE(char*, query_string); if(single_shot >=0) break; } /* end test loop */ } /* end query loop */