You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ja...@apache.org on 2015/11/03 21:17:01 UTC

phoenix git commit: PHOENIX-2362 Rollback doesn't work when checkpointing is done

Repository: phoenix
Updated Branches:
  refs/heads/txn 62ce5dd98 -> bb7c74df8


PHOENIX-2362 Rollback doesn't work when checkpointing is done


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

Branch: refs/heads/txn
Commit: bb7c74df8164dc986aee2000aaddd5baf1da8edd
Parents: 62ce5dd
Author: James Taylor <jt...@salesforce.com>
Authored: Tue Nov 3 08:23:39 2015 -0800
Committer: James Taylor <jt...@salesforce.com>
Committed: Tue Nov 3 08:23:39 2015 -0800

----------------------------------------------------------------------
 .../end2end/index/txn/MutableRollbackIT.java    | 292 +++++++++++++++++--
 .../org/apache/phoenix/tx/TransactionIT.java    |  61 ++++
 .../org/apache/phoenix/tx/TxCheckpointIT.java   |  40 ++-
 .../phoenix/exception/SQLExceptionCode.java     |   1 +
 .../index/PhoenixTransactionalIndexer.java      | 240 +++++++++------
 .../query/ConnectionQueryServicesImpl.java      | 155 ++++++++--
 .../org/apache/phoenix/query/QueryServices.java |   1 +
 .../phoenix/query/QueryServicesOptions.java     |   1 +
 .../apache/phoenix/schema/MetaDataClient.java   |  30 +-
 .../org/apache/phoenix/util/SchemaUtil.java     |  10 +-
 10 files changed, 680 insertions(+), 151 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/bb7c74df/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/txn/MutableRollbackIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/txn/MutableRollbackIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/txn/MutableRollbackIT.java
index 4131c2d..6c0486c 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/txn/MutableRollbackIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/txn/MutableRollbackIT.java
@@ -5,17 +5,25 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import java.io.IOException;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Properties;
 
+import org.apache.hadoop.hbase.client.HTableInterface;
+import org.apache.hadoop.hbase.client.Result;
+import org.apache.hadoop.hbase.client.ResultScanner;
+import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.end2end.BaseHBaseManagedTimeIT;
 import org.apache.phoenix.end2end.Shadower;
+import org.apache.phoenix.jdbc.PhoenixConnection;
 import org.apache.phoenix.query.QueryServices;
 import org.apache.phoenix.util.PropertiesUtil;
 import org.apache.phoenix.util.ReadOnlyProps;
@@ -70,7 +78,7 @@ public class MutableRollbackIT extends BaseHBaseManagedTimeIT {
             conn.commit();
             
             //assert rows exists in DEMO1
-            ResultSet rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
+            ResultSet rs = stmt.executeQuery("select /*+ NO_INDEX */ k, v1, v2 from DEMO1");
             assertTrue(rs.next());
             assertEquals("x", rs.getString(1));
             assertEquals("y", rs.getString(2));
@@ -86,7 +94,7 @@ public class MutableRollbackIT extends BaseHBaseManagedTimeIT {
             assertFalse(rs.next());
             
             //assert no rows exists in DEMO2
-            rs = stmt.executeQuery("select k, v1, v2 from DEMO2");
+            rs = stmt.executeQuery("select /*+ NO_INDEX */ k, v1, v2 from DEMO2");
             assertFalse(rs.next());
             
             //assert no rows exists in DEMO2_idx
@@ -97,7 +105,7 @@ public class MutableRollbackIT extends BaseHBaseManagedTimeIT {
             stmt.executeUpdate("upsert into DEMO2 values('a', 'b', 'c')");
             
             //assert new covered column value 
-            rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
+            rs = stmt.executeQuery("select /*+ NO_INDEX */ k, v1, v2 from DEMO1");
             assertTrue(rs.next());
             assertEquals("x", rs.getString(1));
             assertEquals("y", rs.getString(2));
@@ -113,7 +121,7 @@ public class MutableRollbackIT extends BaseHBaseManagedTimeIT {
             assertFalse(rs.next());
             
             //assert rows exists in DEMO2
-            rs = stmt.executeQuery("select k, v1, v2 from DEMO2");
+            rs = stmt.executeQuery("select /*+ NO_INDEX */ k, v1, v2 from DEMO2");
             assertTrue(rs.next());
             assertEquals("a", rs.getString(1));
             assertEquals("b", rs.getString(2));
@@ -130,7 +138,7 @@ public class MutableRollbackIT extends BaseHBaseManagedTimeIT {
             conn.rollback();
             
             //assert original row exists in DEMO1
-            rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
+            rs = stmt.executeQuery("select /*+ NO_INDEX */ k, v1, v2 from DEMO1");
             assertTrue(rs.next());
             assertEquals("x", rs.getString(1));
             assertEquals("y", rs.getString(2));
@@ -146,12 +154,37 @@ public class MutableRollbackIT extends BaseHBaseManagedTimeIT {
             assertFalse(rs.next());
             
             //assert no rows exists in DEMO2
-            rs = stmt.executeQuery("select k, v1, v2 from DEMO2");
+            rs = stmt.executeQuery("select /*+ NO_INDEX */ k, v1, v2 from DEMO2");
             assertFalse(rs.next());
             
             //assert no rows exists in DEMO2_idx
             rs = stmt.executeQuery("select k, v1 from DEMO2 ORDER BY v1");
             assertFalse(rs.next());
+            
+            stmt.executeUpdate("upsert into DEMO1 values('x', 'z', 'a')");
+            stmt.executeUpdate("upsert into DEMO2 values('a', 'b', 'c')");
+            conn.commit();
+
+            assertDataAndIndexRows(stmt);
+            stmt.executeUpdate("delete from DEMO1 where  k='x'");
+            stmt.executeUpdate("delete from DEMO2 where  v1='b'");
+            
+            //assert no rows exists in DEMO1
+            rs = stmt.executeQuery("select /*+ NO_INDEX */ k, v1, v2 from DEMO1");
+            assertFalse(rs.next());
+            //assert no rows exists in DEMO1_idx
+            rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
+            assertFalse(rs.next());
+
+            //assert no rows exists in DEMO2
+            rs = stmt.executeQuery("select /*+ NO_INDEX */ k, v1, v2 from DEMO2");
+            assertFalse(rs.next());
+            //assert no rows exists in DEMO2_idx
+            rs = stmt.executeQuery("select k, v1 from DEMO2 ORDER BY v1");
+            assertFalse(rs.next());
+            
+            conn.rollback();
+            assertDataAndIndexRows(stmt);
         } finally {
             conn.close();
         }
@@ -198,34 +231,152 @@ public class MutableRollbackIT extends BaseHBaseManagedTimeIT {
             stmt.executeUpdate("upsert into DEMO1 values('x', 'z', 'a')");
             stmt.executeUpdate("upsert into DEMO2 values('a', 'b', 'c')");
             
-            //assert new covered row key value exists in DEMO1
+            assertDataAndIndexRows(stmt);
+            
+            conn.rollback();
+            
+            //assert original row exists in DEMO1
             rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
             assertTrue(rs.next());
             assertEquals("x", rs.getString(1));
-            assertEquals("z", rs.getString(2));
+            assertEquals("y", rs.getString(2));
             assertEquals("a", rs.getString(3));
             assertFalse(rs.next());
             
-            //assert new covered row key value exists in DEMO1_idx
+            //assert original row exists in DEMO1_idx
             rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
             assertTrue(rs.next());
             assertEquals("x", rs.getString(1));
-            assertEquals("z", rs.getString(2));
+            assertEquals("y", rs.getString(2));
             assertFalse(rs.next());
             
-            //assert rows exists in DEMO2
+            //assert no rows exists in DEMO2
             rs = stmt.executeQuery("select k, v1, v2 from DEMO2");
-            assertTrue(rs.next());
-            assertEquals("a", rs.getString(1));
-            assertEquals("b", rs.getString(2));
-            assertEquals("c", rs.getString(3));
             assertFalse(rs.next());
             
-            //assert rows exists in DEMO2 index table
+            //assert no rows exists in DEMO2_idx
             rs = stmt.executeQuery("select k, v1 from DEMO2 ORDER BY v1");
+            assertFalse(rs.next());
+            
+            stmt.executeUpdate("upsert into DEMO1 values('x', 'z', 'a')");
+            stmt.executeUpdate("upsert into DEMO2 values('a', 'b', 'c')");
+            conn.commit();
+
+            assertDataAndIndexRows(stmt);
+            stmt.executeUpdate("delete from DEMO1 where  k='x'");
+            stmt.executeUpdate("delete from DEMO2 where  v1='b'");
+            
+            //assert no rows exists in DEMO1
+            rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
+            assertFalse(rs.next());
+            //assert no rows exists in DEMO1_idx
+            rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
+            assertFalse(rs.next());
+
+            //assert no rows exists in DEMO2
+            rs = stmt.executeQuery("select k, v1, v2 from DEMO2");
+            assertFalse(rs.next());
+            //assert no rows exists in DEMO2_idx
+            rs = stmt.executeQuery("select k, v1 from DEMO2 ORDER BY v1");
+            assertFalse(rs.next());
+            
+            conn.rollback();
+            assertDataAndIndexRows(stmt);
+
+        } finally {
+            conn.close();
+        }
+    }
+
+	protected static void printRawTable(Connection conn, String tableName) throws SQLException, IOException {
+	    HTableInterface htable = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(Bytes.toBytes(tableName));
+	    Scan scan = new Scan();
+	    scan.setRaw(true);;
+	    scan.setMaxVersions();
+	    ResultScanner scanner = htable.getScanner(scan);
+	    Result r = null;
+	    System.out.println("**************** " + tableName);
+	    while ((r = scanner.next()) != null) {
+	        System.out.println("    **********" + r);
+	    }
+	}
+	
+    private static void assertDataAndIndexRows(Statement stmt) throws SQLException, IOException {
+        ResultSet rs;
+        //assert new covered row key value exists in DEMO1
+        rs = stmt.executeQuery("select /*+ NO_INDEX */ k, v1, v2 from DEMO1");
+        assertTrue(rs.next());
+        assertEquals("x", rs.getString(1));
+        assertEquals("z", rs.getString(2));
+        assertEquals("a", rs.getString(3));
+        assertFalse(rs.next());
+        
+        //assert new covered row key value exists in DEMO1_idx
+        rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
+        assertTrue(rs.next());
+        assertEquals("x", rs.getString(1));
+        assertEquals("z", rs.getString(2));
+        assertFalse(rs.next());
+        
+        //assert rows exists in DEMO2
+        rs = stmt.executeQuery("select /*+ NO_INDEX */ k, v1, v2 from DEMO2");
+        assertTrue(rs.next());
+        assertEquals("a", rs.getString(1));
+        assertEquals("b", rs.getString(2));
+        assertEquals("c", rs.getString(3));
+        assertFalse(rs.next());
+        
+        //assert rows exists in DEMO2 index table
+        rs = stmt.executeQuery("select k, v1 from DEMO2 ORDER BY v1");
+        assertTrue(rs.next());
+        assertEquals("a", rs.getString(1));
+        assertEquals("b", rs.getString(2));
+        assertFalse(rs.next());
+    }
+    
+    @Test
+    public void testMultiRollbackOfUncommittedExistingRowKeyIndexUpdate() throws Exception {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        conn.setAutoCommit(false);
+        try {
+            Statement stmt = conn.createStatement();
+            stmt.execute("CREATE TABLE DEMO1(k VARCHAR PRIMARY KEY, v1 VARCHAR, v2 VARCHAR)");
+            stmt.execute("CREATE "+(localIndex? " LOCAL " : "")+"INDEX DEMO1_idx ON DEMO1 (v1, k)");
+            
+            stmt.executeUpdate("upsert into DEMO1 values('x', 'yyyy', 'a')");
+            conn.commit();
+            
+            //assert rows exists in DEMO1 
+            ResultSet rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
             assertTrue(rs.next());
-            assertEquals("a", rs.getString(1));
-            assertEquals("b", rs.getString(2));
+            assertEquals("x", rs.getString(1));
+            assertEquals("yyyy", rs.getString(2));
+            assertEquals("a", rs.getString(3));
+            assertFalse(rs.next());
+            
+            //assert rows exists in DEMO1_idx
+            rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("yyyy", rs.getString(2));
+            assertFalse(rs.next());
+            
+            stmt.executeUpdate("upsert into DEMO1 values('x', 'zzz', 'a')");
+            
+            //assert new covered row key value exists in DEMO1
+            rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("zzz", rs.getString(2));
+            assertEquals("a", rs.getString(3));
+            assertFalse(rs.next());
+            
+            //assert new covered row key value exists in DEMO1_idx
+            rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("zzz", rs.getString(2));
             assertFalse(rs.next());
             
             conn.rollback();
@@ -234,7 +385,7 @@ public class MutableRollbackIT extends BaseHBaseManagedTimeIT {
             rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
             assertTrue(rs.next());
             assertEquals("x", rs.getString(1));
-            assertEquals("y", rs.getString(2));
+            assertEquals("yyyy", rs.getString(2));
             assertEquals("a", rs.getString(3));
             assertFalse(rs.next());
             
@@ -242,19 +393,112 @@ public class MutableRollbackIT extends BaseHBaseManagedTimeIT {
             rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
             assertTrue(rs.next());
             assertEquals("x", rs.getString(1));
-            assertEquals("y", rs.getString(2));
+            assertEquals("yyyy", rs.getString(2));
             assertFalse(rs.next());
             
-            //assert no rows exists in DEMO2
-            rs = stmt.executeQuery("select k, v1, v2 from DEMO2");
+            stmt.executeUpdate("upsert into DEMO1 values('x', 'zz', 'a')");
+            
+            //assert new covered row key value exists in DEMO1
+            rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("zz", rs.getString(2));
+            assertEquals("a", rs.getString(3));
             assertFalse(rs.next());
             
-            //assert no rows exists in DEMO2_idx
-            rs = stmt.executeQuery("select k, v1 from DEMO2 ORDER BY v1");
+            //assert new covered row key value exists in DEMO1_idx
+            rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("zz", rs.getString(2));
+            assertFalse(rs.next());
+            
+            conn.rollback();
+            
+            //assert original row exists in DEMO1
+            rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("yyyy", rs.getString(2));
+            assertEquals("a", rs.getString(3));
             assertFalse(rs.next());
+            
+            //assert original row exists in DEMO1_idx
+            rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("yyyy", rs.getString(2));
+            assertFalse(rs.next());
+                        
         } finally {
             conn.close();
         }
     }
     
+    @Test
+    public void testCheckpointAndRollback() throws Exception {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        conn.setAutoCommit(false);
+        try {
+            Statement stmt = conn.createStatement();
+            stmt.execute("CREATE TABLE DEMO1(k VARCHAR PRIMARY KEY, v1 VARCHAR, v2 VARCHAR)");
+            stmt.execute("CREATE "+(localIndex? " LOCAL " : "")+"INDEX DEMO1_idx ON DEMO1 (v1)");
+            
+            stmt.executeUpdate("upsert into DEMO1 values('x', 'a', 'a')");
+            conn.commit();
+            
+            ResultSet rs;
+            
+            stmt.executeUpdate("upsert into DEMO1(k,v1) SELECT k,v1||'a' FROM DEMO1");
+            
+            rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("aa", rs.getString(2));
+            assertEquals("a", rs.getString(3));
+            assertFalse(rs.next());
+            
+            rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("aa", rs.getString(2));
+            assertFalse(rs.next());
+            
+            stmt.executeUpdate("upsert into DEMO1(k,v1) SELECT k,v1||'a' FROM DEMO1");
+            
+            rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("aaa", rs.getString(2));
+            assertEquals("a", rs.getString(3));
+            assertFalse(rs.next());
+            
+            rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("aaa", rs.getString(2));
+            assertFalse(rs.next());
+            
+            conn.rollback();
+            
+            //assert original row exists in DEMO1
+            rs = stmt.executeQuery("select k, v1, v2 from DEMO1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("a", rs.getString(2));
+            assertEquals("a", rs.getString(3));
+            assertFalse(rs.next());
+            
+            //assert original row exists in DEMO1_idx
+            rs = stmt.executeQuery("select k, v1 from DEMO1 ORDER BY v1");
+            assertTrue(rs.next());
+            assertEquals("x", rs.getString(1));
+            assertEquals("a", rs.getString(2));
+            assertFalse(rs.next());
+
+        } finally {
+            conn.close();
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/bb7c74df/phoenix-core/src/it/java/org/apache/phoenix/tx/TransactionIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/tx/TransactionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/tx/TransactionIT.java
index 71d0790..fc73534 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/tx/TransactionIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/tx/TransactionIT.java
@@ -23,6 +23,8 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.List;
 
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HTableDescriptor;
 import org.apache.hadoop.hbase.client.HBaseAdmin;
 import org.apache.hadoop.hbase.client.HTableInterface;
 import org.apache.hadoop.hbase.client.Put;
@@ -32,6 +34,7 @@ import org.apache.phoenix.exception.SQLExceptionCode;
 import org.apache.phoenix.jdbc.PhoenixConnection;
 import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData;
 import org.apache.phoenix.query.QueryConstants;
+import org.apache.phoenix.query.QueryServicesOptions;
 import org.apache.phoenix.schema.PTableKey;
 import org.apache.phoenix.schema.types.PInteger;
 import org.apache.phoenix.util.ByteUtil;
@@ -341,6 +344,64 @@ public class TransactionIT extends BaseHBaseManagedTimeIT {
         
         htable = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(Bytes.toBytes("SYSTEM.NON_TX_TABLE"));
         assertFalse(htable.getTableDescriptor().getCoprocessors().contains(TransactionProcessor.class.getName()));
+        assertEquals(1,conn.unwrap(PhoenixConnection.class).getQueryServices().
+                getTableDescriptor(Bytes.toBytes("SYSTEM.NON_TX_TABLE")).
+                getFamily(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES).getMaxVersions());
+    }
+    
+    @Test
+    public void testMaxVersions() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        conn.createStatement().execute("CREATE TABLE NON_TX_TABLE1(k INTEGER PRIMARY KEY, a.v VARCHAR, b.v VARCHAR, c.v VARCHAR)");
+        conn.createStatement().execute("CREATE INDEX idx1 ON NON_TX_TABLE1(a.v, b.v)");
+        conn.createStatement().execute("CREATE INDEX idx2 ON NON_TX_TABLE1(c.v) INCLUDE (a.v, b.v)");
+
+        conn.createStatement().execute("ALTER TABLE NON_TX_TABLE1 SET TRANSACTIONAL=true");
+
+        HTableDescriptor desc = conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(Bytes.toBytes("NON_TX_TABLE1"));
+        for (HColumnDescriptor colDesc : desc.getFamilies()) {
+            assertEquals(QueryServicesOptions.DEFAULT_MAX_VERSIONS_TRANSACTIONAL, colDesc.getMaxVersions());
+        }
+
+        desc = conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(Bytes.toBytes("IDX1"));
+        for (HColumnDescriptor colDesc : desc.getFamilies()) {
+            assertEquals(QueryServicesOptions.DEFAULT_MAX_VERSIONS_TRANSACTIONAL, colDesc.getMaxVersions());
+        }
+        
+        desc = conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(Bytes.toBytes("IDX2"));
+        for (HColumnDescriptor colDesc : desc.getFamilies()) {
+            assertEquals(QueryServicesOptions.DEFAULT_MAX_VERSIONS_TRANSACTIONAL, colDesc.getMaxVersions());
+        }
+        
+        conn.createStatement().execute("CREATE TABLE NON_TX_TABLE2(k INTEGER PRIMARY KEY, a.v VARCHAR, b.v VARCHAR, c.v VARCHAR)");
+        conn.createStatement().execute("ALTER TABLE NON_TX_TABLE2 SET TRANSACTIONAL=true, VERSIONS=10");
+        desc = conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(Bytes.toBytes("NON_TX_TABLE2"));
+        for (HColumnDescriptor colDesc : desc.getFamilies()) {
+            assertEquals(10, colDesc.getMaxVersions());
+        }
 
+        conn.createStatement().execute("CREATE TABLE NON_TX_TABLE3(k INTEGER PRIMARY KEY, a.v VARCHAR, b.v VARCHAR, c.v VARCHAR)");
+        conn.createStatement().execute("ALTER TABLE NON_TX_TABLE3 SET TRANSACTIONAL=true, b.VERSIONS=10, c.VERSIONS=20");
+        desc = conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(Bytes.toBytes("NON_TX_TABLE3"));
+        assertEquals(QueryServicesOptions.DEFAULT_MAX_VERSIONS_TRANSACTIONAL, desc.getFamily(Bytes.toBytes("A")).getMaxVersions());
+        assertEquals(10, desc.getFamily(Bytes.toBytes("B")).getMaxVersions());
+        assertEquals(20, desc.getFamily(Bytes.toBytes("C")).getMaxVersions());
+
+        conn.createStatement().execute("CREATE TABLE NON_TX_TABLE4(k INTEGER PRIMARY KEY, a.v VARCHAR, b.v VARCHAR, c.v VARCHAR)");
+        try {
+            conn.createStatement().execute("ALTER TABLE NON_TX_TABLE4 SET TRANSACTIONAL=true, VERSIONS=1");
+            fail();
+        } catch (SQLException e) {
+            assertEquals(SQLExceptionCode.TX_MAX_VERSIONS_MUST_BE_GREATER_THAN_ONE.getErrorCode(), e.getErrorCode());
+        }
+
+        try {
+            conn.createStatement().execute("ALTER TABLE NON_TX_TABLE4 SET TRANSACTIONAL=true, b.VERSIONS=1");
+            fail();
+        } catch (SQLException e) {
+            assertEquals(SQLExceptionCode.TX_MAX_VERSIONS_MUST_BE_GREATER_THAN_ONE.getErrorCode(), e.getErrorCode());
+        }
     }
+        
+
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/bb7c74df/phoenix-core/src/it/java/org/apache/phoenix/tx/TxCheckpointIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/tx/TxCheckpointIT.java b/phoenix-core/src/it/java/org/apache/phoenix/tx/TxCheckpointIT.java
index 63d4851..a493518 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/tx/TxCheckpointIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/tx/TxCheckpointIT.java
@@ -33,6 +33,8 @@ import java.util.Collection;
 import java.util.Map;
 import java.util.Properties;
 
+import co.cask.tephra.Transaction.VisibilityLevel;
+
 import org.apache.phoenix.end2end.BaseHBaseManagedTimeIT;
 import org.apache.phoenix.end2end.Shadower;
 import org.apache.phoenix.execute.MutationState;
@@ -48,8 +50,6 @@ import org.junit.runners.Parameterized.Parameters;
 
 import com.google.common.collect.Maps;
 
-import co.cask.tephra.Transaction.VisibilityLevel;
-
 @RunWith(Parameterized.class)
 public class TxCheckpointIT extends BaseHBaseManagedTimeIT {
 	
@@ -274,7 +274,7 @@ public class TxCheckpointIT extends BaseHBaseManagedTimeIT {
 	}
 	
 	@Test
-    public void testCheckpointForDelete() throws Exception {
+    public void testCheckpointForDeleteAndUpsert() throws Exception {
 		Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
 		ResultSet rs;
 		try (Connection conn = DriverManager.getConnection(getUrl(), props);) {
@@ -308,7 +308,7 @@ public class TxCheckpointIT extends BaseHBaseManagedTimeIT {
 	        assertFalse(rs.next());
 	        
 	        rs = conn.createStatement().executeQuery("select /*+ INDEX(DEMO IDX) */ id1 from DEMO1");
-	        assertTrue(rs.next());
+            assertTrue(rs.next());
 	        assertEquals(3,rs.getLong(1));
 	        assertTrue(rs.next());
 	        assertEquals(1,rs.getLong(1));
@@ -323,7 +323,30 @@ public class TxCheckpointIT extends BaseHBaseManagedTimeIT {
 	        assertEquals(1,rs.getLong(1));
 	        assertFalse(rs.next());
 	
+            rs = conn.createStatement().executeQuery("select /*+ INDEX(DEMO IDX) */ id1 from DEMO1");
+            assertTrue(rs.next());
+            assertEquals(1,rs.getLong(1));
+            assertFalse(rs.next());
+    
+            stmt.executeUpdate("upsert into DEMO1 SELECT id1 + 3, id1, id1 FROM DEMO1");
+            stmt.executeUpdate("upsert into DEMO2 values (2, 4)");
+
+            conn.createStatement().execute("delete from DEMO1 where id1 in (select fk1a from DEMO1 join DEMO2 on (fk2=id1))");
+            assertEquals(VisibilityLevel.SNAPSHOT_EXCLUDE_CURRENT, state.getVisibilityLevel());
+            assertNotEquals(wp, state.getWritePointer()); // Make sure write ptr moved
+    
+            rs = conn.createStatement().executeQuery("select /*+ NO_INDEX */ id1 from DEMO1");
+            assertTrue(rs.next());
+            assertEquals(4,rs.getLong(1));
+            assertFalse(rs.next());
+    
+            rs = conn.createStatement().executeQuery("select /*+ INDEX(DEMO IDX) */ id1 from DEMO1");
+            assertTrue(rs.next());
+            assertEquals(4,rs.getLong(1));
+            assertFalse(rs.next());
+    
 	        conn.rollback();
+	        
 	        rs = conn.createStatement().executeQuery("select /*+ NO_INDEX */ id1 from DEMO1");
 	        assertTrue(rs.next());
 	        assertEquals(1,rs.getLong(1));
@@ -332,6 +355,15 @@ public class TxCheckpointIT extends BaseHBaseManagedTimeIT {
 	        assertTrue(rs.next());
 	        assertEquals(3,rs.getLong(1));
 	        assertFalse(rs.next());
+
+	        rs = conn.createStatement().executeQuery("select /*+ INDEX(DEMO IDX) */ id1 from DEMO1");
+            assertTrue(rs.next());
+            assertEquals(3,rs.getLong(1));
+            assertTrue(rs.next());
+            assertEquals(2,rs.getLong(1));
+            assertTrue(rs.next());
+            assertEquals(1,rs.getLong(1));
+            assertFalse(rs.next());
 		}
     }  
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/bb7c74df/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 9925f75..3279bd4 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
@@ -252,6 +252,7 @@ public enum SQLExceptionCode {
     TX_MAY_NOT_SWITCH_TO_NON_TX(1071, "44A02", "A transactional table may not be switched to non transactional"),
 	STORE_NULLS_MUST_BE_TRUE_FOR_TRANSACTIONAL(1072, "44A03", "Store nulls must be true when a table is transactional"),
     CANNOT_START_TRANSACTION_WITH_SCN_SET(1073, "44A04", "Cannot start a transaction on a connection with SCN set"),
+    TX_MAX_VERSIONS_MUST_BE_GREATER_THAN_ONE(1074, "44A05", "A transactional table must define VERSION of greater than one"),
 
     /** Sequence related */
     SEQUENCE_ALREADY_EXIST(1200, "42Z00", "Sequence already exists.", new Factory() {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/bb7c74df/phoenix-core/src/main/java/org/apache/phoenix/index/PhoenixTransactionalIndexer.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/index/PhoenixTransactionalIndexer.java b/phoenix-core/src/main/java/org/apache/phoenix/index/PhoenixTransactionalIndexer.java
index 9f03747..40ee817 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/index/PhoenixTransactionalIndexer.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/index/PhoenixTransactionalIndexer.java
@@ -13,30 +13,23 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Set;
 
-import co.cask.tephra.Transaction;
-import co.cask.tephra.Transaction.VisibilityLevel;
-import co.cask.tephra.TxConstants;
-import co.cask.tephra.hbase98.TransactionAwareHTable;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.hbase.Cell;
 import org.apache.hadoop.hbase.CellScanner;
-import org.apache.hadoop.hbase.CellUtil;
 import org.apache.hadoop.hbase.CoprocessorEnvironment;
-import org.apache.hadoop.hbase.HConstants;
 import org.apache.hadoop.hbase.KeyValue;
 import org.apache.hadoop.hbase.TableName;
 import org.apache.hadoop.hbase.client.HTableInterface;
 import org.apache.hadoop.hbase.client.Mutation;
-import org.apache.hadoop.hbase.client.Put;
 import org.apache.hadoop.hbase.client.Result;
 import org.apache.hadoop.hbase.client.ResultScanner;
 import org.apache.hadoop.hbase.client.Scan;
@@ -44,9 +37,8 @@ import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
 import org.apache.hadoop.hbase.coprocessor.ObserverContext;
 import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
-import org.apache.hadoop.hbase.io.TimeRange;
 import org.apache.hadoop.hbase.regionserver.MiniBatchOperationInProgress;
-import org.apache.hadoop.hbase.regionserver.RegionScanner;
+import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.hadoop.hbase.util.Pair;
 import org.apache.phoenix.compile.ScanRanges;
 import org.apache.phoenix.hbase.index.MultiMutation;
@@ -60,7 +52,6 @@ import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
 import org.apache.phoenix.hbase.index.write.IndexWriter;
 import org.apache.phoenix.query.KeyRange;
 import org.apache.phoenix.query.QueryConstants;
-import org.apache.phoenix.schema.types.PDataType;
 import org.apache.phoenix.schema.types.PVarbinary;
 import org.apache.phoenix.trace.TracingUtils;
 import org.apache.phoenix.trace.util.NullSpan;
@@ -74,6 +65,12 @@ import org.cloudera.htrace.TraceScope;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
+import com.google.common.primitives.Longs;
+
+import co.cask.tephra.Transaction;
+import co.cask.tephra.Transaction.VisibilityLevel;
+import co.cask.tephra.TxConstants;
+import co.cask.tephra.hbase98.TransactionAwareHTable;
 
 /**
  * Do all the work of managing index updates for a transactional table from a single coprocessor. Since the transaction
@@ -169,7 +166,6 @@ public class PhoenixTransactionalIndexer extends BaseRegionObserver {
 
     private Collection<Pair<Mutation, byte[]>> getIndexUpdates(RegionCoprocessorEnvironment env, PhoenixIndexMetaData indexMetaData, Iterator<Mutation> mutationIterator, byte[] txRollbackAttribute) throws IOException {
         ResultScanner currentScanner = null;
-        ResultScanner previousScanner = null;
         TransactionAwareHTable txTable = null;
         // Collect up all mutations in batch
         Map<ImmutableBytesPtr, MultiMutation> mutations =
@@ -199,9 +195,10 @@ public class PhoenixTransactionalIndexer extends BaseRegionObserver {
             // to aid in rollback if there's a KeyValue column in the index. The alternative would be
             // to hold on to all uncommitted index row keys (even ones already sent to HBase) on the
             // client side.
-                mutableColumns.addAll(indexMaintainer.getAllColumns());
+            mutableColumns.addAll(indexMaintainer.getAllColumns());
         }
 
+        boolean isRollback = txRollbackAttribute!=null;
         Collection<Pair<Mutation, byte[]>> indexUpdates = new ArrayList<Pair<Mutation, byte[]>>(mutations.size() * 2 * indexMaintainers.size());
         try {
             if (!mutableColumns.isEmpty()) {
@@ -216,6 +213,7 @@ public class PhoenixTransactionalIndexer extends BaseRegionObserver {
                 }
                 // Project empty key value column
                 scan.addColumn(indexMaintainers.get(0).getDataEmptyKeyValueCF(), QueryConstants.EMPTY_COLUMN_BYTES);
+                // Project all mutable columns
                 ScanRanges scanRanges = ScanRanges.create(SchemaUtil.VAR_BINARY_SCHEMA, Collections.singletonList(keys), ScanUtil.SINGLE_COLUMN_SLOT_SPAN);
                 scanRanges.initializeScan(scan);
                 scan.setFilter(scanRanges.getSkipScanFilter());
@@ -223,23 +221,24 @@ public class PhoenixTransactionalIndexer extends BaseRegionObserver {
                 HTableInterface htable = env.getTable(tableName);
                 txTable = new TransactionAwareHTable(htable);
                 txTable.startTx(tx);
-                currentScanner = txTable.getScanner(scan);
-                if (txRollbackAttribute!=null) {
-	                tx.setVisibility(VisibilityLevel.SNAPSHOT_EXCLUDE_CURRENT);
-	                previousScanner = txTable.getScanner(scan);
+                // For rollback, we need to see all versions, including
+                // the last committed version as there may be multiple
+                // checkpointed versions.
+                if (isRollback) {
+                    tx.setVisibility(VisibilityLevel.SNAPSHOT_ALL);
                 }
+                currentScanner = txTable.getScanner(scan);
             }
-            // In case of rollback we have to do two scans, one with VisibilityLevel.SNAPSHOT to see the current state of the row 
-            // and another with VisibilityLevel.SNAPSHOT_EXCLUDE_CURRENT to see the previous state of the row
-            // so that we can rollback a previous delete + put 
-            processScanner(env, indexMetaData, txRollbackAttribute, previousScanner, mutations, tx, mutableColumns, indexUpdates, false);
-            processScanner(env, indexMetaData, txRollbackAttribute, currentScanner, mutations, tx, mutableColumns, indexUpdates, true);
-            for (Mutation m : mutations.values()) {
-            	long timestamp = getTimestamp(txRollbackAttribute, tx.getWritePointer(), m);
-            	TxTableState state = new TxTableState(env, mutableColumns, indexMetaData.getAttributes(), timestamp, m);
-            	// if we did not generate valid put, we might have to generate a delete
-                if (!generatePuts(indexMetaData, indexUpdates, state)) {
-                	generateDeletes(indexMetaData, indexUpdates, txRollbackAttribute, state);
+            if (isRollback) {
+                processRollback(env, indexMetaData, txRollbackAttribute, currentScanner, mutations, tx, mutableColumns, indexUpdates);
+            } else {
+                processScanner(env, indexMetaData, txRollbackAttribute, currentScanner, mutations, tx, mutableColumns, indexUpdates);
+                for (Mutation m : mutations.values()) {
+                    TxTableState state = new TxTableState(env, mutableColumns, indexMetaData.getAttributes(), tx.getWritePointer(), m);
+                    // if we did not generate valid put, we might have to generate a delete
+                    if (!generatePuts(indexMetaData, indexUpdates, state)) {
+                        generateDeletes(indexMetaData, indexUpdates, txRollbackAttribute, state);
+                    }
                 }
             }
         } finally {
@@ -249,67 +248,128 @@ public class PhoenixTransactionalIndexer extends BaseRegionObserver {
         return indexUpdates;
     }
 
-	private void processScanner(RegionCoprocessorEnvironment env,
-			PhoenixIndexMetaData indexMetaData, byte[] txRollbackAttribute,
-			ResultScanner scanner,
-			Map<ImmutableBytesPtr, MultiMutation> mutations, Transaction tx,
-			Set<ColumnReference> mutableColumns,
-			Collection<Pair<Mutation, byte[]>> indexUpdates, boolean removeMutation) throws IOException {
-		if (scanner != null) {
-		    Result result;
-		    ColumnReference emptyColRef = new ColumnReference(indexMetaData.getIndexMaintainers().get(0).getDataEmptyKeyValueCF(), QueryConstants.EMPTY_COLUMN_BYTES);
-		    while ((result = scanner.next()) != null) {
-		        Mutation m = removeMutation ? mutations.remove(new ImmutableBytesPtr(result.getRow())) : mutations.get(new ImmutableBytesPtr(result.getRow()));
-		        long timestamp = getTimestamp(txRollbackAttribute, tx.getWritePointer(), m);
-		        TxTableState state = new TxTableState(env, mutableColumns, indexMetaData.getAttributes(), timestamp, m, emptyColRef, result);
-		        generateDeletes(indexMetaData, indexUpdates, txRollbackAttribute, state);
-		        generatePuts(indexMetaData, indexUpdates, state);
-		    }
-		}
-	}
-
-	private long getTimestamp(byte[] txRollbackAttribute, long txnWritePointer, Mutation m) {
-		if (txRollbackAttribute==null) {
-			return txnWritePointer;
-		}
-		// if this is a rollback generate mutations with the same timestamp as the data row mutation as the timestamp might be 
-        // different from the current txn write pointer because of check points
-		long mutationTimestamp = txnWritePointer;
-		for (Entry<byte[], List<Cell>> entry : m.getFamilyCellMap().entrySet()) {
-			mutationTimestamp = entry.getValue().get(0).getTimestamp();
-			break;
-		}
-		return mutationTimestamp;
-	}
-
-	private void generateDeletes(PhoenixIndexMetaData indexMetaData,
-			Collection<Pair<Mutation, byte[]>> indexUpdates,
-			byte[] attribValue, TxTableState state) throws IOException {
-		Iterable<IndexUpdate> deletes = codec.getIndexDeletes(state, indexMetaData);
-		for (IndexUpdate delete : deletes) {
-		    if (delete.isValid()) {
-		        delete.getUpdate().setAttribute(TxConstants.TX_ROLLBACK_ATTRIBUTE_KEY, attribValue);
-		        indexUpdates.add(new Pair<Mutation, byte[]>(delete.getUpdate(),delete.getTableName()));
-		    }
-		}
-	}
-
-	boolean generatePuts(
-			PhoenixIndexMetaData indexMetaData,
-			Collection<Pair<Mutation, byte[]>> indexUpdates,
-			TxTableState state)
-			throws IOException {
-		state.applyMutation();
-		Iterable<IndexUpdate> puts = codec.getIndexUpserts(state, indexMetaData);
-		boolean validPut = false;
-		for (IndexUpdate put : puts) {
-		    if (put.isValid()) {
-		        indexUpdates.add(new Pair<Mutation, byte[]>(put.getUpdate(),put.getTableName()));
-		        validPut = true;
-		    }
-		}
-		return validPut;
-	}
+    private void processScanner(RegionCoprocessorEnvironment env,
+            PhoenixIndexMetaData indexMetaData, byte[] txRollbackAttribute,
+            ResultScanner scanner,
+            Map<ImmutableBytesPtr, MultiMutation> mutations, Transaction tx,
+            Set<ColumnReference> mutableColumns,
+            Collection<Pair<Mutation, byte[]>> indexUpdates) throws IOException {
+        if (scanner != null) {
+            Result result;
+            ColumnReference emptyColRef = new ColumnReference(indexMetaData.getIndexMaintainers().get(0).getDataEmptyKeyValueCF(), QueryConstants.EMPTY_COLUMN_BYTES);
+            while ((result = scanner.next()) != null) {
+                Mutation m = mutations.remove(new ImmutableBytesPtr(result.getRow()));
+                TxTableState state = new TxTableState(env, mutableColumns, indexMetaData.getAttributes(), tx.getWritePointer(), m, emptyColRef, result);
+                generateDeletes(indexMetaData, indexUpdates, txRollbackAttribute, state);
+                generatePuts(indexMetaData, indexUpdates, state);
+            }
+        }
+    }
+
+    private void processRollback(RegionCoprocessorEnvironment env,
+            PhoenixIndexMetaData indexMetaData, byte[] txRollbackAttribute,
+            ResultScanner scanner,
+            Map<ImmutableBytesPtr, MultiMutation> mutations, Transaction tx,
+            Set<ColumnReference> mutableColumns,
+            Collection<Pair<Mutation, byte[]>> indexUpdates) throws IOException {
+        if (scanner != null) {
+            Result result;
+            ColumnReference emptyColRef = new ColumnReference(indexMetaData.getIndexMaintainers().get(0).getDataEmptyKeyValueCF(), QueryConstants.EMPTY_COLUMN_BYTES);
+            while ((result = scanner.next()) != null) {
+                Mutation m = mutations.remove(new ImmutableBytesPtr(result.getRow()));
+                // Sort by timestamp, type, cf, cq so we can process in time batches
+                List<Cell> cells = result.listCells();
+                Collections.sort(cells, new Comparator<Cell>() {
+
+                    @Override
+                    public int compare(Cell o1, Cell o2) {
+                        int c = Longs.compare(o1.getTimestamp(), o2.getTimestamp());
+                        if (c != 0) return c;
+                        c = o1.getTypeByte() - o2.getTypeByte();
+                        if (c != 0) return c;
+                        c = Bytes.compareTo(o1.getFamilyArray(), o1.getFamilyOffset(), o1.getFamilyLength(), o1.getFamilyArray(), o1.getFamilyOffset(), o1.getFamilyLength());
+                        if (c != 0) return c;
+                        return Bytes.compareTo(o1.getQualifierArray(), o1.getQualifierOffset(), o1.getQualifierLength(), o1.getQualifierArray(), o1.getQualifierOffset(), o1.getQualifierLength());
+                    }
+                    
+                });
+                int i = 0;
+                int nCells = cells.size();
+                Result oldResult = null, newResult;
+                long readPtr = tx.getReadPointer();
+                do {
+                    boolean hasPuts = false;
+                    LinkedList<Cell> singleTimeCells = Lists.newLinkedList();
+                    long writePtr;
+                    do {
+                        Cell cell = cells.get(i);
+                        hasPuts |= cell.getTypeByte() == KeyValue.Type.Put.getCode();
+                        writePtr = cell.getTimestamp();
+                        do {
+                            // Add at the beginning of the list to match the expected HBase
+                            // newest to oldest sort order (which TxTableState relies on
+                            // with the Result.getLatestColumnValue() calls).
+                            singleTimeCells.addFirst(cell);
+                        } while (++i < nCells && cells.get(i).getTimestamp() == writePtr);
+                    } while (i < nCells && cells.get(i).getTimestamp() <= readPtr);
+                    
+                    // Generate point delete markers for the prior row deletion of the old index value.
+                    // The write timestamp is the next timestamp, not the current timestamp,
+                    // as the earliest cells are the current values for the row (and we don't
+                    // want to delete the current row).
+                    if (oldResult != null) {
+                        TxTableState state = new TxTableState(env, mutableColumns, indexMetaData.getAttributes(), writePtr, m, emptyColRef, oldResult);
+                        generateDeletes(indexMetaData, indexUpdates, txRollbackAttribute, state);
+                    }
+                    // Generate point delete markers for the new index value.
+                    // If our time batch doesn't have Puts (i.e. we have only Deletes), then do not
+                    // generate deletes. We would have generated the delete above based on the state
+                    // of the previous row. The delete markers do not give us the state we need to
+                    // delete.
+                    if (hasPuts) {
+                        newResult = Result.create(singleTimeCells);
+                        // First row may represent the current state which we don't want to delete
+                        if (writePtr > readPtr) {
+                            TxTableState state = new TxTableState(env, mutableColumns, indexMetaData.getAttributes(), writePtr, m, emptyColRef, newResult);
+                            generateDeletes(indexMetaData, indexUpdates, txRollbackAttribute, state);
+                        }
+                        oldResult = newResult;
+                    } else {
+                        oldResult = null;
+                    }
+                } while (i < nCells);
+            }
+        }
+    }
+
+    private void generateDeletes(PhoenixIndexMetaData indexMetaData,
+            Collection<Pair<Mutation, byte[]>> indexUpdates,
+            byte[] attribValue, TxTableState state) throws IOException {
+        Iterable<IndexUpdate> deletes = codec.getIndexDeletes(state, indexMetaData);
+        for (IndexUpdate delete : deletes) {
+            if (delete.isValid()) {
+                delete.getUpdate().setAttribute(TxConstants.TX_ROLLBACK_ATTRIBUTE_KEY, attribValue);
+                indexUpdates.add(new Pair<Mutation, byte[]>(delete.getUpdate(),delete.getTableName()));
+            }
+        }
+    }
+
+    boolean generatePuts(
+            PhoenixIndexMetaData indexMetaData,
+            Collection<Pair<Mutation, byte[]>> indexUpdates,
+            TxTableState state)
+            throws IOException {
+        state.applyMutation();
+        Iterable<IndexUpdate> puts = codec.getIndexUpserts(state, indexMetaData);
+        boolean validPut = false;
+        for (IndexUpdate put : puts) {
+            if (put.isValid()) {
+                indexUpdates.add(new Pair<Mutation, byte[]>(put.getUpdate(),put.getTableName()));
+                validPut = true;
+            }
+        }
+        return validPut;
+    }
 
 
     private static class TxTableState implements TableState {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/bb7c74df/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
index 2f9e251..69a4c1c 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
@@ -624,25 +624,40 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
             for (Entry<String,Object> entry : family.getSecond().entrySet()) {
                 String key = entry.getKey();
                 Object value = entry.getValue();
-                columnDesc.setValue(key, value == null ? null : value.toString());
+                setHColumnDescriptorValue(columnDesc, key, value);
             }
         }
         return columnDesc;
     }
-
-    private void modifyColumnFamilyDescriptor(HColumnDescriptor hcd, Pair<byte[], Map<String,Object>> family) throws SQLException {
-        if (Bytes.equals(hcd.getName(), family.getFirst())) {
-            modifyColumnFamilyDescriptor(hcd, family.getSecond());
+    
+    // Workaround HBASE-14737
+    private static void setHColumnDescriptorValue(HColumnDescriptor columnDesc, String key, Object value) {
+        if (HConstants.VERSIONS.equals(key)) {
+            columnDesc.setMaxVersions(getMaxVersion(value));
         } else {
-            throw new IllegalArgumentException("Column family names don't match. Column descriptor family name: " + hcd.getNameAsString() + ", Family name: " + Bytes.toString(family.getFirst()));
+            columnDesc.setValue(key, value == null ? null : value.toString());
         }
     }
+
+    private static int getMaxVersion(Object value) {
+        if (value == null) {
+            return -1;  // HColumnDescriptor.UNINITIALIZED is private
+        }
+        if (value instanceof Number) {
+            return ((Number)value).intValue();
+        }
+        String stringValue = value.toString();
+        if (stringValue.isEmpty()) {
+            return -1;
+        }
+        return Integer.parseInt(stringValue);
+    }
     
     private void modifyColumnFamilyDescriptor(HColumnDescriptor hcd, Map<String,Object> props) throws SQLException {
         for (Entry<String, Object> entry : props.entrySet()) {
             String propName = entry.getKey();
             Object value = entry.getValue();
-            hcd.setValue(propName, value == null ? null : value.toString());
+            setHColumnDescriptorValue(hcd, propName, value);
         }
     }
     
@@ -681,7 +696,11 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
                     tableDescriptor.addFamily(columnDescriptor);
                 } else {
                     if (tableType != PTableType.VIEW) {
-                        modifyColumnFamilyDescriptor(tableDescriptor.getFamily(familyByte), family);
+                        HColumnDescriptor columnDescriptor = tableDescriptor.getFamily(familyByte);
+                        if (columnDescriptor == null) {
+                            throw new IllegalArgumentException("Unable to find column descriptor with family name " + Bytes.toString(family.getFirst()));
+                        }
+                        modifyColumnFamilyDescriptor(columnDescriptor, family.getSecond());
                     }
                 }
             }
@@ -1540,18 +1559,23 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
         List<Pair<byte[], Map<String, Object>>> families = new ArrayList<>(stmtProperties.size());
         Map<String, Object> tableProps = new HashMap<String, Object>();
         Set<HTableDescriptor> tableDescriptors = Collections.emptySet();
+        Set<HTableDescriptor> origTableDescriptors = Collections.emptySet();
         boolean nonTxToTx = false;
-        HTableDescriptor tableDescriptor = separateAndValidateProperties(table, stmtProperties, colFamiliesForPColumnsToBeAdded, families, tableProps);
+        Pair<HTableDescriptor,HTableDescriptor> tableDescriptorPair = separateAndValidateProperties(table, stmtProperties, colFamiliesForPColumnsToBeAdded, families, tableProps);
+        HTableDescriptor tableDescriptor = tableDescriptorPair.getSecond();
+        HTableDescriptor origTableDescriptor = tableDescriptorPair.getFirst();
         if (tableDescriptor != null) {
             tableDescriptors = Sets.newHashSetWithExpectedSize(3 + table.getIndexes().size());
+            origTableDescriptors = Sets.newHashSetWithExpectedSize(3 + table.getIndexes().size());
             tableDescriptors.add(tableDescriptor);
+            origTableDescriptors.add(origTableDescriptor);
             nonTxToTx = Boolean.TRUE.equals(tableProps.get(TxConstants.READ_NON_TX_DATA));
             /*
              * If the table was transitioned from non transactional to transactional, we need
              * to also transition the index tables.
              */
             if (nonTxToTx) {
-                updateDescriptorForTx(table, tableProps, tableDescriptor, Boolean.TRUE.toString(), tableDescriptors);
+                updateDescriptorForTx(table, tableProps, tableDescriptor, Boolean.TRUE.toString(), tableDescriptors, origTableDescriptors);
             }
         }
         
@@ -1631,17 +1655,13 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
             // no longer function correctly.
             // Note that if this fails, we're in a corrupt state.
             if (!success && metaDataUpdated && nonTxToTx) {
-                tableDescriptors.clear();
-                tableDescriptors.add(tableDescriptor);
-                tableProps.remove(TxConstants.READ_NON_TX_DATA);
-                updateDescriptorForTx(table, tableProps, tableDescriptor, null, tableDescriptors);
-                sendHBaseMetaData(tableDescriptors, pollingNeeded);
+                sendHBaseMetaData(origTableDescriptors, pollingNeeded);
             }
         }
         return result;
     }
     private void updateDescriptorForTx(PTable table, Map<String, Object> tableProps, HTableDescriptor tableDescriptor,
-            String txValue, Set<HTableDescriptor> descriptorsToUpdate) throws SQLException {
+            String txValue, Set<HTableDescriptor> descriptorsToUpdate, Set<HTableDescriptor> origDescriptors) throws SQLException {
         HBaseAdmin admin = null;
         byte[] physicalTableName = table.getPhysicalName().getBytes();
         try {
@@ -1656,19 +1676,37 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
             }
             for (PTable index : table.getIndexes()) {
                 HTableDescriptor indexDescriptor = admin.getTableDescriptor(index.getPhysicalName().getBytes());
+                origDescriptors.add(indexDescriptor);
+                indexDescriptor = new HTableDescriptor(indexDescriptor);
                 descriptorsToUpdate.add(indexDescriptor);
+                if (index.getColumnFamilies().isEmpty()) {
+                    byte[] dataFamilyName = SchemaUtil.getEmptyColumnFamily(table);
+                    byte[] indexFamilyName = SchemaUtil.getEmptyColumnFamily(index);
+                    indexDescriptor.getFamily(indexFamilyName).setMaxVersions(tableDescriptor.getFamily(dataFamilyName).getMaxVersions());
+                } else {
+                    for (PColumnFamily family : index.getColumnFamilies()) {
+                        byte[] familyName = family.getName().getBytes();
+                        indexDescriptor.getFamily(familyName).setMaxVersions(tableDescriptor.getFamily(familyName).getMaxVersions());
+                    }
+                }
                 setTransactional(indexDescriptor, index.getType(), txValue, indexTableProps);
             }
             try {
                 HTableDescriptor indexDescriptor = admin.getTableDescriptor(MetaDataUtil.getViewIndexPhysicalName(physicalTableName));
+                origDescriptors.add(indexDescriptor);
+                indexDescriptor = new HTableDescriptor(indexDescriptor);
                 descriptorsToUpdate.add(indexDescriptor);
+                setSharedIndexMaxVersion(table, tableDescriptor, indexDescriptor);
                 setTransactional(indexDescriptor, PTableType.INDEX, txValue, indexTableProps);
             } catch (org.apache.hadoop.hbase.TableNotFoundException ignore) {
                 // Ignore, as we may never have created a view index table
             }
             try {
                 HTableDescriptor indexDescriptor = admin.getTableDescriptor(MetaDataUtil.getLocalIndexPhysicalName(physicalTableName));
+                origDescriptors.add(indexDescriptor);
+                indexDescriptor = new HTableDescriptor(indexDescriptor);
                 descriptorsToUpdate.add(indexDescriptor);
+                setSharedIndexMaxVersion(table, tableDescriptor, indexDescriptor);
                 setTransactional(indexDescriptor, PTableType.INDEX, txValue, indexTableProps);
             } catch (org.apache.hadoop.hbase.TableNotFoundException ignore) {
                 // Ignore, as we may never have created a view index table
@@ -1683,6 +1721,21 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
             }
         }
     }
+    private void setSharedIndexMaxVersion(PTable table, HTableDescriptor tableDescriptor,
+            HTableDescriptor indexDescriptor) {
+        if (table.getColumnFamilies().isEmpty()) {
+            byte[] familyName = SchemaUtil.getEmptyColumnFamily(table);
+            indexDescriptor.getFamily(familyName).setMaxVersions(tableDescriptor.getFamily(familyName).getMaxVersions());
+        } else {
+            for (PColumnFamily family : table.getColumnFamilies()) {
+                byte[] familyName = family.getName().getBytes();
+                HColumnDescriptor colDescriptor = indexDescriptor.getFamily(familyName);
+                if (colDescriptor != null) {
+                    colDescriptor.setMaxVersions(tableDescriptor.getFamily(familyName).getMaxVersions());
+                }
+            }
+        }
+    }
     
     private void sendHBaseMetaData(Set<HTableDescriptor> tableDescriptors, boolean pollingNeeded) throws SQLException {
         SQLException sqlE = null;
@@ -1713,12 +1766,15 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
         this.addCoprocessors(tableDescriptor.getName(), tableDescriptor, tableType, tableProps);
     }
     
-    private HTableDescriptor separateAndValidateProperties(PTable table, Map<String, List<Pair<String, Object>>> properties, Set<String> colFamiliesForPColumnsToBeAdded, List<Pair<byte[], Map<String, Object>>> families, Map<String, Object> tableProps) throws SQLException {
+    private Pair<HTableDescriptor,HTableDescriptor> separateAndValidateProperties(PTable table, Map<String, List<Pair<String, Object>>> properties, Set<String> colFamiliesForPColumnsToBeAdded, List<Pair<byte[], Map<String, Object>>> families, Map<String, Object> tableProps) throws SQLException {
         Map<String, Map<String, Object>> stmtFamiliesPropsMap = new HashMap<>(properties.size());
         Map<String,Object> commonFamilyProps = new HashMap<>();
         boolean addingColumns = colFamiliesForPColumnsToBeAdded != null && colFamiliesForPColumnsToBeAdded.size() > 0;
         HashSet<String> existingColumnFamilies = existingColumnFamilies(table);
         Map<String, Map<String, Object>> allFamiliesProps = new HashMap<>(existingColumnFamilies.size());
+        boolean isTransactional = table.isTransactional();
+        boolean willBeTransactional = false;
+        boolean isOrWillBeTransactional = isTransactional;
         for (String family : properties.keySet()) {
             List<Pair<String, Object>> propsList = properties.get(family);
             if (propsList != null && propsList.size() > 0) {
@@ -1748,14 +1804,15 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
                                 // We enforce that all column families have the same TTL.
                                 commonFamilyProps.put(propName, prop.getSecond());
                             } else if (propName.equals(PhoenixDatabaseMetaData.TRANSACTIONAL) && Boolean.TRUE.equals(propValue)) {
+                                willBeTransactional = isOrWillBeTransactional = true;
                                 tableProps.put(TxConstants.READ_NON_TX_DATA, propValue);
                             }
                         } else {
                             if (isHColumnProperty(propName)) {
                                 if (family.equals(QueryConstants.ALL_FAMILY_PROPERTIES_KEY)) {
-                                    commonFamilyProps.put(propName, prop.getSecond());
+                                    commonFamilyProps.put(propName, propValue);
                                 } else {
-                                    colFamilyPropsMap.put(propName, prop.getSecond());
+                                    colFamilyPropsMap.put(propName, propValue);
                                 }
                             } else {
                                 // invalid property - neither of HTableProp, HColumnProp or PhoenixTableProp
@@ -1844,16 +1901,17 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
             allFamiliesProps.put(Bytes.toString(table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES : table.getDefaultFamilyName().getBytes() ), commonFamilyProps);
         }
 
-
         // Views are not allowed to have any of these properties.
         if (table.getType() == PTableType.VIEW && (!stmtFamiliesPropsMap.isEmpty() || !commonFamilyProps.isEmpty() || !tableProps.isEmpty())) {
             throw new SQLExceptionInfo.Builder(SQLExceptionCode.VIEW_WITH_PROPERTIES).build()
             .buildException();
         }
+        
         HTableDescriptor newTableDescriptor = null;
+        HTableDescriptor origTableDescriptor = null;
         if (!allFamiliesProps.isEmpty() || !tableProps.isEmpty()) {
             byte[] tableNameBytes = Bytes.toBytes(table.getPhysicalName().getString());
-            HTableDescriptor existingTableDescriptor = getTableDescriptor(tableNameBytes);
+            HTableDescriptor existingTableDescriptor = origTableDescriptor = getTableDescriptor(tableNameBytes);
             newTableDescriptor = new HTableDescriptor(existingTableDescriptor);
             if (!tableProps.isEmpty()) {
                 // add all the table properties to the existing table descriptor
@@ -1862,22 +1920,69 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
                 }
             }
             if (addingColumns) {
+                // FIXME: we should be setting TTL on all table column families, not
+                // just the ones being modified here (if TTL is being set).
                 // Make sure that all the CFs of the table have the same TTL as the empty CF. 
                 setTTLToEmptyCFTTL(allFamiliesProps, table, newTableDescriptor);
             }
+            Integer defaultTxMaxVersions = null;
+            if (isOrWillBeTransactional) {
+                Map<String, Object> emptyFamilyProps = allFamiliesProps.get(SchemaUtil.getEmptyColumnFamilyAsString(table));
+                if (emptyFamilyProps != null) {
+                    defaultTxMaxVersions = (Integer)emptyFamilyProps.get(HConstants.VERSIONS);
+                }
+                if (defaultTxMaxVersions == null) {
+                    if (isTransactional) {
+                        defaultTxMaxVersions = newTableDescriptor.getFamily(SchemaUtil.getEmptyColumnFamily(table)).getMaxVersions();
+                    } else {
+                        defaultTxMaxVersions = 
+                                this.getProps().getInt(
+                                        QueryServices.MAX_VERSIONS_TRANSACTIONAL_ATTRIB,
+                                        QueryServicesOptions.DEFAULT_MAX_VERSIONS_TRANSACTIONAL);
+                    }
+                }
+                if (willBeTransactional) {
+                    // Set VERSIONS for all column families when transitioning to transactional
+                    for (PColumnFamily family : table.getColumnFamilies()) {
+                        if (!allFamiliesProps.containsKey(family.getName().getString())) {
+                            Map<String,Object> familyProps = Maps.newHashMapWithExpectedSize(1);
+                            familyProps.put(HConstants.VERSIONS, defaultTxMaxVersions);
+                            allFamiliesProps.put(family.getName().getString(), familyProps);
+                        }
+                    }
+                }
+            }
             for (Entry<String, Map<String, Object>> entry : allFamiliesProps.entrySet()) {
-                byte[] cf = entry.getKey().getBytes();
+                Map<String,Object> familyProps = entry.getValue();
+                if (isOrWillBeTransactional) {
+                    if (!familyProps.containsKey(HConstants.VERSIONS)) {
+                        familyProps.put(HConstants.VERSIONS, defaultTxMaxVersions);
+                    }
+                }
+                byte[] cf = Bytes.toBytes(entry.getKey());
                 HColumnDescriptor colDescriptor = newTableDescriptor.getFamily(cf);
                 if (colDescriptor == null) {
                     // new column family
-                    colDescriptor = generateColumnFamilyDescriptor(new Pair<>(cf, entry.getValue()), table.getType());
+                    colDescriptor = generateColumnFamilyDescriptor(new Pair<>(cf, familyProps), table.getType());
                     newTableDescriptor.addFamily(colDescriptor);
                 } else {
-                    modifyColumnFamilyDescriptor(colDescriptor, entry.getValue());
+                    modifyColumnFamilyDescriptor(colDescriptor, familyProps);
+                }
+                if (isOrWillBeTransactional) {
+                    checkTransactionalVersionsValue(colDescriptor);
                 }
             }
         }
-        return newTableDescriptor;
+        return new Pair<>(origTableDescriptor, newTableDescriptor);
+    }
+    
+    private void checkTransactionalVersionsValue(HColumnDescriptor colDescriptor) throws SQLException {
+        int maxVersions = colDescriptor.getMaxVersions();
+        if (maxVersions <= 1) {
+            throw new SQLExceptionInfo.Builder(SQLExceptionCode.TX_MAX_VERSIONS_MUST_BE_GREATER_THAN_ONE)
+            .setFamilyName(colDescriptor.getNameAsString())
+            .build().buildException();
+        }
     }
     
     private boolean isHColumnProperty(String propName) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/bb7c74df/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
index 0d2c36f..9068ee2 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServices.java
@@ -154,6 +154,7 @@ public interface QueryServices extends SQLCloseable {
     public static final String DEFAULT_STORE_NULLS_ATTRIB = "phoenix.table.default.store.nulls";
     public static final String METRICS_ENABLED = "phoenix.query.metrics.enabled";
     public static final String DEFAULT_TRANSACTIONAL_ATTRIB = "phoenix.transactions.default.enabled";
+    public static final String MAX_VERSIONS_TRANSACTIONAL_ATTRIB = "phoenix.transactions.maxVersions";
 
     /**
      * Get executor service used for parallel scans

http://git-wip-us.apache.org/repos/asf/phoenix/blob/bb7c74df/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
index 83944b3..fc17f6a 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java
@@ -185,6 +185,7 @@ public class QueryServicesOptions {
     public static final boolean DEFAULT_AUTO_COMMIT = false;
     public static final boolean DEFAULT_IS_METRICS_ENABLED = true;
     public static final boolean DEFAULT_TRANSACTIONAL = false;
+    public static final int DEFAULT_MAX_VERSIONS_TRANSACTIONAL = Integer.MAX_VALUE;
     
     private final Configuration config;
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/bb7c74df/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 a992529..2758554 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
@@ -1286,12 +1286,10 @@ public class MetaDataClient {
             }
 
             Map<String,Object> tableProps = Maps.newHashMapWithExpectedSize(statement.getProps().size());
-            Map<String,Object> commonFamilyProps = Collections.emptyMap();
+            Map<String,Object> commonFamilyProps = Maps.newHashMapWithExpectedSize(statement.getProps().size() + 1);
             // Somewhat hacky way of determining if property is for HColumnDescriptor or HTableDescriptor
             HColumnDescriptor defaultDescriptor = new HColumnDescriptor(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES);
             if (!statement.getProps().isEmpty()) {
-                commonFamilyProps = Maps.newHashMapWithExpectedSize(statement.getProps().size());
-
                 Collection<Pair<String,Object>> props = statement.getProps().get(QueryConstants.ALL_FAMILY_PROPERTIES_KEY);
                 for (Pair<String,Object> prop : props) {
                     if (defaultDescriptor.getValue(prop.getFirst()) == null) {
@@ -1373,7 +1371,9 @@ public class MetaDataClient {
             }
             tableProps.put(PhoenixDatabaseMetaData.TRANSACTIONAL, Boolean.valueOf(transactional));
             
-            if (transactional) { // FIXME: remove once Tephra handles storing multiple versions of a cell value, 
+            boolean sharedTable = statement.getTableType() == PTableType.VIEW || indexId != null;
+            if (transactional) { 
+                // FIXME: remove once Tephra handles storing multiple versions of a cell value, 
             	// and allows ignoring empty key values for an operation
             	if (Boolean.FALSE.equals(storeNullsProp)) {
             		throw new SQLExceptionInfo.Builder(SQLExceptionCode.STORE_NULLS_MUST_BE_TRUE_FOR_TRANSACTIONAL)
@@ -1383,11 +1383,29 @@ public class MetaDataClient {
             	// Force STORE_NULLS to true when transactional as Tephra cannot deal with column deletes
             	storeNulls = true;
             	tableProps.put(PhoenixDatabaseMetaData.STORE_NULLS, Boolean.TRUE);
+            	
+            	if (!sharedTable) {
+                    Integer maxVersionsProp = (Integer) commonFamilyProps.get(HConstants.VERSIONS);
+                    if (maxVersionsProp == null) {
+                        if (parent != null) {
+                            HTableDescriptor desc = connection.getQueryServices().getTableDescriptor(parent.getPhysicalName().getBytes());
+                            if (desc != null) {
+                                maxVersionsProp = desc.getFamily(SchemaUtil.getEmptyColumnFamily(parent)).getMaxVersions();
+                            }
+                        }
+                        if (maxVersionsProp == null) {
+                            maxVersionsProp = connection.getQueryServices().getProps().getInt(
+                                    QueryServices.MAX_VERSIONS_TRANSACTIONAL_ATTRIB,
+                                    QueryServicesOptions.DEFAULT_MAX_VERSIONS_TRANSACTIONAL);
+                        }
+                        commonFamilyProps.put(HConstants.VERSIONS, maxVersionsProp);
+                    }
+            	}
             }
             timestamp = timestamp==null ? TransactionUtil.getTableTimestamp(connection, transactional) : timestamp;
 
             // Delay this check as it is supported to have IMMUTABLE_ROWS and SALT_BUCKETS defined on views
-            if (statement.getTableType() == PTableType.VIEW || indexId != null) {
+            if (sharedTable) {
                 if (tableProps.get(PhoenixDatabaseMetaData.DEFAULT_COLUMN_FAMILY_NAME) != null) {
                     throw new SQLExceptionInfo.Builder(SQLExceptionCode.DEFAULT_COLUMN_FAMILY_ON_SHARED_TABLE)
                     .setSchemaName(schemaName).setTableName(tableName)
@@ -1403,7 +1421,7 @@ public class MetaDataClient {
             List<PColumn> columns;
             LinkedHashSet<PColumn> pkColumns;
 
-            if (tenantId != null && (tableType != PTableType.VIEW && indexId == null)) {
+            if (tenantId != null && !sharedTable) {
                 throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_TENANT_SPECIFIC_TABLE)
                     .setSchemaName(schemaName).setTableName(tableName).build().buildException();
             }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/bb7c74df/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
index 4dc792f..622e58f 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
@@ -30,7 +30,6 @@ import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 
 import javax.annotation.Nullable;
 
@@ -50,7 +49,6 @@ import org.apache.phoenix.schema.ColumnNotFoundException;
 import org.apache.phoenix.schema.PColumn;
 import org.apache.phoenix.schema.PColumnFamily;
 import org.apache.phoenix.schema.PDatum;
-import org.apache.phoenix.schema.PMetaData;
 import org.apache.phoenix.schema.PName;
 import org.apache.phoenix.schema.PTable;
 import org.apache.phoenix.schema.RowKeySchema;
@@ -238,6 +236,9 @@ public class SchemaUtil {
 
     public static String getMetaDataEntityName(String schemaName, String tableName, String familyName, String columnName) {
         if ((schemaName == null || schemaName.isEmpty()) && (tableName == null || tableName.isEmpty())) {
+            if (columnName == null || columnName.isEmpty()) {
+                return familyName;
+            }
             return getName(familyName, columnName, false);
         }
         if ((familyName == null || familyName.isEmpty()) && (columnName == null || columnName.isEmpty())) {
@@ -366,6 +367,11 @@ public class SchemaUtil {
         return families.isEmpty() ? table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES : table.getDefaultFamilyName().getBytes() : families.get(0).getName().getBytes();
     }
 
+    public static String getEmptyColumnFamilyAsString(PTable table) {
+        List<PColumnFamily> families = table.getColumnFamilies();
+        return families.isEmpty() ? table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY : table.getDefaultFamilyName().getString() : families.get(0).getName().getString();
+    }
+
     public static ImmutableBytesPtr getEmptyColumnFamilyPtr(PTable table) {
         List<PColumnFamily> families = table.getColumnFamilies();
         return families.isEmpty() ? table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES_PTR : table.getDefaultFamilyName().getBytesPtr() : families.get(0)