use of io.airlift.slice.SliceOutput in project presto by prestodb.
the class AbstractTestType method assertPositionValue.
private void assertPositionValue(Block block, int position, Object expectedStackValue, long expectedHash, Object expectedObjectValue) {
Object objectValue = type.getObjectValue(SESSION, block, position);
assertEquals(objectValue, expectedObjectValue);
if (objectValue != null) {
assertInstanceOf(objectValue, objectValueType);
}
if (type.isComparable()) {
assertEquals(hashPosition(type, block, position), expectedHash);
} else {
try {
type.hash(block, position);
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException expected) {
}
}
Block expectedBlock = createBlock(type, expectedStackValue);
if (type.isComparable()) {
assertTrue(positionEqualsPosition(type, block, position, block, position));
assertTrue(positionEqualsPosition(type, block, position, expectedBlock, 0));
assertTrue(positionEqualsPosition(type, expectedBlock, 0, block, position));
}
assertEquals(block.isNull(position), expectedStackValue == null);
if (type.isOrderable()) {
assertTrue(ASC_NULLS_FIRST.compareBlockValue(type, block, position, expectedBlock, 0) == 0);
assertTrue(ASC_NULLS_LAST.compareBlockValue(type, block, position, expectedBlock, 0) == 0);
assertTrue(DESC_NULLS_FIRST.compareBlockValue(type, block, position, expectedBlock, 0) == 0);
assertTrue(DESC_NULLS_LAST.compareBlockValue(type, block, position, expectedBlock, 0) == 0);
} else {
try {
type.compareTo(block, position, expectedBlock, 0);
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException expected) {
}
}
verifyInvalidPositionHandling(block);
if (block.isNull(position)) {
if (type.isOrderable() && type.getJavaType() != void.class) {
Block nonNullValue = toBlock(getNonNullValue());
assertTrue(ASC_NULLS_FIRST.compareBlockValue(type, block, position, nonNullValue, 0) < 0);
assertTrue(ASC_NULLS_LAST.compareBlockValue(type, block, position, nonNullValue, 0) > 0);
assertTrue(DESC_NULLS_FIRST.compareBlockValue(type, block, position, nonNullValue, 0) < 0);
assertTrue(DESC_NULLS_LAST.compareBlockValue(type, block, position, nonNullValue, 0) > 0);
}
return;
}
if (type.isOrderable() && expectedStackValue != Boolean.TRUE) {
Block greaterValue = toBlock(getGreaterValue(expectedStackValue));
assertTrue(ASC_NULLS_FIRST.compareBlockValue(type, block, position, greaterValue, 0) < 0);
assertTrue(ASC_NULLS_LAST.compareBlockValue(type, block, position, greaterValue, 0) < 0);
assertTrue(DESC_NULLS_FIRST.compareBlockValue(type, block, position, greaterValue, 0) > 0);
assertTrue(DESC_NULLS_LAST.compareBlockValue(type, block, position, greaterValue, 0) > 0);
}
if (type.getJavaType() == boolean.class) {
assertEquals(type.getBoolean(block, position), expectedStackValue);
try {
type.getLong(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getDouble(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getObject(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
} else if (type.getJavaType() == long.class) {
assertEquals(type.getLong(block, position), expectedStackValue);
try {
type.getBoolean(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getDouble(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getObject(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
} else if (type.getJavaType() == double.class) {
assertEquals(type.getDouble(block, position), expectedStackValue);
try {
type.getBoolean(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getLong(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getObject(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
} else if (type.getJavaType() == Slice.class) {
assertEquals(type.getSlice(block, position), expectedStackValue);
try {
type.getBoolean(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getLong(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getDouble(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getObject(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
} else {
SliceOutput actualSliceOutput = new DynamicSliceOutput(100);
writeBlock(actualSliceOutput, (Block) type.getObject(block, position));
SliceOutput expectedSliceOutput = new DynamicSliceOutput(actualSliceOutput.size());
writeBlock(expectedSliceOutput, (Block) expectedStackValue);
assertEquals(actualSliceOutput.slice(), expectedSliceOutput.slice());
try {
type.getBoolean(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getLong(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getDouble(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getSlice(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
}
}
use of io.airlift.slice.SliceOutput in project presto by prestodb.
the class PagesResponseWriter method writeTo.
@Override
public void writeTo(List<SerializedPage> serializedPages, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream output) throws IOException, WebApplicationException {
try {
SliceOutput sliceOutput = new OutputStreamSliceOutput(output);
writeSerializedPages(sliceOutput, serializedPages);
// We use flush instead of close, because the underlying stream would be closed and that is not allowed.
sliceOutput.flush();
} catch (RuntimeIOException e) {
// This is not a "server" problem so we don't want to log this
if (!(e.getCause() instanceof EOFException)) {
throw e;
}
}
}
use of io.airlift.slice.SliceOutput in project presto by prestodb.
the class JoniRegexpFunctions method regexpReplace.
@Description("replaces substrings matching a regular expression by given string")
@ScalarFunction
@LiteralParameters({ "x", "y", "z" })
// to get the formula: x + max(x * y / 2, y) * (x + 1)
@Constraint(variable = "z", expression = "min(2147483647, x + max(x * y / 2, y) * (x + 1))")
@SqlType("varchar(z)")
public static Slice regexpReplace(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern, @SqlType("varchar(y)") Slice replacement) {
Matcher matcher = pattern.matcher(source.getBytes());
SliceOutput sliceOutput = new DynamicSliceOutput(source.length() + replacement.length() * 5);
int lastEnd = 0;
// nextStart is the same as lastEnd, unless the last match was zero-width. In such case, nextStart is lastEnd + 1.
int nextStart = 0;
while (true) {
int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
if (offset == -1) {
break;
}
if (matcher.getEnd() == matcher.getBegin()) {
nextStart = matcher.getEnd() + 1;
} else {
nextStart = matcher.getEnd();
}
Slice sliceBetweenReplacements = source.slice(lastEnd, matcher.getBegin() - lastEnd);
lastEnd = matcher.getEnd();
sliceOutput.appendBytes(sliceBetweenReplacements);
appendReplacement(sliceOutput, source, pattern, matcher.getEagerRegion(), replacement);
}
sliceOutput.appendBytes(source.slice(lastEnd, source.length() - lastEnd));
return sliceOutput.slice();
}
use of io.airlift.slice.SliceOutput in project presto by prestodb.
the class TestRcFileReaderManual method testNoStartSync.
@Test
public void testNoStartSync() throws Exception {
SliceOutput output = new DynamicSliceOutput(10 * 1024);
List<Segment> segments = ImmutableList.of(writeSegment(output, ImmutableList.of(ImmutableList.of(0, 2, 3, 4), ImmutableList.of(10, 12, 13))), writeSegment(output, ImmutableList.of(ImmutableList.of(20, 22), ImmutableList.of(30, 33), ImmutableList.of(40, 44))), writeSegment(output, ImmutableList.of(ImmutableList.of(100, 101, 102))));
assertFileSegments(output.slice(), segments);
}
use of io.airlift.slice.SliceOutput in project presto by prestodb.
the class LiteralInterpreter method toExpression.
public static Expression toExpression(Object object, Type type) {
requireNonNull(type, "type is null");
if (object instanceof Expression) {
return (Expression) object;
}
if (object == null) {
if (type.equals(UNKNOWN)) {
return new NullLiteral();
}
return new Cast(new NullLiteral(), type.getTypeSignature().toString(), false, true);
}
if (type.equals(INTEGER)) {
return new LongLiteral(object.toString());
}
if (type.equals(BIGINT)) {
LongLiteral expression = new LongLiteral(object.toString());
if (expression.getValue() >= Integer.MIN_VALUE && expression.getValue() <= Integer.MAX_VALUE) {
return new GenericLiteral("BIGINT", object.toString());
}
return new LongLiteral(object.toString());
}
checkArgument(Primitives.wrap(type.getJavaType()).isInstance(object), "object.getClass (%s) and type.getJavaType (%s) do not agree", object.getClass(), type.getJavaType());
if (type.equals(DOUBLE)) {
Double value = (Double) object;
// When changing this, don't forget about similar code for REAL below
if (value.isNaN()) {
return new FunctionCall(QualifiedName.of("nan"), ImmutableList.of());
} else if (value.equals(Double.NEGATIVE_INFINITY)) {
return ArithmeticUnaryExpression.negative(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()));
} else if (value.equals(Double.POSITIVE_INFINITY)) {
return new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of());
} else {
return new DoubleLiteral(object.toString());
}
}
if (type.equals(REAL)) {
Float value = intBitsToFloat(((Long) object).intValue());
// WARNING for ORC predicate code as above (for double)
if (value.isNaN()) {
return new Cast(new FunctionCall(QualifiedName.of("nan"), ImmutableList.of()), StandardTypes.REAL);
} else if (value.equals(Float.NEGATIVE_INFINITY)) {
return ArithmeticUnaryExpression.negative(new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL));
} else if (value.equals(Float.POSITIVE_INFINITY)) {
return new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL);
} else {
return new GenericLiteral("REAL", value.toString());
}
}
if (type instanceof VarcharType) {
if (object instanceof String) {
object = Slices.utf8Slice((String) object);
}
if (object instanceof Slice) {
Slice value = (Slice) object;
int length = SliceUtf8.countCodePoints(value);
if (length == ((VarcharType) type).getLength()) {
return new StringLiteral(value.toStringUtf8());
}
return new Cast(new StringLiteral(value.toStringUtf8()), type.getDisplayName(), false, true);
}
throw new IllegalArgumentException("object must be instance of Slice or String when type is VARCHAR");
}
if (type.equals(BOOLEAN)) {
return new BooleanLiteral(object.toString());
}
if (object instanceof Block) {
SliceOutput output = new DynamicSliceOutput(((Block) object).getSizeInBytes());
BlockSerdeUtil.writeBlock(output, (Block) object);
object = output.slice();
// This if condition will evaluate to true: object instanceof Slice && !type.equals(VARCHAR)
}
if (object instanceof Slice) {
// HACK: we need to serialize VARBINARY in a format that can be embedded in an expression to be
// able to encode it in the plan that gets sent to workers.
// We do this by transforming the in-memory varbinary into a call to from_base64(<base64-encoded value>)
FunctionCall fromBase64 = new FunctionCall(QualifiedName.of("from_base64"), ImmutableList.of(new StringLiteral(VarbinaryFunctions.toBase64((Slice) object).toStringUtf8())));
Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
return new FunctionCall(QualifiedName.of(signature.getName()), ImmutableList.of(fromBase64));
}
Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
Expression rawLiteral = toExpression(object, FunctionRegistry.typeForMagicLiteral(type));
return new FunctionCall(QualifiedName.of(signature.getName()), ImmutableList.of(rawLiteral));
}
Aggregations