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.tpch;
 
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.DateType.DATE;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static com.facebook.presto.tpch.Types.checkType;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public class TpchMetadata
         extends ReadOnlyConnectorMetadata
 {
     public static final String TINY_SCHEMA_NAME = "tiny";
     public static final double TINY_SCALE_FACTOR = 0.01;
 
     public static final String ROW_NUMBER_COLUMN_NAME = "row_number";
 
     private final String connectorId;
     private final Set<StringtableNames;
 
     public TpchMetadata(String connectorId)
     {
         ImmutableSet.Builder<StringtableNames = ImmutableSet.builder();
         for (TpchTable<?> tpchTable : TpchTable.getTables()) {
             tableNames.add(tpchTable.getTableName());
         }
         this. = tableNames.build();
         this. = connectorId;
     }
 
     @Override
     public List<StringlistSchemaNames(ConnectorSession session)
     {
         return ImmutableList.of("sf1""sf100""sf300""sf1000""sf3000""sf10000""sf30000""sf100000");
     }
 
     @Override
     public TpchTableHandle getTableHandle(ConnectorSession sessionSchemaTableName tableName)
     {
         checkNotNull(tableName"tableName is null");
         if (!.contains(tableName.getTableName())) {
             return null;
         }
 
         // parse the scale factor
         double scaleFactor = schemaNameToScaleFactor(tableName.getSchemaName());
         if (scaleFactor < 0) {
             return null;
         }
 
         return new TpchTableHandle(tableName.getTableName(), scaleFactor);
     }
 
    @Override
    {
        TpchTableHandle tableHandle = checkType(tableTpchTableHandle.class"table");
        Optional<Set<ColumnHandle>> partitioningColumns = Optional.empty();
        List<LocalProperty<ColumnHandle>> localProperties = ImmutableList.of();
        Map<StringColumnHandlecolumns = getColumnHandles(tableHandle);
        if (tableHandle.getTableName().equals(..getTableName())) {
            partitioningColumns = Optional.of(ImmutableSet.of(columns.get(..getColumnName())));
            localProperties = ImmutableList.of(new SortingProperty<>(columns.get(..getColumnName()), .));
        }
        else if (tableHandle.getTableName().equals(..getTableName())) {
            partitioningColumns = Optional.of(ImmutableSet.of(columns.get(..getColumnName())));
            localProperties = ImmutableList.of(
                    new SortingProperty<>(columns.get(..getColumnName()), .),
                    new SortingProperty<>(columns.get(..getColumnName()), .));
        }
        ConnectorTableLayout layout = new ConnectorTableLayout(
                new TpchTableLayoutHandle(tableHandle),
                Optional.<List<ColumnHandle>>empty(),
                TupleDomain.<ColumnHandle>all(), // TODO: return well-known properties (e.g., orderkey > 0, etc)
                partitioningColumns,
                Optional.empty(),
                localProperties);
        return ImmutableList.of(new ConnectorTableLayoutResult(layoutconstraint.getSummary()));
    }
    @Override
    {
        TpchTableLayoutHandle layout = checkType(handleTpchTableLayoutHandle.class"layout");
        // tables in this connector have a single layout
        return getTableLayouts(layout.getTable(), Constraint.<ColumnHandle>alwaysTrue(), Optional.empty())
                .get(0)
                .getTableLayout();
    }
    @Override
    {
        TpchTableHandle tpchTableHandle = checkType(tableHandleTpchTableHandle.class"tableHandle");
        TpchTable<?> tpchTable = TpchTable.getTable(tpchTableHandle.getTableName());
        String schemaName = scaleFactorSchemaName(tpchTableHandle.getScaleFactor());
        return getTableMetadata(schemaNametpchTable);
    }
    private static ConnectorTableMetadata getTableMetadata(String schemaNameTpchTable<?> tpchTable)
    {
        ImmutableList.Builder<ColumnMetadatacolumns = ImmutableList.builder();
        int ordinalPosition = 0;
        for (TpchColumn<? extends TpchEntitycolumn : tpchTable.getColumns()) {
            columns.add(new ColumnMetadata(column.getColumnName(), getPrestoType(column.getType()), ordinalPositionfalse));
            ordinalPosition++;
        }
        columns.add(new ColumnMetadata(ordinalPositionfalsenulltrue));
        SchemaTableName tableName = new SchemaTableName(schemaNametpchTable.getTableName());
        return new ConnectorTableMetadata(tableNamecolumns.build());
    }
    @Override
    {
        ImmutableMap.Builder<StringColumnHandlebuilder = ImmutableMap.builder();
        for (ColumnMetadata columnMetadata : getTableMetadata(tableHandle).getColumns()) {
            builder.put(columnMetadata.getName(), new TpchColumnHandle(columnMetadata.getName(), columnMetadata.getOrdinalPosition(), columnMetadata.getType()));
        }
        return builder.build();
    }
    @Override
    {
        return null;
    }
    @Override
    {
        ImmutableMap.Builder<SchemaTableNameList<ColumnMetadata>> tableColumns = ImmutableMap.builder();
        for (String schemaName : getSchemaNames(sessionprefix.getSchemaName())) {
            for (TpchTable<?> tpchTable : TpchTable.getTables()) {
                if (prefix.getTableName() == null || tpchTable.getTableName().equals(prefix.getTableName())) {
                    ConnectorTableMetadata tableMetadata = getTableMetadata(schemaNametpchTable);
                    tableColumns.put(new SchemaTableName(schemaNametpchTable.getTableName()), tableMetadata.getColumns());
                }
            }
        }
        return tableColumns.build();
    }
    @Override
    public ColumnMetadata getColumnMetadata(ConnectorTableHandle tableHandleColumnHandle columnHandle)
    {
        ConnectorTableMetadata tableMetadata = getTableMetadata(tableHandle);
        String columnName = checkType(columnHandleTpchColumnHandle.class"columnHandle").getColumnName();
        for (ColumnMetadata column : tableMetadata.getColumns()) {
            if (column.getName().equals(columnName)) {
                return column;
            }
        }
        throw new IllegalArgumentException(String.format("Table %s does not have column %s"tableMetadata.getTable(), columnName));
    }
    @Override
    public List<SchemaTableNamelistTables(ConnectorSession sessionString schemaNameOrNull)
    {
        ImmutableList.Builder<SchemaTableNamebuilder = ImmutableList.builder();
        for (String schemaName : getSchemaNames(sessionschemaNameOrNull)) {
            for (TpchTable<?> tpchTable : TpchTable.getTables()) {
                builder.add(new SchemaTableName(schemaNametpchTable.getTableName()));
            }
        }
        return builder.build();
    }
    private List<StringgetSchemaNames(ConnectorSession sessionString schemaNameOrNull)
    {
        List<StringschemaNames;
        if (schemaNameOrNull == null) {
            schemaNames = listSchemaNames(session);
        }
        else if (schemaNameToScaleFactor(schemaNameOrNull) > 0) {
            schemaNames = ImmutableList.of(schemaNameOrNull);
        }
        else {
            schemaNames = ImmutableList.of();
        }
        return schemaNames;
    }
    private static String scaleFactorSchemaName(double scaleFactor)
    {
        return "sf" + scaleFactor;
    }
    private static double schemaNameToScaleFactor(String schemaName)
    {
        if (.equals(schemaName)) {
            return ;
        }
        if (!schemaName.startsWith("sf")) {
            return -1;
        }
        try {
            return Double.parseDouble(schemaName.substring(2));
        }
        catch (Exception ignored) {
            return -1;
        }
    }
    public static Type getPrestoType(TpchColumnType tpchType)
    {
        switch (tpchType) {
            case :
                return ;
            case :
                return ;
            case :
                return ;
            case :
                return ;
        }
        throw new IllegalArgumentException("Unsupported type " + tpchType);
    }
New to GrepCode? Check out our FAQ X