db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r772449 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/types/NumberDataType.java testing/org/apache/derbyTesting/functionTests/tests/lang/FloatTypesTest.java testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
Date Wed, 06 May 2009 22:30:36 GMT
Author: kahatlen
Date: Wed May  6 22:30:35 2009
New Revision: 772449

URL: http://svn.apache.org/viewvc?rev=772449&view=rev
Log:
DERBY-2447: ejbql and floattypes fail intermittently

A bug in the HotSpot optimization made normalization of floating point
values return negative zero instead of positive zero. This patch adds
a workaround for the bug and a regression test. The workaround also
makes the intent of the code clearer.

For details about the HotSpot bug, see:
http://bugs.sun.com/view_bug.do?bug_id=6833879

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FloatTypesTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/NumberDataType.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/NumberDataType.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/NumberDataType.java?rev=772449&r1=772448&r2=772449&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/NumberDataType.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/NumberDataType.java Wed May
 6 22:30:35 2009
@@ -493,7 +493,10 @@
 			throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.REAL_NAME);
         }
         // Normalize negative floats to be "positive" (can't detect easily without using
Float object because -0.0f = 0.0f)
-        if (v == 0.0f) v = 0.0f;
+        // DERBY-2447: It shouldn't matter whether we compare to 0.0f or -0.0f,
+        // both should match negative zero, but comparing to 0.0f triggered
+        // this JVM bug: http://bugs.sun.com/view_bug.do?bug_id=6833879
+        if (v == -0.0f) v = 0.0f;
 
         return v;
 	}
@@ -519,7 +522,10 @@
 			throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.REAL_NAME);
         }
         // Normalize negative floats to be "positive" (can't detect easily without using
Float object because -0.0f = 0.0f)
-        if (v == 0.0d) v = 0.0d;
+        // DERBY-2447: It shouldn't matter whether we compare to 0.0d or -0.0d,
+        // both should match negative zero, but comparing to 0.0d triggered
+        // this JVM bug: http://bugs.sun.com/view_bug.do?bug_id=6833879
+        if (v == -0.0d) v = 0.0d;
 
         return (float)v;
     }
@@ -539,7 +545,10 @@
 			throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, TypeId.DOUBLE_NAME);
         }
         // Normalize negative doubles to be "positive" (can't detect easily without using
Double object because -0.0f = 0.0f)
-        if (v == 0.0d) v = 0.0d;
+        // DERBY-2447: It shouldn't matter whether we compare to 0.0d or -0.0d,
+        // both should match negative zero, but comparing to 0.0d triggered
+        // this JVM bug: http://bugs.sun.com/view_bug.do?bug_id=6833879
+        if (v == -0.0d) v = 0.0d;
 
         return v;
 	}

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FloatTypesTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FloatTypesTest.java?rev=772449&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FloatTypesTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FloatTypesTest.java
Wed May  6 22:30:35 2009
@@ -0,0 +1,77 @@
+/**
+ * Derby - Class org.apache.derbyTesting.functionTests.tests.lang.FloatTypesTest
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.derbyTesting.functionTests.tests.lang;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import junit.framework.Test;
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.JDBC;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+/**
+ * Test floating point data types.
+ */
+public class FloatTypesTest extends BaseJDBCTestCase {
+
+    public FloatTypesTest(String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+        return TestConfiguration.defaultSuite(FloatTypesTest.class);
+    }
+
+    /**
+     * Test that normalization of negative zero to positive zero works for
+     * floats. In some JVMs this used to give wrong results after runtime
+     * optimization. See DERBY-2447 and
+     * <a href="http://bugs.sun.com/view_bug.do?bug_id=6833879">CR6833879</a>
+     * in Sun's bug database.
+     */
+    public void testNegativeZeroFloatJvmBug() throws SQLException {
+        PreparedStatement ps = prepareStatement("values -cast(? as real)");
+        ps.setFloat(1, 0.0f);
+        // Execute the statement many times so that the JVM is likely to
+        // produce native, optimized code.
+        for (int i = 0; i < 7000; i++) {
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), "0.0");
+        }
+    }
+
+    /**
+     * Test that normalization of negative zero to positive zero works for
+     * doubles. In some JVMs this used to give wrong results after runtime
+     * optimization. See DERBY-2447 and
+     * <a href="http://bugs.sun.com/view_bug.do?bug_id=6833879">CR6833879</a>
+     * in Sun's bug database.
+     */
+    public void testNegativeZeroDoubleJvmBug() throws SQLException {
+        PreparedStatement ps = prepareStatement("values -cast(? as double)");
+        ps.setDouble(1, 0.0d);
+        // Execute the statement many times so that the JVM is likely to
+        // produce native, optimized code.
+        for (int i = 0; i < 7000; i++) {
+            JDBC.assertSingleValueResultSet(ps.executeQuery(), "0.0");
+        }
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FloatTypesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java?rev=772449&r1=772448&r2=772449&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
Wed May  6 22:30:35 2009
@@ -65,6 +65,7 @@
         suite.addTest(DatabaseClassLoadingTest.suite());
         suite.addTest(DynamicLikeOptimizationTest.suite());
         suite.addTest(ExistsWithSubqueriesTest.suite());
+        suite.addTest(FloatTypesTest.suite());
         suite.addTest(GrantRevokeTest.suite());
         suite.addTest(GroupByExpressionTest.suite());
 		suite.addTest(LangScripts.suite());



Mime
View raw message