Skip to content

Commit 29da3ea

Browse files
[MOD] XQuery: optimizations on sequence functions
1 parent 78041ec commit 29da3ea

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

basex-core/src/main/java/org/basex/query/func/fn/FnReverse.java

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ protected Expr opt(final CompileContext cc) throws QueryException {
7676
// reverse sequence
7777
if(expr instanceof RangeSeq) return ((RangeSeq) expr).reverse(cc.qc);
7878

79+
// reverse(reverse(E)) -> E
80+
if(REVERSE.is(expr)) return expr.arg(0);
7981
// reverse(tail(reverse(E)) -> util:init(E)
8082
if(TAIL.is(expr) && REVERSE.is(expr.arg(0)))
8183
return cc.function(_UTIL_INIT, info, expr.arg(0).args());

basex-core/src/main/java/org/basex/query/func/fn/FnSort.java

+5
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ protected Expr opt(final CompileContext cc) throws QueryException {
135135
final SeqType st = expr1.arg(0).seqType();
136136
if(st.zeroOrOne() && st.type.isSortable()) return expr1;
137137
}
138+
if(REVERSE.is(expr1) || SORT.is(expr1)) {
139+
final Expr[] args = exprs.clone();
140+
args[0] = args[0].arg(0);
141+
return cc.function(SORT, info, args);
142+
}
138143
if(exprs.length == 3) {
139144
exprs[2] = coerceFunc(exprs[2], cc, SeqType.ANY_ATOMIC_TYPE_ZM, st1.with(Occ.EXACTLY_ONE));
140145
}

basex-core/src/main/java/org/basex/query/func/util/UtilDdo.java

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ protected Expr opt(final CompileContext cc) throws QueryException {
4646
if(type instanceof NodeType) {
4747
// util:ddo(util:replicate(*, 2)) -> util:ddo(*)
4848
if(_UTIL_REPLICATE.is(expr) && ((UtilReplicate) expr).singleEval(false)) return expr.arg(0);
49+
// util:ddo(reverse(*)) -> util:ddo(*)
50+
if(REVERSE.is(expr) || SORT.is(expr)) return cc.function(_UTIL_DDO, info, expr.arg(0));
4951
// util:ddo(/a/b/c) -> /a/b/c
5052
if(expr.ddo()) return expr;
5153
// adopt type of argument

basex-core/src/test/java/org/basex/query/func/FnModuleTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,8 @@ public final class FnModuleTest extends QueryPlanTest {
834834

835835
check(func.args(" (<_/>, ('a', 'b'))"),
836836
"b\na\n<_/>", empty(func));
837-
837+
check("(<a/>, <b/>)[. = ''] =>" + func.args() + " =>" + func.args(),
838+
"<a/>\n<b/>", empty(func));
838839
}
839840

840841
/** Test method. */

basex-core/src/test/java/org/basex/query/func/UtilModuleTest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,19 @@ public final class UtilModuleTest extends QueryPlanTest {
227227
final Function func = _UTIL_DDO;
228228
query(func.args(" <a/>"), "<a/>");
229229
query(func.args(" (<a/>, <b/>)"), "<a/>\n<b/>");
230-
query(func.args(" reverse((<a/>, <b/>))"), "<b/>\n<a/>");
230+
231+
check(func.args(" util:replicate((<a/>, <b/>), 10)"), "<a/>\n<b/>",
232+
empty(_UTIL_REPLICATE));
233+
check(func.args(" util:replicate(<a/>, 2, true())"), "<a/>\n<a/>",
234+
exists(_UTIL_REPLICATE));
235+
236+
check("(<a><b/></a> ! (., *)) => reverse() => " + func.args(),
237+
"<a>\n<b/>\n</a>\n<b/>", empty(REVERSE));
238+
check("(<a><b/></a> ! (., *)) => sort() => " + func.args(),
239+
"<a>\n<b/>\n</a>\n<b/>", empty(SORT));
240+
check("(<a><b/></a> ! (., *)) => sort() => reverse() => sort() => " + func.args(),
241+
"<a>\n<b/>\n</a>\n<b/>", empty(SORT), empty(REVERSE));
242+
231243
error(func.args(1), INVCONVERT_X_X_X);
232244
}
233245

0 commit comments

Comments
 (0)