Java Code Examples for org.apache.phoenix.schema.PColumn#getMaxLength()

The following examples show how to use org.apache.phoenix.schema.PColumn#getMaxLength() . 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: SchemaUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Imperfect estimate of row size given a PTable
 * TODO: keep row count in stats table and use total size / row count instead
 * @param table
 * @return estimate of size in bytes of a row
 */
public static long estimateRowSize(PTable table) {
	int keyLength = estimateKeyLength(table);
	long rowSize = 0;
	for (PColumn column : table.getColumns()) {
		if (!SchemaUtil.isPKColumn(column)) {
            PDataType type = column.getDataType();
            Integer maxLength = column.getMaxLength();
            int valueLength = !type.isFixedWidth() ? VAR_KV_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
			rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, column.getFamilyName().getBytes().length, column.getName().getBytes().length, valueLength);
		}
	}
	// Empty key value
	rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, getEmptyColumnFamily(table).length, QueryConstants.EMPTY_COLUMN_BYTES.length, 0);
	return rowSize;
}
 
Example 2
Source File: IndexColumnNames.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private String getDataTypeString(PColumn col) {
    PDataType<?> dataType = col.getDataType();
    switch (dataType.getSqlType()) {
    case Types.DECIMAL:
        String typeStr = dataType.toString();
        if (col.getMaxLength() != null) {
            typeStr += "(" + col.getMaxLength().toString();
            if (col.getScale() != null) {
                typeStr += "," + col.getScale().toString();
            }
            typeStr += ")";
        }
        return typeStr;
    default:
        if (col.getMaxLength() != null) {
            return String.format("%s(%s)", dataType.toString(), col.getMaxLength());
        }
        return dataType.toString();
    }
}
 
Example 3
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Imperfect estimate of row size given a PTable
 * TODO: keep row count in stats table and use total size / row count instead
 * @param table
 * @return estimate of size in bytes of a row
 */
public static long estimateRowSize(PTable table) {
	int keyLength = estimateKeyLength(table);
	long rowSize = 0;
	for (PColumn column : table.getColumns()) {
		if (!SchemaUtil.isPKColumn(column)) {
            PDataType type = column.getDataType();
            Integer maxLength = column.getMaxLength();
            int valueLength = !type.isFixedWidth() ? VAR_KV_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
			rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, column.getFamilyName().getBytes().length, column.getName().getBytes().length, valueLength);
		}
	}
	byte[] emptyKeyValueKV = EncodedColumnsUtil.getEmptyKeyValueInfo(table).getFirst();
	// Empty key value
	rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, getEmptyColumnFamily(table).length, emptyKeyValueKV.length, 0);
	return rowSize;
}
 
Example 4
Source File: WhereOptimizer.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public KeySlots visitLeave(LikeExpression node, List<KeySlots> childParts) {
    // TODO: optimize ILIKE by creating two ranges for the literal prefix: one with lower case, one with upper case
    if (childParts.isEmpty()) {
        return null;
    }
    // for SUBSTR(<column>,1,3) LIKE 'foo%'
    KeySlots childSlots = childParts.get(0);
    KeySlot childSlot = childSlots.iterator().next();
    final String startsWith = node.getLiteralPrefix();
    byte[] key = PChar.INSTANCE.toBytes(startsWith, node.getChildren().get(0).getSortOrder());
    // If the expression is an equality expression against a fixed length column
    // and the key length doesn't match the column length, the expression can
    // never be true.
    // An zero length byte literal is null which can never be compared against as true
    Expression firstChild = node.getChildren().get(0);
    Integer childNodeFixedLength = firstChild.getDataType().isFixedWidth() ? firstChild.getMaxLength() : null;
    if (childNodeFixedLength != null && key.length > childNodeFixedLength) {
        return EMPTY_KEY_SLOTS;
    }
    // TODO: is there a case where we'd need to go through the childPart to calculate the key range?
    PColumn column = childSlot.getKeyPart().getColumn();
    PDataType type = column.getDataType();
    byte[] lowerRange = key;
    byte[] upperRange = ByteUtil.nextKey(key);
    Integer columnFixedLength = column.getMaxLength();
    if (type.isFixedWidth() && columnFixedLength != null) {
        lowerRange = StringUtil.padChar(lowerRange, columnFixedLength);
        upperRange = StringUtil.padChar(upperRange, columnFixedLength);
    }
    KeyRange keyRange = type.getKeyRange(lowerRange, true, upperRange, false);
    // Only extract LIKE expression if pattern ends with a wildcard and everything else was extracted
    return newKeyParts(childSlot, node.endsWithOnlyWildcard() ? node : null, keyRange);
}
 
Example 5
Source File: JoinCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void addProjectedColumn(List<PColumn> projectedColumns, List<Expression> sourceExpressions,
        ListMultimap<String, String> columnNameMap, PColumn sourceColumn, PName familyName, boolean hasSaltingColumn,
        boolean isLocalIndexColumnRef, StatementContext context)
throws SQLException {
    if (sourceColumn == SALTING_COLUMN)
        return;

    int position = projectedColumns.size() + (hasSaltingColumn ? 1 : 0);
    PTable table = tableRef.getTable();
    String schemaName = table.getSchemaName().getString();
    String tableName = table.getTableName().getString();
    String colName = isLocalIndexColumnRef ? IndexUtil.getIndexColumnName(sourceColumn) : sourceColumn.getName().getString();
    String fullName = getProjectedColumnName(schemaName, tableName, colName);
    String aliasedName = tableRef.getTableAlias() == null ? fullName : getProjectedColumnName(null, tableRef.getTableAlias(), colName);

    columnNameMap.put(colName, aliasedName);
    if (!fullName.equals(aliasedName)) {
        columnNameMap.put(fullName, aliasedName);
    }

    PName name = PNameFactory.newName(aliasedName);
    PColumnImpl column = new PColumnImpl(name, familyName, sourceColumn.getDataType(),
            sourceColumn.getMaxLength(), sourceColumn.getScale(), sourceColumn.isNullable(),
            position, sourceColumn.getSortOrder(), sourceColumn.getArraySize(), sourceColumn.getViewConstant(), sourceColumn.isViewReferenced(), sourceColumn.getExpressionStr());
    Expression sourceExpression = isLocalIndexColumnRef ?
              NODE_FACTORY.column(TableName.create(schemaName, tableName), "\"" + colName + "\"", null).accept(new ExpressionCompiler(context))
            : new ColumnRef(tableRef, sourceColumn.getPosition()).newColumnExpression();
    projectedColumns.add(column);
    sourceExpressions.add(sourceExpression);
}
 
Example 6
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Estimate the max key length in bytes of the PK for a given table
 * @param table the table
 * @return the max PK length
 */
public static int estimateKeyLength(PTable table) {
    int maxKeyLength = 0;
    // Calculate the max length of a key (each part must currently be of a fixed width)
    int i = 0;
    List<PColumn> columns = table.getPKColumns();
    while (i < columns.size()) {
        PColumn keyColumn = columns.get(i++);
        PDataType type = keyColumn.getDataType();
        Integer maxLength = keyColumn.getMaxLength();
        maxKeyLength += !type.isFixedWidth() ? VAR_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
    }
    return maxKeyLength;
}
 
Example 7
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param pCol
 * @return sql type name that could be used in DDL statements, dynamic column types etc. 
 */
public static String getSqlTypeName(PColumn pCol) {
    PDataType dataType = pCol.getDataType();
    Integer maxLength = pCol.getMaxLength();
    Integer scale = pCol.getScale();
    return dataType.isArrayType() ? getArraySqlTypeName(maxLength, scale, dataType) : appendMaxLengthAndScale(maxLength, scale, dataType.getSqlTypeName());
}
 
Example 8
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Estimate the max key length in bytes of the PK for a given table
 * @param table the table
 * @return the max PK length
 */
public static int estimateKeyLength(PTable table) {
    int maxKeyLength = 0;
    // Calculate the max length of a key (each part must currently be of a fixed width)
    int i = 0;
    List<PColumn> columns = table.getPKColumns();
    while (i < columns.size()) {
        PColumn keyColumn = columns.get(i++);
        PDataType type = keyColumn.getDataType();
        Integer maxLength = keyColumn.getMaxLength();
        maxKeyLength += !type.isFixedWidth() ? VAR_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
    }
    return maxKeyLength;
}
 
Example 9
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Pads the data in ptr by the required amount for fixed width data types
 */
public static void padData(String tableName, PColumn column, ImmutableBytesWritable ptr) {
    PDataType type = column.getDataType();
    byte[] byteValue = ptr.get();
    boolean isNull = type.isNull(byteValue);
    Integer maxLength = column.getMaxLength();
    if (!isNull && type.isFixedWidth() && maxLength != null) {
        if (ptr.getLength() < maxLength) {
            type.pad(ptr, maxLength, column.getSortOrder());
        } else if (ptr.getLength() > maxLength) {
            throw new DataExceedsCapacityException(tableName + "." + column.getName().getString() + " may not exceed " + maxLength + " bytes (" + type.toObject(byteValue) + ")");
        }
    }
}
 
Example 10
Source File: IndexUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static PColumn getIndexPKColumn(int position, PColumn dataColumn) {
	assert (SchemaUtil.isPKColumn(dataColumn));
	PName indexColumnName = PNameFactory.newName(getIndexColumnName(null, dataColumn.getName().getString()));
	PColumn column = new PColumnImpl(indexColumnName, null, dataColumn.getDataType(), dataColumn.getMaxLength(),
			dataColumn.getScale(), dataColumn.isNullable(), position, dataColumn.getSortOrder(),
			dataColumn.getArraySize(), null, false, dataColumn.getExpressionStr(), dataColumn.isRowTimestamp(), false,
			// TODO set the columnQualifierBytes correctly
			/*columnQualifierBytes*/null, HConstants.LATEST_TIMESTAMP); 
	return column;
}
 
Example 11
Source File: PhoenixRuntime.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param pCol
 * @return sql type name that could be used in DDL statements, dynamic column types etc. 
 */
public static String getSqlTypeName(PColumn pCol) {
    PDataType dataType = pCol.getDataType();
    Integer maxLength = pCol.getMaxLength();
    Integer scale = pCol.getScale();
    return getSqlTypeName(dataType, maxLength, scale);
}
 
Example 12
Source File: WhereOptimizer.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public KeySlots visitLeave(LikeExpression node, List<KeySlots> childParts) {
    // TODO: optimize ILIKE by creating two ranges for the literal prefix: one with lower case, one with upper case
    if (childParts.isEmpty()) {
        return null;
    }
    // for SUBSTR(<column>,1,3) LIKE 'foo%'
    KeySlots childSlots = childParts.get(0);
    KeySlot childSlot = childSlots.getSlots().get(0);
    final String startsWith = node.getLiteralPrefix();
    SortOrder sortOrder = node.getChildren().get(0).getSortOrder();
    byte[] key = PVarchar.INSTANCE.toBytes(startsWith, sortOrder);
    // If the expression is an equality expression against a fixed length column
    // and the key length doesn't match the column length, the expression can
    // never be true.
    // An zero length byte literal is null which can never be compared against as true
    Expression firstChild = node.getChildren().get(0);
    Integer childNodeFixedLength = firstChild.getDataType().isFixedWidth() ? firstChild.getMaxLength() : null;
    if (childNodeFixedLength != null && key.length > childNodeFixedLength) {
        return EMPTY_KEY_SLOTS;
    }
    // TODO: is there a case where we'd need to go through the childPart to calculate the key range?
    PColumn column = childSlot.getKeyPart().getColumn();
    PDataType type = column.getDataType();
    byte[] lowerRange = key;
    byte[] upperRange = ByteUtil.nextKey(key);
    Integer columnFixedLength = column.getMaxLength();
    if (type.isFixedWidth()) {
        if (columnFixedLength != null) { // Sanity check - should always be non null
            // Always use minimum byte to fill as otherwise our key is bigger
            // that it should be when the sort order is descending.
            lowerRange = type.pad(lowerRange, columnFixedLength, SortOrder.ASC);
            upperRange = type.pad(upperRange, columnFixedLength, SortOrder.ASC);
        }
    } else if (column.getSortOrder() == SortOrder.DESC && table.rowKeyOrderOptimizable()) {
        // Append a zero byte if descending since a \xFF byte will be appended to the lowerRange
        // causing rows to be skipped that should be included. For example, with rows 'ab', 'a',
        // a lowerRange of 'a\xFF' would skip 'ab', while 'a\x00\xFF' would not.
        lowerRange = Arrays.copyOf(lowerRange, lowerRange.length+1);
        lowerRange[lowerRange.length-1] = QueryConstants.SEPARATOR_BYTE;
    }
    KeyRange range = type.getKeyRange(lowerRange, true, upperRange, false);
    if (column.getSortOrder() == SortOrder.DESC) {
        range = range.invert();
    }
    // Only extract LIKE expression if pattern ends with a wildcard and everything else was extracted
    return newKeyParts(childSlot, node.endsWithOnlyWildcard() ? node : null, range);
}
 
Example 13
Source File: PrefixFunction.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public KeyRange getKeyRange(CompareOp op, Expression rhs) {
    byte[] lowerRange = KeyRange.UNBOUND;
    byte[] upperRange = KeyRange.UNBOUND;
    boolean lowerInclusive = true;
    PDataType type = getColumn().getDataType();
    switch (op) {
        case EQUAL:
            lowerRange = evaluateExpression(rhs);
            upperRange = ByteUtil.nextKey(lowerRange);
            break;
        case GREATER:
            lowerRange = ByteUtil.nextKey(evaluateExpression(rhs));
            break;
        case LESS_OR_EQUAL:
            upperRange = ByteUtil.nextKey(evaluateExpression(rhs));
            lowerInclusive = false;
            break;
        default:
            return childPart.getKeyRange(op, rhs);
    }
    PColumn column = getColumn();
    Integer length = column.getMaxLength();
    if (type.isFixedWidth()) {
        if (length != null) { // Sanity check - shouldn't be necessary
            // Don't pad based on current sort order, but instead use our
            // minimum byte as otherwise we'll end up skipping rows in
            // the case of descending, since rows with more padding appear
            // *after* rows with no padding.
            if (lowerRange != KeyRange.UNBOUND) {
                lowerRange = type.pad(lowerRange, length, SortOrder.ASC);
            }
            if (upperRange != KeyRange.UNBOUND) {
                upperRange = type.pad(upperRange, length, SortOrder.ASC);
            }
        }
    } else if (column.getSortOrder() == SortOrder.DESC && getTable().rowKeyOrderOptimizable()) {
        // Append a zero byte if descending since a \xFF byte will be appended to the lowerRange
        // causing rows to be skipped that should be included. For example, with rows 'ab', 'a',
        // a lowerRange of 'a\xFF' would skip 'ab', while 'a\x00\xFF' would not.
        if (lowerRange != KeyRange.UNBOUND) {
            lowerRange = Arrays.copyOf(lowerRange, lowerRange.length+1);
            lowerRange[lowerRange.length-1] = QueryConstants.SEPARATOR_BYTE;
        }
    }
    KeyRange range = KeyRange.getKeyRange(lowerRange, lowerInclusive, upperRange, false);
    if (column.getSortOrder() == SortOrder.DESC) {
        range = range.invert();
    }
    return range;
}