db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpendle...@apache.org
Subject svn commit: r1755133 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/NumericTypeCompiler.java testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ResultSetMiscTest.java
Date Thu, 04 Aug 2016 01:29:02 GMT
Author: bpendleton
Date: Thu Aug  4 01:29:02 2016
New Revision: 1755133

URL: http://svn.apache.org/viewvc?rev=1755133&view=rev
Log:
DERBY-853: ResultSetMetaData.getScale returns inconsistent values

This patch was contributed by Danoja Dias (danojadias at gmail dot com)

When a SQL statement contains arithmetic expressions, the result of the
expression may be of a different type than the operands to the expression,
due to type precedence rules which may require promoting the operand
values during evaluation of the expression.

For example, subtracting a DOUBLE from a DECIMAL results in a DOUBLE.

In some of these cases, Derby was reporting that the result column
had a non-zero scale, although the result column was not of DECIMAL type.

This change modifies the NumericTypeCompiler so that it only computes
a non-zero scale for the result column when it is of DECIMAL type.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/NumericTypeCompiler.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ResultSetMiscTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/NumericTypeCompiler.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/NumericTypeCompiler.java?rev=1755133&r1=1755132&r2=1755133&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/NumericTypeCompiler.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/NumericTypeCompiler.java
Thu Aug  4 01:29:02 2016
@@ -255,10 +255,10 @@ public final class NumericTypeCompiler e
 		 * type, this is also to be consistent with maximumWidth.  Beetle 3906.
 		 */
 		precision = higherTC.getPrecision(operator, leftType, rightType);
-		scale = higherTC.getScale(operator, leftType, rightType);
 
 		if (higherType.getTypeId().isDecimalTypeId()) 
 		{
+			scale = higherTC.getScale(operator, leftType, rightType);
 			maximumWidth = (scale > 0) ? precision + 3 : precision + 1;
 
 			/*
@@ -271,6 +271,7 @@ public final class NumericTypeCompiler e
 		}
 		else
 		{
+			scale = 0;
 			maximumWidth = higherType.getMaximumWidth();
 		}
 		
@@ -466,10 +467,6 @@ public final class NumericTypeCompiler e
 							DataTypeDescriptor rightType)
 	{
 		// Only meaningful for decimal
-		if (getStoredFormatIdFromTypeId() != StoredFormatIds.DECIMAL_TYPE_ID)
-		{
-			return leftType.getScale();
-		}
 
 		long val;
 

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ResultSetMiscTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ResultSetMiscTest.java?rev=1755133&r1=1755132&r2=1755133&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ResultSetMiscTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ResultSetMiscTest.java
Thu Aug  4 01:29:02 2016
@@ -478,6 +478,31 @@ public class ResultSetMiscTest extends B
         stmt.close();
     }
 
+     /**
+     * Test fix for DERBY-853 ResultSetMetaData.getScale 
+     *returns inconsistent values for DOUBLE type.
+     */
+    public void testDerby853() throws SQLException {
+        Connection con = getConnection();
+	
+        Statement stmt = con.createStatement();
+        stmt.executeUpdate("create table derby853(d1 decimal(10,2), d2 double)");
+      
+        ResultSet rs = stmt.executeQuery("select d1 - d2 from derby853");
+        ResultSetMetaData rsmd = rs.getMetaData();
+	assertEquals(0, rsmd.getScale(1));
+	assertEquals(15, rsmd.getPrecision(1));
+
+	rs = stmt.executeQuery("select d2 - d1 from derby853");
+	rsmd = rs.getMetaData();
+	assertEquals(0, rsmd.getScale(1));
+	assertEquals(15, rsmd.getPrecision(1));
+
+        stmt.execute("drop table derby853");
+       
+        stmt.close();
+    }
+
     /**
      * Setup up and run the auto-commit tests.
      * 



Mime
View raw message