Skip to content

Commit

Permalink
Handle unknown type in array contains
Browse files Browse the repository at this point in the history
  • Loading branch information
nezihyigitbasi authored and cberner committed Nov 5, 2015
1 parent d1977c3 commit 046a96f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Expand Up @@ -30,6 +30,7 @@
import io.airlift.slice.Slice;

import java.lang.invoke.MethodHandle;
import java.util.List;
import java.util.Map;

import static com.facebook.presto.metadata.FunctionType.SCALAR;
Expand All @@ -47,6 +48,7 @@ public final class ArrayContains
private static final TypeSignature RETURN_TYPE = parseTypeSignature(StandardTypes.BOOLEAN);
private static final String FUNCTION_NAME = "contains";
private static final Signature SIGNATURE = new Signature(FUNCTION_NAME, SCALAR, ImmutableList.of(comparableTypeParameter("T")), StandardTypes.BOOLEAN, ImmutableList.of("array<T>", "T"), false);
private static final MethodHandle METHOD_HANDLE_UNKNOWN = methodHandle(ArrayContains.class, "arrayWithUnknownType", Type.class, MethodHandle.class, Block.class, Void.class);

@Override
public Signature getSignature()
Expand Down Expand Up @@ -78,11 +80,27 @@ public FunctionInfo specialize(Map<String, Type> types, int arity, TypeManager t
Type type = types.get("T");
TypeSignature valueType = type.getTypeSignature();
TypeSignature arrayType = parameterizedTypeName(StandardTypes.ARRAY, valueType);
MethodHandle methodHandle = methodHandle(ArrayContains.class, "contains", Type.class, MethodHandle.class, Block.class, type.getJavaType());

MethodHandle methodHandle;
MethodHandle equalsHandle = functionRegistry.getScalarFunctionImplementation(internalOperator(OperatorType.EQUAL, BooleanType.BOOLEAN, ImmutableList.of(type, type))).getMethodHandle();

List<Boolean> nullableArguments;
if (type.getJavaType() == void.class) {
nullableArguments = ImmutableList.of(false, true);
methodHandle = METHOD_HANDLE_UNKNOWN;
}
else {
nullableArguments = ImmutableList.of(false, false);
methodHandle = methodHandle(ArrayContains.class, "contains", Type.class, MethodHandle.class, Block.class, type.getJavaType());
}

Signature signature = new Signature(FUNCTION_NAME, SCALAR, RETURN_TYPE, arrayType, valueType);
return new FunctionInfo(signature, getDescription(), isHidden(), methodHandle.bindTo(type).bindTo(equalsHandle), isDeterministic(), true, nullableArguments);
}

return new FunctionInfo(signature, getDescription(), isHidden(), methodHandle.bindTo(type).bindTo(equalsHandle), isDeterministic(), true, ImmutableList.of(false, false));
public static Boolean arrayWithUnknownType(Type elementType, MethodHandle equals, Block arrayBlock, Void value)
{
return null;
}

public static Boolean contains(Type elementType, MethodHandle equals, Block arrayBlock, Block value)
Expand Down
Expand Up @@ -235,6 +235,7 @@ public void testArrayContains()
assertFunction("CONTAINS(ARRAY [ARRAY [1, 2], ARRAY [3, 4]], ARRAY [3])", BOOLEAN, false);
assertFunction("CONTAINS(ARRAY [CAST (NULL AS BIGINT)], 1)", BOOLEAN, null);
assertFunction("CONTAINS(ARRAY [CAST (NULL AS BIGINT)], NULL)", BOOLEAN, null);
assertFunction("CONTAINS(ARRAY [], NULL)", BOOLEAN, null);
}

@Test
Expand Down

0 comments on commit 046a96f

Please sign in to comment.