Java Code Examples for org.apache.phoenix.util.SchemaUtil#getEmptyColumnFamily()

The following examples show how to use org.apache.phoenix.util.SchemaUtil#getEmptyColumnFamily() . 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: MetaDataClient.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public MutationState createTable(CreateTableStatement statement, byte[][] splits, PTable parent, String viewStatement, ViewType viewType, byte[][] viewColumnConstants, BitSet isViewColumnReferenced) throws SQLException {
    PTable table = createTableInternal(statement, splits, parent, viewStatement, viewType, viewColumnConstants, isViewColumnReferenced, null, null);
    if (table == null || table.getType() == PTableType.VIEW) {
        return new MutationState(0,connection);
    }
    // Hack to get around the case when an SCN is specified on the connection.
    // In this case, we won't see the table we just created yet, so we hack
    // around it by forcing the compiler to not resolve anything.
    PostDDLCompiler compiler = new PostDDLCompiler(connection);
    //connection.setAutoCommit(true);
    // Execute any necessary data updates
    Long scn = connection.getSCN();
    long ts = (scn == null ? table.getTimeStamp() : scn);
    // Getting the schema through the current connection doesn't work when the connection has an scn specified
    // Since the table won't be added to the current connection.
    TableRef tableRef = new TableRef(null, table, ts, false);
    byte[] emptyCF = SchemaUtil.getEmptyColumnFamily(table);
    MutationPlan plan = compiler.compile(Collections.singletonList(tableRef), emptyCF, null, null, tableRef.getTimeStamp());
    return connection.getQueryServices().updateData(plan);
}
 
Example 2
Source File: ImmutableIndexExtendedIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static int getRowCountForEmptyColValue(Connection conn, String tableName,
        byte[] valueBytes)  throws IOException, SQLException {

    PTable table = PhoenixRuntime.getTable(conn, tableName);
    byte[] emptyCF = SchemaUtil.getEmptyColumnFamily(table);
    byte[] emptyCQ = EncodedColumnsUtil.getEmptyKeyValueInfo(table).getFirst();
    ConnectionQueryServices queryServices =
            conn.unwrap(PhoenixConnection.class).getQueryServices();
    HTable htable = (HTable) queryServices.getTable(table.getPhysicalName().getBytes());
    Scan scan = new Scan();
    scan.addColumn(emptyCF, emptyCQ);
    ResultScanner resultScanner = htable.getScanner(scan);
    int count = 0;

    for (Result result = resultScanner.next(); result != null; result = resultScanner.next()) {
        if (Bytes.compareTo(result.getValue(emptyCF, emptyCQ), 0, valueBytes.length,
                valueBytes, 0, valueBytes.length) == 0) {
            ++count;
        }
    }
    return count;
}
 
Example 3
Source File: ImmutableIndexIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static boolean verifyRowsForEmptyColValue(Connection conn, String tableName, byte[] valueBytes)
        throws IOException, SQLException {
    PTable table = PhoenixRuntime.getTable(conn, tableName);
    byte[] emptyCF = SchemaUtil.getEmptyColumnFamily(table);
    byte[] emptyCQ = EncodedColumnsUtil.getEmptyKeyValueInfo(table).getFirst();
    HTable htable = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(table.getPhysicalName().getBytes());
    Scan scan = new Scan();
    scan.addColumn(emptyCF, emptyCQ);
    ResultScanner resultScanner = htable.getScanner(scan);

    for (Result result = resultScanner.next(); result != null; result = resultScanner.next()) {
        if (Bytes.compareTo(result.getValue(emptyCF, emptyCQ), 0, valueBytes.length,
                valueBytes, 0, valueBytes.length) != 0) {
            return false;
        }
    }
    return true;
}
 
Example 4
Source File: MetaDataClient.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate what the new column family will be after the column is dropped, returning null
 * if unchanged.
 * @param table table containing column to drop
 * @param columnToDrop column being dropped
 * @return the new column family or null if unchanged.
 */
private static byte[] getNewEmptyColumnFamilyOrNull (PTable table, PColumn columnToDrop) {
    if (table.getType() != PTableType.VIEW && !SchemaUtil.isPKColumn(columnToDrop) && table.getColumnFamilies().get(0).getName().equals(columnToDrop.getFamilyName()) && table.getColumnFamilies().get(0).getColumns().size() == 1) {
        return SchemaUtil.getEmptyColumnFamily(table.getDefaultFamilyName(), table.getColumnFamilies().subList(1, table.getColumnFamilies().size()));
    }
    // If unchanged, return null
    return null;
}
 
Example 5
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());
    }
}
 
Example 6
Source File: BaseResultIterators.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public BaseResultIterators(QueryPlan plan, Integer perScanLimit) throws SQLException {
    super(plan.getContext(), plan.getTableRef(), plan.getGroupBy(), plan.getOrderBy(), plan.getStatement().getHint());
    this.plan = plan;
    StatementContext context = plan.getContext();
    TableRef tableRef = plan.getTableRef();
    PTable table = tableRef.getTable();
    FilterableStatement statement = plan.getStatement();
    RowProjector projector = plan.getProjector();
    physicalTableName = table.getPhysicalName().getBytes();
    tableStats = useStats() ? new MetaDataClient(context.getConnection()).getTableStats(table) : PTableStats.EMPTY_STATS;
    Scan scan = context.getScan();
    // Used to tie all the scans together during logging
    scanId = UUID.randomUUID().toString();
    Map<byte [], NavigableSet<byte []>> familyMap = scan.getFamilyMap();
    boolean keyOnlyFilter = familyMap.isEmpty() && context.getWhereCoditionColumns().isEmpty();
    if (projector.isProjectEmptyKeyValue()) {
        // If nothing projected into scan and we only have one column family, just allow everything
        // to be projected and use a FirstKeyOnlyFilter to skip from row to row. This turns out to
        // be quite a bit faster.
        // Where condition columns also will get added into familyMap
        // When where conditions are present, we can not add FirstKeyOnlyFilter at beginning.
        if (familyMap.isEmpty() && context.getWhereCoditionColumns().isEmpty()
                && table.getColumnFamilies().size() == 1) {
            // Project the one column family. We must project a column family since it's possible
            // that there are other non declared column families that we need to ignore.
            scan.addFamily(table.getColumnFamilies().get(0).getName().getBytes());
        } else {
            byte[] ecf = SchemaUtil.getEmptyColumnFamily(table);
            // Project empty key value unless the column family containing it has
            // been projected in its entirety.
            if (!familyMap.containsKey(ecf) || familyMap.get(ecf) != null) {
                scan.addColumn(ecf, QueryConstants.EMPTY_COLUMN_BYTES);
            }
        }
    } else if (table.getViewType() == ViewType.MAPPED) {
        // Since we don't have the empty key value in MAPPED tables, we must select all CFs in HRS. But only the
        // selected column values are returned back to client
        for (PColumnFamily family : table.getColumnFamilies()) {
            scan.addFamily(family.getName().getBytes());
        }
    }
    // Add FirstKeyOnlyFilter if there are no references to key value columns
    if (keyOnlyFilter) {
        ScanUtil.andFilterAtBeginning(scan, new FirstKeyOnlyFilter());
    }
    
    // TODO adding all CFs here is not correct. It should be done only after ColumnProjectionOptimization.
    if (perScanLimit != null) {
        ScanUtil.andFilterAtEnd(scan, new PageFilter(perScanLimit));
    }

    doColumnProjectionOptimization(context, scan, table, statement);
    
    this.scans = getParallelScans();
    List<KeyRange> splitRanges = Lists.newArrayListWithExpectedSize(scans.size() * ESTIMATED_GUIDEPOSTS_PER_REGION);
    for (List<Scan> scanList : scans) {
        for (Scan aScan : scanList) {
            splitRanges.add(KeyRange.getKeyRange(aScan.getStartRow(), aScan.getStopRow()));
        }
    }
    this.splits = ImmutableList.copyOf(splitRanges);
    // If split detected, this will be more than one, but that's unlikely
    this.allFutures = Lists.newArrayListWithExpectedSize(1);
}
 
Example 7
Source File: BaseResultIterators.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private List<byte[]> getGuidePosts() {
    /*
     *  Don't use guide posts if:
     *  1) We're doing a point lookup, as HBase is fast enough at those
     *     to not need them to be further parallelized. TODO: pref test to verify
     *  2) We're collecting stats, as in this case we need to scan entire
     *     regions worth of data to track where to put the guide posts.
     */
    if (!useStats()) {
        return Collections.emptyList();
    }
    
    List<byte[]> gps = null;
    PTable table = getTable();
    Map<byte[],GuidePostsInfo> guidePostMap = tableStats.getGuidePosts();
    byte[] defaultCF = SchemaUtil.getEmptyColumnFamily(getTable());
    if (table.getColumnFamilies().isEmpty()) {
        // For sure we can get the defaultCF from the table
        if (guidePostMap.get(defaultCF) != null) {
            gps = guidePostMap.get(defaultCF).getGuidePosts();
        }
    } else {
        Scan scan = context.getScan();
        if (scan.getFamilyMap().size() > 0 && !scan.getFamilyMap().containsKey(defaultCF)) {
            // If default CF is not used in scan, use first CF referenced in scan
            GuidePostsInfo guidePostsInfo = guidePostMap.get(scan.getFamilyMap().keySet().iterator().next());
            if (guidePostsInfo != null) {
                gps = guidePostsInfo.getGuidePosts();
            }
        } else {
            // Otherwise, favor use of default CF.
            if (guidePostMap.get(defaultCF) != null) {
                gps = guidePostMap.get(defaultCF).getGuidePosts();
            }
        }
    }
    if (gps == null) {
        return Collections.emptyList();
    }
    return gps;
}
 
Example 8
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 9
Source File: PhoenixRowTimestampParseNode.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
/**
 * Note: Although this ParseNode does not take any children, we are injecting an EMPTY_COLUMN
 * KeyValueColumnExpression so that the EMPTY_COLUMN is evaluated during scan filter processing.
 */
public FunctionExpression create(List<Expression> children, StatementContext context)
        throws SQLException {

    // PhoenixRowTimestampFunction does not take any parameters.
    if (children.size() != 0) {
        throw new IllegalArgumentException(
                "PhoenixRowTimestampFunction does not take any parameters"
        );
    }

    // Get the empty column family and qualifier for the context.
    PTable table = context.getCurrentTable().getTable();
    byte[] emptyColumnFamilyName = SchemaUtil.getEmptyColumnFamily(table);
    byte[] emptyColumnName =
            table.getEncodingScheme() == PTable.QualifierEncodingScheme.NON_ENCODED_QUALIFIERS
                    ? QueryConstants.EMPTY_COLUMN_BYTES
                    : table.getEncodingScheme().encode(QueryConstants.ENCODED_EMPTY_COLUMN_NAME);

    // Create an empty column key value expression.
    // This will cause the empty column key value to be evaluated during scan filter processing.
    Expression emptyColumnExpression = new KeyValueColumnExpression(new PDatum() {
        @Override
        public boolean isNullable() {
            return false;
        }
        @Override
        public PDataType getDataType() {
            return PDate.INSTANCE;
        }
        @Override
        public Integer getMaxLength() {
            return null;
        }
        @Override
        public Integer getScale() {
            return null;
        }
        @Override
        public SortOrder getSortOrder() {
            return SortOrder.getDefault();
        }
    }, emptyColumnFamilyName, emptyColumnName);
    List<Expression> expressionList = Arrays.asList(new Expression[] {emptyColumnExpression});

    // Add the empty column to the projection list.
    // According to PHOENIX-4179 this will then return the timestamp of the empty column cell.
    context.getScan().addColumn(emptyColumnFamilyName, emptyColumnName);
    return new PhoenixRowTimestampFunction(expressionList);
}
 
Example 10
Source File: MutationState.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private void filterIndexCheckerMutations(Map<TableInfo, List<Mutation>> mutationMap,
        Map<TableInfo, List<Mutation>> unverifiedIndexMutations,
        Map<TableInfo, List<Mutation>> verifiedOrDeletedIndexMutations) throws SQLException {
    Iterator<Entry<TableInfo, List<Mutation>>> mapIter = mutationMap.entrySet().iterator();

    while (mapIter.hasNext()) {
        Entry<TableInfo, List<Mutation>> pair = mapIter.next();
        TableInfo tableInfo = pair.getKey();
        if (!tableInfo.getPTable().getType().equals(PTableType.INDEX)) {
            continue;
        }
        PTable logicalTable = tableInfo.getPTable();
        if (tableInfo.getOrigTableRef().getTable().isImmutableRows()
                && IndexUtil.isGlobalIndexCheckerEnabled(connection,
                tableInfo.getHTableName())) {

            byte[] emptyCF = SchemaUtil.getEmptyColumnFamily(logicalTable);
            byte[] emptyCQ = EncodedColumnsUtil.getEmptyKeyValueInfo(logicalTable).getFirst();
            List<Mutation> mutations = pair.getValue();

            for (Mutation m : mutations) {
                if (m == null) {
                    continue;
                }
                if (m instanceof Delete) {
                    Put put = new Put(m.getRow());
                    put.addColumn(emptyCF, emptyCQ, IndexRegionObserver.getMaxTimestamp(m),
                            IndexRegionObserver.UNVERIFIED_BYTES);
                    // The Delete gets marked as unverified in Phase 1 and gets deleted on Phase 3.
                    addToMap(unverifiedIndexMutations, tableInfo, put);
                    addToMap(verifiedOrDeletedIndexMutations, tableInfo, m);
                } else if (m instanceof Put) {
                    long timestamp = IndexRegionObserver.getMaxTimestamp(m);

                    // Phase 1 index mutations are set to unverified
                    // Send entire mutation with the unverified status
                    // Remove the empty column prepared by Index codec as we need to change its value
                    IndexRegionObserver.removeEmptyColumn(m, emptyCF, emptyCQ);
                    ((Put) m).addColumn(emptyCF, emptyCQ, timestamp, IndexRegionObserver.UNVERIFIED_BYTES);
                    addToMap(unverifiedIndexMutations, tableInfo, m);

                    // Phase 3 mutations are verified
                    Put verifiedPut = new Put(m.getRow());
                    verifiedPut.addColumn(emptyCF, emptyCQ, timestamp, IndexRegionObserver.VERIFIED_BYTES);
                    addToMap(verifiedOrDeletedIndexMutations, tableInfo, verifiedPut);
                } else {
                    addToMap(unverifiedIndexMutations, tableInfo, m);
                }
            }

            mapIter.remove();
        }

    }
}