@TypeParameter("T") @SqlType(StandardTypes.BOOLEAN) @Nullable public static Boolean contains( @TypeParameter("T") Type elementType, @OperatorDependency( operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = {"T", "T"}) MethodHandle equals, @SqlType("array(T)") Block arrayBlock, @SqlType("T") Block value) { boolean foundNull = false; for (int i = 0; i < arrayBlock.getPositionCount(); i++) { if (arrayBlock.isNull(i)) { foundNull = true; continue; } try { if ((boolean) equals.invokeExact((Block) elementType.getObject(arrayBlock, i), value)) { return true; } } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, Error.class); Throwables.propagateIfInstanceOf(t, PrestoException.class); throw new PrestoException(INTERNAL_ERROR, t); } } if (foundNull) { return null; } return false; }
@TypeParameter("T") @SqlType(StandardTypes.BIGINT) public static long arrayPosition( @TypeParameter("T") Type type, @OperatorDependency( operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = {"T", "T"}) MethodHandle equalMethodHandle, @SqlType("array(T)") Block array, @SqlType("T") Block element) { int size = array.getPositionCount(); for (int i = 0; i < size; i++) { if (!array.isNull(i)) { Object arrayValue = type.getObject(array, i); try { if ((boolean) equalMethodHandle.invoke(arrayValue, element)) { return i + 1; // result is 1-based (instead of 0) } } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, Error.class); Throwables.propagateIfInstanceOf(t, PrestoException.class); throw new PrestoException(INTERNAL_ERROR, t); } } } return 0; }
public static Object objectElementAt(Type elementType, Block array, long index) { int position = checkedIndexToBlockPosition(array, index); if (array.isNull(position)) { return null; } return elementType.getObject(array, position); }
@UsedByGeneratedCode public static Object objectSubscript(Type elementType, Block array, long index) { checkIndex(array, index); int position = Ints.checkedCast(index - 1); if (array.isNull(position)) { return null; } return elementType.getObject(array, position); }
private static Object getNativeContainerValue(Type type, Block block, int position) { if (block.isNull(position)) { return null; } else if (type.getJavaType() == boolean.class) { return type.getBoolean(block, position); } else if (type.getJavaType() == long.class) { return type.getLong(block, position); } else if (type.getJavaType() == double.class) { return type.getDouble(block, position); } else if (type.getJavaType() == Slice.class) { return type.getSlice(block, position); } else if (type.getJavaType() == Block.class) { return type.getObject(block, position); } else { throw new AssertionError("Unimplemented type: " + type); } }
public static void input(Type type, BlockState state, Block block, int position) { if (state.getBlock() != null) { return; } state.setBlock((Block) type.getObject(block, position)); }