Search in sources :

Example 1 with InternalTable

use of com.facebook.presto.metadata.InternalTable in project presto by prestodb.

the class InformationSchemaPageSourceProvider method buildTables.

private InternalTable buildTables(Session session, String catalogName, Map<String, NullableValue> filters) {
    QualifiedTablePrefix prefix = extractQualifiedTablePrefix(catalogName, filters);
    Set<SchemaTableName> tables = listTables(session, metadata, accessControl, prefix);
    Set<SchemaTableName> views = listViews(session, metadata, accessControl, prefix);
    InternalTable.Builder table = InternalTable.builder(informationSchemaTableColumns(TABLE_TABLES));
    for (SchemaTableName name : union(tables, views)) {
        // if table and view names overlap, the view wins
        String type = views.contains(name) ? "VIEW" : "BASE TABLE";
        table.add(catalogName, name.getSchemaName(), name.getTableName(), type);
    }
    return table.build();
}
Also used : QualifiedTablePrefix(com.facebook.presto.metadata.QualifiedTablePrefix) SchemaTableName(com.facebook.presto.spi.SchemaTableName) InternalTable(com.facebook.presto.metadata.InternalTable)

Example 2 with InternalTable

use of com.facebook.presto.metadata.InternalTable in project presto by prestodb.

the class InformationSchemaPageSourceProvider method buildColumns.

private InternalTable buildColumns(Session session, String catalogName, Map<String, NullableValue> filters) {
    InternalTable.Builder table = InternalTable.builder(informationSchemaTableColumns(TABLE_COLUMNS));
    QualifiedTablePrefix prefix = extractQualifiedTablePrefix(catalogName, filters);
    for (Entry<SchemaTableName, List<ColumnMetadata>> entry : listTableColumns(session, metadata, accessControl, prefix).entrySet()) {
        SchemaTableName tableName = entry.getKey();
        int ordinalPosition = 1;
        for (ColumnMetadata column : entry.getValue()) {
            if (column.isHidden()) {
                continue;
            }
            table.add(catalogName, tableName.getSchemaName(), tableName.getTableName(), column.getName(), ordinalPosition, null, "YES", column.getType().getDisplayName(), column.getComment(), column.getExtraInfo());
            ordinalPosition++;
        }
    }
    return table.build();
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) QualifiedTablePrefix(com.facebook.presto.metadata.QualifiedTablePrefix) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) InternalTable(com.facebook.presto.metadata.InternalTable) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Constraint(com.facebook.presto.spi.Constraint)

Example 3 with InternalTable

use of com.facebook.presto.metadata.InternalTable in project presto by prestodb.

the class InformationSchemaPageSourceProvider method createPageSource.

@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorSplit split, List<ColumnHandle> columns) {
    InternalTable table = getInternalTable(transactionHandle, session, split, columns);
    List<Integer> channels = new ArrayList<>();
    for (ColumnHandle column : columns) {
        String columnName = ((InformationSchemaColumnHandle) column).getColumnName();
        int columnIndex = table.getColumnIndex(columnName);
        channels.add(columnIndex);
    }
    ImmutableList.Builder<Page> pages = ImmutableList.builder();
    for (Page page : table.getPages()) {
        Block[] blocks = new Block[channels.size()];
        for (int index = 0; index < blocks.length; index++) {
            blocks[index] = page.getBlock(channels.get(index));
        }
        pages.add(new Page(page.getPositionCount(), blocks));
    }
    return new FixedPageSource(pages.build());
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) Page(com.facebook.presto.spi.Page) InternalTable(com.facebook.presto.metadata.InternalTable) FixedPageSource(com.facebook.presto.spi.FixedPageSource) Constraint(com.facebook.presto.spi.Constraint) Block(com.facebook.presto.spi.block.Block)

Example 4 with InternalTable

use of com.facebook.presto.metadata.InternalTable in project presto by prestodb.

the class InformationSchemaPageSourceProvider method buildPartitions.

private InternalTable buildPartitions(Session session, String catalogName, Map<String, NullableValue> filters) {
    QualifiedObjectName tableName = extractQualifiedTableName(catalogName, filters);
    InternalTable.Builder table = InternalTable.builder(informationSchemaTableColumns(TABLE_INTERNAL_PARTITIONS));
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    if (!tableHandle.isPresent()) {
        throw new TableNotFoundException(tableName.asSchemaTableName());
    }
    List<TableLayoutResult> layouts = metadata.getLayouts(session, tableHandle.get(), Constraint.alwaysTrue(), Optional.empty());
    if (layouts.size() == 1) {
        Map<ColumnHandle, String> columnHandles = ImmutableBiMap.copyOf(metadata.getColumnHandles(session, tableHandle.get())).inverse();
        Map<ColumnHandle, MethodHandle> methodHandles = new HashMap<>();
        for (ColumnHandle columnHandle : columnHandles.keySet()) {
            try {
                ColumnMetadata columnMetadata = metadata.getColumnMetadata(session, tableHandle.get(), columnHandle);
                Signature operator = metadata.getFunctionRegistry().getCoercion(columnMetadata.getType(), createUnboundedVarcharType());
                MethodHandle methodHandle = metadata.getFunctionRegistry().getScalarFunctionImplementation(operator).getMethodHandle();
                methodHandles.put(columnHandle, methodHandle);
            } catch (OperatorNotFoundException exception) {
            // Do not put the columnHandle in the map.
            }
        }
        TableLayout layout = Iterables.getOnlyElement(layouts).getLayout();
        layout.getDiscretePredicates().ifPresent(predicates -> {
            int partitionNumber = 1;
            for (TupleDomain<ColumnHandle> domain : predicates.getPredicates()) {
                for (Entry<ColumnHandle, NullableValue> entry : TupleDomain.extractFixedValues(domain).get().entrySet()) {
                    ColumnHandle columnHandle = entry.getKey();
                    String columnName = columnHandles.get(columnHandle);
                    String value = null;
                    if (entry.getValue().getValue() != null) {
                        if (methodHandles.containsKey(columnHandle)) {
                            try {
                                value = ((Slice) methodHandles.get(columnHandle).invokeWithArguments(entry.getValue().getValue())).toStringUtf8();
                            } catch (Throwable throwable) {
                                throw Throwables.propagate(throwable);
                            }
                        } else {
                            value = "<UNREPRESENTABLE VALUE>";
                        }
                    }
                    table.add(catalogName, tableName.getSchemaName(), tableName.getObjectName(), partitionNumber, columnName, value);
                }
                partitionNumber++;
            }
        });
    }
    return table.build();
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) OperatorNotFoundException(com.facebook.presto.metadata.OperatorNotFoundException) HashMap(java.util.HashMap) NullableValue(com.facebook.presto.spi.predicate.NullableValue) TableLayoutResult(com.facebook.presto.metadata.TableLayoutResult) InternalTable(com.facebook.presto.metadata.InternalTable) QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) Constraint(com.facebook.presto.spi.Constraint) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) Signature(com.facebook.presto.metadata.Signature) TableHandle(com.facebook.presto.metadata.TableHandle) TableLayout(com.facebook.presto.metadata.TableLayout) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

InternalTable (com.facebook.presto.metadata.InternalTable)4 Constraint (com.facebook.presto.spi.Constraint)3 QualifiedTablePrefix (com.facebook.presto.metadata.QualifiedTablePrefix)2 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)2 SchemaTableName (com.facebook.presto.spi.SchemaTableName)2 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 OperatorNotFoundException (com.facebook.presto.metadata.OperatorNotFoundException)1 QualifiedObjectName (com.facebook.presto.metadata.QualifiedObjectName)1 Signature (com.facebook.presto.metadata.Signature)1 TableHandle (com.facebook.presto.metadata.TableHandle)1 TableLayout (com.facebook.presto.metadata.TableLayout)1 TableLayoutResult (com.facebook.presto.metadata.TableLayoutResult)1 FixedPageSource (com.facebook.presto.spi.FixedPageSource)1 Page (com.facebook.presto.spi.Page)1 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)1 Block (com.facebook.presto.spi.block.Block)1 NullableValue (com.facebook.presto.spi.predicate.NullableValue)1 MethodHandle (java.lang.invoke.MethodHandle)1