db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1500706 [1/2] - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/ engine/org/apache/derby/vti/ testing/org/apache/derbyTesting/functionTests/tests/lang/
Date Mon, 08 Jul 2013 13:00:34 GMT
Author: rhillegas
Date: Mon Jul  8 13:00:34 2013
New Revision: 1500706

URL: http://svn.apache.org/r1500706
Log:
DERBY-6256: Add basic tests for XmlVTIs, including a new way to create XmlVTIs from url strings;
tests passed cleanly on derby-6256-04-aa-addURLFactoryMethodAndTests.diff.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/xmlOptimizer.trace
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OptimizerTracer.java
    db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/XMLOptimizerTraceTest.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/OptimizerTracer.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OptimizerTracer.java?rev=1500706&r1=1500705&r2=1500706&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OptimizerTracer.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OptimizerTracer.java
Mon Jul  8 13:00:34 2013
@@ -141,39 +141,38 @@ public	class   OptimizerTracer  implemen
         try {
             final   OptTrace    tracer = OptimizerTrace.getOptimizerTracer();
 
-            AccessController.doPrivileged
-                (
-                 new PrivilegedAction<Object>()
-                 {
-                     public Object run()
+            boolean     needsClosing = false;
+            PrintWriter pw;
+            
+            if (
+                (configurationParameters != null) &&
+                (configurationParameters.length > 0)
+                )
+            {
+                pw = AccessController.doPrivileged
+                    (
+                     new PrivilegedAction<PrintWriter>()
                      {
-                         try {
-                             boolean     needsClosing = false;
-
-                             PrintWriter pw;
-                             if (
-                                 (configurationParameters != null) &&
-                                 (configurationParameters.length > 0)
-                                 )
-                             {
-                                 pw = new PrintWriter( configurationParameters[ 0 ] );
-                                 needsClosing = true;
-                             }
-                             else { pw = new PrintWriter( System.out ); }
+                         public PrintWriter run()
+                         {
+                             try {
+                                 return new PrintWriter( configurationParameters[ 0 ] );
+                             } catch (IOException ioe) { throw new IllegalArgumentException(
ioe.getMessage(), ioe ); }
+                         }  
+                     }
+                     );
+                needsClosing = true;
+            }
+            else { pw = new PrintWriter( System.out ); }
         
-                             if ( tracer != null )
-                             {
-                                 tracer.printToWriter( pw );
-                                 pw.flush();
-                             }
-
-                             if ( needsClosing ) { pw.close(); }
-                         
-                             return null;
-                         } catch (IOException ioe) { throw new IllegalArgumentException(
ioe.getMessage(), ioe ); }
-                     }  
-                 }
-                 );
+            if ( tracer != null )
+            {
+                tracer.printToWriter( pw );
+                pw.flush();
+            }
+
+            if ( needsClosing ) { pw.close(); }
+            
         }
         catch (Exception e) { throw wrap( e ); }
         finally

Modified: db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java?rev=1500706&r1=1500705&r2=1500706&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java Mon Jul  8 13:00:34 2013
@@ -23,7 +23,9 @@ package org.apache.derby.vti;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.InputStream;
 import java.io.IOException;
+import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.sql.ResultSetMetaData;
@@ -166,7 +168,7 @@ public  class   XmlVTI  extends StringCo
     ///////////////////////////////////////////////////////////////////////////////////
 
     private String      _rowTag;
-    private String      _xmlResourceName;
+    private InputStream _xmlResource;
 
     private int             _rowIdx = -1;
     private int             _rowCount = -1;
@@ -190,15 +192,15 @@ public  class   XmlVTI  extends StringCo
 
     /**
      * <p>
-     * Build a XmlVTI given the name of an xml resource, the  tag of the row
+     * Build a XmlVTI given an xml resource stream, the  tag of the row
      * element, and an array of attribute-names/element-tags underneath the row element
      * </p>
      */
-    public  XmlVTI( String xmlResourceName, String rowTag, int firstChildTagIdx, String...
columnTags )
+    public  XmlVTI( InputStream xmlResource, String rowTag, int firstChildTagIdx, String...
columnTags )
     {
         super( columnTags );
 
-        _xmlResourceName = xmlResourceName;
+        _xmlResource = xmlResource;
         _rowTag = rowTag;
         _firstChildTagIdx = firstChildTagIdx;
     }
@@ -209,22 +211,76 @@ public  class   XmlVTI  extends StringCo
     //
     ///////////////////////////////////////////////////////////////////////////////////
 
-    /** This is the static method for creating functions with only child tags */
-    public  static  XmlVTI  xmlVTI( String xmlResourceName, String rowTag, String... childTags
)
+    /** This is the static method for creating functions from a file name and child tags
*/
+    public  static  XmlVTI  xmlVTI( String fileName, String rowTag, String... childTags )
+        throws Exception
+    {
+        return xmlVTI( fileName, rowTag, null, asList( childTags ) );
+    }
+    
+    /** This is the static method for creating functions from an url and child tags */
+    public  static  XmlVTI  xmlVTIFromURL( String urlString, String rowTag, String... childTags
)
+        throws Exception
     {
-        return new XmlVTI( xmlResourceName, rowTag, 0, childTags );
+        return xmlVTIFromURL( urlString, rowTag, null, asList( childTags ) );
     }
     
-    /** This is the static method for creating functions with both parent and child tags
*/
+    /** This is the static method for creating functions from a file name and both parent
and child tags */
     public  static  XmlVTI  xmlVTI
-        ( String xmlResourceName, String rowTag, ArrayList<String> parentTags, ArrayList<String>
childTags )
+        ( final String fileName, String rowTag, ArrayList<String> parentTags, ArrayList<String>
childTags )
+        throws Exception
+    {
+        FileInputStream fis = AccessController.doPrivileged
+            (
+             new PrivilegedAction<FileInputStream>()
+             {
+                 public FileInputStream run()
+                 {
+                     try {
+                         return new FileInputStream( new File( fileName ) );
+                     }
+                     catch (IOException ioe) { throw new IllegalArgumentException( ioe.getMessage(),
ioe ); }
+                 }  
+             }
+           );
+        return xmlVTI( fis, rowTag, parentTags, childTags );
+    }
+
+    /** This is the static method for creating functions from an URL and both parent and
child tags */
+    public  static  XmlVTI  xmlVTIFromURL
+        ( final String urlString, String rowTag, ArrayList<String> parentTags, ArrayList<String>
childTags )
+        throws Exception
     {
+        InputStream is = AccessController.doPrivileged
+            (
+             new PrivilegedAction<InputStream>()
+             {
+                 public InputStream run()
+                 {
+                     try {
+                         return (new URL( urlString )).openStream();
+                     }
+                     catch (IOException ioe) { throw new IllegalArgumentException( ioe.getMessage(),
ioe ); }
+                 }  
+             }
+           );
+        return xmlVTI( is, rowTag, parentTags, childTags );
+    }
+
+    /** This is the static method for creating functions from an URL and both parent and
child tags */
+    private  static  XmlVTI  xmlVTI
+        ( InputStream xmlResource, String rowTag, ArrayList<String> parentTags, ArrayList<String>
childTags )
+        throws Exception
+    {
+        if ( parentTags == null ) { parentTags = new ArrayList<String>(); }
+        if ( childTags == null ) { childTags = new ArrayList<String>(); }
+        
         String[]    allTags = new String[ parentTags.size() + childTags.size() ];
         int     idx = 0;
         for ( String tag : parentTags ) { allTags[ idx++ ] = tag; }
         for ( String tag : childTags ) { allTags[ idx++ ] = tag; }
         
-        return new XmlVTI( xmlResourceName, rowTag, parentTags.size(), allTags );
+        return new XmlVTI( xmlResource, rowTag, parentTags.size(), allTags );
     }
 
     /** Factory method to create an ArrayList<String> */
@@ -312,30 +368,13 @@ public  class   XmlVTI  extends StringCo
         
         _builder = factory.newDocumentBuilder();
 
-        AccessController.doPrivileged
-            (
-             new PrivilegedAction<Object>()
-             {
-                 public Object run()
-                 {
-                     try {
-                         File                file = new File( _xmlResourceName );
-                         FileInputStream     is = new FileInputStream( file );
-                         Document        doc = _builder.parse( is );
-                         Element             root = doc.getDocumentElement();
-                         
-                         _rawRows = root.getElementsByTagName( _rowTag );
-                         _rowCount = _rawRows.getLength();
+        Document        doc = _builder.parse( _xmlResource );
+        Element             root = doc.getDocumentElement();
                          
-                         is.close();
+        _rawRows = root.getElementsByTagName( _rowTag );
+        _rowCount = _rawRows.getLength();
                          
-                         return null;
-                     }
-                     catch (IOException ioe) { throw new IllegalArgumentException( ioe.getMessage(),
ioe ); }
-                     catch (SAXException ioe) { throw new IllegalArgumentException( ioe.getMessage(),
ioe ); }
-                 }  
-             }
-           );
+        _xmlResource.close();
     }
     
     /**

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/XMLOptimizerTraceTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/XMLOptimizerTraceTest.java?rev=1500706&r1=1500705&r2=1500706&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/XMLOptimizerTraceTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/XMLOptimizerTraceTest.java
Mon Jul  8 13:00:34 2013
@@ -22,6 +22,7 @@
 package org.apache.derbyTesting.functionTests.tests.lang;
 
 import java.io.File;
+import java.net.URL;
 import java.sql.CallableStatement;
 import java.sql.Connection;
 import java.sql.DriverManager;
@@ -49,6 +50,7 @@ public class XMLOptimizerTraceTest  exte
     ///////////////////////////////////////////////////////////////////////////////////
 
     private static  final   String  TRACE_FILE_NAME = "xott.xml";
+    private static  final   String  SAVED_TRACE_NAME = "xmlOptimizer.trace";
 
     ///////////////////////////////////////////////////////////////////////////////////
     //
@@ -83,11 +85,13 @@ public class XMLOptimizerTraceTest  exte
      */
     public static Test suite()
     {
+        String[]    testFiles = new String[] { "functionTests/tests/lang/" + SAVED_TRACE_NAME
};
+
         TestSuite       suite = new TestSuite( "XMLOptimizerTraceTest" );
 
         suite.addTest( TestConfiguration.defaultSuite( XMLOptimizerTraceTest.class ) );
-
-        return new SupportFilesSetup( TestConfiguration.singleUseDatabaseDecorator( suite
) );
+ 
+        return new SupportFilesSetup( TestConfiguration.singleUseDatabaseDecorator( suite
), testFiles );
     }
 
     protected void    setUp()
@@ -291,6 +295,253 @@ public class XMLOptimizerTraceTest  exte
              );
     }
 
+    /**
+     * <p>
+     * Some general tests for XmlVTI.
+     * </p>
+     */
+    public void test_02_xmlVTI() throws Exception
+    {
+        Connection conn = getConnection();
+        File    traceFile = SupportFilesSetup.getReadOnly( SAVED_TRACE_NAME );
+        URL     traceURL = SupportFilesSetup.getReadOnlyURL( SAVED_TRACE_NAME );
+        String[][]  resultsParentAndChild = new String[][]
+            {
+                { "1", "R_A", "HASH", "20.1395", "6" },
+                { "1", "R_A", "NESTEDLOOP", "20.039500000000004", "6" },
+                { "1",  "S_A", "HASH", "20.1395", "6" },
+                { "1", "S_A", "NESTEDLOOP", "20.039500000000004", "6" },
+                { "1", "T_A", "HASH", "20.1395", "6" },
+                { "1", "T_A", "NESTEDLOOP", "20.039500000000004", "6" },
+            };
+        String[][]  resultsChildOnly = new String[][]
+            {
+                { "R_A", "HASH", "20.1395", "6" },
+                { "R_A", "NESTEDLOOP", "20.039500000000004", "6" },
+                { "S_A", "HASH", "20.1395", "6" },
+                { "S_A", "NESTEDLOOP", "20.039500000000004", "6" },
+                { "T_A", "HASH", "20.1395", "6" },
+                { "T_A", "NESTEDLOOP", "20.039500000000004", "6" },
+            };
+
+        // create the type and factory function needed by the XmlVTI
+        goodStatement
+            (
+             conn,
+             "create type ArrayList external name 'java.util.ArrayList' language java"
+             );
+        goodStatement
+            (
+             conn,
+             "create function asList( cell varchar( 32672 ) ... ) returns ArrayList\n" +
+             "language java parameter style derby no sql\n" +
+             "external name 'org.apache.derby.vti.XmlVTI.asList'\n"
+             );
+
+        // create an XmlVTI which reads from a file and incorporates parent tags
+        goodStatement
+            (
+             conn,
+             "create function decorationWithParentInfo\n" +
+             "(\n" +
+             "    fileName varchar( 32672 ),\n" +
+             "    rowTag varchar( 32672 ),\n" +
+             "    parentTags ArrayList,\n" +
+             "    childTags ArrayList\n" +
+             ")\n" +
+             "returns table\n" +
+             "(\n" +
+             "        qbID int,\n" +
+             "        conglomerateName varchar( 36 ),\n" +
+             "        joinStrategy varchar( 20 ),\n" +
+             "        estimatedCost double,\n" +
+             "        estimatedRowCount int\n" +
+             ")\n" +
+             "language java parameter style derby_jdbc_result_set no sql\n" +
+             "external name 'org.apache.derby.vti.XmlVTI.xmlVTI'\n"
+             );
+        goodStatement
+            (
+             conn,
+             "create view decorationWithParentInfo as\n" +
+             "select * from table\n" +
+             "(\n" +
+             "    decorationWithParentInfo\n" +
+             "    (\n" +
+             "        '" + traceFile.getPath() + "',\n" +
+             "        'decoration',\n" +
+             "        asList( 'qbID' ),\n" +
+             "        asList( 'decConglomerateName', 'decJoinStrategy', 'ceEstimatedCost',
'ceEstimatedRowCount' )\n" +
+             "    )\n" +
+             ") v\n"
+             );
+
+        // create an XmlVTI which reads from a file and only used child tags
+        goodStatement
+            (
+             conn,
+             "create function decorationChildOnly\n" +
+             "(\n" +
+             "    fileName varchar( 32672 ),\n" +
+             "    rowTag varchar( 32672 ),\n" +
+             "    childTags varchar( 32672 )...\n" +
+             ")\n" +
+             "returns table\n" +
+             "(\n" +
+             "        conglomerateName varchar( 36 ),\n" +
+             "        joinStrategy varchar( 20 ),\n" +
+             "        estimatedCost double,\n" +
+             "        estimatedRowCount int\n" +
+             ")\n" +
+             "language java parameter style derby_jdbc_result_set no sql\n" +
+             "external name 'org.apache.derby.vti.XmlVTI.xmlVTI'\n"
+             );
+        goodStatement
+            (
+             conn,
+             "create view decorationChildOnly as\n" +
+             "select * from table\n" +
+             "(\n" +
+             "    decorationChildOnly\n" +
+             "    (\n" +
+             "        '" + traceFile.getPath() + "',\n" +
+             "        'decoration',\n" +
+             "        'decConglomerateName', 'decJoinStrategy', 'ceEstimatedCost', 'ceEstimatedRowCount'\n"
+
+             "    )\n" +
+             ") v\n"
+             );
+
+        // create an XmlVTI which reads from an url file and uses parent tags
+        goodStatement
+            (
+             conn,
+             "create function decorationURLParentInfo\n" +
+             "(\n" +
+             "    urlString varchar( 32672 ),\n" +
+             "    rowTag varchar( 32672 ),\n" +
+             "    parentTags ArrayList,\n" +
+             "    childTags ArrayList\n" +
+             ")\n" +
+             "returns table\n" +
+             "(\n" +
+             "        qbID int,\n" +
+             "        conglomerateName varchar( 36 ),\n" +
+             "        joinStrategy varchar( 20 ),\n" +
+             "        estimatedCost double,\n" +
+             "        estimatedRowCount int\n" +
+             ")\n" +
+             "language java parameter style derby_jdbc_result_set no sql\n" +
+             "external name 'org.apache.derby.vti.XmlVTI.xmlVTIFromURL'\n"
+             );
+        goodStatement
+            (
+             conn,
+             "create view decorationURLParentInfo as\n" +
+             "select * from table\n" +
+             "(\n" +
+             "    decorationURLParentInfo\n" +
+             "    (\n" +
+             "        '" + traceURL.toString() + "',\n" +
+             "        'decoration',\n" +
+             "        asList( 'qbID' ),\n" +
+             "        asList( 'decConglomerateName', 'decJoinStrategy', 'ceEstimatedCost',
'ceEstimatedRowCount' )\n" +
+             "    )\n" +
+             ") v\n"
+             );
+
+        // create an XmlVTI which reads from an url file and uses only child tags
+        goodStatement
+            (
+             conn,
+             "create function decorationURLChildOnly\n" +
+             "(\n" +
+             "    urlString varchar( 32672 ),\n" +
+             "    rowTag varchar( 32672 ),\n" +
+             "    childTags varchar( 32672 )...\n" +
+             ")\n" +
+             "returns table\n" +
+             "(\n" +
+             "        conglomerateName varchar( 36 ),\n" +
+             "        joinStrategy varchar( 20 ),\n" +
+             "        estimatedCost double,\n" +
+             "        estimatedRowCount int\n" +
+             ")\n" +
+             "language java parameter style derby_jdbc_result_set no sql\n" +
+             "external name 'org.apache.derby.vti.XmlVTI.xmlVTIFromURL'\n"
+             );
+        goodStatement
+            (
+             conn,
+             "create view decorationURLChildOnly as\n" +
+             "select * from table\n" +
+             "(\n" +
+             "    decorationURLChildOnly\n" +
+             "    (\n" +
+             "        '" + traceURL.toString() + "',\n" +
+             "        'decoration',\n" +
+             "        'decConglomerateName', 'decJoinStrategy', 'ceEstimatedCost', 'ceEstimatedRowCount'\n"
+
+             "    )\n" +
+             ") v\n"
+             );
+
+        // verify that the XmlVTIs work
+        assertResults
+            (
+             conn,
+             "select distinct qbID, conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n"
+
+             "from decorationWithParentInfo\n" +
+             "where conglomerateName like '%_A' and estimatedCost is not null\n" +
+             "order by qbID, conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n",
+             resultsParentAndChild,
+             false
+             );
+
+        assertResults
+            (
+             conn,
+             "select distinct conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n"
+
+             "from decorationChildOnly\n" +
+             "where conglomerateName like '%_A' and estimatedCost is not null\n" +
+             "order by conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n",
+             resultsChildOnly,
+             false
+             );
+        
+        assertResults
+            (
+             conn,
+             "select distinct qbID, conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n"
+
+             "from decorationURLParentInfo\n" +
+             "where conglomerateName like '%_A' and estimatedCost is not null\n" +
+             "order by qbID, conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n",
+             resultsParentAndChild,
+             false
+             );
+
+        assertResults
+            (
+             conn,
+             "select distinct conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n"
+
+             "from decorationURLChildOnly\n" +
+             "where conglomerateName like '%_A' and estimatedCost is not null\n" +
+             "order by conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n",
+             resultsChildOnly,
+             false
+             );
+        
+        // clean up after ourselves
+        goodStatement( conn, "drop view decorationURLChildOnly" );
+        goodStatement( conn, "drop function decorationURLChildOnly" );
+        goodStatement( conn, "drop view decorationURLParentInfo" );
+        goodStatement( conn, "drop function decorationURLParentInfo" );
+        goodStatement( conn, "drop view decorationChildOnly" );
+        goodStatement( conn, "drop function decorationChildOnly" );
+        goodStatement( conn, "drop view decorationWithParentInfo" );
+        goodStatement( conn, "drop function decorationWithParentInfo" );
+        goodStatement( conn, "drop function asList" );
+        goodStatement( conn, "drop type ArrayList restrict" );
+    }
+
    ///////////////////////////////////////////////////////////////////////////////////
     //
     // MINIONS

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=1500706&r1=1500705&r2=1500706&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 Jul  8 13:00:34 2013
@@ -91,7 +91,7 @@
   <target name="copyfiles">
     <copy todir="${out.dir}/${derby.testing.functest.dir}/tests/lang">
       <fileset dir="${derby.testing.src.dir}/${derby.testing.functest.dir}/tests/lang"

-        includes="*.sql,*.properties,*.subsql,*.policy,*.jar,*.dat,*.tstlog,xmlTestFiles/*"/>
 
+        includes="*.sql,*.properties,*.subsql,*.policy,*.jar,*.dat,*.tstlog,*.trace,xmlTestFiles/*"/>
 
     </copy>
   </target> 
 



Mime
View raw message