Repository: phoenix Updated Branches: refs/heads/encodecolumns 3e7aa0065 -> 29dbf051c
Fix test failures Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/29dbf051 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/29dbf051 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/29dbf051 Branch: refs/heads/encodecolumns Commit: 29dbf051c87eb7b0c7d6e3e6b1d899745ecc75ae Parents: 3e7aa00 Author: Samarth <[email protected]> Authored: Thu Sep 22 18:02:43 2016 -0700 Committer: Samarth <[email protected]> Committed: Thu Sep 22 18:02:43 2016 -0700 ---------------------------------------------------------------------- .../apache/phoenix/end2end/AlterTableIT.java | 47 +++++++++++---- .../coprocessor/MetaDataEndpointImpl.java | 38 ++++++++----- .../apache/phoenix/schema/DelegateTable.java | 4 +- .../apache/phoenix/schema/MetaDataClient.java | 22 ++----- .../java/org/apache/phoenix/schema/PTable.java | 9 +-- .../org/apache/phoenix/schema/PTableImpl.java | 60 +++++++++++--------- .../apache/phoenix/util/EncodedColumnsUtil.java | 4 +- 7 files changed, 108 insertions(+), 76 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/29dbf051/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java index d4de783..223f368 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java @@ -2305,20 +2305,34 @@ public class AlterTableIT extends BaseOwnClusterHBaseManagedTimeIT { // assert that the client side cache is updated. baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName)); EncodedCQCounter encodedCqCounter = baseTable.getEncodedCQCounter(); - assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 3), encodedCqCounter.getValue(DEFAULT_COLUMN_FAMILY)); + assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 2), encodedCqCounter.getValue(DEFAULT_COLUMN_FAMILY)); + assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 1), encodedCqCounter.getValue("B")); // assert that the server side metadata is updated correctly. - assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3, true); + assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2, true); + assertEncodedCQCounter("B", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1, true); + + // assert that the server side metadata for columns is updated correctly. assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "COL4", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE); assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "COL5", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); - assertEncodedCQValue("B", "COL6", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2); + assertEncodedCQValue("B", "COL6", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE); assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber + 1); // Create a view String viewDDL = "CREATE VIEW " + fullViewName + " ( VIEW_COL1 INTEGER, A.VIEW_COL2 VARCHAR ) AS SELECT * FROM " + fullTableName; conn.createStatement().execute(viewDDL); + + // assert that the server side metadata is updated correctly. + assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3, true); + assertEncodedCQCounter("A", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1, true); + assertEncodedCQCounter("B", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1, true); + + // assert that the server side metadata for columns is updated correctly. + assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2); + assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE); // Creating a view that adds its own columns should increment the base table's sequence number too. assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber + 2); + // Add column to the view viewDDL = "ALTER VIEW " + fullViewName + " ADD VIEW_COL3 DECIMAL(10, 2), A.VIEW_COL4 VARCHAR, B.VIEW_COL5 INTEGER"; @@ -2327,7 +2341,9 @@ public class AlterTableIT extends BaseOwnClusterHBaseManagedTimeIT { // assert that the client cache for the base table is updated baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName)); encodedCqCounter = baseTable.getEncodedCQCounter(); - assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 8), encodedCqCounter.getValue(DEFAULT_COLUMN_FAMILY)); + assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 4), encodedCqCounter.getValue(DEFAULT_COLUMN_FAMILY)); + assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 2), encodedCqCounter.getValue("A")); + assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 2), encodedCqCounter.getValue("B")); // assert client cache for view PTable view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName)); @@ -2335,10 +2351,14 @@ public class AlterTableIT extends BaseOwnClusterHBaseManagedTimeIT { assertNull("A view should always have the column qualifier counter as null", view.getEncodedCQCounter().getValue(DEFAULT_COLUMN_FAMILY)); // assert that the server side metadata for the base table and the view is also updated correctly. - assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 8, true); - assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL3", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 5); - assertEncodedCQValue("A", "VIEW_COL4", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 6); - assertEncodedCQValue("B", "VIEW_COL5", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 7); + assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 4, true); + assertEncodedCQCounter("A", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2, true); + assertEncodedCQCounter("B", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2, true); + assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2); + assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL3", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3); + assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE); + assertEncodedCQValue("A", "VIEW_COL4", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); + assertEncodedCQValue("B", "VIEW_COL5", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); // Adding a column to the should increment the base table's sequence number too since we update the cq counters for column families. assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber + 3); assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM + 1); @@ -2350,7 +2370,9 @@ public class AlterTableIT extends BaseOwnClusterHBaseManagedTimeIT { // assert that the client cache for the base table is updated encodedCqCounter = baseTable.getEncodedCQCounter(); - assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 10), encodedCqCounter.getValue(DEFAULT_COLUMN_FAMILY)); + assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 5), encodedCqCounter.getValue(DEFAULT_COLUMN_FAMILY)); + assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 3), encodedCqCounter.getValue("A")); + assertEquals((Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 2), encodedCqCounter.getValue("B")); // assert client cache for view view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName)); @@ -2358,9 +2380,10 @@ public class AlterTableIT extends BaseOwnClusterHBaseManagedTimeIT { assertNull("A view should always have the column qualifier counter as null", view.getEncodedCQCounter().getValue(DEFAULT_COLUMN_FAMILY)); // assert that the server side metadata for the base table and the view is also updated correctly. - assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, (ENCODED_CQ_COUNTER_INITIAL_VALUE + 10), true); - assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "COL10", schemaName, viewName, (ENCODED_CQ_COUNTER_INITIAL_VALUE + 8)); - assertEncodedCQValue("A", "COL11", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 9); + assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, (ENCODED_CQ_COUNTER_INITIAL_VALUE + 5), true); + assertEncodedCQCounter("A", schemaName, baseTableName, (ENCODED_CQ_COUNTER_INITIAL_VALUE + 3), true); + assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "COL10", schemaName, viewName, (ENCODED_CQ_COUNTER_INITIAL_VALUE + 4)); + assertEncodedCQValue("A", "COL11", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2); assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber + 4); assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM + 2); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/29dbf051/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java index d8372ce..47865b6 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java @@ -27,6 +27,7 @@ import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.CLASS_NAME_BYTES; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_COUNT_BYTES; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_DEF_BYTES; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_NAME_INDEX; +import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_QUALIFIER_COUNTER_BYTES; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_SIZE_BYTES; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.DATA_TABLE_NAME_BYTES; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.DATA_TYPE_BYTES; @@ -375,6 +376,7 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso static { Collections.sort(COLUMN_KV_COLUMNS, KeyValue.COMPARATOR); } + private static final KeyValue QUALIFIER_COUNTER_KV = KeyValue.createFirstOnRow(ByteUtil.EMPTY_BYTE_ARRAY, TABLE_FAMILY_BYTES, COLUMN_QUALIFIER_COUNTER_BYTES); private static final int DECIMAL_DIGITS_INDEX = COLUMN_KV_COLUMNS.indexOf(DECIMAL_DIGITS_KV); private static final int COLUMN_SIZE_INDEX = COLUMN_KV_COLUMNS.indexOf(COLUMN_SIZE_KV); private static final int NULLABLE_INDEX = COLUMN_KV_COLUMNS.indexOf(NULLABLE_KV); @@ -654,7 +656,7 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso } private void addColumnToTable(List<Cell> results, PName colName, PName famName, - Cell[] colKeyValues, List<PColumn> columns, boolean isSalted, EncodedCQCounter cqCounter) { + Cell[] colKeyValues, List<PColumn> columns, boolean isSalted) { int i = 0; int j = 0; while (i < results.size() && j < COLUMN_KV_COLUMNS.size()) { @@ -728,9 +730,6 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso Cell columnQualifierKV = colKeyValues[ENCODED_COLUMN_QUALIFIER_INDEX]; Integer columnQualifier = columnQualifierKV == null ? null : PInteger.INSTANCE.getCodec().decodeInt(columnQualifierKV.getValueArray(), columnQualifierKV.getValueOffset(), SortOrder.getDefault()); - if (columnQualifier != null) { - cqCounter.increment(famName.getString()); - } PColumn column = new PColumnImpl(colName, famName, dataType, maxLength, scale, isNullable, position-1, sortOrder, arraySize, viewConstant, isViewReferenced, expressionStr, isRowTimestamp, false, columnQualifier); columns.add(column); } @@ -962,15 +961,20 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso PName colName = newPName(colKv.getRowArray(), colKv.getRowOffset() + offset, colKeyLength-offset); int colKeyOffset = offset + colName.getBytes().length + 1; PName famName = newPName(colKv.getRowArray(), colKv.getRowOffset() + colKeyOffset, colKeyLength-colKeyOffset); - if (colName.getString().isEmpty() && famName != null) { - LinkType linkType = LinkType.fromSerializedValue(colKv.getValueArray()[colKv.getValueOffset()]); - if (linkType == LinkType.INDEX_TABLE) { - addIndexToTable(tenantId, schemaName, famName, tableName, clientTimeStamp, indexes); - } else if (linkType == LinkType.PHYSICAL_TABLE) { - physicalTables.add(famName); - } + if (isQualifierCounterKV(colKv)) { + Integer value = PInteger.INSTANCE.getCodec().decodeInt(colKv.getValueArray(), colKv.getValueOffset(), SortOrder.ASC); + cqCounter.setValue(famName.getString(), value); } else { - addColumnToTable(results, colName, famName, colKeyValues, columns, saltBucketNum != null, cqCounter); + if (colName.getString().isEmpty() && famName != null) { + LinkType linkType = LinkType.fromSerializedValue(colKv.getValueArray()[colKv.getValueOffset()]); + if (linkType == LinkType.INDEX_TABLE) { + addIndexToTable(tenantId, schemaName, famName, tableName, clientTimeStamp, indexes); + } else if (linkType == LinkType.PHYSICAL_TABLE) { + physicalTables.add(famName); + } + } else { + addColumnToTable(results, colName, famName, colKeyValues, columns, saltBucketNum != null); + } } } } @@ -983,7 +987,15 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso rowKeyOrderOptimizable, transactional, updateCacheFrequency, baseColumnCount, indexDisableTimestamp, isNamespaceMapped, autoPartitionSeq, isAppendOnlySchema, storageScheme, cqCounter); } - + + private boolean isQualifierCounterKV(Cell kv) { + int cmp = + Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), + kv.getQualifierLength(), QUALIFIER_COUNTER_KV.getQualifierArray(), + QUALIFIER_COUNTER_KV.getQualifierOffset(), QUALIFIER_COUNTER_KV.getQualifierLength()); + return cmp == 0; + } + private PSchema getSchema(RegionScanner scanner, long clientTimeStamp) throws IOException, SQLException { List<Cell> results = Lists.newArrayList(); scanner.next(results); http://git-wip-us.apache.org/repos/asf/phoenix/blob/29dbf051/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateTable.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateTable.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateTable.java index c7547c3..9874866 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateTable.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/DelegateTable.java @@ -287,8 +287,8 @@ public class DelegateTable implements PTable { } @Override - public PColumn getPColumnForColumnQualifier(byte[] cq) throws ColumnNotFoundException, AmbiguousColumnException { - return delegate.getPColumnForColumnQualifier(cq); + public PColumn getPColumnForColumnQualifier(byte[] cf, byte[] cq) throws ColumnNotFoundException, AmbiguousColumnException { + return delegate.getPColumnForColumnQualifier(cf, cq); } @Override http://git-wip-us.apache.org/repos/asf/phoenix/blob/29dbf051/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 3eca340..5f160cc 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 @@ -89,10 +89,10 @@ import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.VIEW_STATEMENT; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.VIEW_TYPE; import static org.apache.phoenix.query.QueryConstants.BASE_TABLE_BASE_COLUMN_COUNT; import static org.apache.phoenix.query.QueryConstants.DEFAULT_COLUMN_FAMILY; -import static org.apache.phoenix.query.QueryConstants.ENCODED_CQ_COUNTER_INITIAL_VALUE; import static org.apache.phoenix.query.QueryServices.DROP_METADATA_ATTRIB; import static org.apache.phoenix.query.QueryServicesOptions.DEFAULT_DROP_METADATA; import static org.apache.phoenix.query.QueryServicesOptions.DEFAULT_RUN_UPDATE_STATS_ASYNC; +import static org.apache.phoenix.schema.PTable.EncodedCQCounter.NULL_COUNTER; import static org.apache.phoenix.schema.PTable.ViewType.MAPPED; import static org.apache.phoenix.schema.PTableType.TABLE; import static org.apache.phoenix.schema.PTableType.VIEW; @@ -232,8 +232,6 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.common.primitives.Ints; -import static org.apache.phoenix.schema.PTable.EncodedCQCounter.NULL_COUNTER; - public class MetaDataClient { private static final Logger logger = LoggerFactory.getLogger(MetaDataClient.class); @@ -729,14 +727,9 @@ public class MetaDataClient { Set<ColumnReference> indexColRefs = indexMaintainer.getIndexedColumns(); for (ColumnReference colRef : indexColRefs) { try { - byte[] cf= colRef.getFamily(); - byte[] cq= colRef.getQualifier(); - if (cf!=null) { - view.getColumnFamily(cf).getPColumnForColumnQualifier(cq); - } - else { - view.getPColumnForColumnQualifier(cq); - } + byte[] cf = colRef.getFamily(); + byte[] cq = colRef.getQualifier(); + view.getPColumnForColumnQualifier(cf, cq); } catch (ColumnNotFoundException e) { // Ignore this index and continue with others containsAllReqdCols = false; break; @@ -2035,7 +2028,7 @@ public class MetaDataClient { * the same HTable. Views always use the base table's column qualifier counter for doling out * encoded column qualifier. */ - viewPhysicalTable = connection.getTable(new PTableKey(null, physicalNames.get(0).getString())); + viewPhysicalTable = PhoenixRuntime.getTable(connection, physicalNames.get(0).getString()); storageScheme = viewPhysicalTable.getStorageScheme(); if (EncodedColumnsUtil.usesEncodedColumnNames(viewPhysicalTable)) { cqCounter = viewPhysicalTable.getEncodedCQCounter(); @@ -3130,11 +3123,8 @@ public class MetaDataClient { List<PColumn> columns = Lists.newArrayListWithExpectedSize(numCols); Set<String> colFamiliesForPColumnsToBeAdded = new LinkedHashSet<>(); Set<String> families = new LinkedHashSet<>(); - //FIXME: samarth change this to fetch table from server if client cache doesn't have it. What about local indexes? - //FIXME: samarth fix this mess of getting table names from connection - PTable tableForCQCounters = tableType == PTableType.VIEW ? connection.getTable(new PTableKey(null, table.getPhysicalName().getString())) : table;; + PTable tableForCQCounters = tableType == PTableType.VIEW ? PhoenixRuntime.getTable(connection, table.getPhysicalName().getString()) : table;; EncodedCQCounter cqCounterToUse = tableForCQCounters.getEncodedCQCounter(); - StorageScheme storageScheme = table.getStorageScheme(); Map<String, Integer> changedCqCounters = new HashMap<>(numCols); if (numCols > 0 ) { //TODO: samarth should these be guarded by storage scheme check. Better to have the map always available. immutable empty for views and non encoded. http://git-wip-us.apache.org/repos/asf/phoenix/blob/29dbf051/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 b9f3eaf..ee8a268 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 @@ -31,9 +31,6 @@ import org.apache.hadoop.hbase.util.Bytes; import org.apache.phoenix.hbase.index.util.KeyValueBuilder; import org.apache.phoenix.index.IndexMaintainer; import org.apache.phoenix.jdbc.PhoenixConnection; -import org.apache.phoenix.query.QueryConstants; - -import com.google.common.collect.ImmutableMap; /** @@ -256,7 +253,7 @@ public interface PTable extends PMetaDataEntity { * @throws ColumnNotFoundException if no column with the given column qualifier can be found * @throws AmbiguousColumnException if multiple columns are found with the given column qualifier */ - PColumn getPColumnForColumnQualifier(byte[] cq) throws ColumnNotFoundException, AmbiguousColumnException; + PColumn getPColumnForColumnQualifier(byte[] cf, byte[] cq) throws ColumnNotFoundException, AmbiguousColumnException; /** * Get the PK column with the given name. @@ -437,6 +434,10 @@ public interface PTable extends PMetaDataEntity { return counter; } + public void setValue(String columnFamily, Integer value) { + familyCounters.put(columnFamily, value); + } + /** * * @param columnFamily http://git-wip-us.apache.org/repos/asf/phoenix/blob/29dbf051/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 fee39ce..63c1c59 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 @@ -110,7 +110,7 @@ public class PTableImpl implements PTable { private Map<byte[], PColumnFamily> familyByBytes; private Map<String, PColumnFamily> familyByString; private ListMultimap<String, PColumn> columnsByName; - private ListMultimap<Integer, PColumn> kvColumnsByEncodedColumnNames; + private ListMultimap<Pair<String, Integer>, PColumn> kvColumnsByEncodedColumnNames; private PName pkName; private Integer bucketNum; private RowKeySchema rowKeySchema; @@ -427,7 +427,7 @@ public class PTableImpl implements PTable { PColumn[] allColumns; this.columnsByName = ArrayListMultimap.create(columns.size(), 1); - this.kvColumnsByEncodedColumnNames = (EncodedColumnsUtil.usesEncodedColumnNames(storageScheme) ? ArrayListMultimap.<Integer, PColumn>create(columns.size(), 1) : null); + this.kvColumnsByEncodedColumnNames = (EncodedColumnsUtil.usesEncodedColumnNames(storageScheme) ? ArrayListMultimap.<Pair<String, Integer>, PColumn>create(columns.size(), 1) : null); int numPKColumns = 0; if (bucketNum != null) { // Add salt column to allColumns and pkColumns, but don't add to @@ -459,16 +459,20 @@ public class PTableImpl implements PTable { } } } - Integer cq = column.getEncodedColumnQualifier(); //TODO: samarth understand the implication of this. - if (kvColumnsByEncodedColumnNames != null && cq != null) { - if (kvColumnsByEncodedColumnNames.put(cq, column)) { - int count = 0; - for (PColumn dupColumn : kvColumnsByEncodedColumnNames.get(cq)) { - if (Objects.equal(familyName, dupColumn.getFamilyName())) { - count++; - if (count > 1) { - throw new ColumnAlreadyExistsException(schemaName.getString(), name.getString(), columnName); + if (kvColumnsByEncodedColumnNames != null) { + Integer cq = column.getEncodedColumnQualifier(); + String cf = column.getFamilyName() != null ? column.getFamilyName().getString() : null; + if (cf != null && cq != null) { + Pair<String, Integer> pair = new Pair<>(cf, cq); + if (kvColumnsByEncodedColumnNames.put(pair, column)) { + int count = 0; + for (PColumn dupColumn : kvColumnsByEncodedColumnNames.get(pair)) { + if (Objects.equal(familyName, dupColumn.getFamilyName())) { + count++; + if (count > 1) { + throw new ColumnAlreadyExistsException(schemaName.getString(), name.getString(), columnName); + } } } } @@ -740,31 +744,33 @@ public class PTableImpl implements PTable { } @Override - public PColumn getPColumnForColumnQualifier(byte[] cq) throws ColumnNotFoundException, AmbiguousColumnException { + public PColumn getPColumnForColumnQualifier(byte[] cf, byte[] cq) throws ColumnNotFoundException, AmbiguousColumnException { Preconditions.checkNotNull(cq); - if (!EncodedColumnsUtil.usesEncodedColumnNames(this)) { + if (!EncodedColumnsUtil.usesEncodedColumnNames(this) || cf == null) { String columnName = (String)PVarchar.INSTANCE.toObject(cq); return getPColumnForColumnName(columnName); } else { Integer qualifier = (Integer)PInteger.INSTANCE.toObject(cq); - List<PColumn> columns = kvColumnsByEncodedColumnNames.get(qualifier); + String family = (String)PVarchar.INSTANCE.toObject(cf); + List<PColumn> columns = kvColumnsByEncodedColumnNames.get(new Pair<>(family, qualifier)); int size = columns.size(); if (size == 0) { //TODO: samarth should we have a column qualifier not found exception? throw new ColumnNotFoundException(Bytes.toString(cq)); } - if (size > 1) { - for (PColumn column : columns) { - if (column.getFamilyName() == null || QueryConstants.DEFAULT_COLUMN_FAMILY.equals(column.getFamilyName().getString())) { - // Allow ambiguity with PK column or column in the default column family, - // since a PK column cannot be prefixed and a user would not know how to - // prefix a column in the default column family. - return column; - } - } - //TODO: samarth should we have a column qualifier not found exception? - throw new AmbiguousColumnException(columns.get(0).getName().getString()); - } + //TODO: samarth I am not convinced if need this logic. +// if (size > 1) { +// for (PColumn column : columns) { +// if (QueryConstants.DEFAULT_COLUMN_FAMILY.equals(column.getFamilyName().getString())) { +// // Allow ambiguity with PK column or column in the default column family, +// // since a PK column cannot be prefixed and a user would not know how to +// // prefix a column in the default column family. +// return column; +// } +// } +// //TODO: samarth should we have a column qualifier not found exception? +// throw new AmbiguousColumnException(columns.get(0).getName().getString()); +// } return columns.get(0); } } @@ -1224,7 +1230,7 @@ public class PTableImpl implements PTable { if (table.getEncodedCQCountersList() != null) { encodedColumnQualifierCounter = new EncodedCQCounter(); for (org.apache.phoenix.coprocessor.generated.PTableProtos.EncodedCQCounter cqCounterFromProto : table.getEncodedCQCountersList()) { - encodedColumnQualifierCounter.increment(cqCounterFromProto.getColFamily()); + encodedColumnQualifierCounter.setValue(cqCounterFromProto.getColFamily(), cqCounterFromProto.getCounter()); } } http://git-wip-us.apache.org/repos/asf/phoenix/blob/29dbf051/phoenix-core/src/main/java/org/apache/phoenix/util/EncodedColumnsUtil.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/EncodedColumnsUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/EncodedColumnsUtil.java index 43f5801..48ec277 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/util/EncodedColumnsUtil.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/util/EncodedColumnsUtil.java @@ -33,8 +33,8 @@ public class EncodedColumnsUtil { return usesEncodedColumnNames(table.getStorageScheme()); } - public static boolean usesEncodedColumnNames(StorageScheme storageSchema) { - return storageSchema != null && storageSchema != StorageScheme.NON_ENCODED_COLUMN_NAMES; + public static boolean usesEncodedColumnNames(StorageScheme storageScheme) { + return storageScheme != null && storageScheme != StorageScheme.NON_ENCODED_COLUMN_NAMES; } public static byte[] getEncodedColumnQualifier(PColumn column) {
