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.type;
 
 
 import java.util.List;
 import java.util.Map;
 
 import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
 import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public final class TypeUtils
 {
     public static final int EXPECTED_ARRAY_SIZE = 1024;
     public static final int NULL_HASH_CODE = 0;
 
     private TypeUtils()
     {
     }
 
     public static int expectedValueSize(Type typeint defaultSize)
     {
         if (type instanceof FixedWidthType) {
             return ((FixedWidthTypetype).getFixedSize();
         }
         return defaultSize;
     }
 
     public static int hashPosition(Type typeBlock blockint position)
     {
         if (block.isNull(position)) {
             return ;
         }
         return type.hash(blockposition);
     }
 
     public static long hashPosition(MethodHandle methodHandleType typeBlock blockint position)
     {
         if (block.isNull(position)) {
             return ;
         }
         try {
             if (type.getJavaType() == boolean.class) {
                 return (longmethodHandle.invoke(type.getBoolean(blockposition));
             }
             else if (type.getJavaType() == long.class) {
                 return (longmethodHandle.invoke(type.getLong(blockposition));
             }
             else if (type.getJavaType() == double.class) {
                 return (longmethodHandle.invoke(type.getDouble(blockposition));
             }
             else if (type.getJavaType() == Slice.class) {
                 return (longmethodHandle.invoke(type.getSlice(blockposition));
             }
             else {
                 throw new UnsupportedOperationException("Unsupported native container type: " + type.getJavaType() + " with type " + type.getTypeSignature());
             }
         }
         catch (Throwable throwable) {
             throw Throwables.propagate(throwable);
         }
     }
    public static boolean positionEqualsPosition(Type typeBlock leftBlockint leftPositionBlock rightBlockint rightPosition)
    {
        boolean leftIsNull = leftBlock.isNull(leftPosition);
        boolean rightIsNull = rightBlock.isNull(rightPosition);
        if (leftIsNull || rightIsNull) {
            return leftIsNull && rightIsNull;
        }
        return type.equalTo(leftBlockleftPositionrightBlockrightPosition);
    }
    public static List<TyperesolveTypes(List<TypeSignaturetypeNamesTypeManager typeManager)
    {
        return typeNames.stream()
                .map((TypeSignature type) -> checkNotNull(typeManager.getType(type), "Type '%s' not found"type))
                .collect(toImmutableList());
    }
    public static TypeSignature parameterizedTypeName(String baseTypeSignature... argumentNames)
    {
        return new TypeSignature(base, ImmutableList.copyOf(argumentNames), ImmutableList.of());
    }
    public static int getHashPosition(List<? extends TypehashTypesBlock[] hashBlocksint position)
    {
        int[] hashChannels = new int[hashBlocks.length];
        for (int i = 0; i < hashBlocks.lengthi++) {
            hashChannels[i] = i;
        }
        HashGenerator hashGenerator = new InterpretedHashGenerator(ImmutableList.copyOf(hashTypes), hashChannels);
        Page page = new Page(hashBlocks);
        return hashGenerator.hashPosition(positionpage);
    }
    public static Block getHashBlock(List<? extends TypehashTypesBlock... hashBlocks)
    {
        checkArgument(hashTypes.size() == hashBlocks.length);
        int[] hashChannels = new int[hashBlocks.length];
        for (int i = 0; i < hashBlocks.lengthi++) {
            hashChannels[i] = i;
        }
        HashGenerator hashGenerator = new InterpretedHashGenerator(ImmutableList.copyOf(hashTypes), hashChannels);
        int positionCount = hashBlocks[0].getPositionCount();
        BlockBuilder builder = .createFixedSizeBlockBuilder(positionCount);
        Page page = new Page(hashBlocks);
        for (int i = 0; i < positionCounti++) {
            .writeLong(builderhashGenerator.hashPosition(ipage));
        }
        return builder.build();
    }
    public static Page getHashPage(Page pageList<? extends TypetypesList<IntegerhashChannels)
    {
        Block[] blocks = Arrays.copyOf(page.getBlocks(), page.getChannelCount() + 1);
        ImmutableList.Builder<TypehashTypes = ImmutableList.builder();
        Block[] hashBlocks = new Block[hashChannels.size()];
        int hashBlockIndex = 0;
        for (int channel : hashChannels) {
            hashTypes.add(types.get(channel));
            hashBlocks[hashBlockIndex++] = blocks[channel];
        }
        blocks[page.getChannelCount()] = getHashBlock(hashTypes.build(), hashBlocks);
        return new Page(blocks);
    }
    public static Block createBlock(Type typeObject element)
    {
        BlockBuilder blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), 1, );
        appendToBlockBuilder(typeelementblockBuilder);
        return blockBuilder.build();
    }
    public static void appendToBlockBuilder(Type typeObject elementBlockBuilder blockBuilder)
    {
        Class<?> javaType = type.getJavaType();
        if (element == null) {
            blockBuilder.appendNull();
        }
        // TODO: This should be removed. Functions that rely on this functionality should
        // be doing the conversion themselves.
        else if (type.getTypeSignature().getBase().equals(.) && element instanceof Iterable<?>) {
            BlockBuilder subBlockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus(), );
            for (Object subElement : (Iterable<?>) element) {
                appendToBlockBuilder(type.getTypeParameters().get(0), subElementsubBlockBuilder);
            }
            type.writeSlice(blockBuilderbuildStructuralSlice(subBlockBuilder));
        }
        else if (type.getTypeSignature().getBase().equals(.) && element instanceof Iterable<?>) {
            BlockBuilder subBlockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus(), );
            int field = 0;
            for (Object subElement : (Iterable<?>) element) {
                appendToBlockBuilder(type.getTypeParameters().get(field), subElementsubBlockBuilder);
                field++;
            }
            type.writeSlice(blockBuilderbuildStructuralSlice(subBlockBuilder));
        }
        else if (type.getTypeSignature().getBase().equals(.) && element instanceof Map<?, ?>) {
            BlockBuilder subBlockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus(), );
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) element).entrySet()) {
                appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
                appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
            }
            type.writeSlice(blockBuilderbuildStructuralSlice(subBlockBuilder));
        }
        else if (javaType == boolean.class) {
            type.writeBoolean(blockBuilder, (Booleanelement);
        }
        else if (javaType == long.class) {
            type.writeLong(blockBuilder, ((Numberelement).longValue());
        }
        else if (javaType == double.class) {
            type.writeDouble(blockBuilder, ((Numberelement).doubleValue());
        }
        else if (javaType == Slice.class) {
            if (element instanceof String) {
                type.writeSlice(blockBuilder, Slices.utf8Slice(element.toString()));
            }
            else if (element instanceof byte[]) {
                type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) element));
            }
            else {
                type.writeSlice(blockBuilder, (Sliceelement);
            }
        }
        else {
            throw new PrestoException(, String.format("Unexpected type %s"javaType.getName()));
        }
    }
    public static Object castValue(Type typeBlock blockint position)
    {
        Class<?> javaType = type.getJavaType();
        if (block.isNull(position)) {
            return null;
        }
        else if (javaType == boolean.class) {
            return type.getBoolean(blockposition);
        }
        else if (javaType == long.class) {
            return type.getLong(blockposition);
        }
        else if (javaType == double.class) {
            return type.getDouble(blockposition);
        }
        else if (type.getJavaType() == Slice.class) {
            return type.getSlice(blockposition);
        }
        else {
            throw new PrestoException(, String.format("Unexpected type %s"javaType.getName()));
        }
    }
    public static Slice buildStructuralSlice(BlockBuilder builder)
    {
        return buildStructuralSlice(builder.build());
    }
    public static Slice buildStructuralSlice(Block block)
    {
        BlockEncoding encoding = block.getEncoding();
        DynamicSliceOutput output = new DynamicSliceOutput(encoding.getEstimatedSize(block));
        encoding.writeBlock(outputblock);
        return output.slice();
    }
    public static Block readStructuralBlock(Slice slice)
    {
        return new VariableWidthBlockEncoding().readBlock(slice.getInput());
    }
    public static void checkElementNotNull(boolean isNullString errorMsg)
    {
        if (isNull) {
            throw new PrestoException(errorMsg);
        }
    }
New to GrepCode? Check out our FAQ X