Source:
<code>public static Block distinct(@TypeParameter("E") Type type, @SqlType("array(E)") Block array) { int arrayLength = array.getPositionCount(); if (arrayLength < 2) { return array; } if (arrayLength == 2) { if (TypeUtils.positionEqualsPosition(type, array, 0, array, 1)) { return array.getSingleValueBlock(0); } else { return array; } } TypedSet typedSet = new TypedSet(type, arrayLength, "array_distinct"); BlockBuilder distinctElementBlockBuilder; int position = 0; if (array.mayHaveNull()) { while (position < arrayLength && typedSet.add(array, position)) { position++; } if (position == arrayLength) { return array; } distinctElementBlockBuilder = type.createBlockBuilder(null, arrayLength); for (int i = 0; i < position; i++) { type.appendTo(array, i, distinctElementBlockBuilder); } <START> for (position++; position < arrayLength; position++) { <END> if (typedSet.add(array, position)) { type.appendTo(array, position, distinctElementBlockBuilder); } } } else { while (position < arrayLength && typedSet.addNonNull(array, position)) { position++; } if (position == arrayLength) { return array; } distinctElementBlockBuilder = type.createBlockBuilder(null, arrayLength); for (int i = 0; i < position; i++) { type.appendTo(array, i, distinctElementBlockBuilder); } for (position++; position < arrayLength; position++) { if (typedSet.addNonNull(array, position)) { type.appendTo(array, position, distinctElementBlockBuilder); } } } return distinctElementBlockBuilder.build(); }</code><technical_language>java for (int i = position + 1; position < arrayLength; i++) { ditto for other implementations</technical_language>

Target:
public static Block distinct(@TypeParameter("E") Type type, @SqlType("array(E)") Block array) { int arrayLength = array.getPositionCount(); if (arrayLength < 2) { return array; } if (arrayLength == 2) { if (TypeUtils.positionEqualsPosition(type, array, 0, array, 1)) { return array.getSingleValueBlock(0); } else { return array; } } TypedSet typedSet = new TypedSet(type, arrayLength, "array_distinct"); BlockBuilder distinctElementBlockBuilder; if (array.mayHaveNull()) { int firstDuplicatePosition = 0; while (firstDuplicatePosition < arrayLength && typedSet.add(array, firstDuplicatePosition)) { firstDuplicatePosition++; } if (firstDuplicatePosition == arrayLength) { return array; } int position = 0; distinctElementBlockBuilder = type.createBlockBuilder(null, arrayLength); while (position < firstDuplicatePosition) { type.appendTo(array, position, distinctElementBlockBuilder); position++; } while (position < arrayLength) { if (typedSet.add(array, position)) { type.appendTo(array, position, distinctElementBlockBuilder); } position++; } } else { int firstDuplicatePosition = 0; while (firstDuplicatePosition < arrayLength && typedSet.addNonNull(array, firstDuplicatePosition)) { firstDuplicatePosition++; } if (firstDuplicatePosition == arrayLength) { return array; } int position = 0; distinctElementBlockBuilder = type.createBlockBuilder(null, arrayLength); while (position < firstDuplicatePosition) { type.appendTo(array, position, distinctElementBlockBuilder); position++; } while (position < arrayLength) { if (typedSet.addNonNull(array, position)) { type.appendTo(array, position, distinctElementBlockBuilder); } position++; } } return distinctElementBlockBuilder.build(); }