public static Slice truncateToLengthAndTrimSpaces(Slice slice, Type type) { requireNonNull(type, "type is null"); if (!isCharType(type)) { throw new IllegalArgumentException("type must be the instance of CharType"); } return truncateToLengthAndTrimSpaces(slice, CharType.class.cast(type)); }
public static Slice truncateToLengthAndTrimSpaces(Slice slice, int maxLength) { requireNonNull(slice, "slice is null"); if (maxLength < 0) { throw new IllegalArgumentException("Max length must be greater or equal than zero"); } return trimTrailingSpaces(truncateToLength(slice, maxLength)); }
public static Slice trimTrailingSpaces(Slice slice) { requireNonNull(slice, "slice is null"); return slice.slice(0, byteCountWithoutTrailingSpace(slice, 0, slice.length())); }
public static Slice padSpaces(Slice slice, Type type) { requireNonNull(type, "type is null"); if (!isCharType(type)) { throw new IllegalArgumentException("type must be the instance of CharType"); } return padSpaces(slice, CharType.class.cast(type)); }
@Override protected void readValue(BlockBuilder blockBuilder, Type type) { if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) { Binary binary = valuesReader.readBytes(); Slice value; if (binary.length() == 0) { value = EMPTY_SLICE; } else { value = wrappedBuffer(binary.getBytes()); } if (isVarcharType(type)) { value = truncateToLength(value, type); } if (isCharType(type)) { value = truncateToLengthAndTrimSpaces(value, type); } type.writeSlice(blockBuilder, value); } else if (isValueNull()) { blockBuilder.appendNull(); } }
ConnectorPageSource delegate) requireNonNull(columnMappings, "columnMappings is null"); requireNonNull(hiveStorageTimeZone, "hiveStorageTimeZone is null"); requireNonNull(typeManager, "typeManager is null"); this.delegate = requireNonNull(delegate, "delegate is null"); prefilledValue = doublePartitionKey(columnValue, name); else if (isVarcharType(type)) { prefilledValue = varcharPartitionKey(columnValue, name, type); else if (isCharType(type)) { prefilledValue = charPartitionKey(columnValue, name, type); throw new PrestoException(NOT_SUPPORTED, format("Unsupported column type %s for prefilled column: %s", type.getDisplayName(), name));
public static Slice truncateToLengthAndTrimSpaces(Slice slice, CharType charType) { requireNonNull(charType, "charType is null"); return truncateToLengthAndTrimSpaces(slice, charType.getLength()); }
public static Slice padSpaces(Slice slice, CharType charType) { requireNonNull(charType, "charType is null"); return padSpaces(slice, charType.getLength()); }
private static void serializePrimitive(Type type, BlockBuilder builder, Object object, PrimitiveObjectInspector inspector) requireNonNull(builder, "parent builder is null"); return; case STRING: type.writeSlice(builder, Slices.utf8Slice(((StringObjectInspector) inspector).getPrimitiveJavaObject(object))); return; case VARCHAR: type.writeSlice(builder, Slices.utf8Slice(((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(object).getValue())); return; case CHAR: CharType charType = (CharType) type; HiveChar hiveChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(object); type.writeSlice(builder, truncateToLengthAndTrimSpaces(Slices.utf8Slice(hiveChar.getValue()), charType.getLength())); return; case DATE:
private static void serializePrimitive(Type type, BlockBuilder builder, Object object) { requireNonNull(builder, "parent builder is null"); if (object == null) { builder.appendNull(); return; } if (BOOLEAN.equals(type)) { BOOLEAN.writeBoolean(builder, (Boolean) object); } else if (BIGINT.equals(type) || INTEGER.equals(type) || SMALLINT.equals(type) || TINYINT.equals(type) || REAL.equals(type) || DATE.equals(type) || TIMESTAMP.equals(type)) { type.writeLong(builder, getLongExpressedValue(object)); } else if (DOUBLE.equals(type)) { DOUBLE.writeDouble(builder, ((Number) object).doubleValue()); } else if (isVarcharType(type) || VARBINARY.equals(type) || isCharType(type)) { type.writeSlice(builder, getSliceExpressedValue(object, type)); } else { throw new UnsupportedOperationException("Unsupported primitive type: " + type); } }
type.writeBoolean(blockBuilder, (Boolean) value); type.writeLong(blockBuilder, ((Number) value).longValue()); type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue()); Slice slice = truncateToLength(utf8Slice((String) value), type); type.writeSlice(blockBuilder, slice); Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type); type.writeSlice(blockBuilder, slice); type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
statement.setBoolean(parameter, type.getBoolean(block, position)); statement.setLong(parameter, type.getLong(block, position)); statement.setInt(parameter, toIntExact(type.getLong(block, position))); statement.setBigDecimal(parameter, readBigDecimal((DecimalType) type, block, position)); else if (isVarcharType(type) || isCharType(type)) { statement.setString(parameter, type.getSlice(block, position).toStringUtf8()); statement.setBytes(parameter, type.getSlice(block, position).getBytes());
private static Slice getSliceExpressedValue(Object value, Type type) { Slice sliceValue; if (value instanceof String) { sliceValue = Slices.utf8Slice((String) value); } else if (value instanceof byte[]) { sliceValue = Slices.wrappedBuffer((byte[]) value); } else if (value instanceof Integer) { sliceValue = Slices.utf8Slice(value.toString()); } else { throw new IllegalStateException("unsupported string field type: " + value.getClass().getName()); } if (isVarcharType(type)) { sliceValue = truncateToLength(sliceValue, type); } if (isCharType(type)) { sliceValue = truncateToLengthAndTrimSpaces(sliceValue, type); } return sliceValue; }
private static void assertByteCountWithoutTrailingSpace(byte[] actual, int offset, int length, int codePointCount, byte[] expected) { Slice slice = wrappedBuffer(actual); int truncatedLength = byteCountWithoutTrailingSpace(slice, offset, length, codePointCount); byte[] bytes = slice.getBytes(offset, truncatedLength); assertEquals(bytes, expected); } }
Slice value = Slices.wrappedBuffer(hiveValue.getBytes(), 0, hiveValue.getLength()); Type type = types[column]; if (isVarcharType(type)) { value = truncateToLength(value, type); if (isCharType(type)) { value = truncateToLengthAndTrimSpaces(value, type); slices[column] = Slices.copyOf(value); nulls[column] = false;
public static Slice charPartitionKey(String value, String name, Type columnType) { Slice partitionKey = trimTrailingSpaces(Slices.utf8Slice(value)); CharType charType = (CharType) columnType; if (SliceUtf8.countCodePoints(partitionKey) > charType.getLength()) { throw new PrestoException(HIVE_INVALID_PARTITION_VALUE, format("Invalid partition value '%s' for %s partition key: %s", value, columnType.toString(), name)); } return partitionKey; }
throws IOException Slice slice = wrappedBuffer(data); boolean isCharType = isCharType(type); if (length > 0) {
if (type.getJavaType() == boolean.class && columnStatistics.getBooleanStatistics() != null) { BooleanStatistics booleanStatistics = columnStatistics.getBooleanStatistics(); return createDomain(type, hasNullValue, columnStatistics.getDecimalStatistics(), value -> encodeUnscaledValue(rescale(value, (DecimalType) type).unscaledValue())); else if (isCharType(type) && columnStatistics.getStringStatistics() != null) { return createDomain(type, hasNullValue, columnStatistics.getStringStatistics(), value -> truncateToLengthAndTrimSpaces(value, type)); else if (isVarcharType(type) && columnStatistics.getStringStatistics() != null) { return createDomain(type, hasNullValue, columnStatistics.getStringStatistics()); else if (type.getTypeSignature().getBase().equals(StandardTypes.DATE) && columnStatistics.getDateStatistics() != null) { return createDomain(type, hasNullValue, columnStatistics.getDateStatistics(), value -> (long) value); else if (type.getJavaType() == long.class && columnStatistics.getIntegerStatistics() != null) { return createDomain(type, hasNullValue, columnStatistics.getIntegerStatistics());
private static void assertByteCountWithoutTrailingSpaceFailure(String string, int offset, int maxLength) { try { byteCountWithoutTrailingSpace(utf8Slice(string), offset, maxLength); fail("Expected exception"); } catch (IllegalArgumentException expected) { } }
@Test public void testTruncateToLengthAndTrimSpaces() { assertEquals(utf8Slice("a"), truncateToLengthAndTrimSpaces(utf8Slice("a c"), 1)); assertEquals(utf8Slice("a"), truncateToLengthAndTrimSpaces(utf8Slice("a "), 1)); assertEquals(utf8Slice("a"), truncateToLengthAndTrimSpaces(utf8Slice("abc"), 1)); assertEquals(utf8Slice(""), truncateToLengthAndTrimSpaces(utf8Slice("a c"), 0)); assertEquals(utf8Slice("a c"), truncateToLengthAndTrimSpaces(utf8Slice("a c "), 3)); assertEquals(utf8Slice("a c"), truncateToLengthAndTrimSpaces(utf8Slice("a c "), 4)); assertEquals(utf8Slice("a c"), truncateToLengthAndTrimSpaces(utf8Slice("a c "), 5)); assertEquals(utf8Slice("a c"), truncateToLengthAndTrimSpaces(utf8Slice("a c"), 3)); assertEquals(utf8Slice("a c"), truncateToLengthAndTrimSpaces(utf8Slice("a c"), 4)); assertEquals(utf8Slice("a c"), truncateToLengthAndTrimSpaces(utf8Slice("a c"), 5)); assertEquals(utf8Slice(""), truncateToLengthAndTrimSpaces(utf8Slice(" "), 1)); assertEquals(utf8Slice(""), truncateToLengthAndTrimSpaces(utf8Slice(""), 1)); }