db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r907097 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/ engine/org/apache/derby/impl/sql/execute/ testing/org/apache/derbyTesting/functionTests/tests/lang/
Date Fri, 05 Feb 2010 21:19:25 GMT
Author: rhillegas
Date: Fri Feb  5 21:19:23 2010
New Revision: 907097

URL: http://svn.apache.org/viewvc?rev=907097&view=rev
Log:
DERBY-651: Make it possible to have UDT columns in table functions.

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/engine/org/apache/derby/impl/sql/execute/DDLConstantAction.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UDTTest.java

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=907097&r1=907096&r2=907097&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
Fri Feb  5 21:19:23 2010
@@ -211,11 +211,9 @@
                 if ( returnType != null )
                 {
                     DataTypeDescriptor dtd = DataTypeDescriptor.getType( returnType );
-                    if ( dtd.getTypeId().isUserDefinedTypeId() )
-                    {
-                        dtd = bindUserType( dtd );
-                        returnType = dtd.getCatalogType();
-                    }
+                    
+                    dtd = bindUserType( dtd );
+                    returnType = dtd.getCatalogType();
                 }
 
 				aliasInfo = new RoutineAliasInfo(this.methodName, paramCount, names, types, modes, drs,

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=907097&r1=907096&r2=907097&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 Fri
Feb  5 21:19:23 2010
@@ -26,6 +26,8 @@
 
 import org.apache.derby.catalog.AliasInfo;
 import org.apache.derby.catalog.types.SynonymAliasInfo;
+import org.apache.derby.catalog.TypeDescriptor;
+import org.apache.derby.catalog.types.RowMultiSetImpl;
 import org.apache.derby.catalog.types.UserDefinedTypeIdImpl;
 import org.apache.derby.iapi.services.i18n.MessageService;
 import org.apache.derby.iapi.error.StandardException;
@@ -1631,6 +1633,9 @@
      */
     public DataTypeDescriptor bindUserType( DataTypeDescriptor originalDTD ) throws StandardException
     {
+        // if the type is a table type, then we need to bind its user-typed columns
+        if ( originalDTD.getCatalogType().isRowMultiSet() ) { return bindRowMultiSet( originalDTD
); }
+        
         // nothing to do if this is not a user defined type
         if ( !originalDTD.getTypeId().userType() ) { return originalDTD; }
 
@@ -1664,6 +1669,43 @@
     }
 
     /**
+     * Bind the UDTs in a table type.
+     *
+     * @param originalDTD A datatype: might be an unbound UDT and might not be
+     *
+     * @return The bound table type if originalDTD was an unbound table type; otherwise returns
originalDTD.
+     */
+    public DataTypeDescriptor bindRowMultiSet( DataTypeDescriptor originalDTD ) throws StandardException
+    {
+        if ( !originalDTD.getCatalogType().isRowMultiSet() ) { return originalDTD; }
+
+        RowMultiSetImpl originalMultiSet = (RowMultiSetImpl) originalDTD.getTypeId().getBaseTypeId();
+        String[] columnNames = originalMultiSet.getColumnNames();
+        TypeDescriptor[] columnTypes = originalMultiSet.getTypes();
+        int columnCount = columnTypes.length;
+
+        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;
+            }
+        }
+
+        return originalDTD;
+    }
+        
+    
+    /**
      * Declare a dependency on a type and check that you have privilege to use
      * it. This is only used if the type is an ANSI UDT.
      *

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/DDLConstantAction.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/DDLConstantAction.java?rev=907097&r1=907096&r2=907097&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/DDLConstantAction.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/DDLConstantAction.java
Fri Feb  5 21:19:23 2010
@@ -1000,6 +1000,21 @@
             if ( returnTypeAD != null ) { udtMap.put( returnTypeAD.getObjectID().toString(),
returnTypeAD ); }
         }
 
+        // table functions can have udt columns. track those dependencies.
+        if ( (rawReturnType != null) && rawReturnType.isRowMultiSet() )
+        {
+            TypeDescriptor[] columnTypes = rawReturnType.getRowTypes();
+            int columnCount = columnTypes.length;
+
+            for ( int i = 0; i < columnCount; i++ )
+            {
+                AliasDescriptor       columnTypeAD = dd.getAliasDescriptorForUDT
+                    ( tc, DataTypeDescriptor.getType( columnTypes[ i ] ) );
+
+                if ( columnTypeAD != null ) { udtMap.put( columnTypeAD.getObjectID().toString(),
columnTypeAD ); }
+            }
+        }
+
         TypeDescriptor[]      paramTypes = aliasInfo.getParameterTypes();
         if ( paramTypes != null )
         {

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UDTTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UDTTest.java?rev=907097&r1=907096&r2=907097&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UDTTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/UDTTest.java
Fri Feb  5 21:19:23 2010
@@ -23,6 +23,7 @@
 
 import java.math.BigDecimal;
 import java.sql.Connection;
+import java.sql.DriverManager;
 import java.sql.ParameterMetaData;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -607,6 +608,52 @@
              badDropSQLState
              );
 
+        // table function with a udt column
+        createTypeStatement = "create type hashmap_07 external name 'java.util.HashMap' language
java\n";
+        dropTypeStatement = "drop type hashmap_07 restrict\n";
+        createObjectStatement = 
+            "create function hashmapReader_07() returns table\n" +
+            "(\n" +
+            "    a int,\n" +
+            "    b hashmap_07\n" +
+            ")\n" +
+            "language java parameter style derby_jdbc_result_set\n" +
+            "external name 'org.apache.derbyTesting.functionTests.tests.lang.UDTTest.hashmapReader'\n";
+        dropObjectStatement = "drop function hashmapReader_07\n";
+        badDropSQLState = ROUTINE_DEPENDS_ON_TYPE;
+        verifyDropRestrictions
+            (
+             conn,
+             createTypeStatement,
+             dropTypeStatement,
+             createObjectStatement,
+             dropObjectStatement,
+             badDropSQLState
+             );
+
+        // table function with a udt column and udt arg
+        createTypeStatement = "create type hashmap_07_a external name 'java.util.HashMap'
language java\n";
+        dropTypeStatement = "drop type hashmap_07_a restrict\n";
+        createObjectStatement = 
+            "create function hashmapReader_07_a( a hashmap_07_a ) returns table\n" +
+            "(\n" +
+            "    a int,\n" +
+            "    b hashmap_07_a\n" +
+            ")\n" +
+            "language java parameter style derby_jdbc_result_set\n" +
+            "external name 'org.apache.derbyTesting.functionTests.tests.lang.UDTTest.hashmapReader'\n";
+        dropObjectStatement = "drop function hashmapReader_07_a\n";
+        badDropSQLState = ROUTINE_DEPENDS_ON_TYPE;
+        verifyDropRestrictions
+            (
+             conn,
+             createTypeStatement,
+             dropTypeStatement,
+             createObjectStatement,
+             dropObjectStatement,
+             badDropSQLState
+             );
+
     }
 
     /**
@@ -726,6 +773,56 @@
         //
     }
 
+    /**
+     * <p>
+     * Verify that table functions can have UDT columns.
+     * </p>
+     */
+    public void test_11_tableFunctionColumns() throws Exception
+    {
+        Connection conn = getConnection();
+
+        goodStatement( conn, "create type hashmap_11 external name 'java.util.HashMap' language
java\n" );
+        goodStatement
+            (
+             conn,
+             "create function makeHashMap_11() returns hashmap_11\n" +
+             "language java parameter style java no sql\n" +
+             "external name 'org.apache.derbyTesting.functionTests.tests.lang.UDTTest.makeHashMap'\n"
+             );
+        goodStatement
+            (
+             conn,
+             "create function putValue_11( map hashmap_11, k varchar( 100 ), v varchar( 100
) ) returns hashmap_11\n" +
+             "language java parameter style java no sql\n" +
+             "external name 'org.apache.derbyTesting.functionTests.tests.lang.UDTTest.putValue'\n"
+             );
+        goodStatement( conn, "create table t_11( intCol int, varcharCol varchar( 10 ), hashmapCol
hashmap_11 )\n" );
+        goodStatement
+            (
+             conn,
+             "create function hashmapReader() returns table\n" +
+             "(\n" +
+             "    a int,\n" +
+             "    b hashmap_11\n" +
+             ")\n" +
+             "language java parameter style derby_jdbc_result_set\n" +
+             "external name 'org.apache.derbyTesting.functionTests.tests.lang.UDTTest.hashmapReader'\n"
+             );
+        goodStatement( conn, "insert into t_11( intCol, varcharCol, hashmapCol ) values (
2, 'def', putValue_11( makeHashMap_11(), 'kangaroo', 'foo' ) )\n" );
+
+        assertResults
+            (
+             conn,
+             "select * from table( hashmapReader() ) s",
+             new String[][]
+             {
+                 { "2" ,         "{kangaroo=foo}" },
+             },
+             true
+             );
+    }
+
     ///////////////////////////////////////////////////////////////////////////////////
     //
     // PROCEDURES AND FUNCTIONS
@@ -752,6 +849,15 @@
         return map;
     }
 
+    public static ResultSet hashmapReader() throws Exception
+    {
+        Connection conn = DriverManager.getConnection( "jdbc:default:connection" );
+
+        PreparedStatement ps = conn.prepareStatement( "select intCol, hashmapCol from t_11"
);
+
+        return ps.executeQuery();
+    }
+
     
 
     ///////////////////////////////////////////////////////////////////////////////////



Mime
View raw message