All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.facebook.presto.tpch.TpchRecordSet Maven / Gradle / Ivy

The newest version!
/*
 * 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.tpch;

import com.facebook.presto.common.predicate.NullableValue;
import com.facebook.presto.common.predicate.TupleDomain;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.RecordCursor;
import com.facebook.presto.spi.RecordSet;
import com.google.common.collect.ImmutableList;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
import io.airlift.tpch.TpchColumn;
import io.airlift.tpch.TpchColumnType;
import io.airlift.tpch.TpchEntity;
import io.airlift.tpch.TpchTable;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.facebook.presto.tpch.TpchMetadata.getPrestoType;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class TpchRecordSet
        implements RecordSet
{
    public static  TpchRecordSet createTpchRecordSet(TpchTable table, double scaleFactor)
    {
        return createTpchRecordSet(table, table.getColumns(), scaleFactor, 1, 1, TupleDomain.all());
    }

    public static  TpchRecordSet createTpchRecordSet(
            TpchTable table,
            List> columns,
            double scaleFactor,
            int part,
            int partCount,
            TupleDomain predicate)
    {
        return new TpchRecordSet<>(table.createGenerator(scaleFactor, part, partCount), table, columns, predicate);
    }

    private final Iterable rows;
    private final TpchTable table;
    private final List> columns;
    private final List columnTypes;
    private final TupleDomain predicate;

    public TpchRecordSet(Iterable rows, TpchTable table, List> columns, TupleDomain predicate)
    {
        this.rows = requireNonNull(rows, "rows is null");
        this.table = requireNonNull(table, "table is null");
        this.columns = ImmutableList.copyOf(requireNonNull(columns, "columns is null"));
        this.columnTypes = columns.stream()
                .map(TpchMetadata::getPrestoType)
                .collect(toImmutableList());
        this.predicate = requireNonNull(predicate, "predicate is null");
    }

    @Override
    public List getColumnTypes()
    {
        return columnTypes;
    }

    @Override
    public RecordCursor cursor()
    {
        return new TpchRecordCursor<>(rows.iterator(), table, columns, predicate);
    }

    public static final class TpchRecordCursor
            implements RecordCursor
    {
        private final Iterator rows;
        private final TpchTable table;
        private final List> columns;
        private final TupleDomain predicate;
        private E row;
        private boolean closed;

        public TpchRecordCursor(Iterator rows, TpchTable table, List> columns, TupleDomain predicate)
        {
            this.rows = requireNonNull(rows, "rows is null");
            this.table = requireNonNull(table, "table is null");
            this.columns = requireNonNull(columns, "columns is null");
            this.predicate = requireNonNull(predicate, "predicate is null");
        }

        @Override
        public long getCompletedBytes()
        {
            return 0;
        }

        @Override
        public long getReadTimeNanos()
        {
            return 0;
        }

        @Override
        public Type getType(int field)
        {
            return getPrestoType(getTpchColumn(field));
        }

        @Override
        public boolean advanceNextPosition()
        {
            while (!closed && rows.hasNext()) {
                row = rows.next();
                if (rowMatchesPredicate()) {
                    return true;
                }
            }

            closed = true;
            row = null;
            return false;
        }

        @Override
        public boolean getBoolean(int field)
        {
            throw new UnsupportedOperationException();
        }

        @Override
        public long getLong(int field)
        {
            checkState(row != null, "No current row");
            return getLong(getTpchColumn(field));
        }

        private long getLong(TpchColumn tpchColumn)
        {
            if (tpchColumn.getType().getBase() == TpchColumnType.Base.DATE) {
                return tpchColumn.getDate(row);
            }
            if (tpchColumn.getType().getBase() == TpchColumnType.Base.INTEGER) {
                return tpchColumn.getInteger(row);
            }
            return tpchColumn.getIdentifier(row);
        }

        @Override
        public double getDouble(int field)
        {
            checkState(row != null, "No current row");
            return getDouble(getTpchColumn(field));
        }

        private double getDouble(TpchColumn tpchColumn)
        {
            return tpchColumn.getDouble(row);
        }

        @Override
        public Slice getSlice(int field)
        {
            checkState(row != null, "No current row");
            return getSlice(getTpchColumn(field));
        }

        private Slice getSlice(TpchColumn tpchColumn)
        {
            return Slices.utf8Slice(tpchColumn.getString(row));
        }

        @Override
        public Object getObject(int field)
        {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isNull(int field)
        {
            return false;
        }

        @Override
        public void close()
        {
            row = null;
            closed = true;
        }

        private boolean rowMatchesPredicate()
        {
            if (predicate.isAll()) {
                return true;
            }
            if (predicate.isNone()) {
                return false;
            }

            Map rowMap = predicate.getDomains().get().keySet().stream()
                    .collect(toImmutableMap(
                            column -> column,
                            column -> {
                                TpchColumnHandle tpchColumnHandle = (TpchColumnHandle) column;
                                Type type = tpchColumnHandle.getType();
                                TpchColumn tpchColumn = table.getColumn(tpchColumnHandle.getColumnName());
                                return NullableValue.of(type, getPrestoObject(tpchColumn, type));
                            }));

            TupleDomain rowTupleDomain = TupleDomain.fromFixedValues(rowMap);

            return predicate.contains(rowTupleDomain);
        }

        private Object getPrestoObject(TpchColumn column, Type type)
        {
            if (type.getJavaType() == long.class) {
                return getLong(column);
            }
            else if (type.getJavaType() == double.class) {
                return getDouble(column);
            }
            else if (type.getJavaType() == Slice.class) {
                return getSlice(column);
            }
            else {
                throw new PrestoException(NOT_SUPPORTED, format("Unsupported column type %s", type.getDisplayName()));
            }
        }

        private TpchColumn getTpchColumn(int field)
        {
            return columns.get(field);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy