Java Code Examples for org.apache.phoenix.schema.PTable#getType()

The following examples show how to use org.apache.phoenix.schema.PTable#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Deprecated
private static List<PColumn> getPkColumns(PTable ptable, Connection conn, boolean forDataTable) throws SQLException {
    PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
    List<PColumn> pkColumns = ptable.getPKColumns();
    
    // Skip the salting column and the view index id column if present.
    // Skip the tenant id column too if the connection is tenant specific and the table used by the query plan is multi-tenant
    int offset = (ptable.getBucketNum() == null ? 0 : 1) + (ptable.isMultiTenant() && pConn.getTenantId() != null ? 1 : 0) + (ptable.getViewIndexId() == null ? 0 : 1);
    
    // get a sublist of pkColumns by skipping the offset columns.
    pkColumns = pkColumns.subList(offset, pkColumns.size());
    
    if (ptable.getType() == PTableType.INDEX && forDataTable) {
        // index tables have the same schema name as their parent/data tables.
        String fullDataTableName = ptable.getParentName().getString();
        
        // Get the corresponding columns of the data table.
        List<PColumn> dataColumns = IndexUtil.getDataColumns(fullDataTableName, pkColumns, pConn);
        pkColumns = dataColumns;
    }
    return pkColumns;
}
 
Example 2
Source File: IndexUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static byte[][] getViewConstants(PTable dataTable) {
    if (dataTable.getType() != PTableType.VIEW && dataTable.getType() != PTableType.PROJECTED) return null;
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    List<byte[]> viewConstants = new ArrayList<byte[]>();
    List<PColumn> dataPkColumns = dataTable.getPKColumns();
    for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
        PColumn dataPKColumn = dataPkColumns.get(i);
        if (dataPKColumn.getViewConstant() != null) {
            if (IndexUtil.getViewConstantValue(dataPKColumn, ptr)) {
                viewConstants.add(ByteUtil.copyKeyBytesIfNecessary(ptr));
            } else {
                throw new IllegalStateException();
            }
        }
    }
    return viewConstants.isEmpty() ? null : viewConstants
            .toArray(new byte[viewConstants.size()][]);
}
 
Example 3
Source File: IndexScrutinyMapper.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static String getSourceTableName(PTable pSourceTable, boolean isNamespaceEnabled) {
    String sourcePhysicalName = pSourceTable.getPhysicalName().getString();
    String physicalTable, table, schema;
    if (pSourceTable.getType() == PTableType.VIEW
            || MetaDataUtil.isViewIndex(sourcePhysicalName)) {
        // in case of view and view index ptable, getPhysicalName() returns hbase tables
        // i.e. without _IDX_ and with _IDX_ respectively
        physicalTable = sourcePhysicalName;
    } else {
        table = pSourceTable.getTableName().toString();
        schema = pSourceTable.getSchemaName().toString();
        physicalTable = SchemaUtil
                .getPhysicalHBaseTableName(schema, table, isNamespaceEnabled).toString();
    }
    return physicalTable;
}
 
Example 4
Source File: ExpressionUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * <pre>
 * Infer OrderBys from the rowkey columns of {@link PTable},for projected table may be there is no rowkey columns,
 * so we should move forward to inspect {@link ProjectedColumn} by {@link #getOrderByFromProjectedTable}.
 * The second part of the return pair is the rowkey column offset we must skip when we create OrderBys, because for table with salted/multiTenant/viewIndexId,
 * some leading rowkey columns should be skipped.
 * </pre>
 * @param tableRef
 * @param phoenixConnection
 * @param orderByReverse
 * @return
 * @throws SQLException
 */
public static Pair<OrderBy,Integer> getOrderByFromTable(
        TableRef tableRef,
        PhoenixConnection phoenixConnection,
        boolean orderByReverse) throws SQLException {

    PTable table = tableRef.getTable();
    Pair<OrderBy,Integer> orderByAndRowKeyColumnOffset =
            getOrderByFromTableByRowKeyColumn(table, phoenixConnection, orderByReverse);
    if(orderByAndRowKeyColumnOffset.getFirst() != OrderBy.EMPTY_ORDER_BY) {
        return orderByAndRowKeyColumnOffset;
    }
    if(table.getType() == PTableType.PROJECTED) {
        orderByAndRowKeyColumnOffset =
                getOrderByFromProjectedTable(tableRef, phoenixConnection, orderByReverse);
        if(orderByAndRowKeyColumnOffset.getFirst() != OrderBy.EMPTY_ORDER_BY) {
            return orderByAndRowKeyColumnOffset;
        }
    }
    return new Pair<OrderBy,Integer>(OrderBy.EMPTY_ORDER_BY, 0);
}
 
Example 5
Source File: TephraTransactionContext.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public void markDMLFence(PTable table) {
    if (table.getType() == PTableType.INDEX) {
        return;
    }
    
    byte[] logicalKey = table.getName().getBytes();
    TransactionAware logicalTxAware = VisibilityFence.create(logicalKey);

    if (this.txContext == null) {
        this.txAwares.add(logicalTxAware);
    } else {
        this.txContext.addTransactionAware(logicalTxAware);
    }

    byte[] physicalKey = table.getPhysicalName().getBytes();
    if (Bytes.compareTo(physicalKey, logicalKey) != 0) {
        TransactionAware physicalTxAware = VisibilityFence
                .create(physicalKey);
        if (this.txContext == null) {
            this.txAwares.add(physicalTxAware);
        } else {
            this.txContext.addTransactionAware(physicalTxAware);
        }
    }
}
 
Example 6
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static List<PColumn> getPkColumns(PTable ptable, Connection conn, boolean forDataTable) throws SQLException {
    PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
    List<PColumn> pkColumns = ptable.getPKColumns();
    
    // Skip the salting column and the view index id column if present.
    // Skip the tenant id column too if the connection is tenant specific and the table used by the query plan is multi-tenant
    int offset = (ptable.getBucketNum() == null ? 0 : 1) + (ptable.isMultiTenant() && pConn.getTenantId() != null ? 1 : 0) + (ptable.getViewIndexId() == null ? 0 : 1);
    
    // get a sublist of pkColumns by skipping the offset columns.
    pkColumns = pkColumns.subList(offset, pkColumns.size());
    
    if (ptable.getType() == PTableType.INDEX && forDataTable) {
        // index tables have the same schema name as their parent/data tables.
        String fullDataTableName = ptable.getParentName().getString();
        
        // Get the corresponding columns of the data table.
        List<PColumn> dataColumns = IndexUtil.getDataColumns(fullDataTableName, pkColumns, pConn);
        pkColumns = dataColumns;
    }
    return pkColumns;
}
 
Example 7
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static PColumn getColumn(PTable table, @Nullable String familyName, String columnName) throws SQLException {
    if (table==null) {
        throw new SQLException("Table must not be null.");
    }
    if (columnName==null) {
        throw new SQLException("columnName must not be null.");
    }
    // normalize and remove quotes from family and column names before looking up.
    familyName = SchemaUtil.normalizeIdentifier(familyName);
    columnName = SchemaUtil.normalizeIdentifier(columnName);
    // Column names are always for the data table, so we must translate them if
    // we're dealing with an index table.
    if (table.getType() == PTableType.INDEX) {
        columnName = IndexUtil.getIndexColumnName(familyName, columnName);
    }
    PColumn pColumn = null;
    if (familyName != null) {
        PColumnFamily family = table.getColumnFamily(familyName);
        pColumn = family.getPColumnForColumnName(columnName);
    } else {
        pColumn = table.getColumnForColumnName(columnName);
    }
    return pColumn;
}
 
Example 8
Source File: ViewMetadataIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void validateCols(PTable table) {
    final String prefix = table.getType() == PTableType.INDEX ? "0:" : "";
    Predicate<PColumn> predicate = new Predicate<PColumn>() {
        @Override
        public boolean apply(PColumn col) {
            return col.getName().getString().equals(prefix + "V3")
                    || col.getName().getString().equals(prefix + "V2");
        }
    };
    List<PColumn> colList = table.getColumns();
    Collection<PColumn> filteredCols = Collections2.filter(colList, predicate);
    assertEquals(1, filteredCols.size());
    assertEquals(prefix + "V3", filteredCols.iterator().next().getName().getString());
}
 
Example 9
Source File: IndexHalfStoreFileReaderGenerator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private byte[][] getViewConstants(PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    byte[][] viewConstants = null;
    int nViewConstants = 0;
    if (dataTable.getType() == PTableType.VIEW) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        List<PColumn> dataPkColumns = dataTable.getPKColumns();
        for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
            PColumn dataPKColumn = dataPkColumns.get(i);
            if (dataPKColumn.getViewConstant() != null) {
                nViewConstants++;
            }
        }
        if (nViewConstants > 0) {
            viewConstants = new byte[nViewConstants][];
            int j = 0;
            for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
                PColumn dataPkColumn = dataPkColumns.get(i);
                if (dataPkColumn.getViewConstant() != null) {
                    if (IndexUtil.getViewConstantValue(dataPkColumn, ptr)) {
                        viewConstants[j++] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
        }
    }
    return viewConstants;
}
 
Example 10
Source File: BaseQueryPlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void serializeViewConstantsIntoScan(Scan scan, PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    int nViewConstants = 0;
    if (dataTable.getType() == PTableType.VIEW) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        List<PColumn> dataPkColumns = dataTable.getPKColumns();
        for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
            PColumn dataPKColumn = dataPkColumns.get(i);
            if (dataPKColumn.getViewConstant() != null) {
                nViewConstants++;
            }
        }
        if (nViewConstants > 0) {
            byte[][] viewConstants = new byte[nViewConstants][];
            int j = 0;
            for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
                PColumn dataPkColumn = dataPkColumns.get(i);
                if (dataPkColumn.getViewConstant() != null) {
                    if (IndexUtil.getViewConstantValue(dataPkColumn, ptr)) {
                        viewConstants[j++] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
            serializeViewConstantsIntoScan(viewConstants, scan);
        }
    }
}
 
Example 11
Source File: UpsertCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public MutationState execute() throws SQLException {
    ImmutableBytesWritable ptr = context.getTempPtr();
    PTable table = tableRef.getTable();
    table.getIndexMaintainers(ptr, context.getConnection());
    byte[] txState = table.isTransactional() ?
            connection.getMutationState().encodeTransaction() : ByteUtil.EMPTY_BYTE_ARRAY;

    ScanUtil.setClientVersion(scan, MetaDataProtocol.PHOENIX_VERSION);
    if (aggPlan.getTableRef().getTable().isTransactional() 
            || (table.getType() == PTableType.INDEX && table.isTransactional())) {
        scan.setAttribute(BaseScannerRegionObserver.TX_STATE, txState);
    }
    if (ptr.getLength() > 0) {
        byte[] uuidValue = ServerCacheClient.generateId();
        scan.setAttribute(PhoenixIndexCodec.INDEX_UUID, uuidValue);
        scan.setAttribute(PhoenixIndexCodec.INDEX_PROTO_MD, ptr.get());
    }
    ResultIterator iterator = aggPlan.iterator();
    try {
        Tuple row = iterator.next();
        final long mutationCount = (Long) aggProjector.getColumnProjector(0).getValue(row,
                PLong.INSTANCE, ptr);
        return new MutationState(maxSize, maxSizeBytes, connection) {
            @Override
            public long getUpdateCount() {
                return mutationCount;
            }
        };
    } finally {
        iterator.close();
    }

}
 
Example 12
Source File: IndexHalfStoreFileReaderGenerator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private byte[][] getViewConstants(PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    byte[][] viewConstants = null;
    int nViewConstants = 0;
    if (dataTable.getType() == PTableType.VIEW) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        List<PColumn> dataPkColumns = dataTable.getPKColumns();
        for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
            PColumn dataPKColumn = dataPkColumns.get(i);
            if (dataPKColumn.getViewConstant() != null) {
                nViewConstants++;
            }
        }
        if (nViewConstants > 0) {
            viewConstants = new byte[nViewConstants][];
            int j = 0;
            for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
                PColumn dataPkColumn = dataPkColumns.get(i);
                if (dataPkColumn.getViewConstant() != null) {
                    if (IndexUtil.getViewConstantValue(dataPkColumn, ptr)) {
                        viewConstants[j++] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
        }
    }
    return viewConstants;
}
 
Example 13
Source File: UpgradeUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Make sure that all tables have necessary column family properties in sync
 * with each other and also in sync with all the table's indexes
 * See PHOENIX-3955
 * @param conn Phoenix connection
 * @param admin HBase admin used for getting existing tables and their descriptors
 * @throws SQLException
 * @throws IOException
 */
public static void syncTableAndIndexProperties(PhoenixConnection conn, Admin admin)
throws SQLException, IOException {
    Set<TableDescriptor> tableDescriptorsToSynchronize = new HashSet<>();
    for (TableDescriptor origTableDesc : admin.listTableDescriptors()) {
        if (MetaDataUtil.isViewIndex(origTableDesc.getTableName().getNameWithNamespaceInclAsString())) {
            // Ignore physical view index tables since we handle them for each base table already
            continue;
        }
        PTable table;
        String fullTableName = SchemaUtil.getPhysicalTableName(
                origTableDesc.getTableName().getName(),
                SchemaUtil.isNamespaceMappingEnabled(
                        null, conn.getQueryServices().getProps())).getNameAsString();
        try {
            // Use this getTable API to get the latest PTable
            table = PhoenixRuntime.getTable(conn, null, fullTableName);
        } catch (TableNotFoundException e) {
            // Ignore tables not mapped to a Phoenix Table
            LOGGER.warn("Error getting PTable for HBase table: " + fullTableName);
            continue;
        }
        if (table.getType() == PTableType.INDEX) {
            // Ignore global index tables since we handle them for each base table already
            continue;
        }
        syncUpdateCacheFreqAllIndexes(conn, table);
        ColumnFamilyDescriptor defaultColFam = origTableDesc.getColumnFamily(SchemaUtil.getEmptyColumnFamily(table));
        Map<String, Object> syncedProps = MetaDataUtil.getSyncedProps(defaultColFam);

        addTableDescIfPropsChanged(origTableDesc, defaultColFam, syncedProps, tableDescriptorsToSynchronize);
        syncGlobalIndexesForTable(conn.getQueryServices(), table, defaultColFam, syncedProps, tableDescriptorsToSynchronize);
        syncViewIndexTable(conn.getQueryServices(), table, defaultColFam, syncedProps, tableDescriptorsToSynchronize);
    }
    for (TableDescriptor t: tableDescriptorsToSynchronize) {
        admin.modifyTable(t);
    }
}
 
Example 14
Source File: PhoenixConfigurationUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static boolean getStatsForParallelizationProp(PhoenixConnection conn, PTable table) {
    Boolean useStats = table.useStatsForParallelization();
    if (useStats != null) {
        return useStats;
    }
    /*
     * For a view index, we use the property set on view. For indexes on base table, whether
     * global or local, we use the property set on the base table. Null check needed when
     * dropping local indexes.
     */
    PName tenantId = conn.getTenantId();
    int retryCount = 0;
    while (retryCount++<2) {
	    if (table.getType() == PTableType.INDEX && table.getParentName() != null) {
	        String parentTableName = table.getParentName().getString();
			try {
	            PTable parentTable =
	                    conn.getTable(new PTableKey(tenantId, parentTableName));
	            useStats = parentTable.useStatsForParallelization();
	            if (useStats != null) {
	                return useStats;
	            }
			} catch (TableNotFoundException e) {
				// try looking up the table without the tenant id (for
				// global tables)
				if (tenantId != null) {
					tenantId = null;
				} else {
					BaseResultIterators.LOGGER.warn(
							"Unable to find parent table \"" + parentTableName + "\" of table \""
									+ table.getName().getString() + "\" to determine USE_STATS_FOR_PARALLELIZATION",
							e);
				}
			}
	    }
    }
    return conn.getQueryServices().getConfiguration()
            .getBoolean(USE_STATS_FOR_PARALLELIZATION, DEFAULT_USE_STATS_FOR_PARALLELIZATION);
}
 
Example 15
Source File: BaseQueryPlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static void serializeViewConstantsIntoScan(Scan scan, PTable dataTable) {
    int dataPosOffset = (dataTable.getBucketNum() != null ? 1 : 0) + (dataTable.isMultiTenant() ? 1 : 0);
    int nViewConstants = 0;
    if (dataTable.getType() == PTableType.VIEW) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        List<PColumn> dataPkColumns = dataTable.getPKColumns();
        for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
            PColumn dataPKColumn = dataPkColumns.get(i);
            if (dataPKColumn.getViewConstant() != null) {
                nViewConstants++;
            }
        }
        if (nViewConstants > 0) {
            byte[][] viewConstants = new byte[nViewConstants][];
            int j = 0;
            for (int i = dataPosOffset; i < dataPkColumns.size(); i++) {
                PColumn dataPkColumn = dataPkColumns.get(i);
                if (dataPkColumn.getViewConstant() != null) {
                    if (IndexUtil.getViewConstantValue(dataPkColumn, ptr)) {
                        viewConstants[j++] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
            serializeViewConstantsIntoScan(viewConstants, scan);
        }
    }
}
 
Example 16
Source File: IndexUtil.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public static void setScanAttributesForIndexReadRepair(Scan scan, PTable table, PhoenixConnection phoenixConnection) throws SQLException {
    if (table.isTransactional() || table.getType() != PTableType.INDEX) {
        return;
    }
    PTable indexTable = table;
    if (indexTable.getIndexType() != PTable.IndexType.GLOBAL) {
        return;
    }
    String schemaName = indexTable.getParentSchemaName().getString();
    String tableName = indexTable.getParentTableName().getString();
    PTable dataTable;
    try {
        dataTable = PhoenixRuntime.getTable(phoenixConnection, SchemaUtil.getTableName(schemaName, tableName));
    } catch (TableNotFoundException e) {
        // This index table must be being deleted. No need to set the scan attributes
        return;
    }
    // MetaDataClient modifies the index table name for view indexes if the parent view of an index has a child
    // view. This, we need to recreate a PTable object with the correct table name for the rest of this code to work
    if (indexTable.getViewIndexId() != null && indexTable.getName().getString().contains(QueryConstants.CHILD_VIEW_INDEX_NAME_SEPARATOR)) {
        int lastIndexOf = indexTable.getName().getString().lastIndexOf(QueryConstants.CHILD_VIEW_INDEX_NAME_SEPARATOR);
        String indexName = indexTable.getName().getString().substring(lastIndexOf + 1);
        indexTable = PhoenixRuntime.getTable(phoenixConnection, indexName);
    }
    if (!dataTable.getIndexes().contains(indexTable)) {
        return;
    }
    if (scan.getAttribute(PhoenixIndexCodec.INDEX_PROTO_MD) == null) {
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        IndexMaintainer.serialize(dataTable, ptr, Collections.singletonList(indexTable), phoenixConnection);
        scan.setAttribute(PhoenixIndexCodec.INDEX_PROTO_MD, ByteUtil.copyKeyBytesIfNecessary(ptr));
    }
    scan.setAttribute(BaseScannerRegionObserver.CHECK_VERIFY_COLUMN, TRUE_BYTES);
    scan.setAttribute(BaseScannerRegionObserver.PHYSICAL_DATA_TABLE_NAME, dataTable.getPhysicalName().getBytes());
    IndexMaintainer indexMaintainer = indexTable.getIndexMaintainer(dataTable, phoenixConnection);
    byte[] emptyCF = indexMaintainer.getEmptyKeyValueFamily().copyBytesIfNecessary();
    byte[] emptyCQ = indexMaintainer.getEmptyKeyValueQualifier();
    scan.setAttribute(BaseScannerRegionObserver.EMPTY_COLUMN_FAMILY_NAME, emptyCF);
    scan.setAttribute(BaseScannerRegionObserver.EMPTY_COLUMN_QUALIFIER_NAME, emptyCQ);
    if (scan.getAttribute(BaseScannerRegionObserver.VIEW_CONSTANTS) == null) {
        BaseQueryPlan.serializeViewConstantsIntoScan(scan, dataTable);
    }
    addEmptyColumnToScan(scan, emptyCF, emptyCQ);
}
 
Example 17
Source File: WhereCompiler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the start/stop key range based on the whereClause expression.
 * @param context the shared context during query compilation
 * @param whereClause the final where clause expression.
 */
private static void setScanFilter(StatementContext context, FilterableStatement statement, Expression whereClause, boolean disambiguateWithFamily) {
    Scan scan = context.getScan();

    if (LiteralExpression.isBooleanFalseOrNull(whereClause)) {
        context.setScanRanges(ScanRanges.NOTHING);
    } else if (whereClause != null && !ExpressionUtil.evaluatesToTrue(whereClause)) {
        Filter filter = null;
        final Counter counter = new Counter();
        whereClause.accept(new KeyValueExpressionVisitor() {

            @Override
            public Iterator<Expression> defaultIterator(Expression node) {
                // Stop traversal once we've found multiple KeyValue columns
                if (counter.getCount() == Counter.Count.MULTIPLE) {
                    return Collections.emptyIterator();
                }
                return super.defaultIterator(node);
            }

            @Override
            public Void visit(KeyValueColumnExpression expression) {
                counter.increment(expression);
                return null;
            }
        });
        PTable table = context.getCurrentTable().getTable();
        QualifierEncodingScheme encodingScheme = table.getEncodingScheme();
        ImmutableStorageScheme storageScheme = table.getImmutableStorageScheme();
        Counter.Count count = counter.getCount();
        boolean allCFs = false;
        byte[] essentialCF = null;
        if (counter.getCount() == Counter.Count.SINGLE && whereClause.requiresFinalEvaluation() ) {
            if (table.getViewType() == ViewType.MAPPED) {
                allCFs = true;
            } else {
                byte[] emptyCF = SchemaUtil.getEmptyColumnFamily(table);
                if (Bytes.compareTo(emptyCF, counter.getColumn().getColumnFamily()) != 0) {
                    essentialCF = emptyCF;
                    count = Counter.Count.MULTIPLE;
                }
            }
        }
        switch (count) {
        case NONE:
            essentialCF = table.getType() == PTableType.VIEW 
                    ? ByteUtil.EMPTY_BYTE_ARRAY 
                    : SchemaUtil.getEmptyColumnFamily(table);
            filter = new RowKeyComparisonFilter(whereClause, essentialCF);
            break;
        case SINGLE:
            filter = disambiguateWithFamily 
                ? new SingleCFCQKeyValueComparisonFilter(whereClause) 
                : new SingleCQKeyValueComparisonFilter(whereClause);
            break;
        case MULTIPLE:
            filter = isPossibleToUseEncodedCQFilter(encodingScheme, storageScheme) 
                ? new MultiEncodedCQKeyValueComparisonFilter(whereClause, encodingScheme, allCFs, essentialCF) 
                : (disambiguateWithFamily 
                    ? new MultiCFCQKeyValueComparisonFilter( whereClause, allCFs, essentialCF) 
                    : new MultiCQKeyValueComparisonFilter(whereClause, allCFs, essentialCF));
            break;
        }
        scan.setFilter(filter);
    }

    ScanRanges scanRanges = context.getScanRanges();
    if (scanRanges.useSkipScanFilter()) {
        ScanUtil.andFilterAtBeginning(scan, scanRanges.getSkipScanFilter());
    }
}
 
Example 18
Source File: MutationState.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private void joinMutationState(TableRef tableRef, MultiRowMutationState srcRows,
        Map<TableRef, MultiRowMutationState> dstMutations) {
    PTable table = tableRef.getTable();
    boolean isIndex = table.getType() == PTableType.INDEX;
    boolean incrementRowCount = dstMutations == this.mutations;
    MultiRowMutationState existingRows = dstMutations.put(tableRef, srcRows);
    if (existingRows != null) { // Rows for that table already exist
        // Loop through new rows and replace existing with new
        for (Map.Entry<ImmutableBytesPtr, RowMutationState> rowEntry : srcRows.entrySet()) {
            // Replace existing row with new row
            RowMutationState existingRowMutationState = existingRows.put(rowEntry.getKey(), rowEntry.getValue());
            if (existingRowMutationState != null) {
                Map<PColumn, byte[]> existingValues = existingRowMutationState.getColumnValues();
                if (existingValues != PRow.DELETE_MARKER) {
                    Map<PColumn, byte[]> newRow = rowEntry.getValue().getColumnValues();
                    // if new row is PRow.DELETE_MARKER, it means delete, and we don't need to merge it with
                    // existing row.
                    if (newRow != PRow.DELETE_MARKER) {
                        // decrement estimated size by the size of the old row
                        estimatedSize -= existingRowMutationState.calculateEstimatedSize();
                        // Merge existing column values with new column values
                        existingRowMutationState.join(rowEntry.getValue());
                        // increment estimated size by the size of the new row
                        estimatedSize += existingRowMutationState.calculateEstimatedSize();
                        // Now that the existing row has been merged with the new row, replace it back
                        // again (since it was merged with the new one above).
                        existingRows.put(rowEntry.getKey(), existingRowMutationState);
                    }
                }
            } else {
                if (incrementRowCount && !isIndex) { // Don't count index rows in row count
                    numRows++;
                    // increment estimated size by the size of the new row
                    estimatedSize += rowEntry.getValue().calculateEstimatedSize();
                }
            }
        }
        // Put the existing one back now that it's merged
        dstMutations.put(tableRef, existingRows);
    } else {
        // Size new map at batch size as that's what it'll likely grow to.
        MultiRowMutationState newRows = new MultiRowMutationState(connection.getMutateBatchSize());
        newRows.putAll(srcRows);
        dstMutations.put(tableRef, newRows);
        if (incrementRowCount && !isIndex) {
            numRows += srcRows.size();
            // if we added all the rows from newMutationState we can just increment the
            // estimatedSize by newMutationState.estimatedSize
            estimatedSize += srcRows.estimatedSize;
        }
    }
}
 
Example 19
Source File: MutationState.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Combine a newer mutation with this one, where in the event of overlaps,
 * the newer one will take precedence.
 * @param newMutation the newer mutation
 */
public void join(MutationState newMutation) {
    if (this == newMutation) { // Doesn't make sense
        return;
    }
    this.sizeOffset += newMutation.sizeOffset;
    // Merge newMutation with this one, keeping state from newMutation for any overlaps
    for (Map.Entry<TableRef, Map<ImmutableBytesPtr,Map<PColumn,byte[]>>> entry : newMutation.mutations.entrySet()) {
        // Replace existing entries for the table with new entries
        TableRef tableRef = entry.getKey();
        PTable table = tableRef.getTable();
        boolean isIndex = table.getType() == PTableType.INDEX;
        Map<ImmutableBytesPtr,Map<PColumn,byte[]>> existingRows = this.mutations.put(tableRef, entry.getValue());
        if (existingRows != null) { // Rows for that table already exist
            // Loop through new rows and replace existing with new
            for (Map.Entry<ImmutableBytesPtr,Map<PColumn,byte[]>> rowEntry : entry.getValue().entrySet()) {
                // Replace existing row with new row
                Map<PColumn,byte[]> existingValues = existingRows.put(rowEntry.getKey(), rowEntry.getValue());
                if (existingValues != null) {
                    if (existingValues != PRow.DELETE_MARKER) {
                        Map<PColumn,byte[]> newRow = rowEntry.getValue();
                        // if new row is PRow.DELETE_MARKER, it means delete, and we don't need to merge it with existing row. 
                        if (newRow != PRow.DELETE_MARKER) {
                            // Replace existing column values with new column values
                            for (Map.Entry<PColumn,byte[]> valueEntry : newRow.entrySet()) {
                                existingValues.put(valueEntry.getKey(), valueEntry.getValue());
                            }
                            // Now that the existing row has been merged with the new row, replace it back
                            // again (since it was replaced with the new one above).
                            existingRows.put(rowEntry.getKey(), existingValues);
                        }
                    }
                } else {
                    if (!isIndex) { // Don't count index rows in row count
                        numRows++;
                    }
                }
            }
            // Put the existing one back now that it's merged
            this.mutations.put(entry.getKey(), existingRows);
        } else {
            if (!isIndex) {
                numRows += entry.getValue().size();
            }
        }
    }
    throwIfTooBig();
}
 
Example 20
Source File: WhereCompiler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the start/stop key range based on the whereClause expression.
 * @param context the shared context during query compilation
 * @param whereClause the final where clause expression.
 */
private static void setScanFilter(StatementContext context, FilterableStatement statement, Expression whereClause, boolean disambiguateWithFamily, boolean hashJoinOptimization) {
    Scan scan = context.getScan();

    if (LiteralExpression.isFalse(whereClause)) {
        context.setScanRanges(ScanRanges.NOTHING);
    } else if (whereClause != null && !LiteralExpression.isTrue(whereClause) && !hashJoinOptimization) {
        Filter filter = null;
        final Counter counter = new Counter();
        whereClause.accept(new KeyValueExpressionVisitor() {

            @Override
            public Iterator<Expression> defaultIterator(Expression node) {
                // Stop traversal once we've found multiple KeyValue columns
                if (counter.getCount() == Counter.Count.MULTIPLE) {
                    return Iterators.emptyIterator();
                }
                return super.defaultIterator(node);
            }

            @Override
            public Void visit(KeyValueColumnExpression expression) {
                counter.increment(expression);
                return null;
            }
        });
        switch (counter.getCount()) {
        case NONE:
            PTable table = context.getResolver().getTables().get(0).getTable();
            byte[] essentialCF = table.getType() == PTableType.VIEW 
                    ? ByteUtil.EMPTY_BYTE_ARRAY 
                    : SchemaUtil.getEmptyColumnFamily(table);
            filter = new RowKeyComparisonFilter(whereClause, essentialCF);
            break;
        case SINGLE:
            filter = disambiguateWithFamily ? new SingleCFCQKeyValueComparisonFilter(whereClause) : new SingleCQKeyValueComparisonFilter(whereClause);
            break;
        case MULTIPLE:
            filter = disambiguateWithFamily ? new MultiCFCQKeyValueComparisonFilter(whereClause) : new MultiCQKeyValueComparisonFilter(whereClause);
            break;
        }
        scan.setFilter(filter);
    }

    ScanRanges scanRanges = context.getScanRanges();
    if (scanRanges.useSkipScanFilter()) {
        ScanUtil.andFilterAtBeginning(scan, scanRanges.getSkipScanFilter());
    }
}