phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamestay...@apache.org
Subject [1/2] git commit: PHOENIX-1266 Disallow setting NOT NULL constraint on non PK columns
Date Sun, 21 Sep 2014 04:45:45 GMT
Repository: phoenix
Updated Branches:
  refs/heads/master 1040fd05c -> 2fe9be740


PHOENIX-1266 Disallow setting NOT NULL constraint on non PK columns


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

Branch: refs/heads/master
Commit: 19e333ad726e993028e54b6612303f454f26b673
Parents: 2e9677d
Author: James Taylor <jtaylor@salesforce.com>
Authored: Sat Sep 20 21:50:12 2014 -0700
Committer: James Taylor <jtaylor@salesforce.com>
Committed: Sat Sep 20 21:50:12 2014 -0700

----------------------------------------------------------------------
 .../org/apache/phoenix/end2end/CreateTableIT.java   | 14 ++++++++++++++
 .../phoenix/end2end/TenantSpecificTablesDDLIT.java  |  2 +-
 .../org/apache/phoenix/schema/MetaDataClient.java   | 16 +++++++++-------
 .../java/org/apache/phoenix/schema/PColumnImpl.java |  6 +++++-
 .../apache/phoenix/compile/QueryCompilerTest.java   |  2 +-
 .../java/org/apache/phoenix/query/BaseTest.java     | 16 ++++++++--------
 6 files changed, 38 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/19e333ad/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 e28273e..1f9afc4 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
@@ -350,4 +350,18 @@ public class CreateTableIT extends BaseClientManagedTimeIT {
         }
    }
 
+    @Test
+    public void testNotNullConstraintForWithSinglePKCol() throws Exception {
+        
+        String ddl = "create table test.testing(k integer primary key, v bigint not null)";
+                    
+        Properties props = new Properties();
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        try {
+            conn.createStatement().execute(ddl);    
+            fail(" Non pk column V 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/19e333ad/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificTablesDDLIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificTablesDDLIT.java
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificTablesDDLIT.java
index 655fe44..2d6c30b 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificTablesDDLIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificTablesDDLIT.java
@@ -179,7 +179,7 @@ public class TenantSpecificTablesDDLIT extends BaseTenantSpecificTablesIT
{
     public void testBaseTableWrongFormatWithTenantTypeId() throws Exception {
         // only two PK columns for multi_tenant, multi_type
         try {
-            createTestTable(getUrl(), "CREATE TABLE BASE_TABLE2 (TENANT_ID VARCHAR NOT NULL
PRIMARY KEY, ID VARCHAR NOT NULL, A INTEGER) MULTI_TENANT=true", null, nextTimestamp());
+            createTestTable(getUrl(), "CREATE TABLE BASE_TABLE2 (TENANT_ID VARCHAR NOT NULL
PRIMARY KEY, ID VARCHAR, A INTEGER) MULTI_TENANT=true", null, nextTimestamp());
             fail();
         }
         catch (SQLException expected) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/19e333ad/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 eafac8b..1f933d8 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
@@ -1149,15 +1149,17 @@ public class MetaDataClient {
                             .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException();
                     }
                     isPK = true;
+                } else {
+                    // do not allow setting NOT-NULL constraint on non-primary columns.
+                    if (  Boolean.FALSE.equals(colDef.isNull()) &&
+                        ( isPK || ( pkConstraint != null && !pkConstraint.contains(colDef.getColumnDefName()))))
{
+                            throw new SQLExceptionInfo.Builder(SQLExceptionCode.INVALID_NOT_NULL_CONSTRAINT)
+                                .setSchemaName(schemaName)
+                                .setTableName(tableName)
+                                .setColumnName(colDef.getColumnDefName().getColumnName()).build().buildException();
+                    }
                 }
                 
-               // 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/19e333ad/phoenix-core/src/main/java/org/apache/phoenix/schema/PColumnImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/PColumnImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/PColumnImpl.java
index 40cf067..e0143c3 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/PColumnImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/PColumnImpl.java
@@ -123,7 +123,11 @@ public class PColumnImpl implements PColumn {
 
     @Override
     public boolean isNullable() {
-        return nullable;
+        // Only PK columns can be NOT NULL. We prevent this in the
+        // CREATE TABLE statement now (PHOENIX-1266), but this extra
+        // check for familyName != null will ensure that for existing
+        // tables we never treat key value columns as NOT NULL.
+        return nullable || familyName != null;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/19e333ad/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 07564d4..cdc1805 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 517 (42895): Invalid
not null constraint on non primary key column columnName=PK"));
+            assertTrue(e.getMessage(), e.getMessage().contains("ERROR 517 (42895): Invalid
not null constraint on non primary key column columnName=FOO.PK"));
         } finally {
             conn.close();
         }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/19e333ad/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java b/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java
index 37285f6..45dbb39 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java
@@ -406,21 +406,21 @@ public abstract class BaseTest {
                 "   (id varchar not null primary key, d DOUBLE, f FLOAT, ud UNSIGNED_DOUBLE,
uf UNSIGNED_FLOAT, i integer, de decimal)");
         builder.put(JOIN_ORDER_TABLE_FULL_NAME, "create table " + JOIN_ORDER_TABLE_FULL_NAME
+
                 "   (\"order_id\" char(15) not null primary key, " +
-                "    \"customer_id\" char(10) not null, " +
-                "    \"item_id\" char(10) not null, " +
-                "    price integer not null, " +
-                "    quantity integer not null, " +
-                "    date timestamp not null)");
+                "    \"customer_id\" char(10), " +
+                "    \"item_id\" char(10), " +
+                "    price integer, " +
+                "    quantity integer, " +
+                "    date timestamp)");
         builder.put(JOIN_CUSTOMER_TABLE_FULL_NAME, "create table " + JOIN_CUSTOMER_TABLE_FULL_NAME
+
                 "   (\"customer_id\" char(10) not null primary key, " +
-                "    name varchar not null, " +
+                "    name varchar, " +
                 "    phone char(12), " +
                 "    address varchar, " +
                 "    loc_id char(5), " +
                 "    date date)");
         builder.put(JOIN_ITEM_TABLE_FULL_NAME, "create table " + JOIN_ITEM_TABLE_FULL_NAME
+
                 "   (\"item_id\" char(10) not null primary key, " +
-                "    name varchar not null, " +
+                "    name varchar, " +
                 "    price integer, " +
                 "    discount1 integer, " +
                 "    discount2 integer, " +
@@ -428,7 +428,7 @@ public abstract class BaseTest {
                 "    description varchar)");
         builder.put(JOIN_SUPPLIER_TABLE_FULL_NAME, "create table " + JOIN_SUPPLIER_TABLE_FULL_NAME
+
                 "   (\"supplier_id\" char(10) not null primary key, " +
-                "    name varchar not null, " +
+                "    name varchar, " +
                 "    phone char(12), " +
                 "    address varchar, " +
                 "    loc_id char(5))");


Mime
View raw message