Search in sources :

Example 26 with ColumnHandle

use of com.facebook.presto.spi.ColumnHandle in project presto by prestodb.

the class SystemPageSourceProvider method createPageSource.

@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorSplit split, List<ColumnHandle> columns) {
    requireNonNull(columns, "columns is null");
    SystemTransactionHandle systemTransaction = (SystemTransactionHandle) transactionHandle;
    SystemSplit systemSplit = (SystemSplit) split;
    SchemaTableName tableName = systemSplit.getTableHandle().getSchemaTableName();
    SystemTable systemTable = tables.get(tableName);
    checkArgument(systemTable != null, "Table %s does not exist", tableName);
    List<ColumnMetadata> tableColumns = systemTable.getTableMetadata().getColumns();
    Map<String, Integer> columnsByName = new HashMap<>();
    for (int i = 0; i < tableColumns.size(); i++) {
        ColumnMetadata column = tableColumns.get(i);
        if (columnsByName.put(column.getName(), i) != null) {
            throw new PrestoException(GENERIC_INTERNAL_ERROR, "Duplicate column name: " + column.getName());
        }
    }
    ImmutableList.Builder<Integer> userToSystemFieldIndex = ImmutableList.builder();
    for (ColumnHandle column : columns) {
        String columnName = ((SystemColumnHandle) column).getColumnName();
        Integer index = columnsByName.get(columnName);
        if (index == null) {
            throw new PrestoException(GENERIC_INTERNAL_ERROR, format("Column does not exist: %s.%s", tableName, columnName));
        }
        userToSystemFieldIndex.add(index);
    }
    TupleDomain<ColumnHandle> constraint = systemSplit.getConstraint();
    ImmutableMap.Builder<Integer, Domain> newConstraints = ImmutableMap.builder();
    for (Map.Entry<ColumnHandle, Domain> entry : constraint.getDomains().get().entrySet()) {
        String columnName = ((SystemColumnHandle) entry.getKey()).getColumnName();
        newConstraints.put(columnsByName.get(columnName), entry.getValue());
    }
    TupleDomain<Integer> newContraint = withColumnDomains(newConstraints.build());
    try {
        return new MappedPageSource(systemTable.pageSource(systemTransaction.getConnectorTransactionHandle(), session, newContraint), userToSystemFieldIndex.build());
    } catch (UnsupportedOperationException e) {
        return new RecordPageSource(new MappedRecordSet(toRecordSet(systemTransaction.getConnectorTransactionHandle(), systemTable, session, newContraint), userToSystemFieldIndex.build()));
    }
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) PrestoException(com.facebook.presto.spi.PrestoException) RecordPageSource(com.facebook.presto.spi.RecordPageSource) MappedRecordSet(com.facebook.presto.split.MappedRecordSet) ColumnHandle(com.facebook.presto.spi.ColumnHandle) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ImmutableMap(com.google.common.collect.ImmutableMap) MappedPageSource(com.facebook.presto.split.MappedPageSource) SystemTable(com.facebook.presto.spi.SystemTable) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 27 with ColumnHandle

use of com.facebook.presto.spi.ColumnHandle in project presto by prestodb.

the class KafkaMetadata method getColumnHandles.

@SuppressWarnings("ValueOfIncrementOrDecrementUsed")
@Override
public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, ConnectorTableHandle tableHandle) {
    KafkaTableHandle kafkaTableHandle = convertTableHandle(tableHandle);
    KafkaTopicDescription kafkaTopicDescription = tableDescriptions.get(kafkaTableHandle.toSchemaTableName());
    if (kafkaTopicDescription == null) {
        throw new TableNotFoundException(kafkaTableHandle.toSchemaTableName());
    }
    ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder();
    int index = 0;
    KafkaTopicFieldGroup key = kafkaTopicDescription.getKey();
    if (key != null) {
        List<KafkaTopicFieldDescription> fields = key.getFields();
        if (fields != null) {
            for (KafkaTopicFieldDescription kafkaTopicFieldDescription : fields) {
                columnHandles.put(kafkaTopicFieldDescription.getName(), kafkaTopicFieldDescription.getColumnHandle(connectorId, true, index++));
            }
        }
    }
    KafkaTopicFieldGroup message = kafkaTopicDescription.getMessage();
    if (message != null) {
        List<KafkaTopicFieldDescription> fields = message.getFields();
        if (fields != null) {
            for (KafkaTopicFieldDescription kafkaTopicFieldDescription : fields) {
                columnHandles.put(kafkaTopicFieldDescription.getName(), kafkaTopicFieldDescription.getColumnHandle(connectorId, false, index++));
            }
        }
    }
    for (KafkaInternalFieldDescription kafkaInternalFieldDescription : internalFieldDescriptions) {
        columnHandles.put(kafkaInternalFieldDescription.getName(), kafkaInternalFieldDescription.getColumnHandle(connectorId, index++, hideInternalColumns));
    }
    return columnHandles.build();
}
Also used : TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) KafkaHandleResolver.convertColumnHandle(com.facebook.presto.kafka.KafkaHandleResolver.convertColumnHandle) ColumnHandle(com.facebook.presto.spi.ColumnHandle) ImmutableMap(com.google.common.collect.ImmutableMap) Constraint(com.facebook.presto.spi.Constraint)

Example 28 with ColumnHandle

use of com.facebook.presto.spi.ColumnHandle in project presto by prestodb.

the class AddColumnTask method execute.

@Override
public ListenableFuture<?> execute(AddColumn statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) {
    Session session = stateMachine.getSession();
    QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getName());
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    if (!tableHandle.isPresent()) {
        throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
    }
    accessControl.checkCanAddColumns(session.getRequiredTransactionId(), session.getIdentity(), tableName);
    Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle.get());
    ColumnDefinition element = statement.getColumn();
    Type type = metadata.getType(parseTypeSignature(element.getType()));
    if ((type == null) || type.equals(UNKNOWN)) {
        throw new SemanticException(TYPE_MISMATCH, element, "Unknown type for column '%s' ", element.getName());
    }
    if (columnHandles.containsKey(element.getName().toLowerCase(ENGLISH))) {
        throw new SemanticException(COLUMN_ALREADY_EXISTS, statement, "Column '%s' already exists", element.getName());
    }
    metadata.addColumn(session, tableHandle.get(), new ColumnMetadata(element.getName(), type));
    return immediateFuture(null);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) Type(com.facebook.presto.spi.type.Type) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) 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) ColumnDefinition(com.facebook.presto.sql.tree.ColumnDefinition)

Example 29 with ColumnHandle

use of com.facebook.presto.spi.ColumnHandle in project presto by prestodb.

the class MetadataManager method getLayouts.

@Override
public List<TableLayoutResult> getLayouts(Session session, TableHandle table, Constraint<ColumnHandle> constraint, Optional<Set<ColumnHandle>> desiredColumns) {
    if (constraint.getSummary().isNone()) {
        return ImmutableList.of();
    }
    ConnectorId connectorId = table.getConnectorId();
    ConnectorTableHandle connectorTable = table.getConnectorHandle();
    CatalogMetadata catalogMetadata = getCatalogMetadata(session, connectorId);
    ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId);
    ConnectorTransactionHandle transaction = catalogMetadata.getTransactionHandleFor(connectorId);
    ConnectorSession connectorSession = session.toConnectorSession(connectorId);
    List<ConnectorTableLayoutResult> layouts = metadata.getTableLayouts(connectorSession, connectorTable, constraint, desiredColumns);
    return layouts.stream().map(layout -> new TableLayoutResult(fromConnectorLayout(connectorId, transaction, layout.getTableLayout()), layout.getUnenforcedConstraint())).collect(toImmutableList());
}
Also used : TypeManager(com.facebook.presto.spi.type.TypeManager) TypeRegistry(com.facebook.presto.type.TypeRegistry) BETWEEN(com.facebook.presto.spi.function.OperatorType.BETWEEN) ConnectorTransactionHandle(com.facebook.presto.spi.connector.ConnectorTransactionHandle) TableIdentity(com.facebook.presto.spi.TableIdentity) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) Privilege(com.facebook.presto.spi.security.Privilege) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) SchemaTableName(com.facebook.presto.spi.SchemaTableName) HashMultimap(com.google.common.collect.HashMultimap) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) Map(java.util.Map) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) LESS_THAN(com.facebook.presto.spi.function.OperatorType.LESS_THAN) LESS_THAN_OR_EQUAL(com.facebook.presto.spi.function.OperatorType.LESS_THAN_OR_EQUAL) ENGLISH(java.util.Locale.ENGLISH) ImmutableSet(com.google.common.collect.ImmutableSet) EQUAL(com.facebook.presto.spi.function.OperatorType.EQUAL) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ConnectorResolvedIndex(com.facebook.presto.spi.ConnectorResolvedIndex) String.format(java.lang.String.format) ConnectorSession(com.facebook.presto.spi.ConnectorSession) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) List(java.util.List) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) OperatorType(com.facebook.presto.spi.function.OperatorType) QualifiedObjectName.convertFromSchemaTableName(com.facebook.presto.metadata.QualifiedObjectName.convertFromSchemaTableName) JsonCodecFactory(io.airlift.json.JsonCodecFactory) Entry(java.util.Map.Entry) Optional(java.util.Optional) GREATER_THAN(com.facebook.presto.spi.function.OperatorType.GREATER_THAN) ConnectorId(com.facebook.presto.connector.ConnectorId) Joiner(com.google.common.base.Joiner) JsonCodec(io.airlift.json.JsonCodec) TypeSignature(com.facebook.presto.spi.type.TypeSignature) ConnectorMetadata(com.facebook.presto.spi.connector.ConnectorMetadata) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) Slice(io.airlift.slice.Slice) HASH_CODE(com.facebook.presto.spi.function.OperatorType.HASH_CODE) ConnectorTableHandle(com.facebook.presto.spi.ConnectorTableHandle) HashMap(java.util.HashMap) ConnectorOutputTableHandle(com.facebook.presto.spi.ConnectorOutputTableHandle) PrestoException(com.facebook.presto.spi.PrestoException) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) LinkedHashMap(java.util.LinkedHashMap) OptionalLong(java.util.OptionalLong) BlockEncodingSerde(com.facebook.presto.spi.block.BlockEncodingSerde) ImmutableList(com.google.common.collect.ImmutableList) Type(com.facebook.presto.spi.type.Type) Objects.requireNonNull(java.util.Objects.requireNonNull) TransactionManager(com.facebook.presto.transaction.TransactionManager) LinkedHashSet(java.util.LinkedHashSet) ConnectorInsertTableHandle(com.facebook.presto.spi.ConnectorInsertTableHandle) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) NOT_EQUAL(com.facebook.presto.spi.function.OperatorType.NOT_EQUAL) ConnectorOutputMetadata(com.facebook.presto.spi.connector.ConnectorOutputMetadata) Session(com.facebook.presto.Session) CatalogSchemaName(com.facebook.presto.spi.CatalogSchemaName) ConnectorTableLayout(com.facebook.presto.spi.ConnectorTableLayout) ColumnIdentity(com.facebook.presto.spi.ColumnIdentity) Constraint(com.facebook.presto.spi.Constraint) ConnectorTableLayoutResult(com.facebook.presto.spi.ConnectorTableLayoutResult) INVALID_VIEW(com.facebook.presto.spi.StandardErrorCode.INVALID_VIEW) TypeDeserializer(com.facebook.presto.type.TypeDeserializer) SYNTAX_ERROR(com.facebook.presto.spi.StandardErrorCode.SYNTAX_ERROR) SchemaTablePrefix(com.facebook.presto.spi.SchemaTablePrefix) ColumnHandle(com.facebook.presto.spi.ColumnHandle) ViewColumn(com.facebook.presto.metadata.ViewDefinition.ViewColumn) QueryId(com.facebook.presto.spi.QueryId) GREATER_THAN_OR_EQUAL(com.facebook.presto.spi.function.OperatorType.GREATER_THAN_OR_EQUAL) ConnectorNewTableLayout(com.facebook.presto.spi.ConnectorNewTableLayout) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TransactionManager.createTestTransactionManager(com.facebook.presto.transaction.TransactionManager.createTestTransactionManager) TableLayout.fromConnectorLayout(com.facebook.presto.metadata.TableLayout.fromConnectorLayout) ConnectorViewDefinition(com.facebook.presto.spi.ConnectorViewDefinition) BlockEncodingManager(com.facebook.presto.block.BlockEncodingManager) ConnectorTableLayoutResult(com.facebook.presto.spi.ConnectorTableLayoutResult) ConnectorTransactionHandle(com.facebook.presto.spi.connector.ConnectorTransactionHandle) ConnectorSession(com.facebook.presto.spi.ConnectorSession) ConnectorMetadata(com.facebook.presto.spi.connector.ConnectorMetadata) ConnectorTableLayoutResult(com.facebook.presto.spi.ConnectorTableLayoutResult) ConnectorId(com.facebook.presto.connector.ConnectorId) ConnectorTableHandle(com.facebook.presto.spi.ConnectorTableHandle)

Example 30 with ColumnHandle

use of com.facebook.presto.spi.ColumnHandle in project presto by prestodb.

the class RaptorPageSourceProvider method createPageSource.

@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorSplit split, List<ColumnHandle> columns) {
    RaptorSplit raptorSplit = (RaptorSplit) split;
    OptionalInt bucketNumber = raptorSplit.getBucketNumber();
    TupleDomain<RaptorColumnHandle> predicate = raptorSplit.getEffectivePredicate();
    ReaderAttributes attributes = ReaderAttributes.from(session);
    OptionalLong transactionId = raptorSplit.getTransactionId();
    if (raptorSplit.getShardUuids().size() == 1) {
        UUID shardUuid = raptorSplit.getShardUuids().iterator().next();
        return createPageSource(shardUuid, bucketNumber, columns, predicate, attributes, transactionId);
    }
    Iterator<ConnectorPageSource> iterator = raptorSplit.getShardUuids().stream().map(shardUuid -> createPageSource(shardUuid, bucketNumber, columns, predicate, attributes, transactionId)).iterator();
    return new ConcatPageSource(iterator);
}
Also used : ConcatPageSource(com.facebook.presto.raptor.util.ConcatPageSource) Iterator(java.util.Iterator) UUID(java.util.UUID) OptionalInt(java.util.OptionalInt) ConnectorTransactionHandle(com.facebook.presto.spi.connector.ConnectorTransactionHandle) ConnectorSession(com.facebook.presto.spi.ConnectorSession) Inject(javax.inject.Inject) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) OptionalLong(java.util.OptionalLong) ConnectorSplit(com.facebook.presto.spi.ConnectorSplit) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Type(com.facebook.presto.spi.type.Type) Objects.requireNonNull(java.util.Objects.requireNonNull) ReaderAttributes(com.facebook.presto.raptor.storage.ReaderAttributes) StorageManager(com.facebook.presto.raptor.storage.StorageManager) ConnectorPageSourceProvider(com.facebook.presto.spi.connector.ConnectorPageSourceProvider) ConcatPageSource(com.facebook.presto.raptor.util.ConcatPageSource) ReaderAttributes(com.facebook.presto.raptor.storage.ReaderAttributes) OptionalLong(java.util.OptionalLong) OptionalInt(java.util.OptionalInt) UUID(java.util.UUID) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource)

Aggregations

ColumnHandle (com.facebook.presto.spi.ColumnHandle)243 ImmutableList (com.google.common.collect.ImmutableList)90 ImmutableMap (com.google.common.collect.ImmutableMap)81 Test (org.testng.annotations.Test)81 ConnectorSession (com.facebook.presto.spi.ConnectorSession)80 ConnectorTableHandle (com.facebook.presto.spi.ConnectorTableHandle)70 Constraint (com.facebook.presto.spi.Constraint)66 Map (java.util.Map)65 SchemaTableName (com.facebook.presto.spi.SchemaTableName)60 Optional (java.util.Optional)57 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)54 List (java.util.List)54 Objects.requireNonNull (java.util.Objects.requireNonNull)53 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)47 ConnectorMetadata (com.facebook.presto.spi.connector.ConnectorMetadata)47 ConnectorTableLayoutHandle (com.facebook.presto.spi.ConnectorTableLayoutHandle)46 ImmutableSet (com.google.common.collect.ImmutableSet)46 TestingConnectorSession (com.facebook.presto.testing.TestingConnectorSession)43 ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)42 PrestoException (com.facebook.presto.spi.PrestoException)42