You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by dj...@apache.org on 2005/02/11 22:36:43 UTC

svn commit: r153460 - incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/execute incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute

Author: djd
Date: Fri Feb 11 13:36:38 2005
New Revision: 153460

URL: http://svn.apache.org/viewcvs?view=rev&rev=153460
Log:
J2ME related work.
Add method to DataValueDescriptor to indicate how a value should
be converted to a BigDecimal now that use of BigDecimal will be moving
to specialized JDBC2/JDBC3 classes only. EmbedResultSet20 and EmbedCallableStatement20
now use this mechanism. Use of DataValueDescriptor.getBigDecimal() restricted to
as few cases as possible, prior to its removal.
Common arithemtic code for TINYINT, SMALLINT and INTEGER moved into NumberDataType.


Modified:
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/execute/ExecAggregator.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataType.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataValueDescriptor.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/NumberDataType.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBoolean.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLClob.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDecimal.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDouble.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLInteger.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLReal.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLSmallint.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTinyint.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement20.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet20.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AvgAggregator.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/CountAggregator.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericAggregator.java
    incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/OrderableAggregator.java

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/execute/ExecAggregator.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/execute/ExecAggregator.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/execute/ExecAggregator.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/execute/ExecAggregator.java Fri Feb 11 13:36:38 2005
@@ -100,8 +100,9 @@
 	 * Produces the result to be returned by the query.
 	 * The last processing of the aggregate.
 	 *
-	 */
-	public Object getResult();
+	 * @exception StandardException on error
+ 	 */
+	public DataValueDescriptor getResult() throws StandardException;
 
 	/**
  	   Return a new initialized copy of this aggregator, any state

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataType.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataType.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataType.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataType.java Fri Feb 11 13:36:38 2005
@@ -179,7 +179,10 @@
 	{
 		throw dataTypeConversion("java.math.BigDecimal");
 	}
-
+	public int typeToBigDecimal() throws StandardException
+	{
+		throw dataTypeConversion("java.math.BigDecimal");
+	}
 	/**
 	 * Gets the value in the data value descriptor as a byte[].
 	 * Throws an exception if the data value is not receivable as a Binary or Varbinary.

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataValueDescriptor.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataValueDescriptor.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataValueDescriptor.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataValueDescriptor.java Fri Feb 11 13:36:38 2005
@@ -208,6 +208,16 @@
 	 * @exception StandardException   Thrown on error
 	 */
 	BigDecimal	getBigDecimal() throws StandardException;
+	
+	/**
+	 * How should this value be obtained so that it can
+	 * be converted to a BigDecimal representation.
+	 * @return Types.CHAR for String conversion through getString
+	 * Types.DECIMAL for BigDecimal through getObject or Types.BIGINT
+	 * for long conversion through getLong
+	 * @exception StandardException Conversion is not possible
+	 */
+	int typeToBigDecimal() throws StandardException;
 
 	/**
 	 * Gets the value in the data value descriptor as a byte array.

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/NumberDataType.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/NumberDataType.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/NumberDataType.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/NumberDataType.java Fri Feb 11 13:36:38 2005
@@ -31,7 +31,6 @@
 import org.apache.derby.iapi.reference.DB2Limit;
 
 import org.apache.derby.iapi.types.*;
-import java.math.BigDecimal;
 
 /**
  * NumberDataType is the superclass for all exact and approximate 
@@ -105,10 +104,164 @@
         result.setValue( Math.sqrt(doubleValue) );
         return result;
     }
+    
+	/**
+	 * This method implements the + operator for TINYINT,SMALLINT,INT.
+	 *
+	 * @param addend1	One of the addends
+	 * @param addend2	The other addend
+	 * @param result	The result of a previous call to this method, null
+	 *					if not called yet
+	 *
+	 * @return	A NumberDataValue containing the result of the addition
+	 *
+	 * @exception StandardException		Thrown on error
+	 */
+
+	public NumberDataValue plus(NumberDataValue addend1,
+							NumberDataValue addend2,
+							NumberDataValue result)
+				throws StandardException
+	{
+		if (result == null)
+		{
+			result = (NumberDataValue) getNewNull();
+		}
+
+		if (addend1.isNull() || addend2.isNull())
+		{
+			result.setToNull();
+			return result;
+		}
+		int addend1Int = addend1.getInt();
+		int addend2Int = addend2.getInt();
+
+		int resultValue = addend1Int + addend2Int;
+
+		/*
+		** Java does not check for overflow with integral types. We have to
+		** check the result ourselves.
+		**
+		** Overflow is possible only if the two addends have the same sign.
+		** Do they?  (This method of checking is approved by "The Java
+		** Programming Language" by Arnold and Gosling.)
+		*/
+		if ((addend1Int < 0) == (addend2Int < 0))
+		{
+			/*
+			** Addends have the same sign.  The result should have the same
+			** sign as the addends.  If not, an overflow has occurred.
+			*/
+			if ((addend1Int < 0) != (resultValue < 0))
+			{
+				throw outOfRange();
+			}
+		}
+
+		result.setValue(resultValue);
+
+		return result;
+	}
+	/**
+	 * This method implements the - operator for TINYINT, SMALLINT and INTEGER.
+	 *
+	 * @param left	The value to be subtracted from
+	 * @param right	The value to be subtracted
+	 * @param result	The result of a previous call to this method, null
+	 *					if not called yet
+	 *
+	 * @return	A SQLInteger containing the result of the subtraction
+	 *
+	 * @exception StandardException		Thrown on error
+	 */
+
+	public NumberDataValue minus(NumberDataValue left,
+							NumberDataValue right,
+							NumberDataValue result)
+				throws StandardException
+	{
+		if (result == null)
+		{
+			result = (NumberDataValue) getNewNull();
+		}
+
+		if (left.isNull() || right.isNull())
+		{
+			result.setToNull();
+			return result;
+		}
+
+		int diff = left.getInt() - right.getInt();
+
+		/*
+		** Java does not check for overflow with integral types. We have to
+		** check the result ourselves.
+		**
+		** Overflow is possible only if the left and the right side have opposite signs.
+		** Do they?  (This method of checking is approved by "The Java
+		** Programming Language" by Arnold and Gosling.)
+		*/
+		if ((left.getInt() < 0) != (right.getInt() < 0))
+		{
+			/*
+			** Left and right have opposite signs.  The result should have the same
+			** sign as the left (this).  If not, an overflow has occurred.
+			*/
+			if ((left.getInt() < 0) != (diff < 0))
+			{
+				throw outOfRange();
+			}
+		}
+
+		result.setValue(diff);
+
+		return result;
+	}
+	
+	/**
+	 * This method implements the / operator for TINYINT, SMALLINT and INTEGER.
+	 * Specialized methods are not required for TINYINT and SMALLINT as the Java
+	 * virtual machine always executes byte and int division as integer.
+	 *
+	 * @param dividend	The numerator
+	 * @param divisor	The denominator
+	 * @param result	The result of a previous call to this method, null
+	 *					if not called yet
+	 *
+	 * @return	A SQLInteger containing the result of the division
+	 *
+	 * @exception StandardException		Thrown on error
+	 */
+
+	public NumberDataValue divide(NumberDataValue dividend,
+							 NumberDataValue divisor,
+							 NumberDataValue result)
+				throws StandardException
+	{
+		if (result == null)
+		{
+			result = (NumberDataValue) getNewNull();
+		}
+
+		if (dividend.isNull() || divisor.isNull())
+		{
+			result.setToNull();
+			return result;
+		}
+
+		/* Catch divide by 0 */
+		int intDivisor = divisor.getInt();
+		if (intDivisor == 0)
+		{
+			throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO);
+		}
+
+		result.setValue(dividend.getInt() / intDivisor);
+		return result;
+	}
 
 	/**
-	 * This is dummy parent divide method.  Put it here for all the children
-	 * that don't need this.  @see NumberDataValue#divide
+	 	Suitable for integral types that ignore scale.
 	 */
 	public NumberDataValue divide(NumberDataValue dividend,
 								  NumberDataValue divisor,
@@ -116,9 +269,7 @@
 								  int scale)
 				throws StandardException
 	{
-		if (SanityManager.DEBUG)
-			SanityManager.NOTREACHED();
-		return null;
+		return divide(dividend, divisor, result);
 	}
 
 	public NumberDataValue mod(NumberDataValue dividend,
@@ -130,7 +281,6 @@
 		return null;
 	}
 
-
 	/** @exception StandardException		Thrown on error */
 	public final int compare(DataValueDescriptor arg) throws StandardException
 	{
@@ -244,7 +394,15 @@
 
 			throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, getTypeName());
 		}
-	}	
+	}
+	
+	/**
+	 * Implementation for integral types. Convert to a BigDecimal using long
+	 */
+	public int typeToBigDecimal()
+	{
+		return java.sql.Types.BIGINT;
+	}
 	/**
 		Return the precision of this specific DECIMAL value.
 		If the value does not represent a SQL DECIMAL then

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBoolean.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBoolean.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBoolean.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBoolean.java Fri Feb 11 13:36:38 2005
@@ -154,7 +154,13 @@
 		if (isNull()) return null;
 		return BigDecimal.valueOf(makeInt(value));
 	}
-
+	/**
+	 * Implementation for BOOLEAN type. Convert to a BigDecimal using long
+	 */
+	public int typeToBigDecimal()
+	{
+		return java.sql.Types.BIGINT;
+	}
 	public String	getString()
 	{
 		if (isNull())

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLChar.java Fri Feb 11 13:36:38 2005
@@ -237,7 +237,13 @@
 			throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "java.math.BigDecimal");
 		}
 	}
-
+	/**
+	 * CHAR/VARCHAR/LONG VARCHAR implementation. Convert to a BigDecimal using getString.
+	 */
+	public int typeToBigDecimal()  throws StandardException
+	{
+		return java.sql.Types.CHAR;
+	}
 	/**
 	 * @see DataValueDescriptor#getDate
 	 * @exception StandardException thrown on failure to convert

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLClob.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLClob.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLClob.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLClob.java Fri Feb 11 13:36:38 2005
@@ -180,6 +180,10 @@
 	{
 		throw dataTypeConversion("java.math.BigDecimal");
 	}
+	public int typeToBigDecimal() throws StandardException
+	{
+		throw dataTypeConversion("java.math.BigDecimal");
+	}
 	public byte[]	getBytes() throws StandardException
 	{
 		throw dataTypeConversion("byte[]");

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDecimal.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDecimal.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDecimal.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDecimal.java Fri Feb 11 13:36:38 2005
@@ -47,6 +47,7 @@
 import java.sql.ResultSetMetaData;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
+import java.sql.Types;
 
 /**
  * SQLDecimal satisfies the DataValueDescriptor
@@ -162,8 +163,7 @@
 	 */
 	public int	getInt() throws StandardException
 	{
-		BigDecimal localValue = getBigDecimal();
-		if (localValue == null)
+		if (isNull())
 			return 0;
 
 		try {
@@ -183,9 +183,7 @@
 	 */
 	public byte	getByte() throws StandardException
 	{
-		BigDecimal localValue = getBigDecimal();
-
-		if (localValue == null)
+		if (isNull())
 			return (byte)0;
 
 		try {
@@ -205,8 +203,7 @@
 	 */
 	public short	getShort() throws StandardException
 	{
-		BigDecimal localValue = getBigDecimal();
-		if (localValue == null)
+		if (isNull())
 			return (short)0;
 
 		try {
@@ -295,6 +292,15 @@
 
 		return value;
 	}
+	
+	/**
+	 * DECIMAL implementation. Convert to a BigDecimal using getObject
+	 * which will return a BigDecimal
+	 */
+	public int typeToBigDecimal()
+	{
+		return java.sql.Types.DECIMAL;
+	}
 
     // 0 or null is false, all else is true
 	public boolean	getBoolean()
@@ -552,9 +558,7 @@
 									  boolean isNullable)
 		throws SQLException
 	{
-			ResultSetMetaData rsmd = resultSet.getMetaData();
-			value = resultSet.getBigDecimal(colNumber,
-											rsmd.getScale(colNumber));
+			value = resultSet.getBigDecimal(colNumber);
 			rawData = null;
 	}
 	/**
@@ -1087,6 +1091,39 @@
 	{
 		BigDecimal localValue = getBigDecimal();
 		return (localValue == null) ? 0 : localValue.scale();
+	}
+	
+	/**
+	 * Get a BigDecimal representing the value of a DataValueDescriptor
+	 * @param value Non-null value to be converted
+	 * @return BigDecimal value
+	 * @throws StandardException Invalid conversion or out of range.
+	 */
+	public static BigDecimal getBigDecimal(DataValueDescriptor value) throws StandardException
+	{
+		if (SanityManager.DEBUG)
+		{
+			if (value.isNull())
+				SanityManager.THROWASSERT("NULL value passed to SQLDecimal.getBigDecimal");
+		}
+		
+		switch (value.typeToBigDecimal())
+		{
+		case Types.DECIMAL:
+			return (BigDecimal) value.getObject();
+		case Types.CHAR:
+			try {
+				return new BigDecimal(value.getString().trim());
+			} catch (NumberFormatException nfe) {
+				throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "java.math.BigDecimal");
+			}
+		case Types.BIGINT:
+			return BigDecimal.valueOf(value.getLong());
+		default:
+			if (SanityManager.DEBUG)
+				SanityManager.THROWASSERT("invalid return from " + value.getClass() + ".typeToBigDecimal() " + value.typeToBigDecimal());
+			return null;
+		}
 	}
 
 	private int getWholeDigits()

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDouble.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDouble.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDouble.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLDouble.java Fri Feb 11 13:36:38 2005
@@ -155,7 +155,13 @@
 		if (isNull()) return null;
 		return new BigDecimal(Double.toString(value));
 	}
-
+	/**
+	 * DOUBLE implementation. Convert to a BigDecimal using getString.
+	 */
+	public int typeToBigDecimal()
+	{
+		return java.sql.Types.CHAR;
+	}
     // for lack of a specification: getDouble()==0 gives true
     // independent of the NULL flag
 	public boolean	getBoolean()

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLInteger.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLInteger.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLInteger.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLInteger.java Fri Feb 11 13:36:38 2005
@@ -602,120 +602,6 @@
 	}
 
 	/**
-	 * This method implements the + operator for "int + int".
-	 *
-	 * @param addend1	One of the addends
-	 * @param addend2	The other addend
-	 * @param result	The result of a previous call to this method, null
-	 *					if not called yet
-	 *
-	 * @return	A SQLInteger containing the result of the addition
-	 *
-	 * @exception StandardException		Thrown on error
-	 */
-
-	public NumberDataValue plus(NumberDataValue addend1,
-							NumberDataValue addend2,
-							NumberDataValue result)
-				throws StandardException
-	{
-		if (result == null)
-		{
-			result = new SQLInteger();
-		}
-
-		if (addend1.isNull() || addend2.isNull())
-		{
-			result.setToNull();
-			return result;
-		}
-		int addend1Int = addend1.getInt();
-		int addend2Int = addend2.getInt();
-
-		int resultValue = addend1Int + addend2Int;
-
-		/*
-		** Java does not check for overflow with integral types. We have to
-		** check the result ourselves.
-		**
-		** Overflow is possible only if the two addends have the same sign.
-		** Do they?  (This method of checking is approved by "The Java
-		** Programming Language" by Arnold and Gosling.)
-		*/
-		if ((addend1Int < 0) == (addend2Int < 0))
-		{
-			/*
-			** Addends have the same sign.  The result should have the same
-			** sign as the addends.  If not, an overflow has occurred.
-			*/
-			if ((addend1Int < 0) != (resultValue < 0))
-			{
-				throw outOfRange();
-			}
-		}
-
-		result.setValue(resultValue);
-
-		return result;
-	}
-
-	/**
-	 * This method implements the - operator for "int - int".
-	 *
-	 * @param left	The value to be subtracted from
-	 * @param right	The value to be subtracted
-	 * @param result	The result of a previous call to this method, null
-	 *					if not called yet
-	 *
-	 * @return	A SQLInteger containing the result of the subtraction
-	 *
-	 * @exception StandardException		Thrown on error
-	 */
-
-	public NumberDataValue minus(NumberDataValue left,
-							NumberDataValue right,
-							NumberDataValue result)
-				throws StandardException
-	{
-		if (result == null)
-		{
-			result = new SQLInteger();
-		}
-
-		if (left.isNull() || right.isNull())
-		{
-			result.setToNull();
-			return result;
-		}
-
-		int diff = left.getInt() - right.getInt();
-
-		/*
-		** Java does not check for overflow with integral types. We have to
-		** check the result ourselves.
-		**
-		** Overflow is possible only if the left and the right side have opposite signs.
-		** Do they?  (This method of checking is approved by "The Java
-		** Programming Language" by Arnold and Gosling.)
-		*/
-		if ((left.getInt() < 0) != (right.getInt() < 0))
-		{
-			/*
-			** Left and right have opposite signs.  The result should have the same
-			** sign as the left (this).  If not, an overflow has occurred.
-			*/
-			if ((left.getInt() < 0) != (diff < 0))
-			{
-				throw outOfRange();
-			}
-		}
-
-		result.setValue(diff);
-
-		return result;
-	}
-
-	/**
 	 * This method implements the * operator for "int * int".
 	 *
 	 * @param left	The first value to be multiplied
@@ -759,45 +645,6 @@
 		return result;
 	}
 
-	/**
-	 * This method implements the / operator for "int / int".
-	 *
-	 * @param dividend	The numerator
-	 * @param divisor	The denominator
-	 * @param result	The result of a previous call to this method, null
-	 *					if not called yet
-	 *
-	 * @return	A SQLInteger containing the result of the division
-	 *
-	 * @exception StandardException		Thrown on error
-	 */
-
-	public NumberDataValue divide(NumberDataValue dividend,
-							 NumberDataValue divisor,
-							 NumberDataValue result)
-				throws StandardException
-	{
-		if (result == null)
-		{
-			result = new SQLInteger();
-		}
-
-		if (dividend.isNull() || divisor.isNull())
-		{
-			result.setToNull();
-			return result;
-		}
-
-		/* Catch divide by 0 */
-		int intDivisor = divisor.getInt();
-		if (intDivisor == 0)
-		{
-			throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO);
-		}
-
-		result.setValue(dividend.getInt() / intDivisor);
-		return result;
-	}
 
 	/**
 		mod(int, int)

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLReal.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLReal.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLReal.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLReal.java Fri Feb 11 13:36:38 2005
@@ -163,7 +163,13 @@
 		if (isNull()) return null;
 		return new BigDecimal(Float.toString(value));
 	}
-
+	/**
+	 * DOUBLE implementation. Convert to a BigDecimal using getString.
+	 */
+	public int typeToBigDecimal()
+	{
+		return java.sql.Types.CHAR;
+	}
     // for lack of a specification: 0 or null is false,
     // all else is true
 	/** 

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLSmallint.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLSmallint.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLSmallint.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLSmallint.java Fri Feb 11 13:36:38 2005
@@ -632,87 +632,7 @@
 	}
 
 
-	/**
-	 * This method implements the + operator for "smallint + smallint".
-	 *
-	 * @param addend1	One of the addends
-	 * @param addend2	The other addend
-	 * @param result	The result of a previous call to this method, null
-	 *					if not called yet
-	 *
-	 * @return	A SQLSmallint containing the result of the addition
-	 *
-	 * @exception StandardException		Thrown on error
-	 */
-
-	public NumberDataValue plus(NumberDataValue addend1,
-							NumberDataValue addend2,
-							NumberDataValue result)
-				throws StandardException
-	{
-		if (result == null)
-		{
-			result = new SQLSmallint();
-		}
-
-		if (addend1.isNull() || addend2.isNull())
-		{
-			result.setToNull();
-			return result;
-		}
-
-		/*
-		** Java does not check for overflow with integral types. We have to
-		** check the result ourselves.
-		**
-			The setValue(int) will perform the overflow check.
-		*/
-		int sum = addend1.getShort() + addend2.getShort();
-
-		result.setValue(sum);
-		return result;
-	}
-
-	/**
-	 * This method implements the - operator for "smallint - smallint".
-	 *
-	 * @param left	The value to be subtracted from
-	 * @param right	The value to be subtracted
-	 * @param result	The result of a previous call to this method, null
-	 *					if not called yet
-	 *
-	 * @return	A SQLSmallint containing the result of the subtraction
-	 *
-	 * @exception StandardException		Thrown on error
-	 */
-
-	public NumberDataValue minus(NumberDataValue left,
-							NumberDataValue right,
-							NumberDataValue result)
-				throws StandardException
-	{
-		if (result == null)
-		{
-			result = new SQLSmallint();
-		}
 
-		if (left.isNull() || right.isNull())
-		{
-			result.setToNull();
-			return result;
-		}
-
-		/*
-		** Java does not check for overflow with integral types. We have to
-		** check the result ourselves.
-		**
-			The setValue(int) will perform the overflow check.
-		*/
-		int difference = left.getShort() - right.getShort();
-
-		result.setValue(difference);
-		return result;
-	}
 
 	/**
 	 * This method implements the * operator for "smallint * smallint".
@@ -754,47 +674,7 @@
 		return result;
 	}
 
-	/**
-	 * This method implements the / operator for "smallint / smallint".
-	 *
-	 * @param dividend	The numerator
-	 * @param divisor	The denominator
-	 * @param result	The result of a previous call to this method, null
-	 *					if not called yet
-	 *
-	 * @return	A SQLSmallint containing the result of the division
-	 *
-	 * @exception StandardException		Thrown on error
-	 */
-
-	public NumberDataValue divide(NumberDataValue dividend,
-							 NumberDataValue divisor,
-							 NumberDataValue result)
-				throws StandardException
-	{
-		short	shortDivisor;
-
-		if (result == null)
-		{
-			result = new SQLSmallint();
-		}
-
-		if (dividend.isNull() || divisor.isNull())
-		{
-			result.setToNull();
-			return result;
-		}
 
-		/* Catch divide by 0 */
-		shortDivisor = divisor.getShort();
-		if (shortDivisor == 0)
-		{
-			throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO);
-		}
-
-		result.setValue((short) (dividend.getShort() / shortDivisor));
-		return result;
-	}
 	/**
 		mod(smallint, smallint)
 	*/

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTinyint.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTinyint.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTinyint.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLTinyint.java Fri Feb 11 13:36:38 2005
@@ -637,89 +637,8 @@
 	}
 
 
-	/**
-	 * This method implements the + operator for "byte + byte".
-	 *
-	 * @param addend1	One of the addends
-	 * @param addend2	The other addend
-	 * @param result	The result of a previous call to this method, null
-	 *					if not called yet
-	 *
-	 * @return	A SQLTinyint containing the result of the addition
-	 *
-	 * @exception StandardException		Thrown on error
-	 */
-
-	public NumberDataValue plus(NumberDataValue addend1,
-							NumberDataValue addend2,
-							NumberDataValue result)
-				throws StandardException
-	{
-		if (result == null)
-		{
-			result = new SQLTinyint();
-		}
 
-		if (addend1.isNull() || addend2.isNull())
-		{
-			result.setToNull();
-			return result;
-		}
-
-		/*
-		** Java does not check for overflow with integral types. We have to
-		** check the result ourselves.
-		**
-		** The sum of 2 bytes is an int, so we check to see if the sum
-		** is in the range of values for a bytes.
-		*/
-		int sum = addend1.getByte() + addend2.getByte();
 
-		result.setValue(sum);
-		return result;
-	}
-
-	/**
-	 * This method implements the - operator for "tinyint - tinyint".
-	 *
-	 * @param left	The value to be subtracted from
-	 * @param right	The value to be subtracted
-	 * @param result	The result of a previous call to this method, null
-	 *					if not called yet
-	 *
-	 * @return	A SQLTinyint containing the result of the subtraction
-	 *
-	 * @exception StandardException		Thrown on error
-	 */
-
-	public NumberDataValue minus(NumberDataValue left,
-							NumberDataValue right,
-							NumberDataValue result)
-				throws StandardException
-	{
-		if (result == null)
-		{
-			result = new SQLTinyint();
-		}
-
-		if (left.isNull() || right.isNull())
-		{
-			result.setToNull();
-			return result;
-		}
-
-		/*
-		** Java does not check for overflow with integral types. We have to
-		** check the result ourselves.
-		**
-		** The difference of 2 bytes is an int, so we check to see if the
-		** difference is in the range of values for a byte.
-		*/
-		int difference = left.getByte() - right.getByte();
-
-		result.setValue(difference);
-		return result;
-	}
 
 	/**
 	 * This method implements the * operator for "tinyint * tinyint".
@@ -762,47 +681,7 @@
 		return result;
 	}
 
-	/**
-	 * This method implements the / operator for "tinyint / tinyint".
-	 *
-	 * @param dividend	The numerator
-	 * @param divisor	The denominator
-	 * @param result	The result of a previous call to this method, null
-	 *					if not called yet
-	 *
-	 * @return	A SQLTinyint containing the result of the division
-	 *
-	 * @exception StandardException		Thrown on error
-	 */
-
-	public NumberDataValue divide(NumberDataValue dividend,
-							 NumberDataValue divisor,
-							 NumberDataValue result)
-				throws StandardException
-	{
-		byte	byteDivisor;
-
-		if (result == null)
-		{
-			result = new SQLTinyint();
-		}
 
-		if (dividend.isNull() || divisor.isNull())
-		{
-			result.setToNull();
-			return result;
-		}
-
-		/* Catch divide by 0 */
-		byteDivisor = divisor.getByte();
-		if (byteDivisor == 0)
-		{
-			throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO);
-		}
-
-		result.setValue((byte) (dividend.getByte() / byteDivisor));
-		return result;
-	}
 	/**
 		mod(tinyint, tinyint)
 	*/

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement20.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement20.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement20.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement20.java Fri Feb 11 13:36:38 2005
@@ -45,6 +45,7 @@
 
 import org.apache.derby.iapi.sql.conn.StatementContext;
 import org.apache.derby.iapi.reference.SQLState;
+import org.apache.derby.iapi.types.DataValueDescriptor;
 
 import org.apache.derby.impl.jdbc.Util;
 import org.apache.derby.impl.jdbc.EmbedConnection;
@@ -97,9 +98,12 @@
 	{
 		checkStatus();
 		try {
-			BigDecimal v =  getParms().getParameterForGet(parameterIndex-1).getBigDecimal();
-			wasNull = (v == null);
-			return v;
+			DataValueDescriptor dvd = getParms().getParameterForGet(parameterIndex-1);
+			if (wasNull = dvd.isNull())
+				return null;
+			
+			return org.apache.derby.iapi.types.SQLDecimal.getBigDecimal(dvd);
+			
 		} catch (StandardException e)
 		{
 			throw EmbedResultSet.noStateChangeException(e);
@@ -1161,18 +1165,12 @@
 	 * @see CallableStatement#getBigDecimal
      * @exception SQLException NoOutputParameters thrown.
      */
-    public BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException
+    public final BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException
 	{
-		checkStatus();
-		try {
-			BigDecimal v =  getParms().getParameterForGet(parameterIndex-1).getBigDecimal();
-			wasNull = (v == null);
-			return v;
-		} catch (StandardException e)
-		{
-			throw EmbedResultSet.noStateChangeException(e);
-		}
-
+    	BigDecimal v = getBigDecimal(parameterIndex);
+    	if (v != null)
+    		v = v.setScale(scale, BigDecimal.ROUND_HALF_DOWN);
+    	return v;
 	}
 	/**
 		Allow explict setObject conversions by sub-classes for classes

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet20.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet20.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet20.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet20.java Fri Feb 11 13:36:38 2005
@@ -113,8 +113,8 @@
 
 				if (wasNull = dvd.isNull())
 					return null;
-
-				return dvd.getBigDecimal();
+				
+				return org.apache.derby.iapi.types.SQLDecimal.getBigDecimal(dvd);
 
 			} catch (StandardException t) {
 				throw noStateChangeException(t);
@@ -183,7 +183,7 @@
      *
          * @exception SQLException Feature not implemented for now.
      */
-    public BigDecimal getBigDecimal(String columnName) throws SQLException {
+    public final BigDecimal getBigDecimal(String columnName) throws SQLException {
                         return getBigDecimal(findColumnName(columnName));
         }
 

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AvgAggregator.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AvgAggregator.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AvgAggregator.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AvgAggregator.java Fri Feb 11 13:36:38 2005
@@ -70,7 +70,7 @@
 				scale = TypeId.DECIMAL_SCALE;
 			} else {
 				// DECIMAL
-				scale = addend.getBigDecimal().scale();
+				scale = ((NumberDataValue) addend).getDecimalValueScale();
 				if (scale < NumberDataValue.MIN_DECIMAL_DIVIDE_SCALE)
 					scale = NumberDataValue.MIN_DECIMAL_DIVIDE_SCALE;
 			}
@@ -104,17 +104,23 @@
 		// this code creates data type objects directly, it is anticipating
 		// the time they move into the defined api of the type system. (djd).
 		String typeName = value.getTypeName();
+		
+		DataValueDescriptor newValue;
 
 		if (typeName.equals(TypeId.INTEGER_NAME)) {
-			value = new org.apache.derby.iapi.types.SQLLongint(value.getLong());
+			newValue = new org.apache.derby.iapi.types.SQLLongint();
 		} else if (typeName.equals(TypeId.TINYINT_NAME) || 
 				   typeName.equals(TypeId.SMALLINT_NAME)) {
-			value = new org.apache.derby.iapi.types.SQLInteger(value.getInt());
+			newValue = new org.apache.derby.iapi.types.SQLInteger();
 		} else if (typeName.equals(TypeId.REAL_NAME)) {
-			value = new org.apache.derby.iapi.types.SQLDouble(value.getDouble());
+			newValue = new org.apache.derby.iapi.types.SQLDouble();
 		} else {
-			value = new org.apache.derby.iapi.types.SQLDecimal(value.getBigDecimal());
+			newValue = new org.apache.derby.iapi.types.SQLDecimal();
 		}
+		
+		newValue.setValue(value);
+		value = newValue;
+		
 		accumulate(addend);
 	}
 
@@ -157,23 +163,39 @@
 	 *
 	 * @return null or the average as Double
 	 */
-	public Object getResult()
+	public DataValueDescriptor getResult() throws StandardException
 	{
 		if (count == 0)
 		{
 			return null;
 		}
 
-		// note we cannot use the Datatype's divide method as it only supports
-		// dividing by the same type, where we need the divisor to be a long
-		// regardless of the sum type.
+		NumberDataValue sum = (NumberDataValue) value;
+		NumberDataValue avg = (NumberDataValue) value.getNewNull();
 
-		BigDecimal avg = null;
-		try {
-			 avg = value.getBigDecimal().divide(BigDecimal.valueOf(count), scale, BigDecimal.ROUND_DOWN);
-		} catch (StandardException se) {
-			// get BigDecimal for a numeric type cannot throw an exception.
+		
+		if (count > (long) Integer.MAX_VALUE)
+		{
+			// TINYINT, SMALLINT, INTEGER implement arithmetic using integers
+			// If the sum is still represented as a TINYINT, SMALLINT or INTEGER
+			// we cannot let their int based arithmetic handle it, since they
+			// will perform a getInt() on the long value which will truncate the long value.
+			// One solution would be to promote the sum to a SQLLongint, but its value
+			// will be less than or equal to Integer.MAX_VALUE, so the average will be 0.
+			String typeName = sum.getTypeName();
+
+			if (typeName.equals(TypeId.INTEGER_NAME) ||
+					typeName.equals(TypeId.TINYINT_NAME) || 
+					   typeName.equals(TypeId.SMALLINT_NAME))
+			{
+				avg.setValue(0);
+				return avg;
+			}
 		}
+
+		NumberDataValue countv = new org.apache.derby.iapi.types.SQLLongint(count);
+		sum.divide(sum, countv, avg, scale);
+				
 		return avg;
 	}
 

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/CountAggregator.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/CountAggregator.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/CountAggregator.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/CountAggregator.java Fri Feb 11 13:36:38 2005
@@ -71,9 +71,9 @@
 	 *
 	 * @return the value as a Long 
 	 */
-	public Object getResult()
+	public DataValueDescriptor getResult()
 	{
-		return new Long(value);
+		return new org.apache.derby.iapi.types.SQLLongint(value);
 	}
 
 

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericAggregator.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericAggregator.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericAggregator.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericAggregator.java Fri Feb 11 13:36:38 2005
@@ -294,35 +294,21 @@
 			ua = getAggregatorInstance();
 		}	
 
-		Object result = ua.getResult();
-
 		/*
-		** Handle setting the result.  If null, call setToNull.
 		**
-		** If we got a DataValueDescriptor then we are going to copy
+		** We are going to copy
 		** then entire DataValueDescriptor into the result column.
 		** We could call setValue(result.setObject()), but we
 		** might loose state (e.g. SQLBit.getObject() returns a
 		** byte[] which looses the precision of the bit.  
 		**
-		** On an object, just call setValue().
 		*/
+		
+		DataValueDescriptor result = ua.getResult();
 		if (result == null)
-		{
 			outputColumn.setToNull();
-		}
-		/*
-		** See above as to why we cannot do: 
-		**		outputColumn.setValue(result.getObject());	
-		*/
-		else if (result instanceof DataValueDescriptor)
-		{
-			row.setColumn(resultColumnId + 1, (DataValueDescriptor)result);
-		}
 		else
-		{
 			outputColumn.setValue(result);
-		}
 
 		return ua.didEliminateNulls();
 	}

Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/OrderableAggregator.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/OrderableAggregator.java?view=diff&r1=153459&r2=153460
==============================================================================
--- incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/OrderableAggregator.java (original)
+++ incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/OrderableAggregator.java Fri Feb 11 13:36:38 2005
@@ -72,7 +72,7 @@
 	 *
 	 * @return the result as a DataValueDescriptor 
 	 */
-	public Object getResult()
+	public DataValueDescriptor getResult() throws StandardException
 	{
 		return value;
 	}