use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class IcebergPageSource method deserializePartitionValue.
private static Object deserializePartitionValue(Type type, String valueString, String name, TimeZoneKey timeZoneKey) {
if (valueString == null) {
return null;
}
try {
if (type.equals(BOOLEAN)) {
if (valueString.equalsIgnoreCase("true")) {
return true;
}
if (valueString.equalsIgnoreCase("false")) {
return false;
}
throw new IllegalArgumentException();
}
if (type.equals(INTEGER)) {
return parseLong(valueString);
}
if (type.equals(BIGINT)) {
return parseLong(valueString);
}
if (type.equals(REAL)) {
return (long) floatToRawIntBits(parseFloat(valueString));
}
if (type.equals(DOUBLE)) {
return parseDouble(valueString);
}
if (type.equals(DATE) || type.equals(TIME) || type.equals(TIMESTAMP)) {
return parseLong(valueString);
}
if (type instanceof VarcharType) {
Slice value = utf8Slice(valueString);
return value;
}
if (type.equals(VarbinaryType.VARBINARY)) {
return utf8Slice(valueString);
}
if (isShortDecimal(type) || isLongDecimal(type)) {
DecimalType decimalType = (DecimalType) type;
BigDecimal decimal = new BigDecimal(valueString);
decimal = decimal.setScale(decimalType.getScale(), BigDecimal.ROUND_UNNECESSARY);
if (decimal.precision() > decimalType.getPrecision()) {
throw new IllegalArgumentException();
}
BigInteger unscaledValue = decimal.unscaledValue();
return isShortDecimal(type) ? unscaledValue.longValue() : Decimals.encodeUnscaledValue(unscaledValue);
}
} catch (IllegalArgumentException e) {
throw new PrestoException(ICEBERG_INVALID_PARTITION_VALUE, format("Invalid partition value '%s' for %s partition key: %s", valueString, type.getDisplayName(), name));
}
// Iceberg tables don't partition by non-primitive-type columns.
throw new PrestoException(GENERIC_INTERNAL_ERROR, "Invalid partition type " + type.toString());
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class TypeCoercer method isTypeOnlyCoercion.
public boolean isTypeOnlyCoercion(Type source, Type result) {
if (source.equals(result)) {
return true;
}
if (!canCoerce(source, result)) {
return false;
}
if (source instanceof VarcharType && result instanceof VarcharType) {
return true;
}
if (source instanceof DecimalType && result instanceof DecimalType) {
DecimalType sourceDecimal = (DecimalType) source;
DecimalType resultDecimal = (DecimalType) result;
boolean sameDecimalSubtype = (sourceDecimal.isShort() && resultDecimal.isShort()) || (!sourceDecimal.isShort() && !resultDecimal.isShort());
boolean sameScale = sourceDecimal.getScale() == resultDecimal.getScale();
boolean sourcePrecisionIsLessOrEqualToResultPrecision = sourceDecimal.getPrecision() <= resultDecimal.getPrecision();
return sameDecimalSubtype && sameScale && sourcePrecisionIsLessOrEqualToResultPrecision;
}
String sourceTypeBase = source.getTypeSignature().getBase();
String resultTypeBase = result.getTypeSignature().getBase();
if (sourceTypeBase.equals(resultTypeBase) && isCovariantParametrizedType(source)) {
List<Type> sourceTypeParameters = source.getTypeParameters();
List<Type> resultTypeParameters = result.getTypeParameters();
checkState(sourceTypeParameters.size() == resultTypeParameters.size());
for (int i = 0; i < sourceTypeParameters.size(); i++) {
if (!isTypeOnlyCoercion(sourceTypeParameters.get(i), resultTypeParameters.get(i))) {
return false;
}
}
return true;
}
return false;
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class DynamicFilters method getPlaceholder.
public static Optional<DynamicFilterPlaceholder> getPlaceholder(RowExpression expression) {
if (!(expression instanceof CallExpression)) {
return Optional.empty();
}
CallExpression call = (CallExpression) expression;
List<RowExpression> arguments = call.getArguments();
if (!call.getDisplayName().equals(DynamicFilterPlaceholderFunction.NAME)) {
return Optional.empty();
}
checkArgument(arguments.size() == 3, "invalid arguments count: %s", arguments.size());
RowExpression probeSymbol = arguments.get(0);
RowExpression operatorExpression = arguments.get(1);
checkArgument(operatorExpression instanceof ConstantExpression);
checkArgument(operatorExpression.getType() instanceof VarcharType);
String operator = ((Slice) ((ConstantExpression) operatorExpression).getValue()).toStringUtf8();
RowExpression idExpression = arguments.get(2);
checkArgument(idExpression instanceof ConstantExpression);
checkArgument(idExpression.getType() instanceof VarcharType);
String id = ((Slice) ((ConstantExpression) idExpression).getValue()).toStringUtf8();
OperatorType operatorType = OperatorType.valueOf(operator);
if (operatorType.isComparisonOperator()) {
return Optional.of(new DynamicFilterPlaceholder(id, probeSymbol, operatorType));
}
return Optional.empty();
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class HiveUtil method varcharPartitionKey.
public static Slice varcharPartitionKey(String value, String name, Type columnType) {
Slice partitionKey = Slices.utf8Slice(value);
VarcharType varcharType = (VarcharType) columnType;
if (SliceUtf8.countCodePoints(partitionKey) > varcharType.getLength()) {
throw new PrestoException(HIVE_INVALID_PARTITION_VALUE, format("Invalid partition value '%s' for %s partition key: %s", value, columnType.toString(), name));
}
return partitionKey;
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class HiveWriteUtils method getJavaObjectInspector.
public static ObjectInspector getJavaObjectInspector(Type type) {
if (type.equals(BooleanType.BOOLEAN)) {
return javaBooleanObjectInspector;
} else if (type.equals(BigintType.BIGINT)) {
return javaLongObjectInspector;
} else if (type.equals(IntegerType.INTEGER)) {
return javaIntObjectInspector;
} else if (type.equals(SmallintType.SMALLINT)) {
return javaShortObjectInspector;
} else if (type.equals(TinyintType.TINYINT)) {
return javaByteObjectInspector;
} else if (type.equals(RealType.REAL)) {
return javaFloatObjectInspector;
} else if (type.equals(DoubleType.DOUBLE)) {
return javaDoubleObjectInspector;
} else if (type instanceof VarcharType) {
return writableStringObjectInspector;
} else if (type instanceof CharType) {
return writableHiveCharObjectInspector;
} else if (type.equals(VarbinaryType.VARBINARY)) {
return javaByteArrayObjectInspector;
} else if (type.equals(DateType.DATE)) {
return javaDateObjectInspector;
} else if (type.equals(TimestampType.TIMESTAMP)) {
return javaTimestampObjectInspector;
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
} else if (isArrayType(type)) {
return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
} else if (isMapType(type)) {
ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
} else if (isRowType(type)) {
return ObjectInspectorFactory.getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toList()), type.getTypeParameters().stream().map(HiveWriteUtils::getJavaObjectInspector).collect(toList()));
}
throw new IllegalArgumentException("unsupported type: " + type);
}
Aggregations