Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.facebook.presto.operator.scalar;
  /*
   * 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.
  */
 
 import java.util.Map;
 
 import static com.facebook.presto.metadata.FunctionRegistry.operatorInfo;
 import static com.facebook.presto.metadata.OperatorType.EQUAL;
 import static com.facebook.presto.metadata.OperatorType.HASH_CODE;
 import static com.facebook.presto.metadata.Signature.comparableTypeParameter;
 import static com.facebook.presto.spi.StandardErrorCode.INTERNAL_ERROR;
 import static com.facebook.presto.type.TypeUtils.readStructuralBlock;
 import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
 import static com.facebook.presto.type.TypeUtils.castValue;
 import static com.facebook.presto.util.Reflection.methodHandle;
 
 public class MapEqualOperator
         extends ParametricOperator
 {
     public static final MapEqualOperator MAP_EQUAL = new MapEqualOperator();
     private static final TypeSignature RETURN_TYPE = parseTypeSignature(.);
 
     private MapEqualOperator()
     {
         super(, ImmutableList.of(comparableTypeParameter("K"), comparableTypeParameter("V")), ., ImmutableList.of("map<K,V>""map<K,V>"));
     }
 
     @Override
     public FunctionInfo specialize(Map<StringTypetypesint arityTypeManager typeManagerFunctionRegistry functionRegistry)
     {
         Type keyType = types.get("K");
         Type valueType = types.get("V");
 
         Type type = typeManager.getParameterizedType(., ImmutableList.of(keyType.getTypeSignature(), valueType.getTypeSignature()), ImmutableList.of());
         TypeSignature typeSignature = type.getTypeSignature();
 
         MethodHandle keyEqualsFunction = functionRegistry.resolveOperator(, ImmutableList.of(keyTypekeyType)).getMethodHandle();
         MethodHandle keyHashcodeFunction = functionRegistry.resolveOperator(, ImmutableList.of(keyType)).getMethodHandle();
         MethodHandle valueEqualsFunction = functionRegistry.resolveOperator(, ImmutableList.of(valueTypevalueType)).getMethodHandle();
 
         MethodHandle methodHandle = methodHandle(MapEqualOperator.class"equals"MethodHandle.classMethodHandle.classMethodHandle.classType.classType.classSlice.classSlice.class);
         MethodHandle method = methodHandle.bindTo(keyEqualsFunction).bindTo(keyHashcodeFunction).bindTo(valueEqualsFunction).bindTo(keyType).bindTo(valueType);
         return operatorInfo(, ImmutableList.of(typeSignaturetypeSignature), methodtrue, ImmutableList.of(falsefalse));
     }
 
     public static Boolean equals(MethodHandle keyEqualsFunctionMethodHandle keyHashcodeFunctionMethodHandle valueEqualsFunctionType keyTypeType valueTypeSlice leftSlice right)
     {
         Block leftMapBlock = readStructuralBlock(left);
         Block rightMapBlock = readStructuralBlock(right);
 
         Map<KeyWrapperIntegerwrappedLeftMap = new LinkedHashMap<>();
         for (int position = 0; position < leftMapBlock.getPositionCount(); position += 2) {
             wrappedLeftMap.put(new KeyWrapper(castValue(keyTypeleftMapBlockposition), keyEqualsFunctionkeyHashcodeFunction), position + 1);
         }
 
         Map<KeyWrapperIntegerwrappedRightMap = new LinkedHashMap<>();
         for (int position = 0; position < rightMapBlock.getPositionCount(); position += 2) {
             wrappedRightMap.put(new KeyWrapper(castValue(keyTyperightMapBlockposition), keyEqualsFunctionkeyHashcodeFunction), position + 1);
         }
 
         if (wrappedLeftMap.size() != wrappedRightMap.size()) {
             return false;
         }
 
         for (Map.Entry<KeyWrapperIntegerentry : wrappedRightMap.entrySet()) {
             KeyWrapper key = entry.getKey();
             Integer leftValuePosition = wrappedLeftMap.get(key);
             if (leftValuePosition == null) {
                 return false;
             }
 
             Object leftValue = castValue(valueTypeleftMapBlockleftValuePosition);
             if (leftValue == null) {
                 return null;
            }
            Object rightValue = castValue(valueTyperightMapBlockentry.getValue());
            if (rightValue == null) {
                return null;
            }
            try {
                Boolean result = (BooleanvalueEqualsFunction.invoke(leftValuerightValue);
                if (result == null) {
                    return null;
                }
                else if (!result) {
                    return false;
                }
            }
            catch (Throwable t) {
                Throwables.propagateIfInstanceOf(tError.class);
                Throwables.propagateIfInstanceOf(tPrestoException.class);
                throw new PrestoException(t);
            }
        }
        return true;
    }
    private static final class KeyWrapper
    {
        private final Object key;
        private final MethodHandle hashCode;
        private final MethodHandle equals;
        public KeyWrapper(Object keyMethodHandle equalsMethodHandle hashCode)
        {
            this. = key;
            this. = equals;
            this. = hashCode;
        }
        @Override
        public int hashCode()
        {
            try {
                return Long.hashCode((long.invoke());
            }
            catch (Throwable t) {
                Throwables.propagateIfInstanceOf(tError.class);
                Throwables.propagateIfInstanceOf(tPrestoException.class);
                throw new PrestoException(t);
            }
        }
        @Override
        public boolean equals(Object obj)
        {
            if (obj == null || !getClass().equals(obj.getClass())) {
                return false;
            }
            KeyWrapper other = (KeyWrapperobj;
            try {
                return (Boolean.invoke(other.key);
            }
            catch (Throwable t) {
                Throwables.propagateIfInstanceOf(tError.class);
                Throwables.propagateIfInstanceOf(tPrestoException.class);
                throw new PrestoException(t);
            }
        }
    }
New to GrepCode? Check out our FAQ X