phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tdsi...@apache.org
Subject [1/2] phoenix git commit: PHOENIX-2795 Support auto partition for views
Date Fri, 29 Apr 2016 20:23:32 GMT
Repository: phoenix
Updated Branches:
  refs/heads/4.x-HBase-1.1 6d5adf97d -> ac8d0c57d


http://git-wip-us.apache.org/repos/asf/phoenix/blob/ac8d0c57/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 ac40554..95fde4b 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
@@ -27,6 +27,7 @@ import static org.apache.phoenix.exception.SQLExceptionCode.INSUFFICIENT_MULTI_T
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.ARG_POSITION;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.ARRAY_SIZE;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.ASYNC_CREATED_DATE;
+import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.AUTO_PARTITION_SEQ;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.BASE_COLUMN_COUNT;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.CLASS_NAME;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_COUNT;
@@ -202,6 +203,7 @@ import org.apache.phoenix.util.IndexUtil;
 import org.apache.phoenix.util.LogUtil;
 import org.apache.phoenix.util.MetaDataUtil;
 import org.apache.phoenix.util.PhoenixRuntime;
+import org.apache.phoenix.util.QueryUtil;
 import org.apache.phoenix.util.ReadOnlyProps;
 import org.apache.phoenix.util.ScanUtil;
 import org.apache.phoenix.util.SchemaUtil;
@@ -211,6 +213,8 @@ import org.apache.phoenix.util.UpgradeUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import co.cask.tephra.TxConstants;
+
 import com.google.common.base.Objects;
 import com.google.common.collect.Iterators;
 import com.google.common.collect.ListMultimap;
@@ -219,8 +223,6 @@ import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import com.google.common.primitives.Ints;
 
-import co.cask.tephra.TxConstants;
-
 public class MetaDataClient {
     private static final Logger logger = LoggerFactory.getLogger(MetaDataClient.class);
 
@@ -256,8 +258,9 @@ public class MetaDataClient {
             BASE_COLUMN_COUNT + "," +
             TRANSACTIONAL + "," +
             UPDATE_CACHE_FREQUENCY + "," +
-            IS_NAMESPACE_MAPPED +
-            ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
+            IS_NAMESPACE_MAPPED + "," +
+            AUTO_PARTITION_SEQ +
+            ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?)";
 
     private static final String CREATE_SCHEMA = "UPSERT INTO " + SYSTEM_CATALOG_SCHEMA +
".\"" + SYSTEM_CATALOG_TABLE
             + "\"( " + TABLE_SCHEM + "," + TABLE_NAME + ") VALUES (?,?)";
@@ -1547,9 +1550,8 @@ public class MetaDataClient {
             List<Mutation> tableMetaData = Lists.newArrayListWithExpectedSize(statement.getColumnDefs().size()
+ 3);
 
             TableName tableNameNode = statement.getTableName();
-            String schemaName = tableNameNode.getSchemaName();
-            schemaName = connection.getSchema() != null && schemaName == null ? connection.getSchema()
: schemaName;
-            String tableName = tableNameNode.getTableName();
+            final String schemaName = connection.getSchema() != null && tableNameNode.getSchemaName()
== null ? connection.getSchema() : tableNameNode.getSchemaName();
+            final String tableName = tableNameNode.getTableName();
             String parentTableName = null;
             PName tenantId = connection.getTenantId();
             String tenantIdStr = tenantId == null ? null : tenantId.getString();
@@ -1672,7 +1674,7 @@ public class MetaDataClient {
                 }
                 addSaltColumn = (saltBucketNum != null);
             }
-
+            
             // Can't set MULTI_TENANT or DEFAULT_COLUMN_FAMILY_NAME on an INDEX or a non
mapped VIEW
             if (tableType != PTableType.INDEX && (tableType != PTableType.VIEW ||
viewType == ViewType.MAPPED)) {
                 Boolean multiTenantProp = (Boolean) tableProps.get(PhoenixDatabaseMetaData.MULTI_TENANT);
@@ -1690,6 +1692,7 @@ public class MetaDataClient {
             if (updateCacheFrequencyProp != null) {
                 updateCacheFrequency = updateCacheFrequencyProp;
             }
+            String autoPartitionSeq = (String) TableProperty.AUTO_PARTITION_SEQ.getValue(tableProps);
 
             Boolean storeNullsProp = (Boolean) TableProperty.STORE_NULLS.getValue(tableProps);
             if (storeNullsProp == null) {
@@ -1803,6 +1806,14 @@ public class MetaDataClient {
                 throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_TENANT_SPECIFIC_TABLE)
                     .setSchemaName(schemaName).setTableName(tableName).build().buildException();
             }
+            if (autoPartitionSeq!=null) {
+                int autoPartitionColIndex = multiTenant ? 1 : 0;
+                PDataType dataType = colDefs.get(autoPartitionColIndex).getDataType();
+                if (!PLong.INSTANCE.isCastableTo(dataType)) {
+                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.SEQUENCE_NOT_CASTABLE_TO_AUTO_PARTITION_ID_COLUMN)
+                    .setSchemaName(schemaName).setTableName(tableName).build().buildException();
+                }
+            }
 
             if (tableType == PTableType.VIEW) {
                 physicalNames = Collections.singletonList(PNameFactory.newName(parent.getPhysicalName().getString()));
@@ -1872,6 +1883,8 @@ public class MetaDataClient {
                         }
                         linkStatement.execute();
                     }
+                    tableMetaData.addAll(connection.getMutationState().toMutations(timestamp).next().getSecond());
+                    connection.rollback();
                 }
             }
 
@@ -2023,7 +2036,7 @@ public class MetaDataClient {
                         Collections.<PTable>emptyList(), isImmutableRows,
                         Collections.<PName>emptyList(), defaultFamilyName == null ?
null :
                                 PNameFactory.newName(defaultFamilyName), null,
-                        Boolean.TRUE.equals(disableWAL), false, false, null, indexId, indexType,
true, false, 0, 0L, isNamespaceMapped);
+                        Boolean.TRUE.equals(disableWAL), false, false, null, indexId, indexType,
true, false, 0, 0L, isNamespaceMapped, autoPartitionSeq);
                 connection.addTable(table, MetaDataProtocol.MIN_TABLE_TIMESTAMP);
             } else if (tableType == PTableType.INDEX && indexId == null) {
                 if (tableProps.get(HTableDescriptor.MAX_FILESIZE) == null) {
@@ -2051,12 +2064,30 @@ public class MetaDataClient {
 
             short nextKeySeq = 0;
             
+            List<Mutation> columnMetadata = Lists.newArrayListWithExpectedSize(columns.size());
             try (PreparedStatement colUpsert = connection.prepareStatement(INSERT_COLUMN_CREATE_TABLE))
{
                 for (int i = 0; i < columns.size(); i++) {
                     PColumn column = columns.get(i);
                     final int columnPosition = column.getPosition();
                     // For client-side cache, we need to update the column
-                    if (isViewColumnReferenced != null) {
+                    // set the autoPartition column attributes   
+                    if (parent != null && parent.getAutoPartitionSeqName() != null
+                            && MetaDataUtil.getAutoPartitionColIndex(parent) == columnPosition)
{
+                        columns.set(i, column = new DelegateColumn(column) {
+                            @Override
+                            public byte[] getViewConstant() {
+                                // set to non-null value so that we will generate a Put that

+                                // will be set correctly on the server
+                                return QueryConstants.EMPTY_COLUMN_VALUE_BYTES;
+                            }
+
+                            @Override
+                            public boolean isViewReferenced() {
+                                return true;
+                            }
+                        });
+                    }
+                    else if (isViewColumnReferenced != null) {
                         if (viewColumnConstants != null && columnPosition < viewColumnConstants.length)
{
                             columns.set(i, column = new DelegateColumn(column) {
                                 @Override
@@ -2079,10 +2110,13 @@ public class MetaDataClient {
                     }
                     Short keySeq = SchemaUtil.isPKColumn(column) ? ++nextKeySeq : null;
                     addColumnMutation(schemaName, tableName, column, colUpsert, parentTableName,
pkName, keySeq, saltBucketNum != null);
+                    columnMetadata.addAll(connection.getMutationState().toMutations(timestamp).next().getSecond());
+                    connection.rollback();
                 }
-                tableMetaData.addAll(connection.getMutationState().toMutations(timestamp).next().getSecond());
-                connection.rollback();
             }
+            // add the columns in reverse order since we reverse the list later
+            Collections.reverse(columnMetadata);
+            tableMetaData.addAll(columnMetadata);
 
             String dataTableName = parent == null || tableType == PTableType.VIEW ? null
: parent.getTableName().getString();
             PIndexState indexState = parent == null || tableType == PTableType.VIEW  ? null
: PIndexState.BUILDING;
@@ -2103,7 +2137,14 @@ public class MetaDataClient {
             tableUpsert.setString(10, indexState == null ? null : indexState.getSerializedValue());
             tableUpsert.setBoolean(11, isImmutableRows);
             tableUpsert.setString(12, defaultFamilyName);
-            tableUpsert.setString(13, viewStatement);
+            if (parent != null && parent.getAutoPartitionSeqName() != null &&
viewStatement==null) {
+                // set to non-null value so that we will generate a Put that 
+                // will be set correctly on the server
+                tableUpsert.setString(13, QueryConstants.EMPTY_COLUMN_VALUE);
+            }
+            else {
+                tableUpsert.setString(13, viewStatement);
+            }
             tableUpsert.setBoolean(14, disableWAL);
             tableUpsert.setBoolean(15, multiTenant);
             if (viewType == null) {
@@ -2130,6 +2171,11 @@ public class MetaDataClient {
             tableUpsert.setBoolean(21, transactional);
             tableUpsert.setLong(22, updateCacheFrequency);
             tableUpsert.setBoolean(23, isNamespaceMapped);
+            if (autoPartitionSeq == null) {
+                tableUpsert.setNull(24, Types.VARCHAR);
+            } else {
+                tableUpsert.setString(24, autoPartitionSeq);
+            }
             tableUpsert.execute();
 
             if (asyncCreatedDate != null) {
@@ -2146,7 +2192,7 @@ public class MetaDataClient {
             /*
              * The table metadata must be in the following order:
              * 1) table header row
-             * 2) everything else
+             * 2) ordered column rows
              * 3) parent table header row
              */
             Collections.reverse(tableMetaData);
@@ -2187,14 +2233,48 @@ public class MetaDataClient {
             case CONCURRENT_TABLE_MUTATION:
                 addTableToCache(result);
                 throw new ConcurrentTableMutationException(schemaName, tableName);
+            case AUTO_PARTITION_SEQUENCE_NOT_FOUND:
+                throw new SQLExceptionInfo.Builder(SQLExceptionCode.AUTO_PARTITION_SEQUENCE_UNDEFINED)
+                .setSchemaName(schemaName).setTableName(tableName).build().buildException();
+            case CANNOT_COERCE_AUTO_PARTITION_ID:
+                throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_COERCE_AUTO_PARTITION_ID)
+                .setSchemaName(schemaName).setTableName(tableName).build().buildException();
             default:
+                // If the parent table of the view has the auto partition sequence name attribute,
+                // set the view statement and relevant partition column attributes correctly
+                if (parent!=null && parent.getAutoPartitionSeqName()!=null) {
+                    int autoPartitionColIndex = parent.isMultiTenant() ? 1 : 0;
+                    final Long autoPartitionNum = Long.valueOf(result.getAutoPartitionNum());
+                    final PColumn column = columns.get(autoPartitionColIndex);
+                    columns.set(autoPartitionColIndex, new DelegateColumn(column) {
+                        @Override
+                        public byte[] getViewConstant() {
+                            byte[] bytes = new byte [Bytes.SIZEOF_LONG + 1];
+                            PDataType dataType = column.getDataType();
+                            Object val = dataType.toObject(autoPartitionNum, PLong.INSTANCE);
+                            dataType.toBytes(val, bytes, 0);
+                            return bytes;
+                        }
+                        @Override
+                        public boolean isViewReferenced() {
+                            return true;
+                        }
+                    });
+                    String viewPartitionClause = QueryUtil.getViewPartitionClause(MetaDataUtil.getAutoPartitionColumnName(parent),
autoPartitionNum);
+                    if (viewStatement!=null) {
+                        viewStatement = viewStatement + " AND " + viewPartitionClause;
+                    }
+                    else {
+                        viewStatement = QueryUtil.getViewStatement(parent.getSchemaName().getString(),
parent.getTableName().getString(), viewPartitionClause);
+                    }
+                }
                 PName newSchemaName = PNameFactory.newName(schemaName);
                 PTable table =  PTableImpl.makePTable(
                         tenantId, newSchemaName, PNameFactory.newName(tableName), tableType,
indexState, timestamp!=null ? timestamp : result.getMutationTime(),
                         PTable.INITIAL_SEQ_NUM, pkName == null ? null : PNameFactory.newName(pkName),
saltBucketNum, columns,
                         dataTableName == null ? null : newSchemaName, dataTableName == null
? null : PNameFactory.newName(dataTableName), Collections.<PTable>emptyList(), isImmutableRows,
                         physicalNames, defaultFamilyName == null ? null : PNameFactory.newName(defaultFamilyName),
viewStatement, Boolean.TRUE.equals(disableWAL), multiTenant, storeNulls, viewType,
-                        indexId, indexType, rowKeyOrderOptimizable, transactional, updateCacheFrequency,
0L, isNamespaceMapped);
+                        indexId, indexType, rowKeyOrderOptimizable, transactional, updateCacheFrequency,
0L, isNamespaceMapped, autoPartitionSeq);
                 result = new MetaDataMutationResult(code, result.getMutationTime(), table,
true);
                 addTableToCache(result);
                 return table;

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ac8d0c57/phoenix-core/src/main/java/org/apache/phoenix/schema/PTable.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/PTable.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/PTable.java
index 84db752..5a3f18e 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/PTable.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/PTable.java
@@ -347,4 +347,11 @@ public interface PTable extends PMetaDataEntity {
     long getUpdateCacheFrequency();
 
     boolean isNamespaceMapped();
+    
+    /**
+     * 
+     * @return The sequence name used to get the unique identifier for views
+     * that are automatically partitioned.
+     */
+    String getAutoPartitionSeqName(); 
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ac8d0c57/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
index f4300fd..076264a 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
@@ -66,6 +66,8 @@ import org.apache.phoenix.util.SizedUtil;
 import org.apache.phoenix.util.StringUtil;
 import org.apache.phoenix.util.TrustedByteArrayOutputStream;
 
+import co.cask.tephra.TxConstants;
+
 import com.google.common.base.Objects;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ArrayListMultimap;
@@ -78,8 +80,6 @@ import com.google.common.collect.Maps;
 import com.google.protobuf.HBaseZeroCopyByteString;
 import com.sun.istack.NotNull;
 
-import co.cask.tephra.TxConstants;
-
 /**
  *
  * Base class for PTable implementors.  Provides abstraction for
@@ -139,6 +139,7 @@ public class PTableImpl implements PTable {
     private int rowTimestampColPos;
     private long updateCacheFrequency;
     private boolean isNamespaceMapped;
+    private String autoPartitionSeqName;
 
     public PTableImpl() {
         this.indexes = Collections.emptyList();
@@ -184,7 +185,7 @@ public class PTableImpl implements PTable {
         init(tenantId, this.schemaName, this.tableName, PTableType.INDEX, state, timeStamp,
sequenceNumber, pkName, bucketNum, columns,
             PTableStats.EMPTY_STATS, this.schemaName, parentTableName, indexes, isImmutableRows,
physicalNames, defaultFamilyName,
             null, disableWAL, multiTenant, storeNulls, viewType, viewIndexId, indexType,
baseColumnCount, rowKeyOrderOptimizable,
-            isTransactional, updateCacheFrequency, indexDisableTimestamp,isNamespaceMpped);
+            isTransactional, updateCacheFrequency, indexDisableTimestamp, isNamespaceMpped,
null);
     }
 
     public PTableImpl(long timeStamp) { // For delete marker
@@ -227,7 +228,7 @@ public class PTableImpl implements PTable {
                 table.getSequenceNumber(), table.getPKName(), table.getBucketNum(), getColumnsToClone(table),
parentSchemaName, table.getParentTableName(),
                 indexes, table.isImmutableRows(), table.getPhysicalNames(), table.getDefaultFamilyName(),
viewStatement,
                 table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(), table.getViewType(),
table.getViewIndexId(), table.getIndexType(),
-                table.getTableStats(), table.getBaseColumnCount(), table.rowKeyOrderOptimizable(),
table.isTransactional(), table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(),
table.isNamespaceMapped());
+                table.getTableStats(), table.getBaseColumnCount(), table.rowKeyOrderOptimizable(),
table.isTransactional(), table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(),
table.isNamespaceMapped(), table.getAutoPartitionSeqName());
     }
 
     public static PTableImpl makePTable(PTable table, List<PColumn> columns) throws
SQLException {
@@ -236,7 +237,7 @@ public class PTableImpl implements PTable {
                 table.getSequenceNumber(), table.getPKName(), table.getBucketNum(), columns,
table.getParentSchemaName(), table.getParentTableName(),
                 table.getIndexes(), table.isImmutableRows(), table.getPhysicalNames(), table.getDefaultFamilyName(),
table.getViewStatement(),
                 table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(), table.getViewType(),
table.getViewIndexId(), table.getIndexType(),
-                table.getTableStats(), table.getBaseColumnCount(), table.rowKeyOrderOptimizable(),
table.isTransactional(), table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(),
table.isNamespaceMapped());
+                table.getTableStats(), table.getBaseColumnCount(), table.rowKeyOrderOptimizable(),
table.isTransactional(), table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(),
table.isNamespaceMapped(), table.getAutoPartitionSeqName());
     }
 
     public static PTableImpl makePTable(PTable table, long timeStamp, long sequenceNumber,
List<PColumn> columns) throws SQLException {
@@ -245,7 +246,7 @@ public class PTableImpl implements PTable {
                 sequenceNumber, table.getPKName(), table.getBucketNum(), columns, table.getParentSchemaName(),
table.getParentTableName(), table.getIndexes(),
                 table.isImmutableRows(), table.getPhysicalNames(), table.getDefaultFamilyName(),
table.getViewStatement(), table.isWALDisabled(),
                 table.isMultiTenant(), table.getStoreNulls(), table.getViewType(), table.getViewIndexId(),
table.getIndexType(), table.getTableStats(),
-                table.getBaseColumnCount(), table.rowKeyOrderOptimizable(), table.isTransactional(),
table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(), table.isNamespaceMapped());
+                table.getBaseColumnCount(), table.rowKeyOrderOptimizable(), table.isTransactional(),
table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(), table.isNamespaceMapped(),
table.getAutoPartitionSeqName());
     }
 
     public static PTableImpl makePTable(PTable table, long timeStamp, long sequenceNumber,
List<PColumn> columns, boolean isImmutableRows) throws SQLException {
@@ -254,7 +255,7 @@ public class PTableImpl implements PTable {
                 sequenceNumber, table.getPKName(), table.getBucketNum(), columns, table.getParentSchemaName(),
table.getParentTableName(),
                 table.getIndexes(), isImmutableRows, table.getPhysicalNames(), table.getDefaultFamilyName(),
table.getViewStatement(),
                 table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(), table.getViewType(),
table.getViewIndexId(),
-                table.getIndexType(), table.getTableStats(), table.getBaseColumnCount(),
table.rowKeyOrderOptimizable(), table.isTransactional(), table.getUpdateCacheFrequency(),
table.getIndexDisableTimestamp(), table.isNamespaceMapped());
+                table.getIndexType(), table.getTableStats(), table.getBaseColumnCount(),
table.rowKeyOrderOptimizable(), table.isTransactional(), table.getUpdateCacheFrequency(),
table.getIndexDisableTimestamp(), table.isNamespaceMapped(), table.getAutoPartitionSeqName());
     }
     
     public static PTableImpl makePTable(PTable table, long timeStamp, long sequenceNumber,
List<PColumn> columns, boolean isImmutableRows, boolean isWalDisabled,
@@ -264,7 +265,7 @@ public class PTableImpl implements PTable {
                 sequenceNumber, table.getPKName(), table.getBucketNum(), columns, table.getParentSchemaName(),
table.getParentTableName(),
                 table.getIndexes(), isImmutableRows, table.getPhysicalNames(), table.getDefaultFamilyName(),
table.getViewStatement(),
                 isWalDisabled, isMultitenant, storeNulls, table.getViewType(), table.getViewIndexId(),
table.getIndexType(), table.getTableStats(),
-                table.getBaseColumnCount(), table.rowKeyOrderOptimizable(), isTransactional,
updateCacheFrequency, table.getIndexDisableTimestamp(), isNamespaceMapped);
+                table.getBaseColumnCount(), table.rowKeyOrderOptimizable(), isTransactional,
updateCacheFrequency, table.getIndexDisableTimestamp(), isNamespaceMapped, table.getAutoPartitionSeqName());
     }
     
     public static PTableImpl makePTable(PTable table, PIndexState state) throws SQLException
{
@@ -274,7 +275,7 @@ public class PTableImpl implements PTable {
                 table.getParentSchemaName(), table.getParentTableName(), table.getIndexes(),
                 table.isImmutableRows(), table.getPhysicalNames(), table.getDefaultFamilyName(),
table.getViewStatement(),
                 table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(), table.getViewType(),
table.getViewIndexId(), table.getIndexType(),
-                table.getTableStats(), table.getBaseColumnCount(), table.rowKeyOrderOptimizable(),
table.isTransactional(), table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(),
table.isNamespaceMapped());
+                table.getTableStats(), table.getBaseColumnCount(), table.rowKeyOrderOptimizable(),
table.isTransactional(), table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(),
table.isNamespaceMapped(), table.getAutoPartitionSeqName());
     }
 
     public static PTableImpl makePTable(PTable table, boolean rowKeyOrderOptimizable) throws
SQLException {
@@ -284,7 +285,7 @@ public class PTableImpl implements PTable {
                 table.getParentSchemaName(), table.getParentTableName(), table.getIndexes(),
                 table.isImmutableRows(), table.getPhysicalNames(), table.getDefaultFamilyName(),
table.getViewStatement(),
                 table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(), table.getViewType(),
table.getViewIndexId(), table.getIndexType(), table.getTableStats(),
-                table.getBaseColumnCount(), rowKeyOrderOptimizable, table.isTransactional(),
table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(), table.isNamespaceMapped());
+                table.getBaseColumnCount(), rowKeyOrderOptimizable, table.isTransactional(),
table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(), table.isNamespaceMapped(),
table.getAutoPartitionSeqName());
     }
 
     public static PTableImpl makePTable(PTable table, PTableStats stats) throws SQLException
{
@@ -294,7 +295,7 @@ public class PTableImpl implements PTable {
                 table.getParentSchemaName(), table.getParentTableName(), table.getIndexes(),
                 table.isImmutableRows(), table.getPhysicalNames(), table.getDefaultFamilyName(),
table.getViewStatement(),
                 table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(), table.getViewType(),
table.getViewIndexId(), table.getIndexType(), stats,
-                table.getBaseColumnCount(), table.rowKeyOrderOptimizable(), table.isTransactional(),
table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(), table.isNamespaceMapped());
+                table.getBaseColumnCount(), table.rowKeyOrderOptimizable(), table.isTransactional(),
table.getUpdateCacheFrequency(), table.getIndexDisableTimestamp(), table.isNamespaceMapped(),
table.getAutoPartitionSeqName());
     }
 
     public static PTableImpl makePTable(PName tenantId, PName schemaName, PName tableName,
PTableType type,
@@ -303,12 +304,12 @@ public class PTableImpl implements PTable {
             boolean isImmutableRows, List<PName> physicalNames, PName defaultFamilyName,
String viewExpression,
             boolean disableWAL, boolean multiTenant, boolean storeNulls, ViewType viewType,
Short viewIndexId,
             IndexType indexType, boolean rowKeyOrderOptimizable, boolean isTransactional,
long updateCacheFrequency,
-            long indexDisableTimestamp, boolean isNamespaceMapped) throws SQLException {
+            long indexDisableTimestamp, boolean isNamespaceMapped, String autoPartitionSeqName)
throws SQLException {
         return new PTableImpl(tenantId, schemaName, tableName, type, state, timeStamp, sequenceNumber,
pkName, bucketNum, columns, dataSchemaName,
                 dataTableName, indexes, isImmutableRows, physicalNames, defaultFamilyName,
                 viewExpression, disableWAL, multiTenant, storeNulls, viewType, viewIndexId,
                 indexType, PTableStats.EMPTY_STATS, QueryConstants.BASE_TABLE_BASE_COLUMN_COUNT,
rowKeyOrderOptimizable, isTransactional,
-                updateCacheFrequency,indexDisableTimestamp, isNamespaceMapped);
+                updateCacheFrequency,indexDisableTimestamp, isNamespaceMapped, autoPartitionSeqName);
     }
 
     public static PTableImpl makePTable(PName tenantId, PName schemaName, PName tableName,
PTableType type,
@@ -317,12 +318,12 @@ public class PTableImpl implements PTable {
             boolean isImmutableRows, List<PName> physicalNames, PName defaultFamilyName,
String viewExpression,
             boolean disableWAL, boolean multiTenant, boolean storeNulls, ViewType viewType,
Short viewIndexId,
             IndexType indexType, boolean rowKeyOrderOptimizable, boolean isTransactional,
long updateCacheFrequency,
-            @NotNull PTableStats stats, int baseColumnCount, long indexDisableTimestamp,
boolean isNamespaceMapped)
+            @NotNull PTableStats stats, int baseColumnCount, long indexDisableTimestamp,
boolean isNamespaceMapped, String autoPartitionSeqName)
             throws SQLException {
         return new PTableImpl(tenantId, schemaName, tableName, type, state, timeStamp, sequenceNumber,
pkName,
                 bucketNum, columns, dataSchemaName, dataTableName, indexes, isImmutableRows,
physicalNames,
                 defaultFamilyName, viewExpression, disableWAL, multiTenant, storeNulls, viewType,
viewIndexId,
-                indexType, stats, baseColumnCount, rowKeyOrderOptimizable, isTransactional,
updateCacheFrequency, indexDisableTimestamp, isNamespaceMapped);
+                indexType, stats, baseColumnCount, rowKeyOrderOptimizable, isTransactional,
updateCacheFrequency, indexDisableTimestamp, isNamespaceMapped, autoPartitionSeqName);
     }
 
     private PTableImpl(PName tenantId, PName schemaName, PName tableName, PTableType type,
PIndexState state,
@@ -330,11 +331,11 @@ public class PTableImpl implements PTable {
             PName parentSchemaName, PName parentTableName, List<PTable> indexes, boolean
isImmutableRows,
             List<PName> physicalNames, PName defaultFamilyName, String viewExpression,
boolean disableWAL, boolean multiTenant,
             boolean storeNulls, ViewType viewType, Short viewIndexId, IndexType indexType,
-            PTableStats stats, int baseColumnCount, boolean rowKeyOrderOptimizable, boolean
isTransactional, long updateCacheFrequency, long indexDisableTimestamp, boolean isNamespaceMapped)
throws SQLException {
+            PTableStats stats, int baseColumnCount, boolean rowKeyOrderOptimizable, boolean
isTransactional, long updateCacheFrequency, long indexDisableTimestamp, boolean isNamespaceMapped,
String autoPartitionSeqName) throws SQLException {
         init(tenantId, schemaName, tableName, type, state, timeStamp, sequenceNumber, pkName,
bucketNum, columns,
                 stats, schemaName, parentTableName, indexes, isImmutableRows, physicalNames,
defaultFamilyName,
                 viewExpression, disableWAL, multiTenant, storeNulls, viewType, viewIndexId,
indexType, baseColumnCount, rowKeyOrderOptimizable,
-                isTransactional, updateCacheFrequency, indexDisableTimestamp, isNamespaceMapped);
+                isTransactional, updateCacheFrequency, indexDisableTimestamp, isNamespaceMapped,
autoPartitionSeqName);
     }
     
     @Override
@@ -367,7 +368,7 @@ public class PTableImpl implements PTable {
             PName pkName, Integer bucketNum, List<PColumn> columns, PTableStats stats,
PName parentSchemaName, PName parentTableName,
             List<PTable> indexes, boolean isImmutableRows, List<PName> physicalNames,
PName defaultFamilyName, String viewExpression, boolean disableWAL,
             boolean multiTenant, boolean storeNulls, ViewType viewType, Short viewIndexId,
-            IndexType indexType , int baseColumnCount, boolean rowKeyOrderOptimizable, boolean
isTransactional, long updateCacheFrequency, long indexDisableTimestamp, boolean isNamespaceMapped)
throws SQLException {
+            IndexType indexType , int baseColumnCount, boolean rowKeyOrderOptimizable, boolean
isTransactional, long updateCacheFrequency, long indexDisableTimestamp, boolean isNamespaceMapped,
String autoPartitionSeqName) throws SQLException {
         Preconditions.checkNotNull(schemaName);
         Preconditions.checkArgument(tenantId==null || tenantId.getBytes().length > 0);
// tenantId should be null or not empty
         int estimatedSize = SizedUtil.OBJECT_SIZE * 2 + 23 * SizedUtil.POINTER_SIZE + 4 *
SizedUtil.INT_SIZE + 2 * SizedUtil.LONG_SIZE + 2 * SizedUtil.INT_OBJECT_SIZE +
@@ -402,6 +403,7 @@ public class PTableImpl implements PTable {
         this.rowKeyOrderOptimizable = rowKeyOrderOptimizable;
         this.updateCacheFrequency = updateCacheFrequency;
         this.isNamespaceMapped = isNamespaceMapped;
+        this.autoPartitionSeqName = autoPartitionSeqName;
         List<PColumn> pkColumns;
         PColumn[] allColumns;
         
@@ -1104,6 +1106,10 @@ public class PTableImpl implements PTable {
       if (table.hasIsNamespaceMapped()) {
           isNamespaceMapped = table.getIsNamespaceMapped();
       }
+      String autoParititonSeqName = null;
+      if (table.hasAutoParititonSeqName()) {
+          autoParititonSeqName = table.getAutoParititonSeqName();
+      }
       
       try {
         PTableImpl result = new PTableImpl();
@@ -1111,7 +1117,7 @@ public class PTableImpl implements PTable {
             (bucketNum == NO_SALTING) ? null : bucketNum, columns, stats, schemaName,dataTableName,
indexes,
             isImmutableRows, physicalNames, defaultFamilyName, viewStatement, disableWAL,
             multiTenant, storeNulls, viewType, viewIndexId, indexType, baseColumnCount, rowKeyOrderOptimizable,
-            isTransactional, updateCacheFrequency, indexDisableTimestamp, isNamespaceMapped);
+            isTransactional, updateCacheFrequency, indexDisableTimestamp, isNamespaceMapped,
autoParititonSeqName);
         return result;
       } catch (SQLException e) {
         throw new RuntimeException(e); // Impossible
@@ -1203,6 +1209,9 @@ public class PTableImpl implements PTable {
       builder.setUpdateCacheFrequency(table.getUpdateCacheFrequency());
       builder.setIndexDisableTimestamp(table.getIndexDisableTimestamp());
       builder.setIsNamespaceMapped(table.isNamespaceMapped());
+      if (table.getAutoPartitionSeqName()!= null) {
+          builder.setAutoParititonSeqName(table.getAutoPartitionSeqName());
+      }
       return builder.build();
     }
 
@@ -1245,4 +1254,8 @@ public class PTableImpl implements PTable {
     public boolean isNamespaceMapped() {
         return isNamespaceMapped;
     }
+    
+    public String getAutoPartitionSeqName() {
+        return autoPartitionSeqName;
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ac8d0c57/phoenix-core/src/main/java/org/apache/phoenix/schema/TableProperty.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/TableProperty.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/TableProperty.java
index 5967d8b..abfc8a5 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/TableProperty.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/TableProperty.java
@@ -67,6 +67,8 @@ public enum TableProperty {
 	        return value;
 	    }	    
 	},
+	
+	AUTO_PARTITION_SEQ(PhoenixDatabaseMetaData.AUTO_PARTITION_SEQ, COLUMN_FAMILY_NOT_ALLOWED_TABLE_PROPERTY,
false, false),
     ;
 
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ac8d0c57/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java
index df37ee0..52c5c71 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java
@@ -36,6 +36,7 @@ import java.util.Set;
 
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.Cell;
+import org.apache.hadoop.hbase.HConstants;
 import org.apache.hadoop.hbase.HRegionLocation;
 import org.apache.hadoop.hbase.HTableDescriptor;
 import org.apache.hadoop.hbase.KeyValue;
@@ -45,6 +46,7 @@ import org.apache.hadoop.hbase.client.HConnectionManager;
 import org.apache.hadoop.hbase.client.Mutation;
 import org.apache.hadoop.hbase.client.Put;
 import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
 import org.apache.hadoop.hbase.protobuf.RequestConverter;
@@ -59,6 +61,7 @@ import org.apache.phoenix.hbase.index.util.VersionUtil;
 import org.apache.phoenix.jdbc.PhoenixConnection;
 import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData;
 import org.apache.phoenix.query.QueryConstants;
+import org.apache.phoenix.schema.PColumn;
 import org.apache.phoenix.schema.PName;
 import org.apache.phoenix.schema.PTable;
 import org.apache.phoenix.schema.PTable.LinkType;
@@ -242,11 +245,20 @@ public class MetaDataUtil {
      * Returns the first Put element in <code>tableMetaData</code>. There could
be leading Delete elements before the
      * table header row
      */
-    public static Mutation getPutOnlyTableHeaderRow(List<Mutation> tableMetaData) {
+    public static Put getPutOnlyTableHeaderRow(List<Mutation> tableMetaData) {
         for (Mutation m : tableMetaData) {
-            if (m instanceof Put) { return m; }
+            if (m instanceof Put) { return (Put) m; }
         }
-        throw new IllegalStateException("No table header row found in table meatadata");
+        throw new IllegalStateException("No table header row found in table metadata");
+    }
+    
+    public static Put getPutOnlyAutoPartitionColumn(PTable parentTable, List<Mutation>
tableMetaData) {
+        int autoPartitionPutIndex = parentTable.isMultiTenant() ? 2: 1;
+        int i=0;
+        for (Mutation m : tableMetaData) {
+            if (m instanceof Put && i++==autoPartitionPutIndex) { return (Put) m;
}
+        }
+        throw new IllegalStateException("No auto partition column row found in table metadata");
     }
 
     public static Mutation getParentTableHeaderRow(List<Mutation> tableMetaData) {
@@ -519,4 +531,28 @@ public class MetaDataUtil {
         }
         return viewNames;
     }
+
+    public static String getAutoPartitionColumnName(PTable parentTable) {
+        List<PColumn> parentTableColumns = parentTable.getColumns();
+        PColumn column = parentTableColumns.get(getAutoPartitionColIndex(parentTable));
+        return column.getName().getString();
+    }
+
+    // this method should only be called on the parent table (since it has the _SALT column)
+    public static int getAutoPartitionColIndex(PTable parentTable) {
+        boolean isMultiTenant = parentTable.isMultiTenant();
+        boolean isSalted = parentTable.getBucketNum()!=null;
+        return (isMultiTenant && isSalted) ? 2 : (isMultiTenant || isSalted) ? 1
: 0;
+    }
+
+    public static String getJdbcUrl(RegionCoprocessorEnvironment env) {
+        String zkQuorum = env.getConfiguration().get(HConstants.ZOOKEEPER_QUORUM);
+        String zkClientPort = env.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT,
+            Integer.toString(HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT));
+        String zkParentNode = env.getConfiguration().get(HConstants.ZOOKEEPER_ZNODE_PARENT,
+            HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
+        return PhoenixRuntime.JDBC_PROTOCOL + PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR + zkQuorum
+            + PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR + zkClientPort
+            + PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR + zkParentNode;
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ac8d0c57/phoenix-core/src/main/java/org/apache/phoenix/util/QueryUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/QueryUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/QueryUtil.java
index a2b598c..48259e0 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/QueryUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/QueryUtil.java
@@ -87,6 +87,7 @@ public final class QueryUtil {
     private static final String SELECT = "SELECT";
     private static final String FROM = "FROM";
     private static final String WHERE = "WHERE";
+    private static final String AND = "AND";
     private static final String[] CompareOpString = new String[CompareOp.values().length];
 
     static {
@@ -400,5 +401,9 @@ public final class QueryUtil {
         }
         return null;
     }
- 
+    
+    public static String getViewPartitionClause(String partitionColumnName, long autoPartitionNum)
{
+        return partitionColumnName  + " " + toSQL(CompareOp.EQUAL) + " " + autoPartitionNum;
+    }
+    
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ac8d0c57/phoenix-core/src/test/java/org/apache/phoenix/execute/CorrelatePlanTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/execute/CorrelatePlanTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/execute/CorrelatePlanTest.java
index 4660e38..aaf158a 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/execute/CorrelatePlanTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/execute/CorrelatePlanTest.java
@@ -256,7 +256,7 @@ public class CorrelatePlanTest {
                     PTableType.SUBQUERY, null, MetaDataProtocol.MIN_TABLE_TIMESTAMP, PTable.INITIAL_SEQ_NUM,
                     null, null, columns, null, null, Collections.<PTable>emptyList(),
                     false, Collections.<PName>emptyList(), null, null, false, false,
false, null,
-                    null, null, true, false, 0, 0L, Boolean.FALSE);
+                    null, null, true, false, 0, 0L, Boolean.FALSE, null);
             TableRef sourceTable = new TableRef(pTable);
             List<ColumnRef> sourceColumnRefs = Lists.<ColumnRef> newArrayList();
             for (PColumn column : sourceTable.getTable().getColumns()) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ac8d0c57/phoenix-core/src/test/java/org/apache/phoenix/execute/LiteralResultIteratorPlanTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/execute/LiteralResultIteratorPlanTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/execute/LiteralResultIteratorPlanTest.java
index aba4be4..04ed6b4 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/execute/LiteralResultIteratorPlanTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/execute/LiteralResultIteratorPlanTest.java
@@ -177,7 +177,7 @@ public class LiteralResultIteratorPlanTest {
             PTable pTable = PTableImpl.makePTable(null, PName.EMPTY_NAME, PName.EMPTY_NAME,
PTableType.SUBQUERY, null,
                     MetaDataProtocol.MIN_TABLE_TIMESTAMP, PTable.INITIAL_SEQ_NUM, null, null,
columns, null, null,
                     Collections.<PTable> emptyList(), false, Collections.<PName>
emptyList(), null, null, false, false,
-                    false, null, null, null, true, false, 0, 0L, false);
+                    false, null, null, null, true, false, 0, 0L, false, null);
             TableRef sourceTable = new TableRef(pTable);
             List<ColumnRef> sourceColumnRefs = Lists.<ColumnRef> newArrayList();
             for (PColumn column : sourceTable.getTable().getColumns()) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ac8d0c57/phoenix-protocol/src/main/MetaDataService.proto
----------------------------------------------------------------------
diff --git a/phoenix-protocol/src/main/MetaDataService.proto b/phoenix-protocol/src/main/MetaDataService.proto
index 9d7b209..34ee5e1 100644
--- a/phoenix-protocol/src/main/MetaDataService.proto
+++ b/phoenix-protocol/src/main/MetaDataService.proto
@@ -49,6 +49,8 @@ enum MutationCode {
   SCHEMA_NOT_IN_REGION = 17;
   TABLES_EXIST_ON_SCHEMA = 18;
   UNALLOWED_SCHEMA_MUTATION = 19;
+  AUTO_PARTITION_SEQUENCE_NOT_FOUND = 20;
+  CANNOT_COERCE_AUTO_PARTITION_ID = 21;
 };
 
 message SharedTableState {
@@ -71,6 +73,7 @@ message MetaDataResponse {
   repeated PFunction function = 8;
   repeated SharedTableState sharedTablesToDelete = 9;
   optional PSchema schema = 10;
+  optional int64 autoPartitionNum = 11;
 }
 
 message GetTableRequest {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ac8d0c57/phoenix-protocol/src/main/PTable.proto
----------------------------------------------------------------------
diff --git a/phoenix-protocol/src/main/PTable.proto b/phoenix-protocol/src/main/PTable.proto
index c86f6b6..9521fba 100644
--- a/phoenix-protocol/src/main/PTable.proto
+++ b/phoenix-protocol/src/main/PTable.proto
@@ -91,4 +91,5 @@ message PTable {
   optional int64 updateCacheFrequency = 28;
   optional int64 indexDisableTimestamp = 29;
   optional bool isNamespaceMapped = 30;
+  optional string autoParititonSeqName = 31;
 }


Mime
View raw message