db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1398489 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/ testing/org/apache/derbyTesting/functionTests/tests/lang/
Date Mon, 15 Oct 2012 20:13:37 GMT
Author: rhillegas
Date: Mon Oct 15 20:13:36 2012
New Revision: 1398489

URL: http://svn.apache.org/viewvc?rev=1398489&view=rev
Log:
DERBY-672: Let user-defined aggregates have different input and return types.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LongMagnitude.java
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/GroupByNode.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UserDefinedAggregatesTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/build.xml

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/GroupByNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/GroupByNode.java?rev=1398489&r1=1398488&r2=1398489&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/GroupByNode.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/GroupByNode.java Mon
Oct 15 20:13:36 2012
@@ -611,7 +611,7 @@ public class GroupByNode extends SingleC
 		ColumnReference	newColumnRef;
 		ResultColumn	newRC;
 		ResultColumn	tmpRC;
-		ResultColumn	aggInputRC;
+		ResultColumn	aggResultRC;
 		ResultColumnList bottomRCL  = childResult.getResultColumns();
 		ResultColumnList groupByRCL = resultColumns;
 		ResultColumnList aggRCL;
@@ -718,7 +718,11 @@ public class GroupByNode extends SingleC
 			bottomRCL.addElement(newRC);
 			newRC.setVirtualColumnId(bottomRCL.size());
 			aggInputVColId = newRC.getVirtualColumnId();
-			aggInputRC = newRC;
+			aggResultRC = (ResultColumn) getNodeFactory().getNode(
+								C_NodeTypes.RESULT_COLUMN,
+								"##aggregate expression",
+								aggregate.getNewNullResultExpression(),
+								getContextManager());
 	
 			/*
 			** Add a reference to this column into the
@@ -755,7 +759,7 @@ public class GroupByNode extends SingleC
 			aggRCL = (ResultColumnList) getNodeFactory().getNode(
 					C_NodeTypes.RESULT_COLUMN_LIST,
 					getContextManager());
-			aggRCL.addElement(aggInputRC);
+			aggRCL.addElement(aggResultRC);
 
 			/*
 			** Note that the column ids in the row are 0 based

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LongMagnitude.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LongMagnitude.java?rev=1398489&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LongMagnitude.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LongMagnitude.java
Mon Oct 15 20:13:36 2012
@@ -0,0 +1,48 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.lang.LongMagnitude
+
+   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 org.apache.derby.agg.Aggregator;
+
+/**
+ * An aggregate which computes max(abs()), always returning a Long.
+ */
+public  class   LongMagnitude<V extends Number>    implements  Aggregator<V,Long,LongMagnitude<V>>
+{
+    private long    _magnitude;
+    private boolean _null = true;
+
+    public  LongMagnitude() {}
+
+    public  void    init() {}
+    public  void    accumulate( V value ) { more( value.longValue() ); }
+    public  void    merge( LongMagnitude<V> other ) { more( other._magnitude ); }
+    public  Long    terminate() { return _null ? null : _magnitude; }
+    
+    private void    more( long value )
+    {
+        value = value > 0 ? value : -value;
+        _magnitude = _magnitude > value ? _magnitude : value;
+        _null = false;
+    }
+    
+}

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

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UserDefinedAggregatesTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UserDefinedAggregatesTest.java?rev=1398489&r1=1398488&r2=1398489&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UserDefinedAggregatesTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UserDefinedAggregatesTest.java
Mon Oct 15 20:13:36 2012
@@ -1961,5 +1961,63 @@ public class UserDefinedAggregatesTest  
         
     }
 
+    /**
+     * <p>
+     * Test aggregates whose input and return types are different.
+     * </p>
+     */
+    public void test_13_differentReturnType() throws Exception
+    {
+        Connection conn = getConnection();
 
+        goodStatement
+            (
+             conn,
+             "create derby aggregate intMagnitude_13 for int returns bigint\n" +
+             "external name 'org.apache.derbyTesting.functionTests.tests.lang.LongMagnitude'\n"
+             );
+        goodStatement
+            (
+             conn,
+             "create derby aggregate stringMagnitude_13 for int returns varchar( 10 )\n"
+
+             "external name 'org.apache.derbyTesting.functionTests.tests.lang.LongMagnitude'\n"
+             );
+        goodStatement
+            (
+             conn,
+             "create table intValues_13( a int, b int )"
+             );
+        goodStatement
+            (
+             conn,
+             "insert into intValues_13 values ( 1, 1 ), ( 2, -100 ), ( 1, 2 ), ( 2, -1234
)"
+             );
+
+        assertResults
+            (
+             conn,
+             "select intMagnitude_13( b ) from intValues_13",
+             new String[][]
+             {
+                 { "1234" },
+             },
+             false
+             );
+        assertResults
+            (
+             conn,
+             "select a, intMagnitude_13( b ) from intValues_13 group by a",
+             new String[][]
+             {
+                 { "1", "2" },
+                 { "2", "1234" },
+             },
+             false
+             );
+
+        // the declared return type of the aggregate does not match the actual return type
+        expectCompilationError( RETURN_OUTSIDE_BOUNDS, "select stringMagnitude_13( b ) from
intValues_13" );
+        expectCompilationError( RETURN_OUTSIDE_BOUNDS, "select a, stringMagnitude_13( b )
from intValues_13 group by a" );
+    }
+    
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/build.xml
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/build.xml?rev=1398489&r1=1398488&r2=1398489&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/build.xml
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/build.xml
Mon Oct 15 20:13:36 2012
@@ -86,6 +86,7 @@
       </classpath>
       <include name="${this.dir}/*.java"/>
       <exclude name="${this.dir}/FullName.java"/>
+      <exclude name="${this.dir}/LongMagnitude.java"/>
       <exclude name="${this.dir}/GenericMode.java"/>
       <exclude name="${this.dir}/ModeAggregate.java"/>
       <exclude name="${this.dir}/LobMode.java"/>
@@ -168,6 +169,7 @@
       </classpath>
       <include name="${this.dir}/FullName.java"/>
       <include name="${this.dir}/GenericMode.java"/>
+      <include name="${this.dir}/LongMagnitude.java"/>
       <include name="${this.dir}/ModeAggregate.java"/>
       <include name="${this.dir}/LobMode.java"/>
     </javac>



Mime
View raw message