Search in sources :

Example 1 with TableLayoutResult

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

the class LocalQueryRunner method createTableScanOperator.

public OperatorFactory createTableScanOperator(Session session, int operatorId, PlanNodeId planNodeId, String tableName, String... columnNames) {
    checkArgument(session.getCatalog().isPresent(), "catalog not set");
    checkArgument(session.getSchema().isPresent(), "schema not set");
    // look up the table
    QualifiedObjectName qualifiedTableName = new QualifiedObjectName(session.getCatalog().get(), session.getSchema().get(), tableName);
    TableHandle tableHandle = metadata.getTableHandle(session, qualifiedTableName).orElse(null);
    checkArgument(tableHandle != null, "Table %s does not exist", qualifiedTableName);
    // lookup the columns
    Map<String, ColumnHandle> allColumnHandles = metadata.getColumnHandles(session, tableHandle);
    ImmutableList.Builder<ColumnHandle> columnHandlesBuilder = ImmutableList.builder();
    ImmutableList.Builder<Type> columnTypesBuilder = ImmutableList.builder();
    for (String columnName : columnNames) {
        ColumnHandle columnHandle = allColumnHandles.get(columnName);
        checkArgument(columnHandle != null, "Table %s does not have a column %s", tableName, columnName);
        columnHandlesBuilder.add(columnHandle);
        ColumnMetadata columnMetadata = metadata.getColumnMetadata(session, tableHandle, columnHandle);
        columnTypesBuilder.add(columnMetadata.getType());
    }
    List<ColumnHandle> columnHandles = columnHandlesBuilder.build();
    List<Type> columnTypes = columnTypesBuilder.build();
    // get the split for this table
    List<TableLayoutResult> layouts = metadata.getLayouts(session, tableHandle, Constraint.alwaysTrue(), Optional.empty());
    Split split = getLocalQuerySplit(session, layouts.get(0).getLayout().getHandle());
    return new OperatorFactory() {

        @Override
        public List<Type> getTypes() {
            return columnTypes;
        }

        @Override
        public Operator createOperator(DriverContext driverContext) {
            OperatorContext operatorContext = driverContext.addOperatorContext(operatorId, planNodeId, "BenchmarkSource");
            ConnectorPageSource pageSource = pageSourceManager.createPageSource(session, split, columnHandles);
            return new PageSourceOperator(pageSource, columnTypes, operatorContext);
        }

        @Override
        public void close() {
        }

        @Override
        public OperatorFactory duplicate() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) DriverContext(com.facebook.presto.operator.DriverContext) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) TableLayoutResult(com.facebook.presto.metadata.TableLayoutResult) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource) QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) PageSourceOperator(com.facebook.presto.operator.PageSourceOperator) Type(com.facebook.presto.spi.type.Type) OperatorFactory(com.facebook.presto.operator.OperatorFactory) OperatorContext(com.facebook.presto.operator.OperatorContext) TableHandle(com.facebook.presto.metadata.TableHandle) ScheduledSplit(com.facebook.presto.ScheduledSplit) Split(com.facebook.presto.metadata.Split)

Example 2 with TableLayoutResult

use of com.facebook.presto.metadata.TableLayoutResult 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)

Example 3 with TableLayoutResult

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

the class TestHiveIntegrationSmokeTest method getHiveTableProperty.

private Object getHiveTableProperty(String tableName, Function<HiveTableLayoutHandle, Object> propertyGetter) {
    Session session = getSession();
    Metadata metadata = ((DistributedQueryRunner) getQueryRunner()).getCoordinator().getMetadata();
    return transaction(getQueryRunner().getTransactionManager(), getQueryRunner().getAccessControl()).readOnly().execute(session, transactionSession -> {
        Optional<TableHandle> tableHandle = metadata.getTableHandle(transactionSession, new QualifiedObjectName(catalog, TPCH_SCHEMA, tableName));
        assertTrue(tableHandle.isPresent());
        List<TableLayoutResult> layouts = metadata.getLayouts(transactionSession, tableHandle.get(), Constraint.alwaysTrue(), Optional.empty());
        TableLayout layout = getOnlyElement(layouts).getLayout();
        return propertyGetter.apply((HiveTableLayoutHandle) layout.getHandle().getConnectorHandle());
    });
}
Also used : TableMetadata(com.facebook.presto.metadata.TableMetadata) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) Metadata(com.facebook.presto.metadata.Metadata) TableHandle(com.facebook.presto.metadata.TableHandle) TableLayoutResult(com.facebook.presto.metadata.TableLayoutResult) TableLayout(com.facebook.presto.metadata.TableLayout) QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) HiveQueryRunner.createBucketedSession(com.facebook.presto.hive.HiveQueryRunner.createBucketedSession) Session(com.facebook.presto.Session)

Aggregations

QualifiedObjectName (com.facebook.presto.metadata.QualifiedObjectName)3 TableHandle (com.facebook.presto.metadata.TableHandle)3 TableLayoutResult (com.facebook.presto.metadata.TableLayoutResult)3 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)3 TableLayout (com.facebook.presto.metadata.TableLayout)2 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 ScheduledSplit (com.facebook.presto.ScheduledSplit)1 Session (com.facebook.presto.Session)1 HiveQueryRunner.createBucketedSession (com.facebook.presto.hive.HiveQueryRunner.createBucketedSession)1 InternalTable (com.facebook.presto.metadata.InternalTable)1 Metadata (com.facebook.presto.metadata.Metadata)1 OperatorNotFoundException (com.facebook.presto.metadata.OperatorNotFoundException)1 Signature (com.facebook.presto.metadata.Signature)1 Split (com.facebook.presto.metadata.Split)1 TableMetadata (com.facebook.presto.metadata.TableMetadata)1 DriverContext (com.facebook.presto.operator.DriverContext)1 OperatorContext (com.facebook.presto.operator.OperatorContext)1 OperatorFactory (com.facebook.presto.operator.OperatorFactory)1 PageSourceOperator (com.facebook.presto.operator.PageSourceOperator)1 ConnectorPageSource (com.facebook.presto.spi.ConnectorPageSource)1