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 static com.facebook.presto.type.TypeUtils.appendToBlockBuilder;
 import static com.facebook.presto.type.TypeUtils.buildStructuralSlice;
 import static com.facebook.presto.type.TypeUtils.checkElementNotNull;
 import static com.facebook.presto.type.TypeUtils.parameterizedTypeName;
 import static com.facebook.presto.type.TypeUtils.readStructuralBlock;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public class ArrayType
         extends AbstractVariableWidthType
 {
     private final Type elementType;
     public static final String ARRAY_NULL_ELEMENT_MSG = "ARRAY comparison not supported for arrays with null elements";
 
     public ArrayType(Type elementType)
     {
         super(parameterizedTypeName("array"elementType.getTypeSignature()), Slice.class);
         this. = checkNotNull(elementType"elementType is null");
     }
 
     public Type getElementType()
     {
         return ;
     }

    
Takes a list of stack types and converts them to the stack representation of an array
 
     public static Slice toStackRepresentation(List<?> valuesType elementType)
     {
         BlockBuilder blockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus(), 0);
         for (Object element : values) {
             appendToBlockBuilder(elementTypeelementblockBuilder);
         }
         return buildStructuralSlice(blockBuilder);
     }
 
     @Override
     public boolean isComparable()
     {
         return .isComparable();
     }
 
     @Override
     public boolean isOrderable()
     {
         return .isOrderable();
     }
 
     @Override
     public boolean equalTo(Block leftBlockint leftPositionBlock rightBlockint rightPosition)
     {
         return compareTo(leftBlockleftPositionrightBlockrightPosition) == 0;
     }
 
     @Override
     public int hash(Block blockint position)
     {
         Slice value = getSlice(blockposition);
         Block array = readStructuralBlock(value);
         List<IntegerhashArray = new ArrayList<>(array.getPositionCount());
         for (int i = 0; i < array.getPositionCount(); i++) {
             checkElementNotNull(array.isNull(i), );
             hashArray.add(.hash(arrayi));
         }
         return Objects.hash(hashArray);
     }
 
     @Override
    public int compareTo(Block leftBlockint leftPositionBlock rightBlockint rightPosition)
    {
        Slice leftSlice = getSlice(leftBlockleftPosition);
        Slice rightSlice = getSlice(rightBlockrightPosition);
        Block leftArray = readStructuralBlock(leftSlice);
        Block rightArray = readStructuralBlock(rightSlice);
        int len = Math.min(leftArray.getPositionCount(), rightArray.getPositionCount());
        int index = 0;
        while (index < len) {
            checkElementNotNull(leftArray.isNull(index), );
            checkElementNotNull(rightArray.isNull(index), );
            int comparison = .compareTo(leftArrayindexrightArrayindex);
            if (comparison != 0) {
                return comparison;
            }
            index++;
        }
        if (index == len) {
            return leftArray.getPositionCount() - rightArray.getPositionCount();
        }
        return 0;
    }
    @Override
    public Object getObjectValue(ConnectorSession sessionBlock blockint position)
    {
        if (block.isNull(position)) {
            return null;
        }
        Slice slice = block.getSlice(position, 0, block.getLength(position));
        Block arrayBlock = readStructuralBlock(slice);
        List<Objectvalues = Lists.newArrayListWithCapacity(arrayBlock.getPositionCount());
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            values.add(.getObjectValue(sessionarrayBlocki));
        }
        return Collections.unmodifiableList(values);
    }
    @Override
    public void appendTo(Block blockint positionBlockBuilder blockBuilder)
    {
        if (block.isNull(position)) {
            blockBuilder.appendNull();
        }
        else {
            block.writeBytesTo(position, 0, block.getLength(position), blockBuilder);
            blockBuilder.closeEntry();
        }
    }
    @Override
    public Slice getSlice(Block blockint position)
    {
        return block.getSlice(position, 0, block.getLength(position));
    }
    @Override
    public void writeSlice(BlockBuilder blockBuilderSlice value)
    {
        writeSlice(blockBuildervalue, 0, value.length());
    }
    @Override
    public void writeSlice(BlockBuilder blockBuilderSlice valueint offsetint length)
    {
        blockBuilder.writeBytes(valueoffsetlength).closeEntry();
    }
    @Override
    public List<TypegetTypeParameters()
    {
        return ImmutableList.of(getElementType());
    }
    @Override
    public String getDisplayName()
    {
        return "array<" + .getDisplayName() + ">";
    }
New to GrepCode? Check out our FAQ X