Search in sources :

Example 16 with TableHandle

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

the class RenameTableTask method execute.

@Override
public ListenableFuture<?> execute(RenameTable statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) {
    Session session = stateMachine.getSession();
    QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getSource());
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    if (!tableHandle.isPresent()) {
        throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
    }
    QualifiedObjectName target = createQualifiedObjectName(session, statement, statement.getTarget());
    if (!metadata.getCatalogHandle(session, target.getCatalogName()).isPresent()) {
        throw new SemanticException(MISSING_CATALOG, statement, "Target catalog '%s' does not exist", target.getCatalogName());
    }
    if (metadata.getTableHandle(session, target).isPresent()) {
        throw new SemanticException(TABLE_ALREADY_EXISTS, statement, "Target table '%s' already exists", target);
    }
    if (!tableName.getCatalogName().equals(target.getCatalogName())) {
        throw new SemanticException(NOT_SUPPORTED, statement, "Table rename across catalogs is not supported");
    }
    accessControl.checkCanRenameTable(session.getRequiredTransactionId(), session.getIdentity(), tableName, target);
    metadata.renameTable(session, tableHandle.get(), target);
    return immediateFuture(null);
}
Also used : TableHandle(com.facebook.presto.metadata.TableHandle) QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) Session(com.facebook.presto.Session) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 17 with TableHandle

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

the class RevokeTask method execute.

@Override
public ListenableFuture<?> execute(Revoke statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) {
    Session session = stateMachine.getSession();
    QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTableName());
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    if (!tableHandle.isPresent()) {
        throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
    }
    Set<Privilege> privileges;
    if (statement.getPrivileges().isPresent()) {
        privileges = statement.getPrivileges().get().stream().map(privilege -> parsePrivilege(statement, privilege)).collect(toImmutableSet());
    } else {
        // All privileges
        privileges = EnumSet.allOf(Privilege.class);
    }
    // verify current identity has permissions to revoke permissions
    for (Privilege privilege : privileges) {
        accessControl.checkCanRevokeTablePrivilege(session.getRequiredTransactionId(), session.getIdentity(), privilege, tableName);
    }
    metadata.revokeTablePrivileges(session, tableName, privileges, statement.getGrantee(), statement.isGrantOptionFor());
    return immediateFuture(null);
}
Also used : TableHandle(com.facebook.presto.metadata.TableHandle) Privilege(com.facebook.presto.spi.security.Privilege) QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) Session(com.facebook.presto.Session) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 18 with TableHandle

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

the class TestSourcePartitionedScheduler method createPlan.

private static StageExecutionPlan createPlan(ConnectorSplitSource splitSource) {
    Symbol symbol = new Symbol("column");
    // table scan with splitCount splits
    PlanNodeId tableScanNodeId = new PlanNodeId("plan_id");
    TableScanNode tableScan = new TableScanNode(tableScanNodeId, new TableHandle(CONNECTOR_ID, new TestingTableHandle()), ImmutableList.of(symbol), ImmutableMap.of(symbol, new TestingColumnHandle("column")), Optional.empty(), TupleDomain.all(), null);
    RemoteSourceNode remote = new RemoteSourceNode(new PlanNodeId("remote_id"), new PlanFragmentId("plan_fragment_id"), ImmutableList.of());
    PlanFragment testFragment = new PlanFragment(new PlanFragmentId("plan_id"), new JoinNode(new PlanNodeId("join_id"), INNER, tableScan, remote, ImmutableList.of(), ImmutableList.<Symbol>builder().addAll(tableScan.getOutputSymbols()).addAll(remote.getOutputSymbols()).build(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(JoinNode.DistributionType.PARTITIONED)), ImmutableMap.of(symbol, VARCHAR), SOURCE_DISTRIBUTION, ImmutableList.of(tableScanNodeId), new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)));
    return new StageExecutionPlan(testFragment, ImmutableMap.of(tableScanNodeId, new ConnectorAwareSplitSource(CONNECTOR_ID, TestingTransactionHandle.create(), splitSource)), ImmutableList.of());
}
Also used : Symbol(com.facebook.presto.sql.planner.Symbol) TestingTableHandle(com.facebook.presto.sql.planner.TestingTableHandle) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) PartitioningScheme(com.facebook.presto.sql.planner.PartitioningScheme) StageExecutionPlan(com.facebook.presto.sql.planner.StageExecutionPlan) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) ConnectorAwareSplitSource(com.facebook.presto.split.ConnectorAwareSplitSource) PlanNodeId(com.facebook.presto.sql.planner.plan.PlanNodeId) TestingColumnHandle(com.facebook.presto.sql.planner.TestingColumnHandle) RemoteSourceNode(com.facebook.presto.sql.planner.plan.RemoteSourceNode) TableScanNode(com.facebook.presto.sql.planner.plan.TableScanNode) TestingTableHandle(com.facebook.presto.sql.planner.TestingTableHandle) TableHandle(com.facebook.presto.metadata.TableHandle) PlanFragmentId(com.facebook.presto.sql.planner.plan.PlanFragmentId)

Example 19 with TableHandle

use of com.facebook.presto.metadata.TableHandle 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 20 with TableHandle

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

TableHandle (com.facebook.presto.metadata.TableHandle)20 QualifiedObjectName (com.facebook.presto.metadata.QualifiedObjectName)13 Session (com.facebook.presto.Session)9 MetadataUtil.createQualifiedObjectName (com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName)7 SemanticException (com.facebook.presto.sql.analyzer.SemanticException)7 ColumnHandle (com.facebook.presto.spi.ColumnHandle)6 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)6 TableScanNode (com.facebook.presto.sql.planner.plan.TableScanNode)6 ConnectorId (com.facebook.presto.connector.ConnectorId)5 Type (com.facebook.presto.spi.type.Type)5 Symbol (com.facebook.presto.sql.planner.Symbol)4 TestingColumnHandle (com.facebook.presto.sql.planner.TestingColumnHandle)4 TestingTableHandle (com.facebook.presto.sql.planner.TestingTableHandle)4 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)4 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)4 TableLayoutResult (com.facebook.presto.metadata.TableLayoutResult)3 TableMetadata (com.facebook.presto.metadata.TableMetadata)3 ImmutableList (com.google.common.collect.ImmutableList)3 HiveQueryRunner.createBucketedSession (com.facebook.presto.hive.HiveQueryRunner.createBucketedSession)2 Metadata (com.facebook.presto.metadata.Metadata)2