Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.operator.scalar;
 
 
 import java.util.Map;
 
 import static com.facebook.presto.metadata.Signature.orderableTypeParameter;
 import static com.facebook.presto.type.TypeUtils.buildStructuralSlice;
 import static com.facebook.presto.type.TypeUtils.parameterizedTypeName;
 import static com.facebook.presto.type.TypeUtils.readStructuralBlock;
 import static com.facebook.presto.util.Reflection.methodHandle;
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.lang.String.format;
 
 public final class ArrayIntersectFunction
         extends ParametricScalar
 {
     public static final ArrayIntersectFunction ARRAY_INTERSECT_FUNCTION = new ArrayIntersectFunction();
     private static final String FUNCTION_NAME = "array_intersect";
     private static final Signature SIGNATURE = new Signature(, ImmutableList.of(orderableTypeParameter("E")), "array<E>", ImmutableList.of("array<E>""array<E>"), falsefalse);
     private static final MethodHandle METHOD_HANDLE = methodHandle(ArrayIntersectFunction.class"intersect"Type.classSlice.classSlice.class);
 
     @Override
     public Signature getSignature()
     {
         return ;
     }
 
     @Override
     public boolean isHidden()
     {
         return false;
     }
 
     @Override
     public boolean isDeterministic()
     {
         return true;
     }
 
     @Override
     public String getDescription()
     {
         return "Intersects elements of the two given arrays";
     }
 
     @Override
     public FunctionInfo specialize(Map<StringTypetypesint arityTypeManager typeManagerFunctionRegistry functionRegistry)
     {
         checkArgument(types.size() == 1, format("%s expects only one argument"));
         TypeSignature typeSignature = parameterizedTypeName("array"types.get("E").getTypeSignature());
         MethodHandle methodHandle = .bindTo(types.get("E"));
         Signature signature = new Signature(typeSignaturetypeSignaturetypeSignature);
         return new FunctionInfo(signaturegetDescription(), isHidden(), methodHandleisDeterministic(), false, ImmutableList.of(falsefalse));
     }
 
     private static IntComparator IntBlockCompare(Type typeBlock block)
     {
         return new AbstractIntComparator()
         {
             @Override
             public int compare(int leftint right)
             {
                 if (block.isNull(left) && block.isNull(right)) {
                     return 0;
                 }
                 if (block.isNull(left)) {
                     return -1;
                 }
                 if (block.isNull(right)) {
                    return 1;
                }
                return type.compareTo(blockleftblockright);
            }
        };
    }
    public static Slice intersect(Type typeSlice leftArraySlice rightArray)
    {
        Block leftBlock = readStructuralBlock(leftArray);
        Block rightBlock = readStructuralBlock(rightArray);
        int leftPositionCount = leftBlock.getPositionCount();
        int rightPositionCount = rightBlock.getPositionCount();
        int[] leftPositions = new int[leftPositionCount];
        int[] rightPositions = new int[rightPositionCount];
        for (int i = 0; i < leftPositionCounti++) {
            leftPositions[i] = i;
        }
        for (int i = 0; i < rightPositionCounti++) {
            rightPositions[i] = i;
        }
        IntArrays.quickSort(leftPositionsIntBlockCompare(typeleftBlock));
        IntArrays.quickSort(rightPositionsIntBlockCompare(typerightBlock));
        BlockBuilder resultBlockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus(), leftBlock.getSizeInBytes());
        int leftCurrentPosition = 0;
        int rightCurrentPosition = 0;
        int leftBasePosition;
        int rightBasePosition;
        while (leftCurrentPosition < leftPositionCount && rightCurrentPosition < rightPositionCount) {
            leftBasePosition = leftCurrentPosition;
            rightBasePosition = rightCurrentPosition;
            int compareValue = type.compareTo(leftBlockleftPositions[leftCurrentPosition], rightBlockrightPositions[rightCurrentPosition]);
            if (compareValue > 0) {
                rightCurrentPosition++;
            }
            else if (compareValue < 0) {
                leftCurrentPosition++;
            }
            else {
                type.appendTo(leftBlockleftPositions[leftCurrentPosition], resultBlockBuilder);
                leftCurrentPosition++;
                rightCurrentPosition++;
                while (leftCurrentPosition < leftPositionCount && type.equalTo(leftBlockleftPositions[leftBasePosition], leftBlockleftPositions[leftCurrentPosition])) {
                    leftCurrentPosition++;
                }
                while (rightCurrentPosition < rightPositionCount && type.equalTo(rightBlockrightPositions[rightBasePosition], rightBlockrightPositions[rightCurrentPosition])) {
                    rightCurrentPosition++;
                }
            }
        }
        return buildStructuralSlice(resultBlockBuilder);
    }
New to GrepCode? Check out our FAQ X