Beispiel #1
0
fs_binding *fs_binding_apply_filters(fs_query *q, int block, fs_binding *b, raptor_sequence *constr)
{
    fs_binding *ret = fs_binding_copy(b);
    if (!constr) {
        /* if there's no constriants then we don't need to do anything */

        return ret;
    }
    for (int col=0; b[col].name; col++) {
        ret[col].vals->length = 0;
    }
    int length = fs_binding_length(b);
    fs_binding *restore = q->bt;
    q->bt = b;
    /* TODO should prefetch lexical vals here */
    /* expressions that have been optimised out will be replaces with NULL,
     * so we have to be careful here */
/* --------------------------- */
/* PREFETCH should go here XXX */
/* --------------------------- */
    for (int row=0; row<length; row++) {
        for (int c=0; c<raptor_sequence_size(constr); c++) {
            rasqal_expression *e =
                raptor_sequence_get_at(constr, c);
            if (!e) continue;

            fs_value v = fs_expression_eval(q, row, block, e);
#ifdef DEBUG_FILTER
            rasqal_expression_print(e, stdout);
            printf(" -> ");
            fs_value_print(v);
            printf("\n");
#endif
            if (v.valid & fs_valid_bit(FS_V_TYPE_ERROR) && v.lex) {
                q->warnings = g_slist_prepend(q->warnings, v.lex);
            }
            fs_value result = fn_ebv(v);
            /* its EBV is not true, so we skip to the next one */
            if (result.valid & fs_valid_bit(FS_V_TYPE_ERROR) || !result.in) {
                continue;
            }
            for (int col=0; b[col].name; col++) {
                if (b[col].bound) {
                    fs_rid_vector_append(ret[col].vals, b[col].vals->data[row]);
                }
            }
        }
    }
    q->bt = restore;

    return ret;
}
Beispiel #2
0
void fs_values_order(fs_query *q) {
    int conditions;
    for (conditions = 0; rasqal_query_get_order_condition(q->rq, conditions);
            conditions++); 
    int length = q->agg_values->len;
    fs_value *ordervals = malloc(length * conditions * sizeof(fs_value));
    struct order_row *orows = malloc(sizeof(struct order_row) * length);
    for (int i=0; i<length; i++) {
	for (int j=0; j<conditions; j++) {
	    ordervals[i * conditions + j] = fs_expression_eval(q, i, 0,
				rasqal_query_get_order_condition(q->rq, j));

#ifdef DEBUG_ORDER
printf("@@_ ORDER VAL (%d, %d) = ", i, j);
fs_value_print(ordervals[i * conditions + j]);
printf("\n");
#endif
	}
        orows[i].row = i;
        orows[i].width = conditions;
        orows[i].vals = ordervals + (i * conditions);
    }

    qsort(orows, length, sizeof(struct order_row), orow_compare);

    int *ordering = malloc(sizeof(int) * length);
    for (int i=0; i<length; i++) {
        ordering[i] = orows[i].row;
    }
#ifdef DEBUG_ORDER
printf("Output order:\n");
for (int i=0; i<length; i++) {
    printf("output row %d row %d\n", i, ordering[i]);
}
#endif

    q->ordering = ordering;
    free(ordervals);
    free(orows);
}
Beispiel #3
0
void fs_query_order(fs_query *q)
{
    int conditions;

    for (conditions = 0; rasqal_query_get_order_condition(q->rq, conditions);
            conditions++);
    const int length = q->length;
#ifdef DEBUG_ORDER
printf("@@ ORDER (%d x %d)\n", conditions, length);
#endif

    /* trap trivial cases */
    if (conditions == 0 || length == 0) {
        return;
    }

    /* spot the case where we have ORDER BY ?x, saves evaluating expressions */
    if (conditions == 1) {
        rasqal_expression *oe = rasqal_query_get_order_condition(q->rq, 0);
        if ((oe->op == RASQAL_EXPR_ORDER_COND_ASC ||
             oe->op == RASQAL_EXPR_ORDER_COND_DESC) &&
            oe->arg1->op == RASQAL_EXPR_LITERAL &&
            oe->arg1->literal->type == RASQAL_LITERAL_VARIABLE) {
            long int col = (long int)oe->arg1->literal->value.variable->user_data;
            if (col == 0) {
                fs_error(LOG_CRIT, "missing column");

                return;
            }
            int *ordering;
            if (!fs_sort_column(q, q->bt, col, &ordering)) {
                if (oe->op == RASQAL_EXPR_ORDER_COND_DESC) {
                    reverse_array(ordering, q->bt[col].vals->length);
                }
                q->ordering = ordering;

                return;
            }
        }
    }

    struct order_row *orows = malloc(sizeof(struct order_row) * length);
    fs_value *ordervals = malloc(length * conditions * sizeof(fs_value));
    for (int i=0; i<length; i++) {
	for (int j=0; j<conditions; j++) {
	    ordervals[i * conditions + j] = fs_expression_eval(q, i, 0,
				rasqal_query_get_order_condition(q->rq, j));

#ifdef DEBUG_ORDER
printf("@@ ORDER VAL (%d, %d) = ", i, j);
fs_value_print(ordervals[i * conditions + j]);
printf("\n");
#endif
	}
        orows[i].row = i;
        orows[i].width = conditions;
        orows[i].vals = ordervals + (i * conditions);
    }

    qsort(orows, length, sizeof(struct order_row), orow_compare);

    int *ordering = malloc(sizeof(int) * length);
    for (int i=0; i<length; i++) {
        ordering[i] = orows[i].row;
    }
#ifdef DEBUG_ORDER
printf("Output order:\n");
for (int i=0; i<length; i++) {
    printf("output row %d row %d\n", i, ordering[i]);
}
#endif

    q->ordering = ordering;
    free(ordervals);
    free(orows);
}