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.connector.informationSchema;
 
 
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_COLUMNS;
 import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_INTERNAL_FUNCTIONS;
 import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_INTERNAL_PARTITIONS;
 import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_SCHEMATA;
 import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_TABLES;
 import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.TABLE_VIEWS;
 import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.informationSchemaTableColumns;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static com.facebook.presto.util.Types.checkType;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Strings.nullToEmpty;
 import static com.google.common.collect.Sets.union;
 import static java.lang.String.format;
 
         implements ConnectorPageSourceProvider
 {
     private final Metadata metadata;
     private final SplitManager splitManager;
 
     @Inject
     public InformationSchemaPageSourceProvider(Metadata metadataSplitManager splitManager)
     {
         this. = checkNotNull(metadata"metadata is null");
         this. = checkNotNull(splitManager"splitManager is null");
     }
 
     @Override
     {
         InternalTable table = getInternalTable(splitcolumns);
 
         List<Integerchannels = new ArrayList<>();
         for (ConnectorColumnHandle column : columns) {
             String columnName = checkType(columnInformationSchemaColumnHandle.class"column").getColumnName();
             int columnIndex = table.getColumnIndex(columnName);
             channels.add(columnIndex);
         }
 
         ImmutableList.Builder<Pagepages = ImmutableList.builder();
         for (Page page : table.getPages()) {
             Block[] blocks = new Block[channels.size()];
             for (int index = 0; index < blocks.lengthindex++) {
                blocks[index] = page.getBlock(channels.get(index));
            }
            pages.add(new Page(page.getPositionCount(), blocks));
        }
        return new FixedPageSource(pages.build());
    }
    private InternalTable getInternalTable(ConnectorSplit connectorSplitList<ConnectorColumnHandlecolumns)
    {
        InformationSchemaSplit split = checkType(connectorSplitInformationSchemaSplit.class"split");
        checkNotNull(columns"columns is null");
        InformationSchemaTableHandle handle = split.getTableHandle();
        Map<StringSerializableNativeValuefilters = split.getFilters();
        return getInformationSchemaTable(handle.getSession(), handle.getCatalogName(), handle.getSchemaTableName(), filters);
    }
    public InternalTable getInformationSchemaTable(Session sessionString catalogSchemaTableName tableMap<StringSerializableNativeValuefilters)
    {
        if (table.equals()) {
            return buildColumns(sessioncatalogfilters);
        }
        if (table.equals()) {
            return buildTables(sessioncatalogfilters);
        }
        if (table.equals()) {
            return buildViews(sessioncatalogfilters);
        }
        if (table.equals()) {
            return buildSchemata(sessioncatalog);
        }
        if (table.equals()) {
            return buildFunctions();
        }
        if (table.equals()) {
            return buildPartitions(sessioncatalogfilters);
        }
        throw new IllegalArgumentException(format("table does not exist: %s"table));
    }
    private InternalTable buildColumns(Session sessionString catalogNameMap<StringSerializableNativeValuefilters)
    {
        InternalTable.Builder table = InternalTable.builder(informationSchemaTableColumns());
        for (Entry<QualifiedTableNameList<ColumnMetadata>> entry : getColumnsList(sessioncatalogNamefilters).entrySet()) {
            QualifiedTableName tableName = entry.getKey();
            for (ColumnMetadata column : entry.getValue()) {
                if (column.isHidden()) {
                    continue;
                }
                table.add(
                        tableName.getCatalogName(),
                        tableName.getSchemaName(),
                        tableName.getTableName(),
                        column.getName(),
                        column.getOrdinalPosition() + 1,
                        null,
                        "YES",
                        column.getType().getDisplayName(),
                        column.isPartitionKey() ? "YES" : "NO",
                        column.getComment());
            }
        }
        return table.build();
    }
    private Map<QualifiedTableNameList<ColumnMetadata>> getColumnsList(Session sessionString catalogNameMap<StringSerializableNativeValuefilters)
    {
        return .listTableColumns(sessionextractQualifiedTablePrefix(catalogNamefilters));
    }
    private InternalTable buildTables(Session sessionString catalogNameMap<StringSerializableNativeValuefilters)
    {
        Set<QualifiedTableNametables = ImmutableSet.copyOf(getTablesList(sessioncatalogNamefilters));
        Set<QualifiedTableNameviews = ImmutableSet.copyOf(getViewsList(sessioncatalogNamefilters));
        InternalTable.Builder table = InternalTable.builder(informationSchemaTableColumns());
        for (QualifiedTableName name : union(tablesviews)) {
            // if table and view names overlap, the view wins
            String type = views.contains(name) ? "VIEW" : "BASE TABLE";
            table.add(
                    name.getCatalogName(),
                    name.getSchemaName(),
                    name.getTableName(),
                    type);
        }
        return table.build();
    }
    private List<QualifiedTableNamegetTablesList(Session sessionString catalogNameMap<StringSerializableNativeValuefilters)
    {
        return .listTables(sessionextractQualifiedTablePrefix(catalogNamefilters));
    }
    private List<QualifiedTableNamegetViewsList(Session sessionString catalogNameMap<StringSerializableNativeValuefilters)
    {
        return .listViews(sessionextractQualifiedTablePrefix(catalogNamefilters));
    }
    private InternalTable buildViews(Session sessionString catalogNameMap<StringSerializableNativeValuefilters)
    {
        InternalTable.Builder table = InternalTable.builder(informationSchemaTableColumns());
        for (Entry<QualifiedTableNameViewDefinitionentry : getViews(sessioncatalogNamefilters).entrySet()) {
            table.add(
                    entry.getKey().getCatalogName(),
                    entry.getKey().getSchemaName(),
                    entry.getKey().getTableName(),
                    entry.getValue().getOriginalSql());
        }
        return table.build();
    }
    private Map<QualifiedTableNameViewDefinitiongetViews(Session sessionString catalogNameMap<StringSerializableNativeValuefilters)
    {
        return .getViews(sessionextractQualifiedTablePrefix(catalogNamefilters));
    }
    private InternalTable buildFunctions()
    {
        for (ParametricFunction function : .listFunctions()) {
            if (function.isApproximate()) {
                continue;
            }
            table.add(
                    function.getSignature().getName(),
                    Joiner.on(", ").join(function.getSignature().getArgumentTypes()),
                    function.getSignature().getReturnType().toString(),
                    getFunctionType(function),
                    function.isDeterministic(),
                    nullToEmpty(function.getDescription()));
        }
        return table.build();
    }
    private InternalTable buildSchemata(Session sessionString catalogName)
    {
        for (String schema : .listSchemaNames(sessioncatalogName)) {
            table.add(catalogNameschema);
        }
        return table.build();
    }
    private InternalTable buildPartitions(Session sessionString catalogNameMap<StringSerializableNativeValuefilters)
    {
        QualifiedTableName tableName = extractQualifiedTableName(catalogNamefilters);
        int partitionNumber = 1;
        Optional<TableHandletableHandle = .getTableHandle(sessiontableName);
        checkArgument(tableHandle.isPresent(), "Table %s does not exist"tableName);
        Map<ColumnHandleStringcolumnHandles = ImmutableBiMap.copyOf(.getColumnHandles(tableHandle.get())).inverse();
        PartitionResult partitionResult = .getPartitions(tableHandle.get(), Optional.empty());
        for (Partition partition : partitionResult.getPartitions()) {
            for (Entry<ColumnHandleSerializableNativeValueentry : partition.getTupleDomain().extractNullableFixedValues().entrySet()) {
                ColumnHandle columnHandle = entry.getKey();
                String columnName = columnHandles.get(columnHandle);
                String value = null;
                if (entry.getValue().getValue() != null) {
                    ColumnMetadata columnMetadata  = .getColumnMetadata(tableHandle.get(), columnHandle);
                    try {
                        FunctionInfo operator = .getFunctionRegistry().getCoercion(columnMetadata.getType(), );
                        value = ((Sliceoperator.getMethodHandle().invokeWithArguments(entry.getValue().getValue())).toStringUtf8();
                    }
                    catch (OperatorNotFoundException e) {
                        value = "<UNREPRESENTABLE VALUE>";
                    }
                    catch (Throwable throwable) {
                        throw Throwables.propagate(throwable);
                    }
                }
                table.add(
                        catalogName,
                        tableName.getSchemaName(),
                        tableName.getTableName(),
                        partitionNumber,
                        columnName,
                        value);
            }
            partitionNumber++;
        }
        return table.build();
    }
    private static QualifiedTableName extractQualifiedTableName(String catalogNameMap<StringSerializableNativeValuefilters)
    {
        Optional<StringschemaName = getFilterColumn(filters"table_schema");
        checkArgument(schemaName.isPresent(), "filter is required for column: %s.%s""table_schema");
        Optional<StringtableName = getFilterColumn(filters"table_name");
        checkArgument(tableName.isPresent(), "filter is required for column: %s.%s""table_name");
        return new QualifiedTableName(catalogNameschemaName.get(), tableName.get());
    }
    private static QualifiedTablePrefix extractQualifiedTablePrefix(String catalogNameMap<StringSerializableNativeValuefilters)
    {
        Optional<StringschemaName = getFilterColumn(filters"table_schema");
        Optional<StringtableName = getFilterColumn(filters"table_name");
        if (!schemaName.isPresent()) {
            return new QualifiedTablePrefix(catalogName, Optional.empty(), Optional.empty());
        }
        return new QualifiedTablePrefix(catalogNameschemaNametableName);
    }
    private static Optional<StringgetFilterColumn(Map<StringSerializableNativeValuefiltersString columnName)
    {
        SerializableNativeValue value = filters.get(columnName);
        if (value == null || value.getValue() == null) {
            return Optional.empty();
        }
        if (Slice.class.isAssignableFrom(value.getType())) {
            return Optional.ofNullable(((Slicevalue.getValue()).toStringUtf8());
        }
        if (String.class.isAssignableFrom(value.getType())) {
            return Optional.ofNullable((Stringvalue.getValue());
        }
        return Optional.empty();
    }
    private static String getFunctionType(ParametricFunction function)
    {
        if (function.isAggregate()) {
            return "aggregate";
        }
        if (function.isWindow()) {
            return "window";
        }
        return "scalar";
    }
New to GrepCode? Check out our FAQ X