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.readStructuralBlock;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
 import static com.google.common.base.Preconditions.checkNotNull;

As defined in ISO/IEC FCD 9075-2 (SQL 2011), section 4.8
 
 public class RowType
         extends AbstractVariableWidthType
 {
     private final List<RowFieldfields;
 
     public RowType(List<TypefieldTypesOptional<List<String>> fieldNames)
     {
         super(new TypeSignature(
                         "row",
                         Lists.transform(fieldTypes, Type::getTypeSignature),
                         fieldNames.orElse(ImmutableList.of()).stream()
                                 .map(Object.class::cast)
                                 .collect(toImmutableList())),
                 Slice.class);
 
         ImmutableList.Builder<RowFieldbuilder = ImmutableList.builder();
         for (int i = 0; i < fieldTypes.size(); i++) {
             int index = i;
             builder.add(new RowField(fieldTypes.get(i), fieldNames.map((names) -> names.get(index))));
         }
          = builder.build();
     }
 
     @Override
     public String getDisplayName()
     {
         // Convert to standard sql name
         List<Stringfields = new ArrayList<>();
         for (int i = 0; i < this..size(); i++) {
             RowField field = this..get(i);
             if (field.getName().isPresent()) {
                 fields.add(field.getName() + " " + field.getType().getDisplayName());
             }
             else {
                 fields.add(field.getType().getDisplayName());
             }
         }
         return "ROW(" + Joiner.on(", ").join(fields) + ")";
     }
 
     @Override
     public Object getObjectValue(ConnectorSession sessionBlock blockint position)
     {
         if (block.isNull(position)) {
             return null;
         }
 
         Slice slice = getSlice(blockposition);
         Block arrayBlock = readStructuralBlock(slice);
         List<Objectvalues = Lists.newArrayListWithCapacity(arrayBlock.getPositionCount());
 
         for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
             values.add(.get(i).getType().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 .stream()
                .map(RowField::getType)
                .collect(toImmutableList());
    }
    public List<RowFieldgetFields()
    {
        return ;
    }
    public static class RowField
    {
        private final Type type;
        private final Optional<Stringname;
        public RowField(Type typeOptional<Stringname)
        {
            this. = checkNotNull(type"type is null");
            this. = checkNotNull(name"name is null");
        }
        public Type getType()
        {
            return ;
        }
        public Optional<StringgetName()
        {
            return ;
        }
    }
    @Override
    public boolean isComparable()
    {
        return Iterables.all(new Predicate<RowField>()
        {
            @Override
            public boolean apply(RowField field)
            {
                return field.getType().isComparable();
            }
        });
    }
    @Override
    public boolean equalTo(Block leftBlockint leftPositionBlock rightBlockint rightPosition)
    {
        Slice leftSlice = getSlice(leftBlockleftPosition);
        Slice rightSlice = getSlice(rightBlockrightPosition);
        Block leftArray = readStructuralBlock(leftSlice);
        Block rightArray = readStructuralBlock(rightSlice);
        for (int i = 0; i < leftArray.getPositionCount(); i++) {
            checkElementNotNull(leftArray.isNull(i));
            checkElementNotNull(rightArray.isNull(i));
            Type fieldType = .get(i).getType();
            if (!fieldType.equalTo(leftArrayirightArrayi)) {
                return false;
            }
        }
        return true;
    }
    @Override
    public int hash(Block blockint position)
    {
        Slice value = getSlice(blockposition);
        Block arrayBlock = readStructuralBlock(value);
        int result = 1;
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            checkElementNotNull(arrayBlock.isNull(i));
            Type elementType = .get(i).getType();
            result = 31 * result + elementType.hash(arrayBlocki);
        }
        return result;
    }
    private static void checkElementNotNull(boolean isNull)
    {
        if (isNull) {
            throw new PrestoException(."ROW comparison not supported for fields with null elements");
        }
    }
New to GrepCode? Check out our FAQ X