Search in sources :

Example 1 with UnknownType

use of com.facebook.presto.common.type.UnknownType 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.getSqlFunctionProperties(), 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 instanceof UnknownType)) {
            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(blockEncodingSerde, actualSliceOutput, (Block) type.getObject(block, position));
        SliceOutput expectedSliceOutput = new DynamicSliceOutput(actualSliceOutput.size());
        writeBlock(blockEncodingSerde, 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) {
        }
    }
}
Also used : UnknownType(com.facebook.presto.common.type.UnknownType) SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Slice(io.airlift.slice.Slice) BlockSerdeUtil.writeBlock(com.facebook.presto.common.block.BlockSerdeUtil.writeBlock) Block(com.facebook.presto.common.block.Block) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput)

Example 2 with UnknownType

use of com.facebook.presto.common.type.UnknownType in project presto by prestodb.

the class AbstractTestType method createAlternatingNullsBlock.

private Block createAlternatingNullsBlock(Block testBlock) {
    BlockBuilder nullsBlockBuilder = type.createBlockBuilder(null, testBlock.getPositionCount());
    for (int position = 0; position < testBlock.getPositionCount(); position++) {
        if (testBlock.isNull(position)) {
            checkState(type instanceof UnknownType);
            nullsBlockBuilder.appendNull();
        } else if (type.getJavaType() == boolean.class) {
            type.writeBoolean(nullsBlockBuilder, type.getBoolean(testBlock, position));
        } else if (type.getJavaType() == long.class) {
            type.writeLong(nullsBlockBuilder, type.getLong(testBlock, position));
        } else if (type.getJavaType() == double.class) {
            type.writeDouble(nullsBlockBuilder, type.getDouble(testBlock, position));
        } else if (type.getJavaType() == Slice.class) {
            Slice slice = type.getSlice(testBlock, position);
            type.writeSlice(nullsBlockBuilder, slice, 0, slice.length());
        } else {
            type.writeObject(nullsBlockBuilder, type.getObject(testBlock, position));
        }
        nullsBlockBuilder.appendNull();
    }
    return nullsBlockBuilder.build();
}
Also used : UnknownType(com.facebook.presto.common.type.UnknownType) Slice(io.airlift.slice.Slice) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 3 with UnknownType

use of com.facebook.presto.common.type.UnknownType in project presto by prestodb.

the class AbstractTestType method verifyInvalidPositionHandling.

private void verifyInvalidPositionHandling(Block block) {
    try {
        type.getObjectValue(SESSION.getSqlFunctionProperties(), block, -1);
        fail("expected RuntimeException");
    } catch (RuntimeException expected) {
    }
    try {
        type.getObjectValue(SESSION.getSqlFunctionProperties(), block, block.getPositionCount());
        fail("expected RuntimeException");
    } catch (RuntimeException expected) {
    }
    try {
        type.hash(block, -1);
        fail("expected RuntimeException");
    } catch (RuntimeException expected) {
    }
    try {
        type.hash(block, block.getPositionCount());
        fail("expected RuntimeException");
    } catch (RuntimeException expected) {
    }
    if (type.isComparable() && !(type instanceof UnknownType)) {
        Block other = toBlock(getNonNullValue());
        try {
            type.equalTo(block, -1, other, 0);
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
        try {
            type.equalTo(block, block.getPositionCount(), other, 0);
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
    }
    if (type.isOrderable() && !(type instanceof UnknownType)) {
        Block other = toBlock(getNonNullValue());
        try {
            ASC_NULLS_FIRST.compareBlockValue(type, block, -1, other, 0);
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
        try {
            ASC_NULLS_FIRST.compareBlockValue(type, block, block.getPositionCount(), other, 0);
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
    }
    if (type.getJavaType() == boolean.class) {
        try {
            type.getBoolean(block, -1);
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
        try {
            type.getBoolean(block, block.getPositionCount());
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
    } else if (type.getJavaType() == long.class) {
        try {
            type.getLong(block, -1);
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
        try {
            type.getLong(block, block.getPositionCount());
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
    } else if (type.getJavaType() == double.class) {
        try {
            type.getDouble(block, -1);
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
        try {
            type.getDouble(block, block.getPositionCount());
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
    } else if (type.getJavaType() == Slice.class) {
        try {
            type.getSlice(block, -1);
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
        try {
            type.getSlice(block, block.getPositionCount());
            fail("expected RuntimeException");
        } catch (RuntimeException expected) {
        }
    }
}
Also used : UnknownType(com.facebook.presto.common.type.UnknownType) Slice(io.airlift.slice.Slice) BlockSerdeUtil.writeBlock(com.facebook.presto.common.block.BlockSerdeUtil.writeBlock) Block(com.facebook.presto.common.block.Block)

Example 4 with UnknownType

use of com.facebook.presto.common.type.UnknownType in project presto by prestodb.

the class ArrayJoin method specializeArrayJoin.

private static BuiltInScalarFunctionImplementation specializeArrayJoin(Map<String, Type> types, FunctionAndTypeManager functionAndTypeManager, List<Boolean> nullableArguments, MethodHandle methodHandleStack, MethodHandle methodHandleProvidedBlock) {
    Type type = types.get("T");
    List<ArgumentProperty> argumentProperties = nullableArguments.stream().map(nullable -> nullable ? valueTypeArgumentProperty(USE_BOXED_TYPE) : valueTypeArgumentProperty(RETURN_NULL_ON_NULL)).collect(toImmutableList());
    if (type instanceof UnknownType) {
        return new BuiltInScalarFunctionImplementation(false, argumentProperties, methodHandleStack.bindTo(null), Optional.empty());
    } else {
        try {
            MethodHandle cast = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.lookupCast(CAST, type.getTypeSignature(), VARCHAR_TYPE_SIGNATURE)).getMethodHandle();
            MethodHandle getter;
            Class<?> elementType = type.getJavaType();
            if (elementType == boolean.class) {
                getter = GET_BOOLEAN;
            } else if (elementType == double.class) {
                getter = GET_DOUBLE;
            } else if (elementType == long.class) {
                getter = GET_LONG;
            } else if (elementType == Slice.class) {
                getter = GET_SLICE;
            } else {
                throw new UnsupportedOperationException("Unsupported type: " + elementType.getName());
            }
            // if the cast doesn't take a SqlFunctionProperties, create an adapter that drops the provided session
            if (cast.type().parameterArray()[0] != SqlFunctionProperties.class) {
                cast = MethodHandles.dropArguments(cast, 0, SqlFunctionProperties.class);
            }
            // Adapt a target cast that takes (SqlFunctionProperties, ?) to one that takes (Block, int, SqlFunctionProperties), which will be invoked by the implementation
            // The first two arguments (Block, int) are filtered through the element type's getXXX method to produce the underlying value that needs to be passed to
            // the cast.
            cast = MethodHandles.permuteArguments(cast, MethodType.methodType(Slice.class, cast.type().parameterArray()[1], cast.type().parameterArray()[0]), 1, 0);
            cast = MethodHandles.dropArguments(cast, 1, int.class);
            cast = MethodHandles.dropArguments(cast, 1, Block.class);
            cast = MethodHandles.foldArguments(cast, getter.bindTo(type));
            MethodHandle targetStack = MethodHandles.insertArguments(methodHandleStack, 0, cast);
            MethodHandle targetProvidedBlock = MethodHandles.insertArguments(methodHandleProvidedBlock, 0, cast);
            return new BuiltInScalarFunctionImplementation(ImmutableList.of(new ScalarFunctionImplementationChoice(false, argumentProperties, STACK, targetStack, Optional.empty()), new ScalarFunctionImplementationChoice(false, argumentProperties, PROVIDED_BLOCKBUILDER, targetProvidedBlock, Optional.empty())));
        } catch (PrestoException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Input type %s not supported", type), e);
        }
    }
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) STACK(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ReturnPlaceConvention.STACK) MethodHandle(java.lang.invoke.MethodHandle) StandardTypes(com.facebook.presto.common.type.StandardTypes) FunctionKind(com.facebook.presto.spi.function.FunctionKind) Slice(io.airlift.slice.Slice) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) GENERIC_INTERNAL_ERROR(com.facebook.presto.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignature(com.facebook.presto.common.type.TypeSignature) ArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty) DEFAULT_NAMESPACE(com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE) Signature.typeVariable(com.facebook.presto.spi.function.Signature.typeVariable) ImmutableList(com.google.common.collect.ImmutableList) Reflection.methodHandle(com.facebook.presto.util.Reflection.methodHandle) Map(java.util.Map) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) INVALID_FUNCTION_ARGUMENT(com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) UnknownType(com.facebook.presto.common.type.UnknownType) RETURN_NULL_ON_NULL(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.RETURN_NULL_ON_NULL) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) Type(com.facebook.presto.common.type.Type) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) BoundVariables(com.facebook.presto.metadata.BoundVariables) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) PROVIDED_BLOCKBUILDER(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ReturnPlaceConvention.PROVIDED_BLOCKBUILDER) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) MethodHandles(java.lang.invoke.MethodHandles) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) USE_BOXED_TYPE(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_BOXED_TYPE) String.format(java.lang.String.format) SqlFunctionVisibility(com.facebook.presto.spi.function.SqlFunctionVisibility) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) PUBLIC(com.facebook.presto.spi.function.SqlFunctionVisibility.PUBLIC) List(java.util.List) MethodType(java.lang.invoke.MethodType) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) Optional(java.util.Optional) Block(com.facebook.presto.common.block.Block) CAST(com.facebook.presto.metadata.CastType.CAST) ArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) PrestoException(com.facebook.presto.spi.PrestoException) UnknownType(com.facebook.presto.common.type.UnknownType) UnknownType(com.facebook.presto.common.type.UnknownType) Type(com.facebook.presto.common.type.Type) MethodType(java.lang.invoke.MethodType) Slice(io.airlift.slice.Slice) Block(com.facebook.presto.common.block.Block) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

UnknownType (com.facebook.presto.common.type.UnknownType)4 Slice (io.airlift.slice.Slice)4 Block (com.facebook.presto.common.block.Block)3 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)2 BlockSerdeUtil.writeBlock (com.facebook.presto.common.block.BlockSerdeUtil.writeBlock)2 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)1 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)1 SqlFunctionProperties (com.facebook.presto.common.function.SqlFunctionProperties)1 StandardTypes (com.facebook.presto.common.type.StandardTypes)1 Type (com.facebook.presto.common.type.Type)1 TypeSignature (com.facebook.presto.common.type.TypeSignature)1 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)1 VARCHAR (com.facebook.presto.common.type.VarcharType.VARCHAR)1 BoundVariables (com.facebook.presto.metadata.BoundVariables)1 DEFAULT_NAMESPACE (com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE)1 CAST (com.facebook.presto.metadata.CastType.CAST)1 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)1 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)1 ArgumentProperty (com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty)1 ArgumentProperty.valueTypeArgumentProperty (com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty)1