Search in sources :

Example 1 with PColumnFamily

use of org.apache.phoenix.schema.PColumnFamily in project phoenix by apache.

the class FormatToBytesWritableMapper method initColumnIndexes.

/*
    Map all unique pairs <family, name>  to index. Table name is part of TableRowkey, so we do
    not care about it
     */
private void initColumnIndexes() throws SQLException {
    columnIndexes = new TreeMap<>(Bytes.BYTES_COMPARATOR);
    int columnIndex = 0;
    for (int index = 0; index < logicalNames.size(); index++) {
        PTable table = PhoenixRuntime.getTable(conn, logicalNames.get(index));
        if (!table.getImmutableStorageScheme().equals(ImmutableStorageScheme.ONE_CELL_PER_COLUMN)) {
            List<PColumnFamily> cfs = table.getColumnFamilies();
            for (int i = 0; i < cfs.size(); i++) {
                byte[] family = cfs.get(i).getName().getBytes();
                byte[] cfn = Bytes.add(family, QueryConstants.NAMESPACE_SEPARATOR_BYTES, QueryConstants.SINGLE_KEYVALUE_COLUMN_QUALIFIER_BYTES);
                columnIndexes.put(cfn, new Integer(columnIndex));
                columnIndex++;
            }
        } else {
            List<PColumn> cls = table.getColumns();
            for (int i = 0; i < cls.size(); i++) {
                PColumn c = cls.get(i);
                byte[] family = new byte[0];
                byte[] cq;
                if (!SchemaUtil.isPKColumn(c)) {
                    family = c.getFamilyName().getBytes();
                    cq = c.getColumnQualifierBytes();
                } else {
                    cq = c.getName().getBytes();
                }
                byte[] cfn = Bytes.add(family, QueryConstants.NAMESPACE_SEPARATOR_BYTES, cq);
                if (!columnIndexes.containsKey(cfn)) {
                    columnIndexes.put(cfn, new Integer(columnIndex));
                    columnIndex++;
                }
            }
            byte[] emptyColumnFamily = SchemaUtil.getEmptyColumnFamily(table);
            byte[] emptyKeyValue = EncodedColumnsUtil.getEmptyKeyValueInfo(table).getFirst();
            byte[] cfn = Bytes.add(emptyColumnFamily, QueryConstants.NAMESPACE_SEPARATOR_BYTES, emptyKeyValue);
            columnIndexes.put(cfn, new Integer(columnIndex));
            columnIndex++;
        }
    }
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) PColumnFamily(org.apache.phoenix.schema.PColumnFamily) PTable(org.apache.phoenix.schema.PTable)

Example 2 with PColumnFamily

use of org.apache.phoenix.schema.PColumnFamily in project phoenix by apache.

the class ConnectionQueryServicesImpl method separateAndValidateProperties.

private Pair<HTableDescriptor, HTableDescriptor> separateAndValidateProperties(PTable table, Map<String, List<Pair<String, Object>>> properties, Set<String> colFamiliesForPColumnsToBeAdded, List<Pair<byte[], Map<String, Object>>> families, Map<String, Object> tableProps) throws SQLException {
    Map<String, Map<String, Object>> stmtFamiliesPropsMap = new HashMap<>(properties.size());
    Map<String, Object> commonFamilyProps = new HashMap<>();
    boolean addingColumns = colFamiliesForPColumnsToBeAdded != null && !colFamiliesForPColumnsToBeAdded.isEmpty();
    HashSet<String> existingColumnFamilies = existingColumnFamilies(table);
    Map<String, Map<String, Object>> allFamiliesProps = new HashMap<>(existingColumnFamilies.size());
    boolean isTransactional = table.isTransactional();
    boolean willBeTransactional = false;
    boolean isOrWillBeTransactional = isTransactional;
    Integer newTTL = null;
    for (String family : properties.keySet()) {
        List<Pair<String, Object>> propsList = properties.get(family);
        if (propsList != null && propsList.size() > 0) {
            Map<String, Object> colFamilyPropsMap = new HashMap<String, Object>(propsList.size());
            for (Pair<String, Object> prop : propsList) {
                String propName = prop.getFirst();
                Object propValue = prop.getSecond();
                if ((MetaDataUtil.isHTableProperty(propName) || TableProperty.isPhoenixTableProperty(propName)) && addingColumns) {
                    // setting HTable and PhoenixTable properties while adding a column is not allowed.
                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_SET_TABLE_PROPERTY_ADD_COLUMN).setMessage("Property: " + propName).build().buildException();
                }
                if (MetaDataUtil.isHTableProperty(propName)) {
                    // Can't have a column family name for a property that's an HTableProperty
                    if (!family.equals(QueryConstants.ALL_FAMILY_PROPERTIES_KEY)) {
                        throw new SQLExceptionInfo.Builder(SQLExceptionCode.COLUMN_FAMILY_NOT_ALLOWED_TABLE_PROPERTY).setMessage("Column Family: " + family + ", Property: " + propName).build().buildException();
                    }
                    tableProps.put(propName, propValue);
                } else {
                    if (TableProperty.isPhoenixTableProperty(propName)) {
                        TableProperty.valueOf(propName).validate(true, !family.equals(QueryConstants.ALL_FAMILY_PROPERTIES_KEY), table.getType());
                        if (propName.equals(TTL)) {
                            newTTL = ((Number) prop.getSecond()).intValue();
                            // Even though TTL is really a HColumnProperty we treat it specially.
                            // We enforce that all column families have the same TTL.
                            commonFamilyProps.put(propName, prop.getSecond());
                        } else if (propName.equals(PhoenixDatabaseMetaData.TRANSACTIONAL) && Boolean.TRUE.equals(propValue)) {
                            willBeTransactional = isOrWillBeTransactional = true;
                            tableProps.put(TxConstants.READ_NON_TX_DATA, propValue);
                        }
                    } else {
                        if (MetaDataUtil.isHColumnProperty(propName)) {
                            if (family.equals(QueryConstants.ALL_FAMILY_PROPERTIES_KEY)) {
                                commonFamilyProps.put(propName, propValue);
                            } else {
                                colFamilyPropsMap.put(propName, propValue);
                            }
                        } else {
                            // as HTableProp if its neither HColumnProp or PhoenixTableProp.
                            throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_ALTER_PROPERTY).setMessage("Column Family: " + family + ", Property: " + propName).build().buildException();
                        }
                    }
                }
            }
            if (!colFamilyPropsMap.isEmpty()) {
                stmtFamiliesPropsMap.put(family, colFamilyPropsMap);
            }
        }
    }
    commonFamilyProps = Collections.unmodifiableMap(commonFamilyProps);
    boolean isAddingPkColOnly = colFamiliesForPColumnsToBeAdded.size() == 1 && colFamiliesForPColumnsToBeAdded.contains(null);
    if (!commonFamilyProps.isEmpty()) {
        if (!addingColumns) {
            // Add the common family props to all existing column families
            for (String existingColFamily : existingColumnFamilies) {
                Map<String, Object> m = new HashMap<String, Object>(commonFamilyProps.size());
                m.putAll(commonFamilyProps);
                allFamiliesProps.put(existingColFamily, m);
            }
        } else {
            // Add the common family props to the column families of the columns being added
            for (String colFamily : colFamiliesForPColumnsToBeAdded) {
                if (colFamily != null) {
                    // only set properties for key value columns
                    Map<String, Object> m = new HashMap<String, Object>(commonFamilyProps.size());
                    m.putAll(commonFamilyProps);
                    allFamiliesProps.put(colFamily, m);
                } else if (isAddingPkColOnly) {
                    // only for the kv cols and not pk cols.
                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.SET_UNSUPPORTED_PROP_ON_ALTER_TABLE).build().buildException();
                }
            }
        }
    }
    // and merge them with the common family properties.
    for (String f : stmtFamiliesPropsMap.keySet()) {
        if (!addingColumns && !existingColumnFamilies.contains(f)) {
            String schemaNameStr = table.getSchemaName() == null ? null : table.getSchemaName().getString();
            String tableNameStr = table.getTableName() == null ? null : table.getTableName().getString();
            throw new ColumnFamilyNotFoundException(schemaNameStr, tableNameStr, f);
        }
        if (addingColumns && !colFamiliesForPColumnsToBeAdded.contains(f)) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_SET_PROPERTY_FOR_COLUMN_NOT_ADDED).build().buildException();
        }
        Map<String, Object> commonProps = allFamiliesProps.get(f);
        Map<String, Object> stmtProps = stmtFamiliesPropsMap.get(f);
        if (commonProps != null) {
            if (stmtProps != null) {
                // merge common props with statement props for the family
                commonProps.putAll(stmtProps);
            }
        } else {
            // if no common props were specified, then assign family specific props
            if (stmtProps != null) {
                allFamiliesProps.put(f, stmtProps);
            }
        }
    }
    // ALTER TABLE ADD CF.COL
    for (String cf : colFamiliesForPColumnsToBeAdded) {
        if (cf != null && allFamiliesProps.get(cf) == null) {
            allFamiliesProps.put(cf, new HashMap<String, Object>());
        }
    }
    if (table.getColumnFamilies().isEmpty() && !addingColumns && !commonFamilyProps.isEmpty()) {
        allFamiliesProps.put(Bytes.toString(table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES : table.getDefaultFamilyName().getBytes()), commonFamilyProps);
    }
    // Views are not allowed to have any of these properties.
    if (table.getType() == PTableType.VIEW && (!stmtFamiliesPropsMap.isEmpty() || !commonFamilyProps.isEmpty() || !tableProps.isEmpty())) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.VIEW_WITH_PROPERTIES).build().buildException();
    }
    HTableDescriptor newTableDescriptor = null;
    HTableDescriptor origTableDescriptor = null;
    if (!allFamiliesProps.isEmpty() || !tableProps.isEmpty()) {
        byte[] tableNameBytes = Bytes.toBytes(table.getPhysicalName().getString());
        HTableDescriptor existingTableDescriptor = origTableDescriptor = getTableDescriptor(tableNameBytes);
        newTableDescriptor = new HTableDescriptor(existingTableDescriptor);
        if (!tableProps.isEmpty()) {
            // add all the table properties to the existing table descriptor
            for (Entry<String, Object> entry : tableProps.entrySet()) {
                newTableDescriptor.setValue(entry.getKey(), entry.getValue() != null ? entry.getValue().toString() : null);
            }
        }
        if (addingColumns) {
            // Make sure that all the CFs of the table have the same TTL as the empty CF.
            setTTLForNewCFs(allFamiliesProps, table, newTableDescriptor, newTTL);
        }
        // Set TTL on all table column families, even if they're not referenced here
        if (newTTL != null) {
            for (PColumnFamily family : table.getColumnFamilies()) {
                if (!allFamiliesProps.containsKey(family.getName().getString())) {
                    Map<String, Object> familyProps = Maps.newHashMapWithExpectedSize(1);
                    familyProps.put(TTL, newTTL);
                    allFamiliesProps.put(family.getName().getString(), familyProps);
                }
            }
        }
        Integer defaultTxMaxVersions = null;
        if (isOrWillBeTransactional) {
            // Calculate default for max versions
            Map<String, Object> emptyFamilyProps = allFamiliesProps.get(SchemaUtil.getEmptyColumnFamilyAsString(table));
            if (emptyFamilyProps != null) {
                defaultTxMaxVersions = (Integer) emptyFamilyProps.get(HConstants.VERSIONS);
            }
            if (defaultTxMaxVersions == null) {
                if (isTransactional) {
                    defaultTxMaxVersions = newTableDescriptor.getFamily(SchemaUtil.getEmptyColumnFamily(table)).getMaxVersions();
                } else {
                    defaultTxMaxVersions = this.getProps().getInt(QueryServices.MAX_VERSIONS_TRANSACTIONAL_ATTRIB, QueryServicesOptions.DEFAULT_MAX_VERSIONS_TRANSACTIONAL);
                }
            }
            if (willBeTransactional) {
                // Set VERSIONS for all column families when transitioning to transactional
                for (PColumnFamily family : table.getColumnFamilies()) {
                    if (!allFamiliesProps.containsKey(family.getName().getString())) {
                        Map<String, Object> familyProps = Maps.newHashMapWithExpectedSize(1);
                        familyProps.put(HConstants.VERSIONS, defaultTxMaxVersions);
                        allFamiliesProps.put(family.getName().getString(), familyProps);
                    }
                }
            }
        }
        // an already transactional table.
        if (isOrWillBeTransactional) {
            int ttl = getTTL(table, newTableDescriptor, newTTL);
            if (ttl != HColumnDescriptor.DEFAULT_TTL) {
                for (Map.Entry<String, Map<String, Object>> entry : allFamiliesProps.entrySet()) {
                    Map<String, Object> props = entry.getValue();
                    if (props == null) {
                        props = new HashMap<String, Object>();
                    }
                    props.put(TxConstants.PROPERTY_TTL, ttl);
                    // or if the existing transactional table wasn't originally non transactional.
                    if (!willBeTransactional && !Boolean.valueOf(newTableDescriptor.getValue(TxConstants.READ_NON_TX_DATA))) {
                        props.remove(TTL);
                    }
                }
            }
        }
        for (Entry<String, Map<String, Object>> entry : allFamiliesProps.entrySet()) {
            Map<String, Object> familyProps = entry.getValue();
            if (isOrWillBeTransactional) {
                if (!familyProps.containsKey(HConstants.VERSIONS)) {
                    familyProps.put(HConstants.VERSIONS, defaultTxMaxVersions);
                }
            }
            byte[] cf = Bytes.toBytes(entry.getKey());
            HColumnDescriptor colDescriptor = newTableDescriptor.getFamily(cf);
            if (colDescriptor == null) {
                // new column family
                colDescriptor = generateColumnFamilyDescriptor(new Pair<>(cf, familyProps), table.getType());
                newTableDescriptor.addFamily(colDescriptor);
            } else {
                modifyColumnFamilyDescriptor(colDescriptor, familyProps);
            }
            if (isOrWillBeTransactional) {
                checkTransactionalVersionsValue(colDescriptor);
            }
        }
    }
    return new Pair<>(origTableDescriptor, newTableDescriptor);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyValueBuilder(org.apache.phoenix.hbase.index.util.KeyValueBuilder) NonTxIndexBuilder(org.apache.phoenix.hbase.index.covered.NonTxIndexBuilder) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) PhoenixIndexBuilder(org.apache.phoenix.index.PhoenixIndexBuilder) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo) Pair(org.apache.hadoop.hbase.util.Pair) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) PColumnFamily(org.apache.phoenix.schema.PColumnFamily) ColumnFamilyNotFoundException(org.apache.phoenix.schema.ColumnFamilyNotFoundException) PTinyint(org.apache.phoenix.schema.types.PTinyint) PUnsignedTinyint(org.apache.phoenix.schema.types.PUnsignedTinyint) MultiRowMutationEndpoint(org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) PInteger(org.apache.phoenix.schema.types.PInteger) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 3 with PColumnFamily

use of org.apache.phoenix.schema.PColumnFamily in project phoenix by apache.

the class ConnectionQueryServicesImpl method existingColumnFamilies.

private HashSet<String> existingColumnFamilies(PTable table) {
    List<PColumnFamily> cfs = table.getColumnFamilies();
    HashSet<String> cfNames = new HashSet<>(cfs.size());
    for (PColumnFamily cf : table.getColumnFamilies()) {
        cfNames.add(cf.getName().getString());
    }
    return cfNames;
}
Also used : PColumnFamily(org.apache.phoenix.schema.PColumnFamily) HashSet(java.util.HashSet)

Example 4 with PColumnFamily

use of org.apache.phoenix.schema.PColumnFamily in project phoenix by apache.

the class IndexUtil method getDataColumn.

public static PColumn getDataColumn(PTable dataTable, String indexColumnName) {
    int pos = indexColumnName.indexOf(INDEX_COLUMN_NAME_SEP);
    if (pos < 0) {
        throw new IllegalArgumentException("Could not find expected '" + INDEX_COLUMN_NAME_SEP + "' separator in index column name of \"" + indexColumnName + "\"");
    }
    if (pos == 0) {
        try {
            return dataTable.getPKColumn(indexColumnName.substring(1));
        } catch (ColumnNotFoundException e) {
            throw new IllegalArgumentException("Could not find PK column \"" + indexColumnName.substring(pos + 1) + "\" in index column name of \"" + indexColumnName + "\"", e);
        }
    }
    PColumnFamily family;
    try {
        family = dataTable.getColumnFamily(getDataColumnFamilyName(indexColumnName));
    } catch (ColumnFamilyNotFoundException e) {
        throw new IllegalArgumentException("Could not find column family \"" + indexColumnName.substring(0, pos) + "\" in index column name of \"" + indexColumnName + "\"", e);
    }
    try {
        return family.getPColumnForColumnName(indexColumnName.substring(pos + 1));
    } catch (ColumnNotFoundException e) {
        throw new IllegalArgumentException("Could not find column \"" + indexColumnName.substring(pos + 1) + "\" in index column name of \"" + indexColumnName + "\"", e);
    }
}
Also used : ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) PColumnFamily(org.apache.phoenix.schema.PColumnFamily) ColumnFamilyNotFoundException(org.apache.phoenix.schema.ColumnFamilyNotFoundException)

Example 5 with PColumnFamily

use of org.apache.phoenix.schema.PColumnFamily in project phoenix by apache.

the class PhoenixRuntime method getColumn.

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);
    // 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;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) SQLException(java.sql.SQLException) PColumnFamily(org.apache.phoenix.schema.PColumnFamily)

Aggregations

PColumnFamily (org.apache.phoenix.schema.PColumnFamily)24 PColumn (org.apache.phoenix.schema.PColumn)13 PTable (org.apache.phoenix.schema.PTable)10 ColumnFamilyNotFoundException (org.apache.phoenix.schema.ColumnFamilyNotFoundException)9 ColumnNotFoundException (org.apache.phoenix.schema.ColumnNotFoundException)7 SQLException (java.sql.SQLException)6 Map (java.util.Map)5 HashMap (java.util.HashMap)4 List (java.util.List)4 TreeMap (java.util.TreeMap)4 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)4 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)4 Pair (org.apache.hadoop.hbase.util.Pair)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 TableRef (org.apache.phoenix.schema.TableRef)3 ByteString (com.google.protobuf.ByteString)2