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 2014/04/06 17:23:33 UTC

svn commit: r1585313 - in /db/derby/code/trunk/java: engine/org/apache/derby/loc/ optional/org/apache/derby/optional/lucene/ shared/org/apache/derby/shared/common/reference/ testing/org/apache/derbyTesting/functionTests/tests/lang/

Author: rhillegas
Date: Sun Apr  6 15:23:32 2014
New Revision: 1585313

URL: http://svn.apache.org/r1585313
Log:
DERBY-590: Rename rank to score and forbid duplicate column names for the Lucene plugin; commit derby-590-18-aa-cleanupAPI.diff.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
    db/derby/code/trunk/java/optional/org/apache/derby/optional/lucene/LuceneQueryVTI.java
    db/derby/code/trunk/java/optional/org/apache/derby/optional/lucene/LuceneSupport.java
    db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LuceneCoarseAuthorizationTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LuceneSupportPermsTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml?rev=1585313&r1=1585312&r2=1585313&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml Sun Apr  6 15:23:32 2014
@@ -2370,6 +2370,12 @@ Guide.
                 <arg>directoryName</arg>
             </msg>
 
+	        <msg>
+                <name>42XBJ</name>
+                <text>Cannot create a Lucene index involving a column named '{0}'. Try renaming the column by declaring a view.</text>
+                <arg>columnName</arg>
+            </msg>
+
             <msg>
                 <name>42Y00</name>
                 <text>Class '{0}' does not implement org.apache.derby.iapi.db.AggregateDefinition and thus cannot be used as an aggregate expression.</text>

Modified: db/derby/code/trunk/java/optional/org/apache/derby/optional/lucene/LuceneQueryVTI.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/optional/org/apache/derby/optional/lucene/LuceneQueryVTI.java?rev=1585313&r1=1585312&r2=1585313&view=diff
==============================================================================
--- db/derby/code/trunk/java/optional/org/apache/derby/optional/lucene/LuceneQueryVTI.java (original)
+++ db/derby/code/trunk/java/optional/org/apache/derby/optional/lucene/LuceneQueryVTI.java Sun Apr  6 15:23:32 2014
@@ -86,7 +86,7 @@ public class LuceneQueryVTI extends Stri
     // constructor args
     private Connection  _connection;
     private String  _queryText;
-    private double  _rankCutoff;
+    private float   _scoreCeiling;
 
     private String      _schema;
     private String      _table;
@@ -101,7 +101,7 @@ public class LuceneQueryVTI extends Stri
     private int _minKeyID;
     private int _maxKeyID;
     private int _docIDColumnID;
-    private int _rankColumnID;
+    private int _scoreColumnID;
 	
     /////////////////////////////////////////////////////////////////////
     //
@@ -115,7 +115,7 @@ public class LuceneQueryVTI extends Stri
 	LuceneQueryVTI
         (
          String queryText,
-         double rankCutoff
+         float scoreCeiling
          )
         throws SQLException
     {
@@ -123,7 +123,7 @@ public class LuceneQueryVTI extends Stri
         
         _connection = LuceneSupport.getDefaultConnection();
         _queryText = queryText;
-        _rankCutoff = rankCutoff;
+        _scoreCeiling = scoreCeiling;
 	}
 
     /////////////////////////////////////////////////////////////////////
@@ -136,7 +136,7 @@ public class LuceneQueryVTI extends Stri
 	 * columns:
 	 * 1 ... $_maxKeyID == key columns
 	 * $_maxKeyID + 1 == lucene docId
-	 * $_maxKeyID + 2 == lucene rank
+	 * $_maxKeyID + 2 == lucene score
 	 */
 	public String getRawColumn( int columnid ) throws SQLException
     {
@@ -154,7 +154,7 @@ public class LuceneQueryVTI extends Stri
     public  float   getFloat( int columnid )    throws SQLException
     {
 		try {
-            if ( columnid == _rankColumnID ) { return getScoreDoc().score; }
+            if ( columnid == _scoreColumnID ) { return getScoreDoc().score; }
 			else if ( isKeyID( columnid ) )
             {
                 Number  number = getNumberValue( columnid );
@@ -412,8 +412,8 @@ public class LuceneQueryVTI extends Stri
             for ( int i = 0; i < returnColumns.length; i++ ) { columnNames[ i ] = returnColumns[ i ].columnName; }
             setColumnNames( columnNames );
 
-            _rankColumnID = getColumnCount();
-            _docIDColumnID = _rankColumnID - 1;
+            _scoreColumnID = getColumnCount();
+            _docIDColumnID = _scoreColumnID - 1;
             _maxKeyID = _docIDColumnID - 1;
             _minKeyID = 1;
             
@@ -432,8 +432,8 @@ public class LuceneQueryVTI extends Stri
             QueryParser qp = new QueryParser( LuceneUtils.currentVersion(), TEXT_FIELD_NAME, analyzer );
             Query luceneQuery = qp.parse( _queryText );
             TopScoreDocCollector tsdc = TopScoreDocCollector.create(1000, true);
-            if ( _rankCutoff != 0 ) {
-                tsdc = TopScoreDocCollector.create(1000, new ScoreDoc(0, (float) _rankCutoff ), true);
+            if ( _scoreCeiling != 0 ) {
+                tsdc = TopScoreDocCollector.create(1000, new ScoreDoc(0, _scoreCeiling ), true);
             }
             _searcher.search(luceneQuery, tsdc);
             TopDocs topdocs = tsdc.topDocs();

Modified: db/derby/code/trunk/java/optional/org/apache/derby/optional/lucene/LuceneSupport.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/optional/org/apache/derby/optional/lucene/LuceneSupport.java?rev=1585313&r1=1585312&r2=1585313&view=diff
==============================================================================
--- db/derby/code/trunk/java/optional/org/apache/derby/optional/lucene/LuceneSupport.java (original)
+++ db/derby/code/trunk/java/optional/org/apache/derby/optional/lucene/LuceneSupport.java Sun Apr  6 15:23:32 2014
@@ -95,6 +95,10 @@ public class LuceneSupport implements Op
 
     private static  final   String  LUCENE_DIR = "lucene";
 
+    // names of columns in all query table functions
+    private static  final   String  SCORE = "SCORE";
+    private static  final   String  DOCUMENT_ID = "DOCUMENTID";
+
     // for decomposing a function name into the table and column parts
     static  final   int TABLE_PART = 0;
     static  final   int COLUMN_PART = TABLE_PART + 1;
@@ -296,7 +300,7 @@ public class LuceneSupport implements Op
 	 * Query a Lucene index created by createIndex
 	 * 
 	 * @param queryText a Lucene query, see the Lucene classic queryparser syntax 
-	 * @param rankCutoff Return results only below this rank
+	 * @param scoreCeiling Return results only below this score
 	 * @return A result set in the form of LuceneQueryVTI table
 	 * @throws ParseException
 	 * @throws IOException
@@ -305,11 +309,11 @@ public class LuceneSupport implements Op
 	public static LuceneQueryVTI luceneQuery
         (
          String queryText,
-         double rankCutoff
+         float scoreCeiling
          )
         throws ParseException, IOException, SQLException
     {
-		LuceneQueryVTI lqvti = new LuceneQueryVTI( queryText, rankCutoff );
+		LuceneQueryVTI lqvti = new LuceneQueryVTI( queryText, scoreCeiling );
 		return lqvti;
 	}
 	
@@ -444,8 +448,15 @@ public class LuceneSupport implements Op
         {
             throw newSQLException( SQLState.LUCENE_NO_PRIMARY_KEY );
         }
-        int             keyCount = 0;
 
+        // don't let the user create a table function with duplicate column names
+        vetColumnName( textcol );
+        for ( VTITemplate.ColumnDescriptor key : primaryKeys )
+        {
+            vetColumnName(  key.columnName );
+        }
+        
+        int             keyCount = 0;
         File            propertiesFile = getIndexPropertiesFile( conn, schema, table, textcol );
 
         //
@@ -471,7 +482,7 @@ public class LuceneSupport implements Op
             
         StringBuilder   tableFunction = new StringBuilder();
         tableFunction.append( "create function " + makeTableFunctionName( schema, table, textcol ) + "\n" );
-        tableFunction.append( "( query varchar( 32672 ), rankCutoff double )\n" );
+        tableFunction.append( "( query varchar( 32672 ), scoreCeiling real )\n" );
         tableFunction.append( "returns table\n(" );
 
         PreparedStatement   ps = null;
@@ -495,8 +506,8 @@ public class LuceneSupport implements Op
                 tableFunction.append( "\n\t" + keyName + " " + keyType );
                 keyCount++;
             }
-            tableFunction.append(",\n\tdocumentID int");
-            tableFunction.append(",\n\trank real");
+            tableFunction.append(",\n\t" + DOCUMENT_ID + " int");
+            tableFunction.append(",\n\t" + SCORE + " real");
             tableFunction.append( "\n)\nlanguage java parameter style derby_jdbc_result_set contains sql\n" );
             tableFunction.append( "external name '" + LuceneSupport.class.getName() + ".luceneQuery'" );
 
@@ -1000,6 +1011,25 @@ public class LuceneSupport implements Op
     /////////////////////////////////////////////////////////////////////
 
     /**
+     * A Lucene query table function already has system-supplied columns
+     * named documentID and score. These can't be the names of the key
+     * or text columns supplied by the user.
+     */
+    private static  void    vetColumnName( String columnName )
+        throws SQLException
+    {
+        String  derbyColumnName = derbyIdentifier( columnName );
+
+        if (
+            DOCUMENT_ID.equals( derbyColumnName ) ||
+            SCORE.equals( derbyColumnName )
+            )
+        {
+            throw newSQLException( SQLState.LUCENE_BAD_COLUMN_NAME, derbyColumnName );
+        }
+    }
+
+    /**
      * Return the qualified name of the table.
      */
 	static String   makeTableName( String schema, String table )
@@ -1431,7 +1461,7 @@ public class LuceneSupport implements Op
         keyArray.toArray( temp );
         Arrays.sort( temp );
 
-        // remove the last two columns, which are not keys. they are the DOCUMENT_ID and RANK columns.
+        // remove the last two columns, which are not keys. they are the DOCUMENTID and SCORE columns.
         int     count = temp.length - 2;
         VTITemplate.ColumnDescriptor[] result = new VTITemplate.ColumnDescriptor[ count ];
         for ( int i = 0; i < count; i++ ) { result[ i ] = temp[ i ]; }

Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java?rev=1585313&r1=1585312&r2=1585313&view=diff
==============================================================================
--- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java (original)
+++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java Sun Apr  6 15:23:32 2014
@@ -1183,6 +1183,7 @@ public interface SQLState {
     String LUCENE_ALREADY_LOADED                                  = "42XBG";
     String LUCENE_ALREADY_UNLOADED                                  = "42XBH";
     String LUCENE_BAD_INDEX                                               = "42XBI";
+    String LUCENE_BAD_COLUMN_NAME                                   = "42XBJ";
     
 	// org.apache.derby.impl.sql.execute.rts
 	String RTS_ATTACHED_TO											   = "43X00.U";

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LuceneCoarseAuthorizationTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LuceneCoarseAuthorizationTest.java?rev=1585313&r1=1585312&r2=1585313&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LuceneCoarseAuthorizationTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LuceneCoarseAuthorizationTest.java Sun Apr  6 15:23:32 2014
@@ -169,10 +169,10 @@ public class LuceneCoarseAuthorizationTe
         goodStatement( readWriteConnection, INDEX_POEMS );
 
         String  readPoemsIndex =
-            "select p.originalAuthor, i.rank\n" +
+            "select p.originalAuthor, i.score\n" +
             "from ruth.poems p, table ( ruth.poems__poemText( 'star', 0 ) ) i\n" +
             "where p.poemID = i.poemID and p.versionStamp = i.versionStamp\n" +
-            "order by i.rank desc\n";
+            "order by i.score desc\n";
         String[][]  defaultPoemResults =
             new String[][]
             {

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LuceneSupportPermsTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LuceneSupportPermsTest.java?rev=1585313&r1=1585312&r2=1585313&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LuceneSupportPermsTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LuceneSupportPermsTest.java Sun Apr  6 15:23:32 2014
@@ -83,6 +83,7 @@ public class LuceneSupportPermsTest exte
 	private static  final   String      DOUBLE_LOAD_ILLEGAL         = "42XBG";
 	private static  final   String      DOUBLE_UNLOAD_ILLEGAL       = "42XBH";
 	private static  final   String      BAD_DIRECTORY                      = "42XBI";
+	private static  final   String      BAD_COLUMN_NAME                 = "42XBJ";
 
     private static  final   String      POLICY_FILE = "org/apache/derbyTesting/functionTests/tests/lang/luceneSupport.policy";
 
@@ -441,10 +442,10 @@ public class LuceneSupportPermsTest exte
         assertResults
             (
              ruthConnection,
-             "select p.originalAuthor, i.rank\n" +
+             "select p.originalAuthor, i.score\n" +
              "from ruth.poems p, table ( ruth.poems__poemText( 'star', 0 ) ) i\n" +
              "where p.poemID = i.poemID and p.versionStamp = i.versionStamp\n" +
-             "order by i.rank desc\n",
+             "order by i.score desc\n",
              new String[][]
              {
                  { "Walt Whitman", "0.26756266" },
@@ -523,10 +524,10 @@ public class LuceneSupportPermsTest exte
         
 
         String  query =
-            "select p.originalAuthor, i.rank\n" +
+            "select p.originalAuthor, i.score\n" +
             "from ruth.poems p, table ( ruth.poems__poemText( 'star', 0 ) ) i\n" +
             "where p.poemID = i.poemID and p.versionStamp = i.versionStamp\n" +
-            "order by i.rank desc\n";
+            "order by i.score desc\n";
 
         assertResults
             (
@@ -631,10 +632,10 @@ public class LuceneSupportPermsTest exte
 
         // vet index contents
         String  selectFromViewIndex =
-            "select p.originalAuthor, i.rank\n" +
+            "select p.originalAuthor, i.score\n" +
             "from ruth.poems p, table ( ruth.poemView__poemText( 'star', 0 ) ) i\n" +
             "where p.poemID = i.poemID and p.versionStamp = i.versionStamp\n" +
-            "order by i.rank desc\n";
+            "order by i.score desc\n";
         assertResults
             (
              ruthConnection,
@@ -738,7 +739,7 @@ public class LuceneSupportPermsTest exte
              ruthConnection,
              "select *\n" +
              "from table ( ruth.poems__poemText( 'star', 0 ) ) i\n" +
-             "order by i.rank desc\n",
+             "order by i.score desc\n",
              new String[][]
              {
                  { "5", "4", "0.26756266" },
@@ -754,7 +755,53 @@ public class LuceneSupportPermsTest exte
         dropSchema( ruthConnection );
     }
 
+    /**
+     * <p>
+     * Test that you can't create an index involving a column with the same name
+     * as one of the system-supplied column names (documentID and score).
+     * </p>
+     */
+    public  void    test_008_columnNames()
+        throws Exception
+    {
+        Connection  dboConnection = openUserConnection( TEST_DBO );
+        Connection  ruthConnection = openUserConnection( RUTH );
+
+        goodStatement( dboConnection, LOAD_TOOL );
+
+        goodStatement( ruthConnection, "create table badTable1( keyCol int primary key, score clob )" );
+        goodStatement( ruthConnection, "create table badTable2( keyCol int primary key, documentID clob )" );
+        goodStatement( ruthConnection, "create table badTable3( score int primary key, textCol clob )" );
+        goodStatement( ruthConnection, "create table badTable4( documentID int primary key, textCol clob )" );
 
+        expectExecutionError
+            (
+             ruthConnection, BAD_COLUMN_NAME,
+             "call LuceneSupport.createIndex( 'ruth', 'badTable1', 'score', null )"
+             );
+        expectExecutionError
+            (
+             ruthConnection, BAD_COLUMN_NAME,
+             "call LuceneSupport.createIndex( 'ruth', 'badTable2', 'documentID', null )"
+             );
+        expectExecutionError
+            (
+             ruthConnection, BAD_COLUMN_NAME,
+             "call LuceneSupport.createIndex( 'ruth', 'badTable3', 'textCol', null )"
+             );
+        expectExecutionError
+            (
+             ruthConnection, BAD_COLUMN_NAME,
+             "call LuceneSupport.createIndex( 'ruth', 'badTable4', 'textCol', null )"
+             );
+
+        goodStatement( dboConnection, UNLOAD_TOOL );
+        goodStatement( ruthConnection, "drop table badTable1" );
+        goodStatement( ruthConnection, "drop table badTable2" );
+        goodStatement( ruthConnection, "drop table badTable3" );
+        goodStatement( ruthConnection, "drop table badTable4" );
+    }
+    
     ///////////////////////////////////////////////////////////////////////////////////
     //
     // MINIONS