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.spi.StandardErrorCode.INTERNAL_ERROR;
 import static com.facebook.presto.type.TypeJsonUtils.createBlock;
 import static com.facebook.presto.type.TypeJsonUtils.stackRepresentationToObject;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static java.lang.String.format;

As defined in ISO/IEC FCD 9075-2 (SQL 2011), section 4.8
 
 public class RowType
         extends AbstractVariableWidthType
 {
     private final List<RowFieldfields;
     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapperProvider().get();
     private static final CollectionType COLLECTION_TYPE = .getTypeFactory().constructCollectionType(List.classObject.class);
 
     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++) {
             final 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 = block.getSlice(position, 0, block.getLength(position));
        return stackRepresentationToObject(sessionslicethis);
    }
    @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 BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus)
    {
        return new VariableWidthBlockBuilder(blockBuilderStatus);
    }
    @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)
    {
        List<ObjectleftRow = extractElements(leftBlockleftPosition);
        List<ObjectrightRow = extractElements(rightBlockrightPosition);
        int nFields = leftRow.size();
        for (int i = 0; i < nFieldsi++) {
            Object leftElement = leftRow.get(i);
            Object rightElement = rightRow.get(i);
            checkElementNotNull(leftElement);
            checkElementNotNull(rightElement);
            Type fieldType = .get(i).getType();
            if (!fieldType.equalTo(createBlock(fieldTypeleftElement), 0, createBlock(fieldTyperightElement), 0)) {
                return false;
            }
        }
        return true;
    }
    @Override
    public int hash(Block blockint position)
    {
        List<Objectelements = extractElements(blockposition);
        int result = 1;
        int nFields = elements.size();
        for (int i = 0; i < nFieldsi++) {
            Object element = elements.get(i);
            checkElementNotNull(element);
            Type elementType = .get(i).getType();
            result = 31 * result + elementType.hash(createBlock(elementTypeelement), 0);
        }
        return result;
    }
    private static void checkElementNotNull(Object element)
    {
        if (element == null) {
            throw new PrestoException(."ROW comparison not supported for rows with null elements");
        }
    }
    private List<ObjectextractElements(Block blockint position)
    {
        Slice value = getSlice(blockposition);
        try {
            return .readValue(value.getBytes(), );
        }
        catch (IOException e) {
            throw new PrestoException(format("Bad native value, '%s'"value.toStringUtf8()), e);
        }
    }
New to GrepCode? Check out our FAQ X