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 rh...@apache.org on 2012/09/27 16:43:23 UTC

svn commit: r1391034 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/ testing/org/apache/derbyTesting/functionTests/tests/lang/

Author: rhillegas
Date: Thu Sep 27 14:43:23 2012
New Revision: 1391034

URL: http://svn.apache.org/viewvc?rev=1391034&view=rev
Log:
DERBY-672: Allow aggregates on user defined types.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FullName.java   (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateAliasNode.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/QueryTreeNode.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GenericMode.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/CreateAliasNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateAliasNode.java?rev=1391034&r1=1391033&r2=1391034&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateAliasNode.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateAliasNode.java Thu Sep 27 14:43:23 2012
@@ -147,8 +147,8 @@ public class CreateAliasNode extends DDL
 				this.javaClassName = (String) targetObject;
 
 				Object[] aggElements = (Object[]) aliasSpecificInfo;
-                TypeDescriptor  aggForType = (TypeDescriptor) aggElements[ AGG_FOR_TYPE ];
-                TypeDescriptor  aggReturnType = (TypeDescriptor) aggElements[ AGG_RETURN_TYPE ];
+                TypeDescriptor  aggForType = bindUserCatalogType( (TypeDescriptor) aggElements[ AGG_FOR_TYPE ] );
+                TypeDescriptor  aggReturnType = bindUserCatalogType( (TypeDescriptor) aggElements[ AGG_RETURN_TYPE ] );
 
 				aliasInfo = new AggregateAliasInfo( aggForType, aggReturnType );
 				implicitCreateSchema = true;
@@ -488,16 +488,7 @@ public class CreateAliasNode extends DDL
         int count = parameterTypes.length;
         for ( int i = 0; i < count; i++ )
         {
-            TypeDescriptor td = parameterTypes[ i ];
-
-            // if this is a user defined type, resolve the Java class name
-            if ( td.isUserDefinedType() )
-            {
-                DataTypeDescriptor dtd = DataTypeDescriptor.getType( td );
-
-                dtd = bindUserType( dtd );
-                parameterTypes[ i ] = dtd.getCatalogType();
-            }
+            parameterTypes[ i ] = bindUserCatalogType( parameterTypes[ i ] );
         }
     }
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/QueryTreeNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/QueryTreeNode.java?rev=1391034&r1=1391033&r2=1391034&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/QueryTreeNode.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/QueryTreeNode.java Thu Sep 27 14:43:23 2012
@@ -1651,6 +1651,21 @@ public abstract class QueryTreeNode impl
         return result;
     }
 
+    /** Bind user defined types as necessary */
+    public  TypeDescriptor  bindUserCatalogType( TypeDescriptor td )
+        throws StandardException
+    {
+        // if this is a user defined type, resolve the Java class name
+        if ( !td.isUserDefinedType() ) { return td; }
+        else
+        {
+            DataTypeDescriptor dtd = DataTypeDescriptor.getType( td );
+
+            dtd = bindUserType( dtd );
+            return dtd.getCatalogType();
+        }
+    }
+
     /**
      * Bind the UDTs in a table type.
      *
@@ -1669,19 +1684,7 @@ public abstract class QueryTreeNode impl
 
         for ( int i = 0; i < columnCount; i++ )
         {
-            TypeDescriptor columnType = columnTypes[ i ];
-
-            if ( columnType.isUserDefinedType() )
-            {
-                DataTypeDescriptor newColumnDTD = DataTypeDescriptor.getType( columnType );
-
-                newColumnDTD = bindUserType( newColumnDTD );
-
-                TypeDescriptor newColumnType = newColumnDTD.getCatalogType();
-
-                // poke the bound type back into the multi set descriptor
-                columnTypes[ i ] = newColumnType;
-            }
+            columnTypes[ i ] = bindUserCatalogType( columnTypes[ i ] );
         }
 
         return originalDTD;

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FullName.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FullName.java?rev=1391034&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FullName.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FullName.java Thu Sep 27 14:43:23 2012
@@ -0,0 +1,98 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.lang.FullName
+
+   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.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+/**
+ * A Comparable UDT for tests.
+ */
+public class FullName implements Comparable<FullName>, Externalizable
+{
+    // initial version id
+    private static final int FIRST_VERSION = 0;
+
+    private String  _firstName;
+    private String  _lastName;
+
+    // 0-arg constructor needed for UDT contract
+    public  FullName() {}
+
+    public FullName( String firstName, String lastName )
+    {
+        _firstName = firstName;
+        _lastName = lastName;
+    }
+
+    public  String  firstName() { return _firstName; }
+    public  String  lastName() { return _lastName; }
+
+        // methods to be registered as functions
+    public static FullName makeFullName( String firstName, String lastName ) { return new FullName( firstName, lastName ); }
+
+    public  String  toString()
+    {
+        return _firstName + " " + _lastName;
+    }
+
+    // Externalizable implementation
+    public void writeExternal( ObjectOutput out ) throws IOException
+    {
+        // first write the version id
+        out.writeInt( FIRST_VERSION );
+
+        // now write the state
+        out.writeObject( _firstName );
+        out.writeObject( _lastName );
+    }  
+    public void readExternal( ObjectInput in )throws IOException, ClassNotFoundException
+    {
+        // read the version id
+        int oldVersion = in.readInt();
+
+        _firstName = (String) in.readObject();
+        _lastName = (String) in.readObject();
+    }
+
+    // Comparable implementation
+    public  int compareTo( FullName that )
+    {
+        if ( that == null ) { return 1; }
+
+        int     result = this._lastName.compareTo( that._lastName );
+        if ( result != 0 ) { return result; }
+
+        return this._firstName.compareTo( that._firstName );
+    }
+    public  boolean equals( Object other )
+    {
+        if ( other == null ) { return false; }
+        else if ( !(other instanceof FullName) ) { return false; }
+        else { return (compareTo( (FullName) other ) == 0); }
+    }
+    public  int hashCode() { return toString().hashCode(); }
+
+}
+

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

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GenericMode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GenericMode.java?rev=1391034&r1=1391033&r2=1391034&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GenericMode.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GenericMode.java Thu Sep 27 14:43:23 2012
@@ -35,6 +35,7 @@ public  class   GenericMode<V extends Co
 
     public  static  final   class   IntMode extends GenericMode<Integer> {}
     public  static  final   class   StringMode extends GenericMode<String> {}
+    public  static  final   class   FullNameMode extends GenericMode<FullName> {}
     
     ///////////////////////////////////////////////////////////////////////////////////
     //

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=1391034&r1=1391033&r2=1391034&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 Thu Sep 27 14:43:23 2012
@@ -817,4 +817,69 @@ public class UserDefinedAggregatesTest  
         
     }
     
+    /**
+     * <p>
+     * Test aggregates on user defined types.
+     * </p>
+     */
+    public void test_08_basicUDTaggregates() throws Exception
+    {
+        Connection conn = getConnection();
+
+        goodStatement
+            ( conn,
+              "create type FullName external name 'org.apache.derbyTesting.functionTests.tests.lang.FullName' language java" );
+        goodStatement
+            (
+             conn,
+             "create function makeFullName( firstName varchar( 32672 ), lastName varchar( 32672 ) )\n" +
+             "returns FullName language java parameter style java\n" +
+             "external name 'org.apache.derbyTesting.functionTests.tests.lang.FullName.makeFullName'"
+             );
+        goodStatement
+            (
+             conn,
+             "create derby aggregate fullNameMode for FullName\n" +
+             "external name 'org.apache.derbyTesting.functionTests.tests.lang.GenericMode$FullNameMode'"
+             );
+        goodStatement
+            ( conn,
+              "create table fullNameMode_inputs( a int, b FullName )" );
+        goodStatement
+            (
+             conn,
+             "insert into fullNameMode_inputs( a, b )\n" +
+             "values\n" +
+             "( 1, makeFullName( 'one', 'name'  ) ),\n" +
+             "( 1, makeFullName( 'two', 'name' ) ),\n" +
+             "( 1, makeFullName( 'two', 'name' ) ),\n" +
+             "( 1, makeFullName( 'two', 'name' ) ),\n" +
+             "( 2, makeFullName( 'three', 'name' ) ),\n" +
+             "( 2, makeFullName( 'three', 'name' ) ),\n" +
+             "( 2, makeFullName( 'four', 'name' ) )\n"
+             );
+        
+        assertResults
+            (
+             conn,
+             "select fullNameMode( b ) from fullNameMode_inputs",
+             new String[][]
+             {
+                 { "two name" }
+             },
+             false
+             );
+        assertResults
+            (
+             conn,
+             "select a, fullNameMode( b ) from fullNameMode_inputs group by a",
+             new String[][]
+             {
+                 { "1", "two name" },
+                 { "2", "three name" }
+             },
+             false
+             );
+    }
+
 }

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=1391034&r1=1391033&r2=1391034&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 Thu Sep 27 14:43:23 2012
@@ -85,6 +85,7 @@
       	<pathelement path="${junit}"/>
       </classpath>
       <include name="${this.dir}/*.java"/>
+      <exclude name="${this.dir}/FullName.java"/>
       <exclude name="${this.dir}/GenericMode.java"/>
       <exclude name="${this.dir}/ModeAggregate.java"/>
       <exclude name="${this.dir}/declareGlobalTempTableJavaJDBC30.java"/>
@@ -164,6 +165,7 @@
       <classpath>
         <pathelement path="${java15compile.classpath}"/>
       </classpath>
+      <include name="${this.dir}/FullName.java"/>
       <include name="${this.dir}/GenericMode.java"/>
       <include name="${this.dir}/ModeAggregate.java"/>
     </javac>