Skip to content

Commit

Permalink
Fix modulo normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
timowest committed Jan 25, 2016
1 parent b7a4c16 commit 4c7bf60
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
Expand Up @@ -27,11 +27,11 @@ public final class Normalization {
// TODO simplify
private static final Pattern FULL_OPERATION = Pattern.compile(
"(?<![\\d*/\"?' ])" + "(\\b|\\(|\\s+)" +
"(" + NUMBER + WS + "[+\\-/*]" + WS + ")+" + NUMBER + WS +
"(" + NUMBER + WS + "[+\\-/*%]" + WS + ")+" + NUMBER + WS +
"(?![\\d*/\"' ])");

private static final Pattern[] OPERATIONS = {
Pattern.compile(NUMBER + WS + "([*/])" + WS + NUMBER),
Pattern.compile(NUMBER + WS + "([*/%])" + WS + NUMBER),
Pattern.compile(NUMBER + WS + "([+-])" + WS + NUMBER),
};

Expand All @@ -47,6 +47,7 @@ private static String normalizeOperation(String queryString) {
switch (operator) {
case '*': result = first.multiply(second); break;
case '/': result = first.divide(second, 10, RoundingMode.HALF_UP); break;
case '%': result = first.remainder(second); break;
case '+': result = first.add(second); break;
case '-': result = first.subtract(second); break;
default: throw new IllegalStateException();
Expand Down Expand Up @@ -78,7 +79,7 @@ public static String normalize(String queryString) {
private static boolean hasOperators(String queryString) {
for (int i = 0; i < queryString.length(); i++) {
char ch = queryString.charAt(i);
if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {
return true;
}
}
Expand Down
Expand Up @@ -68,6 +68,12 @@ public void Normalize_Division() {
assertEquals("where 2.6 = 2.6", Normalization.normalize("where 5.2 / 2 = 2.6"));
}

@Test
public void Normalize_Modulo() {
assertEquals("1", Normalization.normalize("4%3"));
assertEquals("3", Normalization.normalize("2 + 4%3"));
}

@Test
public void Mixed() {
assertEquals("13", Normalization.normalize("2 * 5 + 3"));
Expand Down
2 changes: 0 additions & 2 deletions querydsl-sql/src/test/java/com/mysema/query/Connections.java
Expand Up @@ -17,8 +17,6 @@
import java.util.Map;
import java.util.Properties;

import org.hsqldb.types.Types;

import com.google.common.collect.Maps;
import com.mysema.query.ddl.CreateTableClause;
import com.mysema.query.ddl.DropTableClause;
Expand Down
14 changes: 14 additions & 0 deletions querydsl-sql/src/test/java/com/mysema/query/SelectBase.java
Expand Up @@ -1112,6 +1112,20 @@ public void Math() {
assertEquals(Math.tanh(0.5), singleResult(MathExpressions.tanh(expr)), 0.001);
}

@Test
@ExcludeIn(DERBY) // Derby doesn't support mod with decimal operands
public void Math2() {
// 1.0 + 2.0 * 3.0 - 4.0 / 5.0 + 6.0 % 3.0
NumberExpression<Double> one = Expressions.numberTemplate(Double.class, "1.0");
NumberExpression<Double> two = Expressions.numberTemplate(Double.class, "2.0");
NumberExpression<Double> three = Expressions.numberTemplate(Double.class, "3.0");
NumberExpression<Double> four = Expressions.numberTemplate(Double.class, "4.0");
NumberExpression<Double> five = Expressions.numberTemplate(Double.class, "5.0");
NumberExpression<Double> six = Expressions.numberTemplate(Double.class, "6.0");
Double num = query().singleResult(one.add(two.multiply(three)).subtract(four.divide(five)).add(six.mod(three)));
assertEquals(6.2, num, 0.001);
}

@Test
public void Nested_Tuple_Projection() {
Concatenation concat = new Concatenation(employee.firstname, employee.lastname);
Expand Down

0 comments on commit 4c7bf60

Please sign in to comment.