提交 aac5e958 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #498 from yuanwhy/fix-round-function

Fix h2 ROUND
...@@ -1234,7 +1234,11 @@ public class Function extends Expression implements FunctionCall { ...@@ -1234,7 +1234,11 @@ public class Function extends Expression implements FunctionCall {
break; break;
case ROUND: { case ROUND: {
double f = v1 == null ? 1. : Math.pow(10., v1.getDouble()); double f = v1 == null ? 1. : Math.pow(10., v1.getDouble());
result = ValueDouble.get(Math.round(v0.getDouble() * f) / f);
double middleResult = v0.getDouble() * f;
int oneWithSymbol = middleResult > 0 ? 1 : -1;
result = ValueDouble.get(Math.round(Math.abs(middleResult)) / f * oneWithSymbol);
break; break;
} }
case TRUNCATE: { case TRUNCATE: {
......
...@@ -117,6 +117,7 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -117,6 +117,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
testThatCurrentTimestampStaysTheSameWithinATransaction(); testThatCurrentTimestampStaysTheSameWithinATransaction();
testThatCurrentTimestampUpdatesOutsideATransaction(); testThatCurrentTimestampUpdatesOutsideATransaction();
testAnnotationProcessorsOutput(); testAnnotationProcessorsOutput();
testRound();
deleteDb("functions"); deleteDb("functions");
} }
...@@ -2010,6 +2011,28 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -2010,6 +2011,28 @@ public class TestFunctions extends TestBase implements AggregateFunction {
} }
} }
private void testRound() throws SQLException {
deleteDb("functions");
Connection conn = getConnection("functions");
Statement stat = conn.createStatement();
final ResultSet rs = stat.executeQuery(
"select ROUND(-1.2), ROUND(-1.5), ROUND(-1.6), ROUND(2), ROUND(1.5), ROUND(1.8), ROUND(1.1) from dual");
rs.next();
assertEquals(-1, rs.getInt(1));
assertEquals(-2, rs.getInt(2));
assertEquals(-2, rs.getInt(3));
assertEquals(2, rs.getInt(4));
assertEquals(2, rs.getInt(5));
assertEquals(2, rs.getInt(6));
assertEquals(1, rs.getInt(7));
rs.close();
conn.close();
}
private void testThatCurrentTimestampIsSane() throws SQLException, private void testThatCurrentTimestampIsSane() throws SQLException,
ParseException { ParseException {
deleteDb("functions"); deleteDb("functions");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论