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(); } }
/** * Constructs a column info for the supplied pColumn * @param pColumn * @return columnInfo * @throws SQLException if the parameter is null. */ public static ColumnInfo getColumnInfo(PColumn pColumn) throws SQLException { if (pColumn == null) { throw new SQLException("pColumn must not be null."); } return ColumnInfo.create(pColumn.toString(), pColumn.getDataType().getSqlType(), pColumn.getMaxLength(), pColumn.getScale()); }
/** * * @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); }
/** * 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; }
Object value = values[i]; Expression expr = LiteralExpression.newConstant(value, col.getDataType(), col.getMaxLength(), col.getScale()); expressions.add(expr); i++;
colUpsert.setInt(6, column.getDataType().getSqlType()); colUpsert.setInt(7, SchemaUtil.getIsNullableInt(column.isNullable())); if (column.getMaxLength() == null) { colUpsert.setNull(8, Types.INTEGER); } else { colUpsert.setInt(8, column.getMaxLength());
/** * * @param conn connection that was used for reading/generating value. * @param fullTableName fully qualified table name * @param values values of the columns * @param columns list of pair of column that includes column family as first part and column name as the second part. * Column family is optional and hence nullable. Columns in the list have to be in the same order as the order of occurence * of their values in the object array. * @return values encoded in a byte array * @throws SQLException * @see {@link #decodeValues(Connection, String, byte[], List)} */ public static byte[] encodeColumnValues(Connection conn, String fullTableName, Object[] values, List<Pair<String, String>> columns) throws SQLException { PTable table = getTable(conn, fullTableName); List<PColumn> pColumns = getColumns(table, columns); List<Expression> expressions = new ArrayList<Expression>(pColumns.size()); int i = 0; for (PColumn col : pColumns) { Object value = values[i]; // for purposes of encoding, sort order of the columns doesn't matter. Expression expr = LiteralExpression.newConstant(value, col.getDataType(), col.getMaxLength(), col.getScale()); expressions.add(expr); i++; } KeyValueSchema kvSchema = buildKeyValueSchema(pColumns); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); ValueBitSet valueSet = ValueBitSet.newInstance(kvSchema); return kvSchema.toBytes(expressions.toArray(new Expression[0]), valueSet, ptr); }
@Test public void testCharArrayLength() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); conn.createStatement().execute( "CREATE TABLE TEST.TEST (testInt INTEGER, testCharArray CHAR(3)[], testByteArray BINARY(7)[], " + "CONSTRAINT test_pk PRIMARY KEY(testInt)) DEFAULT_COLUMN_FAMILY='T'"); conn.createStatement().execute("CREATE INDEX TEST_INDEX ON TEST.TEST (testInt) INCLUDE (testCharArray, testByteArray)"); PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class); QueryPlan plan = stmt.optimizeQuery("SELECT /*+ INDEX(TEST.TEST TEST_INDEX)*/ testCharArray,testByteArray FROM TEST.TEST"); List<PColumn> columns = plan.getTableRef().getTable().getColumns(); assertEquals(3, columns.size()); assertEquals(3, columns.get(1).getMaxLength().intValue()); assertEquals(7, columns.get(2).getMaxLength().intValue()); }
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
PColumn newColumn = new PColumnImpl(oldColumn.getName(), oldColumn.getFamilyName(), oldColumn.getDataType(), oldColumn.getMaxLength(), oldColumn.getScale(), oldColumn.isNullable(), i-1+positionOffset, oldColumn.getSortOrder(), oldColumn.getArraySize(), oldColumn.getViewConstant(), oldColumn.isViewReferenced(), oldColumn.getExpressionStr(), oldColumn.isRowTimestamp(), oldColumn.isDynamic(), oldColumn.getColumnQualifierBytes(), oldColumn.getTimestamp()); columns.add(newColumn);
public PColumnImpl(PColumn column, boolean derivedColumn, int position, byte[] viewConstant) { this(column.getName(), column.getFamilyName(), column.getDataType(), column.getMaxLength(), column.getScale(), column.isNullable(), position, column.getSortOrder(), column.getArraySize(), viewConstant, column.isViewReferenced(), column.getExpressionStr(), column.isRowTimestamp(), column.isDynamic(), column.getColumnQualifierBytes(), column.getTimestamp(), derivedColumn); }
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; }
public PColumnImpl(PColumn column, byte[] viewConstant, boolean isViewReferenced) { this(column.getName(), column.getFamilyName(), column.getDataType(), column.getMaxLength(), column.getScale(), column.isNullable(), column.getPosition(), column.getSortOrder(), column.getArraySize(), viewConstant, isViewReferenced, column.getExpressionStr(), column.isRowTimestamp(), column.isDynamic(), column.getColumnQualifierBytes(), column.getTimestamp(), column.isDerived()); }
Integer length = getColumn().getMaxLength(); if (type.isFixedWidth() && length != null) {
/** * 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) + ")"); } } }
@Override public KeyRange getKeyRange(CompareOp op, Expression rhs) { ImmutableBytesWritable ptr = new ImmutableBytesWritable(); rhs.evaluate(null, ptr); // If the column is fixed width, fill is up to it's byte size PDataType type = getColumn().getDataType(); if (type.isFixedWidth()) { Integer length = getColumn().getMaxLength(); if (length != null) { // Go through type to pad as the fill character depends on the type. type.pad(ptr, length, SortOrder.ASC); } } byte[] key = ByteUtil.copyKeyBytesIfNecessary(ptr); KeyRange range = ByteUtil.getKeyRange(key, rhs.getSortOrder().transform(op)/*op*/, type); // Constants will have been inverted, so we invert them back here so that // RVC comparisons work correctly (see PHOENIX-3383). if (rhs.getSortOrder() == SortOrder.DESC) { range = range.invert(); } return range; }
/** * 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; }
Integer length = column.getMaxLength(); if (type.isFixedWidth()) { if (length != null) { // Sanity check - shouldn't be necessary
builder.setDataType(column.getDataType().getSqlTypeName()); if (column.getMaxLength() != null) { builder.setMaxLength(column.getMaxLength());
} else { ImmutableBytesWritable ptr = new ImmutableBytesWritable(byteValue); Integer maxLength = column.getMaxLength(); Integer scale = column.getScale(); SortOrder sortOrder = column.getSortOrder();