Package com.facebook.presto.split

Source Code of com.facebook.presto.split.PartitionFunction

/*
* 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.split;

import com.facebook.presto.metadata.TablePartition;
import com.facebook.presto.spi.ConnectorColumnHandle;
import com.facebook.presto.spi.ConnectorPartition;
import com.facebook.presto.spi.Domain;
import com.facebook.presto.spi.PartitionKey;
import com.facebook.presto.spi.TupleDomain;
import com.facebook.presto.split.NativeSplitManager.NativePartition;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import io.airlift.slice.Slice;

import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static io.airlift.slice.Slices.utf8Slice;

public class PartitionFunction
        implements Function<TablePartition, ConnectorPartition>
{
    private final Map<String, ConnectorColumnHandle> columnHandles;
    private final Multimap<String, ? extends PartitionKey> allPartitionKeys;

    PartitionFunction(Map<String, ConnectorColumnHandle> columnHandles,
            Multimap<String, ? extends PartitionKey> allPartitionKeys)
    {
        this.columnHandles = checkNotNull(columnHandles, "columnHandles is null");
        this.allPartitionKeys = checkNotNull(allPartitionKeys, "allPartitionKeys is null");
    }

    @Override
    public ConnectorPartition apply(TablePartition tablePartition)
    {
        String partitionName = tablePartition.getPartitionName();

        ImmutableMap.Builder<ConnectorColumnHandle, Domain> builder = ImmutableMap.builder();
        for (PartitionKey partitionKey : allPartitionKeys.get(partitionName)) {
            ConnectorColumnHandle columnHandle = columnHandles.get(partitionKey.getName());
            checkArgument(columnHandles != null, "Invalid partition key for column %s in partition %s", partitionKey.getName(), tablePartition.getPartitionName());

            String value = partitionKey.getValue();
            Class<?> javaType = partitionKey.getType().getJavaType();
            if (javaType == boolean.class) {
                if (value.length() == 0) {
                    builder.put(columnHandle, Domain.singleValue(false));
                }
                else {
                    builder.put(columnHandle, Domain.singleValue(Boolean.parseBoolean(value)));
                }
            }
            else if (javaType == long.class) {
                if (value.length() == 0) {
                    builder.put(columnHandle, Domain.singleValue(0L));
                }
                else {
                    builder.put(columnHandle, Domain.singleValue(Long.parseLong(value)));
                }
            }
            else if (javaType == double.class) {
                if (value.length() == 0) {
                    builder.put(columnHandle, Domain.singleValue(0.0));
                }
                else {
                    builder.put(columnHandle, Domain.singleValue(Double.parseDouble(value)));
                }
            }
            else if (javaType == Slice.class) {
                builder.put(columnHandle, Domain.singleValue(utf8Slice(value)));
            }
        }

        return new NativePartition(tablePartition.getPartitionId(), TupleDomain.withColumnDomains(builder.build()));
    }
}
TOP

Related Classes of com.facebook.presto.split.PartitionFunction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.