Skip to content

Commit e63ca9f

Browse files
authored
Merge pull request #4432 from evolvedbinary/bugfix/div-error
Fix div operator error when arg is a variable.
2 parents 7779435 + f32ff68 commit e63ca9f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

exist-core/src/main/java/org/exist/xquery/OpNumeric.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ public OpNumeric(XQueryContext context, Expression left, Expression right, Arith
7878
} else if (ltype == Type.NUMBER || rtype == Type.NUMBER) {
7979
// if one of both operands returns a number, we can safely assume
8080
// the return type of the whole expression will be a number
81-
returnType = Type.NUMBER;
81+
if(operator == ArithmeticOperator.DIVISION) {
82+
returnType = Type.DECIMAL;
83+
}else {
84+
returnType = Type.NUMBER;
85+
}
8286
}
8387
}
8488
add(left);

exist-core/src/test/java/org/exist/xquery/XQueryTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,41 @@ public void divYieldsWrongInf_1816496() throws XMLDBException {
25102510
assertEquals(query, "INF",
25112511
result.getResource(0).getContent().toString());
25122512
}
2513+
2514+
/**
2515+
* @see https://github.com/eXist-db/exist/issues/3441
2516+
*/
2517+
@Test
2518+
public void divErrorArgVariable() throws XMLDBException {
2519+
String query = "let $x := 2 " +
2520+
"return 1 div $x * 4";
2521+
2522+
XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
2523+
ResourceSet result = service.query(query);
2524+
2525+
assertEquals(1, result.getSize());
2526+
2527+
assertEquals(query, "2", result.getResource(0).getContent().toString());
2528+
2529+
}
2530+
2531+
/**
2532+
* @see https://github.com/eXist-db/exist/issues/3441
2533+
*/
2534+
@Test
2535+
public void divErrorArgVariable2() throws XMLDBException {
2536+
String query = "let $x := 2 \n" +
2537+
"let $y := 1 div $x * 4\n" +
2538+
"return $y";
2539+
2540+
XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
2541+
ResourceSet result = service.query(query);
2542+
2543+
assertEquals(1, result.getSize());
2544+
2545+
assertEquals(query, "2", result.getResource(0).getContent().toString());
2546+
2547+
}
25132548

25142549
/**
25152550
* @see http://sourceforge.net/support/tracker.php?aid=1841635

0 commit comments

Comments
 (0)