Permalink
Browse files

Merge pull request #28 from davechallis/master

Added support for ABS() function, bugfixes to: fs_decimal_init_from_str, fn_minus, tests/httpd/post
  • Loading branch information...
2 parents 50eddbf + c72b0a3 commit a16b1c8d5994fa2a7efb9fbaf2df664e8ba85dc1 garlik committed Jun 10, 2011
View
@@ -0,0 +1,25 @@
+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+
+<a> <test:num> "0"^^xsd:integer .
+<b> <test:num> "-0"^^xsd:integer .
+<c> <test:num> "+0"^^xsd:integer .
+
+<d> <test:num> "1234"^^xsd:integer .
+<e> <test:num> "-1234"^^xsd:integer .
+<f> <test:num> "+1234"^^xsd:integer .
+
+<g> <test:num> "1234567890123456789"^^xsd:integer .
+<h> <test:num> "-1234567890123456789"^^xsd:integer .
+<i> <test:num> "+1234567890123456789"^^xsd:integer .
+
+<j> <test:num> "1234567890.23239300"^^xsd:decimal .
+<k> <test:num> "-1234567890.23239300"^^xsd:decimal .
+<l> <test:num> "+1234567890.23239300"^^xsd:decimal .
+
+<m> <test:num> "23.0e999999999999999999999999"^^xsd:float .
+<n> <test:num> "+23.0e999999999999999999999999"^^xsd:float .
+<o> <test:num> "-23.0e999999999999999999999999"^^xsd:float .
+
+<p> <test:num> "12345678901234567890.23239300"^^xsd:float .
+<q> <test:num> "-12345678901234567890.23239300"^^xsd:float .
+<r> <test:num> "+12345678901234567890.23239300"^^xsd:float .
View
@@ -136,16 +136,16 @@ int fs_decimal_init_from_str(fs_decimal *d, const char *str)
fs_decimal_init(d);
int decpos = -1;
- int negative = 0;
+ int has_sign = 0;
int len = strlen(str);
for (int cpos = 0; str[cpos]; cpos++) {
if (str[cpos] < '0' || str[cpos] > '9') {
if (str[cpos] == '-' && cpos == 0) {
- negative = 1;
+ has_sign = 1;
d->flags = FS_D_NEGATIVE;
} else if (str[cpos] == '+' && cpos == 0) {
- /* do nothing, +ve is default */
+ has_sign = 1;
} else if (str[cpos] == '.') {
if (decpos == -1) {
decpos = cpos;
@@ -162,7 +162,7 @@ int fs_decimal_init_from_str(fs_decimal *d, const char *str)
}
}
if (decpos == -1) decpos = len;
- int offset = negative ? 1 : 0;
+ int offset = has_sign ? 1 : 0;
for (int cpos = offset; cpos < decpos; cpos++) {
int dpos = FS_D_OVER_DIGITS + FS_D_INT_DIGITS - decpos + cpos;
View
@@ -248,7 +248,7 @@ fs_value fn_plus(fs_query *q, fs_value a)
fs_value_print(a);
#endif
if (fs_is_numeric(&a)) {
- return a;
+ return a;
}
return fs_value_error(FS_ERROR_INVALID_TYPE, "bad arguments to fn:plus");
@@ -257,22 +257,52 @@ fs_value_print(a);
fs_value fn_minus(fs_query *q, fs_value a)
{
if (fs_is_numeric(&a)) {
- if (a.attr == fs_c.xsd_double || a.attr == fs_c.xsd_float) {
- a.fp = -a.fp;
- } else if (a.attr == fs_c.xsd_decimal) {
+ if (a.attr == fs_c.xsd_double || a.attr == fs_c.xsd_float) {
+ a.fp = -a.fp;
+ } else if (a.attr == fs_c.xsd_decimal) {
fs_decimal_negate(&a.de, &a.de);
- } else if (a.attr == fs_c.xsd_integer) {
- a.in = -a.in;
- } else {
- return fs_value_error(FS_ERROR_INVALID_TYPE, "bad arguments to fn:minus");
- }
+ } else if (a.attr == fs_c.xsd_integer) {
+ a.in = -a.in;
+ } else {
+ return fs_value_error(FS_ERROR_INVALID_TYPE, "bad arguments to fn:minus");
+ }
- return a;
+ if (a.lex != NULL) {
+ a.lex = NULL;
+ }
+ return a;
}
return fs_value_error(FS_ERROR_INVALID_TYPE, "non-numeric arguments to fn:minus");
}
+fs_value fn_numeric_abs(fs_query *q, fs_value a)
+{
+ if (!fs_is_numeric(&a)) {
+ return fs_value_error(FS_ERROR_INVALID_TYPE, "non-numeric arguments to fn:abs");
+ }
+
+ if (a.attr == fs_c.xsd_double || a.attr == fs_c.xsd_float) {
+ a.fp = fabs(a.fp);
+ } else if (a.attr == fs_c.xsd_decimal) {
+ if (fs_decimal_less_than(&a.de, fs_decimal_zero)) {
+ fs_decimal_negate(&a.de, &a.de);
+ }
+ } else if (a.attr == fs_c.xsd_integer) {
+ if (a.in < 0) {
+ a.in = -a.in;
+ }
+ } else {
+ return fs_value_error(FS_ERROR_INVALID_TYPE, "bad arguments to fn:abs");
+ }
+
+ if (a.lex != NULL) {
+ a.lex = NULL;
+ }
+
+ return a;
+}
+
fs_value fn_numeric_add(fs_query *q, fs_value a, fs_value b)
{
#if 0
View
@@ -23,6 +23,7 @@ fs_value fn_less_than_equal(fs_query *q, fs_value a, fs_value b);
fs_value fn_greater_than_equal(fs_query *q, fs_value a, fs_value b);
/* binary maths operators */
+fs_value fn_numeric_abs(fs_query *q, fs_value a);
fs_value fn_numeric_add(fs_query *q, fs_value a, fs_value b);
fs_value fn_numeric_subtract(fs_query *q, fs_value a, fs_value b);
fs_value fn_numeric_multiply(fs_query *q, fs_value a, fs_value b);
View
@@ -240,6 +240,10 @@ fs_value fs_expression_eval(fs_query *q, int row, int block, rasqal_expression *
}
switch (e->op) {
+#if RASQAL_VERSION >= 925
+ case RASQAL_EXPR_ABS:
+ return fn_numeric_abs(q, fs_expression_eval(q, row, block, e->arg1));
+#endif
case RASQAL_EXPR_AND:
return fn_logical_and(q, fs_expression_eval(q, row, block, e->arg1),
fs_expression_eval(q, row, block, e->arg2));
@@ -0,0 +1,24 @@
+201 imported successfully
+This is a 4store SPARQL server [VERSION]
+Query: SELECT ?s (ABS(?d) AS ?abs_d) WHERE { ?s <test:num> ?d . } ORDER BY ?s
+?s ?abs_d
+<http://example.com/a> "0"^^<http://www.w3.org/2001/XMLSchema#integer>
+<http://example.com/b> "0"^^<http://www.w3.org/2001/XMLSchema#integer>
+<http://example.com/c> "0"^^<http://www.w3.org/2001/XMLSchema#integer>
+<http://example.com/d> "1234"^^<http://www.w3.org/2001/XMLSchema#integer>
+<http://example.com/e> "1234"^^<http://www.w3.org/2001/XMLSchema#integer>
+<http://example.com/f> "1234"^^<http://www.w3.org/2001/XMLSchema#integer>
+<http://example.com/g> "1234567890123456789"^^<http://www.w3.org/2001/XMLSchema#integer>
+<http://example.com/h> "1234567890123456789"^^<http://www.w3.org/2001/XMLSchema#integer>
+<http://example.com/i> "1234567890123456789"^^<http://www.w3.org/2001/XMLSchema#integer>
+<http://example.com/j> "1234567890.232393"^^<http://www.w3.org/2001/XMLSchema#decimal>
+<http://example.com/k> "1234567890.232393"^^<http://www.w3.org/2001/XMLSchema#decimal>
+<http://example.com/l> "1234567890.232393"^^<http://www.w3.org/2001/XMLSchema#decimal>
+<http://example.com/m> "inf"^^<http://www.w3.org/2001/XMLSchema#float>
+<http://example.com/n> "inf"^^<http://www.w3.org/2001/XMLSchema#float>
+<http://example.com/o> "inf"^^<http://www.w3.org/2001/XMLSchema#float>
+<http://example.com/p> "1.23457e+19"^^<http://www.w3.org/2001/XMLSchema#float>
+<http://example.com/q> "1.23457e+19"^^<http://www.w3.org/2001/XMLSchema#float>
+<http://example.com/r> "1.23457e+19"^^<http://www.w3.org/2001/XMLSchema#float>
+200 deleted successfully
+This is a 4store SPARQL server [VERSION]
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+source sparql.sh
+
+put "$EPR" ../../data/numbers.ttl 'text/turtle' 'http://example.com/numbers.ttl'
+
+sparql "$EPR" 'SELECT ?s (ABS(?d) AS ?abs_d) WHERE { ?s <test:num> ?d . } ORDER BY ?s'
+
+delete "$EPR" 'http://example.com/numbers.ttl'
View
@@ -6,6 +6,8 @@ post "$EPR" '<test:a> <test:b> <test:c> .' 'text/turtle' 'http://example.org/tes
sparql "$EPR" 'SELECT * WHERE { ?x ?y ?z } ORDER BY ?x ?y ?z'
post "$EPR" '<test:d> <test:e> <test:f> . <test:d> <test:g> "text\nwith\tspaces" .' 'text/turtle' 'http://example.org/test1'
sparql "$EPR" 'SELECT * WHERE { ?x ?y ?z } ORDER BY ?x ?y ?z'
-put "$EPR" /dev/null 'text/turtle' 'http://example.org/test1'
+TEMPFILE=`mktemp`
+put "$EPR" "$TEMPFILE" 'text/turtle' 'http://example.org/test1'
+rm $TEMPFILE
sparql "$EPR" 'SELECT * WHERE { ?x ?y ?z } ORDER BY ?x ?y ?z'
delete "$EPR" 'http://example.org/test1'

0 comments on commit a16b1c8

Please sign in to comment.