db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r540856 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/master/ functionTests/suites/ functionTests/tests/lang/ junit/
Date Wed, 23 May 2007 06:34:40 GMT
Author: kahatlen
Date: Tue May 22 23:34:39 2007
New Revision: 540856

URL: http://svn.apache.org/viewvc?view=rev&rev=540856
Log:
DERBY-2642: Convert lang/dynamicLikeOptimization.sql to JUnit

Added missing test cases and removed the old test.

Removed:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/dynamicLikeOptimization.out
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dynamicLikeOptimization.sql
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dynamicLikeOptimization_app.properties
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dynamicLikeOptimization_derby.properties
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbylang.runall
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DynamicLikeOptimizationTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/RuntimeStatisticsParser.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbylang.runall
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbylang.runall?view=diff&rev=540856&r1=540855&r2=540856
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbylang.runall
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbylang.runall
Tue May 22 23:34:39 2007
@@ -23,7 +23,6 @@
 lang/declareGlobalTempTableJavaJDBC30.java
 lang/desc_index.sql
 lang/dropTable.sql
-lang/dynamicLikeOptimization.sql
 lang/emptyStatistics.sql
 lang/errorStream.java
 lang/fk_nonSPS.sql

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DynamicLikeOptimizationTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DynamicLikeOptimizationTest.java?view=diff&rev=540856&r1=540855&r2=540856
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DynamicLikeOptimizationTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DynamicLikeOptimizationTest.java
Tue May 22 23:34:39 2007
@@ -33,6 +33,8 @@
 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
 import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
 import org.apache.derbyTesting.junit.JDBC;
+import org.apache.derbyTesting.junit.RuntimeStatisticsParser;
+import org.apache.derbyTesting.junit.SQLUtilities;
 import org.apache.derbyTesting.junit.TestConfiguration;
 
 /**
@@ -520,5 +522,49 @@
         s.close();
         ps[0].close();
         ps[1].close();
+    }
+
+    /**
+     * Test that dynamic like optimization is performed. That is, the LIKE
+     * predicate is rewritten to >=, < and LIKE.
+     */
+    public void testDynamicLikeOptimization() throws SQLException {
+        Statement s = createStatement();
+        s.execute("CALL SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1)");
+        PreparedStatement ps =
+            prepareStatement("select id from test where vc10 like ?");
+        ps.setString(1, "%");
+        JDBC.assertDrainResults(ps.executeQuery());
+        RuntimeStatisticsParser p = SQLUtilities.getRuntimeStatisticsParser(s);
+        assertTrue(p.hasGreaterThanOrEqualQualifier());
+        assertTrue(p.hasLessThanQualifier());
+        s.close();
+        ps.close();
+    }
+
+    public void testCast() throws SQLException {
+        Statement s = createStatement();
+        JDBC.assertSingleValueResultSet(
+            s.executeQuery("select 1 from t1 where 'asdf' like " +
+                           "cast('%f' as varchar(2))"),
+            "1");
+        JDBC.assertEmpty(s.executeQuery("select 1 from t1 where 'asdf' like " +
+                                        "cast(null as char)"));
+        JDBC.assertSingleValueResultSet(
+            s.executeQuery("select 1 from t1 where '%foobar' like 'Z%foobar' " +
+                           "escape cast('Z' as varchar(1))"),
+            "1");
+        // quoted values clause should not match anything
+        JDBC.assertEmpty(s.executeQuery(
+                "select vc10 from test where vc10 like " +
+                "'values cast(null as varchar(1))'"));
+        JDBC.assertEmpty(s.executeQuery(
+                "select id from test where c10 like " +
+                "cast ('%f' as varchar(2))"));
+        JDBC.assertUnorderedResultSet(s.executeQuery(
+                "select id from test where vc10 like " +
+                "cast ('%f' as varchar(2))"),
+                new String[][] { {"asdf"}, {"aasdf"} });
+        s.close();
     }
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/RuntimeStatisticsParser.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/RuntimeStatisticsParser.java?view=diff&rev=540856&r1=540855&r2=540856
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/RuntimeStatisticsParser.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/RuntimeStatisticsParser.java
Tue May 22 23:34:39 2007
@@ -20,6 +20,8 @@
 package org.apache.derbyTesting.junit;
 
 import java.sql.Connection;
+import java.util.HashSet;
+import java.util.StringTokenizer;
 
 public class RuntimeStatisticsParser {
 
@@ -31,6 +33,7 @@
     private final boolean indexRowToBaseRow;
     private String statistics = "";
     private boolean scrollInsensitive = false;
+    private final HashSet qualifiers;
 
     /**
      * Create a RuntimeStatistics object to parse the text and extract
@@ -68,6 +71,71 @@
         }
         if (rts.indexOf("Scroll Insensitive ResultSet:") > 0)
             scrollInsensitive = true;
+
+        qualifiers = findQualifiers();
+    }
+
+    /**
+     * Class which represents a qualifier used in a scan.
+     */
+    private static class Qualifier {
+        String operator;
+        boolean negated;
+        Qualifier(String operator, boolean negated) {
+            this.operator = operator;
+            this.negated = negated;
+        }
+        public int hashCode() {
+            if (negated) {
+                return ~(operator.hashCode());
+            }
+            return operator.hashCode();
+        }
+        public boolean equals(Object o) {
+            if (o instanceof Qualifier) {
+                Qualifier q = (Qualifier) o;
+                return (negated == q.negated) && operator.equals(q.operator);
+            }
+            return false;
+        }
+        /**
+         * Represent the qualifier as a string for debugging.
+         */
+        public String toString() {
+            return (negated ? "\u00ac" : "" ) + operator;
+        }
+    }
+
+    /**
+     * Find all qualifiers in a query plan.
+     *
+     * @return set of <code>Qualifier</code>s
+     */
+    private HashSet findQualifiers() {
+        HashSet set = new HashSet();
+        int startPos = statistics.indexOf("qualifiers:\n");
+        if (startPos >= 0) {
+            // start search after "qualifiers:\n"
+            String searchString = statistics.substring(startPos + 12);
+            StringTokenizer t = new StringTokenizer(searchString, "\n");
+            while (t.hasMoreTokens()) {
+                String s = t.nextToken();
+                if (s.startsWith("Operator: ")) {
+                    String operator = s.substring(10);
+                    t.nextToken();  // skip "Ordered nulls: ..."
+                    t.nextToken();  // skip "Unknown return value: ..."
+                    s = t.nextToken();
+                    if (!s.startsWith("Negate comparison result: ")) {
+                        throw new AssertionError(
+                            "Expected to find \"Negate comparison result\"");
+                    }
+                    boolean negated =
+                        Boolean.valueOf(s.substring(26)).booleanValue();
+                    set.add(new Qualifier(operator, negated));
+                }
+            }
+        }
+        return set;
     }
 
     /**
@@ -119,5 +187,19 @@
     public boolean isScrollInsensitive(){
         return scrollInsensitive;
     }
-    
+
+    /**
+     * Return whether or not the query used a &gt;= scan qualifier.
+     */
+    public boolean hasGreaterThanOrEqualQualifier() {
+        // < negated is equivalent to >=
+        return qualifiers.contains(new Qualifier("<", true));
+    }
+
+    /**
+     * Return whether or not the query used a &lt; scan qualifier.
+     */
+    public boolean hasLessThanQualifier() {
+        return qualifiers.contains(new Qualifier("<", false));
+    }
 }



Mime
View raw message