Search in sources :

Example 86 with ConnectorId

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

the class TestLocalExchange method testCreatePartitionFunction.

@Test
public void testCreatePartitionFunction() {
    int partitionCount = 10;
    PartitioningProviderManager partitioningProviderManager = new PartitioningProviderManager();
    partitioningProviderManager.addPartitioningProvider(new ConnectorId("prism"), new ConnectorNodePartitioningProvider() {

        @Override
        public ConnectorBucketNodeMap getBucketNodeMap(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorPartitioningHandle partitioningHandle, List<Node> sortedNodes) {
            return createBucketNodeMap(Stream.generate(() -> sortedNodes).flatMap(List::stream).limit(10).collect(toImmutableList()), SOFT_AFFINITY);
        }

        @Override
        public ToIntFunction<ConnectorSplit> getSplitBucketFunction(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorPartitioningHandle partitioningHandle) {
            return null;
        }

        @Override
        public BucketFunction getBucketFunction(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorPartitioningHandle partitioningHandle, List<Type> partitionChannelTypes, int bucketCount) {
            return (Page page, int position) -> partitionCount;
        }

        @Override
        public int getBucketCount(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorPartitioningHandle partitioningHandle) {
            return 10;
        }
    });
    PartitioningHandle partitioningHandle = new PartitioningHandle(Optional.of(new ConnectorId("prism")), Optional.of(new ConnectorTransactionHandle() {

        @Override
        public int hashCode() {
            return super.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            return super.equals(obj);
        }
    }), new ConnectorPartitioningHandle() {

        @Override
        public boolean isSingleNode() {
            return false;
        }

        @Override
        public boolean isCoordinatorOnly() {
            return false;
        }
    });
    PartitionFunction partitionFunction = createPartitionFunction(partitioningProviderManager, session, partitioningHandle, 600, ImmutableList.of(), false);
    assertEquals(partitionFunction.getPartitionCount(), partitionCount);
}
Also used : LocalExchange.createPartitionFunction(com.facebook.presto.operator.exchange.LocalExchange.createPartitionFunction) PartitionFunction(com.facebook.presto.operator.PartitionFunction) PartitioningProviderManager(com.facebook.presto.sql.planner.PartitioningProviderManager) Node(com.facebook.presto.spi.Node) ConnectorTransactionHandle(com.facebook.presto.spi.connector.ConnectorTransactionHandle) Page(com.facebook.presto.common.Page) Type(com.facebook.presto.common.type.Type) ConnectorPartitioningHandle(com.facebook.presto.spi.connector.ConnectorPartitioningHandle) ConnectorNodePartitioningProvider(com.facebook.presto.spi.connector.ConnectorNodePartitioningProvider) ConnectorBucketNodeMap(com.facebook.presto.spi.connector.ConnectorBucketNodeMap) ConnectorSession(com.facebook.presto.spi.ConnectorSession) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ToIntFunction(java.util.function.ToIntFunction) ConnectorPartitioningHandle(com.facebook.presto.spi.connector.ConnectorPartitioningHandle) PartitioningHandle(com.facebook.presto.sql.planner.PartitioningHandle) BucketFunction(com.facebook.presto.spi.BucketFunction) ConnectorId(com.facebook.presto.spi.ConnectorId) Test(org.testng.annotations.Test)

Example 87 with ConnectorId

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

the class CallTask method execute.

@Override
public ListenableFuture<?> execute(Call call, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    if (!transactionManager.getTransactionInfo(session.getRequiredTransactionId()).isAutoCommitContext()) {
        throw new PrestoException(NOT_SUPPORTED, "Procedures cannot be called within a transaction (use autocommit mode)");
    }
    QualifiedObjectName procedureName = createQualifiedObjectName(session, call, call.getName());
    ConnectorId connectorId = metadata.getCatalogHandle(session, procedureName.getCatalogName()).orElseThrow(() -> new SemanticException(MISSING_CATALOG, call, "Catalog %s does not exist", procedureName.getCatalogName()));
    Procedure procedure = metadata.getProcedureRegistry().resolve(connectorId, toSchemaTableName(procedureName));
    // map declared argument names to positions
    Map<String, Integer> positions = new HashMap<>();
    for (int i = 0; i < procedure.getArguments().size(); i++) {
        positions.put(procedure.getArguments().get(i).getName(), i);
    }
    // per specification, do not allow mixing argument types
    Predicate<CallArgument> hasName = argument -> argument.getName().isPresent();
    boolean anyNamed = call.getArguments().stream().anyMatch(hasName);
    boolean allNamed = call.getArguments().stream().allMatch(hasName);
    if (anyNamed && !allNamed) {
        throw new SemanticException(INVALID_PROCEDURE_ARGUMENTS, call, "Named and positional arguments cannot be mixed");
    }
    // get the argument names in call order
    Map<String, CallArgument> names = new LinkedHashMap<>();
    for (int i = 0; i < call.getArguments().size(); i++) {
        CallArgument argument = call.getArguments().get(i);
        if (argument.getName().isPresent()) {
            String name = argument.getName().get();
            if (names.put(name, argument) != null) {
                throw new SemanticException(INVALID_PROCEDURE_ARGUMENTS, argument, "Duplicate procedure argument: %s", name);
            }
            if (!positions.containsKey(name)) {
                throw new SemanticException(INVALID_PROCEDURE_ARGUMENTS, argument, "Unknown argument name: %s", name);
            }
        } else if (i < procedure.getArguments().size()) {
            names.put(procedure.getArguments().get(i).getName(), argument);
        } else {
            throw new SemanticException(INVALID_PROCEDURE_ARGUMENTS, call, "Too many arguments for procedure");
        }
    }
    procedure.getArguments().stream().filter(Argument::isRequired).filter(argument -> !names.containsKey(argument.getName())).map(Argument::getName).findFirst().ifPresent(argument -> {
        throw new SemanticException(INVALID_PROCEDURE_ARGUMENTS, call, format("Required procedure argument '%s' is missing", argument));
    });
    // get argument values
    Object[] values = new Object[procedure.getArguments().size()];
    for (Entry<String, CallArgument> entry : names.entrySet()) {
        CallArgument callArgument = entry.getValue();
        int index = positions.get(entry.getKey());
        Argument argument = procedure.getArguments().get(index);
        Expression expression = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(parameters), callArgument.getValue());
        Type type = metadata.getType(argument.getType());
        checkCondition(type != null, INVALID_PROCEDURE_DEFINITION, "Unknown procedure argument type: %s", argument.getType());
        Object value = evaluateConstantExpression(expression, type, metadata, session, parameters);
        values[index] = toTypeObjectValue(session, type, value);
    }
    // fill values with optional arguments defaults
    for (int i = 0; i < procedure.getArguments().size(); i++) {
        Argument argument = procedure.getArguments().get(i);
        if (!names.containsKey(argument.getName())) {
            verify(argument.isOptional());
            values[i] = toTypeObjectValue(session, metadata.getType(argument.getType()), argument.getDefaultValue());
        }
    }
    // validate arguments
    MethodType methodType = procedure.getMethodHandle().type();
    for (int i = 0; i < procedure.getArguments().size(); i++) {
        if ((values[i] == null) && methodType.parameterType(i).isPrimitive()) {
            String name = procedure.getArguments().get(i).getName();
            throw new PrestoException(INVALID_PROCEDURE_ARGUMENT, "Procedure argument cannot be null: " + name);
        }
    }
    // insert session argument
    List<Object> arguments = new ArrayList<>();
    Iterator<Object> valuesIterator = asList(values).iterator();
    for (Class<?> type : methodType.parameterList()) {
        if (ConnectorSession.class.isAssignableFrom(type)) {
            arguments.add(session.toConnectorSession(connectorId));
        } else {
            arguments.add(valuesIterator.next());
        }
    }
    try {
        procedure.getMethodHandle().invokeWithArguments(arguments);
    } catch (Throwable t) {
        if (t instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        throwIfInstanceOf(t, PrestoException.class);
        throw new PrestoException(PROCEDURE_CALL_FAILED, t);
    }
    return immediateFuture(null);
}
Also used : ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) WarningCollector(com.facebook.presto.spi.WarningCollector) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) HashMap(java.util.HashMap) PrestoException(com.facebook.presto.spi.PrestoException) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) MISSING_CATALOG(com.facebook.presto.sql.analyzer.SemanticErrorCode.MISSING_CATALOG) Verify.verify(com.google.common.base.Verify.verify) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) ExpressionInterpreter.evaluateConstantExpression(com.facebook.presto.sql.planner.ExpressionInterpreter.evaluateConstantExpression) INVALID_PROCEDURE_DEFINITION(com.facebook.presto.spi.StandardErrorCode.INVALID_PROCEDURE_DEFINITION) TransactionManager(com.facebook.presto.transaction.TransactionManager) Type(com.facebook.presto.common.type.Type) Argument(com.facebook.presto.spi.procedure.Procedure.Argument) Failures.checkCondition(com.facebook.presto.util.Failures.checkCondition) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) MetadataUtil.toSchemaTableName(com.facebook.presto.metadata.MetadataUtil.toSchemaTableName) Iterator(java.util.Iterator) ParameterRewriter(com.facebook.presto.sql.planner.ParameterRewriter) Predicate(java.util.function.Predicate) Session(com.facebook.presto.Session) TypeUtils.writeNativeValue(com.facebook.presto.common.type.TypeUtils.writeNativeValue) CallArgument(com.facebook.presto.sql.tree.CallArgument) Throwables.throwIfInstanceOf(com.google.common.base.Throwables.throwIfInstanceOf) String.format(java.lang.String.format) ConnectorSession(com.facebook.presto.spi.ConnectorSession) INVALID_PROCEDURE_ARGUMENT(com.facebook.presto.spi.StandardErrorCode.INVALID_PROCEDURE_ARGUMENT) Procedure(com.facebook.presto.spi.procedure.Procedure) List(java.util.List) MethodType(java.lang.invoke.MethodType) Expression(com.facebook.presto.sql.tree.Expression) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) Entry(java.util.Map.Entry) Call(com.facebook.presto.sql.tree.Call) ConnectorId(com.facebook.presto.spi.ConnectorId) PROCEDURE_CALL_FAILED(com.facebook.presto.spi.StandardErrorCode.PROCEDURE_CALL_FAILED) INVALID_PROCEDURE_ARGUMENTS(com.facebook.presto.sql.analyzer.SemanticErrorCode.INVALID_PROCEDURE_ARGUMENTS) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) CallArgument(com.facebook.presto.sql.tree.CallArgument) Argument(com.facebook.presto.spi.procedure.Procedure.Argument) CallArgument(com.facebook.presto.sql.tree.CallArgument) ParameterRewriter(com.facebook.presto.sql.planner.ParameterRewriter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) PrestoException(com.facebook.presto.spi.PrestoException) LinkedHashMap(java.util.LinkedHashMap) Procedure(com.facebook.presto.spi.procedure.Procedure) ConnectorId(com.facebook.presto.spi.ConnectorId) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) MethodType(java.lang.invoke.MethodType) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) Type(com.facebook.presto.common.type.Type) MethodType(java.lang.invoke.MethodType) ExpressionInterpreter.evaluateConstantExpression(com.facebook.presto.sql.planner.ExpressionInterpreter.evaluateConstantExpression) Expression(com.facebook.presto.sql.tree.Expression)

Example 88 with ConnectorId

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

the class AddColumnTask method execute.

@Override
public ListenableFuture<?> execute(AddColumn statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getName());
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    if (!tableHandle.isPresent()) {
        if (!statement.isTableExists()) {
            throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
        }
        return immediateFuture(null);
    }
    Optional<ConnectorMaterializedViewDefinition> optionalMaterializedView = metadata.getMaterializedView(session, tableName);
    if (optionalMaterializedView.isPresent()) {
        if (!statement.isTableExists()) {
            throw new SemanticException(NOT_SUPPORTED, statement, "'%s' is a materialized view, and add column is not supported", tableName);
        }
        return immediateFuture(null);
    }
    ConnectorId connectorId = metadata.getCatalogHandle(session, tableName.getCatalogName()).orElseThrow(() -> new PrestoException(NOT_FOUND, "Catalog does not exist: " + tableName.getCatalogName()));
    accessControl.checkCanAddColumns(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName);
    Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle.get());
    ColumnDefinition element = statement.getColumn();
    Type type;
    try {
        type = metadata.getType(parseTypeSignature(element.getType()));
    } catch (IllegalArgumentException e) {
        throw new SemanticException(TYPE_MISMATCH, element, "Unknown type '%s' for column '%s'", element.getType(), element.getName());
    }
    if (type.equals(UNKNOWN)) {
        throw new SemanticException(TYPE_MISMATCH, element, "Unknown type '%s' for column '%s'", element.getType(), element.getName());
    }
    if (columnHandles.containsKey(element.getName().getValue().toLowerCase(ENGLISH))) {
        if (!statement.isColumnNotExists()) {
            throw new SemanticException(COLUMN_ALREADY_EXISTS, statement, "Column '%s' already exists", element.getName());
        }
        return immediateFuture(null);
    }
    if (!element.isNullable() && !metadata.getConnectorCapabilities(session, connectorId).contains(NOT_NULL_COLUMN_CONSTRAINT)) {
        throw new SemanticException(NOT_SUPPORTED, element, "Catalog '%s' does not support NOT NULL for column '%s'", connectorId.getCatalogName(), element.getName());
    }
    Map<String, Expression> sqlProperties = mapFromProperties(element.getProperties());
    Map<String, Object> columnProperties = metadata.getColumnPropertyManager().getProperties(connectorId, tableName.getCatalogName(), sqlProperties, session, metadata, parameters);
    ColumnMetadata column = new ColumnMetadata(element.getName().getValue(), type, element.isNullable(), element.getComment().orElse(null), null, false, columnProperties);
    metadata.addColumn(session, tableHandle.get(), column);
    return immediateFuture(null);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition) PrestoException(com.facebook.presto.spi.PrestoException) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) ColumnDefinition(com.facebook.presto.sql.tree.ColumnDefinition) Type(com.facebook.presto.common.type.Type) Expression(com.facebook.presto.sql.tree.Expression) TableHandle(com.facebook.presto.spi.TableHandle) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) ConnectorId(com.facebook.presto.spi.ConnectorId)

Example 89 with ConnectorId

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

the class ResetSessionTask method execute.

@Override
public ListenableFuture<?> execute(ResetSession statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) {
    List<String> parts = statement.getName().getParts();
    if (parts.size() > 2) {
        throw new SemanticException(INVALID_SESSION_PROPERTY, statement, "Invalid session property '%s'", statement.getName());
    }
    // validate the property name
    if (parts.size() == 1) {
        metadata.getSessionPropertyManager().getSystemSessionPropertyMetadata(parts.get(0)).orElseThrow(() -> new SemanticException(INVALID_SESSION_PROPERTY, statement, "Session property %s does not exist", statement.getName()));
    } else {
        ConnectorId connectorId = metadata.getCatalogHandle(stateMachine.getSession(), parts.get(0)).orElseThrow(() -> new SemanticException(MISSING_CATALOG, statement, "Catalog %s does not exist", parts.get(0)));
        metadata.getSessionPropertyManager().getConnectorSessionPropertyMetadata(connectorId, parts.get(1)).orElseThrow(() -> new SemanticException(INVALID_SESSION_PROPERTY, statement, "Session property %s does not exist", statement.getName()));
    }
    stateMachine.addResetSessionProperties(statement.getName().toString());
    return immediateFuture(null);
}
Also used : SemanticException(com.facebook.presto.sql.analyzer.SemanticException) ConnectorId(com.facebook.presto.spi.ConnectorId)

Example 90 with ConnectorId

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

the class SetSessionTask method execute.

@Override
public ListenableFuture<?> execute(SetSession statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) {
    Session session = stateMachine.getSession();
    QualifiedName propertyName = statement.getName();
    List<String> parts = propertyName.getParts();
    if (parts.size() > 2) {
        throw new SemanticException(INVALID_SESSION_PROPERTY, statement, "Invalid session property '%s'", propertyName);
    }
    // validate the property name
    PropertyMetadata<?> propertyMetadata;
    if (parts.size() == 1) {
        accessControl.checkCanSetSystemSessionProperty(session.getIdentity(), session.getAccessControlContext(), parts.get(0));
        propertyMetadata = metadata.getSessionPropertyManager().getSystemSessionPropertyMetadata(parts.get(0)).orElseThrow(() -> new SemanticException(INVALID_SESSION_PROPERTY, statement, "Session property %s does not exist", statement.getName()));
    } else {
        ConnectorId connectorId = metadata.getCatalogHandle(stateMachine.getSession(), parts.get(0)).orElseThrow(() -> new SemanticException(MISSING_CATALOG, statement, "Catalog %s does not exist", parts.get(0)));
        accessControl.checkCanSetCatalogSessionProperty(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), parts.get(0), parts.get(1));
        propertyMetadata = metadata.getSessionPropertyManager().getConnectorSessionPropertyMetadata(connectorId, parts.get(1)).orElseThrow(() -> new SemanticException(INVALID_SESSION_PROPERTY, statement, "Session property %s does not exist", statement.getName()));
    }
    Type type = propertyMetadata.getSqlType();
    Object objectValue;
    try {
        objectValue = evaluatePropertyValue(statement.getValue(), type, session, metadata, parameters);
    } catch (SemanticException e) {
        throw new PrestoException(StandardErrorCode.INVALID_SESSION_PROPERTY, format("Unable to set session property '%s' to '%s': %s", propertyName, statement.getValue(), e.getMessage()));
    }
    String value = serializeSessionProperty(type, objectValue);
    // verify the SQL value can be decoded by the property
    propertyMetadata.decode(objectValue);
    stateMachine.addSetSessionProperties(propertyName.toString(), value);
    return immediateFuture(null);
}
Also used : Type(com.facebook.presto.common.type.Type) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) PrestoException(com.facebook.presto.spi.PrestoException) Session(com.facebook.presto.Session) SetSession(com.facebook.presto.sql.tree.SetSession) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) ConnectorId(com.facebook.presto.spi.ConnectorId)

Aggregations

ConnectorId (com.facebook.presto.spi.ConnectorId)162 ConnectorMetadata (com.facebook.presto.spi.connector.ConnectorMetadata)75 TableHandle (com.facebook.presto.spi.TableHandle)33 Test (org.testng.annotations.Test)29 ConnectorSession (com.facebook.presto.spi.ConnectorSession)26 ConnectorTableHandle (com.facebook.presto.spi.ConnectorTableHandle)18 PrestoException (com.facebook.presto.spi.PrestoException)18 ConnectorTransactionHandle (com.facebook.presto.spi.connector.ConnectorTransactionHandle)18 Session (com.facebook.presto.Session)16 ImmutableList (com.google.common.collect.ImmutableList)16 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)15 List (java.util.List)15 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)14 Map (java.util.Map)12 Optional (java.util.Optional)12 InMemoryNodeManager (com.facebook.presto.metadata.InMemoryNodeManager)11 ConnectorOutputTableHandle (com.facebook.presto.spi.ConnectorOutputTableHandle)11 SchemaTableName (com.facebook.presto.spi.SchemaTableName)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 ConnectorInsertTableHandle (com.facebook.presto.spi.ConnectorInsertTableHandle)10