Repository: phoenix
Updated Branches:
  refs/heads/master dbda9b702 -> a56f78ba6


PHOENIX-1028 Prevent declaration of non PK columns as NOT NULL (Ravi)


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/a56f78ba
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/a56f78ba
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/a56f78ba

Branch: refs/heads/master
Commit: a56f78ba68a257b91a42ba0d9b24fcfe1d204c2b
Parents: dbda9b7
Author: James Taylor <jtay...@salesforce.com>
Authored: Sun Jun 8 21:37:35 2014 -0700
Committer: James Taylor <jtay...@salesforce.com>
Committed: Sun Jun 8 21:37:35 2014 -0700

----------------------------------------------------------------------
 .../apache/phoenix/end2end/CreateTableIT.java   | 31 ++++++++++++++++++++
 .../phoenix/exception/SQLExceptionCode.java     |  1 +
 .../apache/phoenix/query/QueryConstants.java    |  8 ++---
 .../apache/phoenix/schema/MetaDataClient.java   |  8 +++++
 .../phoenix/compile/QueryCompilerTest.java      |  2 +-
 .../phoenix/index/IndexMaintainerTest.java      |  4 +--
 6 files changed, 47 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/a56f78ba/phoenix-core/src/it/java/org/apache/phoenix/end2end/CreateTableIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/CreateTableIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/CreateTableIT.java
index 96b4a8e..e28273e 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/CreateTableIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/CreateTableIT.java
@@ -32,6 +32,7 @@ import java.util.Properties;
 import org.apache.hadoop.hbase.HColumnDescriptor;
 import org.apache.hadoop.hbase.client.HBaseAdmin;
 import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.exception.SQLExceptionCode;
 import org.apache.phoenix.jdbc.PhoenixStatement;
 import org.apache.phoenix.query.KeyRange;
 import org.apache.phoenix.schema.TableAlreadyExistsException;
@@ -318,5 +319,35 @@ public class CreateTableIT extends BaseClientManagedTimeIT 
{
        assertEquals("a", columnFamilies[0].getNameAsString());
        assertEquals(10000, columnFamilies[0].getTimeToLive());
     }
+    
+    
+    /**
+     * Test to ensure that NOT NULL constraint isn't added to a non primary 
key column.
+     * @throws Exception
+     */
+    @Test
+    public void testNotNullConstraintForNonPKColumn() throws Exception {
+        
+        String ddl = "CREATE TABLE IF NOT EXISTS EVENT.APEX_LIMIT ( " +
+                " ORGANIZATION_ID CHAR(15) NOT NULL, " +
+                " EVENT_TIME DATE NOT NULL, USER_ID CHAR(15) NOT NULL, " +
+                " ENTRY_POINT_ID CHAR(15) NOT NULL, ENTRY_POINT_TYPE CHAR(2) 
NOT NULL , " +
+                " APEX_LIMIT_ID CHAR(15) NOT NULL,  USERNAME CHAR(80),  " +
+                " NAMESPACE_PREFIX VARCHAR, ENTRY_POINT_NAME VARCHAR  NOT NULL 
, " +
+                " EXECUTION_UNIT_NO VARCHAR, LIMIT_TYPE VARCHAR, " +
+                " LIMIT_VALUE DOUBLE  " +
+                " CONSTRAINT PK PRIMARY KEY (" + 
+                "     ORGANIZATION_ID, EVENT_TIME,USER_ID,ENTRY_POINT_ID, 
ENTRY_POINT_TYPE, APEX_LIMIT_ID " +
+                " ) ) VERSIONS=1";
+                    
+        Properties props = new Properties();
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        try {
+            conn.createStatement().execute(ddl);    
+            fail(" Non pk column ENTRY_POINT_NAME has a NOT NULL constraint");
+        } catch( SQLException sqle) {
+            
assertEquals(SQLExceptionCode.INVALID_NOT_NULL_CONSTRAINT.getErrorCode(),sqle.getErrorCode());
+        }
+   }
 
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/a56f78ba/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java 
b/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
index d9e23f5..39b951d 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
@@ -138,6 +138,7 @@ public enum SQLExceptionCode {
     }),
     ORDER_BY_ARRAY_NOT_SUPPORTED(515, "42893", "ORDER BY of an array type is 
not allowed"),
     NON_EQUALITY_ARRAY_COMPARISON(516, "42894", "Array types may only be 
compared using = or !="),
+    INVALID_NOT_NULL_CONSTRAINT(517, "42895", "Invalid not null constraint on 
non primary key column"),
     
     /** 
      * HBase and Phoenix specific implementation defined sub-classes.

http://git-wip-us.apache.org/repos/asf/phoenix/blob/a56f78ba/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java 
b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java
index 7a5d4f6..db411ab 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java
@@ -217,10 +217,10 @@ public interface QueryConstants {
             TENANT_ID + " VARCHAR NULL," +
                SEQUENCE_SCHEMA + " VARCHAR NULL, \n" + 
             SEQUENCE_NAME +  " VARCHAR NOT NULL, \n" +
-            START_WITH + " BIGINT NOT NULL, \n" + 
-               CURRENT_VALUE + " BIGINT NOT NULL, \n" + 
-            INCREMENT_BY  + " BIGINT NOT NULL, \n" + 
-            CACHE_SIZE  + " BIGINT NOT NULL \n" + 
+            START_WITH + " BIGINT, \n" + 
+               CURRENT_VALUE + " BIGINT, \n" + 
+            INCREMENT_BY  + " BIGINT, \n" + 
+            CACHE_SIZE  + " BIGINT \n" + 
                " CONSTRAINT " + SYSTEM_TABLE_PK_NAME + " PRIMARY KEY (" + 
TENANT_ID + "," + SEQUENCE_SCHEMA + "," + SEQUENCE_NAME + "))\n" + 
                HConstants.VERSIONS + "=" + 
MetaDataProtocol.DEFAULT_MAX_META_DATA_VERSIONS + "\n";
        

http://git-wip-us.apache.org/repos/asf/phoenix/blob/a56f78ba/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java 
b/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
index 0fee51d..ead223a 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
@@ -941,6 +941,14 @@ public class MetaDataClient {
                     }
                     isPK = true;
                 }
+                
+               // do not allow setting NOT-NULL constraint on non-primary 
columns.
+                if (!isPK && pkConstraint != null && 
!pkConstraint.contains(colDef.getColumnDefName())) {
+                    if(Boolean.FALSE.equals(colDef.isNull())) {
+                        throw new 
SQLExceptionInfo.Builder(SQLExceptionCode.INVALID_NOT_NULL_CONSTRAINT)
+                            
.setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException();
+                    }  
+                }
                 PColumn column = newColumn(position++, colDef, pkConstraint, 
defaultFamilyName, false);
                 if (SchemaUtil.isPKColumn(column)) {
                     // TODO: remove this constraint?

http://git-wip-us.apache.org/repos/asf/phoenix/blob/a56f78ba/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java 
b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
index 47ba25c..2fef055 100644
--- 
a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
+++ 
b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
@@ -208,7 +208,7 @@ public class QueryCompilerTest extends 
BaseConnectionlessQueryTest {
             statement.execute();
             fail();
         } catch (SQLException e) {
-            assertTrue(e.getMessage(), e.getMessage().contains("ERROR 509 
(42888): The table does not have a primary key. tableName=FOO"));
+            assertTrue(e.getMessage(), e.getMessage().contains("ERROR 517 
(42895): Invalid not null constraint on non primary key column columnName=PK"));
         } finally {
             conn.close();
         }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/a56f78ba/phoenix-core/src/test/java/org/apache/phoenix/index/IndexMaintainerTest.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/test/java/org/apache/phoenix/index/IndexMaintainerTest.java 
b/phoenix-core/src/test/java/org/apache/phoenix/index/IndexMaintainerTest.java
index 17d8b3f..e387387 100644
--- 
a/phoenix-core/src/test/java/org/apache/phoenix/index/IndexMaintainerTest.java
+++ 
b/phoenix-core/src/test/java/org/apache/phoenix/index/IndexMaintainerTest.java
@@ -175,7 +175,7 @@ public class IndexMaintainerTest  extends 
BaseConnectionlessQueryTest {
  
    @Test
     public void testSingleKeyValueIndex() throws Exception {
-        testIndexRowKeyBuilding("k1 VARCHAR, k2 INTEGER NOT NULL, v VARCHAR", 
"k1", "v", new Object [] {"a",1,"b"});
+        testIndexRowKeyBuilding("k1 VARCHAR, k2 INTEGER, v VARCHAR", "k1", 
"v", new Object [] {"a",1,"b"});
     }
  
     @Test
@@ -190,7 +190,7 @@ public class IndexMaintainerTest  extends 
BaseConnectionlessQueryTest {
  
     @Test
     public void testSingleKeyValueDescIndex() throws Exception {
-        testIndexRowKeyBuilding("k1 VARCHAR, k2 INTEGER NOT NULL, v VARCHAR", 
"k1", "v DESC", new Object [] {"a",1,"b"});
+        testIndexRowKeyBuilding("k1 VARCHAR, k2 INTEGER, v VARCHAR", "k1", "v 
DESC", new Object [] {"a",1,"b"});
     }
  
     @Test

Reply via email to