Search in sources :

Example 1 with IS_DISTINCT_FROM

use of com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM in project presto by prestodb.

the class MapDistinctFromOperator method isDistinctFrom.

@TypeParameter("K")
@TypeParameter("V")
@SqlType(StandardTypes.BOOLEAN)
public static boolean isDistinctFrom(@OperatorDependency(operator = EQUAL, argumentTypes = { "K", "K" }) MethodHandle keyEqualsFunction, @OperatorDependency(operator = HASH_CODE, argumentTypes = { "K" }) MethodHandle keyHashcodeFunction, @OperatorDependency(operator = IS_DISTINCT_FROM, argumentTypes = { "V", "V" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = FAIL_ON_NULL)) MethodHandle valueDistinctFromFunction, @TypeParameter("map(K, V)") Type mapType, @SqlType("map(K,V)") Block leftMapBlock, @IsNull boolean leftMapNull, @SqlType("map(K,V)") Block rightMapBlock, @IsNull boolean rightMapNull) {
    if (leftMapNull != rightMapNull) {
        return true;
    }
    if (leftMapNull) {
        return false;
    }
    Type keyType = ((MapType) mapType).getKeyType();
    MethodHandle keyBlockEqualsFunction = compose(keyEqualsFunction, nativeValueGetter(keyType));
    MethodHandle keyBlockHashCodeFunction = compose(keyHashcodeFunction, nativeValueGetter(keyType));
    // Note that we compare to NOT distinct here and so negate the result.
    return !MapGenericEquality.genericEqual(keyType, keyHashcodeFunction, keyBlockEqualsFunction, keyBlockHashCodeFunction, leftMapBlock, rightMapBlock, (leftMapIndex, rightMapIndex) -> !(boolean) valueDistinctFromFunction.invokeExact(leftMapBlock, leftMapIndex, rightMapBlock, rightMapIndex));
}
Also used : Convention(com.facebook.presto.spi.function.Convention) HASH_CODE(com.facebook.presto.common.function.OperatorType.HASH_CODE) MethodHandle(java.lang.invoke.MethodHandle) MapType(com.facebook.presto.common.type.MapType) StandardTypes(com.facebook.presto.common.type.StandardTypes) OperatorDependency(com.facebook.presto.spi.function.OperatorDependency) TypeParameter(com.facebook.presto.spi.function.TypeParameter) BLOCK_POSITION(com.facebook.presto.spi.function.InvocationConvention.InvocationArgumentConvention.BLOCK_POSITION) MethodHandleUtil.compose(com.facebook.presto.common.block.MethodHandleUtil.compose) BlockPosition(com.facebook.presto.spi.function.BlockPosition) IS_DISTINCT_FROM(com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM) BlockIndex(com.facebook.presto.spi.function.BlockIndex) MethodHandleUtil.nativeValueGetter(com.facebook.presto.common.block.MethodHandleUtil.nativeValueGetter) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) IsNull(com.facebook.presto.spi.function.IsNull) EQUAL(com.facebook.presto.common.function.OperatorType.EQUAL) FAIL_ON_NULL(com.facebook.presto.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) Block(com.facebook.presto.common.block.Block) Type(com.facebook.presto.common.type.Type) SqlType(com.facebook.presto.spi.function.SqlType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) SqlType(com.facebook.presto.spi.function.SqlType) MapType(com.facebook.presto.common.type.MapType) MethodHandle(java.lang.invoke.MethodHandle) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 2 with IS_DISTINCT_FROM

use of com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM in project presto by prestodb.

the class TestPolymorphicScalarFunction method testSelectsMultipleChoiceWithBlockPosition.

@Test
public void testSelectsMultipleChoiceWithBlockPosition() throws Throwable {
    Signature signature = SignatureBuilder.builder().kind(SCALAR).operatorType(IS_DISTINCT_FROM).argumentTypes(DECIMAL_SIGNATURE, DECIMAL_SIGNATURE).returnType(parseTypeSignature(BOOLEAN)).build();
    SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class, IS_DISTINCT_FROM).signature(signature).deterministic(true).choice(choice -> choice.argumentProperties(valueTypeArgumentProperty(USE_NULL_FLAG), valueTypeArgumentProperty(USE_NULL_FLAG)).implementation(methodsGroup -> methodsGroup.methods("shortShort", "longLong"))).choice(choice -> choice.argumentProperties(valueTypeArgumentProperty(BLOCK_AND_POSITION), valueTypeArgumentProperty(BLOCK_AND_POSITION)).implementation(methodsGroup -> methodsGroup.methodWithExplicitJavaTypes("blockPositionLongLong", asList(Optional.of(Slice.class), Optional.of(Slice.class))).methodWithExplicitJavaTypes("blockPositionShortShort", asList(Optional.of(long.class), Optional.of(long.class))))).build();
    BuiltInScalarFunctionImplementation functionImplementation = function.specialize(SHORT_DECIMAL_BOUND_VARIABLES, 2, FUNCTION_AND_TYPE_MANAGER);
    assertEquals(functionImplementation.getAllChoices().size(), 2);
    assertEquals(functionImplementation.getAllChoices().get(0).getArgumentProperties(), Collections.nCopies(2, valueTypeArgumentProperty(USE_NULL_FLAG)));
    assertEquals(functionImplementation.getAllChoices().get(1).getArgumentProperties(), Collections.nCopies(2, valueTypeArgumentProperty(BLOCK_AND_POSITION)));
    Block block1 = new LongArrayBlock(0, Optional.empty(), new long[0]);
    Block block2 = new LongArrayBlock(0, Optional.empty(), new long[0]);
    assertFalse((boolean) functionImplementation.getAllChoices().get(1).getMethodHandle().invoke(block1, 0, block2, 0));
    functionImplementation = function.specialize(LONG_DECIMAL_BOUND_VARIABLES, 2, FUNCTION_AND_TYPE_MANAGER);
    assertTrue((boolean) functionImplementation.getAllChoices().get(1).getMethodHandle().invoke(block1, 0, block2, 0));
}
Also used : StandardTypes(com.facebook.presto.common.type.StandardTypes) Slice(io.airlift.slice.Slice) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) IS_DISTINCT_FROM(com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TypeSignature(com.facebook.presto.common.type.TypeSignature) MAX_SHORT_PRECISION(com.facebook.presto.common.type.Decimals.MAX_SHORT_PRECISION) ADD(com.facebook.presto.common.function.OperatorType.ADD) ImmutableList(com.google.common.collect.ImmutableList) Arrays.asList(java.util.Arrays.asList) Slices(io.airlift.slice.Slices) Math.toIntExact(java.lang.Math.toIntExact) VARCHAR_TO_VARCHAR_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_VARCHAR_RETURN_VALUE) Assert.assertFalse(org.testng.Assert.assertFalse) BOOLEAN(com.facebook.presto.common.type.StandardTypes.BOOLEAN) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) ImmutableSet(com.google.common.collect.ImmutableSet) Signature.comparableWithVariadicBound(com.facebook.presto.spi.function.Signature.comparableWithVariadicBound) ImmutableMap(com.google.common.collect.ImmutableMap) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) BLOCK_AND_POSITION(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.BLOCK_AND_POSITION) USE_NULL_FLAG(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_NULL_FLAG) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) VARCHAR(com.facebook.presto.common.type.StandardTypes.VARCHAR) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) VARCHAR_TO_BIGINT_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_BIGINT_RETURN_VALUE) Signature(com.facebook.presto.spi.function.Signature) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) Block(com.facebook.presto.common.block.Block) Collections(java.util.Collections) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) Block(com.facebook.presto.common.block.Block) Test(org.testng.annotations.Test)

Aggregations

Block (com.facebook.presto.common.block.Block)2 IS_DISTINCT_FROM (com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM)2 StandardTypes (com.facebook.presto.common.type.StandardTypes)2 LongArrayBlock (com.facebook.presto.common.block.LongArrayBlock)1 MethodHandleUtil.compose (com.facebook.presto.common.block.MethodHandleUtil.compose)1 MethodHandleUtil.nativeValueGetter (com.facebook.presto.common.block.MethodHandleUtil.nativeValueGetter)1 ADD (com.facebook.presto.common.function.OperatorType.ADD)1 EQUAL (com.facebook.presto.common.function.OperatorType.EQUAL)1 HASH_CODE (com.facebook.presto.common.function.OperatorType.HASH_CODE)1 MAX_SHORT_PRECISION (com.facebook.presto.common.type.Decimals.MAX_SHORT_PRECISION)1 MapType (com.facebook.presto.common.type.MapType)1 BOOLEAN (com.facebook.presto.common.type.StandardTypes.BOOLEAN)1 VARCHAR (com.facebook.presto.common.type.StandardTypes.VARCHAR)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 FunctionAndTypeManager.createTestFunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager)1 VARCHAR_TO_BIGINT_RETURN_VALUE (com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_BIGINT_RETURN_VALUE)1 VARCHAR_TO_VARCHAR_RETURN_VALUE (com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_VARCHAR_RETURN_VALUE)1 BuiltInScalarFunctionImplementation (com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation)1