Search in sources :

Example 21 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class SessionPropertyManager method decodeCatalogPropertyValue.

public <T> T decodeCatalogPropertyValue(CatalogName catalog, String catalogName, String propertyName, @Nullable String propertyValue, Class<T> type) {
    String fullPropertyName = catalogName + "." + propertyName;
    PropertyMetadata<?> property = getConnectorSessionPropertyMetadata(catalog, propertyName).orElseThrow(() -> new TrinoException(INVALID_SESSION_PROPERTY, "Unknown session property " + fullPropertyName));
    return decodePropertyValue(fullPropertyName, propertyValue, type, property);
}
Also used : TrinoException(io.trino.spi.TrinoException)

Example 22 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class PropertyUtil method evaluateProperties.

public static Map<String, Optional<Object>> evaluateProperties(Iterable<Property> setProperties, Session session, PlannerContext plannerContext, AccessControl accessControl, Map<NodeRef<Parameter>, Expression> parameters, boolean includeAllProperties, Map<String, PropertyMetadata<?>> metadata, ErrorCodeSupplier errorCode, String propertyTypeDescription) {
    Map<String, Optional<Object>> propertyValues = new LinkedHashMap<>();
    // Fill in user-specified properties
    for (Property property : setProperties) {
        // property names are case-insensitive and normalized to lower case
        String propertyName = property.getName().getValue().toLowerCase(ENGLISH);
        PropertyMetadata<?> propertyMetadata = metadata.get(propertyName);
        if (propertyMetadata == null) {
            throw new TrinoException(errorCode, format("%s '%s' does not exist", capitalize(propertyTypeDescription), propertyName));
        }
        Optional<Object> value;
        if (property.isSetToDefault()) {
            value = Optional.ofNullable(propertyMetadata.getDefaultValue());
        } else {
            value = Optional.of(evaluateProperty(property.getNonDefaultValue(), propertyMetadata, session, plannerContext, accessControl, parameters, errorCode, propertyTypeDescription));
        }
        propertyValues.put(propertyMetadata.getName(), value);
    }
    if (includeAllProperties) {
        for (PropertyMetadata<?> propertyMetadata : metadata.values()) {
            if (!propertyValues.containsKey(propertyMetadata.getName())) {
                propertyValues.put(propertyMetadata.getName(), Optional.ofNullable(propertyMetadata.getDefaultValue()));
            }
        }
    }
    return ImmutableMap.copyOf(propertyValues);
}
Also used : Optional(java.util.Optional) TrinoException(io.trino.spi.TrinoException) Property(io.trino.sql.tree.Property) LinkedHashMap(java.util.LinkedHashMap)

Example 23 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class PropertyUtil method evaluateProperty.

private static Object evaluateProperty(Expression expression, PropertyMetadata<?> property, Session session, PlannerContext plannerContext, AccessControl accessControl, Map<NodeRef<Parameter>, Expression> parameters, ErrorCodeSupplier errorCode, String propertyTypeDescription) {
    Object sqlObjectValue;
    try {
        Type expectedType = property.getSqlType();
        Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(parameters), expression);
        Object value = evaluateConstantExpression(rewritten, expectedType, plannerContext, session, accessControl, parameters);
        // convert to object value type of SQL type
        BlockBuilder blockBuilder = expectedType.createBlockBuilder(null, 1);
        writeNativeValue(expectedType, blockBuilder, value);
        sqlObjectValue = expectedType.getObjectValue(session.toConnectorSession(), blockBuilder, 0);
    } catch (TrinoException e) {
        throw new TrinoException(errorCode, format("Invalid value for %s '%s': Cannot convert [%s] to %s", propertyTypeDescription, property.getName(), expression, property.getSqlType()), e);
    }
    if (sqlObjectValue == null) {
        throw new TrinoException(errorCode, format("Invalid null value for %s '%s' from [%s]", propertyTypeDescription, property.getName(), expression));
    }
    try {
        return property.decode(sqlObjectValue);
    } catch (Exception e) {
        throw new TrinoException(errorCode, format("Unable to set %s '%s' to [%s]: %s", propertyTypeDescription, property.getName(), expression, e.getMessage()), e);
    }
}
Also used : Type(io.trino.spi.type.Type) ParameterRewriter(io.trino.sql.planner.ParameterRewriter) ExpressionInterpreter.evaluateConstantExpression(io.trino.sql.planner.ExpressionInterpreter.evaluateConstantExpression) Expression(io.trino.sql.tree.Expression) TrinoException(io.trino.spi.TrinoException) TrinoException(io.trino.spi.TrinoException) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 24 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class TableProceduresRegistry method resolve.

public TableProcedureMetadata resolve(CatalogName catalogName, String name) {
    Map<String, TableProcedureMetadata> procedures = tableProcedures.get(catalogName);
    if (procedures == null) {
        throw new TrinoException(GENERIC_INTERNAL_ERROR, format("Catalog %s not registered", catalogName));
    }
    TableProcedureMetadata procedure = procedures.get(name);
    if (procedure == null) {
        throw new TrinoException(PROCEDURE_NOT_FOUND, format("Procedure %s not registered for catalog %s", name, catalogName));
    }
    return procedure;
}
Also used : TableProcedureMetadata(io.trino.spi.connector.TableProcedureMetadata) TrinoException(io.trino.spi.TrinoException)

Example 25 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class ParametricAggregation method findMatchingImplementation.

private AggregationImplementation findMatchingImplementation(BoundSignature boundSignature) {
    Signature signature = boundSignature.toSignature();
    Optional<AggregationImplementation> foundImplementation = Optional.empty();
    if (implementations.getExactImplementations().containsKey(signature)) {
        foundImplementation = Optional.of(implementations.getExactImplementations().get(signature));
    } else {
        for (AggregationImplementation candidate : implementations.getGenericImplementations()) {
            if (candidate.areTypesAssignable(boundSignature)) {
                if (foundImplementation.isPresent()) {
                    throw new TrinoException(AMBIGUOUS_FUNCTION_CALL, format("Ambiguous function call (%s) for %s", boundSignature, getFunctionMetadata().getSignature()));
                }
                foundImplementation = Optional.of(candidate);
            }
        }
    }
    if (foundImplementation.isEmpty()) {
        throw new TrinoException(FUNCTION_IMPLEMENTATION_MISSING, format("Unsupported type parameters (%s) for %s", boundSignature, getFunctionMetadata().getSignature()));
    }
    return foundImplementation.get();
}
Also used : Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) TrinoException(io.trino.spi.TrinoException)

Aggregations

TrinoException (io.trino.spi.TrinoException)623 IOException (java.io.IOException)151 ImmutableList (com.google.common.collect.ImmutableList)105 List (java.util.List)100 Type (io.trino.spi.type.Type)93 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)90 SchemaTableName (io.trino.spi.connector.SchemaTableName)83 Path (org.apache.hadoop.fs.Path)83 Optional (java.util.Optional)79 ArrayList (java.util.ArrayList)77 Map (java.util.Map)76 ImmutableMap (com.google.common.collect.ImmutableMap)70 Objects.requireNonNull (java.util.Objects.requireNonNull)69 ConnectorSession (io.trino.spi.connector.ConnectorSession)63 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)62 ImmutableSet (com.google.common.collect.ImmutableSet)56 VarcharType (io.trino.spi.type.VarcharType)54 Set (java.util.Set)54 Slice (io.airlift.slice.Slice)53 Table (io.trino.plugin.hive.metastore.Table)52